From 2e6d3da1f115c664c95301732f914a13355dd377 Mon Sep 17 00:00:00 2001 From: dwfncar Date: Tue, 8 Aug 2023 11:30:59 -0600 Subject: [PATCH 01/12] Check for args.micm_dir, create if does not exists. --- etc/scripts/kpp_to_micm.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index dfe4615cd..9c5c2545e 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -277,6 +277,8 @@ def micm_equation_json(lines): Write MICM JSON """ micm_mechanism_dir = os.path.join(args.micm_dir, args.mechanism) + if not os.path.exists(args.micm_dir): + os.mkdir(args.micm_dir) if not os.path.exists(micm_mechanism_dir): os.mkdir(micm_mechanism_dir) with open(os.path.join(micm_mechanism_dir, 'species.json'), 'w') as f: From a942efa2b96241a9b2ccfd2a5a63fe8b8caaba1c Mon Sep 17 00:00:00 2001 From: dwfncar Date: Wed, 9 Aug 2023 16:50:36 -0600 Subject: [PATCH 02/12] Start on kpp_to_micm.parse_kpp_arrhenius, added file test_kpp_to_micm.py for unit tests. --- etc/scripts/kpp_to_micm.py | 24 ++++++++++++++++++++++++ etc/scripts/test_kpp_to_micm.py | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 etc/scripts/test_kpp_to_micm.py diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 9c5c2545e..3337c0213 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -129,6 +129,30 @@ def micm_species_json(lines, fixed=False, tolerance=1.0e-12): return species_json +def parse_kpp_arrhenius(): + """ + KPP_REAL ARR_abc( float A0, float B0, float C0 ) + { + double ARR_RES; + + ARR_RES = (double)A0 + * exp( -(double)B0/TEMP ) + * pow( (TEMP/300.0), (double)C0 ); + + return (KPP_REAL)ARR_RES; + } + + inline double ArrheniusRateConstant::calculate( + const double& temperature, const double& pressure) const + { + return parameters_.A_ * std::exp(parameters_.C_ / temperature) + * pow(temperature / parameters_.D_, parameters_.B_) * + (1.0 + parameters_.E_ * pressure); + } + """ + pass + + def micm_equation_json(lines): """ Generate MICM equation JSON diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py new file mode 100644 index 000000000..692c89d82 --- /dev/null +++ b/etc/scripts/test_kpp_to_micm.py @@ -0,0 +1,5 @@ +import kpp_to_micm + +def test_parse_kpp_arrhenius(): + kpp_to_micm.parse_kpp_arrhenius() + assert True From 4982a40205ef013cc99fb882a443ffd27796a37d Mon Sep 17 00:00:00 2001 From: dwfncar Date: Sat, 12 Aug 2023 14:09:16 -0600 Subject: [PATCH 03/12] Adding parse_kpp_arrhenius test examples. --- etc/scripts/kpp_to_micm.py | 4 ++-- etc/scripts/test_kpp_to_micm.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 3337c0213..b4b72bd3a 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -129,7 +129,7 @@ def micm_species_json(lines, fixed=False, tolerance=1.0e-12): return species_json -def parse_kpp_arrhenius(): +def parse_kpp_arrhenius(kpp_str): """ KPP_REAL ARR_abc( float A0, float B0, float C0 ) { @@ -150,7 +150,7 @@ def parse_kpp_arrhenius(): (1.0 + parameters_.E_ * pressure); } """ - pass + logging.debug(kpp_str) def micm_equation_json(lines): diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index 692c89d82..d236d7433 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -1,5 +1,16 @@ +import logging import kpp_to_micm +logger = logging.getLogger(__name__) + def test_parse_kpp_arrhenius(): - kpp_to_micm.parse_kpp_arrhenius() + """ + examples + ARR_ab(1.0e-12, 2000.0) + ARR_ac(1.0e-12, -3.0) + ARR_abc(1.0e-12, 2000.0, -3.0) + """ + kpp_str = 'ARR_ab(1.0e-12, 2000.0)' + logger.debug(kpp_str) + kpp_to_micm.parse_kpp_arrhenius(kpp_str) assert True From ede0b2c5e27572432abc9dc70426041a3892d9b5 Mon Sep 17 00:00:00 2001 From: dwfncar Date: Sun, 13 Aug 2023 09:30:26 -0600 Subject: [PATCH 04/12] Added ARR_ab, ARR_ac, ARR_abc parse unit tests. --- etc/scripts/test_kpp_to_micm.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index d236d7433..74a262c4b 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -1,7 +1,16 @@ -import logging -import kpp_to_micm +""" +Copyright (C) 2023 +National Center for Atmospheric Research, +SPDX-License-Identifier: Apache-2.0 + +File: + test_kpp_to_micm.py + +Usage: + pytest test_kpp_to_micm.py --log-cli-level=DEBUG +""" -logger = logging.getLogger(__name__) +import kpp_to_micm def test_parse_kpp_arrhenius(): """ @@ -10,7 +19,15 @@ def test_parse_kpp_arrhenius(): ARR_ac(1.0e-12, -3.0) ARR_abc(1.0e-12, 2000.0, -3.0) """ - kpp_str = 'ARR_ab(1.0e-12, 2000.0)' - logger.debug(kpp_str) - kpp_to_micm.parse_kpp_arrhenius(kpp_str) + + kpp_A, kpp_B, kpp_C = 1.0e-12, 2000.0, -3.0 + + kpp_to_micm.parse_kpp_arrhenius('ARR_ab(%.2e, %.2f)' % (kpp_A, kpp_B)) assert True + + kpp_to_micm.parse_kpp_arrhenius('ARR_ac(%.2e, %.2f)' % (kpp_A, kpp_C)) + assert True + + kpp_to_micm.parse_kpp_arrhenius('ARR_abc(%.2e, %.2f, %.2f)' % (kpp_A, kpp_B, kpp_C)) + assert True + From bcc32d1f6de810ddc68c9b236f1e7b47ddcd8eec Mon Sep 17 00:00:00 2001 From: dwfncar Date: Tue, 15 Aug 2023 19:41:14 -0600 Subject: [PATCH 05/12] Added method kpp_to_micm.parse_kpp_arrhenius. --- etc/scripts/kpp_to_micm.py | 19 +++++++++++++++++-- etc/scripts/test_kpp_to_micm.py | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index b4b72bd3a..28db235d0 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -30,7 +30,7 @@ (4) Add support for many more reaction types ... Revision History: - 2023/08/03 Initial implementation + v1.00 2023/08/03 Initial implementation """ import os @@ -40,7 +40,7 @@ import json from glob import glob -__version__ = 1.0 +__version__ = 'v1.00' def read_kpp_config(kpp_dir): @@ -151,6 +151,21 @@ def parse_kpp_arrhenius(kpp_str): } """ logging.debug(kpp_str) + if 'ARR' in kpp_str: + coeffs = [float(coeff) for coeff in + kpp_str.split('(')[1].split(')')[0].split(',')] + logging.debug(coeffs) + if ('_ab' in kpp_str): + arr_dict = {'A': coeffs[0], 'B': coeffs[1]} + elif ('_ac' in kpp_str): + arr_dict = {'A': coeffs[0], 'C': coeffs[1]} + elif ('_abc' in kpp_str): + arr_dict \ + = {'A': coeffs[0], 'B': coeffs[1], 'C': coeffs[2]} + else: + arr_dict = {} + logging.debug(arr_dict) + return arr_dict def micm_equation_json(lines): diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index 74a262c4b..77eedf89a 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -23,7 +23,7 @@ def test_parse_kpp_arrhenius(): kpp_A, kpp_B, kpp_C = 1.0e-12, 2000.0, -3.0 kpp_to_micm.parse_kpp_arrhenius('ARR_ab(%.2e, %.2f)' % (kpp_A, kpp_B)) - assert True + assert True kpp_to_micm.parse_kpp_arrhenius('ARR_ac(%.2e, %.2f)' % (kpp_A, kpp_C)) assert True From 71efdbe0adc3e78441fc2929717093930deabeaa Mon Sep 17 00:00:00 2001 From: dwfncar Date: Tue, 15 Aug 2023 20:00:03 -0600 Subject: [PATCH 06/12] Added more tests for parse_kpp_arrhenius. --- etc/scripts/kpp_to_micm.py | 10 +++++----- etc/scripts/test_kpp_to_micm.py | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 28db235d0..dfa679ff9 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -155,13 +155,13 @@ def parse_kpp_arrhenius(kpp_str): coeffs = [float(coeff) for coeff in kpp_str.split('(')[1].split(')')[0].split(',')] logging.debug(coeffs) - if ('_ab' in kpp_str): - arr_dict = {'A': coeffs[0], 'B': coeffs[1]} - elif ('_ac' in kpp_str): - arr_dict = {'A': coeffs[0], 'C': coeffs[1]} - elif ('_abc' in kpp_str): + if ('_abc(' in kpp_str): arr_dict \ = {'A': coeffs[0], 'B': coeffs[1], 'C': coeffs[2]} + elif ('_ab(' in kpp_str): + arr_dict = {'A': coeffs[0], 'B': coeffs[1]} + elif ('_ac(' in kpp_str): + arr_dict = {'A': coeffs[0], 'C': coeffs[1]} else: arr_dict = {} logging.debug(arr_dict) diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index 77eedf89a..d99d2fdff 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -22,12 +22,18 @@ def test_parse_kpp_arrhenius(): kpp_A, kpp_B, kpp_C = 1.0e-12, 2000.0, -3.0 - kpp_to_micm.parse_kpp_arrhenius('ARR_ab(%.2e, %.2f)' % (kpp_A, kpp_B)) - assert True - - kpp_to_micm.parse_kpp_arrhenius('ARR_ac(%.2e, %.2f)' % (kpp_A, kpp_C)) - assert True - - kpp_to_micm.parse_kpp_arrhenius('ARR_abc(%.2e, %.2f, %.2f)' % (kpp_A, kpp_B, kpp_C)) - assert True - + arr_dict = kpp_to_micm.parse_kpp_arrhenius( + 'ARR_ab(%.2e, %.2f)' % (kpp_A, kpp_B)) + assert arr_dict['A'] == kpp_A + assert arr_dict['B'] == kpp_B + + arr_dict = kpp_to_micm.parse_kpp_arrhenius( + 'ARR_ac(%.2e, %.2f)' % (kpp_A, kpp_C)) + assert arr_dict['A'] == kpp_A + assert arr_dict['C'] == kpp_C + + arr_dict = kpp_to_micm.parse_kpp_arrhenius( + 'ARR_abc(%.2e, %.2f, %.2f)' % (kpp_A, kpp_B, kpp_C)) + assert arr_dict['A'] == kpp_A + assert arr_dict['B'] == kpp_B + assert arr_dict['C'] == kpp_C From d573ebdebc01b8e052c248d94c83e6ebb983c14e Mon Sep 17 00:00:00 2001 From: dwfncar Date: Wed, 16 Aug 2023 08:04:03 -0600 Subject: [PATCH 07/12] Call parse_kpp_arrhenius if ARR is in the KPP equation string. --- etc/scripts/kpp_to_micm.py | 39 +++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index dfa679ff9..8880267a8 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -31,6 +31,7 @@ Revision History: v1.00 2023/08/03 Initial implementation + v1.01 2023/08/16 Added method parse_kpp_arrhenius """ import os @@ -40,7 +41,7 @@ import json from glob import glob -__version__ = 'v1.00' +__version__ = 'v1.01' def read_kpp_config(kpp_dir): @@ -151,19 +152,23 @@ def parse_kpp_arrhenius(kpp_str): } """ logging.debug(kpp_str) - if 'ARR' in kpp_str: - coeffs = [float(coeff) for coeff in - kpp_str.split('(')[1].split(')')[0].split(',')] - logging.debug(coeffs) - if ('_abc(' in kpp_str): - arr_dict \ - = {'A': coeffs[0], 'B': coeffs[1], 'C': coeffs[2]} - elif ('_ab(' in kpp_str): - arr_dict = {'A': coeffs[0], 'B': coeffs[1]} - elif ('_ac(' in kpp_str): - arr_dict = {'A': coeffs[0], 'C': coeffs[1]} - else: - arr_dict = {} + coeffs = [float(coeff) for coeff in + kpp_str.split('(')[1].split(')')[0].split(',')] + logging.debug(coeffs) + arr_dict = dict() + arr_dict['type'] = 'ARRHENIUS' + if ('_abc(' in kpp_str): + arr_dict['A'] = coeffs[0] + arr_dict['B'] = coeffs[1] + arr_dict['C'] = coeffs[2] + elif ('_ab(' in kpp_str): + arr_dict['A'] = coeffs[0] + arr_dict['B'] = coeffs[1] + elif ('_ac(' in kpp_str): + arr_dict['A'] = coeffs[0] + arr_dict['C'] = coeffs[1] + else: + logging.error('unrecognized KPP Arrhenius syntax') logging.debug(arr_dict) return arr_dict @@ -202,12 +207,12 @@ def micm_equation_json(lines): if 'SUN' in coeffs: equation_dict['type'] = 'PHOTOLYSIS' + elif 'ARR' in coeffs: + equation_dict = parse_kpp_arrhenius(coeffs) else: + # default to Arrhenius with a single coefficient equation_dict['type'] = 'ARRHENIUS' - # assuming a single coefficient here equation_dict['A'] = float(coeffs) - # need to generalize to parse both A and C from KPP - equation_dict['C'] = 0.0 equation_dict['reactants'] = dict() equation_dict['products'] = dict() From 153008025d5d474eab0c7c0bd24353e87a7757da Mon Sep 17 00:00:00 2001 From: dwfncar Date: Wed, 16 Aug 2023 09:28:44 -0600 Subject: [PATCH 08/12] Adding KPP Arrhenius test configs. --- etc/configs/kpp/arr.eqn | 15 +++++++++++++++ etc/configs/kpp/arr.spc | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 etc/configs/kpp/arr.eqn create mode 100644 etc/configs/kpp/arr.spc diff --git a/etc/configs/kpp/arr.eqn b/etc/configs/kpp/arr.eqn new file mode 100644 index 000000000..a4016e5d2 --- /dev/null +++ b/etc/configs/kpp/arr.eqn @@ -0,0 +1,15 @@ +#EQUATIONS + +<1> NO2 + hv = NO + O3P : 6.69e-1*(SUN/60.0e0); +<2> O3P + O2 + AIR = O3 : ARR_ac(5.68e-34, -2.80e0); +<3> O3P + O3 = 2O2 : ARR_ab(8.00e-12, 2060.0e0); +<4> O3P + NO + AIR = NO2 : ARR_ac(1.00e-31, -1.60e0); +<5> O3P + NO2 = NO : ARR_ab(6.50e-12,- 120.0e0); +<7> O3 + NO = NO2 : ARR_ab(1.80e-12, 1370.0e0); +<8> O3 + NO2 = NO3 : ARR_ab(1.40e-13, 2470.0e0); +<9> NO + NO3 = 2NO2 : ARR_ab(1.80e-11, -110.0e0); +<10> NO + NO + O2 = 2NO2 : ARR_ab(3.30e-39, -530.0e0); +<14> NO2 + NO3 = NO + NO2 : ARR_ab(4.50e-14, 1260.0e0); +<15> NO3 + hv = NO : 1.59e0*(SUN/60.0e0); +<16> NO3 + hv = NO2 + O3P : 1.50e+1*(SUN/60.0e0); +<17> O3 + hv = O3P : 3.76e-2*(SUN/60.0e0); diff --git a/etc/configs/kpp/arr.spc b/etc/configs/kpp/arr.spc new file mode 100644 index 000000000..cffabd3bc --- /dev/null +++ b/etc/configs/kpp/arr.spc @@ -0,0 +1,19 @@ +#INCLUDE atoms.kpp + +#DEFVAR + + O3 = 3O; + H2O2 = 2H + 2O; + NO = N + O; + NO2 = N + 2O; + NO3 = N + 3O; + O3P = O; + + + +#DEFFIX + AIR = IGNORE; + O2 = 2O; + H2O = 2H + O; + H2 = 2H; + CH4 = C + 4H; From 954513f9fd56b8d229478f14a8a93023488a3791 Mon Sep 17 00:00:00 2001 From: dwfncar Date: Wed, 16 Aug 2023 10:23:14 -0600 Subject: [PATCH 09/12] Completed initial implementation of parse_kpp_arrhenius. --- etc/scripts/kpp_to_micm.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 8880267a8..48f8ae612 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -19,14 +19,12 @@ the KPP sections #ATOMS (not yet used), #DEFFIX, #DEFVAR, and #EQUATIONS are read and parsed. Equations with the hv reactant are MICM PHOTOLYSIS reactions, - all others are assumed to be ARRHENIUS reactions. + equations with a single coefficient are assumed to be ARRHENIUS reactions. TODO: - (1) Parse both A and C Arrhenius coefficients from KPP equations - Currently a single rate coeffient is assigned to A and C is set to 0. - (2) Translate stoichiometric coefficients in the equation string + (1) Translate stoichiometric coefficients in the equation string with more than one digit. - (3) Add method unit tests with pytest. + (3) Add more method unit tests with pytest. (4) Add support for many more reaction types ... Revision History: @@ -152,7 +150,7 @@ def parse_kpp_arrhenius(kpp_str): } """ logging.debug(kpp_str) - coeffs = [float(coeff) for coeff in + coeffs = [float(coeff.replace(' ', '')) for coeff in kpp_str.split('(')[1].split(')')[0].split(',')] logging.debug(coeffs) arr_dict = dict() @@ -187,9 +185,11 @@ def micm_equation_json(lines): equations = list() # list of dict for line in lines: + logging.debug(line) lhs, rhs = tuple(line.split('=')) - reactants = lhs.split('+') - products = rhs.split('+') + # temporary assumption of leading and trailing spaces + reactants = lhs.split(' + ') + products = rhs.split(' + ') # extract equation label delimited by < > label, reactants[0] = tuple(reactants[0].split('>')) @@ -197,7 +197,7 @@ def micm_equation_json(lines): # extract equation coefficients delimited by : products[-1], coeffs = tuple(products[-1].split(':')) - coeffs = coeffs.replace('(', '').replace(')', '').replace(';', '') + coeffs = coeffs.replace(';', '') # remove trailing and leading whitespace reactants = [reactant.strip().lstrip() for reactant in reactants] @@ -211,6 +211,7 @@ def micm_equation_json(lines): equation_dict = parse_kpp_arrhenius(coeffs) else: # default to Arrhenius with a single coefficient + coeffs = coeffs.replace('(', '').replace(')', '') equation_dict['type'] = 'ARRHENIUS' equation_dict['A'] = float(coeffs) From 2d0b99a8f2aaade43aa2f79e154f9603a2d217b8 Mon Sep 17 00:00:00 2001 From: dwfncar Date: Thu, 17 Aug 2023 10:34:17 -0600 Subject: [PATCH 10/12] Reworking method micm_equation_json. --- etc/scripts/kpp_to_micm.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 48f8ae612..050830435 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -24,8 +24,8 @@ TODO: (1) Translate stoichiometric coefficients in the equation string with more than one digit. - (3) Add more method unit tests with pytest. - (4) Add support for many more reaction types ... + (2) Add pytest unit test for method micm_equation_json. + (3+) Add support for many more reaction types ... Revision History: v1.00 2023/08/03 Initial implementation @@ -186,19 +186,22 @@ def micm_equation_json(lines): for line in lines: logging.debug(line) + + # split on equal sign into left hand and right hand sides lhs, rhs = tuple(line.split('=')) - # temporary assumption of leading and trailing spaces - reactants = lhs.split(' + ') - products = rhs.split(' + ') + + # extract reaction coefficients + rhs, coeffs = tuple(rhs.split(':')) + coeffs = coeffs.replace(';', '') + + # get reactants and products + reactants = lhs.split('+') + products = rhs.split('+') # extract equation label delimited by < > label, reactants[0] = tuple(reactants[0].split('>')) label = label.lstrip('<') - # extract equation coefficients delimited by : - products[-1], coeffs = tuple(products[-1].split(':')) - coeffs = coeffs.replace(';', '') - # remove trailing and leading whitespace reactants = [reactant.strip().lstrip() for reactant in reactants] products = [product.strip().lstrip() for product in products] From d3fe44c0605e0a6ca4e1a3f3120fbf2560c15c87 Mon Sep 17 00:00:00 2001 From: dwfncar Date: Thu, 17 Aug 2023 19:18:23 -0600 Subject: [PATCH 11/12] Added arg comments to parse_kpp_arrhenius. --- etc/scripts/kpp_to_micm.py | 27 +++++++++++++++++++++++---- etc/scripts/test_kpp_to_micm.py | 1 + 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 050830435..2e739bf4c 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -13,7 +13,8 @@ Description: kpp_to_micm.py translates KPP config files to MICM JSON config files (desginated by the suffixes .kpp, .spc, .eqn, .def) - from a single directory specified by the --kpp_dir argument. + from a single directory specified by the + --kpp_dir and --kpp_name arguments. In the initial implementation, the KPP sections #ATOMS (not yet used), #DEFFIX, @@ -42,7 +43,7 @@ __version__ = 'v1.01' -def read_kpp_config(kpp_dir): +def read_kpp_config(kpp_dir, kpp_name): """ Read all KPP config files in a directory @@ -58,7 +59,7 @@ def read_kpp_config(kpp_dir): lines = list() for suffix in suffixes: - files = glob(os.path.join(kpp_dir, '*' + suffix)) + files = glob(os.path.join(kpp_dir, kpp_name + '*' + suffix)) logging.debug(files) for filename in files: with open(filename, 'r') as f: @@ -130,6 +131,18 @@ def micm_species_json(lines, fixed=False, tolerance=1.0e-12): def parse_kpp_arrhenius(kpp_str): """ + Parse KPP Arrhenius reaction + + Parameters + (str) kpp_str: Arrhenius reaction string + + Returns + (dict): MICM Arrhenius reaction coefficients + + + Arrhenius formula from KPP + -------------------------- + KPP_REAL ARR_abc( float A0, float B0, float C0 ) { double ARR_RES; @@ -141,6 +154,9 @@ def parse_kpp_arrhenius(kpp_str): return (KPP_REAL)ARR_RES; } + Arrhenius formula from MICM + --------------------------- + inline double ArrheniusRateConstant::calculate( const double& temperature, const double& pressure) const { @@ -256,6 +272,9 @@ def micm_equation_json(lines): parser.add_argument('--kpp_dir', type=str, default=os.path.join('..', 'configs', 'kpp'), help='KPP input config directory') + parser.add_argument('--kpp_name', type=str, + default='small_strato', + help='KPP config name') parser.add_argument('--micm_dir', type=str, default=os.path.join('..', 'configs', 'micm'), help='MICM output species config file') @@ -275,7 +294,7 @@ def micm_equation_json(lines): """ Read KPP config files """ - lines = read_kpp_config(args.kpp_dir) + lines = read_kpp_config(args.kpp_dir, args.kpp_name) """ Split KPP config by section diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index d99d2fdff..874165593 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -37,3 +37,4 @@ def test_parse_kpp_arrhenius(): assert arr_dict['A'] == kpp_A assert arr_dict['B'] == kpp_B assert arr_dict['C'] == kpp_C + From bc3fffba5826e32ac0143012b597f7f9ee75402e Mon Sep 17 00:00:00 2001 From: dwfncar Date: Thu, 17 Aug 2023 20:11:24 -0600 Subject: [PATCH 12/12] Corrected for differing KPP and MICM coefficient conventions. --- etc/scripts/kpp_to_micm.py | 13 +++++++++---- etc/scripts/test_kpp_to_micm.py | 8 ++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/etc/scripts/kpp_to_micm.py b/etc/scripts/kpp_to_micm.py index 2e739bf4c..004ac88ea 100644 --- a/etc/scripts/kpp_to_micm.py +++ b/etc/scripts/kpp_to_micm.py @@ -171,16 +171,21 @@ def parse_kpp_arrhenius(kpp_str): logging.debug(coeffs) arr_dict = dict() arr_dict['type'] = 'ARRHENIUS' + # note the interchange of B and C, and change of sign + # in the KPP and MICM conventions if ('_abc(' in kpp_str): arr_dict['A'] = coeffs[0] - arr_dict['B'] = coeffs[1] - arr_dict['C'] = coeffs[2] + arr_dict['B'] = coeffs[2] + arr_dict['C'] = - coeffs[1] + arr_dict['D'] = 300.0 elif ('_ab(' in kpp_str): arr_dict['A'] = coeffs[0] - arr_dict['B'] = coeffs[1] + arr_dict['C'] = - coeffs[1] + arr_dict['D'] = 300.0 elif ('_ac(' in kpp_str): arr_dict['A'] = coeffs[0] - arr_dict['C'] = coeffs[1] + arr_dict['B'] = coeffs[1] + arr_dict['D'] = 300.0 else: logging.error('unrecognized KPP Arrhenius syntax') logging.debug(arr_dict) diff --git a/etc/scripts/test_kpp_to_micm.py b/etc/scripts/test_kpp_to_micm.py index 874165593..2eac98e50 100644 --- a/etc/scripts/test_kpp_to_micm.py +++ b/etc/scripts/test_kpp_to_micm.py @@ -25,16 +25,16 @@ def test_parse_kpp_arrhenius(): arr_dict = kpp_to_micm.parse_kpp_arrhenius( 'ARR_ab(%.2e, %.2f)' % (kpp_A, kpp_B)) assert arr_dict['A'] == kpp_A - assert arr_dict['B'] == kpp_B + assert arr_dict['C'] == - kpp_B arr_dict = kpp_to_micm.parse_kpp_arrhenius( 'ARR_ac(%.2e, %.2f)' % (kpp_A, kpp_C)) assert arr_dict['A'] == kpp_A - assert arr_dict['C'] == kpp_C + assert arr_dict['B'] == kpp_C arr_dict = kpp_to_micm.parse_kpp_arrhenius( 'ARR_abc(%.2e, %.2f, %.2f)' % (kpp_A, kpp_B, kpp_C)) assert arr_dict['A'] == kpp_A - assert arr_dict['B'] == kpp_B - assert arr_dict['C'] == kpp_C + assert arr_dict['C'] == - kpp_B + assert arr_dict['B'] == kpp_C