Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelica total time calculation of simulation and equation initialization

Tags:

time

modelica

I would like to measure the total simulation and initialization time of a system of DAEs. I am interested in the wall-clock time (like the one given in Matlab by the function tic-toc).

I noticed in Modelica there are different flags for the simulation time but actually the time I get is very small compared to the time that elapses since I press the simulation button to the end of the simulation (approximately measured with the clock of my phone).

I guess this short time is just the time required for the simulation and it does not include the initialization of the system of eqs.

Is there a way to calculate this total time?

Thank you so much in advance,

Gabriele


Dear Marco, Thank you so much for your extremely detailed and useful reply!

I am actually using OpenModelica and not Dymola so unfortunately I have to build the function that does it for me and I am very new with OpenModelica language.

So far, I have a model that simulate the physical behavior based on a DAEs. Now, I am trying to build what you suggest here:

With get time() you can build a function that: reads the system time as t_start translates the model and simulate for 0 seconds reads the system time again and as t_stop computes the difference between t_start and t_stop.

Could you please, give me more details: Which command can I use to read the system at time t_start and to simulate it for 0 seconds? To do this for both t_start and t_stop do I need to different function?

Once I have done this, do I have to call the function (or functions) inside the OpenModelica Model of which I want to know its time?

Thank you so much again for your precious help!

Very best regards, Gabriele

like image 342
Gabriele Galli Avatar asked Apr 22 '20 21:04

Gabriele Galli


1 Answers

Depending on the tool you have, this could mean a lot of work.

The first problem is that the MSL allows you to retrieve the system time, but there is nothing included to easily compute time deltas. Therefore the Testing library in Dymola features the operator records DateTime and Duration. Note, that it is planned to integrate them in future MSL versions, but at the moment this is only available via the Testing library for Dymola users.

The second problem is that there is no standardized way to translate and simulate models. Every tools has its own way to do that from scripts. So without knowing what tool you are using, it's not possible to give an exact answer.

What Modelica offers in the MSL

In the current Modelica Standard Library version 3.2.3 you can read the actual system time via Modelica.Utilities.System.getTime().

This small example shows how to use it:

function printSystemTime
protected 
  Integer ms, s, min, h, d, mon, a;
algorithm 
  (ms, s, min, h, d, mon, a) := Modelica.Utilities.System.getTime();
  Modelica.Utilities.Streams.print("Current time is: "+String(h)+":"+String(min)+":"+String(s));
end printSystemTime;

You see it gives the current system date and time via 7 return values. These variables are not very nice to deal with if you want to compute a time delta, as you will end up with 14 variables, each with its own value range.

How to measure translation and simulation time in general

With gettime() you can build a function that:

  1. reads the system time as t_start
  2. translates the model and simulate for 0 seconds
  3. reads the system time again and as t_stop
  4. computes the difference of t_start and t_stop.

Step 2 depends on the tool. In Dymola you would call

DymolaCommands.SimulatorAPI.simulateModel("path-to-model", 0, 0);

which translates your model and simulates it for 0 seconds, so it only runs the initialization section.

For Dymola users

The Testing library contains the function Testing.Utilities.Simulation.timing, which does almost exactly what you want.

To translate and simulate your model call it as follows:

Testing.Utilities.Simulation.timing(
  "Modelica.Blocks.Examples.PID_Controller", 
  task=Testing.Utilities.Simulation.timing.Task.fullTranslate_simulate, 
  loops=3);

This will translate your model and simulate for 1 second three times and compute the average.

To simulate for 0s, duplicate the function and change this

if simulate then
  _ :=simulateModel(c);
end if;

to

if simulate then
  _ :=simulateModel(c, 0, 0);
end if;
like image 112
marco Avatar answered Oct 21 '22 10:10

marco