DeckKeyword w/ vectors: takes unit system as arg.

DeckKeyword w/ vector: added unit as arg.

DeckKeyword vector works with units.

DecValuetests: use zcorn instead of poro.

python: deckkweyword works with int data.

...

python deckkeyword. cant init with double array.

moved numpy_array converters to converters.hpp.
This commit is contained in:
Steinar Foss
2019-10-24 13:00:06 +02:00
parent 0421173686
commit ce3781775f
6 changed files with 113 additions and 36 deletions

View File

@@ -3,6 +3,7 @@
#include <sstream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
@@ -20,4 +21,28 @@ std::string str( const T& t ) {
return stream.str();
}
template <class T>
std::vector<T> numpy_array_to_vector(py::array_t<T>& input) {
T * input_ptr = (T *) input.request().ptr;
std::vector<T> output(input.size());
for (int i = 0; i < input.size(); i++)
output[i] = input_ptr[i];
return output;
}
template <class T>
py::array_t<T> vector_to_numpy_array(const std::vector<T>& input) {
auto output = py::array_t<T>(input.size());
T * py_array_ptr = (T*)output.request().ptr;
for (size_t i = 0; i < input.size(); i++)
py_array_ptr[i] = input[i];
return output;
}
#endif //SUNBEAM_CONVERTERS_HPP

View File

@@ -97,6 +97,19 @@ void push_string_as_deck_value(std::vector<DeckValue>& record, const std::string
}
py::array_t<int> get_int_array(const DeckKeyword& kw) {
return vector_to_numpy_array( kw.getIntData() );
}
py::array_t<double> get_raw_array(const DeckKeyword& kw) {
return vector_to_numpy_array( kw.getRawDoubleData() );
}
py::array_t<double> get_SI_array(const DeckKeyword& kw) {
return vector_to_numpy_array( kw.getSIDoubleData() );
}
}
void python::common::export_DeckKeyword(py::module& module) {
@@ -148,7 +161,19 @@ void python::common::export_DeckKeyword(py::module& module) {
.def( "__getitem__", getRecord, ref_internal)
.def( "__len__", &DeckKeyword::size )
.def_property_readonly("name", &DeckKeyword::name )
;
.def(py::init([](const ParserKeyword& parser_keyword, py::array_t<int> py_data) {
return DeckKeyword(parser_keyword, numpy_array_to_vector(py_data));
} ) )
.def(py::init([](const ParserKeyword& parser_keyword, py::array_t<double> py_data, UnitSystem& active_system, UnitSystem& default_system) {
return DeckKeyword(parser_keyword, numpy_array_to_vector(py_data), active_system, default_system);
} ) )
.def("get_int_array", &get_int_array)
.def("get_raw_array", &get_raw_array)
.def("get_SI_array", &get_SI_array)
;
py::class_< DeckRecord >( module, "DeckRecord")

View File

@@ -2,6 +2,8 @@ import unittest
import os.path
import sys
import numpy as np
from opm.io.parser import Parser
from opm.io.parser import ParseContext
from opm.io.deck import DeckKeyword
@@ -52,7 +54,7 @@ FIPNUM
deck = parser.parse_string(string)
deck = parser.parse_string(string, context)
def test_create_deck_kw(self):
def test_deck_kw_records(self):
parser = Parser()
deck = parser.parse_string(self.REGIONDATA)
active_unit_system = deck.active_unit_system()
@@ -127,6 +129,26 @@ FIPNUM
raise DeckKeyword(parser["AQANTRC"], [["1*2.2", "ABC", 8]], active_unit_system, default_unit_system)
def test_deck_kw_vector(self):
parser = Parser()
deck = parser.parse_string(self.REGIONDATA)
active_unit_system = deck.active_unit_system()
default_unit_system = deck.default_unit_system()
self.assertEqual(active_unit_system.name, "Field")
int_array = np.array([0, 1, 2, 3])
hbnum_kw = DeckKeyword( parser["HBNUM"], int_array)
assert( np.array_equal(hbnum_kw.get_int_array(), int_array) )
raw_array = np.array([1.1, 2.2, 3.3])
zcorn_kw = DeckKeyword( parser["ZCORN"], raw_array, active_unit_system, default_unit_system)
assert( np.array_equal(zcorn_kw.get_raw_array(), raw_array) )
si_array = zcorn_kw.get_SI_array()
self.assertAlmostEqual( si_array[0], 1.1 * unit_foot )
self.assertAlmostEqual( si_array[2], 3.3 * unit_foot )
if __name__ == "__main__":
unittest.main()