想象一下,存在重复的组件模式(例如,动态管道),其具有将根据其所驻留的模式中的位置而改变的参数(例如,长度).我建议可以通过在感兴趣的参数之前省略“each”前缀来实现.
例如.让我们采用n个管道并按以下方式将它们添加到模型中:
parameter Integer n;
Modelica.Fluid.Pipes.DynamicPipe[n] pipe;
对于一个非常简单的示例,我们指定我们希望模式中第一个管道的长度具有一定长度(length_1),并且所有其他管道具有length_n.我的想法是,在以下列方式定义参数时可以放置if语句,以便将length_1分配给n = 1管道组件,并为所有其他组件分配length_n:
parameter Integer n;
Modelica.Fluid.Pipes.DynamicPipe[n] pipe(
length=if n<2 then length_1 else cat(1,length_1,fill(length_n,n-1)));
下面提到的框架中的简单模型如下:
model Test
parameter Integer n(min=1);
parameter Modelica.SIunits.Length length_1 = 0.1;
parameter Modelica.SIunits.Length length_n = 0.2;
Modelica.Fluid.Pipes.DynamicPipe[n] pipe(
redeclare each package Medium = Modelica.Media.Water.StandardWater,
each modelStructure=Modelica.Fluid.Types.ModelStructure.av_b,
each diameter=1,
length=if n==1 then length_1 else cat(1,length_1,fill(length_n,n-1)));
Modelica.Fluid.Sources.Boundary_pT boundary(
nPorts=1,
redeclare package Medium = Modelica.Media.Water.StandardWater,
p=100000,
T=293.15);
Modelica.Fluid.Sources.MassFlowSource_T boundary1(
nPorts=1,
redeclare package Medium = Modelica.Media.Water.StandardWater,
m_flow=1,
T=293.15);
inner Modelica.Fluid.System system(m_flow_start=1, T_start=293.15);
equation
if n == 1 then
connect(boundary1.ports[1], pipe[1].port_a);
connect(boundary.ports[1], pipe[1].port_b);
else
connect(boundary1.ports[1], pipe[1].port_a);
for i in 1:n-1 loop
connect(pipe[i].port_b, pipe[i+1].port_a);
end for;
connect(boundary.ports[1], pipe[n].port_b);
end if;
end Test;
目前,模型不起作用.我不确定构造if语句的方式是错误的还是这是不允许的(如果我对“each”前缀的解释是错误的话可能就是这种情况).
有什么想法吗?
最佳答案 首先感谢matth指出我迈克特勒的书中的
pendulum example.
下面是修改代码以实现重复组件模式的两种可能方式,其中参数以某种预定方式改变.该错误与定义数组的长度有关.
选项1:
更换
length=if n==1 then length_1 else cat(1,length_1,fill(length_n,n-1))
同
length={if i==1 then length_1 else length_n for i in 1:n}
选项#2:
插入
final parameter Modelica.SIunits.Length[n]
lengths_pipe = {if i==1 then length_1 else length_n for i in 1:n};
并取代
... length=lengths_pipe
以下是使用第二个选项的完整工作代码:
model Test
parameter Integer n(min=1)=2;
parameter Modelica.SIunits.Length length_1 = 0.1;
parameter Modelica.SIunits.Length length_n = 0.2;
final parameter Modelica.SIunits.Length[n] lengths_pipe = {if i==1 then length_1 else length_n for i in 1:n};
Modelica.Fluid.Pipes.DynamicPipe[n] pipe(
redeclare each package Medium = Modelica.Media.Water.StandardWater,
each modelStructure=Modelica.Fluid.Types.ModelStructure.av_b,
each diameter=1,
length=lengths_pipe);
Modelica.Fluid.Sources.Boundary_pT boundary(
nPorts=1,
redeclare package Medium = Modelica.Media.Water.StandardWater,
p=100000,
T=293.15);
Modelica.Fluid.Sources.MassFlowSource_T boundary1(
nPorts=1,
redeclare package Medium = Modelica.Media.Water.StandardWater,
m_flow=1,
T=293.15);
inner Modelica.Fluid.System system(m_flow_start=1, T_start=293.15);
equation
if n == 1 then
connect(boundary1.ports[1], pipe[1].port_a);
connect(boundary.ports[1], pipe[1].port_b);
else
connect(boundary1.ports[1], pipe[1].port_a);
for i in 1:n-1 loop
connect(pipe[i].port_b, pipe[i+1].port_a);
end for;
connect(boundary.ports[1], pipe[n].port_b);
end if;
annotation (uses(Modelica(version="3.2.1")));
end Test;