atmospheric modeling fortran introduction part 2
play

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:


  1. Atmospheric modeling: Fortran introduction, part 2

  2. Topics Monday 1.11. Tuesday 2.11. ✔ Data types ➔ Arrays ✔ Precision ➔ Procedures ✔ Operators ➔ Modules ✔ Control structures ➔ File I/O ➔ Compiling ➔ Debugging

  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

  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

  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]

  6. Array intrinsic functions ANY (L_array [, dim]) returns a scalar value of .TRUE. if any value in L_array is .TRUE. ALL (L_array [, dim]) returns a scalar value of .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”

  7. Structured programming • Structured programming based on functions, subroutines and modules – testing and debugging separately – recycling of code – improved readability – re-occurring tasks

  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

  9. Procedures = subroutines & functions Function Subroutine [TYPE] FUNCTION func(ARGS) SUBROUTINE sub(ARGS) [RESULT(arg)] [declarations] [declarations] [statements] [statements] END SUBROUTINE sub END SUBROUTINE func Function call: Subroutine call: res = func(ARGS) CALL sub(ARGS)

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

  11. INTENT keyword • Declares how SUBROUTINE f(x,y,z) IMPLICIT NONE formal argument is REAL,INTENT(in) ::x intended for REAL,INTENT(inout) ::y REAL,INTENT(out) ::z transferring a value x=10 ! Compilation error – in – out y=10 ! Correct – inout (default) y=z ! Compilation error z=y*x ! Correct • Compiler uses this END SUBROUTINE f for error checking and optimization

  12. Dividing the source code into modules • Declaration: • Usage: MODULE accuracy IMPLICIT NONE INTEGER, PARAMETER ::& PROGRAM testprog realp = SELECTED_REAL_KIND(12,6) USE check INTEGER, PARAMETER ::& IMPLICIT NONE intp =SELECTED_INT_KIND(4) INTEGER(KIND=intp) :: x,test END MODULE accuracy test=check_this(x) MODULE check USE accuracy END PROGRAM testprog IMPLICIT NONE INTEGER(KIND=intp) ::y CONTAINS FUNCTION check_this(x)RESULT(z) INTEGER:: x, z z = HUGE(x) END FUNCTION END MODULE check

  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

  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

  15. File read/write • The syntax is (the brackets [ ] indicate optional 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')

  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

  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’

  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'

  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)

  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

  21. Source code remarks - 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.

  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

  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

  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 ...)

  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

  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

  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/

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

  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 ...

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend