Aims Wednesday, 29 August 2012 • Numerical solution of differential equations • Techniques • Stability • Boundary conditions • Introduction to parallelism • Applications to solar physics (MHD) Wednesday, 29 August 2012 Chris Brady Centre for Fusion, Space and Astrophysics, Warwick Computer Simulations in Solar Physics B) Wednesday, 29 August 2012 vxn+1 t vxn q n = (vy Bz ) m dvx vxn+1 vxn ⇥ dt t vx (t) dx =v dt dvx vx (t + dt) = lim dt 0 dt dt Wednesday, 29 August 2012 dv q = (v dt m Wednesday, 29 August 2012 Wednesday, 29 August 2012 -10 -5 vy0 = 0 vx0 = qBz/m 0 5 10 t = 0.01 m/(qBz) x0y = 1 n xn+1 = x y y + tvyn tq n = + (vy Bz ) m tq n n = vy (vx Bz ) m n n xn+1 = x + tv x x x vxn x0x = 0 vyn+1 vxn+1 10 5 0 -5 -10 t2 d2 f (t) + 2 dt2 t t Wednesday, 29 August 2012 0 2 4 6 8 Wednesday, 29 August 2012 0 0.5 1 Time 1.5 d2 V dV + =V dt2 dt dV =V dt 2 + O( t3 ) • Ignoring higher order terms • Effectively diffuses the solution outwards t) = f (t) + df (t) t dt Why didn’t it work? f (t + Value t f (t) = f (t + ) 2 Wednesday, 29 August 2012 f (t + t df 2 dt t t df t) = f (t + )+ 2 2 dt Wednesday, 29 August 2012 f (t + t+ t+ 2 t2 d 2 f + 8 dt2 t 2 t2 d 2 f + 8 dt2 t t t ) 2 2 t t t) = f (t + + ) 2 2 f (t) = f (t + t+ t+ t 2 t 2 +O( t3 ) +O( t3 ) t+ t/2 ⇥ t+ f (t + t df 2 dt t+ 2 t) t f (t) t2 d 2 f + 8 dt2 t 2 t2 d 2 f + 8 dt2 t t+ t+ t 2 t 2 = = = = = n+ 12 vy xn+1 x xn+1 y vxn+1 vyn+1 Wednesday, 29 August 2012 = n+ 12 vx + + n+ 12 tvy n+ 12 tvx +O( t3 ) +O( t3 ) vxn tq n+ 12 + (vy Bz ) m tq n+ 12 n vy + (vx Bz ) m xny xnx tq n (vy Bz ) + 2m tq n n vy + (vx Bz ) 2m vxn • Only need to know them to 1st order timestep • Need to know RHS of equations at half df dt t f (t) = f (t + ) 2 Wednesday, 29 August 2012 f (t + t t df t) = f (t + )+ 2 2 dt Wednesday, 29 August 2012 Wednesday, 29 August 2012 1.5 1 0.5 0 -0.5 -1 -1.5 -1.5 -1 -0.5 0 0.5 1 1.5 Wednesday, 29 August 2012 Wednesday, 29 August 2012 = k4 = = k3 t) = k2 f (t + = df (t) dt k1 = tg(f (t) + t t k1 , t + ) 2 2 t t tg(f (t) + k2 , t + ) 2 2 tg(f (t) + tk3 , t + t) k1 + 2k2 + 2k3 + k4 f (t) + 6 tg(f (t), t) g(f (t), t) called the Runge-Kutta 4th order algorithm • Yes. Arbitrarily high order is possible. • Most common high order time integrator is Can we do better? Conclusions Wednesday, 29 August 2012 integrator instead • For many particles usually use Boris cases • 4th order good enough for most problems • Can use sympletic integrator in extreme problem under consideration • Have to have high enough order for Wednesday, 29 August 2012 Wednesday, 29 August 2012 Space Hyperbolic equations Wednesday, 29 August 2012 Time place + O( x3 ) ⇥ Error terms x) ui+1 ui 2 x x2 2 u u 3 + x +O( x ) x x 2 x2 x Wednesday, 29 August 2012 wrong • Odd derivative terms are dispersive • Numerical dispersion - get wave speeds • Once again ignoring higher order terms • Even derivative terms are diffusive • Numerical diffusion - energy in wrong Wednesday, 29 August 2012 x) u(x 2 x x) = u(x) u(x u u(x + = x x x) = u(x) + u(x + u x2 2 u 3 x + +O( x ) x x 2 x2 x ⇥u(x, t) ⇥u(x, t) +c =0 ⇥t ⇥x 1 x c Lx /2 un0 = unnx unnx +1 = un1 t= u0i = e xi x = Lx /nx x = [0, Lx ] ⇥2 Wednesday, 29 August 2012 uni = Sin + n i identify how error terms grow from iteration to iteration • Write new equation for the errors and solution S and error terms ε • Write the numerical solution as an exact Von Neumann Stability Analysis Wednesday, 29 August 2012 un+1 i n ⇥ n u u i 1 = uni + c t i+1 2 x Wednesday, 29 August 2012 c t n (S 2 x i+1 Sin 1 ) G= =1+ = = n i 1) + c t ( 2 x c t i sin(k x) x x) eat eik(x = n+1 i n i x) eat eik(x+ = 0 eat eikx n i+1 n i+1 n i 1 = n i nx /2 c t ( 2 x eat eikx + n i = = n i n+1 i n i n ik x ie n ik x ie Sin+1 + =0 since exact solution = Sin + Wednesday, 29 August 2012 n+1 i n i+1 n i 1) ⇥2 c t 1+ sin(k x) x Wednesday, 29 August 2012 • Alternatively use upwind difference Higher order schemes need less diffusion to be stable. • Can be stabilized by adding finite diffusion. approximations are unconditionally unstable for pure advection (See Baldauf JCP 227 6638). • In fact most “centered” finite difference Stability of advection schemes Wednesday, 29 August 2012 exist values of Δt and Δx that make the scheme stable • The error will grow without bounds • This method is unconditionally unstable • Some methods are conditionally stable. There than one • The amplification factor |G| is always larger |G| = GG = ⇤ Wednesday, 29 August 2012 |G| = 1 2 c t ik (e 2 x ⇤ G=1+ x2 t x (1 e c t n (ui+1 2 x u ui+1 = t2 x un+1 = uni + i Wednesday, 29 August 2012 x) = u(x) u(x 2 x) = u(x) + u(x + ⇥2u =0 ⇥x2 ) t t ⇥2 x 2+e ik x ) 2uni + uni 1 ) ⇥2 c t + sin(k x) x (eik (uni+1 + O( x3 ) x2 1 x2 cos(k x)) ik x uni 1 ) 2ui + ui x2 x2 2 u u 3 + x +O( x ) x x 2 x2 x u x2 2 u 3 x + +O( x ) x x 2 x2 x ⇥u ⇥u +c ⇥t ⇥x Wednesday, 29 August 2012 ⇧ ⇧ ⇧ ⇧ ⇧ ⇤ Wednesday, 29 August 2012 1 a 0 0 .. . a 1 a 0 .. . 0 a 1 a .. . |G| = un+1 i = Rc 1 2 2 0 0 a 1 .. . 1+ x ... ... ... ... .. . c ⌃⇧ ⌃⇧ ⌃⇧ ⌃⇧ ⌃⇧ ⌅⇤ ⇥ 1 ⇥ t 2 un+1 0 un+1 1 un+1 2 un+1 3 .. . ⌃ ⇧ ⌃ ⇧ ⌃= ⇧ ⌃ ⇧ ⌃ ⇧ ⌅ ⇤ ⇥ sin2 (kx) un0 un1 un2 un3 .. . ⌃ ⌃ ⌃ ⌃ ⌃ ⌅ ⇥ n+1 n+1 ⇥ u u i 1 = uni + c t i+1 2 x c x x2 t Wednesday, 29 August 2012 Wednesday, 29 August 2012 u x2 2 u x x 4th x ⇥ ⇥ 4th FTCS ui+2 + 16ui+1 1 1 2 2 BTCS ui + ui 4th order 30ui + 16ui 12 x2 ui+2 + 8ui+1 8ui 12 x Higher order in space Reflecting boundary Wednesday, 29 August 2012 • Velocity reverses sign boundary • Needs to be physically correct • Density is unchanged at reflecting for advection equation. • Can’t do for every problem. Not possible Wednesday, 29 August 2012 Wednesday, 29 August 2012 boundary conditions • Have to specify more ghost cells • Essentially specify higher order derivative stencils • Higher order spatial derivatives have wider Boundary conditions at high order Wednesday, 29 August 2012 numerical gradient at the boundary to zero • Decent first approximation to set the • Set all incoming characteristics to zero outgoing • Set all outgoing characteristics to still be characteristics. • To do properly need to consider Absorbing Boundary Wednesday, 29 August 2012 Wednesday, 29 August 2012 • • • Nonlinear equations Quick and efficient. Copy data from adjacent processor Same principle as boundary conditions Processor 1 Processor 0 Wednesday, 29 August 2012 • • • Wednesday, 29 August 2012 • • • Differential calculus breaks! • • Upwinding Viscosity Odd thing is that we can continue. • The problem is that the solution steepens into a discontinuity. Not the timestep. What went wrong now? Timestep can now change while the code runs Timestep is created by analogy with the linear advection equation Non-linear problem so can’t do von Neumann analysis x t= MAXVAL(u) u u +u =0 t x Wednesday, 29 August 2012 0.6 0.8 1 u 1 u2 + =0 t 2 x u ui ⇥ x x ui x 1 x2 2 u u + x +O( x3 ) 2 x x 2 x x u x2 2 u x + +O( x3 ) 2 x x 2 x x 0.4 u ui+1 ui ⇥ x x x x) = u(x) u(x 0.2 x) = u(x) + 0 u(x + Wednesday, 29 August 2012 -0.2 0 0.2 0.4 0.6 0.8 Wednesday, 29 August 2012 -0.2 0 0.2 0.4 0.6 0.8 Wednesday, 29 August 2012 0.0 0.0 0.2 0.4 0.6 0.8 0 0.2 0.2 0.4 0.4 0.6 0.6 0.8 0.8 1 1.0 Wednesday, 29 August 2012 Wednesday, 29 August 2012 ui x 1 Upwind for right traveling objects u ui ⇥ x x Upwind for left traveling objects u ui+1 ui ⇥ x x x • Not just diffusion • Godunov’s order barrier theory • Only schemes of first order are TVD shock works! • Take derivative onesided backwards across Upwinding ⇥ u dx = |ui+1 x i ui | Why not 1st order? Wednesday, 29 August 2012 • Leading order error term is second order • Large diffusive error • Works well enough for shocks • Very poor for smooth solution regions Wednesday, 29 August 2012 • TVD scheme is monotonicity preserving • No spurious oscillations timestep • TVD is simply that TV decreases every TV = ⇤ Total Variation Diminishing (TVD) Wednesday, 29 August 2012 T p= m ⇧⇤ + ⇤.(⇤V) ⇧t ⇧V ⇤ + ⇤(V.⇤)V ⇧t ⇧B ⇧t ⇤ d p⇥ 1 dt ⇤ Wednesday, 29 August 2012 ⇤ ⇥ (V ⇥ B) 0 = = .B = 0 B⇥⇤⇥B ⇤p = 1 0 = µ Hydrodynamics & MHD Wednesday, 29 August 2012 Wednesday, 29 August 2012 1 A < A t x cf Timestep • Flux constrained transport • Divergence cleaners solenoidal condition • Apply techniques above • Have to find some mechanism to ensure MHD Wednesday, 29 August 2012 Wednesday, 29 August 2012 Nonlinear Hydrodynamics & MHD • Have to use viscosity to stabilize shocks • Poor choice for shock dominated problems order • Job done • Good for linear wave problems at high Finite Difference Code Wednesday, 29 August 2012 Wednesday, 29 August 2012 .f = 0 ⇥u ⇥u2 /2 + =0 ⇥t ⇥x ⇥u + ⇥t Conservative form viscosity. • This corresponds to a real, physical, shock shock • Real shocks have entropy jump across scheme to resolve shock is NOT shock viscosity • Viscosity needed to allow finite difference Brief aside 1 2 1 2 fi+ 12 ) Choices fi+ 12 ui+1 t(fi Wednesday, 29 August 2012 • Godunov schemes - Riemann solvers order reconstruction • Flux limiters -Weighted average of upwind and high equivalent to upwinding in finite difference • Say flux at edge is the same as flux in the centre. • If always select flux in upwind direction then Wednesday, 29 August 2012 fi ui n un+1 = u i + i Conservative form Wednesday, 29 August 2012 Wednesday, 29 August 2012 Contact discontinuity Rarefaction fan Pressure Shock Wednesday, 29 August 2012 Wednesday, 29 August 2012 • Some Riemann-solver less versions not assumed to be simple piecewise linear blocks • Bram van Leer 1979 • Godunov type scheme where states are Law scheme • Monotone Upwind for Scalar Conservation MUSCL Scheme Lagrangian Remap Mesh can tangle for rotational flows Mesh is compressed at shocks Mesh follows fluid flow Wednesday, 29 August 2012 code • Good example is Warwick’s own LARE is shock capturing • Take Lagrangian step • Remap back to original grid • So long as remap using flux limiters scheme Wednesday, 29 August 2012 • • • Lagrangian Codes AMR Wednesday, 29 August 2012 • Only works well for certain problems computers • Allows problems to be run on smaller “interesting” regions • Adaptive mesh refinement • Locally adjusts the grid spacing in Wednesday, 29 August 2012 • Specialised and hard to program performance • Good shock performance, good linear wave remap every step and don’t have to remap back to original grid • Arbitrary Lagrangian Eulerian • Like Lagrangian Remap, but don’t have to ALE Codes Wednesday, 29 August 2012 • Structure design • Parallelism • Output and visualisation • User interaction software is not science related • Most of the work in developing scientific Words on developing codes