如何定义参数之间的关系并保持灵活使用?
How do I define relations between parameters and retain flexible use?
如何定义多个参数之间的关系,以便我可以从集合中选择要定义的参数,一些方程式定义其余参数?
例如,我有一个包含 3 个参数的模型,分别是圆柱体的半径、高度和体积,它们通过体积方程 V=r^2*h
相关联。
如果我定义 r
和 h
(并且计算 V
),我希望能够选择模型实例化,r
,V
(和 h
被计算),或 h
、V
(并且 r
被计算)。
在下面的最小示例中,我尝试了方法 1-3,但 none 满足了我的需要。我感觉这一定是一个 common/solved 问题,我只是缺少某种建模技术。你能帮忙吗?
model test_params "Model for a cylinder"
model Cylinder
parameter Real r;
parameter Real h;
parameter Real V;
//Approach 3: binding equation. Works, but what to do if I have V, h and want to know r??
//parameter Real V=r^2*Modelica.Constants.pi*h;
Real t;
initial equation
//Approach 2, apparently chooses V as 0 before getting to the initialisation equation
//V=r^2*Modelica.Constants.pi*h;
//Parameter cylinder.V has no value, and is fixed during initialization (fixed=true), using available start value (start=0.0) as default value.
// The initial conditions are over specified. The following 1 initial equations are redundant, so they are removed from the initialization sytem:
// cylinder.V = 3.141592653589793 * cylinder.r ^ 2.0 * cylinder.h.
equation
t = V;
// Approach 1
//V=r^2*Modelica.Constants.pi*h; //Leads to: Too many equations, over-determined system.
end Cylinder;
test_params.Cylinder cylinder(h = 1, r = 2) annotation(
Placement(visible = true, transformation(origin = {-44, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
end test_params;
这是在 OpenModelica 1.14 上,如果相关的话。
如果您将依赖参数设置为 fixed=false 参数,则可以使用您提到的方法 2 来完成此操作。
model TestParams "Model for a cylinder"
encapsulated model Cylinder
import Modelica.Constants.pi;
parameter Real r;
parameter Real h;
parameter Real V;
initial equation
V = r^2*pi*h "Cylinder volume";
end Cylinder;
TestParams.Cylinder cylinder_1(h=1, r=2, V(fixed=false)) "Compute cylinder V(h, r)"
annotation (Placement(transformation(origin={-70,50}, extent={{-10,-10},{10,10}})));
TestParams.Cylinder cylinder_2(h(start=0, fixed=false), r=2, V=13) "Compute cylinder h(r, V)"
annotation (Placement(transformation(origin={-30,50}, extent={{-10,-10},{10,10}})));
TestParams.Cylinder cylinder_3(h=1, r(start=0, fixed=false), V=13) "Compute cylinder r(h, V)"
annotation (Placement(transformation(origin={10,50}, extent={{-10,-10},{10,10}})));
end TestParams;
我建议,为了有一个图形选择,类似于:
package test_params2 "Model for a cylinder"
type InputChoice = enumeration(rh,rv,hv);
model Cylinder
parameter InputChoice choice;
parameter Real r;
parameter Real h;
parameter Real v;
Real R,H,V;
equation
if choice==InputChoice.rh then
R=r;
H=h;
elseif choice==InputChoice.rv then
R=r;
V=v;
else
H=h;
V=v;
end if;
V=R^2*Modelica.Constants.pi*H;
end Cylinder;
model test_params
Cylinder cylinder(choice = test_params2.InputChoice.rh,h=1,r=2, v = 30) annotation(
Placement(visible = true, transformation(origin = {-44, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
end test_params;
end test_params2;
如何定义多个参数之间的关系,以便我可以从集合中选择要定义的参数,一些方程式定义其余参数?
例如,我有一个包含 3 个参数的模型,分别是圆柱体的半径、高度和体积,它们通过体积方程 V=r^2*h
相关联。
如果我定义 r
和 h
(并且计算 V
),我希望能够选择模型实例化,r
,V
(和 h
被计算),或 h
、V
(并且 r
被计算)。
在下面的最小示例中,我尝试了方法 1-3,但 none 满足了我的需要。我感觉这一定是一个 common/solved 问题,我只是缺少某种建模技术。你能帮忙吗?
model test_params "Model for a cylinder"
model Cylinder
parameter Real r;
parameter Real h;
parameter Real V;
//Approach 3: binding equation. Works, but what to do if I have V, h and want to know r??
//parameter Real V=r^2*Modelica.Constants.pi*h;
Real t;
initial equation
//Approach 2, apparently chooses V as 0 before getting to the initialisation equation
//V=r^2*Modelica.Constants.pi*h;
//Parameter cylinder.V has no value, and is fixed during initialization (fixed=true), using available start value (start=0.0) as default value.
// The initial conditions are over specified. The following 1 initial equations are redundant, so they are removed from the initialization sytem:
// cylinder.V = 3.141592653589793 * cylinder.r ^ 2.0 * cylinder.h.
equation
t = V;
// Approach 1
//V=r^2*Modelica.Constants.pi*h; //Leads to: Too many equations, over-determined system.
end Cylinder;
test_params.Cylinder cylinder(h = 1, r = 2) annotation(
Placement(visible = true, transformation(origin = {-44, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
end test_params;
这是在 OpenModelica 1.14 上,如果相关的话。
如果您将依赖参数设置为 fixed=false 参数,则可以使用您提到的方法 2 来完成此操作。
model TestParams "Model for a cylinder"
encapsulated model Cylinder
import Modelica.Constants.pi;
parameter Real r;
parameter Real h;
parameter Real V;
initial equation
V = r^2*pi*h "Cylinder volume";
end Cylinder;
TestParams.Cylinder cylinder_1(h=1, r=2, V(fixed=false)) "Compute cylinder V(h, r)"
annotation (Placement(transformation(origin={-70,50}, extent={{-10,-10},{10,10}})));
TestParams.Cylinder cylinder_2(h(start=0, fixed=false), r=2, V=13) "Compute cylinder h(r, V)"
annotation (Placement(transformation(origin={-30,50}, extent={{-10,-10},{10,10}})));
TestParams.Cylinder cylinder_3(h=1, r(start=0, fixed=false), V=13) "Compute cylinder r(h, V)"
annotation (Placement(transformation(origin={10,50}, extent={{-10,-10},{10,10}})));
end TestParams;
我建议,为了有一个图形选择,类似于:
package test_params2 "Model for a cylinder"
type InputChoice = enumeration(rh,rv,hv);
model Cylinder
parameter InputChoice choice;
parameter Real r;
parameter Real h;
parameter Real v;
Real R,H,V;
equation
if choice==InputChoice.rh then
R=r;
H=h;
elseif choice==InputChoice.rv then
R=r;
V=v;
else
H=h;
V=v;
end if;
V=R^2*Modelica.Constants.pi*H;
end Cylinder;
model test_params
Cylinder cylinder(choice = test_params2.InputChoice.rh,h=1,r=2, v = 30) annotation(
Placement(visible = true, transformation(origin = {-44, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
end test_params;
end test_params2;