Skip to content

Commit fb12fdb

Browse files
committed
BUG: fix issue in swfunction when x value gets zero
1 parent e91db27 commit fb12fdb

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/fmu/tools/properties/swfunction.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SwFunction:
7676

7777
grid: xtgeo.Grid
7878
# all input are constants or xtgeo grid or gridproperty
79-
x: float | xtgeo.GridProperty = 0.0
79+
x: float | xtgeo.GridProperty = 1e-6 # default to avoid division by zero
8080
a: float | xtgeo.GridProperty = 1.0
8181
b: float | xtgeo.GridProperty = -1.0
8282
ffl: float | xtgeo.GridProperty = 999.0
@@ -163,6 +163,9 @@ def _process_input(self) -> None:
163163
new_b,
164164
)
165165

166+
# ensure that self.x is not zero, as this will cause division by zero
167+
self.x.values = np.ma.where(self.x.values == 0.0, 1e-6, self.x.values)
168+
166169
def _compute_htop_hbot(self) -> None:
167170
"""Setting geometries for 'bottom' and 'top' cell.
168171
@@ -321,4 +324,11 @@ def compute(self, compute_method: str = "integrated") -> xtgeo.GridProperty:
321324
else:
322325
self._compute_direct()
323326

327+
# ensure that the result's mask is not inconcistent with the grid
328+
actnum = self.grid.get_actnum()
329+
grid_mask = np.ma.masked_where(actnum.values == 0, actnum.values)
330+
assert np.array_equal(grid_mask.mask, self._sw.values.mask), (
331+
"Bug: Grid mask and Sw mask are not equal, contact developer!"
332+
)
333+
324334
return self._sw

tests/properties/test_swfunction.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,54 @@ def test_swj_simple(avalue, bvalue, ffl, direct, cellmethod, expected_mean):
4242
assert sw.values.mean() == pytest.approx(expected_mean, rel=0.01)
4343

4444

45+
def test_swj_simple_x_zero():
46+
"""Test that masked cells are properly treated"""
47+
48+
grid1 = xtgeo.create_box_grid((2, 3, 1), increment=(1, 1, 1), origin=(0, 0, 0))
49+
poro = 0.3
50+
perm = 300
51+
52+
xvalues = math.sqrt(perm / poro)
53+
54+
xprop = xtgeo.GridProperty(grid1, values=xvalues)
55+
xprop.values[0, 0, 0] = 0.0 # Set one value to zero
56+
57+
sw_obj = SwFunction(
58+
grid=grid1,
59+
a=1,
60+
b=-2,
61+
x=xprop,
62+
ffl=12.5,
63+
invert=True,
64+
method="cell_center_above_ffl",
65+
)
66+
sw = sw_obj.compute("integrated")
67+
assert not sw.values.mask[0, 0, 0]
68+
69+
# now make a grid with masked cells
70+
grid2 = grid1.copy()
71+
act = grid2.get_actnum()
72+
act.values[0, 0, 0] = 0
73+
grid2.set_actnum(act)
74+
75+
assert grid2.get_actnum().values[0, 0, 0] == 0
76+
77+
xprop = xtgeo.GridProperty(grid2, values=xvalues)
78+
xprop.values[0, 0, 0] = 1000.0
79+
80+
sw_obj = SwFunction(
81+
grid=grid2,
82+
a=1,
83+
b=-2,
84+
x=xprop,
85+
ffl=12.5,
86+
invert=True,
87+
method="cell_center_above_ffl",
88+
)
89+
sw = sw_obj.compute("integrated")
90+
assert sw.values.mask[0, 0, 0]
91+
92+
4593
def test_swj_simple_threshold_2grids():
4694
"""Test a simple SwJ setup, expected mean are checked with RMS Sw job.
4795

0 commit comments

Comments
 (0)