Fortran basics
Fortran Basics
Data types and declaraion
- Character
- Real (same as double in C-series languages )
- Integer
- Complex
- Logical (.TRUE. and .FALSE.)
Arrays
- Whole-array arithmetic (similar to some functional programming)
- Column major order/indexing (inherited by R/Matlab, unlike C/Python), therefore, a(1,1), a(2,1), a(3,1) will be faster to index.
|
|
Derived types
Derived types are “struct” in C/C++.
A derived type is a special form of data type that can encapsulate other built-in types as well as other derived types
|
|
Implicit and explicit declaration
FORTRAN has a historical feature called implicit typing which allows variable types to be inferred by the compiler based on the first letter of the variable (stupid). Basically, any variable that began with I, J, K, L, M, or N was an integer, and it was a real (floating point) otherwise.
Anyway, always use implict none
to override this stupid declaring!
Structure
Program: Top-level unit that can be invoked only from the operating system. It contains sub-program as following.
Function: An executable subprogram that is invoked from expressions and always returns a single result. No side effects.can only call it from the main program or another procedure.
Subroutine: Modify multiple arguments in-place but can’t be used in expressions. Cause side effcts. (side effects can be prevented by pure statement)
Module: A nonexecutable collection of variable, function, and subroutine definitions (since F90). The variable declaration and procedure definition sections (function and subroutine) are separated with a contains statement.
Submodule: Extends an existing module and is used for defining variable and procedure definitions that only that module can access; useful for more complex apps and libraries
In FORTRAN function/subroutine, dummy parameters are same as formal argument (形参) in other languages. In other languages, dummy arguments equals unused arguments.
In subroutine, INTEND (IN): indicating take value from outside and cannot be modified
INTEND (OUT): indicating that their values will be computed and passed to the outside.
|
|
IO
Standard streams
Standard streams include standard input/output/error. 标准输入stdin是指从键盘输入,标准输出stdout是指输出到终端, 如printf, system.out.println(). 标准错误stderr是另外一种输出流,用于输出错误消息或诊断.
general I/O STATEMENTS
|
|
File handler: a identifier assigned to an open file that is currently being utilized by an operating system
An I/O unit is like a file handle in other programming languages. It’s a unique identifier that’s assigned to a file when you open it.
The first asterisk *
means the input comes from the keyboard in a READ statement and goes to the screen in a WRITE statement. The second asterisk (*) means the free format
or list-directed
: multiple variables in the sequence.
|
|
Format string
Python
|
|
%4.2f
means 4 characters at total, with 2 floating points.
Fortran
|
|
![](images/iShot2022-02-18 18.33.21.png)
Legacy: use ID to label and format statement (i.e. they are separated!)
![](images/iShot2022-02-18 18.33.49.png)
File IO
|
|
writing file
|
|
check existence of file
|
|
Error handling keywords
Each of the read
, write
, open
, close
, inquire
, flush
, and rewind
statements allows passing the iostat
and err
keyword parameters:
iostat = 0 if all good, otherwise positive
err—An integer error label to which the program will jump if the error is encountered
e.g.
|
|
TODO:
parallel fortran
compile and linking