The Drawback
Let’s begin with this easy mannequin, which makes use of 3 variables outlined within the MATLAB base workspace: okay, c, and m:
I can efficiently simulate this mannequin:
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = sim(in,‘ShowProgress’,‘off’);
plot(out(1).logsout.get(‘x’).Values); maintain on
plot(out(2).logsout.get(‘x’).Values);
Now, let’s attempt to use parsim to simulate the mannequin in parallel with completely different values of okay:
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in,‘ShowProgress’,‘on’,‘ShowSimulationManager’,‘on’);
What Occurred?
Let’s look in particulars at what occurred. For that, we are able to have a look at the progress displayed by parsim. The very first thing we see is that parsim begins a parallel pool.
[08-Nov-2022 14:11:17] Checking for availability of parallel pool…
Beginning parallel pool (parpool) utilizing the ‘Processes’ profile …
Related to the parallel pool (variety of employees: 2).
Beginning a parallel pool means the MATLAB session you might be interacting with (the Shopper within the subsequent picture) is launching new MATLAB classes within the background, with out the person interface (the Staff within the subsequent picture)
As soon as the parallel employees are up and operating, we begin Simulink on every employee:
[08-Nov-2022 14:11:39] Beginning Simulink on parallel employees…
To keep away from conflicts between employees, we configure every of them to make use of a special cache folder:
[08-Nov-2022 14:11:46] Configuring simulation cache folder on parallel employees…
The mannequin is loaded on every employee:
[08-Nov-2022 14:11:47] Loading mannequin on parallel employees…
Lastly, the mannequin is being simulated:
[08-Nov-2022 14:11:52] Operating simulations…
Hopefully, you see the place I’m going with that… the parallel employees must be configured! Within the above instance, “be configured” means loading variables within the base workspace. In case you are coping with giant complicated fashions, you in all probability perceive that “be configured” can imply lots of issues.
What we’ll focus on on this submit are the completely different choices provided by parsim and batchsim to automate the configuration of the parallel employees
A number of Choices to Configure Parallel Staff
Articles have been printed on this weblog on the subject of parallel simulations:
Should you undergo these posts, you will note that the method of configuring parallel employees have been considerably simplified all through the years. As of MATLAB R2022b, parsim presents many choices to mechanically configure the parallel employees.
Here’s a image giving an outline of what parsim configures mechanically versus what you possibly can optionally opt-in.
Within the subsequent sections, we’ll cowl the next choices to configure parallel employees:
- Information Dictionary and Mannequin Workspace
- Transferring the Shopper Base Workspace
- Challenge Startup Duties
- Mannequin Callbacks
- SetupFcn Callback
- preSimFcn Callback
Information Dictionary and Mannequin Workspace
mdl = ‘simpleSuspensionWithSldd’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in,‘ShowProgress’,‘off’);
Transferring the Shopper Base Workspace
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in,‘TransferBaseWorkspaceVariables’,‘on’,‘ShowProgress’,‘on’);
What are the professionals and cons of this method?
- Professional: Simplicity, one extra argument and we mechanically copy all the shopper base workspace to the employees.
- Con: Presumably inefficient if the shopper session has many giant variables in its base workspace that aren’t wanted by the simulation.
Challenge Startup Duties
openProject(‘ParsimDataManagment.prj’);
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in,‘ShowProgress’,‘off’);
Mannequin Callbacks
When the mannequin can be loaded on the employees, the preLoadFcn callback will execute and outline the variables wanted by the mannequin.
mdl = ‘simpleSuspensionWithCallback’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in,‘ShowProgress’,‘off’);
SetupFcn Callback
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable(‘okay’,10);
in(2) = in(2).setVariable(‘okay’,20);
out = parsim(in, ‘SetupFcn’,@()mySetupFunction());
With the setup operate being:
operate mySetupFunction()
evalin(‘base’,‘c=3;okay=5;m=2;’);
finish
preSimFcn Callback
mdl = ‘simpleSuspension’;
in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setPreSimFcn(@(in) presim(in,10));
in(2) = in(2).setPreSimFcn(@(in) presim(in,20));
out = parsim(in);
With the presim operate:
operate in = presim(in, okay)
in = in.setVariable(‘okay’,okay*rand);
in = in.setVariable(‘c’,3);
in = in.setVariable(‘m’,2);
finish
Now it’s your flip
Tell us within the feedback under which combos of the strategies listed on this submit you employ to arrange your parallel simulations.