Skip to content

Commit

Permalink
Fix IEEE754-related bug in gasoil.add_fromtable
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Sep 3, 2020
1 parent ad0db74 commit 97bdf0f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pyscal/gasoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ def add_fromtable(
swlfrominput,
)
logger.warning(" Do not trust the result near the endpoint.")

if 0 < swlfrominput - self.swl < epsilon:
# Perturb max sg in incoming dataframe when we are this close,
# or we will get into floating trouble when interpolating.
dframe.loc[dframe[sgcolname].idxmax(), sgcolname] += swlfrominput - self.swl

if krgcolname in dframe:
if not (dframe[krgcolname].diff().dropna() > -epsilon).all():
raise ValueError("Incoming krg not increasing")
Expand Down
20 changes: 19 additions & 1 deletion tests/test_fromtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from hypothesis import given, settings
import hypothesis.strategies as st

from pyscal import WaterOil, GasOil
from pyscal import WaterOil, GasOil, WaterOilGas

from common import check_table, float_df_checker

Expand Down Expand Up @@ -186,6 +186,24 @@ def test_wo_singlecolumns():
assert wateroil.table["pc"].sum() == 0


def test_ieee_754():
wateroilgas = WaterOilGas(swl=0.18)
# For this particular swl value, we get:
# wateroilgas.gasoil.table.sg.max()
# Out[18]: 0.8200000000000001 # = (1 - 0.18)

# Can we then interpolate from a table that goes up to 0.82?
sgof = pd.DataFrame(
columns=["Sg", "krg", "krog", "pcog"], data=[[0, 0, 1, 0], [0.82, 1, 0, 0]]
)
# Note, replacing 0.82 in the table above with 1-0.18 *is not the same*
wateroilgas.gasoil.add_fromtable(sgof)
assert wateroilgas.gasoil.table["krg"].max() == 1.0
assert wateroilgas.gasoil.table["krog"].min() == 0.0
assert wateroilgas.gasoil.table["pc"].min() == 0.0
assert wateroilgas.gasoil.table["pc"].max() == 0.0


def test_wo_invalidcurves():
"""Test what happens when we give in invalid data"""
# Sw data not ordered:
Expand Down

0 comments on commit 97bdf0f

Please sign in to comment.