parsim: Create SimulinkInput Object from currently open model with initial values

3 views (last 30 days)
Good morning everyone,
I am trying to parallelize a large number of Simulink simulations for a Monte Carlo study on a multicore Machine using parsim.
The Help on parsim details, that one has to create a "SimulinkInput object" for each simulation that will run in parsim.
I have my model set up for calculation, initial values and other external values loaded into the model.
What I do not understand is: How can I generate the SimulinkInput Object from my currently open Simulink model? Especially: Without looping over all Input values that are loaded in "from Workspace" blocks? (they are also initial values for my ode)
Currently I run my simulations in nested for loops over all the parameters that change during the study but I think of running the loop for all possible combinations, create the SimulinkInput object for each set of data and afterwards run everything in parallel using parsim, avoiding the nested loops. Is this even possible?

Accepted Answer

Reid Spence
Reid Spence on 23 Jun 2021
Hi Sebastian,
If I understand correctly you have a lot of variables and inputs in the base workspace, some of these variables change from run to run while others remain constant. Currently you have nested for loops to iterate over all possible combinations and Im guessing you are calling sim() in the deepest nested for loop?
Based on that understanding, I think the best way to transition your code to use parsim is to keep your nested for loops but instead of calling sim() instead use setVariable() on the SimulationInput object for only the variables that are changing from run to run. For all variables in the base workspace that are the same from run-to-run, you can leave those in the base workspace (no need to attach them directly to the SimuationInput object) and then transfer the base workspace to the workers with the "TransferBaseWorkspaceVariables" option on parsim.
Hope that helps
  2 Comments
S_L
S_L on 24 Jun 2021
Hi Reid,
thanks for your answer.
  • If I understand correctly you have a lot of variables and inputs in the base workspace, some of these variables change from run to run while others remain constant. Currently you have nested for loops to iterate over all possible combinations and Im guessing you are calling sim() in the deepest nested for loop?
  • Exactly. A lot of variables come from the base workspace and do not change like gas tables etc.
  • Some of them, especially the initial values, do change from loop to loop and are generated in the nested for-loops
  • Based on that understanding, I think the best way to transition your code to use parsim is to keep your nested for loops but instead of calling sim() instead use setVariable() on the SimulationInput object for only the variables that are changing from run to run. For all variables in the base workspace that are the same from run-to-run, you can leave those in the base workspace (no need to attach them directly to the SimuationInput object) and then transfer the base workspace to the workers with the "TransferBaseWorkspaceVariables" option on parsim.
  • Ok, I get the idea. So you are suggesting that I apply setVariable() to all of my "fromWorkspace" Blocks, that contain initial Values of my ode that will change during loops?
  • Is any change inside the model required?
  • Will I still be able to run a single simulation with sim() afterwards?
Regards
Sebastian
Reid Spence
Reid Spence on 24 Jun 2021
Exactly, yes use setVariable for the values you want to vary between runs. The Simulink.SimulationInput object does not affect the model file. The point of the input object is to store run specific information in a contained "box" so that you do not have to dirty the model or the workspace to sweep through the iterations. So by that philosophy, no changes to the mode should be required and you can still run a single simulation with sim() afterwards.

Sign in to comment.

More Answers (0)

Categories

Find more on Run Multiple Simulations in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!