Simscape: switch subsystem during simulation
1 view (last 30 days)
Show older comments
Hello, I have a variant-subsystem that contains physical elements.
- Variant 1 should be active for the first 25% of the simulation.
- Variant 2 should be active for 50%.
- The remaining 25% of the simulation should be active again in variant 1.
How can I do that? Unfortunately, the Atomic subsystem doesn't work. I have set up the variant subsystem, but I find it very complicated to set this up correctly. I ask for help.
Answers (1)
Simar
on 10 Jun 2024
Edited: Simar
on 19 Jun 2024
Switching variants in Simscape based on simulation time, one can use variant subsystems controlled by variant control variables. This requires leveraging Simulink's “MATLAB Function blocks” or ”Stateflow charts” to adjust these variables during simulation.
It is crucial to recognize, that while control logic can be adapted, Simscape networks demand continuity and cannot modify their physical structure instantaneously during simulation. An alternative strategy involves simulating configuration switches by activating or deactivating specific model paths as follow:
1.Define Variant Control Variables: Define variant control variables in the MATLAB workspace or a data dictionary that correspond to the different states of system (e.g., VariantCondition1, VariantCondition2).
2.Configure Variant Subsystem: Set up variant subsystem with the variant choices controlled by these variables. Each variant will represent a different configuration of the physical system.
3.Control Logic for Switching: Use a “MATLAB Function block” or a “Stateflow chart” to implement logic based on the simulation time (simTime) that updates the variant control variables. This logic can check the percentage of the simulation time elapsed and update the variant control variables accordingly.
For example, in MATLAB Function block:
function variantSwitch(simTime, totalSimTime)
if simTime/totalSimTime <= 0.25 || simTime/totalSimTime > 0.75
VariantCondition1 = 1; % Activate Variant 1
VariantCondition2 = 0;
elseif simTime/totalSimTime > 0.25 && simTime/totalSimTime <= 0.75
VariantCondition1 = 0;
VariantCondition2 = 1; % Activate Variant 2
end
assignin('base','VariantCondition1',VariantCondition1);
assignin('base','VariantCondition2',VariantCondition2);
end
Note: This approach requires that simulation is set to stop and then restart at switching points, as direct runtime changes to physical network configurations are not supported within a continuous simulation step.
4. Simulating Disabling a Block: To simulate turning off a block like a fuel cell during simulation, consider using a controlled source (for electrical systems) or a controlled flow (for hydraulic systems) that can be set to effectively zero or bypassed based on the simulation condition. This does not remove the block from the simulation but can simulate its inactivity.
5. Simulation Settings: Ensure simulation settings, especially the solver settings, are compatible with the dynamic nature of the model. Solver settings might need to be adjusted to ensure stability and accuracy.
Important Considerations:
- Simscape requires continuity in its equations and physical networks. Directly switching physical configurations mid-simulation can introduce discontinuities that are not physically realizable.
- Implementing the control logic for variant switching outside the Simscape domain (e.g., using MATLAB Function blocks or Stateflow) and then mapping these controls onto Simscape elements is a common strategy.
- Testing the model extensively to ensure that the switching logic works as intended and does not introduce numerical instability is crucial.
This approach allows for simulation of systems that change configuration over time, though with some limitations inherent to the simulation of physical systems.
Please refer to the following documentation links-
- Variant Subsystems- https://www.mathworks.com/support/search.html/videos/how-to-use-variant-subsystem-in-simulink-1691040230334.html?fq%5B%5D=asset_type_name:video&fq%5B%5D=category:simulink/index&page=1
- MATLAB Function Blocks- https://www.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html?searchHighlight=matlab%20function%20blocks&s_tid=srchtitle_support_results_1_matlab%20function%20blocks
- Stateflow Chart- https://www.mathworks.com/help/stateflow/api/stateflow.chart.html?searchHighlight=stateflow%20chart&s_tid=srchtitle_support_results_1_stateflow%20chart
Hope it helps!
Best Regards
Simar
See Also
Categories
Find more on Nonlinear Operators in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!