Worksheet 1

Due: Sept 9, 2005

Physics 201 homepage

FORTRAN (FORmula TRANslation) remains the workhorse of scientific computing. The majority of existing codes for scientific simulation are in fortran, many still in the old Fortran 77 standard. Fortran 90 contains many important improvements over Fortran 77. Fortran is a continuously evolving language, and a great deal of ``backward compatibility'' is maintained. Only the worst of the older constructs have been explicitly removed in recent versions of Fortran.

Here is a brief history of Fortran: 1954, Fortran I; 1958, Fortran II; 1958, Fortran IV; 1966, Fortran 66 standard; 1978, Fortran 77 standard; 1991, Fortran 90(and HP) standard; 1996, Fortran 95 standard; 2001, Fortran 2000 (??). You need to understand the main features of Fortran 77 through Fortran 95, because in looking at existing codes, you will find a variety of different constructs drawn from these standards. We will concentrate on the Fortran 90 standard but we will also go over some of the F77 and F95 structures. HPF is a superset of Fortran 90 and is designed to take advantage of parallel computing architectures. The first thing to do however is to learn Fortran 90, which provides many of the constructs which modern compilers can automatically optimize for parallel platforms.

Although Fortran is often criticized and is sometimes referred to as the ``programming language of the past'' it is, and likely will continue to be, the most efficient language for large-scale scientific computing. The ``Object-Oriented-Programming'' (OOP) revolution (e.g. C++ and more recently Java) have not superceded Fortran, as the modern Fortran standards incorporate many of the most useful features of OOP. OOP attempts to enable maximum transferability of code, so that in C++, subunits are placed in ``CLASSES'' which can be included in a variety of larger programming units. In the Fortran 77 standard FUNCTIONS and SUBROUTINES do some of this, but their transferability is limited. Fortran has responded by introducing MODULES which do provide many of the features of CLASSES. You should also note that the performance of C++ and Java in large scale computing is also continuously improving as the compilers for these programming languages are being optimized for scientific computing.

Though the above list of programming languages seems overwhelming at first, learning a new programming language in not a difficult task, once you have learned one of them well. In fact Mathematica is in many ways a programming language and many of the constructs used there appear in Fortran and/or C,C++, Java. In general Mathematica is less efficient for large-scale calculations, but it is very convenient and useful for smaller scale calculations and for checking your large scale FORTRAN codes.

For the first worksheet

Read the first chapter of Ortega. Here are two examples which you can use as templates for this week's problems.

- 1. sample file 1 which prints out the name and ambition of a previous instructor.

- 2. sample file 2 which squares the sine of an integer and sums that over the first 100 integers. (This example from a previous year provides a useful object lesson: observe that the description given here, the description contained in the code, and the actually quantity calculated are all different!)

(There is a further curiosity associated with this example: The sum of sin(j*x) from j1 to j2 can be evaluated in a simple closed form for any x by writing sin(j*x) in the form (exp(i*j*x)-exp(-i*j*x)/(2*i) and noticing that the sums become finite geometric series. Mathematica 4.1 is able to recognize this for Sum[Sin[j*x],{j,0,n}]. However, it gives a wrong answer to Sum[Sin[j*x],{j,2,n}], and a bizarre answer to Sum[Sin[j*x],{j,n1,n2}]. (I have reported this to the Mathematica enterprise; they have acknowledged the problem, but it has not been fixed as of the version 5.0 release. Similar sums of (sin(j*x)**2 can also be done in closed form by writing (sin(z))**2 = (1-cos(2*z))/2.)

PROBLEMS

- 1. Write a Fortran program that inputs two integers from the keyboard, and does the following with them: Sum them, subtract them, multiply them and divide them. Then print out the two integers along with the results of the above operations. Make sure you also print out some text so you know what each output number means. Also print out a "prompt" so you know what to input. Test your program to see that it is working and gives satisfactory results. See what happens if you divide by zero, and then fix the program so that that possibility is handled in a graceful manner.

- 2. Write a Fortran program that calculates and prints out the sum of 1/k^2 for three cases: Where k runs over the first 3 positive integers; where k runs over the first 10 positive integers; where k runs over the first 100 positive integers. (It would be elegant to use the same code for each of these 3 cases; you could do that by defining a function whose argument is the upper limit.) Check your answers by doing the same sums using Mathematica. Include your Mathematica code in the results that you hand in.

Linux commands you need to know

- (i) yppasswd - change your password (and remember it!!!)

- (ii) gedit or emacs or vi -- choose one of these to edit files.

- (iii) f90-vast - Use this to compile your fortran program

e.g. f90-vast -o test.exe test.f90
This generates the executable and names it test.exe. If you do not specify a destination file e.g., f90-vast test.f90, the executable is assigned the file name a.out, which overwrites any previous a.out that you may have had.

Even better, use f90-vast -O -Wall -o test.exe test.f90
The -Wall switch asks the compiler to give warning messages for things like the use of undefined variables. The -O switch asks for optimization (to produce code that executes faster); but it is also needed for good functioning of the -Wall switch.

- (iv) How to run an executable - Type ./test.exe (or ./a.out)
The ./ specifies the PATH to the fortran directory. On many computers it is not necessary to specify the path, but with the current setup in 1240 BPS you must specify the path.

- (v) Another compiler you should know about is g77. This is a Fortran 77 compiler than comes with the Linux distribution. It also understands quite a few of the basic Fortran 90 instructions, though it does not handle some more sophisticated aspects of fortran 90.

- (vi) You can run Mathematica by typing   mathematica   (or   mathematica &   to run it in the background). Make sure the keyboard NumLock is off to get normal operation of the backspace and arrow keys.