MATLAB Answers

simulink/simscape final state 'cannot find a matching block state to element 135' output by the same model

4 views (last 30 days)
jessupj
jessupj on 15 Sep 2020
Edited: jessupj on 17 Sep 2020
I have a model that contains some simulink and simcape model.
The gist is: initail simulation from T0 to T1, perfrom some analysis, then restart at T1 and run to T2, etc. But this breaks when restarting T1. Kindly note: This intermediate analysis affects workspace parameters that affect values of e.g. electrical resistances in the model.
cset = getConfigSet( gcs, 'Configuration1' );
set_param(cset, 'Solver','VariableStepAuto')
set_param(cset, 'SolverName','VariableStepAuto')
set_param(cset, 'LoadInitialState','off');
set_param(cset, 'StateSaveName', 'state_history');
set_param(cset, 'SaveFinalState','on')
set_param(cset, 'FinalStateName','xFinal')
set_param(cset,'StartTime', string(T0) );
set_param(cset,'StopTime', string(T1) );
simOUT = sim( gcs, cset );
assignin('base','xFinal',simOUT.get('xFinal'))
set_param(cset, 'LoadInitialState','on')
set_param(cset,'InitialState', 'xFinal')
set_param(cset,'StartTime', string(T1) );
set_param(cset,'StopTime', string(T2) );
simOUT = sim( gcs, cset );
The first run is fine. everything looks fine. The second model call yields:
Unable to load the specified initial state for model 'your_model'. Cannot find a matching block state
corresponding to element 135 of signals structure array.
Nothing has changed in the model, and the error sound like as if a block has simply vanished.
This is field is present in the first run's output:
simOUT.xFinal.signals(135)
ans =
struct with fields:
values: 16.6848539456316
dimensions: 1
label: 'CSTATE'
blockName: 'your_model/comp1/C'
stateName: 'your_model.comp1.C.p.v'
inReferencedModel: 0
and is the exact same thing stored in 'xFinal' that's parsed to the model workspace and pushed as an initial state.
I'm wondering if restarting models with simscape elements need further considerations than those shown in : https://www.mathworks.com/help/simulink/slref/saving-and-restoring-simulation-operating-point.html

  0 Comments

Sign in to comment.

Accepted Answer

Paul
Paul on 15 Sep 2020
Edited: Paul on 15 Sep 2020
Are you trying to use operatining points? If you are:
Comparing your code to the examples in that link to the doc two things stand out ....
In your first call to sim do you need 'SaveOperatingPoint', 'on' ?
In your second call to sim, should you be setting the initial time to T1? The doc explicitly says:
"The start time value must remain 0.0 (that value was the start time of the original simulation). The software stores the original simulation start time in xFinal. This value must match the start time of the current simulation to enable restoration of the simulation state." So in your case maybe you should set the start time to T0 in the second call to sim.
I also came across this tip: Before you save the operating point, disable the Block Reduction parameter in Configuration Settings > Simulation Target > Advanced Parameters.

  7 Comments

Show 4 older comments
jessupj
jessupj on 17 Sep 2020
I was changing block values by manipulating workspace variables. All blocks were set for compilation time, rather than run-time interpretation. I changed those which needed to be dynamic to run-time interpretation, which led to some new problems but errors for those made sense.
Namely, changing this configuration mode required that I supply derivatives to my driving file, which was trivial, but that was the only block-diagram change that i needed to make.
I was also declaring global variables to pass values to the model, rather than pushing them to the model workspace via 'assignin'. Here are chagnes to the original post ultimately what worked:
cset = getConfigSet( gcs, 'Configuration1' );
modelworkspace = get_param(gcs,'ModelWorkspace');
assignin(modelworkspace,'B',B)
set_param(cset, 'Solver','VariableStepAuto')
set_param(cset, 'SolverName','VariableStepAuto')
set_param(cset, 'LoadInitialState','off');
set_param(cset, 'StateSaveName', 'state_history');
set_param(cset, 'SaveFinalState','on')
set_param(cset, 'FinalStateName','xFinal')
set_param(cset,'StartTime', string(T0) );
set_param(cset,'StopTime', string(T1) );
simOUT = sim( gcs, cset );
B = determine_new_values('state_history');
assignin(modelworkspace,'B',B)
assignin('base','xFinal',simOUT.get('xFinal'))
set_param(cset, 'LoadInitialState','on')
set_param(cset,'InitialState', 'xFinal')
set_param(cset,'StartTime', string(T1) );
set_param(cset,'StopTime', string(T2) );
simOUT = sim( gcs, cset );
This is working now, and I consider it to be closed. Thanks so much for your help and the same goes to Fangjun Jiang.
My interpretation of what happend is that 'block135' was dependent on one of the values i was changing. Rather than toss me that error that something had changed in the model/values since compilation, it gave an error that it couldn't find the corresponding block property in the restart file. That became more evident when using SaveCompleteFinalSimState and not modifying StartTime; the model would not restart because block values had been modified since it was compiled.
What a ridiculous debug ride. Thanks again. I'll accept this as the answer, and give props to Jiang by accepting another (related, earlier) open question since that is now also resolved.
Paul
Paul on 17 Sep 2020
I'm glad you got it working the way you want, but if you don't mind I still have some questions so I can learn from your experience.
"I was changing block values by manipulating workspace variables." What exactly do you mean by "block values?"
Are you talking about dialog parameters like Amplitude in a Sine Wave block? Or are you talking about other parameters that you can only set via set_param?
Variables in which workspace? The model workspace or the base workspace or something else?
"All blocks were set for compilation time, rather than run-time interpretation."
I'm not familiar witth this terminology. How do you change from compilation time to run-time interpretation? Are yout talking about in-lined vs tunable pararameters?
In your code snippet the variable modelworkspace isn't used?
The next line is:
assignin(gcs,'B',B)
At this point, B is not defined, perhaps it's defined earlier?
How does assignin work with gcs being the first argument? For your use case, I would have expected the first argument to be either 'base', 'caller' or a Simulink.ModelWorkspace object, none of which is returned by gcs (I don't believe). Same question for the later call to assignin. Maybe I don't understand all of the usages of assignin.
I guess you decided to not use the operating point approach. Is that because it wasn't needed or because it didn't suit your needs.
Finally, for the approach that you are using did you find anything in the doc pages that tell you what changes are and are not valid to make after the first call to sim and before the second call to sim?
jessupj
jessupj on 17 Sep 2020
gcs isn't defined either, unless the model has been opened. what i've provided is a relevant snippet of ~400lines of code plus calls to subfunctions rather than the actual code which is, naturally, messy.
surely i meant 'assign(modelworkspace,...' in those lines
block values, i mean thinks like the capacitance of a capacitor, or the resistance of a resistor. amplitude of a sine wave block i guess is similar.
as far as 'compilation time interpretation' vs 'run-time interpretation' of parameters. the simscape blocks (and maybe it's true of simulink in general --. all our stuff is electrical analogs of fluids so we're using simulink) have a setting depicted in is image:
i'm sure there is a better word
i didn't try to too hard (yet) to get a model operating point method set, although i think jiang's advice was leading that way also. i only tried it once, and taht was when i got the error that the model had been changed -- since the values changed were set to compilation-time interpretation -- and the error suggtested i rerun the spin-up (or whatever you call the t=0 to t=operating point simulation) to resolve the problem

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 15 Sep 2020
This is frustrating! Try this, note the difference between 'Save complete SimState in final state' and 'Save Final States'

  2 Comments

jessupj
jessupj on 15 Sep 2020
Frustrating indeed. Unfortunately, this wasn't very successful either. It seems that by setting the complete state in the output, I need an additional work-around to overcome the fact that I'm changing model parameters in before restarting. This gives me an error like:
Simulink cannot load the initial operating point because the model, 'your_model', was changed after the operating
point was saved. Run the simulation again and resave the operating point.
I feel so naive to think that you could just take the previous output, edit values, and restart it easily. I appreciate your help and insights though!
Fangjun Jiang
Fangjun Jiang on 16 Sep 2020
Maybe you should consider a different approach. Use set_param(Model,'SimulationCommand','Pause') to pause the simulation, change parameters, update model and then continue the simulation. The pause can be triggered by a callback function of a block in the model, which compares the clock with T1.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!