Atmospheric modeling: Fortran introduction, part 2 Topics Monday - - PowerPoint PPT Presentation

atmospheric modeling fortran introduction part 2
SMART_READER_LITE
LIVE PREVIEW

Atmospheric modeling: Fortran introduction, part 2 Topics Monday - - PowerPoint PPT Presentation

Atmospheric modeling: Fortran introduction, part 2 Topics Monday 1.11. Tuesday 2.11. Data types Arrays Precision Procedures Operators Modules Control structures File I/O Compiling Debugging Arrays:


slide-1
SLIDE 1

Atmospheric modeling: Fortran introduction, part 2

slide-2
SLIDE 2

Topics

Monday 1.11.

✔ Data types ✔ Precision ✔ Operators ✔ Control structures

Tuesday 2.11.

➔ Arrays ➔ Procedures ➔ Modules ➔ File I/O ➔ Compiling ➔ Debugging

slide-3
SLIDE 3

Arrays: initialization

INTEGER, PARAMETER :: M = 100, N = 500 INTEGER :: idx(M) REAL(kind = 4) :: vector(0:N-1) REAL(kind = 8) :: matrix(M, N) CHARACTER (len = 80) :: screen ( 24) ! or INTEGER, DIMENSION(1:M) :: idx REAL(kind = 4), DIMENSION(0:N-1) :: vector REAL(kind = 8), DIMENSION(1:M, N) :: matrix CHARACTER(len=80), dimension(24) :: screen

slide-4
SLIDE 4

Arrays: usage

INTEGER, PARAMETER :: M = 4, N = 5 REAL (kind = 8) :: A(M,N) , x(N), y(M) INTEGER :: I , J do I=1,M ; y(I) = 0 ; end do do J = 1, N do I = 1, M y(I) = y(I) + A(I , J) * x(J) end do end do

slide-5
SLIDE 5

Array intrinsic functions

  • SIZE (array [, dim]) returns # of elements in

the array [, along the specified dimension]

  • SHAPE (array) returns an INTEGER vector

containing SIZE of array in each dimension

  • COUNT (L_array [,dim]) returns count of

elements which are .TRUE. in L_array

  • SUM (array[, dim][, mask]) : sum of the

elements [, along dimension] [, under mask]

slide-6
SLIDE 6

Array intrinsic functions

ANY (L_array [, dim]) returns a scalar value

  • f .TRUE. if any value in L_array is .TRUE.

ALL (L_array [, dim]) returns a scalar value

  • f .TRUE. if all values in L_array are .TRUE.

INTEGER :: j, IA(4, 2) IA(:, 1) = (/ (j, j = 1,SIZE(IA,dim=1)) /) IA(:, 2) = (/ (SIZE(IA,dim=1) + j, j = 1, SIZE(IA,dim=1)) /) PRINT *, SHAPE(IA) PRINT *, COUNT(IA > 0), COUNT(IA <= 0, dim = 2) PRINT *, SUM(IA), SUM(IA, dim=2, mask = IA > 3) IF (ANY(IA < 0)) PRINT *,”Some IA’s less than zero” IF (ALL(IA >= 0)) PRINT *,”All IA’s non-negative”

slide-7
SLIDE 7

Structured programming

  • Structured programming based on

functions, subroutines and modules

– testing and debugging separately – recycling of code – improved readability – re-occurring tasks

slide-8
SLIDE 8

Procedures = subroutines & functions

  • By procedures we mean subroutines and

functions

  • Subroutines exchange data with

arguments only

  • Functions return value according to its

declared data type

slide-9
SLIDE 9

Procedures = subroutines & functions

Function

[TYPE] FUNCTION func(ARGS) [RESULT(arg)] [declarations] [statements] END SUBROUTINE func

Function call:

res = func(ARGS)

Subroutine

SUBROUTINE sub(ARGS) [declarations] [statements] END SUBROUTINE sub

Subroutine call:

CALL sub(ARGS)

slide-10
SLIDE 10

Procedures = subroutines & functions

INTEGER FUNCTION test(s) IMPLICIT NONE INTEGER::s test=10*s END FUNCTION test PROGRAM dosomething ... result=test(s) ... SUBROUTINE test(s,test) IMPLICIT NONE INTEGER::s,test test=10*s END FUNCTION test PROGRAM dosomething ... call test(s,result) ...

slide-11
SLIDE 11

INTENT keyword

SUBROUTINE f(x,y,z) IMPLICIT NONE REAL,INTENT(in) ::x REAL,INTENT(inout) ::y REAL,INTENT(out) ::z x=10 ! Compilation error y=10 ! Correct y=z ! Compilation error z=y*x ! Correct END SUBROUTINE f

  • Declares how

formal argument is intended for transferring a value

– in – out – inout (default)

  • Compiler uses this

for error checking and optimization

slide-12
SLIDE 12
  • Declaration:

MODULE accuracy IMPLICIT NONE INTEGER, PARAMETER ::& realp = SELECTED_REAL_KIND(12,6) INTEGER, PARAMETER ::& intp =SELECTED_INT_KIND(4) END MODULE accuracy MODULE check USE accuracy IMPLICIT NONE INTEGER(KIND=intp) ::y CONTAINS FUNCTION check_this(x)RESULT(z) INTEGER:: x, z z = HUGE(x) END FUNCTION END MODULE check

  • Usage:

PROGRAM testprog USE check IMPLICIT NONE INTEGER(KIND=intp) :: x,test test=check_this(x) END PROGRAM testprog

Dividing the source code into modules

slide-13
SLIDE 13

Dividing the source code into modules

  • Procedures defined in modules can be

used in any other program unit

  • Placing procedures in modules helps

compiler to detect programming errors and to optimize the code

  • Module procedures are declared after

CONTAINS-statement

slide-14
SLIDE 14

Dividing the source code into modules

  • Objects in modules can be PRIVATE or

PUBLIC

– Default is PUBLIC, visible for all program units using the module

  • PRIVATE will hide the objects from other

program units

INTEGER, PRIVATE :: x INTEGER, PUBLIC :: y ! PUBLIC is the default PRIVATE :: z

slide-15
SLIDE 15

File read/write

  • The syntax is (the brackets [ ] indicate
  • ptional keywords or arguments) :

OPEN([unit=]iu, file='name' [, options]) CLOSE([unit=]iu [, options])

  • For example :

OPEN(10, file= 'output.dat', status='new') CLOSE(unit=10, status='keep')

slide-16
SLIDE 16

File read/write

  • The first parameter is the unit number
  • The keyword unit= can be omitted
  • The unit numbers 0, 5 and 6 are predefined

– 0 is output for standard (system) error messages – 5 is for standard (user) input – 6 is for standard (user) output

  • These units are opened by default and should

not be closed

16 16

slide-17
SLIDE 17

File read/write

  • You can also refer to the default output or

input unit with asterisk WRITE(*, ...) ! or READ(*, ...)

  • Note that they are NOT necessarily the

same as the unit numbers 5 and 6

  • If the file name is omitted in the OPEN, the

a file based on unit number will be opened, f.ex. for unit=12  ’fort.12’

slide-18
SLIDE 18

File open options

  • status : existence of the file

'old', 'new', 'replace', 'scratch', 'unknown'

  • position : offset, where to start writing

'append'

  • action : file operation mode

'write', 'read', 'readwrite'

  • form : text or binary file

'formatted', 'unformatted'

slide-19
SLIDE 19

File write

  • Writing to a file is done by giving the

corresponding unit number (iu) as a parameter : write(iu,*) str write(unit=iu, fmt=*) str

  • Formats and other options can be used as

needed

  • If keyword 'unit' used, also 'fmt' keyword

must be used (for formatted, text files)

slide-20
SLIDE 20

File read

  • Reading from a file is done by giving the

corresponding unit number (iu) as a parameter : read(iu,*) str read(unit=iu, fmt=*) str

  • Formats and other options can be used as

needed

  • Star (‘*’) format indicates free format input
slide-21
SLIDE 21
  • A variable name can be no longer than 31 characters. Only

letters, digits or underscore. Must start with a letter.

  • Maximum row length may be 132 characters.
  • There may be 39 continuation lines, if a line is ended

with ampersand, &, it will continued on the next line.

  • No distinction between lower and uppercase character
  • Character strings are case sensitive

! Continuation line example INTEGER :: a, b, c, d a = a + b + & c + d ! The above is equivalent to following two lines a = a + b + c + d A = A + b + C + d ! Fortran is not case sensitive ! Some compilers may support case sensitivity if a programmer likes ! to have it.

Source code remarks

slide-22
SLIDE 22

Source code remarks

! Character strings are case sensitive CHARACTER(LEN=32) :: ch1, ch2 Logical :: ans ch1 = 'a' ch2 = 'A' ans = ch1 .EQ. ch2 WRITE(*,*) ans ! OUTPUT from that WRITE statement is: F ! WARNING, when strings are compared ! the shorter string is extended with blanks WRITE(*,*) 'A' .EQ. 'A ' !OUTPUT: T WRITE(*,*) 'A' .EQ. ' A' !OUTPUT: F

  • Statement separation: newline and semicolon, ;

! Semicolon as a statement separator a = a * b; c = d**a ! The above is equivalent to following two lines a = a * b c = d**a

slide-23
SLIDE 23

Compiling

  • To compile a source code (for running):

gfortran program.f90 -o program

  • To compile a module (to be linked to main program)

gfortran -c module.f90

  • To combine (link) a source code and a module to a runnable

program gfortran -c module.f90 gfortran program.f90 module.o

slide-24
SLIDE 24

Makefile

  • For a project with more than one source-file
  • Compile all source codes (or selected ones) with one

command 'make'

  • First, define compiler (FC) and compiler options

(FCFLAGS)

  • Then list all items that need to be compiled as

TARGET: SOURCE1 (SOURCE2 ...) ${FC} ${FCFLAGS} SOURCE1 (SOURCE2 ...)

slide-25
SLIDE 25

Makefile: example

FC=gfortran FCFLAGS=-g temperature : temperature.f90 module.o ${FC} ${FCFLAGS} temperature.f90 module.o -o temperature module.o : module.f90 ${FC} ${FCFLAGS} -c module.f90 clean : rm -f temperature module.o

slide-26
SLIDE 26

Makefile: example

$ make gfortran -c module.f90 gfortran temperature.f90 module.o -o temperature $ make make: `temperature' is up to date. $ make clean rm -f temperature module.o $ make module.o gfortran -c module.f90

slide-27
SLIDE 27

Debugging

  • Useful when the code crashes unexpectedly
  • Simple debugging: add lines of WRITE/PRINT

to see which lines are being executed

– You can use a logical variable debug to

enable/disable debugging

  • More advanced debugging with separage

debugger software (for e.g. Gdb)

– http://www.gnu.org/software/gdb/

slide-28
SLIDE 28

Debugging: example

PROGRAM myprogram IMPLICIT NONE ... LOGICAL :: debug=.TRUE. ... DO i=1, 10 ... IF(debug) THEN PRINT *, value(i) END IF END DO ...

slide-29
SLIDE 29

Debugging: gdb

$ gdb myprogram (gdb) break myprogram.f90:20 (gdb) r Starting program: myprogram Breakpoint 1, myprogram () at myprogram.f90:20 20 PRINT *, T (gdb) n ...

slide-30
SLIDE 30

Must on this course

  • Fortran 90/95
  • Argument intent (in/out/inout)
  • Implicit none
  • Proper indenting
  • Proper variable naming conventions
  • Modularization
  • Clarity, enough commenting