Saving Periodic Snapshots
To improve the possibility to restart a lengthy simulation from multiple time points, periodic snapshots of results can be saved during simulation. The reason for such a restart can be some problems in the simulation that needs debugging, or to perform parameter studies from a certain time point.
In this scenario, you save snapshots of the demo model Coupled Clutches, and use one of them to restart the simulation with those values.
- Open the model example Coupled Clutches. For more information, see Accessing Model Examples.
-
Display the Execution Setup dialog box.
For more information, see Setting up a Simulation.
-
In the dialog box, do the following
-
In the General tab, change, by typing, the
Stop time to
2s.
For more information about this tab, see General Tab.
The stop time is changed to 2 seconds. The reason for changing the stop time is just to illustrate changing the duration which is used when specifying the interval between snapshots. -
In the Output tab, change, by typing, the
Interval between snapshots to
0.2.
For more information about the Output tab, see Output Tab.
The saving of snapshots is activated, with the time 0.2 seconds between each snapshot.Tips: - You can also set the interval by scriping, by setting
Advanced.Simulation.ResultSnapshotInterval=0.2
. Compare with Step 5 on how do this. - If you want to start a simulation from close to where it failed, you can specify a short interval between snapshots and deactivate the option Timestamps in snapshot filenames. You now have no timestamps, each new file has the name dsfinal.txt. Each new snapshot then overwrites the previous one, leaving only the last one when simulation fails. (If the simulation runs to the end, the last snapshot is overwritten by the dsfinal.txt file that is always created at the end of the simulation.)
- You can also set the interval by scriping, by setting
-
In the General tab, change, by typing, the
Stop time to
2s.
-
Simulate the model. For more information, see Simulating a Behavior.
Note: In this scenario, do not use the associated command Simulate and Plot to simulate, it has a predefined simulation output interval of 0.0024 seconds, this does not match the snapshot interval that is selected. The closest subsequent output point will be used for each snapshot. This means, for this scenario, that you have look in the simulation log to see the exact names of the snapshot files to be able to call them.The result of the simulation, concerning snapshots, are:
- After each 0.2 seconds, a snapshot of the simulation result is
saved, the first one at time t=0.2 seconds, the last one at time
t=1.8 seconds. The files are saved with the name
dsfinal_<time>.txt
, where <time> is the time in seconds. An example isdsfinal_0.4.txt
. - Each time a snapshot is saved, it is printed in the simulation log.
Example of such a line is
..."dsfinal_0.4.txt" creating (snapshots at time t = 0.4)
Note: The simulation log can be seen from the Messages Reporting window, by expanding the line Simulation of <model>, and then clicking the line Modelica simulation log. For more information about the Messages Reporting window, see Messages Reporting Window.
- After each 0.2 seconds, a snapshot of the simulation result is
saved, the first one at time t=0.2 seconds, the last one at time
t=1.8 seconds. The files are saved with the name
-
To be able to restart the simulation with the saved values from time t =
0.4, do the following:
-
Open the Modelica Scripting window.
For more information about this window, see Working with the Modelica Scripting Window.
-
Type, in the command input line of that window, the text
importInitial("dsfinal_0.4.txt")
and press Enter.
The result are:- You get the output true in the
Modelica Scripting window that indicates
that the call succeeded.Note: Reason for failure may be, for example, that the file name is not correct due to the time when the file was saved was not exactly the time you expected. See the first note in Step 4.
- A new Variable Browser is created, with the
content from the file
dsfinal_0.4.txt
.Tip: You can also work with the Modelica Script Editor when working with scripting.
-
Open the Modelica Scripting window.
-
To start the simulation from the imported values of time t = 0.4, you can
do any of the following:
- To do it with a command, to the lower right of the
Variable Browser, click Refresh
results from initial conditions
.
Important: This command may be grayed. Pause over it to display it. - To do it with scripting, type, in the command line of the Modelica Scripting window, the text simulate() and press Enter.
The simulation is restarted with the values from the time t=0.4 seconds.Notes:- The simulation still runs the whole duration, that is, the start time now is 0.4 seconds, and the stop time is 2.4 seconds.
- New snapshots are created.
- To do it with a command, to the lower right of the
Variable Browser, click Refresh
results from initial conditions
.