我正在考虑一个在 Modelica 语言中很不错的功能(我使用的是 OpenModelica 1.14.1)。
该功能将自动添加一个表示所需值总和的方程。
表示总和的值将在global
模型顶层实例化的组件中声明。
较低级别(可能是嵌套的)组件的每个贡献都将由一个connect
语句表示。
为此,可以使用connector
带有flow
变量的类,因为它会生成正确的方程。关键字可以使这成为可能inner
。outer
我尝试通过以下方式编写一个示例:
package global_sum_test
connector X_sum
flow Real x;
end X_sum;
model Global
X_sum x_sum;
Real x = x_sum.x "Variable representing the sum";
end Global;
model Local
parameter Real x=1 "Local contribution to the sum";
outer Global global "Reference to the Global instance";
X_sum x_sum;
equation
x_sum.x = -x "negative sign makes x flowing flom local into global";
connect(x_sum, global.x_sum) "Pushing local x into global sum";
end Local;
model Local_nested
parameter Real x=1 "Local contribution to the sum";
outer Global global "Reference to the global";
X_sum x_sum;
Local local "Component within another component to test nesting";
equation
x_sum.x = -x "Negative sign makes x flowing flom local into global";
connect(global.x_sum, x_sum) "Pushing local x into global sum";
end Local_nested;
model test_model
inner Global global "Instance of Global that is available to lower-level components";
Local local1 "Instance of Local";
Local_nested local2 "Instance of Local_nested with one more Local in it";
end test_model;
end global_sum_test;
不幸的是,这不起作用。当我实例化test_model
它时,它会给出以下输出(我已经适当地评论过):
class global_sum_test.test_model
Real global.x_sum.x;
Real global.x = global.x_sum.x "Variable representing the sum";
parameter Real local1.x = 1.0 "Local contribution to the sum";
Real local1.x_sum.x;
parameter Real local2.x = 1.0 "Local contribution to the sum";
Real local2.x_sum.x;
parameter Real local2.local.x = 1.0 "Local contribution to the sum";
Real local2.local.x_sum.x;
equation
local1.x_sum.x = -local1.x "negative sign makes x flowing flom local into global";
local2.local.x_sum.x = -local2.local.x "negative sign makes x flowing flom local into global";
local2.x_sum.x = -local2.x "Negative sign makes x flowing flom local into global";
global.x_sum.x + (-local1.x_sum.x) + (-local2.x_sum.x) + (-local2.local.x_sum.x) = 0.0; // <- this is correct
local1.x_sum.x = 0.0; // <- this line should not be generated and makes system over-determined.
local2.x_sum.x = 0.0; // <- this line should not be generated and makes system over-determined.
local2.local.x_sum.x = 0.0; // <- this line should not be generated and makes system over-determined.
end global_sum_test.test_model;
最后生成的三行导致系统超定。如果不是这三行,它将完全按照我的需要工作。
问题:
这是预期的行为吗?额外的行是使用开放的 Modelica 编译器造成的吗?
有没有办法改进我的代码,不会生成另外三行?
任何人都可以global_sum_test.test_model
在 Dymola 中对此进行测试,以确保此问题不仅仅针对 OpenModelica 吗?
还有其他方法可以自动获取总和吗?