- Index
- » Programming
- » Modelica Language
- » Modelica variable behavior
Modelica variable behavior
Modelica variable behavior
Hello,
i've got a problem with variables in an algorithm section that are depending on the simulation time. At an event I want to set the current simulation time to a variable, which I want to use in the next cycles. But this variable resets to 0 at the next step. I tried to initialize the variable at the beginning of a cycle with "var := pre(var)", which led to errors during compilation.
The following code section contains a minimal example of the problem. If you compare the variables "state" and "timeVar" you can see that "state" changes its value and holds it between cycles but timeVar is always 0.
Code:
model TimeVarTest
Modelica.SIunits.Time timeVar(start = 0);
Modelica.SIunits.Time timeOut(start = 0);
Modelica.Blocks.Tables.CombiTable1Ds tableVar(table = [0,0;1,0.5]);
Integer state(start = 0);
algorithm
if state == 0 then
// Action
tableVar.u := time-timeVar;
// Transition
if time - timeVar > 1 then
state := 1;
timeVar := time;
end if;
end if;
if state == 1 then
// Action
tableVar.u := 1;
// Transition
if time - timeVar > 2 then
state := 0;
timeVar := time;
end if;
end if;
equation
timeOut = tableVar.y[1];
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.1));
end TimeVarTest;
Im using windows 10 and OpenModelica v1.12.0 (64-bit).
Thank you very much for your help,
Leo
- Leonard
- 3 Posts
Re: Modelica variable behavior
Yes, if an algorithm section assigns to a variable, this variable is always re-initialized at every entry of the algorithm section according to the Modelica specification. For non-discrete variables this is the start-value, for discrete variables it is pre(v). If you simply declare "discrete Modelica.SIunits.Time timeVar(start = 0);" I think you get the behavior that you are after (or perhaps some more variables need to be declared discrete).
- sjoelund.se
- 1700 Posts
Re: Modelica variable behavior
Thank you!
I was looking for the chapter in which the variable initialization description is written, but i couldn't really find it.
In the actual Modelica Specification (3.4), I can read the following.
Chapter 8.4. point 1: "All variables keep their actual values until these values are explicitly changed. Variable values can be
accessed at any time instant during continuous integration and at event instants."
and
Chapter 8.6. point 2: "For all non-discrete (that is continuous-time) Real variables vc, the equation pre(vc) = vc is added to the
initialization equations."
Which tells me the opposite of the expected behavior.
I really have got my problems with Modelica and I think that I maybe didn't understand some fundamental aspects of the language. But those descriptions make it hard to understand how the language works, because they can be understood as a contradiction to actual behavior.
I've got a question to the quotes i listed. Are those quotes referring to the problem I had the with initialization of discrete and continous variables or do they describe something else?
Best regards,
Leonard
- Leonard
- 3 Posts
Re: Modelica variable behavior
You are changing the values according to 11.1.2 Execution of an algorithm in a model And the rules for initialization only apply before the first step.
- sjoelund.se
- 1700 Posts
- Index
- » Programming
- » Modelica Language
- » Modelica variable behavior