1

我的工作是了解库中的参数化包如何适应外部应用程序代码,即我喜欢将库代码和应用程​​序代码保持分离。

在我的示例中,我的设备包有两个参数,一个连接器 LCon 和一个模型 CType。连接器影响设备包中的所有模型。模型 CType 仅影响设备中的一个模型 RType(并且具有更密切的内外关系)到该模型。

当我使 Package Equipment 适应 LCon2 和 CTyp2 的应用要求时,我可以如下所示一次性完成。

   code1

   package Equipment2
          import BR5i.Equipment;
          extends Equipment(redeclare connector LCon=LCon2,
                      redeclare model CType=CType2);
   end Equipment2;    

但是,如果我将这两种改编分成两个不同的部分,我认为代码(从长远来看)更具可读性。我尝试下面的代码,但不起作用。错误文本:找不到 RType 的类声明 - 当我在 JModelica 中运行它时。

   code2

   package Equipment2
       import BR5i.Equipment;
       extends Equipment(redeclare connector LCon=LCon2);
   end Equipment2;

   model BRType2        
       import BR5i.Equipment2.RType;
       extends RType(redeclare model CType=CType2);
   end BRType2;

(对于代码 2,库进行了修改,因此将参数 CType 移动到了单个模型 RType,其中 CType 应作为参数。最后,我希望代码 2 的 BRType2 对应于代码 1 中的 Equipment2.RType) .

我想知道是否有可能在这样的几个步骤中进行更改,即首先 RType 获得一个新的连接器 LCon2,然后在下一步中,现在从 Equipment2 导入的 RType 将 CType 替换为 CType2?

我知道代码不应被视为一系列“赋值语句”,而应视为并行。在我看来,代码 2 中“方程式”的逻辑应该可以得到正确的 BRType2。

4

5 回答 5

2

您的“code2”将导致 BRType2没有修改 CType。重新声明并不意味着“将包 A 更改为这样”,而是“这应该像包 A,但有这些更改”。因此,要获得您想要的结果,您应该执行以下操作:

package Equipment2
    import BR5i.Equipment;
    extends Equipment(redeclare connector LCon=LCon2);
end Equipment2;

model BRType2
    // Equipment2 has the change in LCon, so extend RType from there instead
    import Equipment2.RType;
    extends RType(redeclare model CType=CType2);
end BRType2;

另请注意,如果 Equipment 包含 Rtype 的任何实例或其他引用,则此方法不会给出预期结果,因为它们将引用未更改的 RType,而不是 BRType2。

为什么你会收到关于找不到 RType 的错误,我不能说。这可能是一个错误,但我会首先检查您是否正确编写了它的路径。

于 2019-03-28T13:01:14.887 回答
1

根据要求,我在这里提供了一个小的独立示例,大约 150 行。我决定重新使用我最近在其他几篇文章中使用的玩具示例,现在添加说明此处讨论的问题所需的内容。这意味着与上面写的相比,我使用了不同的事物名称,但问题的结构完全相同。

玩具示例描述了将介质从一个容器泵送到另一容器的过程,并且是通用的,以便我们可以轻松更换介质,并且泵和罐的设备会自动更新。原来介质中含有两种物质。使用单独的简短应用程序代码,我们定义了具有更多组件的介质,并通过重新声明连接器 LiquidCon 来更新设备包,然后简单地重新使用系统设置。

现在我在收获中添加了一个内部模型,描述了物质之间的一些反应。最初我们有一个反应模型 NoReaction,它将物质留在收获罐中而没有任何反应。另一个反应模型是反应 3,它显示物质 3 的降解。

我想强调的问题是,如果我们首先使用连接器来调整设备,例如三种物质。然后在第二轮中将适配设备3的反应模型更改为反应模型Reaction3,JModelica编译器给出错误信息,而OpenModelica没有并产生可以正常的仿真结果。这在代码中被标记为备选方案 2。

备选方案 1. 另一方面,如果两个非常不同的重新声明同时完成,即都更改了连接器 LiquidCon 和模型 ReactionType,那么它在 JModelica 和 OpenModelica 中都有效。

下面的自包含代码和示例 Test now 是 Alternative 2 并在 JModelica 2.4 中生成错误,但在 OpenModelica 中有效。我不清楚对 Modelica def 本身有什么期望。

    package DEMO_v14

        // 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.

        // Author: Jan Peter Axelsson

    //  ---------------------------------------------------------------------------------------------
    //     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_v14.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;

        model Reaction3
            constant Integer nc = 3;
            outer Real[nc] c;
            outer Real[nc] q;           
        equation
            q[1] = 0;
            q[2] = 0;
            q[3] =-c[3];
        end Reaction3;      

    //  ---------------------------------------------------------------------------------------------
    //     Equipment dependent on the medium  
    //  ---------------------------------------------------------------------------------------------

        package Equipment
            replaceable connector LiquidCon
            end LiquidCon;

    //      replaceable model ReactionType                         // Alternative 1
    //      end ReactionType;

            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
                // Connection to reaction
                replaceable model ReactionType                     // Alternative 2 
                end ReactionType;

                ReactionType reaction;
                inner Real[medium_nc] c                            "Substance conc";
                inner Real[medium_nc] q                            "Reaction rate";     

                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] 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 + q[i];
                    c[i] = m[i]/V;
                    port.c[i] = c[i];
                end for;
                der(V) = inlet.F;               
            end HarvesttankType;

            model NoReaction
                constant Integer nc = Medium.nc;
                outer Real[nc] c;
                outer Real[nc] q;           
            equation
                for i in 1:nc loop
                    q[i] = 0;
                end for;
            end NoReaction;     

        end Equipment;

    //  ---------------------------------------------------------------------------------------------   
    //     Control 
    //  ---------------------------------------------------------------------------------------------

        package Control
            block FixValueType
                RealOutput out;
                parameter Real val=0;
            equation
                out = val;
            end FixValueType;
        end Control;

    //  ---------------------------------------------------------------------------------------------
    //  Adaptation of library for the actual culture and media
    //  ---------------------------------------------------------------------------------------------

    //  package Equipment3                                                     // Alternative  1
    //      import DEMO_v14.Equipment;
    //      extends Equipment(redeclare connector LiquidCon=LiquidCon3,
    //                        redeclare model ReactionType=Reaction3);
    //  end Equipment3;

        package Equipment3                                                     // Alternative 2
            import DEMO_v14.Equipment;
            extends Equipment(redeclare connector LiquidCon=LiquidCon3);
        end Equipment3;

        model HarvesttankType3
            import DEMO_v14.Equipment3.HarvesttankType;
            extends HarvesttankType(redeclare model ReactionType=Reaction3);
        end HarvesttankType3;

    //  ---------------------------------------------------------------------------------------------
    //     Examples of systems 
    //  ---------------------------------------------------------------------------------------------

        model Test
            Medium3 medium;
            Equipment3.FeedtankType feedtank;
    //      Equipment3.HarvesttankType harvesttank;                            // Alternative 1 
            HarvesttankType3 harvesttank;                                      // Alternative 2
            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_v14;
于 2019-04-02T13:27:52.370 回答
1

我没有时间详细分析您的问题,但我想在此指出,如果某些代码在一个工具中被接受,但在另一个工具中不被接受,这并不一定意味着错误出现在工具中不接受代码。随着时间的推移,Modelica 语义变得更加严格,以使 Modelica 使用起来更安全,并且更容易跨工具移植。由于 OpenModelica 内核是最古老的内核之一,众所周知,它可以接受许多在最新版本中实际上(在许多情况下不再)合法的 Modelica 代码。

抱歉,我没有时间从 Modelica 语义合法性的角度深入分析这一点。在任何情况下,我都相信您可以通过合法的 Modelica 实现您想要实现的目标。关于建模实践的两点评论: 将空类(在您的情况下为连接器)作为可替换类是不安全的,应该避免,因为它可以被任何东西替换,这本质上是不安全的。我也认为你可以通过连接器中的可变长度向量来实现你想要实现的目标,连接器本身不需要是可替换的。

一切顺利,/Hubertus

于 2019-04-18T18:49:49.873 回答
0

我认为,要以安全的方式对库包进行参数化,您可以使灵活性尽可能“小”。您可以在包中拥有一个常量整数 nc,您在调整包时将其指定为 Medium3.nc。然后在 Equipment 包内定义了连接器 LiquidCon,并将浓度向量声明为 Real [nc] c;除了 nc 之外,关于 Medium 的其他信息可以在配置级别上带到应用程序测试中,而不是作为设备包的改编,在这个例子中没有讨论,而是在其他相关帖子中讨论。通过这种方式,包适应过程将尽可能“安全”。

涉及将 ReactionType 引入 HarvesttankType(现在已适应实际的 nc)的其他适应需要非常灵活,以使该库包具有任何兴趣。我们对 ReactionType 的要求是具有接口:outer Real(nc] c, q; 并且我们可以在部分模型中描述并使用 constrainedby 构造,以带来一些安全性。

请参阅下面的代码 DEMO_v17_alt1 和 d17_alt1_app7。

但是,我更愿意将代码编写为 alt2 并保留 ReactionType 等仅为 HarvesttankType 而不是整个 Equipment 包定义。这将需要允许对库进行两步调整。第一级使设备适应介质组件的数量。第二级使现在适应的 HarvesttankType 适应 ReactionType。这在 JModelica 中是不可能的,但实际上在 OpenModelica 中是不可能的。

所以只有 alt1 在 JModelica 2.4 中有效,而 alt1 和 alt2 在 OpenModelica 1.13.2(和 1.14 beta)中有效。当前的 Modelica 定义说明了什么?

库代码 DEMO_v17_alt1:

    package DEMO_v17_alt1

        // 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;

        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_v17_alt1.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;

        model Reaction3
            constant Integer nc = 3;
            outer Real[nc] c;
            outer Real[nc] q;           
        equation
            q[1] = 0;
            q[2] = 0;
            q[3] =-c[3];
        end Reaction3;      

    //  ---------------------------------------------------------------------------------------------
    //     Equipment dependent on the medium  
    //  ---------------------------------------------------------------------------------------------

        package Equipment

            constant Integer nc;

            connector LiquidCon
                Real[nc] c                                         "Substance conc";
                flow Real F (unit="m3/s")                          "Flow rate";
            end LiquidCon;              

            replaceable model ReactionType = NoReaction            // Alternative 1
                constrainedby ReactionTypeInterface;               

            partial model ReactionTypeInterface                    // Alternative 1
                outer Real[nc] c, q;
            end ReactionTypeInterface;

            model NoReaction                                       // Alternative 1
                extends ReactionTypeInterface;          
            equation
                for i in 1:nc loop
                    q[i] = 0;
                end for;
            end NoReaction;             

            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
                // Connection to reaction
                // replaceable model ReactionType = NoReaction constrainedby ReactionTypeInterface;   // Alternative 2

                ReactionType reaction;
                inner Real[medium_nc] c                            "Substance conc";
                inner Real[medium_nc] q                            "Reaction rate";     

                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] 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 + q[i];
                    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 library DEMO_v17_alt1 to Medium3 and Reaction3
    //  ---------------------------------------------------------------------------------------------

        package Equipment3                                         // Alternative 1
            import DEMO_v17_alt1.Equipment;
            extends Equipment(nc=Medium3.nc,
                              redeclare model ReactionType=Reaction3);
        end Equipment3;

    //  package Equipment3                                         // Alternative 2
    //      import DEMO_v17_alt2.Equipment;
    //      extends Equipment(nc=3);
    //  end Equipment3;

    //  model HarvesttankType3
    //      import DEMO_v17_alt2.Equipment3.HarvesttankType;
    //      extends HarvesttankType(redeclare model ReactionType=Reaction3);
    //  end HarvesttankType3;

    //  ---------------------------------------------------------------------------------------------
    //     Examples of systems 
    //  ---------------------------------------------------------------------------------------------

        model Test
            Medium3 medium;
            Equipment3.FeedtankType feedtank;
            Equipment3.HarvesttankType harvesttank;                // Alternative 1 
    //      HarvesttankType3 harvesttank;                          // Alternative 2
            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_v17_alt1;

和应用程序代码 d17_alt1_app7

    encapsulated package d17_alt1_app7 


    //  ---------------------------------------------------------------------------------------------
    //     Interfaces  
    //  ---------------------------------------------------------------------------------------------

        import Modelica.Blocks.Interfaces.RealInput;
        import Modelica.Blocks.Interfaces.RealOutput;

        package Medium7
            import M2 = DEMO_v17_alt1.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;

        model Reaction7
            constant Integer nc = 7;
            outer Real[nc] c;
            outer Real[nc] q;           
        equation
            q[1] = 0;
            q[2] = 0;
            q[3] = 0;
            q[4] = 0;       
            q[5] = 0;               
            q[6] = 0;       
            q[7] =-c[7];
        end Reaction7;  

    //  ---------------------------------------------------------------------------------------------
    //     Adaptation of library DEMO_v17_alt1 to Medium7 and Reaction7  
    //  ---------------------------------------------------------------------------------------------

        package Equipment7
            import DEMO_v17_alt1.Equipment;
            extends Equipment(nc=Medium7.nc,
                              redeclare model ReactionType=Reaction7);
        end Equipment7;

    //  ---------------------------------------------------------------------------------------------       
    //     Examples of systems 
    //  ---------------------------------------------------------------------------------------------

        import DEMO_v17_alt1.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 d17_alt1_app7;
于 2019-04-22T10:15:14.197 回答
0

我最近从与 Modelon 和 OpenModelica 相关的人那里得到了一些帮助,对此我表示感谢。下面是库和应用程序的更新文件。提供的代码在 JModelica 和 OpenModelica 以及现在也可以在 Dymola 中运行。

对已更正的代码错误的一些评论。

  1. 在模型测试中,我习惯于制作感兴趣的媒介的实例。尽管当前版本的 JModelica 和 OpenModelica 支持它,但实际上不允许(也不是很有意义)在 Modelica 中创建这样的包的实例。我做这个包 Medium 实例的原因有两个:

    1. 我通常需要在测试中(但在此示例中没有)可以访问中包中的信息来进行配置。例如,如果我将传感器连接到收获罐并且该传感器是普通类型的,那么我需要指定我想要测量的物质,最好使用包装介质中状态向量存储中物质位置的助记符来完成。我当然可以将配置所需的助记符一一导入,但使用 medium 包时更短,更易读。

    2. 从 FMU 能够访问中包中的信息是件好事。这个包可能不仅包含助记符,还包含我们在为 FMU 和交互式模拟设计定制的用户界面时想要使用的媒体的各种事实。这就是我在 Python 中使用 JModelica 所做的。这实际上很好用,就像现在使用 JModelica 和 PyFMI 一样,但我所学到的东西在 Modelica 中是被禁止的。

  2. 在几个地方,我将媒体 nc 中的组件数量传输到不同的设备模型。我在某种程度上直接使用连接器进行 nc 的传输并“测量”向量的大小。在编译时在 Modelica 中这样做是不行的。目前这在 JModelica 和 OpenModelica 中都有效,但不是在 Dymola 中。我通过在通用包 Equipment 中引入一个未指定的局部常量来解决此问题,但稍后当包适应应使用的介质时给出适当的数字。然后它得到值 medium.nc

我希望这些修改使代码更被普遍接受,并且至少适用于 JModelica、OpenModelica 和 Dymola。但是,我对这些解决方案不太满意,因为它没有解决我上面描述的基本用户需求。

此外,对于这个“解决方案”,“替代方案 2”——分两步(或更多步骤)调整库是不相关的——这毕竟是帖子中的关键问题。我将在稍后的新帖子中尝试用一个较小的示例重新提出这个问题。

在库 DEMO_v18_alt1 下方,然后是应用程序 d18_alt1_app7

    package DEMO_v18_alt1

        // 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;

        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_v18_alt1.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;

        model Reaction3
            constant Integer nc = 3;
            outer Real[nc] c;
            outer Real[nc] q;           
        equation
            q[1] = 0;
            q[2] = 0;
            q[3] =-c[3];
        end Reaction3;      

    //  ---------------------------------------------------------------------------------------------
    //     Equipment dependent on the medium  
    //  ---------------------------------------------------------------------------------------------

        package Equipment

            constant Integer nc;

            connector LiquidCon
                Real[nc] c                                         "Substance conc";
                flow Real F (unit="m3/s")                          "Flow rate";
            end LiquidCon;              

            replaceable model ReactionType = NoReaction            // Alternative 1
                constrainedby ReactionTypeInterface;               

            partial model ReactionTypeInterface                    // Alternative 1
                outer Real[nc] c, q;
            end ReactionTypeInterface;

            model NoReaction                                       // Alternative 1
                extends ReactionTypeInterface;          
            equation
                for i in 1:nc loop
                    q[i] = 0;
                end for;
            end NoReaction;             

            model PumpType
                LiquidCon inlet, outlet;                                                     
                RealInput Fsp;
            equation
                inlet.F = Fsp;                                         
                connect(outlet, inlet);                          
            end PumpType;

            model FeedtankType
                LiquidCon outlet;                                  
                parameter Real[nc] c_in (each unit="kg/m3") 
                                = {1.0*k for k in 1: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:nc loop
                    outlet.c[i] = c_in[i];
                end for;
                der(V) = outlet.F;               
            end FeedtankType;

            model HarvesttankType
                // Connection to reaction
                // replaceable model ReactionType = NoReaction constrainedby ReactionTypeInterface;   // Alternative 2

                ReactionType reaction;
                inner Real[nc] c                            "Substance conc";
                inner Real[nc] q                            "Reaction rate";        

                LiquidCon inlet, port;                                   
                parameter Real V_0 (unit="m3") = 1.0   "Initial harvest liquid volume";
                parameter Real[nc] m_0 
                      (each unit="kg/m3") = zeros(nc)       "Initial substance mass";
                Real[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:nc loop
                    der(m[i]) = inlet.c[i]*inlet.F + q[i];
                    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 library for the actual culture and media
    //  ---------------------------------------------------------------------------------------------

        package Equipment3                                         // Alternative 1
            import DEMO_v18_alt1.Equipment;
            extends Equipment(nc=Medium3.nc,
                              redeclare model ReactionType=Reaction3);
        end Equipment3;

    //  package Equipment3                                         // Alternative 2
    //      import DEMO_v18_alt2.Equipment;
    //      extends Equipment(nc=Medium3.nc);
    //  end Equipment3;

    //  model HarvesttankType3
    //      import DEMO_v18_alt2.Equipment3.HarvesttankType;
    //      extends HarvesttankType(redeclare model ReactionType=Reaction3);
    //  end HarvesttankType3;

    //  ---------------------------------------------------------------------------------------------
    //     Examples of systems 
    //  ---------------------------------------------------------------------------------------------

        model Test
            package medium = DEMO_v18_alt1.Medium3;                // Not accessible in FMU though
            Equipment3.FeedtankType feedtank;
            Equipment3.HarvesttankType harvesttank;                // Alternative 1 
    //      HarvesttankType3 harvesttank;                          // Alternative 2
            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_v18_alt1;

这里是应用程序代码

    encapsulated package d18_alt1_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_v18_alt1.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;

        model Reaction7
            constant Integer nc = 7;
            outer Real[nc] c;
            outer Real[nc] q;           
        equation
            q[1] = 0;
            q[2] = 0;
            q[3] = 0;
            q[4] = 0;       
            q[5] = 0;               
            q[6] = 0;       
            q[7] =-c[7];
        end Reaction7;  

    //  ---------------------------------------------------------------------------------------------
    //     Adaptation of library DEMO_v18_alt1 to Medium7 and Reaction7  
    //  ---------------------------------------------------------------------------------------------

        package Equipment7
            import DEMO_v18_alt1.Equipment;
            extends Equipment(nc=Medium7.nc,
                              redeclare model ReactionType=Reaction7);
        end Equipment7;

    //  ---------------------------------------------------------------------------------------------       
    //     Examples of systems 
    //  ---------------------------------------------------------------------------------------------

        import DEMO_v18_alt1.Control;

        model Test
            package medium = d18_alt1_app7.Medium7;                // Not accessible in FMU though
            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 d18_alt1_app7;
于 2019-05-14T12:10:22.187 回答