Skip to content

Commit c4ed6af

Browse files
authored
Merge pull request AtChem#507 from AlfredMayhew/custom_functions
Specifying Custom FORTRAN Functions in the Mechanism
2 parents 63cf98e + d4042c3 commit c4ed6af

File tree

75 files changed

+4964
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+4964
-6
lines changed

build/mech_converter.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# - mechanism.ro2
2525
# - mechanism.f90
2626
#
27-
# Acknowledgements: B. Nelson, M. Newland
27+
# Acknowledgements: B. Nelson, M. Newland, A. Mayhew
2828
#
2929
# ARGUMENTS:
3030
# 1. path to the mechanism .fac file
@@ -71,9 +71,10 @@ def tokenise_and_process(input_string, vars_dict):
7171
assert isinstance(vars_dict, dict), \
7272
'tokenise_and_process: vars_dict is not of type dict: ' + str(vars_dict)
7373

74-
# Generate start and end points of sections of symbols and non-symbols.
75-
symbol_regex = r'[()\-+*@/ ]+'
76-
nonsymbol_regex = r'[^()\-+*@/ ]+'
74+
# Generate start and end points of sections of symbols and nonsymbols
75+
symbol_regex = '[()\-+*@/, ]+'
76+
nonsymbol_regex = '[^()\-+*@/, ]+'
77+
7778
list_of_symbol_starts = [m.start(0) for m in re.finditer(symbol_regex, input_string)]
7879
list_of_symbol_ends = [m.end(0) for m in re.finditer(symbol_regex, input_string)]
7980
list_of_nonsymbol_starts = [m.start(0) for m in re.finditer(nonsymbol_regex, input_string)]
@@ -302,6 +303,15 @@ def convert_to_fortran(input_file, mech_dir, mcm_vers):
302303
continue
303304

304305
# -------------------------------------------------
306+
# Read in the names of user-defined custom rate functions and add them
307+
# to the list of reserved names so that they will be carried through the
308+
# rate definitions (in a similar manner to LOG10)
309+
with open(mech_dir + '/customRateFuncs.f90') as custom_func_file:
310+
func_def_pat = "function +([a-zA-Z0-9_]*) *\("
311+
custom_func_names = re.findall(func_def_pat, custom_func_file.read(), re.I)
312+
313+
for n in custom_func_names:
314+
reservedOtherList.append(n)
305315

306316
# Initialise list, dictionary and a counter.
307317
mechanism_rates_coeff_list = []
@@ -551,6 +561,7 @@ def convert_to_fortran(input_file, mech_dir, mcm_vers):
551561
552562
subroutine update_p(p, q, TEMP, N2, O2, M, RH, H2O, BLHEIGHT, DEC, JFAC, DILUTE, ROOFOPEN, ASA, J, RO2) bind(c,name='update_p')
553563
564+
use custom_functions_mod
554565
integer, parameter :: DP = selected_real_kind( p = 15, r = 307 )
555566
real(c_double), intent(inout) :: p(*), q(*)
556567
real(c_double), intent(in) :: TEMP, N2, O2, M, RH, H2O, BLHEIGHT, DEC, JFAC, DILUTE, ROOFOPEN, ASA, J(*), RO2

model/configuration/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Ignore auto-generated mechanism files in this directory
22
mechanism.*
3+
customRateFuncs.o
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod

tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mechanism.reac
99
mechanism.ro2
1010
mechanism.species
1111
mechanism.so
12+
customRateFuncs.o
1213
tests/*/output/reactionRates/*
1314
model_tests/*/output/reactionRates/*
1415

tests/model_tests/INFO.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ starting at 6:30 am on 9/11/2008.
2424

2525
- `spec_model_kpp` is the same as the base case but the mechanism is in KPP format.
2626

27-
- `spec_model_stoich` is the same as the base case but the mechanism has been adjusted to include stoichometric coefficients (e.g. 'NO + NO = NO2 + NO2' becomes '2 NO = 2 NO2')
27+
- `spec_model_stoich` is the same as the base case but the mechanism has been adjusted to include stoichometric coefficients (e.g. 'NO + NO = NO2 + NO2' becomes '2 NO = 2 NO2')
28+
29+
- `spec_model_func` is the same as the base case but the KMT15 rate definition has been moved to `customRateFuncs.f90` as opposed to being defined in the mechanism.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
!Define your functions here.
28+
29+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
! -----------------------------------------------------------------------------
2+
!
3+
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
4+
! Peter Jimack, Mike Pilling
5+
!
6+
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
7+
!
8+
! Copyright (c) 2023 Alfred Mayhew
9+
!
10+
! This file is part of the AtChem2 software package.
11+
!
12+
! This file is covered by the MIT license which can be found in the file
13+
! LICENSE.md at the top level of the AtChem2 distribution.
14+
!
15+
! -----------------------------------------------------------------------------
16+
! ******************************************************************** !
17+
! ATCHEM2 -- MODULE customRateFunctions
18+
!
19+
! This module contains user-defined functions that can be referenced
20+
! in the mechanism file
21+
! ******************************************************************** !
22+
module custom_functions_mod
23+
implicit none
24+
25+
contains
26+
27+
! -----------------------------------------------------------------
28+
! Calculates the rate of KMT15
29+
pure function calcKMT15( t, m ) result ( KMT15 )
30+
real*8, intent(in) :: t, m
31+
real :: K150, K15I, KR15, FC15, NC15, F15
32+
real :: KMT15
33+
34+
K150 = 8.6E-29 * m * (t / 300)**(-3.1)
35+
K15I = 9.0E-12 * (t / 300)**(-0.85)
36+
KR15 = K150 / K15I
37+
FC15 = 0.48
38+
NC15 = 0.75 - 1.27 * (LOG10(FC15))
39+
F15 = 10**(LOG10(FC15) / (1 + (LOG10(KR15) / NC15)**2))
40+
KMT15 = (K150*K15I)*F15/(K150+K15I)
41+
42+
return
43+
end function calcKMT15
44+
45+
end module custom_functions_mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1 TEMP 291.45
2+
2 PRESS 950.2
3+
3 RH 67.4
4+
4 H2O CALC
5+
5 DEC CALC
6+
6 BLHEIGHT NOTUSED
7+
7 DILUTE NOTUSED
8+
8 JFAC NOTUSED
9+
9 ROOF OPEN
10+
10 ASA NOTUSED
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CO 4.8e+12
2+
O3 6.11e11
3+
NO 6.8e10
4+
NO2 8.37e10
5+
C2H4 2.76e+9

0 commit comments

Comments
 (0)