Fortran 9095 by xxk47264

VIEWS: 0 PAGES: 24

									Fortran 90/95




A brief description of the Fortran 90/95 language and
its use with the NAG libraries. Installation and use of
Salford FTN95 for Microsoft Windows 95/98/NT and
use of nagf95 on the SUN Solaris Service are included.




December 1999
                                                             CONTENTS

1   The Fortran Programming Language.............................................................................................1
  1.1 Introduction................................................................................................................................. 1
  1.2 Using Old Fortran 77 Programs.................................................................................................. 1
  1.2.1 On the PCs........................................................................................................................... 2
  1.2.2 On the SUNs ............................................................................................................................ 2
  1.3 Creating New Programs.............................................................................................................. 3
  1.3.1 New Programs on the PCs ....................................................................................................... 3
  1.3.2 New Programs on the SUNs .................................................................................................... 3
  1.3.3 General..................................................................................................................................... 3
  1.4 Summary of the New Features.................................................................................................... 4
  1.4.1 New Source Form .................................................................................................................... 4
  1.4.2 New Characters........................................................................................................................ 4
  1.4.3 New Declarations of Type ....................................................................................................... 5
  1.4.4 Pointers & Targets ................................................................................................................... 6
  1.4.5 New Control Structures ........................................................................................................... 7
  1.4.6 Modules ................................................................................................................................... 8
  1.4.7 Dynamic Memory Allocation .................................................................................................. 9
  1.4.8 Vector and Matrix Manipulation ............................................................................................. 9
  1.4.9 New Intrinsic Procedures....................................................................................................... 11
  1.4.10 Subprograms ........................................................................................................................ 13
  1.4.11 New I/O Controls................................................................................................................. 14
  1.4.12 Deprecated Features ......................................................................................................... 15
  1.5 Fortran 95 Changes............................................................................................................... 16
  1.5.1 Fortran 77 features deleted under Fortran 95 ....................................................................16
  1.5.2 Fortran 77 features obsolescent under Fortran 95 .............................................................16
  1.5.3 New features of Fortran 95................................................................................................ 16
2 Using Fortran 95 on the PCs (FTN95) ........................................................................................17
  2.1 Access from Windows.............................................................................................................. 17
  2.1.1 Producing Windows Applications (ClearWin+)....................................................................17
  2.2 Using the NAG or Uniras Libraries .......................................................................................... 18
3 Using Fortran 90/95 on the SUNs (sunf90/nagf95) ....................................................................18
  3.1 Access ....................................................................................................................................... 18
  3.2 Using the NAG Libraries .......................................................................................................... 18
  3.2.1 The Main Numerical Libraries .............................................................................................. 19
  3.2.1.1 The Current Fortran Library ............................................................................................... 19
  3.2.1.2 The New Fortran 90 Library (fl90 - Release 3) .................................................................. 19
  3.2.2 The Graphics Library............................................................................................................. 19
  3.2.3 Getting On-line Manual Help ................................................................................................ 19
Links from here take you to information on the various NAG products available. ........................... 20
Appendix A Installing FTN95 and Plato on your own Computer .....................................................21
  System Requirements ..................................................................................................................... 21
  Installing FTN95............................................................................................................................. 21
Appendix B Installing the NAG Libraries on your own Computer ................................................... 22
                                                                                            Fortran 90/95

  1       The Fortran Programming Language
  1.1 Introduction


  The Fortran 90 Language superseded the previous Fortran 77 standard, this in its turn was
  superseded by Fortran 95 and this is now the current internationally recognised Fortran standard.
  By 2001/2 the next Fortran standard, Fortran 2000, is expected to replace this The change from
  Fortran 77 to Fortran 90 was a major one but its strong feature is its full backward compatibility
  with Fortran 77. The changes between Fortran 90 and 95 are much less dramatic and amount to
  minor enhancements and additions to clarify certain weaknesses in the original Fortran 90 standard.
  This means that a fully standard conforming Fortran 77 program will immediately compile and run
  using a Fortran 90 or 95 compiler. Fortran 95 is available on all public PCs through either the SUN
  Solaris system on the muser service or directly on the PCs (Windows 95 or NT).
  On the SUN system there is a choice of compilers. SUN have their own Fortran 90 compiler
  (sunf90), but we also have available the two NAG compilers nagf90 and nagf95. The NAG Fortran
  90 compiler was the first new Fortran compiler available, their Fortran 95 compiler was considered
  a normal upgrade and so is now the fully supported and recommended Fortran compiler to use on
  the SUN system.
  On the PCs the Salford FTN95 compiler is now the only Fortran compiler available under
  Windows 95, 98 or NT this is a true 32 bit system taking full advantage of the modern machine
  architecture. Included in with this is Plato (a Programming Environment editor) and ClearWin+ for
  extending a normal Fortran program to make it a complete Windows application. Within Plato a
  program can be created and changed and also compiled, linked and run. Projects can be defined
  with several source code files or with one or more libraries attached. ClearWin+ can be used very
  simply to just display program output in a window with a slider (useful if the volume of output is
  greater than a single screen) or with more sophisticated features such as buttons, edit windows, drop
  down lists etc. Included with FTN95 is a powerful windows debugger for detailed analysis of
  problem programs.

  1.2 Using Old Fortran 77 Programs
  It is necessary to differentiate between Fortran 77 style code and the new Fortran 90 free source
  form code. Fortran 77 demands all statements to lie between columns 7 and 72 inclusive, with
  columns 1 to 5 being used for labels and column 6 to indicate that the line is a continuation of the
  previous line. Also comments may lie on a line commencing with either the letter C (upper or lower
  case) or the * symbol in column 1. This can still be the rule, if wished, for Fortran 90; it is known
  as fixed source form code; but it is not recommended since it is now classed as a deprecated
  feature and is unlikely to be available at a future update to the standard. All code using the old
  style fixed source form should therefore be considered as temporary, and if it is likely to be required
  for any appreciable time it is advisable to convert it to free source form code.
  When converting from the fixed source form to the free the following will need to be done:
• The C or * on comment lines will need changing to ! since in Fortran 90 everything on a line after
  this character is treated as comment (wherever ! occurs on a line, unless it is part of a character
  literal).
• All continuation characters in column 6 must be deleted and the symbol & placed at some point
  after the final character of the previous line.

                                                    1                                      December 1999
                                                                                           Fortran 90/95
• The now spurious spaces at the start of each line may be removed, if desired. It is recommended
  that labels have at least a single space after them to assist in clarity.
  Many users will already have working programs that they will wish to continue using with the new
  compiler without performing major modifications. The following simple rules will enable this to be
  achieved.

  1.2.1 On the PCs
  Simply leave the extension as .FOR or change the file extension from .FOR to .F90 or .F95 and
  add the directive /FIXED to the end of the FTN95 command line. So if a Fortran 77 program
  existed in a file PROG.FOR and was previously compiled and run using the command line:-
         FTN77 PROG /LGO
  then in order to perform the identical operation in Fortran 95 the command would be either one of:-
         FTN95 PROG.FOR /LGO
  or, (after the source file has been renamed from PROG.FOR to PROG.F90)
         FTN90 PROG /LGO/FIXED
  where the ordering of the compiler options is unimportant.

  1.2.2 On the SUNs
  The nagf90 or nagf95 compilers are both currently available under Solaris on muser. It is
  likely that the older nagf90 compiler will get dropped in the near future particularly when SUN
  release a Fortran 95 compiler of their own.
  The old Fortran 77 program will be in a file with the extension .f , for example prog.f, and
  would be compiled with the command:-
         f77 prog.f
  producing, by default, an executable program in a file a.out.
  Fortran 95 requires an extension .f95 (or Fortran 90 an extension .f90) for a free source form
  program layout, but will accept a fixed format layout if the file extension is .f. So to compile a
  fixed source format Fortran 77 style program using the Fortran 95 compiler, the command is:
         nagf95 prog.f
  again producing, by default, an executable in a file a.out.
  Alternatively, as on the PCs, there is also a switch -fixed for the commands nagf90 or
  nagf95. So, if the program is in a file with an extension .f90 or .f95, it could still be
  compiled as a fixed source format program. For example if the program is in a file prog.f95, the
  command is:


         nagf95 prog -fixed


  Note that the extension .f90 or .f95 is not required (although it may be included). nagf90 will
  look for a file with the given root name and extension .f90, correspondingly nagf95 looks for
  the file with extension .f95. If the file with .f95 does not exist, nagf95 will then look for a file
  with extension .f90 and finally for a file with extension .f. If more than one of these exist the
                                                    2                                    December 1999
                                                                                           Fortran 90/95
compiler reports an ambiguity and you will need to type the full name of the file (with the
extension).
When using the sunf90 compiler the full file name (with the extension) must always be used.

1.3 Creating New Programs
New programs should always be written in the new free source form.

1.3.1 New Programs on the PCs
Filenames must have an extension .F90 or .F95, thus, in order to compile, link and run a program
in file MY_PROG.F95 the command line would be:
       FTN95 MY_PROG /LGO
Note that the .F90 or .F95 extensions are not required (although they may be included as with the
NAG compiler on the SUNs).
On the PCs the debugger is part of the FTN95 system. It is activated by including the option
/BREAK on the FTN95 command line. The program then compiles, links and then enters the
debugger at the first line of executable code. The source file will be displayed in a window and you
will be able to step through the program in blocks of one or more statements. At any stage it is
possible to display the current values of the objects in the current program unit - right down to the
contents of arrays or sub-objects in a user defined data type.

1.3.2 New Programs on the SUNs
Filenames must have an extension .f90 or .f95. On the SUNs the command to compile and link
is:
       nagf90 my_prog
and then to run (as with f77):
       a.out
Note that as stated earlier, the .f90 or .f95 extensions are not required on the nagf95 command
(although they may be included).
Debugging is available using the command dbx90 command (based on the standard dbx). The
source must first be compiled using the -g90 option on the nagf95 compiler and then dbx90
started running on the a.out file produced. The reason a special system is required for the NAG
compiler is that it first produces intermediate C code and then automatically compiles it. This can
be listed if required by using the -S option (note upper case S). It means that if the standard dbx
command is used all variables and other names will refer to the C version of the program and these
names have been slightly modified (variable names have _ added to them and other names have
similar modifications). So if dbx90 is used all names and line numbers refer back to the original
Fortran source code.
The SUN compiler is fully compatible with the SUN system and so dbx can be used directly or the
SUN Workshop debugger used to display the source code as the program is debugged.

1.3.3 General
The compiler makes several passes of the Fortran 90 code before accepting it and passing on to the
linking or loading stage. This means that it is possible for quite sophisticated errors to be detected

                                                   3                                      December 1999
                                                                                            Fortran 90/95
especially if the program is written using some of the special new features available in Fortran 90
such as Interface blocks. Warning messages may also be given at this stage where possibly
dangerous or dubious techniques are being used in the code. For example, testing two real numbers
for equality in an ‘If’ statement would be flagged with such a warning message. (Non integer
numbers should always be tested so that the difference is compared against a tolerance value using a
relational operator), for example:
       If (abs(a-b) < tolerance) then
       ...
instead of simply,
       If (a == b) then
       ...
Since the compilation is much more thorough than with Fortran 77 run time errors may also be more
helpful and explicit and debugging tools exist to help locate particularly difficult problems or errors
(described in sections 1.3.1 and 1.3.2 above).

1.4 Summary of the New Features
Fortran 90/95 is a major change to the Fortran language and has many new features. This
document is not intended to teach Fortran 90 and only gives a brief outline of some of the major
differences. Some of the details in this section are likely to change in the light of experience.

1.4.1 New Source Form
The new source form no longer restricts the placement of code at any particular point on a line; code
may be placed anywhere between columns 1 and 132. No distinction is made between upper and
lower case letters (except in a character literal) although it is recommended that case be used to
improve the readability of the code. Spaces are generally not significant except in a character literal
and in the middle of certain command words such as CASE DEFAULT where the space is
mandatory. Spaces should not be used within key words.
Long lines are continued by adding the symbol & to the end of the line to be continued. Normally it
would then be continued from the first column of the next line. If it was part of a long character
string, and it was not wished to start at the first column position of the continuation line, a further &
may be placed on the new line and the continuation is then from the character immediate following
the second &.
Comments may be inserted anywhere following the character !, so inline comments are possible to
help with the understanding of the code. Note that comments may also be included in this way at
the end of lines that are being continued (after the & character). Completely blank lines are also
considered to be comment lines, so may be freely used to improve the general layout.
Finally, multiple statements are now allowed on a single line separated by the ; symbol. However it
is recommended that this facility should only be used with short statements in order to reduce the
number of lines of code without decreasing the readability of the program.

1.4.2 New Characters
New characters that are allowed in Fortran 90 are the & and ! mentioned above (1.4.1) and also the
_ (underscore character) and £ (currency symbol). The % sign has a special purpose in derived data
type variables (to extract a particular field) and the ? may also be used. The currency symbol and

                                                    4                                      December 1999
                                                                                           Fortran 90/95
the ? have no special purpose and can simply be used in character strings. Variable names may
contain any alphanumeric value and also the _ symbol making it possible to have long, more
descriptive names, for example:
       Maximum_value = max(10,n)
Other characters that are now allowed are < > and “, where < and > may be used in their
mathematical sense and “ may be used in place of ‘ to denote the limits of a literal character string.

1.4.3 New Declarations of Type
Variables now have a type concept associated with them by extending the idea of real and double
precision to include a kind parameter for each variable. For example, integers may have kind=1 or
kind=2 depending on the maximum sized number that might be expected for the variable. The
precise definition of kind=1 and kind=2 is left to the implementers of the language and on our
machines kind 1 integers have a range -128 to +127, kind 2 integers -32768 to +32767 and
kind 3 integers              -2147483648 to +2147483647. Similar ranges exist for real and
complex variables but with only two kind values in each of those cases.
Associated with the kind concept is the range and precision of a number. So that for example it is
possible to specify a variable K6=SELECTED_INT_KIND(6) to be the appropriate kind value to
give an integer in the range -999999 to +999999 (-1E6 to +1E6). The variable K6 would then
be used whenever reference is made to the number (e.g. -123456_K6). The idea is similarly
extended to include real and complex variables (so making the Double Precision type redundant
although it is retained for the time being).
There is also a kind associated with character variables which is just kind=1 in our case. It is
intended to make Fortran more widely available in non European language areas, such as Japan.
The way data objects are declared and initialised has been greatly extended by allowing an object’s
attributes to be placed together in the declaration statement and also to be initialised at the same
time if it is wished. So for example:
       Integer, parameter :: max_A = 100
       Real, dimension(max_A) :: A, AA, AAA
                       ! defining 3 arrays each of size 100


       Character(len=15), dimension(20) :: names
                 ! defining an array of character strings,
       ! size 20, each element of length 15


       Character(len=9), dimension(7), parameter ::                                   &
                                                                     week_days= &
       (/‘Sunday          ‘,’Monday          ‘,’Tuesday        ‘,’Wednesday’, &
          ’Thursday ‘,’Friday                ‘,’Saturday ’/)
! this gives an array of parameterised names of the days
! of the week note the (/.../) structure for initialising
! an array, note too when it is a character array the
                                                   5                                      December 1999
                                                                                         Fortran 90/95
! character items must all be the same length (at least
! that of the largest individual value)
Another important addition to the language is the ability to construct Derived Data Types or
structures. This is best illustrated with an example:
       Type person
            Character(len=15) first_name(4), last_name, title*3
            Integer age, id
            Real height, weight
            Logical male
       end type person
which would then be used:
       Type(person) you, crowd(50)
Different components of these constructed types may be accessed by the component selector (%).
For example the age of you would be extracted by:
       you%age        ! of type integer
Arrays of derived types may be used so that the average age of crowd could be calculated by:
       average_age = sum(crowd(:n)%age)/n
! where n is the actual number of elements in crowd,
! average_age is an integer and the construction (:n)
! defines the array section 1 to n, sum is an intrinsic
! function that sums the elements of an array, so here
! it would sum all the ages from crowd(1) to crowd(n)
! before dividing by n to compute the average.
The component array first_name(4) would allow up to 4 first names to be defined and would
be referenced:
       you%first_name(1)                     ! for the first name
       you%first_name(2)                     ! for the second first_name
       you%first_name(:n_first)
          ! to get an array of size n_first of first names
When a simple variable is replaced by an array of this type, as with crowd, you would for example
get:
       crowd(6)%first_name(1)
this would reference element 1 of first_name of the 6th member of crowd.

1.4.4 Pointers & Targets
An object can be made to refer to other objects. It is then called a Pointer and must be declared
with a pointer attribute. It provides, amongst other things, a much safer way to perform the

                                                  6                                     December 1999
                                                                                         Fortran 90/95
functionality of the old EQUIVALENCE statement where one variable is associated with another.
Pointers may also point at Targets by using a special assignment statement:
       A => X
       B => Y
X and Y must also be declared as targets in a declaration statement.

1.4.5 New Control Structures
There is now a CASE statement with the following structure:
       SELECT CASE (expression)
       CASE (list 1)
       ...
       CASE (list 2)
       ...
       etc...


       CASE DEFAULT
       ...
       END SELECT
where ‘expression’ is any valid Fortran 90 expression, which may of course be just a simple
variable. ‘list 1’, ‘list 2’ etc. are lists of one or more instances of the possible results of the
‘expression’ and CASE DEFAULT, which is itself optional, will catch the cases not covered
explicitly. For example:
       Select Case (CH)              ! CH of type character
       Case (‘C’,‘D’,‘R’:) ! CH specifically C, D or R to Z
       ! actually R and beyond in the collating sequence
               CH_TYPE = .true.


       Case (‘I’:‘N’)                ! CH I to N
               INT_TYPE = .true.


       Case Default                  ! all other cases
               REAL_TYPE = .true.


       End Select
The DO loop has also been greatly extended. The terminating label is now no longer required and is
replaced by END DO. It is recommended that all DO loops are constructed in this way since it will
greatly reduce the number of labels required in a program. Coupled with this are new EXIT and
CYCLE statements. EXIT enables a current DO loop to be terminated (to the next statement after the
                                                  7                                    December 1999
                                                                                          Fortran 90/95
corresponding or named END DO). CYCLE enables the DO loop to loop again from the beginning
with the next value of the DO loop control variable. In the very simplest of cases it is possible to
have just DO to start a DO loop and then to terminate it internally by means of a test and the EXIT
statement.
A further extension allows a DO WHILE ( relational expression). It is terminated by the
END DO statement and will be continuously repeated until the relational expression becomes false.
This may be useful in some applications, but it is not recommended because it is exactly equivalent
to the two statements:
       DO; If (.NOT. relational expression) EXIT
The DO WHILE statement has been placed in the deprecated list of items that are likely to be
removed in the future, because it produces optimisation penalties in Fortran 90 compilers.

1.4.6 Modules
This is a most important new feature in Fortran 90. It provides a means to collect type declarations
etc. into MODULES which may then be referenced from various program units using the USE
statement. It eliminates the need for COMMON blocks to communicate data values between program
units, but it goes much further than this so that Functions and Subroutines may also be defined as
derived data types (data structures). Variables may be initialised on the type declaration line
(within or outside MODULES) and symbolic constants can also be defined by adding the attribute
PARAMETER so making them generally available through the MODULE. It is possible to define
interface blocks here which define the user interface to program units (for example, whether
routine arguments are for input, output or may be used for either). An example of a MODULE
program unit would be:
       Module definitions
       Integer, parameter :: max_size=100, min_size=10
       Integer, dimension(min_size:max_size) :: A, B ,C
       Real (kind=2), dimension(20) :: X, Y


       Contains
            Subroutine Print_array(Z, N)
            Real Z(min_size:max_size)


         ! Prints title after blank line
            Print ‘(/1x,a)’, &
                ’Array has the value:’
         ! Prints array from min_size to N, 5 values to a line
            Print ‘(1x,5f10.3)’, Z(:N)
            End Subroutine Print_array
       End Module definitions



                                                  8                                     December 1999
                                                                                         Fortran 90/95
       Program Example
       Use definitions


       ...
       Call Print_array(A,20)
       ...
       End
There is also the notion of PUBLIC or PRIVATE entities which can be defined, so that only certain
parts of a MODULE are generally available. This ensures that name clashes can be avoided.

1.4.7 Dynamic Memory Allocation
One of the major problems of Fortran 77 and earlier was its inability to provide dynamic storage, so
that all arrays had to be defined before compilation with their maximum allowable size. This is no
longer necessary with Fortran 90. Arrays can be defined with no specific size and then the required
size can be read in or calculated and the array allocated this size with the ALLOCATE command.
When the array is no longer needed, it can be deallocated with the DEALLOCATE command. For
example:
       Real, dimension(:, :), allocatable :: a
       ...
       Allocate (a(n, 0:n+1))                ! n of type integer
       ...
       Deallocate (a)
A different version of this facility uses automatic dynamic arrays. Sometimes an array passed to a
subroutine may vary from call to call and the size of a work array, for instance, might depend on the
size of the input array. For example:
       Subroutine Swap (A, B)
          Real, dimension(:) :: A,B
          Real, dimension (size(A)) :: work
! intrinsic function ‘size’ provides the size of an array


            work = A          ! this also illustrates how whole
            A = B             ! arrays may be handled in Fortran 90
            B = work
       end Subroutine Swap


1.4.8 Vector and Matrix Manipulation
The above example shows one case of how arrays may be handled in Fortran 90, but there are other
ways in which vector and array manipulation may be performed. Besides straight assignment they
                                                  9                                     December 1999
                                                                                          Fortran 90/95
can be multiplied, divided and acted on by several special array intrinsic functions. To deal with
degenerate general cases, arrays may now also have zero size (which is the same as a scalar or
simple variable). An intrinsic function operating on an array, operates on each element of the array
in turn (but considered as simultaneous) so that an expression such as:
       A = sqrt(A)
for finding the square root of A and placing the result back in A would work equally as well for an
array as for a scalar variable. For an array the square root of each element would be placed back in
the corresponding position in A.
Similarly an expression such as:
       B = 2*A
for A and B arrays of the same shape and size, would multiply each element of A by 2 and place the
result at the corresponding position in B.
However, it must be remembered that an expression such as:
       A = B*C
would not perform a matrix multiplication. Rather, the first element of B would be multiplied by
the first element of C and the resultant product placed in the first element of A, the second element
of B multiplied by the second element of C and the product placed in the second element of A, etc.
If true vector or matrix multiplication is required some intrinsic functions exist to do it.
Dot_product (Vector_A, Vector_B) performs the dot multiplication of two rank 1 same
sized arrays (i.e. vectors) and Matmul (Matrix_A, Matrix_B) performs true matrix
multiplication on the two arrays called Matrix_A and Matrix_B.
Other intrinsic functions exist to find, say, the minimum or maximum array elements, to count the
number of elements that fit a specified criterion (in a logical mask), to find the sum of all the
elements of an array and many other things.
Since an array may be specified as an array section (rather than the whole array), the above intrinsic
functions could equally well find, say, the sum of every other element up to element n, or every
third element, or elements 3, 5, 7, 11, 13, 17 of an array. The following array subobjects or array
sections are permissible:
       A (I,1:N)              ! elements 1 to N of row I
       A (1:M, J)             ! elements 1 to M of column J
       A (I, :)               ! the whole of row I
       A (I, 1:N:3)           ! elements 1, 4, 7 ... of row I


       ... etc.
Vector subscripts can be specified in this way:
       V((/ 1, 7, 3, 2 /)) ! note the 2 layers of brackets
This would define a section of a rank 1 array V with elements V(1), V(7), V(3), V(2), in
this order.
A powerful Where statement exists so that, for instance:
       Where (A > 0.0) A = 1.0/A                             ! A is a real array
                                                  10                                     December 1999
                                                                                           Fortran 90/95
or
       Where (A > B)
            ...
       elsewhere
            ...
       end where
Here each element of the array (A in this case) is checked against the condition set and the operation
performed only for those elements for which the condition is true. When the condition fails the
operation is not performed in the first instance and the second, elsewhere block, would be
performed in the second case.
Finally, a few definitions used in Fortran 90. The number of dimensions of an array is known as
its rank. Thus scalars have rank zero. The number of elements along a dimension is known as its
extent and the sequence of extents is known as its shape. The size of an array is the total number
of elements in the array. For two arrays to conform they must both be the same shape.

1.4.9 New Intrinsic Procedures
There are over 100 intrinsic functions and subroutines available in Fortran 90. Many are enquiry
type functions, for example to determine whether an optional argument is present or not, to give the
parts of a floating point number, or to find the length of a character string. The idea of generic type
functions has been extended so that functions may be array valued as well as scalar. Some
examples of the more useful procedures are:-


Call Date_and_Time (Date=D, Time=T, Zone=Z, Values)
! returns the date, time or time zone in the scalar
! character variables D, T and Z, Values is an integer
! rank 1 array holding all the previous information in
! sequence. All the arguments are optional and may be
! with keywords or positional. Zone gives the difference
! between local time and Co-ordinated Universal Time (UTC)
! i.e. Greenwich Mean Time. E.g. a processor in New York
! in winter would return the value -0500 (5 hours,
! zero minutes behind GMT).


Present (A)
! returns .TRUE. or .FALSE. depending on whether the
! dummy argument A is present in an actual call or not


AdjustL (string)


                                                  11                                      December 1999
                                                              Fortran 90/95
AdjustR (string)
! adjusts string Left or Right by inserting spaces


Trim (string)
! trims all trailing spaces from the end of string


Scan (string, set, back)
! scans string for any character in set, back is
! optional, if it is true it scans from the right


Verify (string, set, back)
! this is the converse of scan and enables an operation
! such as find the next non space character


Huge (X)
! returns the largest value in the number model that
! includes X


MaxVal (array)
! returns the largest value of array


Sum (array, dim, mask)
! returns the sum of the elements of array, dim and mask
! are optional. dim gives the dimension or rank required
! and mask provides a logical filter with which to process
! the array


Lbound (array, dim)
! gives the lower bound of array, dim is optional and may
! specify the dimension it is required for


Size (array, dim)
! gives the total size of array or the extent along
! dimension dim (if present)


                                12                           December 1999
                                                                                           Fortran 90/95
Transpose (matrix)
! returns an array of the rank-two array matrix with the
! elements transposed


MaxLoc (array, mask)
! returns the position of the maximum element in array
! conforming to logical mask if present. The result
! is an array in size equal to the rank of array.


Call Random_Number(harvest)
! returns a pseudorandom number in the range 0 < = x < 1,
! harvest may be real, scalar or real array


Call Random_Seed (size, put, get)
! all arguments may be omitted or one of the three.
! It enables the random number seed to be fetched or
! defined as required.
Before attempting to use any of these procedures, or any others, it is advisable to check the full
specification.

1.4.10 Subprograms
The statement function idea has been extended to enable internal procedures to be defined within
another program unit - either functions or subroutines. The statement CONTAINS is put just before
the END statement and this may then be followed by as many functions or subroutines as required -
these only being available within that program unit. This is equally possible for Modules so
procedures can then be defined in a Module and used within those program units using those
Modules.
Arguments to procedures may be keyword defined or optional, for example:
       ...
       Call one(array1=A, npts=n)
       ! subroutine one called with 2 arguments
       ...
       call one(array1=X, array2=Y, npts=m)
       ! subroutine one called with 3 arguments
       ...
       End
       Subroutine one (npts, array1, array2)

                                                  13                                      December 1999
                                                                                        Fortran 90/95
                      ! note that when called with keywords,
                      ! the argument order is unimportant
       ...
       End Subroutine one
The positional or keyword arguments may be mixed in a particular call, but once a keyword
argument has been used the rest must also be keyword. An intrinsic function exists to test whether a
particular argument has been used or not. For example:
       If (present(array2)) then
       ...
Procedures may now also be recursive i.e. a routine may directly or indirectly call itself. Such
procedures must be defined as RECURSIVE. If the routine is referencing itself directly then the
result is returned by another clause to the FUNCTION statement. For example:
       Recursive Function factorial(n) result(res)
            Integer res, n
            If (n.eq.1) then
               res = 1
            else
                res = n*factorial(n-1)
            end if
       end

1.4.11 New I/O Controls
A greater degree of flexibility is now available with Fortran 90. The specifier ADVANCE=‘no’ can
be added to I/O statement so that a line may be read in stages or a line printed in stages. For
example:
Write (*, *, advance=‘no’) &
    ‘Please give the number of points: ‘
Read *, n
would appear as a single line when the program is run, with the question and reply on the same line.
Besides the ADVANCE specifier there are now also an EOR specifier and a SIZE specifier for
READ statements. This enables part records to be dealt with properly, i.e. a record may be read in
several stages, with SIZE giving the number of characters (bytes) actually read and the label
specified on EOR skipped to if an attempt is made to read past the end of the record.
A few small changes have been made with the FORMAT statement with the addition of binary,
octal or hexadecimal edit descriptors for integer variables (B, O and Z respectively). These forms
of integer constants may also be used elsewhere in a program, e.g. B’01100110’, O’076543’
or Z’10FA’. EN (for engineering) or ES (for scientific) can also be used in formats - both these
are similar to the usual E descriptor but with different positioning of the decimal point.
An annoying restriction in Fortran 77 has been removed in that list directed input/output may now
be applied to internal files. So for example:
                                                 14                                    December 1999
                                                                                          Fortran 90/95
       Read (record, *) A, B, C
       ! where record is of type character
       ! holding the information to be read
The specifiers for the OPEN statement have been extended. STATUS may now also include
REPLACE, POSITION has been added which may take the values ASIS, REWIND or APPEND;
ACTION has been added taking values READ, WRITE or READWRITE. There are also specifiers
DELIM and PAD available. Extensions have also been made to the INQUIRE statement.

1.4.12 Deprecated Features
Deprecated features are those elements still part of the language but now effectively replaced by
superior methods of doing the same thing. No features were removed from the language when
moving from Fortran 77 to Fortran 90/95, but the plan is to remove the deprecated features at a
future revision of the language (Fortran 2000 is soon to be released and other future standards are
already in the preparation process). Users are therefore advised to cease using any of the deprecated
features and to start using the new features replacing them. Briefly, those features which are in this
class, and which may be divided into three groups.
Those linked to storage association:
       EQUIVALENCE statement
       COMMON blocks
       BLOCK DATA
       Shape and character length disagreement (using * to define the length of a dummy array)
       ENTRY statement
Those introduced into Fortran 90/95 because of strong public pressure, but which are really
redundant:
       INCLUDE statement
       DO WHILE loop control
Those which are made redundant by newer features:
       Fixed source form
       DOUBLE PRECISION real
       Computed GO TO
       Character length specification *len
       DATA statement
       Statement functions
       Specific names of intrinsic procedures
       Assumed character length of function results - this has been included in Fortran 90 only for
       compatibility with Fortran 77 and is completely at variance with the philosophy of Fortran
       90/95. It is illustrated by the following example:


               FUNCTION COPY(WORD)

                                                  15                                     December 1999
                                                                                             Fortran 90/95
                      CHARACTER(LEN=*) COPY, WORD
                      COPY = WORD
                 END


Here the assumed length of COPY is that of the argument WORD. The facility may be replaced by
use of a subroutine whose arguments pass back the above function results.

1.5       Fortran 95 Changes
The following changes have been made between Fortran 90 and Fortran 95.
1.5.1 Fortran 77 features deleted under Fortran 95
The following features of Fortran 77 have been deleted under Fortran 95.
      •   Real and Double precision DO-loop variables (use only integer, other types can then be
          generated within the loop as required)
      •   ASSIGN statements and assigned GO TO statements and the use of an assigned integer as a
          format specification (avoid using statement labels as much as possible, using the many block
          structures instead. In-line formats in I/O statements can be used although format statements
          with reference labels are still acceptable)
      •   Branching to an END IF statement (rearrange the logic path)
      •   PAUSE statements (use READ * instead)
      •   H edit descriptor (use quote symbols ' or " around character strings or use other edit
          descriptors such as X or T to produce required spacing)
The suggested alternative methods are available in Fortran 90 and 95 to accomplish these
requirements.
1.5.2 Fortran 77 features obsolescent under Fortran 95
In addition, Fortran 95 has designated the following features as obsolescent which means that they
may be deleted from the language at the next release of the standard (in about the year 2002). The
features should therefore be avoided and replaced in existing programs by the newer more reliable
features of Fortran. They are:
      •   Fixed source form
      •   Computed GO TO statement
      •   CHARACTER* form of CHARACTER specification
      •   DATA statements among executable statements
      •   Statement functions
      •   Assumed character length of function results
Many other features of the old Fortran 77 are now also redundant and should be avoided, but any
good book on Fortran 90 or 95 will explain this in more detail.
1.5.3 New features of Fortran 95
These are best described in a text book, but they include the following areas:

                                                    16                                      December 1999
                                                                                          Fortran 90/95
    •   Improved compliance with IEEE arithmetic to give further help with portability
    •   PURE Procedures for improved optimisation (certain features banned from this type of
        procedure)
    •   ELEMENTAL procedures defined by the programmer
    •   Automatic deallocation of allocatable arrays
    •   The FORALL statement and construct
    •   WHERE construct extensions
    •   Specification functions
    •   Pointer initialisation and the function NULL
    •   Initialisation of components
    •   Elemental functions CEILING, FLOOR and SIGN
    •   Transformational functions (extensions to MINLOC and MAXLOC)
    •   CPU_TIME intrinsic subroutine
    •   Comments in NAMELIST
    •   Minimal field width editing in formats (allowing field widths of zero meaning use the
        minimal field width to express the value)
    •   Generic specification on END INTERFACE
Further information on these Fortran 95 features and the Fortran 90/95 language in general is
available from books in the University shop or the Computer Centre shop and also in the J.B.
Priestley Library.

2       Using Fortran 95 on the PCs (FTN95)
If you are installing the Salford Fortran 95 system on your own PC it can be used directly from a
DOS window, but it is recommended that it is used under Windows using Plato.

2.1 Access from Windows
On a public PC, after logging in to a PC you click on Start/Programs/Program Languages/Fortran 95
and select the Plato icon to start the Plato Programming Environment Editor. If you have installed
FTN95 on your own PC it is likely you have placed it somewhere else - probably
Start/Programs/Salford Software. Remember in this case it is also possible to create an icon on the
desktop which will start Plato directly.

2.1.1 Producing Windows Applications (ClearWin+)
The simplest way of producing a Windows application from an existing program is to include a
single line at the very beginning with the command WINAPP and two integer numbers separated
by a comma. The two numbers provide the stack and heap size respectively to be used for the
program, they are the decimal number of bytes and are normally quite large, say,
WINAPP 100000, 100000
 If either of the numbers are too small you will be informed, but if they are larger than necessary no
indication is given.


                                                  17                                     December 1999
                                                                                           Fortran 90/95


When your program is linked various Windows routines are also included so that, when run, a
conventional window is first opened and the results of your program are displayed in it. The usual
arrows are provided so that output of more than a screen can be scanned through forward or
backwards.
In order to use the ClearWin+ debugger your program must be compiled as a Windows application
and also with /DEBUG (set in File/Options). When this is done the ‘Debug’ icon on the Plato
toolbar can be used to enter the debugger and to run your program in a very controlled way so that
run time errors may easily be found. The current state of variables can be displayed, and
breakpoints set as desired. Probably one of the most useful facilities is to be able to run (execute) to
the current cursor position.

2.2 Using the NAG or Uniras Libraries
The NAG library is available under the Fortran 95 system on the PCs. This is the main NAG
Fortran library as is also available on the SUNs under Unix. The Uniras library is also the same as
that available on the SUNs. The full details of accessing these libraries is given in the document,
"Using the Salford FTN95 Fortran compiler with the NAG and Uniras Libraries".

3      Using Fortran 90/95 on the SUNs (sunf90/nagf95)
The NAG Fortran 90 compiler nagf95 (or nagf90) is available under Solaris 2. We only have
a single licence for the nagf90 compiler at present, we have chosen for this the Solaris server
muser. Whether you are running from a PC, a SUN Workstation or an X terminal the Unix script
(which runs when nagf90 or nagf95 is typed) checks which server you are running on. If it is not
muser the compilation will automatically be performed remotely in order to keep to the license
constraints.

3.1 Access
Create your program with any editor of your choice, but the file name must end in .f95 (or .f90)
if it is to be a free source form Fortran 90 program. If your program is in a file test.f95 it may
then be compiled and linked by:
       nagf95 test
If your program is contained in several files these may all be referenced in the same way with just a
single nagf95 command. Further if some of the source is in the old fixed source format of Fortran
77, it may be mixed with the free source format, but in separate files. So if you had files
test1.f90, test2.f95 and test3.f90 and a Fortran 77 set of routines, say, in a file
mylib.f, these could all be compiled by:
       nagf95 test1 test2 test3 mylib.f
giving an executable in the usual a.out file.
Many of the compiler options for nagf95 are identical to those for the regular f77 command,
others are different. You are advised either to look at the NAG publication ‘NAGWare Compilers
and Tools’ f95 Compiler Rel. 4.0 [NP3409] or to use the man pages with man f95.

3.2 Using the NAG Libraries
Three NAG libraries are available with Fortran 95, two numerical libraries and the NAG Graphics
Library.
                                                  18                                      December 1999
                                                                                         Fortran 90/95



3.2.1 The Main Numerical Libraries
The main NAG Library is the same as is available under f77, but there is now also a specifically
NAG Fortran 90 Library which is only available under Solaris.

3.2.1.1 The Current Fortran Library
Under Solaris simply add $naglib to the end of your nagf95 (or nagf90 or sunf90)
command line, in exactly the same way as is done for f77.

3.2.1.2 The New Fortran 90 Library (fl90 - Release 3)
The NAG Fortran 90 Library is used by adding the directive $nagfl90 to the end of the nagf90
(or nagf90 or sunf90) command line. For example:
       nagf90 nag_lib_support_ex1 $nagfl90
       a.out
would compile the program nag_lib_support_ex1.f90, link it with the nagfl90 Library and
then run it by referencing the executable in the default file name a.out.
There is a utility available called nagexample which copies the specified example program,
compiles, links it with the library and runs it. So using the above example, the same result could be
obtained by:
       nagexample nag_lib_support_ex1

3.2.2 The Graphics Library
The NAG Graphics library is accessed by adding an appropriate parameter to the end of your
nagf95 (or nagf90 or sunf90) command line, depending on what type of final output is
required. For instance if Postscript output is required $nagps would be added. This would mean
that when your program was run, a file would be produced containing the Postscript information.
This would then be directed to whichever Postscript printer you required by typing the appropriate
lpr command yourself, or by using the plt command to perform the same operation but
interactively. If you then produce a similar graph using the same file name, just typing plt would
repeat the previous operation. If you wish to start from the beginning again, just add the parameter
new (plt new). The following devices are available for producing graphical output by using the
appropriate names shown here:
       $nagps                 Adobe Postscript
       $naglpr                Lineprinter
       $nagX                  X Interface (to display a graph directly on the screen)

3.2.3 Getting On-line Manual Help
On the SUN servers there is a NAG TextWare product available called dynatext which is a browser
that can access the whole NAG manual on-line. It is started by typing dtext, but is only usable on a
graphics terminal, such as a SUN Workstation, or through eXceed when connected through a PC.
However there is also a PC version of this and those who choose to install the NAG library on their
own PC will have it available to them. It is not currently available on the public PCs.


                                                 19                                     December 1999
                                                                                      Fortran 90/95
This gives access to the full NAG manual (12 volumes at mark 18), including all the example
programs and data. These can be 'exported' to your own file space and them modified and used as
you wish.
Remember it is frequently better to start with a working program and to modify it to suit your
own requirements.


Other information and help will be found on the Web under:
http://www.brad.ac.uk/cc/documentation/nag

Links from here take you to information on the various NAG products available.




                                                20                                   December 1999
                                                                                          Fortran 90/95
Appendix A
Installing FTN95 and Plato on your own Computer
The hardware requirements for running FTN90 are as follows:

System Requirements
•   PC with an 486 processor or above (Including Pentium, Pentium Pro and Pentium II)
•   Windows 95, Windows 98 or Windows NT
•   15Mb of Hard Disk Space
•   32MB RAM is recommended

Installing FTN95
The installation is normally fully automatic, simply insert the CD and select FTN95 under Install
Software. You are also likely to want to install Plato under the same heading, but this needs to be
done after installing FTN95.
Automatic Installation
1. Insert the Salford Compilers and Tools CD into your CD-ROM drive.
2. If you are running Windows 95 or Windows NT 4.0, the installation process will normally be
   started automatically. If the installation process does not start when the CD is inserted in the
   CD-ROM drive, you will need to run the installation program manually.
3. The installation program will now give you several options including installation of software,
   Browse CD documentation etc.
Manual Installation (Windows 95 and Windows NT)
1. Insert the Salford Compilers and Tools CD into your CD-ROM drive.
2. Run the installation program from the root directory of the CD-ROM drive.
    Click on the START button and select the RUN entry in the menu. Enter the location of your
    CD-ROM drive followed by \setup.exe (e.g. d:\setup.exe) and press the ENTER key.
3. The installation program will now give you several options including installation of software,
   Browse CD documentation etc.




                                                  21                                     December 1999
                                                                                                      Fortran 90/95


Appendix B
Installing the NAG Libraries on your own Computer
Please Note: Before the NAG Library is installed it is essential that the FTN95 system is
installed first.
Load the NAG CD and the system will normally automatically install, if not the setup program
will need to be started manually using the following method:
1. Insert the Salford Compilers and Tools CD into your CD-ROM drive.
2. Run the installation program from the root directory of the CD-ROM drive.
    Click on the START button and select the RUN entry in the menu. Enter the location of your
    CD-ROM drive followed by \setup.exe (e.g. d:\setup.exe) and press the ENTER key.
In either case the setup program will then guide you through a series of questions to install the DVF1
version of the NAG Library.




1
 DVF Digital Visual Fortran, which took over from Microsoft Fortran. This in turn has now been handed over to
Compaq Fortran, so future releases of the NAG Library may use different vendor implementations.
                                                        22                                          December 1999

								
To top