I define
type Enum = enumeration(A, B, C);
and
partial model PM
parameter Real[Enum] a;
...
end PM;
and
model M
extends PM(a = {1, 2, 3});
...
end M;
which gives an error. My best guess is that the a = {1, 2, 3} is the culprit. Is this indeed illegal, and if so, how can I set a in M?
UPDATE
Upon further examination I've found that everything above is indeed legal. Let me just go the whole hog:
type Enum = enumeration(A, B, C);
connector Conn
Real[Enum] a;
flow Real[Enum] f;
end Conn;
model M1
Conn conn;
equation
conn.f = -conn.a;
end M1;
model M2
Conn conn;
initial equation
conn.a = {1, 2, 3};
equation
der(conn.a) = conn.f;
end M2;
model M3
M1 m1;
M2 m2;
equation
connect(m1.conn, m2.conn);
end M3;
This fails to build. But if I just change each Enum in the connector class to 3, then it works.
The example is (as far as I can tell) legal, and some tools already handle it (assuming there is nothing else hidden).
Since you need a tool-specific work-around you need to specify which tool.
The legality will be clarified further in later versions of the Modelica specification since it was unclear - https://trac.modelica.org/Modelica/ticket/2212#comment:3
Related
I have a set of nested models and sub-models, as well as records. Here is the simple example:
package Unnamed1
record A
Real a;
end A;
record B
extends A;
Real b;
end B;
record C
extends B;
Real c;
end C;
model M
replaceable record Data = A;
parameter Data data;
end M;
model H
replaceable record Data = A;
parameter Data data;
protected
M m(redeclare replaceable record Data = Data, data=data);
end H;
model R
parameter B bb(b=5, a=2.0);
parameter A aa(a=4.0);
parameter C cc(a=0.0, b=0.2, c=0.8);
H h(redeclare record Data = B, data=bb);
H g(redeclare record Data = A, data = aa);
H f(redeclare record Data = C, data = cc);
end R;
end Unnamed1;
They are 3 sets of nested records (A, B, C), and 3 nested models (R, H, M). I want to pass an instance of a record from R to M through H, with instances having different types. That way of doing I found sounds heavy, passing both type of record AND its content through a different variable. When I have 2 levels, no problem, I can use redeclare that is fine. When I have more than two levels of nested classes (let say 5), I don't know how to pass both Type and instance at the same time. Note, in H, m is protected.
Any though on doing in a different way ?
One solution I found is by passing a record encapsulating an instance of record (called component in Modelica terminology). The new code is:
package Unnamed1
record A
Real a=0.0; // value for Dymola... don't know why yet
end A;
record B
extends A;
Real b;
end B;
record C
extends B;
Real c;
end C;
model M
replaceable record Data = A;
Data data;
end M;
model H
replaceable record Data = A;
M m(redeclare record Data = Data);
end H;
model R
record AA
extends A(a=5);
end AA;
record BB
extends B(b=5, a=2.0);
end BB;
record CC
extends C(a=0.0, b=0.2, c=0.8);
end CC;
H f(redeclare record Data = AA);
H g(redeclare record Data = BB);
H h(redeclare record Data = CC);
end R;
end Unnamed1;
The main difference being instead of passing a component B parameter B bb(b=5, a=2.0); to the various sub-models, one passes a record BB
containing values of B
record BB
extends B(b=5, a=2.0);
end BB;
One then have to redeclare the record in the sub-classe definition, which can be repetated again and again.
Dummy values have to defined in the base record A, let say a=0.0 for Dymola to accept the code, while OpenModelica and SimulationX does not need it.
I want to present a slight variation of the other answer and then explain how it is often solved.
The variation is that you can modify value and class in the redeclare without having explicit record declarations.
package Unnamed1
record A
parameter Real a;
end A;
record B
extends A;
parameter Real b;
end B;
record C
extends B;
parameter Real c;
end C;
model M
replaceable record Data = A;
parameter Data data;
end M;
model H
replaceable record Data = A;
protected
M m(redeclare replaceable record Data = Data);
end H;
model R
H h(redeclare record Data = B(b=5,a=2.0));
H g(redeclare record Data = A(a=4.0));
H f(redeclare record Data = C(a=0, b=0.2, c=0.8));
end R;
end Unnamed1;
However, when looking inside H we see that even if the model is legal the new parameters cannot be used. Thus a more normal case is that it is a model with some parameters in it leading to:
package Unnamed2
partial model SO
output Real y;
end SO;
model Constant
extends SO;
parameter Real b;
equation
y=b;
end Constant;
model Ramp
extends SO;
parameter Real b;
parameter Real k;
equation
y=b+time*k;
end Ramp;
model Other
extends SO;
parameter Real offset;
equation
y=time+offset;
end Other;
model M
replaceable model Source = SO;
Source source;
protected
Real x;
equation
der(x)=source.y;
end M;
model H
replaceable model Source = SO;
protected
M m(redeclare replaceable model Source = Source);
end H;
model R
H h(redeclare model Source = Constant(b=5));
H g(redeclare model Source = Other(offset=4.0));
H f(redeclare model Source = Ramp(b=0.2, k=0.8));
end R;
end Unnamed2;
I've been trying to plot a graph of function f(x) on interval 0,6. For some reason maple doesn't plot the graph when I use 'f(x)' as an argument. It works fine when I substitute assigned function as an argument. What can be the reason? How can I plot it using 'f(x)' as an argument?
My code is as follows and the error is on the pictures:
mystep:=proc(x,a,b,c)
if x<a
then
b;
else
c;
end if:
end proc:
f(x):=mystep(x,3,-2,7);
plot('f(x)', x=0..6);
enter image description here
Your syntax for creation of the operator (that you hope to assign to f) is incorrect for 1D plaintext input.
Here is the usual syntax for creation of such an operator, and assignment to name f.
restart;
mystep:=proc(x,a,b,c)
if x<a then b; else c; end if:
end proc:
f:=x->mystep(x,3,-2,7);
These should now all work as you expect.
plot('f(x)', x=0..6);
plot(f, 0..6);
plot(x->mystep(x,3,-2,7), 0..6);
In recent versions of Maple the syntax that you used can work in (only) 2D Input mode. But it is a poor syntax to use, since is easily confused with the 1D syntax for assigning into the remember table of an operator (and with even more confusion ensuing). You should avoid ambiguous syntax.
The type of function you have is piecewise-defined function (see this wikipedia page https://en.wikipedia.org/wiki/Piecewise). And in Maple there is already a command for defining this type of a function, piecewise, see its help page for a complete explanation of how to use it. Now for your mysetup, you have a condition x < a, and a value for when this happens, b, and a value for otherwise, c. So you want piecewise( x < a, b, c ). I think it is better to just use this command, but if it is really necessary to define a new procedure, then your mysetup becomes like the following.
mystep := proc(x, a, b, c)
return( piecewise( x < a, b, c ) ):
end proc:
Now for your plot you can use either
plot( piecewise( x < 3, -2, 7 ), x = 0..6 );
or
f(x) := mystep(x, 3, -2, 7);
plot( f(x), x = 0..6);
In Modelica, I'm able to access the contents of a record instance like so:
model Unnamed1
record Example
parameter Real x = 5;
end Example;
Example ex;
Real test;
equation
test = ex.x;
end Unnamed1;
However, I'd like to access the contents of the record without declaring an instance of the record, like so:
model Unnamed1
record Example
parameter Real x = 5;
end Example;
Real test;
equation
test = Example().x;
end Unnamed1;
...but this doesn't work. Is there some way to achieve what I'm trying to do?
Yes, it is possible without having an actual instance in the model but it requires some extra code.
model Unnamed1
record Example
parameter Real x = 5;
end Example;
function getX
input Example r;
output Real x;
algorithm
x:=r.x;
end getX;
Real test;
equation
test = getX(Example());
end Unnamed1;
(I'm aware that it is cheating by having the instance in the function, but....)
Another option is
for r in {Example()} loop
test=r.x;
end for;
(allowed according to https://github.com/modelica/ModelicaSpecification/issues/1521 )
This is not possible (even from a grammar point of view). The right hand side of dot (.) needs to be a class or component reference. You can only access constants/parameters in packages via the dot notation.
package X
constant Real x = 1;
end X;
model M
Real x = X.x
end M;
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;
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;