Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Jan 6, 2025
1 parent e491e7d commit f5a3764
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 14 deletions.
2 changes: 1 addition & 1 deletion python/resdata/grid/faults/fault_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def contains_polyline(self, polyline):
"""
Will return true if at least one point from the polyline is inside the block.
"""
edge_polyline = self.getEdgePolygon()
edge_polyline = self.get_edge_polygon()
for p in polyline:
if GeometryTools.pointInPolygon(p, edge_polyline):
return True
Expand Down
2 changes: 1 addition & 1 deletion python/resdata/grid/faults/fault_block_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def add_fault_link(self, fault1, fault2):

def join_faults(self, fault1, fault2):
if not fault1.intersectsFault(fault2, self.getK()):
layer = self.getGeoLayer()
layer = self.get_geo_layer()
try:
layer.addIJBarrier(Fault.joinFaults(fault1, fault2, self.getK()))
except ValueError:
Expand Down
4 changes: 2 additions & 2 deletions python/resdata/grid/faults/fault_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __str__(self):
return self.__segment_map.__str__()

def verify(self):
for C, count in self.__count_map.iteritems():
for C, count in self.__count_map.items():
if count > 0:
d = self.__segment_map[C]
if len(d) != count:
Expand Down Expand Up @@ -135,7 +135,7 @@ def pop_next(self, segment):

def print_content(self):
for d in self.__segment_map.values():
for C, S in d.iteritems():
for C, S in d.items():
print(S)


Expand Down
4 changes: 2 additions & 2 deletions python/resdata/grid/faults/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def add_ij_barrier(self, ij_list):
if len(ij_list) < 2:
raise ValueError("Must have at least two (i,j) points")

nx = self.getNX()
ny = self.getNY()
nx = self.get_nx()
ny = self.get_ny()
p1 = ij_list[0]
i1, j1 = p1
for p2 in ij_list[1:]:
Expand Down
15 changes: 14 additions & 1 deletion python/tests/rd_tests/test_fault_blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from unittest import skipIf
import cwrap
import pytest

from resdata import ResDataType
from resdata.resfile import ResdataKW
Expand Down Expand Up @@ -147,6 +147,7 @@ def test_neighbours2(self):
f.write("FAULTS\n")
f.write("'FY' 1 4 4 4 1 1 'Y' /\n")
f.write("'FX' 4 4 1 8 1 1 'X' /\n")
f.write("'FXX' 6 6 1 8 1 1 'X' /\n")
f.write("/")

faults = FaultCollection(grid, "faults.grdecl")
Expand Down Expand Up @@ -185,6 +186,10 @@ def test_neighbours2(self):
nb = b1.getNeighbours()
self.assertEqual(len(nb), 0)

layer.join_faults(faults["FX"], faults["FY"])
with pytest.raises(ValueError, match="Failed to join faults FXX and FY"):
layer.join_faults(faults["FXX"], faults["FY"])

def test_neighbours3(self):
nx = 8
ny = 8
Expand Down Expand Up @@ -286,6 +291,9 @@ def test_fault_block_layer(self):
polyline = Polyline(init_points=[(1.0, 0.0), (2.0, 1.0)])
assert l1.contains_polyline(polyline)

polyline2 = Polyline(init_points=[(10.5, 1.0), (11, 5)])
assert not l1.contains_polyline(polyline2)

with self.assertRaises(KeyError):
l = layer.getBlock(66)

Expand Down Expand Up @@ -417,6 +425,11 @@ def test_internal_blocks(self):
faults = FaultCollection(grid, "faults.grdecl")

layer.loadKeyword(kw)
faulty_kw = ResdataKW("SOIL", 10000, ResDataType.RD_INT)
with pytest.raises(
ValueError, match="The fault block keyword had wrong type/size"
):
layer.load_keyword(faulty_kw)
layer.addFaultBarrier(faults["FX"])
b1 = layer.getBlock(1)
b2 = layer.getBlock(2)
Expand Down
31 changes: 31 additions & 0 deletions python/tests/rd_tests/test_faults.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FaultLine,
FaultSegment,
FaultBlockLayer,
SegmentMap,
)
from resdata.util.test import TestAreaContext
from resdata.geometry import Polyline, CPolyline
Expand Down Expand Up @@ -127,6 +128,36 @@ def test_faultLine_center(self):
self.assertEqual(len(fl), 2)
self.assertEqual(fl.center(), (0.50, 0.50))

def test_fault_segments(self):
S1 = FaultSegment(0, 10)
S2 = FaultSegment(10, 20)
S3 = FaultSegment(0, 10)
S4 = FaultSegment(10, 0)
S5 = FaultSegment(30, 40)
S6 = FaultSegment(50, 40)
assert S1 != S2
assert S1 == S3
assert S3 == S4
assert S1.joins(S2)
assert S2.joins(S4)
assert S1.joins(S4)
assert not S2.joins(S5)
assert not S5.joins(S2)
assert S6.joins(S5)

def test_segment_map(self):
S1 = FaultSegment(0, 10)
S2 = FaultSegment(10, 20)
S5 = FaultSegment(30, 40)
SM = SegmentMap()
SM.add_segment(S1)
SM.add_segment(S2)
assert len(SM) == 3
SM.verify()
SM.add_segment(S5)
assert len(SM) == 5
SM.print_content()

def test_faultLine(self):
fl = FaultLine(self.grid, 10)
S1 = FaultSegment(0, 10)
Expand Down
6 changes: 5 additions & 1 deletion python/tests/rd_tests/test_grdecl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import pytest

from numpy import allclose

import cwrap
from resdata.resfile import ResdataKW


def test_eclkw_read_grdecl(tmp_path):
def test_resdatakw_read_grdecl(tmp_path):
block_size = 10
num_blocks = 5
value = 0.15
Expand All @@ -17,6 +19,8 @@ def test_eclkw_read_grdecl(tmp_path):
with cwrap.open(str(tmp_path / "test.grdecl")) as f:
kw = ResdataKW.read_grdecl(f, "COORD")
assert ResdataKW.fseek_grdecl(f, "COORD", True)
with pytest.raises(TypeError, match="Sorry keyword:TOOLONGAKW"):
ResdataKW.read_grdecl(f, "TOOLONGAKW")

assert kw.get_name() == "COORD"
assert len(kw.numpy_view()) == block_size * num_blocks
Expand Down
29 changes: 24 additions & 5 deletions python/tests/rd_tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import os.path
from unittest import skip
import itertools
import pytest

import functools
from numpy import linspace, allclose
import numpy as np
import cwrap

from resdata.util.util import IntVector, DoubleVector
Expand Down Expand Up @@ -260,6 +261,13 @@ def test_dims(self):

self.assertEqual(grid.getDims(), (10, 20, 30, 6000))

def test_global_index(self):
grid = GridGen.createRectangular((10, 20, 30), (1, 1, 1))
with pytest.raises(
IndexError, match=r"Invalid value global_index:7000 Range: \[0,6000\)"
):
grid.get_active_index(global_index=7000)

def test_load_column(self):
column = DoubleVector(2 * 3 * 4)
grid = GridGen.createRectangular((2, 3, 4), (1, 1, 1))
Expand Down Expand Up @@ -455,7 +463,18 @@ def test_create_3d_is_create_kw_inverse(self):
numpy_3d = grid.create3D(kw1)
kw2 = grid.create_kw(numpy_3d, "SWAT", False)
self.assertEqual(kw2.name, "SWAT")
assert allclose(grid.create3D(kw2), numpy_3d)
assert np.allclose(grid.create3D(kw2), numpy_3d)

def test_create_kw(self):
rng = np.random.default_rng()
nx = 10
ny = 7
nz = 5
grid = GridGen.create_rectangular((nx, ny, nz), (1, 1, 1))
array1 = rng.integers(0, 100, size=(nx, ny, nz), dtype=np.int32)
array2 = rng.normal(10, 2, size=(nx, ny, nz))
_ = grid.create_kw(array1, "SWAT1", False)
_ = grid.create_kw(array2, "SWAT2", False)

def test_create_3d_agrees_with_get_value(self):
nx = 5
Expand Down Expand Up @@ -539,13 +558,13 @@ def test_unique_containment(self):

(xmin, xmax), (ymin, ymax), (zmin, zmax) = getMinMaxValue(wgrid)

x_space = linspace(
x_space = np.linspace(
xmin - 1, xmax + 1, int(xmax - xmin + 2) * steps_per_unit + 1
)
y_space = linspace(
y_space = np.linspace(
ymin - 1, ymax + 1, int(ymax - ymin + 2) * steps_per_unit + 1
)
z_space = linspace(
z_space = np.linspace(
zmin - 1, zmax + 1, int(zmax - zmin + 2) * steps_per_unit + 1
)

Expand Down
3 changes: 3 additions & 0 deletions python/tests/rd_tests/test_rd_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def test_block_view(self):

view = rd_file.blockView2(None, "DATA2", 0)

with pytest.raises(KeyError, match="The keyword:FAULTY is not in file"):
rd_file.block_view2("HEADER", "FAULTY", 0)


def test_report_list(tmpdir):
with tmpdir.as_cwd():
Expand Down
22 changes: 22 additions & 0 deletions python/tests/rd_tests/test_rd_kw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env python

import pytest

import random
import warnings
import cwrap
Expand Down Expand Up @@ -622,6 +625,25 @@ def test_iadd():

assert list(kw1) == list(kw2)

with pytest.raises(TypeError, match="Type mismatch"):
kw1 += "a"


def test_imul():
kw1 = ResdataKW("KW1", 5, ResDataType.RD_INT)
for i in range(len(kw1)):
kw1[i] = 1
kw1 *= 10

assert list(kw1) == [10] * 5

with pytest.raises(TypeError, match="Type mismatch"):
kw1 *= 3.2

kw2 = ResdataKW("KW2", 5, ResDataType.RD_FLOAT)
with pytest.raises(TypeError, match="Only muliplication with scalar supported"):
kw2 *= "a"


def test_get_ptr_data():
assert ResdataKW("KW1", 10, ResDataType.RD_INT).get_data_ptr()
Expand Down
5 changes: 4 additions & 1 deletion python/tests/rd_tests/test_rd_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def test_enums(self):

def test_file_type(self):
file_type, fmt, report = ResdataUtil.inspectExtension("CASE.X0078")
self.assertEqual(file_type, FileType.RESTART)
assert file_type == FileType.RESTART
file_type, fmt, report = ResdataUtil.inspectExtension("CASE.UNRST")
assert file_type == FileType.UNIFIED_RESTART
assert report == None

def test_file_report_nr(self):
report_nr = ResdataUtil.reportStep("CASE.X0080")
Expand Down
19 changes: 19 additions & 0 deletions python/tests/rd_tests/test_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_equal(self):
with self.assertRaises(ValueError):
region.select_equal(kw_float, 1)

with pytest.raises(
ValueError,
match="The select_equal method must have an integer valued keyword",
):
region.deselect_equal(kw_float, 2)

def test_sum(self):
grid = GridGenerator.create_rectangular((10, 10, 1), (1, 1, 1))
kw_mask = ResdataKW("INT", grid.getGlobalSize(), ResDataType.RD_INT)
Expand Down Expand Up @@ -220,6 +226,13 @@ def poro(grid):
)


@pytest.fixture
def poro_int(grid):
return grid.create_kw(
np.ones((grid.nx, grid.ny, grid.nz), dtype=np.int32), "PORO", True
)


def test_select_in_range(empty_region, active_region, poro):
empty_region.select_in_range(poro, 0.9, 1.1)
assert empty_region == active_region
Expand Down Expand Up @@ -464,6 +477,12 @@ def test_idiv_kw_full(full_region, poro):
assert list(poro) == [1.0] * len(poro)


def test_idiv_kw_int(full_region, poro_int):
poro_int += 1
full_region.idiv_kw(poro_int, 10)
assert list(poro_int) == [0] * len(poro_int) # ???


def test_mul_kw_full(full_region, poro):
poro += 1.0
poro.mul(poro, mask=full_region)
Expand Down

0 comments on commit f5a3764

Please sign in to comment.