Modelica模型及其fmu对于相同的输入给出不同的结果
我在 Modelica 中有一个简单的电机模型,它在 Dymola 中以恒定电压运行时给出了预期的速度输出。但是当我将它导出为联合仿真 fmu(使用 Dymola 的 dassl 求解器)时,它在 fmpy 和 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;
只有当输入(此处为 vol)是恒定输入时,才能看到这种差异。对于阶跃或斜坡输入,Modelica 模型和 fmu 给出相同的答案。因此我认为这可能与 Dymola vs fmu 中 vol 的起始值有关。因此,我在输入端口上添加了起始值,但这没有帮助。
Modelica.Blocks.Interfaces.RealInput vol( start= 110) "voltage" annotation (Placement(
transformation(extent={{-140,-20},{-100,20}})));
例如,Modelica 测试给我 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;
在 fmpy 中运行的 motor.fmu(从 motor.mo 导出)给出 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())
有没有人见过类似的行为?
回答
我认为问题的根源在于状态空间方程的实现。发布的方程delay在连续系统中使用运算符。据我了解,这是不正确的。
连续版本应该是
der(x) = A*x + B*vol;
v = C*x;
或者离散应该是
when sampleTrigger then
x = A*pre(x) + B*u;
y = C*pre(x) + D*u;
end when;
完整的代码可以分别在Modelica.Blocks.Continuous.StateSpace和 中查找Modelica.Blocks.Discrete.StateSpace。
我不完全确定,不同结果的确切来源是什么(它们是不同的,我可以确认)。但问题是,这是否值得研究,如果方程本身可能不是它们应该的样子……