next up previous


PHY201 - Worksheet 6, F00


Numerical Calculation of erf(x)- Part V

Arrays and a graphics library

Aleksandar Donev - Dr. Phillip Duxbury

Due Friday October 13th


Physics 201 home

Introduction

In this worksheet you will learn about the use of arrays (vectors, matrices and higher order arrays) in Fortran and also how to use existing Fortran libraries. To a large extent, Fortran remains the workhorse of scientific computing due to its efficient use of arrays and due to the large number of existing scientific computing libraries which are written in Fortran. As mentioned before, the high level of compiler optimisation in Fortran is another important factor.

The task in this worksheet is to reuse the Erf timing code from the previous worksheet. We will now plot the execution time of ErfOfReal, t(x), versus x using a ready made Fortran graphics library called DISLIN and a module that Aleksandar Donev made with easy-to-use plotting routines. This will introduce you to using ready-made libraries of Fortran functions. Since the routines in DISLIN accept arrays and strings as arguments. You thus need to know about simple arrays. Arrays are discussed in sections 1.9 (exclude 1.9.3 for now) and 2.6 (read subsections 1,2,3,4 and 9) in the Fortran manual. You could also read section 1.5 on character strings, though this is not neccessary for this worksheet.

Arrays

In the previous assignment, we had a DO loop (different people used different strategies) which invoked the function ErfOfReal for several values of x, storing the result into a temporary variable erf, printing the result, and then going on to a new iteration. Some people wrote the values to a file and then plotted the results using xmgrace.

Now, assume you really needed to save the results inside the program, rather than writing it to an external file. Arrays are used for this purpose. So we will declare two new allocatable arrays, say x_values and elapsed_time, and store the values of x and the values of t(x) in them. Make these arrays single precision since this is the kind of arrays the graphics library DISLIN accepts:

   REAL(KIND=sp), DIMENSION(:), ALLOCATABLE :: x_values, elapsed_time
   ...
   ALLOCATE(x(n_points),elapsed_time(n_points)) ! Allocate
   ...
   dx=(x_max-x_min)/REAL(n_points-1) ! The step size
   x=x_min ! Initialize
   DO i=1,n_points
     elapsed_time(i)=1E6*ErfTiming(x) ! In us
     x_values(i)=x ! Store x in memory
     x=x+dx ! Increment
   END DO

Now, we have two arrays in memory holding the values xi and t(xi), and we are going to pass them as arguments to plotting routines to vizualize them. Before you try that though, print your arrays to make sure they contain the correct numbers, simply using WRITE:

   WRITE(*,*) ''x   :'',x_values
   WRITE(*,*) ''t(x):'',elapsed_time

Only after this works, go to the next section.

Using external libraries

One of the most important things about Fortran is that there are literally thousands of high-quality libraries of scientific (especially numerical) procedures written and ready-to-be-used in Fortran. Using these libraries is a balance between knowledge and ignorance. A beginner need only know how to use the libraries. In Fortran 90 terms, he needs to know the INTERFACE of each of the routines in the library--is it a FUNCTION or a SUBROUTINE, how many and what type arguments does it accept, what does each argument mean, etc. As you use a library for more and more sophisticated tasks, you need to learn more about its inner functionings. In this worksheet you will go through the first two steps in using a library--learning how to call the routine you need from the provided documentation (in this case HTML), and then actually using the routine with your Fortran compiler (this requires linking libraries).

The SimpleGraphics module

The DISLIN library is a high-level graphics library that has a Fortran 90 interface. However using it is not a trivial task. Therefore, we made a simple module called SimpleGraphics so you can USE it in your program. This module has documentation provided in HTML format at:

http://computation.pa.msu.edu/phy201/SimpleGraphics.html

Read this documentation and in particular take a look at the example TestSimple_2D.f90. You need not worry about the 3D routine SurfPlot at this time. As for any documentation, this document may contain information that you can not understand or need not use at the moment. But it is good excercise to learn how to extract the relevant information from the document.

We will not give an example of using this library in this worksheet. The example in the documentation is enough. Just remember which arrays you need to pass to the routine Plot2D as x and y data points.

Compiling the program

After you write your program that USEs the module SimpleGraphics and its routines, you need to compile it. This process is tricky, so we have made some shortcuts for you.

First, at the Linux command prompt, type:

>  source /classes/phy201/SimpleGraphics.init

which will read in an environmental variable $DISLINvf90 which has all the neccessary commands to link in the appropriate files. Just append $DISLINvf90 at the end of your compilation line, as in:

>  f90-vast erf_plot.f90 -o erf_plot.x erf_series.o erf_timing.o $DISLINvf90

The last worksheet explained why you need to link the .o files. Compiled working programs for both the example TestSimple_2D.f90 and erf_plot.f90 are in our /classes/phy201 directory under the same name but with a .x extension. Try these programs to see how they work. Notice the proper labeling of the axis, the labels and the legends on the plots. Add as many of these ``embelishments'' as you have time to.

About this document ...

This document was generated using the LaTeX2HTML translator Version 98.1p1 release (March 2nd, 1998)

Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 worksheet6_f00.tex.

The translation was initiated by Phil Duxbury on 2000-10-09


next up previous
Phil Duxbury
2000-10-09