/
facsimile_rate.py
155 lines (135 loc) · 6.13 KB
/
facsimile_rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# #################################################################### #
# #
# FACSIMILE REACTION RATE #
# #
# Script to generate the FACSIMILE code for the calculation of the #
# rates of production and destruction of selected species in a #
# chemical mechanism #
# #
# The script uses the 'facmecha' function in the 'facsimile_funcs' #
# module to extract the chemical equations from the mechanism, and #
# the 'ropa' function in the 'facsimile_funcs' module to select the #
# reactions containing a given species #
# #
# #################################################################### #
# #
# version 2.6, april 2021 #
# #
# author: R.S. #
# #
# #################################################################### #
import textwrap
import facsimile_funcs
# #################################################################### #
## function to write the lists of parameters, the expressions to
## calculate the production and destruction rates, and the definitions
## of the new parameters to an output file
def outputrates(r,fname,paramlist,totparamlist,expresslist,definlist):
fout = open(fname, "w")
# write list of parameters to output file
fout.write("""* ;\nPARAMETER\n""")
for p in paramlist:
facsimile_funcs.listblock(p,fout)
fout.write(" ")
fout.write("\n")
facsimile_funcs.listblock(totparamlist,fout)
fout.write(" ;")
# write the expressions to calculate the production/destruction
# rates to output file
if r == 'p':
rstr = "production"
elif r == 'd':
rstr = "destruction"
fout.write("""\n* ;
* ********************************************** * ;
* * Rate of """ + rstr.capitalize() + """ Analysis
* ********************************************** * ;\n""")
# write the expressions for each of the selected species
i = 0
for species in listspecies:
fout.write("* ;\n* *** " + species + " " + rstr +"\n* ;\n")
for el in expresslist[i]:
# write the expression only if it contains a specified
# reactant (default=all)
if reactant in el:
fout.write(el)
# write expression for total production/destruction rate
if reactant == '':
fout.write("* ;\n")
tot = totparamlist[i] + " = " + " + ".join(paramlist[i])
fout.write(textwrap.fill(tot) + " ;\n")
i = i + 1
# write definition of parameters to output file
fout.write("""* ;
* ********************************************** * ;
* ;\nDEFINITION OF THE PARAMETERS\n\n""")
for dlist in definlist:
for d in dlist:
fout.write(d)
# close file and print message
fout.close()
print "\n--- output written to", fname, "---\n"
# #################################################################### #
print """
.......................................................
: FACSIMILE REACTION RATE v2.6 :
: :
: generate the FACSIMILE code to calculate the rates :
: of production and destruction of selected species :
: :
: >>> selected species in 'facsimile_rate.in' <<< :
:.....................................................:
"""
# open input and output files
print "enter name of the file with the chemical mechanism"
filename = raw_input("filename: ")
fin = open(filename, "r")
fileoutP = filename + ".rateP.out"
fileoutD = filename + ".rateD.out"
# read input file in string
facstring = fin.read()
# call 'facmecha' function
# the function returns a list of the reactions in the mechanism
mechanism = []
mechanism = facsimile_funcs.facmecha(facstring)
# input species of interest (all species in upper case)
finput = open("facsimile_rate.in", "r")
listspecies = facsimile_funcs.openlist("facsimile_rate.in")
for i in range(len(listspecies)):
listspecies[i] = listspecies[i].upper()
# select the rates of the reactions involving a specific reactant
# (reactant name in upper case)
print "\nwrite only the rates of reactions that include a specific reactant?"
print "[default: write the rates of all reactions]"
reactant = raw_input("reactant name [press enter for default]: ")
if reactant != '':
reactant = "*" + reactant.upper() + "*"
# call 'ropa' function
# the function returns a list with this output:
# [0 [list of production parameters]
# 1 [list of destruction parameters]
# 2 [list of production expressions]
# 3 [list of destruction expressions]
# 4 [list of number codes of production reactions]
# 5 [list of number codes of destruction reactions]
# 6 [list of definitions of production parameters]
# 7 [list of definitions of destruction parameters] ]
paramPlist = []; totparamPlist = []; expressPlist = []; definPlist = []
paramDlist = []; totparamDlist = []; expressDlist = []; definDlist = []
for species in listspecies:
ratelist = []
ratelist = facsimile_funcs.ropa(mechanism,species)
paramPlist.append(ratelist[0])
paramDlist.append(ratelist[1])
totparamPlist.append("P_" + species.lower() + "_tot")
totparamDlist.append("D_" + species.lower() + "_tot")
expressPlist.append(ratelist[2])
expressDlist.append(ratelist[3])
definPlist.append(ratelist[6])
definDlist.append(ratelist[7])
# write output files
outputrates('p',fileoutP,paramPlist,totparamPlist,expressPlist,definPlist)
outputrates('d',fileoutD,paramDlist,totparamDlist,expressDlist,definDlist)
# close files
fin.close()
finput.close()