modelicadymolafmi

Modelica model and its fmu give different results for the same input


I have a simple motor model in Modelica which gives expected velocity output when run in Dymola for constant voltage. But when I export it as a co-simulation fmu (with Dymola's dassl solver) it gives different answer in other environments like fmpy and Simulink.

model motor
  Modelica.Blocks.Interfaces.RealInput vol "voltage" annotation (Placement(
        transformation(extent={{-140,-20},{-100,20}})));
  Modelica.Blocks.Interfaces.RealOutput v[1] "velocity" annotation (Placement(
        transformation(extent={{100,-10},{120,10}})));
  Real x[2](start={0,0}) "stateVector";
  parameter Real A[:,:]=[1.511, -0.5488;1, 0];
  parameter Real B[:] = {0.0625,0};
  parameter Real C[:,:] = [0.03294,0.02697];
  parameter Modelica.SIunits.Time dT = 0.1 "samplingTime";
equation 
  x = A*delay(x, dT) + B*vol;
  v = C*x;
  annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
        coordinateSystem(preserveAspectRatio=false)));
end motor;

This difference is seen only when the input (here vol) is a constant input. With step or ramp input the Modelica model and fmu give same answer. Therefore I thought this might be related to the start value of vol in Dymola vs fmu . Therefore I added start value on the input port but that didn't help.

  Modelica.Blocks.Interfaces.RealInput vol( start= 110) "voltage" annotation (Placement(
    transformation(extent={{-140,-20},{-100,20}})));

For example, the Modelica test give me v = 0.2264

model example
  motor motor1 annotation (Placement(transformation(extent={{-20,0},{0,20}})));
  Modelica.Blocks.Sources.Constant const(k=110)
    annotation (Placement(transformation(extent={{-60,0},{-40,20}})));
equation 
  connect(const.y, motor1.vol)
    annotation (Line(points={{-39,10},{-22,10}}, color={0,0,127}));
  annotation (
    Icon(coordinateSystem(preserveAspectRatio=false)),
    Diagram(coordinateSystem(preserveAspectRatio=false)));
end example;

The motor.fmu (exported from motor.mo) run in fmpy gives v = 10.8963

import fmpy as fm
import pandas as pd
import numpy as np

filename = 'motor.fmu'
input = np.array([(   0, 110), (30, 110)],
      dtype=[('time', '<i4'), ('vol', '<i4')])
output = ['v[1]',]

result = fm.simulate_fmu(filename, input = input, output=output, stop_time=30.0)
df = pd.DataFrame(result)
print(df.tail())

Has anyone seen similar behavior?


Solution

  • I think the root of the issue is the implementation of the state-space equations. The posted equations use a delay operator within a continuous system. As far as I understand, this is not correct.

    The continuous version should be

      der(x) = A*x + B*vol;
      v = C*x;
    

    or the discrete should be

      when sampleTrigger then
        x = A*pre(x) + B*u;
        y = C*pre(x) + D*u;
      end when;
    

    The full code can be looked up in Modelica.Blocks.Continuous.StateSpace and Modelica.Blocks.Discrete.StateSpace respectively.

    I'm not entirely sure, what the origin of the different result exactly is (and they are different, I can confirm). But the question is, whether that is worth investigating, if the equations itself are likely not what they should be...