def test__sortby_mult(): """ test mechanalyzer.parser.sort Sort by multiplicity of the reaction """ results = { (('C5H10-2',), ('C4H71-3', 'CH3'), ('(+M)',)): str(1), (('C5H11-1',), ('C2H4', 'NC3H7'), (None,)): str(2), (('H', 'OH'), ('H2O',), ('+M',)): str(4), (('H', 'OH', 'AR'), ('H2O', 'AR'), (None,)): str(4), (('CH3', 'IC4H7'), ('AC5H10',), ('(+M)',)): str(4), (('H', 'O2'), ('HO2',), ('(+HE)',)): str(6) } # Read the mechanism files into strings spc_path = os.path.join(CWD, 'data', 'NUIG_species.csv') mech_path = os.path.join(CWD, 'data', 'NUIG_mechred.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort mechanism by multiplicity - No Headers Included isolate_spc = [] sort_lst = ['mult', 0] param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) for rxn in param_dct_sort.keys(): assert cmts_dct[rxn]['cmts_inline'][-1] == results[rxn] print('ok')
def test__readwrite_thirdbody(): """ test mechanalyzer.parser.sort Checks read/write of a small set of rxns involving third bodies """ # Setting the values of the dictionary to be None since they don't matter trd_bdy_dct = { (('H', 'OH'), ('H2O',), ('+M',)): None, (('H', 'OH', 'AR'), ('H2O', 'AR'), (None,)): None, (('H', 'O2'), ('HO2',), ('(+HE)',)): None, (('CH3', 'IC4H7'), ('AC5H10',), ('(+M)',)): None, (('C5H10-2',), ('C4H71-3', 'CH3'), ('(+M)',)): None, (('C5H11-1',), ('C2H4', 'NC3H7'), (None,)): None } # Read the mechanism files into strings spc_path = os.path.join(CWD, 'data', 'NUIG_species.csv') mech_path = os.path.join(CWD, 'data', 'NUIG_mechred.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort mechanism by PES - No Headers Included isolate_spc = [] sort_lst = ['pes', 0] param_dct_sort, _, _, _, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) # Just checking keys since this is what the sorting is according to assert param_dct_sort.keys() == trd_bdy_dct.keys() print('ok')
def test__sort_with_input(): """ sort by using the auxlilary input files to specify parameters """ results = [ [(('C2H4',), ('H2', 'H2CC'), ('(+M)',)), '! pes.subpes.NR.rxntype 1.1.1.Decomposition'], [(('C2H3', 'H'), ('C2H4',), ('(+M)',)), '! pes.subpes.NR.rxntype 1.1.2.Recombination H'], [(('C2H4', 'H'), ('C2H5',), ('(+M)',)), '! pes.subpes.NR.rxntype 2.1.2.Addition H'], [(('C2H4', 'H'), ('C2H3', 'H2'), (None,)), '! pes.subpes.NR.rxntype 2.2.2.H abstraction'], [(('CH2(S)', 'CH3'), ('C2H4', 'H'), (None,)), '! pes.subpes.NR.rxntype 2.3.2.Addition-decomposition - propagation'], [(('C2H5', 'H'), ('C2H4', 'H2'), (None,)), '! pes.subpes.NR.rxntype 3.1.2.Recombination-decomposition - termination'], [(('C2H4', 'O'), ('CH3', 'HCO'), (None,)), '! pes.subpes.NR.rxntype 5.1.2.Addition-decomposition - branching'], [(('C2H4', 'OH'), ('PC2H4OH',), (None,)), '! pes.subpes.NR.rxntype 6.1.2.Addition OH'], [(('C2H5', 'OH'), ('C2H4', 'H2O'), (None,)), '! pes.subpes.NR.rxntype 7.1.2.Recombination-decomposition - termination'], [(('C2H4', 'O2'), ('C2H3', 'HO2'), (None,)), '! pes.subpes.NR.rxntype 9.1.2.H abstraction'], [(('C2H5O2',), ('C2H4', 'HO2'), (None,)), '! pes.subpes.NR.rxntype 10.1.1.Beta-scission +HO2'], [(('C2H4', 'CH3'), ('C2H3', 'CH4'), (None,)), '! pes.subpes.NR.rxntype 11.1.2.H abstraction'], [(('C3H4-A', 'O'), ('C2H4', 'CO'), (None,)), '! pes.subpes.NR.rxntype 12.1.2.Addition-decomposition - termination'] ] # Read the mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_C2H4_mech.dat') sort_path = os.path.join(CWD, 'data', 'sort.dat') spc_str, mech_str, sort_str = _read_files(spc_path, mech_path, sort_path) # Sort mechanism isolate_spc, sort_lst = mparser.parse_sort(sort_str) param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) index = 0 for rxn in param_dct_sort.keys(): assert [rxn, cmts_dct[rxn]['cmts_inline']] == results[index] index += 1 print('ok')
def test__sortby_molec_r1(): """ test mechanalyzer.parser.sort Sort by first (heavier) reactant and molecularity of the reaction """ comments_results = [ 'C2H3.2', 'C2H3.2', 'C2H3.2', 'C2H3.2', 'C2H3.2', 'C2H3.2', 'C2H3OO.1', 'C2H3OO.1', 'C2H4.1', 'C2H4.2', 'C2H4.2', 'C2H4.2', 'C2H4.2', 'C2H4.2', 'C2H4.2', 'C2H5.2', 'C2H5.2', 'C2H5.2', 'C2H5O2.1', 'C2H6.2', 'C3H4-A.2', 'CH3.2', 'CH3.2', 'HOCH2CO.1'] # Read mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_C2H4_mech.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort mechanism by R1-molecularity - No Headers Included isolate_spc = [] sort_lst = ['r1', 'molecularity', 0] # NO HEADERS INCLUDED # ONLY BY MOLEC- CHECK HEADERS WITH INT NUMBER sort_lst_2 = ['molecularity', 0] param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) param_dct_sort_2, _, _, cmts_dct_2, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst_2) # NO NEED TO CALL IT AFTERWARDS comments = [] for rxn in param_dct_sort.keys(): comments.append(''.join(cmts_dct[rxn]['cmts_inline'].split()[-1:])) assert comments == comments_results print('ok')
def test__sortby_species_subpes(): """ test mechanalyzer.parser.sort Select a species subset from a mechanism and extract all reactions they are involved to Within the reaction subset, classify according to subpes (or potentially any other criteria) """ results = [ [(('IC8',), ('NEOC5H11', 'IC3H7'), (None,)), '! pes.subpes 26.1'], [(('IC8', 'O2'), ('IC8-1R', 'HO2'), (None,)), '! pes.subpes 28.1'], [(('IC8-1R',), ('IC8-5R',), (None,)), '! pes.subpes 25.1'], [(('IC8-1R', 'O2'), ('IC8-1O2R',), (None,)), '! pes.subpes 27.1'], [(('IC8-1R', 'CH3O2'), ('IC8-1OR', 'CH3O'), (None,)), '! pes.subpes 32.1'], ] # Read mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_IC8_red_submech.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort mechanism by subpes with Headers for specues subset isolate_spc = ['IC8', 'IC8-1R'] sort_lst = ['species', 'subpes', 1] print('Sort by species subset + subpes test:') param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) sorted_results = [] for rxn in param_dct_sort.keys(): sorted_results.append( [rxn, cmts_dct[rxn]['cmts_inline']]) assert sorted_results == results print('ok')
# PAR.add_argument('-o2', '--out2', default='outmech2.dat', # help='output file name (outmech2.dat)') OPTS = vars(PAR.parse_args()) # Read the input files spc_str = pathtools.read_file(CWD, OPTS['spc'], remove_comments='#') mech_str = pathtools.read_file(CWD, OPTS['mech'], remove_comments='#') sort_str = pathtools.read_file(CWD, OPTS['sort'], remove_comments='#') # Check if the input strings exist if any(string is None for string in (spc_str, mech_str, sort_str)): print('ERROR: Input file missing') sys.exit() # Build sorted mechanism files isolate_spc, sort_list = mparser.parse_sort(sort_str) param_dct_sort, _, spc_dct, cmts_dct, elems = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_str) # Write the output files (need to make general at some point) sortd_mech_str = chemkin_io.writer.mechanism.write_chemkin_file( elem_tuple=elems, spc_dct=spc_dct, rxn_param_dct=param_dct_sort, comments=cmts_dct) sortmech_out_path = os.path.join(CWD, OPTS['out']) # restmech_out_path = os.path.join(CWD, OPTS['out2']) with open(sortmech_out_path, 'w') as mech_obj: mech_obj.write(sortd_mech_str)
def test__sortby_submech_class(): """ test mechanalyzer.parser.sort sort by fuel submechanism: extract reactions of fuel, fuel radicals, R+O2, R+O4 then order by subpes and broad class """ results = [ [(('IC8',), ('NEOC5H11', 'IC3H7'), (None,)), ' FUEL.2.1.Bond fission'], [(('IC8', 'O2'), ('IC8-1R', 'HO2'), (None,)), ' FUEL.4.2.H abstraction'], [(('IC8OOH1',), ('IC8-1OR', 'OH'), (None,)), ' FUEL_ADD_O2.4.1.Bond fission +OH'], [(('IC8-1O2R', 'HO2'), ('IC8OOH1', 'O2'), (None,)), ' FUEL_ADD_O2.6.1.Recombination-decomposition - termination'], [(('IC8-1O2R', 'H2O2'), ('IC8OOH1', 'HO2'), (None,)), ' FUEL_ADD_O2.7.1.H abstraction'], [(('IC8-1R',), ('IC8-5R',), (None,)), ' FUEL_RAD.1.1.Isomerization'], [(('IC8-1R', 'O2'), ('IC8-1O2R',), (None,)), ' FUEL_RAD.3.1.Recombination O2'], [(('IC8-3R', 'O2'), ('IC8D3', 'HO2'), (None,)), ' FUEL_RAD.3.3.Recombination-decomposition - termination'], [(('IC8-1R', 'CH3O2'), ('IC8-1OR', 'CH3O'), (None,)), ' FUEL_RAD.8.1.Recombination-decomposition - propagation'], [(('IC8OOH1-1AR',), ('IC4H7OOH', 'IC4H9'), (None,)), ' R_O2.3.1.Beta-scission'], [(('IC8OOH1-1AR',), ('IC8O1-1A', 'OH'), (None,)), ' R_O2.3.1.Beta-scission +OH'], [(('IC8OOH1-1AR',), ('CH2O', 'I24C7D1', 'OH'), (None,)), ' R_O2.3.1.Decomposition(lumped)'], [(('IC8-1O2R',), ('IC8OOH1-1AR',), (None,)), ' R_O2.3.1.Isomerization'], [(('IC8-3O2R',), ('IC8D3', 'HO2'), (None,)), ' R_O2.3.2.Beta-scission +HO2'], [(('IC8OOH1-1AR', 'O2'), ('IC8OOH1-1AO2R',), (None,)), ' R_O2.5.1.Recombination O2'], ] # Read mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_IC8_red_mech.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort with headers for species subset isolate_spc = ['IC8'] sort_lst = ['submech', 'subpes', 'rxn_class_broad', 0] param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) print('Sort by submech-subpes-classbroad test:') sorted_results = [] for rxn in param_dct_sort.keys(): sorted_results.append( [rxn, cmts_dct[rxn]['cmts_inline'].split('rxntype')[1]]) assert results == sorted_results print('ok')
def test__sortby_submech_subpes_chnl(): """ test mechanalyzer.parser.sort sort by fuel submechanism: extract reactions of fuel, fuel radicals, R+O2, R+O4 then order by subpes and broad class """ results = [ [(('IC8-1R',), ('IC8-5R',), (None,)), '! pes.subpes.channel 1.1.1'], [(('IC8',), ('NEOC5H11', 'IC3H7'), (None,)), '! pes.subpes.channel 2.1.1'], [(('IC8OOH1-1AR',), ('IC8O1-1A', 'OH'), (None,)), '! pes.subpes.channel 3.1.1'], [(('IC8OOH1-1AR',), ('IC4H7OOH', 'IC4H9'), (None,)), '! pes.subpes.channel 3.1.2'], [(('IC8OOH1-1AR',), ('CH2O', 'I24C7D1', 'OH'), (None,)), '! pes.subpes.channel 3.1.3'], [(('IC8-1R', 'O2'), ('IC8-1O2R',), (None,)), '! pes.subpes.channel 3.1.4'], [(('IC8-1O2R',), ('IC8OOH1-1AR',), (None,)), '! pes.subpes.channel 3.1.5'], [(('IC8-3O2R',), ('IC8D3', 'HO2'), (None,)), '! pes.subpes.channel 3.2.6'], [(('IC8-3R', 'O2'), ('IC8D3', 'HO2'), (None,)), '! pes.subpes.channel 3.3.7'], [(('IC8OOH1',), ('IC8-1OR', 'OH'), (None,)), '! pes.subpes.channel 4.1.1'], [(('IC8', 'O2'), ('IC8-1R', 'HO2'), (None,)), '! pes.subpes.channel 4.2.2'], [(('IC8OOH1-1AR', 'O2'), ('IC8OOH1-1AO2R',), (None,)), '! pes.subpes.channel 5.1.1'], [(('IC8-1O2R', 'HO2'), ('IC8OOH1', 'O2'), (None,)), '! pes.subpes.channel 6.1.1'], [(('IC8-1O2R', 'H2O2'), ('IC8OOH1', 'HO2'), (None,)), '! pes.subpes.channel 7.1.1'], [(('IC8-1R', 'CH3O2'), ('IC8-1OR', 'CH3O'), (None,)), '! pes.subpes.channel 8.1.1'] ] # Read mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_IC8_red_mech.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort with headers for species subset isolate_spc = [] sort_lst = ['subpes', 'chnl', 0] param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) print('Sort by submech-subpes-classbroad test:') sorted_results = [] for rxn in param_dct_sort.keys(): sorted_results.append( [rxn, cmts_dct[rxn]['cmts_inline']]) assert results == sorted_results print('ok')
def test_sortby_rxnclass(): """ test mechanalyzer.parser.sort sort by reaction class: both "broad" (based on multiplicity, reactants/products..) and "graph" (based on graph classification - warning, CPU intensive) prior to rxn class, the mech is also subdivided into PESs """ results = [ [(('C2H5', 'H'), ('C2H6',), ('(+M)',)), ' addition.Recombination H'], [(('C2H3', 'H'), ('C2H4',), ('(+M)',)), ' addition.Recombination H'], [(('HOCH2CO',), ('CH2OH', 'CO'), (None,)), ' beta scission.Decomposition'], [(('C2H3OO',), ('CH2O', 'HCO'), (None,)), ' elimination.Beta-scission'], [(('C2H5O2',), ('C2H4', 'HO2'), (None,)), ' elimination.Beta-scission +HO2'], # removed because classifer is not working well right now # [(('C2H4',), ('H2', 'H2CC'), ('(+M)',)), # ' elimination.Decomposition'], [(('C2H4', 'H'), ('C2H3', 'H2'), (None,)), ' hydrogen abstraction.H abstraction'], [(('C2H5', 'H'), ('C2H4', 'H2'), (None,)), ' hydrogen abstraction.Recombination-decomposition - termination'], [(('C2H3', 'O2'), ('C2H2', 'HO2'), (None,)), ' hydrogen abstraction.Recombination-decomposition - termination'], [(('C3H5-A',), ('C3H5-T',), (None,)), ' hydrogen migration.Isomerization'], [(('C3H5-A',), ('C3H5-S',), (None,)), ' hydrogen migration.Isomerization'], [(('CH2(S)', 'CH3'), ('C2H4', 'H'), (None,)), ' substitution.Addition-decomposition - propagation'], [(('CH3', 'CH3'), ('H', 'C2H5'), (None,)), ' substitution.Recombination-decomposition - propagation'], # removed because classifer is not working well right now # [(('C3H4-A', 'O'), ('C2H4', 'CO'), (None,)), # ' unclassified.Addition-decomposition - termination'] ] # Read mechanism files into strings spc_path = os.path.join(CWD, 'data', 'LLNL_species.csv') mech_path = os.path.join(CWD, 'data', 'LLNL_C2H4_mech_class.dat') sort_path = None spc_str, mech_str, _ = _read_files(spc_path, mech_path, sort_path) # Sort mechanism by reaction class isolate_spc = [] sort_lst = ['rxn_class_graph', 'rxn_class_broad', 0] print('Sort by rxn class broad+graph test:') param_dct_sort, _, _, cmts_dct, _ = sorter.sorted_mech( spc_str, mech_str, isolate_spc, sort_lst) sorted_results = [] for rxn in param_dct_sort.keys(): sorted_results.append( [rxn, cmts_dct[rxn]['cmts_inline'].split('type')[1]]) assert sorted_results == results print('ok')
# Write the new (sorted) species dictionary to a string HEADERS = ('smiles', 'inchi', 'inchikey', 'mult', 'charge') ste_mech_spc_dct_sort = mechanalyzer.parser.spc.reorder_by_atomcount( ste_mech_spc_dct) csv_str = mechanalyzer.parser.spc.csv_string(ste_mech_spc_dct_sort, HEADERS) # Write initial string to call the sorter mech_str = chemkin_io.writer.mechanism.write_chemkin_file( elem_tuple=None, mech_spc_dct=ste_mech_spc_dct_sort, spc_nasa7_dct=None, rxn_param_dct=ste_rxn_dct, rxn_cmts_dct=None) # Use strings to generate ordered objects param_dct_sort, _, ste_mech_spc_dct_sort, cmts_dct, elems = sorter.sorted_mech( csv_str, mech_str, isolate_spc, sort_lst) rxn_cmts_dct = chemkin_io.writer.comments.get_rxn_cmts_dct( rxn_sort_dct=cmts_dct) # WRITE OUTPUT AND EXIT # Write the dictionaries to ordered strings header_lst = list(FILE_DCT['headers']) header_lst.remove('name') # remove redundant 'name' entry csv_str = mechanalyzer.parser.spc.csv_string( ste_mech_spc_dct_sort, header_lst) mech_str = chemkin_io.writer.mechanism.write_chemkin_file( elem_tuple=elems, mech_spc_dct=ste_mech_spc_dct_sort, spc_nasa7_dct=None, rxn_param_dct=param_dct_sort,