Skip to content

Commit

Permalink
Add more tests to check c calls
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed May 14, 2024
1 parent efd2e70 commit 266d528
Show file tree
Hide file tree
Showing 21 changed files with 432 additions and 69 deletions.
4 changes: 4 additions & 0 deletions python/resdata/util/test/mock/rd_sum_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def createSummary(

for var in var_list:
key = var.getKey1()
key2 = var.getKey2()
if key and key2:
assert var.keyword in key
assert var.keyword in key2

if key in func_table:
func = func_table[key]
Expand Down
3 changes: 3 additions & 0 deletions python/tests/geometry_tests/test_cpolyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def test_name(self):
p2 = CPolyline(name="Poly2")
self.assertEqual(p2.getName(), "Poly2")

p1 = CPolyline.createFromXYZFile(self.polyline1, name="poly")
self.assertEqual(p1.getName(), "poly")

def test_unzip(self):
pl = CPolyline(init_points=[(0, 3), (1, 4), (2, 5)])
x, y = pl.unzip()
Expand Down
7 changes: 7 additions & 0 deletions python/tests/geometry_tests/test_geo_pointset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ def test_getitem(self):
gp = GeoPointset.fromSurface(srf)
for i in (561, 1105, 1729, 2465, 2821):
self.assertEqual(gp[i], srf[i])

def test_equal(self):
srf_path = self.createTestPath("local/geometry/surface/valid_ascii.irap")
srf = Surface(srf_path)
gp = GeoPointset.fromSurface(srf)
gp2 = GeoPointset.fromSurface(srf)
self.assertEqual(gp, gp2)
44 changes: 44 additions & 0 deletions python/tests/rd_tests/create_restart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from resdata import ResDataType
from resdata.resfile import ResdataKW, openFortIO, FortIO


def create_restart(grid, case, p1, p2=None, rporv1=None, rporv2=None):
with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f:
seq_hdr = ResdataKW("SEQNUM", 1, ResDataType.RD_FLOAT)
seq_hdr[0] = 10
p = ResdataKW("PRESSURE", grid.getNumActive(), ResDataType.RD_FLOAT)
for i in range(len(p1)):
p[i] = p1[i]

header = ResdataKW("INTEHEAD", 67, ResDataType.RD_INT)
header[64] = 1
header[65] = 1
header[66] = 2000

seq_hdr.fwrite(f)
header.fwrite(f)
p.fwrite(f)

if rporv1:
rp = ResdataKW("RPORV", grid.getNumActive(), ResDataType.RD_FLOAT)
for idx, val in enumerate(rporv1):
rp[idx] = val

rp.fwrite(f)

if p2:
seq_hdr[0] = 20
header[66] = 2010
for i in range(len(p2)):
p[i] = p2[i]

seq_hdr.fwrite(f)
header.fwrite(f)
p.fwrite(f)

if rporv2:
rp = ResdataKW("RPORV", grid.getNumActive(), ResDataType.RD_FLOAT)
for idx, val in enumerate(rporv2):
rp[idx] = val

rp.fwrite(f)
7 changes: 7 additions & 0 deletions python/tests/rd_tests/test_fault_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ def test_fault_block_layer(self):
l0 = layer[0]
l1 = layer[1]
self.assertTrue(isinstance(l1, FaultBlock))
assert l1[0].i == 7
assert l1[0].j == 0
l0.getCentroid()
l1.getBlockID()
assert list(l1.get_region_list()) == []

with self.assertRaises(IndexError):
l2 = layer[2]
Expand All @@ -278,6 +281,10 @@ def test_fault_block_layer(self):

l1 = layer.getBlock(1)
self.assertTrue(isinstance(l1, FaultBlock))
l1.add_cell(9, 9)
assert len(l1.get_global_index_list()) == len(l1)
polyline = Polyline(init_points=[(1.0, 0.0), (2.0, 1.0)])
assert l1.contains_polyline(polyline)

with self.assertRaises(KeyError):
l = layer.getBlock(66)
Expand Down
49 changes: 7 additions & 42 deletions python/tests/rd_tests/test_geertsma.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import datetime
import pytest
from resdata import ResDataType
from resdata.resfile import ResdataKW, openFortIO, FortIO, ResdataFile
from resdata.grid import Grid
from resdata.gravimetry import ResdataSubsidence

from resdata.util.test import TestAreaContext
from tests import ResdataTest
from tests.rd_tests.create_restart import create_restart

import numpy as np

Expand All @@ -22,48 +24,6 @@ def create_init(grid, case):
porv.fwrite(f)


def create_restart(grid, case, p1, p2=None, rporv1=None, rporv2=None):
with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f:
seq_hdr = ResdataKW("SEQNUM", 1, ResDataType.RD_FLOAT)
seq_hdr[0] = 10
p = ResdataKW("PRESSURE", grid.getNumActive(), ResDataType.RD_FLOAT)
for i in range(len(p1)):
p[i] = p1[i]

header = ResdataKW("INTEHEAD", 67, ResDataType.RD_INT)
header[64] = 1
header[65] = 1
header[66] = 2000

seq_hdr.fwrite(f)
header.fwrite(f)
p.fwrite(f)

if rporv1:
rp = ResdataKW("RPORV", grid.getNumActive(), ResDataType.RD_FLOAT)
for idx, val in enumerate(rporv1):
rp[idx] = val

rp.fwrite(f)

if p2:
seq_hdr[0] = 20
header[66] = 2010
for i in range(len(p2)):
p[i] = p2[i]

seq_hdr.fwrite(f)
header.fwrite(f)
p.fwrite(f)

if rporv2:
rp = ResdataKW("RPORV", grid.getNumActive(), ResDataType.RD_FLOAT)
for idx, val in enumerate(rporv2):
rp[idx] = val

rp.fwrite(f)


class GeertsmaTest(ResdataTest):
@staticmethod
def test_geertsma_kernel():
Expand All @@ -80,6 +40,7 @@ def test_geertsma_kernel():

subsidence = ResdataSubsidence(grid, init)
subsidence.add_survey_PRESSURE("S1", restart_view1)
subsidence.add_survey_PRESSURE("S2", restart_view1)

youngs_modulus = 5e8
poisson_ratio = 0.3
Expand All @@ -100,6 +61,10 @@ def test_geertsma_kernel():
)
np.testing.assert_almost_equal(dz, 5.8160298201497136e-08)

assert subsidence.eval(
"S1", "S2", receiver, youngs_modulus, poisson_ratio
) == pytest.approx(0.0)

@staticmethod
def test_geertsma_kernel_2_source_points_2_vintages():
grid = Grid.createRectangular(dims=(2, 1, 1), dV=(100, 100, 100))
Expand Down
89 changes: 83 additions & 6 deletions python/tests/rd_tests/test_grav.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,101 @@
import time
import datetime
from resdata import ResDataType
from resdata.resfile import ResdataKW, ResdataFile, openFortIO, FortIO
from resdata.grid import Grid
from resdata.gravimetry import ResdataGrav
from resdata.util.test import TestAreaContext
from tests import ResdataTest
from resdata.rd_util import Phase


class ResdataGravTest(ResdataTest):
def setUp(self):
self.grid = Grid.createRectangular((10, 10, 10), (1, 1, 1))

def test_create(self):
# The init file created here only contains a PORO field. More
# properties must be added to this before it can be used for
# any usefull gravity calculations.
poro = ResdataKW("PORO", self.grid.getGlobalSize(), ResDataType.RD_FLOAT)

kws = [
ResdataKW(kw, self.grid.getGlobalSize(), ResDataType.RD_FLOAT)
for kw in [
"PORO",
"PORV",
"PRESSURE",
"SWAT",
"OIL_DEN",
"RPORV",
"PORV_MOD",
"FIPOIL",
"RFIPOIL",
]
]
int_kws = [
ResdataKW(kw, self.grid.getGlobalSize(), ResDataType.RD_INT)
for kw in ["FIP_NUM", "PVTNUM"]
]

for kw in kws:
for i in range(self.grid.getGlobalSize()):
kw[i] = 0.5
for kw in int_kws:
for i in range(self.grid.getGlobalSize()):
kw[i] = 0

kws += int_kws

with TestAreaContext("grav_init"):
with openFortIO("TEST.UNRST", mode=FortIO.WRITE_MODE) as f:
seq_hdr = ResdataKW("SEQNUM", 1, ResDataType.RD_FLOAT)
seq_hdr[0] = 10

header = ResdataKW("INTEHEAD", 67, ResDataType.RD_INT)
header[64] = 1
header[65] = 1
header[66] = 2000

seq_hdr.fwrite(f)
header.fwrite(f)
for kw in kws:
kw.fwrite(f)

seq_hdr[0] = 20
header[66] = 2009

seq_hdr.fwrite(f)
header.fwrite(f)
for kw in kws:
kw.fwrite(f)

seq_hdr[0] = 20
header[66] = 2010

seq_hdr.fwrite(f)
header.fwrite(f)
for kw in kws:
kw.fwrite(f)

# The init file created here only contains a PORO field. More
# properties must be added to this before it can be used for
# any usefull gravity calculations.
header = ResdataKW("INTEHEAD", 95, ResDataType.RD_INT)
header[14] = 1 # sets phase to oil
header[94] = 100 # E100
with openFortIO("TEST.INIT", mode=FortIO.WRITE_MODE) as f:
poro.fwrite(f)
header.fwrite(f)
for kw in kws:
kw.fwrite(f)
self.init = ResdataFile("TEST.INIT")

grav = ResdataGrav(self.grid, self.init)

restart_file = ResdataFile("TEST.UNRST")
restart_view = restart_file.restartView(sim_time=datetime.date(2000, 1, 1))

grav.new_std_density(1, 0.5)
grav.add_std_density(1, 0, 0.5)

grav.add_survey_RPORV("rporv", restart_view)
grav.add_survey_PORMOD("pormod", restart_view)
grav.add_survey_FIP("fip", restart_view)
grav.add_survey_RFIP("fip", restart_view)

grav.eval("rporv", "pormod", (0, 0, 0), phase_mask=1)
1 change: 1 addition & 0 deletions python/tests/rd_tests/test_grdecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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)

assert kw.get_name() == "COORD"
assert len(kw.numpy_view()) == block_size * num_blocks
Expand Down
76 changes: 75 additions & 1 deletion python/tests/rd_tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import six
from numpy import linspace, allclose
import cwrap

from resdata.util.util import IntVector
from resdata.util.util import IntVector, DoubleVector
from resdata import ResDataType, UnitSystem
from resdata.resfile import ResdataKW, ResdataFile
from resdata.grid import Grid
Expand Down Expand Up @@ -223,13 +224,52 @@ def test_posXYEdge(self):
def test_dims(self):
grid = GridGen.createRectangular((10, 20, 30), (1, 1, 1))
self.assertEqual(grid.get_global_index1F(0), None)
self.assertTrue(grid.equal(grid))
self.assertFalse(grid.dual_grid())
self.assertEqual(grid.get_num_active_fracture(), 0)
self.assertEqual(grid.get_active_fracture_index(global_index=0), -1)
self.assertEqual(
grid.get_bounding_box_2d(),
((0.0, 0.0), (10.0, 0.0), (10.0, 20.0), (0.0, 20.0)),
)
self.assertEqual(grid.depth(ijk=(0, 0, 0)), 0.5)
self.assertEqual(grid.top(0, 0), 0.0)
self.assertEqual(grid.top_active(0, 0), 0.0)
self.assertEqual(grid.bottom(0, 0), 30.0)
self.assertEqual(grid.locate_depth(1.0, 0, 0), 1)
self.assertEqual(grid.find_cell(1.1, 0.0, 0.0), (1, 0, 0))
self.assertTrue(grid.cell_regular(ijk=(1, 0, 0)))
self.assertEqual(grid.get_layer_xyz(1, 0), (1.0, 0.0, 0.0))
self.assertEqual(grid.distance(0, 1), (-1.0, 0.0, 0.0))
self.assertEqual(grid.get_num_lgr(), 0)
self.assertFalse(grid.has_lgr("lgr"))
self.assertEqual(grid.coarse_groups(), 0)
self.assertFalse(grid.in_coarse_group(ijk=(0, 0, 0)))

with self.assertRaises(KeyError):
grid.get_lgr("lgr")
with self.assertRaises(KeyError):
grid.get_lgr(0)
with self.assertRaises(IndexError):
grid.get_cell_lgr(ijk=(0, 0, 0))
self.assertEqual(grid.getNX(), 10)
self.assertEqual(grid.getNY(), 20)
self.assertEqual(grid.getNZ(), 30)
self.assertEqual(grid.nx, 10)
self.assertEqual(grid.ny, 20)
self.assertEqual(grid.nz, 30)
self.assertEqual(grid.getGlobalSize(), 30 * 10 * 20)

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

def test_load_column(self):
column = DoubleVector(2 * 3 * 4)
grid = GridGen.createRectangular((2, 3, 4), (1, 1, 1))
kw = ResdataKW("KW", 2 * 3 * 4, ResDataType.RD_DOUBLE)
kw[0] = 1.0
grid.load_column(kw, 0, 0, column)
assert list(column) == [1.0, 0.0, 0.0, 0.0]

def test_create(self):
with self.assertRaises(ValueError):
grid = GridGen.createRectangular(
Expand Down Expand Up @@ -684,3 +724,37 @@ def test_concvex_cell_containment(self):
middle_point[2] = 30
for p in points[4:8:]:
assertNotPoint(average(20 * [p] + [middle_point]))


def test_save_grdecl(tmpdir):
grid = GridGen.createRectangular((2, 3, 4), (1, 1, 1))
with tmpdir.as_cwd():
with cwrap.open("grid.grdecl", "w") as f:
grid.save_grdecl(f)
assert grid.equal(Grid.load_from_grdecl("grid.grdecl"))
assert grid.equal(Grid.load_from_file("grid.grdecl"))


def test_save_grid(tmpdir):
grid = GridGen.create_rectangular((2, 3, 4), (1, 1, 1))
with tmpdir.as_cwd():
grid.save_GRID("grid.GRID")
assert grid.equal(Grid("grid.GRID"))


def test_write_grdecl(tmpdir):
kw = ResdataKW("KW", 2 * 3 * 4, ResDataType.RD_DOUBLE)
grid = GridGen.create_rectangular((2, 3, 4), (1, 1, 1))

with tmpdir.as_cwd():
with cwrap.open("kw.grdecl", "w") as f:
grid.write_grdecl(kw, f)
with cwrap.open("kw.grdecl", "r") as f:
kw2 = ResdataKW.read_grdecl(f, "KW")

assert list(kw) == list(kw2)


def test_create_volume_keyword():
grid = GridGen.create_rectangular((2, 3, 4), (1, 1, 1))
assert list(grid.create_volume_keyword()) == [1.0] * (2 * 3 * 4)
Loading

0 comments on commit 266d528

Please sign in to comment.