Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying Custom FORTRAN Functions in the Mechanism #507

Merged
merged 8 commits into from
Jan 17, 2024
19 changes: 15 additions & 4 deletions build/mech_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# - mechanism.ro2
# - mechanism.f90
#
# Acknowledgements: B. Nelson, M. Newland
# Acknowledgements: B. Nelson, M. Newland, A. Mayhew
#
# ARGUMENTS:
# 1. path to the mechanism .fac file
Expand Down Expand Up @@ -71,9 +71,10 @@ def tokenise_and_process(input_string, vars_dict):
assert isinstance(vars_dict, dict), \
'tokenise_and_process: vars_dict is not of type dict: ' + str(vars_dict)

# Generate start and end points of sections of symbols and non-symbols.
symbol_regex = r'[()\-+*@/ ]+'
nonsymbol_regex = r'[^()\-+*@/ ]+'
# Generate start and end points of sections of symbols and nonsymbols
symbol_regex = '[()\-+*@/, ]+'
nonsymbol_regex = '[^()\-+*@/, ]+'

list_of_symbol_starts = [m.start(0) for m in re.finditer(symbol_regex, input_string)]
list_of_symbol_ends = [m.end(0) for m in re.finditer(symbol_regex, input_string)]
list_of_nonsymbol_starts = [m.start(0) for m in re.finditer(nonsymbol_regex, input_string)]
Expand Down Expand Up @@ -302,6 +303,15 @@ def convert_to_fortran(input_file, mech_dir, mcm_vers):
continue

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

for n in custom_func_names:
reservedOtherList.append(n)

# Initialise list, dictionary and a counter.
mechanism_rates_coeff_list = []
Expand Down Expand Up @@ -551,6 +561,7 @@ def convert_to_fortran(input_file, mech_dir, mcm_vers):

subroutine update_p(p, q, TEMP, N2, O2, M, RH, H2O, BLHEIGHT, DEC, JFAC, DILUTE, ROOFOPEN, ASA, J, RO2) bind(c,name='update_p')

use custom_functions_mod
integer, parameter :: DP = selected_real_kind( p = 15, r = 307 )
real(c_double), intent(inout) :: p(*), q(*)
real(c_double), intent(in) :: TEMP, N2, O2, M, RH, H2O, BLHEIGHT, DEC, JFAC, DILUTE, ROOFOPEN, ASA, J(*), RO2
Expand Down
1 change: 1 addition & 0 deletions model/configuration/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Ignore auto-generated mechanism files in this directory
mechanism.*
customRateFuncs.o
29 changes: 29 additions & 0 deletions model/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mechanism.reac
mechanism.ro2
mechanism.species
mechanism.so
customRateFuncs.o
tests/*/output/reactionRates/*
model_tests/*/output/reactionRates/*

Expand Down
4 changes: 3 additions & 1 deletion tests/model_tests/INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ starting at 6:30 am on 9/11/2008.

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

- `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')
- `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')

- `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.
29 changes: 29 additions & 0 deletions tests/model_tests/env_model_1/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/env_model_2/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/env_model_3/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/env_model_4/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/firstorder/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/secondorder/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
29 changes: 29 additions & 0 deletions tests/model_tests/spec_model_1/configuration/customRateFuncs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

!Define your functions here.

end module custom_functions_mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
! -----------------------------------------------------------------------------
!
! Copyright (c) 2009 - 2012 Chris Martin, Kasia Boronska, Jenny Young,
! Peter Jimack, Mike Pilling
!
! Copyright (c) 2017 Sam Cox, Roberto Sommariva
!
! Copyright (c) 2023 Alfred Mayhew
!
! This file is part of the AtChem2 software package.
!
! This file is covered by the MIT license which can be found in the file
! LICENSE.md at the top level of the AtChem2 distribution.
!
! -----------------------------------------------------------------------------
! ******************************************************************** !
! ATCHEM2 -- MODULE customRateFunctions
!
! This module contains user-defined functions that can be referenced
! in the mechanism file
! ******************************************************************** !
module custom_functions_mod
implicit none

contains

! -----------------------------------------------------------------
! Calculates the rate of KMT15
pure function calcKMT15( t, m ) result ( KMT15 )
real*8, intent(in) :: t, m
real :: K150, K15I, KR15, FC15, NC15, F15
real :: KMT15

K150 = 8.6E-29 * m * (t / 300)**(-3.1)
K15I = 9.0E-12 * (t / 300)**(-0.85)
KR15 = K150 / K15I
FC15 = 0.48
NC15 = 0.75 - 1.27 * (LOG10(FC15))
F15 = 10**(LOG10(FC15) / (1 + (LOG10(KR15) / NC15)**2))
KMT15 = (K150*K15I)*F15/(K150+K15I)

return
end function calcKMT15

end module custom_functions_mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 TEMP 291.45
2 PRESS 950.2
3 RH 67.4
4 H2O CALC
5 DEC CALC
6 BLHEIGHT NOTUSED
7 DILUTE NOTUSED
8 JFAC NOTUSED
9 ROOF OPEN
10 ASA NOTUSED
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CO 4.8e+12
O3 6.11e11
NO 6.8e10
NO2 8.37e10
C2H4 2.76e+9
Loading