Member not replaced as expected, why? - modelica

Aims: all the derived classes should inherit some default equations from their base class. When the default equation is not valid for a derived class then it should redeclare it.
Here is a somewhat silly minimalistic example.
package Pkg
class Equations
Real x;
end Equations;
class DefaultEquations
extends Equations;
equation
x = 0.0;
end DefaultEquations;
class Base
replaceable DefaultEquations equations extends Equations;
end Base;
end Pkg;
model DuplicateEquations
import Pkg.*;
class CustomizedClass
extends Base;
redeclare Equations equations;
equation
equations.x = 3;
end CustomizedClass;
CustomizedClass customized;
end DuplicateEquations;
For some mysterious reason, the default equation is not overriden:
omc Test.mo Package.mo
class DuplicateEquations
Real customized.equations.x;
equation
customized.equations.x = 0.0;
customized.equations.x = 3.0;
end DuplicateEquations;
Why is this happening? Why are both x=0 and x=3 generated?
If I comment out the package declaration I get only the expected x=3 equation.

The type has to be redeclared, not the component, as pointed out by Adrian Pop.

Related

How to parametrise a redeclaration?

I have a package for representing certain media, built similar to but not exactly like Modelica.Media. I want to allow the user to choose between different functions for a particular property without having to change the way the remaining properties are calculated.
The package is:
package myPac
replaceable function prop = F1 constrainedby partialF
annotation (choicesAllMatching=true);
function partialF
input Real x;
output Real y;
end partialF;
function F1
extends partialF;
algorithm
y := x;
end F1;
function F2
extends partialF;
algorithm
y := 2*x;
end F2;
end myPac;
I want to then choose F1 or F2 from inside a model.
model myModel
replaceable package pack1 = myPac(redeclare function prop = pack1.F2);
Real x;
Real y;
equation
y = pack1.prop(x);
end myModel;
works as expected. How can I parametrise the redeclaration of prop so that it can be set from the parameters dialog box?
I was looking of something like:
model myModel2
replaceable package pack1 = myPac(redeclare function prop = if a == 1 then pack1.F1 else pack1.F2);
parameter Integer a = 1;
Real x;
Real y;
equation
y = pack1.prop(x);
end myModel2;
which is clearly incorrect syntax. Also, the choicesAllMatching=true in the declaration of prop does show me a drop-down menu, but there are no choices listed.
How can I parametrise the redeclaration of prop so that it can be set
from the parameters dialog box?
You don't need the additional parameter a to select the functions. Simply click the edit button of prop to get the parameter window of the selected replaceable package, where you can choose between F1 and F2.
I added an animation below to make this clearer.
Also, the choicesAllMatching=true in the declaration of prop does show
me a drop-down menu, but there are no choices listed.
This works for me, as you can see in the animation. Note that I declared the function partialF as partial, so it is not included in the drop-down menu. And I added another choicesAllMatching annotation to the replaceable package pack1 in myModel to create another drop-down for the selection of pack1 (like Hans did in his answer).
You cannot currently have redeclarations depending on parameter-values.
However, you can make a choice between two packages that will appear in the dialog box:
package myPac
replaceable function prop = F1 constrainedby partialF
annotation (choicesAllMatching=true);
function partialF
input Real x;
output Real y;
end partialF;
function F1
extends partialF;
algorithm
y := x;
end F1;
function F2
extends partialF;
algorithm
y := 2*x;
end F2;
end myPac;
package myPac1=myPac(redeclare function prop=myPac.F1);
package myPac2=myPac(redeclare function prop=myPac.F2);
model myModel
replaceable package pack1 = myPac annotation(choicesAllMatching=true);
Real x;
Real y;
equation
y = pack1.prop(x);
end myModel;

Parametrised Modelica library and possibility to use models as parameters

I try to make a parametrised library. I works fine using packages and connectors as parameters.
Using a model as a parameter is also possible.
However, if the model is used in the library to build new models using extend, then it is not allowed, what I understand.
I wonder if the library contains a model with inner/outer style of connector, is it then allowed to let the inner model to be a parameter of the library?
Below a simple example to illustrate the problem. TEST is the library and Fish3b is an application. When I run the Example in the library TEST it all works, but when I have a separate application file it does not.
The error text is: cannot find class declaration for AquariumType running JModelica 2.4
package TEST
model FishType1
outer Real T;
Real health;
equation
health = 30-T;
end FishType1;
model FishType2
outer Real T;
Real health;
equation
health = 32-T;
end FishType2;
package Equipment
model AquariumType
replaceable model FishType
end FishType;
FishType fish;
inner Real T;
equation
T = 29;
end AquariumType;
end Equipment;
// Adapt AquariumType model to actual fish
model Aquarium
import TEST.Equipment.AquariumType;
extends AquariumType(redeclare model FishType=FishType2);
end Aquarium;
// Example
model Example
Aquarium aquarium;
end Example;
end TEST;
And below an example of application code that import from library above
- and here is some error I think.
encapsulated package Fish3b
model FishType3
outer Real T;
Real health;
equation
health = 34-T;
end FishType3;
// Adapt package Equipment with AquariumType model to actual fish
package Equipment3
import TEST.Equipment;
extends Equipment.AquariumType(redeclare model FishType=FishType3);
end Equipment3;
// Example
model Example
import Fish3b.Equipment3;
Equipment3.AquariumType aquarium;
end Example;
end Fish3b;
Thanks “tbeu” for the comment!
I have modified the code so that package Equipment is not extended from a model. The updated code below also represent my underlying “true” problem much better. And it all works.
Thanks!
The updated library file TEST2.mo:
package TEST2
model FishType1
outer Real T;
Real health;
equation
health = 30-T;
end FishType1;
model FishType2
outer Real T;
Real health;
equation
health = 32-T;
end FishType2;
package Equipment
replaceable model FishType
end FishType;
constant Integer dummy = 1;
model AquariumType
FishType fish;
inner Real T;
equation
T = 29;
end AquariumType;
end Equipment;
// Adapt package Equipment to the actual fish
package Equipment1
import TEST2.Equipment;
extends Equipment(redeclare model FishType=FishType1);
end Equipment1;
// Example
model Example
Equipment1.AquariumType aquarium;
end Example;
end TEST2;
And the application code T2_Fish3 that now uses the above library TEST2:
encapsulated package T2_Fish3
model FishType3
outer Real T;
Real health;
equation
health = 34-T;
end FishType3;
// Adapt package Equipment to the actual fish
package Equipment3
import TEST2.Equipment;
extends Equipment(redeclare model FishType=FishType3);
end Equipment3;
// Example
model Example
Equipment3.AquariumType aquarium;
end Example;
end T2_Fish3;
The answer from janpeter works.
Another alternative that avoids introducing models called "FishType1", "FishType3" etc is to use "redeclare model extends" as follows (the Test2 can be unchanged or same change for Equipment1), but it uses more advanced constructs.
encapsulated package T2_Fish3
// Adapt package Equipment to the actual fish
package Equipment3
import TEST2.Equipment;
extends Equipment;
redeclare model extends FishType
outer Real T;
Real health;
equation
health = 32-T;
end FishType;
end Equipment3;
// Example
model Example
Equipment3.AquariumType aquarium;
end Example;
end T2_Fish3;
Additionally it would be possible to move the common "outer Real T" to the base-model FishType leading to:
package TEST2
package Equipment
replaceable model FishType
outer Real T;
end FishType;
constant Integer dummy = 1;
model AquariumType
FishType fish;
inner Real T;
equation
T = 29;
end AquariumType;
end Equipment;
// Adapt package Equipment to the actual fish
package Equipment1
import TEST2.Equipment;
extends Equipment;
redeclare model extends FishType
Real health;
equation
health = 30 - T;
end FishType;
end Equipment1;
// Example
model Example
Equipment1.AquariumType aquarium;
end Example;
end TEST2;
and
encapsulated package T2_Fish3
// Adapt package Equipment to the actual fish
package Equipment3
import TEST2.Equipment;
extends Equipment;
redeclare model extends FishType
Real health;
equation
health = 32-T;
end FishType;
end Equipment3;
// Example
model Example
Equipment3.AquariumType aquarium;
end Example;
end T2_Fish3;
Updated the code with input from Hans Olsson before, but slightly different with the corrections suggested above.
And I manage to avoid “extend from a replaceable model”, but I am not too sure how critical that is, see my previous comment.
Also, I think I would like to keep the clear identity of the different fishes, and also declare them outside package Equipment.
package TEST3
partial model FishBase
outer Real T;
end FishBase;
model FishType1
extends FishBase;
Real health;
equation
health = 30 - T;
end FishType1;
model FishType2
extends FishBase;
Real health;
equation
health = 32 - T;
end FishType2;
package Equipment
replaceable model FishType
end FishType;
constant Integer dummy = 1;
model AquariumType
FishType fish;
inner Real T;
equation
T = 29;
end AquariumType;
end Equipment;
// Adapt package Equipment to the actual fish
package Equipment3
import TEST3.Equipment;
extends Equipment;
redeclare model FishType=FishType2;
end Equipment3;
// Example
model Example
Equipment3.AquariumType aquarium;
end Example;
end TEST3;
And an example of application code:
encapsulated package T3_Fish3
model FishType3
import TEST3.FishBase;
extends FishBase;
Real health;
equation
health = 34-T;
end FishType3;
// Adapt package Equipment to the actual fish
package Equipment3
import TEST3.Equipment;
extends Equipment(redeclare model FishType=FishType3);
end Equipment3;
// Example
model Example
Equipment3.AquariumType aquarium;
end Example;
end T3_Fish3;

Replaceable function and function calls from strings

The following three questions are tied together so please forgive the length of the post.
Using Dymola 2016.
Using a replaceable function call within a model provides the opportunity for the user to have the drop down options. Example below:
model Test1
parameter Real x = 1;
Real y;
replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
equation
y = a(x);
end Test1;
Doing the same replaceable function call within a function seems to not permit the same drop down functionality with the the function is called (i.e. right click call function in package browser. I assume this is intentional as a function is typically called within other functions/models. Example below:
function Test2
input Real x;
output Real y;
replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
algorithm
y :=a(x);
end Test2;
Question #1. Is it possible to use a replaceable function call within a function in the same way you do a model? If so, what is the appropriate syntax? Alternative approach?
Alternatively, a different option would be to perform the replaceable function call in the model and then pass the result to another function that then makes the appropriate call. Example shown below:
model Test3mod
parameter Real x = 1;
Real y;
replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
equation
y = Test3func(x,a);
end Test3mod;
Which passes parameter x and function handle a to:
function Test3func
input Real x;
input ???? a;
output Real y;
algorithm
y :=a(x);
end Test3func;
Question #2. Is this allowable in Modelica and if so, how? Alternative approach?
Question #3. Is it possible to define a string and turn that into a the name of a function. Example below:
model Test4
parameter String 'functionname';
parameter Real x = 1;
Real y;
equation
y = functionname(x);
end Test4;
Thank you in advance! I appreciate your feedback as I continue to explore the use of Modelica.
This should work fine:
model Test3mod
parameter Real x = 1;
Real y;
replaceable function a=b constrainedby d annotation(choicesAllMatching=true);
equation
y = Test3Func(x,a);
end blah;
function Test3func
input Real x;
input d f;
output Real y;
algorithm
y := f(x);
end Test3func;

Modelica Class Method

I would like to use a class function/method in my Modelica model as follows:
optimization Moo(objective=-x(finalTime), startTime = 0, finalTime = 12)
parameter Real e = 0.05;
Real x(start=2, fixed=true, min=0, max=100);
input Real v (min=0, max=1);
function omega
input Real t;
output Real y;
algorithm
y := e;
end omega;
equation
der(x) = v*omega(time);
constraint
v<=1;
end Moo;
I would like the variable e in the function omega to be a variable so that I can easily change its value at a later point in time when I am doing a parameter sweep. Unfortunately, the function omega does not seem to know about the variable e and the JModelica compiler returns the error:
Cannot find class or component declaration for e
I would naïvely expect that since omega and e belong to the same class omega would be able to see e.
Is there a way to achieve this?
Member functions are not supported in Modelica, so a function declared inside a model acts like a standalone function without having access to the surrounding model variables.
Member functions are not allowed due to functions need to be pure, i.e. they are not allowed to have any side effect. This is a fundamental assumption in Modelica that makes it possible for a tool to apply symbolic transformation and rearrange calculations.
You can have something like a member function, if you explicitly pass the needed variables as additional input to the function. See this example:
package MemberFunction
model A
Real x=1;
function get_x = MemberFunction.get(u=x);
end A;
function get
input Real u;
output Real y;
algorithm
y := u;
end get;
model Test
A a;
Real x;
equation
x = a.get_x();
end Test;
end MemberFunction;

Modelica C++ templates

Does Modelica have something equivalent to C++ templates? I would like to build a class that would process an input of type T, but T would be known only when instantiating the class. I tried to pass the type as a parameter but it gives errors.
You can use replaceable types/classes.
model M
replaceable class C; // = some partial class if you need an interface constrainedby ...
C c;
end M;
model Test
M m1(redeclare class C = C1);
M m2(redeclare class C = C2);
end Test;
See more in the Modelica Specification 4.5 Class declarations https://modelica.org/documents/ModelicaSpec32Revision2.pdf.