如何在 OpenModelica 中设置包含 extend/redeclare 的库应用程序?
How to setup library-application including extend/redeclare in OpenModelica?
我将 Modelica 代码分为一个小库 DEMO_v11.mo 和一个应用程序 D11_APP7.mo 应用程序代码包括使库接口适应应用程序的部分:导入-扩展-重新声明。这一切都适用于 JModelica。现在我想在 OpenModelica 中设置它,但我不知道如何处理我的两个不同文件。浏览文档我找不到什么帮助。
我之前设法引入了一个库,并在 OpenModelica 中添加了图形符号,然后基于库中的组件组成了一个新模型。
但是,现在我需要做一个更“高级”的导入,以扩展-重新声明导入的模型。因此我的问题是如何做这个更“高级”的部分。
感谢一些建议,或关于在哪里阅读的建议。
我找到的这个问题的答案既简单又困难。
简单的部分是您应该在 OpenModelica 中使用命令“File/Open Modelica/Library 文件以相同的方式加载库和应用程序代码。然后图书馆和应用程序并排着陆,某种程度上。它们的图标显示在 MSL 库 Modelica 下方的左侧窗格中。然后,如果需要,应用程序代码可以从加载的库和 MSL 中以类似的方式导入(并重新声明)。
困难的部分是,当您在两个(或更多)步骤而不是一个步骤中引用模型时,这似乎是 OpenModelica 中的错误。我与 OpenModelica 支持人员讨论了这个问题。
如上所述,可以导入的代码是库 DEMO_v15 和应用程序 D15_app7,如下所示(并根据提到和描述的 DEMO_v11 和 D11_app7 稍作修改在另一个线程中)。
package DEMO_v15
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium2
replaceable constant String name = "Two components" "Medium name";
replaceable constant Integer nc = 2 "Number of substances";
replaceable type Concentration = Real[nc] "Substance conc";
replaceable constant Real[nc] mw = {10, 20} "Substance weight";
constant Integer A = 1 "Substance index";
constant Integer B = 2 "Substance index";
end Medium2;
package Medium3
import M2 = DEMO_v15.Medium2;
extends M2
(name="Three components" "Medium name",
nc=3 "Number of substances",
mw = cat(1,M2.mw,{30}) "Substance weight",
redeclare type Concentration = Real[nc] "Substance conc");
constant Integer C = 3 "Substance index";
end Medium3;
connector LiquidCon3
Medium3.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon3;
// ---------------------------------------------------------------------------------------------
// Equipment dependent on the medium
// ---------------------------------------------------------------------------------------------
package Equipment
replaceable connector LiquidCon
end LiquidCon;
model PumpType
LiquidCon inlet, outlet;
RealInput Fsp;
equation
inlet.F = Fsp;
connect(outlet, inlet);
end PumpType;
model FeedtankType
LiquidCon outlet;
constant Integer medium_nc = size(outlet.c,1);
parameter Real[medium_nc] c_in (each unit="kg/m3")
= {1.0*k for k in 1:medium_nc} "Feed inlet conc";
parameter Real V_0 (unit="m3") = 100 "Initial feed volume";
Real V(start=V_0, fixed=true, unit="m3") "Feed volume";
equation
for i in 1:medium_nc loop
outlet.c[i] = c_in[i];
end for;
der(V) = outlet.F;
end FeedtankType;
model HarvesttankType
LiquidCon inlet;
constant Integer medium_nc = size(inlet.c,1);
parameter Real V_0 (unit="m3") = 1.0 "Initial harvest liquid volume";
parameter Real[medium_nc] m_0
(each unit="kg/m3") = zeros(medium_nc) "Initial substance mass";
Real[medium_nc] c "Substance conc";
Real[medium_nc] m
(start=m_0, each fixed=true) "Substance mass";
Real V(start=V_0, fixed=true, unit="m3") "Harvest liquid volume";
equation
for i in 1:medium_nc loop
der(m[i]) = inlet.c[i]*inlet.F;
c[i] = m[i]/V;
end for;
der(V) = inlet.F;
end HarvesttankType;
end Equipment;
// ---------------------------------------------------------------------------------------------
// Control
// ---------------------------------------------------------------------------------------------
package Control
block FixValueType
RealOutput out;
parameter Real val=0;
equation
out = val;
end FixValueType;
end Control;
// ---------------------------------------------------------------------------------------------
// Adaptation of package Equipment to Medium3
// ---------------------------------------------------------------------------------------------
// package Equipment3 = Equipment(redeclare connector LiquidCon=LiquidCon3); // Just shorter
package Equipment3
import DEMO_v15.Equipment;
extends Equipment(redeclare connector LiquidCon=LiquidCon3);
end Equipment3;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
model Test
Medium3 medium;
Equipment3.FeedtankType feedtank;
Equipment3.HarvesttankType harvesttank;
Equipment3.PumpType pump;
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
end Test;
end DEMO_v15;
以及申请代码:
encapsulated package D15_app7
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium7
import M2 = DEMO_v15.Medium2;
extends M2
(name = "Seven components" "Medium name",
nc = 7 "Number of substances",
mw = cat(1,M2.mw,{30,40,50,60,70}) "Substance weight",
redeclare type Concentration = Real[nc] "Substance conc");
constant Integer C = 3 "Substance index";
constant Integer D = 4 "Substance index";
constant Integer E = 5 "Substance index";
constant Integer F = 6 "Substance index";
constant Integer G = 7 "Substance index";
end Medium7;
connector LiquidCon7
Medium7.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon7;
// ---------------------------------------------------------------------------------------------
// Adaptation of library DEMO_v15 to Medium7
// ---------------------------------------------------------------------------------------------
package Equipment7
import DEMO_v15.Equipment;
extends Equipment(redeclare connector LiquidCon=LiquidCon7);
end Equipment7;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
import DEMO_v15.Control;
model Test
Medium7 medium; // Instance not necessary but helpful for user interface
Equipment7.PumpType pump;
Equipment7.FeedtankType feedtank;
Equipment7.HarvesttankType harvesttank;
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
end Test;
end D15_app7;
我将 Modelica 代码分为一个小库 DEMO_v11.mo 和一个应用程序 D11_APP7.mo 应用程序代码包括使库接口适应应用程序的部分:导入-扩展-重新声明。这一切都适用于 JModelica。现在我想在 OpenModelica 中设置它,但我不知道如何处理我的两个不同文件。浏览文档我找不到什么帮助。
我之前设法引入了一个库,并在 OpenModelica 中添加了图形符号,然后基于库中的组件组成了一个新模型。 但是,现在我需要做一个更“高级”的导入,以扩展-重新声明导入的模型。因此我的问题是如何做这个更“高级”的部分。
感谢一些建议,或关于在哪里阅读的建议。
我找到的这个问题的答案既简单又困难。
简单的部分是您应该在 OpenModelica 中使用命令“File/Open Modelica/Library 文件以相同的方式加载库和应用程序代码。然后图书馆和应用程序并排着陆,某种程度上。它们的图标显示在 MSL 库 Modelica 下方的左侧窗格中。然后,如果需要,应用程序代码可以从加载的库和 MSL 中以类似的方式导入(并重新声明)。
困难的部分是,当您在两个(或更多)步骤而不是一个步骤中引用模型时,这似乎是 OpenModelica 中的错误。我与 OpenModelica 支持人员讨论了这个问题。
如上所述,可以导入的代码是库 DEMO_v15 和应用程序 D15_app7,如下所示(并根据提到和描述的 DEMO_v11 和 D11_app7 稍作修改在另一个线程中)。
package DEMO_v15
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium2
replaceable constant String name = "Two components" "Medium name";
replaceable constant Integer nc = 2 "Number of substances";
replaceable type Concentration = Real[nc] "Substance conc";
replaceable constant Real[nc] mw = {10, 20} "Substance weight";
constant Integer A = 1 "Substance index";
constant Integer B = 2 "Substance index";
end Medium2;
package Medium3
import M2 = DEMO_v15.Medium2;
extends M2
(name="Three components" "Medium name",
nc=3 "Number of substances",
mw = cat(1,M2.mw,{30}) "Substance weight",
redeclare type Concentration = Real[nc] "Substance conc");
constant Integer C = 3 "Substance index";
end Medium3;
connector LiquidCon3
Medium3.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon3;
// ---------------------------------------------------------------------------------------------
// Equipment dependent on the medium
// ---------------------------------------------------------------------------------------------
package Equipment
replaceable connector LiquidCon
end LiquidCon;
model PumpType
LiquidCon inlet, outlet;
RealInput Fsp;
equation
inlet.F = Fsp;
connect(outlet, inlet);
end PumpType;
model FeedtankType
LiquidCon outlet;
constant Integer medium_nc = size(outlet.c,1);
parameter Real[medium_nc] c_in (each unit="kg/m3")
= {1.0*k for k in 1:medium_nc} "Feed inlet conc";
parameter Real V_0 (unit="m3") = 100 "Initial feed volume";
Real V(start=V_0, fixed=true, unit="m3") "Feed volume";
equation
for i in 1:medium_nc loop
outlet.c[i] = c_in[i];
end for;
der(V) = outlet.F;
end FeedtankType;
model HarvesttankType
LiquidCon inlet;
constant Integer medium_nc = size(inlet.c,1);
parameter Real V_0 (unit="m3") = 1.0 "Initial harvest liquid volume";
parameter Real[medium_nc] m_0
(each unit="kg/m3") = zeros(medium_nc) "Initial substance mass";
Real[medium_nc] c "Substance conc";
Real[medium_nc] m
(start=m_0, each fixed=true) "Substance mass";
Real V(start=V_0, fixed=true, unit="m3") "Harvest liquid volume";
equation
for i in 1:medium_nc loop
der(m[i]) = inlet.c[i]*inlet.F;
c[i] = m[i]/V;
end for;
der(V) = inlet.F;
end HarvesttankType;
end Equipment;
// ---------------------------------------------------------------------------------------------
// Control
// ---------------------------------------------------------------------------------------------
package Control
block FixValueType
RealOutput out;
parameter Real val=0;
equation
out = val;
end FixValueType;
end Control;
// ---------------------------------------------------------------------------------------------
// Adaptation of package Equipment to Medium3
// ---------------------------------------------------------------------------------------------
// package Equipment3 = Equipment(redeclare connector LiquidCon=LiquidCon3); // Just shorter
package Equipment3
import DEMO_v15.Equipment;
extends Equipment(redeclare connector LiquidCon=LiquidCon3);
end Equipment3;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
model Test
Medium3 medium;
Equipment3.FeedtankType feedtank;
Equipment3.HarvesttankType harvesttank;
Equipment3.PumpType pump;
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
end Test;
end DEMO_v15;
以及申请代码:
encapsulated package D15_app7
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium7
import M2 = DEMO_v15.Medium2;
extends M2
(name = "Seven components" "Medium name",
nc = 7 "Number of substances",
mw = cat(1,M2.mw,{30,40,50,60,70}) "Substance weight",
redeclare type Concentration = Real[nc] "Substance conc");
constant Integer C = 3 "Substance index";
constant Integer D = 4 "Substance index";
constant Integer E = 5 "Substance index";
constant Integer F = 6 "Substance index";
constant Integer G = 7 "Substance index";
end Medium7;
connector LiquidCon7
Medium7.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon7;
// ---------------------------------------------------------------------------------------------
// Adaptation of library DEMO_v15 to Medium7
// ---------------------------------------------------------------------------------------------
package Equipment7
import DEMO_v15.Equipment;
extends Equipment(redeclare connector LiquidCon=LiquidCon7);
end Equipment7;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
import DEMO_v15.Control;
model Test
Medium7 medium; // Instance not necessary but helpful for user interface
Equipment7.PumpType pump;
Equipment7.FeedtankType feedtank;
Equipment7.HarvesttankType harvesttank;
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
end Test;
end D15_app7;