Merge pull request #1164 from stefoss23/deckkeyword_vector_unit
DeckKeyword w/ vectors: takes numpy vectors.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <sstream>
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/numpy.h>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
@@ -20,4 +21,31 @@ std::string str( const T& t ) {
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
namespace convert {
|
||||
|
||||
template <class T>
|
||||
std::vector<T> 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> 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
|
||||
|
||||
@@ -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 convert::numpy_array( kw.getIntData() );
|
||||
}
|
||||
|
||||
py::array_t<double> get_raw_array(const DeckKeyword& kw) {
|
||||
return convert::numpy_array( kw.getRawDoubleData() );
|
||||
}
|
||||
|
||||
py::array_t<double> get_SI_array(const DeckKeyword& kw) {
|
||||
return convert::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, convert::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, convert::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")
|
||||
|
||||
@@ -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
|
||||
@@ -53,7 +55,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()
|
||||
@@ -128,6 +130,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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user