At the startup of a simulation, large transients are often present. These transients can cause a model to be slow and unstable at initialisation, therefore improved initial conditions should be found. This article describes a method to store start values in the model so that initial transients can be reduced.
Simulate the model
Firstly, setup a simulation in which the model will be driven to the desired starting values. For example in a vehicle simulation the vehicle could be simulated at rest as in the simulation below:
Figure 1: Vehicle simulation with initial transients
In this test the simulation ends with the vehicle settling to the desired start values for vehicle height for example. Before simulating the model make sure protected variables are being stored by going to Simulation->Setup and on the Outputs tab check Protected variables.
Import start values
The importInitial() function or the importInitialResults() function can be used to import the start values into the current Variable Browser model. The syntax for these functions can be displayed within the Simulation window command line by typing help followed by the name of the function:
function importInitial "Import start values from file" input String dsName = "dsfinal.txt"; output Boolean ok; external "builtin"; end importInitial; function importInitialResult "Import start values from ds file" input String dsResName; input Real atTime; output Boolean ok; end importInitialResult;
importInitial() loads the start variables from the last time step which are stored in dsfinal.txt (output within working directory)
importInitialResults() loads the start variables and the parameter values from a given time.
Save Start Values
Save the start values displayed within the Variable Browser to the model by right clicking on the result file and selecting Save Start Values in Model as in Figure 2.
Figure 2: Selecting Save Start Values in Model
If you use Save the Start Values in Model and save a new model and open it, it should look like Figure 3.
Figure 3: Text layer of model saved with start values
The start values of the variables are set in the model. If you select the option to create a new model then the original model is extended to create the new model as in Figure 3. If the importInitialResults() function is used to create the model then parameter modifiers for the start conditions will also be included in the model.
The model in Figure 3 simulates with no visible transients.
There are some limitations:
- Issues related to obtaining the required start values:
- Variables with the annotation Hide=true will not appear in the result file and so cannot be set using importInitialResults(), the importInitial() method should work.
- Persistence variable values in external functions are not stored in the result file so these values will be set to the default values at initialisation.
- Issues related to saving start values to a model:
- Saving part of array values is not supported. The issue arises when only some of the elements of an array start values are set (for example if you have a variable defined as Real a and only a’s start value is set then when Save Start Values to Model is used you get an error). The workaround is to provide start values for the rest of the array.
- The start values for values present in initial equation or initial algorithm sections will not be set.
- Variables that are set using the final keyword cannot be modified.
- Issues related to simulating new model:
- If protected variables’ start values are modified this will generate warnings (make sure that Advanced.PedanticModelica=false; this can be set from the Dymola command line)
- When simulating you should start the simulation at the same time that start values were extracted.
Written by: Garron Fish – Chief Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion