8#include <pybind11/numpy.h>
9#include <pybind11/pybind11.h>
19namespace py = pybind11;
30py::array_t<double> stlVectorToNumpyArray_Scalar(
const std::vector<double>& stl_vector,
const std::vector<std::array<unsigned int, 3>>& indices) {
31 if (indices.empty() || stl_vector.empty())
32 throw std::runtime_error(
"One or more input vector is empty");
34 std::vector<unsigned int> shape{(indices.back()[0]) + 1, (indices.back()[1]) + 1, (indices.back()[2]) + 1};
35 py::array_t<double> res = py::array_t<double>(shape);
36 auto res_unchecked = res.mutable_unchecked<3>();
39 for (
unsigned int i = 0; i < shape[0]; i++)
40 for (
unsigned int j = 0; j < shape[1]; j++)
41 for (
unsigned int k = 0; k < shape[2]; k++) {
42 res_unchecked(i, j, k) = stl_vector[c];
50py::array_t<double> stlVectorToNumpyArray_Vector(
const std::vector<std::array<double, 3>>& stl_vector,
51 const std::vector<std::array<unsigned int, 3>>& indices) {
52 if (indices.empty() || stl_vector.empty())
53 throw std::runtime_error(
"One or more input vector is empty");
55 std::vector<unsigned int> shape({indices.back()[0] + 1, indices.back()[1] + 1, indices.back()[2] + 1, 3});
56 py::array_t<double> res = py::array_t<double>(shape);
57 auto res_unchecked = res.mutable_unchecked<4>();
60 for (
unsigned int i = 0; i < shape[0]; i++)
61 for (
unsigned int j = 0; j < shape[1]; j++)
62 for (
unsigned int k = 0; k < shape[2]; k++) {
63 res_unchecked(i, j, k, 0) = stl_vector[c][0];
64 res_unchecked(i, j, k, 1) = stl_vector[c][1];
65 res_unchecked(i, j, k, 2) = stl_vector[c][2];
74std::vector<double> numpyArrayToStlVector_Scalar(
const py::array_t<double>& numpy_array) {
75 if (numpy_array.ndim() != 3)
76 throw std::runtime_error(
"Input array must be of exactly 3 dimensions.");
78 std::vector<double> res;
79 auto np_unchecked = numpy_array.unchecked<3>();
81 for (
unsigned int i = 0; i < numpy_array.shape(0); i++)
82 for (
unsigned int j = 0; j < numpy_array.shape(1); j++)
83 for (
unsigned int k = 0; k < numpy_array.shape(2); k++)
84 res.push_back(np_unchecked(i, j, k));
90std::vector<std::array<double, 3>> numpyArrayToStlVector_Vector(
const py::array_t<double>& numpy_array) {
91 if (numpy_array.ndim() != 4)
92 throw std::runtime_error(
"Input array must be of exactly 4 dimensions.");
93 if (numpy_array.shape(3) != 3)
94 throw std::runtime_error(
"Input array's 4th dimension must be {0..2}.");
96 std::vector<std::array<double, 3>> res;
97 auto np_unchecked = numpy_array.unchecked<4>();
99 for (
unsigned int i = 0; i < numpy_array.shape(0); i++)
100 for (
unsigned int j = 0; j < numpy_array.shape(1); j++)
101 for (
unsigned int k = 0; k < numpy_array.shape(2); k++) {
102 std::array<double, 3> vec = {np_unchecked(i, j, k, 0), np_unchecked(i, j, k, 1), np_unchecked(i, j, k, 2)};
110std::function<std::vector<double>(std::vector<double> , std::vector<std::array<unsigned int, 3>> )>*
111functionWrapper_Scalar(std::function<py::array_t<double>(py::array_t<double>)>* py_func_ptr) {
115 auto py_func = *py_func_ptr;
116 return new std::function<std::vector<double>(std::vector<double> , std::vector<std::array<unsigned int, 3>> )>{
117 [py_func](std::vector<double> stl_vector, std::vector<std::array<unsigned int, 3>> indices) {
118 py::array_t<double> np_input = conversion::stlVectorToNumpyArray_Scalar(stl_vector, indices);
119 py::array_t<double> np_output = py_func(np_input);
120 return conversion::numpyArrayToStlVector_Scalar(np_output);
125 return new std::function<std::vector<double>(std::vector<double> , std::vector<std::array<unsigned int, 3>> )>{
126 [](std::vector<double> stl_vector, std::vector<std::array<unsigned int, 3>> indices) {
return stl_vector; }};
130std::function<std::vector<std::array<double, 3>>(std::vector<std::array<double, 3>> , std::vector<std::array<unsigned int, 3>> )>*
131functionWrapper_Vector(std::function<py::array_t<double>(py::array_t<double>)>* py_func_ptr) {
135 auto py_func = *py_func_ptr;
136 return new std::function<std::vector<std::array<double, 3>>(std::vector<std::array<double, 3>> ,
137 std::vector<std::array<unsigned int, 3>> )>{
138 [py_func](std::vector<std::array<double, 3>> stl_vector, std::vector<std::array<unsigned int, 3>> indices) {
139 auto np_input = conversion::stlVectorToNumpyArray_Vector(stl_vector, indices);
140 auto np_output = py_func(np_input);
141 return conversion::numpyArrayToStlVector_Vector(np_output);
146 return new std::function<std::vector<std::array<double, 3>>(std::vector<std::array<double, 3>> ,
147 std::vector<std::array<unsigned int, 3>> )>{
148 [](std::vector<std::array<double, 3>> stl_vector, std::vector<std::array<unsigned int, 3>> indices) {
return stl_vector; }};
everything necessary for coupling operations, is defined in here
Definition AdditiveMomentumInsertion.h:15