IN229/Simulation: Lecture 4 Knut–Andreas Lie Department of Informatics University of Oslo February 2003 We must specify boundary conditions on initial conditions on and or at We call the equation a partial differential equation (PDE) This is a time- and space-dependent problem Mathematical model: the wave equation Physical phenomenon: small vibrations on a string The wave equation - our £rst PDE and IN229/Simulation: Lecture 4 1 Consider a function of several variables The th partial derivative is de£ned as Higher order derivatives are de£ned analogously. and For the wave equation we have . But £rst a digression: partial derivatives IN229/Simulation: Lecture 4 2 IN229/Simulation: Lecture 4 Derivation of the model I y x Physical assumptions: the string = a line in 2D space no gravity forces up-down movement (i.e., only in -direction) Physical quantities: : position : tension force (along the string) : angle with horizontal direction : density 3 IN229/Simulation: Lecture 4 Derivation of the model II y ρ ∆s T(x+h/2) u(x,t) T(x-h/2) x h Physical principle, Newton’s second law: total mass acceleration sum of forces Total mass of line segment: Acceleration: The tension is a vector (with two components): 4 and in the limit we get: use geometrical considerations eliminate -component of equation Then we do some mathematical manipulations A vector equation with two components Newton’s law on a string element: Derivation of the model III IN229/Simulation: Lecture 4 5 ) this simpli£es to: String initially at rest: String £xed at the ends: Initial and boundary conditions: For small vibrations ( Final equation IN229/Simulation: Lecture 4 6 After a scaling, the equation becomes The complete equation For those interested, Glenn Terje will give a more thorough derivation of the equation on ..day IN229/Simulation: Lecture 4 7 Introduce a grid in space-time Finite difference approximation Central difference approximations IN229/Simulation: Lecture 4 8 is the CFL number Here . Then the difference equation reads Solve for Inserted into the equation: Finite difference approximation... IN229/Simulation: Lecture 4 9 Two conditions at for all : Initial conditions The second condition inserted into the equation for Two choices: either introduce a special formula for £ctitious value We use the second approach in the following. , or a IN229/Simulation: Lecture 4 10 IN229/Simulation: Lecture 4 Algorithm De£ne storage Set , for , , !#"$!% Set initial conditions De£ne , ( ,6 78/3121213/54 , +* &(')% ,- .0/2131213/54 ) . . :9 <;='> :9 70 ? @ While BA ? * / 7 Set 9 stop ! Update all inner points ( ,6 78/3121312/54 . ) 70 9 !;=') 70 :9 * Set boundary conditions @8/ C @ Initialize for next step / / ,- .0/2131213/54 11 up, MyArray<double>& u, um, double tstop, double C); u0, MyArray<double>& um, double C); u, double t); } // u at time level l+1 // u at time level l // u at time level l-1 setIC(u, um, C); timeLoop (up, u, um, tstop, C); return 0; cout << "Give Courant number: "; double C; cin >> C; cout << "Compute u(x,t) for t <= tstop, where tstop = "; double tstop; cin >> tstop; MyArray<double> up (n); MyArray<double> u (n); MyArray<double> um (n); int main (int argc, const char* argv[]) { cout << "Give number of intervals in (0,1): "; int i; cin >> i; int n = i+1; // forward declarations: void timeLoop (MyArray<double>& MyArray<double>& void setIC (MyArray<double>& void plotSolution (MyArray<double>& #include .... Straightforward F77/C style implementation IN229/Simulation: Lecture 4 12 } } // initial displacement to file plotSolution (up, t); up(1) = 0; up(n) = 0; um = u; u = up; // plot displacement to file // update boundary points: // update data struct. for next step // update inner points according to finite difference scheme: for (i = 2; i <= n-1; i++) up(i) = 2*u(i) - um(i) + Csq * (u(i+1) - 2*u(i) + u(i-1)); plotSolution (u, t); while (t <= tstop) { t += dt; step_no++; void timeLoop (MyArray<double>& up, MyArray<double>& u, MyArray<double>& um, double tstop, double C) { int n = u.size(); // length of the vector u (no of grid points) double h = 1.0/(n-1); // length of grid intervals double dt = C*h; // time step, assumes unit wave velocity!! double t = 0; // time double Csq = C*C; // = (dt/dx)^2 int i; // loop counter over grid points int step_no = 0; // current step number The timeLoop function IN229/Simulation: Lecture 4 13 } // set the help variable um: for (i = 2; i <= n-1; i++) um(i) = u0(i) + 0.5*Csq * (u0(i+1) - 2*u0(i) + u0(i-1)); um(1) = 0; um(n) = 0; // dummy values, not used in the scheme // set the initial displacement u(x,0) for (i = 1; i <= n; i++) { x = (i-1)*h; if (x < 0.7) u0(i) = (umax/0.7) * x; else u0(i) = (umax/0.3) * (1 - x); } void setIC (MyArray<double>& u0, MyArray<double>& um, double C) { int n = u0.size(); // length of the vector u double x; // coordinate of a grid point double h = 1.0/(n-1); // length of grid intervals double umax = 0.05; // max string displacement int i; // loop counter over grid points double Csq=C*C; The setIC function IN229/Simulation: Lecture 4 14 } Here we have chosen to plot each time step in a separate (hidden) £le with name .u.dat.<step number> i++; sprintf(fn,".u.dat.%03d",i); ofstream outfile(fn); for (int i = 1; i <= n; i++) outfile << h*(i-1) << " " << u(i) << endl; void plotSolution (MyArray<double>& u, double t) { int n = u.size(); // the number of unknowns double h = 1.0/(n-1); // length of grid intervals char fn[30]; static int i=-1; The plotSolution function IN229/Simulation: Lecture 4 15 visualisation = animated motion of string! eleonoradus:~/in229/2000/prog/Wave1D$ ls .u.dat.* .u.dat.000 .u.dat.012 .u.dat.024 .u.dat.036 ..... .u.dat.001 : : : : : : : : : The simulation produces a set of 68 £les eleonoradus:~/in229/2000/prog/Wave1D$ ./wave Give number of intervals in (0,1): 20 Give Courant number: 0.9 Compute u(x,t) for t <= tstop, where tstop = 3 Running the simulator IN229/Simulation: Lecture 4 16 t=3.0; nr=68; incr=1; j=0; for i=0:incr:nr-1 % % read simulation file number <i> fn=sprintf(’.u.dat.%03d’,i); fp = fopen(fn,’r’); [d,n]=fscanf(fp,’%f’,[2,inf]); fclose(fp); % % plot the result plot(d(1,:),d(2,:),’-o’); axis([0 1 -0.1 0.1]); tittel = sprintf(’Time t=%.3f’, (t*i)/nr); title(tittel); % % force drawing explicitly and wait 0.2 seconds drawnow; pause(0.2); end Animation: in Matlab IN229/Simulation: Lecture 4 17 Run gnuplot afterwards: gnuplot gnuplt.com } ofstream outfile(fn); : i++; sprintf(fn,".u.dat.%03d",i); comfile << "plot ’" << fn << "’ title ’u(t=" << t << ")’;"; if (i==0) comfile << "pause 6;" << endl; else comfile << "pause 0;" << endl; if (i<0) { comfile << "set data style linespoints;" << endl; comfile << "set yrange [-0.1:0.1];" << endl; } void plotSolution (MyArray<double>& u, double t) { : static int i=-1; static ofstream comfile("gnuplt.com"); Alternative: generate a Gnuplot command £le IN229/Simulation: Lecture 4 18 IN229/Simulation: Lecture 4 What about the parameter ? How do we choose the parameter Solution at time DFEHG0IKJ for LME ? NPO G 0.08 0.015 0.06 0.01 0.04 0.005 0.02 0 0 −0.02 −0.005 −0.04 −0.01 −0.06 −0.015 −0.08 −0.02 0 0.1 0.2 0.3 0.4 QSR 0.5 0.6 0.7 0.8 0.9 1 −0.1 0 0.01 0.01 0.005 0.005 0 0 −0.005 −0.005 −0.01 −0.01 0.1 0.2 0.3 0.4 QSR 0.3 0.5 UYTVZ 0.4 QWR 0.015 0 0.2 +TVU 0.015 −0.015 0.1 0.6 0.7 0.8 0.9 1 −0.015 0 0.1 0.2 0.3 0.4 QSR 0.5 0.6 0.7 0.8 0.9 1 0.6 0.7 0.8 0.9 1 >TKUYX 0.5 UYTV[ 19 IN229/Simulation: Lecture 4 Numerical stability and accuracy We have two parameters, related through and How do we choose and ? Too large values of and give , that are - too large numerical errors - or in the worst case: unstable solutions Too small and computing power means too much Simpli£ed problems can be analysed theoretically Guide to choosing and 20 IN229/Simulation: Lecture 4 Example of stability analysis The discrete solution can be written on the form \] Inserted in the difference equation ^`_ba ^`_a D Aim: solve for ^dceagf ^dceagf D as a function of In the real (continuous) problem: no damping, which means is real Want the same qualitative behavior in the numerical scheme. This means is real, which implies , i.e., This is the stability criterion, often called the CFL-condition. 21 IN229/Simulation: Lecture 4 Numerical example Time t=0.0, σ=103 Time t=0.0, σ=1 1 1 0.8 0.8 0.6 0.6 0.4 0.4 0.2 0.2 0 −10 −5 0 5 10 0 −10 Time t=15.0, CFL=1.0 1 0.8 0.8 0.6 0.6 0.4 0.4 0.2 0.2 −5 0 5 10 0 −10 Time t=15.0, CFL=0.8 1 0.8 0.8 0.6 0.6 0.4 0.4 0.2 0.2 −5 0 5 5 10 −5 0 5 10 Time t=15.0, CFL=0.8 1 0 −10 0 Time t=15.0, CFL=0.99 1 0 −10 −5 10 0 −10 −5 0 5 10 22 IN229/Simulation: Lecture 4 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 We will return to this topic later in the next lecture.. 23 m idjlk h is the still-water depth (typically obtained from an electronic map). For now: equation in 1-D n Here The simplest (but also widely used) model for propagation of water waves is the wave equation The mathematical model IN229/Simulation: Lecture 4 24 The term phenomena o o o o Two-step discretization, £rst outer operator is common for many models of physical Discretization of variable coef£cients r q p p r q p And the overall discretization reads Then inner operator IN229/Simulation: Lecture 4 r p s s s 25 Geometric mean: Harmonic mean: Arithmetic mean: Often the function is only given in the grid points, e.g., from measurements. Thus we need to de£ne the value at the midpoint Discretization of variable coef£cients... IN229/Simulation: Lecture 4 s 26 IN229/Simulation: Lecture 4 Algorithm: arithmetic mean De£ne storage Set Set t , , for , , !#"$!% t , +* ')% ,- .0/2131213/54 Set initial conditions De£ne ( ,6 78/3121213/54 . , +* &(')% ,- .0/2131213/54 ) . :9 <; u ')t 9 +* t F9 ')t ') t * +* ') Set ? @ While BA ! Update all inner points ( / stop 9 ? * ,6 78/3121312/54 . ) . 70 9 <; u ')t 9 :9 ')t v* t t ') * ') +* * Set boundary conditions @8/ C @ Initialize for next step / / ,- .0/2131213/54 27 We are going to study: “Oblig”: Tsunami due to a slide IN229/Simulation: Lecture 4 28