Search code examples
importpackagemodelica

What is the difference between declaring a package and importing a package in a Modelica model?


I could declare a package or import a package in Modelica models, but I am not sure if there is any difference between them, I tried the following code, both of them work fine. My question is : Is there anything I should pay attention to when using these two methods?

partial model A
  package SI1=Modelica.SIunits;
  import SI2=Modelica.SIunits;
  SI1.Voltage u1;
  SI2.Voltage u2;
end A;

enter image description here


Solution

  • You are doing two fundamentally different things here, which both work for this case:

    package SI1=Modelica.SIunits; is called a short class definition. You create a new package named SI1, which inherits everything from Modelica.SIunits.

    Short class definitions are basically the same as writing

    package SI1
      extends Modelica.SIunits;
    end SI1;
    

    See chapter 4.5.1 Short Class Definitions in the Modelica spec for details.

    import SI2=Modelica.SIunits on the other hand simply influences where the Modelica tool looks for class definitions - so no new class is defined here. The chapter 13.2.1.1 Lookup of Imported Names explains that in the Modelica spec.

    If you just want to use the package, import it. That's what import was designed for. Declaring a new package only makes sense if you want to add functionality or change anything (which is very limited though, if you are using the short class definition).