Example #1
0
def chemkin_to_kinetic_lib(chem_path,
                           dict_path,
                           name,
                           save_path='',
                           use_chemkin_names=True):
    """
    Convert a CHEMKIN file into a RMG kinetic library given species dictionary
    and the library name.

    Args:
        chem_path (str): The path to the CHEMKIN file
        dict_path (str): The path to a species dictionary
        name (str): The name of the new library
        save_path (str): The path to the saving directory. By default, the library
                         will be saved to RMG-database repository
        use_chemkin_names (bool): Use the original CHEMKIN species name
    """
    # Load the reactions from the CHEMKIN FILE
    logging.info('Loading CHEMKIN file %s with species dictionary %s' %
                 (chem_path, dict_path))
    _, rxns = load_chemkin_file(chem_path,
                                dict_path,
                                use_chemkin_names=use_chemkin_names)
    kinetic_lib = KineticsLibrary(name=name)
    kinetic_lib.entries = {}
    # Create new entries
    for i in range(len(rxns)):
        rxn = rxns[i]
        entry = Entry(
            index=i + 1,
            label=str(rxn),
            item=rxn,
            data=rxn.kinetics,
        )
        try:
            entry.long_desc = 'Originally from reaction library: ' + \
                rxn.library + "\n" + rxn.kinetics.comment
        except AttributeError:
            entry.long_desc = rxn.kinetics.comment
        kinetic_lib.entries[i + 1] = entry
        logging.info('Adding reaction %s in to the kinetic library %s' %
                     (entry.label, name))
    # Check for duplicates and convert them to multiArrhenius / multiPdepArrehenius
    kinetic_lib.check_for_duplicates(mark_duplicates=True)
    kinetic_lib.convert_duplicates_to_multi()
    # Save the library
    if not save_path:
        save_path = os.path.join(settings['database.directory'], 'kinetics',
                                 'libraries')
    try:
        os.makedirs(os.path.join(save_path, name))
    except:
        pass
    logging.info('Saving the kinetic library to %s' %
                 (os.path.join(save_path, name)))
    kinetic_lib.save(os.path.join(save_path, name, 'reactions.py'))
    kinetic_lib.save_dictionary(os.path.join(save_path, name,
                                             'dictionary.txt'))
Example #2
0
    def load_old(self, path):
        """
        Load an old-style RMG kinetics library from the location `path`.
        """
        path = os.path.abspath(path)

        self.load_old_dictionary(os.path.join(path, 'species.txt'),
                                 pattern=False)
        species = dict([(label, Species(label=label, molecule=[entry.item]))
                        for label, entry in self.entries.items()])

        # Add common bath gases (Ar, Ne, He, N2) if not already present
        for label, smiles in [('Ar', '[Ar]'), ('He', '[He]'), ('Ne', '[Ne]'),
                              ('N2', 'N#N')]:
            if label not in species:
                molecule = Molecule().from_smiles(smiles)
                spec = Species(label=label, molecule=[molecule])
                species[label] = spec

        reactions = []
        reactions.extend(
            self._load_old_reactions(os.path.join(path, 'reactions.txt'),
                                     species))
        if os.path.exists(os.path.join(path, 'pdepreactions.txt')):
            pdep_reactions = self._load_old_reactions(
                os.path.join(path, 'pdepreactions.txt'), species)
            # RMG-Py likes otherwise equivalent PDep and non-pdep reactions to be marked as duplicates
            self.mark_valid_duplicates(reactions, pdep_reactions)
            reactions.extend(pdep_reactions)

        self.entries = {}
        for index, reaction in enumerate(reactions):
            entry = Entry(index=index + 1,
                          item=reaction,
                          data=reaction.kinetics,
                          label=str(reaction))
            entry.long_desc = reaction.kinetics.comment
            reaction.kinetics.comment = ''
            self.entries[index + 1] = entry
            reaction.kinetics = None

        self.check_for_duplicates()
        self.convert_duplicates_to_multi()
Example #3
0
    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
                """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)
    kinetics_library.entries = {}
    for i in range(len(reaction_list)):
        reaction = reaction_list[i]
        entry = Entry(
            index=i + 1,
            label=str(reaction),
            item=reaction,
            data=reaction.kinetics,
        )
        try:
            entry.long_desc = 'Originally from reaction library: ' + reaction.library + "\n" + reaction.kinetics.comment
        except AttributeError:
            entry.long_desc = reaction.kinetics.comment
        kinetics_library.entries[i + 1] = entry

    # Mark as duplicates where there are mixed pressure dependent and non-pressure dependent duplicate kinetics
    # Even though CHEMKIN does not require a duplicate flag, RMG needs it.
    # Using flag mark_duplicates = True
    kinetics_library.check_for_duplicates(mark_duplicates=True)
    kinetics_library.convert_duplicates_to_multi()

    # Save in Py format
    database_directory = settings['database.directory']
    try:
        os.makedirs(
            os.path.join(database_directory, 'kinetics', 'libraries', name))