The Wave Equation in 1D and 2D



Similar documents
INF5620: Numerical Methods for Partial Differential Equations p. 1. About the course. Basic features of the course. Course data.

Part II: Finite Difference/Volume Discretisation for CFD

Second Order Linear Partial Differential Equations. Part I

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

Solutions to Homework 10

Chapter 9 Partial Differential Equations

The Heat Equation. Lectures INF2320 p. 1/88

Multi-Block Gridding Technique for FLOW-3D Flow Science, Inc. July 2004

Numerical Analysis Lecture Notes

Frequency-domain and stochastic model for an articulated wave power device

Copyright 2011 Casa Software Ltd. Centre of Mass

Linköping University Electronic Press

The aerodynamic center

The two dimensional heat equation

Class Meeting # 1: Introduction to PDEs

MODULE VII LARGE BODY WAVE DIFFRACTION

POISSON AND LAPLACE EQUATIONS. Charles R. O Neill. School of Mechanical and Aerospace Engineering. Oklahoma State University. Stillwater, OK 74078

Euler s Method and Functions

Determination of source parameters from seismic spectra

Høgskolen i Narvik Sivilingeniørutdanningen STE6237 ELEMENTMETODER. Oppgaver

APPLIED MATHEMATICS ADVANCED LEVEL

v v ax v a x a v a v = = = Since F = ma, it follows that a = F/m. The mass of the arrow is unchanged, and ( )

Midterm Solutions. mvr = ω f (I wheel + I bullet ) = ω f 2 MR2 + mr 2 ) ω f = v R. 1 + M 2m

Scientific Programming

Let s first see how precession works in quantitative detail. The system is illustrated below: ...

Chapter 3 RANDOM VARIATE GENERATION

ME6130 An introduction to CFD 1-1

Numerical Methods for Differential Equations

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Introduction to the Finite Element Method

Computational Physics

Sample Questions for the AP Physics 1 Exam

2 Session Two - Complex Numbers and Vectors

Numerical methods for American options

State Newton's second law of motion for a particle, defining carefully each term used.

3. KINEMATICS IN TWO DIMENSIONS; VECTORS.

Awell-known lecture demonstration1

19.7. Applications of Differential Equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Metric Spaces. Chapter Metrics

Displacement (x) Velocity (v) Acceleration (a) x = f(t) differentiate v = dx Acceleration Velocity (v) Displacement x

Computational Geometry Lab: FEM BASIS FUNCTIONS FOR A TETRAHEDRON

Swedge. Verification Manual. Probabilistic analysis of the geometry and stability of surface wedges Rocscience Inc.

Math 115 Spring 2011 Written Homework 5 Solutions

Mechanics 1: Conservation of Energy and Momentum

SIXTY STUDY QUESTIONS TO THE COURSE NUMERISK BEHANDLING AV DIFFERENTIALEKVATIONER I

Microeconomic Theory: Basic Math Concepts

Section 1.4. Difference Equations

Finite Difference Approach to Option Pricing

27.3. Introduction. Prerequisites. Learning Outcomes

Math 1B, lecture 5: area and volume

Graphs of Polar Equations

Waves disturbances caused by the movement of energy from a source through some medium.

Lesson 11. Luis Anchordoqui. Physics 168. Tuesday, December 8, 15

Simple Harmonic Motion

Understanding Poles and Zeros

Stress Recovery 28 1

AP1 Oscillations. 1. Which of the following statements about a spring-block oscillator in simple harmonic motion about its equilibrium point is false?

Charlesworth School Year Group Maths Targets

The Method of Least Squares. Lectures INF2320 p. 1/80

Finite Element Analysis

Unified Lecture # 4 Vectors

2.016 Hydrodynamics Reading # Hydrodynamics Prof. A.H. Techet

Core Maths C1. Revision Notes

= δx x + δy y. df ds = dx. ds y + xdy ds. Now multiply by ds to get the form of the equation in terms of differentials: df = y dx + x dy.

CONCEPT-II. Overview of demo examples

Lecture 16 - Free Surface Flows. Applied Computational Fluid Dynamics

39th International Physics Olympiad - Hanoi - Vietnam Theoretical Problem No. 3

Georgia Department of Education Kathy Cox, State Superintendent of Schools 7/19/2005 All Rights Reserved 1

Maximum Likelihood Estimation

Separable First Order Differential Equations

Lecture L22-2D Rigid Body Dynamics: Work and Energy

Solution: F = kx is Hooke s law for a mass and spring system. Angular frequency of this system is: k m therefore, k

Biggar High School Mathematics Department. National 5 Learning Intentions & Success Criteria: Assessing My Progress

Numerical PDE methods for exotic options

Copyright 2011 Casa Software Ltd.

Tsunami Practice Questions and Answers Revised November 2008

ANALYTICAL METHODS FOR ENGINEERS

1. First-order Ordinary Differential Equations

ebb current, the velocity alternately increasing and decreasing without coming to

An Introduction to Partial Differential Equations

FRICTION, WORK, AND THE INCLINED PLANE

Differential Relations for Fluid Flow. Acceleration field of a fluid. The differential equation of mass conservation

1 Error in Euler s Method

Second Order Linear Differential Equations

The HLLC Riemann Solver

Numerical Methods for Option Pricing

Inner Product Spaces

Glencoe. correlated to SOUTH CAROLINA MATH CURRICULUM STANDARDS GRADE 6 3-3, , , 4-9

Parabolic Equations. Chapter 5. Contents Well-Posed Initial-Boundary Value Problem Time Irreversibility of the Heat Equation

CALCULATIONS & STATISTICS

Hypothesis Testing for Beginners

FINITE DIFFERENCE METHODS

5 Numerical Differentiation

Application of Fourier Transform to PDE (I) Fourier Sine Transform (application to PDEs defined on a semi-infinite domain)

On computer algebra-aided stability analysis of dierence schemes generated by means of Gr obner bases

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

1 Finite difference example: 1D implicit heat equation

Applications of Second-Order Differential Equations

DYNAMIC ANALYSIS OF THICK PLATES SUBJECTED TO EARTQUAKE

CALCULATING THE RESPONSE OF AN OSCILLATOR TO ARBITRARY GROUND MOTION* By G~ORGE W. I-IouSl~ER

Transcription:

The Wave Equation in 1D and 2D Knut Andreas Lie Dept. of Informatics, University of Oslo INF234 / Spring 25 p.

Wave Equation in 1D Physical phenomenon: small vibrations on a string Mathematical model: the wave equation 2 u t 2 = γ2 2 u, x (a, b) x2 This is a time- and space-dependent problem We call the equation a partial differential equation (PDE) We must specify boundary conditions on u or u x at x = a, b and initial conditions on u(x, ) and u t (x, ) INF234 / Spring 25 p.

Derivation of the Model y Physical assumptions: the string = a line in 2D space no gravity forces up-down movement (i.e., only in y-direction) Physical quantities: r = xi + u(x, t)j : position T(x) : tension force (along the string) θ(x) : angle with horizontal direction ϱ(x) : density x INF234 / Spring 25 p.

Derivation of the Model, cont d y ρ s T(x+h/2) u(x,t) T(x-h/2) x Physical principle, Newton s second law: h total mass acceleration = sum of forces INF234 / Spring 25 p.

Derivation of the Model, cont d y ρ s T(x+h/2) u(x,t) T(x-h/2) x h Total mass of line segment: ϱ(x) s Acceleration: a = 2 r t 2 = 2 u t 2 j The tension is a vector (with two components): T(x) = T (x) cos θ(x) i + T (x) sin θ(x) j INF234 / Spring 25 p.

Derivation of the Model, cont d Newton s law on a string element: ϱ(x) s 2 u (x t (x, t) j = T + h ) 2 2 A vector equation with two components ( T x h ) 2 Now we do some mathematical manipulations eliminate x-component of equation use geometrical considerations and in the limit h we get: ϱ [ 1 + ( ) ] 1 2 2 u x 2 u t 2 = x T [ 1 + ( ) ] 2 1 2 u x u x INF234 / Spring 25 p.

The Linearised Equation For small vibrations ( u/ x ) this simplifies to: 2 u t 2 = c2 2 u x 2 c 2 = T/ϱ Initial and boundary conditions: String fixed at the ends: u(a, t) = u(b, t) = String initially at rest: u(x, ) = I(x), u t (x, ) = INF234 / Spring 25 p.

The Complete Linear Model After a scaling, the equation becomes 2 u t 2 = γ2 2 u, x2 x (, 1), t > u(x, ) = I(x), x (, 1) u t (x, ) =, x (, 1) u(, t) =, t >, u(1, t) =, t > Exercise: try to go through the derivation yourself INF234 / Spring 25 p.

Finite Difference Approximation Introduce a grid in space-time x i = (i 1) x, i = 1,..., n t l = l t, l =, 1,... Central difference approximations 2 u x 2 (x i, t l ) ul i 1 2ul i + ul i+1 x 2, 2 u t 2 (x i, t l ) ul 1 i 2u l i + ul+1 i t 2 INF234 / Spring 25 p.

Finite Difference Approximation, cont d Inserted into the equation: u l 1 i 2u l i + ul+1 i t 2 = γ 2 ul i 1 2ul i + ul i+1 x 2 Solve for u l+1 i. Then the difference equation reads u l+1 i ( ) = 2u l i ui l 1 + C 2 u l i 1 2u l i + u l i+1 Here C = γ t x is the CFL number INF234 / Spring 25 p. 1

Initial Conditions Two conditions at l = for all i: u(x, ) = I(x) u i = I(x i) u t (x, ) = u1 i u 1 i t =, u 1 i = u 1 i The second condition inserted into the equation for l = u 1 i = 2u i u 1 i + C 2 ( u i 1 2u i + u ) i+1 u 1 i = u i + 1 ( 2 C2 u i 1 2u i + u ) i+1 Two choices: either introduce a special stencil for l =, or a set of fictitious values u 1 i = u i + 1 `u 2 C2 i 1 2u i + u i+1 We use the second approach in the following. INF234 / Spring 25 p. 1

Algorithm Define storage u + i, u i, u i for u l+1 i, u l i, ul 1 i Set t = and C = γ t/ x Set initial conditions ui = I(x i ), i = 1,..., n Define u i (i = 2,..., n 1) u i = u i + 1 2 C2 (u i+1 2u i + u i 1 ), While t < tstop t = t + t Update all inner points (i = 2,..., n 1) u + i = 2u i u i + C 2 (u i+1 2u i + u i 1 ) Set boundary conditions u + 1 =, u+ n = Initialize for next step u i = u i, u i = u + i, i = 1,..., n INF234 / Spring 25 p. 1

Straightforward F77/C Implementation int main (int argc, const char argv[]) { cout << "Give number of intervals in (,1): "; int i ; cin >> i ; int n = i+1; MyArray<double> up (n); // u at time level l+1 MyArray<double> u (n); // u at time level l MyArray<double> um (n); // u at time level l 1 cout << "Give Courant number: "; double C; cin >> C; cout << "Compute u(x,t) for t <= tstop, where tstop = "; double tstop; cin >> tstop; } setic(u, um, C); timeloop (up, u, um, tstop, C); return ; INF234 / Spring 25 p. 1

The timeloop Function void timeloop (MyArray<double>& up, MyArray<double>& u, MyArray<double>& um, double tstop, double C) { int i, step_no=, n = u.size (); double h = 1./(n 1), dt = C h, t=, Csq = C C; plotsolution ( u, t ); // initial displacement to file while (t <= tstop ) { t += dt ; step_no++; for ( i = 2; i <= n 1; i++) // inner points up(i ) = 2 u(i ) um(i) + Csq (u(i+1) 2 u(i) + u(i 1)); up (1) = ; up(n ) = ; // update boundary points: um = u; u = up; // update data struct. for next step } } plotsolution ( up, t ); // plot displacement to file INF234 / Spring 25 p. 1

The setic Function void setic (MyArray<double>& u, MyArray<double>& um, double C) { int i, n = u.size (); double x, h = 1./( n 1); // length of grid intervals double umax =.5, Csq=C C; // set the initial displacement u(x,) for ( i = 1; i <= n; i ++) { x = ( i 1) h; if ( x <.7) u(i ) = ( umax/.7) x; else u(i ) = ( umax/.3) (1 x); } } // set the help variable um: for ( i = 2; i <= n 1; i++) um(i) = u(i ) +.5 Csq (u(i+1) 2 u(i) + u(i 1)); um(1) = ; um(n) = ; // dummy values, not used in the scheme INF234 / Spring 25 p. 1

The plotsolution Function void plotsolution ( MyArray<double>& u, double t) { int n = u.size (); // the number of unknowns double h = 1./(n 1); // length of grid intervals char fn [3]; static int i = 1; } i ++; sprintf (fn, ".u.dat.%3d",i); ofstream outfile (fn ); for ( int i = 1; i <= n; i++) outfile << h (i 1) << " " << u(i ) << endl; Here we have chosen to plot each time step in a separate (hidden) file with name.u.dat.<step number> INF234 / Spring 25 p. 1

Animation in Matlab function myplot(nr,t, incr ) if ( nargin==2) incr=1; end; j=; for i =:incr :nr % % read simulation file number <i> fn=sprintf(.u.dat.%3d,i ); fp = fopen(fn, r ); [ d,n]=fscanf(fp, %f,[2, inf ]); fclose(fp ); % % plot the result plot(d (1,:), d (2,:), o ); axis ([ 1.1.1]); tittel = sprintf( Time t=%.3f, (t i )/ nr ); title ( tittel ); % % force drawing explicitly and wait.2 seconds drawnow; pause(.5); end INF234 / Spring 25 p. 1

What about the Parameter C? How do we choose the parameter C = t/ x?.15.8.1.6.4.5.2.5.2.4.1.6.15.8.2.1.2.3.4.5.6.7.8.9 1.1.1.2.3.4.5.6.7.8.9 1 C = 1. C = 1.5.15.15.1.1.5.5.5.5.1.1.15.1.2.3.4.5.6.7.8.9 1.15.1.2.3.4.5.6.7.8.9 1 C =.8 C =.3 Solution at time t =.5 for h = 1/2 INF234 / Spring 25 p. 1

Numerical Stability and Accuracy We have two parameters, t and x, that are related through C = t/ x How do we choose t and x? Too large values of t and x give - too large numerical errors - or in the worst case: unstable solutions Too small t and x means too much computing power Simplified problems can be analysed theoretically Guide to choosing t and x INF234 / Spring 25 p. 1

Large Destructive Water Waves The wave equations may also be used to simulate large destructive waves Waves in fjords, lakes, or the ocean, generated by - slides - earthquakes - subsea volcanos - meteorittes Human activity, like nuclear detonations, or slides generated by oil drilling, may also generate tsunamis Propagation over large distances Wave amplitude increases near shore Run-up at the coasts may result in severe damage INF234 / Spring 25 p. 2

Tsunamis (in the Pacific) Japanese word for large wave in harbor. Often used as synonym for large destructive waves generated by slides, earthquakes, volcanos, etc. Map of older incidents: Scenario: Earthquake outside Chile, generates tsunami, propagating at 8 km/h accross the Pacific, run-up on densly populated coasts in Japan INF234 / Spring 25 p. 2

Norwegian Tsunamis Tromsø 7 Circles: Major incidents, > 1 killed Bodø Triangles: Selected smaller incidents 65 6 Bergen NORWAY Trondheim Oslo SWEDEN Stockholm Square: Storegga (5 B.C.) More information (e.g.,): math-www.uio.no/avdb/en/research/geophys/ www.forskning.no/temaer/jordskjelv/ www.aftenposten.no/meninger/ kronikker/article94524.ece 1 15 5 INF234 / Spring 25 p. 2

Why Numerical Simulation? Increase the understanding of tsunamis Assist warning systems Assist building of harbor protection (break waters) Recognize critical coastal areas (e.g. move population) Hindcast historical tsunamis (assist geologists) INF234 / Spring 25 p. 2

Simple Mathematical Model The simplest model for tsunami propagation is the wave equation 2 u t 2 = x ( H(x, y, t) u ) x + y ( H(x, y, t) u ) 2 H y t 2 Here H(x, y, t) is the still-water depth (typically obtained from an electronic map). The t-dependence in H allows a moving bottom to model, e.g., an underwater slide or earthquake. A common approximation of the effect of an earthquake (or volcano or faulting) is to set H = H(x, y) and prescribe an initial disturbance of the sea surface. INF234 / Spring 25 p. 2

First: the 1D Case 2 u t 2 = ( H(x) u ) x x ( ) The term x H(x) u x is common for many models of physical phenomena Heat equation ( with ) spatially varying conductivity: u t = x λ(x) u x Heat equation ( with ) temperature-dependent conductivity: u t = x λ(u) u x Pressure( distribution ) in a reservoir: cp t = x K(x) p x... INF234 / Spring 25 p. 2

Discretisation Two-step discretization, first outer operator ( H(x) u x=xi x x) 1 ( (H u h x) x=xi+1/2 ( H x) u ) x=xi 1/2 Then inner operator ( H u x) x=xi+1/2 H i+1/2 u i+1 u i h And the overall discretization reads ( H u ) H i+1/2(u i+1 u i ) H i 1/2 (u i u i 1 ) x x h 2 INF234 / Spring 25 p. 2

Discretisation, cont d Often the function H(x) is only given in the grid points, e.g., from measurements. Thus we need to define the value at the midpoint Arithmetic mean: H i+ 1 2 = 1 2 (H i + H i+1 ) Harmonic mean: 1 H i+ 1 2 = 1 2 ( 1 + 1 ) H i H i+1 Geometric mean: H i+ 1 2 = (H ih i+1 ) 1/2 INF234 / Spring 25 p. 2

Oblig: Tsunami Due to a Slide We are going to study: 2 u t 2 = ( H(x, t) u ) x x + 2 H t 2 Some physics for verification: Surface elevation ahead of the slide, dump behind Initially, negative dump propagates backwards The surface waves propagate faster than the slide moves INF234 / Spring 25 p. 2

Discretisation of 2D Equation Introduce a rectangular grid: x i = (i 1) x, y j = (j 1) y (i,j+1) (i-1,j) (i,j) (i+1,j) (i,j-1) Seek approximation u l i,j on the grid at discrete times t l = l t INF234 / Spring 25 p. 2

Discretisation, cont d Approximate derivatives by central differences 2 u t 2 ul+1 i,j 2u l i,j + ul 1 i,j t 2 Similarly for the x and y derivatives. Assume for the moment that λ 1 and that x = y. Then u l+1 i,j 2u l i,j + ul 1 i,j t 2 = ul i+1,j 2ul i,j + ul i 1,j x 2 + ul i,j+1 2ul i,j + ul i,j 1 y 2 or (with r = t/ x) u l+1 i,j = 2u l i,j u l 1 i,j = 2u l i,j u l 1 i,j + r 2( u l i+1,j + u l i 1,j + u l i,j+1 + u l i,j 1 4u l ) i,j + [ u] l i,j INF234 / Spring 25 p. 3

Graphical Illustration u l i,j 1 u l i 1,j u l+1 i,j u l i,j u l i+1,j u l i,j+1 r 2 1 2 4r 2 r 2 r 2 r 2 u l 1 i,j 1 Computational molecule (stencil) in (x,y,t) space. INF234 / Spring 25 p. 3

The Full Approximation As we have seen earlier, a spatial term like (λu y ) y takes the form 1 y ( Thus we derive λ i,j+ 1 2 ( u l i,j+1 u l i,j y ) λ i,j 1 2 ( u l i,j u l i,j 1 y )) u l+1 i,j =2u l i,j u l 1 i,j ( + rx 2 ( λ 1 i+ u l 2,j i+1,j u l ) ( i,j λi 1 u l 2,j i,j u l ) ) i 1,j ( + ry 2 ( λ 1 u l i,j+ i,j+1 u l ) ( i,j λi,j 1 u l i,j u l i,j 1) ) 2 2 =2u l i,j u l 1 i,j + [ u] l i,j where r x = t/ x and r y = t/ y. INF234 / Spring 25 p. 3

Boundary Conditions For the 1-D wave equation we imposed u = at the boundary. Now, we would like to impose full reflection of waves like in a swimming pool u n u n = Assume a rectangular domain. At the vertical (x =constant) boundaries the condition reads: = u n = u (±1, ) = ± u x Similarly at the horizontal boundaries (y =constant) = u n = u (, ±1) = ± u y INF234 / Spring 25 p. 3

Boundary Conditions, cont d For the heat equation we saw that there are two ways of implementing the boundary conditions: ghost cells or modified stencils 1 Ghost cells 1 Modified stencile 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 2 3 4 5 6 7 8 9 1 1 1 1 2 3 4 5 6 7 8 9 1 Here we will use modified stencil to avoid the need to postprocess the data to remove ghost cells INF234 / Spring 25 p. 3

Solution Algorithm DEFINITIONS: storage, grid, internal points INITIAL CONDITIONS: u i,j = I(x i, y j ), (i, j) Ī VARIABLE COEFFICIENT: set/get values for λ SET ARTIFICIAL QUANTITY u i,j : WAVE(u, u, u,.5,,.5) Set t = While t t stop t t + t (If λ depends on t: update λ) update all points: WAVE(u +, u, u, 1, 1, 1) initialize for next step: u i,j = u i,j, u i,j = u + i,j, (i, j) I INF234 / Spring 25 p. 3

Updating Internal and Boundary Points WAVE(u +, u, u, a, b, c) UPDATE ALL INNER POINTS: u + i,j = 2au i,j bu i,j + c[ u] i,j, (i, j) I UPDATE BOUNDARY POINTS: i = 1, j = 2,..., n y 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:i 1 i+1, i = n x, j = 2,..., n y 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:i+1 i 1, j = 1, i = 2,..., n x 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:j 1 j+1, j = n y, i = 2,..., n x 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:j 1 j+1, INF234 / Spring 25 p. 3

Updating Internal and Boundary Points, cont d UPDATE CORNER POINTS ON THE BOUNDARY: i = 1, j = 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:i 1 i+1,j 1 j+1 i = n x, j = 1; u + i,j = 2au i,j bu i,j + c[ u] i,j:i+1 i 1,j 1 j+1 i = 1, j = n y ; u + i,j = 2au i,j bu i,j + c[ u] i,j:i 1 i+1,j+1 j 1 i = n x, j = n y ; u + i,j = 2au i,j bu i,j + c[ u] i,j:i+1 i 1,j+1 j 1 INF234 / Spring 25 p. 3

Fragments of an Implementation Suppose we have implemented ArrayGen for multidimensional arrays: ArrayGen(real) up (nx,ny ); // u at time level l +1 ArrayGen(real) u (nx,ny ); // u at time level l ArrayGen(real) um (nx,ny); // u at time level l 1 ArrayGen(real) lambda (nx,ny); // variable coefficient : // Set initial data : // Set the artificial um WAVE (um, u, um,.5,,.5, lambda, dt, dx, dy); // Main loop t =; int step_no = ; while (t <= tstop ) { t += dt ; step_no++; WAVE (up, u, um, 1, 1, 1, um = u; u = up; } lambda, dt, dx,dy); INF234 / Spring 25 p. 3

Central Parts of WAVE void WAVE(...) { // update inner points according to finite difference scheme: for ( j =2; j<ny; j++) for ( i =2; i<nx; 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): for ( i =1, j =2; j<ny; 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); for ( i=nx, j =2; j<ny; 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); : : INF234 / Spring 25 p. 3

Trick: We Use Macros! To avoid typos and increase readability we used a macro for the (long) finite difference formula corresponding to [ u] i,j : #define LaplaceU(i,j,im1,ip1,jm1,jp1 ) \ sqr(dt/dx) \ (.5 ( lambda(ip1,j )+lambda(i, j )) ( u(ip1, j ) u(i, j )) \.5 (lambda(i, j )+lambda(im1,j )) ( u( i, j ) u(im1,j )))\ +sqr(dt/dy) \ (.5 ( lambda(i, jp1)+lambda(i, j )) ( u( i,jp1) u(i, j )) \.5 (lambda(i, j )+lambda(i,jm1)) (u( i, j ) u(i,jm1))) The macro is expanded by the C/C++ preprocessor (cpp). Macros are handy to avoid typos and increase readability, but should be used with care... What does the macro do? Consider the simple macro: #define mac(x) q(i,j (X)) When called in the code with mac(i+2), this expands to q(i,j-i+2) INF234 / Spring 25 p. 4

Efficiency Issues Efficiency of plain loops is very important in numerics. Two things should be considered in our case: Loops should be ordered such that u(i, j) is traversed in the order it is stored. In our ArrayGen we assume that objects are stored columnwise. Therefore the loop should read: for ( j =1; j<ny+1; j++) for ( i =1; i<nx+1; i++) u(i, j ) =... One should avoid if statements in loops if possible; hence we will split the loop over all grid points separate loops over inner and boundary points. Remark I: Get the code to work before optimizing it Remark II: Focus on a readable and maintainable code before thinking of efficiency INF234 / Spring 25 p. 4

Visualising the Results Time t=. Time t=.25 Time t=.75.1.1.1.8.8.8.6.6.6.4.4.4.2.2.2.2.2.2.4.4.4 5 5 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 Plots generated in Matlab by the following sequence of commands: >> load W..dat; >> n=sqrt(length(w)); s=reshape(w,n,n); >> mesh(s); caxis ([.1]); >> axis ([1 51 1 51.5.1]); >> title ( Time t=. ); INF234 / Spring 25 p. 4

Various Ways of Visualisation Time t=.694 Time t=.694 5 5 45 45.1 4 4.5 35 3 25 35 3 25.5 2 2.1 15 1 5 15 1 5.15.2 5 1 15 2 25 3 35 4 45 5 contour plot 5 1 15 2 25 3 35 4 45 5 color plot Time t=.694 Time t=.694.1.8.6.4.2.2.4 5 4 3 2 1 1 2 3 4 5 surface mesh lighted surface INF234 / Spring 25 p. 4

Example: Waves Caused by Earthquake Physical assumption: long waves in shallow water. Mathematical model 2 u t 2 = [H(x) u ] Consider a rectangular domain Ω = (s x, s x + w x ) (s y, s y + w y ) with initial (Gaussian bell) function I(x, y) = A u exp ( 1 2 ( x x c u σ ux ) 2 1 2 ( ) ) y y c 2 u σ uy This models an initial elevation caused by an earthquake. INF234 / Spring 25 p. 4

Example, cont d The earthquake takes place near an underwater seamount H(x, y) = 1 A H exp ( 1 2 ( x x c H σ Hx ) 2 1 2 ( y y c H σ Hy ) 2 ) Simulation case inspired by the Gorringe Bank southwest of Portugal. Severe ocean waves have been generated due to earthquakes in this region. http://www.math.uio.no/avdb/gitec/ INF234 / Spring 25 p. 4

Boundary Conditions waves should propagate out of the domain, without being reflected this is difficult to model numerically alternative: u n = which gives full reflection from the boundary What? An unphysical boundary condition??? This is in fact okay for a hyperbolic equation, like the wave equation; waves travel at a finite speed and the u/ n = condition is feasible up to the point in time where waves are reflected from the boundary INF234 / Spring 25 p. 4

Boundary Conditions, cont d Use a circular bell for both I and H and set symmetry line bottom contour surface contour y c H = yc u = xc H = xc u = Thus we have symmetry about the lines x = y = can reduce computational domain by a factor 4! Appropriate boundary condition at symmetry lines: u n = u n = If possible: one should always try to reduce computational domain by symmetry INF234 / Spring 25 p. 4

Computational Results time: t=. time: t=3.771.2.2.2.2.4.4.6.6.8.8 1 5 1 15 2 5 1 15 2 1 5 1 15 2 5 1 15 2 time: t=5.657 time: t=9.9.2.2.2.2.4.4.6.6.8.8 1 5 1 15 2 5 1 15 2 1 5 1 15 2 5 1 15 2 INF234 / Spring 25 p. 4