Vous êtes sur la page 1sur 5

Fortran Quick Reference/Cheat Concepts and Elements Order of Statements and Execution

Sheet Sequence
Concept Statements PROGRAM, FUNCTION, SUBROUTINE,
Remember: FORTRAN 77 and below is case sensitive. MODULE or BLOCK DATA statement
Fortran 90 and above is NOT case sensitive. Module Module
Contains USE statements
Private IMPORT statements
Introduction Public FORMAT IMPLICIT NONE
Important things to note are: End Module and PARAMTER State- IMPLICIT Statements
Use ENTRY ments
• Fortran can perform array arithmetic operations. Interface Block Interface Statements PARAMTER and Derived-type Definitions, In-
Module Procedure DATA Statements terface Blocks, Type Decla-
• Spaces are ignored? ration Statements, Statement
End Interface
Function Statements and Spec-
• Fortran is a compiled language which is compiled into Derived data type Derived type
Private ification Statements.
an executable
Sequence DATA Statements Executable Constructs
• Blue text indicates a feature which is available from End Type CONTAINS Statement
Fortran 90 onwards. Subprogram Function Internal Subprograms or Module Subprograms
Subroutine END statement
• Purple text indicates a feature which is available from
Fortran 95 onwards. Entry
Contains Statements Allowed in Scoping Units
• Red text indicates a feature which is available from Return
Fortran 2003 onwards. Input/Output Backspace Scoping Main Module3 Block External Module Internal Interface
Close unit → Prog Data subprog subprog subprog Body
Terminology Endfile
USE Yes Yes Yes Yes Yes Yes Yes
Format
Statement - An instruction which is either executabe or Inquire ENTRY No No No Yes Yes No No
nonexecutable. Open FORMAT Yes No No Yes Yes Yes No
Construct - A sequence of statements ending with a Print DATA Yes Yes Yes Yes Yes Yes No
construct terminal statement. Read CONTAINS Yes Yes No Yes Yes No No
Function - A procedure that returns the value of a single Rewind Derived Yes Yes Yes Yes Yes Yes Yes
variable. Write data type
Procedure - Either a function or subroutine. Intrinsic definition
procedure, external procedure, module procedure, internal Interface Yes Yes No Yes Yes Yes Yes
procedure, dummy procedure or statement function. block
Subroutine - A procedure that is invoked by a CALL Executable Yes No No Yes Yes Yes No
statement or defined assignment statement. It can return more statement
than one argument. Flow Control
Statement Yes No No Yes Yes Yes No
function
Special Characters statement
’ (Apostrophe) Editing, declaring a string Group Statements Misc1 Yes Yes Yes Yes Yes Yes Yes
" (Quotation Declaring a string IF IF
Marks) ELSE IF Notes
* (Asterisk) Comment lines. ELSE
: (Colon) Editing. ENDIF
:: (Double Separator. CASE SELECT CASE 1. Miscellaneous declarations are PARAMETER
Colon) CASE statements, IMPLICIT statements, type declaration
! (Exclamation) inline comment. END SELECT statements, and specification statements such as
/ (Slash) Skip a line in a fmt statment? Do/Do while DO PUBLIC, SAVE, etc.
; (Semicolon) Separates Statement on single source DO WHILE
line. Except when it is in a character END DO 2. Derived type definitions are also scoping units, but they
context, a comment or in line 6. EXIT do not contain any of the above statements, and so have
+ (Plus) Arithmetic operator. CYCLE bot been listed in the table.
& (Ampersand) Line continuation charachter.(Must be WHERE Construct WHERE
in line 7 of fixed format F77. For F90 ELSEWHERE 3. The scoping unit of a module does not include any
can be anywhere after the line. END WHERE module subprograms that the module contains.
Data types PASS REAL, DIMENSION(-2:9,0:5) :: arr b =-1 - A rank two
Declares that the derived data type variable used to array with 12 elements in the first dimension starting at
invoke a bound procedure will be passed to its as its subscript -2. This is different form C where array subscripting
first calling argument. always starts from zero.
Type Declaration Conversion PROCEDURE,PASS::add
INTEGER INT(arg, kind) POINTER
IDINT(arg, kind) Declares that a variable is a pointer. Array Constructors
IFIX(arg, kind) INTEGER,POINTER::ptr
REAL REAL(arg, kind) PRIVATE vector= (/1,2,3,4/)
FLOAT(arg, kind) Declares that an object is private to a module. vector= (/ (M,M=1,10) /) - using an implied do loop.
SNGL(arg, kind) REAL,PRIVATE::internal data array=(/ ((M,M=1,10),N=1,3) /)
CHARACTER INTEGER:: i PROTECTED
CHARACTER(len=10) :: ch Declares that an object in a module is protected,
.... meaning that it can be used but not modified outside
WRITE(ch,*) i the module in which it is defined. Array Selection
COMPLEX CMPLX(x,y, kind) REAL,PROTECTED::x
PUBLIC arr(i,j) Subscript for a single value
Declares that an object is private to a module. arr(i,*) Column i of a two dimensional array.
REAL,PUBLIC::cir=2.54 arr(i:k,j:l) A 2D subarray of columns i to k, rows j to
SAVE l.
Type Declaration Statements Declares that an object is private to a module. arr(i:k:m) A 1D array starting at subscript i and fin-
REAL,SAVE::sum SAVE ishing at subscript k with a stride of m.
TARGET arr1(arr2) The elements of Array2 subscript the ele-
NON OVERRIDABLE Declares that an object is private to a module. ments in Array1.
Declares a bound procedure cannot be overridden in a REAL,TARGET::val1
subclass of this class. VOLATILE
PROCEDURE,NON OVERRIDABLE::pr Declares that a value of a variable might be changed
ALLOCATABLE at any time by some source external to the program. Functions for Determing Array Properties
Declares an array is allocatable. REAL,VOLATILE::vol1
REAL,ALLOCATABLE,DIMENSION(:)::a = -1 ALL(Mask,dim ) Determines if all values are true in Mask
DIMENSION
Derived Data Types along dimension dim.
Declares the rank and and shape of an array. Arrays ANY(Mask.dim ) Determines if any value is true in Mask
REAL,DIMENSION(-7:10,3:10)::matrix = -1 • Arrays can be up to seven dimensions. along dimension dim.
EXTERNAL ALLOCATED(Array) Returns true if array is allocated.
Declares that a name is a function external to a pro- • Fortran 90 allows the use of arithmetic array operations COUNT(Mask,Dim) Returns the number of true elements in
gram unit. without the use of loops. Mask along dimension Dim.
REAL,EXTERNAL::fun1 • Unsubscripted arrays are passed by reference. MINLOC(arr) Returns smallest element of entire array.
INTENT Subscripted arrays are passed by value? MINVAL(arr,dim ) Returns smallest element in dimension
Specifies the intended use of a dummy argument. of array.
• Arrays are stored in column major format. This is not
REAL,INTENT(IN)::ndim MAXLOC(arr ) Returns largest element of entire array.
the same as C which is stored in row major format.
INTRINSIC MAXVAL(arr,dim ) Returns largest element in dimension of
Declares that a name is a specific intrinsic function Terminology array.
REAL,INTRINSIC::sin UBOUND(arr,dim ) a)Returns the upper bound of the sub-
Automatic Arrays -
NOPASS script for the the array b)If the array ar-
Adjustable Arrays
Declares a bound procedure cannot be overridden in a gument is an array selection then result
Assumed-shape Arrays -
subclass of this class. is the number of elements.
Deferred-shape Arrays -
PROCEDURE,NOPASS::add LBOUND(arr,dim) a)Returns the lower bound of the sub-
Allocatable Arrays -
OPTIONAL Array Pointers - script for the the array b)If the array
Declares that a dummey argument is optional. Assumed-size Arrays - argument is an array selection then re-
REAL,OPTIONAL,INTENT(IN)::maxval sult is 1.
NON OVERRIDABLE Declaration SHAPE(arr,dim,mask )Returns a one dimensional integer array.
Declares a bound procedure cannot be overridden in a Explicit Shaped Arrays With each element being the extent of
subclass of this class. the dimensions of the source array.
PROCEDURE,NON OVERRIDABLE::pr Can have the attribute of ALLOCATABLE. SIZE(arr,dim ) Returns the total number of elements in
PARAMETER INTEGER, DIMENSION (10) :: arr a = -1 - A rank one array the array.
Defines named constant.. having ten elements starting at subscript 0? It is good practice SUM(arr,dim,mask ) Calculates the sum of selected elements
REAL,PARAMETER::PI=3.141593 to intailize your array with a value. in this case -1. in the array. Similar to total() in IDL.
Array Manipulation Functions Type Conversion Functions LOG10(X) Returns the logarithm of X to the base of
10.
MATMUL(Matrix 1, Matrix 2)
CSHIFT(Array,shift, Dim ) AIMAG(Z) Imaginary part of a complex number.
Performs mathematical matrix multiplica-
Circular shift on a rank 1 array or rank 1 section of AINT(R,kind ) Returns R truncated to a whole number.
tion of the array arguments.
higher rank arrays. ANINT(R,kind ) Returns the nearest whole number to R.
MAX(A1,A2,A3) Returns the maximum value of A1,A2 etc.
PACK(arr,mask,vec) CEILING(R,kind ) Returns the smallest integer greater than
MIN(A1,A2,A3) Returns the minimun value of A1,A2 etc.
Takes some or all elements from an array and packs R.
MOD(A,P) The remainder of A/P.
them into a one dimensional array, under the control of CMPLX(X,Y)kind ) Returns a complex value as follows. 1)
a mask. If X is complex, then Y must not exist, MODULO(A,P) Returns the modulo of A.
RESHAPE(source arr,shape,pad,order ) and the value of X is returned. 2) If X RANDOM NUMBER(harvest)
Constructs an array of a specified shape from the ele- is not complex, and Y does,nt exst, then Returns psudorandom number(s) from a
ments of a given array. the returned value is (X,0). 3) If X is uniform distribution of 0 to 1. ’harvest’
not complex and Y exists, then returned may be either a scalar or an array.
TRANSPOSE(matrix)
value is (X,Y). RANDOM SEED(size,put,get )
Takes the transpose of a 2d array (i.e matrix) turning
CONJG(Z) Returns the complex conjugate of a com- Performs three functions 1)Restarts the
each column into a row.
plex argument. peudorandom number generator in RAN-
UNPACK(vec,mask,field)
DBLE(A) Converts value of A to double-precision DOM NUMBER 2) Gets information
Takes some or all elements from a one dimensional ar-
real. If A is complex, then only the real about the generator. 3) Puts a new seed
ray and re-arranges them into another, possibly larger
part of A is converted. into the generator.
array.
IBITS(C) ??? SIN(X) Returns the sine of X.
MERGE(Tsource,Fsource,Mask)
INT(A,kind ) Returns a truncated A If A is complex SINH(X) Returns the hyperbolic sine of X.
Merges two arrays based on a logical mask.
then only the real part is converted. SQRT(X) Returns the square root of X.
EOSHIFT(Array, Shift,Boundary,Dim )
LOGICAL(L,kind ) Converts the logical value L to the spec- TAN(X) Returns the tangent of X.
End of shift of a rank 1 array or rank 1 section of a
ified kind. TANH(X) Returns the hyperbolic tangent of X.
higher-rank array.
NINT(R,kind ) Returns the nearest integer to the real
MATMUL(Matrix 1, Matrix 2)
value A.
Kind and Numeric Processor Intrinsic
Performs mathematical matrix multiplication of the ar-
ray arguments. REAL(A, kind) Converts A into a real value. If A is com- Functions
plex, it converts the real part only. BIT SIZE(I) Returns the number of bits in integer I.
PRODUCT(arr,dim,mask)
SIGN(A,B) Returns the value of A with the sign of DIGITS(X) Returns the number of significant digits in
Multiplies together all elements in an entire array, or
B. X in the base of the numbering system.
selected elements from all vectors along a dimension.
Which is in most cases is 2. If you want
SPREAD(source arr,dim,ncopies)
the number of significant decimal digits us
Replicates an array in a additional dimension by making
copies of existing elements along that dimension.
Intrinsic Mathematical Procedures PRECISION(X).
EPSILON(R) Returns a positive number that is almost
TRANSFER(source,mold,size ) ?? ABS(A) Returnspthe absolute value of A. If complex negligible compared to 1.0 of the same type
Returns either a scalar or rank 1 array with a physical
returns real2 + imag 2 and kind as R. R must be a real. Essen-
representation identical to that of SOURCE, but inter-
ACOS(X) Returns the arcosine of X. tially the result is the number that when
preted with type and kind of MOLD. Effectively this
AIMAG(Z) Returns the imaginary part of the complex added to 1.0, produces the next number
function takes the bit patterns of SOURCE and inter-
argument Z. representable by the given KIND of a real
prets them as though they were the type and kind of
ASIN(X) Returns the arcsine of X. number on a particular processor.
MOLD.
ATAN(X) Returns the arctan of X. EXPONENT(X) Returns the exponent of X in the base of
ATAN2(Y,X) Returns the arctan of Y/X in the range of the the computer numbering system.
Miscellanous Array statements −π to π FRACTION(X) Returns the mantissa or fractional part of
COS(X) Returns the cosine of X. the model representation of X.
FORALL (I = 1:N, J = 1:N) H(I, J) = 3.14 COSH(X) Returns the hyperbolic cosine of X. HUGE(X) Returns the largest number of the same
Allows elements of the array to worked on in a parrallel DIM(X,Y) Returns X-Y if > 0, otherwise returns 0. type and kind as X.
processing environment Both X and Y must be of the same type KIND(X) Returns the kind value of X.
name: FORALL (I = 1:N, J = 1:N) and kind. MAXEXPONENT(R)
H(I, J) = 3.14 DOT PRODUCT(Vector 1,Vector 2) Returns the maximium exponent of the
END FORALL Performs the mathematical dot product of same type and kind as R.
the two rank 1 arrays. MINEXPONENT(R)
DPROD(X,Y) Returns the double precision product of X Returns the minimum exponent of the same
Structures/Derived Data Types and Y. type and kind as R.
EXP(X) Returns ex . NEAREST(X,S) Returns the nearest machine-representable
Unlike arrays structures allow different data types to be FLOOR(A,kind ) number different from X in the direction of
packaged together into one entity. They are similar to Returns the largest integer ≤ A. S. The returned value will be of the same
Structures in C and Derived Data types in Fortran. LOG(X) Returns the natural logarithm of X kind as X.
PRECISION(A) Intrinsic Character Functions Input/Output
Returns the number of significant decimal
digits in values of the same type and kind OPEN(unit,file, iostat )
as A. Opens a file for I/O.There are too many options
RADIX(A) Returns the base of the mathematical which this statement has for the space here.
model for the type and kind of I or R. Since READ(unit,fmt, iostat ),var
most modern computers work on a base of Reads a file in a variable. There are too many op-
2. This number will almost certainly be 2. tions which this statement has for the space here.
RANGE(X) Returns the deciaml exponent range for val- WRITE(unit,fmt,iostat ),var
ues of the same type and kind as X. Writes a variable to a file. There are too many op-
RRSPACING(R) Returns the reciprocal of the relative spac- tions which this statement has for the space here.
ACHAR(I,kind ) Returns character in position I of the
ing of the numbers near R. CLOSE(unit,iostat,err,status)
ASCII collating sequence.
SCALE(R,I) Closes a particular file unit.
ADJUSTL(string) Adjust string left, inserting trailing blanks
Returns the value x ∗ bI , where b is the base (Which is FLUSH(unit)
and removing leading blanks.
almost always 2). Flush output buffers to disk.
ADJUSTR(string) Adjust string right, removing trailing
SELECTED CHAR KIND(String) WAIT(unit)
blanks and inserting leading blanks.
Returns the kind number associated with the character Wait for asynchronous I/O to complete.
CHAR(I,Kind ) Returns character in position I of the pro-
input argument. cessor collating sequence associated with
SELECTED INT KIND(I) UNIT=5 for stdin,
the specified kind. and UNIT=6 for stdout
Returns the kind number for the smallest integer kind IACHAR(C) Returns the te position of the character
that can be represent al integers n whose values satisfy argument in the ASCII collating sequence.
the condition ABS(n) < 10 ∗ ∗I. If more than one kind Pointers
ICHAR(C) Returns the position of the character in
satisfies this constraint, then the kind returned will be POINTER Attribute must be used in variable declara-
the processor collating sequence.
the one with the smallest decimal range. If no kind tion.
INDEX(String,Substring,Back ) TARGET Attribute must be used in variable declara-
satisfies the requiremnt, the value -1 is returned.
Locates one substring in another, i.e re- tion.
SELECTED REAL KIND(P,A) turns position of Substring in characters.
Returns the kind number for the smallest real kind that var 1 => var 2 Assigns the pointer from variable 1 to vari-
LEN TRIM(String) Returns the length of a character string able 2.
has a decimal precison of at least P digits and an ex- without any trailing blank characters.
ponent range of a least A powers of 10. If more than ASSOCIATED(var 1)Returns a logical result depending on
LGE(Str a,Str b) Tests whether a string is lexically greater whether the pointer has been associated.
one kind satisfies the the constraint, then the kind re- than or equal to another string, based on
turned will be the one with the smallest decimal preci- NULL(MOLD)?? Returns a disassociated pointer of the same
the ASCII collating sequence. type as MOLD if present. If MOLD is not
sion. If no real kind satisfies the requirement, 1) If the LGT(Str a,Str b) Tests whether a string is lexically greater
requested precision is not available a -1 is returned. 2) present, the pointer type is determined by
than another string, based on the ASCII context. MOLD is a pointer of any type.
If the requested precision is available a -2 is returned. collating sequence.
3) If neither is available a -3 is returned. Both P and Its pointer association status may be un-
LLE(Str a,Str b) Tests whether a string is lexically less than defined, disassociated, or associated. This
A must be integers. or equal to another string, based on the
SET EXPONENT(X,I) function is useful for initializing the status
ASCII collating sequence. of a pointer at the time it is declared.
Returns the number whose fractional part is the part is LLT(Str a,Str b) Tests whether a string is lexically less than
the fractional part of the number, and whose exponent NULLIFY(var 1) Causes pointer to become disassociated. If
another string, based on the ASCII collat- the pointer is not assigned to anything it is
part is I. If X is 0 the the result is 0. X must be real ing sequence.
SPACING() good programming practice to have them
NEW LINE(C) Returns the newline character for the disassociated. Always initialize as pointer
Returns the absolute spacing of the numbers near X KIND of the input character string.
in the model used to represent real numbers. If the iwth NULLIY or with the pointer assigned
REPEAT(Str,n copies) ALLOCATE(var 1) Dynamically provides storage for pointer
absolute spacing is out of range, then this function re- Concatenate several copies of a string.
turns the same value as TINY(X). The result is use- targets and allocatable arrays.
SCAN(Str,Set,Back )
ful for establishing convergence criteria in a processor- Scan a string for any one of the characters
indepenent manner.
Miscellaneous Functions
in a set of characters. Returns the posi-
TINY() tion of the left most character of str that PRESENT(A)??? Returns true if optional argument A is present.
Returns the smallest positive number of the same type is in set.
and kind as X. TRIM(Str,SubStr,back)
Returns the string without any trailing
Debugging techniques
blank characters. 1. Switch on all error testing that can be provided by the
VERIFY(Str,Set,Back ) compiler.
Verify that a set of characters contains all 2. Use interface blocks to trap a very common error which is
the characters in a string. Returns the parameter mismatch between calling and called subroutine.
first character in the string that does NOT 3. Check for mixed-mode arithmetic.
appear in the set. 4. Putting in simple print statements.
Good programming Practise 5. Specify constants with a much precision as your computer comp.lang.fortran - Usenet group.
will support.
1. Use meaningful variable names. 6. Initialize all variables. This card was created using LATEX. Released under the GNU
general public license. $Revision: 0.118 $, $Date: 27/02/2009 $.
2. Use IMPLICIT NONE. 7. Always print the physical units associated with any value. To contact me regarding improvements/mistakes on this sheet or
3. Echo all input values. to download the latest version please follow the links from:
4. Create a data dictionary in each program that you write. Useful Links http://www.BenjaminEvans.net
Including the physical units used. www.fortran.com

Vous aimerez peut-être aussi