Adding python bindings for C++ class ESmry
This commit is contained in:
parent
931fa96a59
commit
f97dfda92e
@ -47,7 +47,7 @@ public:
|
||||
|
||||
int timestepIdxAtReportstepStart(const int reportStep) const;
|
||||
|
||||
int numberOfTimeSteps() const { return param[0].size(); }
|
||||
size_t numberOfTimeSteps() const { return param[0].size(); }
|
||||
|
||||
const std::string& get_unit(const std::string& name) const;
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <opm/io/eclipse/EclFile.hpp>
|
||||
#include <opm/io/eclipse/EclIOdata.hpp>
|
||||
#include <src/opm/io/eclipse/ERst.cpp>
|
||||
#include <src/opm/io/eclipse/ESmry.cpp>
|
||||
|
||||
#include "export.hpp"
|
||||
#include "converters.hpp"
|
||||
@ -124,6 +125,17 @@ npArray get_erst_vector(Opm::EclIO::ERst * file_ptr, const std::string& key, siz
|
||||
}
|
||||
|
||||
|
||||
py::array get_smry_vector(Opm::EclIO::ESmry * file_ptr, const std::string& key)
|
||||
{
|
||||
return convert::numpy_array( file_ptr->get(key) );
|
||||
}
|
||||
|
||||
|
||||
py::array get_smry_vector_at_rsteps(Opm::EclIO::ESmry * file_ptr, const std::string& key)
|
||||
{
|
||||
return convert::numpy_array( file_ptr->get_at_rstep(key) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -162,4 +174,12 @@ void python::common::export_IO(py::module& m) {
|
||||
.def("__get_data", &get_erst_by_index)
|
||||
.def("__get_data", &get_erst_vector);
|
||||
|
||||
py::class_<Opm::EclIO::ESmry>(m, "ESmry")
|
||||
.def(py::init<const std::string &, const bool>(), py::arg("filename"), py::arg("load_base_run") = false)
|
||||
.def("__contains__", &Opm::EclIO::ESmry::hasKey)
|
||||
.def("__len__", &Opm::EclIO::ESmry::numberOfTimeSteps)
|
||||
.def("__get_all", &get_smry_vector)
|
||||
.def("__get_at_rstep", &get_smry_vector_at_rsteps)
|
||||
.def("__get_startdat", &Opm::EclIO::ESmry::get_startdat)
|
||||
.def_property_readonly("list_of_keys", &Opm::EclIO::ESmry::keywordList);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ from .libopmcommon_python import OpmLog
|
||||
from .libopmcommon_python import SummaryConfig
|
||||
from .libopmcommon_python import EclFile, eclArrType
|
||||
from .libopmcommon_python import ERst
|
||||
from .libopmcommon_python import ESmry
|
||||
from .libopmcommon_python import SummaryState
|
||||
|
||||
#from .schedule import Well, Connection, Schedule
|
||||
|
@ -1,10 +1,12 @@
|
||||
from opm._common import eclArrType
|
||||
from opm._common import EclFile
|
||||
from opm._common import ERst
|
||||
from opm._common import ESmry
|
||||
|
||||
import sys
|
||||
import datetime
|
||||
import numpy as np
|
||||
import datetime
|
||||
|
||||
# When extracting the strings from CHAR keywords we get a character array, in
|
||||
# Python this becomes a list of bytes. This desperate monkey-patching is to
|
||||
@ -80,6 +82,40 @@ def contains_erst(self, arg):
|
||||
raise ValueError("expecting tuple (array name , report step number) or \
|
||||
or report step number")
|
||||
|
||||
@property
|
||||
def esmry_start_date(self):
|
||||
|
||||
startd = self.__get_startdat()
|
||||
|
||||
if len(startd) < 6:
|
||||
for n in range(len(startd), 6):
|
||||
startd.append(0)
|
||||
|
||||
|
||||
sec = startd[5] // 1000000
|
||||
microseconds = startd[5] % 1000000
|
||||
|
||||
return datetime.datetime( startd[2], startd[1], startd[0], startd[3],
|
||||
startd[4], sec, microseconds )
|
||||
|
||||
@property
|
||||
def esmry_end_date(self):
|
||||
|
||||
start = self.start_date
|
||||
time = self.__get_all("TIME")
|
||||
|
||||
return start + datetime.timedelta(days = float(time[-1]))
|
||||
|
||||
|
||||
def getitem_esmry(self, arg):
|
||||
|
||||
if isinstance(arg, tuple):
|
||||
if arg[1] == True:
|
||||
return self.__get_at_rstep(arg[0])
|
||||
else:
|
||||
return self.__get_all(arg[0])
|
||||
else:
|
||||
return self.__get_all(arg)
|
||||
|
||||
|
||||
setattr(EclFile, "__getitem__", getitem_eclfile)
|
||||
@ -89,4 +125,6 @@ setattr(ERst, "__contains__", contains_erst)
|
||||
setattr(ERst, "arrays", erst_get_list_of_arrays)
|
||||
setattr(ERst, "__getitem__", getitem_erst)
|
||||
|
||||
|
||||
setattr(ESmry, "start_date", esmry_start_date)
|
||||
setattr(ESmry, "end_date", esmry_end_date)
|
||||
setattr(ESmry, "__getitem__", getitem_esmry)
|
||||
|
BIN
python/tests/data/SPE1CASE1.SMSPEC
Normal file
BIN
python/tests/data/SPE1CASE1.SMSPEC
Normal file
Binary file not shown.
BIN
python/tests/data/SPE1CASE1.UNSMRY
Normal file
BIN
python/tests/data/SPE1CASE1.UNSMRY
Normal file
Binary file not shown.
BIN
python/tests/data/SPE1CASE1_RST60.SMSPEC
Normal file
BIN
python/tests/data/SPE1CASE1_RST60.SMSPEC
Normal file
Binary file not shown.
BIN
python/tests/data/SPE1CASE1_RST60.UNSMRY
Normal file
BIN
python/tests/data/SPE1CASE1_RST60.UNSMRY
Normal file
Binary file not shown.
97
python/tests/test_esmry.py
Executable file
97
python/tests/test_esmry.py
Executable file
@ -0,0 +1,97 @@
|
||||
import unittest
|
||||
import sys
|
||||
import numpy as np
|
||||
import datetime
|
||||
|
||||
from opm.io.ecl import ESmry
|
||||
try:
|
||||
from tests.utils import test_path
|
||||
except ImportError:
|
||||
from utils import test_path
|
||||
|
||||
|
||||
|
||||
class TestEclFile(unittest.TestCase):
|
||||
|
||||
def test_base_runs(self):
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
ESmry("/file/that/does_not_exists")
|
||||
|
||||
smry1 = ESmry(test_path("data/SPE1CASE1.SMSPEC"))
|
||||
|
||||
self.assertEqual(len(smry1), 67)
|
||||
self.assertEqual(smry1.start_date, datetime.datetime(2015,1,1) )
|
||||
self.assertEqual(smry1.end_date, datetime.datetime(2020,4,29) )
|
||||
|
||||
self.assertTrue("TIME" in smry1)
|
||||
self.assertTrue("BPR:10,10,3" in smry1)
|
||||
self.assertFalse("XXXX" in smry1)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
test = smry1["XXX"]
|
||||
|
||||
time1a = smry1["TIME"]
|
||||
|
||||
self.assertEqual(len(time1a), len(smry1))
|
||||
|
||||
time1b = smry1["TIME", True]
|
||||
|
||||
self.assertEqual(len(time1b), 64)
|
||||
|
||||
|
||||
def test_restart_runs(self):
|
||||
|
||||
base_smry = ESmry(test_path("data/SPE1CASE1.SMSPEC"))
|
||||
self.assertEqual(base_smry.start_date, datetime.datetime(2015,1,1) )
|
||||
time0 = base_smry["TIME"]
|
||||
self.assertEqual(time0[0], 1.0)
|
||||
|
||||
rst_smry1 = ESmry(test_path("data/SPE1CASE1_RST60.SMSPEC"))
|
||||
time1 = rst_smry1["TIME"]
|
||||
gor1 = rst_smry1["WGOR:PROD"]
|
||||
|
||||
self.assertEqual(rst_smry1.start_date, datetime.datetime(2015,1,1) )
|
||||
self.assertEqual(len(rst_smry1), 60)
|
||||
self.assertEqual(time1[0], 1856.0)
|
||||
self.assertEqual(len(time1), 60)
|
||||
self.assertEqual(len(gor1), 60)
|
||||
|
||||
rst_smry2 = ESmry(test_path("data/SPE1CASE1_RST60.SMSPEC"), load_base_run = True)
|
||||
time2 = rst_smry2["TIME"]
|
||||
gor2 = rst_smry2["WGOR:PROD"]
|
||||
|
||||
self.assertEqual(rst_smry2.start_date, datetime.datetime(2015,1,1) )
|
||||
self.assertEqual(len(rst_smry2), 123)
|
||||
|
||||
self.assertEqual(time2[0], 1.0)
|
||||
self.assertEqual(len(rst_smry2), 123)
|
||||
self.assertEqual(len(time2), 123)
|
||||
self.assertEqual(len(gor2), 123)
|
||||
|
||||
|
||||
def test_keylist(self):
|
||||
|
||||
ref_key_list = ["BPR:1,1,1", "BPR:10,10,3", "FGOR", "FOPR", "TIME", "WBHP:INJ", "WBHP:PROD",
|
||||
"WGIR:INJ", "WGIR:PROD", "WGIT:INJ", "WGIT:PROD", "WGOR:PROD", "WGPR:INJ",
|
||||
"WGPR:PROD", "WGPT:INJ", "WGPT:PROD", "WOIR:INJ", "WOIR:PROD", "WOIT:INJ",
|
||||
"WOIT:PROD", "WOPR:INJ", "WOPR:PROD", "WOPT:INJ", "WOPT:PROD", "WWIR:INJ",
|
||||
"WWIR:PROD", "WWIT:INJ", "WWIT:PROD", "WWPR:INJ", "WWPR:PROD", "WWPT:INJ",
|
||||
"WWPT:PROD"]
|
||||
|
||||
smry1 = ESmry(test_path("data/SPE1CASE1.SMSPEC"))
|
||||
|
||||
self.assertEqual(len(smry1.list_of_keys), len(ref_key_list))
|
||||
|
||||
for key, ref_key in zip(smry1.list_of_keys, ref_key_list):
|
||||
self.assertEqual(key, ref_key)
|
||||
|
||||
for key in smry1.list_of_keys:
|
||||
data = smry1[key]
|
||||
self.assertEqual(len(smry1), len(data))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user