0

I have a piece of modelica code in a long when-elsewhen statement that uses reinit() for a continuous time variable and with if-elseif embedded in an equation section. In some cases, if a specific condition occurs, then I'm using the kind of code below to do nothing and have the same variables assignments as with other sections of the when and if sections:

variable = pre(variable) ;

It seems that Dymola rewrites my equations in the form (I try to reproduce in a very synthetic form the code I'm using):

condition1 = pre(variable1);
...
conditionN = .... ;
when {condition1, ..., consitionN}  then
    variable1 = if NothingToDoCondition then pre(variable)  else value;
    variable2 = if NothingToDoCondition then pre(variable)  else anotherValue;
    ....
    variableM =... //as above
    if condition then
        reinit(x,y) ;
    else
        //no equation
    end if ;
elsewhen ....
   // repeat as above

end when;

The Dymola check complains that:

This elsewhen in equations is not supported, it can be worked around by rewriting as algorithm, however be careful since the right-hand-side is dependent on left-hand-side variables and that is handled differently in algorithms

First, I don't know how to convert my piece of code into an algorithm section since I'm using reinit(), which cannot be used in the algorithm section. Is there an alternative to the reinit() that I can use in an algorithm section?

Second, how to interpret the message from the Dymola check ? What's the implications for using a when statement in an algorithm or equation ?

1 Answer 1

2

There's an alternative to using algorithms.

In general

equation
  when {condition1, ..., conditionN}  then
    variable1 = if NothingToDoCondition then pre(variable1)  else value11;
    variable2 = if NothingToDoCondition then pre(variable2)  else value12;
    ....
    variableM =... //as above
    if condition then
        reinit(x,y) ;
    else
        //no equation
    end if ;
elsewhen ....
   variable1 = if NothingToDoCondition then pre(variable1)  else value21;
   variable2 = if NothingToDoCondition then pre(variable2)  else value22;
    ....
   variableM =... //as above
end when;

is equivalent to:

equation
  when {condition1, ..., conditionN}  then
    variable1 = if NothingToDoCondition then pre(variable1)  else value11;
  elsewhen ....
    variable1 = if NothingToDoCondition then pre(variable1)  else value21;
  end when;

  when {condition1, ..., conditionN}  then
    variable2 = if NothingToDoCondition then pre(variable2)  else value12;
  elsewhen ....
    variable2 = if NothingToDoCondition then pre(variable2)  else value22;
  end when;

  ...

  when {condition1, ..., conditionN}  then
    variableM = if NothingToDoCondition then pre(variableM)  else value1M;
  elsewhen ....
    variableM = if NothingToDoCondition then pre(variableM)  else value2M;
  end when;

  when {condition1, ..., conditionN}  then
    if condition then
        reinit(x,y) ;
    else
        //no equation
    end if ;
  end when;

or variants thereof - it might suffice to split off the reinit.

And Dymola should be able to do this automatically (depending on version) as far as I can tell. However, if variables don't appear in the same form in all of the branches that may fail - and in that case just splitting off the reinit would likely work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.