def load_old(self, path): """ Load the old RMG kinetics database from the given `path` on disk, where `path` points to the top-level folder of the old RMG database. """ self.families = {} self.libraries = {} libraries_path = os.path.join(path, 'kinetics_libraries') for (root, dirs, files) in os.walk(os.path.join(path, 'kinetics_libraries')): if os.path.exists(os.path.join(root, 'species.txt')) and \ os.path.exists(os.path.join(root, 'reactions.txt')): library = KineticsLibrary(label=root[len(libraries_path) + 1:], name=root[len(libraries_path) + 1:]) logging.warning("Loading {0}".format(root)) library.load_old(root) self.libraries[library.label] = library for (root, dirs, files) in os.walk(os.path.join(path, 'kinetics_groups')): if os.path.exists(os.path.join(root, 'dictionary.txt')) and \ os.path.exists(os.path.join(root, 'rateLibrary.txt')): label = os.path.split(root)[1] family = KineticsFamily(label=label) logging.warning("Loading {0}".format(root)) family.load_old(root) self.families[family.label] = family return self
def import_kinetics(kinetics_path, kinetic_model, models): local_context = { "KineticsData": kinetics.KineticsData, "Arrhenius": kinetics.Arrhenius, "ArrheniusEP": kinetics.ArrheniusEP, "MultiArrhenius": kinetics.MultiArrhenius, "MultiPDepArrhenius": kinetics.MultiPDepArrhenius, "PDepArrhenius": kinetics.PDepArrhenius, "Chebyshev": kinetics.Chebyshev, "ThirdBody": kinetics.ThirdBody, "Lindemann": kinetics.Lindemann, "Troe": kinetics.Troe, "R": constants.R, } library = KineticsLibrary(label=kinetic_model.model_name) library.SKIP_DUPLICATES = True library.load(kinetics_path, local_context=local_context) for entry in library.entries.values(): try: with transaction.atomic(): rmg_kinetics_data = entry.data comment = entry.short_desc rmg_reaction = entry.item reaction = get_or_create_reaction(kinetic_model, rmg_reaction, models) kinetics_data = create_kinetics_data(kinetic_model, rmg_kinetics_data, models) base_fields = get_base_kinetics_data_fields(rmg_kinetics_data) kinetics_instance, created = models.Kinetics.objects.get_or_create( reaction=reaction, raw_data=kinetics_data, defaults=base_fields) if created and kinetics_data.get("type") not in [ "arrhenius", "arrhenius_ep", "multi_arrhenius", ]: create_and_save_efficiencies(kinetic_model, kinetics_instance, rmg_kinetics_data, models) models.KineticsComment.objects.get_or_create( kinetics=kinetics_instance, kinetic_model=kinetic_model, defaults={"comment": comment}, ) except Exception: logger.exception(f"Failed to import reaction {entry.label}")
def save_kinetics_lib(rxn_list, path, name, lib_long_desc): """ Save an RMG kinetics library. Args: rxn_list (list): Entries are Reaction object instances for which kinetics will be saved. path (str): The base folder in which the kinetic library will be saved. name (str): The library name. lib_long_desc (str): A multiline string with relevant description. """ entries = dict() if rxn_list: for i, rxn in enumerate(rxn_list): if rxn.kinetics is not None: entry = Entry(index=i, item=rxn, data=rxn.kinetics, label=rxn.label) entries[i + 1] = entry else: logging.warning( f'Reaction {rxn.label} did not contain any kinetic data and was omitted from the ' f'kinetics library.') kinetics_library = KineticsLibrary(name=name, long_desc=lib_long_desc, auto_generated=True) kinetics_library.entries = entries if os.path.exists(path): shutil.rmtree(path) try: os.makedirs(path) except OSError: pass kinetics_library.save(os.path.join(path, 'reactions.py')) kinetics_library.save_dictionary(os.path.join(path, 'dictionary.txt'))
def load_libraries(self, path, libraries=None): """ Load the listed kinetics libraries from the given `path` on disk. Loads them all if `libraries` list is not specified or `None`. The `path` points to the folder of kinetics libraries in the database, and the libraries should be in files like :file:`<path>/<library>.py`. """ if libraries is not None: for library_name in libraries: library_file = os.path.join(path, library_name, 'reactions.py') if os.path.exists(library_file): logging.info( 'Loading kinetics library {0} from {1}...'.format( library_name, library_file)) library = KineticsLibrary(label=library_name) library.load(library_file, self.local_context, self.global_context) self.libraries[library.label] = library else: if library_name == "KlippensteinH2O2": logging.info( """\n** Note: The KlippensteinH2O2 library was replaced and is no longer available in RMG. For H2 combustion chemistry consider using either the BurkeH2inN2 or BurkeH2inArHe library instead, depending on the main bath gas (N2 or Ar/He, respectively)\n""" ) raise IOError("Couldn't find kinetics library {0}".format( library_file)) else: # load all the libraries you can find # this cannot be activated in a normal RMG job. Only activated when loading the database for other purposes self.library_order = [] for (root, dirs, files) in os.walk(os.path.join(path)): for f in files: name, ext = os.path.splitext(f) if ext.lower() == '.py': library_file = os.path.join(root, f) label = os.path.dirname(library_file)[len(path) + 1:] logging.info( 'Loading kinetics library {0} from {1}...'.format( label, library_file)) library = KineticsLibrary(label=label) try: library.load(library_file, self.local_context, self.global_context) except: logging.error( "Problem loading reaction library {0!r}". format(library_file)) raise self.libraries[library.label] = library self.library_order.append( (library.label, 'Reaction Library'))
def save_kinetics_lib(rxn_list, path, name, lib_long_desc): """ Save an RMG kinetics library of all reactions in `rxn_list` in the supplied `path` `rxn_list` is a list of ARCReaction objects `name` is the library's name (or project's name) `long_desc` is a multiline string with level of theory description """ entries = dict() if rxn_list: for i, rxn in enumerate(rxn_list): if rxn.kinetics is not None: if len(rxn.rmg_reaction.reactants): reactants = rxn.rmg_reaction.reactants products = rxn.rmg_reaction.products elif rxn.r_species.mol_list is not None: reactants = [Species(molecule=arc_spc.mol_list) for arc_spc in rxn.r_species] products = [Species(molecule=arc_spc.mol_list) for arc_spc in rxn.p_species] elif rxn.r_species.mol is not None: reactants = [Species(molecule=[arc_spc.mol]) for arc_spc in rxn.r_species] products = [Species(molecule=[arc_spc.mol]) for arc_spc in rxn.p_species] else: reactants = [Species(molecule=[arc_spc.xyz_mol]) for arc_spc in rxn.r_species] products = [Species(molecule=[arc_spc.xyz_mol]) for arc_spc in rxn.p_species] rxn.rmg_reaction.reactants = reactants rxn.rmg_reaction.products = products entry = Entry( index=i, item=rxn.rmg_reaction, data=rxn.kinetics, label=rxn.label) rxn.ts_species.make_ts_report() entry.longDesc = rxn.ts_species.ts_report + '\n\nOptimized TS geometry:\n' + rxn.ts_species.final_xyz rxn.rmg_reaction.kinetics = rxn.kinetics rxn.rmg_reaction.kinetics.comment = str('') entries[i+1] = entry else: logging.warning('Reaction {0} did not contain any kinetic data and was omitted from the kinetics' ' library.'.format(rxn.label)) kinetics_library = KineticsLibrary(name=name, longDesc=lib_long_desc, autoGenerated=True) kinetics_library.entries = entries lib_path = os.path.join(path, 'kinetics', '') if os.path.exists(lib_path): shutil.rmtree(lib_path) try: os.makedirs(lib_path) except OSError: pass kinetics_library.save(os.path.join(lib_path, 'reactions.py')) kinetics_library.saveDictionary(os.path.join(lib_path, 'dictionary.txt'))
def get_libraries(self): """Get RMG kinetics and thermo libraries""" name = 'kineticsjobs' species_list = list(self.species_dict.values()) reaction_list = list(self.reaction_dict.values()) # remove duplicate species for rxn in reaction_list: for i, rspc in enumerate(rxn.reactants): for spc in species_list: if spc.is_isomorphic(rspc): rxn.reactants[i] = spc break for i, rspc in enumerate(rxn.products): for spc in species_list: if spc.is_isomorphic(rspc): rxn.products[i] = spc break del_inds = [] for i, spc1 in enumerate(species_list): for j, spc2 in enumerate(species_list): if j > i and spc1.is_isomorphic(spc2): del_inds.append(j) for j in sorted(del_inds)[::-1]: del species_list[j] thermo_library = ThermoLibrary(name=name) for i, species in enumerate(species_list): if species.thermo: thermo_library.load_entry( index=i + 1, label=species.label, molecule=species.molecule[0].to_adjacency_list(), thermo=species.thermo, shortDesc=species.thermo.comment) else: logging.warning( 'Species {0} did not contain any thermo data and was omitted from the thermo library.' .format(str(species))) # load kinetics library entries kinetics_library = KineticsLibrary(name=name, auto_generated=True) kinetics_library.entries = {} for i, reaction in enumerate(reaction_list): entry = Entry(index=i + 1, label=reaction.to_labeled_str(), item=reaction, data=reaction.kinetics) if reaction.kinetics is not None: if hasattr(reaction, 'library') and reaction.library: entry.long_desc = 'Originally from reaction library: ' + \ reaction.library + "\n" + reaction.kinetics.comment else: entry.long_desc = reaction.kinetics.comment kinetics_library.entries[i + 1] = entry kinetics_library.label = name return thermo_library, kinetics_library, species_list
def getLibraries(self): name = 'kineticsjobs' speciesList = self.speciesDict.values() reactionList = self.reactionDict.values() # remove duplicate species for rxn in reactionList: for i,rspc in enumerate(rxn.reactants): for spc in speciesList: if spc.isIsomorphic(rspc): rxn.reactants[i] = spc break for i,rspc in enumerate(rxn.products): for spc in speciesList: if spc.isIsomorphic(rspc): rxn.products[i] = spc break del_inds = [] for i,spc1 in enumerate(speciesList): for j,spc2 in enumerate(speciesList): if j>i and spc1.isIsomorphic(spc2): del_inds.append(j) for j in sorted(del_inds)[::-1]: del speciesList[j] thermoLibrary = ThermoLibrary(name=name) for i,species in enumerate(speciesList): if species.thermo: thermoLibrary.loadEntry(index = i + 1, label = species.label, molecule = species.molecule[0].toAdjacencyList(), thermo = species.thermo, shortDesc = species.thermo.comment ) else: logging.warning('Species {0} did not contain any thermo data and was omitted from the thermo library.'.format(str(species))) # load kinetics library entries kineticsLibrary = KineticsLibrary(name=name,autoGenerated=True) kineticsLibrary.entries = {} for i,reaction in enumerate(reactionList): entry = Entry( index = i+1, label = reaction.toLabeledStr(), item = reaction, data = reaction.kinetics, ) if reaction.kinetics is not None: if hasattr(reaction,'library') and reaction.library: entry.longDesc = 'Originally from reaction library: ' +\ reaction.library + "\n" + reaction.kinetics.comment else: entry.longDesc = reaction.kinetics.comment kineticsLibrary.entries[i+1] = entry kineticsLibrary.label = name return thermoLibrary,kineticsLibrary,speciesList