From 7f5fa5cf1679fff0221e828b11fbe016da398906 Mon Sep 17 00:00:00 2001 From: Agnes Date: Tue, 15 Oct 2019 12:50:13 -0400 Subject: [PATCH] Update jupyter notebooks. --- ...ycyclicThermoGroupsFromThermoLibrary.ipynb | 372 +++++++++--------- scripts/generateFilterArrheniusFits.ipynb | 60 ++- scripts/showNewTrainingRxnsImprovements.ipynb | 147 +++---- 3 files changed, 291 insertions(+), 288 deletions(-) diff --git a/scripts/fitPolycyclicThermoGroupsFromThermoLibrary.ipynb b/scripts/fitPolycyclicThermoGroupsFromThermoLibrary.ipynb index 1095df53ff..92bde2a02f 100644 --- a/scripts/fitPolycyclicThermoGroupsFromThermoLibrary.ipynb +++ b/scripts/fitPolycyclicThermoGroupsFromThermoLibrary.ipynb @@ -18,35 +18,27 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "# Fill in the list of thermo libraries to be used for fitting polycyclic thermo groups\n", - "thermoLibraries = ['vinylCPD_H','C3','C10H11','Fulvene_H','naphthalene_H']\n" + "thermo_libraries = ['vinylCPD_H','C3','C10H11','Fulvene_H','naphthalene_H']\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "import os\n", - "import re\n", "import copy\n", - "import numpy\n", - "from IPython.display import Image, display\n", + "from IPython.display import display\n", "from rmgpy.data.thermo import *\n", "from rmgpy.data.base import Entry\n", "from rmgpy.data.rmg import RMGDatabase\n", "from rmgpy import settings\n", "from rmgpy.species import Species\n", - "from rmgpy.molecule import Molecule\n", - "from rmgpy.cantherm.output import prettify" + "from arkane.output import prettify" ] }, { @@ -59,50 +51,48 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "def extractPolycyclicGroups(molecule):\n", + "def extract_polycyclic_groups(molecule):\n", " \"\"\"\n", " Extract polycyclic functional groups from a real molecule\n", " \"\"\"\n", " struct = molecule.copy(deep=True)\n", " # Saturate the structure if it is a radical\n", - " if struct.isRadical():\n", + " if struct.is_radical():\n", " struct.saturate()\n", - " struct.deleteHydrogens()\n", + " struct.delete_hydrogens()\n", " \n", - " polyRings = struct.getPolycyclicRings()\n", - " groups = [convertCycleToGroup(ring) for ring in polyRings]\n", + " poly_rings = struct.get_polycycles()\n", + " groups = [convert_cycle_to_group(ring) for ring in poly_rings]\n", " \n", " return groups\n", " \n", - "def convertCycleToGroup(cycle):\n", + "def convert_cycle_to_group(cycle):\n", " \"\"\"\n", " This function converts a list of atoms in a cycle to a functional Group object\n", " \"\"\"\n", " from rmgpy.molecule.group import GroupAtom, GroupBond, Group\n", " \n", " # Create GroupAtom object for each atom in the cycle, label the first one in the cycle with a *\n", - " groupAtoms = {}\n", + " group_atoms = {}\n", " bonds = []\n", " for atom in cycle:\n", - " groupAtoms[atom] = GroupAtom(atomType=[atom.atomType],\n", - " radicalElectrons=[0],\n", + " group_atoms[atom] = GroupAtom(atomType=[atom.atomtype],\n", + " radical_electrons=[0],\n", " label='*' if cycle.index(atom)==0 else '')\n", " \n", - " group = Group(atoms=groupAtoms.values()) \n", + " group = Group(atoms=group_atoms.values()) \n", " \n", " # Create GroupBond for each bond between atoms in the cycle, but not outside of the cycle\n", " for atom in cycle:\n", - " for bondedAtom, bond in atom.edges.iteritems():\n", - " if bondedAtom in cycle:\n", + " for bonded_atom, bond in atom.edges.iteritems():\n", + " if bonded_atom in cycle:\n", " # create a group bond with the same bond order as in the original molecule,\n", " # if it hasn't already been created\n", - " if not group.hasBond(groupAtoms[atom],groupAtoms[bondedAtom]):\n", - " group.addBond(GroupBond(groupAtoms[atom],groupAtoms[bondedAtom],order=[bond.order]))\n", + " if not group.has_bond(group_atoms[atom],group_atoms[bonded_atom]):\n", + " group.add_bond(GroupBond(group_atoms[atom],group_atoms[bonded_atom],order=[bond.order]))\n", " else:\n", " pass\n", " \n", @@ -121,23 +111,21 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "def displayThermo(thermoData):\n", - " print 'H298 = {0} kcal/mol'.format(thermoData.H298.value_si/4184)\n", - " print 'S298 = {0} cal/mol*K'.format(thermoData.S298.value_si/4.184)\n", - "def compareThermoData(thermoData1, thermoData2):\n", - " delH = thermoData1.H298.value_si - thermoData2.H298.value_si\n", - " print 'Difference in H298 = {0} kcal/mol'.format(delH/4184)\n", - " delS = thermoData1.S298.value_si - thermoData2.S298.value_si\n", - " print 'Difference S298 = {0} cal/mol*K'.format(delS/4.184)\n", - " #Tdata = [300,500,1000,2000]\n", - " #for T in Tdata:\n", - " # delCp = thermoData1.getHeatCapacity(T) - thermoData2.getHeatCapacity(T)\n", - " # print 'Difference in Cp at {0} = {1} cal/mol*K'.format(T, delCp/4.184)" + "def display_thermo(thermoData):\n", + " print('H298 = {0} kcal/mol'.format(thermoData.H298.value_si/4184))\n", + " print('S298 = {0} cal/mol*K'.format(thermoData.S298.value_si/4.184))\n", + "def compare_thermo_data(thermo_data1, thermo_data2):\n", + " del_H = thermo_data1.H298.value_si - thermo_data2.H298.value_si\n", + " print('Difference in H298 = {0} kcal/mol'.format(del_H/4184))\n", + " del_S = thermo_data1.S298.value_si - thermo_data2.S298.value_si\n", + " print('Difference S298 = {0} cal/mol*K'.format(del_S/4.184))\n", + " #T_data = [300,500,1000,2000]\n", + " #for T in T_data:\n", + " # del_cp = thermo_data1.get_heat_capacity(T) - thermo_data2.get_heat_capacity(T)\n", + " # print('Difference in cp at {0} = {1} cal/mol*K'.format(T, del_cp/4.184))" ] }, { @@ -150,16 +138,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "database = RMGDatabase()\n", - "database.load(settings['database.directory'], thermoLibraries = thermoLibraries, kineticsFamilies='none', kineticsDepositories='none', reactionLibraries=[])\n", + "database.load(settings['database.directory'], thermo_libraries = thermo_libraries, kinetics_families='none', kinetics_depositories='none', reaction_libraries=[])\n", "\n", - "thermoDatabase = database.thermo\n", - "thermoDatabase0 = copy.deepcopy(database.thermo)" + "thermo_database = database.thermo\n", + "thermo_database0 = copy.deepcopy(database.thermo)" ] }, { @@ -174,93 +160,93 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "fittingDictionary={}\n", - "for libraryName in thermoLibraries:\n", - " thermoLibrary = database.thermo.libraries[libraryName]\n", - " for label, entry in thermoLibrary.entries.iteritems():\n", + "fitting_dictionary={}\n", + "for library_name in thermo_libraries:\n", + " thermo_library = database.thermo.libraries[library_name]\n", + " for label, entry in iter(thermo_library.entries.items()):\n", " molecule = entry.item\n", - " libraryThermoData = entry.data\n", - " if molecule.getAllPolycyclicVertices():\n", - " print label\n", + " library_thermo_data = entry.data\n", + " if molecule.get_all_polycyclic_vertices():\n", + " print(label)\n", " species = Species(molecule=[molecule])\n", " species.generate_resonance_structures() \n", - " print 'Species has {0} resonance structures'.format(len(species.molecule))\n", - " estimatedThermos = [thermoDatabase.estimateThermoViaGroupAdditivity(molecule) for molecule in species.molecule]\n", - " for estimatedThermo in estimatedThermos:\n", - " index = estimatedThermos.index(estimatedThermo)\n", - " ringGroups, polycyclicGroups = database.thermo.getRingGroupsFromComments(estimatedThermo)\n", + " print('Species has {0} resonance structures'.format(len(species.molecule)))\n", + " estimated_thermos = [thermo_database.estimate_thermo_via_group_additivity(molecule) \n", + " for molecule in species.molecule]\n", + " for estimated_thermo in estimated_thermos:\n", + " index = estimated_thermos.index(estimated_thermo)\n", + " ring_groups, polycyclic_groups = database.thermo.get_ring_groups_from_comments(estimated_thermo)\n", " \n", - " if len(polycyclicGroups) == 0:\n", - " raise Exception('Species {0} detected as polycyclic but estimated thermo contained no polycyclic groups: \\\n", - " you need to create a new polycyclic group'.format(label))\n", + " if len(polycyclic_groups) == 0:\n", + " raise Exception('Species {0} detected as polycyclic but estimated thermo contained no \\\n", + " polycyclic groups: you need to create a new polycyclic group'.format(label))\n", "\n", - " elif len(polycyclicGroups) == 1:\n", - " polycyclicGroup = polycyclicGroups[0]\n", - " print 'Molecule {0} has a single polycyclic group match in thermo estimate.'.format(species.molecule[index].toSMILES())\n", + " elif len(polycyclic_groups) == 1:\n", + " polycyclic_group = polycyclic_groups[0]\n", + " print('Molecule {0} has a single polycyclic group match in thermo estimate.'.format(\n", + " species.molecule[index].to_smiles()))\n", " # Draw the molecule in ipython notebook\n", " display(species.molecule[index])\n", - " print 'Molecule SMILES: {0}'.format(species.molecule[index].toSMILES())\n", - " print 'Estimated thermo data:'\n", - " print prettify(repr(estimatedThermo))\n", - " displayThermo(estimatedThermo)\n", + " print('Molecule SMILES: {0}'.format(species.molecule[index].to_smiles()))\n", + " print('Estimated thermo data:')\n", + " print(prettify(repr(estimated_thermo)))\n", + " display_thermo(estimated_thermo)\n", "\n", - " withoutPolycyclicGroupThermo = removeThermoData(copy.deepcopy(estimatedThermo), polycyclicGroup.data)\n", - " newPolycyclicGroupThermo = removeThermoData(copy.deepcopy(libraryThermoData), withoutPolycyclicGroupThermo)\n", + " without_polycyclic_group_thermo = remove_thermo_data(copy.deepcopy(estimated_thermo), \n", + " polycyclic_group.data)\n", + " new_polycyclic_group_thermo = remove_thermo_data(copy.deepcopy(library_thermo_data), \n", + " without_polycyclic_group_thermo)\n", "\n", "\n", " # Check to make sure that the polycyclic group is not generic\n", " # If it is, create a new polycyclicGroup as the child\n", - " if polycyclicGroup.label == 'PolycyclicRing':\n", - " groups = extractPolycyclicGroups(species.molecule[index])\n", - " print groups[0].toAdjacencyList()\n", + " if polycyclic_group.label == 'PolycyclicRing':\n", + " groups = extract_polycyclic_groups(species.molecule[index])\n", + " print(groups[0].to_adjacency_list())\n", " assert len(groups) == 1\n", " # Create a new entry in the polycyclic groups with the same name as the thermo library entry\n", " # Make sure it does not already exist\n", - " entryLabel = label\n", + " entry_label = label\n", " counter = 0\n", - " while entryLabel in thermoDatabase.groups['polycyclic'].entries:\n", + " while entry_label in thermoDatabase.groups['polycyclic'].entries:\n", " counter += 1\n", - " entryLabel = entryLabel.split('-')[0]\n", - " entryLabel += '-{0}'.format(counter)\n", - "\n", + " entry_label = entry_label.split('-')[0]\n", + " entry_label += '-{0}'.format(counter)\n", "\n", - " print 'Polycyclic group was found to be generic \"PolycyclicRing\". Creating new entry: {0}.'.format(entryLabel)\n", - " thermoDatabase.groups['polycyclic'].entries[entryLabel] = Entry(index = len(thermoDatabase.groups['polycyclic'].entries)+1,\n", - " label = entryLabel,\n", - " item = groups[0],\n", - " data = polycyclicGroup.data, # Use dummy thermo here so other estimates can find this group\n", - " parent = polycyclicGroup,\n", - " )\n", "\n", - " # Set the new entry as the polycyclicGroup and make it a child of the generic group\n", - " polycyclicGroup = thermoDatabase.groups['polycyclic'].entries[entryLabel] \n", - " thermoDatabase.groups['polycyclic'].entries['PolycyclicRing'].children.append(polycyclicGroup)\n", + " print('Polycyclic group was found to be generic \"PolycyclicRing\". \\\n", + " Creating new entry: {0}.'.format(entry_label))\n", + " thermo_database.groups['polycyclic'].entries[entry_label] = Entry(index = len(\n", + " thermo_database.groups['polycyclic'].entries)+1,\n", + " label = entry_label,\n", + " item = groups[0],\n", + " data = polycyclic_group.data, # Use dummy thermo here so other estimates can find this group\n", + " parent = polycyclic_group,\n", + " )\n", "\n", + " # Set the new entry as the polycyclic_group and make it a child of the generic group\n", + " polycyclic_group = thermo_database.groups['polycyclic'].entries[entryLabel] \n", + " thermo_database.groups['polycyclic'].entries['PolycyclicRing'].children.append(\n", + " polycyclic_group)\n", "\n", " else:\n", - " print 'Matched polycyclic group \"{0}\"'.format(polycyclicGroup.label)\n", - "\n", - "\n", - "\n", - "\n", + " print('Matched polycyclic group \"{0}\"'.format(polycyclic_group.label))\n", "\n", " # Add the new group value to the fitting dictionary\n", - " if polycyclicGroup not in fittingDictionary:\n", + " if polycyclic_group not in fitting_dictionary:\n", " # Add a tuple containing fitted group data, the original library entry, and thermo library\n", - " fittingDictionary[polycyclicGroup]=[(newPolycyclicGroupThermo, entry, thermoLibrary)]\n", + " fitting_dictionary[polycyclic_group]=[(new_polycyclic_group_thermo, entry, thermo_library)]\n", " else:\n", - " fittingDictionary[polycyclicGroup].append((newPolycyclicGroupThermo, entry, thermoLibrary))\n", + " fitting_dictionary[polycyclic_group].append((new_polycyclic_group_thermo, entry, \n", + " thermo_library))\n", "\n", - " elif len(polycyclicGroups) > 1:\n", - " print 'Species {0} has matched multiple polycyclic groups. \\\n", - " This cannot be fitted with a single molecule\\'s thermo data.'.format(label)\n", + " elif len(polycyclic_groups) > 1:\n", + " print('Species {0} has matched multiple polycyclic groups. This cannot be fitted with a single molecule\\'s thermo data.'.format(label))\n", " raise Exception\n", - " print '====================='" + " print('=====================')" ] }, { @@ -273,40 +259,38 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "for polycyclicGroup, fittingGroups in fittingDictionary.iteritems():\n", - " print 'Original thermo data for polycyclic group: {0}'.format(polycyclicGroup.label)\n", - " if polycyclicGroup.data:\n", - " print prettify(repr(polycyclicGroup.data))\n", + "for polycyclic_group, fitting_groups in iter(fitting_dictionary.items()):\n", + " print('Original thermo data for polycyclic group: {0}'.format(polycyclic_group.label))\n", + " if polycyclic_group.data:\n", + " print(prettify(repr(polycyclic_group.data)))\n", " else:\n", - " print 'No data found. Was created as a new group.'\n", + " print('No data found. Was created as a new group.')\n", " \n", - " thermoDataset = [fitTuple[0] for fitTuple in fittingGroups]\n", - " labels = [fitTuple[1].label for fitTuple in fittingGroups]\n", - " libraryLabels = [fitTuple[2].name for fitTuple in fittingGroups]\n", + " thermo_dataset = [fit_tuple[0] for fit_tuple in fitting_groups]\n", + " labels = [fit_tuple[1].label for fit_tuple in fitting_groups]\n", + " library_labels = [fit_tuple[2].name for fit_tuple in fitting_groups]\n", " # Average the new group values to fit the original polycyclic group\n", - " fittedGroupData = averageThermoData([fitTuple[0] for fitTuple in fittingGroups])\n", - " #print fittedGroupData\n", - " #print fittingGroups\n", - " polycyclicGroup.data = fittedGroupData\n", - " polycyclicGroup.shortDesc = \"Fitted from thermo library values\"\n", + " fitted_group_data = average_thermo_data([fit_tuple[0] for fit_tuple in fitting_groups])\n", + " #print fitted_group_data\n", + " #print fitting_groups\n", + " polycyclic_group.data = fitted_group_data\n", + " polycyclic_group.short_desc = \"Fitted from thermo library values\"\n", " \n", " comment = ''\n", " for i in range(len(labels)):\n", - " comment += \"Fitted from species {0} from {1} library.\\n\".format(labels[i],libraryLabels[i])\n", - " polycyclicGroup.longDesc = comment.strip()\n", + " comment += \"Fitted from species {0} from {1} library.\\n\".format(labels[i],library_labels[i])\n", + " polycyclic_group.long_desc = comment.strip()\n", " \n", - " print 'Fitted thermo data for polycyclic group: {0}'.format(polycyclicGroup.label)\n", - " print prettify(repr(polycyclicGroup.data))\n", - " print polycyclicGroup.longDesc\n", - " print '===================================================================='\n", + " print('Fitted thermo data for polycyclic group: {0}'.format(polycyclic_group.label))\n", + " print(prettify(repr(polycyclic_group.data)))\n", + " print(polycyclic_group.long_desc)\n", + " print('====================================================================')\n", " # At this point, save and overwrite the entire polycyclic thermo library\n", "\n", - "thermoDatabase.groups['polycyclic'].save('new_polycyclic.py')" + "thermo_database.groups['polycyclic'].save('new_polycyclic.py')" ] }, { @@ -319,96 +303,112 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# Test that the new group additivity values can estimate the old library ones.\n", - "\n", - "for libraryName in thermoLibraries:\n", - " thermoLibrary = database.thermo.libraries[libraryName]\n", - " for label, entry in thermoLibrary.entries.iteritems():\n", + "for library_name in thermo_libraries:\n", + " thermo_library = database.thermo.libraries[library_name]\n", + " for label, entry in iter(thermo_library.entries.items()):\n", " molecule = entry.item\n", - " libraryThermoData = entry.data\n", + " library_thermo_data = entry.data\n", "\n", - " if molecule.getAllPolycyclicVertices():\n", + " if molecule.get_all_polycyclic_vertices():\n", " species = Species(molecule=[molecule])\n", " species.generate_resonance_structures()\n", - " print label\n", + " print(label)\n", " display (species.molecule[0])\n", - " print 'Species has {0} resonance structures(s)'.format(len(species.molecule))\n", - " thermoDatabase.findCp0andCpInf(species, libraryThermoData)\n", + " print('Species has {0} resonance structures(s)'.format(len(species.molecule)))\n", + " find_cp0_and_cpinf(species, library_thermo_data)\n", " \n", - " estimatedThermo = thermoDatabase.getThermoDataFromGroups(species)\n", - " originalEstimatedThermo = thermoDatabase0.getThermoDataFromGroups(species)\n", - " if libraryThermoData.isIdenticalTo(estimatedThermo):\n", - " print 'Library thermo data for species {0} matches the estimate from group additivity.\\n'.format(label)\n", + " estimated_thermo = thermo_database.get_thermo_data_from_groups(species)\n", + " original_estimated_thermo = thermo_database0.get_thermo_data_from_groups(species)\n", + " if library_thermo_data.is_identical_to(estimated_thermo):\n", + " print('Library thermo data for species {0} matches the estimate from group additivity.\\n'.format(\n", + " label))\n", " \n", - " print 'Library thermo data:' \n", - " displayThermo(libraryThermoData)\n", - " print '' \n", + " print('Library thermo data:') \n", + " display_thermo(library_thermo_data)\n", + " print('') \n", " \n", - " print 'Original estimated thermo data:' \n", - " ringGroups, polycyclicGroups = database.thermo.getRingGroupsFromComments(estimatedThermo)\n", - " print 'Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclicGroups]))\n", - " displayThermo(originalEstimatedThermo)\n", - " print ''\n", + " print('Original estimated thermo data:') \n", + " ring_groups, polycyclic_groups = database.thermo.get_ring_groups_from_comments(estimated_thermo)\n", + " print('Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclic_groups])))\n", + " display_thermo(original_estimated_thermo)\n", + " print('')\n", " \n", - " print 'Comparison of library thermo with original estimated thermo'\n", - " compareThermoData(libraryThermoData,originalEstimatedThermo)\n", - " print ''\n", + " print('Comparison of library thermo with original estimated thermo')\n", + " compare_thermo_data(library_thermo_data, original_estimated_thermo)\n", + " print('')\n", " else:\n", - " print 'Library thermo data for species {0} does not match the estimate from group additivity\\n'.format(label)\n", + " print('Library thermo data for species {0} does not match the estimate from group \\\n", + " additivity\\n'.format(label))\n", " \n", - " print 'Library thermo data:' \n", - " displayThermo(libraryThermoData)\n", - " print '' \n", + " print('Library thermo data:')\n", + " display_thermo(library_thermo_data)\n", + " print('') \n", " \n", - " print 'Estimated thermo data:' \n", + " print('Estimated thermo data:') \n", " \n", - " ringGroups, polycyclicGroups = database.thermo.getRingGroupsFromComments(estimatedThermo)\n", - " print 'Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclicGroups]))\n", - " displayThermo(estimatedThermo)\n", - " print ''\n", + " ring_groups, polycyclic_groups = database.thermo.get_ring_groups_from_comments(estimated_thermo)\n", + " print('Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclic_groups])))\n", + " display_thermo(estimated_thermo)\n", + " print('')\n", " \n", - " print 'Comparison of library thermo with estimated thermo'\n", - " compareThermoData(libraryThermoData,estimatedThermo)\n", - " print ''\n", + " print('Comparison of library thermo with estimated thermo')\n", + " compare_thermo_data(library_thermo_data, estimated_thermo)\n", + " print('')\n", " \n", - " print 'Original estimated thermo data:' \n", + " print('Original estimated thermo data:') \n", " \n", - " ringGroups, polycyclicGroups = database.thermo.getRingGroupsFromComments(originalEstimatedThermo)\n", - " print 'Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclicGroups]))\n", - " displayThermo(originalEstimatedThermo)\n", - " print ''\n", + " ring_groups, polycyclic_groups = database.thermo.get_ring_groups_from_comments(\n", + " original_estimated_thermo)\n", + " print('Polycyclic groups: {0}'.format(' '.join([grp.label for grp in polycyclic_groups])))\n", + " display_thermo(original_estimated_thermo)\n", + " print('')\n", " \n", - " print 'Comparison of library thermo with original estimated thermo'\n", - " compareThermoData(libraryThermoData,originalEstimatedThermo)\n", - " print ''\n", - " print '======================='" + " print('Comparison of library thermo with original estimated thermo')\n", + " compare_thermo_data(library_thermo_data, original_estimated_thermo)\n", + " print('')\n", + " print('=======================')" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.7.4" + }, + "pycharm": { + "stem_cell": { + "cell_type": "raw", + "source": [], + "metadata": { + "collapsed": false + } + } } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/scripts/generateFilterArrheniusFits.ipynb b/scripts/generateFilterArrheniusFits.ipynb index 1ebc4fb584..2fc94a3141 100644 --- a/scripts/generateFilterArrheniusFits.ipynb +++ b/scripts/generateFilterArrheniusFits.ipynb @@ -23,8 +23,6 @@ "metadata": {}, "outputs": [], "source": [ - "from __future__ import division, print_function\n", - "\n", "import os\n", "import operator\n", "\n", @@ -33,7 +31,7 @@ "import numpy\n", "\n", "from rmgpy import settings\n", - "from rmgpy.data.kinetics.database import FilterLimitFits\n", + "from rmgpy.data.kinetics.database import filter_limit_fits\n", "from rmgpy.data.rmg import RMGDatabase\n", "from rmgpy.kinetics.arrhenius import Arrhenius\n", "from rmgpy.thermo.thermoengine import submit" @@ -55,7 +53,7 @@ "database = RMGDatabase()\n", "database.load(\n", " settings['database.directory'], \n", - " thermoLibraries = [\n", + " thermo_libraries = [\n", " 'primaryThermoLibrary',\n", " 'Klippenstein_Glarborg2016',\n", " 'BurkeH2O2',\n", @@ -67,11 +65,11 @@ " 'SABIC_aromatics',\n", " 'vinylCPD_H'\n", " ],\n", - " transportLibraries = [],\n", - " reactionLibraries = [],\n", - " seedMechanisms = [],\n", - " kineticsFamilies = 'all',\n", - " kineticsDepositories = ['training'],\n", + " transport_libraries = [],\n", + " reaction_libraries = [],\n", + " seed_mechanisms = [],\n", + " kinetics_families = 'all',\n", + " kinetics_depositories = ['training'],\n", " depository = False, \n", ")" ] @@ -97,10 +95,10 @@ "outputs": [], "source": [ "# Temperature range to fit Arrhenius\n", - "Tmin = 298.0\n", - "Tmax = 2500.0\n", - "Tcount = 50\n", - "Ts = 1 / numpy.linspace(1 / Tmax, 1 / Tmin, Tcount)" + "T_min = 298.0\n", + "T_max = 2500.0\n", + "T_count = 50\n", + "Ts = 1 / numpy.linspace(1 / T_max, 1 / T_min, T_count)" ] }, { @@ -121,7 +119,7 @@ " print(fam_name)\n", " \n", " fam = database.kinetics.families[fam_name]\n", - " dep = fam.getTrainingDepository()\n", + " dep = fam.get_training_depository()\n", " rxns = []\n", "\n", " # Extract all training reactions for selected family\n", @@ -144,7 +142,7 @@ " k_list.append(rxn.kinetics)\n", " index_list.append(rxn.index)\n", " if len(rxn.products) == molecularity:\n", - " k_list.append(rxn.generateReverseRateCoefficient())\n", + " k_list.append(rxn.generate_reverse_rate_coefficient())\n", " index_list.append(rxn.index)\n", "\n", " # Get max. kinetic rates at each discrete temperature\n", @@ -152,30 +150,30 @@ " k_max_list = []\n", " max_rxn_list = set()\n", " for T in Ts:\n", - " kvals = [k.getRateCoefficient(T) for k in k_list]\n", + " kvals = [k.get_rate_coefficient(T) for k in k_list]\n", " mydict = dict(zip(index_list, kvals))\n", "\n", " # Find key and value of max rate coefficient\n", - " key_max_rate = max(mydict.iteritems(), key=operator.itemgetter(1))[0]\n", + " key_max_rate = max(mydict.items(), key=operator.itemgetter(1))[0]\n", " \n", " max_entry = dep.entries.get(key_max_rate)\n", " max_rxn = max_entry.item\n", " max_rxn_list.add(max_rxn)\n", " \n", - " kval = mydict[key_max_rate]\n", - " k_max_list.append(kval)\n", + " k_val = mydict[key_max_rate]\n", + " k_max_list.append(k_val)\n", " \n", - " if molecularity==2 and max_rxn.check_collision_limit_violation(Tmin, Tmax, 10000.0, 1.0e7):\n", + " if molecularity==2 and max_rxn.check_collision_limit_violation(T_min, T_max, 10000.0, 1.0e7):\n", " display(max_rxn)\n", " print(\"\"\"The collision limit of {0} m^3/(mol*s) at {1} (K) is violated by \n", " training reaction {2} with index {3}.\n", " \n", " The rate of training reaction {2} \n", - " is {4} m^3/(mol*s).\"\"\".format(max_rxn.calculate_coll_limit(T), T, max_entry, key_max_rate, kval))\n", + " is {4} m^3/(mol*s).\"\"\".format(max_rxn.calculate_coll_limit(T), T, max_entry, key_max_rate, k_val))\n", "\n", " units = 's^-1' if molecularity == 1 else 'm^3/(mol*s)'\n", " \n", - " arr = Arrhenius().fitToData(Ts, numpy.array(k_max_list), units)\n", + " arr = Arrhenius().fit_to_data(Ts, numpy.array(k_max_list), units)\n", " \n", " fig = plt.figure()\n", " fig_name = fam_name\n", @@ -259,7 +257,7 @@ "outputs": [], "source": [ "# Generate empty ArrheniusRMGObject\n", - "filter_fits = FilterLimitFits(unimol=dict_unimol, bimol=dict_bimol)" + "filter_fits = filter_limit_fits(unimol=dict_unimol, bimol=dict_bimol)" ] }, { @@ -283,32 +281,32 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.16" + "pygments_lexer": "ipython3", + "version": "3.7.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", + "source": [], "metadata": { "collapsed": false - }, - "source": [] + } } } }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/scripts/showNewTrainingRxnsImprovements.ipynb b/scripts/showNewTrainingRxnsImprovements.ipynb index 20875c2714..fc289ce508 100644 --- a/scripts/showNewTrainingRxnsImprovements.ipynb +++ b/scripts/showNewTrainingRxnsImprovements.ipynb @@ -11,17 +11,15 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from rmgpy.data.rmg import RMGDatabase\n", - "from rmgpy.chemkin import saveChemkinFile, saveSpeciesDictionary\n", - "from rmgpy.rmg.model import Species, getFamilyLibraryObject, CoreEdgeReactionModel\n", + "from rmgpy.chemkin import save_chemkin_file, save_species_dictionary\n", + "from rmgpy.rmg.model import Species, get_family_library_object, CoreEdgeReactionModel\n", "from rmgpy import settings\n", "from IPython.display import display\n", - "from rmgpy.cantherm.output import prettify\n", + "from arkane.output import prettify\n", "from rmgpy.kinetics.kineticsdata import KineticsData\n", "from rmgpy.data.kinetics.family import TemplateReaction\n", "from rmgpy.data.kinetics.depository import DepositoryReaction\n", @@ -31,14 +29,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "database = RMGDatabase()\n", "libraries = ['vinylCPD_H']\n", - "database.load(settings['database.directory'], kineticsFamilies='all', reactionLibraries = libraries, kineticsDepositories='all')" + "database.load(settings['database.directory'], kinetics_families='all', reaction_libraries = libraries, \n", + " kinetics_depositories='all')" ] }, { @@ -51,15 +48,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "reactionDict = {}\n", - "for libraryName in libraries:\n", - " kineticLibrary = database.kinetics.libraries[libraryName]\n", - " for index, entry in kineticLibrary.entries.iteritems():\n", + "reaction_dict = {}\n", + "for library_name in libraries:\n", + " kinetic_library = database.kinetics.libraries[library_name]\n", + " for index, entry in kinetic_library.entries.items():\n", " lib_rxn = entry.item\n", " display(lib_rxn)\n", " # Let's make RMG generate this reaction from the families!\n", @@ -75,7 +70,7 @@ " for mutation_i in range(rxt_mol_mutation_num):\n", " rxts_mol = [spc.molecule[mutation_i%(len(spc.molecule))] for spc in lib_rxn.reactants]\n", " pdts_mol = [spc.molecule[0] for spc in lib_rxn.products]\n", - " fam_rxn_list.extend(database.kinetics.generateReactionsFromFamilies(\n", + " fam_rxn_list.extend(database.kinetics.generate_reactions_from_families(\n", " reactants=rxts_mol, products=pdts_mol))\n", "\n", " if len(fam_rxn_list) == 1:\n", @@ -84,7 +79,7 @@ " fam_reactants = [r for r in fam_rxn.reactants]\n", " for lib_reactant in lib_reactants:\n", " for fam_reactant in fam_reactants:\n", - " if lib_reactant.isIsomorphic(fam_reactant):\n", + " if lib_reactant.is_isomorphic(fam_reactant):\n", " fam_reactants.remove(fam_reactant)\n", " break\n", "\n", @@ -92,20 +87,20 @@ " fam_products = [r for r in fam_rxn.products]\n", " for lib_product in lib_products:\n", " for fam_product in fam_products:\n", - " if lib_product.isIsomorphic(fam_product):\n", + " if lib_product.is_isomorphic(fam_product):\n", " fam_products.remove(fam_product)\n", " break\n", "\n", " forward = not (len(fam_reactants) != 0 or len(fam_products) != 0)\n", " # find the labeled atoms using family and reactants & products from fam_rxn \n", " family_database = database.kinetics.families[fam_rxn.family]\n", - " family_database.addAtomLabelsForReaction(fam_rxn)\n", + " family_database.add_atom_labels_for_reaction(fam_rxn)\n", " fam_rxn.index = lib_rxn.index\n", - " reactionDict[lib_rxn]=fam_rxn\n", + " reaction_dict[lib_rxn]=fam_rxn\n", " for spec in fam_rxn.reactants + fam_rxn.products:\n", - " print spec.molecule[0].toSMILES()\n", + " print(spec.molecule[0].to_smiles())\n", " else:\n", - " print 'There was an issue finding a single reaction family for this reaction. Please investigate.'" + " print('There was an issue finding a single reaction family for this reaction. Please investigate.')" ] }, { @@ -125,9 +120,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "# Pick a temperature to evaluate the kinetics\n", @@ -137,106 +130,118 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "cem = CoreEdgeReactionModel()\n", - "cem.kineticsEstimator = 'rate rules'\n", - "cem.verboseComments = True" + "cem.kinetics_estimator = 'rate rules'\n", + "cem.verbose_comments = True" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "for libraryName in libraries:\n", - " kineticLibrary = database.kinetics.libraries[libraryName]\n", - " for index, entry in kineticLibrary.entries.iteritems():\n", + "for library_name in libraries:\n", + " kinetic_library = database.kinetics.libraries[library_name]\n", + " for index, entry in kinetic_library.entries.items():\n", " lib_rxn = entry.item\n", " try:\n", - " fam_rxn = reactionDict[lib_rxn]\n", + " fam_rxn = reaction_dict[lib_rxn]\n", " except:\n", - " print 'There was an issue finding a single reaction family for this reaction in step 1. Skipping.'\n", + " print('There was an issue finding a single reaction family for this reaction in step 1. Skipping.')\n", " continue\n", " for idx, spec in enumerate(fam_rxn.reactants):\n", " spec = Species(label=spec.label, molecule=spec.molecule)\n", - " spec.getThermoData()\n", + " spec.get_thermo_data()\n", " fam_rxn.reactants[idx] = spec\n", " for idx, spec in enumerate(fam_rxn.products):\n", " spec = Species(label=spec.label, molecule=spec.molecule)\n", - " spec.getThermoData()\n", + " spec.get_thermo_data()\n", " fam_rxn.products[idx] = spec\n", "\n", - " family = getFamilyLibraryObject(fam_rxn.family)\n", + " family = get_family_library_object(fam_rxn.family)\n", "\n", " # Set the reaction kinetics\n", - " kinetics, source, entryFamily, isForward = cem.generateKinetics(fam_rxn)\n", + " kinetics, source, entry_family, is_forward = cem.generate_kinetics(fam_rxn)\n", " fam_rxn.kinetics = kinetics\n", " # Flip the reaction direction if the kinetics are defined in the reverse direction\n", - " if not isForward:\n", + " if not is_forward:\n", " fam_rxn.reactants, fam_rxn.products = fam_rxn.products, fam_rxn.reactants\n", " fam_rxn.pairs = [(p,r) for r,p in fam_rxn.pairs]\n", - " if family.ownReverse and hasattr(fam_rxn,'reverse'):\n", - " if not isForward:\n", + " if family.own_reverse and hasattr(fam_rxn,'reverse'):\n", + " if not is_forward:\n", " fam_rxn.template = fam_rxn.reverse.template\n", " # We're done with the \"reverse\" attribute, so delete it to save a bit of memory\n", " delattr(fam_rxn,'reverse')\n", "\n", " # convert KineticsData to Arrhenius forms\n", " if isinstance(fam_rxn.kinetics, KineticsData):\n", - " fam_rxn.kinetics = fam_rxn.kinetics.toArrhenius()\n", + " fam_rxn.kinetics = fam_rxn.kinetics.to_arrhenius()\n", " # correct barrier heights of estimated kinetics\n", " if isinstance(fam_rxn,TemplateReaction) or isinstance(fam_rxn,DepositoryReaction): # i.e. not LibraryReaction\n", - " fam_rxn.fixBarrierHeight() # also converts ArrheniusEP to Arrhenius.\n", + " fam_rxn.fix_barrier_height() # also converts ArrheniusEP to Arrhenius.\n", "\n", - " if cem.pressureDependence and fam_rxn.isUnimolecular():\n", + " if cem.pressure_dependence and fam_rxn.is_unimolecular():\n", " # If this is going to be run through pressure dependence code,\n", " # we need to make sure the barrier is positive.\n", - " fam_rxn.fixBarrierHeight(forcePositive=True)\n", - " print '==============='\n", - " print index\n", + " fam_rxn.fix_barrier_height(force_positive=True)\n", + " print('===============')\n", + " print(index)\n", " display(lib_rxn)\n", - " print ''\n", - " print 'Library Kinetics'\n", - " print prettify(repr(entry.data))\n", + " print('')\n", + " print('Library Kinetics')\n", + " print(prettify(repr(entry.data)))\n", " \n", - " print ''\n", - " print 'Reaction Family Kinetics After Training'\n", - " print 'Family: {}'.format(fam_rxn.family)\n", - " print prettify(repr(fam_rxn.kinetics))\n", + " print('')\n", + " print('Reaction Family Kinetics After Training')\n", + " print('Family: {}'.format(fam_rxn.family))\n", + " print(prettify(repr(fam_rxn.kinetics)))\n", " \n", - " print ''\n", - " print 'Kinetics evaluated at {} K'.format(T)\n", - " print 'Library Kinetics: {:.2E}'.format(entry.data.getRateCoefficient(T))\n", - " print 'Reaction Family Kinetics After Training: {:.2E}'.format(fam_rxn.kinetics.getRateCoefficient(T))" + " print('')\n", + " print('Kinetics evaluated at {} K'.format(T))\n", + " print('Library Kinetics: {:.2E}'.format(entry.data.get_rate_coefficient(T)))\n", + " print('Reaction Family Kinetics After Training: {:.2E}'.format(fam_rxn.kinetics.get_rate_coefficient(T)))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.7.4" + }, + "pycharm": { + "stem_cell": { + "cell_type": "raw", + "source": [], + "metadata": { + "collapsed": false + } + } } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file