Solving PDEs with freefem++ Tutorials at Basque Center BCA Olivier Pironneau 1 with Frederic Hecht, LJLL-University of Paris VI 1 March 13, 2011 Do not forget That everything about freefem++ is at www.freefem.org Check for new release every month Now in 3D and in MPI OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 1 / 38
Outline I 1 Part I: Algorithms Introduction Blocks Rannacher s Projection Scheme for Navier-Stokes eqs Floating Object Fluids and Temperature A Large Example 2 Part II: Playing with Meshes Mesh Adaptivity Domain Decomposition and zoom Application to Hydrology Discontinuous Galerkin freefem++ and Option Pricing OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 2 / 38
Introduction freefem++ has two building blocks An elliptic PDE system solver problem, solve A convection module convect Most complex nonlinear problems can be solved by appropriate algorithmic combination of both Except hyperbolic problems with shocks The power of freefem++ is also due to the capacity to handle multiple meshes in one script freefem++ has an advanced mesh adaptivity module OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 3 / 38
A simple elliptic problem u = f in Ω, u Ω = u Γ Ω u û f û = 0, Ω û H 1 0 (Ω) Take Ω the unit circle, f a Dirac at x 0, y 0 and u Γ = 0. The problems is solved in variational form approximated by a finite element method : u H 1 0 (Ω) V h = {û C 0 (Ω h ) : u h Tk P 2 triangle in the mesh} Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 4 / 38
A simple convection problem Let u be solution of D t u := t u + a u = 0, u(0) = u 0 in Ω, t < T One has D t u(x 0, t 0 ) := lim δt [u(χ(t 0), t 0 ) u(χ(t 0 δt), t 0 δt)] where χ(t) = a(χ(t), t), χ(t 0 ) = x 0 δt 0 1 Hence D t u(x 0 ) 1 δt (um+1 (x 0 ) u m (X m (x 0 ))) with X m (x 0 ) := χ(t δt) x 0 a(x 0 )δt (1) A possible scheme is u m+1 (x) = u m (X m (x)) Π h u m (x a(x)δt) The keyword convect([a1,a2],-dt,u) returns uoχ. Note that the result is not a piecewise polynomial function even when u is. The difference with u(x adt) is a that x adt is not a precise computation of χ(t dt), especially near boundaries. OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 5 / 38
A simple convection problem (II) Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 6 / 38
A convection diffusion problem Let u be solution of the convection-diffusion-dissipation equation t u + a u (µ u) + bu = 0, u(0) = u 0 in Ω, t < T It can be rewritten as D t u (µ u) + bu = f, u(0) = u 0 where D t u is the convective derivative. Let us use an implicit Euler scheme in time combined with the previous methods [ u m+1 u m ox m ] û + µ u m+1 û + bu m+1 û = 0, û H0 1 (Ω) δt Ω OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 7 / 38
Convection Diffusion problem (II) Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 8 / 38
Publish the results. e.g. Evolution of u and u 2 This exercise will show how to communicate with the outside world Execute Use gnuplot to create a one dimensional graph >cd /Users/pironneau/Desktop/bilbaoFreefem/edp >gnuplot plot "norms.txt"using 1:2 w l, "norms.txt"using 1:3 w l OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 9 / 38
Rannacher s Projection Scheme Her we used Neumann bdy conditions for the PDE instead of Dirichlet to illustrate the fact that u remains constant and u 2 decays exponentially OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 10 / 38
Rannacher s Projection Scheme Consider t u + u u + p ν u = 0, u = 0 with u(0) = u 0 and u Γ = u Γ. With X (x) = x u m (x)δt, the following is first order in time: 1 δt (um+1/2 u m ox ) + p m ν u m+1/2 = 0 q = u m+1/2 u m+1/2 u m+1 = u m+1/2 + qδt, p m+1 = p m q p m q Exercise Compare this scheme with a fully coupled scheme like 1 δt (um+1 u m ox ) + p m+1 ν u m+1 = 0, u m+1 = 0 on the driven cavity problem. OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 11 / 38
Freefem Script for Fully Implicit and Rannacher s OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 12 / 38
Floating Object Consider a cylinder C of radius R and mass µ and center q 0 := x 0 (t), y 0 (t) floating in the flow of a driven cavity with viscosity ν. To find its motion q 0 and its angular velocity ω we must apply Newtons law: µ d2 q 0 dt 2 = d ω dt = q C [ν( u + u T ) p)] n [ ] [ν( u + u T ) p] n q C The no-slip boundary condition on C requires to impose on C a velocity for the fluid equal to q 0 + ω (q q 0 ). The mesh must be rebuilt at each time step because C moves. we shall use the trick u=u to project u defined on the old triangulation onto a u defined on the new one. OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 13 / 38
Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 14 / 38
Fluids at Varying Temperature The Boussinesq approximation assumes that the density changes slightly with the temperature θ and the buyancy is a force in the fluid: t u + u u + p ν u = aθ e y, u = 0 t θ + u θ κ θ = 0 with u(0) = u 0, θ(0) = θ 0 and u Γ = u Γ, θ Γ = θ Γ. Let us study the case of a box with a hot (θ Γ = 1) and a cold wall (θ Γ = 0) t=0.107 IsoValue -412.673-281.849-151.025-20.2012 110.623 t=0.13 IsoValue -335.375-205.732-76.0899 53.5525 183.195 241.447 372.271 503.095 633.919 764.743 895.567 1026.39 1157.22 1288.04 1418.86 1549.69 1680.51 1811.34 1942.16 312.837 442.479 572.122 701.764 831.406 961.049 1090.69 1220.33 1349.98 1479.62 1609.26 1738.9 1868.55 1998.19 2072.98 2127.83 Vec Value 0 5.78617 11.5723 17.3585 23.1447 28.9309 34.717 40.5032 46.2894 52.0755 57.8617 63.6479 69.434 75.2202 81.0064 86.7926 92.5787 98.3649 104.151 Vec Value 0 6.38247 12.7649 19.1474 25.5299 31.9124 38.2948 44.6773 51.0598 57.4423 63.8247 70.2072 76.5897 82.9721 89.3546 95.7371 102.12 108.502 114.885 109.937 121.267 OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 15 / 38
Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 16 / 38
Fluids, Temperature and Turbulence t θ + u θ (κ m T θ) = 0 t u + u u (µ T u) + p + e(θ θ 0 ) e 2, u = 0 k 2 µ T = c µ ɛ, κ T = κµ T t k + u k + ɛ (µ T k) = µ T 2 u + ut 2 ɛ 2 t ɛ + u ɛ + c 2 k c ɛ (µ T ɛ) = c 1 c µ 2 k u + ut 2 = 0 1 δt (θm+1 θ m X m ) (κ m T θ m+1 ) = 0 1 δt (um+1 u m X m ) (µ m T u m+1 ) + p m+1 + e(θ m+1 θ 0 ) e 2, u m+1 1 δt (km+1 k m X m ) + k 1 δt (ɛm+1 ɛ m X m ) + c 2 ɛ µ m+1 T = c µ k m+12 ɛ m+1, m+1 ɛm k m (µm T k m+1 ) = µm T 2 um + u mt 2 m+1 ɛm κm+1 T = κµ m+1 T k m c ɛ (µ m T ɛ m+1 ) = c 1 c µ 2 km u m + u mt 2 OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 17 / 38
Fluids, Temperature and Turbulence (II) IsoValue 22.8513 23.7927 24.4204 25.048 25.6756 26.3032 26.9309 27.5585 28.1861 28.8138 29.4414 30.069 30.6967 31.3243 31.9519 32.5795 33.2072 33.8348 34.4624 36.0315 Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 18 / 38
Conclusion of Part I 1 Learn Variational Formulations 2 Learn how to devise Algorithms 3 Choose your finite element spaces Thank you for your attention OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 19 / 38
Outline I 1 Part I: Algorithms Introduction Blocks Rannacher s Projection Scheme for Navier-Stokes eqs Floating Object Fluids and Temperature A Large Example 2 Part II: Playing with Meshes Mesh Adaptivity Domain Decomposition and zoom Application to Hydrology Discontinuous Galerkin freefem++ and Option Pricing OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 20 / 38
Local Error Minimized: Estimated Results OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 21 / 38
Mesh Refinement OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 22 / 38
Local Error and Adaptivity OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 23 / 38
Mesh Refinement OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 24 / 38
Domain Decomposition with Overlapping Schwarz Algoritm To Solve u = f in Ω, u Γ = 0 in a domain Ω = Ω 1 Ω 2, on computes u as the limit of u k = f in Ω 1, u k Γ1 = u k 1 u k = f in Ω 2, u k Γ2 = u k 1 OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 25 / 38
file ddm.edp Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 26 / 38
The Schwarz-Zoom Method γ H (resp γ h ) is the interpolation operator on V H (resp V h ) Find u m+1 H V H, u m+1 h V h, such that w H V 0H, w h V 0h a H (u m+1 H, w H ) = (f, w H ), u m+1 H SH = γ H uh m, um+1 H ΓH = g H, a h (u m+1 h, w h ) = (f, w h ), u m+1 h Sh = γ h uh m, um+1 Γh = g h h OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 27 / 38
Hilbert Space Decomposition Method (JL. Lions) Model problem (for instance K = I, f = 1 + δ( x x 0 )): (K v) = f in Ω, u Γ = 0 v = U + u, U H 1 0 (Ω), u Λ H 1 0 (Λ) β(u n+1 U n ) (U n+1 + u n ) = f in Ω β(u n+1 u n ) (U n + u n+1 ) = f in Λ 0-0.1 "usmall.gnu" using 1:3 "ularge.gnu" using 1:3 "u.gnu" using 1:3 "log.gnu" using 1:3-0.2-0.3-0.4-0.5-0.6-0.7. -0.8-1 -0.5 0 0.5 1 OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 28 / 38
The COUPLEX I Test Case Figure: A 2D multilayered geometry 20km long, 500m high with permeability variations K + K = O(10 9 ). Hydrostatic pressure by a FEM. (K H) = 0, H or H given on Γ n OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 29 / 38
COUPLEX I : Concentration of Radio-Nucleides Figure: Concentration at 4 times with Discontinuous Galerkin FEM (Apoung-Despré). r t c + λc + u c (K c) = q(t)δ(x x R ) OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 30 / 38
Solution by Zoom Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 31 / 38
Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 32 / 38
Discontinuous Galerkin (II) Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 33 / 38
Pricing a Basket Option Find Given U h (T ) = (K S 1 S 2 ) + find U h H h such that V h H h ( t U h )V h [ [ 1 2 S i (σi 2 Si 2 V h ) Si U h µ i S i ( Si U h )V h ] Ω Ω i=1,2 S1 (ρσ 1 σ 2 S 1 S 2 V ) h S2 U h ] Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 34 / 38
Heston Model t P (yx)2 2 xxp (yµ)2 yy P ρy 2 xµ xy P r x P + rp = 0 2 P(x, y, T ) = (K x y) + P(0, y, t) = Ke rt P(+, y, t) = 0 y P(x, 0, t) = y P(x, +, t) = 0 Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 35 / 38
American Options u t σ2 x 2 2 u 2 x 2 rx u x + ru 0 u u, one is = with u(t, 0) = Ke rt, u(0, x) = u (x) := (K x) + { V = v L 2 (R + ), x v } x L2 (R + ), K = {v L 2 (0, T; V), v u }. Find u K C 0 ([0, T ]; L 2 (R + u )), t L2 (0, T ; V ), s.t. ( u t, v u) + a(u, v u) 0, v K, u(t = 0) = u, with ( σ 2 v w a(v, w) = x2 2 x x + (σ2 + x σ ) x r)x v x w + rvw dx. R + Assume σ [σ, σ] and x x σ 2 < M, then the solution exists and is unique. OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 36 / 38
Semi-Smooth Newton Method Kunisch et al suggested to reformulate the problem as a(u, v) (λ, v) = (f, v) v H 1 (R + ), λ min{0, λ + c(u φ)} = 0, i.e.au λ = f The last eq. is equivalent to λ 0, λ λ + c(u φ) i.e. u φ, λ 0, with equality on one of them for each S. This problem is equivalent for any real constant c > 0 because λ is the Lagrange multiplier of the constraint. Newton s algorithm applied to (3) gives Choose c > 0,, u 0, λ 0, set k = 0. Determine A k := {S : λ k (S) + c(u k (S) φ(s)) < 0} Set u k+1 = arg min u H 1 (R + ){ 1 2 a(u, u) (f, u) : u = φ on A k} Set λ k+1 = f Au k+1 OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 37 / 38
Results "exercise_250" 100 90 80 70 60 50 40 30 20 10 0 10 20 30 40 50 60 70 80 90 100 0 Best of put basket option, σ 1 = 0.2, σ 2 = 0.1, ρ = 0.8 Execute OP ( www/ann.jussieu.fr/pironneau) Solving PDEs with freefem++ March 13, 2011 38 / 38