Section L3.1: Fortran as a Modelling Language

More chemical engineering programs are written in one or other of the `dialects' of Fortran than in any other programming language. This is largely because Fortran has been around longer than any other useful language, and because chemical engineers were amongst the first engineers to realise the potential of the then newly invented computer to solve important and useful problems.

Most of these programs were written by chemical engineers. The ability to write large and complicated programs is much less likely to be required of chemical engineering graduates nowadays, but you may well encounter programs which you may have to understand or even modify in industry.

Fortran, as indicated, comes in various dialects, which have evolved from the earliest in the late 1950s through Fortran 66 (1966), Fortran 77 (1977, still sometimes encountered) to the current Fortran 90 which we will use. The earlier versions are rather horrible and unsuitable for teaching purposes. But if you do have to deal with, say a Fortran 77 program, then your knowledge of Fortran 90 and a reference manual should be enough to enable you to work out what is going on.

Fortran has all the necessary features required to represent a mathematical model. These are described in the following sections.

A Sequential Programming Language

A Fortran program at its simplest consists of a list of formulas as we have defined them in section 3.1. (In fact the name of the language stands for Formula translator.)

A key feature of the language is that the formulas are evaluated in the sequence in which they are written by the programmer. This makes Fortran different from a spreadsheet system which (in the more sophisticated versions) works out a `correct' order for evaluating its formulas.

In fact a Fortran program consists of other `instructions' all of which are carried out in the order in which the programmer has specified them. Creating this `ordered list', `recipe' of instructions or algorithm is what most of programming is concerned with.

We will describe the instructions necessary to create a simple model in the next section. It is helpful to think of Fortran instructions as falling into three main categories:

The use of the first two categories can quite easily be understood once a few examples have been examined. The third group are best just learned by rote or looked up in a manual when required.

Example: Ideal Gas Density Calculation

A simple model is required to calculate the molar and mass density of a gas of given molecular weight at a specified temperature and pressure in atmospheres, using the ideal gas law:

P v = R T

Remember that here v is the specific molar volume, i.e. m³/kmol, and that if R is 8.314 J/mol/K then pressure P must be in pascals.

Developing the model program

It is vital that any program be written down before you attempt to type it in to the computer. Under no circumstances try to `compose' a program at the keyboard!

Simple programs which consist essentially only of formulas to be evaluated can be developed in the following stages:

  1. Write down all the equations.
  2. Identify the unknown variables and parameters of the model, and check that you have as many equations as unknowns.
  3. Rearrange the equations into formulas.
  4. Rewrite the formulas in the appropriate order for evaluation: at this point you have produced the algorithm for solving the problem.
  5. Finally, rewrite the algorithm as a program by adding the necessary housekeeping instructions.

Step 1: write all the equations

The relevant equations here are the gas law from above:
P v = R T

The other equations are concerned with calculating molar density and mass density from specific volume:
= 1 / v

= M / 1000

Here M is the molecular weight in kg/kmol. The factor 1000 arises of course because R is in gram mols. If pressure Pa is given in atmosphere, then this must be converted to pascals using the relationship:

101300 Pa = P

Step 2: Identify and count the variables

There are 4 equations. The 4 unknowns are P in pascals, v and the two densities. All other quantities are parameters, i.e. specified quantities T, Pa, and M, or true constants like R.

Step 3: Rewrite as formulas

Every unknown variable must appear once and only once on the LHS of a formula. There may be more than one acceptable set of rearrangements. Here, after noting what quantities are given, the only possible rearrangements of the 4 equations are as shown below.

v = R T / P

= 1 / v

= M / 1000

P = 101300 Pa

Step 4: Rearrange in calculation sequence

Again, there may be more than one acceptable reordered sequence. A suitable order here is:

P = 101300 Pa

v = R T / P

= 1 / v

= M / 1000

Step 5: Write the program

This involves two separate but entirely mechanical activities.

Tun the formulas into computer notation...

The first of these involves firstly suitable names for unknowns and parameters which are acceptable to the computer; broadly this requires that they consists only of letters and digits and start with a letter. Upper and lower case letters are equivalent. Secondly the computer notation of `*' and `/' for multiply and divide must be used, along with the computer-recognised names and format for any higher functions, e.g. log(x) for ln x.

The working part of the program thus now becomes:


P = 1.013e5 * Patm             ! P in Pa

v = R * T / P                  ! specific volume in m3/mol

rhomols = 1 / v                ! in mol/m3

rhomass = rhomols * M / 1000   ! ... in kg/m3

Note particularly the `comments' preceded by `!' which are ignored by the program, but help the reader to understand what is going on - remember, the most important thing about a program is that it must be intelligible to a reader!

Add the `housekeeping' ...

Before reading this section you may wish to refer to notes on how to write simple Fortran programs (section L3.2).

The housekeeping can be divided into the parts that make sense to the fairly uninitiated, and the other bits that just have to be learned by rote!

The first of these include the definition of `declaration' of variables and parameters. these must appear at the beginning of the program, logically enough, before any of the variables or parameters are used.

To declare variables which will contain general or `real' numbers we say:

real :: P, v, rhomols, rhomass
To declare parameters or constants we use a similar construction, but include the values to be specified, e.g.:
real, parameter :: Patm=2.0, T=298, R=8.314, M=16
Having calculated the solution in terms of variable values, these have to be communicated to the outside world by means of the computer instructions `print' or `write'. The simplest form for these here is:
print *, 'Specific molar volume is ', v, ' m3/mol'
print *, 'Molar density is ', rhomols, ' mol/m3'
print *, 'Mass density is ', rhomass, ' kg/m3'
The text printed out with the values should be regarded as an essential part of the program. Logically, the values cannot be printed out on the screen until they have been calculated, so these instructions come after the calculation part.

The program is required to start with the word program and be given a name, and to end with the words end program , followed by the program name. Good programming practice dictates a comment on the purpose of the program, and author details. Finally the statement implicit none should be present to override the default implicit naming scheme.

The complete program is thus:

program pvt

!this program calculates the specific molar volume, molar density
!and mass density for the given data using the ideal gas law
!JWP 11/12/98

implicit none

real :: P, v, rhomols, rhomass
real, parameter :: Patm=2.0, T=298, R=8.314, M=16

P = 1.013e5 * Patm             ! P in Pa

v = R * T / P                  ! specific volume in m3/mol

rhomols = 1 / v                ! in mol/m3

rhomass = rhomols * M / 1000   ! ... in kg/m3

print *, 'Specific molar volume is ', v, ' m3/mol'
print *, 'Molar density is ', rhomols, ' mol/m3'
print *, 'Mass density is ', rhomass, ' kg/m3'

end program pvt
You can copy the program from here to try it for yourself.

Return to Modelling Lab

Course Organiser
Last Modified 4/9/00