Skip to content

Commit

Permalink
Fix an issue where SummaryVector first_lt failed
Browse files Browse the repository at this point in the history
The failure is due to the self.__values cache being
mismanaged.
  • Loading branch information
eivindjahren committed May 15, 2024
1 parent 34a7958 commit df63a49
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
17 changes: 7 additions & 10 deletions python/resdata/summary/rd_sum_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ def __getitem__(self, index):
not be a proper SummaryVector instance, but rather a normal
Python list of SummaryNode instances.
"""
self.assert_values()
length = len(self.values)
vals = self.values
length = len(vals)
if isinstance(index, int):
if index < 0:
index += len(self.__values)
index += len(vals)
if index < 0 or index > length:
raise KeyError("Invalid index:%d out of range [0:%d)" % (index, length))
else:
Expand Down Expand Up @@ -185,19 +185,15 @@ def last(self):
"""
Will return the last SummaryNode in this vector.
"""
self.assert_values()

index = len(self.__values) - 1
index = len(self.values) - 1
return self.__iget(index)

@property
def last_value(self):
"""
Will return the last value in this vector.
"""
self.assert_values()

index = len(self.__values) - 1
index = len(self.values) - 1
return self.__iget(index).value

def get_interp(self, days=None, date=None):
Expand Down Expand Up @@ -261,7 +257,7 @@ def first_gt(self, limit):
vectors with report_only = True.
"""
time_index = self.first_gt_index(limit)
print(time_index)
self.assert_values()
if time_index >= 0:
return self.__iget(time_index)
else:
Expand Down Expand Up @@ -289,6 +285,7 @@ def first_lt(self, limit):
See first_gt() for further details.
"""
time_index = self.first_lt_index(limit)
self.assert_values()
if time_index >= 0:
return self.__iget(time_index)
else:
Expand Down
64 changes: 61 additions & 3 deletions python/tests/rd_tests/test_sum.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import csv
import datetime
import inspect
import os
import os.path
import shutil
import stat
import datetime

import cwrap
import pandas


def assert_frame_equal(a, b):
Expand All @@ -21,7 +20,6 @@ def assert_frame_equal(a, b):
pass

from contextlib import contextmanager
from unittest import skipIf, skipUnless

from resdata import ResDataType, UnitSystem
from resdata.resfile import FortIO, ResdataFile, ResdataKW, openFortIO
Expand Down Expand Up @@ -179,11 +177,16 @@ def test_identify_var_type(self):
node2 = case.smspec_node("AARQ:10")
self.assertEqual(node2.varType(), SummaryVarType.RD_SMSPEC_AQUIFER_VAR)
self.assertEqual(node2.getNum(), 10)
self.assertFalse(node2.isRate())

node3 = case.smspec_node("RGPT:1")
self.assertEqual(node3.varType(), SummaryVarType.RD_SMSPEC_REGION_VAR)
self.assertEqual(node3.getNum(), 1)
self.assertEqual(node3.default, 0)
self.assertTrue(node3.isTotal())
self.assertFalse(node3.isHistorical())

self.assertEqual(node3.keyword, "RGPT")

self.assertLess(node1, node3)
self.assertGreater(node2, node3)
Expand Down Expand Up @@ -347,6 +350,7 @@ def test_kw_vector(self):
num_mini_step=10,
func_table={"FOPT": fopt, "FOPR": fopr, "FWPT": fgpt},
)
self.assertEqual(case2.get_key_index("FOPR"), 1)

kw_list = SummaryKeyWordVector(case1)
kw_list.add_keyword("FOPT")
Expand Down Expand Up @@ -587,6 +591,11 @@ def test_vector(self):
s1 = sum([x.value for x in v1])
s2 = sum([x.value for x in v2])

def test_wells_and_groups(self):
case = create_case()
self.assertEqual(case.wells(), [])
self.assertEqual(case.groups(), [])

def test_pandas(self):
case = create_case()
dates = (
Expand Down Expand Up @@ -752,3 +761,52 @@ def test_resample_extrapolate(self):
resampled.iget(key_rate, time_index),
rd_sum.get_interp_direct(key_rate, t),
)


def test_t_step():
sum = createSummary(
"CASE",
[
("FOPT", None, 0, "SM3"),
("FOPR", None, 0, "SM3/DAY"),
("FGPT", None, 0, "SM3"),
],
sim_length_days=100,
num_report_step=10,
num_mini_step=10,
sim_start=datetime.date(2010, 1, 1),
func_table={"FOPT": fopt, "FOPR": fopr, "FGPT": fgpt},
)
assert sum.path is None

t_step = sum.add_t_step(11, 101)
assert t_step.get_report() == 11
assert t_step.get_sim_days() == 101
assert t_step.get_mini_step() == 10 * 10
assert sum.iget_days(100) == 101
assert "FOPT" in t_step
assert t_step["FOPT"] == 0.0
t_step["FOPT"] = 100.0
assert t_step["FOPT"] == 100.0
assert t_step.get_sim_time().datetime() == datetime.datetime(2010, 4, 12, 0, 0)

node = sum.get_last("FOPT")
assert node.days == 101
assert sum.get_report(days=101) == 11

assert list(sum.get_interp_vector("FOPT", days_list=[1, 10, 20, 30, 101])) == [
1.0,
10.0,
20.0,
30.0,
100.0,
]

assert sum.get_from_report("FOPT", 11) == 100.0
assert sum.first_gt("FOPT", 10).days == 11
assert sum.first_lt("FOPT", 10).days == 0
assert sum.first_gt_index("FOPT", 10) == 11
assert sum.first_lt_index("FOPT", 10) == 0

assert sum.get_last("FOPT").days == 101.0
assert sum["FOPT"].first.days == 0

0 comments on commit df63a49

Please sign in to comment.