def main(inputPath, outputPath):

    # Replicate rmgpy.chemkin.loadSpeciesDictionary here with some modifcations
    # We don't need to compare against inerts, and we want to maintain the same species order
    speciesList = []
    with open(inputPath, 'r+b') as f:
        adjlist = ''
        for line in f:
            if line.strip() == '' and adjlist.strip() != '':
                # Finish this adjacency list
                species = Species().fromAdjacencyList(adjlist)

                # Try to generate the aromatic form of the molecule
                aromatic = generateAromaticResonanceIsomers(
                    species.molecule[0])
                if aromatic:
                    species.molecule = aromatic

                speciesList.append(species)
                adjlist = ''
            else:
                if "InChI" in line:
                    line = line.split()[0] + '\n'
                if '//' in line:
                    index = line.index('//')
                    line = line[0:index]
                adjlist += line

    # Write to dictionary file
    saveSpeciesDictionary(outputPath, speciesList)
def main(inputPath, outputPath):

    species = []

    with open(inputPath, 'r+b') as f:
        for line0 in f:
            line1 = line0.strip()
            if line1 and line1[0] != '!':
                # Parse line using regex
                match = re.match(r"([^\s!]+)\s+([^\s!]+)", line1.strip())

                # Get label and SMILES string
                label = match.group(1)
                smiles = match.group(2)

                # Try to convert to aromatic form
                mol = Molecule().fromSMILES(smiles)
                aromatic = generateAromaticResonanceStructures(mol)

                if aromatic:
                    spec = Species(molecule=aromatic)
                else:
                    spec = Species(molecule=[mol])

                # Save in species dictionary
                spec.label = label
                species.append(spec)

    # Write to dictionary file
    saveSpeciesDictionary(outputPath, species)
Beispiel #3
0
def execute(inputModelFiles, **kwargs):
    
    try:
        wd = kwargs['wd']
    except KeyError:
        wd = os.getcwd()

    transport = kwargs['transport']
    
    outputChemkinFile = os.path.join(wd, 'chem.inp')
    outputSpeciesDictionary = os.path.join(wd, 'species_dictionary.txt')
    outputTransportFile = os.path.join(wd, 'tran.dat') if transport else None

    models = get_models_to_merge(inputModelFiles)

    finalModel = combine_models(models)

    # Save the merged model to disk
    saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions)
    saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species)
    if transport:
        saveTransportFile(outputTransportFile, finalModel.species)

    print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile)
    print 'Merged species dictionary saved to {0}'.format(outputSpeciesDictionary)
    if transport:
        print 'Merged transport file saved to {0}'.format(outputTransportFile)
Beispiel #4
0
def main(inputPath, outputPath):

    species = []

    with open(inputPath, 'r+b') as f:
        xyz = ''
        label = ''
        lineNum = 0
        specNum = 0
        for line0 in f:
            line1 = line0.strip()
            if line1:
                # The line is not blank, so add it to the xyz string
                xyz += line1 + '\n'
                lineNum += 1
                # In openbabel xyz format, the second line is a comment/name
                if lineNum == 2:
                    label = line1
            else:
                # Check if we've found a valid entry
                if not xyz:
                    continue
                lines = xyz.strip().split('\n')
                # The first line is the number of atoms, which should match the number of lines minus the first two
                if int(lines[0]) != len(lines) - 2:
                    print 'Invalid entry found'
                    xyz = ''
                    lineNum = 0
                    continue

                # Assume that we finished reading this entry
                specNum += 1

                # Make sure the label is appropriate
                label = label.split()[0]
                label = label.strip()
                if not label:
                    label = 'SPC({0})'.format(specNum)

                # Read xyz string using openbabel
                obmol = pybel.readstring('xyz', xyz)

                # Convert to RMG molecule object
                mol = fromOBMol(Molecule(), obmol.OBMol)

                # Save in species dictionary
                spec = Species(molecule=[mol])
                spec.label = label
                species.append(spec)

                # Reset our counter
                xyz = ''
                lineNum = 0

    # Write to dictionary file
    saveSpeciesDictionary(outputPath, species)
Beispiel #5
0
def execute(inputModelFiles, **kwargs):

    try:
        wd = kwargs['wd']
    except KeyError:
        wd = os.getcwd()

    transport = kwargs['transport']

    outputChemkinFile = os.path.join(wd, 'chem.inp')
    outputSpeciesDictionary = os.path.join(wd, 'species_dictionary.txt')
    outputTransportFile = os.path.join(wd, 'tran.dat') if transport else None

    # Load the models to merge
    models = []
    for chemkin, speciesPath, transportPath in inputModelFiles:
        print 'Loading model #{0:d}...'.format(len(models) + 1)
        model = ReactionModel()
        model.species, model.reactions = loadChemkinFile(
            chemkin, speciesPath, transportPath=transportPath)
        models.append(model)

    finalModel = ReactionModel()
    for i, model in enumerate(models):
        print 'Ignoring common species and reactions from model #{0:d}...'.format(
            i + 1)
        Nspec0 = len(finalModel.species)
        Nrxn0 = len(finalModel.reactions)
        finalModel = finalModel.merge(model)
        Nspec = len(finalModel.species)
        Nrxn = len(finalModel.reactions)
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(
            i + 1, Nspec - Nspec0, len(model.species),
            (Nspec - Nspec0) * 100. / len(model.species))
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(
            i + 1, Nrxn - Nrxn0, len(model.reactions),
            (Nrxn - Nrxn0) * 100. / len(model.reactions))

    print 'The merged model has {0:d} species and {1:d} reactions'.format(
        len(finalModel.species), len(finalModel.reactions))

    # Save the merged model to disk
    saveChemkinFile(outputChemkinFile, finalModel.species,
                    finalModel.reactions)
    saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species)
    if transport:
        saveTransportFile(outputTransportFile, finalModel.species)

    print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile)
    print 'Merged species dictionary saved to {0}'.format(
        outputSpeciesDictionary)
    if transport:
        print 'Merged transport file saved to {0}'.format(outputTransportFile)
Beispiel #6
0
def execute(inputModelFiles, **kwargs):
    
    try:
        wd = kwargs['wd']
    except KeyError:
        wd = os.getcwd()

    transport = kwargs['transport']
    
    outputChemkinFile = os.path.join(wd, 'chem.inp')
    outputSpeciesDictionary = os.path.join(wd, 'species_dictionary.txt')
    outputTransportFile = os.path.join(wd, 'tran.dat') if transport else None
    
    # Load the models to merge
    models = []
    for chemkin, speciesPath, transportPath in inputModelFiles:
        print 'Loading model #{0:d}...'.format(len(models)+1)
        model = ReactionModel()
        model.species, model.reactions = loadChemkinFile(chemkin, speciesPath, transportPath=transportPath)
        models.append(model)

    finalModel = ReactionModel()
    for i, model in enumerate(models):        
        print 'Ignoring common species and reactions from model #{0:d}...'.format(i+1)
        Nspec0 = len(finalModel.species)
        Nrxn0 = len(finalModel.reactions)
        finalModel = finalModel.merge(model)
        Nspec = len(finalModel.species)
        Nrxn = len(finalModel.reactions)
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(i+1, Nspec - Nspec0, len(model.species), (Nspec - Nspec0) * 100. / len(model.species))
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(i+1, Nrxn - Nrxn0, len(model.reactions), (Nrxn - Nrxn0) * 100. / len(model.reactions))
    
    print 'The merged model has {0:d} species and {1:d} reactions'.format(len(finalModel.species), len(finalModel.reactions))
        
    # Save the merged model to disk
    saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions)
    saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species)
    if transport:
        saveTransportFile(outputTransportFile, finalModel.species)
        
    print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile)
    print 'Merged species dictionary saved to {0}'.format(outputSpeciesDictionary)
    if transport:
        print 'Merged transport file saved to {0}'.format(outputTransportFile)
Beispiel #7
0
def runThermoEstimator(inputFile, library_flag):
    """
    Estimate thermo for a list of species using RMG and the settings chosen inside a thermo input file.
    """

    rmg = RMG()
    rmg.loadThermoInput(inputFile)

    rmg.database = RMGDatabase()
    path = os.path.join(settings['database.directory'])

    # forbidden structure loading
    rmg.database.loadThermo(os.path.join(path, 'thermo'),
                            rmg.thermoLibraries,
                            depository=False)

    if rmg.solvent:
        rmg.database.loadSolvation(os.path.join(path, 'solvation'))
        Species.solventData = rmg.database.solvation.getSolventData(
            rmg.solvent)
        Species.solventName = rmg.solvent

    for species in rmg.initialSpecies:
        submit(species)

    if library_flag:
        library = ThermoLibrary(name='Thermo Estimation Library')
        for species in rmg.initialSpecies:
            library.loadEntry(
                index=len(library.entries) + 1,
                label=species.label,
                molecule=species.molecule[0].toAdjacencyList(),
                thermo=species.getThermoData().toThermoData(),
                shortDesc=species.getThermoData().comment,
            )
        library.save(os.path.join(rmg.outputDirectory, 'ThermoLibrary.py'))

    # Save the thermo data to chemkin format output files and dictionary, with no reactions
    saveChemkinFile(os.path.join(rmg.outputDirectory, 'chem_annotated.inp'),
                    species=rmg.initialSpecies,
                    reactions=[])
    saveSpeciesDictionary(os.path.join(rmg.outputDirectory,
                                       'species_dictionary.txt'),
                          species=rmg.initialSpecies)
def main(inputPath, outputPath):

    species = []

    with open(inputPath, 'r+b') as f:
        for line0 in f:
            line1 = line0.strip()
            if line1 and line1[0] != '!':
                # Parse line using regex
                match = re.match(r"([^\s!]+)\s+([^\s!]+)", line1.strip())

                # Get label and SMILES string
                label = match.group(1)
                smiles = match.group(2)

                # Save in species dictionary
                spec = Species().fromSMILES(smiles)
                spec.label = label
                species.append(spec)

    # Write to dictionary file
    saveSpeciesDictionary(outputPath, species)
Beispiel #9
0
def runThermoEstimator(inputFile):
    """
    Estimate thermo for a list of species using RMG and the settings chosen inside a thermo input file.
    """
    
    rmg = RMG()
    rmg.loadThermoInput(inputFile)
    
    rmg.database = RMGDatabase()
    path = os.path.join(settings['database.directory'])

    # forbidden structure loading
    rmg.database.loadThermo(os.path.join(path, 'thermo'), rmg.thermoLibraries, depository=False)
   
    if rmg.solvent:
        rmg.database.loadSolvation(os.path.join(path, 'solvation'))
        Species.solventData = rmg.database.solvation.getSolventData(rmg.solvent)
        Species.solventName = rmg.solvent

    for species in rmg.initialSpecies:
        submit(species)

    # library = ThermoLibrary(name='Thermo Estimation Library')
    # for spc in rmg.initialSpecies:
    #     library.loadEntry(
    #         index = len(library.entries) + 1,
    #         label = species.label,
    #         molecule = species.molecule[0].toAdjacencyList(),
    #         thermo = species.getThermoData().toThermoData(),
    #         shortDesc = species.getThermoData().comment,
    #     )
    # library.save(os.path.join(rmg.outputDirectory,'ThermoLibrary.py'))
    

    # Save the thermo data to chemkin format output files and dictionary, with no reactions    
    saveChemkinFile(os.path.join(rmg.outputDirectory, 'chem_annotated.inp'), species=rmg.initialSpecies, reactions=[])
    saveSpeciesDictionary(os.path.join(rmg.outputDirectory, 'species_dictionary.txt'), species=rmg.initialSpecies)
Beispiel #10
0
    for chemkin, speciesPath, transportPath in inputModelFiles:
        print 'Loading model #{0:d}...'.format(len(models)+1)
        model = ReactionModel()
        model.species, model.reactions = loadChemkinFile(chemkin, speciesPath, transportPath=transportPath)
        models.append(model)

    allSpecies = []
    speciesIndices = [[] for i in range(len(models))]
    for i, model in enumerate(models):       
        speciesIndices[i] = []
        for j, species in enumerate(model.species):
            for index, species0 in enumerate(allSpecies):
                if species0.isIsomorphic(species):
                    speciesIndices[i].append(index)
                    break; 
            else:
                allSpecies.append(species)
                speciesIndices[i].append(allSpecies.index(species))
    # Reassign species names and labels according to the list of all species in all models
    # We must retain the original thermochemistry
    for i, model in enumerate(models):       
        for j, species in enumerate(model.species):
            index = speciesIndices[i][j]
            species.label = allSpecies[index].label
            species.index = allSpecies[index].index
            
        # Resave the models    
        saveChemkinFile('chem{0}.inp'.format(i+1), model.species, model.reactions)
        saveSpeciesDictionary('species_dictionary{0}.txt'.format(i+1), model.species)
        
    print 'Saving of new models with consistent names is complete!'
Beispiel #11
0
    
    # Load the models to merge
    models = []
    for chemkin, speciesDict in zip(args.files[0::2], args.files[1::2]):
        print 'Loading model #{0:d}...'.format(len(models)+1)
        model = ReactionModel()
        model.species, model.reactions = loadChemkinFile(chemkin, speciesDict)
        models.append(model)
    


    finalModel = ReactionModel()
    for i, model in enumerate(models):        
        print 'Ignoring common species and reactions from model #{0:d}...'.format(i+1)
        Nspec0 = len(finalModel.species)
        Nrxn0 = len(finalModel.reactions)
        finalModel = finalModel.merge(model)
        Nspec = len(finalModel.species)
        Nrxn = len(finalModel.reactions)
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(i+1, Nspec - Nspec0, len(model.species), (Nspec - Nspec0) * 100. / len(model.species))
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(i+1, Nrxn - Nrxn0, len(model.reactions), (Nrxn - Nrxn0) * 100. / len(model.reactions))
    
    print 'The merged model has {0:d} species and {1:d} reactions'.format(len(finalModel.species), len(finalModel.reactions))
        
    # Save the merged model to disk
    saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions)
    saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species)

    print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile)
    print 'Merged species dictionary saved to {0}'.format(outputSpeciesDictionary)
    for chemkin, speciesPath, transportPath in inputModelFiles:
        print 'Loading model #{0:d}...'.format(len(models)+1)
        model = ReactionModel()
        model.species, model.reactions = loadChemkinFile(chemkin, speciesPath, transportPath=transportPath)
        models.append(model)

    allSpecies = []
    speciesIndices = [[] for i in range(len(models))]
    for i, model in enumerate(models):       
        speciesIndices[i] = []
        for j, species in enumerate(model.species):
            for index, species0 in enumerate(allSpecies):
                if species0.isIsomorphic(species):
                    speciesIndices[i].append(index)
                    break; 
            else:
                allSpecies.append(species)
                speciesIndices[i].append(allSpecies.index(species))
    # Reassign species names and labels according to the list of all species in all models
    # We must retain the original thermochemistry
    for i, model in enumerate(models):       
        for j, species in enumerate(model.species):
            index = speciesIndices[i][j]
            species.label = allSpecies[index].label
            species.index = allSpecies[index].index
            
        # Resave the models    
        saveChemkinFile('chem{0}.inp'.format(i+1), model.species, model.reactions)
        saveSpeciesDictionary('species_dictionary{0}.txt'.format(i+1), model.species)
        
    print 'Saving of new models with consistent names is complete!'
        reaction.kinetics = entry.data
        reactionList.append(reaction)

    speciesList = []
    index = 0
    for spec in kineticLibrary.getSpecies().values():
        index = index + 1
        species = Species(molecule = spec.molecule)
        species.generateThermoData(database)
        species.index = index
        speciesList.append(species)

    for reaction in reactionList:
        for reactant in reaction.reactants:
            for spec in speciesList:
                if reactant.isIsomorphic(spec):
                    reactant.index = spec.index
                    spec.label = reactant.label
        for product in reaction.products:
            for spec in speciesList:
                if product.isIsomorphic(spec):
                    product.index = spec.index
                    spec.label = product.label
            

    print 'Saving the chem.inp and species_dictionary.txt to local directory'
    saveChemkinFile('chem.inp', speciesList, reactionList)
    saveSpeciesDictionary('species_dictionary.txt', speciesList)
    

Beispiel #14
0
        reaction.kinetics = entry.data
        reactionList.append(reaction)

    speciesList = []
    index = 0
    speciesDict = kineticLibrary.getSpecies(
        os.path.join(settings['database.directory'], 'kinetics', 'libraries',
                     libraryName, 'dictionary.txt'))
    for spec in speciesDict.values():
        index = index + 1
        species = Species(molecule=spec.molecule)
        species.generateThermoData(database)
        species.index = index
        speciesList.append(species)

    for reaction in reactionList:
        for reactant in reaction.reactants:
            for spec in speciesList:
                if reactant.isIsomorphic(spec):
                    reactant.index = spec.index
                    spec.label = reactant.label
        for product in reaction.products:
            for spec in speciesList:
                if product.isIsomorphic(spec):
                    product.index = spec.index
                    spec.label = product.label

    print 'Saving the chem.inp and species_dictionary.txt to local directory'
    saveChemkinFile('chem.inp', speciesList, reactionList)
    saveSpeciesDictionary('species_dictionary.txt', speciesList)
Beispiel #15
0
    for i, model in enumerate(models):
        print 'Ignoring common species and reactions from model #{0:d}...'.format(
            i + 1)
        Nspec0 = len(finalModel.species)
        Nrxn0 = len(finalModel.reactions)
        finalModel = finalModel.merge(model)
        Nspec = len(finalModel.species)
        Nrxn = len(finalModel.reactions)
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(
            i + 1, Nspec - Nspec0, len(model.species),
            (Nspec - Nspec0) * 100. / len(model.species))
        print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(
            i + 1, Nrxn - Nrxn0, len(model.reactions),
            (Nrxn - Nrxn0) * 100. / len(model.reactions))

    print 'The merged model has {0:d} species and {1:d} reactions'.format(
        len(finalModel.species), len(finalModel.reactions))

    # Save the merged model to disk
    saveChemkinFile(outputChemkinFile, finalModel.species,
                    finalModel.reactions)
    saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species)
    if transport:
        saveTransportFile(outputTransportFile, finalModel.species)

    print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile)
    print 'Merged species dictionary saved to {0}'.format(
        outputSpeciesDictionary)
    if transport:
        print 'Merged transport file saved to {0}'.format(outputTransportFile)
        )
        reactionList.append(library_reaction)

    speciesList = []
    index = 0
    speciesDict = kineticLibrary.getSpecies(
        os.path.join(settings["database.directory"], "kinetics", "libraries", libraryName, "dictionary.txt")
    )
    for spec in speciesDict.values():
        index = index + 1
        species = Species(molecule=spec.molecule)
        species.getThermoData()
        species.index = index
        speciesList.append(species)

    for reaction in reactionList:
        for reactant in reaction.reactants:
            for spec in speciesList:
                if reactant.isIsomorphic(spec):
                    reactant.index = spec.index
                    spec.label = reactant.label
        for product in reaction.products:
            for spec in speciesList:
                if product.isIsomorphic(spec):
                    product.index = spec.index
                    spec.label = product.label

    print "Saving the chem.inp and species_dictionary.txt to local directory"
    saveChemkinFile("chem.inp", speciesList, reactionList)
    saveSpeciesDictionary("species_dictionary.txt", speciesList)