Presentation is loading. Please wait.

Presentation is loading. Please wait.

Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1.

Similar presentations


Presentation on theme: "Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1."— Presentation transcript:

1 Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1

2 Overview Program the 1D linear wave equation using the upstream approximation in a periodic domain Task illustrates the basics of – Model initialization – Time stepping – Application of boundary conditions – Amplitude and phase errors – Dependence on wavelength and time step – Avoidance of roundoff errors Use programming language (e.g., Fortran, C/C++, Python) and visualization package (e.g., NCL, GrADS, GNUplot, Excel * ) of your choice *Excel won’t be useful for model tasks ≥ 1 2

3 Problem 1D linear wave equation – When c > 0, wave translates to the right Upstream approximation is forward in time and upwind in space 3

4 Explicit form This approximation can be written explicitly as The initial time = 0 seconds The time index n starts at 0 The space index runs from i = 1 to NX The exact solution preserves its original shape With periodic boundary conditions, the exact solution will return to its original position after a certain number of time steps 4

5 Program structure Declare needed arrays – I will call present value “u”, forecast value “up” Initialize parameters and constants (NX, n, dt, dx, trigonometric , etc.) Provide initial condition for u at n = 0 for all i Step ahead by time step dt, using upstream scheme to create up – i.e., time = time + dt … not the best choice (see next slide) Apply boundary conditions (BCs) on up Set for new time step (i.e., u = up) If time < timend, loop 5

6 Tips Trigonometric  to machine precision can be obtained with 4*arctan(1.0) – To avoid confusion with nondimensional pressure (also called “pi”), I call this “trigpi” To avoid roundoff error, advance time as time = (n-1)*dt instead of time = time + dt (see Appendix) 6

7 Periodic boundary conditions For NX points, NX-2 will be real points, and the other two will facilitate application of the boundary conditions (BCs) For periodic BCs u(1) = u(nx-1) and u(nx) = u(2) – So, we loop from i = 2, nx-1 (the real points) and then update the fake points 7

8 Test problem Let c’ = c∆t/∆x (by definition) Test: c = 1.0, dt = 1.0, dx = 1.0, so c’ = 1.0 NX = 52 (50 real points) NT = 50 (timend = 50 since dt = 1.0) Wavelength L = 50∆x (one sine wave in domain), with amplitude 1.0 Execute for one revolution Plot on next slide shows initial condition (= exact solution) and upstream approximation after one revolution For c’ = 1.0, there should be no significant error (so this is your model test) We will manipulate c’ by changing ∆t 8

9 After 1 revolution (No significant error) 9

10 Initial condition for u wavelength = 50 ! i.e., 50∆x amp = 1.0 ! amplitude do i=2,nx-1 ! Loop over real points xi=float(i-2) u(i) = amp*sin(2*xi*trigpi/wavelength) print *,' i ',i,' u ',u(i) enddo C enforce periodic boundary conditions u(nx)=u(2) u(1)=u(nx-1) 10

11 Exact solution for u at time step n do i=2,nx-1 c exact solution at any time n xi = float(i-2)-c*n*dt uexact(i) = amp*sin(2*xi*trigpi/wavelength) enddo 11

12 Errors Amplitude error occurs if the original wave amplitude is not preserved Phase error occurs if the simulated wave translates too quickly or slowly Plots on next slide show amplitude error (left) and phase error (right) as a function of wavelength (horizontal axis, longer waves at left end) Plots on next slide show: – Amplitude decays if c’ 1, and shorter waves handled worse than longer waves – Waves propagate too slowly for all wavelengths; errors reasonable for long waves but large for short waves 12

13 Amplitude and phase errors (Fig. 4.3 from notes) 0.9 0.6 0.96 0.9 1.0 > 1.0 Values < 1: damping Values < 1: too slow 13

14 Amplitude error For c’ = 1, upstream scheme has essentially zero amplitude error For c’ ≠ 1, performance worst for shortest waves – c’ > 1 unstable – solution grows with time – Sampling the wave propagation better (i.e., c’ < 1) makes the solution worse (counterintuitive!) – For c’ < 1, performance worst at c’ = 0.5 for all waves (even more counterintuitive!) – At wavelength of 4∆x and c’ = 0.5, amplitude loss is 30% (0.7). That is per time step. 14

15 Phase error For the exact solution, phase speed is independent of wavelength. All waves move at the same speed, c. The exact solution is nondispersive. Upstream scheme phase propagation is not absolutely perfect even for c’ = 1 – For wavelengths > 4∆x, propagation too slow by about 4% (0.96) – For c’ < 1, some wavelengths move too quickly, others too slowly Again, the shorter the wavelength, the poorer the performance – 2∆x waves are stationary. (Really?) Since phase error depends on wavelength, the numerical scheme is dispersive. 15

16 Experiments 16

17 Experiment #1 Try values of ∆t < 1.0 (so c’ < 1) and compare to exact solution for the 50∆x wave Observe how amplitude and phase error vary with c’ after 1 revolution Compare your results to Fig. 4.3 (left) 17

18 Experiment #2 Try values of c’ > 1 (by increasing ∆t). What happens? How does amplitude error after 1 revolution change with c’? This is linear instability. 18

19 Experiment #3 Change the wavelength of the initial condition to, say, 5∆x ( wavelength = 5 ) and c’ = 0.5 (∆t = 0.5). From Fig. 4.3 from the notes, expect to lose 20% amplitude per time step. Plot maximum wave value vs. time. Do you? Warning: because of the periodic BCs, your initial condition has to have an integer number of waves in the domain, or the BC will mishandle the wave. To explore some wavelengths, it may be necessary to change NX. 19

20 Experiment #4 Compare solutions for the 50∆x wave with c’ = 0.5 and 0.25. Run each simulation for 50 time units. According to Fig. 4.3 (left), the amplitude error is supposed to be worse for c’ = 0.5. Is it? If not, why not? 20

21 Experiment #5 According to Fig. 4.3 (right), the 2∆x wave isn’t supposed to move with time for any value of c’. Does it move? If it does, why does it? 21

22 Experiment #6 An initial condition can consist of several waves, which combine to make a certain shape. The exact solution, being nondispersive, preserves that shape as the wave moves. The upstream scheme, however, is dispersive, as it has a different amplitude and phase error for different waves, so the combined shape will change. Demonstrate this with an initial condition with two or more waves, of possibly different amplitude. (Keep in mind you need to have an integral number of waves in the initial condition.) For example, combine a 50 and 10∆x wave, each with initial amplitude of 1.0, and set c’=0.5. Anticipate what the result would look like after 1 revolution before running the model and checking your guess. 22

23 Appendix: roundoff error 23

24 Test #1 For Fortran, a real-valued (single precision) constant is 4 bytes long. A double precision value is 8 bytes. Write a program that assigns 10 entries of an single precision array to values of 0.1 to 0.9. Print these values to at least 10 decimal places. What do you observe? Now make the array elements double precision. What happens? 24

25 Test #2 Let dt, time1 and time2 be single precision, real variables Set dt to a value like 0.6, and time1 and time2 to 0. Compare these after 20000 iterations ( iter ) time1 = time1 + dt time2 = float(iter)*dt (For a model with a 4-5 sec time step, 20000 iterations represents about 1 day of integration.) 25

26 Question for thought Most NWP models are single precision. Why? 26


Download ppt "Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1."

Similar presentations


Ads by Google