Self-Study Guide 2: Programming in Fortran 95 by Dr. Rachael Padman - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

8 Use of numerical libraries: NAG

There are many libraries of subroutines performing mathematical and graphics operations. The library you are encouraged to use here is the NAG library, which has an on-line documentation system. One important feature of the NAG library in particular is that all the variables are of double precision type, and all arguments must be declared as such when NAG routines are called. The on-line information is available via the link on the course web page.

When using the NAG library you must tell the linker where to find the library code.  This is done by adding some extra information to the f95 command line; e.g. let’s imagine we were compiling and linking (to form an executable program) a simple program in the file ex.f90. The command we need is:

f95 -o ex ex.f90 -lnag

The -lnag tells the linker to search the NAG library file for the code that it needs.

8.1 A simple NAG example

  • Enter this simple program and try compiling and running it; it should report some information about the NAG library we are going to use

img42.png

8.2 A non-trivial NAG example: matrix determinant

Use the link on the course web page to bring up the NAG documentation. Find the documentation for the routine F03AAF.

The first page of the documentation appears slightly cryptic:

img43.png

The note at the beginning draws attention to the bold italicised term real, which you should simply interpret as “double precision” or real(kind(1.0d0)) in all NAG documentation.

One would expect a subroutine which calculates a determinant to need arguments including the array in which the matrix is stored, the size of the matrix, and a variable into which to place the answer. Here there are just a couple extra.

  • Because the library is written in Fortran 77 (F77, an earlier version of Fortran than we are using), which does not support dynamic memory allocation, it is often necessary to pass “workspace” to a routine. The routine will use this space for its own internal temporary requirements. Hence WKSPCE(*).
  • Array dimensions of unknown size are denoted by a ‘*’, not a ‘:’.
  • Most NAG routines have the ifail argument which is used to control error handling and communicate error information back to the calling routine. In general you should set ifail = 0 before calling a NAG routine, and test it on return (zero indicates success).

img44.png

Further examples of F95 programs calling the NAG library can be found in the lecture handout on the Physics of Computational Physics.