simulink c: advanced features
TRANSCRIPT
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 1
Simulink C: Advanced features
Robert GreplInstitute of Solid Mechanics, Mechatronics and Biomechanics
Faculty of Mechanical Engineering, Brno University of Technology
2008-2016
Simulating Dynamic Systems
After the click on “Run”, Simulink does following: [Simulink docs]
Model Compilation– Evaluates the model's block parameter expressions to determine their
values.
– Determines signal attributes, e.g., name, data type, numeric type, and dimensionality.
– A process called attribute propagation is used to determine unspecified attributes.
– Performs block reduction optimizations.
– Flattens the model hierarchy by replacing virtual subsystems with the blocks that they contain.
– Determines the block sorted order.
– Determines the sample times of all blocks in the model whose sample times you did not explicitly specify.
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
2
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 2
Simulating Dynamic Systems
Link Phase– In this phase, the Simulink engine allocates memory, creates method
execution lists(the most efficient order in which to invoke a model's block methods to compute its outputs).
Simulation Loop Phase– Loop Initialization phase – occurs once at the start of the loop.
– Loop Iteration phase – repeated once per time step from the simulation start time to the simulation stop time.
1. Computes the model's outputs.
2. Computes the model's states - the Simulink engine computes a model's states by invoking a solver.
3. Optionally checks for discontinuities in the continuous states of blocks – Zero-Crossing Detection.
4. Computes the time for the next time step.
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
3
Solver
Solver = algorithm for computing model states. How to compute model states? This depends on the type of the dynamical system.
Discrete-Time System – described by difference equation(s)
– solution = evaluation of an equation
– Solver in Simulink: Fixed-Step / discrete
Continuous-Time System – described by ordinary differential equations = ODE
– solution = numerical integration.
– Solvers in Simulink: Fixed-Step / ode1, ode2, ode3,ode4, ode5,ode8, ode14xVariable-Step / ode45, ode23, ode113, ode15s, ode23s, ode23t, ode23tb
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
4
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 3
Solvers – ode1 = Euler
initial value problem:
Euler approximation of solution:
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
5
0( ) ( ( ), ), ( 0)y t f y t t y t y
1 ( , )n n n ny y hf y t
Solvers
What means “Fixed step” and “Variable step”?
types of solvers– fixed step
– variable stepThe variable-step solvers in the Simulink product dynamically vary the step size during the simulation. Each of these solvers increases or reduces the step size using its local error control to achieve the tolerances that you specify. Computing the step size at each time step adds to the computational overhead but can reduce the total number of steps, and the simulation time required to maintain a specified level of accuracy. [Simulink help – Choose a Solver]
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
6
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 4
How solver determines the time step?
Experiment: solver: ode45
model: sine input, integrator.
1. t_sim = 10 -> number of steps = 51
2. t_sim = 110 -> number of steps = 51
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
7
How to get number of steps?1. Set “Save data to
workspace”in Scope.2. length(ScopeData.time)
How solver determines the time step?
What is happening if the time of simulation further increase?
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
8-100 0 100 200 300 400 5000
20
40
60
80
100
120
140
160
180
200
time of simulation
nr.
of
ste
ps
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 5
How solver determines the time step?
Experiment #2: Minimalistic model
Warning: The model 'example_minimalistic_model' does not have continuous states, hence Simulink is using the solver 'VariableStepDiscrete' instead of solver 'ode45'.
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
9
How solver determines the time step?
Experiment #3:
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
10
-100 0 100 200 300 400 5000
20
40
60
time of simulation
nr.
of
ste
ps
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 6
How solver determines the time step?
Now, let’s study the length of time step.Experiment #4:
o model = mechanical oscillator
o stiffnes k will be changed and the length of time step plot.
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
11
0 5 10 15 200.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
k = 0.01
2
10
25
100
How solver determines the time step?
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
12
Nonlinear model
k = 10
b = 2
How the step size is changed using different solvers?
0 5 10 15 20-1
-0.5
0
0.5
1
0 5 10 15 200
0.1
0.2
0.3
0.4
0.5
t [s]
ste
p s
ize
ode45
ode15s
ode23
ode45
ode15s
ode23
{SL07}
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 7
Sample Times
Discrete sample time[positive_num offset]
Continuous sample time [0 0]
Fixed-in-Minor steps [0 1]Simulink does not execute the block at the minor time steps; updates occur only at the major time steps. This process eliminates unnecessary computations of blocks whose output cannot change between major steps. [help]
% [0 offset] : Continuous sample time
%
%
% [-1, 0] : Inherited sample time
% [-2, 0] : Variable sample time
block.SampleTimes = [0 0];
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
13
Run simulation programmatically – command sim
command sim:simOut = sim('model', 'ParameterName1',Value1,...)
simOut = sim('pendulum','StopTime', '15')
How to plot results? ScopeData = simOut.get('ScopeData')
plot(ScopeData.time, ScopeData.signals.values,'b-o')
simOut.who – lists variables available in the output object
Alternatively, you can say:simOut = sim('model', ConfigSet)
where ConfigSet includes all parameters. How to get ConfigSet:configSet = getActiveConfigSet('pendulum')
– This may be useful, the several sets can be stored, ...
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
14
{SL07}
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 8
Run simulation programmatically – sim old-fashioned
Backwards Compatible Syntax[T,X,Y] =sim('model',Timespan, Options, UT)
options = simset('Refine',2)
[T,X,Y] = sim('pendulum',[0 15], options)
plot(T,X(:,1),'k-o')
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
15
Run simulation programmatically – sim_set
Alternative – start, stop, pause, continue simulation usingsim_set
set_param('my_pendulum','SimulationCommand','start')
get_param('my_pendulum','SimulationStatus')
set_param('my_pendulum','SimulationCommand','pause')
set_param('my_pendulum','SimulationCommand','WriteDataLogs')
set_param('my_pendulum','SimulationCommand','stop')
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
16
{SL08}
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 9
Control and Display the Sorted Order
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
17
Custom Blocks
Custom block types:
SubSystem
Fcn
Interpretted MATLAB Function
MATLAB Function
Level-2 MATLAB S-Function
C MEX S-Function
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
18
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 10
S-functions
How S-Functions works? -> How Simulink works? Dynamical system:
output is calculated based on states and input
states are discrete and continuous:
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
19
An S-function comprises a set of S-function callback methods that perform tasks required at
each simulation stage.
S-function template msfuntmpl_basic
Template for s-function consists of these functions:function my_sfcn(block)
setup(block);
function setup(block)
function DoPostPropSetup(block)
function InitializeConditions(block)
function Start(block)
function Outputs(block)
function Update(block)
function Derivatives(block)
function Terminate(block)
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
20
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 11
S-function – Major and minor steps
What is Minor step:Some continuous solvers subdivide the simulation time span into major and minor time steps, where a minor time step represents a subdivision of the major time step. The solver produces a result at each major time step. It uses results at the minor time steps to improve the accuracy of the result at the major time step. [help]
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
21
S-function – How to animate simulation results
model penddemo, s-function pendan.m
Some tricks...:– The handle to Figure (properties, lines, ...) is stored in ‘UserData’ of
the S-function Simulink block. To acces Figure, say: Fig = get_param(gcbh,'UserData');
– Use line + set(hline,'XData',x,...) instead of plot(...)
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
22
{SL10}
Mechlab, Brno University of Technology (R. Grepl, [email protected]) Page 12
Optimizing Graphics Performance
Set automatic-mode properties to manual whenever possible to prevent MATLAB from performing unnecessary operations.
Modify existing objects instead of creating new ones.
Use low-level core objects when creating objects repeatedly.
Do not recreate legends or other annotations in a program loop; add these after you finish modifying the graph.
Set the text Interpreter property to none if you are not using TeX characters.
Try various renderers and erase modes. MATLAB might not have auto-selected the fastest renderer for your application.
www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology
23
www.mechlab.czInstitute of Solid Mechanics, Mechatronics and Biomechanics
Faculty of Mechanical EngineeringBrno University of Technology
Contakt: assoc. prof. Robert Grepl, [email protected], tel.: +420 5114 288