I want to find ways to re-use code with a minimum of code changes. When structuring libraries you can use packages and use 'import' of a package to bring it in to a specific model. Another way is the command 'within' to make references shorter and bring convenience. However in OpenModelica the use of 'within' does work when use of OMEdit for simulation but the model cannot be exported as FMU. In JModelica the use of 'within' does not to work in the example below. What is the proper Modelica usage to conveniently access a package?
The minimal example below illustrates the problem. First a package DEMO_v90.mo that contains both a package A and a models M1 and M2 - all in one file. Works fine.
package DEMO_v90
package A
constant Real a=1;
constant Real b=2;
constant Real c=3;
end A;
model M1
replaceable package A = DEMO_v90.A;
Real x, y, z;
equation
x = A.a;
y = A.b;
z = A.c;
end M1;
model M2
replaceable package A = DEMO_v90.A;
Real x, y, z;
equation
x = 2*A.a;
y = 2*A.b;
z = 2*A.c;
end M2;
end DEMO_v90;
Typically we would like to make a configuration using the content of DEMO_v90 in a separate file. I have two ways to do that D90_1 and D90_2. Here the configuration only means to make an instance of M1 and M2 and no actual further configuration, to keep it simple.
In D90_1 we import DEMO_v90 and make the instances. Note that we need to spell out DEMO_v90 five times. This is however a way that works well in both OpenModelica and JModelica.
model D90_1
import DEMO_v90;
DEMO_v90.M1 m1(redeclare package A = DEMO_v90.A);
DEMO_v90.M2 m2(redeclare package A = DEMO_v90.A);
end D90_1;
In D90_2 we use the concept within DEMO_v90 instead. In this way we do need to refer to DEMO_v90 only once. This is an advantage since we may want to re-use the configuration D90_2 with some other package, say DEMO_v91 and then only need to change at one place.
within DEMO_v90;
model D90_2
M1 m1(redeclare package A = A);
M2 m2(redeclare package A = A);
end D90_2;
The D90_2 code do simulate in OpenModelica, but you cannot export an FMU. For JModelica it does not work at all. The error text from OpenModelica is as follows.
We also see that when we use GUI and provide icons for M1 and M2 then when we choose to access parameters we have a more clear display of what package A is used for method 1) compared to method 2). (The code equipped with icons is called 91 instead of 90, and to the left 91_1 and to the right 91_2)
To sum up - the questions are:
Regarding 1:
The within
-clause is only intended to ensure that source-code files haven't been moved by accident in the file-system - using them to add contents inside packages is not standard Modelica.
So, the bug is in OpenModelica's translation, whereas the FMU-export is ok.
(And see answer by @marco for a better solution.)