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

Slides:



Advertisements
Similar presentations
Courant and all that Consistency, Convergence Stability Numerical Dispersion Computational grids and numerical anisotropy The goal of this lecture is to.
Advertisements

Finite Difference Discretization of Hyperbolic Equations: Linear Problems Lectures 8, 9 and 10.
If you have not watched the PowerPoint on the unit circle you should watch it first. After you’ve watched that PowerPoint you are ready for this one.
Numerical Methods for Partial Differential Equations CAAM 452 Spring 2005 Lecture 9 Instructor: Tim Warburton.
Roundoff and truncation errors
Algebraic, transcendental (i.e., involving trigonometric and exponential functions), ordinary differential equations, or partial differential equations...
DFT/FFT and Wavelets ● Additive Synthesis demonstration (wave addition) ● Standard Definitions ● Computing the DFT and FFT ● Sine and cosine wave multiplication.
Complex Power – Background Concepts
HST 952 Computing for Biomedical Scientists Lecture 10.
This slide intentionally left blank. The Rayleigh Plateau Instability By Mike Cromer and Patrick C. Rowe.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD.
Complexity Analysis (Part I)
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software.
Chapter 4 Numerical Solutions to the Diffusion Equation.
Finite Difference Methods Or Computational Calculus.
Numerical Methods for Partial Differential Equations CAAM 452 Spring 2005 Lecture 9 Instructor: Tim Warburton.
Chapter 2 Functions and Graphs
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Copyright © Cengage Learning. All rights reserved. 1 Functions and Limits.
Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e1.
1 Web Based Interface for Numerical Simulations of Nonlinear Evolution Equations Ryan N. Foster & Thiab Taha Department of Computer Science The University.
Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Analysis of Algorithms
1 Spring 2003 Prof. Tim Warburton MA557/MA578/CS557 Lecture 5a.
Round-off Errors.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Model Task 0B: Implementing different schemes ATM 562 Fall 2015 Fovell 1.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Enhancing One‐Dimensional FDTD
CIS888.11V/EE894R/ME894V A Case Study in Computational Science & Engineering We will apply several numerical methods to find a steady state solution of.
Model Task 5: Implementing the 2D model ATM 562 Fall 2015 Fovell (see updated course notes, Chapter 13) 1.
Numerical Analysis CC413 Propagation of Errors.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Model Task 4: Time stepping and the leapfrog scheme ATM 562 Fall 2015 Fovell (see updated course notes, Chapter 12) 1.
Advanced Engineering Mathematics, 7 th Edition Peter V. O’Neil © 2012 Cengage Learning Engineering. All Rights Reserved. CHAPTER 4 Series Solutions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Errors in Numerical Methods
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
1 Copyright © Cengage Learning. All rights reserved. 6. The Trigonometric Functions Graphs of Trigonometric Functions.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
The story so far… ATM 562 Fovell Fall, Convergence We will deploy finite difference (FD) approximations to our model partial differential equations.
COSC 1P02 Introduction to Computer Science 5.1 Cosc 1P02 Week 5 Lecture slides Psychiatrist to patient "You have nothing to worry about - anyone who can.
Adjoint models: Theory ATM 569 Fovell Fall 2015 (See course notes, Chapter 15) 1.
S3-1 ADM703, Section 3, August 2005 Copyright  2005 MSC.Software Corporation SECTION 3 SUSPENSION SYSTEM.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Numerical Analysis CC413 Propagation of Errors. 2 In numerical methods, the calculations are not made with exact numbers. How do these inaccuracies propagate.
Windows Programming Lecture 03. Pointers and Arrays.
Chapter 2 Errors in Numerical Methods and Their Impacts.
Numerical Solutions to the Diffusion Equation
Lecture 4: Numerical Stability
ECE 2202 Circuit Analysis II
Mechanical Engineering at Virginia Tech
Points, Lines, and Their Graphs
Variables Title slide variables.
Drawing Trigonometric Graphs.
Overloading functions
AS-Level Maths: Core 2 for Edexcel
Arrays.
Presentation transcript:

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

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

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

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

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

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

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

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

After 1 revolution (No significant error) 9

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

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

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

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

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

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

Experiments 16

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

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

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

Experiment #4 Compare solutions for the 50∆x wave with c’ = 0.5 and 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

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

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

Appendix: roundoff error 23

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

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 iterations ( iter ) time1 = time1 + dt time2 = float(iter)*dt (For a model with a 4-5 sec time step, iterations represents about 1 day of integration.) 25

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