How to restart a simulation from a given state

How to restart a simulation from a given state

Often it is useful to be able to repeatedly restart a simulation from a given state.  This is useful when we have a model that needs to simulate for a significant amount of time to reach a stable operating point from which we are interested in trying various different tests.  We can achieve this provided we have a Dymola result file from which we can read in the model state that we want to start the simulation from.

Models are defined by a set of states, for example a mass falling towards earth is described by its position (x) and velocity (v).  In the plot below, the mass has an initial velocity of +3m/s and then accelerates under gravity.

When a Dymola model is simulated a dsin.txt file is read that contains all the initial variable values including the states of the system.  When a simulation completes another file called dsfinal.txt is created that stores the final values of the system.  Both of these files are found in the current Dymola working directory.

If you simply wanted to continue a simulation from where the previous simulation ended you could select the Continue option from the Simulation menu.  It is also possible to do this outside of the Dymola user interface by renaming the dsfinal.txt file to dsin.txt and then running dymosim.exe which would now simulate using the final values of the previous simulation as the initial values and effectively continue the simulation.

Example: Rotational shaft being batch simulated from the states at 1s

This example simulates a rotational shaft, below, for 1 second.  The states at 1 second are stored and the simulation is continued multiple times from 1s changing a parameter in the model.

You can download a copy of this model here.

The script used to simulate this model is as follows:

// CALCULATE INITIAL STATES TO START BATCH FROM
translateModel("RotationalSpringSystem");
simulateModel("RotationalSpringSystem", method="dassl", resultFile="RotationalSpringSystem");

// Store the final states under a different name to avoid overwriting
system("copy dsfinal.txt dsfinal_1.txt")

// Store 10 results files
plotSetFilesToKeep(10);
// BATCH SIMULATE
for i in 1:10 loop
  // Load initial values with dsfinal_1.txt values
  importInitial("dsfinal_1.txt");
  step.height=i;   // Change the step height parameter in the model
  simulate();      // Simulate the model
  plot({"inertia3.w"},fill("",0),true);
end for;

The script consists of two parts, the first is to create the initialisation data and the second is to simulate the model.

The initialisation data is created by simulating the model from 0 to 1 [s], this generates a dsfinal.txt file in the current working directory.  We then copy dsfinal.txt to dsfinal_1.txt using the dos copy command.  The system() command in Dymola gives the user a way to run commands from the dos prompt.

The “Batch Simulation” section makes use of the importInitial() command to copy the data in dsfinal_1.txt to dsin.txt and so is used to define the initial condition.  The parameter step.height is changed to the current loop iteration number and the model is simulated using the simulate() function.

Note that it is also possible to use the built-in importInitialResult() function to extract initial conditions at a set time from the results file and store them in the dsin.txt.  The documentation for the importIntialResult() function is as follows:

function importInitialResult "Import start values from ds file"
 input String dsResName;
 input Real atTime;
 output Boolean ok;
end importInitialResult;
Example:
importInitialResult("dsres.mat",0.5)

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2023 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?