3469 - Practical Numerical Simulations Mike Peardon — mjp@maths.tcd.ie http://www.maths.tcd.ie/~mjp/3469 School of Mathematics Trinity College Dublin Michaelmas Term 2015 Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 1 / 14 Numerical Solution of PDEs - overview Partial differential equations occur in many (maybe most) numerical simulations of physical systems. This is a HUGE topic; we’ll only cover the basic ideas. Start by deciding which are dependent and independent variables. . . . then decide which method to use (just like for ODEs) by considering the boundary data. Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 2 / 14 Classification of partial differential equations Some second order PDEs can be classified as hyperbolic, elliptic or parabolic. The classification is made by looking at the discriminant. For the family of 2nd order PDEs in u(x, y ), A ∂ 2u ∂ 2u ∂u ∂ 2u ∂u + B +D + C +E + Fu = g 2 2 ∂x ∂x∂x ∂y ∂x ∂y , ∂u ) etc. , the discriminant is with A = A(x, y , u, ∂u ∂x ∂y d = B2 − 4AC Condition d>0 d=0 d<0 Classification Example 2 2 hyperbolic wave eqn: ∂∂tu2 − v 2 ∂∂xu2 =0 ∂ parabolic diffusion: ∂u − ∂x D(x, t) ∂u =0 ∂t ∂x 2 ∂ u ∂2u elliptic Poisson’s eqn: ∂x 2 + ∂x 2 = ρ(x, y ) Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 3 / 14 Boundary data for PDEs As with numerical solutions to ODEs, the boundary data usually decides the method to use. Hyperbolic and parabolic equations are usually specified with initial value (or Cauchy) data: For example, u(x, t) is required for t > 0 and u(x, 0) = u0 (x) is given (perhaps with other conditions true for all t > 0). Elliptic eqations are usually specified with boundary value data. For example, u(x, y ) is required for (x, y ) ∈ A, and u is ∂u specified on the boundary, ∂A (Dirichlet) or ∂u and ∂y are ∂x specified on ∂A (von Neumann) or a mixture of the two (Robin). Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 4 / 14 Boundary data for PDEs t u(dA) given u(t,x0)=f0(t) A u(t,x1)=f1(t) A dA Start solver here and propagate in t dA u(0,x)=u0(x) Construct solution globally x Hyperbolic or Parabolic Mike Peardon — mjp@maths.tcd.ie (TCD) u(x,y) in A required Elliptic 3469 Michaelmas Term 2015 5 / 14 Overview of methods for solving PDEs As with ODEs, the distict boundary data types lead to two distinct algorithms for solving PDEs. For parabolic and hyperbolic PDEs, a standard numerical solver Begins with the initial data, u(x, t0 ) in memory Applies an update algorithm to compute the solution at a small time-step later, u(x, t0 + h) Repeats this stepping process, to compute u(x, t0 + 2h), u(x, t0 + 3h), . . . . For elliptic PDEs, the numerical solver Begins with an initial guess, u0 (x, y ), ∀(x, y ) ∈ A. Estimates how “wrong” the guess is, and improves it, giving a better guess to the solution, u1 (x, y ). Repeats this refinement process, until the solution is sufficiently accurate. Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 6 / 14 Laplace equation on a grid Find a numerical approximation for φ(x, y ) which solves the Laplace equation ∇2 φ = 0 for φ(x, y ) inside the unit square, x, y ∈ [0, 1], given boundary data on the edges of the square. First step - discretise the unit square with a grid, with spacing a = 1/n for some integer n. Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 7 / 14 Simple iterative schemes Want to solve Ax = b where A is sparse. Introduce any matrix P and re-write expression Ax = b Px = b + (P − A)x Now promote x to be a sequence {x 0 , x 1 , x 2 , . . . } for some initial guess x 0 , defined by the recursive iteration Px (k+1) = b + (P − A)x (k) , k = 0, 1, 2, . . . If the sequence converges, then it converges to the solution to Ax = b. A good choice for P is invertible with P −1 easy to apply Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 8 / 14 Simple iterative schemes (2) Sequence is Px (k+1) = b + (P − A)x (k) Define d k to be the difference between successive entries in the sequence. d (k) = x (k+1) − x (k) k = 0, 1, 2, . . . Then sequence for d becomes Pd (k+1) = (P − A)d (k) k = 0, 1, 2, . . . so if P is invertible can solve for this recursion k = 0, 1, 2, . . . d (k) = (I − P −1 A)k d (0) Sequence converges if d → 0 so if Eigenvalues of this matrix inside unit disk: |λ(I − P −1 A)| < 1 Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 9 / 14 The Jacobi iteration Perhaps simplest choice for P is the diagonal elements of A. P = D. If none of these are zero, this is easy to invert. This simple iteration is called... Jacobi iteration: P = D x (k+1) = D −1 b − (I − D −1 A)x (k) For the Laplace equation, the iteration can be written 1 (k) (k) (k) (k) + φ + φ + φ φ(k+1) = φ x,y x+1,y x,y −1 x,y +1 4 x−1,y The new value of the field at a site is given by the average of the four neighbours in the old iteration. For Dirichlet boundaries, Laplace solver always converges (for simple rectangular geometries, this can be seen using Fourier analysis). Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 10 / 14 The Gauss-Seidel iteration With iterative sequence Px (k+1) = b + (P − A)x (k) and writing A = D + L + U with D the diagonal part of A, and L, U the upper/lower components, choose P =D+L and the iteration is now Gauss-Seidel iteration: x (k+1) = (D + L)−1 b − (D + L)−1 U x (k) Applying the inverse of P = D + L is easily done using forward substitution. Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 11 / 14 Gauss-Seidel iteration and Laplace’s equation What does this look like for Laplace’s equation? (D + L)x (k+1) = b − U x (k) To define which hopping terms appear in L and U means specifying a site ordering. With the “natural” site order, GS becomes 1 (k+1) (k) (k) φ x−1,y + φ(k+1) x,y −1 + φ x+1,y + φ x,y +1 4 This has a simple interpretation as an algorithm; loop over sites in order and over-write φ with the average of the four nearest neighbours. φ(k+1) = x,y Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 12 / 14 Successive over-relaxation Gauss-Seidel has better convergence than Jacobi, but simple trick improves further. Introduce parameter ω and write 1 P = D+L ω this gives the iteration Successive over-relaxation (D + ωL) x (k+1) = ωb − (ωU + (ω − 1)D) x (k) Idea is to move further along current update direction. For Laplace problem, the iteration becomes ω (k+1) (k) (k+1) (k) (k+1) φx,y = φx−1,y + φx+1,y + φx,y −1 + φx,y +1 + (1 − ω)φ(k) x,y 4 ω = 1 is the Gauss-Seidel iteration, 0 < ω < 2 converges for large grid. ω close to 2 best. Mike Peardon — mjp@maths.tcd.ie (TCD) 3469 Michaelmas Term 2015 13 / 14 Von Neumann boundary data Von Neumann boundary condition constrains derivative of φ normal to the boundary. δφ.n How can this be implemented for our mesh? Rectangular domain: with a simple mesh. At one edge, can represent derivative perpendicular using another Taylor approx. n For all points on the boundary, if n is a vector normal to the surface, then ∂φ · n is given along the surface. Mike Peardon — mjp@maths.tcd.ie (TCD) φ0,y 3469 φ1,y φ2,y Michaelmas Term 2015 14 / 14