Week 10 Questions


Are the characters "A" and "a" associated with different integers?

Yes. Take a look at my new full list of the ASCII definitions for how each integer from 0 to 127 is to be translated to a "character".

How exactly does a program allocate space for arrays when you wish for the arrays to be ALLOCATABLE? What usually determines the sizes?

Allocation of space begins with the ALLOCATABLE declaration.

real, allocatable :: b(:)

This tells the compiler that whenever it sees the variable "b" in statements, it shouldn't insert a specific address in memory, but coding that will go to another specific location in memory to get the address of "b" (this is related to pointers in some language). When you actually ask for a specific amount of space in "b" with

allocate ( b(nspace))

the details of what happens next are probably machine dependent, but the net effect is as follows: if each element of "b" is 4 bytes long, and your original program took up "proglen" bytes, then the new amount of space that your program uses in machine memory is:

new_proglen=proglen + 4*nspace

That location in memory that was reserved to contain the address in memory for "b" now contains the address at the beginning of the new 4*nspace bytes of memory.

The only reason that you bother with allocatable memory is to respond to varying user needs for memory. This means that there is always some clue from the input to the program that will specify the size of your arrays. If you are lucky (as in the homework), one element of "b" is contained in each line of the input file. You count the number of lines in the input to get "nspace". Generally, you are not that lucky, but it's the same general idea. You make one pass through the input file, simply counting the number of elements set or requested in each array to be allocated. You allocate the arrays, then REWIND the input file, and READ again, picking up actual values for the array elements.

External means you want to use a variable in the call statement for a subroutine as a function, right?

Basically yes, but the "variable" in the call statement must be the name of a function that you have created somewhere else in your program. In the Subroutine statement, the corresponding argument can have any name, but must be also declared in an EXTERNAL statement so it is recognized as a function.

Why do you pass an intrinsic function as an argument to a subroutine? Why can't you just call it within your subroutine?

Normally you do just call the intrinsic function within your subroutine. The only time you pass a function name through an argument is when you are trying to improve the flexibility of your subroutine. Look at plot2.f for a crude example of this. You may need a little more math to understand my next example. I have at times needed to generate "analytic" series solutions to heat conduction problems. In Cartesian coordinates this involves a Fourier series sum of trigonometric functions. In Cylindrical coordinates the series is in multiples of Bessel functions. For common boundary conditions, the mechanics of generating the solution is the basically the same. I can create a single subroutine to produce solutions for both Cartesian and Cylindrical coordinates, and let the argument list pass the appropriate functions for the desired coordinate system.

What is the Web location of the site for useful subroutines?


also worth looking at http://http.ucar.edu/SOFTLIB/mathlib.html

I missed what an INTRINSIC statement is. What does it do?

An INTRINSIC statement makes a list of intrinsic functions (e.g. sin, cos, exp) that you want to pass as arguments to subroutines, or functions.

How do you use FORMAT statements and the format number in a write statement?

Always think of the FORMAT and WRITE as a tightly coupled pair. First construct the write statement to put out the values of whatever variables that you need to see (say "j", "x", and "y"). Next think about how you want the numbers to appear in the file or on the screen. If an existing FORMAT gives that result use its number. If not pick any number not currently used as a label and include it in the second position within the WRITE's parentheses (first number is the unit number or a "*" for screen). The statement:

write(11, 2222) j, x, y

writes to the file opened on unit 11 using a FORMAT labeled 2222. Now I construct the format to give the desired appearance of the line. Perhaps I wanted to handle up to 4 digits in the INTEGER "j", standard decimal notation for "x" with 3 digits after the decimal, allowing for values of x between 0 and 9999.999 , and exponential notation for "y" with 4 digits after the decimal, a significant digit before the decimal point, and room for a negative sign in front. I'll also allow at least 5 blank spaces between each number.

2222 format (i4, 5x, f8.3, 5x, 1p, e11.4)

I can insert quoted strings above, if labels are needed for the numbers. The best thing that you can do is to try a few of these with a short test problem.


What is the first line of the runit file ( "#!/bin/csh")?

It tells the operating system that I am using a script for the C Shell. A number of Shells are available under Unix, and you have to do something specific here, or Unix assumes that you are writing a script for the Bourne Shell. This doesn't make a difference until you get into some of the fancy loop and conditional structures.

Class Procedures

Do we have to know about interpolation for the exam, or just be familiar with what it is and what it does?

You should always know how to do a linear interpolation (obtain the equation for a straight line given two points). Beyond that be familiar with what it is and what it does. This includes ideas of keeping derivatives continuous at table points, the concept that cubic ( also quadratic , and higher order) interpolation functions can develop "kinks" (at which point they are worse than linear), and relative merits of even versus uneven spacing of the table independent variable (x value). I will not ask you to derive any quadratic or cubic interpolation functions.

Will the test be the same format as the last one?


Is there going to be a review with everything we need to know for exam 2?

"Everything" may be stretching things, but look at the Web notes for Monday and come prepared to ask questions during Monday's review session.

How many more homework assignments are we going to have?

Two more group projects. Maybe one more very short individual program.

How much do we need to know about the FORMAT statement for the exam?

FORMAT statement and FORMAT edit descriptors: "i", "f", "e", "a", "x", "p", "/"

Repeat Count in FORMAT: 5X, 3(f5.1,2x,i5) etc.

Re-use of portions of FORMAT (wrap-around) when the list of variables to write exceeds number of edit descriptors.

Would it be possible for you to display our HW and test scores on the class homepage?

I've been told that is a violation of University privacy guidelines.

Why have the lectures gotten progressively more confusing?

Two major problems. First we are now mixing learning syntax with learning algorithms for the solution of problems in engineering. Solution methods are more complex than syntax, and more confusing. Secondly, I am not helping with timing of my lectures. Key material is carrying from the end of one lecture to the beginning of the next. This portion of the class has changed substantially from last year, and I'm working at adjusting the pace. Please take the time to go over my Web notes both before and after each class, and keep after me with questions, especially questions in class for clarifications.

Computational Solution Methods

How exactly do you use the cubic fit and what's its purpose?

The reason for the fit is that I have values of some function only at a limited number of points. For example, I may need viscosity of a liquid at any temperature between 0 and 120 Fahrenheit. However, I only keep values of viscosity stored for a limited number of temperatures, say T= 0, 20, 40, 60, 80, 100, and 120 F. What I do is to obtain coefficients for a cubic function within each of the intervals between my temperatures (6 intervals, 4 coefficients in each interval). There are two ways to get these coefficients. As an example, let's get a cubic for temperatures between 40 F and 60 F. One way is to use the viscosities at 20, 40, 60, and 80 F. to generate 4 equations with 4 unknown coefficients. The other is to use temperatures and first derivatives at 40 F and 60 F to get enough equations. In either case, you end up with an expression:

visc = a0 + a1*T + a2*T**2 + a3*T**3

With known values for a0, a1, a2, and a3. Plug in any T between 40 and 60, and you will get an approximation to the viscosity at that temperature. Usually, this will give a more accurate approximation for values of viscosity in the interval 40 < T < 60, than you would get with a simple linear interpolation. Unfortunately, there are times when the cubic fit can behave very badly, "kinking" by placing the maximum and/or minimum of the cubic within the interpolation range. I'll provide examples of generating and using these coefficients in next Wednesday's notes.