The Modelica language support type parametrisation of packages (and classes in general) and has done that for att least 7-8 years, perhaps since conception. However, not all Modelica-implementations has a GUI that supports this flexibility though. The following example illustrate the problem. The example was introduced in a recent post and shown again, and now extended with an extra package to handle these different Modelica-GUI-implementations. (GUI annotations omitted).
package DEMO_v82
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_central
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_central;
package A_generic_local
model M1
replaceable package Medium = MediumBase
constrainedby MediumBase;
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_local;
package Medium2
import DEMO_v82.MediumBase;
extends MediumBase(nc=2);
end Medium2;
package A_specific
import DEMO_v82.A_generic_central;
extends A_generic_central(redeclare package Medium = Medium2);
end A_specific;
model TEST1
A_specific.M1 m1;
A_specific.M2 m2;
equation
connect(m2.y, m1.u);
end TEST1;
model TEST2
A_generic_local.M1 m1(redeclare package Medium=Medium2);
A_generic_local.M2 m2;
equation
connect(m2.y, m1.u);
end TEST2;
end DEMO_v82;
The approach here to the problem to make a library that works for both types of GUIs, is to simply add a parallel package A_generic_local that for all components include a "locally" defined replaceable package Medium. Then components from this package can then be dragged out and connected to TEST2 (which they could not from A_specific). However, we need (in principle) to go through each component one-by-one and redeclare Medium=Medium2. (In this example only M1 use Medium but you typically have a number of components in a configuration and many of them use Medium).
Configuration like TEST1 can be done with Dymola GUI and SystemModeler GUI, but cannot be done with GUI using OpenModelica and some other implementations (see previous post), while configuration like TEST2 can be done with also OpenModelica GUI and hopefully some others too, as seen is the screenshot below.
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.
Questions:
I wish A_generic_local did not need to repeat all code of A_generic_central as well as icons, but that seems not possible.
I wish here was a way to avoid the need to go through each component and set Medium=Medium2. Is there?
Since in my eyes the configuration TEST1 is simpler and I tend to prefer over TEST2 while in practice the pattern in TEST2 seems to be commonly used (cf MSL Fluid/Media), I wonder why is it so? Is here some obvious draw-backs with TEST1?