forked from loftytopping/PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_coeff_conversion.py
More file actions
332 lines (278 loc) · 12.8 KB
/
rate_coeff_conversion.py
File metadata and controls
332 lines (278 loc) · 12.8 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
##########################################################################################
# #
# Scripts to convert a text string definition of a rate coefficient and convert to #
# a format that can be used by a Python and/or Fortran environment #
# #
# #
# Copyright (C) 2018 David Topping : david.topping@manchester.ac.uk #
# : davetopp80@gmail.com #
# Personal website: davetoppingsci.com #
# #
# All Rights Reserved. #
# This file is part of PyBox. #
# #
# PyBox is free software: you can redistribute it and/or modify it under #
# the terms of the GNU General Public License as published by the Free Software #
# Foundation, either version 3 of the License, or (at your option) any later #
# version. #
# #
# PyBox is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
# details. #
# #
# You should have received a copy of the GNU General Public License along with #
# PyBox. If not, see <http://www.gnu.org/licenses/>. #
# #
##########################################################################################
# Developed using the Anaconda Python 3 distribution and with the Assimulo ODE solver #
# suite: http://www.jmodelica.org/assimulo #
##########################################################################################
# This script takes a chemical equation file, following the standard KPP format, and generates
# information used to create an ODE instance to solver for set of specific conditions.
# The rules used to generate this instance have been based on standard KPP examples.
# The left hand / right hand descriptions are relatively easy.
# The definition of a rate coefficient however might change with time.
# For this reason, this file is likely to change until a generic variant can be created that
# covers all expected formats. This is dealt with in a seperate file
import pdb
def convert_rate_mcm(rate_dict):
""" This function takes the defintions of rate coefficients and converts to Python command
inputs:
• rate_dict - parsed string representations of rate coefficients
outputs:
• rate_dict - converted defintions for use in Python
"""
# KPP formats for a specific mechanism also tend to use pre-requisite constants.
# For example, the MCM often uses formats for rate coefficients as follows:
# K234*J(0)*J(3)
# J(4)+J(5)*EXP(234/TEMP)
# where K234, J(2) etc is a given constant
# In this case the string is saved, but conversions for floats and 'exp' expressions
# carried out and the expression then evaluated as a whole. For example
# J(4)*2.34D-4*EXP(234/TEMP)
# is converted to
# J(4)*2.34E-4*numpy.exp(234.0/TEMP)
# which is then processed as an entire command where needed. Specific variables are saved
# in a seperate file, updated as the MCM updates
#Store conversions in lists
math_list=[
('dabs','numpy.abs'),
('dsqrt','numpy.sqrt'),
('dlog','numpy.log'),
('LOG','numpy.log'),
('EXP','numpy.exp')
]
map_list=[('D-','E-'),
('D+','E+')
]
afterText = ['numpy.abs','numpy.sqrt','numpy.log','numpy.exp']
new_rate_full=[]
for equation_step, rate_full in rate_dict.items():
for operation in math_list:
temp_text=rate_full.replace(operation[0],operation[1])
for syntax in map_list:
temp_text=temp_text.replace(syntax[0],syntax[1])
#Replace all () with [] in the first instance
temp_text=temp_text.replace('(','[').replace(')',']')
#Now deal with replacing generic () with [] unless it is a numpy command
#Find the first occurance of [ and replace back to ) if numpy command
searchText1 = '['
searchText2 = ']'
temp_text_list=list(temp_text)
for substr in afterText:
try:
after_index = temp_text.index(substr)
index1=temp_text.find(searchText1, after_index)
index2=temp_text.find(searchText2, after_index)
temp_text_list[index1]='('
temp_text_list[index2]=')'
except:
pass
temp_text=''.join(temp_text_list)
#new_rate_full.append(temp_text)
#if print_options==1:
# print temp_text
rate_dict[equation_step]=temp_text
return rate_dict
def convert_rate_mcm_numba(rate_dict):
""" This function takes the defintions of rate coefficients and converts to Python [Numba] command
inputs:
• rate_dict - parsed string representations of rate coefficients
outputs:
• rate_dict - converted defintions for use in Python
"""
# KPP formats for a specific mechanism also tend to use pre-requisite constants.
# For example, the MCM often uses formats for rate coefficients as follows:
# K234*J(0)*J(3)
# J(4)+J(5)*EXP(234/TEMP)
# where K234, J(2) etc is a given constant
# In this case the string is saved, but conversions for floats and 'exp' expressions
# carried out and the expression then evaluated as a whole. For example
# J(4)*2.34D-4*EXP(234/TEMP)
# is converted to
# J(4)*2.34E-4*numpy.exp(234.0/TEMP)
# which is then processed as an entire command where needed. Specific variables are saved
# in a seperate file, updated as the MCM updates
#Store conversions in lists
math_list=[
('dabs','numba_abs'),
('dsqrt','numba_sqrt'),
('dlog','numba_log'),
('LOG','numba_log'),
('EXP','numba_exp')
]
map_list=[('D-','E-'),
('D+','E+'),
('D1','E+1'),
('D2','E+2'),
('D3','E+3'),
('D4','E+4'),
('D5','E+5'),
('D6','E+6'),
('D7','E+7'),
('D8','E+8'),
('D9','E+9'),
]
afterText = ['numba_abs','numba_sqrt','numba_log','numba_exp']
new_rate_full=[]
for equation_step, rate_full in rate_dict.items():
for operation in math_list:
temp_text=rate_full.replace(operation[0],operation[1])
for syntax in map_list:
temp_text=temp_text.replace(syntax[0],syntax[1])
temp_text_list=list(temp_text)
indices= []
index = -1
while True:
index = temp_text.find('J(', index + 1)
if index == -1:
break # All occurrences have been found
indices.append(index)
#Replace () with [] if associated with J for photolysis rates
try:
for num in indices:
#after_index=temp_text.index('J(')
#pdb.set_trace()
index1=temp_text.find('(', num)
index2=temp_text.find(')', num)
temp_text_list[index1]='['
temp_text_list[index2]=']'
except:
pass
temp_text=''.join(temp_text_list)
##Replace all () with [] in the first instance
#temp_text=temp_text.replace('(','[').replace(')',']')
##Now deal with replacing generic () with [] unless it is a numpy command
##Find the first occurance of [ and replace back to ) if numpy command
#searchText1 = '['
#searchText2 = ']'
#temp_text_list=list(temp_text)
#for substr in afterText:
# try:
# after_index = temp_text.index(substr)
# index1=temp_text.find(searchText1, after_index)
# index2=temp_text.find(searchText2, after_index)
# temp_text_list[index1]='('
# temp_text_list[index2]=')'
# except:
# pass
# temp_text=''.join(temp_text_list)
#new_rate_full.append(temp_text)
#if print_options==1:
# print temp_text
rate_dict[equation_step]=temp_text
return rate_dict
def convert_rate_mcm_fortran(rate_dict):
""" This function takes the defintions of rate coefficients and converts to Fortran command
inputs:
• rate_dict - parsed string representations of rate coefficients
outputs:
• rate_dict - converted defintions for use in Fortran
"""
# KPP formats for a specific mechanism also tend to use pre-requisite constants.
# For example, the MCM often uses formats for rate coefficients as follows:
# K234*J(0)*J(3)
# J(4)+J(5)*EXP(234/TEMP)
# where K234, J(2) etc is a given constant
# In this case the string is saved, but conversions for floats and 'exp' expressions
# carried out and the expression then evaluated as a whole. For example
# J(4)*2.34D-4*EXP(234/TEMP)
# is converted to
# J(4)*2.34E-4*numpy.exp(234.0/TEMP)
# which is then processed as an entire command where needed. Specific variables are saved
# in a seperate file, updated as the MCM updates
#Store conversions in lists
math_list=[
('dabs','ABS'),
('dsqrt','SQRT'),
('dlog','LOG')
]
map_list=[('D-','E-'),
('D+','E+'),
('D1','E+1'),
('D2','E+2'),
('D3','E+3'),
('D4','E+4'),
('D5','E+5'),
('D6','E+6'),
('D7','E+7'),
('D8','E+8'),
('D9','E+9'),
]
afterText = ['ABS','SQRT','LOG','EXP']
new_rate_full=[]
for equation_step, rate_full in rate_dict.items():
for operation in math_list:
temp_text=rate_full.replace(operation[0],operation[1])
for syntax in map_list:
temp_text=temp_text.replace(syntax[0],syntax[1])
temp_text_list=list(temp_text)
#indices= []
#index = -1
#while True:
# index = temp_text.find('J(', index + 1)
# if index == -1:
# break # All occurrences have been found
# indices.append(index)
##Replace () with [] if associated with J for photolysis rates
#try:
# for num in indices:
# #after_index=temp_text.index('J(')
# #pdb.set_trace()
# index1=temp_text.find('(', num)
# index2=temp_text.find(')', num)
# temp_text_list[index1]='['
# temp_text_list[index2]=']'
#except:
# pass
temp_text=''.join(temp_text_list)
#new_rate_full=[]
#for equation_step, rate_full in rate_dict.items():
#
# for operation in math_list:
# temp_text=rate_full.replace(operation[0],operation[1])
# for syntax in map_list:
# temp_text=temp_text.replace(syntax[0],syntax[1])#
#Replace all () with [] in the first instance
# temp_text=temp_text.replace('(','[').replace(')',']')
#Now deal with replacing generic () with [] unless it is a numpy command
#Find the first occurance of [ and replace back to ) if numpy command
# searchText1 = '['
# searchText2 = ']'
# temp_text_list=list(temp_text)
# for substr in afterText:
# try:
# after_index = temp_text.index(substr)
# index1=temp_text.find(searchText1, after_index)
# index2=temp_text.find(searchText2, after_index)
# temp_text_list[index1]='('
# temp_text_list[index2]=')'
# except:
# pass
# temp_text=''.join(temp_text_list)
#new_rate_full.append(temp_text)
#if print_options==1:
# print temp_text
rate_dict[equation_step]=temp_text
return rate_dict