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.
Controller subsystem of the
HeatPumpScenario model and its test harness,
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',... 'FirstScenario');
Add a second scenario to the Test Sequence block. Name the scenario
Edit the first step of the new scenario to change the values of the
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',... 'NewScenario.step_1','Action',action);
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.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 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',... 'NewScenario');
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.
Activate the first scenario.
sltest.testsequence.activateScenario('ScenarioTest/Test Sequence',... 'FirstScenario');
Rerun the model.
In the test harness, view the Scope block to see the simulation result for the first scenario.
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:
Set the scenario control source to the workspace by using
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;
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,
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.