def objects_dct(job_path): """ Get the sections for the run block """ # Read the obj section run_str = ptt.read_inp_str(job_path, RUN_INP, remove_comments='#') obj_str = object_block(run_str) # Read the sections of the obj section pes_block_str = apf.first_capture(ptt.paren_section('pes'), obj_str) spc_block_str = apf.first_capture(ptt.paren_section('spc'), obj_str) # Check if the obj section has been specified check_obj_spec(obj_str, pes_block_str, spc_block_str) # Build the run dictionary run_dct = {} if pes_block_str is not None: run_dct['pes'] = get_pes_idxs( ioformat.remove_whitespace(pes_block_str)) else: run_dct['pes'] = [] if spc_block_str is not None: run_dct['spc'] = get_spc_idxs( ioformat.remove_whitespace(spc_block_str)) else: run_dct['spc'] = [] return run_dct
def build_thermo_name_dcts(mech1_str, mech2_str, temps): """ Builds the thermo dictionaries indexed by names. :param mech1_str: string of mechanism 1 input file :type mech1_str: str :param mech2_str: string of mechanism 2 input file :type mech2_str: str :param temps: Temperatures to calculate thermochemistry (K) :type temps: list(float) :return: mech1_thermo_dct :rtype: dict[name: [thermo]] :return: mech2_thermo_dct :rtype: dict[name: [thermo]] """ mech1_thermo_block = mech_parser.thermo_block(mech1_str) if mech1_thermo_block is not None: mech1_thermo_block = remove_whitespace(mech1_thermo_block) mech1_thermo_dct = thermo.mechanism(mech1_thermo_block, temps) else: mech1_thermo_dct = None mech2_thermo_block = mech_parser.thermo_block(mech2_str) if mech2_thermo_block is not None: mech2_thermo_block = remove_whitespace(mech2_thermo_block) mech2_thermo_dct = thermo.mechanism(mech2_thermo_block, temps) else: mech2_thermo_dct = None return mech1_thermo_dct, mech2_thermo_dct
def build_reaction_name_dcts(mech1_str, mech2_str, t_ref, temps, pressures, ignore_reverse=True, remove_bad_fits=False): """ Parses the strings of two mechanism files and calculates rate constants [k(T,P)]s at an input set of temperatures and pressures. :param mech1_str: string of mechanism 1 input file :type mech1_str: str :param mech2_str: string of mechanism 2 input file :type mech2_str: str :param t_ref: Reference temperature (K) :type t_ref: float :param temps: List of Temperatures (K) :type temps: numpy.ndarray :return mech1_ktp_dct: rate constants for mechanism 1 :rtype: dict[pressure: rates] :return mech2_ktp_dct: rate constants for mechanism 2 :rtype: dict[pressure: rates] """ mech1_reaction_block = remove_whitespace( mech_parser.reaction_block(mech1_str)) mech1_units = reaction_units(mech1_str) mech1_ktp_dct = rates.mechanism(mech1_reaction_block, mech1_units, t_ref, temps, pressures, ignore_reverse=ignore_reverse, remove_bad_fits=remove_bad_fits) if mech2_str: mech2_reaction_block = remove_whitespace( mech_parser.reaction_block(mech2_str)) mech2_units = reaction_units(mech2_str) mech2_ktp_dct = rates.mechanism(mech2_reaction_block, mech2_units, t_ref, temps, pressures, ignore_reverse=ignore_reverse, remove_bad_fits=remove_bad_fits) else: mech2_ktp_dct = {} return mech1_ktp_dct, mech2_ktp_dct
def build_vals_lst(section_str): """ build lst """ val_lst = [] section_str = ioformat.remove_whitespace(section_str) for line in section_str.splitlines(): val_lst.extend((float(val) for val in line.split())) return val_lst
def build_keyword_lst(section_str): """ build lst """ keyword_lst = [] section_str = ioformat.remove_whitespace(section_str) for line in section_str.splitlines(): keyword_lst.append(line) return keyword_lst
def trans_tsks_block(inp_str): """ Read the string that has the global model information """ cap = apf.first_capture(ptt.end_section('trans_tsks'), inp_str) if cap is not None: trans_str = ioformat.remove_whitespace(cap) else: trans_str = None return trans_str
def build_keyword_dct(section_str): """ Take a section with keywords defined and build a dictionary for the keywords """ keyword_dct = {} section_str = ioformat.remove_whitespace(section_str) for line in section_str.splitlines(): key_val = apf.first_capture(KEYWORD_KEYVALUE_PATTERN, line) formtd_key, formtd_val = format_param_vals(key_val) keyword_dct[formtd_key] = formtd_val return keyword_dct
def _clean_up(mech_str, remove_comments=True): """ Cleans up mechanism input string by converting specific comment lines that are used later and removes other comments and whitespace from mech string. :param mech_str: string of mechanism input file :param mech_str: str :return mech_str: string with altered comment lines :rtype: string """ mech_str = _convert_comment_lines(mech_str) if remove_comments: mech_str = remove_comment_lines( mech_str, delim_pattern=app.escape('!')) mech_str = remove_whitespace(mech_str) return mech_str
def es_tsks_from_lst(es_tsks_str): """ Take the es tsk list string from input and set the tasks Right now, we presume the tasks given in the file are correct """ # Split the string into different strings of keywords tsk_lst = [] es_tsks_str = ioformat.remove_whitespace(es_tsks_str) for line in es_tsks_str.splitlines(): tsk_line = line.split() if len(tsk_line) > 2: obj, tsk, keyword_lst = tsk_line[0], tsk_line[1], tsk_line[2:] keyword_dct = format_tsk_keywords(keyword_lst) else: print('BAAD') tsk_lst.append([obj, tsk, keyword_dct]) return tsk_lst
def _build_cla_dct(cla_str): """ read file """ cla_dct = {} cla_str = remove_whitespace(cla_str) for line in cla_str.splitlines(): # try: [rxn_line, rclass] = line.split('||') reacs = chemkin_io.parser.reaction.reactant_names(rxn_line) prods = chemkin_io.parser.reaction.product_names(rxn_line) cla_dct[(reacs, prods)] = rclass # except: # print('*ERROR: Error in formatting line') # print(line) # sys.exit() return cla_dct
def test__string_alter(): """ test ioformat.headlined_sections test ioformat.remove_whitespace test ioformat.remove_trail_whitespace test ioformat.remove_comment_lines """ head_secs = ioformat.headlined_sections(INI_STR, '=') assert head_secs == [ 'A + B = C + D 1.00 0.00 0.00 \n PLOG / 0.01 3.0E+8 2.0 100.0\n', 'A + D = E 2.00 0.00 0.00 \n\n! From Experiment', 'E = F 3.00 0.00 0.00 \n PLOG / 0.01 6.0 2.5 105\n\n' ] out_str = ioformat.remove_whitespace(INI_STR) assert out_str == ('! From Theory\n' 'A + B = C + D 1.00 0.00 0.00\n' 'PLOG / 0.01 3.0E+8 2.0 100.0\n' 'A + D = E 2.00 0.00 0.00\n' '! From Experiment\n' 'E = F 3.00 0.00 0.00\n' 'PLOG / 0.01 6.0 2.5 105\n') out_str = ioformat.remove_trail_whitespace(INI_STR) assert out_str == ('! From Theory\n' 'A + B = C + D 1.00 0.00 0.00\n' ' PLOG / 0.01 3.0E+8 2.0 100.0\n' 'A + D = E 2.00 0.00 0.00\n' '! From Experiment\n' 'E = F 3.00 0.00 0.00\n' ' PLOG / 0.01 6.0 2.5 105\n') out_str = ioformat.remove_comment_lines(INI_STR, '!') assert out_str == ('\n' 'A + B = C + D 1.00 0.00 0.00 \n' ' PLOG / 0.01 3.0E+8 2.0 100.0\n' '\n' 'A + D = E 2.00 0.00 0.00 \n' '\n' '\n' 'E = F 3.00 0.00 0.00 \n' ' PLOG / 0.01 6.0 2.5 105\n' '\n' '\n')
def trans_tsk_lst(trans_tsk_str, thy_dct, saddle=False): """ Set the sequence of electronic structure tasks for a given species or PESs """ # Split the string into different strings of keywords tsk_lst = [] trans_tsks_str = ioformat.remove_whitespace(trans_tsk_str) for line in trans_tsks_str.splitlines(): tsk_line = line.split() if len(tsk_line) > 2: obj, tsk, keyword_lst = tsk_line[0], tsk_line[1], tsk_line[2:] keyword_dct = format_tsk_keywords(keyword_lst) else: print('BAAD') tsk_lst.append([obj, tsk, keyword_dct]) # Add defaults if they are missing mod_tsk_lst = add_defaults_to_trans_keyword_dct(tsk_lst) return mod_tsk_lst
def _tsk_lst(tsk_str, num): """ Set the sequence of electronic structure tasks for a given species or PESs """ # Build the task lists from the string if tsk_str is not None: tsks = [] tsk_str = ioformat.remove_whitespace(tsk_str) for line in tsk_str.splitlines(): try: _tsk = _split_line(line, num) except: print('Task line not formatted correctly:\n{}'.format(line)) sys.exit() tsks.append(_tsk) mod_tsks = _expand_tsks(tsks) if num == 3 else tsks else: mod_tsks = None return mod_tsks
def sm_rates(mech_str, t_ref, temps, pressures, dir_prefix='rate_plots', ignore_reverse=False, remove_bad_fits=False): """ Calculate rates for a single mechanism """ # Parse mechanism file and cacluate the rates mech1_reaction_block = remove_whitespace( mech_parser.reaction_block(mech_str)) mech1_units = chemkin_io.parser.mechanism.reaction_units(mech_str) mech1_ktp_dct = calculator.rates.mechanism(mech1_reaction_block, mech1_units, t_ref, temps, pressures, ignore_reverse=ignore_reverse, remove_bad_fits=remove_bad_fits) # Build the plots plotter.sm_rates.build(mech1_ktp_dct, temps, dir_prefix=dir_prefix)
FAKE_CSV_NAME = 'fake_species.csv' # Read mechanism and csv strings FAKE1_MECH_STR = _read_file( os.path.join(DATA_PATH, FAKE1_MECH_NAME)) FAKE2_MECH_STR = _read_file( os.path.join(DATA_PATH, FAKE2_MECH_NAME)) FAKE_CSV_STR = _read_file( os.path.join(DATA_PATH, FAKE_CSV_NAME)) # Read species blocks FAKE1_THERMO_BLOCK = parser.mechanism.thermo_block( FAKE1_MECH_STR) FAKE1_BLOCK_STRS = parser.thermo.data_strings( FAKE1_THERMO_BLOCK) FAKE2_THERMO_BLOCK = remove_whitespace( parser.mechanism.thermo_block(FAKE2_MECH_STR)) FAKE2_BLOCK_STRS = parser.thermo.data_strings( FAKE2_THERMO_BLOCK) # Temperatures to run TEMPS = [500.0, 1000.0, 2000.0] def test__compare_thermo(): """ test chemkin_io.calculator.combine.mechanism_thermo """ # Build dictionaries containing the thermo data strings for each species # Dictionaries indexed by the given mechanism names or InCHI string # Build name for test but use inchi
def inp_block(inp_str): """ Read the string that has the global model information """ return ioformat.remove_whitespace( apf.first_capture(ptt.end_section('input'), inp_str))