Appendix A A Fortran 9095 primer
Document Sample


48 APPENDIX A. A FORTRAN 90/95 PRIMER
The types of all variables must be declared. Real numbers are written as 2.0 rather than 2 and declared as
DOUBLE PRECISION. In general we discorauge the use of single precision in scientific computing, the
achieved precision is in general not good enough. Comments begin with a ! and can be included anywhere
in the program. Statements are written on lines which may contain up to 132 characters. The asterisks
(*,*) following WRITE represent the default format for output, i.e., the output is e.g., written out on the
screen. Similarly, the READ(*,*) statement means that the program is expecting a line input. Note also the
Appendix A IMPLICIT NONE statement which we strongly recommend the use of. In many Fortran 77 one can see
statements like IMPLICIT REAL*8(a-h,o-z), meaning that all variables beginning with any of the above
letters are by deafult floating numbers. However, such a usage makes it hard to spot eventual errors due
to misspelling of variable names. With IMPLICIT NONE you have to declare all variables and therefore
A Fortran 90/95 primer detect possible errors already while compiling.
A.1.2 DO construct
Fortran 90/95 use a do construct to have the computer execute the same statements more than once. An
A.1 Introduction to F90/F95 example of a do construct follows from example 4 in chapter 2. There we summed ½ Ò up to a given
number, say 1000
Fortran (FORmula TRANslation) was introduced in 1957 and remains the language of choice for most ÈÊÇ Ê Å × Ö ×
scientific programming. The latest standard, Fortran 90 and 95, includes extensions that are familiar to ÁÅÈÄÁ ÁÌ ÆÇÆ
users of C. Some of the most important features of Fortran 90 include recursive subroutines, dynamic ÇÍ Ä ÈÊ ÁËÁÇÆ ×ÙÑ
storage allocation and pointers, user defined data structures, modules, and the ability to manipulate entire ÁÆÌ Ê Ò
arrays.
Fortran 90 is compatible with Fortran 77 and includes syntax that is no longer considered desirable. Fortran ÁÒ Ø Ð Þ Ø ×ÙÑ
90 does however not allow for pointer algebra. This feature is included in Fortran 95 together with other ×ÙÑ ¼º
features such as the FORALL statement. Ç Ò ½¸ ½¼¼¼
×ÙÑ ×ÙÑ · ½º¼» ÄÇ Ì´Òµ
ÏÊÁÌ ´¶¸¶µ Ò¸×ÙÑ
A.1.1 Introduction Æ Ç
In order to get started, consider the following simple Fortran 90 program which sets up Newton’s second
law. Æ ÈÊÇ Ê Å × Ö ×
Note that n is an integer variable. In this case the do statement specifies the first and last values of n; n
ÈÊÇ Ê Å Ò ÛØÓÒ
increases by unity (default). Note here that we wish to avoid a division with by an integer through the use
ÁÅÈÄÁ ÁÌ ÆÇÆ
of FLOAT(n). Moreover, Fortran does not allow floating numbers as loop variables.
ÇÍ Ä ÈÊ ÁËÁÇÆ Ñ ×׸ Ð Ö Ø ÓÒ¸ ÓÖ
ÛÖ Ø ØÓ × Ö Ò Ò × ÓÖ Ø Ñ ×× Ò A.1.3 Logical constructs
ÏÊÁÌ ´¶¸¶µ ³ Ú Ø Ñ ×× Ò ÙÒ Ø× Ó ³ In the next program example, the do loop is exited by satisfying a test.
Ö Ò Ø Ú ÐÙ
Ê ´¶¸¶µ Ñ ×× ÈÊÇ Ê Å × Ö × Ø ×Ø
ÛÖ Ø ØÓ × Ö Ò Ò × ÓÖ Ø Ð Ö Ø ÓÒ Ò Ñ×¹¾ ÁÅÈÄÁ ÁÌ ÆÇÆ
ÏÊÁÌ ´¶¸¶µ ³ Ú Ø Ð Ö Ø ÓÒ³ ÇÍ Ä ÈÊ ÁËÁÇÆ ×ÙѸ Ò ÛØ ÖѸ Ö Ð Ø Ú Ò
Ö Ò Ø Ú ÐÙ ÁÆÌ Ê Ò
Ê ´¶¸¶µ Ð Ö Ø ÓÒ Ò Ø Ð Þ ×ÙѸ Ò ÛØ ÖÑ Ò Ö Ð Ø Ú Ò
ÓÖ Ñ ××¶ Ð Ö Ø ÓÒ ×ÙÑ ¼º Ò ÛØ ÖÑ ¼º Ö Ð Ø Ú Ò ¼º
ÛÖ Ø ØÓ × Ö Ò
ÏÊÁÌ ´¶¸¶µ Ñ ×׸ Ð Ö Ø ÓÒ¸ ÓÖ Ç Ò ½¸ ½¼¼¼
Ò ÛØ ÖÑ ½º¼» ÄÇ Ì´Òµ
Æ ÈÊÇ Ê Å Ò ÛØÓÒ ×ÙÑ ×ÙÑ · Ò ÛØ ÖÑ
Ö Ð Ø Ú Ò Ò ÛØ ÖÑ»×ÙÑ
The first statement must be a program statement; the last statement must have a corresponding end program Á ´ Ö Ð Ø Ú Ò ¼º¼¼¼¼½ µ ÁÌ
statement. Æ Ç
Integer numerical variables and floating point numerical variables are distinguished. The names of all ÏÊÁÌ ´¶¸¶µ Ò¸ ×ÙѸ Ö Ð Ø Ú Ò
variables must be between 1 and 31 alphanumeric characters of which the first must be a letter and the last
must not be an underscore. Æ ÈÊÇ Ê Å × Ö × Ø ×Ø
47
A.1. INTRODUCTION TO F90/F95 49 50 APPENDIX A. A FORTRAN 90/95 PRIMER
The features included in the above program include: Ç ¾¸ÒÙÑ Ö Ø ÖÑ×½
A do construct can be exited by using the EXIT statement. The IF construct allows the execution of a ÔÖ ×× ÔÖ ××·´ ÄÇ Ì´ ¹½µ»¿º¼¶ÔÓÐÝÒÓÑ Ø ÖÑ×½´ µ¶´Ü¶¶´ ÄÇ Ì´ ¹½µ»¿ºµµµ
sequence of statements (a block) to depend on a condition. The if construct is a compound statement and Æ Ç
begins with IF ... THEN and ends with ENDIF. Examples of more general IF constructs using ELSE and
ELSEIF statements are given in the program library or in the main text. Another feature to observe is the Æ ÍÆ ÌÁÇÆ ÔÖ ××
CYCLE command, which allows the loop variable n to start at a new value. As a rule of thumb, if possible,
you should avoid IF statements or calls to other functions if you operate on arrays inside loops. This may Æ ÅÇ ÍÄ Ó×
reduce the effect of optimizations gained through various compiler options.
INTENT(IN) means that the dummy argument cannot be changed within the subprogram. INTENT(OUT)
means that the dummy argument cannot be used within the subprogram until it is given a value with
A.1.4 Subprograms the intent of passing a value back to the calling program. The statement INTENT(INOUT) means that
the dummy argument has an initial value which is changed and passed back to the calling program. We
Subprograms are called from the main program or other subprograms. Subprograms (subroutines and recommend that you use these options when calling subprograms
functions) can be included in modules. The form of a module, subroutine, and a function is similar to The module(s) can be included in a separate file.
that of a main program. A module is accessed in the main program by the use statement. Subroutines are
invoked in the main program by using the call statement. A subprogram always has access to other entities
in the module. The subprograms in a module are preceded by a contains statement. A.1.5 Arrays
Variables and subprograms may be declared public in a module and be available to the main program (and An array is declared in the declaration section of a program, module, or procedure using the dimension
other modules). An example follows here. attribute. Examples include
ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÅ ÆËÁÇÆ ´½¼µ ܸÝ
Ì × ÑÓ ÙÐ ÓÒØ Ò× Ø Ô Ö Ñ ØÖ Þ Ø ÓÒ Ó Ø ÇË × Ê Ä¸ ÁÅ ÆËÁÇÆ ´½ ½¼µ ܸÝ
ÔÓÐÝÒÓÑ Ð Ò Ò× ØÝº Ì ÒÙÑ Ö Ó Ø ÖÑ× ÔØ Ò Ø ÁÆÌ ʸ ÁÅ ÆËÁÇÆ ´¹½¼ ½¼µ ÔÖÓ
ÔÓÐÝÒÓÑ Ð ÜÔ Ò× ÓÒ × Ú Ò Ý ÒÙÑ Ö Ø ÖÑ׺ ÁÆÌ ʸ ÁÅ ÆËÁÇÆ ´½¼¸½¼µ ×Ô Ò
ÅÇ ÍÄ Ó× The default value of the lower bound of an array is 1. For this reason the first two statements are equivalent
ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÄÄÇ Ì Ä ¸ ÁÅ ÆËÁÇÆ´ µ¸ ÈÍ ÄÁ ÔÓÐÝÒÓÑ Ø ÖÑ× to the first. The lower bound of an array can be negative. The last statement is an example of two-
ÁÆÌ ʸ ÈÍ ÄÁ ÒÙÑ Ö Ø ÖÑ× dimensional array.
Rather than assigning each array element explicitly, we can use an array constructor to give an array a set
ÇÆÌ ÁÆË of values. An array constructor is a one-dimensional a list of values, separated by commas, and delimited
by "(/" and "/)". An example is
Ö Ó Ò Ö Ý Ô Ö Ô ÖØ Ð Ò ÙÒ Ø× Ó Å Î» Ñ ¿ ´½ ¿µ ´» ¾º¼¸ ¹¿º¼¸ ¹ º¼ »µ
Ö Ó ×ÙÑ ß ½ ßÒÙÑ Ö Ó ÔÓÐÝÒÓÑ× ¶ Ò× ØÝ ß´ ¹½µ»¿
is equivalent to the separate assignments
ÇÍ Ä ÈÊ ÁËÁÇÆ ÍÆ ÌÁÇÆ Ö Ó´Üµ
´½µ ¾º¼
ÁÅÈÄÁ ÁÌ ÆÇÆ
´¾µ ¹¿º¼
ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÆÌ ÆÌ´ÁƵ Ü
´¿µ ¹ º¼
ÁÆÌ Ê
Ö Ó ÔÓÐÝÒÓÑ Ø ÖÑ×½´½µ A.1.6 Allocate statement and mathematical operations on arrays
Ç ¾¸ÒÙÑ Ö Ø ÖÑ×½ One of the better features of Fortran 90 is dynamic storage allocation. That is, the size of an array can
Ö Ó Ö Ó·ÔÓÐÝÒÓÑ Ø ÖÑ×½´ µ¶´Ü¶¶´ ÄÇ Ì´ ¹½µ»¿º ¼µµ
¢
be changed during the execution of the program. To see how the dynamic allocation works in Fortran 90,
Æ Ç consider the following simple example where we set up a unity matrix.
Æ ÍÆ ÌÁÇÆ Ö Ó ºººººº
ÁÅÈÄÁ ÁÌ ÆÇÆ
ÔÖ ××ÙÖ Ò ÙÒ Ø× Ó Å Î» Ñ ¿ Ì Ò Ø ÓÒ Ó Ø Ñ ØÖ ܸ Ù× Ò ÝÒ Ñ ÐÐÓ Ø ÓÒ
ÔÖ ××ÙÖ Ò× ØÝ¶ Ö Ó» Ò× ØÝ ¹ Ö Ó ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÄÄÇ Ì Ä ¸ ÁÅ ÆËÁÇÆ´ ¸ µ ÙÒ ØÝ
Ì × Þ Ó Ø Ñ ØÖ Ü
ÇÍ Ä ÈÊ ÁËÁÇÆ ÍÆ ÌÁÇÆ ÔÖ ×״ܵ ÁÆÌ Ê Ò
ÁÅÈÄÁ ÁÌ ÆÇÆ À Ö Û × Ø Ø Ñ Ò
ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÆÌ ÆÌ´ÁƵ Ü Ò
ÁÆÌ Ê ÐÐÓ Ø ÒÓÛ ÔÐ Ò Ñ ÑÓÖÝ ÓÖ Ø Ñ ØÖ Ü
ÄÄÇ Ì ´ ÙÒ ØÝ´Ò¸Òµ µ
ÔÖ ×× ¹Ö Ӵܵ ÐÐ Ð Ñ ÒØ× Ö × Ø ÕÙ Ð Þ ÖÓ
A.1. INTRODUCTION TO F90/F95 51 52 APPENDIX A. A FORTRAN 90/95 PRIMER
ÙÒ ØÝ ¼º where the MASK argument prevents that the diagonal terms are included. Another way of writing the
× ØÙÔ ÒØ ØÝ Ñ ØÖ Ü above MASK statement is through teh WHERE statement. Consider a division of all matrix elements of a
Ç ½¸Ò matrix B with a matrix C, i.e., we have
ÙÒ ØÝ´ ¸ µ ½º
Æ Ç
ÄÄÇ Ì ´ ÙÒ ØÝµ meaning that we are performing a division for i and j of . If we wish to avoid division by zero we
ººººººº could write the above equation as
We always recommend to use to deallocation statement, since this frees space in memory. If the matrix is
ÏÀ Ê ´ » ¼µ »
transferred to a function from a calling program, one can transfer the dimensionality Ò of that matrix with and that’s all which is needed. The statement inside WHERE checks all matrix elements of the matrix C .
the call. Another possibility is to determine the dimensionality with the SIZE command, i.e., Other useful functions are the dot products of two vectors, i.e.,
Ò ËÁ ´ÙÒ ØÝ¸ ÁÅ ½µ ÇÌ ÈÊÇ Í Ì ´ ¸ µ
will give the size of the rows, while using DIM=2 gives that of the columns.
which means ´½µ ´½µ · ´¾µ ´¾µ · · ´Òµ ´Òµ. This could also have been written as
Other useful Fortran 90 intrisic functions are given by the following examples. Suppose we need to find the ËÍÅ ´ ¶ µ
maximum absolute value of of the column elements in a two-dimensional matrix. In Fortran 77 we would Here’s an example of a module for matrix operations which employs the module mesh_variables through
have to code something like the USE statement.
ÐÓÓÔ ÓÚ Ö ÖÓÛ× ÅÇ ÍÄ Ñ ØÖ Ü Ñ Ò ÔÙÐ Ø ÓÒ×
Ç ½¸Ò
Ñ Ü Ú ÐÙ ¼º ÍË Ñ × Ú Ö Ð ×
Ø ÐÓÓÔ ÓÚ Ö ÓÐÙÑÒ× ÁÅÈÄÁ ÁÌ ÆÇÆ
Ç ½¸Ò
Á ´ Ë´ ´ ¸ µµ Ñ Ü Ú ÐÙ µ Ñ Ü Ú ÐÙ Ë´ ´ ¸ µµ ÇÆÌ ÁÆË
Æ Ç
Ø Ò Û ×ØÓÖ Ø × ÒÙÑ Ö Ò ÓÒ ¹ Ñ Ò× ÓÒ Ð Ú ØÓÖ Ô Ö ÓÖÑ Ø ÓÙØ Ö ÔÖÓ Ù Ø Ó ØÛÓ Ú ØÓÖ× Ò
Ñ Ü Ú ÐÙ ÖÓÛ´ µ Ñ Ü Ú ÐÙ
Æ Ç ÍÆ ÌÁÇÆ ÓÙØ Ö ÔÖÓ Ù Ø´ ¸ µ
In Fortran 90 this statement is replaced by one line ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÆÌ ÆÌ´ÁƵ ¸ ÁÅ ÆËÁÇÆ´ µ ¸
Ñ Ü Ú ÐÙ ÖÓÛ Å Î Ä´ Ë´ µ¸ ÁÅ ¾µ ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÅ ÆËÁÇÆ´ËÁ ´ µ¸ ËÁ ´ µµ ÓÙØ Ö ÔÖÓ Ù Ø
where DIM=2 tells that we are searching among columns. It is understood that all rows are evaluated ÓÙØ Ö ÔÖÓ Ù Ø ËÈÊ ´ ¸ ÁÅ ¾¸ Æ ÇÈÁ Ë ËÁ ´ µ µ ¶ ²
simultaneously. ËÈÊ ´ ¸ ÁÅ ½¸ Æ ÇÈÁ Ë ËÁ ´ µ µ
As another example, suppose we need to evaluate
Ò Æ ÍÆ ÌÁÇÆ ÓÙØ Ö ÔÖÓ Ù Ø
Û Ü ·Ü
½ ÑÙÐØ ÔÐÝ Ñ ØÖ Ü Û Ø Ú ØÓÖ
which in Fortran 77 would involve a sum over i and j can be written in Fortran 90 using the intrisic function ÍÆ ÌÁÇÆ Ñ ØÖ Ü Ú ØÓÖ ÑÙÐØ´ ¸ µ
SUM
Ç ½¸Ò ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÆÌ ÆÌ´ÁƵ ¸ ÁÅ ÆËÁÇÆ´ µ
Û´ µ ËÍÅ´ Ë´Ü´ µ·Üµ ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÅ ÆËÁÇÆ´ ¸ µ¸ ÁÆÌ ÆÌ´ÁƵ
Æ Ç ÇÍ Ä ÈÊ ÁËÁÇÆ¸ ÁÅ ÆËÁÇÆ´ËÁ ´ µ µ Ñ ØÖ Ü Ú ØÓÖ ÑÙÐØ
ÁÆÌ Ê
Similarly, the product Ç ½¸ ËÁ ´ µ
Ò Ñ ØÖ Ü Ú ØÓÖ ÑÙÐØ´ µ ËÍÅ´ ´ ¸ µ¶ ´ µµ
Û ´Ü Ü µ Æ Ç
½
Æ ÍÆ ÌÁÇÆ Ñ ØÖ Ü Ú ØÓÖ ÑÙÐØ
can be coded by aid of the PRODUCT function
Ç ½¸Ò Æ ÅÇ ÍÄ Ñ ØÖ Ü Ñ Ò ÔÙÐ Ø ÓÒ×
Û´ µ ÈÊÇ Í Ì´Ü´ µ¹Ü¸ Å Ëà ´Ü » Ü´ µµ µ For more functions, we recommend that you consult a F90 manual or see the link to F90 lectures on the
Æ Ç web page.
A.1. INTRODUCTION TO F90/F95 53 54 APPENDIX A. A FORTRAN 90/95 PRIMER
A.1.7 Complex variables Æ ÍÆ ÌÁÇÆ Ø ÓÒ
Fortran 90 is uniquely suited to handle complex variables through variable definitions like COMPLEX and
ÍÆ ÌÁÇÆ ×Ù ØÖ Ø ÓÒ´ ¸ µ
functions like AIMAG.
ÈÊÇ Ê Å ÓÑÔÐ Ü Ü ÑÔÐ Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ¸ ÁÆÌ ÆÌ´ÁƵ ¸
ÁÅÈÄÁ ÁÌ ÆÇÆ Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ ×Ù ØÖ Ø ÓÒ
ÇÍ Ä ÈÊ ÁËÁÇÆ¸ Ô Ö Ñ Ø Ö Ô ¿º½ ½ ¾
ÇÅÈÄ ¸ ר Ö¸ ¸ Ö ¸ ×Ù ØÖ Ø ÓÒ ²
ÇÍ Ä ÈÊ ÁËÁÇÆ ÓÑÔÐ Ü Ú Ö Ð ´ ±Ö Ð Ô ÖØ¹ ±Ö Ð Ô ÖØ¸ ± ÓÑÔÐ Ü Ô ÖØ¹ ± ÓÑÔÐ Ü Ô ÖØ
ÁÆÌ Ê
Æ ÍÆ ÌÁÇÆ ×Ù ØÖ Ø ÓÒ
ÓÑÔÐ Ü ÓÒר ÒØ × ÛÖ ØØ Ò × ØÛÓ Ö Ð ÒÙÑ Ö׸ × Ô Ö Ø Ý
ÓÑÑ Ò Ò ÐÓ× Ò Ô Ö ÒØ × ×º ÍÆ ÌÁÇÆ ÑÙÐØ ÔÐ Ø ÓÒ´ ¸ µ
´¾º ¼¸¹¿º ¼µ Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ¸ ÁÆÌ ÆÌ´ÁƵ ¸
Á ÓÒ Ó Ô ÖØ × Ò ¸ Ø ÓØ Ö Ô ÖØ ÑÙר Ú × Ñ Ò Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ ÑÙÐØ ÔÐ Ø ÓÒ
´¼º ¼¸¼º ¼µ
ÏÊÁÌ ´¶¸¶µ ³ ¸ ÑÙÐØ ÔÐ Ø ÓÒ±Ö Ð Ô ÖØ ²
ÏÊÁÌ ´¶¸¶µ ³ ¶ ³¸ ¶ ±Ö Ð Ô ÖØ¶ ±Ö Ð Ô ÖØ¹ ± ÓÑÔÐ Ü Ô ÖØ¶ ± ÓÑÔÐ Ü Ô ÖØ
ÏÊÁÌ ´¶¸¶µ ³ ³¸
ÏÊÁÌ ´¶¸¶µ ³ ¶ ³¸ ¶ ÑÙÐØ ÔÐ Ø ÓÒ± ÓÑÔÐ Ü Ô ÖØ ²
Ö Ð Ô ÖØ Ó ±Ö Ð Ô ÖØ¶ ± ÓÑÔÐ Ü Ô ÖØ· ± ÓÑÔÐ Ü Ô ÖØ¶ ±Ö Ð Ô ÖØ
Ê Ä´ µ
ÏÊÁÌ ´¶¸¶µ ³Ö Ð Ô ÖØ Ó ³¸ Æ ÍÆ ÌÁÇÆ ÑÙÐØ ÔÐ Ø ÓÒ
Ñ Ò ÖÝ Ô ÖØ Ó
Ñ ´ µ Æ ÅÇ ÍÄ ÓÑÔÐ Ü ÓÔ Ö Ø ÓÒ×
ÏÊÁÌ ´¶¸¶µ ³ Ñ Ò ÖÝ Ô ÖØ Ó ³¸
Ö ÅÈÄ ´¼º¼ ¼¸Ô µ
È´ Ö µ
ÓÑÔÐ Ü ÓÒ Ù Ø Ó
ר Ö ÇÆÂ ´ µ
×ÓÐÙØ Ú ÐÙ Ó
Ë´ µ
ÏÊÁÌ ´¶¸¶µ ³ÔÖÓÔ ÖØ × Ó ³¸ ¸ ר Ö¸ ¶ ר Ö¸
Æ ÈÊÇ Ê Å ÓÑÔÐ Ü Ü ÑÔÐ
As another example, you could define your own complex operations through the following example.
ÅÇ ÍÄ ÓÑÔÐ Ü ÓÔ Ö Ø ÓÒ×
ÁÅÈÄÁ ÁÌ ÆÇÆ
Ì È ÓÑÔÐ Ü Ú Ö Ð
ÇÍ Ä ÈÊ ÁËÁÇÆ Ö Ð Ô ÖØ¸ ÓÑÔÐ Ü Ô ÖØ
Æ Ì È ÓÑÔÐ Ü Ú Ö Ð
ÇÆÌ ÁÆË
ÍÆ ÌÁÇÆ Ø ÓÒ´ ¸ µ
Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ¸ ÁÆÌ ÆÌ´ÁƵ ¸
Ì È ´ ÓÑÔÐ Ü Ú Ö Ð µ Ø ÓÒ
Ø ÓÒ ²
ÓÑÔÐ Ü Ú Ö Ð ´ ±Ö Ð Ô ÖØ· ±Ö Ð Ô ÖØ¸ ± ÓÑÔÐ Ü Ô ÖØ· ± ÓÑÔÐ Ü Ô ÖØ µ
Related docs
Get documents about "