Introduction to Multidimensional Arrays

Assignment :

New Fortran:

Multidimensional Arrays, EQUIVALENCE

As I mentioned in a past lecture, interpolation of Equations of State, leads to the use of Multidimensional arrays. To save time in graphics conversion, my discussion on that subject is in the Postscript document

Actually multidimensional arrays are also useful in a simple bivariate interpolation, when higher order fits are used. I recently needed to do some flow analysis on a system containing Freon 11. The only information available to me was in the form of tables. I was interested in continuous derivatives to avoid problems with a Newton iteration in my solution procedure. This led to a program to generate and display a quadratic interpolator (actually I tried a couple cubic spline algorithms first, but got some unacceptable kinks in my curves.).

I have placed an example of the generation of my interpolation tables in the directory "quadint". The program doing the job is called quad.f. Because of the large number of tables that I had to generate (only a few shown in "quadint"), I compiled and executed this program with a shell script, runquad. One peculiarity that you will notice in this script is the compiler option "-qnoescape". I need to work with the Backslash character in this program, and the default for the IBM compiler is to place a special interpretation on the Backslash.

The program quad.f takes input files, each giving the temperature dependence of a specific Freon property, and generates two output files with names linked to the input name. The files ending with ".fit" are formatted as Fortran continuation lines, and are ready for insertion in a subroutine to obtain a specific property. I have provided examples of 4 such subroutines: viscl.f, rholiq.f, thcl.f, and satprs.f. Some of these subroutines have arguments that are not used for compatibility with other applications. The program also provides files ending with ".sam", giving sample values resulting from interpolation at a large number of temperature points. These files can be viewed with gnuplot using the commands placed in the file "quadfit.plt", by typing "gnuplot quadfit.plt". This step is extremely important to be certain that the interpolation does not exhibit unexpected behavior relative to the original data points.

As I noted in my introduction to arrays, assigning a variable as an array reserves a contiguous block of machine memory for the contents of that array. For a singly dimensioned array, its fairly obvious that the second element will immediately follow the first in memory, but what happens with multiple dimension arrays? One clue should appear when you look at the DATA statement initializing the doubly dimensioned array "tabl" in viscl.f. The numbers in the DATA statement are used in order by tabl(1,1), tabl(2,1), tabl(3,1), tabl(4,1), tabl(1,2), tabl(2,2), tabl(3,2) , etc. The rule for a doubly dimensioned array is that the order of storage is such that the second index is fixed at a given value while the value of the first index sweeps through all of its permitted values, then the second index is incremented and the sweep through the first is repeated.

I have provided another example of the memory order of array information with the program aorder.f. It takes advantage of an interesting Fortran statement called EQUIVALENCE. I establish arrays "a" and "b" with the statement:

real a(5,5), b(25)

Both have the same total number of elements, and I force the arrays to occupy the same memory locations with the statement

equivalence (a(1,1),b(1))

aligning the first element of each array at the same memory location. Values that I assign to "a" will also appear in "b", and the order of appearance in "b", tells us exactly what the compiler was doing when storing the values for "a". Compile and execute the program, and study the results.

Why does all of this stuff about order in memory matter? Three reasons. First, I need to know the order to correctly construct my DATA statements. Second, pulling numbers out in immediate sequential order, can significantly improve speed on many computers. By thinking about the order of storage, when you construct nested DO loops you may significantly improve execution speed of your program. Finally, C uses exactly the opposite ordering scheme with doubly dimensioned arrays. In C the Fortran array reference "a(i,j)" translates to "a[i][j]", and C stores the "j" elements contiguously. You may at some point construct a program using a mix of Fortran and C subroutines, and will have to account for this difference.

Another application of multi-dimensional arrays comes in calculations of processes in space and time. Consider fluid flow in a box heated on the bottom and cooled on the top. The computer can't evaluate physical quantities everywhere in the box. You have to pick a fixed number of points, perhaps on a rectangular grid of points at which pressure, temperature, velocity, etc are evaluated. This leads to Fortran arrays for a 3-D box in the form:

REAL P(100,100,100),T(100,100,100), VX(100,100,100), VY(100,100,100), ...

Programming applications to solve such problems are too complex to consider here. However, they are very important, providing the means to improve design of aircraft, pumps, turbines and engines, improve weather prediction capabilities, design better structures, analyze ground water flow for environmental impact of contaminants, and many more applications.

Back to the Table of Contents / Home

Written and Maintained by John Mahaffy :