Aleksandar Donev - Dr. Phillip Duxbury
In this worksheet you will learn about file I/O (input and output) in Fortran 77 and Fortran 90. Scientific computing tasks often produce huge amounts of data that needs to be stored on disk and later used for analysis, such as plotting. In this worksheet you will learn how to read and write data from and to files rather than from and to the screen, which is the default. In FORTRAN (both 77 and 90) this is carried out using the OPEN (and CLOSE once you are done) command to assign a datastream that a program produces or requires to a file. To understand how this works, read sections 1.6.4 and 2.3 of the fortran manual. Your first task is to modify the main program part of your code from last week to write data to a file. Once you have done this, you will use a simple GUI (graphical user interface) program xmgrace for data visualization.
Your assignment this time is:
(i) Firstly to modify the program that
you developed previously so that
it writes to a file the values of the function
Erf(x) on the interval [x_min, x_max].
(You may also choose to modify the solution
we provide for last week's assignment.
Here is the more general solution
which contains the INTERFACE which
enables both REAL and COMPLEX arguments x.)
Design your program so that the user enters the precision
and the number of points on the interval (n_points),
as well as x_min and x_max.
Here is an example of writing to a file called ErfSeries.dat.
OPEN(UNIT=10,FILE="ErfSeries.dat",STATUS="OLD") WRITE(UNIT=10, FMT=*) x, erf CLOSE(Unit=10)
In this example the file ErfSeries.dat already exists.
Modify the DO loop in the main program to run over the n_points values of x which you should use, for example.
DO i=1, n_points x= ... ! Calculate x here erf=ErfSeries(x) ! Call your function WRITE(...) x, erf END DO
(ii) Secondly, visualize (plot) the
error function by using the program xmgrace:
> xmgrace your_output_file.dat &
The .dat extension is standard for output
data files. The program is menu driven and pretty
self-explanatory, so
you should be able to make your plot look nice. Make sure
the axes are labeled properly. You should also give your
plot a title before you print it.
A question of precision and convergence
Note that the series that we use ceases to converge, in single precision, for x>3.0. You need to change the cutoff criterion in the MODULE Erf_Series from x<5.0 to x<3.0. Even Mathematica has trouble evaluating this series near x = 3.2, to high precision! In double precision it can handle larger values of x though it takes many terms in the series. Actually for most practical purposes erf(x) is one for x greater than 3.
You may have noticed that if you tried to set wp=dp and print the results, they still print with 7-8 digits. This is because in g77 an explicit format descriptor (other than FMT=*) is needed to print in double precision. See section 1.6.3 in the manual for details. For example, FMT=``(D21.15)'' can be used in this case.
There are two executables for you to use and play with:
erf_calculator_real.x and
erf_calculator_complex.x
These files, and all files we give you, will be in the directory /classes/phy201/. These programs will enable you to compare the value found by the series expansion we have been using in our worksheets with the ``correct'' result found from the executables above (Fortran 90 programs by Alan Miller, which will be made available with other sources later).
Finally, an interesting facility in Unix is the utility script which records all of the commands you send from the screen and all of the data which returns to your screen e.g.:
> script erf_calculator.txt > /classes/phy201/erf_calculator_real.x ... Play with a few numbers here ... > exitThis would write to the file erf_calculator.txt all of the screen information you generate, until you end the script by entering exit (be careful not to generate enormous garbage files this way!)
Now if you want to edit it or just look at it you could do the following just like for any other text file:
> gedit erf_calculator.txt & ... Look at the session file and then print it ... > enscript erf_calculator.txt
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 worksheet4_f00.tex.
The translation was initiated by Phil Duxbury on 2000-09-24