首页 > 解决方案 > Error using the library Modelica_LinearSystems2 in OpenModelica

问题描述

I am trying to use the Kalman Filter from the Modelica_LinearSystem2 Library (Modelica_LinearSystems2.WorkInProgress.Controller.KalmanFilter.KF) in OpenModelica, but it seems the functions of the library are not working properly in my test models.

To find the problem I copied the code example from the documentation (https://build.openmodelica.org/Documentation/Modelica_LinearSystems2.StateSpace.%27constructor%27.fromABCDMatrices.html)

model test3

  Real A[1,1] = [1];
  Real B[1,1] = [1];
  Real C[1,1] = [1];
  Real D[1,1] = [0];

public
  Modelica_LinearSystems2.StateSpace ss;

algorithm
  ss := Modelica_LinearSystems2.StateSpace.'constructor'.fromABCDMatrices(A, B, C, D);


equation

end test3; 

When I click on check model I receive the Error:

[Modelica_LinearSystems2.StateSpace: 7:3-8:68]: Failed to deduce dimension 1 of A due to missing binding equation.

This refers to the line

Real A[:,size(A, 1)];

When I predefine this (and other) matrices with for example

Real A[4,4];

I get the error

Internal error Instantiation of test3 failed with no error message.

My question is: Why is this and how can I prevent these errors?

标签: modelicaopenmodelica

解决方案


发现

看起来 Modelica_LinearSystems2 仅受 Dymola 支持。他们的github 存储库的登录页面指出:

请注意,已知该库仅适用于 Dymola。

看起来情况仍然如此。至少在我的机器上,Modelica_LinearSystems2 v2.4.1 库在 OpenModelica v1.18.0 中存在严重问题。大多数示例都会出现错误或什么也不做。

不过,问题中的代码在 Dymola 中不起作用。您可以在下面找到解释和更正示例,这些示例已通过 Dymola 成功测试。所有工具的基本问题应该是相同的,我希望我的解决方案在支持 Modelica_LinearSystems2 库后也能在 OpenModelica 中工作。

原始答案(与 Dymola 用户最相关)

您的示例代码的问题是 Modelica 工具在执行模拟时必须知道向量、矩阵和数组的大小,而不是在调用函数时。由于您正在构建模型,因此该工具假定您要对其进行仿真

您的代码实例化StateSpace记录ssss持有矩阵A,B和. 只要你不给 分配任何东西,它们的大小是未知的。当然有一个算法,它设置,但这发生在模拟过程中。在翻译过程中,无法确定矩阵的大小。因此,典型的 Modelica 工具要求您使用绑定方程。CDssssss

为了使您的剪辑工作,您可以将其更改为:

model Demo
  Real A[1,1] = [1];
  Real B[1,1] = [1];
  Real C[1,1] = [1];
  Real D[1,1] = [0];
  Modelica_LinearSystems2.StateSpace ss = Modelica_LinearSystems2.StateSpace.'constructor'.fromABCDMatrices(A, B, C, D);
end Demo;

请注意,ss现在有一个绑定方程。ss因此,可以确定内部矩阵的大小。

问题是,如果你真的想用你的StateSpace记录运行模拟。通常,线性系统库中的函数用于 Modelica 函数。在这种情况下,您的代码可能如下所示:

function demo
  output Modelica_LinearSystems2.StateSpace ss;
protected
  Real A[1,1] = [1];
  Real B[1,1] = [1];
  Real C[1,1] = [1];
  Real D[1,1] = [0];
algorithm
  ss = Modelica_LinearSystems2.StateSpace.'constructor'.fromABCDMatrices(A, B, C, D);
end demo;

我建议查看包中的各种示例以Modelica_LinearSystems2.Examples.StateSpace正确使用StateSpace记录。


推荐阅读