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

FMU simulates incorrectly

FMU simulates incorrectly

When simulating the model below in OpenModelica, x is a damped oscillation, as expected. However, when simulating the model using the generated FMU in co-simulation mode, x is undamped. Does anyone have an explanation for this? I have verified my program for running the FMU with simpler models like an exponential decay, and that looks OK.

model RLC

  type Voltage=Real(unit="V");
  type Time=Real(unit="s");
  type Current=Real(unit="A");
  type Resistance=Real(unit="Ohm");
  type Capacitance=Real(unit="F");
  type Inductance=Real(unit="H");
 
  parameter Voltage qi = 0;
  parameter Voltage qf = 0.02;
  Voltage x;
 
protected
  Voltage V;
  Voltage Vs = qf;
  Current i_L;
  Current i_R;
  Current i_C;
  Inductance L = 1;
  Resistance R = 100;
  Capacitance C = 1e-3;
 
initial equation
  V = qi;

equation
  i_R = V/R;
  i_C = C*der(V);
  i_L=i_R+i_C;
  L*der(i_L) = (Vs-V);
  x = V; 
end RLC;

Re: FMU simulates incorrectly

What step size is your FMU simulation tool using? My guess: It is bigger then 0.01.

Be aware that the internal integrator for a Co-Simulation FMU defaults to an explicit Euler method, which could be a non-optimal choice for this model, it appears to be stiff. Because of that every explicit method for this example is a bad choice:
Compare what happens for an explicit and implicit Euler method for step sizes = 0.005, 0.01 and 0.015. The implicit methods are stabilizing, while the explizit method will explode for too big step sizes. This bad behavior should be visible for every explicit method when the step size is above some critical value.

So you have two options:
1) Export an Co-Simulation FMU with a better (implicit) integrator. You can use Sundials CVODE for OpenModelica FMUs, see the user's guide on fmi-export.
2) Export an ModelExchange FMU and implement a decent integration method in the FMU simulation tool (e.g. CVODE as in OMSimulator or IDA as in OMEdit).

Edited by: AnHeuermann - Jun-24-21 09:46:06

Re: FMU simulates incorrectly

AnHeuermann wrote:

What step size is your FMU simulation tool using? My guess: It is bigger then 0.01

You were absolutely right. Thank you very much!

Re: FMU simulates incorrectly

I find the subject is interesting. one question about using CVODE in FMU

from OM User Guide:

To have CVODE in a SourceCode FMU the user needs to add all sources for SUNDIALS manualy and create a build script as well

what or which build script is this reffering to? is it a build script to zip the FMU?

Thanks in advance

Re: FMU simulates incorrectly

You can generate source code FMUs that can be compiled on any system, at least in theory. When using more features of OpenModelica it get's more complicated.

The C source files generated by OpenModelica will need some dynamic or static libraries to work. If you want to link some SUNDIALS libraries into the executable to use CVODE you will need either a dynamic/static library for the target system or add all sources of SUNDIALS to compile it on your target system. There are some configure and makefiles in the source directory of the FMU you would need to adapt.

I guess the best way to use FMUs with CVODE on a system you can't generate binaries from the generating OS, is to compile/install CVODE on the target system and link dynamically. But in any case it is not straight forward and only applies to you if you need source code FMUs.

Edited by: AnHeuermann - Jun-29-21 08:47:03
There are 0 guests and 0 other users also viewing this topic