Next: Output Up: Some Notes on Previous: Declarations

Input

Avoid hardcoding variables. You might think that you only want to compute unsteady heat conduction in a bar of unit nondimensional length using a unit nondimensional heat conduction coefficient with 16 points along the bar. However, do not incorporate such assumptions in your computer code. Instead, create an input file and put the desired values into the input file. Then in your program, read in values for the bar length l, heat conduction coefficient k, number of points J, etcetera, from that file.

This makes it possible to simply change the input numbers if later you decide you would like dimensional values after all, or you would like to try 8 mesh points instead. It also makes your program easier to read and understand: it is easier to figure out that J or jmax is the maximum value of variable j than to understand where that 16 comes from. Similarly, j/16 or j/J may be cryptic, but (j/J)*l is easily understood to be the x-coordinate along the bar. It also allows you to check whether your statements make sense dimensionally.

Do not give variables such as the examples above their values inside your program itself. It should not be necessary to read through your computer program to figure out what the value of the heat conduction coefficient is. Further you should not have to recompile your entire code to change a single value of a parameter. This is especially important for big codes, but consistency requires that you program the same way even for small codes.

Put comments in the input file to indicate the meaning of the various numbers. Than the program does not have to be searched to figure out what is in the input file.

Select a good name for your input file. If the input file is specific to program heat.f, you could call it heat.in. Later when you have 50 files in this directory, you can still find it. Do not allow the compiler to select such meaningless names as fort.1.

When you read in a variable which must be within certain bounds, check these bounds as soon as you have read in the number. For example, check a requested number of mesh points jmax versus the maximum dimensioned number of points jdim. Exceeding declared array bounds can produce very nasty errors.

If you will be dividing by a number that you read in, check that it is nonzero. Print a clear error message and terminate the program if it is. This is easier and neater than trying to figure out why the program suddenly crashed.

When you plan to be running a program many different times for different values of a parameter such as a time step, you may want to enter this variable from the keyboard. Write a clear prompt to the screen if you do so. Give the program user a chance to abort or to back up, for example by entering zero, and explain that in the prompt. Check the input even more careful than input from a file and allow the user to correct typos. Avoid possible program crashes when the user types an invalid character.


Next: Output Up: Some Notes on Previous: Declarations