Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and fix Molecule.remove_van_der_waals_bonds #1799

Merged
merged 4 commits into from
Nov 14, 2019
Merged

Test and fix Molecule.remove_van_der_waals_bonds #1799

merged 4 commits into from
Nov 14, 2019

Conversation

rwest
Copy link
Member

@rwest rwest commented Oct 30, 2019

Motivation or Problem

There was a bug where remove_van_der_waals_bonds would modify the dictionary of bonds while iterating over it. I guess Python 3 detects this and crashes, wheras Python 2 didn't.

Description of Changes

This pull request adds a failing unit test, then a patch that makes it pass.

Reviewer Tips

For testing, you could run the tests after the first commit (and see them fail) then run them again after the second commit (and see them pass).

Note the test coverage is misleading because this code is cythonized. Coverage should go up, as I added a test.

@codecov
Copy link

codecov bot commented Oct 30, 2019

Codecov Report

Merging #1799 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1799   +/-   ##
=======================================
  Coverage   42.99%   42.99%           
=======================================
  Files          80       80           
  Lines       21090    21090           
  Branches     5513     5513           
=======================================
  Hits         9067     9067           
+ Misses      11008    10995   -13     
- Partials     1015     1028   +13
Impacted Files Coverage Δ
arkane/kinetics.py 12.14% <0%> (ø) ⬆️
rmgpy/data/statmech.py 42.2% <0%> (ø) ⬆️
rmgpy/rmg/pdep.py 12.21% <0%> (ø) ⬆️
rmgpy/data/kinetics/database.py 50.61% <0%> (ø) ⬆️
rmgpy/data/kinetics/family.py 48.35% <0%> (ø) ⬆️
rmgpy/statmech/ndTorsions.py 59.78% <0%> (ø) ⬆️
rmgpy/yml.py 15.71% <0%> (ø) ⬆️
arkane/sensitivity.py 10% <0%> (ø) ⬆️
rmgpy/rmg/input.py 34% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f561368...df170e4. Read the comment docs.

@ChrisBNEU
Copy link
Contributor

This worked for me. I was getting the following error before while running a reaction with a Pt catalyst:
RuntimeError: dictionary changed size during iteration (see attached file)
After merging I am no longer getting that error.

RMGCat_HANModelError.txt

@amarkpayne amarkpayne self-requested a review November 12, 2019 19:22
@amarkpayne
Copy link
Member

I am assigning myself as the merger for now to give an opportunity for one of the newer developers to take a look at it if they want the experience. If there is no movement on this by tomorrow evening though I'll go ahead and review/merge this in.

@amarkpayne
Copy link
Member

I think this fix is fine, but @mliu49 had implemented something very similar here. Ideally we wouldn't have duplicated code like this, but I don't know if adding this method to the parent class Graph makes sense either.

I think the next best thing would be to make these functions consistent, but this isn't a big deal either. @rwest do you mind updating this branch when you get a chance? If you want feel free to make the code consistent between these functions, or if there is a better solution to get rid of duplicating code I am all for that as well. Either way I think this PR is good to go and I'll merge it as soon as it is updated.

This method currently modifies the dictionary of
bonds while iterating over it. Apparently that 
was undetected in Python 2.

Currently this test fails with:

======================================================================
ERROR: Test we can remove a van-der-Waals bond
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/rwest/Code/Cat/RMG-Py/rmgpy/molecule/moleculeTest.py", line 2699, in test_remove_van_der_waals_bonds
    mol.remove_van_der_waals_bonds()
  File "rmgpy/molecule/molecule.py", line 1091, in rmgpy.molecule.molecule.Molecule.remove_van_der_waals_bonds
    def remove_van_der_waals_bonds(self):
  File "rmgpy/molecule/molecule.py", line 1097, in rmgpy.molecule.molecule.Molecule.remove_van_der_waals_bonds
    for bond in atom.edges.values():
RuntimeError: dictionary changed size during iteration

Subsequent commit will fix it
Fixes this problem:

Error: Problem family: Surface_Adsorption_vdW
Error: Problem reactants: (Molecule(smiles="[Pt]"), Molecule(smiles="CO"))
Traceback (most recent call last):
snip
  File "/Users/rwest/Code/Cat/RMG-Py/rmgpy/data/kinetics/family.py", line 1567, in _generate_product_structures
    product_structures = self.apply_recipe(reactant_structures, forward=forward)
  File "/Users/rwest/Code/Cat/RMG-Py/rmgpy/data/kinetics/family.py", line 1483, in apply_recipe
    struct.remove_van_der_waals_bonds()
  File "rmgpy/molecule/molecule.py", line 1091, in rmgpy.molecule.molecule.Molecule.remove_van_der_waals_bonds
  File "rmgpy/molecule/molecule.py", line 1097, in rmgpy.molecule.molecule.Molecule.remove_van_der_waals_bonds
RuntimeError: dictionary changed

Done in a similar way to the Group method.
@rwest
Copy link
Member Author

rwest commented Nov 13, 2019

Thanks for spotting the code duplication. It also annoys me, but I think adding it to "Graph" doesn't make sense, as a Graph should know nothing about whether its 'edges' are van der Waals or not. So I did what you called the "next best thing" and made my fix the same as Max's.

While updating the code I spotted a couple of tiny tweaks. Just rebased and pushed.

Copy link
Member

@amarkpayne amarkpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge this after the tests pass. Thanks for the additional fixes!!

@amarkpayne amarkpayne merged commit 41f09da into master Nov 14, 2019
@amarkpayne amarkpayne deleted the catfix branch November 14, 2019 01:07
@mliu49 mliu49 mentioned this pull request Dec 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Complexity: Low Status: Ready for Review PR is complete and ready to be reviewed Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants