Title: Introduction to scientific programming in Earth's Sciences
1Introduction to scientific programming in Earth's
Sciences
J.W. Goethe University. Frankfurt
- Dr Guillaume RICHARD
- Institüt für Geowissenchaften 1.232
- richard_at_geophysik.uni-frankfurt.de
2Overview
- 12 x 45min Lectures / 45min training classes
(praktical) -
- Basics (Hardware, OS, editor, etc.. )
- Basics 2 (Linux commands, Compile,Visualize )
- Languages Fortran (FORmula TRANslation)
- Languages Fortran (2)
- Languages Fortran (3)
- Languages C/C
- Languages Matlab (1)
- Languages Matlab (2)
- Languages Maple
- Introduction to Finite difference / Finite Volume
method - Introduction to Finite difference / Finite Volume
method (2) - Introduction to Finite elements method
3L. 3 Fortran1. Basics
4Fortran Basics
Some History Fifty Years of Fortran
- FORTRAN, the first high level programming
language, was announced to the computing world by
John Backus and his team from IBM at the Western
Joint Computer Conference held in Los Angeles,
California in February 1957 - For an An IBM 704 mainframe to use in nuclear
reactor design - F66, F77, F88 (oupps F90), F95, F2003
- More information on the background and
development of the FORTRAN I, II and III
compilers can be found on the Computer History
Museum website (http//community.computerhistory.o
rg/scc/projects/FORTRAN/)
5Fortran Basics
Sayings from cards distributed at Pioneer Day. 25
years anniversary (1982)
6Fortran Basics
- Compilers
- g95 GNU Fortran 95 compiler (gcc)
- gfortran (supports OpenMP-Open Multi Processing )
- ifort (Intel fortran compiler)
- Silverfrost FTN95
- FORCE Project - Fortran Compiler and Editor. an
IDE for Fortran 77 that integrates the GNU
Fortran 77 compiler (g77). - (http//www.thefreecountry.com/compilers/fortran.s
html)
7Fortran Basics
- Program Structure
- A program starts with the keyword PROGRAM,
- followed by a program name,
- followed by the IMPLICIT NONE statement,
- followed my some specification statements,
- followed by the execution part,
- followed by a set of internal subprograms,
- followed by the keywords END PROGRAM and the
program name. - For improving readability, your program should
(must) add comment lines.
8Fortran Basics
- Comments
- All characters following an exclamation mark, !,
except in a character string, are commentary, and
are ignored by the compiler. - A blank line is also interpreted as a comment
line. - Continuation Lines
- a statement must start on a new line. If a
statement is too long to fit on a line, it can be
continued with - Ex A 174.5 Year
- () Count / 100
9Fortran Basics
- Literal constants
- Fortran has five types of constants
- Integer a string of digits with an optional
sign - Real 2 representations,
- decimal A decimal point must be presented ( no
commas). Can have an optional sign. - exponential an integer or a real number in
decimal representation (the mantissa or
fractional part), followed by the letter E or e,
followed by an integer (the exponent). - Ex12.3456e2 1234.56
10Fortran Basics
- Literal constants (next)
- Complex We will see if we have time to cover it
- Logical True or False (more later)
- character string must be enclosed between
double quotes or apostrophes (single quotes). The
length of the string is the number of characters
of its content - Ex 'Bill 2' and "Bill 2" content Bill 2
and length 7 - Ex "Newton's apple" or 'Newton'' apple'
11Fortran Basics
- Fortran Identifiers
- It has no more than 31 characters
- The first character must be a letter,
- The remaining characters, if any, may be letters,
digits, or underscores - Fortran identifiers are case insensitive. Except
for strings - Fortran does not have any reserved words.
Keywords as END, PROGRAM, DO are perfectly legal
Fortran identifiers. However, this is definitely
not a good practice. Keywords are in upper case.
12Fortran Basics
- Variables and Their Types
- A variable can be considered as a box that is
capable of holding a single value of certain type
(i.e. memory space). - INTEGER the variable is capable of holding an
integer - REAL capable of holding a real number
- COMPLEX capable of holding a complex number
- LOGICAL capable of holding a logical value (i.e.
.TRUE. or .FALSE.) - CHARACTER capable of holding a character string
of certain length
13Fortran Basics
- Variable Declarations
- Declaring the type of a Fortran variable is done
with type statements. - Ex type-specifier list
- For CHARACTER. Since a string has a length
attribute, a length value must be attached to
character variable declarations. - Ex CHARACTER(LEN1) letter, digit
- CHARACTER(1) letter, digit
- CHARACTER letter, digit
- CHARACTER(LEN10) letter, digit1
- CHARACTER(LEN) letter, digit
14Fortran Basics
- The PARAMETER Attribute
- To assign a name to a value
- Add PARAMETER in front of the double colon ()
and use a comma to separate the type name (i.e.,
REAL) and the word PARAMETER - Following each name, one should add an equal sign
() followed by an expression. The value of this
expression is then assigned the indicated name. - The name assigned to a value is not a variable.
- After assigning a name to a value, that name can
be used in a program, even in subsequent type
statements. - Â Â Ex REAL, PARAMETER PI3.141592,
PI2PIPI - CHARACTER(LEN4), PARAMETER City "LA"
- CHARACTER(LEN), PARAMETER Name 'Billy'
15Fortran Basics
- Variables Initialization
- Initially the content of a variable (or a box) is
empty. Therefore, before one can use a variable,
it must receive a value. - The use of un-initialized variables may cause
unexpected result. - To put a value into a variable
- initializing it when the program is run
- Ex REAL Offset 0.1, Length 10.0
- using an assignment statement (more later)
- Ex length 3.
- reading a value from keyboard or other device
with a READ statement. (more later)
16Fortran Basics
- The Assignment Statement
- variable expression
- To save the result of the expression to the right
of the assignment operator to the variable on the
left. - The expression is evaluated first (more soon).
- If the type of the expression is identical to
that of the variable, the result is saved in the
variable. - Otherwise, the result is converted to the type of
the variable and saved there. - CHARACTER assignment follows the rules stated in
the discussion of the PARAMETER attribute. - Exercise swaps the values in A and B, with the
help of C.
INTEGER A 3, B 5, C C A A B B C
17Fortran Basics
- Arithmetic Operators
- Fortran has 4 types of operators
- Arithmetic , , /, , -
- Fortran has an exponential operator . Thus,
raising X to the Y-th power is written as XY - Relational lt, gt, lt, gt, ,/
- Logical .NOT., .AND., .OR., .EQV., .NEQV.
- Character
- Operators are evaluated from left to right
(excepted the exponential ). - Ex ABC is equal to A(BC)
18Fortran Basics
- Arithmetic Expressions Modes
- An arithmetic expression is an expression using
an operator ( , -, , /, ) - Single mode expression all of whose operands
are of the same type. The type of the result of
an operation is identical to that of the
operands. - Ex 2 4 5 / 3 2
- Mixed mode INTEGER operands are always
converted to REAL. The result of a mixed mode
expression is of REAL type.
--gt 2 4 5 / 3 2 --gt 8 5 / 3 2 --gt
8 5 / 3 2 --gt 40 / 3 2 --gt 40 / 3
2 --gt 40 / 9 --gt 4
- In expression ab where a is REAL, the result is
undefined if the value of a is negative. - Ex -4.02 is defined with -16.0 as its result,
while (-4.0)2 is undefined.
19Fortran Basics
- Arithmetic Expressions Modes
- Mixed mode examples
- 5 (11.0 - 5) 2 / 4 9
- 25.0 1 / 2 3.5 (1 / 3)
- 5 (11.0 - 5) 2 / 4 9?5 (11.0 - 5.0)
2 / 4 9 - 5 (11.0 - 5.0) 2 / 4 9 ? 5 6.0 2 /
4 9 - 5 6.0 2 / 4 9 ? 5 36.0 / 4 9 ? 5
36.0 / 4 9 - 5.0 36.0 / 4 9 ? 5.0 36.0 / 4 9 ? 180.0
/ 4 9 - 180.0 / 4 9 ?180.0 / 4.0 9?180.0 / 4.0
9 - 45.0 9 ? 45.0 9?45.0 9.0 ? 54.0
- ?25.0 1 / 2 3.5 (1 / 3)?25.0 / 2 3.5
(1 / 3) - 25.0 / 2 3.5 (1 / 3) ? 25.0 / 2.0 3.5
(1 / 3) - 12.5 3.5 (1 / 3) --gt 12.5 3.5 (1 /
3) - 12.5 3.5 0 --gt 12.5 3.5 0 --gt 12.5
1.0 --gt 12.5
20Fortran Basics
CHARACTER Operator
- Only one character operator, the concatenation
operator // - Ex CHARACTER(LEN6)fn "Bob"4, ln"Marley",
fulln11 - fullnfn//ln
-
- Substrings
- One can append the extent specifier at the end of
a CHARACTER variable to indicate a substring. - Ex ln(26) contains "arley", fn(2) contains
"Bo - fulln(24)ln(24) -gt fulln contains "BarlMarley "
FullN contains "Bob Marley "
21Fortran Basics
Exercises PROGRAM Fortran_Traps IMPLICIT
NONE INTEGER, PARAMETER A 2, B 2, H
3 INTEGER, PARAMETER O 4, P
6 CHARACTER(LEN5), PARAMETER M 'Smith', N
'TEXAS' CHARACTER(LEN4), PARAMETER X
'Smith CHARACTER(LEN6), PARAMETER Y
'TEXAS ! The exponential trap PRINT, "First,
the exponential trap " PRINT, A, ' ', B, '
', H, ' ', ABH PRINT, '( ', A, ' ',
B, ' ) ', H, ' ', (AB)H PRINT, A, '
( ', B, ' ', H, ' ) ', A(BH) PRINT, !
The integer division trap. Intrinsic function
REAL() converts ! an integer to a real
number PRINT, "Second, the integer division
trap " PRINT, PRINT, O, ' / ', P, ' ',
O/P PRINT, 'REAL( ', O, ' ) / ', P, ' ',
REAL(O)/P PRINT, O, ' / REAL( ', P, ' ) ',
O/REAL(P) PRINT, ! The string truncation
trap PRINT, "Third, the string truncation trap"
PRINT, 'IS ', M, ' STILL IN ', N, '?' PRINT,
'IS ', X, ' STILL IN ', Y, '? ' END PROGRAM
Fortran_Traps