Difference between equation and algorithm section
Asked Answered
G

1

10
  • Equation section : declarative

  • Algorithm section : imperative

Can anyone help me with examples that show the difference between these two sections in Modelica Language? When to use what section?

Gannie answered 19/11, 2013 at 17:39 Comment(0)
N
23

The main difference between an algorithm section and an equation section is that each equation in an equation section is used in simulating a model. With an algorithm section, you have assignment statements that are imperative. This means that you can overwrite the impact of a previous assignment. As a concrete example, in the following equation section:

equation
  a = b;
  a = c;

There are two equations. Each will be used. If, on the other hand, I had the following algorithm section

algorithm
  a := b;
  a := c;

The second assignment always overrides the first one.

But let me add a little bit of a "big picture" to help you understand the real semantic difference. You can think of an algorithm as a black box. A bunch of information comes in and certain variables get assigned to. What happens in between isn't important. Consider the following algorithm:

algorithm
  sum := 0;
  prod := 1;
  for i in 1:10 loop
    sum := sum + i;
    prod := prod * i;
  end for;

Both sum and prod are assigned to multiple times. But in the end, all this is effectively the same as:

algorithm
  sum := 1+2+3+4+5+6+7+8+9+10;
  prod := 1*2*3*4*5*6*7*8*9*10;

...or even...

equation
  sum = 1+2+3+4+5+6+7+8+9+10;
  prod = 1*2*3*4*5*6*7*8*9*10;

But the same doesn't apply to equation sections. If I did this in an equation section:

equation
  sum = 0;
  for i in 1:10 loop
    sum = sum + i;
  end for;

I'd have a serious problem because this would expand into 11 equations:

equation
  sum = 0;
  sum = sum + 1;
  sum = sum + 2;
  ...
  sum = sum + 10;

So I've got one variable and 11 equations! That's not what we want.

So, to summarize, with an algorithm you can ignore what happens in the algorithm and only pay attention to what gets assigned a value. It does not matter how many times it is assigned a value it still counts as one "equation" for that variable. With an equation section, every equation persists.

P.S. - When Modelica tools do things like equation sorting, I'm pretty sure the semantics dictate that an algorithm is treated as a single block. My guess is this doesn't mean anything to you yet. But as you learn more about Modelica, you'll recognize that this can be bad for performance. So the general rule in Modelica is always using an equation over an algorithm if you can. Your simulation will be faster as a result.

Nasser answered 19/11, 2013 at 23:17 Comment(3)
Shouldn't x[i] be i in your third code block? πŸ€” – Darby
We had a relevant discussion with Martin Sjölund here on Discord. I think the algorithm section could be used to model causal relations, but apparently, that was not the intention. πŸ˜” – Darby
I applied some formatting edits, I hope you don't mind. – Darby

© 2022 - 2024 β€” McMap. All rights reserved.