Main Content

Programmatically Create Multiple Scenarios in a Test Sequence Block

This example shows how to create and define multiple test scenarios in a single Test Sequence block. Being able to define more than one test sequence in a block lets you reduce the number of separate Test Sequence blocks in your test harness.

This example uses the HeatPumpScenario model, which already has a test harness that contains a Test Sequence block. In this example, you convert the block to use scenario, add a new scenario to the block, edit a scenario step, and activate the new scenario so that it runs when the model simulates.

Open Model and Test Harness

Open the Controller subsystem of the HeatPumpScenario model and its test harness, ScenarioTest.


Enable Scenarios

Set the Test Sequence block to use scenarios. The existing steps and transitions are moved into a scenario that, in this example, is named FirstScenario. Note that once you change a Test Sequence block to use scenarios, you cannot revert that block to non-scenario mode.

sltest.testsequence.useScenario('ScenarioTest/Test Sequence',...

Add Another Scenario

Add a second scenario to the Test Sequence block. Name the scenario NewScenario.

sltest.testsequence.addScenario('ScenarioTest/Test Sequence','NewScenario');

Edit the Scenario Contents

Edit the first step of the new scenario to change the values of the Troom_in and Tset variables. Preface the name of the step with the scenario name that contains the step. Similarly, when adding or changing transitions, you must also preface the transition with the scenario name.

action = sprintf('Troom_in = 75;\nTset = 75;\n');
sltest.testsequence.editStep('ScenarioTest/Test Sequence',...

To view the scenario contents, use sltest.testsequence.findStep(blockPath), which returns an array containing the step names for all scenarios. Then, use sltest.testsequence.readStep(stepName) or sltest.testsequence.readTransition(stepName) to see the contents of the specified step or transition, respectively. You can also view the scenario contents by double-clicking the Test Sequence block in the harness to open the block editor.

Specify Which Scenario to Run

Specify the new scenario to run during model simulation. This scenario is the active scenario, which is the only scenario that runs during the simulation. (For an alternative way to activate and run a scenario, see below.)

sltest.testsequence.activateScenario('ScenarioTest/Test Sequence',...

Run the Model Using the New Scenario

Run the model. You can run only one active scenario a time. Note that fast restart is supported when switching active scenarios and running the model.


In the test harness, view the Scope block to see the simulation results for the new scenario.

Run a Different Scenario

Activate the first scenario.

sltest.testsequence.activateScenario('ScenarioTest/Test Sequence',...

Rerun the model.


In the test harness, view the Scope block to see the simulation result for the first scenario.

Close Test Harness and Model


Activate and Run a Scenario Using a Workspace Variable

In some cases, such as looping through scenarios, you might want to use a workspace variable to control which scenario to activate, instead of using activateScenario. The steps for using a workspace variable are:

  1. Set the scenario control source to the workspace by using sltest.testsequence.setScenarioControlSource('ScenarioTest/Test Sequence',sltest.testsequence.ScenarioControlSource.Workspace);

  2. Create a variable in the base workspace, model workspace, or data dictionary to specify the active scenario using its index value. For example, Active_Scenario_Index = 1;

  3. Run the model using the steps and transitions in the active scenario.

To run a different scenario, change the Active_Scenario_Index to the desired scenario, for example, Active_Scenario_Index = 2, and then rerun the model.

To change the name of the active scenario parameter from Active_Scenario_Index to, for example, ScenarioIndex, use sltest.testsequence.editSymbol('ScenarioTest/Test Sequence',... 'Active_Scenario_Index','Name','ScenarioIndex'); and then create the ScenarioIndex variable in the base workspace. Use Scenario_Index = 2 to set the variable to run the scenario identified by index 2, and then run the model.

See Also

| | | | | |

Related Topics