Repeating/Looping a Simulink model (accessed by an embedded function within a Simulink model)

2 visualizzazioni (ultimi 30 giorni)
Hello everyone, this question is going to be quite lengthy, please bear with me.
If you want to save time, the main questions are highlighted in bold.
---
I'm going to start off with a description of my simulation:
Currently I am working on a material flow simulation using a combination of Matlab, Simulink and Stateflow. Matlab governs the overall simulation, e. g. initializing simulation objects or calling the Simulink (main) model.
Within the Simulink models, plants and system components are modeled using Stateflow charts. The required variables are estimated within interpreted Matlab functions which are accessed before and after running a Stateflow Chart.
In addition, an energy model evaluates continuous heat transfer processes happening simultaneously to the discrete material flow processes.
---
The problem: The Simulink model, as of now, needs one subsystem per component. In order to add a new component of an existing type, copying the subsystem is not enough, since variables need to be renamed (especially in the solely Simulink-based energy model, including numerous 'Goto' and 'From' blocks).
Considering the fact that there are about 40 plants included, extending the model or making changes to a plant type can be quite time consuming.
---
Therefore, my idea was to loop the execution of specific subsystems according to the number of plants. To achieve this, I came up with two solutions, neither of which seem to work.
1) Putting the components' models into separate model files, which are accessed within a for loop
I will try to llustrate this approach:
Main_Simulation.m (Main matlab file) starts the simulation:
sim('Simulation_V_x',[0 t_end])
Simulation_V_x, at some point, executes an Interpreted Matlab Function, e.g. 'Sim_Subsystem'
Sim_Subsystem accesses existing global variables:
global Num_Plants % Number of plants intended for simulation (e.g. Num_Plants = 5)
global t_sim % Elapsed simulation time
A for loop is used to run the the model 'Sim_Plant' multiple times
for current_Plant = 1:Num_Plants
sim('Sim_Plant',[1 1]) % Simulate only one time step (correct approach?)
end
Within the Simulink model 'Sim_Plant', the variables current_Plant and t_sim are provided using constant blocks. Existing clock blocks within the model are replaced by constant blocks providing the variable t_sim. Otherwise, the model remains the exact same as the subsystem within the main model 'Simulation_V_x'.
The problem with this approach: the variables current_Plant and t_sim can not be accessed within the model 'Sim_Plant'. To determine the source of this error, I tried running the model directly from a Matlab function.
This reduced the execution order from
Matlab-->Simulink(Main)-->Interpreted Matlab Function-->Simulink(Sub)
to
Matlab --> Simulink(Sub)
Using this approach, the function had access to the necessary variables. Judging from this, I presume the error stems from some limitation of the workspace, which only occurs when trying to access a Simulink model from within a function which is accessed by a Simulink model itself.
My question would now be: is there any way to get the needed variables into the model 'Sim_Plant' with the execution order being
Matlab-->Simulink(Main)-->Interpreted Matlab Function-->Simulink(Sub/Sim_Plant)
or is it not possible because of workspace limitations?
---
2) In a second approach I tried using the For Iterator block within the subsystem (with the subsystem remaining within the main model). However, this resulted in errors within every integrator block inside the model because of the sample time being set to 1. Once I changed every sample time to -1, clock blocks caused errors as well. Therefore, my question is: can For Iterator blocks be used within subsystems including clock blocks?
---
These are the two approaches I could think of to simplify model creation/extensions. I also considered creating the simulink models from looped Matlab code, but taking into account the complexity and the number of different model types, this could take weeks or more likely months to achieve.
I presume there is still a lot of crucial information missing. Please excuse me for this. I am not an expert Matlab user and am currently trying to find a way through this maze of a model and implement improvements on the way. If someone bothered reading through this and is willing to help: Thank you in advance!

Risposte (0)

Categorie

Scopri di più su Modeling in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by