INF5620: Numerical Methods for Partial Differential
Equations
Hans Petter Langtangen
Simula Research Laboratory, and Dept. of Informatics, Univ. of Oslo
January 2006
INF5620: Numerical Methods for Partial Differential Equations – p. 1
About the course
About the course – p. 2
Course data
10 p
Lectures: Wednesdays 10-14 in B70, math building
Sometimes 4 h lectures, sometimes less, sometimes 4 h exercises Course web page:http://folk.uio.no/hpl/INF5620, reachable from the official central UiO web page of the course Look for messages at the web page!
Teachers:
Xing Cai:[email protected]
Hans Petter Langtangen:[email protected], 99 53 20 21
About the course – p. 3
Basic features of the course
Goal: produce solutions of PDEs Integrated approach:
mechanics, numerics, algorithms, software Generic approach –
methods applicable to a wide range of PDE problems Modern numerical methods
Modern implementation techniques
Non-trivial applications with nonlinear systems of PDEs Analysis of simplified problems
Discovery of numerical properties by computer experiments Carry out your own 2-week PDE project
About the course – p. 4
Contents
Numerical methods: Finite difference methods
Finite element methods (main emphasis) Application areas:
Heat transfer Diffusion Wave phenomena Thermo-elasticity Viscous fluid flow
About the course – p. 5
How to learn it
Overview from lectures
Exercises with hand calculations (get the details!) Compulsory exercises:
2 computer implementations of 1D finite difference methods 2 1D finite element hand calculations
2-week PDE project (comprehensive implementation)
About the course – p. 6
The exam
20 min talk Additional questions
6 topics given two weeks on beforehand Focus on overview and understanding
Some focus on mathematical details, derivations, intricate steps in algorithms etc.
No focus on details regarding software tools (but some topics will involve overview and principle workings of software tools)
About the course – p. 7
Acronyms
PDE = partial differential equation
(plural: PDEs)
ODE = ordinary differential equation
(plural: ODEs)
OOP = object-oriented programming
Scientific software trends
Dramatic increase in the interest of problem solving environments: Maple, Matlab, Mathematica, S-Plus, ...
PDE solvers are often huge & expensive It’s difficult to build a flexible “Matlab” for PDEs, but
modern programming techniques and languages (e.g. C++) simplify the task
Diffpack is one attempt (used in this course)
Practical problem solving in industry makes use of large program packages – that is one reason why we use a package in this course New numerical projects in industry make increasing use of C++ instead of Fortran – therefore we expose students to C++ and more modern implementation techniques
We also see the potential of high-level languages like Python, in combination with C++ or Fortran, for solving PDEs – INF5660 may be a companion course
About the course – p. 9
Diffpack
Numerical library for PDE solution
(Almost) a full problem-solving environment for PDEs A tool for programmers
Implementated in C++ and requires you to program in C++ Relies on object-oriented programming
Reduced implementation efforts for finite elements and PDEs Enables real-world problem solving in a course
About the course – p. 10
Some features of Diffpack
Free version (though with array-size limition) Free version at UiO and for students
Over 200 commercial installations: www.diffpack.com
(Siemens, Xerox, DaimlerCrysler, Mitsubishi, NASA, Intel, Stanford, Cornell, Cambridge, Harvard, ...)
Some application areas:
basic model equations in applied math. (Laplace, heat and wave equations) viscous fluid flow (Navier-Stokes equations) many types of water wave equations heat transfer, incl. phase changes thermo-elasticity
stochastic PDEs and ODEs
computational engineering, medicine, geology, finance 1D, 2D, 3D within the same code lines
About the course – p. 11
The Diffpack philosophy
Diffpack relies on programming and scripting
Diffpack is a set of libraries, consisting of C++ classes in hierarchies (OO design), applications (examples), and (Perl/Python) scripts A simulator mainly contains problem-dependent code; generic methods and data structures are already programmed in the libraries Diffpack acts as a computational engine with a layered design:
primitive layers: arrays, input/output, ...
intermediate layers: linear systems/solvers, grids, fields, ... higher-level layers: simulators, parallel toolbox, ...
About the course – p. 12
How to learn Diffpack
Required:
good general programming skills some familiarity with the class concept thorough knowledge of the numerics the right attitude:
don’t reinvent the wheel – learn to use others’ code don’t try to understand all details – utilize black boxes Principles:
learn on demand rely on program examples stay cool!
Have access to a C++ textbook, e.g., Barton and Nackman’s Scientific
and Engineering C++
About the course – p. 13
Literature
H. P. Langtangen: Computational Partial Differential Equations, Springer, 2nd ed., 2003
About the course – p. 14
Warnings
Numerical solution of PDEs is a huge field in rapid growth; it takes years to master the field
Many other fields (computer science, physics, mathematics) are wired into PDE numerics
C++ takes time to master OOP takes time to understand
Diffpack requires you to have a thorough and generic understanding of the numerics
Difficulties with this course are usually not due to C++/OOP/Diffpack details – but lack of the proper overview of mathematics and numerics
About the course – p. 15
1D heat conduction
Heat conduction in the continental crust
earth surface x s T x=b x=0 -Q mantleKnowing the temperature at the earth’s surface and the heat flow from the mantle, what is the temperature distribution through the continental crust?
Interesting question in geology and geophysics – and for those nations exploring oil resources...
1D heat conduction – p. 17
Basic assumptions
x s T x=b x=0 -Q Physical assumptions:Crust of infinite area Steady state heat flow
Heat generated by radioactive elements Physical quantities:
u(x): temperature
q(x): heat flux (velocity of heat)
s(x): heat release per unit time and mass
1D heat conduction – p. 18
Summary of the model
Differential equations and boundary conditions:
−u00
(x) =f(x), x∈(0,1), u(0) = 0, u0 (1) = 1 (f(x)is a scaled version ofs(x))
Finite difference method (h= cell size):
u1 = 0
ui+1−2ui+ui−1 = −h2fi 2un−1−2un = −2h−h2fn which can be written as a linear system
Au=b whereu= (u1, . . . , un)andAis tridiagonal
What to do: FillAandb, solve foruby Gaussian elimination
1D heat conduction – p. 19
Derivation of the model (1)
x x=0 x=b outflow inflow s(x)=R exp(-x/L) Physical principles:
First law of thermodynamics:
net outflow of heat=total generated heat
Fourier’s law: heat flows from hot to cold regions (i.e. heat velocity is poportional with changes in temperature)
q(x) =−λu0 (x)
λreflects the material’s ability to conduct heat
1D heat conduction – p. 20
Derivation of the model (2)
x s(x) h q(x-h/2) q(x+h/2) x=0 x=b
The first law of thermodynamics: outflow = heat generation
q(x+h/2)−q(x−h/2) =s(x)h
Here: heat generations(x)due to radioactive decay,
s(x) =Rexp(−x/L) Divide left-hand side byhand makehsmall,
q(x+h/2)−q(x−h/2)
h =s(x) →q
0 (x) =s(x)
1D heat conduction – p. 21
Derivation of the model (3)
We have more information (boundary conditions):
u(0) =Ts(at the surface of the earth)
q(b) =−Q(at the bottom of the crust)
We need to getuinto the model; combining the 1st law of thermodynamics
q0 (x) =s(x) with Fourier’s law
q(x) =−λu0 (x)
we can eliminateqand get a differential equation foru:
−d dx λdu dx =s(x) 1D heat conduction – p. 22
Mathematical model
−dxdλdu dx =Re−x/L, u(0) =T s, λ(b)u0(b) =−Q or ifλis constant: −u00 (x) =λ−1Re−x/L, u(0) =T s, λ(b)u0(b) =−Q Observe:u=u(x;λ, R, L, b, Ts, Q)u varies with 7 parameters!
Suppose that we want to investigate the influence of the different parameters. Assume (modestly) three values of each parameter: Number of possible combinations:36= 729.
Using scaling we can reduce the six physical parameters
λ, R, L, b, Ts, Qto only two!
1D heat conduction – p. 23
Scaling
We introduce dimensionless quantities (see HPL A.1 and assume thatλis constant):
x= ¯xb, u=Ts+Qb¯u/λ, s(b¯x) =R¯s(¯x) −d 2u¯ d¯x2=γe −x/β¯ , u¯= 0, du¯ dx¯(1) = 1 where we have two dimensionless quantities
β=b/L, γ=bR/Q
Dropping the bars, we get a problem on the form
−u00 (x) = f(x), x∈(0,1) u(0) = 0 u0 (1) = 1 1D heat conduction – p. 24
Discretization of our equation
1. Divide the domain[0,1]inton−1cells, the cell edgesxiare called nodes (i= 1, . . . , n) 2. Letui=u(xi),
our goal is to let the computer calculateu1,u2,u3, . . .
u u u u u 3 4 5 1 2 x=1 x=0 x
3. The differential equation is to be fulfilled at the nodes only:
−u00
(xi) =f(xi), i= 1, . . . , n 4. Derivatives are approximated by finite differences
1D heat conduction – p. 25
Finite difference approximations (1)
Recall the definition of the derivative from introductory calculus: lim h→0 u(x+h)−u(x) h =u 0 (x) Idea: use this formula with a finiteh
– this is a finite difference approximation to the derivative What is the error in this approximation? Expandu(x+h)in a Taylor series and compute
1 h(u(x+h)−u(x)) = 1 h u(x) +u0 (x)h+1 2u 00 (x)h2+· · · −u(x) = u0 (x) +1 2u 00 (x)h+· · ·
The largest error term isu00
h/2, proportional toh
1D heat conduction – p. 26
Finite difference approximations (2)
An alternative finite difference approximation:
u0
(x)≈u(x+h)−u(x−h)
2h
Compute the error by Taylor series expansion ofu(x+h)and
u(x−h)aroundx: u(x+h)−u(x−h) 2h =u 0 (x) +1 6u 000 (x)h2+. . .
Leading error term proportional toh2
1D heat conduction – p. 27
Finite difference approximations (3)
Approximation tou00 (x): u00 (x)≈u(x+h)−2u(x) +u(x−h) h2 or u00 (xi)≈ u(xi+h)−2u(x) +u(xi−h) h2
Alternative notation, noting thatui≡u(xi),ui+1=u(xi+h), and
ui−1=u(xi−h): [u00
]i≈ui+1−2ui+ui −1
h2
Show that the error isO(h2)
(Hint: expandui+1,ui, andui−1in Taylor series aroundxiand insert the series in the finite difference formula)
1D heat conduction – p. 28
The discrete differential equation
The equation at the nodes:
−u00
(xi) =f(xi), i= 1, . . . , n Replaceu00
by a centered finite difference:
u00
(xi)≈ui+1−2ui+ui −1
h2
⇒The differential equation is transformed to a system of algebraic equations:
−ui+1−2ui+ui−1
h2 =fi, i= 1, . . . , n
1D heat conduction – p. 29
Discretizing boundary conditions
u(0) = 0simply becomesu1= 0
u0
(1) = 1can be approximated as
un+1−un−1
2h = 1
Problem:un+1is not in the mesh!
Solution: Use the discrete differential equation fori=n:
−un−1−2un+un+1
h2 =fn
and the discrete boundary condition to eliminateun+1
The result is
2un−1−2un=−2h−h2fn
1D heat conduction – p. 30
System of equations
The complete set of finite difference equations,
u1 = 0
ui+1−2ui+ui−1 = −h2fi 2un−1−2un = −2h−h2fn can be written as a linear system on matrix form
Au=b whereu= (u1, . . . , un)andAis a tridiagonal matrix
1D heat conduction – p. 31
Tridiagonal coefficient matrix
A= A1,1 A1,2 0 · · · 0 A2,1 A2,2 A2,3 . .. . .. . .. . .. ... .. . . .. . .. . .. . .. . .. ... .. . . .. . .. . .. . .. . .. ... ..
. . .. 0 Ai,i−1 Ai,i Ai,i+1 0 ...
.. . . .. . .. . .. . .. . .. . .. 0 .. . . .. . .. . .. . .. . .. . .. An−1,n 0 · · · 0 An,n−1 An,n A1,1 = 1, A1,2= 0, An,n−1= 2 Ai,i−1 = 1, Ai,i+1= 1, i= 2, . . . , n−1 Ai,i = −2, i= 2, . . . , n 1D heat conduction – p. 32
Solution of linear systems
The system is solved by Gaussian elimination: Compute the LU factorization:A=LU L: lower triangular matrix
U: upper triangular matrix SolveLy=b(easy) SolveU x=y(easy) Computational work: Ais dense:O(n3) Ais tridiagonal:O(n) 1D heat conduction – p. 33
Solution of linear systems; general case
LU factorization (Gaussian elimination) is the optimal solution method whenAis tridiagonal
However, in 2D and 3D problems,
LU factorization is a very slow process (Ais no longer tridiagonal) the structure ofAfavors iterative methods, which are very much faster than LU factorization
Iterative methods are discussed at the end of the course
1D heat conduction – p. 34
Implementation
We want the computer to solve our linear system (for arbitraryn) This task can easily be accomplished using any computer language and any computer
The program fillsAandbwith numbers according to the derived formulas and then calls a Gaussian elimination procedure to findu In numerical simulation in general, computer codes are large and complicated and using effective tools is fundamental
We shall use a comprehensive tool, Diffpack, even for this very simple problem
The Diffpack code will be close similar codes in Python, Fortran 77, Matlab, C, C++, Java, ...
There is no particular advantage of using Diffpack (except that Diffpack has a solver for tridiagonal linear system), but it is a simple problem for the first Diffpack encounter
1D heat conduction – p. 35
Diffpack intro
Diffpack intro – p. 36
The standard intro to a new language
A “scientific” Hello World code:
#include <iostream> // make input/output functionality available #include <cmath> // make math functions available: e.g. sin(x) int main () // function "main" is always the main program {
std::cout << "Hello, World! Give a number: ";
double r; std::cin >> r; // read number into double precision r double s = sin(r); // declare s and initialize with sin(r) std::cout << "\nThe value of sin(" << r << ") is " << s << "\n"; }
This is pure C++ - no Diffpack!
Diffpack intro – p. 37
Compiling and linking
Compile: g++ -c hw.cpp
Link
hw.o
to the C/C++ standard and math library g++ -o app hw.o -lm# -lm (link to math lib.) can often be left out: g++ -o app hw.o
Run the program: ./app
Compiling and linking in one step: g++ -o app hw.cpp -lm
C++ compilers can have other names: CC, xlC
Diffpack intro – p. 38
The corresponding Diffpack program
Make special directory:Mkdir myfirstdp cd myfirstdp
Make a filehw.cppwith the following contents: #include <IsOs.h> // Diffpack tools for input/output #include <cmath> // make math functions available: sin(x) int main (int argc, const char* argv[])
{
initDiffpack (argc, argv); // should always be performed s_o << "Hello world! Give a number: ";
real r; s_i >> r; // read real number into r real s = sin(r);
s_o << "\nThe value of sin(" << r << ") is " << s << "\n"; }
/* Explanation:
IsOs.h : input/output in Diffpack, much like iostream real : real variables in Diffpack, equals double by default s_i : standard input in Diffpack, corresponds to std::cin s_o : standard input in Diffpack, corresponds to std::cout */
Diffpack intro – p. 39
Compiling and linking
Diffpack is compiled using makefiles (which are automatically generated by the
Mkdir
command)Compilation and linking is just a matter of Make (safe, but results in slow code) Make MODE=opt (fast code, but less safety checks)
Always start with
Make
; use only optimized mode (MODE=opt
) when the program is thoroughly tested!Arrays in Diffpack
Conventions as in Fortran: first index is 1 subscript syntax: a(i)
Different from C, where arrays start at 0 and brackets are used: a[0], a[1], ...
Diffpack arrays are not a built-in feature of C++, but they are defined by a programmer (and can in principle be extended by anybody to meet the demands in a particular application)
Diffpack intro – p. 41
Code example with arrays
#include <Arrays_real.h>
int main (int argc, const char* argv[]) {
initDiffpack (argc, argv); int i,j,k,n,m,p; real r; n = m = 4; p = 3; Vec(real) w(n); w.redim (m); i = w.size(); w = -3.14; Vec(real) z; z = w; z(n-1) = w(1) - 4.3; z.print (s_o, "z"); z.printAscii(s_o,"z"); z.print (s_o); } Diffpack intro – p. 42
Heat conduction problem in Diffpack
Find a suitable test problem with known analytical solution
−u00 (x) =γexp (−βx), u(0) = 0, u0 (1) = 1 u(x) = γ β2 1−e −βx+ 1−βγe−β x, β6= 0 u(x) =x 1 +γ 1−1 2x , β= 0 Readβ,γandn InitializeAandb
Call a Gaussian elimination procedure in Diffpack to solve foru
Diffpack intro – p. 43
Test problem for debugging
Choose e.g.n= 2and solve the discrete equations by hand,
u1= 0, 2u1−2u2=−2h−h2γe−β Solutionu2: u2= 1 + γ 2e −β
Whenβ= 0, the numerical solution is exact for alln(!), i.e., the analytical solution u(xi) =ui= (i−1)h 1 +γ 1−1 2(i−1)h
fulfills the discrete equations In general,u00
=const is solved exactly by finite difference methods on uniform grids
Diffpack intro – p. 44
Diffpack/C++ program in F77/C style
Declaration and initialization of variables:
#include <Arrays_real.h> // for array functionality (and I/O) #include <cmath> // for the exponential function int main(int argc, const char* argv[])
{
initDiffpack(argc, argv);
s_o << "Give number of solution points: "; // write to the screen
int n; // declare an integer n (no of grid points)
s_i >> n; // read n from s_i, i.e. the keyboard real h=1.0/(n-1); // note: 1/(n-1) gives integer division (=0) Mat(real) A(n,n); // create an nxn matrix
ArrayGen(real) b(n); // create a vector of length n. ArrayGen(real) u(n); // the grid point values s_o << "Give beta: "; real beta; s_i >> beta; s_o << "Give gamma: "; real gamma; s_i >> gamma;
Diffpack intro – p. 45
Fill matrix and right-hand side
A.fill(0.0); // set all entries in A equal to 0.0 b.fill(0.0); // set all entries in b equal to 0.0 real x; int i;
i = 1; A(i,i) = 1; b(i) = 0; // inner grid points:
for (i = 2; i <= n-1; i++) // i++ means i=i+1
{
x = (i-1)*h;
A(i,i-1) = 1; A(i,i) = -2; A(i,i+1) = 1; b(i) = - h*h*gamma*exp(-beta*x); } // i = n: i = n; x = (i-1)*h; A(i,i-1) = 2; A(i,i) = -2; b(i) = - 2*h - h*h*gamma*exp(-beta*x); if (n <= 10) {
A.print (s_o,"A matrix"); // print matrix to the screen b.print (s_o,"right-hand side"); // print vector to the screen }
Diffpack intro – p. 46
Solve for u and write out solution
A.factLU(); A.forwBack(b,u); // Gaussian elimination s_o << "\n\n x numerical error:\n"; real u_exact;
for (i = 1; i <= n; i++) { // \n is newline
x = (i-1)*h;
if (beta < 1.0E-09) { // is beta zero? u_exact = x*(1 + gamma*(1 - 0.5*x)); } else {
u_exact = gamma/(beta*beta)*(1 - exp(-beta*x)) + (1 - gamma/beta*exp(-beta))*x; }
s_o << oform("%4.3f %8.5f %12.5e\n", x,u(i),u_exact-u(i));
}
// test for the case of only one cell:
if (n == 2) { s_o << "u(2)=" << 1+0.5*gamma*exp(-beta) << "\n"; } // write results to the file "SIMULATION.res"
Os file ("SIMULATION.res", NEWFILE); // open file for (i = 1; i <= n; i++)
file << (i-1)*h << " " << u(i) << "\n"; file->close();
}
Diffpack intro – p. 47
Tridiagonal matrices
Ais tridiagonal
Mat(real) A(n,n)
is a dense matrixSave memory and CPU-time: use tridiagonal matrix (this can be quite dramatic savings!)
MatTri(real) A(n)
A(i,-1)
,A(i,0)
,A(i,1)
forAi,i−1,Ai,i,Ai,i+1Otherwise the program remains the same
Exercises
1. Perform the steps to be a Diffpack user
2. Type in the Diffpack version of our numerical “Hello World!” program, compile and run the program
3. Introduce MatTri instead of Mat in the 1D heat conduction program (Exercise 1.4 in HPL)
Diffpack intro – p. 49
The heat conduction coefficient
The derivation of the 1D model ends in
−dxd λdu dx =s(x) and allows a variableλ
λ: heat conduction coefficient
The continental crust is typically not homogeneous!
⇒ λvaries in space!
Model simplification:λ=λ(x) (λ=λ(x, y, z)would require a 3D model) Need to discretize the operator
d dx λdu dx Diffpack intro – p. 50
Discretization of variable coefficients
Mathematical problem −d dx λ(x)du dx =f(x), 0< x <1 u(0) = 0, u0 (1) = 1.
NEVER expand(λu0
)0(by the rule of product differentiation) Two-step discretization, first outer operator:
d dx λ(x)du dx x=x i ≈1 h λdu dx x=x i+ 12 −λdu dx x=x i−12
Then inner operator:
λdu dx x=x i+ 12 ≈λi+1 2 ui+1−ui h Diffpack intro – p. 51
Finite difference equations
Left point, inner points, right point:
u1 = 0 λi+1 2(ui+1−ui)−λi−12(ui−ui−1) = −h 2f i, i= 2, . . . , n−1 2λn(un−1−un) = −2hλn+1 2−h 2f n Arithmetic mean: λi+1 2= 1 2(λi+λi+1) Harmonic mean: 1 λi+1 2 =1 2 1 λi + 1 λi+1 Geometric mean: λi+1 2= (λiλi+1) 1/2 Diffpack intro – p. 52
A nonlinear problem
A nonlinear problem – p. 53Nonlinear heat conduction
Heat conduction typically depends upon the temperature
−dxd λ(u)du dx =f(x), 0< x <1, u(0) = 0, u0 (1) = 1 This is a nonlinear differential equation
Using the same discretization reasoning as whenλ=λ(x),
u1= 0 λi+1 2(ui+1−ui)−λi−12(ui−ui−1) =−h 2f i 2λn(un−1−un) =−2hλn+1 2−h 2f n where λi+1 2≡λ(ui+12) A nonlinear problem – p. 54
The new problem
Our discrete equations containsλ(ui+1
2), i.e., the coefficients that we
previously put in the matrixA, now depend on the solutionuiand
ui+1
The linear system can be written as A(u)u=b This is a set of nonlinear algebraic equations The nonlinearity arises from theλ(u)u0
product in the underlying differential equation
We cannot use LU decomposition becauseAdepends onu What can we do?
A nonlinear problem – p. 55
Solution method
If we only had a linear equation, we would get a linear system Au=b, which know how to solve...
Idea: Guess a solutionu0and use this inλ:
−d dx λ(u0)du1 dx =f(x)
u1is – hopefully – a better approximation thanu0
This approach suggest an iteration procedure: use solution from last iteration inλ
the equation is now linear
use the solution technology for−(λ(x)u0 (x))0
=f(x)
Algorithm
Guess a solutionu0(need not be correct)
Solve the recursive equations
−d dx λ(u k−1)du dx k! =f(x), uk(0) = 0,du dx k (1) = 1
until difference betweenukanduk−1is small
“Small” can mean v u u t n X j=1 |uk j−uk −1 j |2≤
Pros:may reuse previous code by inserting an evaluation ofλ(ui+1 2)
Cons:slow convergence (faster methods exist)
A nonlinear problem – p. 57
The complete scheme (1)
Fori= 1,2, . . . , n−1: 1 2 λ(uk−1 i ) +λ(uk −1 i+1) uk i+1−uki − 1 2 λ(uk−1 i−1) +λ(uk −1 i ) uk i−uki−1 =−h2f(x i) A nonlinear problem – p. 58
The complete scheme (2)
Fori=n 2λ(uk−1 n )(ukn−1−ukn) =−2hλn+1 2−h 2f(x n) Now, λn+1 2= 1 2 λ(u k−1 n ) +λ(uk −1 n+1) Using the boundary condition
uk−1 n+1−uk −1 n−1 2h = 1, k >1 gives λn+1 2= 1 2 λ(uk−1 n ) +λ(uk −1 n−1+ 2h) A nonlinear problem – p. 59
Implementation
Reuse old program (Heat1D) with:
Loop around system generation and solution Two arrays
uk
andukm
Initial guess in
ukm
New auxiliary variables (for iteration etc.) Function
lambda
to evaluateλ(u) UpdateA
andb
for each stepCall
A.resetFact()
to enable new LU decomposition prior tocall to
A.factLU()
Check for termination upon convergence Set
ukm
equaluk
before new iterationA nonlinear problem – p. 60
The central code segment (1)
int k = 0; // iteration counter
const int k_max = 200; // max no of iterations real udiff = INFINITY; // udiff = ||uk - ukm|| const real epsilon = 0.0000001; // tolerance in termination crit. while (udiff > epsilon && k <= k_max)
{
k++; // increase k by 1
A.fill(0.0); b.fill(0.0); // initialize A and b for (i = 1; i <= n; i++) { if (i == 1) { A(1,0) = 1; } else if (i > 1 && i < n) { lambda1 = lambda(ukm(i-1), m); lambda2 = lambda(ukm(i), m); lambda3 = lambda(ukm(i+1), m); A(i,-1) = 0.5*(lambda1 + lambda2); A(i, 0) = -0.5*(lambda1 + 2*lambda2 + lambda3); A(i, 1) = 0.5*(lambda2 + lambda3); } else if (i == n) { A(i,-1) = 2*lambda(ukm(i), m); A(i, 0) = - A(i,-1); b(i) = -(h*lambda(ukm(i-1)+2*h,m)+lambda(ukm(i),m)); } } A nonlinear problem – p. 61
The central code segment (1)
A.resetFact(); // ready for new factLU
A.factLU(); A.forwBack(b,uk); // Gaussian elimination // check termination criterion:
udiff = 0;
for (i = 1; i <= n; i++) udiff += sqr(uk(i) - ukm(i)); udiff = sqrt(udiff);
s_o << "iteration " << k << ": udiff = " << udiff << "\n"; ukm = uk; // ready for next iteration
} A nonlinear problem – p. 62
No of iterations
λ(u) =um 0 10 20 30 40 50 60 0 0.5 1 1.5 2 2.5 3 3.5 4 no of iterations m number of iterations n=100 A nonlinear problem – p. 63Numerical error
λ(u) =um -35 -30 -25 -20 -15 -10 -5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 log(error) m numerical error n=100 A nonlinear problem – p. 64Convergence
Define the error from iterations:
EI=||uk−uk−1|| where|| · ||is some norm, e.g.,
||u||= v u u t1 n n X i=1 u2 i
Define the discretization error:
E∆=||u−uk||
Basic issue in discretization: how doesE∆vary with the cell sizeh?
Investigation: makeEInegligible (EIE∆), computeE∆for
different choices ofh
Common model for relating discretization error to grid size:
E∆=Chr
fitCandrto data (linear least squares): logE∆= logC+rlogh
y=aξ+b, y= logE∆, b= logC, ξ= logh, r=a
DoesE∆→0ash→0? And how fast?
Second-order finite difference approximations suggestr= 2
A nonlinear problem – p. 65
Convergence plot (1)
λ(u) =um -10 -9 -8 -7 -6 -5 -4 -3 -2 -8 -7 -6 -5 -4 -3 -2 -1 log(error) log(h) numerical error m=0.2 m=1.2 m=3.2 A nonlinear problem – p. 66Convergence plot (2)
λ(u) = (1 +u)m -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -8 -7 -6 -5 -4 -3 -2 -1 log(error) log(h) numerical error m=0.2 m=1.2 m=3.2 A nonlinear problem – p. 67Summary of results
Number of iterations increase withm m= 1: numerical solution is exact (!)
λ(u) =um:O(hr)forr≤1
(despite our use ofO(h2)accurate finite differences!)
λ(u) = (1 +u)m:O(h2)– as expected
Explanation:umgivesu0 (0)→ ∞,
need a very fine grid aroundx= 0to get accurate results Note: theory does not extend well to nonlinear problems; systematic experiments may be an important additional tool
A nonlinear problem – p. 68
Simulation of waves
Simulation of waves – p. 69
Vibration of a string
Mathematical model: the wave equation
∂2u
∂t2 =γ 2∂2u
∂x2, x∈(a, b)
- Time- and space-dependent problem - This is a partial differential equation (PDE) - Boundary conditions atx=a, b(uor∂u/∂x) - Initial conditions: knownu(x,0)andut(x,0) Explicit finite difference method:
u`+1
i =f(u`i, ui`−1, u`i+1, u`
−1
i )
Implementation: run through a space-time grid and computeu`+1
i for each grid point
Simulation of waves – p. 70
Derivation of the model (1)
y
x
Physical assumptions:
the string = a line in 2D space no gravity forces
up-down movement (i.e., only iny-direction) Physical quantities:
r=xi+u(x, t)j: position T(x): tension force (along the string)
θ(x): angle with horizontal direction
%(x): density
Simulation of waves – p. 71
Derivation of the model (2)
y x T T (x-h/2) (x+h/2) u(x,t) ρ ∆s h
Physical principle: Newton’s second law applied to a small (infinitesimal) part of the string
sum of forces=total mass·acceleration
Derivation of the model (3)
Great mathematicians had great problems with understanding how to set up the mathematical model for a vibrating string
Euler, D’Alambert and Taylor all made various attempts (which look “stupid” by today’s standards...)
Lagrange was the first one to derive the right partial differential equation
This happened about 100 years after Newton had presented the mathematics and physics we need to derive this PDE
The derivation to be presented here is typical: simple principles, but lots of mathematical details; it’s easy to get lost in the details
Simulation of waves – p. 73
Derivation of the model (4)
y x T T (x-h/2) (x+h/2) u(x,t) ρ ∆s h Acceleration: a=∂ 2r ∂t2= ∂2u ∂t2j
Newton’s law applied to a string element: Tx+h 2 −Tx−h2=%(x)∆s∂ 2u ∂t2(x, t)j
⇒A vector equation with two components
Simulation of waves – p. 74
Derivation of the model (5)
y x T T (x-h/2) (x+h/2) u(x,t) ρ ∆s h The tension reads
T(x) =T(x) cosθ(x)i+T(x) sinθ(x)j Newton’s law in component form
T(x+h 2) cosθ(x+ h 2)−T(x− h 2) cosθ(x− h 2) = 0 T(x+h 2) sinθ(x+ h 2)−T(x− h 2) sinθ(x− h 2) =%(x)∆s ∂2u ∂t2 Simulation of waves – p. 75
Derivation of the model (6)
Divide the first component byhand leth→0
∂ ∂x Tcosθ
= 0 Similarly for the second component
∂ ∂x Tsinθ =%lim h→0 ∆s h ∂2u ∂t2 Simulation of waves – p. 76
Derivation of the model (7)
We need to determine the limitlimh→0∆s/h
Assume linear segment, then by Pythagoras:
∆s2=h2+ ∆u2, i.e., ∆s h = limh→0 s 1 + ∂u ∂x 2 Furthermore, tanθ=∂u ∂x,
which means that
sinθ=√ tanθ 1 + tan2θ= ∂u ∂x q 1 + ∂u ∂x 2 Simulation of waves – p. 77
Derivation of the model (8)
Altogether this gives
% " 1 + ∂u ∂x 2#12 ∂2u ∂t2= ∂ ∂x T " 1 + ∂u ∂x 2#−1 2 ∂u ∂x
which is a nonlinear partial differential equation.
Assume small vibrations, i.e.,(∂u/∂x)21. For small vibrations,
θ(x)≈0, such that 0 = ∂ ∂x Tcosθ = ∂ ∂x T 1−θ 2 2!+. . . ≈ ∂ ∂xT
This means thatTis approximately a constant and that the square roots are 1
Simulation of waves – p. 78
Summing up
The governing PDE:
∂2u
∂t2=c 2∂2u
∂x2 c 2=T /%
String fixed at the ends:
u(a, t) =u(b, t) = 0 String initially at rest:
u(x,0) =I(x), ∂u ∂t(x,0) = 0
Simulation of waves – p. 79
The scaled wave equation problem
We scale the equations (γ≡1, but kept as a label) and arrive at the following initial-boundary value problem:
∂2u ∂t2=γ 2∂2u ∂x2, x∈(0,1), t >0 u(x,0) =I(x), x∈(0,1) ∂ ∂tu(x,0) = 0, x∈(0,1) u(0, t) = 0, t >0, u(1, t) = 0, t >0 Simulation of waves – p. 80
Finite difference approximation (1)
Introduce a grid in space-time
xi = (i−1)h, i= 1, . . . , n t` = `∆t, `= 0,1, . . . h x t ∆t Simulation of waves – p. 81
Finite difference approximation (2)
Central difference approximations:
∂2 ∂x2u(xi, t`) = u` i−1−2u`i+u`i+1 h2 +O(h 2) ∂2 ∂t2u(xi, t`) = u`−1 i −2u`i+u`i+1 ∆t2 +O(∆t 2)
Insert these in the PDE
∂2u
∂t2=γ 2∂2u
∂x2
Simulation of waves – p. 82
Finite difference approximation (3)
The PDE has been transformed to a difference equation
u`−1
i −2u`i+u`i+1
∆t2 =γ
2u`i−1−2u`i+u`i+1
h2
Alluvalues at time levels`and`−1are assumed known
⇒Only one unknown term:u`+1
i
⇒Can solve foru`+1
i explicitly: u`+1 i = 2u`i−u` −1 i +γ2 ∆t2 h2 u`i−1−2u`i+u`i+1 This scheme is classified as a an explicit finite difference method; no need to solve coupled systems of linear equations (⇒easier programming!)
Simulation of waves – p. 83
The computational procedure
h x
t
∆t
Can findu`+1
i for oneiat a time ifuatt`andt`−1is known
Needu−1
i andu0ifor allito start the algorithm
Simulation of waves – p. 84
Initial conditions (1)
u0
i=I(xi): evaluate directly A bit more challenging:
∂u ∂t t=0 = 0 ⇒ u 1 i−u −1 i 2∆t = 0 ⇒ u −1 i =u1i butu−1
i is outside the legal time grid... Idea: eliminateu−1
i by using the discrete PDE att= 0, which gives a special formula for the first step:
u1 i=u0i+γ2 ∆t2 2h2 u 0 i−1−2u0i+u0i+1 Simulation of waves – p. 85
Initial conditions (2)
It is awkward to have a special first step. Instead we introduce
u−1 i =u0i+γ2 ∆t2 2h2(u 0 i+1−2u0i+u0i−1)
and use the standard difference equation also at the first step
Simulation of waves – p. 86
Algorithm
Define storageu+ i ,ui,u−i forui`+1,u`i,u` −1 i and setC=γ∆t/h Set initial conditions:ui=I(xi), i= 1, . . . , nDefine the artificial quantityu−
i (i= 2, . . . , n−1) u− i =ui+1 2C 2(u i+1−2ui+ui−1),
Sett= 0; whilet < tstop
t=t+ ∆t
Update all inner points (i= 2, . . . , n−1)
u+
i = 2ui−u−i +C2(ui+1−2ui+ui−1)
Set boundary condition:u+
1= 0, u+n= 0 Initialize for next stepu−
i =ui, ui=u+i, alli
Simulation of waves – p. 87
Diffpack code in F77/C style
We use functions in C++
void timeLoop (ArrayGen(real)& up, ArrayGen(real)& u, ArrayGen(real)& um, real tstop, real C); void setIC (real C, ArrayGen(real)& u0, ArrayGen(real)& um);
The main program:
ArrayGen(real) up (n); // u at time level l+1 ArrayGen(real) u (n); // u at time level l ArrayGen(real) um (n); // u at time level l-1 // get n and Courant number C=dt/dx from the user timeLoop (up, u, um, tstop, C); // finite difference scheme
The timeLoop function
void timeLoop (ArrayGen(real)& up, ArrayGen(real)& u, ArrayGen(real)& um, real tstop, real C) {
int n = u.size(); // length of the vector u (no of grid points) real h = 1.0/(n-1); // length of grid intervals
real dt = C*h; // time step, assumes unit wave velocity!!
real t = 0; // time
setIC (C, u, um); // set initial conditions
int i; // loop counter over grid points
int step_no = 0; // current step number
while (t <= tstop) {
t += dt; step_no++; // increase time; count no. of steps // update inner points according to finite difference scheme: for (i = 2; i <= n-1; i++)
up(i) = 2*u(i) - um(i) + sqr(C)*(u(i+1) - 2*u(i) + u(i-1)); up(1) = 0; up(n) = 0; // update boundary points: um = u; u = up; // update data struct. for next step }
}
Simulation of waves – p. 89
The setIC function
void setIC (real C, ArrayGen(real)& u0, ArrayGen(real)& um) {
int n = u0.size(); // length of the vector u
real x; // coordinate of a grid point
real h = 1.0/(n-1); // length of grid intervals real umax = 0.05; // max string displacement
int i; // loop counter over grid points
for (i = 1; i <= n; i++) { // set the initial displacement u(x,0) x = (i-1)*h;
if (x < 0.7) u0(i) = (umax/0.7) * x; else u0(i) = (umax/0.3) * (1 - x); }
for (i = 2; i <= n-1; i++) // set the help variable um: um(i) = u0(i) + 0.5*sqr(C) * (u0(i+1) - 2*u0(i) + u0(i-1)); um(1) = 0; um(n) = 0; // dummy values, not used in the scheme }
Simulation of waves – p. 90
Dumping solution to file
We dump the solution at each time point to file such that we can make a movie after the simulation is finished
Diffpack has tools for managing a large number of curves on files
CurvePlotFile
: manager for a collection of curvesCurvePlot
: a variable that holds a curveCode example:
int n = u.size(); // the number of unknowns real h = 1.0/(n-1); // length of grid intervals CurvePlot plot (plotfile); // a single plot plot.initPair ("displacement", // plot title
oform("u(x,%g)",t), // name of function
"x", // name of indep. var.
oform("C=%g, h=%g",C,h)); // comment
for (int i = 1; i <= n; i++) // add (x,y) data points plot.addPair (h*(i-1) /* x-value */, u(i) /* y value */); plot.finish();
Simulation of waves – p. 91
Visualizing the results
The simulation produces a Diffpack case with name
SIMULATION
Central files generated in the simulation:
SIMULATION.dp
— logfile for simulation, i.e., runtimeSIMULATION.map
— overview of data filesSIMULATION.files
— explanation of what the files are.SIMULATION_1
,.SIMULATION_2
, ... the (hidden) datafiles
Simulation of waves – p. 92
Animation
Make the animation using Diffpack features: curveplotmovie gnuplot SIMULATION.map -0.1 0.1
(script) (program) (name of map file) (ymin) (ymax)
Can replace
gnuplot
bymatlab
curveplotmovie matlab SIMULATION.map -0.1 0.1
Simulation of waves – p. 93
Varying the Courant number C
C=γ∆t h, t= 0.5, h= 1/20 0 0.1 0.20.3 0.4 0.5 0.6 0.70.8 0.9 1 −0.02 −0.015 −0.01 −0.005 0 0.005 0.01 0.015 (a)C= 1.0 0 0.1 0.2 0.30.4 0.5 0.6 0.7 0.8 0.9 1 −0.1 −0.08 −0.06 −0.04 −0.02 0 0.02 0.04 0.06 0.08 (b)C= 1.05 Simulation of waves – p. 94
Varying the Courant number C
0 0.1 0.2 0.3 0.40.5 0.6 0.7 0.80.9 1 −0.015 −0.01 −0.005 0 0.005 0.01 0.015 (c)C= 0.8 0 0.1 0.2 0.30.4 0.5 0.6 0.70.8 0.9 1 −0.015 −0.01 −0.005 0 0.005 0.01 0.015 (d)C= 0.3 Simulation of waves – p. 95
Another numerical example
−10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=0.0, σ=103 −10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=15.0, C=1.0 −10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=15.0, C=0.8 −10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=0.0, σ=1 −10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=15.0, C=0.99 −10 −5 0 5 10 0 0.2 0.4 0.6 0.8 1 Time t=15.0, C=0.8 Simulation of waves – p. 96
Numerical stability and accuracy
We have two parameters:∆tandh
How do we choose∆tandh? Too large∆tandhgive
- too large numerical errors
- or in the worst case: unstable solutions
Too small∆tandhrequire too much computing power Simplified problems can be analyzed theoretically, which yields a guide to choosing∆tandh
Basic result for our wave equation:∆t≤h/γ(derived later) Peculiar case: exact solution is obtained by∆t=h/γ, regardless of
h(!!!)
Simulation of waves – p. 97
2D wave equation
2D wave equation – p. 98
A more general wave equation
General form of a 1D/2D/3D wave equation foru(x, t):
∂2u
∂t2=∇ ·[λ(x)∇u]
Wave travels with velocityγ=√λ
The operator∇ ·[λ(x)∇u]is frequently encountered in this course! In 2D the operator is written out as
∂ ∂x λ(x, y)∂u ∂x +∂ ∂y λ(x, y)∂u ∂y
Goal for the next slides: Learn how to discretize a 2D wave equation with variable coefficients
We shall do this by putting together elements we have learned so far
2D wave equation – p. 99
Applications of wave the equation
Vibrations of a string (1D) Vibrations of a drum (2D)
Large destructive water waves (2D; water elevation) Sound waves (1D: organ pile, flute; 3D: room, space) Light and radio waves (3D)
2D wave equation – p. 100
Example: earthquake-generated waves
−0.15 −0.105 −0.06 −0.015 0.03 0.075 0.12 0.165 0.21 0.255 0.3 X Y Z −1 0 0.2 −20 −10 0 10 20 −20 −10 0 10 20
Note: scales are distorted! Earthquake close to seamount
Effect of earthquake: Sudden elevation of the surface, modeled here as a prescribed initial surface at rest
Domain = segment of the ocean
2D wave equation – p. 101
Principles
∂2u ∂t2 =∇ ·[λ(x)∇u] ∂2u ∂t2 = ∂ ∂x λ∂u ∂x +∂ ∂y λ∂u ∂yTime discretization as for 1D wave eq. Space discretization: generalized from(λ(x)u0
)0 Boundary conditions: generalized fromu(0) = 0andu0
(1) = 1 Initial conditions as for 1D wave eq.
Overall algorithm as for 1D wave eq.
2D wave equation – p. 102
Discretization
Seek approximationu`
i,jon a rectangular grid tou(xi, yj, t`)
xi= (i−1)∆x, yj= (j−1)∆y, t`=`∆t Approximate derivatives by central differences
∂2u ∂t2 ` i,j ≈u `+1 i,j −2u`i,j+u` −1 i,j ∆t2
A spatial term like ∂ ∂y
λ∂u
∂y
takes the form 1 ∆y λi,j+1 2 u` i,j+1−u`i,j ∆y ! −λi,j−1 2 u` i,j−u`i,j−1 ∆y !! 2D wave equation – p. 103
The scheme
The finite difference scheme takes the form
u`+1
i,j = 2u`i,j−u` −1
i,j + [∆u]`i,j Exercise: derive the expression for[∆u]`
i,j
Algorithm (BC:
u
= 0
)
DEFINITIONS:
Storageu+
i,j,ui,j, andu−i,jforui,j`+1,u`i,j, andu` −1
i The whole grid:
¯ (∞) ={i= 1, . . . , nx, j= 1, . . . , ny} Inner points: (∞) ={i= 2, . . . , nx−1, j= 2, . . . , ny−1} INITIAL CONDITIONS: ui,j=I(xi, yj), (i, j)∈(∞) SET ARTIFICIAL QUANTITYu− i,j: u−
i,j=ui,j+12[4u]i,j, (i, j)∈(∞) Sett= 0
2D wave equation – p. 105
Algorithm (BC:
u
= 0
)
Whilet≤tstop
t←t+ ∆t
UPDATE ALL INNER POINTS:
u+
i,j= 2ui,j−u −
i,j+ [4u]i,j, (i, j)∈(∞)
INITIALIZE FOR NEXT STEP:
u−
i,j=ui,j, ui,j=u+i,j, (i, j)∈(∞)
2D wave equation – p. 106
A model for water waves
Physical assumption: long waves in shallow water Corresponding mathematical model:
∂2u
∂t2=∇ ·
gH(x)∇u
Physical quantities:
u(x, y, t): water surface elevation
g: acceleration of gravity
H(x, y): still-water depth Boundary condition at coastline
∂u
∂n≡ ∇u·n= 0
(full reflection of waves)
2D wave equation – p. 107
Scaling
LetHcbe a characteristic value ofH(x, y) We introduce new variables
¯
x=x/Hc,y¯=y/Hc,¯t=t/
p
Hc/g, λ=H/Hc ¯
u=u/uc(uccancels and can be arbibtary) Inserted in the equation:
∂2u¯
∂¯t2 = ¯∇ ·
λ∇¯u¯
⇒ g(the driving force) is scaled away
2D wave equation – p. 108
Implementing boundary conditions (1)
There are two ways of handling∂u/∂n= 0conditions:
“Ghost cells” at boundary with explicit updating of fictitious values Modify stencil at boundary
We choose the second option, as this allows direct output ofui,jto a visualization program, i.e., no need to remove ghost cells.
2D wave equation – p. 109
Implementing boundary conditions (2)
Consider the boundaryi= 1(x=const) Boundary condition: ∂u ∂n≡ ∂u ∂x= 0 Discrete version: u2,j−u0,j 2∆x = 0 ⇔ u2,j=u0,j u0,jis outside the legal mesh
Use the discrete PDE fori= 1and eliminateu0,j, that is, just replace
u0,jbyu2,j
2D wave equation – p. 110
Modified difference operator
The boundary condition modifies the finite difference equations, and this can be viewed as modifying the operator[4u]i,j(in this example ati= 1) according to [4u]1,j:i−1→i+1≡ ∆t ∆x 2 λ1+1 2,j(u2,j−u1,j)−λ1−12,j(u1,j−u2,j) + ∆t ∆y 2 λ1,j+1 2(u1,j+1−u1,j)−λ1,j−12(u1,j−u1,j−1) 2D wave equation – p. 111
Efficiency issues
Two things should be considered:
Loops should be ordered such thatu(i, j)is traversed in the order it is stored. In Diffpack ArrayGen objects are stored columnwise. Therefore the loop should read:
for (j = 1; j <= ny; j++) for (i = 1; i <= nx; i++)
u(i,j) = ...
One should avoid
if
statements in loops if possible (they prevent many compiler optimization techniques); hence we will have separate loops over inner and boundary points.Remark I: Debug code before optimizing it!!
Remark II: Focus on a readable and maintainable code before thinking of efficiency
Updating of internal points
We define a function for updating the solution: WAVE(u+, u, u−
, a, b, c)
This function reads, at inner points,
u+
i,j= 2aui,j−bu−i,j+c[4u]i,j, (i, j)∈(∞)
2D wave equation – p. 113
Updating of boundary points (1)
i= 1, j= 2, . . . , ny−1;
u+
i,j= 2aui,j−bu−i,j+c[4u]i,j:i−1→i+1,
i=nx, j= 2, . . . , ny−1; u+ i,j= 2aui,j−bu − i,j+c[4u]i,j:i+1→i−1, j= 1, i= 2, . . . , nx−1; u+ i,j= 2aui,j−bu − i,j+c[4u]i,j:j−1→j+1, j=ny, i= 2, . . . , nx−1; u+
i,j= 2aui,j−bu−i,j+c[4u]i,j:j−1→j+1,
2D wave equation – p. 114
Updating of boundary points (2)
i= 1, j= 1; u+ i,j= 2aui,j−bu − i,j+c[4u]i,j:i−1→i+1,j−1→j+1 i=nx, j= 1; u+
i,j= 2aui,j−bu−i,j+c[4u]i,j:i+1→i−1,j−1→j+1
i= 1, j=ny;
u+
i,j= 2aui,j−bu−i,j+c[4u]i,j:i−1→i+1,j+1→j−1
i=nx, j=ny;
u+
i,j= 2aui,j−bu−i,j+c[4u]i,j:i+1→i−1,j+1→j−1
2D wave equation – p. 115
Modified algorithm (BC:
∂u/∂n
= 0
)
DEFINITIONS: as above INITIAL CONDITIONS: ui,j=I(xi, yj), (i, j)∈(∞) SET ARTIFICIAL QUANTITYu− i,j: WAVE(u− , u, u− ,0.5,0,0.5) Sett= 0 2D wave equation – p. 116
Modified algorithm (BC:
∂u/∂n
= 0
)
Whilet≤tstop
t←t+ ∆t
UPDATE ALL POINTS:
WAVE(u+, u, u−
,1,1,1)
INITIALIZE FOR NEXT STEP:
u−
i,j=ui,j, ui,j=u+i,j, (i, j)∈(∞)
2D wave equation – p. 117
Diffpack/C++ written in F77/C style
src/fdm/intro/Wave2D #include <Arrays_real.h>
#include <FieldLattice.h> #include <SimRes2mtv.h>
// We define a macro LaplaceU to save typing of long // finite difference formulas. For example, //
// #define mac(X) q0(i,j-X) //
// defines a macro mac(X) and any text mac(i+2) will then be // transformed to q0(i,j-i+2) by the C/C++ preprocessor (cpp). #define LaplaceU(i,j,im1,ip1,jm1,jp1) \
sqr(dt/dx)*\
( 0.5*(lambda(ip1,j )+lambda(i ,j ))*(u(ip1,j )-u(i ,j )) \ -0.5*(lambda(i ,j )+lambda(im1,j ))*(u(i ,j )-u(im1,j )))\ +sqr(dt/dy)*\
( 0.5*(lambda(i ,jp1)+lambda(i ,j ))*(u(i ,jp1)-u(i ,j )) \ -0.5*(lambda(i ,j )+lambda(i ,jm1))*(u(i ,j )-u(i ,jm1)))
2D wave equation – p. 118
More code ...
void WAVE (ArrayGenSel(real)& up, const ArrayGen(real)& u, const ArrayGenSel(real)& um, real a, real b, real c, const ArrayGenSel(real)& lambda, real dt, real dx, real dy) {
int nx, ny; up.getDim (nx, ny); int i,j;
// update inner points according to finite difference scheme: for (j = 2; j <= ny-1; j++)
for (i = 2; i <= nx-1; i++) up(i,j) = a*2*u(i,j) - b*um(i,j)
+ c*LaplaceU(i,j,i-1,i+1,j-1,j+1);
// update boundary points (modified finite difference schemes): i=1; for (j = 2; j <= ny-1; j++) up(i,j)=a*2*u(i,j)-b*um(i,j) +c*LaplaceU(i,j,i+1,i+1,j-1,j+1); i=nx; for (j = 2; j <= ny-1; j++) up(i,j)=a*2*u(i,j)-b*um(i,j) + c*LaplaceU(i,j,i-1,i-1,j-1,j+1); j=1; for (i = 2; i <= nx-1; i++) up(i,j)=a*2*u(i,j)-b*um(i,j) + c*LaplaceU(i,j,i-1,i+1,j+1,j+1); ... 2D wave equation – p. 119
More code ...
int main (int argc, const char* argv[]) {
initDiffpack (argc, argv);
s_o << "Give number of intervals in x and y direction: "; int h; s_i >> h; int nx = h+1; s_i >> h; int ny = h+1; s_o << "Give width of domain in x direction: "; real wx; s_i >> wx;
s_o << "Give width of domain in y direction: "; real wy; s_i >> wy;
// ArrayGenSel is like ArrayGen, but has increased functionality // for finite difference methods
// (we need it for the FieldLattice object for visualization) ArrayGenSel(real) up (nx,ny); // u at time level l+1 ArrayGenSel(real) u (nx,ny); // u at time level l ArrayGenSel(real) um (nx,ny); // u at time level l-1 ArrayGenSel(real) lambda (nx,ny); // variable coefficient const real dx = wx/(nx-1); // length of grid intervals in x dir. const real dy = wy/(ny-1); // length of grid intervals in y dir. s_o << "Give time step length: (0 gives dt=dx): ";
real dt; s_i >> dt;
More code ...
// fill lambda with values .... (see source file) // fill um with initial values:
um.fill(0.0);
// set the help variable um:
WAVE (um, u, um, 0.5, 0, 0.5, lambda, dt, dx, dy);
int step_no = 0; // current step number
while (t <= tstop) {
t += dt; // increase time by the time step step_no++; // increase step number by 1 s_o << "t=" << t << "\n";
WAVE (up, u, um, 1, 1, 1, lambda, dt, dx ,dy);
um = u; u = up; // update data struct. for next step }
// dump solution to file ... (see source file)
2D wave equation – p. 121
Visualizing the results
unix> plotmtv -colorps W0017.tmp.mtv
2D wave equation – p. 122
Nature of some PDEs
Nature of some PDEs – p. 123
Hyperbolic equations (1)
Linear PDEs can be divided into basic categories: elliptic, parabolic, and hyperbolic
A typical hyperbolic equation is the wave equation
∂2u
∂t2 =γ 2∂2u
∂x2
which has the general solution
u(x, t) =f(x−γt) +g(x+γt) i.e., two waves propagating to the left and to the right
The functionsfandgare determined fromu(x,0)and∂u(x,0)/∂t. Disturbances travel with a finite wave speed.
Reference: HPL appendix A.5
Nature of some PDEs – p. 124
Changing boundary conditions
Boundary conditions:u(0, t) = 0,u(1, t) = 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.300 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.667 Boundary conditions:u(0, t) = 0,ux(1, t) = 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.300 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.667
Nature of some PDEs – p. 125
Observations
Changing the BC influences the solution at only some(x, t)points (e.g., at the midpoint, after the initial disturbance has left, a change in the BC atx= 1is not felt before the pulse has been reflected from the boundary)
An initial disturbance is transported without change of shape (essential for oral communication!)
Nature of some PDEs – p. 126
BC: let waves leave the domain
Boundary conditions:u(0, t) = 0and ut+γux
|x=1= 0 0 0.10.2 0.30.4 0.50.60.7 0.80.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.300 0 0.10.2 0.30.4 0.50.60.7 0.80.9 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 Time t=0.667
The wave propagates out of the domain, exactly as we want for ocean waves! Unfortunately, the condition is hard to generalize successfully to physically relevant 2D cases
Nature of some PDEs – p. 127
Other hyperbolic equations (2)
Uni-directional “wave” or transport equation:
∂u ∂t+γ ∂u ∂x= 0 where u(x, t) =I(x−γt)
I(x)is the initial conditionu(x,0)
Systems of wave equations (here: long ocean waves)
∂η ∂t = − ∂ ∂x(uH)− ∂ ∂y(vH) −∂H ∂t ∂u ∂t = − ∂η ∂x, x∈Ω, t >0 ∂v ∂t = − ∂η ∂y, x∈Ω, t >0
Other hyperbolic equations (2)
Multi-dimensional standard wave equation:
∂2u
∂t =∇ ·(λ∇u)
Applications: radio waves, light, sound, membranes, ... Nonlinear hyperbolic conservation law:
∂u ∂t+
∂ ∂xf(u) = 0
or a system of such equations:
∂u
∂t+ ∂ ∂xf(u) =0
Applications: gas dynamics, oil reservoir flow
Nature of some PDEs – p. 129
Elliptic equations
Elliptic equations are stationary (equilibrium or steady-state physical conditions)
Example:
−u00
(x) = 2, u(0) = 0, u0 (1) = 0 Let us look at the effect of changingu0
(1) = 0tou(1) = 0
Nature of some PDEs – p. 130
Changing boundary conditions
u0 (1) = 0versusu(1) = 0: 0 0.2 0.4 0.6 0.8 1 1.2 0 0.2 0.4 0.6 0.8 1 u(1)=0 u’(1)=0
Observe: All points in the interior are effected by the boundary condition! And the solution is smooth.
Nature of some PDEs – p. 131
Multi-dimensional elliptic equations
The Poisson equation is a typical multi-dimensional elliptic equation:
−∇2u(x) =f(x)
or
−∇ ·(λ(x)∇u(x)) =f(x) Another elliptic equation:
−∇ ·(λ(x)∇u(x)) +αu=f(x)
Nature of some PDEs – p. 132
The Helmholtz equation
Sometimes one solves the wave equation
∂2u
∂t2 =γ 2∇2u
by assuming periodical waves in time:
u(x, y, z, t) = exp (−iωt)ˆu(x, y, z), i=√−1 This results in the famous Helmholtz equation
∇2uˆ+k2ˆu= 0, k=ω/γ
The Helmholtz equation is not an elliptic equation (wrong sign!)
Nature of some PDEs – p. 133
Parabolic equations (1)
A 1D heat equation is a typical parabolic equation:
∂u ∂t=
∂2u
∂x2+ 2, u(x,0) = 0, u(0, t) = 0
Typically for parabolic equations:
time derivative = elliptic counterpart That is, ast→ ∞, the eq. above tends to the elliptic equation
−∂
2u
∂x2= 2, u(x,0) = 0, u(0, t) = 0
Nature of some PDEs – p. 134
Changing boundary conditions
Let us see the effect of two different conditions atx= 1:u(1, t) = 0 versusux(1, t) = 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 Time t=0.300
As for the elliptic counterpart, all points in the interior are effected by the boundary condition
Nature of some PDEs – p. 135
Parabolic equations (2)
Multi-dimensional parabolic (heat) equation:
∂u
∂t=∇ ·(λ(x)∇u) +f
Recall: ast→ ∞, we normally have that∂u/∂t→0, and the parabolic equation approaches the elliptic counterpart
Numerical methods
Hyperbolic equations:
explicit schemes, must choose∆t∼h
Elliptic equations:
all grid-point values are coupled in a linear system, time-consuming to solve, requires sophisticated iterative methods
Parabolic equations:
can use explicit schemes, if∆t∼h2, but implicit schemes coupling
all points (like in elliptic equations) are preferred
Nature of some PDEs – p. 137
Why solutions of elliptic eqs. are smooth
Solutions of elliptic equations are smooth!
−u00 (x) =f(x)with “noisy”f(x): u=− Z Z f(x)dx dx
Twice integration of noisyf(x)⇒smoothu(x) (λu0
)0
= 0with noisy coefficientλ(x):
u(x) = const· Zx 0 dτ λ(τ) Roughλ⇒smoothu
Nature of some PDEs – p. 138
More general result from Fourier series
−u00
(x) =f(x)solved by Fourier series (here: sine series):
u(x) = ∞ X
i=1
uisiniπx
Expandingf(x)also in a Fourier series:
f(x) = ∞ X
i=1
fisiniπx
Inserting this in the equation gives
ui∼fi/i2
i.e., theuseries converges faster than thefseries and from Fourier series theory this means thatuis smoother thanf
Nature of some PDEs – p. 139
Smoothness and variational calculus
∇2u= 0is equivalent with min v(x) Z Ω ||∇v||2dΩ
⇒uis “smoothest” among “all”voverΩ
Nature of some PDEs – p. 140
Analysis of difference schemes
Analysis of difference schemes – p. 141
Outline
Try to explain the observed numerical behavior (accuracy, stability) by mathematical means
Tool: exact solution of the discrete equations Main focus on the wave and heat equations
Intro to classical topics like truncation error and von Neumann
stability
Analysis of difference schemes – p. 142
Operator notation (1)
Finite difference schemes are often long and difficult to read compared to the underlying PDE
Operator notation gives condensed expressions much like the PDE Define [δxu]`i,j,k≡ u` i+1 2,j,k−u ` i−1 2,j,k ∆x
with similar definitions ofδy,δz, andδt Another difference:
[δ2xu]`i,j,k≡
u`
i+1,j,k−u`i−1,j,k 2∆x
Analysis of difference schemes – p. 143
Operator notation (2)
Compound difference (1D now, to save index writing): [δxδxu]`i= 1 h2 u ` i−1−2u`i+u`i+1 One-sided forward difference:
[δ+
xu]`i≡
u` i+1−u`i
h
and the backward difference: [δ−
xu]`i≡
u` i−u`i−1
h
Operator notation for arithmetic average: [ux]` i≡ 1 2 u` i+1 2+u ` i−1 2
Operator notation (3)
Put the whole equation inside brackets: [δxδxu=−f]i This is a finite difference scheme foru00
=−f Example:(λu0 )0 = 0is discretized as [δxλ x δxu= 0]i Another example, the heat equation:
∂u ∂t=κ ∂2u ∂x2+ ∂2u ∂y2 [δ+ tu=κ(δxδxu+δyδyu)]`i,j
Analysis of difference schemes – p. 145
Typical solution of a wave equation
Wave equation: ∂2u ∂t2=γ 2∂2u ∂x2 Typical solution u=Aei(kx−ωt) for arbitraryk
Only real or imaginary part has physical interpretation, e.g., the real part isAcos(kx−ωt)
Inserting solution gives the dispersion relationω=ω(k):
ω=±γk ⇒ u=Aeik(x±γt)
Analysis of difference schemes – p. 146
General solutions
Can build general solutions as Fourier series
u=X k
Akei(kx−ωt)
...or Fourier integrals
u= Z∞
−∞
A(k)ei(kx−ωt)dk
The basic component in these general solutions is
ei(kx−ωt)
which we will study in the following
Analysis of difference schemes – p. 147
Physical interpretation
What doA,k, andωin u=Aei(kx−ωt) really mean? -1.5 -1 -0.5 0 0.5 1 1.5 0 1 2 3 4 5 6 λ c Aλ= 2π/kis the wave length,c=ω/k=γis the wave velocity
Analysis of difference schemes – p. 148
A heat equation
The heat (diffusion) equation:
∂u ∂t =κ
∂2u
∂x2
Typical solutionu=Aexp (i(kx−ωt))for arbitraryk
Inserting solution givesω=−iκk2(called the dispersion relation)
The form of the solution:
u=Ae−κk2t eikx
Can build more complicated solutions through Fourier series or integral
Analysis of difference schemes – p. 149
The damping in the heat equation
A general solution component:
u=Ae−κk2t eikx damps short waves (bigk) significantly (exp (−k2))
Example: add two components,k=πandk= 100π(choose
κπ= 1), consider the imaginary part:
u(x, t) = 1·e−tsinπx+ 0.6·e−10000tsin 100πx This is a sine with period 1 plus a 60 percent perturbation which oscillates 100 times faster
The damping factor of the perturbation isexp (−10000)that of the first term; aftert= 1/1000this damping os about5·10−5
Recall: no damping in the wave equation
Analysis of difference schemes – p. 150
Plot of the damping
-0.5 0 0.5 1 1.5 2 0 0.2 0.4 0.6 0.8 1 u(x,0) -0.5 0 0.5 1 1.5 2 0 0.2 0.4 0.6 0.8 1 u(x,0.001)
Analysis of difference schemes – p. 151
Solution of discrete equations
Consider the explicit finite difference for the wave equation, withu` jas unknown
A typical solution reads
u` j=Aei(kjh
−ω`˜∆t)=Aei(kx−ωt˜)
⇒same structure as the solution of the PDE! Inserting this form ofu`
iin the scheme: ˜
ω= ˜ω(k, h,∆t) (numerical dispersion relation)
The velocityc=ω/kshould beγ(constant), but will now depend on
k,h, and∆t
Hopefully,c→γash,∆t→0
The wave equation scheme
Insertingu`
j= exp (i(kjh−ω`˜∆t))in the wave equation scheme gives sin˜ω∆t 2 =± γ∆t h sin kh 2
Can solve forω˜and then we have an analytical solution of the discrete equations: ˜ ω=±2 ∆tarcsin γ∆t h sin kh 2
Can assess the accuracy by plottingω−ω˜
Better: plotc=ω/kas a function ofkhfor different Courant (γ∆t/h) numbers
Can assess the accuracy by investigating
ω−ω˜=−241γk3(h2−γ2∆t2) +O(h2∆t2, h4,∆t4)
Analysis of difference schemes – p. 153
Stability (1)
We know that the exact solution of the wave equation PDE contains no damping or no growth of a wave component, i.e.,
ωis real
A numerical wave component should exhibit the same qualitative behavior
...it can be slightly damped, but not amplified (for sufficiently larget
the wave becomes arbitrarily large)
⇒ Should have real˜ω(or a small negative imaginary part only, i.e., slight damping)
The equation for˜ω: sinω˜∆t 2 =± γ∆t h sin kh 2
Analysis of difference schemes – p. 154
Stability (2)
The equation forω˜, sin˜ω∆t 2 =± γ∆t h sin kh 2 can also have complexω˜
Complexω˜will occur in conjugate pairs, i.e., one root has positive imaginary part, leading to wave growth, and cannot be allowed
⇒Only realω˜can be accepted
sin = const×sin⇒the const must be in[−1,1], here
γ∆t h ≤1
or
∆t≤h
γ
which is the stability criterion Analysis of difference schemes – p. 155
The effect of round-off errors
Consider the initial conditionu(x,0) =Aexp (ikx)for the wave eq. General solution:
u(x, t) =Aei(kx−ωt)
Perturb initial condition: ˆ
u(x,0) =Aeikx+aei10kx, aA
This always happens on a computer, because of round-off errors If∆t≤h/γ, no numerical wave components are damped or amplified and the solution reads
u` j=Aei(kjh
−ω`˜∆t)+aei(10kjh−˜ω`∆t)
⇒ Initial perturbation (round-off errors) is not amplified
Analysis of difference schemes – p. 156
Consequences of stability
If∆t > h/γ, at least one wave component starts to grow in time, i.e., the initial perturbation is amplified
After some time, the solution is completely nonphysical
When a program gives nonsense solutions, recall that the reason can either be a bug or a too large∆t!
Analysis of difference schemes – p. 157
Accuracy
The exact and numerical solutions,
exp (i(kx−ωt)) and exp (i(kx−ωt˜)) have the same basic structure, only theωvalues differ Can assess the numerical accuracy asEω=ω−ω˜
Can plotEωor make a Taylor series expansion in terms of the grid and physical parameters:
Eω=−1 24γk
3(h2−γ2∆t2) +O(h2∆t2, h4,∆t4)
Errors go to zero ash2,∆t2
It turns out thatEω= 0if∆t=h/γ(!)
Analysis of difference schemes – p. 158
Summary of numerical properties
The wave equation:
Accuracy:O(h2,∆t2), but exact curves can also be produced
Special result:C≡γ∆t/h= 1implies that the numerical solution is exact
Stability:∆t≤h/γ
The method of analysis applies to linear, homogeneous, time-dependent equations with constant coefficients
Analysis of difference schemes – p. 159
The heat equation; stability
The heat equation:
∂u ∂t=κ ∂2u ∂x2 can be discretized by [δ+ tu=κδxδxu]`j (explicit forward scheme,u`+1
i =old values) Inserting discrete wave component
u` j=Aei(kjh
−ω`˜∆t)=Aξ`eikjh, ξ=e−iω˜∆t
in the numerical scheme results in
ξ= 1−κ4∆t h2 sin
2kh
2
Stability criterion
Damping, i.e. no growth, implies|ξ| ≤1 With ξ= 1−κ4∆t h2 sin 2kh 2 this leads to ∆t≤h 2 2κ
as the stability criterion
Analysis of difference schemes – p. 161
Truncation error
PDE:L(u) =f
Numerical approximation:u∆,L∆(u∆) =f∆
The truncation error is defined as
τ=L∆(u)−f∆
i.e.τreflects the residual when the analytical solution is inserted in
the numerical scheme
Computational technique:
expand analyticalu(x)atxi±1in Taylor series aboutxi:
u(xi±1) = u(xi)± ∂u ∂x xi h+1 2 ∂2u ∂x xi h2±1 6 ∂3u ∂x xi h3 + 1 24 ∂4u ∂x xi h4+· · ·
Analysis of difference schemes – p. 162
Truncation error; example (1)
Problem: L(u) ≡ −u00 (x) L∆(u∆) ≡ −ui −1−2ui+ui+1 h2 Don’t multiply byh2! (τ∼ L ∆)
Insert Taylor expansions:
τ=u00
(xi) +f(xi) +1 12u
0000
(x)h2+O(h4)
PDE is fulfilled pointwise:u00
(xi) +f(xi) = 0 Second order scheme because:
τ= 1 12u
0000
(x)h2+O(h4), τ∼O(h2)
Analysis of difference schemes – p. 163
What is the truncation error?
τmeasures the error in the discrete equations when the exact solution of the continuous problem is inserted
That is,τmeasures the error in an equation (this error is called a residual), not the error in the solutionu−u∆
Hopefully,τreflects the true erroru−u∆
Analysis of difference schemes – p. 164
Truncation error; example (2)
Model problem: the 1D wave equation
L(u) ≡ ∂ 2u ∂t2−γ 2∂2u ∂x2 L∆(u∆) ≡ u `−1 i −2u`i+u`i+1 ∆t2 − γ2u`i−1−2u`i+u`i+1 h2 τ = ∂ 2u ∂t2+ 1 12 ∂4u ∂t4∆t 2+O(∆t4) − γ2∂2u ∂x2−γ 21 12 ∂4u ∂x4h 2+O(h4) τ = O(∆t2, h2)
Analysis of difference schemes – p. 165
von Neumann stability analysis (1)
Make an equation for the numerical error (see later) Seek discrete solution for the error:
e` j= X k ξ`exp (ikjh) Inserte`
jin error equation, compute for a single componentk:
e`
j= exp (ikjh)
Common stability requirement:
|e`
j|<∞ ⇒ |ξ| ≤1
Gives a condition on∆t
Very similar to discussing numerical dispersion relations
Analysis of difference schemes – p. 166
von Neumann stability analysis (2)
1D heat equation:ut=κuxx Perturbed solutionv:vt=κvxx Equation for errore=u−v:et=κexx Insertinge` j=ξ`exp (ikjh): ξ−1 ∆t =− 4κ h2sin 2kh 2 |ξ| ≤1⇒ −1≤ξ≤1: −1≤ξ= 1−4κ∆t h2 ⇒∆t≤ h 2κ 1D wave equation:utt=c2uxx, ∆t≤h c
Analysis of difference schemes – p. 167
Consistency and convergence
u: solution of continuous problem
u∆: solution of discrete problem
∆: mesh parameter (h,∆tetc) Definition of consistency:τ→0as∆→0 Example: τ= 1 12u 0000 (xi)h2+O(h4)→0ash→0
Interpretation: The analytical solution fulfulls the discrete equations as∆→0
Convergence:u∆→uas∆→0
τ→0does not implyu∆→u
Lax’ theorem
Convergence⇔consistency and stability
⇒easy tool for proving convergence!
Analysis of difference schemes – p. 169
Intro to finite elements
Intro to finite elements – p. 170
Features of the method
Flexibility
Straightforward handling of complicated geometries Easy to construct higher-order approximations Broad spectrum of applications
Popular method for demanding engineering applications Strong mathematical foundation
Intro to finite elements – p. 171
Basic principles
Finite difference method:
∂u ∂x �