无效的 Modelica 模型在另一个模型中实例化时工作正常。为什么?
Invalid Modelica model works fine when instantiated in another model. Why?
我有 2 个 Modelica 模型:
model parent
child dingus;
equation
dingus.a = 37;
dingus.c = 666;
end parent;
和
model child
Real a;
Real b;
Real c;
equation
a^3 + b^3 + c^3 = 1;
end child;
当我检查代码时,parent
检查正常,但 child
给出了关于未知数和方程不匹配的错误,正如预期的那样。为什么它在 parent
中有效,是否有更清晰的最佳实践方法来实现它?我想在多个不同的 parents 中使用 child,并且我可能想更改与它的交互方式,因此我不想过度定义它。
嗯,很明显 child
是不平衡的,因为正如您所指出的,它没有与未知数相同数量的方程。 parent
模型是可以的,因为它确实具有与未知数相同数量的方程。它从 child
中得到一个方程式,并提供自己的两个方程式。由于正好有三个变量child.a
、child.b
和child.c
,所以整个事情是平衡的。
但更大的问题(我认为这就是您想要解决的问题)是如何避免 child
看起来像 "bad" 模型的问题。一个简单的解决方案是:
model child
input Real a;
output Real b;
input Real c;
equation
a^3 + b^3 + c^3 = 1;
end child;
这表明 a
和 c
的值将来自 "outside" 某处。
预测你的下一个问题..."But what happens in cases when I want to specify b
and c
and solve for a
? Surely I don't have to write another model with the same equations and variables but with the input
and output
qualifiers on different variables?"
您可以毫无问题地使用上述模型。原因是 input
和 output
限定符仅限制事物如何 连接 。我们实际上没有建立任何联系,所以我们没事。因此,您可以为 output
提供方程并求解 input
。虽然这看起来有点违反直觉,但这在 Modelica 中是完全合法的。 input
和 output
在这里有用的原因是它们隐含地指定了我们期望这个模型本身提供多少方程(输出)以及我们期望来自外部的方程(输入)。但他们不会(也不能)限制我们为哪些变量提供显式方程。
最后一点,我建议您在声明中添加 child
实例的方程式。我似乎记得在确定某物是否平衡时,Modelica 中有一些特殊规则来处理这个问题。据我所知,解决这个问题的正确方法是:
model child
input Real a;
input Real b;
output Real c; // A bit arbitrary which one is output
equation
a^3 + b^3 + c^3 = 1;
end child;
model parent
child dingus(a=37, c=666);
end parent;
通过这种方式,您可以将 child
视为相当于 "named equation"。更好的是,您可以将其设置为 replaceable
,这样您就可以将其交换给其他人。这个的典型应用是用一个状态方程代替另一个状态方程,或者用一个平衡条件代替另一个。
希望对您有所帮助。
我有 2 个 Modelica 模型:
model parent
child dingus;
equation
dingus.a = 37;
dingus.c = 666;
end parent;
和
model child
Real a;
Real b;
Real c;
equation
a^3 + b^3 + c^3 = 1;
end child;
当我检查代码时,parent
检查正常,但 child
给出了关于未知数和方程不匹配的错误,正如预期的那样。为什么它在 parent
中有效,是否有更清晰的最佳实践方法来实现它?我想在多个不同的 parents 中使用 child,并且我可能想更改与它的交互方式,因此我不想过度定义它。
嗯,很明显 child
是不平衡的,因为正如您所指出的,它没有与未知数相同数量的方程。 parent
模型是可以的,因为它确实具有与未知数相同数量的方程。它从 child
中得到一个方程式,并提供自己的两个方程式。由于正好有三个变量child.a
、child.b
和child.c
,所以整个事情是平衡的。
但更大的问题(我认为这就是您想要解决的问题)是如何避免 child
看起来像 "bad" 模型的问题。一个简单的解决方案是:
model child
input Real a;
output Real b;
input Real c;
equation
a^3 + b^3 + c^3 = 1;
end child;
这表明 a
和 c
的值将来自 "outside" 某处。
预测你的下一个问题..."But what happens in cases when I want to specify b
and c
and solve for a
? Surely I don't have to write another model with the same equations and variables but with the input
and output
qualifiers on different variables?"
您可以毫无问题地使用上述模型。原因是 input
和 output
限定符仅限制事物如何 连接 。我们实际上没有建立任何联系,所以我们没事。因此,您可以为 output
提供方程并求解 input
。虽然这看起来有点违反直觉,但这在 Modelica 中是完全合法的。 input
和 output
在这里有用的原因是它们隐含地指定了我们期望这个模型本身提供多少方程(输出)以及我们期望来自外部的方程(输入)。但他们不会(也不能)限制我们为哪些变量提供显式方程。
最后一点,我建议您在声明中添加 child
实例的方程式。我似乎记得在确定某物是否平衡时,Modelica 中有一些特殊规则来处理这个问题。据我所知,解决这个问题的正确方法是:
model child
input Real a;
input Real b;
output Real c; // A bit arbitrary which one is output
equation
a^3 + b^3 + c^3 = 1;
end child;
model parent
child dingus(a=37, c=666);
end parent;
通过这种方式,您可以将 child
视为相当于 "named equation"。更好的是,您可以将其设置为 replaceable
,这样您就可以将其交换给其他人。这个的典型应用是用一个状态方程代替另一个状态方程,或者用一个平衡条件代替另一个。
希望对您有所帮助。