Error when running a model in a loop, programmatically, when using fast restart

I have a model that runs in a loop such that the result of first iteration is used in the second iteration, result from second is used in third and so on. The model works fine in normal mode (through script and also through UI). Then I use 'Fast Restart' to run the model through UI, and it works as expected i.e. running the model many times gives the desired behavior of a loop as output from first run is used in the next and so on. The issue comes when running the 'Fast Restart' mode through script (which has loops).
Error message:
Error using TWCmodel_global (line 90) Can not change the start time when initialized in Fast Restart
I believe the issue is due to the fact that I am using an ill-formed 'sim' function i.e. I am passing a second parameter as the StopTime but I am not able to find any documentation on this usage:
sim('GasFlow', <StopTime>);
But even if the syntax is wrong, the code still works in Normal Mode (without Fast Restart), so not sure what is going on.
How to fix this?

 Accepted Answer

You are correct in that it is something about the 'sim' syntax. You are trying to use the backwards compatible syntax. To view the explanation, please run the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'simulink/slref/sim.html'))
The issue is that when we turn on fast restart, we also require that the model uses "Single Simulation Output," which means that the output is returned in a 'Simulink.SimulationOutput' object instead of the workspace. This prevents the use of the backwards compatible syntax. This is described in the documentation. To view it, run the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'simulink/ug/how-fast-restart-improves-iterative-simulations.html?searchHighlight=Simulink.SimulationOutput'))
For more information on "Single Simulation Output," please refer to the documentation by executing the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'simulink/gui/single-simulation-output.html'))
Workaround: Convert to using the modern syntax that uses the Simulation Output and Simulation Input objects. This will avoid the error and has the added benefit of the extra containerization that these objects provide. See an example below. With this, you can call sim once with an array of Simulation Input objects, which tends to be cleaner than 'sim' in a for loop.
To view the relevant documentation, execute the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'simulink/slref/simulink.simulationinput-class.html?searchHighlight=simulationinput'))
Please follow the link below to search for the required information regarding the current release:

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!