英文:
How to access model jacobian from FMU or Dymola without analytical jacobian
问题
I am trying to find a way to access the jacobian for a model in dymola either through a compiled FMU or from the exported Dymola source code.
我正在尝试找到一种通过编译后的FMU或从导出的Dymola源代码来访问模型雅可比矩阵的方法。
The final objective is to use the same procedure to access the jacobian for a much more complex multibody vehicle model (205 states).
最终目标是使用相同的方法来访问更复杂的多体车辆模型(205个状态)的雅可比矩阵。
Using fmi2GetDirectionalDerivative()
from the FMI Standard seemed promising so I made a simple linear vehicle model to test this.
使用FMI标准中的fmi2GetDirectionalDerivative()
看起来很有前途,所以我制作了一个简单的线性车辆模型来测试它。
This model has:
这个模型有:
- states -
v
andr
- 状态 -v
和r
- inputs -
delta
- 输入 -delta
- outputs -
ay
- 输出 -ay
For this test,
在这个测试中,
-
delta=amp*sin(2*Modelica.Constants.pi*freq*time)
with -delta=amp*sin(2*Modelica.Constants.pi*freq*time)
, -
amp = 1*Modelica.Constants.pi/180
-amp = 1*Modelica.Constants.pi/180
-
freq = 0.5
-freq = 0.5
-
Version: Dymola 2020x - 版本:Dymola 2020x
-
Solver: RKFIX2 - 求解器:RKFIX2
-
Timestep: 0.01s - 时间步长:0.01秒
-
Co-simulation FMU - 协同仿真FMU
Since this is a linear model, the jacobian should be a constant value throughout the simulation. For this model, when I set the flag Advanced.GenerateAnalyticJacobian = true
, I get the following values for the model jacobian computed from fmi2GetDirectionalDerivative()
for all combinations of knowns and unknowns. In all cases, dvKnown = 1
for the function.
由于这是一个线性模型,雅可比矩阵在整个仿真过程中应该是一个恒定的值。对于这个模型,当我将标志Advanced.GenerateAnalyticJacobian = true
设置为true时,我从所有已知和未知组合中计算的模型雅可比矩阵的值如下所示。在所有情况下,函数的dvKnown = 1
。
These values are correct based on the state space equation:
这些值基于状态空间方程式是正确的:
+--------------+----------+
| Derivative | Value |
+--------------+----------+
| der(v)/delta | 200 |
+--------------+----------+
| ay/delta | 200 |
+--------------+----------+
| der(r)/delta | 300 |
+--------------+----------+
| der(v)/v | -33.3333 |
+--------------+----------+
| ay/v | -33.3333 |
+--------------+----------+
| der(r)/v | -20 |
+--------------+----------+
| der(v)/r | -36.6667 |
+--------------+----------+
| ay/r | -26.6667 |
+--------------+----------+
| der(r)/r | -70 |
+--------------+----------+
However, if I set the flag Advanced.GenerateAnalyticJacobian = false
, I get completely junk values below:
然而,如果我将标志Advanced.GenerateAnalyticJacobian = false
设置为false,我会得到完全无用的值如下:
+--------------+-----------+
| Derivative | Value |
+--------------+-----------+
| der(v)/delta | -1.57E+11 |
+--------------+-----------+
| ay/delta | -1.57E+11 |
+--------------+-----------+
| der(r)/delta | 1.52942 |
+--------------+-----------+
| der(v)/v | -9.12E+08 |
+--------------+-----------+
| ay/v | -9.12E+08 |
+--------------+-----------+
| der(r)/v | 14999.8 |
+--------------+-----------+
| der(v)/r | 5.47E+11 |
+--------------+-----------+
| ay/r | 5.47E+11 |
+--------------+-----------+
| der(r)/r | -2.25E+07 |
+--------------+-----------+
I expect the value to be different from the analytical value since its numerically calculated but I don't get why its completely wrong.
我期望这个值与分析值不同,因为它是通过数值计算得出的,但我不明白为什么它完全错误。
I tried enabling some other flags (Advanced.AllowNumericDifferentiation
, Advanced.AutomaticDifferentiation
) and changing the solver to CVODE, DASSL etc. but the values remain incorrect.
我尝试启用了一些其他标志(Advanced.AllowNumericDifferentiation
,Advanced.AutomaticDifferentiation
)并将求解器更改为CVODE、DASSL等,但值仍然不正确。
Unfortunately, Dymola can't calculate an analytical jacobian for the large model so I can't use that option. All the literature I read point to fmi2GetDirectionalDerivative()
.
不幸的是,Dymola不能为大型模型计算解析雅可比矩阵,因此我不能使用该选项。我阅读的所有文献都指向fmi2GetDirectionalDerivative()
。
I would appreciate any inputs on how to get the model jacobian out of the FMU.
我将感激任何有关如何从FMU中获取模型雅可比矩阵的建议。
If there are other methods that can be used through Dymola, that would also work since we have a source code export license.
如果有其他可以通过Dymola使用的方法,那也可以,因为我们有源代码导出许可证。
英文:
I am trying to find a way to access the jacobian for a model in dymola either through a compiled FMU or from the exported Dymola source code.
The final objective is to use the same procedure to access the jacobian for a much more complex multibody vehicle model (205 states).
Using fmi2GetDirectionalDerivative()
from the FMI Standard seemed promising so I made a simple linear vehicle model to test this.
model Vehicle "Single-track Linear bicycle vehicle model"
extends Modelica.Blocks.Icons.Block;
import SI = Modelica.SIunits;
import MB = Modelica.Mechanics.MultiBody;
// model parameters
parameter SI.Velocity u = 10 "forward velocity";
parameter SI.Inertia Iz = 2000 "yaw moment of inertia";
parameter SI.Length L = 3 "wheel base";
parameter SI.Mass Mf = 900 "front axle mass";
parameter SI.Mass Mr = 600 "rear axle mass";
parameter Real Cf(unit="N/rad") = 300000 "front axle cornering stiffness";
parameter Real Cr(unit="N/rad") = 200000 "rear axle cornering stiffness";
// calculated parameters
final parameter SI.Mass M = Mf + Mr "mass";
final parameter SI.Length a = Mr/Mf*L "CG position front";
final parameter SI.Length b = L - a "CG position front";
input SI.Angle delta "steering angle" annotation(Dialog(group="Inputs"));
public
SI.Velocity v "lateral velocity";
output SI.Acceleration ay "lateral acceleration";
SI.AngularVelocity r "yaw rate";
equation
ay = der(v) + u*r;
M*(der(v) + u*r) = Cf*(delta-(v+a*r)/u) + Cr*(-(v-b*r)/u);
Iz*der(r) = a*Cf*(delta-(v+a*r)/u) - b*Cr*(-(v-b*r)/u);
end Vehicle;
This model has:
- states -
v
andr
- inputs -
delta
- outputs -
ay
For this test,
-
delta=amp*sin(2*Modelica.Constants.pi*freq*time)
with -
amp = 1*Modelica.Constants.pi/180
-
freq = 0.5
-
Version: Dymola 2020x
-
Solver: RKFIX2
-
Timestep: 0.01s
-
Co-simulation FMU
Since this is a linear model, the jacobian should be a constant value throughout the simulation. For this model, when I set the flag Advanced.GenerateAnalyticJacobian = true
, I get the following values for the model jacobian computed from fmi2GetDirectionalDerivative()
for all combinations of knowns and unknowns. In all cases, dvKnown = 1
for the function.
These values are correct based on the state space equation:
+--------------+----------+
| Derivative | Value |
+--------------+----------+
| der(v)/delta | 200 |
+--------------+----------+
| ay/delta | 200 |
+--------------+----------+
| der(r)/delta | 300 |
+--------------+----------+
| der(v)/v | -33.3333 |
+--------------+----------+
| ay/v | -33.3333 |
+--------------+----------+
| der(r)/v | -20 |
+--------------+----------+
| der(v)/r | -36.6667 |
+--------------+----------+
| ay/r | -26.6667 |
+--------------+----------+
| der(r)/r | -70 |
+--------------+----------+
However, if I set the flag Advanced.GenerateAnalyticJacobian = false
, I get completely junk values below:
+--------------+-----------+
| Derivative | Value |
+--------------+-----------+
| der(v)/delta | -1.57E+11 |
+--------------+-----------+
| ay/delta | -1.57E+11 |
+--------------+-----------+
| der(r)/delta | 1.52942 |
+--------------+-----------+
| der(v)/v | -9.12E+08 |
+--------------+-----------+
| ay/v | -9.12E+08 |
+--------------+-----------+
| der(r)/v | 14999.8 |
+--------------+-----------+
| der(v)/r | 5.47E+11 |
+--------------+-----------+
| ay/r | 5.47E+11 |
+--------------+-----------+
| der(r)/r | -2.25E+07 |
+--------------+-----------+
I expect the value to be different from the analytical value since its numerically calculated but I don't get why its completely wrong.
I tried enabling some other flags (Advanced.AllowNumericDifferentiation
, Advanced.AutomaticDifferentiation
) and changing the solver to CVODE, DASSL etc. but the values remain incorrect.
Unfortunately, Dymola can't calculate an analytical jacobian for the large model so I can't use that option. All the literature I read point to fmi2GetDirectionalDerivative()
.
I would appreciate any inputs on how to get the model jacobian out of the FMU.
If there are other methods that can be used through Dymola, that would also work since we have a source code export license.
答案1
得分: 0
以下是您要翻译的内容:
无法评论,所以这里是一个不是答案的答案:
结果并不完全是垃圾:对于第一个表中相同的值(例如,前两行的值为200),您在第二个表中得到相同的值(-1.57E+11)。一个例外是der(v)/r和ay/r,它们在第二个表中是相同的,但也许是因为这些值被截断了。
请要求Dymola检查他们对fmi2GetDirectionalDerivative()
与Advanced.GenerateAnalyticJacobian = false
组合的实现。
英文:
Cannot comment, so here is an answer that is not an answer:
The results are not complete junk: For identical values in the first table (e.g. 200 for the first two rows) you get identical values in the second table (-1.57E+11). An exception is der(v)/r and ay/r, which are identical in the second table, but maybe that is because the values are truncated.
Ask Dymola to check their implementation of fmi2GetDirectionalDerivative()
in combination with Advanced.GenerateAnalyticJacobian = false
.
答案2
得分: 0
以下是翻译好的内容:
为了以后参考,我已经部分解决了这个问题。
当我导出一个模型交换 FMU 时,我获得了数值雅可比的有意义的值(非常接近解析值)。
我猜想这可能是 Dymola 的共模拟实现中关于数值雅可比的某种错误。
英文:
For future reference, I was able to partially solve this issue.
I get meaningful values for the numerical jacobian (very close to the analytical values) when I export a Model Exchange FMU.
I am guessing this is some kind of bug in Dymola's Co-Simulation implementation w.r.t numerical jacobians.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论