我将继续努力了解如何最好地划分库和应用程序中的代码。在先前的几篇文章中,我使用了带有示例dxx_app7的玩具示例DEMO_xx库。以下是稍作更新的版本,以解决此处的问题。我已经包括在收获箱中添加传感器的可能性。
现在,我想了解如何在应用程序代码中从应用程序Medium7引入信息,该信息是从Media2库扩展而来的,并在应用程序的新组件模型中完全使用这些信息。
我了解的是,如果导入从另一个软件包扩展的软件包,则只会得到什么是“最新”软件包,而不是通过扩展构建的内容以及汇总的Medium7信息的总和。
在示例中,通过将常量SensorX.component从有效的C更改为A或无效的B来说明这一事实。
但是,如果我创建了Medium7的本地实例,那么我会获得Medium7的完整信息。我可以在SensorX中以标记为问题1-替代2的方式进行操作。 我也可以选择这种底物以在系统配置中进行测量,并标记为Question 1 alt3。我认为这是更具可读性的代码。
但是我如何使Medium7的所有内容在本地可用?我是否必须为我需要的每个常量在本地定义一个新的常量(如此处在sensorX.component中所示)?通常,您可能希望在模型中访问介质的各种属性,并且可以很方便地将其放入介质包
我也希望我可以导入适用于Medium7的连接器LiquidCon,而不是再次在程序包Sensor7中编写该代码。按照建议的导入无效。还有另一种方法吗?我只测试了JModelica 2.4中的代码,这可能是一个错误吗?
将感谢您对这两个问题的一些投入。 /扬·彼得(Jan Peter)
在问题所感兴趣的应用程序代码d12_app7下面,然后是相关的库DEMO_v12。我已经标记了这两个问题的代码注释更改,按照代码的原样,它是问题1的替代项1。
encapsulated package d12_app7
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium7
import M2 = DEMO_v12.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;
// ---------------------------------------------------------------------------------------------
// New sensor introduced in this application for measurement of substance A
// ---------------------------------------------------------------------------------------------
package Sensor7
connector LiquidCon
Medium7.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon;
model SensorX
// import d12_app7.Equipment7.LiquidCon; // Question 2
import d12_app7.Medium7.*; // Question 1 - alt 1
constant Integer component = C; // Question 1 - alt 1
// constant Integer component = d12_app7.Medium7.A; // Question 1 - alt 2
LiquidCon probe;
RealOutput out;
equation
probe.F = 0;
out = probe.c[component];
end SensorX;
end Sensor7;
// ---------------------------------------------------------------------------------------------
// Adaptation of library DEMO_v12 to Medium7
// ---------------------------------------------------------------------------------------------
package Equipment7
import DEMO_v12.Equipment;
extends Equipment(redeclare package Medium=Medium7);
end Equipment7;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
import DEMO_v12.Control;
model Test
Equipment7.Medium medium; // Instance not necessary but helpful for user interface
Equipment7.PumpType pump;
Equipment7.FeedtankType feedtank;
Equipment7.HarvesttankType harvesttank;
Sensor7.SensorX sensor; // Question 1 alt 1 and 2
// Sensor7.SensorX sensor(component = medium.A); // Question 1 alt 3
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
connect(sensor.probe, harvesttank.port);
end Test;
end d12_app7;
最后是库代码DEMO_v12
package DEMO_v12
// ---------------------------------------------------------------------------------------------
// 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_v12.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;
// ---------------------------------------------------------------------------------------------
// Equipment dependent on the medium
// ---------------------------------------------------------------------------------------------
package Equipment
replaceable package Medium
end Medium;
connector LiquidCon
Medium.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
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, port;
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;
port.c[i] = c[i];
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;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
// package Equipment3 = Equipment(redeclare package Medium=Medium3); // Just shorter version
package Equipment3
import DEMO_v12.Equipment;
extends Equipment(redeclare package Medium=Medium3);
end Equipment3;
model Test
Equipment3.Medium 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_v12;
答案 0 :(得分:0)
最近我在这篇文章中也得到了与Modelon-JModelica和OpenModelica有关的人员的帮助-谢谢!下面的答案和评论以及更新的代码。
该代码适用于JModelica,既可以单独运行DEMO_v20,又可以运行三种物质的测试示例,还可以运行七种物质的应用程序代码。但是,在OpenModelica中,仅适用于三种物质的测试示例。
非常感谢您提供帮助,以使代码在OpenModelica中运行并在Dymolas中进行检查。
库代码DEMO_v20
package DEMO_v20
// Here I have put together a small demo-library to illustrate questions
// around structuring handling of medium. The key structures are taken
// from MSL fluid, I think it is fair to say.
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
partial package MediumBase
constant String name "Medium name";
constant Integer nc "Number of substances";
replaceable type Concentration = Real[nc] "Substance conc";
end MediumBase;
package Medium2
extends MediumBase
(name="Two components",
nc=2);
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_v20.Medium2;
extends M2
(name="Three components" "Medium name",
nc=3 "Number of substances",
mw = cat(1,M2.mw,{30}) "Substance weight");
constant Integer C = 3 "Substance index";
end Medium3;
// ---------------------------------------------------------------------------------------------
// Equipment dependent on the medium
// ---------------------------------------------------------------------------------------------
package Equipment
replaceable package Medium = MediumBase
constrainedby MediumBase;
connector LiquidCon
Medium.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
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, port;
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;
port.c[i] = c[i];
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
extends DEMO_v20.Equipment(redeclare package Medium=Medium3);
end Equipment3;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
model Test
package medium = DEMO_v20.Medium3; // Not accessible in FMU though
constant String name = medium.name; // But name here is accessible
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_v20;
以及相应的应用程序代码d20_app7.mo
encapsulated package d20_app7
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium7
import M2 = DEMO_v20.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");
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;
// ---------------------------------------------------------------------------------------------
// Adaptation of library DEMO_v20 to Medium7 and including a new model SensorX
// ---------------------------------------------------------------------------------------------
package Equipment7
extends DEMO_v20.Equipment(redeclare package Medium=Medium7);
model SensorX
LiquidCon probe;
RealOutput out;
constant Integer component "Component measured";
equation
probe.F = 0;
out = probe.c[component];
end SensorX;
end Equipment7;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
import DEMO_v20.Control;
model Test
package medium = Medium7; // Not accessible in FMU though
constant String name = medium.name; // But name here is accessible
Equipment7.PumpType pump;
Equipment7.FeedtankType feedtank;
Equipment7.HarvesttankType harvesttank;
Equipment7.SensorX sensor(component = medium.G);
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
connect(sensor.probe, harvesttank.port);
end Test;
end d20_app7;
答案 1 :(得分:0)
在Dymola版本2018(64位),2017-04-10中测试了DEMO_v20.mo
和d20_app7.mo
。
加载文件DEMO_v20.mo
会导致错误,
<medium declaration> (line 137, column 27: C:/Users/adeas31/Desktop/DEMO_v20.mo)
medium already declared on line 135.
运行DEMO_v20.Test
会得到
Translation of DEMO_v20.Test:
For variable feedtank.medium_nc
declared in class DEMO_v20.Equipment.FeedtankType, C:/Users/adeas31/Desktop/DEMO_v20.mo at line 77, and used in component feedtank.
The variability of the definition equation:
feedtank.medium_nc = size(feedtank.outlet.c, 1);
is higher than the declared variability of the variables.
For variable harvesttank.medium_nc
declared in class DEMO_v20.Equipment.HarvesttankType, C:/Users/adeas31/Desktop/DEMO_v20.mo at line 91, and used in component harvesttank.
The variability of the definition equation:
harvesttank.medium_nc = size(harvesttank.inlet.c, 1);
is higher than the declared variability of the variables.
Basic type inconsistencies detected.
Translation aborted.
ERRORS have been issued.
运行d20_app7.Test
会给出
Translation of d20_app7.Test:
Encapsulation of d20_app7
prevented us from finding DEMO_v20 in global scope.
Missing base class DEMO_v20.Equipment
the class DEMO_v20.Equipment exists, but Modelica is case-sensitive and uses scoping
File: C:/Users/adeas31/Desktop/d20_app7.mo, line 46
Context: d20_app7.Equipment7
Component type specifier Equipment7.PumpType not found
File: C:/Users/adeas31/Desktop/d20_app7.mo, line 67
Component context: pump
Component declared as Equipment7.PumpType pump in d20_app7.Test
Component type specifier Equipment7.FeedtankType not found
File: C:/Users/adeas31/Desktop/d20_app7.mo, line 68
Component context: feedtank
Component declared as Equipment7.FeedtankType feedtank in d20_app7.Test
Component type specifier Equipment7.HarvesttankType not found
File: C:/Users/adeas31/Desktop/d20_app7.mo, line 69
Component context: harvesttank
Component declared as Equipment7.HarvesttankType harvesttank in d20_app7.Test
Component type specifier LiquidCon not found
File: C:/Users/adeas31/Desktop/d20_app7.mo, line 49
Component context: sensor.probe
Component declared as LiquidCon probe in d20_app7.Equipment7.SensorX
WARNINGS have been issued.
ERRORS have been issued.
希望有帮助。
答案 2 :(得分:0)
谢谢你阿黛尔! Dymola错误日志使我对代码进行了以下更改:
更新的代码DEMO_v22和d22_app7现在可与JModelica和OpenModelica一起使用。也可以在Dymola中对其进行测试
更新的代码DEMO_v22.mo
package DEMO_v22
// Here I have put together a small demo-library to illustrate questions
// around structuring handling of medium. The key structures are taken
// from MSL fluid, I think it is fair to say.
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
partial package MediumBase
constant String name "Medium name";
constant Integer nc "Number of substances";
replaceable type Concentration = Real[nc] "Substance conc";
end MediumBase;
package Medium2
extends MediumBase
(name="Two components",
nc=2);
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_v22.Medium2;
extends M2
(name="Three components" "Medium name",
nc=3 "Number of substances",
mw = cat(1,M2.mw,{30}) "Substance weight");
constant Integer C = 3 "Substance index";
end Medium3;
// ---------------------------------------------------------------------------------------------
// Equipment dependent on the medium
// ---------------------------------------------------------------------------------------------
package Equipment
replaceable package Medium = MediumBase
constrainedby MediumBase;
connector LiquidCon
Medium.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon;
model PumpType
LiquidCon inlet, outlet;
RealInput Fsp;
equation
inlet.F = Fsp;
connect(outlet, inlet);
end PumpType;
model FeedtankType
LiquidCon outlet;
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, port;
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;
port.c[i] = c[i];
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
extends DEMO_v22.Equipment(redeclare package Medium=Medium3);
end Equipment3;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
model Test
package medium = DEMO_v22.Medium3; // Not accessible in FMU though
constant String name = medium.name; // But name here is now accessible
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_v22;
和更新的应用程序代码d22_app7.mo
encapsulated package d22_app7
// Here I put together an application for 7 substances - print 8 pt
// and import code from the library DEMO.
// ---------------------------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------------------------
import Modelica.Blocks.Interfaces.RealInput;
import Modelica.Blocks.Interfaces.RealOutput;
package Medium7
import M2 = DEMO_v22.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");
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;
// ---------------------------------------------------------------------------------------------
// Adaptation of library DEMO_v22 to Medium7 and including a new model SensorX
// ---------------------------------------------------------------------------------------------
package Equipment7
import DEMO_v22.Equipment;
extends Equipment(redeclare package Medium=Medium7);
model SensorX
LiquidCon probe;
RealOutput out;
constant Integer component "Component measured";
equation
probe.F = 0;
out = probe.c[component];
end SensorX;
end Equipment7;
// ---------------------------------------------------------------------------------------------
// Examples of systems
// ---------------------------------------------------------------------------------------------
import DEMO_v22.Control;
model Test
package medium = Medium7; // Not accessible in FMU though
constant String name = medium.name; // But name here is accessible
Equipment7.PumpType pump;
Equipment7.FeedtankType feedtank;
Equipment7.HarvesttankType harvesttank;
Equipment7.SensorX sensor(component = medium.G);
Control.FixValueType Fsp(val=0.2);
equation
connect(feedtank.outlet, pump.inlet);
connect(pump.outlet, harvesttank.inlet);
connect(Fsp.out, pump.Fsp);
connect(sensor.probe, harvesttank.port);
end Test;
end d22_app7;