|
| 1 | +""" Script for running a comparison of rate constants between mechanisms |
| 2 | +""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import numpy |
| 7 | +import yaml |
| 8 | +import mechanalyzer.calculator.compare as compare |
| 9 | +import mechanalyzer.plotter.rates as plot_rates |
| 10 | +import mechanalyzer.plotter._util as util |
| 11 | +import mechanalyzer.parser.new_spc as spc_parser |
| 12 | +import mechanalyzer.parser.ckin_ as ckin_parser |
| 13 | +from ioformat import pathtools |
| 14 | + |
| 15 | + |
| 16 | +def read_mechs_yaml(mechs_yaml_file): |
| 17 | + """ read the yaml file that specifies each mechanism file |
| 18 | + for each mechanism, makes sure all files are given |
| 19 | + """ |
| 20 | + def _mech_is_fully_specified(mech_data): |
| 21 | + """ make sure all files are given in yaml for mechanism |
| 22 | + """ |
| 23 | + keys = ['rate_file', 'therm_file', 'species_csv'] |
| 24 | + _fully_specified = True |
| 25 | + if not all(key in mech_data for key in keys): |
| 26 | + _fully_specified = False |
| 27 | + print( |
| 28 | + 'missing in yaml: ', |
| 29 | + ','.join([key for key in keys if key not in mech_data])) |
| 30 | + return _fully_specified |
| 31 | + |
| 32 | + with open(mechs_yaml_file, 'r') as file: |
| 33 | + mechs = yaml.safe_load(file) |
| 34 | + mechs = { |
| 35 | + key: mech_files for key, mech_files in mechs.items() |
| 36 | + if _mech_is_fully_specified(mech_files)} |
| 37 | + |
| 38 | + labels = [] |
| 39 | + mech_files = [] |
| 40 | + therm_files = [] |
| 41 | + csv_files = [] |
| 42 | + for key, mech in mechs.items(): |
| 43 | + labels.append(key) |
| 44 | + mech_files.append(mech['rate_file']) |
| 45 | + therm_files.append(mech['therm_file']) |
| 46 | + csv_files.append(mech['species_csv']) |
| 47 | + |
| 48 | + return labels, mech_files, therm_files, csv_files |
| 49 | + |
| 50 | + |
| 51 | +def main( |
| 52 | + mechs_yaml='mechs.yaml', |
| 53 | + plot_filename='rate_plot.pdf', |
| 54 | + out_txt_filename='ordering.txt', |
| 55 | + job_path='.', |
| 56 | + temps_lst=None, pressures=None, |
| 57 | + sort_method=None, rev_rates=True, |
| 58 | + remove_loners=True, write_file=False): |
| 59 | + |
| 60 | + labels, mech_files, therm_files, csv_files = read_mechs_yaml(mechs_yaml) |
| 61 | + if temps_lst is None or len(temps_lst) < 2: |
| 62 | + temps_lst = [numpy.linspace(500, 1000, 16)] |
| 63 | + else: |
| 64 | + temps_lst = [numpy.linspace(temps_lst[0], temps_lst[1], 16)] |
| 65 | + if pressures is None or len(pressures) < 1: |
| 66 | + pressures = [1, 10, 100] |
| 67 | + |
| 68 | + rxn_ktp_dcts = ckin_parser.load_rxn_ktp_dcts( |
| 69 | + mech_files, job_path, temps_lst, pressures) |
| 70 | + spc_therm_dcts = ckin_parser.load_spc_therm_dcts( |
| 71 | + therm_files, job_path, temps_lst[0]) # NOTE: taking first entry |
| 72 | + spc_dcts = spc_parser.load_mech_spc_dcts(csv_files, job_path) |
| 73 | + |
| 74 | + # Get the algn_rxn_ktp_dct |
| 75 | + temps = temps_lst[0] # function receives a single Numpy array of temps |
| 76 | + algn_rxn_ktp_dct = compare.get_algn_rxn_ktp_dct( |
| 77 | + rxn_ktp_dcts, spc_therm_dcts, spc_dcts, temps, rev_rates=rev_rates, |
| 78 | + remove_loners=remove_loners, write_file=write_file) |
| 79 | + |
| 80 | + # Run the plotter |
| 81 | + figs = plot_rates.build_plots( |
| 82 | + algn_rxn_ktp_dct, |
| 83 | + mech_names=labels, |
| 84 | + ratio_sort=bool(sort_method == 'ratios')) |
| 85 | + util.build_pdf(figs, filename=plot_filename, path=job_path) |
| 86 | + |
| 87 | + # Write the ordered text file |
| 88 | + FSTR = compare.write_ordered_str(algn_rxn_ktp_dct, dct_type='rxn') |
| 89 | + pathtools.write_file(FSTR, job_path, out_txt_filename) |
| 90 | + |
0 commit comments