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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 | def make_chemdf(re_table, ofname):
"""
Make the function chemdf for calculating the chemical production/loss term
"""
chem_dict = {}
reac_dict = {} # reaction dict. packed with v_i
exp_reac_dict = {} # explicit reaction dict.
i = -1
j = -1 # index of forward reaction(odd number:1,3,5,...)
count = 0 # count for even/odd term in v_exp
reac_list = []
rate_dict = {}
sp_rate = {} # to store each term of prod and loss individually
re_sp_dic = {}
re_reac_prod = {} # store the products and the reactants for reaction j (without M)
re_reac_prod_wM = {} # same as re_reac_prod but including M
re_dict_str = 're_dict = {'
re_wM_dict_str = 're_wM_dict = {' # including M
for line in re_table.splitlines():
"""
reac : e.g. [[1, 'H'], [1, 'H'], [1, 'M']]
reac_args: e.g. ['H2', 'H', 'M']
"""
# skip the space and #
if line == '':
continue
elif line[0] == '#':
continue
else:
reac = []
mol_reac = []
mol_prod = []
prod = []
rate_exp = ''
rate_str = ''
v_str = ''
v_exp = '' # to store the explicit expression of v_i (for jacobian)
# sp_rate = [] # to store species
# R = True:reactants R = False:products
R = True # if reads '->'
N = False
# Need to take car of M!!!
for term in line.split():
if term == '+':
continue
elif term == '->':
# R=True:reactants R=False:products
R = False
continue
# mol_list = [stoi-number, species name]
mol_list = term.split('*')
if len(mol_list) == 1:
mol_list = [1] + mol_list
else:
mol_list = [int(mol_list[0])] + mol_list[1:]
mol = mol_list[1]
stoi = int(mol_list[0])
# check if the species is already included
if mol not in chem_dict and not term == 'M':
i += 1
chem_dict.update({mol: i})
reac_dict.update({chem_dict[mol]: ''})
exp_reac_dict.update({chem_dict[mol]: ''})
# creating a new list for new species
sp_rate[mol] = []
# if R is true, it's the reactants
if R:
reac.append(mol_list)
mol_reac.append(mol)
else:
prod.append(mol_list)
mol_prod.append(mol)
j += 2
reac_args = list(set(mol_reac + mol_prod)) # Remove repeating elements
# because set exclude duplicates
# v_i() is the rate equation function for i
rate_str = 'v_' + str(j) + '(k, M, '
reac_noM = [ele for ele in reac if not ele[1] == 'M']
prod_noM = [ele for ele in prod if not ele[1] == 'M']
reac_args_noM = [ele for ele in reac_args if not ele == 'M']
# store the products and the reactants in the 1st and 2nd element for reaction j (without M)
re_reac_prod[j] = [
[ele[1] for ele in reac if not ele[1] == 'M'],
[ele[1] for ele in prod if not ele[1] == 'M'],
]
# store the products and the reactants in the 1st and 2nd element for reaction j (with M)
re_reac_prod_wM[j] = [[ele[1] for ele in reac], [ele[1] for ele in prod]]
# skip line
if j % 51 == 0:
re_dict_str += '\n'
re_wM_dict_str += '\n'
re_dict_str += str(j) + ':' + str(re_reac_prod[j]) + ', '
# reverse the list "re_reac_prod[j]" for the reverse reaction
re_dict_str += str(j + 1) + ':' + str(re_reac_prod[j][::-1]) + ', '
# with M
re_wM_dict_str += str(j) + ':' + str(re_reac_prod_wM[j]) + ', '
# reverse
re_wM_dict_str += str(j + 1) + ':' + str(re_reac_prod_wM[j][::-1]) + ', '
for term in [ele for ele in reac_args if not ele == 'M']:
rate_str += 'y[' + str(chem_dict[term]) + '], '
rate_str = rate_str[0:-2] + ')'
v_exp += 'k[' + str(j) + ']*'
for term in reac:
if term[0] != 1:
if term[1] == 'M':
v_exp += term[1] + '**' + str(term[0]) + '*'
else:
v_exp += (
'y[' + str(chem_dict[term[1]]) + ']' + '**' + str(term[0]) + '*'
)
else:
if term[1] == 'M':
v_exp += term[1] + '*'
else:
v_exp += 'y[' + str(chem_dict[term[1]]) + ']*'
v_exp = v_exp[0:-1] # Delete the last '*'
fv_exp = v_exp
v_exp += ' - k[' + str(j + 1) + ']*'
b_exp = ' k[' + str(j + 1) + ']*'
for term in prod:
if term[0] != 1:
if term[1] == 'M':
v_exp += term[1] + '**' + str(term[0]) + '*'
b_exp += term[1] + '**' + str(term[0]) + '*'
else:
v_exp += (
'y[' + str(chem_dict[term[1]]) + ']' + '**' + str(term[0]) + '*'
)
b_exp += (
'y[' + str(chem_dict[term[1]]) + ']' + '**' + str(term[0]) + '*'
)
else:
if term[1] == 'M':
v_exp += term[1] + '*'
b_exp += term[1] + '*'
else:
v_exp += 'y[' + str(chem_dict[term[1]]) + ']*'
b_exp += 'y[' + str(chem_dict[term[1]]) + ']*'
v_exp = v_exp[0:-1] # Delete the last '*'
rv_exp = b_exp[0:-1]
for term in reac_noM:
# term[0] os the stoi-number of the species
reac_dict[chem_dict[term[1]]] += ' -' + str(term[0]) + '*' + rate_str
# for each term of prod and loss individually
sp_rate[term[1]].append(' -' + str(term[0]) + '*' + fv_exp)
sp_rate[term[1]].append(' +' + str(term[0]) + '*' + rv_exp)
if term[0] == 1:
exp_reac_dict[chem_dict[term[1]]] += ' -' + '(' + v_exp + ')'
count += 1
else:
exp_reac_dict[chem_dict[term[1]]] += (
' -' + str(term[0]) + '*(' + v_exp + ')'
)
count += 1
for term in prod_noM:
reac_dict[chem_dict[term[1]]] += ' +' + str(term[0]) + '*' + rate_str
sp_rate[term[1]].append(' +' + str(term[0]) + '*' + fv_exp)
sp_rate[term[1]].append(' -' + str(term[0]) + '*' + rv_exp)
if term[0] == 1:
exp_reac_dict[chem_dict[term[1]]] += ' +' + '(' + v_exp + ')'
count += 1
else:
exp_reac_dict[chem_dict[term[1]]] += (
' +' + str(term[0]) + '*(' + v_exp + ')'
)
count += 1
v_str = '#' + line + '\n'
v_str += 'v_' + str(j) + ' = lambda k, M, '
for term in reac_args_noM:
v_str += term + ', '
v_str = v_str[0:-2] + ' : '
v_str += 'k[' + str(j) + ']*'
for term in reac:
if term[0] != 1:
v_str += term[1] + '**' + str(term[0]) + '*'
else:
v_str += term[1] + '*'
v_str = v_str[0:-1] # Delete the last '*'
v_str += ' - k[' + str(j + 1) + ']*'
for term in prod:
if term[0] != 1:
v_str += term[1] + '**' + str(term[0]) + '*'
else:
v_str += term[1] + '*'
v_str = v_str[0:-1]
reac_list.append(v_str)
# ouput of each single rate from k1...
rate_exp += 'k[' + str(j) + ']*'
for term in reac:
if term[1] == 'M':
rate_exp += 'M*'
else:
if term[0] == 1:
rate_exp += 'y[' + str(chem_dict[term[1]]) + ']*'
else:
rate_exp += 'y[' + str(chem_dict[term[1]]) + ']**' + str(term[0]) + '*'
rate_exp = rate_exp[0:-1] # Delete the last '*'
rate_dict[j] = rate_exp
rate_exp = ''
rate_exp += 'k[' + str(j + 1) + ']*' # j+1 even number for reverse index
for term in prod:
if term[1] == 'M':
rate_exp += 'M*'
else:
if term[0] == 1:
rate_exp += 'y[' + str(chem_dict[term[1]]) + ']*'
else:
rate_exp += 'y[' + str(chem_dict[term[1]]) + ']**' + str(term[0]) + '*'
rate_exp = rate_exp[0:-1] # Delete the last '*'
rate_dict[j + 1] = rate_exp
re_dict_str = re_dict_str[:-2] # delet the last ','
re_dict_str += '}\n'
re_wM_dict_str = re_wM_dict_str[:-2]
re_wM_dict_str += '}\n'
# print re_dict_str
# save output
chem_dict_r = {}
spec_list = []
ofstr = '#!/usr/bin/env python3 \n\n'
ofstr += '# This file was generated by VULCAN \n\n'
ofstr += 'import numpy as np \n'
ofstr += 'from .phy_const import kb, Navo\n\n'
ofstr += "'''\n## Reaction ##\n\n"
ofstr += re_table + '\n\n'
ofstr += '## Mapping ##\n\n'
for term in chem_dict:
chem_dict_r.update({chem_dict[term]: term})
for term in reac_dict:
ofstr += chem_dict_r[term] + ': y[' + str(term) + '], '
ofstr += '\n\n'
for term in reac_dict:
ofstr += chem_dict_r[term] + '\t' + str(term) + '\t' + reac_dict[term] + '\n'
for i in chem_dict_r:
spec_list.append(chem_dict_r[i])
ofstr += "'''\n\n"
ofstr += '#species list\n'
ofstr += 'spec_list = ' + str(spec_list)
ofstr += '\n# the total number of species'
ofstr += '\nni = ' + str(len(chem_dict.keys()))
ofstr += '\n# the total number of reactions (forward and reverse)'
ofstr += '\nnr = ' + str(len(rate_dict.keys()))
ofstr += (
'\n\n# store the products and the reactants in the 1st and the 2nd element for reaction j (without M)\n'
+ re_dict_str
)
ofstr += (
'\n\n# store the products and the reactants in the 1st and the 2nd element for reaction j (with M)\n'
+ re_wM_dict_str
)
ost = '\n\ndef chemdf(y, M, k): \n' # Note: making M as input!!!
ost += '\t y = np.transpose(y) \n'.expandtabs(3)
ost += '\t dydt = np.zeros(shape=y.shape) \n'.expandtabs(3)
for num in reac_dict:
ost += '\t dydt['.expandtabs(3) + str(num) + '] = ' + reac_dict[num] + '\n'
ost += '\t dydt = np.transpose(dydt) \n'.expandtabs(3)
ost += '\t return dydt \n\n'.expandtabs(3)
ost += 'def df(y, M, k):\n'
ost += '\t df_list = [] \n'.expandtabs(3)
for num in exp_reac_dict:
ost += '\t df_list.append( '.expandtabs(3) + exp_reac_dict[num] + ' )\n'
ost += '\t return df_list \n\n'.expandtabs(3)
ofstr += ost
for term in reac_list:
ofstr += term + '\n\n\n'
# for rate analysis
ost = 'def rate_ans(sp): \n'
ost += '\t rate_str = {}\n'.expandtabs(3)
ost += '\t re_sp_dic = {}\n'.expandtabs(3)
for sp in sp_rate:
ost += ' rate_str["' + sp + '"] = ['
re_sp_dic[sp] = []
for i in sp_rate[sp]:
ost += i
ost += ','
start, end = False, False
for n, letter in enumerate(i):
if letter == 'k' and not start:
k_start = n + 2
start = True
elif letter == ']' and start and not end:
k_end = n
end = True
re_sp_dic[sp].append(int(i[k_start:k_end]))
ost = ost[0:-1]
ost += ']'
ost += '\n'
ost += '\t return np.array(rate_str[sp]) \n\n'.expandtabs(3)
ofstr += ost
log.debug(f'Writing ofstr to {ofname}')
with open(ofname, 'w') as of:
of.write(ofstr)
# return (ni, nr, the list of species)
return (len(chem_dict.keys()), len(rate_dict.keys()), chem_dict.keys())
|