-
Notifications
You must be signed in to change notification settings - Fork 230
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
Conversation
The Codacy complaints are about a few spaces missing after commas in some code generated by our own |
There was a problem hiding this 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.
rmgpy/kinetics/arrhenius.pyx
Outdated
@@ -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." |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.
17bfe93
to
fc97fb2
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
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)
fc97fb2
to
523cf48
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
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)