I would like to have a package that extends from another basic package. The basic package contains a vector type and constant integers with name for each index number of the vector. It also contains a vector constant that gives each element a value (describing some property fo the element). In the extended package I would like to add one element of the vector type and add one new name for the new index, and also add to the vector constant one element with a specific value (describing some property of the substance).
Using the technique with replaceable - extend - redeclare it is straight forward to extend the vector type with an element and also append to the package a new constant integer for the for the appended element. But I am not sure how to append the constant vector with a new element.
The code below works in JModelica (2.4) but involves both redeclaration and copy of values from the original basic package. However in Medium3 JModelica does not accept a fourth redeclare statement for the constant vector mw. But if I instead make the redeclare statement as first line after extension is done, it does work (see Fritzson section 4.3.1). However, redeclaration should be a subtype of the original and Real3] is not a subtype of Real[2], but the compiler seems to manage anyway.
When I try the same code in OpenModelica (1.13) I get error message due to the fact that I redeclare a constant, and error already on the first redeclaration in Medium3. I am not sure this is a correct error message, and does not appear in JModelica.
Otherwise OpenModelica (and JModelica) accept both Medium2 without any warnings or errors. And these tests by just changing the medium used in LiquidCon.
My main question is if here is a more direct solution to do the extension of the medium package with one substance as described above, than my code, and that is more standard (and do work with both JModelica and OpenModelica and Modelica in general of course).
It is of course of interest to sort out what the Modelica standard say here and then we can bring that information to the organisations behind JModelica and OpenModelica as bug-reports.
Would appreciate your input / Jan Peter
Below an extract of package DEMO_v8
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_v8.Medium2;
extends M2
(redeclare constant String name="Three components" "Medium name",
redeclare constant Integer nc=3 "Number of substances",
redeclare type Concentration = Real[nc] "Substance conc");
redeclare constant Real[nc] mw = cat(1,M2.mw,{30}) "Substance weight";
constant Integer C = 3 "Substance index";
end Medium3;
connector LiquidCon
replaceable package medium=DEMO_v8.Medium3;
medium.Concentration c "Substance conc";
flow Real F (unit="m3/s") "Flow rate";
end LiquidCon;
You can (since Modelica Language 3.2 - it was illegal in 3.1) just modify the value of the constant as follows:
package Demo_v8
package Medium2
replaceable constant String name="Two components" "Medium name";
constant Integer nc=2 "Number of substances";
replaceable type Concentration = Real[nc] "Substance conc";
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_v8.Medium2;
extends M2(
name="Three components" "Medium name",
nc=3 "Number of substances",
mw=cat(1, M2.mw, {30}),
redeclare type Concentration = Real[nc] "Substance conc");
constant Integer C=3 "Substance index";
end Medium3;
connector LiquidCon
replaceable package medium = Demo_v8.Medium3;
medium.Concentration c "Substance conc";
flow Real F(unit="m3/s") "Flow rate";
end LiquidCon;
end Demo_v8;
However, I have not verified that JModelica.org or OpenModelica can handle it.
BTW: The error message is correct, as redeclaring a constant has been illegal since Modelica 1.2.