Modelling Languages with Reference to Fortran

Models can in principle be written in any computer language. A large number of special purpose languages have been developed to represent the steady state behaviour of process flowsheets. Rather fewer specialised languages are available for modelling at the level with which most of this course in concerned, and the majority of process models are in practice described using general purpose computer languages.

One of the most widely used general languages historically is Fortran, and many large models, particularly of special purpose process units such as reactors will be found in this language. We have given a number of examples of algorithms in Fortran, and there is a free version of a fortran compiler from Lahey which you can download to run on you own PC.

A very large number of models in the last few years have been written to run in spreadsheet systems. The spreadsheet is extremely convenient, as it can be found on every PC and most palmtops. However, spreadsheets have several very serious disadvantags:

As a result of the above, manually generated spreadsheets cannot be regarded as a safe modelling tool! The most important property which a model must have is that it should be UNDERSTANDABLE. This is much more important than that it should be correct!

The risks of using spreadsheets are now well well documented, see work by Raymond Panko in the USA and Paine in the UK.

However, the spreadsheet is so universal and so convenient for running models (as opposed to building them) that we have developed a simple modelling language. which avoids most of the above problems by providing a high level description of the model, but generates a spreadsheet which can be used on any PC.

The main purpose of this language is thus to facilitate the production of documented models. In its simplest version (release 2.1, September 1998 for algebraic equations) it provides no solution facilities not available to the standard spreadsheet system.

The Elements of a Modelling Language

The most important function of a modelling is that it should encourage the production of readable models. All models contain the following elements which must be represented in a modelling language: Additionally it is highly convenient to be able to represent: The definition of the model does not itself have to say anything about how the model is to be solved, although this is a necessary part of a Fortran program to solve a model, this is not strictly speaking part of the model itself.

The model itself should be easy to read and self explanatory.

Fortran as a Modelling Language

Fortran contains the three main elements above. In general each model element should be written on a separate line, but related parts of the model can usually be written on one line separated by semicolons if this makes the model more readable.

The model can, and should, contain plentiful comments which explain what different parts of it mean. These are preceded by the symbols `!' and are ignored by the system.

Variables

The user specifies the variables in the model by `declaring' them as Fortran variables, e.g.
real :: vmolar, romolar, romass, P

There are various things that can be done to improve readability by putting comments in the program, e.g.:

real :: vmolar          ! specific molar volume
real :: romolar, romass ! densities
real :: P               ! N/m2 required for gas law
Comments cannot be `embedded' in Fortran instructions, but lines can be broken up using th `&' symbol which means `continued on next line, ignore anything else on this one'. (This is also used to when it is necessary to write instruction which do not fit on a single line.) The ability to give user specified names to variables is a key feature of any modelling language, and the choice of `meaningful' names is an important factor in making a model comprehensible.

This example is a very simple model to calculate the mass density in kg/m³ of an ideal gas at a given Kelvin temperature and pressure in atmospheres. We are also given the molecular weight of the gas. We decide that we need variables to represent the specific molar volume and mass and molar densities. Although the pressure is given, and is not an unknown or variable (it is a parameter, see below) the Gas Law requires pressure in pascals which the model will require to calculate, so we require a variable for this pressure, giving 4 variables in all.

The model declaration instructions can also be written:

real ::   &
 vmolar,  &  ! specific molar volume
 romolar, romass,  & ! densities
 P        ! N/m2 required for gas law

Note the use of both standard symbols for standard quantities, e.g. P for pressure, and mnemonic names for other quantities.

Parameters

Parameter is a term used by modellers to mean a `variable constant', that is a quantity which is known, but which the modeller may wish to change. Here the specified temperature, pressure and gas molecular weight are parameters. They are declared in the model in a rather way to the variables. However parameters must be given values, which is done as in the example below.
 
real, parameter  :: MW = 16.0 ! methane, kg/kmol
real, parameter  :: T = 273.0 ! K temperature
real, parameter  :: Patm = 1.0 ! atmospheres, sensible units
real, parameter  :: R = 8.314  ! gas constant in SI units: gmol, Pa etc
 

Note the appearance of R, the gas constant in this section. strictly, R is a true constant, having a fixed value, but most modelling langauges do not distinguish these from parameters.

The modelling languages requires initial values for parameters, but these can be changed once the model has been generated. Do not however change the values of constants like R!

Equations

Equations are written in normal `computer algebra' notation. However, simple programs in Fortran require that all equations be written as formulas, and that every unknown (variable) appears once and only once on the LHS of a formula. This approach is discussed in the
general introduction to algebraic equations. The equations must also be ordered so that a value for each variable used on the LHS of a formula is calculated before it is used. A formal approach to this is described here. The equations section for this model is as follows:
! Start equations
 P = Patm * 101300.0      ! pressure required in SI
 vmolar = R*T/P           ! m3/gmol
 romolar = 1/ vmolar      ! gmol/m3
 romass = romolar*MW/1000 ! kg.m3
! end of equations

Housekeeping

The most important additional instructions are those required to print out the final values of the variables. It is also a good idea, to make the results selfcontained, to print out the values of key parameters.

For this example the printout instructions could be:

print *, 'Parameters:'
print *, 'MW ', MW
print *, 'T, K ', T
print *, 'Pressure, atm ', Patm
print *, 'Variables:'
print *, 'Molar density, mol/m3 ', romolar
print *, 'Mass density, kg/m3 ', romass

The order of the sections is thus logical, and required to be:

  1. Parameters and/or
  2. Variables
  3. Equations
  4. Printout
The whole program must of course be enclosed by the keywords
 program SomeName 
and
 end program 
`SomeName' is a name which you can give the model.

Note that all names used in the model must be a combination of letters and number only , and must start with a letter. Upper and lower case letters are treated as being the same.

Complete Model Program

Here is the complete model:

program idealgas
! ideal gas density calculation

real ::   &
 vmolar,  &  ! specific molar volume
 romolar, romass,  & ! densities
 P        ! N/m2 required for gas law

real, parameter  :: MW = 16.0 ! methane, kg/kmol
real, parameter  :: T = 273.0 ! K temperature
real, parameter  :: Patm = 1.0 ! atmospheres, sensible units
real, parameter  :: R = 8.314  ! gas constant in SI units: gmol, Pa etc

print *, 'Program to calculate gas densities'

! Start equations
 P = Patm * 101300.0      ! pressure required in SI
 vmolar = R*T/P           ! m3/gmol
 romolar = 1/ vmolar      ! gmol/m3
 romass = romolar*MW/1000 ! kg.m3
! end of equations

! output..
print *, 'Parameters:'
print *, 'MW ', MW
print *, 'T, K ', T
print *, 'Pressure, atm ', Patm
print *, 'Variables:'
print *, 'Molar density, mol/m3 ', romolar
print *, 'Mass density, kg/m3 ', romass

end program
 

You can copy it from here.