parsim
Simulate dynamic system multiple times in parallel or serial
Syntax
Description
simulates a model using the inputs specified in the simOut
= parsim(in
)SimulationInput
object, in. The parsim
command
uses an array of SimulationInput
objects to run
multiple simulations.
simulates a model in parallel using the inputs specified in the simOut
= parsim(in
,'ShowSimulationManager'
,'on'
)SimulationInput
object and opens the Simulation Manager
UI. For more information, see Simulation
Manager
.
simulates a model in parallel using the inputs specified in the simOut
= parsim(in
,Name,Value
)SimulationInput
object and the options specified as the
Name,Value
pair.
The parsim
command uses the Parallel Computing Toolbox™ license to run the simulations in parallel.
parsim
runs the simulations in serial if a parallel pool
cannot be created or if Parallel Computing Toolbox is not used.
Examples
Using parsim
with Rapid Accelerator
Simulate the model, vdp
, in rapid
accelerator mode.
Load the model.
model = 'vdp';
load_system(model)
This step builds the Rapid Accelerator target
Simulink.BlockDiagram.buildRapidAcceleratorTarget(model);
Create a SimulationInput
object and use
setModelParameter
method to set
RapidAcceleratorUpToDateCheck
to
'off'
.
in = Simulink.SimulationInput(model); in = in.setModelParameter('SimulationMode', 'rapid-accelerator'); in = in.setModelParameter('RapidAcceleratorUpToDateCheck', 'off');
Simulate the model.
out = parsim(in)
Simulate Model in Parallel with parsim
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')
[29-Jun-2022 15:05:52] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 6). [29-Jun-2022 15:07:01] Starting Simulink on parallel workers... [29-Jun-2022 15:07:34] Configuring simulation cache folder on parallel workers... [29-Jun-2022 15:07:35] Loading model on parallel workers... [29-Jun-2022 15:07:49] Running simulations... [29-Jun-2022 15:08:24] Completed 1 of 6 simulation runs [29-Jun-2022 15:08:24] Completed 2 of 6 simulation runs [29-Jun-2022 15:08:24] Completed 3 of 6 simulation runs [29-Jun-2022 15:08:27] Completed 4 of 6 simulation runs [29-Jun-2022 15:08:28] Completed 5 of 6 simulation runs [29-Jun-2022 15:08:28] Completed 6 of 6 simulation runs [29-Jun-2022 15:08:28] Cleaning up parallel workers... out = 1x6 Simulink.SimulationOutput array
Input Arguments
in
— Simulink.SimulationInput
object used to simulate the model
object, array
A Simulink.SimulationInput
object or an
array of Simulink.SimulationInput
objects
that is used to specify changes to the model for a simulation.
Example: in =
Simulink.SimulationInput('vdp')
Example: for i = 10:-1:1 in(i) = Simulink.SimulationInput('vdp');
end
Name-Value Arguments
Example: 'ShowProgress'
, 'on'
Note
All parameters passed to parsim
command are unrelated to
the parameters that are used with the sim
command. To pass to
the parsim
command, use the list of following input
arguments
Specify optional comma-separated pairs of Name,Value
arguments. Name
is the argument name and Value
is the corresponding value. Name
and Value
must appear inside single quotes (' '
). You can specify several
name and value pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
AttachedFiles
— Files to attach to parallel pool
cell array
Additional files to attach to the parallel pool, specified as a cell
array. parsim
does not support loading data from
From File blocks that make reference to mat files in
subdirectories.
ShowProgress
— Show the progress of the simulations
'on'(default) | 'off'
Set to 'on'
, to see the progress of the simulations
in the command window. The progress is hidden when set to
'off'
.
Note
When the progress is shown, a message 'Cleaning up
parallel workers..'
may 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
.
RunInBackground
— Run simulations in background
'off' (default) | '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
method on the
Simulink.Simulation.Future
objects.
SetupFcn
— Function handle to run once per worker
function handle
Specify a function handle
to
'SetupFcn'
to run once per worker before the
start of the simulations.
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.
CleanupFcn
— Function handle to run once per worker after running simulations
function handle
Specify a function handle
to
'CleanupFcn'
to run once per worker after the
simulations are completed.
ManageDependencies
— Manage model dependencies
'on' (default) | '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.
UseFastRestart
— Use fast restart
'off' (default) | 'on'
When UseFastRestart
is set to
on
, simulations run on the workers using fast
restart.
When performing a parameter sweep varying properties of a Simscape™ component (e.g. 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
When using parsim
, use the
UseFastRestart
option and not the
FastRestart
option. See Get Started with Fast Restart for more
information.
parsim
in fast restart mode does not support
models with ToFile blocks.
TransferBaseWorkspaceVariables
— Transfer variables to the parallel workers
'off' (default) | '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
option to define your base workspace
data.
ShowSimulationManager
— Starts the Simulation Manager app
'off' (default) | 'on'
When 'ShowSimulationManager'
is set to
'on'
, you can use the Simulation Manager App to
monitor simulations.
StopOnError
— Stop simulations on errors
'off' (default) | 'on'
Setting 'StopOnError'
to 'on'
stops the execution of simulations if an error is encountered.
Output Arguments
simOut
— Simulation object containing logged simulation results
object
Array of Simulink.SimulationOutput
objects that
contains all of the logged simulation results. The size of the array is
equal to the size of the array of
Simulink.SimulationInput
objects.
All simulation outputs (logged time, states, and signals) are returned in
a single Simulink.SimulationOutput
object. You define the
model time, states, and output that is logged using the Data
Import/Export pane of the Model Configuration Parameters
dialog box. You can log signals using blocks such as the To
Workspace and Scope blocks. The Signal
& Scope Manager can directly log signals.
Extended Capabilities
Automatic Parallel Support
Accelerate code by automatically running computation in parallel using Parallel Computing Toolbox™.
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
Simulink.SimulationInput
| Simulink.Simulation.Future
| applyToModel
| setBlockParameter
| setModelParameter
| setInitialState
| setExternalInput
| setVariable
| validate
| setPreSimFcn
| setPostSimFcn
| Simulation
Manager
| 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
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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 bat365 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)