def read_inp_str(filepath, filename, remove_comments=None): """ read the run parameters from a file """ input_file = os.path.join(filepath, filename) try: with open(input_file, 'r') as inp_file: inp_str = inp_file.read() if remove_comments: inp_str = ioformat.remove_comment_lines(inp_str, remove_comments) except FileNotFoundError: print('*ERROR: Input file does not exist: ', input_file) sys.exit() return inp_str
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 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 ped_names(input_str): """ Reads the ped_species and ped_output from the MESS input file string that were used in the master-equation calculation. :param input_str: string of lines of MESS input file :type input_str: str :return ped_species: list of names of connected REACS/PRODS :rtype: list(list(str)) :return ped_output: output file with the product energy distribution :rtype: str """ # Get the MESS input lines input_str = remove_comment_lines(input_str, delim_pattern=app.escape('!')) mess_lines = input_str.splitlines() check = 0 ped_species = () ped_output = None for line in mess_lines: if 'PEDSpecies' in line: check += 1 ped_species_all = line.strip().split()[1:] for coupled_species in ped_species_all: ped_species += (tuple(coupled_species.split('_')), ) if 'PEDOutput' in line: check += 1 ped_output = line.strip().split()[1] if check == 2: # found everything you need: exit from loop break if not ped_species or not ped_output: print('Error: PEDSpecies and PEDOutput options incomplete. ' 'Exiting now') sys.exit() return ped_species, ped_output
def test__string_remove(): """ test ioformat.remove_whitespace test ioformat.remove_trail_whitespace test ioformat.remove_comment_lines """ out_str = ioformat.remove_whitespace_from_string(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')
default='me_ktp_hoten.inp', help='MESS hoten input name (me_ktp_hoten.inp)') PAR.add_argument('-ohot', '--hotoutput', default='me_ktp_hoten.log', help='MESS hoten log name (me_ktp_hoten.log)') #optional: to rename the reactions - unnecessary if they come from automech OPTS = vars(PAR.parse_args()) # it's a dictionary ############ DO NOT MODIFY ################## # SECONDO ME CONVIENE ALLA FINE METTERE TUTTO IN UNA CLASSE # OPERATIONS # 0. EXTRACT INPUT INFORMATION from me_ped.inp me_ped_inp = read_file(os.path.join(CWD, OPTS['pedinput'])) me_ped_inp = remove_comment_lines(me_ped_inp, delim_pattern=app.escape('!')) me_ped_inp = remove_comment_lines(me_ped_inp, delim_pattern=app.escape('#')) me_ped_out = read_file(os.path.join(CWD, OPTS['pedoutput'])) species_blocks_ped = mess_io.reader.get_species(me_ped_inp) T_lst, _ = mess_io.reader.rates.temperatures(me_ped_inp, mess_file='inp') P_lst, _ = mess_io.reader.rates.pressures(me_ped_inp, mess_file='inp') P_lst = P_lst[:-1] # drop the last element in the pressure list ('high') pedspecies, pedoutput = mess_io.reader.ped.ped_names(me_ped_inp) energy_dct, _, conn_lst_dct, _ = mess_io.reader.pes(me_ped_inp) # 1. INFO FROM rate_ped.out and ke_ped.out: rate dct, energy barriers, dofs, fragment names ktp_dct = {} ene_bw_dct = {} dof_dct = {} fragments_dct = {} # get the rates for all set of pedspecies
def get_species(input_string): """ Read a MESS input file string and get the block of each species Bimolecular fragments are listed together, but header Fragment is changed to Species :param input_string: string for a MESS (rates) input file :type input_string: str :return species_blocks: dictionary with the species blocks {name:[frag1 block, frag2 block], name:[unimol block],} :rtype: dict{label: list} """ input_string = remove_comment_lines(input_string, delim_pattern=app.escape('!')) lines = input_string.splitlines() lines = [line for line in lines if line.strip() != ''] # find where data of interest are bad_wellwrds = [ 'WellDepth', 'WellCutoff', 'WellExtension', 'WellReductionThreshold', 'WellPartitionMethod', 'WellProjectionThreshold' ] bad_fragwrds = ['FragmentGeometry', 'PEDSpecies'] _name_arr = np.array([('Bimolecular' in line or 'Well' in line) and all(bad not in line for bad in bad_wellwrds) for line in lines], dtype=int) names_i = np.where(_name_arr == 1)[0] _init_arr = np.array([('Fragment' in line or 'Species' in line) and all(bad not in line for bad in bad_fragwrds) for line in lines], dtype=int) init_i = np.where(_init_arr == 1)[0] init_i = init_i[init_i > names_i[0]] _end_arr = np.array(['End' in line for line in lines], dtype=int) end_i = np.where(_end_arr == 1)[0] + 1 _levels_arr = np.array(['ElectronicLevels' in line for line in lines], dtype=int) levels_i = np.where(_levels_arr == 1)[0] final_i = np.array([end_i[i < end_i][0] for i in levels_i])[:len(init_i)] # dictionary labels labels = [lines[i].strip().split()[1] for i in names_i] species_blocks = {k: [] for k in labels} # extract the data for i in np.arange(0, len(init_i)): # type sp_type = lines[init_i[i]].strip().split()[0] label = lines[names_i[init_i[i] > names_i][-1]].strip().split()[1] # name and label if sp_type == 'Fragment': name = 'Species ' + lines[init_i[i]].strip().split()[1] elif sp_type == 'Species': name = 'Species ' + label # store in the dictionary block = '\n'.join(lines[init_i[i] + 1:final_i[i]]) block = name + '\n' + block species_blocks[label].append(block) return species_blocks
def pes(input_string, read_fake=False): """ Read a MESS input file string and get info about PES :param input_string: string for a MESS (rates) input file :type input_string: str :param read_fake: value to include fake wells and barriers :type read_fake: bool :return energy_dct: dict[label: energy] :rtype: dict[label: energy] :return conn_lst_dct: defines the wells connected by each barrier :rtype: dict[barrier: (reac, prod)] all str :return conn_lst :rtype: lst(str) """ # Initialize energy and connection information energy_dct = {} conn_lst = tuple() conn_lst_dct = {} pes_label_dct = {} input_string = remove_comment_lines(input_string, delim_pattern=app.escape('!')) input_lines = input_string.splitlines() for idx, line in enumerate(input_lines): if 'Well' in line: line_lst = line.strip().split() if line_lst[0].strip() == 'Well' and '!' not in line: # Get label label = line_lst[1] if ('F' not in label) or ('F' in label and read_fake): # Get energy for line2 in input_lines[idx:]: if 'ZeroEnergy' in line2: ene = float(line2.split()[-1]) break # Add value to energy dct energy_dct[label] = ene line_lst2 = line.split('!') if len(line_lst2) == 1: line_lst2 = line.split('#') if len(line_lst2) > 1: spc = line_lst2[1] pes_label_dct[spc.strip()] = label else: pes_label_dct[label] = label print('Warning: labeling not found for ' f'species {label}') if 'Bimolecular' in line: line_lst = line.strip().split() if line_lst[0].strip() == 'Bimolecular' and '!' not in line: # Get label label = line_lst[1] # Get energy for line2 in input_lines[idx:]: if 'Dummy' in line2: ene = -10.0 break if 'GroundEnergy' in line2: ene = float(line2.split()[-1]) break # Add value to dct energy_dct[label] = ene # Add value to PES dct - NB THIS DEPENDS ON THE INPUT FILE. # IF NOT PRESENT, DO NOT GENERATE THE PES LABEL DICTIONARY cnt = 0 frags = [] for line2 in input_lines[idx:]: if 'Fragment' in line2: # Try and grab name from comment line frag_line_lst = line2.split('!') if len(frag_line_lst) == 1: frag_line_lst = line2.split('#') if len(frag_line_lst) > 1: frag = frag_line_lst[1] # strip gets rid of the spaces before and after frags.append(frag.strip()) else: frag_line_lst = line2.split() frag = frag_line_lst[1] frags.append(frag.strip()) print('Warning: labeling not found for ' f'bimol fragments for {label}') cnt += 1 if cnt == 2: break pes_label_dct[' + '.join(frags)] = label if 'Barrier' in line: line_lst = line.strip().split() if line_lst[0].strip() == 'Barrier' and '!' not in line: # Get label [tslabel, rlabel, plabel] = line_lst[1:4] if ('F' not in tslabel) or ('F' in tslabel and read_fake): # Get energy for line2 in input_lines[idx:]: if 'ZeroEnergy' in line2: ene = float(line2.split()[-1]) break # Add value to dct energy_dct[tslabel] = ene # Amend fake labels (may be wrong) if not read_fake: rlabel = rlabel.replace('F', 'P') plabel = plabel.replace('F', 'P') # Add the connection to lst conn_lst += ((rlabel, tslabel), ) conn_lst += ((tslabel, plabel), ) conn_lst_dct[tslabel] = (rlabel, plabel) return energy_dct, conn_lst, conn_lst_dct, pes_label_dct
""" import os import numpy from ioformat import pathtools from ioformat import remove_comment_lines import autoparse.pattern as app import mess_io PATH = os.path.dirname(os.path.realpath(__file__)) INP_PATH = os.path.join(PATH, 'data', 'inp') INP_STR = pathtools.read_file(INP_PATH, 'mess.inp') PROMPT_INP_PATH = os.path.join(PATH, 'data', 'inp') PED_INP_STR = pathtools.read_file(PROMPT_INP_PATH, 'me_ktp_ped.inp') PED_INP_STR = remove_comment_lines(PED_INP_STR, delim_pattern=app.escape('!')) PED_INP_STR = remove_comment_lines(PED_INP_STR, delim_pattern=app.escape('#')) def test_pes(): """ test mess_io.reader.pes """ # Test reading with removing any fake wells energy_dct1, conn_lst1, _, _ = mess_io.reader.pes(input_string=INP_STR, read_fake=False) ref_energy_dct1 = {'P1': 0.0, 'P2': 3.22, 'B1': 13.23} ref_conn_lst1 = (('P1', 'B1'), ('B1', 'P2')) ref_keys = tuple(ref_energy_dct1.keys())