Main Content

Running Multiple Simulations

For workflows that involve multiple parallel simulations and logging of large amounts of data, you can use the parsim or batchsim functions, or run the simulations with the Multiple Simulations panel in the Simulink® Editor. This approach is useful in scenarios like model testing, experiment design, Monte Carlo analysis, and model optimization.

For the command line workflow (parsim or batchsim functions), you can create simulation sets by using an array of Simulink.SimulationInput objects to run multiple simulations. Alternatively, you can use the Multiple Simulations panel in Simulink.

In the Multiple Simulations panel, you can create design studies to make specify values for simulations. The panel allows you to specify values for block parameters, and variables for the simulations.

With the Parallel Computing Toolbox™, you can also use the parsim and batchsim functions, and Multiple Simulations panel to run the simulations in parallel.

Note

When the pool is not already open and simulations are run for the first time, simulations take an additional time to start. Subsequent parallel simulations are faster.

Multiple Simulations with Simulink Editor

The Multiple Simulations panel allows you to set up multiple simulations for different values of block parameters and variables. You can pick a block parameter of interest from the model canvas and specify values that you want to use for the simulations. You can also provide variables for simulations. To run the simulations that you have set up, first, select the design study, then click Run All button on Simulation tab on the Simulink toolstrip. To learn more, see the Multiple Simulations Panel: Simulate for Different Values of Stiffness for a Vehicle Dynamics System example.

Multiple Simulations with Parallel Computing Toolbox

The parsim function and the Multiple Simulations panel distributes each simulation to parallel pool workers to decrease your overall simulation time when running in parallel. Using the parsim function and the Multiple Simulations panel creates a parallel pool, which allows you to identify file dependencies and manage build artifacts for simulations in accelerator and rapid accelerator mode.

The batchsim function offloads the simulations to the compute cluster. The simulations are executed on the cluster, which allows you to carry out other tasks while the batch job is processing. You can also close the client MATLAB® session and access the batch job later.

The batchsim function uses the Parallel Computing Toolbox™ license to run the simulations on compute cluster. The batchsim function runs the simulations in serial if a parallel pool cannot be created. Without the Parallel Computing Toolbox license, batchsim function gives an errors.

Multiple Simulations with the sim Function

In the absence of a Parallel Computing Toolbox license, the parsim function and the Multiple Simulations panel behave like the sim command. The simulations run in serial.

Multiple Simulations with Simulink.SimulationInput Object

In command line workflows, you can make changes to your model using the Simulink.SimulationInput object and run a simulation in parallel with those changes. Changing the values in the Simulink.SimulationInput object, overrides the values in the model, which allows you to modify the model without dirtying it. Through the Simulink.SimulationInput object, you can also specify MATLAB functions to run at the start and the end of each simulation by using the PreSimFcn and PostSimFcn functions, respectively. When you use Simulink.SimulationInput objects, the model parameters are restored after the simulation ends. See Run Parallel Simulations for a Thermal Model of a House Using parsim. The Simulink.SimulationInput object allows you to change these settings in your model:

  • Initial state

  • External inputs

  • Model parameters

  • Block parameters

  • Variables

Simulation Manager to Monitor Simulations

The Simulation Manager allows you to monitor multiple parallel simulations. It shows the progress of the runs as they are running in parallel. You can view the details of every run such as parameters, elapsed time, and diagnostics. The Simulation Manager acts as a useful tool by giving you the option to analyze and compare your results in the Simulation Data Inspector. You can also select a run and apply its values to the model. For more information, see Simulation Manager.

The parsim/batchsim Function Capabilities

  • Outputs errors in the simulation output object for easier debugging

  • Compatible with rapid accelerator and fast restart

  • Compatible with file logging (to facilitate big data)

  • Compatible with MATLAB Parallel Server™ in addition to local parallel pools

  • Capable of transferring base workspace variables to workers

  • Avoids transparency errors

Data Logging for Multiple Simulations

The resulting Simulink.SimulationOutput object, which contains the simulation outputs, captures error messages and the simulation metadata. When you select the Data Import/Export > Log Dataset data to file configuration parameter, Simulink creates a Simulink.SimulationData.DatasetRef object for each Dataset stored in the resulting MAT file. You can use the DatasetRef object to access the data for a Dataset element. For simulations that are run using the Simulink.SimulationInput objects, the DatasetRef object is returned as part of the SimulationOutput object. As a result, you have quicker access to and do not need to create them.

Parallel simulations can produce more logged data than the MATLAB memory can hold. Consider logging to persistent storage for parallel simulations to reduce the memory requirement. When you select the Data Import/Export > Log Dataset data to file configuration parameter (LoggingToFile), for parallel simulations in Simulink:

  • Data is logged in Dataset format in a MAT-file

  • A Simulink.SimulationData.DatasetRef object is created for each Dataset element (for example, logsout) for each simulation

You can use DatasetRef objects to access data for a specific signal. You can create matlab.io.datastore.SimulationDatastore objects to use for streaming logged data from persistent storage in to a model.

See Also

| | | | | | | | | |

Related Topics