如何在OpenModelica中设置包括扩展/重新声明在内的库应用程序?

时间:2019-03-08 09:28:38

标签: openmodelica

我将Modelica代码划分在一个小的库DEMO_v11.mo和一个应用程序D11_APP7.mo中。该应用程序代码包括一些部分,这些部分使用以下代码使该库的接口适应该应用程序:import-extend-redeclare。所有这些都可以在JModelica中使用。现在,我想在OpenModelica中进行设置,但是我不知道如何处理两个不同的文件。浏览文档时,我几乎找不到帮助。

我以前设法引入了一个库,并在OpenModelica中添加了图形符号,然后根据该库中的组件构建了一个新模型。 但是,现在我需要做一个更“高级”的导入,以扩展-重新声明导入的模型。因此,我的问题是如何做得更“高级”。

赞赏一些建议或关于在哪里读书的建议。

1 个答案:

答案 0 :(得分:0)

对于我发现的这个问题,答案既简单又困难。

最简单的部分是,您应该在OpenModelica中使用命令“文件/打开Modelica /库文件”以相同的方式加载库和应用程序代码。然后,库和应用程序并排登陆。它们的图标显示在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;