2D1263 : Scientific Computing Lecture 8 (2) 2D1263 : Scientific Computing Lecture 8 (1) Difference operators Let xi m i=0 be equidistant points in [a, b], x i = a + (b a)i/m, and define the forward difference operator D+ by, = b a m, 0 i < m. D+ui = u(x i+1) u(xi) By Taylor expansion around xi, ) (u(xi) + u (xi) + 2 2 u (xi) + O( 3 ) u(xi) D+ui = 1 = u (xi) + 2 u (xi) + O( 2 ) D+ui(t) ux(xi, t) as 0 if u(x, t) sufficiently smooth (in x). Difference operators Some other common operators include Backward difference: D u i = u i u i 1 Central difference: D 0 u i = u i+1 u i 1 2 Second difference : = u x (x i ) + O() = u x (x i ) + O( 2 ) D + D u i = u i+1 2u i + u i 1 2 = u xx (x i )+O( 2 ) Systematic way of formulating semidiscrete system. Replace spatial derivatives with difference operators. Example, u t + au x = ɛu xx approximated by du i dt + ad 0u i = ɛd + D u i, 0 i n Boundary conditions are used to determine u 1 and u m+1. 2D1263 : Scientific Computing Lecture 8 (4) 2D1263 : Scientific Computing Lecture 8 (3) Time discretization Discretization in time using similar formulas. Forward Euler for ut = f(u): f(u(t)) = ut(t) + 2 u tt(t) + O( 2 ) f(u(t)) = 2 u tt(t) + O( 2 ) u(t + ) u(t) Backward Euler: Function f evaluated at time t +. Example, ut + aux = ɛuxx with forward Euler u n i = u n 1 i + ( ɛd+d u n 1 i ad0u n 1 ) i, 0 i m, n 1 where the superscript denotes the time level. Consider ODE y (t) = f(y). Linear Multistep Method: c i y n i = d i f(y n i ) i=0 i=0 Explicit scheme if d 0 = 0. Implicit if d 0 0. Explicit: y n = 1 c 0 ( d i f(y n i ) ) c i y n i Solution at new time level given explicitly in terms of previous solutions. Implicit: c 0 y n d 0 f(y n ) = d i f(y n i ) c i y n i Solution given implicitly by (possibly nonlinear) equation.
2D1263 : Scientific Computing Lecture 8 (5) y (t) = y(t) y(0) = 1 Forward Euler (explicit): y n y n 1 = y n 1 y n = (1 ) n Backward Euler (implicit): 10 1 10 0 y n y n 1 = y n y n = (1 + ) n 2D1263 : Scientific Computing Lecture 8 (6) Explicit: + fast: known expression for solution at next time level stability restrictions on. Implicit: + less restrictive conditions on must solve equation to update solution ln( error ) 10 1 10 2 10 3 10 4 Forward Euler Backward Euler 10 5 0 2 4 6 8 10 Time Note: For > 2 forward Euler leads to growth. Unstable! For PDEs: stability restrictions depend on grid spacing (CFL condition) must solve systems of equations for implicit discretization 2D1263 : Scientific Computing Lecture 8 (7) Full discretization A general linear difference scheme in one dimension can be written m different time levels different grid points k l m= q r k= p cm,ku n+m j+k. The cm,k are not necessarily determined by the method of lines. We can insert u(x, t) in the formula, and determine cm,k such that only desired terms disappear in the Taylor expansion. See for example Lax-Wendroff scheme, p. 46 in the lecture notes. 2D1263 : Scientific Computing Lecture 8 (8) Difference schemes Three important characteristics of a scheme: 1. Consistency difference scheme should approach PDE as, 0. 2. Accuracy The scheme is said to be accurate of order α and β in space and time respectively, if l m= q r k= p cm,ku(x + k, t + m) = O ( ( α + β)), when the exact solution u(x, t) is inserted. 3. Stability The solution from the difference scheme grows at most exponentially in time (independent of, ). If a scheme is consistent & stable convergence to u(x, t) is obtained as, 0. Rate determined by order of accuracy.
2D1263 : Scientific Computing Lecture 8 (9) 2D1263 : Scientific Computing Lecture 8 (10) Difference schemes Order of accuracy: Taylor expansion and investigation of local truncation error. Measure of how well scheme approximates PDE. Consistency: Order of accuracy (α, β) > 0. Stability: Exponential growth must be allowed to solve e.g. u t + au x = bu, u(x, t) = exp(bt)u 0 (x at). Techniques for stability analysis: Fourier method Fourier components The CFL condition For time-dependent problems explicit methods are subject to an additional CFL condition, which ensures stability. Generically: Hyperbolic equations u t + au x = 0: a c 0 Parabolic equations u t = bu xx : b c 0 () 2 Energy method norm of discrete solution Normal mode analysis Laplace transform 2D1263 : Scientific Computing Lecture 8 (11) Non uniform grids (outlook) Consider a non uniform grid: a = x 0 < x 1 < < x m 1 < x m = b, Ansatz (cf D 0 ): u (x j ) a u(x j 1 ) + a 0 u(x j ) + a + u(x j+1 ) Taylor expansion gives a = j+1/2 j 1/2 ( j+1/2 + j 1/2 ) a 0 = j+1/2 j 1/2 j+1/2 j 1/2 a + = j 1/2 j+1/2 ( j+1/2 + j 1/2 ) Note: For generalizing to higher dimensions, grid mapping techniques are better suited. j+1/2 = x j+1 x 2D1263 : Scientific Computing Lecture 8 (12) Several space dimensions (outlook) In case of a cartesian grid (grid lines aligned with the coordiante axis in physical space), it is natural to sum up one-dimensional approximations: Example: divu(xi, yj) = ux + vy a x u(xi 1, yj) + a x 0u(xi, yj) + a x +u(xi+1, yj) + a y u(xi, yj 1) + a y 0 u(x i, yj) + a y +u(xi, yj+1)
2D1263 : Scientific Computing Lecture 8 (13) 2D1263 : Scientific Computing Lecture 8 (14) Several space dimensions (cont) The immediate generalization to general structured grids is 2D l,m α l,m u i+l,j+m 3D where α l,m,n u i+l,j+m,k+n l,m,n u i,j u(x i,j, y i,j ), u i,j,k u(x i,j,k, y i,j,k, z i,j,k ). The sets (l, m) α l,m 0 (in 2D) and (l, m, n) α l,m,n 0 are called the stencil of the approximation. Second order approximation of u x + v y Physical domain Grid mapped Array representations Computationally, dd-dimensional grids as well as dd-dimensional grid data are conveniently mapped to dd-dimensional arrays. Note: An array does not necessarily represent a matrix! Which basic operations are really necessary? There are many standard libraries available for this purpose: Blitz++, Lapack++, MTL, MET, and many many others. Aditionally, there are C++-interfaces to standard libraries available. We will have a look at some basics. Note: Compare chapter 5.3 of the Lecture Notes. 2D1263 : Scientific Computing Lecture 8 (15) 2D1263 : Scientific Computing Lecture 8 (16) A basic array class in C++ class arc2 private: double *w; int ni, nj; arc2() ; public: arc2(int nii, int nji) ni = nii; nj = nji; double ind(int i, int j) return w[i+j*ni]; arc2 operator+(const arc2 &b); ~arc2() delete [] w; ; Comments on arc2 The array is stored in column major order just like in Fortran. This is the convention in many standard libraries. We used (C-style) indexing starting with 0. This leads to the index mapping ind = i + j ni. It is easy to generalize to 3D arrays. array elements can be accessed by a.ind(i,j). The standard constructor is private thus preventing anybody from using it! There is no security checking! Do it! Operators can be overloaded.
2D1263 : Scientific Computing Lecture 8 (18) 2D1263 : Scientific Computing Lecture 8 (17) Operator overloading C++ allows operators to be overloaded similarly to functions. arc2 arc2::operator+(const arc2 &b) if ((ni!= b.ni) (nj!= b.nj)) exit(-1); arc2 c(ni, nj); for (int i = 0; i < ni*nj; i++) c.w[i] = w[i] + b.w[i]; return c; Hint: For large objects, use reference arguments in order to avoid excessive copying. An indexing operator double& arc2::operator()(int i, int j) if (i < 0 i >= ni j < 0 j >= nj) exit(-1); return w[i+j*ni]; Remarks: The elements can now be accessed by a.ind(i,j) or a(i,j). The latter can be used on the left-hand side of an assinment a(1,2)=1.0. The previous must be modified. Stroustrup s recommendation: use functions for speed, operators for debugging purposes. Try to overload [], too. 2D1263 : Scientific Computing Lecture 8 (19) 2D1263 : Scientific Computing Lecture 8 (20) Copy assignment Let arc2 a(n,m), b(n,m);. The assignment b = a; copies each member of a to b: b.ni = a.ni; b.nj = a.nj; b.w = a.w; The arrays are not copied such that both a and b use the same data! Therefore, the copy assignment operator = must be overloaded: arc2& arc2::operator=(const arc2 & a) if (ni!= a.ni nj!= a.nj) delete [] w; ni = a.ni; nj = a.nj; for (int i = 0; i < ni*nj; i++) w[i] = a.w[i]; return *this; An (almost) complete header class arc2 private: double *w; int ni, nj; arc2() ; public: arc2(int nii, int nji) ni = nii; nj = nji; arc2(const arc2&); double ind(int i, int j) return w[i+j*ni]; double& operator()(int i, int j); double& operator[](int l) return w[l]; arc2& operator=(const arc2 & a); arc2 operator+(const arc2 &b); ~arc2() delete [] w; ;