Intro to Computing with Finite Difference Methods
Total Page:16
File Type:pdf, Size:1020Kb
INF5620 in a nutshell Study guide: Intro to Computing with Finite Dierence Methods Numerical methods for partial dierential equations (PDEs) How do we solve a PDE in practice and produce numbers? How do we trust the answer? Hans Petter Langtangen1,2 Approach: simplify, understand, generalize Center for Biomedical Computing, Simula Research Laboratory1 After the course Department of Informatics, University of Oslo2 You see a PDE and can't wait to program a method and visualize a solution! Somebody asks if the solution is right and you can give a Oct 5, 2014 convincing answer. The new ocial six-point course description More specic contents: nite dierence methods After having completed INF5620 you can derive methods and implement them to solve frequently arising partial dierential equations (PDEs) from physics and mechanics. have a good understanding of nite dierence and nite ODEs element methods and how they are applied in linear and the wave equation utt = uxx in 1D, 2D, 3D nonlinear PDE problems. the diusion equation ut = uxx in 1D, 2D, 3D can identify numerical artifacts and perform mathematical write your own software from scratch analysis to understand and cure non-physical eects. understand how the methods work and why they fail can apply sophisticated programming techniques in Python, combined with Cython, C, C++, and Fortran code, to create modern, exible simulation programs. can construct verication tests and automate them. have experience with project hosting sites (GitHub), version control systems (Git), report writing (LATEX), and Python scripting for performing reproducible computational science. More specic contents: nite element methods More specic contents: nonlinear and advanced problems stationary diusion equations uxx = f in 1D Nonlinear PDEs time-dependent diusion and wave equations in 1D Newton and Picard iteration methods, nite dierences and PDEs in 2D and 3D by use of the FEniCS software elements perform hand-calculations, write your own software (1D) More advanced PDEs for uid ow and elasticity understand how the methods work and why they fail Parallel computing Philosophy: simplify, understand, generalize The exam Oral exam Start with simplied ODE/PDE problems 6 problems (topics) are announced two weeks before the exam Learn to reason about the discretization Work out a 20 min presentations (talks) for each problem Learn to implement, verify, and experiment At the exam: throw a die to pick your problem to be presented Understand the method, program, and results Aids: plots, computer programs Generalize the problem, method, and program Why? Very eective way of learning Sure? Excellent results over 15 years This is the power of applied mathematics! When? Late december Required software Assumed/ideal background INF1100: Python programming, solution of ODEs Our software platform: Python (sometimes combined with Cython, Fortran, C, C++) Some experience with nite dierence methods Important Python packages: numpy, scipy, matplotlib, Some analytical and numerical knowledge of PDEs sympy, fenics, scitools, ... Much experience with calculus and linear algebra Suggested installation: Run Ubuntu in a virtual machine Much experience with programming of mathematical problems Alternative: run a Vagrant machine Experience with mathematical modeling with PDEs (from physics, mechanics, geophysics, or ...) Start-up example for the course Start-up example What if you don't have this ideal background? Students come to this course with very dierent backgrounds ODE problem First task: summarize assumed background knowledge by u0 = au, u(0) = I , t (0, T ], going through a simple example − ∈ Also in this example: where a > 0 is a constant. Some fundamental material on software implementation and software testing Everything we do is motivated by what we need as building blocks Material on analyzing numerical methods to understand why for solving PDEs! they can fail Applications to real-world problems What to learn in the start-up example; standard topics What to learn in the start-up example; programming topics How to verify an implementation and automate verication through nose tests in Python How to think when constructing nite dierence methods, How to structure code in terms of functions, classes, and with special focus on the Forward Euler, Backward Euler, and modules Crank-Nicolson (midpoint) schemes How to work with Python concepts such as arrays, lists, How to formulate a computational algorithm and translate it dictionaries, lambda functions, functions in functions into Python code (closures), doctests, unit tests, command-line interfaces, How to make curve plots of the solutions graphical user interfaces How to compute numerical errors How to perform array computing and understand the How to compute convergence rates dierence from scalar computing How to conduct and automate large-scale numerical experiments How to generate scientic reports What to learn in the start-up example; mathematical What to learn in the start-up example; generalizations analysis How to uncover numerical artifacts in the computed solution Generalize the example to u0(t) = a(t)u(t) + b(t) How to analyze the numerical schemes mathematically to − Present additional methods for the general nonlinear ODE understand why artifacts occur u0 = f (u, t), which is either a scalar ODE or a system of ODEs How to derive mathematical expressions for various measures How to access professional packages for solving ODEs of the error in numerical methods, frequently by using the How our model equations like u au arises in a wide range sympy software for symbolic computation 0 = of phenomena in physics, biology, and− nance Introduce concepts such as nite dierence operators, mesh (grid), mesh functions, stability, truncation error, consistency, and convergence Finite dierence methods Topics in the rst intro to the nite dierence method The nite dierence method is the simplest method for solving dierential equations Contents Fast to learn, derive, and implement How to think about nite dierence discretization A very useful tool to know, even if you aim at using the nite Key concepts: element or the nite volume method mesh mesh function nite dierence approximations The Forward Euler, Backward Euler, and Crank-Nicolson methods Finite dierence operator notation How to derive an algorithm and implement it in Python How to test the implementation A basic model for exponential decay The ODE model has a range of applications in many elds The world's simplest (?) ODE: u0(t) = au(t), u(0) = I , t (0, T ] − ∈ Growth and decay of populations (cells, animals, human) Growth and decay of a fortune Observation Radioactive decay We can learn a lot about numerical methods, computer implementation, program testing, and real applications of these Cooling/heating of an object tools by using this very simple ODE as example. The teaching Pressure variation in the atmosphere principle is to keep the math as simple as possible while learning Vertical motion of a body in water/air computer tools. Time-discretization of diusion PDEs by Fourier techniques See the text for details. The ODE problem has a continuous and discrete version The steps in the nite dierence method Continuous problem. Solving a dierential equation by a nite dierence method consists u0 = au, t (0, T ], u(0) = I (1) − ∈ of four steps: (varies with a continuous t) 1 discretizing the domain, Discrete problem. Numerical methods applied to the continuous 2 fullling the equation at discrete time points, problem turns it into a discrete problem 3 replacing derivatives by nite dierences, 4 formulating a recursive algorithm. n+1 n n u = constu , n = 0, 1,... Nt 1, u = I (2) − (varies with discrete mesh points tn) Step 1: Discretizing the domain Step 1: Discretizing the domain The time domain [0, T ] is represented by a mesh: a nite number of Nt + 1 points n u is a mesh function, dened at the mesh points tn, n = 0,..., Nt only. 0 = t0 < t1 < t2 < < tNt 1 < tNt = T ··· − We seek the solution u at the mesh points: u(tn), n = 1, 2,..., Nt . Note: u0 is known as I . Notational short-form for the numerical approximation to n u(tn): u In the dierential equation: u is the exact solution In the numerical method and implementation: un is the numerical approximation, ue(t) is the exact solution What about a mesh function between the mesh points? Step 2: Fullling the equation at discrete time points Can extend the mesh function to yield values between mesh points by linear interpolation: un+1 un u t un t t (3) ( ) + t − t ( n) ≈ n+1 n − The ODE holds for all t (0, T ] (innite no of points) − ∈ Idea: let the ODE be valid at the mesh points only (nite no of points) u0(tn) = au(tn), n = 1,..., Nt (4) − Step 3: Replacing derivatives by nite dierences Step 3: Replacing derivatives by nite dierences Now it is time for the nite dierence approximations of derivatives: n 1 n u + u Inserting the nite dierence approximation in u0(tn) − (5) ≈ tn 1 tn + − u(t) u0(tn) = au(tn) − gives n+1 n u u n − = au , n = 0, 1,..., Nt 1 (6) tn 1 tn − − + − (Known as discrete equation, or discrete problem, or nite dierence method/scheme) forward tn 1 tn tn +1 − Step 4: Formulating a recursive algorithm Let us apply the scheme by hand How can we actually compute the un values? Assume constant time spacing: ∆t = tn+1 tn = const − given u0 = I compute u1 from u0 u0 = I , 2 1 0 0 compute u from u u1 = u a∆tu = I (1 a∆t), 3 2 − 2 − compute u from u (and so forth) u2 = I (1 a∆t) , 3 − 3 u = I (1 a∆t) , In general: we have un and seek un+1 . − . The Forward Euler scheme Nt Nt n+1 u = I (1 a∆t) Solve wrt u to get the computational formula: − n+1 n n Ooops - we can nd the numerical solution by hand (in this simple u = u a(tn+1 tn)u (7) − − example)! No need for a computer (yet)..