Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Behavior of algorithms in OpenModelica

Behavior of algorithms in OpenModelica

Hello all,

i am looking for a way to read csv-data into an array, and tried to use a loop in an algorithm section. While doing so i observed a kind of strange behavior, or, at least, i have some pain to see this as desirable semantics. I would like to illustrate what i mean with a simple model:

model Why
  Real x;
  Boolean stop;
algorithm
  if not stop then
    x:=x + 1;
    stop:=true;
  else

  end if;
end Why;

Running that model i would expect to see a value of x = 1 (since the algorithm should be executed exactly once). But the value was 0. (The assigned value of 1 had been discarded.) So, i would like to know whether anyone could explain this behavior.

(Aside from discussing this behavior i am still interested in a neat way to read in csv-data---i have tried this by using the reading and scanning functions in the Modelica.Utilities package in a loop, but in order to get this working, the read values would have to be remembered after the loop!)

--Paul

Edited by: paul - Jul-12-13 18:17:45

Re: Behavior of algorithms in OpenModelica

Oh, unfortunately nobody has something said yet. To me, the topic seems to be rather important. It just cannot see this behavior as specification compliant or even sensible. It could be that i am completely mistaken; in this case it would be nice to give me a hint. In the other case, i would be glad to see some statement from an OpenModelica developer, whether one could expect this behvior to be changed within one of the next builds.

--Paul

Re: Behavior of algorithms in OpenModelica

Hi,

Things are not very clear in the specification when it comes to first simulation step.
See the discussion here:
https://trac.modelica.org/Modelica/ticket/1195

Running your models with debugging of initialization gives me:

Code:


adrpo@ida-liu050 ~/dev/OpenModelicaNoChanges/build/bin
$ ./Why.exe -lv LOG_INIT
LOG_INIT          | info    | ### START INITIALIZATION ###
LOG_INIT          | info    | updating start-values
LOG_INIT          | info    | initialization method: symbolic        [solves the initialization problem symbolically - default]
LOG_SOTI          | info    | ### SOLUTION OF THE INITIALIZATION ###
|                 | |       | | states variables
|                 | |       | | | [1] Real $dummy(start=0, nominal=1) = 0 (pre: 0)
|                 | |       | | derivatives variables
|                 | |       | | | [2] Real der($dummy) = 0 (pre: 0)
|                 | |       | | other real variables
|                 | |       | | | [3] Real x(start=0, nominal=1) = 1 (pre: 0)
|                 | |       | | integer variables
|                 | |       | | boolean variables
|                 | |       | | | [1] Boolean stop(start=false) = true (pre: false)
|                 | |       | | string variables
LOG_INIT          | info    | ### END INITIALIZATION ###

Which means that at the start of simulation stop is true and x is 1 and the algorithm part is never ran.

I'll ask Lennart (one of our back-end/runtime system developers) to give more information about this issue.

Cheers,
Adrian Pop/

Re: Behavior of algorithms in OpenModelica

Hi Adrian,

thanks for your answer. Given that the initialization results in x == 1 and stop == true, and the algorithm part is never run, i wonder why at the end x == 0.

--Paul

Re: Behavior of algorithms in OpenModelica

Yes, it looks like a bug as far as I can tell (x should not be 0, especially if x = 1 from the initialization).
Lennart will have a closer look at it on Monday.

Cheers,
Adrian Pop/

Re: Behavior of algorithms in OpenModelica

Hi,

the issue here is the algorithm specification, which says that every continues variable on the left side, needs to intialized with it' s start value( in this case =0).
So after the intialization the stop variable is true, but the algorithm is executed anyway and while this exection x is set to it's start value ( = 0).
That's also the point were the spec is unclear, maybe we need to overwrite the startvalues with the initialize values.

For that specific model it world help to declare x as an discrete variable.

Code:


model Why
  discrete Real x;
  Boolean stop;
algorithm
  if not stop then
    x:=x + 1;
    stop:=true;
  else

  end if;
end Why;

so long.
Willi

Re: Behavior of algorithms in OpenModelica

Hi Willi,

thanks for your comment; the 'discrete'-prefix will probably help to solve my original problem.

However, once stumbled upon that topic i'm also curious what's going on with it. I fear i'm missing something. The first thing is, why are 'stop' and 'x' initialized as they are (true and 1)? And then, even more intriguing, if it is so, why the 'if'-part is executed anyway (as you say)? Given that everything is as said, and 'x' is set to 0 at the beginning of the execution of the algorithm, then still 1 should be added later on, shouldn't it?

One more thing: what's the difference between start- and initialize-values?

Ok, these are quite a few questions, but the topic is still heavily obscure to me.

--Paul

Re: Behavior of algorithms in OpenModelica

Hey Paul,

paul wrote:


The first thing is, why are 'stop' and 'x' initialized as they are (true and 1)?

This model has no initial equations, so the initialization step basically just
execute the model equation once and the result of that is (true and 1).

paul wrote:


And then, even more intriguing, if it is so, why the 'if'-part is executed anyway (as you say)?
Given that everything is as said, and 'x' is set to 0 at the beginning of the execution of the algorithm, then still 1 should be added later on, shouldn't it?

In modelica an algorithm is basically just a function call with (lhs1,lhs2,...) := algorithm_function(nonLhs1,nonLhs2,...).
So the not the 'if'-part is execute instead the whole algorithm_function is executed and the modelica specification
say in "11.1.2 Execution of an algorithm in a model" how variables are initialized when an algorithm
is executed.

paul wrote:


Given that everything is as said, and 'x' is set to 0 at the beginning of the execution of the algorithm, then still 1 should be added later on, shouldn't it?

No, since the variable stop is true, so the true-branche of the algorithm is never executed again.
The variable stop is implicit an discrete variable, because it's boolean.

paul wrote:


One more thing: what's the difference between start- and initialize-values?

The start-value is an attribute of every variable. (e.g Real v(start=1); ).
The initialize-value is the value of a variable at the beginning of the simulation.


so long.
Willi

Re: Behavior of algorithms in OpenModelica

wbraun wrote:


That's also the point were the spec is unclear, maybe we need to overwrite the startvalues with the initialize values.

No, we need not. The specification is clear in that point.

paul wrote:


It just cannot see this behavior as specification compliant or even sensible. It could be that i am completely mistaken; in this case it would be nice to give me a hint.

As Willi already wrote, there is nothing wrong with your Model as well as the OpenModelica implementation. I can briefly explain what happens and what you need to do to get the results you want:

The specifications says:

11.1.2 Execution of an algorithm in a model
An algorithm section is conceptually a code fragment that remains together and the statements of an algorithm
section are executed in the order of appearance. Whenever an algorithm section is invoked, all variables appearing
on the left hand side of the assignment operator ":=" are initialized (at least conceptually):
- A non-discrete variable is initialized with its start value (i.e. the value of the start-attribute).
- A discrete variable v is initialized with pre(v).



Hence, x gets set to its start value whenever the algorithm is invoked.
There are at least two ways to get your expected behavior:
1. introduce “x := pre(x)” as else-statement
2. declare x as discrete Real

There are 0 guests and 0 other users also viewing this topic
You are here: