Solving static equations (or systems thereof)
Asked Answered
A

2

5

I wanted to do the following in Modelica: in a specific model I have several parameters h and I want to deduce some time independent values k from them by solving a set of implicit equations between the parameters and the other values. Since the equations are implicit by nature, I cannot simply assign an expression, I rather have to let the solver find the solution.

Since the parameters are constant, I want to be able to solve the equations only once at the beginning, before the actual time integration of the rest of the system (e.g. a differential equation that contains k as a coefficient) takes place.

See the following example of what I would like to be able to write:

model ConstantTest
  parameter Real h = 2;
  const Real k;
initial equation
  k*k=h; // just an example of an implicit equation, which in this simple case could also be written explicitly
end ConstantTest;

But this fails because a "constant" in Modelica is not only constant in the sense of having vanishing time derivative, but it is also already immutable at the time when the initial equations are being solved. So the solver complains that it cannot solve the initial equation 0 = 2, which is because it is assuming that k invariably equals 0.

Of course I can just make k a variable, but then I have to tell the solver explicitly that k has vanishing time derivative (amounting to it being practically "constant" in the naive physical sense):

model ConstantTest
  parameter Real h = 2;
  Real k;
initial equation
  k*k=h;
equation
  der(k) = 0;
end ConstantTest;

This works, but it is somewhat odd because the solver has to solve a trivial differential equation at every time step in order to do basically nothing at all to k. And that would be a waste of computational resources.

Is there any way to solve static implicit equations with Modelica without introducing "time evolution overhead"?

Arcane answered 7/2, 2019 at 17:21 Comment(0)
R
8

I guess you could do this:

model ConstantTest
  parameter Real h = 2;
  parameter Real k(fixed=false);
initial equation
  k*k=h;
end ConstantTest;

k will be computed at initialization.

Rausch answered 7/2, 2019 at 18:7 Comment(3)
Nice! Is there also a way to keep this (dummy) parameter from appearing in the configuration dialog of the instantiated model? Because, it means 1) clutter, 2) something's changeable which ought not be changed...Arcane
Just make it protected.Rausch
Cool. Mission accomplished. :-)Arcane
B
2

I think the best way to define these kinds of systems is:

model ConstantTest
  parameter Real h = 2;
  Real k;
equation
  2*k=h;
end ConstantTest;

Which OpenModelica will put in an initial section and solve only once. I would consider OpenModelica's behaviour for your system a bug since it's solving a time-independent equation multiple times.

Blond answered 7/2, 2019 at 18:45 Comment(2)
I am not sure whether OpenModelica's model compiler will actually solve the equation multiple times (or in my example, solve the zero derivative). In traditional programming languages, implicit code optimizations are usually not required by the standard (implementation details), so I have just assumed that I can't rely on the compiler optimizing such redundancies away. Do you know if the Modelica standard requires these optimizations?Arcane
It's not required, but in order to get any kind of performance, Modelica tools perform a ton of optimizations.Blond

© 2022 - 2024 — McMap. All rights reserved.