parsim
Simulate dynamic system multiple times in parallel or serial
Description
simulates a model using the inputs specified in the simOut
= parsim(in
)SimulationInput
object, in. The parsim
function
uses an array of SimulationInput
objects to run
multiple simulations.
simulates a model in parallel using the inputs specified in the simOut
= parsim(in
,Name=Value
)SimulationInput
object and the options specified using
one or more name-value arguments.
simulates massive simulations with the futureOut
= parsim(designStudy
)simulink.multisim.DesignStudy
object.
The parsim
function requires the Parallel Computing Toolbox™ license to run the simulations in parallel. The
parsim
function runs the simulations in serial if a
parallel pool cannot be created or if Parallel Computing Toolbox is not used.
Examples
Simulate the model vdp
in rapid
accelerator mode.
Load the model.
openExample('simulink_general/VanDerPolOscillatorExample') mdl = "vdp"; load_system(mdl)
This step builds the Rapid Accelerator target:
Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);
Create a SimulationInput
object and use
the setModelParameter
function to set
RapidAcceleratorUpToDateCheck
to
"off"
.
in = Simulink.SimulationInput(mdl); in = setModelParameter(in,SimulationMode="rapid-accelerator"); in = setModelParameter(in,RapidAcceleratorUpToDateCheck="off");
Simulate the model.
out = parsim(in);
Simulate the model, CSTR, in parallel by sweeping over a variable. An array of SimulationInput objects is used to perform the sweep.
Specify sweep values.
FeedTempSweep = 250:10:300;
Create an array of SimulationInput objects.
for i = length(FeedTempSweep):-1:1 in(i) = Simulink.SimulationInput('CSTR'); in(i) = in(i).setVariable('FeedTemp0',FeedTempSweep(i)); end
Simulate the model in parallel.
out = parsim(in, 'ShowSimulationManager', 'on')
[28-Aug-2024 10:44:10] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'Processes' profile ... 28-Aug-2024 10:45:15: Job Queued. Waiting for parallel pool job with ID 2 to start ... Connected to parallel pool with 8 workers. [28-Aug-2024 10:45:41] Starting Simulink on parallel workers... [28-Aug-2024 10:46:32] Configuring simulation cache folder on parallel workers... [28-Aug-2024 10:46:33] Loading model on parallel workers... [28-Aug-2024 10:46:49] Running simulations... [28-Aug-2024 10:47:40] Completed 1 of 6 simulation runs [28-Aug-2024 10:47:41] Received simulation output (size: 20.18 KB) for run 1 from parallel worker. [28-Aug-2024 10:47:41] Completed 2 of 6 simulation runs [28-Aug-2024 10:47:41] Received simulation output (size: 20.22 KB) for run 2 from parallel worker. [28-Aug-2024 10:47:41] Completed 3 of 6 simulation runs [28-Aug-2024 10:47:41] Received simulation output (size: 20.61 KB) for run 3 from parallel worker. [28-Aug-2024 10:47:44] Completed 4 of 6 simulation runs [28-Aug-2024 10:47:44] Received simulation output (size: 35.84 KB) for run 4 from parallel worker. [28-Aug-2024 10:47:45] Completed 5 of 6 simulation runs [28-Aug-2024 10:47:45] Received simulation output (size: 37.76 KB) for run 6 from parallel worker. [28-Aug-2024 10:47:45] Completed 6 of 6 simulation runs [28-Aug-2024 10:47:45] Received simulation output (size: 39.48 KB) for run 5 from parallel worker. [28-Aug-2024 10:47:45] Cleaning up parallel workers... out = 1x6 Simulink.SimulationOutput array
Input Arguments
Inputs and configurations for parallel simulations, specified as an array
of Simulink.SimulationInput
objects.
Inputs and configurations for massive parallel simulations, specified as a
simulink.multisim.DesignStudy
object.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: ShowProgress="on"
Additional files to attach to the parallel pool, specified as a cell
array of character vectors. The parsim
function
does not support using From File blocks to load data from
MAT files in subdirectories.
Option to show progress of simulations, specified as
"on"
or "off"
. Set to
"on"
, to see the progress of the simulations in
the MATLAB® Command Window. The progress is hidden when set to
"off"
.
Note
When the progress is shown, a message 'Cleaning up
parallel workers..'
might be displayed before the
completion of the last few simulations. This message does not
depend on the completion of the simulations. Simulations
complete when the outputs are fetched from the
future
. For more information, see Simulink.Simulation.Future
.
Option to run simulations in background, specified as
"off"
or "on"
. Set to
"on"
to run simulations asynchronously, keeping
the MATLAB command prompt available for use. To terminate the
simulations running in the background, use the cancel
function of the
Simulink.Simulation.Future
objects.
When you specify the 'RunInBackground'
argument as
"on"
, the parsim
function
returns Simulink.Simulation.Future
objects. Use the
Simulink.Simulation.Future
object to access the
SimulationOutput
object.
Function to run once per worker before starting simulations, specified as a function handle.
Example: SetupFcn=@()openProject("Modelex/Model_example.prj")
Note
When buildRapidAcceleratorTarget
is used in the
SetupFcn
and the model has external inputs
specified, either set LoadExternalInput
to
"off"
or ensure that the specified external
input is available on the workers to prevent compilation
error.
Function to run once per worker after running simulations, specified as a function handle.
Option to manage model dependencies, specified as
"on"
or "off"
. When
ManageDependencies
is set to
"on"
, model dependencies are automatically sent
to the parallel workers if required. If
ManageDependencies
is set to
"off"
, explicitly attach model dependencies to
the parallel pool.
Option to log diary information for parallel simulations and
PreSimFcn
and PostSimFcn
,
specified as 'on'
or 'off'
. You
can use the setPreSimFcn
to print diagnostics to the
diary to help debug parsim
and
batchsim
setup issues.
Option to use fast restart, specified as "off"
or
"on"
. When UseFastRestart
is
set to "on"
, simulations run on the workers using
fast restart. For more information, see Script Iterative or Batch Simulations Using Fast Restart.
When performing a parameter sweep that varies properties of a Simscape™ component, such as the mass and inertia of a Solid block in Simscape Multibody™, specify the Simscape block parameter as run-time configurable. For more information, see About Simscape Run-Time Parameters (Simscape).
Note
The parsim
function does not support fast
restart for models that contain To File
blocks.
Option to transfer variables to parallel workers, specified as
"off"
or "on"
. When
TransferBaseWorkspaceVariables
is set to
"on"
, variables used in the model and everything
defined in the base workspace are transferred to the parallel
workers.
The TransferBaseWorkspaceVariables
option is not
recommended for large scale simulations. Transferring unnecessary base
workspace data leads to wasted network and memory usage. Instead,
consider using project startup scripts, model callback functions, or the
SetupFcn
name-value argument to define the base
workspace data.
Option to open Simulation Manager, specified as
"off"
or "on"
. When
ShowSimulationManager
is set to
"on"
, you can use the Simulation
Manager to monitor simulations.
Option to skip remaining simulations if simulation error occurs,
specified as "off"
or "on"
.
Setting StopOnError
to "on"
stops
the execution of simulations if a simulation error occurs.
A location path for saving simulation outputs (including logging and
input metadata) while running parsim
the
simulink.multisim.DesignStudy
object. The
OutputLocation
is specified as one of these:
'cluster-storage'
– By default,OutputLocation
uses a backing job or parallel pool storage to writer simulation results. The lifetime of the storage is tied to the lifetime of the underlying parallel job. For interactive pools, the job is deleted on pool shutdown.'memory'
– In-memory storage where output data is stored in-memory and returned directly to the MATLAB client.'locationPath'
– Absolute path, specified as a string or a character vector.
Output Arguments
Simulation results and metadata, returned as an array of
Simulink.SimulationOutput
objects, or a vector. The
Simulink.SimulationOutput
object contains all data
logged from simulation as well as metadata about the simulation, including
timing information and diagnostics.
Extended Capabilities
If you have Parallel Computing Toolbox installed, then when you use parsim
, MATLAB automatically opens a parallel pool of workers on your local machine.
MATLAB runs the simulations across the available workers. Control parallel
behavior with the parallel preferences, including scaling up to a cluster.
For details, see Running Multiple Simulations.
Version History
Introduced in R2017a
See Also
Objects
Tools
Functions
applyToModel
|setBlockParameter
|setModelParameter
|setInitialState
|setExternalInput
|setVariable
|validate
|setPreSimFcn
|setPostSimFcn
|cancel
|fetchNext
|fetchOutputs
|wait
Topics
- Rapid Accelerator Simulations Using Parsim
- Parallel Simulations Using Parsim: Test-Case Sweep
- Parallel Simulations Using Parsim: Parameter Sweep in Normal Mode
- Parallel Simulations Using Parsim: Parameter Sweep in Rapid Accelerator Mode
- Running Multiple Simulations
- Run Parallel Simulations for a Thermal Model of a House Using parsim
- Run Parallel Simulations
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)