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

Enable fitting and reversing of negative reaction rates. #1834

Merged
merged 5 commits into from
Dec 6, 2019

Conversation

rwest
Copy link
Member

@rwest rwest commented Dec 1, 2019

Motivation or Problem

Sometimes people represent a complex rate expression as the sum of Arrhenius, or sum of PDepArrhenius terms, one or more of which is negative. Although an overall negative rate expression is not allowed, one part of the sum may be.
When generating reverse rate expressions, these multiple sum type expressions are reversed one part at a time. The function to generate a reverse rate expression for an individual Arrhenius expression does so by evaluating the rate at some array of temperatures, calculating the reverses (from the equilibrium constants) then doing least squares regression to fit a new Arrhenius. But that last step involves taking logarithms of the rates, which leads to nan if they're negative, which then messes up the least squares regression.

This was the underlying cause of #1833 (not problems in my LAPACK implementation, as originally suspected)

Description of Changes

If trying to fit a negative rate, negate it then fit it then negate the A factor.
Also ensure that all k(T) have the same sign, and are all finite.
Also added a couple of unit tests, that would fail without this fix.

Testing

Unit tests have been added.
(The second one doesn't really test the result, just tries not to crash. But it used to crash)

@rwest
Copy link
Member Author

rwest commented Dec 2, 2019

The Codacy complaints are about a few spaces missing after commas in some code generated by our own __repr__ calls.

Copy link
Contributor

@mliu49 mliu49 left a comment

Choose a reason for hiding this comment

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

I think it would be nice to update or __repr__ methods to conform to PEP-8, but that's not urgent or related to this PR.

I think this is a good fix for the issues with negative rates in a MultiArrhenius.

@@ -152,6 +152,13 @@ cdef class Arrhenius(KineticsModel):
data.
"""
import scipy.stats
assert all(np.isfinite(klist)), "Rates must all be finite, not inf or NaN"
if any(klist<0):
assert all(klist<0), "Rates must all be positive or all be negative."
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you convert these assertions to raise ValueError instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. as a fixup, when rebasing to latest master.

rwest added 2 commits December 5, 2019 21:37
This test fails, for me.
File "/Users/rwest/opt/anaconda3/envs/rmg3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 2156, in lstsq
    x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
  File "/Users/rwest/opt/anaconda3/envs/rmg3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 101, in _raise_linalgerror_lstsq
    raise LinAlgError("SVD did not converge in Linear Least Squares")
numpy.linalg.linalg.LinAlgError: SVD did not converge in Linear Least Squares

See #1833
The scipy version allows you to change the algorithm used,
to work around some bugs in certain versions of MKL.
@codecov
Copy link

codecov bot commented Dec 6, 2019

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1834   +/-   ##
=======================================
  Coverage   43.91%   43.91%           
=======================================
  Files          83       83           
  Lines       21564    21564           
  Branches     5652     5652           
=======================================
  Hits         9469     9469           
+ Misses      11048    11034   -14     
- Partials     1047     1061   +14
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/family.py 48.35% <0%> (ø) ⬆️
rmgpy/statmech/ndTorsions.py 59.78% <0%> (ø) ⬆️
rmgpy/yml.py 15.71% <0%> (ø) ⬆️
arkane/sensitivity.py 10% <0%> (ø) ⬆️
rmgpy/data/kinetics/database.py 50.61% <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 9a46923...523cf48. Read the comment docs.

rwest added 3 commits December 5, 2019 21:50
Some MultiArrhenius and MultiPDepArrhenius expressions
use negative A factors for one of the expressions.
This is permitted as long as the overall sum is positive.

This commit allows the Arrhenius.fit_to_data to work
with negative rates.

Probably solves #1833
This reverts commit c49521f.

Having fixed the problem of trying to take logarithms of negative rates,
I think the numpy algorithm is probably robust enough, and there
is no need to use the scipy version. Though I have no idea which is "best".
Sometimes you have negative k and want a negative A...
(as part of a MultiArrhenius expression)
Copy link
Contributor

@mliu49 mliu49 left a comment

Choose a reason for hiding this comment

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

Thanks!

@mliu49 mliu49 merged commit 9ea90fe into master Dec 6, 2019
@mliu49 mliu49 deleted the reversekinetics branch December 6, 2019 15:42
This was referenced Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Next Release Status: Ready for Review PR is complete and ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants