Using Fortran 90 and CTT's FNL Numerical Library

When you are unable to find a suitable Fortran 90 CTT module, you will have to use the earlier IMSL Fortran 77 routines, now known as the Fortran Numerical Library (FNL).

The VNI-supplied Numerical_Libraries module provides some features only available with modules (such as parameter type-checking). Therefore, when programming in Fortran 90 with FNL routines, you should add a

use Numerical_Libraries

statement to your Fortran 90 program.

The following example calculates the product of gamma(a) and gamma(1-a) by two methods and compares their results. The first method uses the gamma function from the FNL library. The second method uses standard Fortran functions and the "Reflection Formula":

The output from a sample terminal session is shown below, followed by a discussion of the steps. This example is run using the the Sun Studio 11, and the f90 source file is in gammaTest.f90. The cttshell alias and environment variables used below are available if you have made the suggested UNIX configuration changes described in Getting Started with CTT: Setting Up Your UNIX Account.

setenv commands in setup files
CTT_DIR /opt/vni/CTT6.1
<100>% cttshell 
<1>% touch gammaTest.f90 
<2>% make gammaTest 
f90 -openmp=noopt -ftrap=%none -xtarget=generic
-M/opt/vni/CTT6.1/include/solaris -o gammaTest gammaTest.f90
-R/opt/vni/CTT6.1/lib/lib.solaris -L/opt/vni/CTT6.1/lib/lib.solaris
-Bdynamic -limsl -limslsmp -xlic_lib=sunperf -ldl -lnsl -lsocket
<3>% ./gammaTest 
                   a= 1.255 
            gamma(a)= 0.9053857662433382 
          gamma(1-a)= -4.8318715357682604 
 gamma(a)*gamma(1-a)= -4.374707712800922 
        pi/sin(pi*a)= -4.374707712800924 
<4>% exit
  • The cttshell command starts a new shell with the correct environment for all CTT products. The command could also be followed by a 4-digit UNIX group project ID if desired.
  • The sample Fortran 90 program that invokes the gamma function is stored in a file named gammaTest.f90 (whose listing is below).
  • The make gammaTest command finds the Fortran 90 source file and creates the executable gammaTest. Since the source file has an ".f90" extension, make uses a suffix rule to invoke an f90 compiler. (The specific compiler used is the one defined by the environment variable F90, which has been set to the Sun Studio 11 f90 compiler as part of the configuration setup.)
  • The Fortran 90 source program is touched so that its modification time is newer than the executable. That forces the make command to recompile gammaTest.f90.
  • The executable is run and it reports the results. The last two lines of output should (and do) have approximately identical values.
  • Finally, exiting the shell returns us to the parent shell (command number <101>) with its previous environment variable and alias definitions.

Listing of the gammaTest.f90 sample program

program gammaTest 
    use Numerical_Libraries 
    implicit none
    integer, parameter :: F=selected_real_kind(12,100) 

    real(kind=F) :: a = 1.255_F 
    real(kind=F) :: pi = 3.1415926535897932384626433832795_F 

! Write some results. Use gamma or dgamma to go with kind=F 
    write(*,*) '                 a=', a
    write(*,*) '          gamma(a)=', dgamma(a)
    write(*,*) '        gamma(1-a)=', dgamma(1-a) 

! These last two results should be the same
    write(*,*) 'gamma(a)*gamma(1-a)=', dgamma(a)*dgamma(1-a) 
    write(*,*) '       pi/sin(pi*a)=', pi/sin(pi*a)