forked from loftytopping/PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParse_eqn_file.py
More file actions
3652 lines (3291 loc) · 207 KB
/
Parse_eqn_file.py
File metadata and controls
3652 lines (3291 loc) · 207 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##########################################################################################
# #
# Scripts to build model from chemical mechanism file and/or extract species #
# #
# #
# 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 #
##########################################################################################
# Please note - All mechanism work here is based on the MCM. This includes definitions of contribution
# to RO2 concetrations etc.
import collections
import pdb
import re
import pybel # Needed for calculating olecular properties in aerosol model [Parse_eqn_file used for both gas and aerosol]
import numpy as np
import datetime
from scipy.sparse import lil_matrix
import os
from xml.dom import minidom
import multiprocessing
def extract_mechanism(filename,print_options): #Change to the filename of interest
""" This reads the mechanism equation file, held in the the 'mechanisms_file' folder and extracts
reactants, products and a definition of the rate coefficient for each reaction. This is the information
that is then stored in dictionaries used to create files that define the ordinary differential equations
[ODEs] being solved by PyBox.
inputs:
• filename - name of mechanism file [pre .eqn.txt extension]
• print_options - flag to define verbosity of extraction process
outputs:
• output_dict['rate_dict']=rate_dict - string of reaction rate coefficient [to be converted]
• output_dict['rate_dict_reactants'] - rate_dict_reactants [reactants involved in each reaction]
• output_dict['loss_dict']=loss_dict - compounds lost in each reaction
• output_dict['gain_dict']=gain_dict - compounds produced in each reaction
• output_dict['stoich_dict']=stoich_dict - stoichiometry of each reactant/product in each reaction
• output_dict['species_dict']=species_dict - dict listing all compounds
• output_dict['species_dict2array']=species_dict2array - dict that maps compound name to array index for use in numerical simulations
• output_dict['species_hess_data']=species_hess_data - hessian compound listing [not used in current version]
• output_dict['species_hess_loss_data']=species_hess_loss_data - hessian loss information [not used in current version]
• output_dict['species_hess_gain_data']=species_hess_gain_data - hessian gain information [not used in current version]
• output_dict['max_equations']=max_equations - total nummer of reactions
"""
# The following steps are used:
# 1) Read the equation file
# --------------------------------------------------------------------------------------------
#[1] - Read the species equation file. Count how many equations there are
# Find all instances of curly brackets {} which enclose an equation number
print("Opening file ", str(filename), "for parsing")
#pdb.set_trace()
text=open(filename,'rU')
with open (filename, "r") as myfile:
data=myfile.read().replace('\n', '')
char1='{'
char2='}'
max_equations=int(re.findall(r"\{(.*?)\}",data)[-1].strip('.'))
print("Calculating total number of equations = ",max_equations)
# --------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------
#[2] - Cycle through all equations and extract:
# - Reactants and stochiometric coefficients
# - Products and stochiometric coefficients
# - Rate coefficients:
# = Coefficients and typical forms used in MCM models
#
# This information is stored in dictionaries
# The following loop looks into the .eqn file and finds the text sat between each equation.
# For example, the first step pulls out the equation information for equation 1, as defined
# by the KPP standard. We cannot just parse line by line since equations can span multiple
# lines according to KPP files. Each equation 'line' can be defined to start after a '}' and
# end with a ';' For eaxmple:
#{1} NO2 + hv = NO + O : 0.533 ;
#{2} O + O2 = O3 : 2.183E-5 ;
#{3} NO + 2.0O3 = NO2 + O2 : 26.59 ;
#{4} RH + OH = RO2 + H2O : 3.775E+3 ;
#
#Dictionaries used to store information about reactants/products/equations
#reaction_dict=collections.defaultdict(lambda: collections.defaultdict())
rate_dict=collections.defaultdict(lambda: collections.defaultdict())
#rate_def=collections.defaultdict()
loss_dict=collections.defaultdict(lambda: collections.defaultdict())
gain_dict=collections.defaultdict(lambda: collections.defaultdict())
stoich_dict=collections.defaultdict(lambda: collections.defaultdict())
rate_dict_reactants=collections.defaultdict(lambda: collections.defaultdict())
species_dict=collections.defaultdict()
species_dict2array=collections.defaultdict()
species_hess_data=collections.defaultdict()
species_hess_loss_data=collections.defaultdict()
species_hess_gain_data=collections.defaultdict()
# Extract all lines with equations on as a list
eqn_list=re.findall(r"\}(.*?)\;",data)
#Create an integer that stores number of unque species
species_step=0
#pdb.set_trace()
print("Parsing each equation")
for equation_step in range(max_equations):
equation_full=eqn_list[equation_step] #pulls out text in between {n}..{n+1}
equation_full=re.sub('\t','',equation_full)
equation=equation_full.split(':',1)[0].split('=',1) #split the line into reactants and products
reactants=equation[0].split('+') # extract content to the left of the previous split [reactants]
reactants= [x.strip(' ') for x in reactants] #strip away all whitespace
products=equation[1].split('+') # extract content to the right of the previous split [products]
products = [x.strip(' ') for x in products] #strip away all whitespace
#pdb.set_trace()
#At the moment, we have not seperated the reactant/product from its stochiometric value
##rate_full=equation_full.split(':',1)[1].rsplit('\t',1)[1].split(';',1)[0] #pulls out the rate
rate_full=equation_full.split(':',1)[1]
#but as a text string
rate_full=rate_full.strip() #[x.strip(' ') for x in rate_full]
#rate_dict[equation_step]=rate_full
rate_dict[equation_step]="".join(rate_full.split())
#This assumes everyline, as in KPP, finishes with a ';' character
#pdb.set_trace()
#print "Extracting equation :"
#print equation_step
#if print_options['Full_eqn']==1:
# print "Full equation extracted"
# print equation_full
#if print_options['Full_eqn']==1:
# print "Rate extracted :"
# print rate_full
# Now cycle through all reactants and products, splitting the unique specie from its stochiometry
# This information is then stored in dictionaries for use in the ODE solver
# At the moment the reactants, and products, include joint stoichiometric information.
# This means we need to identify these numbers and then split the string again to ensure
# the specie always remains unique. Thus, we may have saved something like
# '2.0NO2' or '5ISOPOOH'. The use of integer versus float can vary so have to assume no care taken
# in being consistent
reactant_step=0 #used to identify reactants by number, for any given reaction
product_step=0
for reactant in reactants: #This are the 'reactants' in this equation
reactant=reactant.split()[0] #remove all tables, newlines, whitespace
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# - Extract stochiometry and unique reactant identifier
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try: #Now try to extract a stochiometric coefficient if given
temp=re.findall(r"[-+]?\d*\.\d+|\d+|\d+",reactant) #This extracts all numbers either side
#of some text.
stoich=temp[0] #This selects the first number extracted, if any.
# Now need to work out if this value is before the variable
# If after, we ignore this. EG. If '2' it could come from '2NO2' or just 'NO2'
# If len(temp)==1 then we only have one number and can proceed with the following
if len(temp)==1:
if reactant.index(stoich) == 0 :
reactant=reactant.split(stoich,1)[1]
stoich=float(stoich)
else:
stoich=1.0
elif len(temp)>1:
#If this is the case, we need to ensure the reactant extraction is unique. For example
#If the string is '2NO2' the above procedure extracts 'NO' as the unique reactant.
#We therefore need to ensure that the reactant is 'NO2'. To do this we cut the value
#in temp[0] away from the original string. Lets assume that we can attach the first
#part of the text with the second number in temp. Thus
if reactant.index(stoich) == 0 :
reactant=reactant.split(stoich,1)[1]+temp[1]
stoich=float(stoich)
else:
stoich=1.0
except:
stoich=1.0
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# - Store stoichiometry, species flags and hessian info in dictionaries
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Now store the stoichiometry and reactant in dictionaries
if reactant not in ['hv']:
stoich_dict[equation_step][reactant_step]=stoich
rate_dict_reactants[equation_step][reactant_step]=reactant
# -- Update species dictionaries --
if reactant not in species_dict.values(): #check to see if entry already exists
species_dict[species_step]=reactant # useful for checking all parsed species
species_dict2array[reactant]=species_step #useful for converting a dict to array
species_step+=1
# -- Update hessian dictionaries --
if reactant not in species_hess_loss_data:
species_hess_loss_data[reactant]=[]
species_hess_loss_data[reactant].append(equation_step) #so from this we can work out a dx/dy
# -- Update loss dictionaries --
if equation_step in loss_dict[reactant]:
loss_dict[reactant][equation_step]+=stoich
else:
loss_dict[reactant][equation_step]=stoich
reactant_step+=1
if len(products) > 0:
for product in products: #This are the 'reactants' in this equation
try:
product=product.split()[0] #remove all tables, newlines, whitespace
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# - Extract stochiometry and unique product identifier
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try: #Now try to extract a stochiometric coefficient if given
temp=re.findall(r"[-+]?\d*\.\d+|\d+|\d+",product) #This extracts all numbers either side
#of some text.
stoich=temp[0] #This selects the first number extracted, if any.
# Now need to work out if this value is before the variable
# If after, we ignore this. EG. If '2' it could come from '2NO2' or just 'NO2'
# If len(temp)==1 then we only have one number and can proceed with the following
if len(temp)==1:
if product.index(stoich) == 0 :
product=product.split(stoich,1)[1]
stoich=float(stoich)
else:
stoich=1.0
elif len(temp)>1:
#If this is the case, we need to ensure the reactant extraction is unique. For example
#If the string is '2NO2' the above procedure extracts 'NO' as the unique reactant.
#We therefore need to ensure that the reactant is 'NO2'. To do this we cut the value
#in temp[0] away from the original string. Lets assume that we can attach the first
#part of the text with the second number in temp. Thus
if product.index(stoich) == 0 :
product=product.split(stoich,1)[1]+temp[1]
stoich=float(stoich)
else:
stoich=1.0
except:
stoich=1.0
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# - Store stoichiometry, species flags and hessian info in dictionaries
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Now store the stoichiometry and reactant in dictionaries
if product not in ['hv']:
# -- Update species dictionaries --
if product not in species_dict.values(): #check to see if entry already exists
species_dict[species_step]=product # useful for checking all parsed species
species_dict2array[product]=species_step #useful for converting a dict to array
species_step+=1
# -- Update hessian dictionaries --
if product not in species_hess_gain_data:
species_hess_gain_data[product]=[]
species_hess_gain_data[product].append(equation_step) #so from this we can work out a dx/dy
# -- Update loss dictionaries --
if equation_step in gain_dict[reactant]:
gain_dict[product][equation_step]+=stoich
else:
gain_dict[product][equation_step]=stoich
product_step+=1
except:
pass
#pdb.set_trace()
# --------------------------------------------------------------------------------------------
print("Total number of species = ", len(species_dict.keys()))
print("Saving all equation information to dictionaries")
output_dict=dict()
#output_dict['reaction_dict']=reaction_dict
output_dict['rate_dict']=rate_dict
output_dict['rate_dict_reactants']=rate_dict_reactants
#output_dict['rate_def']=rate_def
output_dict['loss_dict']=loss_dict
output_dict['gain_dict']=gain_dict
output_dict['stoich_dict']=stoich_dict
output_dict['species_dict']=species_dict
output_dict['species_dict2array']=species_dict2array
output_dict['species_hess_data']=species_hess_data
output_dict['species_hess_loss_data']=species_hess_loss_data
output_dict['species_hess_gain_data']=species_hess_gain_data
output_dict['max_equations']=max_equations
return output_dict
def extract_smiles_species(output_dict, SMILES_filename):
""" Function to map the SMILES string of a given molecule to its name.
For the MCM, this information is stored in an XML file.
inputs:
• SMILES_filename - the name of the .xml file you need to provide [stored in root directory]
outputs:
• output_dict['SMILES_dict']=SMILES_dict - dict of SMILES strings for a given compound name
• output_dict['Pybel_object_dict']=Pybel_object_dict - Pybel object of each SMILES string [needed for properties calculated in UManSysProp]
"""
print('Mapping species names to SMILES and Pybel objects')
SMILES_dict=collections.defaultdict()
Pybel_object_dict=collections.defaultdict()
species_dict=output_dict['species_dict']
# parse an xml file by name - use this to store species name versus SMILES
dom = minidom.parse(SMILES_filename)
for node in dom.getElementsByTagName('species'):
species_name=node.attributes['species_name'].value
if species_name not in ['OH','HO2','O3','hv']:
#Now check to see if the species is in your dictionary extracted from the mechanism
if species_name in species_dict.values():
#pdb.set_trace()
#species_SMILES=node.attributes['smiles'].value
#Right, the following is an overkill way t extract the SMILES. If the XML was written correctly, and the SMILES were in quotation marks, the node. method would be all that is needed
try:
species_SMILES=re.search(r'<smiles>(.*?)</smiles>',node.getElementsByTagName('smiles').item(0).toxml()).group(1)
except:
print('No SMILES entry for species ',species_name)
continue
SMILES_dict[species_name]=species_SMILES
Pybel_object=pybel.readstring('smi',species_SMILES)
Pybel_object_dict[species_SMILES]=Pybel_object
output_dict['SMILES_dict']=SMILES_dict
output_dict['Pybel_object_dict']=Pybel_object_dict
#pdb.set_trace()
#return species mapped to SMILES - bare in mind this might not be complete.
return output_dict
def extract_species(filename):
""" Function used to test box-models by extracting from a pre-defined chemical mechanism snapshot.
In PyBox, this function is used within the 'Fixed_yield' folder variants
inputs:
• filename - File that defines SMILES strings and concetrations
outputs:
• output_dict['species_dict']=species_dict - dict of all compounds
• output_dict['species_dict2array']=species_dict2array - dict that maps compound name to array index for use in numerical simulations
• output_dict['Pybel_object_dict']=Pybel_object_dict - dict of Pybel objects for each compound [required for UManSysProp]
• output_dict['smiles_array']=smiles_array - array of all SMILES strings
• output_dict['SMILES_dict']=SMILES_dict - dict of all SMILES strings
• output_dict['concentration_array']=concentration_array - extracted concetration from [filename]
• output_dict['species_number']=species_step - total number of compounds
• output_dict['Pybel_object_activity']=Pybel_object_activity - special PyBel dict used in activity coefficients [not used in this version]
• output_dict['concentration_dict']=concentration_dict - dict of compound concetrations
"""
#This function is used to test box-models by extracting from a pre-defined chemical mechanism snapshot
smiles_array=[]
concentration_array=[]
concentration_dict=collections.defaultdict()
SMILES_dict=collections.defaultdict()
Pybel_object_dict=collections.defaultdict()
Pybel_object_activity=collections.defaultdict()
species_dict=collections.defaultdict()
species_dict2array=collections.defaultdict()
#text=open(filename,'rU')
#Create an integer that stores number of unique species
species_step=0
for line in open(filename, "r"):
input = line.split()
# Keep a list of the information
smiles=input[0]
#pdb.set_trace()
if smiles not in species_dict.values():
smiles_array.append(smiles)
SMILES_dict[smiles]=smiles
concentration_array.append(float(input[1]))
# Now create Pybel objects which are used in all property predictive techniquexs
Pybel_object=pybel.readstring('smi',input[0])
#Pybel_object=pybel.readstring(b'smi',str(input[0],'utf-8'))
Pybel_object_dict[smiles]=Pybel_object
Pybel_object_activity[Pybel_object]=float(input[1])
species_dict[species_step]=smiles # useful for checking all parsed species
species_dict2array[smiles]=species_step #useful for converting a dict to array
concentration_dict[Pybel_object]=float(input[1])
species_step+=1
print("Total number of species = ", species_step)
print("Saving all equation information to dictionaries")
output_dict=dict()
output_dict['species_dict']=species_dict
output_dict['species_dict2array']=species_dict2array
output_dict['Pybel_object_dict']=Pybel_object_dict
output_dict['smiles_array']=smiles_array
output_dict['SMILES_dict']=SMILES_dict
output_dict['concentration_array']=concentration_array
output_dict['species_number']=species_step
output_dict['Pybel_object_activity']=Pybel_object_activity
output_dict['concentration_dict']=concentration_dict
return output_dict
def write_rate_file(filename,rate_dict):
""" Function to write a .py file that defines rate coefficients for each reaction
inputs:
• filename - name that will define the extension of the produced module
• rate_dict - dictionary that holds reaction number and associated string [Python command friendly] of calculations required to predict the rate coefficient
outputs:
• N/A - generates a .py file for later use
"""
#Put all of the rate coefficient functional forms into a new python file.
f = open('Rate_coefficients.py','w')
f.write('##################################################################################################### \n') # python will convert \n to os.linesep
f.write('# Python function to hold expressions for calculating rate coefficients for a given equation number # \n') # python will convert \n to os.linesep
f.write('# Copyright (C) 2018 David Topping : david.topping@manchester.ac.uk # \n')
f.write('# : davetopp80@gmail.com # \n')
f.write('# Personal website: davetoppingsci.com # \n')
f.write('# # \n')
f.write('# All Rights Reserved. # \n')
f.write('# This file is part of PyBox. # \n')
f.write('# # \n')
f.write('# PyBox is free software: you can redistribute it and/or modify it under # \n')
f.write('# the terms of the GNU General Public License as published by the Free Software # \n')
f.write('# Foundation, either version 3 of the License, or (at your option) any later # \n')
f.write('# version. # \n')
f.write('# # \n')
f.write('# PyBox is distributed in the hope that it will be useful, but WITHOUT # \n')
f.write('# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # \n')
f.write('# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # \n')
f.write('# details. # \n')
f.write('# # \n')
f.write('# You should have received a copy of the GNU General Public License along with # \n')
f.write('# PyBox. If not, see <http://www.gnu.org/licenses/>. # \n')
f.write('# # \n')
f.write('##################################################################################################### \n')
f.write('# File created at %s \n' % datetime.datetime.now()) # python will convert \n to os.linesep
f.write('\n')
f.write('import numpy \n')
#f.write('import numba')
f.write('\n')
#f.write('@numba.jit(nopython=True)\n')
f.write('def evaluate_rates(ttime,RO2,H2O,temp):\n')
# Now cycle through all of the mcm_constants_dict values.
# Please note this includes photolysis rates that will change with time of day or condition in the chamber. You will
# need to modify this potentially.
f.write(' # Creating reference to constant values used in rate expressions\n')
f.write(' # constants used in calculation of reaction rates\n')
f.write(' M = 2.55E+19 #Check this against pressure assumed in model\n')
f.write(' N2 = 0.79*M\n')
f.write(' O2 = 0.2095*M\n')
f.write(' # kro2no : ro2 + no = ro + no2\n')
f.write(' # : ro2 + no = rono2\n')
f.write(' # iupac 1992\n')
f.write(' KRONO2 = 2.70E-12*numpy.exp(360.0/temp)\n')
f.write(' KRO2NO = 2.7E-12*numpy.exp(360.0/temp) \n')
f.write(' # kro2ho2: ro2 + ho2 = rooh + o2\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KRO2HO2 = 2.91E-13*numpy.exp(1300.0/temp)\n')
f.write(' # kapho2 : rcoo2 + ho2 = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KAPHO2 = 5.2E-13*numpy.exp(980.0/temp)\n')
f.write(' # kapno : rcoo2 + no = products\n')
f.write(' # mej [1998]\n')
f.write(' KAPNO = 7.5E-12*numpy.exp(290.0/temp)\n')
f.write(' # kro2no3: ro2 + no3 = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KRO2NO3 = 2.3E-12\n')
f.write(' # kno3al : no3 + rcho = rcoo2 + hno3\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KNO3AL = 1.4E-12*numpy.exp(-1860.0/temp)\n')
f.write(' # kdec : ro = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KDEC = 1.00E+06\n')
f.write(' KROSEC = 2.50E-14*numpy.exp(-300.0/temp)\n')
f.write(' KALKOXY=3.70E-14*numpy.exp(-460.0/temp)*O2\n')
f.write(' KALKPXY=1.80E-14*numpy.exp(-260.0/temp)*O2\n')
f.write(' KROPRIM = 2.50E-14*numpy.exp(-300/temp)\n')
f.write(' KCH3O2 = 1.03E-13*numpy.exp(365/temp)\n')
f.write(' K298CH3O2 = 3.5E-13\n')
f.write(' K14ISOM1 = 3.00E7*numpy.exp(-5300/temp) ')
f.write(' # -------------------------------------------------------------------\n')
f.write(' # complex reactions\n')
f.write(' # -------------------------------------------------------------------\n')
f.write(' # kfpan kbpan\n')
f.write(' # formation and decomposition of pan\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' kc0 = 3.28E-28*M*(temp/300.0)**(-6.87)\n')
f.write(' kci = 1.125E-11*(temp/300.0)**(-1.105)\n')
f.write(' krc = kc0/kci\n')
f.write(' fcc = 0.30\n')
f.write(' nc = 0.75-(1.27*numpy.log10(fcc))\n')
f.write(' fc = 10**(numpy.log10(fcc)/(1.0+((numpy.log10(krc))/nc)**2.0))\n')
f.write(' KFPAN = (kc0*kci)*fc/(kc0+kci)\n')
f.write(' kd0 = 1.10E-05*M*numpy.exp(-10100.0/temp)\n')
f.write(' kdi = 1.90E+17*numpy.exp(-14100.0/temp)\n')
f.write(' krd = kd0/kdi\n')
f.write(' fcd = 0.30\n')
f.write(' ncd = 0.75-(1.27*numpy.log10(fcd))\n')
f.write(' fd = 10.0**(numpy.log10(fcd)/(1.0+((numpy.log10(krd))/ncd)**2.0))\n')
f.write(' KBPAN = (kd0*kdi)*fd/(kd0+kdi)\n')
f.write(' KPPN0 = 1.7E-03*M*numpy.exp(-11280.0/temp)\n')
f.write(' KPPNI = 8.3E+16*numpy.exp(-13940.0/temp)\n')
f.write(' KRPPN = KPPN0/KPPNI\n')
f.write(' FCPPN = 0.36\n')
f.write(' NCPPN = 0.75-(1.27*numpy.log10(fcd))\n')
f.write(' FPPN = 10.0**(numpy.log10(fcd)/(1.0+((numpy.log10(krd))/ncd)**2.0))\n')
f.write(' KBPPN = (KPPN0*KPPNI)*FCPPN/(KPPN0+KPPNI)\n')
f.write(' # kmt01 : o + no = no2\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' k10 = 1.00E-31*M*(temp/300.0)**(-1.6)\n')
f.write(' k1i = 5.0E-11*(temp/300.0)**(0.3)\n')
f.write(' kr1 = k10/k1i\n')
f.write(' fc1 = 0.85\n')
f.write(' nc1 = 0.75-(1.27*numpy.log10(fc1))\n')
f.write(' f1 = 10.0**(numpy.log10(fc1)/(1.0+((numpy.log10(kr1)/nc1))**2.0))\n')
f.write(' KMT01 = (k10*k1i)*f1/(k10+k1i)\n')
f.write(' # kmt02 : o + no2 = no3\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' k20 = 1.30E-31*M*(temp/300.0)**(-1.5)\n')
f.write(' k2i = 2.30E-11*(temp/300.0)**(0.24)\n')
f.write(' kr2 = k20/k2i\n')
f.write(' fc2 = 0.6\n')
f.write(' nc2 = 0.75-(1.27*numpy.log10(fc2))\n')
f.write(' f2 = 10.0**(numpy.log10(fc2)/(1.0+((numpy.log10(kr2)/nc2))**2.0))\n')
f.write(' KMT02 = (k20*k2i)*f2/(k20+k2i)\n')
f.write(' # kmt03 : no2 + no3 = n2o5\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k30 = 3.60E-30*M*(temp/300.0)**(-4.1)\n')
f.write(' k3i = 1.90E-12*(temp/300.0)**(0.2)\n')
f.write(' kr3 = k30/k3i\n')
f.write(' fc3 = 0.35\n')
f.write(' nc3 = 0.75-(1.27*numpy.log10(fc3))\n')
f.write(' f3 = 10.0**(numpy.log10(fc3)/(1.0+((numpy.log10(kr3)/nc3))**2.0))\n')
f.write(' KMT03 = (k30*k3i)*f3/(k30+k3i)\n')
f.write(' # kmt04 : n2o5 = no2 + no3\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k40 = 1.30E-03*M*(temp/300.0)**(-3.5)*numpy.exp(-11000.0/temp)\n')
f.write(' k4i = 9.70E+14*(temp/300.0)**(0.1)*numpy.exp(-11080.0/temp)\n')
f.write(' kr4 = k40/k4i\n')
f.write(' fc4 = 0.35\n')
f.write(' nc4 = 0.75-(1.27*numpy.log10(fc4))\n')
f.write(' f4 = 10.0**(numpy.log10(fc4)/(1+((numpy.log10(kr4)/nc4))**2.0))\n')
f.write(' KMT04 = (k40*k4i)*f4/(k40+k4i)\n')
f.write(' # kmt05 : oh + co(+o2) = ho2 + co2\n')
f.write(' # iupac 2006\n')
f.write(' KMT05 = 1.44E-13*(1.0 + (M/4.2E19))\n')
f.write(' # kmt06 : ho2 + ho2 = h2o2 + o2\n')
f.write(' # water enhancement factor\n')
f.write(' # iupac 1992\n')
f.write(' KMT06 = 1.0 + (1.40E-21*numpy.exp(2200.0/temp)*H2O)\n')
f.write(' # kmt06 = 1.0 + (2.00E-25*numpy.exp(4670.0/temp)*h2o)\n')
f.write(' # S+R 2005 values\n')
f.write(' # kmt07 : oh + no = hono\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k70 = 7.40E-31*M*(temp/300.0)**(-2.4)\n')
f.write(' k7i = 3.30E-11*(temp/300.0)**(-0.3)\n')
f.write(' kr7 = k70/k7i\n')
f.write(' fc7 = 0.81 \n')
f.write(' nc7 = 0.75-(1.27*numpy.log10(fc7))\n')
f.write(' f7 = 10.0**(numpy.log10(fc7)/(1+((numpy.log10(kr7)/nc7))**2.0))\n')
f.write(' KMT07 = (k70*k7i)*f7/(k70+k7i)\n')
f.write(' # kmt08 : oh + no2 = hno3\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k80 = 3.2E-30*M*(temp/300.0)**(-4.5)\n')
f.write(' k8i = 3.0E-11\n')
f.write(' kr8 = k80/k8i\n')
f.write(' fc8 = 0.41\n')
f.write(' nc8 = 0.75-(1.27*numpy.log10(fc8))\n')
f.write(' f8 = 10.0**(numpy.log10(fc8)/(1.0+((numpy.log10(kr8)/nc8))**2.0))\n')
f.write(' KMT08 = (k80*k8i)*f8/(k80+k8i)\n')
f.write(' # kmt09 : ho2 + no2 = ho2no2\n')
f.write(' # iupac 1997, mcmv3.2\n')
f.write(' k90 = 1.4E-31*M*(temp/300.0)**(-3.1)\n')
f.write(' k9i = 4.0E-12\n')
f.write(' kr9 = k90/k9i\n')
f.write(' fc9 = 0.4\n')
f.write(' nc9 = 0.75-(1.27*numpy.log10(fc9))\n')
f.write(' f9 = 10.0**(numpy.log10(fc9)/(1.0+((numpy.log10(kr9)/nc9))**2.0))\n')
f.write(' KMT09 = (k90*k9i)*f9/(k90+k9i)\n')
f.write(' # kmt10 : ho2no2 = ho2 + no2\n')
f.write(' # iupac 1997, mcmv3.2\n')
f.write(' k100 = 4.10E-05*M*numpy.exp(-10650.0/temp)\n')
f.write(' k10i = 6.0E+15*numpy.exp(-11170.0/temp)\n')
f.write(' kr10 = k100/k10i\n')
f.write(' fc10 = 0.4\n')
f.write(' nc10 = 0.75-(1.27*numpy.log10(fc10))\n')
f.write(' f10 = 10.0**(numpy.log10(fc10)/(1.0+((numpy.log10(kr10)/nc10))**2.0))\n')
f.write(' KMT10 = (k100*k10i)*f10/(k100+k10i)\n')
f.write(' # kmt11 : oh + hno3 = h2o + no3\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k1 = 2.40E-14*numpy.exp(460.0/temp)\n')
f.write(' k3 = 6.50E-34*numpy.exp(1335.0/temp)\n')
f.write(' k4 = 2.70E-17*numpy.exp(2199.0/temp)\n')
f.write(' k2 = (k3*M)/(1.0+(k3*M/k4))\n')
f.write(' KMT11 = k1 + k2\n')
f.write(' # kmt12 iupac 2006, mcmv3.2\n')
f.write(' k120 = 2.5E-31*((temp/300.0)**(-2.6))*M\n')
f.write(' k12i = 2.0E-12\n')
f.write(' kr12 = k120/k12i\n')
f.write(' fc12 = 0.53\n')
f.write(' nc12 = 0.75-(1.27*numpy.log10(fc12))\n')
f.write(' f12 = 10.0**(numpy.log10(fc12)/(1.0+((numpy.log10(kr12)/nc12))**2.0))\n')
f.write(' KMT12 = (k120*k12i)*f12/(k120+k12i)\n')
f.write(' # kmt13 : ch3o2 + no2 = ch3o2no2\n')
f.write(' # iupac 2006\n')
f.write(' k130 = 2.50E-30*((temp/300.0)**(-5.5))*M\n')
f.write(' k13i = 1.80E-11\n')
f.write(' kr13 = k130/k13i\n')
f.write(' fc13 = 0.36\n')
f.write(' nc13 = 0.75-(1.27*numpy.log10(fc13))\n')
f.write(' f13 = 10.0**(numpy.log10(fc13)/(1.0+((numpy.log10(kr13)/nc13))**2.0))\n')
f.write(' KMT13 = (k130*k13i)*f13/(k130+k13i)\n')
f.write(' # kmt14 : ch3o2no2 = ch3o2 + no2\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k140 = 9.00E-05*numpy.exp(-9690.0/temp)*M\n')
f.write(' k14i = 1.10E+16*numpy.exp(-10560.0/temp)\n')
f.write(' kr14 = k140/k14i\n')
f.write(' fc14 = 0.36\n')
f.write(' nc14 = 0.75-(1.27*numpy.log10(fc14))\n')
f.write(' f14 = 10.0**(numpy.log10(fc14)/(1.0+((numpy.log10(kr14)/nc14))**2.0))\n')
f.write(' KMT14 = (k140*k14i)*f14/(k140+k14i)\n')
f.write(' # kmt15 iupac 2006, mcmv3.2\n')
f.write(' k150 = 8.60E-29*((temp/300.0)**(-3.1))*M\n')
f.write(' k15i = 9.00E-12*((temp/300.0)**(-0.85))\n')
f.write(' kr15 = k150/k15i\n')
f.write(' fc15 = 0.48\n')
f.write(' nc15 = 0.75-(1.27*numpy.log10(fc15))\n')
f.write(' f15 = 10.0**(numpy.log10(fc15)/(1.0+((numpy.log10(kr15)/nc15))**2.0))\n')
f.write(' KMT15 = (k150*k15i)*f15/(k150+k15i)\n')
f.write(' # kmt16 : oh + c3h6\n')
f.write(' # iupac 2006\n')
f.write(' k160 = 8.00E-27*((temp/300.0)**(-3.5))*M\n')
f.write(' k16i = 3.00E-11*((temp/300.0)**(-1.0))\n')
f.write(' kr16 = k160/k16i\n')
f.write(' fc16 = 0.5\n')
f.write(' nc16 = 0.75-(1.27*numpy.log10(fc16))\n')
f.write(' f16 = 10.0**(numpy.log10(fc16)/(1.0+((numpy.log10(kr16)/nc16))**2.0))\n')
f.write(' KMT16 = (k160*k16i)*f16/(k160+k16i)\n')
f.write(' # kmt17 iupac 2006\n')
f.write(' k170 = 5.00E-30*((temp/300.0)**(-1.5))*M\n')
f.write(' k17i = 1.00E-12\n')
f.write(' kr17 = k170/k17i\n')
f.write(' fc17 = (0.17*numpy.exp(-51./temp))+numpy.exp(-1.0*temp/204.)\n')
f.write(' nc17 = 0.75-(1.27*numpy.log10(fc17))\n')
f.write(' f17 = 10.0**(numpy.log10(fc17)/(1.0+((numpy.log10(kr17)/nc17))**2.0))\n')
f.write(' KMT17 = (k170*k17i)*f17/(k170+k17i)\n')
f.write(' KMT18 = 9.5E-39*O2*numpy.exp(5270/temp)/(1+7.5E-29*O2*numpy.exp(5610/temp))\n')
f.write(' # ************************************************************************\n')
f.write(' # define photolysis reaction rates using derwent method from mcm2box.fac\n')
f.write(' # ************************************************************************\n')
f.write(' # solar declination angle \n')
f.write(' dec = 23.79\n')
f.write(' # latitude\n')
f.write(' lat = 50.0\n')
f.write(' pi = 4.0*numpy.arctan(1.0)\n')
f.write(' # local hour angle - representing time of day\n')
f.write(' lha = (1.0+ttime/4.32E+4)*pi\n')
f.write(' radian = 180.0/pi\n')
f.write(' lat = lat/radian\n')
f.write(' dec = dec/radian\n')
f.write(' theta = numpy.arccos(numpy.cos(lha)*numpy.cos(dec)*numpy.cos(lat)+numpy.sin(dec)*numpy.sin(lat))\n')
f.write(' sinld = numpy.sin(lat)*numpy.sin(dec)\n')
f.write(' cosld = numpy.cos(lat)*numpy.cos(dec)\n')
f.write(' cosx = (numpy.cos(lha)*cosld)+sinld\n')
f.write(' cosx = numpy.cos(theta)\n')
f.write(' secx = 1.0E+0/(cosx+1.0E-30)\n')
f.write(' # Data taken from photolysis.txt. Calculations done in the form of:\n')
f.write(' # j(k) = l(k)*cosx**( mm(k))*numpy.exp(-nn(k)*secx)\n')
f.write(' J=[None]*62\n')
f.write(' #J L M N\n')
f.write(' J[1]=6.073E-05*cosx**(1.743)*numpy.exp(-1.0*0.474*secx)\n')
f.write(' J[2]=4.775E-04*cosx**(0.298)*numpy.exp(-1.0*0.080*secx)\n')
f.write(' J[3]=1.041E-05*cosx**(0.723)*numpy.exp(-1.0*0.279*secx)\n')
f.write(' J[4]=1.165E-02*cosx**(0.244)*numpy.exp(-1.0*0.267*secx)\n')
f.write(' J[5]=2.485E-02*cosx**(0.168)*numpy.exp(-1.0*0.108*secx)\n')
f.write(' J[6]=1.747E-01*cosx**(0.155)*numpy.exp(-1.0*0.125*secx)\n')
f.write(' J[7]=2.644E-03*cosx**(0.261)*numpy.exp(-1.0*0.288*secx)\n')
f.write(' J[8]=9.312E-07*cosx**(1.230)*numpy.exp(-1.0*0.307*secx)\n')
f.write(' J[11]=4.642E-05*cosx**(0.762)*numpy.exp(-1.0*0.353*secx)\n')
f.write(' J[12]=6.853E-05*cosx**(0.477)*numpy.exp(-1.0*0.323*secx)\n')
f.write(' J[13]=7.344E-06*cosx**(1.202)*numpy.exp(-1.0*0.417*secx)\n')
f.write(' J[14]=2.879E-05*cosx**(1.067)*numpy.exp(-1.0*0.358*secx)\n')
f.write(' J[15]=2.792E-05*cosx**(0.805)*numpy.exp(-1.0*0.338*secx)\n')
f.write(' J[16]=1.675E-05*cosx**(0.805)*numpy.exp(-1.0*0.338*secx)\n')
f.write(' J[17]=7.914E-05*cosx**(0.764)*numpy.exp(-1.0*0.364*secx)\n')
f.write(' J[18]=1.482E-06*cosx**(0.396)*numpy.exp(-1.0*0.298*secx)\n')
f.write(' J[19]=1.482E-05*cosx**(0.396)*numpy.exp(-1.0*0.298*secx)\n')
f.write(' J[20]=7.600E-04*cosx**(0.396)*numpy.exp(-1.0*0.298*secx)\n')
f.write(' J[21]=7.992E-07*cosx**(1.578)*numpy.exp(-1.0*0.271*secx)\n')
f.write(' J[22]=5.804E-06*cosx**(1.092)*numpy.exp(-1.0*0.377*secx)\n')
f.write(' J[23]=2.4246E-06*cosx**(0.395)*numpy.exp(-1.0*0.296*secx)\n')
f.write(' J[24]=2.424E-06*cosx**(0.395)*numpy.exp(-1.0*0.296*secx)\n')
f.write(' J[31]=6.845E-05*cosx**(0.130)*numpy.exp(-1.0*0.201*secx)\n')
f.write(' J[32]=1.032E-05*cosx**(0.130)*numpy.exp(-1.0*0.201*secx)\n')
f.write(' J[33]=3.802E-05*cosx**(0.644)*numpy.exp(-1.0*0.312*secx)\n')
f.write(' J[34]=1.537E-04*cosx**(0.170)*numpy.exp(-1.0*0.208*secx)\n')
f.write(' J[35]=3.326E-04*cosx**(0.148)*numpy.exp(-1.0*0.215*secx)\n')
f.write(' J[41]=7.649E-06*cosx**(0.682)*numpy.exp(-1.0*0.279*secx)\n')
f.write(' J[51]=1.588E-06*cosx**(1.154)*numpy.exp(-1.0*0.318*secx)\n')
f.write(' J[52]=1.907E-06*cosx**(1.244)*numpy.exp(-1.0*0.335*secx)\n')
f.write(' J[53]=2.485E-06*cosx**(1.196)*numpy.exp(-1.0*0.328*secx)\n')
f.write(' J[54]=4.095E-06*cosx**(1.111)*numpy.exp(-1.0*0.316*secx)\n')
f.write(' J[55]=1.135E-05*cosx**(0.974)*numpy.exp(-1.0*0.309*secx)\n')
f.write(' J[56]=4.365E-05*cosx**(1.089)*numpy.exp(-1.0*0.323*secx)\n')
f.write(' J[57]=3.363E-06*cosx**(1.296)*numpy.exp(-1.0*0.322*secx)\n')
f.write(' J[61]=7.537E-04*cosx**(0.499)*numpy.exp(-1.0*0.266*secx)\n')
f.write(' TEMP=temp\n')
#for key in mcm_constants_dict.keys():
# f.write(' %s =mcm_constants_dict[%s] \n' %(key,repr(str(key))))
#f.write('\n')
# # constants used in calculation of reaction rates
#f.write(' M = 2.55E+19\n') #Check this against pressure assumed in model!
#f.write(' N2 = 0.79*M\n')
#f.write(' O2 = 0.2095*M\n')
f.write('\n')
f.write(' # Creating numpy array to hold results of expressions taken from .eqn file \n')
f.write(' rate_values=numpy.zeros(%s)\n' %(len(rate_dict.keys())))
# Now cycle through the rate_dict dictionary and print to file
for key in rate_dict.keys():
f.write(' rate_values[%s] =%s \n' %(key,rate_dict[key]))
f.write('\n')
f.write(' return rate_values \n')
f.close()
def write_rate_file_numba(filename,rate_dict):
""" Function to write a Numba enabled .py file that defines rate coefficients for each reaction
inputs:
• filename - name that will define the extension of the produced module
• rate_dict - dictionary that holds reaction number and associated string [Python command friendly] of calculations required to predict the rate coefficient
outputs:
• N/A - generates a .py file for later use
"""
#Put all of the rate coefficient functional forms into a new python file.
f = open('Rate_coefficients_numba.py','w')
f.write('##################################################################################################### \n') # python will convert \n to os.linesep
f.write('# Personal website: davetoppingsci.com # \n')
f.write('# Copyright (C) 2018 David Topping : david.topping@manchester.ac.uk # \n')
f.write('# : davetopp80@gmail.com # \n')
f.write('# Personal website: davetoppingsci.com # \n')
f.write('# # \n')
f.write('# All Rights Reserved. # \n')
f.write('# This file is part of PyBox. # \n')
f.write('# # \n')
f.write('# PyBox is free software: you can redistribute it and/or modify it under # \n')
f.write('# the terms of the GNU General Public License as published by the Free Software # \n')
f.write('# Foundation, either version 3 of the License, or (at your option) any later # \n')
f.write('# version. # \n')
f.write('# # \n')
f.write('# PyBox is distributed in the hope that it will be useful, but WITHOUT # \n')
f.write('# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # \n')
f.write('# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # \n')
f.write('# details. # \n')
f.write('# # \n')
f.write('# You should have received a copy of the GNU General Public License along with # \n')
f.write('# PyBox. If not, see <http://www.gnu.org/licenses/>. # \n')
f.write('# # \n')
f.write('##################################################################################################### \n')
f.write('# File created at %s \n' % datetime.datetime.now()) # python will convert \n to os.linesep
f.write('\n')
f.write('import numpy as np \n')
f.write('import numba as nb')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_exp(x): \n')
f.write(' return np.exp(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_log10(x): \n')
f.write(' return np.log10(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_log(x): \n')
f.write(' return np.log(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_abs(x): \n')
f.write(' return np.abs(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_sqrt(x): \n')
f.write(' return np.sqrt(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_arccos(x): \n')
f.write(' return np.arccos(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_cos(x): \n')
f.write(' return np.cos(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_sin(x): \n')
f.write(' return np.sin(x) \n')
f.write('\n')
f.write('@nb.jit(nb.f8(nb.f8), nopython=True) \n')
f.write('def numba_arctan(x): \n')
f.write(' return np.arctan(x) \n')
f.write('\n')
f.write('@nb.jit(\'float64[:](float64,float64,float64,float64,float64[:],float64[:])\', nopython=True, cache=True)\n')
f.write('def evaluate_rates(ttime,RO2,H2O,temp,rate_values,J):\n')
# Now cycle through all of the mcm_constants_dict values.
# Please note this includes photolysis rates that will change with time of day or condition in the chamber. You will
# need to modify this potentially.
f.write(' # Creating reference to constant values used in rate expressions\n')
f.write(' # constants used in calculation of reaction rates\n')
f.write(' M = 2.55E+19 #Check this against pressure assumed in model\n')
f.write(' N2 = 0.79*M\n')
f.write(' O2 = 0.2095*M\n')
f.write(' # kro2no : ro2 + no = ro + no2\n')
f.write(' # : ro2 + no = rono2\n')
f.write(' # iupac 1992\n')
f.write(' KRONO2 = 2.70E-12*numba_exp(360.0/temp)\n')
f.write(' KRO2NO = 2.7E-12*numba_exp(360.0/temp) \n')
f.write(' # kro2ho2: ro2 + ho2 = rooh + o2\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KRO2HO2 = 2.91E-13*numba_exp(1300.0/temp)\n')
f.write(' # kapho2 : rcoo2 + ho2 = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KAPHO2 = 5.2E-13*numba_exp(980.0/temp)\n')
f.write(' # kapno : rcoo2 + no = products\n')
f.write(' # mej [1998]\n')
f.write(' KAPNO = 7.5E-12*numba_exp(290.0/temp)\n')
f.write(' # kro2no3: ro2 + no3 = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KRO2NO3 = 2.3E-12\n')
f.write(' # kno3al : no3 + rcho = rcoo2 + hno3\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KNO3AL = 1.4E-12*numba_exp(-1860.0/temp)\n')
f.write(' # kdec : ro = products\n')
f.write(' # mcm protocol [1997]\n')
f.write(' KDEC = 1.00E+06\n')
f.write(' KROSEC = 2.50E-14*numba_exp(-300.0/temp)\n')
f.write(' KALKOXY=3.70E-14*numba_exp(-460.0/temp)*O2\n')
f.write(' KALKPXY=1.80E-14*numba_exp(-260.0/temp)*O2\n')
f.write(' KROPRIM = 2.50E-14*numba_exp(-300.0/temp)\n')
f.write(' KCH3O2 = 1.03E-13*numba_exp(365.0/temp)\n')
f.write(' K298CH3O2 = 3.5E-13\n')
f.write(' K14ISOM1 = 3.00E7*numba_exp(-5300.0/temp) ')
f.write(' # -------------------------------------------------------------------\n')
f.write(' # complex reactions\n')
f.write(' # -------------------------------------------------------------------\n')
f.write(' # kfpan kbpan\n')
f.write(' # formation and decomposition of pan\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' kc0 = 3.28E-28*M*(temp/300.0)**(-6.87)\n')
f.write(' kci = 1.125E-11*(temp/300.0)**(-1.105)\n')
f.write(' krc = kc0/kci\n')
f.write(' fcc = 0.30\n')
f.write(' nc = 0.75-(1.27*numba_log10(fcc))\n')
f.write(' fc = 10**(numba_log10(fcc)/(1.0+((numba_log10(krc))/nc)**2.0))\n')
f.write(' KFPAN = (kc0*kci)*fc/(kc0+kci)\n')
f.write(' kd0 = 1.10E-05*M*numba_exp(-10100.0/temp)\n')
f.write(' kdi = 1.90E+17*numba_exp(-14100.0/temp)\n')
f.write(' krd = kd0/kdi\n')
f.write(' fcd = 0.30\n')
f.write(' ncd = 0.75-(1.27*numba_log10(fcd))\n')
f.write(' fd = 10.0**(numba_log10(fcd)/(1.0+((numba_log10(krd))/ncd)**2.0))\n')
f.write(' KBPAN = (kd0*kdi)*fd/(kd0+kdi)\n')
f.write(' KPPN0 = 1.7E-03*M*numba_exp(-11280.0/temp)\n')
f.write(' KPPNI = 8.3E+16*numba_exp(-13940.0/temp)\n')
f.write(' KRPPN = KPPN0/KPPNI\n')
f.write(' FCPPN = 0.36\n')
f.write(' NCPPN = 0.75-(1.27*numba_log10(fcd))\n')
f.write(' FPPN = 10.0**(numba_log10(fcd)/(1.0+((numba_log10(krd))/ncd)**2.0))\n')
f.write(' KBPPN = (KPPN0*KPPNI)*FCPPN/(KPPN0+KPPNI)\n')
f.write(' # kmt01 : o + no = no2\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' k10 = 1.00E-31*M*(temp/300.0)**(-1.6)\n')
f.write(' k1i = 5.0E-11*(temp/300.0)**(0.3)\n')
f.write(' kr1 = k10/k1i\n')
f.write(' fc1 = 0.85\n')
f.write(' nc1 = 0.75-(1.27*numba_log10(fc1))\n')
f.write(' f1 = 10.0**(numba_log10(fc1)/(1.0+((numba_log10(kr1)/nc1))**2.0))\n')
f.write(' KMT01 = (k10*k1i)*f1/(k10+k1i)\n')
f.write(' # kmt02 : o + no2 = no3\n')
f.write(' # iupac 2001 (mcmv3.2)\n')
f.write(' k20 = 1.30E-31*M*(temp/300.0)**(-1.5)\n')
f.write(' k2i = 2.30E-11*(temp/300.0)**(0.24)\n')
f.write(' kr2 = k20/k2i\n')
f.write(' fc2 = 0.6\n')
f.write(' nc2 = 0.75-(1.27*numba_log10(fc2))\n')
f.write(' f2 = 10.0**(numba_log10(fc2)/(1.0+((numba_log10(kr2)/nc2))**2.0))\n')
f.write(' KMT02 = (k20*k2i)*f2/(k20+k2i)\n')
f.write(' # kmt03 : no2 + no3 = n2o5\n')
f.write(' # iupac 2006, mcmv3.2\n')
f.write(' k30 = 3.60E-30*M*(temp/300.0)**(-4.1)\n')