Skip to content

Commit

Permalink
Fix to avoid numerical overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Jun 27, 2021
1 parent 3c6e761 commit 732bd0e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions chemicals/vapor_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

import os
from fluids.constants import R
from fluids.numerics import numpy as np, trunc_exp
from fluids.numerics import numpy as np, trunc_exp, trunc_log
from math import e
from chemicals.utils import log, log10, exp, sqrt, isnan, mark_numba_incompatible
from chemicals.utils import PY37, source_path, os_path_join, can_load_data
Expand Down Expand Up @@ -826,9 +826,15 @@ def Antoine_AB_coeffs_from_point(T, Psat, dPsat_dT, base=10.0):
.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition.
New York: McGraw-Hill Professional, 2000.
'''

log_base_inv = 1.0/log(base)
Psat_inv = 1.0/Psat
A = log(Psat*exp(T*dPsat_dT*Psat_inv))*log_base_inv
# The expression from SymPy is as follows
#A = log(Psat*exp(T*dPsat_dT*Psat_inv))*log_base_inv
# However, that expression has overflows a lot
# Mathematical manipulation yields the following, which avoids overflows
A = (T*dPsat_dT*Psat_inv + log(Psat))*log_base_inv

B = T*T*dPsat_dT*log_base_inv*Psat_inv
return (A, B)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_vapor_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def test_test_Antoine_AB():
AB = Antoine_AB_coeffs_from_point(T, Psat, dPsat_dT, base=exp(1))
assert_close(A, AB[0])
assert_close(B, AB[1])

# Nasty overflow point
kwargs = {'T': 159.10000000000002, 'Psat': 0.0007352199499947608, 'dPsat_dT': 0.004770694513544764, 'base': 2.718281828459045}
AB = Antoine_AB_coeffs_from_point(**kwargs)
# Validated with mpmath
assert_close1d(AB, (1025.1525881065513, 164249.7374972777), rtol=1e-13)



Expand Down

0 comments on commit 732bd0e

Please sign in to comment.