Main Content

Run Parallel Simulations for a Thermal Model of a House Using parsim

This example shows how to use a Simulink.SimulationInput object to change block and model parameters and run simulations in parallel with those changes.

The example model sldemo_househeat is a system that models the thermal characteristics of a house, its outdoor environment, and a house heating system. This model calculates heating costs for a generic house.

Set Point block, Thermostat subsystem, Heater subsystem, House subsystem, and Cost Calculator component are the main components. For a detailed explanation of the model, see Thermal Model of a House.

Run Multiple Parallel Simulations with Different Set Points

This model uses a Constant block to specify a temperature set point that must be maintained indoors. The default value of set point value is 70 degrees Fahrenheit. This example shows you how to simulate the model in parallel using different values of Set Point

Note

Because this example loads the example files at a location in matlabroot, the model file cannot be loaded on parallel workers. To run the simulations in this example on parallel workers, set up the parallel workers to open the required files by running this command::

parfevalOnAll(@openExample,0,'simulink_general/sldemo_househeatExample')

Open the example and the model

openExample('simulink_general/sldemo_househeatExample');
open_system('sldemo_househeat');

Define a set of values for Set Point.

SetPointValues = 65:2:85;
spv_length = length(SetPointValues);

Using the defined values, initialize an array of Simulink.SimulationInput objects. Use these Simulink.SimulationInput objects to specify the Set Point values. In this step, to preallocate the array, the loop index is made to start from the largest value.

for i = spv_length:-1:1
    in(i) = Simulink.SimulationInput('sldemo_househeat');
    in(i) = in(i).setBlockParameter('sldemo_househeat/Set Point',...
        'Value',num2str(SetPointValues(i)));
end

This example produces an array of 11 Simulink.SimulationInput objects, each corresponding to a different value of Set Point.

Now, run these multiple simulations in parallel using the command parsim. To monitor and analyze the runs, open the Simulation Manager by setting the ShowSimulationManager argument to on. The ShowProgress argument when set to on shows the progress of the simulations.

out = parsim(in,'ShowSimulationManager','on','ShowProgress','on')

The output is generated as a Simulink.SimulationOutput object. To see all of the different set point values, open the plot of the Temperatures (Indoor and Outdoor) and the Heat Cost component. The constant block Avg Outdoor Temp specifies the average air temperature outdoors. The Daily Temp Variation Sine Wave block generates daily temperature fluctuations of outdoor temperature. The indoor temperature is derived from the House subsystem. The temperature outdoor varies sinusoidally, whereas the temperature indoors is maintained within 5 degrees Fahrenheit of the set point.

In the absence of the Parallel Computing Toolbox™ licenses, the parsim command behaves like the sim command. The simulations run in serial.

View the Runs in the Simulation Manager

Setting the ShowSimulationManager argument to on enables the Simulation Manager. For more information, see Simulation Manager.

You can view the status of all the runs and detailed information about them.

The Simulation Manager enables you to view your results in the Simulation Data Inspector, which in turn allows you to analyze and compare your data. You can view the plot of the Temperatures (Indoor and Outdoor) and the Heat Cost in Simulation Data Inspector. Select the runs for which you want to view the plot and click icon.

You can now see the heat cost for three simulations.

Using the Simulation Manager, you can apply the parameters of any run to your model. Now, suppose that you want to apply the parameters of Run 3 to your model. Select Run 3 and click the icon. Your parameters are applied to the model.

See Also

Objects

Functions

Tools

Related Topics