NAP4 NUMERICAL ANALYSIS OF PROCESSES NONLINEAR MODELS of systems described by ordinary differential equations Initial problem (Runge Kutta, Adams) Time delay Strange attractors Rudolf Žitný, Ústav procesní a zpracovatelské techniky ČVUT FS 2010 NAP4 Models ODE initial problem Systems which are described by a system of ordinary differential equations and their solutions are fully described by the initial state, are for example integral models of mass and enthalpy balances elementary units ("lumped parameter" or "compartment" models). The aim is to determine the evolution of state variables (concentration, temperature) at the time, so it is an evolutionary problems. These elementary units may be specific devices (reactors, absorbers, columns, tanks, filters, ...) in "spread sheets" or zones in apparatuses in which the state can be characterized by mean temperature, and the concentration of components (ideal mixers, plug flow zone). An example is the description of a stirred vessel system of ideal mixers below and above a turbine stirrer or a replacement shell and tube heat exchanger by a systém of elementary exchangers between the baffles. Each elementary unit correspond to ordinary differential equations describing the mass balance (one for each component of the differential equation describing the rate of change of concentration) dc n V Qi ci dt i 1 volumetric flowrate from neigbouring compartments and a enthalpy balance (or total energy balance) describing temperature evolution in the department n dT mc p mi hi H ( g ) dt i 1 wnthalpic flows from neighbouring compartments volumetric hear source NAP4 Models ODE initial problem A system need not be described by ordinary differential equations with only first derivatives. Eg. the equations of vibration include also the second time derivative (acceleration) 2 m d y dy d ky f (t ) 2 dt dt But every such equation can be replaced by a system of equations with only first derivatives, e.g. dv dv ky f (t ) dt dy v dt m The initial problem is characterized by the fact that each computed variable corresponds to one differential equation of the first order and to one initial condition (value) corresponding to the initial state. …or how many equations so many initial conditions corresponding to one value of independent variable. NAP4 Models ODE initial problem backmixing model (dispersion, diffusiion) Example of continuous system: (1+r+b)Q Q,x(t) (1+r)Q 2 1 5 4 6 bQ Ci, Ti sQ rQ 3 7 model of stagnant zone recirkulation with transport delay Differential equations for concentrations dc1 Q( x c1 ) sQ (c3 c1 ) dt dc V2 2 Q(c1 c2 ) dt dc V3 3 sQ (c1 c3 ) dt V1 dc4 Q(c2 rc7 bc5 (1 r b)c4 ) dt dc V5 5 Q((1 r b)c4 bc6 (1 r 2b)c5 ) dt dc V6 5 Q(1 r b)(c5 c6 ) dt c7 (t ) c6 (t ) V4 Parameters determining distribution of flowrates (s-stagnant zone, r-recirculation, b-backmixing) must be specified, r-from flowmeter measurement, b-from correlations. Also the volume of individual compartments must be known. These parameters are most frequently identiofied from fit of predicted characteristics (for example RTD - residence time distribution) with experiment. As a result information about the size of dead zones, recirculation zones, shortcuts, intensity of dispersion etc. are obtained. NAP4 Models ODE Fourier transform The models can be solved by the Fourier transformation method (see previous lecture NAZP3). It is not enough to apply only a convolution, because the model includes compartments with back mixing, stagnant zone ... However, you can come out of these differential equations, transformed by FFT. For the Fourier transform of derivation applies (assuming that the concentration at time zero is infinity) dc 2ift 2ift e dt c ( t ) e dt 2if 2ift ~( f ) c ( t ) e dt 2 if c Differential equations are transformed to algebraic equations, where instead of time t is frequency f. 2 iV1 fc1 Q( x c1 ) sQ(c3 c1 ) 2 iV4 fc4 Q(c2 rc7 bc5 (1 r b)c4 ) 2 iV2 fc2 Q(c1 c2 ) 2 iV5 fc5 Q((1 r b)c4 bc6 (1 r 2b)c5 ) 2 iV3 fc3 sQ(c1 c3 ) 2 iV6 fc6 Q(1 r b)(c5 c6 ) c7 exp(2 if )c6 This system of algebraic equations (linear!) is already fairly easy to solve, and the result will be the Fourier transform of concentration. Concentration courses in time are then get back by FFT. The solution is accurate and fast (using FFT), but is limited to linear models. The procedure is preferred when the coefficients of the system (r, b, s) are constant. Otherwise, it was necessary to compute the Fourier transform of the product of two functions (and it is not equal to the product of the transformation). In the general case with nonlinear terms is therefore better to stay in the time domain and seek solutions to the numerical integration of the ODE. NAP4 Models ODE Laplace transform Similar relationships hold also for Laplace transform: the same convolution, correlation and transformation of derivatives dc st st st e dt c ( t ) e s c ( t ) e dt c(0) sc (s) 0 dt 0 0 initial Laplace transform value Inverse Laplace transform is usually searched in dictionaries, for example L t m e t sc~1 ~x c~1 sc~2 c~1 c~2 ... sc~m c~m 1 c~m m! ( s ) m 1 L (t ) 1 NAP4 ODE dc f (t , c) dt initial problem numerical solutions t t k c ck One-step methods approximate solution at time tk+1=tk+ by estimating mean value of derivative f in interval <tk,tk+1> ck 1 ck f c c Euler metods are easily programmable even in Excel ck ck t tk tk+1 ck 1 ck f (tk , ck ) t tk tk+1 ck 1 ck f (tk 1 , ck 1 ) Euler method explicit Euler method implicit order of accuracy (first) order of accuracy (first) stable only for small integration steps stable, but iterations are necessary (because ck+1 is unknown) first order of accuracy means that the error is directly proportional to the step when you write a Taylor expansion for one time step, the error is O(2), a second-order accuracy. Errors, however, accumulate, so 1/ steps will result in an error O(). NAP4 ODE dc f (t , c) dt initial problem numerical solutions t t k c ck One-step methods approximate solution at time tk+1=tk+ by estimating mean value of derivative f in interval <tk,tk+1> ck 1 ck f Runge Kutta methods calculate mean value of derivative as a weighted average of derivatives f(t,c). The most often used version calculates four points (endpoints tk, tk+1 and two midpoints at tk+1/2) c f1 f 2 f 3 f 4 6 3 3 6 f1 f (t k , ck ) f ck t tk tk+1 This variant is implemented in MATLABu as a function ode45 f 2 f (t k / 2, ck f1 / 2) f 3 f (t k / 2, ck f 2 / 2) f 4 f (t k , ck f 3 ) Weight coefficients (1/6,1/3,1/3,1/6) and increments c are designed so that the order of accuracy is the highest (4th order of accuracy in this case). Generally, the order of accuracy of RK methods is the same as the number of evaluated points. ODE NAP4 dc f (t , c ) dt initial problem numerical solutions t t k c ck t t k 1 c ck 1 Multistep methods approximate solution at time tk+1 on the basis of several previous values for example at times tk, tk-1 (two), or tk-2 (three-step method). This is usually a combination of predictor (explicit) and corrector (default formula with higher precision and stable) c ck 1 ck 2 ck ck-1 t tk-2 tk-1 tk tk+1 ck 1 ck 3 ( f (tk , ck ) f (tk 1 , ck 1 )) 2 1 ( f (tk , ck ) f (tk 1 , ck 1 )) 2 it is sufficient to evaluate just one value Both formulas are second order accuracy but corrector (implicit formula) has the absolute value of the error about 9-fold lower (implicit formulas tend to have an error less than explicit, even if the order errors is the same). The disadvantage is that for the first two steps a single-step method, for example RK, must be used. On the other hand, number of computed derivatives is smaller. NAP4 ODE initial problem numerical solutions dc f (t , c) dt Dynamic adjustment of integration step The simplest solution is that each step is performed twice, once with step and then with increments /2 (twice). If the difference is greater than the specified tolerance, the step shrinks. Using the two calculated values (for full and half step) the accuracy of the final value can be improved by so called Aitken extrapolation. MATLAB uses a dynamic choice of the time step automatically. Stiff problem When you solve a system of equations may be that the integration step is dictated by the equation with an extremely short time constant, and the stability of the solution then requires extremely short integration step (the calculation time is prohibitively increased). We say that the system is stiff. Some methods of integrating ODE this unpleasant impact suppresses (an example is Gearova method). Transport delay (time shift) Time delay was included in the previous example. In this case the solver should remember the whole history of calculated results and not only the last step. Even the values for negative times (before the initial time) have to be defined. NAP4 ODE initial problem numerical solutions dc f (t , c) dt Stability analysis Consider a differential equation describing e.g. the rate of a chemical reaction of the first order dc Ac dt stability condition restriction of time step (coef. of gain less than 1) Euler explicit integration formula of the first order ck ck 1 Ack 1 ck c0 (1 A) k | 1 A | 1 2 A Euler implicit integration formula of the first order gain less than 1 for arbitrary ck ck 1 Ack ck c0 1 (1 A) k | 1 | 1 1 A NAP4 ODE examples in MATLAB ode45, ode23 Runge-Kutta (one step method with different order of accuracy) ode113 Adams (multistep method) ode15s Stiff (Gear multistep method with variable accuracy order-suitable for stiff problems) dde23 transport delay NAP4 RTD series of mixers 1/4 An example has been solved in the previous lectures using FFT. Series 4 ideally mixed vessels (container volume, flow rate Q). The aim is to find the time courses of concentrations c1,…,c4 for arbitrary initial conditions and for arbitrary course of concentration at inlet x(t). x(t) y(t) Mass balances (t) dc1 dt dc2 dt dc3 dt dc4 dt Q ( x c1 ) V Q (c1 c2 ) V Q (c2 c3 ) V Q (c3 c4 ) V c1 c2 c3 c4 tm=2 odpovídá poměru V/Q=0.5 (to je střední doba prodlení jedné nádoby) As an initial concentration x(t) is applied impulse response of a set of two mixers (the same volume of tanks) and zero initial concentration in all tanks. The reason for such an assignment is that there is an analytical solution that can be compared with the results of numerical solution of the ODE. Stimulus function x(t) is the impulse response of E (t) for N=2 and tm=1; outlet concentration y(t)=c4(t) shou;ld be impulse response for N=6 and tm=3 N N t N 1 Nt / tm E (t ) e ( N 1)!t mN NAP4 RTD series of mixers 2/4 MATLAB provides for the solution of ODE variety of methods (ode45, ode113, ode15s, ...) which can automatically adjust the integration step so as to achieve the required accuracy. Procedure is always the same, first define a function whose output is the vector of first derivatives for a given value of integration variable (time) and a solution vector. In our case it is necessary to define a function whose result is a column vector (4x1) of right-hand sides of 4 differential equations. It will be assumed that the stimulus function x(t) is the impulse response of nx series mixers, with a mean time tmx. The parameter tm is the mean time of the modeled system of 4 mixers. function dy = serie4(t,y,tm,tmx,nx) dy = zeros(4,1); xx=efun(t,tmx,nx); It would probably be smarter if the stimulus tm1=tm/4; function x(t) was directly the next formal dy(1)=(xx-y(1))/tm1; parameter of the function serie4. I have a dy(2)=(y(1)-y(2))/tm1; little problem with that in Matlab, I need dy(3)=(y(2)-y(3))/tm1; advice. dy(4)=(y(3)-y(4))/tm1; The Efun function can be defined as a sub-function in the m-file serie4.m, but maybe it's better to define it as a separate m-file efun.m function y=efun(t,tm,n) y=n^n*t^(n-1)/(factorial(n-1)*tm^n)*exp(-t*n/tm); N N t N 1 Nt / tm E (t ) e ( N 1)!t mN NAP4 RTD series of mixers 3/4 Integration ODE by Runge Kutta method Sol = ode45(function describing right side, time range, initial conditions) in our case for example sol=ode45(@(t,y)serie4(t,y,2,1,1), [0 10] , [0 0 0 0]) Vector of initial conditions (zero concentrations) solver: 'ode45' extdata: [1x1 struct] x: [1x29 double] y: [4x29 double] stats: [1x1 struct] idata: [1x1 struct]. Result is the structure sol containing vector of time steps sol.x (in this specific case 29 steps was necessary for prescribed accuracy) and a corresponding matrix sol.y of calculated concentrations (four rows for four concentrations) Integration from t=0, to t=10. It is not possible to specify a constant integration step in Matlab Using a function as an actual parameter looks a bit strange in Matlab. It is not possible to write simply the name of this function (serie4) because ode45 expects only two parameters (t, y) and the function serie4 has 5 parameters. Therefore, the so-called anonymous function @(t,y)expression.must be used. NAP4 RTD series of mixers 4/4 Numerical solution by Runge Kutta (ode45) can be compared with the analytical solution E(t) for N=6 and tm=3 N N t N 1 Nt / tm E (t ) e ( N 1)!t mN This is demonstration how to define a one-line function (without using a m-file) e=@(t,tm,n) n^n.*t.^(n-1)./(factorial(n-1)*tm^n).*exp(-n.*t/tm) t=linspace(0,10.23,1024); Standard function linspace y=e(t,3,6); 0.4 generates vector of 1024 0.7 equidistant time steps 0.35 0.6 these points are results of ode45 with automatically adjusted integration time steps 0.3 0.25 Sy=deval(sol,t) 0.5 function deval interpolates the nonequidistant results sol.y according to the prescribed time scale t 0.4 0.2 0.3 0.15 0.2 0.1 0.1 0.05 0 0 2 4 6 8 10 0 0 2 4 6 8 10 NAP4 RTD time delay (transport delay) In the case that there are time delays (long connecting pipelines, recycles) it is not possible to use the standard integration functions (ode23,ode45,…), but a different „family“ of MATLAB functions which is capable to save previous results (with prescribed vector of time delays 1 2 …) NAP4 RTD delay in recycle 1/2 Example: continuous system formed by two mixed tanks with time delay in recycle. It is quite common: connecting pipelines are usually modeled by a time delay. x(t) (1+r)Q Q 1 2 rQ The aim is to find time courses of concentrations c1(t), c2(t), for given flowrate (Q), recirculation ratio (r) and volume of tanks (V1,V2), for prescribed time course of inlet concentration x(t). The system is described by the mass balances: dc1 Q ( x(t ) c1 (t )) dt V1 dc2 Q (c1 (t ) rc2 (t ) (1 r )c2 (t )) dt V2 time delay Vd rQ NAP4 RTD delay in recycle 2/2 Special ODE solvers must be used in MATLAB (dde23 for a constant time delay, and ddesd for variabl;e time delay) sol=dde23(@derivatives, [1, 2…], history for t<tmin, [tmin,tmax]) function dcdt=dclag(t,c,z) q=1; q-flowrate,v1,v2 volumes, r-recycle ratio, impulse v1=2; stimulus function x(t) with duration 0.2 seconds v2=1; r=10; if t<.2 x=1; else z is the matrix of solutions in x=0; times t-k. Column index (1) is the index in vector of end delays dcdt=zeros(2,1); clag=z(:,1); dcdt(1)=q/v1*(x-c(1)); dcdt(2)=q/v2*(c(1)+r*clag(2)-(1+r)*c(2)); zero history c1=c2=0 for t<0 0.1 0.09 0.08 0.07 0.06 0.05 0.04 dc1 Q ( x(t ) c1 (t )) dt V1 0.03 dc2 Q (c1 (t ) rc2 (t ) (1 r )c2 (t )) dt V2 0.02 0.01 0 sol=dde23(@dclag,1,[0;0],[0,20]) plot(sol.x,sol.y) vector of delays 1=1 (there is only one delay) 0 2 4 6 8 10 12 14 16 18 20 NAP4 Trajectory of particles The initial problem is also description of motion of particles under action of forces. Examples are trajectory of droplets in the spray drying chamber, motion of particles in a fluidized bed, burning coal particles in the combustion chamber, particles in the mixer, a settling tank, etc. The equations of motion are ODE of the type: du m F (v u ) g dt acceleratio n drag for in fluid moving with velocity v buoyant, centrifugal, electrical forces NAP4 Trajectories of particles In the last lecture of the course we will pay attention to the type of modern methods of DEM (Discrete Element Method), which simulates the dynamic behavior of particulate systems such as hoppers, silos, mills, mixers, separators, solid and fluid bed. Calculations are based on the solution of ordinary differential equations of motion and the consideration of interactions of individual particles. Verlet integration of dynamic equations d 2x A( x ) 2 dt forces acting upon a particle/ acceleration mass of particle xk 1 2 xk xk 1 t 2 A( xk ) second derivative substituted by second differences xk 1 2 xk xk 1 t 2 A( xk ) O (t 4 ) NAP4 Theory of chaos strange attractor Trajectories of particles calculated also the meteorologist E.Lorenz (1963) who tried to model the natural convection in atmosphere, the layer of air that is heated from below and cooled from above. After drastic simplifications he obtained the three ordinary differential equations for the coordinates x, y, z of a particle in the atmosphere dx Ra ( y x) dt dy xz Pr x y dt dz xy bz dt Rayleigh-Bénárdova instability The model has only 3 parameters, Rayleigh number Ra, Prandtl number Pr and the parameter b is a slenderness ratio of rotating cells. These 3 equations can be solve easily in MATLAB see next slide… NAP4 Theory of chaos strange attractor 50 45 trajektory pf particle function dy = chaos(t,y) fopr Ra=10, Pr=28, b=8/3 dy = zeros(3,1); dy(1) = 10*(y(2)-y(1)); dy(2) = -y(1)*y(3)+28*y(1)-y(2); dy(3) = y(1)*y(2)-8/3*y(3); 40 35 30 25 20 15 10 sol=ode45(@chaos,[0 30],[.10001 .1 .1]) 5 0 -30 integration up to 30 s initial coordinates x,y,z plot(sol.y(2,:),sol.y(3,:)) -10 0 10 -20 -10 0 10 20 30 50 45 40 plot graph y-z fro automatically calculated integration steps tt=linspace(0,30,3000); sy=deval(sol,tt); plot(sy(2,:),sy(3,:)) -20 35 30 25 20 15 10 5 0 -30 20 30 NAP4 Theory of chaos strange attractor Numerically calculated trajectories behave erratically for Pr> 1 (they do not converge to a trivial steady steate solution x = y = z = 0), but they create something that looks like a counter-rotating swirls of final dimensions, and these seemingly random trajectories "jump" from the left to the right wing, and never intersect. The trajectories are extremely sensitive to initial conditions and to any disturbances (including approximation errors of numerical integration). This limiting state is called „strange attractor“ parametric trajectory of particle z Such behavior (deterministic chaos) appears at nonlinear systems when a stability limit is exceeded (eg Prantl, Reynolds or Rayleigh numbers). Deterministic chaos is typical for turbulent flow. 50 45 40 35 30 25 20 15 10 5 0 -30 y -20 -10 0 10 20 30 NAP 4 EXAM ODE Initial problem numerical solutions, stability NAP4 Exam - remember Fourier transform of derivatives dc 2 ift dt e dt 2 ifc( f ) What is it initial problem What is it order of accuracy O(n) Explicit and implicit Euler method ck 1 ck f (tk , ck ) ck 1 ck f (tk 1 , ck 1 ) Principles of RK and multistep methods (predictor, corrector) Principle of stability analysis Principle of integration step optimisation