CalebBell / thermo

Thermodynamics and Phase Equilibrium component of Chemical Engineering Design Library (ChEDL)
MIT License
594 stars 114 forks source link

Funny "in_range" parameter behaviour for extrapolation #87

Closed yoelcortes closed 2 years ago

yoelcortes commented 2 years ago

Hi Caleb,

There is a minor bug with extrapolation concerning the use of the "in_range" parameter:

>>> from thermo import Chemical
>>> VaporPressure = Chemical('Water').VaporPressure
>>> VaporPressure.extrapolation = 'constant'
>>> VaporPressure.extrapolate(VaporPressure.Tc+10., method=VaporPressure.method, in_range='low')
697.8319382463911

I can easily fix this, but I was wondering whether it might be better to deprecate the in_range parameter? It adds a bit of coding and maintenance overhead for the new extrapolate_integral and extrapolate_derivative methods as well.

Thanks!

CalebBell commented 2 years ago

Hi Yoel, I don't believe this to be a bug at all - and the in_range argument was quite useful during development of the feature. It allows for observation of the extrapolation behavior of the extrapolation methods themselves :) For example, the following code can be used to check that the extrapolation method 'AntoineAB' is correctly operating by testing both extrapolations in the main-correlation regime.

from chemicals import *
from thermo import *
obj = VaporPressure(extrapolation='AntoineAB')
f = lambda T: Antoine(T=T, A=3.45604+5, B=1044.038, C=0)
obj.add_method(f=f, name='WebBook', Tmin=177.70, Tmax=264.93)
obj.calculate(200, 'WebBook'), obj.extrapolate(200, 'WebBook', in_range='low'), obj.extrapolate(200, 'WebBook', in_range='high')

(1721.2739653404378, 1721.3038977469278, 1721.3098351750775)

Sincerely, Caleb

yoelcortes commented 2 years ago

Hi Caleb,

Sounds good! But there is actually a simple bug, sorry my last example was a little vague. When the temperature is above the limit, the extrapolation chooses the low value (but it should choose the high one):

>>> from thermo import Chemical
>>> VaporPressure = Chemical('Water').VaporPressure
>>> VaporPressure.extrapolation = 'constant'
>>> VaporPressure.extrapolate(VaporPressure.Tc+10000, method=VaporPressure.method, in_range='low')
697.8319382463911
>>> VaporPressure.T_limits[VaporPressure.method]
(275.0, 647.35)

The other way around it works fine. When we set in_range as 'high', the extrapolation chooses the low value when the temperature is below the limits.

>>> from thermo import Chemical
>>> VaporPressure = Chemical('Water').VaporPressure
>>> VaporPressure.extrapolation = 'constant'
>>> VaporPressure.extrapolate(T=1, method=VaporPressure.method, in_range='high')
697.8319382463911

I'll patch this up real soon and will keep the "in_range" behavior for new extrapolate methods (for integral and derivative).

Thanks again!