Atmospheric Modeling (5 ECTS) ● Lectures: – Mon 10.15-12.00, Physicum D105 – Tue 10.15-12.00, Physicum D104 ● Exercises: – Mon 14.15-16.00, Physicum D105
Atmospheric Modeling (5 ECTS) ● Teachers – Michael Boy, Dynamicum 1D01b – Sampo Smolander, Dynamicum 1D09a – Kenty Ortega, Dynamicum 1D16b – Risto Makkonen, Physicum B415 ● Course web page – http://www.courses.physics.helsinki.fi/fys/atmodel/ – slides, links to Fortran-guides, timetable
● Preceding studies: A basic knowledge in one programming language (Fortran, MATLAB, C+, …) is required. However, we will use the first two lectures and exercises to give a short and comprehensive introduction and training to Fortran . Each student should have access to one computer and we will help to install a Fortran compiler if not already available.
Course content ● Introduction to Fortran ● Introduction to boundary layer meteorology modeling ● Including chemistry in the model ● Implementing emissions ● Modeling aerosol dynamical processes ● Overview of atmospheric models and complex modeling techniques
Course structure ● Lectures ● Exercise – Developing a chemical transport model including biogenic emissions, chemistry and aerosol dynamics – Bring your exercise material to the exercise session on memory stick, paper, or your laptop ● Exam – Based on lecture content – Your model must be ready before the exam
Timetable for ● Mon 10.15-12.00 first week – introduction to course – starting Fortran introduction – 1st exercise given (small Fortran task) ● Mon 14.15-16.00 – Checking that everyone has an environment for editing, compiling and running Fortran ● Tue 10.15-12.00 – Fortran introduction continues – Checking 1st exercise – Example of solving an equation in Fortran
Atmospheric modeling
Atmospheric modeling
Atmospheric modeling
Atmospheric modeling Meteorology -transport, radiative fluxes, ... Chemistry & aerosols -OH, O 3 , photolysis, ... -aerosol formation, condensation, ... Emissions -anthropogenic (SO 2 , black carbon, ...) -natural (organic compounds, dust, ...)
Atmospheric particle formation chemistry c h e nucleation condensation m i s t r y Emissions
How to make the computer solve our equations?
Modern programming languages C, C++ Python Java Perl Actionscript Matlab OCaml
Why would we use a programming language from the 50's?
Fortran (FORmula TRANslation) ● Simple to learn, less mistakes by beginners ● Almost all current scientific computational code is written in Fortran – Availability of well tested libraries, especially for mathematics ● More space for compiler optimization, fast code ● Portability of code
The classic ”Hello World!” example. Java Fortran public class HelloWorld { program hello public static void main(String[] args) { print *,"Hello World!" end program hello System.out.println("Hello, world!"); } } C++ Python #include <iostream> print "Hello, World!" int main() { std::cout << "Hello World!" << std::endl; return 0; }
PROGRAM squarerootexample ! Comments start with an exclamation point. ! Some exponentiation and squareroot computations. ! You will find data type declarations, couple arithmetic operations ! and an interface that will ask values for these computations. IMPLICIT NONE !do not use implicit variable declarations REAL :: x, y ! Command line interface. Ask a number and read it in WRITE (*,*) 'Give a value (number) for x:' READ (*,*) x y=x**2+1 ! exponentiation and addition arithmetic WRITE (*,*) 'given value for x:', x WRITE (*,*) 'computed value of x**2 + 1:', y ! SQRT(y), Return the square root of the argument y WRITE (*,*) 'computed value of SQRT(x**2 + 1):', SQRT(y) END PROGRAM squarerootexample
Data types INTEGER An integer number REAL A real number COMPLEX A pair of real numbers used in complex arithmetic LOGICAL TRUE or FALSE CHARACTER A string consisting one or more characters
!Variables can be initialized at their declaration IMPLICIT NONE ! After declaration and initialization the value of a variable can be ! changed whenever needed INTEGER :: n0 INTEGER :: n1=0 REAL :: a, b REAL :: r1=0.0 COMPLEX :: c COMPLEX :: imag_unit=(0.1, 1.0) CHARACTER(LEN=80) :: place CHARACTER(LEN=80) :: name='James Bond' LOGICAL :: test0 = .TRUE. LOGICAL :: test1 = .FALSE. ! How to define constants? After declaration it can not be changed REAL, PARAMETER :: pi=3.14159
Precision • The precision of a variable may be declared using the KIND-statement 1) SELECTED_INT_KIND(r) 2) SELECTED_REAL_KIND(p) 3) SELECTED_REAL_KIND(p,r) Explanations: 1) integer, range between -10 r < n < 10 r 2) real, precision at least p decimals 3) real, range -10 r < x < 10 r and precision at least p decimals INTEGER, PARAMETER :: short=SELECTED_INT_KIND(4) INTEGER, PARAMETER :: double=SELECTED_REAL_KIND(12,100) INTEGER (KIND=short) :: index REAL (KIND=double) :: x,y,z COMPLEX (KIND=double) :: c x=1.0_double; y=2.0_double * ACOS(x)
Operators Arithmetic operators REAL :: x,y INTEGER :: i = 10 x=2.0**(-i) !exponentiation and negation precedence: first x=x*REAL(i) !multiplication and type change precedence: second x=x/2.0 !division precedence: second i=i+1 !addition precedence: third i=i-1 !subtraction precedence: third Relational operators .LT. or < !less than .LE. or <= !less than or equal to .EQ. or == !equal to .NE. or /= !not equal to .GT. or > !greater than .GE. or >= !greater than or equal to Logical operators .NOT. !logical negation precedence: first .AND. !logical conjunction precedence: second .OR. !logical inclusive disjunction precedence: third .EQV. !logical equivalence precedence: fourth .NEQV. !logical nonequivalence precedence: fourth
Logical operators LOGICAL :: L1, L2 ! How to determine the values of logical expressions ! F means .FALSE. T means .TRUE. L1 L2 .AND. .OR. .EQV. .NEQV. F F F F T F F T F T F T T F F T F T T T T T T F
Control structures 1) IF THEN ELSE (branching) 2) SELECT CASE (selecting) 3) DO (looping) PROGRAM test_if IMPLICIT NONE REAL :: x,y,eps,t WRITE(*,*)' Give x and y :' READ(*,*)x, y eps = EPSILON(x) IF (ABS(x) > eps) THEN t=y/x ELSE WRITE(*,*)'division by zero' t=0.0 END IF WRITE(*,*)' y/x = ',t END PROGRAM
Control structures: IF, ELSE, ELSE IF PROGRAM test_if IMPLICIT NONE REAL :: x,y,eps,t WRITE(*,*)' Give x and y :' READ(*,*)x, y eps = EPSILON(x) IF (ABS(x) > eps) THEN IF (y > eps) THEN t=y/x ELSE IF (ABS(y) < eps) THEN t = 0.0 ELSE t = 1.0 END IF END IF END PROGRAM
Control structures: SELECT CASE -SELECT CASE statements matches the entries of a list against the case index. Only one found match is allowed. -Usually arguments are character strings or integers. -DEFAULT-branch if no match found. -If there is no CASE DEFAULT and no match found then the statement following END SELECT is executed ... INTEGER :: i LOGICAL :: isprimenumber ... SELECT CASE (i) CASE (2,3,5,7) ! variables are not allowed on the list isprimenumber = .TRUE. CASE (1,4,6,8:10) ! case value range, form low:high isprimenumber = .FALSE. CASE DEFAULT ! DEFAULT-branch isprimenumber = testprinumber(i) ! function call END SELECT ...
Control structures: DO, DO WHILE 1) DO-loop with an integer counter (count controlled) INTEGER :: i, stepsize, NumberOfPoints INTEGER, PARAMETER :: max_points=100000 REAL :: x_coodinate(max_points), x, totalsum ... stepsize=2 DO i = 1, NumberOfPoints, stepsize x_coordinate(i) = i*stepsize*0.05 END DO 2) DO WHILE-construct (condition controlled loop) totalsum = 0.0 READ(*,*) x DO WHILE (x > 0) totalsum = totalsum + x; READ(*,*) x END DO
Control structures: DO, DO WHILE 3) DO-loop without loop control REAL :: x, totalsum, eps totalsum = 0.0 DO READ(*,*) x IF (x < 0) THEN EXIT ! inside loop control, exit the loop END IF totalsum = totalsum + x END DO 4) DO-loop, “repeat until” style eps = 1000.0*EPSILON(1.0) DO ... !Iterative loop body where ABS(x) is getting smaller and smaller IF (ABS(x) < eps) THEN EXIT ! inside loop control, exit the loop END IF END DO
Control structures: example PROGRAM gcd ! Computes the greatest common divisor, EUCLIDEAN ALGORITHM IMPLICIT NONE INTEGER, PARAMETER :: long = SELECTED_INT_KIND(9) INTEGER (KIND=long) :: m, n, t WRITE(*,*)' Give positive integers m and n :' READ(*,*) m, n WRITE(*,*)'m:', m,' n:', n positivecheck: IF (m > 0 .AND. n > 0) THEN main_algorithm: DO WHILE (n /= 0) t = MOD(m,n) m = n n = t END DO main_algorithm WRITE(*,*)'Greatest common divisor: ',m ELSE WRITE(*,*)'Negative value entered' END IF positivecheck END PROGRAM gcd
Exercise #1 ● Create a Fortran program that – reads a real number from a file – calls a subroutine, which multiplies the value with pi ● (subroutine should be in a separate module-file) – prints the multiplied value to screen ● The whole program is about 15-20 lines
Recommend
More recommend