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 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:
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.
Simple programs which consist essentially only of formulas to be evaluated can be developed in the following stages:
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
v = R T / P
= 1 / v
= M / 1000
P = 101300 Pa
P = 101300 Pa
v = R T / P
= 1 / v
= M / 1000
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/m3Note 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, rhomassTo 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=16Having 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 pvtYou can copy the program from here to try it for yourself.