Issue with resuming simulation from saved operating point

4 次查看(过去 30 天)
I am running a Simulink file iteratively through a loop in MATLAB. What I want to acheive is that during the first iteration, the simulation would run from time zero to the first time step and save the final operating point. In the following iterations, the simulations would be initialized by the final operating point of the previous iteration and the final operating point of the current iteration would again be saved to be used by the following iteration. In other words, the program would run seamlessly from time 0 to the end time point. (example: simulation runs from 0 to 10ms, then 10ms to 20ms, then 20ms to 30ms... etc).
In addition, before running each simulation segment , I would like to solve the load flow for the system first.
This is the part that does that in my loop:
if i<2
siminOP1 = Simulink.SimulationInput(pdmodel);
siminOP1 = setModelParameter(siminOP1,'StartTime','0','StopTime',num2str(step),...
'SaveFinalState','on','SaveOperatingPoint','on');
LF = power_loadflow(pdmodel,'solve');
op1 = sim(siminOP1);
elseif i<3
siminOP2 = Simulink.SimulationInput(pdmodel);
siminOP2 = setInitialState(siminOP2,op1.xFinal);
siminOP2 = setModelParameter(siminOP2,'StopTime',num2str(time+step),...
'SaveFinalState','on','SaveOperatingPoint','on');
LF = power_loadflow(pdmodel,'solve'); %**************************ERROR HERE******************************
op1 = sim(siminOP2);
else
siminOP2 = setInitialState(siminOP2,op2.xFinal);
siminOP2 = setModelParameter(siminOP2,'StopTime',num2str(time+step),...
'SaveFinalState','on','SaveOperatingPoint','on');
LF = power_loadflow(pdmodel,'solve');
op1 = sim(siminOP2);
end
However, for i =2, I get the following error when the compiler attempts to run the load flow line:
Simulink cannot load the initial operating point because the model, 'cosim_pd_29',
was changed after the operating point was saved. Run the simulation again and resave
the operating point.
If you could help me resolve this issue or help me restructure the code for the loop in a better way I would really appreciate it.
Thank you very much for your time and effort.

回答(1 个)

SANKALP DEV
SANKALP DEV 2023-11-6
Hello Deemah,
I understand that you are facing a problem while running a file iteratively through a loop in MATLAB.
The error you are encountering indicates that the Simulink model was modified after saving the initial operating point. This inconsistency prevents the model from correctly loading the operating point during the second iteration.
To resolve this issue, you can consider two possible approaches:
  1. Reordering the code: Move the load flow calculation (power_loadflow) before creating the simulation input object (siminOP2) in the second iteration. By doing this, you ensure that the model is not modified between the load flow calculation and saving the operating point. Here is an example of how you can restructure your code.
elseif i<3
LF = power_loadflow(pdmodel,'solve');
siminOP2 = Simulink.SimulationInput(pdmodel);
siminOP2 = setInitialState(siminOP2,op1.xFinal);
siminOP2 = setModelParameter(siminOP2,'StopTime',num2str(time+step),...
'SaveFinalState','on','SaveOperatingPoint','on');
op1 = sim(siminOP2);
2.Explicitly saving and loading the operating point: Another option is to use the save and load functions to save and load the operating point explicitly. This approach ensures that the correct operating point is used in each iteration. Here's an example of how you can modify your code to implement this approach.
elseif i<3
load('op1.mat', 'op1'); % Load operating point
siminOP2 = Simulink.SimulationInput(pdmodel);
siminOP2 = setInitialState(siminOP2,op1.xFinal);
siminOP2 = setModelParameter(siminOP2,'StopTime',num2str(time+step),...
'SaveFinalState','on','SaveOperatingPoint','on');
op1 = sim(siminOP2);
save('op1.mat', 'op1'); % Save operating point
Hope it helps to resolve the issue.
Regards,
Sankalp

Community Treasure Hunt

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

Start Hunting!

Translated by