modeling and simulation - unimore€¦ · simulation let’s consider a generic ode (ordinary...
TRANSCRIPT
Università degli Studi
di Modena e Reggio Emilia
Automation
Robotics and
System
CONTROL
Modeling and Simulation
Cesare Fantuzzi
University of Modena and Reggio Emilia
02/11/2011
1
Course syllabus
Part 1: Theory of Modeling and Simulation.
Part 2: Numerical simulation.
Part 3: How develop simulation projects using Matlab
and Simulink.
Part 4: Case studies.
21/03/2011 2
Part 2: Numerical Simulation
The purpose of this part is to discuss the properties of numerical Ordinary Differential Equation (ODE) solvers and their codes, as well as the algorithms behind these solvers.
Numerical simulation. Simulation time and physical time (simulation clock, time step).
Integration using Euler formula. Numerical stability of algorithm. Stiff Differential Equation. Fixed and variable simulation step.
Simulation Error Analysis.
Multi-domain simulation. Co-Simulation (Cooperative simulation).
21/03/2011 3
Time
When we simulate a continuous-time system on a digital
computer, some quantity will have to be discretized, as
we cannot update the state variables infinitely often
within a finite time period.
Most numerical ODE solvers discretize the time axis, i.e.,
they advance the simulation clock using finite time steps.
The time step, h, may either be fixed or variable.
21/03/2011 4
Principles of Numerical
Simulation
Let’s consider a generic ODE (ordinary differential
equation) which represents first order ordinary
differential equation (could be non-linear).
where x is the state vector, u is the input vector, and t
denotes the time, the independent variable across which
we wish to simulate.
We also require initial conditions for the state variables:
21/03/2011 5
)),(),(()( ttutxftx
00)( xtx
Taylor Series Expansion
The model can be simulated using a Taylor series
expansion. If we know the state vector at a certain
instant of time, t, the state vector can be calculated at
some later time instant, t + h by means of a Taylor series
expansion:
The state-space model is used to compute the first
derivative in the Taylor series:
21/03/2011 6
...!2
)()()()(
2
2
2
h
dt
txdh
dt
tdxtxhtx
...!2
)()),(),(()()(
2
2
2
h
dt
txdhttutxftxhtx
The Truncation Error
Numerical integration approximate a certain number of
terms of the Taylor series.
This number can be either fixed or variable.
Approximation order of the numerical method.
An algorithm that approximates the terms of the Taylor
series up to the third derivative:
is thus an algorithm of third-order.
The truncation error of the method grows proportionally
with the fourth power of the integration step size, h.
21/03/2011 7
)...(!3
)(
!2
)()()()( 4
3
2
22
hoh
dt
tfdh
dt
tdfhtftxhtx
The Explicit Euler Integration
The most simple numerical ODE solver is based on the
explicit so-called “Forward Euler” (FE) formula
First-order integration method:
21/03/2011 8
httxftxhtx
htxtxhtx
)),(()()(
)()()(
Iterative integration
When using explicit integration methods, the simulation
doesn’t require any iteration within an integration step,
unless the model contains algebraic loops:
21/03/2011 9
Time and Again
The step size, h, is not necessarily identical with the time
advance, dt, of model evaluations.
Many integration algorithms, such as the famous Runge-
Kutta algorithms, perform multiple model evaluations
within a single time step.
Thus, each time step, h, contains several micro-steps,
dt, whereby dt is not necessarily a fixed divider of h.
Instead, the simulation clock may jump back and forth
within each individual time step.
21/03/2011 10
Variable step solvers in
Simulink
The 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.
21/03/2011 11
Time and Again
Even if the integration algorithm used is such that dt remains
positive at all times, the simulation clock does not necessarily
advance monotonously with real time.
There are two types of error-controlled integration algorithms
that differ in the way they handle steps that exhibit an error
estimate that is too large.
– Optimistic algorithms simply continue, in spite of the exceeded error
tolerance, while reducing the step size for the subsequent step.
– conservative algorithms reject the step, and repeat it with a smaller
step size.
Thus, whenever a step is rejected, the simulation clock in a
conservative algorithm turns back to repeat the step, while
not committing the same error.
21/03/2011 12
Time and Again
Even if an optimistic algorithm with positive dt values is being
employed, the simulation clock may still not advance
monotonously with real time.
The reason is that integration algorithms cannot integrate
across discontinuities in the model.
Thus, if a discontinuity is encountered somewhere inside an
integration step, the step size must be reduced and the
step must be repeated, in order to place the discontinuity in
between subsequent steps.
21/03/2011 13
Example
21/03/2011 14
1
s+1
Transfer Fcn
Time
To Workspace1
simout
To WorkspaceCoulomb &
Viscous Friction
Clock
Chirp Signal
Variable Step
21/03/2011 15
Prompt Matlab
>> whos
>> % how long are the vector Time and simout?
>> plot(Time,’x’)
>> % how is spaced the vector Time?
>> plot(Time, simout, ‘x’)
>> % Do you see the variation on space in time are related
with the discontinuities in simout?
21/03/2011 16
Fixed Step
21/03/2011 17
Programmatic simulation
Entering simulation commands in the MATLAB
Command Window or from a MATLAB file enables you
to run unattended simulations.
For example, you can perform Monte Carlo analysis by
changing the parameters randomly and executing
simulations in a loop.
You can use the sim command to run a simulation
programmatically.
21/03/2011 18
Example
Run several simulations to test PID tuning parameters.
21/03/2011 19
Kg
s +(1/tau)s2
Transfer Fcn
SimTime
To Workspace1
simout
To WorkspaceStep
PID(s)
PID Controller
Clock
Add
Algebraic Loop
An algebraic loop is formed when two or more blocks
with direct feedthrough form a feedback loop.
The basic problem with algebraic loops is that the output,
y, at time, t, is a function of itself
21/03/2011 20
y(t)Ku(t) - Ky(t) = K 211
Simulink and Algebraic Loops
In general, at each time t, SIMULINK assumes that the initial guess for y at the current integration step, yguess, is equal to the value at the previous integration step, y[t-1]:
The current calculated output of the K1 Gain block is then computed as
If ycalc = yguess: yguess is accepted as the output, y, at time t.
If ycalc != yguess, then an iterative Newton-Raphson technique is used to modify yguess until either the solution converges (within some tolerance) or the maximum number of iterations has been reached.
The maximum number of iterations, 200, is hard-coded into SIMULINK.
21/03/2011 21
1]-y[t = yguess
yguessKK -u K = ycalc 211
An «unsuspecting» algebraic
loop
21/03/2011 22
s+1
s+10
Transfer Fcn
SimTime
To Workspace1
simout
To WorkspaceStep
Clock
Backward Euler method
Another numerical integration method of first order is the
“Backward Euler” (BE) method:
21/03/2011 23
hhthtxftxhtx )),(()()(
The Numerical Stability
Domain
21/03/2011 24
The Numerical Stability Domain II
21/03/2011 25
Simulation With the FE Algorithm
21/03/2011 26
Computation of the Largest Numerically
Stable Integration Step Size for FE
21/03/2011 27
Simulink Example
21/03/2011 28
Improve accuracy
Improve solution accuracy with an integration algorithm
consisting in a prediction step using FE (Forward E.)
correction step using BE (Backward E.):
After some math:
21/03/2011 29
The Heun Integration Algorithm
The method, called Heun integration algorithm,
combines FE e PE algorithms:
To obtain:
Which is a second-order approximation of the ODE
solution.
21/03/2011 30
The Family of Explicit Runge-
Kutta Methods
If we allow more than one prediction stage, it is possible
to obtain higher-order integration algorithms:
21/03/2011 31
4th-order accurate Runge-
Kutta (RK4) algorithm
21/03/2011 32
RK4 algorithm
The next value (xn + 1) is determined by the present value (xn) plus
the product of the size of the interval (h) and an estimated slope
The slope is a weighted average of slopes:
is the slope at the beginning of the interval;
is the slope at the midpoint of the interval, using slope to
determine the value of x at the point tn + h / 2 using Euler's method;
is again the slope at the midpoint, but now using the slope
to determine the x-value;
is the slope at the end of the interval, with its y-value
determined using
21/03/2011 33
kx
1Px
2Px
3Px
kx
1Px
2Px
History of Explicit Runge-
Kutta Methods
The number of non-linear equations grows rapidly with the
order of the methods. Already for RK methods of order 5, there
no longer exists a solution in 5 stages.
More stages must be added in order to increase the number of
parameters.
Because of the many non-linear equations to be solved, it took
a long timebefore higher-order RK methods were found.
21/03/2011 34
Concluding Remarks
Explicit Runge-Kutta algorithms of various orders of
approximation accuracy were developed.
All FRK algorithms except FE are multi-stage algorithms that
require internal function evaluations.
FRK algorithms do not preserve any information across multiple
steps, i.e., these algorithms are self-starting and start afresh
with each new integration step.
The class of explicit RK algorithms are among the most widely
used numerical ODE solvers on the market today.
For most engineering problems, 4th-order FRK algorithms offer
a good compromise between the needed accuracy and the
economy of simulating across a single step.
21/03/2011 35
Stiff Systems
We call a linear system stiff, if it is stable and its eigenvalues vary a lot in terms of their real parts (have parts with very different dynamics).
Non-linear systems are called stiff, if they are stable and exhibit both fast and slow modes in their behavior
– The linearization of such systems leads to stiff linear systems.
These systems cannot be simulated efficiently by means of any explicit RK algorithm, because we would need very small time steps to move those eigenvalues (of either the system itself or of its linearization) that are located most to the left in the complex plane into the numerical stability domain.
This is not only a problem of the explicit RK algorithm, but of all explicit numerical ODE solvers in general.
For the efficient simulation of stiff systems, implicit integration algorithms are required.
21/03/2011 36
An example
A stiff ODE is an ordinary differential equation that has a
transient region whose behavior is on a different scale from
that outside this transient region.
A physical example of a stiff system involves chemical
reaction rates, where typically the convergence to a final
solution can be quite rapid.
Simulate with Matlab/Simulink the system and display the
length of the output array with the command ‘whos’:
21/03/2011 37
1
s +1001s+10002
Transfer Fcn
SimTime
To Workspace1
simout
To WorkspaceStep
Clock
Rules of thumb
A nonstiff solver is primarily concerned with accuracy.
When encountering a stiff problem, a nonstiff solver will
reduce its step size accordingly, making it much more
inefficient.
Identifying a stiff system is one of the more important
steps in the process of numerical integration, as a
nonstiff solver is much less efficient than a stiff solver.
21/03/2011 38
Rules of thumb (cont.)
If the eigenvalues are obtainable, or available, a measure of stiffness can be calculated.
This stiffness ratio is the ratio of the eigenvalue with the largest magnitude to the eigenvalue with the smallest magnitude.
If the region of integration is on a region with no transient, the equation is not stiff. A stiff equation must have a transient.
Understanding what you are modeling is a great advantage when you are choosing a solver, especially in this instance. If you expect behaviors on different scales, you might want to choose a stiff solver.
Finally, and somewhat unfortunately, you might want to choose a stiff solver if you have tried a nonstiff solver and found it to be very inefficient and time-consuming.
21/03/2011 39
System with jumps and
discontinuities.
All of the simulation methods that we encountered until now
operate, in one form or another, on polynomial extrapolations
using Taylor series expansions.
A fundamental property of polynomials is that they don’t exhibit
discontinuities.
Consequently, a model that contains discontinuities cannot be
simulated across these discontinuities using polynomial
extrapolations.
A large majority of engineering systems exhibit many
discontinuities that need to be included in their models.
The numerical integration methods for dynamic systems that
we discussed until now can thus not be used for the simulation
of hybrid systems.
21/03/2011 40
Abusing the Step-size Control
What happens if we simply close our eyes and integrate across a discontinuity of the model using any one of the numerical ODE solvers with step-size control introduced earlier?
The algorithm doesn’t know that there exists a discontinuity. What it does notice is a rapid change in the trajectory.
The algorithm concludes that a new eigenvalue appeared far out to the left in the complex plane.
Consequently, the algorithm reduces the step size in order to capture this eigenvalue in its accuracy domain.
However, the new “eigenvalue” is a joker. It doesn’t allow itself to be captured.
Irrespective of how much the step size is being reduced, the eigenvalue remains outside the accuracy domain.
21/03/2011 41
When does the iteration on the
rejected step end?
The step-size reduction continues until one of two things
happens:
– The step size is reduced to the smallest value specified in the step-size
control algorithm.
– The step-size control algorithm decides that the integration accuracy is
acceptable. This will eventually happen, as by reducing the step size, the
non-linear terms in the Taylor series expansion lose their importance.
With a sufficiently small step size, every algorithm behaves like either
Forward or Backward Euler.
Once the discontinuity lies in the past, the evasive eigenvalue
disappears as miraculously as it had shown up before.
The step-size control algorithm slowly increases the step size
again, until it reaches its optimal value . . . or until it encounters
the next discontinuity, whichever happens first.
21/03/2011 42
Discontinuity
Abuse of the step-size control algorithm for the localization of
discontinuities often works quite well, and it is for this reason
that many of the more primitive environments for the modeling
and simulation of dynamic systems don’t offer any special
provisions for handling discontinuities in the model.
21/03/2011 43
Time Event
Our problems arose from the fact that we didn’t instruct the
ODE solver that there were discontinuities. The ODE solver is
incapable of reading and interpreting models. It can only
execute them.
What we need is a syntactical element in the model description
language that enables us to explicitly inform the integrator of
occurrences of discontinuities.
From now on, we shall call discontinuities discrete events. What
we need are explicit mechanisms for the description and
handling of events.
In some cases, the time of occurrence of an event is known in
advance. In this case, we talk about time events.
21/03/2011 44
Time Event Scheduling
One way to advise the simulation of forthcoming time events is
by offering in the language a schedule statement that can be
used to schedule future time events.
For example, we might include in the initial section of the
program the following statements:
State = Open;
schedule CloseGateEvent at tevent;
and in the code of the CloseGateEvent function:
State = Closed;
schedule CloseGateEvent at time=t + period ;
21/03/2011 45
Integrate between
discontinuities
It should be mentioned that the discontinuity associated with the event is not part of the continuous-time model. All simulation trajectories in between events are perfectly continuous.
For this reason, the ODE solver won’t encounter any problems. It never attempts to simulate across a discontinuity in the model.
The localization of a time event is trivial. All we need in order to localize time events accurately is an ODE solver that provides dense output.
We proceed in exactly the same manner that we use to localize a communication instant, at which we wish to report the values of the output variables.
If the integration algorithm reduces the step in order to hit the communication instants (common for single-step algorithms), we shall do the same in order to arrive at the time of the next time event.
On the other hand, if the integration algorithm uses interpolation for the purpose of calculating the values of the output variables at communication instants (common for multi-step algorithms), we shall do the same for localizing time events.
21/03/2011 46
Time events
Once an event has been localized, the actions associated with the event are being executed.
Afterwards, we are dealing with a new continuous-time simulation with new initial conditions.
The time events are stored in an event calendar, a linear linked list, in such a way that the next time event is always stored at the beginning of the list.
The continuous simulation only needs to know the instant of time when the next time event is scheduled to occur.
The continuous simulation proceeds until the time of occurrence of the next event. At that moment, the continuous simulation terminates, the actions associated with the event (i.e., the discontinuity) are being processed, and a new continuous simulation starts with new initial conditions.
21/03/2011 47
Execute in segments
Consequently, the continuous simulations are now
executed in segments. The individual continuous-time
simulation segments are interrupted for handling discrete
events that occur.
A model that specifies explicitly the discrete events is
called hybrid model.
The simulation of a hybrid model is called hybrid
simulation.
21/03/2011 48
State event
Often the time of occurrence of an event is not known in
advance. What is known is only the condition under which the
event is to take place.
Such events are called state events.
State events cannot be planned.
To this end, we might include in the section where the
simulation equations are being described by Boolean equation
as:
schedule OpenEvent when Condition is True;
and the code of the OpenEvent function consists of:
Gate = open;
21/03/2011 49
Discrete event solver
State events are specified using event conditions, usually
formulated in the form of zero-crossing functions.
Such a function is called event detection function.
During the continuous simulation, all active event detection
functions must be constantly monitored to check whether any of
the zero crossings has taken place.
Once one of the event detection functions has found a zero
crossing, an event localization algorithm is triggered that
iterates on the exact time of the zero crossing.
In the mathematical literature, the event localization algorithm is
often also referred to as root finding algorithm.
21/03/2011 50
How Simulink detects
discontinuities?
The Simulink software uses a technique known as zero-crossing detection to accurately locate a discontinuity without resorting to excessively small time steps. Usually this technique improves simulation run time, but it can cause some simulations to halt before the intended completion time.
A block can register a set of zero-crossing variables, each of which is a function of a state variable that can have a discontinuity. The zero-crossing function passes through zero from a positive or negative value when the corresponding discontinuity occurs. The registered zero-crossing variables are updated at the end of each simulation step, and any variable that has changed sign is identified as having had a zero-crossing event.
If any zero crossings are detected, the Simulink software interpolates between the previous and current values of each variable that changed sign to estimate the times of the zero crossings (that is, the discontinuities)
21/03/2011 51
Real Time Simulation
Conceptually, the implementation of real-time simulation
software is straightforward.
The difficulties of real-time simulation are not of a
conceptual nature. They have to do with keeping track of
real time.
21/03/2011 52
Real Time Clock
The real-time clock is responsible for the synchronization of real time
and simulated time.
The real-time clock is programmed to send a trigger impulse once
every h time units of real time, where h is the current step size of the
integration algorithm, and the simulation program is equipped with a
busy waiting mechanism that is launched as soon as all computations
associated with the current step have been completed, and that
checks for arrival of the next trigger signal. The new step will not
begin until the trigger signal has been received.
A input data channel that feeds input signals at the beginning of each
integration step to update the values of all external driving functions.
The inputs are updated once at the beginning of every integration
step and are then kept constant during the entire step.
21/03/2011 53
A output data channel, that feeds output signals at the end of
each integration step, i.e., the newest output information is put
out through the D/A converters for inspection by the user, or for
driving real hardware (for so-called hardware-in-the-loop
simulations.
External events are time events that are generated outside the
simulation. External events are used for asynchronous
communication with the simulation program, e.g. for the
modification of parameter values, or for handling asynchronous
readout requests, or for communication between several
asynchronously running computer programs either on the same
or different computers. External events are usually postponed
to the end of the current step and replace a portion of the busy
waiting period.
21/03/2011 54
how event handling may
proceed under real-time
constraints. We perform a regular integration step, advancing the
simulation from time tn to time tn+1.
At the end of the step, we discover that a zero crossing has taken place.
We interpolate to the next event time, tnext in [tn, tn+1], using a formula of the same order of accuracy as that of the integration method in use.
We repeat the last integration step to advance the entire state vector from time tn to time tnext.
We then perform the actions associated with the event, and compute a new consistent initial state.
Starting from the new initial state, we perform a partial step advancing the state vector from time tnext to time tn+1.
21/03/2011 55
As no iteration takes place, the amount of work, i.e., the total number of floating-point operations needed, can be estimated accurately.
Assuming that only one state event is allowed to occur within a single integration step, we can thus calculate, how much extra time we need to allot, in order to handle single state events within an integration step adequately.
We perform three integration steps instead of only one, and we have to accommodate the additional computations needed to process the event actions themselves.
Thus, the total effort may grow by about a factor of four.
For this reason, the allowed resource utilization for regular integration steps needs to drop from about 80% to about 20%.
21/03/2011 56
END OF PART 2
21/03/2011
57