Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
Computer code generator for chemical
reactions
J.R. Varela," E. Torijano,* A. Vazquez," C.R. Munoz Ledo/
M. Ley-Koo*
" Universidad Automa Metropolitana-Iztapalapa, Depto. de
Ingenieria de Procesos e Hidraulica, Iztapalapa 09340,
Mexico, D.F., Mexico
^Instituto de Investigations Electricas, Depto. de
Fisico-Quimica Aplicada, Cuemavaca, Mor. 62000, Mexico
ABSTRACT
The study of reactive fluids involves as a fundamental part the kinetics of the
chemical reactions that can take place in the fluid. The kinetics will be described
through a set of differential equations, which may be thought of mass balance
equations, and a set of constants representing the rate of change of the species
involved. The solution to the set of differential equations will provide the change
of the concentration as a function of temperature and time. There are computing
tools, such as IMSL, NAG,... , which use Gear's method to obtain such solution,
once the equations are provided. Since the structure of the equations that describe
the kinetics of chemical reactions does not vary, it is possible to develop a program
that simplifies the use of the mentioned computing tools in problems involving
chemical kinetics. This work describes the development of a computer program
that allows code generation, in FORTRAN, for the differential equations which
will represent the chemical mechanism, once the reactions, and its associated
constants, are specified. Thus, the use of the code generator will simplify the
work in the development of chemical models, by reducing, on one hand coding
time and, on the other, possibility of typing mistakes; such reduction becomes
important when the number of species and reactions increases.
INTRODUCTION
Studies related with air pollution involve the knowledge of the time evolution for
the concentration of the chemical species in the atmosphere. Such evolution is
provided by the solution to the Ordinary Differential Equations (ODE's) describing the chemical reactions that can take place among the species. In order to
solve the ODE's one can, either write its own computer program to obtain the
solution, or use one of the conventional integration programs, such as DGEAR [1]
or EPISODE [2,3]; however, if one follows the latter, then the appropriate input
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
306
Air Pollution
must be prepared. Such input is in the form of a FORTRAN subroutine containing the statements for the set of ODE's to be solved, for both EPISODE and
DGEAR; in addition, the Jacobian is also required but, in the case of DGEAR,
it may be provided by the user or generated internally.
It is convenient to point out that the time evolution of the concentration for
chemical species is not only of interest in air pollution studies but also in the
analysis of combustion, photochemical processes, reactive flows and others. The
main obstacle in these problems is the testing of the so-called chemical mechanisms, more so when the number of species and reactions increases; the reason
being, on one hand, the amount of time invested in preparing the input and, on
the other, the computer time required by the conventional programs. Therefore,
it would be convenient to have a computer program capable of generating the
subroutines [4] required by the integration programs and, at the same time, be of
help to reduce typing mistakes. The purpose of this work is to describe a program
which will have the above mentioned features.
INPUT FILE
The developed program has been named GENCOD2 and its initial function is to
read in an ASCIIfilecontaining
1. the number of chemical species (N.,) and the number of chemical reactions
(Nr),
2. names of the chemical species, and
3. listing of the chemical reactions, with the corresponding Arrhenius constants
Such ASCII file is provided by the user and is identified by the code generator
as *.BCO, the default being NAME. Thefilemust follow a format consistent
with that in GENCOD2, which is
REAL*8 C(Kr,Kp),A(Kr,3)
INTEGERS M(Kr,4),N(Kr,Kp)
CHARACTERS CHEM(Ks)
OPEN(UNIT=1,FILE='NAME.BCO',STATUS='OLD' )
READ(1, *) Ns,Nr
DO 1 1=1, Ns
1 READ(1,'(I2,2X,A6)')IDUM,CHEM(I)
DO 2 1=1, Nr
2 READ(l,*)IDUM,(M(I,J),J=l,4),(C(I,K),N(I,K),K=l,Kp),(A(I,L),L=l,3)
where Kr, Kp, Ks are the maximum number of reactions, products and species
accounted for in the mechanism, respectively, and are defined through a parameter statement. As GENCOD2 reads in the ASCIIfile,it generates internally the
arrays CHEM, M, C, N, and A, whose contents are
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
Air Pollution
307
1. CHEM(I), (1=1, Ns), has the names of the chemical species, with no more
than six characters per name, which be part of the mechanism;
2. M(I,J) (1=1, Nr) is a number representing the reactive species in the I-th
reaction, at present, this may be up to third order (J=l,3); for binary
reactions a value of zero will be in any of the first three possible values of J.
It is important to note that, in this work, the J may have higher values, but
they will be used to point special features in the 1-th reaction; e.g. M,4=9
is reserved to indicate that the I-th reaction is photolytic, M,4=l indicates
the presence of a third body;
3. N(I,J) (J=l,Kp) contains the code number referred to the species produced
in the I-th reaction, in the same way C(I,J) is the corresponding stoichiometric coefficient;
4.finally,A(I,J) (J=l,3) are the Arrhenius constants for the I-th reaction and
will provide the temperature dependence of the constant reaction rate K(T)
by
SYSTEM'S MATRIX
The above is the minimum required information to write the ODE's which will
describe the time evolution of the concentration of the chemical species. In order
to write the ODE's, we build an array 5, which will include this information in
the following way:
REAL S(Nr,Ns)
DO 6 I=l,Nr
DO 5 L=l,3
5 IF(M(I,L).NE.O)S(I,M(I,L))=S(I,M(I,L))-1
DO 6 L=l,Np
6 IF(N(I,L).NE.O)S(I,N(I,L))=S(I,N(I,L))+C(I,L)
Thus, the array element may be
!<
0 if the J-th specie is a reactant in the I-th reaction
= 0 if the J-th specie does not participates in I-th reaction
> 0 if the J-th specie is a product in the I-th reaction
If Sij 7*^ 0 then its absolute value will represent the stoichiometric coefficient.
RATES
The code generator relies heavily on the matrix S; we first build the subroutine
named RATES, which essentially writes each of the Nr instructions to calculate
the same number of reaction rates based on the equation
3
(2)
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
308
Air Pollution
for i=l,Nr and M,4 ^ 9, in general, or
(3)
for photolytic reactions, with M,*4 = 9; where Y denotes the concentration and
P the photolysis rate, respectively. Y and P take a value of 1 if M,-j = 0.
There are four possibilities to write the above mentioned instructions:
1. ternary reactions,
2. binary reactions,
3. decomposition reactions, and
4. photolytic reactions
Since it is easier for ternary reactions, we take it as an example, for which we
have
CHARACTER B*40
DO.... I=l,Nr
WRITE(B(7:37),51)I,I,(M(I,J),J=1,3)
51 FORMAT('R(',r3,')=K(',I3,3(')*Y(',I2),')')
in this way, the string B contains the instruction to calculate the corresponding
reaction rate. For binary reactions, on of the array elements (M(I,J), J=l,3)
would be zero, which forces us to identify the species that are present, for this
case the last two instructions must be replaced by
WRITE(B(7:31),52)I,I,MR1,MR2
52 FORMAT('R(\I3,0=K(\I3,2(')*Y(',I2),')')
where MR1 and MR2 are the non-zero elements of (M(I,J), J=l,3). For decomposition reactions, only one non-zero element MR1 is present in the array and
the following must be used
WRITE(B(7:25),53)I,I,MR1
53 FORMAT('R(',I3,')=K(',I3,')*Y(',I2,')')
Finally, for photolytic reactions, we use the instructions
WRITE(B(7:25),54)I,MR1,MR1
54 FORMAT('R(\I3/)=P(\I3,')*Y(',I2,T)
It is clear that the type of reaction must be recognized, which is achieved by a
thorough analysis of array M and the correct placement of the information so
that the above instructions are correctly executed.
The subroutine RATES(x,T,Y) generated by GENCOD2 has the following form
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
Air Pollution
309
SUBROUTINE RATES(x,T,Y)
REAL'S Y(Ns),P(Ns),K(Nr),R(Nr)
COMMON/DATA1/K, R, P
CALL CONST(T)
CALL PHOTO(x)
R( 25)=K( 25)*Y(12)*Y( 8)
R( 43)=P( 16)*Y(16)
RETURN
END
In the above example, the instruction to calculate R(25) assumes a binary thermochemical reaction with species 8 and 12 as reactants; while R(43), assumes a
photolytic dissociation of specie 16. The argument x in the subroutine represents
the time at which the rates will be evaluated.
DIFFERENTIAL EQUATIONS
The next step in the code generator is to write the subroutine for the ODE's, to
do so, we take into account that the time derivative of the concentration is given
by
d
"'
Fi = -Y(i) = ^SijRj
(4)
<"
j=l
while an element of the Jacobian is
where for simplicity we use Yj = Y(i).
As mentioned before, the user of EPISODE must provide two routines, one for the
set of ODE's and one for the Jacobian. The user may choose among two options;
the first, and most direct, is to program eqs. 2-5 and the second is to write the
instructions in detail the developed form of the above equations. The second option avoids unnecessary operations and may be used for particular photochemical
mechanisms, as is the case for the subroutines generated by GENCOD2.
In order to generate the subroutine DIFFUN, whose contents are the ODE's, we
use equation 4 and look for non-zero elements of array S. There are two cases
that must be considered for non-zero elements of S; thefirstis for Sj, # 1 values,
the following instructions will be used
CHARACTER SIGN*1,LINE*72,BLANK*72
WRITE(BLANK(1:72),'(72' ')')
DO .... I=l,Ns
DO .... J=l, Nr
SIGN='+'
IF(S(J,I).LT.O.O)SIGN='-'
IF(L.GT.61)THEN
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
310
Air Pollution
WRITE(..., ... )LINE
LINE=BLANK
LINE(6: 6)='*'
L=7
ENDIF
K=L+11
WRITE(LINE(L:K),61)SIGN,ABS(S(J,I)),J
61 FORMAT(A1,F4.1,'*R('I3,')')
L=K+1
so that the contribution of the J-th reaction to the time derivative of the concentration of the 1-th specie will be written in string LINE. For Sji = 1, the
numerical multiplicative factor in LINE is not needed, so that the instructions
reduce to
IF(L.GT.66)THEN
ENDIF
K=L+6
WRITE(LINE(L:K),62)SIGN,J
62 FORMAT( Al, ' *R ( ' ,13, ' ) ' )
Finally, the subroutine DIFFUN generated by GENCOD2 complies with the specifications required by EPISODE, and has the following form
SUBROUTINE DlFFUN(Ns,x,Y,F)
REAL*8 Y(Ns),F(Ns),P(Ns),K(Nr),R(Nr)
COMMON/DATA1/K, R, P
CALL RATES ( x, T, Y )
F( 4)=+R(14)...-2.00*R(25)
RETURN
END
It is convenient to point out that the code generator will not write beyond column
72, as required by FORTRAN, and will generate a new line if needed.
JACOBIAN
In order to generate the subroutine PEDERV, whose contents are instructions to
evaluate the Jacobian, we follow equation 5. Let us consider the calculation of
element J,-^; we first look, one by one, each of the reactions where the i-th specie
appears, let the j-th reaction be one of them. We then ask if the k-th specie is a
reactant in the j-th reaction, if this is the case then we write the corresponding
contribution;
DO ....I=l,Ns
DO K=l,Ns
DO
J=l,Nr
IF(S(J,I).NE.O.O)THEN
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
Air Pollution
311
IF(S(J,K).LT.O.O)THEN
WRITE(.. ,.. )
ENDIF
To write such a term, array M must be looked up and then proceed as in RATES.
The goal is to write the rate of j excluding the contribution from the k-th specie.
This is done in similar form as it was done in DIFFUN. The resulting subroutine
being
SUBROUTINE PEDERV(Ns,x,Y,J,No)
REAL*8 Y(Ns),J(Ns,Ns),P(Ns),K(Nr),R(Nr)
COMMON/DATA1/K,R,P
J(12, 5)=
+2.00*K( 3)*Y( 2)
RETURN
END
OTHERS
The described code generator may generate other helpful subroutines as may be
that named CONST, which calls upon RATES and whose function is to keep
updated the temperature in the constant rates of the thermochemical reactions.
To do so, GENCOD2 looks up the array A, and writes the instructions for M%4 ^ 9
in the following way
DO....I=l,Nr
IF(M(I,4).NE.9)THEN
LINE=BLANK
WRITE(LINE(7:23),'(6X,'K(',I3,')=',E10.3)')I,A(I,1)
L=24
IF(A(I,2).NE.O.O)THEN
WRITE(LINE(L:L-hlO),'('*T**(',F5.2,')')')A(I,2)
L=L+11
ENDIF
IF(A(I,3).NE.O.O)THEN
WRITE(LINE(L:L-h7),'('*EXP(',E12.5,VT)')')A(I,3)
L=L+18
ENDIF
IF(M(I,4).NE.O)THEN
WRITE(LINE(L:L+5),'('*TC',I1,')')')M(I,4)
ENDIF
ENDIF
If the mechanism includes photolytic reactions then the user must provide a
subroutine named PHOTO(x), which updates the dissociation frequency at time
x.
CONCLUSIONS
Transactions on Ecology and the Environment vol 1, © 1993 WIT Press, www.witpress.com, ISSN 1743-3541
312
Air Pollution
The developed program has been used to compare the options mentioned above,
to program or to write the ASCIIfileand use GENCOD2; both options provide
the same numerical results, but considering a mechanism with 81 reactions and
32 species there may be a saving in time of at least a day, when the described
program is used. Another important feature of the program here described, is
that it may provide a listing of the reactions, which may be compared with the
original, so that mistakes are readily found.
Some modifications may be made so as to include source terms into the subroutine
DIFFUN. Currently, the program is being used to analize combustion processes
as well as reactive flows.
REFERENCES
1. Rice, J.R. Numerical Methods, Software and Analysis McGraw-Hill, 1983.
2. Hindmarsh, A.C. GEAR: Ordinary Differential Equation System Solver
Lawrence Livermore Laboratory, University of California 1973.
3. Hindmarsh, A.C. 'Application of EPISODE: An Experimental Package for
the Integration of Systems of Differential Equations ' Numerical Methods
for Differential Systems ed. Lapidus, L. and Schiesser, W.E., pp!47-166,
Academic Press 1976.
4. Sistema GCPIMSL1 Direccion General de Derecho de Autor Reg.5039/92,
Mexico.
© Copyright 2026 Paperzz