How to run a simulink sim called from Matlab in a piecemeal fashion?

1 visualización (últimos 30 días)
OC
OC el 28 de Jul. de 2019
Hi,
I'm trying to run a simulink model from a Matlab script, the matlab generates a the inputs to the model, the model runs and returns the outputs to the workspace. The simulation is very long though so I would like to run the model piecemeal in multiple calls to the sim() function and each time I would like the simulink model to pick up where it left off on the previous call. The code below shows an example of what I mean with a very simple model.
As written this works fine using the LoadInitialState and saveFinalState arguments to sim() however for very long sims the inputs AB, TB will get huge and I will run out of memory. So, rather than generating the full inputs in one go I would like to generate new chunks of the inputs as needed (ie before each call to sim()). This also works fine PROVIDED the length of the inputs is identical for each call eg. If I run 100 samples as 50 samples followed by 50 samples ie set T = TB(1:50) for the first call and T = T(51:100) for the second. So it seems the sim() with LoadInitialState and saveFinalState behaves differently depending on the size of T, if length(T) is equal to the stop_time-current_time then it starts from the beginning of T on each sim() call but if the length of T is sim_dur then it 'remembers' where it left off on each subsequent call to sim().
Anyway, for my particular case I want to run the sim with UNEQUAL durations for each call to sim(), ie the commented out code in the sample below, this runs but does not give the expected output. Note that the padding with zeros was added to make the length of the inputs the same for all sim() calls but the 'stop_time' argument to each sim() call was expected to make it ignore these extra padded samples.
If I just supply inputs of differing lengths on each call to sim() (ie no padding) then the sim crashes with the error message:
Simulink cannot load the initial SimState because the model, 'TestSimscape', was changed after the SimState was saved. Run the simulation
again and resave the SimState.
So, is it possible to do what I want, ie to break the simulation of a Simulink model into multiple calls when the time to sim on each call is differnet and => the size of the inputs which come from the Matlab workspace are different ?
If not , can anyone explain why not ? My tests indicate that Simulink is perfectly capable of 'picking up where it left off' what causes the restriction that the input sizes musn't change and if this is a fundamental restriction why does it not work when I artificially pad the Matlab-generated inputs to make them the same size on each call to sim() ?
Thanks,
tic
TB = 1:100;
AB = -1:-1:-200;
sim_dur = 100/7.5e6;
T = TB; %(1:50);
A = AB; %(1:100);
cnt = 1;
stop_time = 50/7.5e6; %sim_dur; %100e-3;
simout = sim('TestSimscape.slx','StopTime',num2str(stop_time), ...
'LoadInitialState','off','SaveFinalState','on', ...
'FinalStateName','xFinal');
eval(['simout' num2str(cnt) ' = simout;']);
T = TB; %[TB(51:60) zeros(1,40)];
A = AB; %[AB(101:120) zeros(1,80)];
cnt = 2;
xInitial = simout.xFinal;
stop_time = 60/7.5e6;
simout = sim('TestSimscape.slx','StopTime',num2str(stop_time), ...
'LoadInitialState','on','SaveFinalState','on', ...
'FinalStateName','xFinal');
eval(['simout' num2str(cnt) ' = simout;']);
T = TB; %[TB(61:end) zeros(1,10)];
A = AB; %[AB(121:end) zeros(1,20)];
cnt = 3;
xInitial = simout.xFinal;
stop_time = sim_dur;
simout = sim('TestSimscape.slx','StopTime',num2str(stop_time), ...
'LoadInitialState','on','SaveFinalState','on', ...
'FinalStateName','xFinal');
eval(['simout' num2str(cnt) ' = simout;']);
toc

Respuestas (0)

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by