Search code examples
modelicaopenmodelicadymolasystemmodelergraphical-interaction

Gap GUI and code configuration for parametrised packages?


It is very practical to parametrise packages to improve reusability of code and a central theme of Modelica. The language support since long time back to also use types like packages and models as parameters for packages. The concept is sometimes called "type formal parameter" and described in Fritzson's book (2015) section 4.4 and related to Modelica specification section 7.

I have recently found it difficult to use GUI in OpenModelica to handle this parametrisation of packages, and first contacts indicates that also Dymola and Impact have similar shortcomings. It all boils down to how "class extends" is handled by the GUI. The code example below illustrate the problem together with the screenshot of the GUI further down.

 package DEMO_v80

     partial package MediumBase
        constant Integer nc = 0;
         replaceable type C = Real[nc];     
     end MediumBase;

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

     package A_generic
         replaceable package Medium = MediumBase
             constrainedby MediumBase;

         model M1
             RealInput u;
             Medium.C c (each start=1, each fixed=true);
         equation
             for i in 1:Medium.nc loop
                 der(c[i]) = u*c[i];
             end for;
         end M1;
    
         model M2
             RealOutput y;
         equation
             y = -1;
         end M2;    
     end A_generic;

     package Medium2
         import DEMO_v80.MediumBase;
         extends MediumBase(nc=2);
     end Medium2;

     package A  
         import DEMO_v80.A_generic;
         extends A_generic(redeclare package Medium = Medium2);
     end A;

    model TEST
         A.M1 m1;
         A.M2 m2;
     equation
         connect(m2.y, m1.u);
     end TEST;  

 end DEMO_v80;

A screenshot of the OpenModelica library below shows that the package A_generic can be expanded while the specialised package A can not. I wish I could from A drag out models M1 and M2 and connect them instead of making the configuration by coding as in model TEST.

enter image description here

I can understand that the GUI should protect the package A from change of value of the the formal parameter, just given in the code. But It does not make sense to protect from access to the content of package A, i.e. model M1 and M2.

Is here some other way to implement the desired reusability of the code?

(And I want that all models in A_generic get the Medium by the value of the formal parameter).

Is this a problem not only for OpenModelica but also for Dymola, Impact etc?

Can perhaps someone briefly outline why GUI is difficult to implement the way I wish?

Note: This "gap" talked about here should be seen as a design-choice rather than a bug (and not very well documented for the user). I have some dialogue with people related to the different software discussed.


Solution

  • It works as you wish in Dymola (at least since Dymola 2016 FD01).

    Dymola's package browser for the example

    And dragging that M1 into the new model M gives (apart from annotations):

    model M
      DEMO_v80.A.M1 m1_1;
    end M;