32
MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr Hilary Weller, <[email protected]>(Lecturer version) Time Weeks Lectures: Mon, 2-4pm 1-5,7-10 (5-9, 11-14) Practicals: Tuesday 2-4pm 1-5,7-9 (5-9, 11-13) Coursework Due Worth 1. Differentiation Mon 22 Oct 0% 2. Diffusion Mon 5 Nov 25% 3a Advection Code Mon 26 Nov 8% 3b Advection Report Mon 3 Dec 27% Test: Mon 10 Dec 40% 2-4pm Week Monday lectures Tuesday practicals Hand in on 2-4pm 2-4pm blackboard 1/5 1 Oct. Integration, 2 Oct. Integration, roots and Python roots and Python 2/6 8 Oct. Taylor series 9 Oct. 15 Oct and interpolation Differentiation 0% 3/7 15 Oct. Git and 16 Oct. writing software Software 4/8 22 Oct. Navier Stokes 237 Oct and diffusion Diffusion 5/9 29 Oct. Fourier series 30 Oct. Diffusion and numerical analysis Diffusion 5 Nov (25%) 6/10 Week 6 7/11 12 Nov. Linear advection 13 Nov. and numerical analysis Advection 8/12 19 Nov. Further 20 Nov. Code 26 Nov (8%) advection Advection 9/13 26 Nov 27 Nov Reports 3 Dec (27%) Extra practical 10/14 3 Dec Revision 11/15 Mon 10 Dec, 2-4pm Test. 40% 2

MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

MTMW12: Introduction to Numerical Methods forAtmosphere and Ocean Models

Dr Hilary Weller, <[email protected]>(Lecturer version)

Time WeeksLectures: Mon, 2-4pm 1-5,7-10

(5-9, 11-14)Practicals: Tuesday 2-4pm 1-5,7-9

(5-9, 11-13)Coursework Due Worth1. Differentiation Mon 22 Oct 0%2. Diffusion Mon 5 Nov 25%3a Advection Code Mon 26 Nov 8%3b Advection Report Mon 3 Dec 27%Test: Mon 10 Dec 40%

2-4pm

Week Monday lectures Tuesday practicals Hand in on2-4pm 2-4pm blackboard

1/5 1 Oct. Integration, 2 Oct. Integration,roots and Python roots and Python

2/6 8 Oct. Taylor series 9 Oct. 15 Octand interpolation Differentiation 0%

3/7 15 Oct. Git and 16 Oct.writing software Software

4/8 22 Oct. Navier Stokes 237 Octand diffusion Diffusion

5/9 29 Oct. Fourier series 30 Oct. Diffusionand numerical analysis Diffusion 5 Nov (25%)

6/10 Week 67/11 12 Nov. Linear advection 13 Nov.

and numerical analysis Advection8/12 19 Nov. Further 20 Nov. Code 26 Nov (8%)

advection Advection9/13 26 Nov 27 Nov Reports 3 Dec (27%)

Extra practical10/14 3 Dec

Revision11/15 Mon 10 Dec, 2-4pm Test. 40%

2

Page 2: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Further Resources (Not essential reading)

Python• “A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences” by

Johnny Wei-Bing Lin. http://www.johnny-lin.com/pyintro .• “Think Python. How to Think Like a Computer Scientist” by Allen B. Downey .http://www.greenteapress.com/thinkpython/thinkpython.html

• http://www.python.org

• http://docs.python.org/tutorial/

• http://matplotlib.sourceforge.net/users/index.html

• http://software-carpentry.org/

• http://www.diveintopython.net/

• http://learnpythonthehardway.org/

• http://www.ibiblio.org/g2swap/byteofpython/read/

• “Making Use of Phython” by Rashi Gupta• “Python Essential Reference” by David M. Beazley (Addison Wesley)• “Learning Python” Mark Lutz (O’Reilly Media)

Numerical Methods• Weller, H. Introduction to Numerical Methods for Weather and Climate Models. Draft of

a chapter for a book.http://www.met.reading.ac.uk/~sws02hs/teaching/PDEsNumerics/WellerPrimer.pdf

• Wikipedia http://en.wikipedia.org• Durran, D. R. Numerical methods for fluid dynamics (Springer).

3

• LeVeque, R. Numerical Methods for Conservation Laws (Springer)• Ortega, J.M. and Poole, W.G. An Introduction to Numerical Methods for Differential

Equations. 1981 (Pitman Publishing Inc)• Ferziger, J. H. and Peric, M. Computational Methods for Fluid Dynamics (Springer).• Numerical Recipes: The Art of Scientific Computing, Third Edition (2007), (Cambridge

University Press). http://www.nr.com/fortran

Other People’s lecture notes for similar courses• https://people.maths.ox.ac.uk/trefethen/pdetext.html

Discussion citeIf you have questions about the module or assignments outside lesson times, you can askthem on the discussion cite for this module on blackboard. You are encouraged to look therefor answers first and answer questions if you can.

4

Page 3: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Numerical Modelling of the Atmosphere and OceanWeather, ocean and climate forecasts predict the winds, temperature and pressure from theNavier-Stokes equations:

Simulated surface pressure and winds from www.windyty.com using data from theECMWF prediction model: a numerical solution of the Navier-Stokes equations.

5

Additional equations predict the transport of moisture and pollutants in the atmosphere orsalinity and nutrients in the ocean:

All of these simulations involve numerical integration. We will start here ...

6

Page 4: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Chapter 1: Introduction to Numerical Methods in Python1.1 Numerical IntegrationIntegration cannot always be done exactly because:

• Not all functions can be integrated analytically• An analytic function is not known, just the values at some points.

There are a number of methods that can be used to integrate a function numerically. Forexample:

• The trapezium rule• Simpson’s rule• n-point Gaussian quadrature

1.1.1 1-point Gaussian quadratureTo integrate function f (x) approximatelybetween x = a and x = b. Divide the intervalinto N parts of length ∆x = (b−a)/N. Then

∫ b

af (x)dx≈

N−1

∑i=0

∆x f(a+(i+

12)∆x)

This is 1-point because there is only 1evaluation of f in each sub-interval of size∆x. 0.0 0.2 0.4 0.6 0.8 1.0

x

0.0

0.2

0.4

0.6

0.8

1.0

f

7

1.1.2 1-point Gaussian quadrature in Python# Calculate the approximate integral of sin(x) between a and b using# 1-point Gaussiaun quadrature using N intervals

# Use division and the print function from python 3 and import numerical libraryfrom __future__ import division, print_functionimport numpy as np

# Set integration limits and number of intervalsa = 0b = np.piN = 20# from these calculate the interval lengthdx = (b - a)/N# Initialise the integralI = 0.0

# Sum contribution from each interval (end of loop at end of indentation)for i in xrange(0,N): # note: ":" before indented region

I += np.sin(a + (i+0.5)*dx) # note: i takes values 0 then 1

I *= dx # same as I = I*dx

exact = -np.cos(b) + np.cos(a) # the exact values for comparisonprint(’Gaussian quadrature integral (with ’, N, ’ intervals) is ’, I, \

’\nExact integral = ’, exact, ’, error = ’, I - exact)

Exercise: Pretend to be the computer. Work through the above code in order to calculate I.Use N=2 to make it quicker. What value do you get for I and for the error? π

√2/2,

π√

2/2−2≈ 0.22

8

Page 5: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.2 Data TypesNumbers in Python 2.7 take one of the following types:int An integer in the range ±2147483647 (sys.maxint)long An integer as big as necessary (limited by the computer memory)float A real number in the range ±1.7976931348623157×10308

(sys.float_info.max)Smallest float 2.2250738585072014×10−308 (sys.float_info.min)Smallest float distinguishable from 1: 2.220446049250313×10−16

(sys.float_info.eps)complex A complex number consisting of two floating point numbers

with complex arithmetic (eg complex(1,2) or 1+2j)Dividing an integer by an integer gives the integer part of the number. What do these codesnippets produce?

3/2 1 3./2 1.5 3/2. 1.5

Using division from python 3:from __future__ import division3/2

A float in python is “double precision”: it takes up 64 bits of memory (on a 64 bitcomputer).A numpy.float32 is “single precision”.A numpy.float16 is “half precision”.

9

1.2.1 Working with Real NumbersWhat will this code produce?# Calculate the pressure over a circular area,# given a force and the radius of the circle.# Pressure = force/areafrom __future__ import divisionfrom numpy import piforce = 10radius = 1e-164if radius != 0:

area = pi*radius**2pressure = force/areaprint(’pressure = ’, pressure)

else:print(’error, cannot calculate pressure over \

a zero radius circle’)

ZeroDivisionError: float division by zero

Why?radius is not exactly zero but area is truncated to zero and so an error is generated ondivision by zero.How can we avoid errors like this?

10

Page 6: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Avoid division by zero and avoid comparing real numbers for equality# Calculate the pressure over a circular area, given a# force and the radius of the circle. Pressure = force/areafrom __future__ import division, print_functionfrom numpy import pi# Import system package which defines a small floating point numberimport sys

force = 10radius = 1e-164eps = sys.float_info.epsilon # see help(sys.float_info)area = pi*radius**2

if abs(area) > eps:pressure = force/areaprint(’pressure = ’, pressure)

else:print(’radius = ’, radius, ’ gives area = ’, area, \’ which is too small to calculate the pressure’)

What will this code produce?radius = 1e-164 gives area = 0.0 which is too small tocalculate the pressure

11

1.2.2 Representation of Real Numbers on Computers• Integers can be stored by a computer exactly if they are not too large.• Real numbers are stored with limited precision. They are truncated to the number of

significant figures that the machine can record.• Real numbers are stored in a binary form of scientific notation. For example

−1.01×211 or 1.101×2−101

– The (positive part of the) first number is called the mantissa

– The (positive part of the) second number is called the exponent:

±(mantissa)×2±exponent

– On a 32-bit computer the 32 bits could be assigned as follows:± mantissa ± exponent

1 bit 23 bits 1 bit 7 bits

– the mantissa is a binary number between 1 and less than 2.– the exponent is a binary integer• If you try to create a real number with more significant figures than will fit into the

mantissa or exponent, something will be lost. This can lead to some problems with realnumbers:

12

Page 7: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.2.2.1 ExercisesCalculators store real numbers similarly to computers. On your calculator, calculate theseand explain the answers.Voting for problems: I no problem, II not enough space in mantissa, III not enough space inexponent.

1. 1×1010 +1×10−10 =1×1010

Problem: Truncation errors when adding large and small reals. (Not enough bits availablefor the mantissa). These problems accumulate and can cause big problems!

2. 1×10−10 +1×10−10 = 2×10−10

Problem: No problem. Two small numbers can be added accurately3. (1×1099)2 = Error

Problem: The exponent won’t fit in the number of bits available.4. (1×10−99)2 = 0

Problem: The exponent won’t fit in the number of bits available.

5.√

1+5×10−10 = 1Problem: On my calculator, there is enough space in the mantissa to store 1+5×10−10

exactly but the number is even closer to 1 after the square root so there is no longerenough space in the mantissa.

13

1.2.3 Summing small and large numbersThe sum of a large and a small floating point number leads to round-off errors. If this isdone many times, errors can accumulate and become significant.1.2.4 Summing lots of numbersIf you have to sum lots of numbers, eventually you will get to a stage where you are addinga relatively small number to a much larger number. This will lead to rounding errors.1.2.4.1 Binary ConversionDecimals cannot always be converted to binary numbers precisely. For example, the decimalnumber, 0.01 cannot be expressed exactly in binary see table below). Therefore the binaryrepresentation in the computer will be truncated which will not necessarily convert back tothe same value.

The decimal number 0.01 in binary, truncated and then converted backDecimal Binary

0.01 → 0.00000010100011110101110000101 ...↓ truncate

0.009999275... ← 0.00000010100011110101

14

Page 8: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.3 FunctionsYou can use built in functions:a = 3.5b = 3.5 + aa = max(a,b)

What is a? 7And define your own functions:def closerToZero(a,b):

"Return the argument closer to zero"if abs(a) < abs(b):

return aelse:

return b

x = 1.1y = -1.1z = closerToZero(x,y)

What is z? -1.1Functions can change their arguments:def maxAbs(a,b):

"Make both arguments non-negative and return the bigger one"a = abs(a)b = abs(b)return max(a,b)

z = maxAbs(1.1, -1.2)

What is z? 1.2

15

Functions can return zero, one or many thingsdef sayHello():

"Say hello to the caller"print("Hello")

def sayHelloTo(name):"Say hello to the name"print("Hello", name)

def swap(a,b):"Swap the order of a and b in the return"return b,a

[x,y] = swap(1,2)

What are x and y? 2 and 1Functions should be used to avoid global variables:def main():

"Put a script inside a main function"[x,y] = swap(closerToZero(1,2), maxAbs(3,4))print(’x = ’, x, ’ y = ’, y)

if __name__ == ’__main__’:main()

What gets printed? x = 4, y = 1

16

Page 9: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.3.1 Doc-stringsDoc-strings are the text inside quotes after the def line of a funciton. They explain

1. What the function does2. What the arguments are3. What is returned

Functions should ALWAYS have doc-strings!

If you typehelp(functionName)

python will print out the doc-string.

17

1.4 Roots of Non-linear Equations• The Navier Stokes equations are non-linear and so difficult to solve numerically.• Many much simpler non-linear equations cannot be solved analytically. Eg:

x5 + x+1 = 0sinx+ x+1 = 0

• There are a number of methods to solve f (x) = 0 approximately:– Fixed point iteration– Newton-Raphson

– Bracketing methods ∗ Bisection∗ Regula falsi∗ Secant

1.4.1 The Bisection MethodTo find an approximate solution, xa, to f (x) = 0 with error < ε (so that | f (xa)|< ε).

1. Find a and b either side of a root(so that f (a) f (b)< 0).

0.0 0.2 0.4 0.6 0.8 1.0x

1.0

0.8

0.6

0.4

0.2

0.0

0.2

0.4

0.6

0.8

f

2. Calculate the mid-point, c = (a+b)/23. Find f at the mid-point, ie f (c).4. The solution is sufficiently close to c if | f (c)|< ε .5. Otherwise, if f (a) and f (c) are opposite signs, then

set c as the new value for b, but if f (b) and f (c) areopposite signs then set c as the new a.

6. Go to 2

• Bisection is a simple and robust but slow.• ∴ often used to get a first guess, before using a more rapidly converging method

18

Page 10: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.4.2 The Bisection Method in PythonThis code defines a function,solveByBisect, which finds a root ofany function, f, between a and b by thebisection method. The functionsolveByBisect also has some argumentswith default parameters (nmax and e).

Note:• The main code is within a function,

“solveByBisect”. Ideally, there should notbe any variables defined outside afunction. Declaring variables outside afunction makes them global which meansthat they are visible from within all otherfunctions. This can lead to some nastybugs.

• The second else occurs only if you do notbreak out of the for loop

Exercise What will these produce?

1. solveByBisect(sin,-10,50,10,.1)

2. solveByBisect(exp,-0.1, 0.5)

Check your answers by writing the code.

def solveByBisect(f, a, b, nmax=100, e=1e-6):"Find a root of function f by bisection""between a and b to tolerance e.""Maximum nmax iterations. Returns the""root and the number of iterations used."

for it in range(nmax):c = 0.5*(a+b)if f(a)*f(c) < 0:

b = celse:

a = cif abs(f(c)) < abs(e):

breakelse:

raise Exception("No root found by \solveByBisect")

return c, it+1

# Define a function to solvedef poly5(x):

return x**5 + x + 1

root, nIts = solveByBisect(poly5, -1, 1)print("Root of poly5 is ", root, " in ",

nIts, " iterations. Check ","poly5(root) = ", poly5(root))

19

1.5 Python on University Windows 10 PCs• You may have been introduced to Spyder or PyCharm during welcome week. You can use

these for this module but I would not recommend them as they are big and, to me,cumbersome and confusing. I would recommend that you remove them from theCloudpaging player on your Windows 10 PC if they have already been installed.

• From Software (Apps Anywhere) search for “python” and launch Python 2.7.15.• In the file manager, create a new folder on the N drive called MTMW12 and create a

sub-folder called assig1.• Having launched Python, you will have a Python text editor called “Idle” installed which I

recommend. Search for and open it using the searcher in the bottom left corner.• In Idle, choose “New File” from the file menu.• Typeprint("hello world")

in the file and save the file as helloWorld.py in the new folder that you have created.• Run the helloWorld.py script by pressing F5 or using the run menu.

20

Page 11: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.6 Order of OperatorsThe order of operator in Python is similar to in mathematics.The operators closer to the top of the list are done first.

Python Operator Description** Exponentiation+x, -x Positive, negative*, /, % Multiplication, Division and Remainder+, - Addition and subtraction<<, >> Shifts& Bitwise AND^ Bitwise XOR| Bitwise OR<, <=, >, >=, <>, !=, == Comparisonsis, is not Identity testsin, not in Membership testsnot Boolean NOTand Boolean ANDor Boolean OR

Use brackets if you need the order of operators to be different:In Mathematics ax2 +bx+ c In Python a*x**2 + b*x + c

(a+b)2

c+d (a + b)**2/(c + d)

Do not use more brackets than are needed!

21

1.7 Help with Python• Type help() to enter the interactive help. Read the output and follow the suggestions.• Type quit to quit online help

1.8 Python Functions and Packages Python has some built in functions:exit() to exit python

abs() divmod() input() open() staticmethod()all() enumerate() int() ord() str()any() eval() isinstance() pow() sum()basestring() execfile() issubclass() print() super()bin() file() iter() property() tuple()bool() filter() len() range() type()bytearray() float() list() raw_input() unichr()callable() format() locals() reduce() unicode()chr() frozenset() long() reload() vars()classmethod() getattr() map() repr() xrange()cmp() globals() max() reversed() zip()compile() hasattr() memoryview() round() __import__()complex() hash() min() set() apply()delattr() help() next() setattr() buffer()dict() hex() object() slice() coerce()dir() id() oct() sorted() intern()

Type help(functionName) to see what any of them do

22

Page 12: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

However you will need many more functions than this. What about sin and cos? In order toget access to more functions you will need to import packages. Some packages you mightuse:

numpy Mathematical functions and multidimensional arrays supportmatplotlib 2D plotting librarypylab A combination of the two above

There are 4 ways that you can use the functions in a package:

1. from numpy import *Now you can use all the functions in numpy

2. from numpy import sinNow you can use the function sin from numpy

3. import numpyNow you can use all the functions from numpy but you must refer to them as:numpy.sin(3) etc

4. import numpy as npNow you can use all the functions from numpy but you must refer to them as:np.sin(3)etc

5. In order to find out what functions are defined in a library (say numpy), type:import numpydir(numpy)

23

1.9 IndentationLoops and conditionals in Python are controlled by indentation and colons (see the examplecode in the notes). It is therefore very important that you get the indentation right and thatyou put the colon at the end of the first line.1.9.1 Tabs or spaces for indentation - very important to read thisIf you mix tabs and spaces in python, it will behave in unexpected ways. The python codethat I am giving you has spaces not tabs. If you are using Idle it will already insert spacesinstead of tabs for indentation. If you are using another text editor you will need to check thesettings. In spyder go to Tools -> Preferences-> Editor-> Advancedsetting and make sure that “4 spaces” is selected for “Indentationcharacters”

1.10 Manually Wrap Long LinesWrap long lines – unless inside brackets, use \ at the end of the line so that the next line is acontinuation. Set up your text editor to avoid lines longer than 80 characters. For example:gradient = (value2 - value1) \

/(x2 - x1)

1.11 Global and Local VariablesA global variable is a variable that can be accessed from all functions, even if it is not anargument to that function. This can be very dangerous and lead to bugs in your code. A localvariable is a variable that is only defined within a specific function. Avoid global variablesby putting all code within functions.

24

Page 13: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.12 Python Arrays and ListsA list of variables can be declared usingmyList = [8, 3, 5, 1]

The elements are numbered from zero and can be accessed using square brackets so thatExample Returns Example Returns Example ReturnsmyList[0] 8 myList[1] 3 myList[3] 1

myList[-1] 1 myList[-2] 5myList[4] IndexError: list index out of range

We will mostly be using numpy arrays which can be declared in many ways. For example,for an array of five zeros:import numpy as nparraySize = 5myArray = np.zeros(arraySize)

These can be set or accessed using a loopfor i in range(arraySize):

myArray[i] = 2*i + 3print(myArray)

What does this code print out?[ 3. 5. 7. 9. 11.]

25

1.12.1 Range Commands and Array Access• range([start,] stop[, step])

returns a list of integers from start to just before stop every stepExamples Returnsrange(4) [0, 1, 2, 3]

range(1,4) [1, 2, 3]

range(2,7,2) [2, 4, 6]

range(7,2,-2) [7, 5, 3]

• xrange([start,] stop[, step])Like range but instead of returning a list, returns the numbers one by one as needed.Therefore less memory is used if the values are used in turn for a loop.

• numpy.arange([start,] stop[, step,])Returns an array of integer or non-integer values

Examples Returnsarange(4) array([0, 1, 2, 3])

arange(3.,1.,-0.5) array([3., 2.5, 2., 1.5])

• Accessing elements of lists . Define x=range(7,1,-1). Fill in the table below.Note: x[-1] returns the last element of the list and x[-2] returns the last but one.

Example Returns Example Returns Example Returnsx [7,6,5,4,3,2] x[-1] 2 x[:5] [7,6,5,4,3]

x[0] 7 x[-2] 3 x[1:] [6,5,4,3,2]

x[1] 6 x[1:5] [6,5,4,3] x[1:5:2] [6,4]

26

Page 14: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.12.2 Copying lists and arraysAssignment statements in Python do not copy objects, they create bindings between a targetand an object (http://docs.python.org/2/library/copy.html). So if I do:a = [’Jack’, ’Jill’, ’Tim’, ’Dave’]b = aa[0] = ’Fred’print(b)

I get:[’_________’, ’Jill’, ’Tim’, ’Dave’]

because “a” and “b” are just two names for the same list. However if I want two separatearrays and I want to copy the contents of one array to the other:import numpy as npa = np.array([’Jack’, ’Jill’, ’Tim’, ’Dave’])b = a.copy()a[0] = ’Fred’print(b)

I get:[’________’, ’Jill’, ’Tim’, ’Dave’]

Which is probably what you wanted

27

1.13 Plotting Graphs using the Python library matplotlib1.13.1 A first exampleFirst import the plotting library (and numpy for array, sin and cos):import numpy as np # get access to arraysimport matplotlib.pyplot as plt # the plotting functions

Next create the x and y data and plotx = np.arange (-np.pi, np.pi, np.pi/100) # create x - datay = np.sin(x) # compute y - dataplt.plot(x, y) # create plotplt.show() # show plot (makes window pop up)

1.13.2 More control over graphsimport numpy as npimport matplotlib.pyplot as pltx = np.linspace (-np.pi, np.pi, 100) # 100 points from -pi to pi inclusivey1 = np.sin(x)y2 = np.sin(3*x)plt.ion() # interactive mode onplt.clf() # clear any existing figureplt.plot(x, y1, label=’sin(x)’) # plot the first function with a legendplt.plot(x, y2, label=’sin(3x)’) # and the second (over-plot)plt.legend() # shows the legendplt.grid() # show a gridplt.xlabel(’x’) # insert this x labelplt.axis([x[0], x[-1], -1, 1]) # sets the axis ranges

See copious online support and examples for matplotlib

28

Page 15: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

1.13.3 Exercises (for outside practical session time)1. How do you change the color and line-style of graphs2. How to plot symbols instead of lines3. How to change the placing of ticks and grid intervals4. How to plot a log-log plot5. How do you put the legend in a different place?6. How do you save the figure?

29

30

Page 16: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Chapter 2: Taylor Series and Numerical ApproximationsThis is an introduction to the concept of a Taylor series and how these are used in numericalanalysis to find numerical approximations and estimate their accuracy.There is a series of four (or five) short videos to accompany these printed notes. You shouldwatch the videos or read through the notes on Taylor series before the next lecture. Duringthe class you can work through the exercises and ask for help.The videos are on Blackboard under:Videos/VideosonTaylorSeries

or at:https://www.youtube.com/playlist?list=PLEG35I51CH7UttDft9tOB0ej_kkAJF368

Or go to Hilary Weller’s Youtube page and see the Taylor Series playlist

31

• Many functions can be expressed as Taylor series• Taylor series are infinite polynomials

For example an exponential:

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

1

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

1

1 +x

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

1

1 +x

1 +x+ x2

2!

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

1

1 +x

1 +x+ x2

2!

1 +x+ x2

2!+ x3

3!

2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.02

1

0

1

2

3

4

5

6ex

1

1 +x

1 +x+ x2

2!

1 +x+ x2

2!+ x3

3!

1 +x+ x2

2!+ x3

3!+ x4

4!

32

Page 17: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

For example a sine wave:

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5

sinx

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5

sinx

x

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5

sinx

x

x− x3

3!

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5

sinx

x

x− x3

3!

x− x3

3!+ x5

5!

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5

sinx

x

x− x3

3!

x− x3

3!+ x5

5!

x− x3

3!+ x5

5!− x7

7!

The more terms you include, the more accurate it should get.

33

Functions with discontinuities cannot be expressed as Taylor series:

6 4 2 0 2 4 61.5

1.0

0.5

0.0

0.5

1.0

1.5square wave

A polynomial cannot jump between 1 and -1

34

Page 18: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Functions with discontinuities in some of their derivatives cannot be expressed asTaylor series:

Eg. cosine bell, f (x) =

{1/2(1+ cosx) |x|< π0 otherwise

6 4 2 0 2 4 60.5

0.0

0.5

1.0

1.5

cosine bell1−1

2x2

2!

1−12x2

2!+1

2x4

4!

1−12x2

2!+1

2x4

4!−1

2x6

6!

A polynomial cannot be uniform in one place and non-uniform in another

35

The Taylor Series:A function, f , can be represented as a Taylor series about position a if:

• is continuous near a and• all of its derivatives are continuous near a

Using the notation ∆x = x−a:

f (x) = f (a)+∆x f ′(a)+∆x2

2!f ′′(a)+

∆x3

3!f ′′′(a)+ · · ·+ ∆x j

j!f ( j)(a)+ · · ·

where f ′(x) =d fdx

(x), f ′′(x) =d2 fdx2 (x), ...

• If infinitely many terms are used then this approximation is exact near a.• If all terms of order n and above are discarded then the error is approximately proportional

to ∆xn (assuming that ∆x is small). Then the approximation is said to be nth order accurate• A third order accurate approximation for f (x) has error proportional to ∆x3:

f (x) = f (a)+∆x f ′(a)+∆x2

2!f ′′(a)+O

(∆x3) .

We say that the error is of order ∆x3 or O(∆x3).

• If ∆x is small, then higher order accuracy generally means higher accuracy

36

Page 19: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

2.0.1 Exercises (answers at end)1. Write down the infinite Taylor series for function f at position x+∆x about x

2. Write the third order approximation for f (x−∆x) in terms of f (x), f ′(x) and f ′′(x). Writethe error term using the O(∆xn) notation.

3. Write the third order approximation for f (x+∆x) in terms of f (x−∆x), f ′(x−∆x) andf ′′(x−∆x).

37

2.1 Numerical DifferentiationConsider a set of points, x0,x1, · · ·x j, · · ·xn where x j = j∆x (the points are distance ∆x apart).Assume that we know the value of the function f (x) at these points, as shown in figure 2.1.

x0 x1 x2 x j x j+1x∆ x j−1 x

f f jj−1

f j+1

f

Figure 2.1: Values of a function f at points x0, x1, · · · ,x j, · · · .Some possible estimate of f ′j = f ′(x j) are:

forward difference backward difference centred difference

f ′j ≈f j+1− f j

∆xf ′j ≈

f j− f j−1

∆xf ′j ≈

f j+1− f j−1

2∆xTaylor series can be used to derive estimates of derivatives and to find their order ofaccuracy.

38

Page 20: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

2.2 Taylor Series to find Finite Difference GradientsIn order to use a Taylor series (below) to find an approximation for f ′

f (x+∆x) = f (x)+∆x f ′(x)+∆x2

2!f ′′(x)+

∆x3

3!f ′′′(x)+ · · ·+ ∆x j

j!f ( j)(x)+ · · ·

1. write down the knowns2. consider where we want to find f ′

3. consider what order of accuracy we want4. write down Taylor series for some of the knowns5. eliminate the additional unknowns to find f ′

2.2.1 Example1. Assume that we know f j = f (x j)

f j−1 = f (x j−1) = f (x−∆x)f j+1 = f (x j+1) = f (x+∆x)

2. and we want to find f ′j.3. For 3 knowns we wonder if we can get second order accuracy4. We do not want to generate too many unknowns. We don’t know f ′j−1 or f ′j+1 so no

Taylor series about x j−1 or x j+1. So let’s try Taylor series for f j+1 and f j−1 about x j

f j+1 = f j +∆x f ′j +∆x2

2!f ′′j +

∆x3

3!f ′′′ +O(∆x4)

f j−1 = f j−∆x f ′j +∆x2

2!f ′′j −

∆x3

3!f ′′′ +O(∆x4)

39

5. Eliminate f ′′j by taking the difference of the two equations

f j+1− f j−1 = 2∆x f ′j +∆x3

3f ′′′j +O(∆x4)

Rearrange to get f ′j

f ′j =f j+1− f j−1

2∆x− ∆x2

3!f ′′′j +O(∆x3)

We cannot eliminate f ′′′j so this is part of the error:

f ′j =f j+1− f j−1

2∆x+O(∆x2)

The error, ε , is proportional to ∆x2 (ε ∝ ∆x2) so this approximation is second orderaccurate.

This is a worked example on my YouTube page:https://youtu.be/xPTnH0PAAXs?list=PLEG35I51CH7UttDft9tOB0ej_kkAJF368called TaylorSeries2

40

Page 21: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

2.2.2 Exercises (answers at the end)1. Use the Taylor series to find an approximation for f ′j in terms of f j and f j−1. What order

accuracy is it?

2. Derive an uncentred, second order difference formula for f ′j that uses f j, f j+1 and f j+2.(And show that it is second order accurate)

3. Find an uncentred approximation for f ′′j using f j, f j+1 and f j+2. What order accurate isit?

4. Derive a second order approximation for f ′b from fa, fb and fc at x locations a < b < cwhen the grid spacing is not regular. (And show that it is second order accurate)

41

2.3 Order of Accuracy of Numerical SolutionsIn order to demonstrate the order of accuracy (or order of convergence) of a numericalmethod, we can calculate the error of solving a problem that has an analytic solution. Forexample our numerical method calculates the gradient of sinx and gives these results:

∆x numerical gradient of sinx at x = 0 Error, ε (Difference from cos(0))0.4 0.97355 -0.026450.2 0.99335 -0.006660.1 0.99833 -0.00167

Assume that ε = A∆xn where n is the order of accuracy and A is unknown. From the data,eliminate A and find two possible values for n.

10-2 10-1 100

∆x

10-3

10-2

10-1

|erro

r|

Plot |ε| as a function of ∆x on log paper. Then the order of accuracy is the gradient. Doesthis value agree with the values found from calculating n directly from the data above? (bothshould give about n = 2. See YouTube video for a worked example.

42

Page 22: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

2.4 Interpolation

An Example:

• Function f is known at points x1 and x2(values f1 and f2)

• We want to estimate the value of f atpoint xi in between x1 and x2 x

1x

2

1f

if

2f

xi

β∆x (1−β)∆ x

∆x

x

• Exercise: Use linear interpolation (ie assume that fi lies on a straight line between f1 andf2): to find f at xiHint: First write down expressions for ∆x, β and the gradient, f ′ between x1 and x2. Thenfind an expression for f at xi along the straight line between x1 and x2.

∆x = x2− x1 β =xi− x1

x2− x1f ′ =

f2− f1

x2− x1=⇒ fi = (1−β ) f1 +β f2

• If f is known at n points then a polynomial of degree n−1 can be fit to estimate f

Eg. Cubic Lagrange interpolation for constant grid spacing, ∆x:

f̂ (x) =−16

β (1−β )(2−β ) fk−1 +12(1+β )(1−β )(2−β ) fk

+12(1+β )β (2−β ) fk+1−

16(1+β )β (1−β ) fk+2

43

2.4.1 Finding Finite Difference Formulae for Interpolation using Taylor SeriesAn Example:Assume that we know f j = f (x j) and f j+1 = f (x j+1) and we want to find the interpolatedvalue, f j+1/2

, mid-way between x j and x j+1.

• Start by writing down Taylor series for f j and f j+1 about f j+1/2

f j+1 = f j+1/2+

∆x2

f ′j+1/2+

12!

(∆x2

)2

f ′′j+1/2+

13!

(∆x2

)3

f ′′′j+1/2+O(∆x)4

f j = f j+1/2− ∆x

2f ′j+1/2

+12!

(∆x2

)2

f ′′j+1/2− 1

3!

(∆x2

)3

f ′′′j+1/2+O(∆x)4

• Eliminate the largest unknown, f ′j+1/2by adding the two equations

f j + f j+1 = 2 f j+1/2+

∆x2

4f ′′j+1/2

+O(∆x)4

• Rearrange to find f j+1/2and express the error based on the largest unknown

f j+1/2= ( f j + f j+1)/2+O(∆x)2

• So this is a second-order accurate approximation

44

Page 23: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

2.4.2 Exercises (answers at the end)1. Derive a centred, second order difference interpolation formula for f j that uses f j−1 and

f j+1. (And show that it is second order accurate)2. Derive a centred fourth order difference formula for f ′j+1/2

that uses f j−1, f j, f j+1 andf j+2. (And show that it is fourth order accurate)

3. Show that the first order forward difference formula for f ′j is exact for linear functions,f (x) = ax+b.

4. Show that the centred second order difference formula for f ′j is exact for quadraticfunctions f (x) = ax2 +bx+ c.

45

SolutionsSolutions to Exercises 2.0.11. Write down the infinite Taylor series for function f at position x+∆x about x

f (x+∆x) = f (x)+∆x f ′(x)+∆x2

2!f ′′(x)+

∆x3

3!f ′′′(x)+ · · ·+ ∆x j

j!f ( j)(x)+ · · ·

2. Write the third order approximation for f (x−∆x) in terms of f (x), f ′(x) and f ′′(x). Writethe error term using the O(∆xn) notation.

f (x−∆x) = f (x)−∆x f ′(x)+∆x2

2!f ′′(x)+O

(∆x3)

3. Write the third order approximation for f (x+∆x) in terms of f (x−∆x), f ′(x−∆x) andf ′′(x−∆x).

f (x+∆x) = f (x−∆x)+2∆x f ′(x−∆x)+2∆x2 f ′′(x−∆x)+O(∆x3)

Solutions to Exercises 2.2.21. Use the Taylor series to find an approximation for f ′j in terms of f j and f j−1. What order

accuracy is it?Write the Taylor series for f j−1 in terms of f j:f j−1 = f j−∆x f ′j +O(∆x2)

Rearrange to find f ′j:f ′j = ( f j− f j−1)/∆x+O(∆x)Note dividing O(∆x2) by ∆x gives O(∆x) so the approximation is first order accurate

2. Derive an uncentred, second order difference formula for f ′j that uses f j, f j+1 and f j+2.(And show that it is second order accurate)

46

Page 24: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Taylor approximations for f j+1 and f j+2 about f j:

f j+1 = f j +∆x f ′j +∆x2

2!f ′′j +

∆x3

3!f ′′′j +

∆x4

4!f ′′′′j +O(∆x5)

f j+2 = f j +2∆x f ′j +2∆x2 f ′′j +4∆x3

3f ′′′j +

2∆x4

3f ′′′′j +O(∆x5)

Eliminate the largest unknown, f ′′j by calculating f j+2−4 f j+1:f j+2−4 f j+1 =−3 f j−2∆x f ′j +O(∆x3)

Rearrange to find f ′j:f ′j =

(− f j+2 +4 f j+1−3 f j

)/(2∆x)+O(∆x2)

3. Find an uncentred approximation for f ′′j using f j, f j+1 and f j+2. What order accurate isit?Taylor approximations for f j+1 and f j+2 about f j:

f j+1 = f j +∆x f ′j +∆x2

2!f ′′j +

∆x3

3!f ′′′j +

∆x4

4!f ′′′′j +O(∆x5)

f j+2 = f j +2∆x f ′j +2∆x2 f ′′j +4∆x3

3f ′′′j +

2∆x4

3f ′′′′j +O(∆x5)

Eliminate the largest unknown, f ′j by calculating f j+2−2 f j+1:f j+2−2 f j+1 =− f j +∆x2 f ′′j +O(∆x3)

Rearrange to find f ′′j :f ′′j =

(f j+2−2 f j+1 + f j

)/∆x2 +O(∆x)

4. Derive a second order approximation for f ′b from fa, fb and fc at x locations a < b < cwhen the grid spacing is not regular. (And show that it is second order accurate)Define ∆x1 = b−a and ∆x2 = c−b and ∆x = max(∆x1,∆x2)Taylor approximations for fa and fc about fb:

47

fa = fb−∆x1 f ′b +∆x2

12!

f ′′b −∆x3

13!

f ′′′b +∆x4

14!

f ′′′′b +O(∆x51)

fc = fb +∆x2 f ′b +∆x2

22!

f ′′b +∆x3

23!

f ′′′b +∆x4

24!

f ′′′′b +O(∆x52)

Eliminate the largest unknown, f ′′b :∆x2

1 fc−∆x22 fa = ∆x2

1{

fb +∆x2 f ′b +O(∆x32)}−∆x2

2{

fb−∆x1 f ′b +O(∆x31)}

Rearrange to find f ′b:∆x2

1 fc−∆x22 fa = ∆x2

1 fb +∆x21∆x2 f ′b +O(∆x3

2∆x21)−∆x2

2 fb +∆x1∆x22 f ′b +O(∆x3

1∆x22)

=⇒ ∆x21 fc−∆x2

2 fa =(∆x2

1−∆x22)

fb +∆x1∆x2 (∆x1 +∆x2) f ′b +O(∆x5)

=⇒ f ′b =∆x2

1 fc−∆x22 fa−

(∆x2

1−∆x22)

fb

∆x1∆x2 (∆x1 +∆x2)+O(∆x2)

Solutions to Exercises 2.4.21. Use 2 Taylor series about f j so that they both contain f ′j so that we can eliminate it:

f j−1 = f j−∆x f ′j +12!

∆x2 f ′′j +O(∆x3)

f j+1 = f j +∆x f ′j +12!

∆x2 f ′′j +O(∆x3)

Eliminate f ′j by adding the two equations:f j−1 + f j+1 = 2 f j +∆x2 f ′′j +O(∆x3)Rearrange for f j and express the unknowns as the order of accuracy:f j = ( f j−1 + f j+1)/2+O(∆x2)

2. Taylor series about f j+1/2:

f j−1 = f j+1/2− 3

2∆x f ′j+1/2

+12!

94

∆x2 f ′′j+1/2− 1

3!278

∆x3 f ′′′j+1/2+

14!

8116

∆x4 f ′′′′j+1/2+O(∆x5)

48

Page 25: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

f j = f j+1/2− 1

2∆x f ′j+1/2

+12!

14

∆x2 f ′′j+1/2− 1

3!18

∆x3 f ′′′j+1/2+

14!

116

∆x4 f ′′′′j+1/2+O(∆x5)

f j+1 = f j+1/2+

12

∆x f ′j+1/2+

12!

14

∆x2 f ′′j+1/2+

13!

18

∆x3 f ′′′j+1/2+

14!

116

∆x4 f ′′′′j+1/2+O(∆x5)

f j+2 = f j+1/2+

32

∆x f ′j+1/2+

12!

94

∆x2 f ′′j+1/2+

13!

278

∆x3 f ′′′j+1/2+

14!

8116

∆x4 f ′′′′j+1/2+O(∆x5)

First eliminate f ′′j , the biggest unknown (we only need to leave two equations):

f j+2− f j−1 = 3∆x f ′j+1/2+

98

∆x3 f ′′′j +O(∆x5)

f j+1− f j = ∆x f ′j+1/2+

13

18

∆x3 f ′′′j +O(∆x5)

Next eliminate f ′′′j leaving one equation for f ′j:f j+2− f j−1−27 f j+1 +27 f j =−24∆x f ′j+1/2

+O(∆x5)

=⇒ f ′j+1/2=

f j−1−27 f j +27 f j+1− f j+2

24∆x+O(∆x4)

3. If f (x) = ax+b and we use the approximation f ′j =f j+1− f j

∆xbetween any two points

x j+1 and x j then we get:

f ′j =(ax j+1 +b)− (ax j +b)

x j+1− x j= a which is the exact solution.

4. If f (x) = ax2 +bx+ c and we use the approximation f ′j =f j+1− f j−1

2∆xfor f j at x j

between two points x j+1 and x j−1 then we get:

f ′j =(ax2

j+1 +bx j+1 + c)− (ax2j−1 +bx j−1 + c)

x j+1− x j−1=

a(x j +∆x)2−a(x j−∆x)2 +2b∆x2∆x

= 2ax j +b which is the exact solution.

49

50

Page 26: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

Chapter 3: Software ArchitectureThere are numerous very bad programming practices in science. We will cover a few waysin which things could be done better.

3.1 Some Good Programming Practices1. Plan your code before writing it. Work out what variables you will need and how big the

arrays will be. Work out how to structure your code using functions.2. Comments:

(a) All files should have over-arching comments describing the file contents.(b) All functions should have doc-strings to describe and document them. They are in

double quotes and appear immediately after the function definition line.(c) Doc-strings should describe all arguments and output of a function.(d) Blocks of code and loops should have comments describing what they do.(e) Do not make obvious comments (eg loop from 0 to n−1 every 1).(f) Make sure that comments are up to date with the code.(g) Consider using print statements instead of comments to monitor progress during

execution.3. Write efficient code:

(a) Do not declare more arrays than you need (unless this dramatically improves the style).(b) Where possible, avoid conditionals inside loops. For example, do not test a conditional

every time around a loop if you know exactly where it will be different.51

Good u[0] = ...u[N] = ...for i in xrange(1,N):

u[i] = ...

Bad for i in xrange(0,N+1):if i == 0:

u[i] = ...else if i == N:

u[i] = ...else:

u[i] = ...The second version is more complex code, longer and more expensive to run.

(c) When looping over an array, do not recalculate the whole array every time around.4. Coding to Avoid Errors:

(a) Avoid code duplication by using functions and by getting the structure of the code right.(b) Avoid very deep nested loops. Use functions instead.(c) Use variables rather than having input parameters as numbers and pass variables as

arguments to functions rather than re-defining them in each function.(d) Use data structures to avoid long argument lists to functions.(e) If there are any dependencies between variables, calculate these in the code.(f) Avoid global variables.

5. Code readability:

(a) Put space between functions, space between blocks, space either side of = and spacewhere needed to make the code more readable. For example:y = a*x**2 + b*x + c not y=x ** 2+b*x+c

(b) Do not use more brackets than you need.(c) Wrap long lines – unless inside brackets, use \ at the end of the line so that the next line

is a continuation. Set up your text editor to avoid lines longer than 80 characters.

52

Page 27: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

(d) Make the code look like the maths.(e) Use good variable names (eg use i to loop over an array x in direction x.)

6. Re-write. It is difficult to get the design right first time. Once you have got it all working,now is the time to re-structure to improve readability, efficiency and extensibility.

7. If you are new to using arrays and looping over arrays you should make sure that you cando all initialisation and manipulation of arrays by hand rather than using inbuilt pythonhandling of arrays. Once you are confident manipulating arrays, you should use numpyarray manipulations instead of explicit loops as this will be faster and take fewer lines ofcode.

53

3.2 Testing CodeWithout proper testing, it is impossible for human beings to write correct code once aproject becomes substantial. Not enough testing is done in scientific programming whichhampers the progress of science and weakens scientific arguments. This is a situation thatyou need to be involved in changing. This chapter will cover just the very beginning; raisingand catching exceptions and making assertions in order to test the functions that you write.There are some excellent modules to help with testing in Python that are not covered here,for example, unittest and nosetest. Tools like coverage can show you how muchof your code you have tested. There is good integration with some integrated developmentenvironments (IDEs), such as PyCharm, which helps with most aspects of writing, testing,debugging, and running code. The testing presented here is based on material fromhttps://docs.python.org/2/tutorial/errors.html.You should write tests that give no output if the test passes and gives a message if the testfails. You can then write scripts full of tests and run them which should give no output.The testing described here will use the code solveByBisect.py available onblackboard under assignment 2.

54

Page 28: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

3.2.1 Making AssertionsSee https://docs.python.org/2/reference/simple_stmts.htmlWe will start by testing the function for finding a root of a function by the bisection method:def solveByBisect(f, a, b, nmax=100, e=1e-6):

"Solve function f by bisection method.""Solve to error e starting from a and b. Maximum nmax iterations."

# Iterate until the solution is within the error or too many iterationsfor it in range(nmax):

c = 0.5*(a+b)if f(a)*f(c) < 0:

b = celse:

a = cif abs(f(c)) < abs(e):

break # Break out of the for loopelse: # If you haven’t broken out of the for loop by the end,

# raise an exceptionraise Exception("No root found by solveByBisect")

return c, it+1

TestingWe can now make assertions about what solveByBisect should do for particular sets ofarguments and the error message to give if the wrong answer is foundimport numpy as npassert abs(np.cos(solveByBisect(np.cos, 0, np.pi)[0])) < 1e-6, \

"solveByBisect gave the wrong answer for cos"assert abs(solveByBisect(lambda x: 2*x+3, -10,10)[0] + 1.5) < 1e-6, \

"solveByBisect gave the wrong answer for a linear function"

55

3.2.2 Raising ExceptionsSee https://docs.python.org/2/tutorial/errors.htmlThe standard types of exception that can be raised in Python are athttps://www.tutorialspoint.com/python/standard_exceptions.htm

We will catch exceptions to check that solveByBisect doesn’t find a root when there isno root in the range of values supplied. In this example we want the exception to be foundso the test passes in this situation but fails if the function finds a root where it shouldn’t:try:

solveByBisect(np.cos, -1., 1.)except:

passelse:

print("Error in solveByBisect, an exception should be raised as there is no root of cosbetween -1. and 1.)

3.2.2.1 Value and Type ErrorsValue and type errors are types of exception that can be used to check that the correct typesor ranges of values are passed to our functions. For example, the solveByBisectfunction could include some value and type checks:

if nmax <= 0:raise ValueError(’Argument nmax to solveByBisect should be >0’)

if e <= 0:raise ValueError(’Argument e to solveByBisect should be >0’)

if not(isinstance(a, float)) or not(isinstance(b, float)):raise TypeError(’Arguments a and b to solveByBisect should be a floats’)

if not(callable(f)):raise Exception(’A callable function must be sent to solveByBisect’)

56

Page 29: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

You should use TypeError sparingly; it is not Pythonic and can make your codeinflexible. It may be more useful, for example, to convert integer values of a and b tofloating point values.a = float(a)b = float(b)

Having raised all these exceptions, you will need to include try,except,elsestatements in your test script to check that they are raised correctly.try:

solveByBisect(0,1.,3.)except:

passelse:

print("Error in solveByBisect, an exception should be raised if no function is passed.")

try:solveByBisect(np.cos, 1.,1.,0)

except ValueError:pass

else:print("Error in solveByBisect, an exception should be raised if nmax <= 0")

try:solveByBisect(np.cos, 1.,1.,1,0)

except ValueError:pass

else:print("Error in solveByBisect, an exception should be raised if e <= 0")

57

3.3 Version Control using gitSoon after starting to write code, you are likely to find that you have modified the code andit doesn’t do what you want any more, you have multiple versions and you can’t keep track,you cannot reproduce the results that you got yesterday and if two people are working on thesame code then you are likely to get in a muddle. You need to be using version controlsoftware like git and a repository hosting service like https://github.com/ or theUniversity of Reading version, https://gitlab.act.reading.ac.uk.There are numerous online resources for how to use git and github. This will thereforebe a tiny introduction. A more comprehensive introduction is at, for example:https://swcarpentry.github.io/git-novice/andhttp://product.hubspot.com/blog/git-and-github-tutorial-for-beginners

Most online documentation describes how to use git on a command line. Thisdocumentation will describe git on the University PCs.You should really also know about collaborating, branching and merging in git but thislittle introduction will cover setting up a repository, adding to it, pushing and pulling from aremote repository and recovering old versions. This introduction draws heavily from theSoftware Carpentry introduction.

58

Page 30: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

3.3.1 Setting up a Local Repository on the University PCsYou should start by putting your code from the first week’s assignment into a repository.The repository will be located in the highest level directory that contains the code. Forexample, your directory structure might be MTMW12/assig1.

• Launch git from https://appsanywhere.reading.ac.uk

• Create a new repository by choosing a directory holding the code that you want to include(for example directory assig1) and give the repository a name (for exampleMTMW12_assig1).

• Select the files that you want to include – source code should be included but output suchas graphics files should not be included

– Click on the file name to view the file– Click on the file icon to add the file to the repository

• The files are now staged for commit. In order to commit them into the repository, youneed to write a commit message and press “Commit”

This can also be achieved with commands on the command line. You can access thecommand line by selecting “Git Bash” from the “Repository” menu. Then you will need thecommands:$ git add fileName1.py fileName2.py# Then to check what has been staged and then commit:$ git status$ git commit -m "First commit"

59

3.3.2 Log into https://gitlab.act.reading.ac.uk and create a RemoteRepository

• Create a new repository with the same name as the local repository (do not initialise witha Readme file).

3.3.3 Link the local and remote repositoriesFor this you will need to use the “Git Bash” command line (from the “Repository” menu).(This should be possible by choosing “Add” from the “Remote” menu but I couldn’t get thisto work.)Copy the url of your new repository from the github website. For example it might besomething like https://github.com/hilaryweller0/MTMW12_assig1. Then,on the command line type, using your url rather than mine, type:$ git remote add origin https://github.com/hilaryweller0/MTMW12_assig1$ git remote -v # (to verify the new remove URL)

3.3.4 Push the changes in your local repository to GitHub.If you initialised the remote repository with a README pull and merge before push$ git pull origin master

After closing the window that asks you to write a merge message you can now do:$ git push --set-upstream origin master

Now have a look at the website for your repository and see your new code there.

60

Page 31: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

3.3.5 More Things You can Do with the Git GUI• First you will need to make some changes to the files that you committed to the repository

(using your Python text editor). For example you could add some comments or adoc-string to a function.

• Next, in the Git GUI click Rescan and you will see which files have changed.• You can add (stage) them individually by clicking on the file icon or add all files in the

directory structure by clicking on StageChanged• Write a commit message and then press Commit• Click Push to push them to github• Check the github website to see the changes to your repository• In order to see a history of the changes that you have made choose “Browse Master’s

Files” from the “Repository” menu.

61

3.3.6 Adding all suitable filesYou may have a directory structure full of files that you want to add to the repository.However it is important not to add large or binary files to a git repository. Therefore youneed a .gitignore file to tell git not to add files with well known extensions that implythat they are binary files. Using a text editor, create a file called .gitignore in yourrepository directory. Add the following lines to the file so that git does not automaticallyinclude these files:# File types to ignore

*.pdf

*.png

*.jpg

*.bak

*~

*.pcy

*.docx

*.gif

*.xlxs

*.zip*.out

*.log

*.aux

*.svg

You can now add all files in the directory structure to the repository without the risk ofadding large or binary files by clicking StageChanged.

62

Page 32: MTMW12: Introduction to Numerical Methods for Atmosphere ...sws02hs/teaching/MTMW12/MTMW12_2_lec.pdf · MTMW12: Introduction to Numerical Methods for Atmosphere and Ocean Models Dr

3.3.7 More Things on the git command lineEverything that you can do with the gui you can also do on the command line and a lotmore. Also most online help will tell you commands rather than gui actions:

• To add all suitable files within the directory structure$ git add .

• To add a list of files$ git add fileName1.py fileName2.txt fileName3.tex

• To see the list of staged and unstaged files$ git status

• To make a commit$ git commit -m "Put commit message here"

• To push to or pull from remote repository$ git push$ git pull

63

3.3.8 Retrieving Old Versions• In order to see a list of commit ids and messages:

$ git log

• If you make some changes to a file (say helloWorld.py), you can see the differencesbetween what is on disk and the version of the previous commit:$ git diff helloWorld.py# or if you have already added helloWorld.py but not committed:$ git diff HEAD helloWorld.py# or if you want to compare helloWorld.py to the version from# three commits ago:$ git diff HEAD~3 helloWorld.py

• You can also show the difference between a file on disk and a previous commit using thecommit id:$ git diff cb27fc3e526dbdcd49b70b3f08a520ae271f8be5 helloWorld.py

• To show what changes were made at an older commit rather than differences betweenworking directory and a commit:$ git show HEAD~3 helloWorld.py

• If you need to discard the changes that you have made and go back to the most recentcommit:$ git checkout -- helloWorld.py

or to go back to a particular commit (just using first few letters of the commit id):$ git checkout f22b25e helloWorld.py

64