+ All Categories
Home > Documents > Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1.

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

Date post: 16-Jan-2016
Category:
Upload: kathryn-fox
View: 215 times
Download: 1 times
Share this document with a friend
26
Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1
Transcript
Page 1: Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1.

1

Model Task 0A: Programming the 1D upstream scheme

ATM 562 Fall 2015Fovell

Page 2: 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

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

3

Problem

• 1D linear wave equation

– When c > 0, wave translates to the right• Upstream approximation is forward in time

and upwind in space

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

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

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

5

Program structure

• Declare needed arrays– I will call present value “u”, forecast value “up”

• Initialize parameters and constants (NX, n, dt, dx, trigonometric p, 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

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

6

Tips

• Trigonometric p 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)

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

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 BCsu(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

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

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

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

9

After 1 revolution

(No significant error)

0 5 10 15 20 25 30 35 40 45 50

-1.5

-1.0

-0.5

0.0

0.5

1.0

1.5

Upstream approximation to u_t = -cu_x

exact upstream (cfl=1, 1 rev)

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

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) enddoC enforce periodic boundary conditions u(nx)=u(2) u(1)=u(nx-1)

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

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

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

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, grows 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

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

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

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

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.

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

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.

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

16

Experiments

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

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)

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

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.

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

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.

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

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?

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

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?

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

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.

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

23

Appendix: roundoff error

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

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?

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

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 + dttime2 = float(iter)*dt

(For a model with a 4-5 sec time step, 20000 iterations represents about 1 day of integration.)

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

26

Question for thought

• Most NWP models are single precision. Why?


Recommended