4

Even though Modelica is an acausal modeling language, we learned e.g. here that it can make a difference how you write your equations.

The code of the MutualInductor model in the Modelica.Electrical.Polyphase.Basic package puzzled me a bit and I am wondering if the current implementation was chosen for a specific reason.

The model uses this for loop

  for j in 1:m loop
    v[j] = sum(L[j, k]*der(i[k]) for k in 1:m);
  end for;

to model the inductive coupling of m phases using the inductance matrix L.

The code above could be replaced by this much shorter and cleaner equation:

  v = L * der(i);

I would expect that a Modelica translator will usually not realize that the for loop is equivalent to a matrix multiplication. Hence, my expectation is that the multiplication should be chosen, so we give the translator more information.

Does anybody know if the for loop or the matrix multiplication is beneficial for Modelica translators to solve the equation system?

marco
  • 5,944
  • 10
  • 22
  • 1
    I suppose one way to try it out would be to make a model using both patterns and try them out in a wide range of Modelica compilers to see if it fails anywhere. Usually the shorter form is preferred if it is possible to solve the system by preserving the array equation. This should make compilation faster (fewer variables to consider if the arrays are not expanded), and runtime faster (the C compiler can take advantage of code in vector form). The first commit was 98c87d388a, and since it didn't change I suppose it just worked and nobody thought about alternative formulations. – sjoelund.se Jul 08 '21 at 13:13
  • 1
    here is an example where it is easier to translate the single-line expression than the full for-loop: https://stackoverflow.com/a/59165496/874701 – matth Jul 09 '21 at 11:01

1 Answers1

4

I cannot see any advantage with using that expression. And if really were the case that such an expression was more efficient it would be straightforward to add it to the tools.

If it had been a complex current it might have been some left-over code when support was lacking.

Hans Olsson
  • 11,123
  • 15
  • 38