dymola.readTrajectory - different sample times

2019-07-27 03:10发布

问题:

Given a python script that triggers simulations in the Dynamic Modeling Laboratory (Dymola).

My goal is to get the data of the trajectories of my simulation, but in different sampling intervals. E.g. I want the transient section from 0 sec to 1000 sec to be sampled in 1sec intervals and the steady state section from 1000sec to 100000sec to be sampled in 100sec intervals without having to perform two simulations with different start times, stop times and output intervals.

An excerpt of the code that triggers the simulation for the transient section:

[result, simulation_result] = dymola.simulateExtendedModel(problem=problem,
                                                           initialNames=["input1", "input2", "input3"],
                                                           initialValues=[input1, input2, input3],
                                                           finalNames=output_names,
                                                           outputInterval=1,
                                                           startTime=0,
                                                           stopTime=1000,
                                                           resultFile=result_filename)

trajectory_size_transient = dymola.readTrajectorySize("simulation_result_python.mat")
sim_res_transient = np.array(dymola.readTrajectory("simulation_result_python.mat",
                                                                     output_names,
                                                                     trajectory_size_transient))

The code for the simulation of the the steady state:

[result, simulation_result] = dymola.simulateExtendedModel(problem=problem,
                                                           initialNames=["input1", "input2", "input3"],
                                                           initialValues=[input1, input2, input3],
                                                           finalNames=output_names,
                                                           outputInterval=100,
                                                           startTime=1000,
                                                           stopTime=10000,
                                                           resultFile=result_filename)

trajectory_size_steady = dymola.readTrajectorySize("simulation_result_python.mat")
sim_res_steady = np.array(dymola.readTrajectory("simulation_result_python.mat",
                                                                     output_names,
                                                                     trajectory_size_steady))

The value "outputInterval" is the interval in which the results are written into the trajectory. As seen, I have to perform two simulations and stack the results later on. I'd rather have one simulation with two different values of "outputInterval" and set

 startTime=0
 stopTime=10000

so that I can get two trajectories from one simulation.

Does anyone know if this is possible? Or maybe is there another approach?

回答1:

I think the following do what you need. It is a bit cumbersome, but should be worth the try:

  1. Build a column vector containing the desired output points and store it in Dymola's working directory, e.g. using MATLAB:
tgrid = [0,10,13.75,97]'
save tgrid.mat tgrid -v4
  1. Modify dsin.txt to look like this:
#    Method tuning parameters
double method(27,1)
       2                   # grid     type of communication time grid, defined by
                           #          = 1: equidistant points ("Increment/nInterval")
                           #          = 2: vector of grid points ("tgrid")
                           #          = 3: variable step integrator (automatically)
                           #          = 4: model (call of "increment" in Dymola, e.g.
                           #                      incr=Time > 2 then 0 else 0.1
                           #                      dummy=increment(incr))
                           #          grid = 1,3 is stopped by "StopTime"
                           #          grid = 2   is stopped by "tgrid(last)"
                           #          grid = 4   runs forever (stopped by model)
       1                   # nt       Use every NT time instant, if grid = 3
       3                   # dense    1/2/3 restart/step/interpolate GRID points
       0                   # evgrid   0/1 do not/save event points in comm. time grid

Usually the following has to be changed: (1) The flag in line 3 (2) activates the usage of the tgrid-vector. (2) The flag in the last line (0) deactivates the creation of grid points at events.

  1. Use alist.exe to apply the tgrid-vector to dsin.txt using the command line:

"<DymolaInstallDir>\Mfiles\alist.exe" -b "<DymolaWD>\dsin.txt" "<DymolaWD>\tgrid.mat" "<DymolaWD>\dsin.mat"

This should create a file dsin.mat in which the new grid is stored. This file should not be overwritten if the model is re-translated. Simulating from the Dymola GUI will ignore the settings in dsin.mat.

  1. Simulate:

In DymolaWD execute dymosim.exe -s dsin.mat, which should result in the following for the given example:

Integration terminated successfully at T = 97
   CPU-time for integration      : 0.031 seconds
   CPU-time for one GRID interval: 10.3 milli-seconds
   Number of result points       : 4
   Number of GRID   points       : 4
   Number of (successful) steps  : 492
   Number of F-evaluations       : 19726
   Number of Jacobian-evaluations: 0
   Number of (model) time events : 0
   Number of (U) time events     : 0
   Number of state    events     : 0
   Number of step     events     : 0
   Minimum integration stepsize  : 0.000732
   Maximum integration stepsize  : 20
   Maximum integration order     : 7
Calling terminal section
... "dsfinal.txt" creating (final states)

and create a file dsres.mat containing the simulation's results.



标签: python dymola