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
● 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
Atmospheric Modeling (5 ECTS)
● 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 first week● Mon 10.15-12.00
– 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
Emissions-anthropogenic (SO
2, black carbon, ...)
-natural (organic compounds, dust, ...)
Meteorology-transport, radiative fluxes, ...
Chemistry & aerosols-OH, O
3, photolysis, ...
-aerosol formation, condensation, ...
Atmospheric modeling
Emissions
nucleation condensationchem
istry
chemistry
Atmospheric particle formation
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
#include <iostream> int main(){ std::cout << "Hello World!" << std::endl; return 0;}
C++
program hello print *,"Hello World!" end program hello
Fortranpublic class HelloWorld { public static void main(String[] args) {
System.out.println("Hello, world!");
}}
Java
print "Hello, World!"
Python
The classic ”Hello World!” example.
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 declarationsREAL :: 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:', xWRITE (*,*) 'computed value of x**2 + 1:', y! SQRT(y), Return the square root of the argument yWRITE (*,*) '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 declarationIMPLICIT NONE! After declaration and initialization the value of a variable can be ! changed whenever neededINTEGER :: n0INTEGER :: n1=0
REAL :: a, bREAL :: r1=0.0
COMPLEX :: cCOMPLEX :: imag_unit=(0.1, 1.0)
CHARACTER(LEN=80) :: placeCHARACTER(LEN=80) :: name='James Bond'
LOGICAL :: test0 = .TRUE.LOGICAL :: test1 = .FALSE.
! How to define constants? After declaration it can not be changedREAL, 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 -10r < n < 10r 2) real, precision at least p decimals3) real, range -10r < x < 10r and precision at least p decimals
INTEGER, PARAMETER :: short=SELECTED_INT_KIND(4)INTEGER, PARAMETER :: double=SELECTED_REAL_KIND(12,100)INTEGER (KIND=short) :: indexREAL (KIND=double) :: x,y,zCOMPLEX (KIND=double) :: c
x=1.0_double; y=2.0_double * ACOS(x)
OperatorsArithmetic operatorsREAL :: x,yINTEGER :: i = 10x=2.0**(-i) !exponentiation and negation precedence: firstx=x*REAL(i) !multiplication and type change precedence: secondx=x/2.0 !division precedence: secondi=i+1 !addition precedence: thirdi=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
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
LOGICAL :: L1, L2! How to determine the values of logical expressions! F means .FALSE. T means .TRUE.
Logical operators
Control structures1) IF THEN ELSE (branching)2) SELECT CASE (selecting) 3) DO (looping) PROGRAM test_ifIMPLICIT NONEREAL :: x,y,eps,t
WRITE(*,*)' Give x and y :'READ(*,*)x, yeps = EPSILON(x)
IF (ABS(x) > eps) THEN t=y/xELSE WRITE(*,*)'division by zero' t=0.0END IFWRITE(*,*)' y/x = ',tEND PROGRAM
Control structures: IF, ELSE, ELSE IF
PROGRAM test_if
IMPLICIT NONEREAL :: x,y,eps,t
WRITE(*,*)' Give x and y :'READ(*,*)x, yeps = 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 IFEND 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 :: iLOGICAL :: 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 callEND SELECT...
Control structures: DO, DO WHILE1) DO-loop with an integer counter (count controlled)
INTEGER :: i, stepsize, NumberOfPointsINTEGER, PARAMETER :: max_points=100000REAL :: x_coodinate(max_points), x, totalsum...stepsize=2DO i = 1, NumberOfPoints, stepsize x_coordinate(i) = i*stepsize*0.05END DO
2) DO WHILE-construct (condition controlled loop)
totalsum = 0.0READ(*,*) xDO WHILE (x > 0) totalsum = totalsum + x; READ(*,*) xEND DO
Control structures: DO, DO WHILE3) DO-loop without loop control
REAL :: x, totalsum, epstotalsum = 0.0DO READ(*,*) x IF (x < 0) THEN EXIT ! inside loop control, exit the loop END IF totalsum = totalsum + xEND DO
4) DO-loop, “repeat until” styleeps = 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 IFEND 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