| 89 | |
| 90 | template<class TDataType> |
| 91 | void AddNDData( |
| 92 | pybind11::module& m, |
| 93 | const std::string& rName) |
| 94 | { |
| 95 | py::class_<NDData<TDataType>, typename NDData<TDataType>::Pointer>(m, rName.c_str()) |
| 96 | .def(py::init<const DenseVector<unsigned int>&>(), py::arg("shape")) |
| 97 | .def(py::init<const DenseVector<unsigned int>&, const TDataType>(), py::arg("shape"), py::arg("value")) |
| 98 | .def(py::init<const NDData<TDataType>&>(), py::arg("other")) |
| 99 | .def(py::init([](py::array& rArray, const bool Copy){ |
| 100 | KRATOS_TRY |
| 101 | |
| 102 | KRATOS_ERROR_IF_NOT(rArray.flags() & pybind11::detail::npy_api::constants::NPY_ARRAY_C_CONTIGUOUS_) |
| 103 | << "Only supports C-style (row-major) arrays from numpy."; |
| 104 | |
| 105 | DenseVector<unsigned int> shape(rArray.ndim()); |
| 106 | std::copy(rArray.shape(), rArray.shape() + rArray.ndim(), shape.data().begin()); |
| 107 | |
| 108 | if (!Copy) { |
| 109 | KRATOS_ERROR_IF_NOT(pybind11::isinstance<pybind11::array_t<TDataType>>(rArray)) |
| 110 | << "The numpy array needs to be of the same type as internal data to use copy = false [ passed numpy array's dtype = " |
| 111 | << rArray.dtype() << " ]."; |
| 112 | auto casted_array = rArray.cast<pybind11::array_t<TDataType, pybind11::array::c_style>>(); |
| 113 | return Kratos::make_shared<NDData<TDataType>>(casted_array.mutable_data(), shape, Copy); |
| 114 | } else { |
| 115 | auto p_dda = Kratos::make_shared<NDData<TDataType>>(shape); |
| 116 | if (!AssignData< |
| 117 | TDataType, |
| 118 | bool, |
| 119 | std::uint8_t, |
| 120 | std::uint16_t, |
| 121 | std::uint32_t, |
| 122 | std::uint64_t, |
| 123 | std::int8_t, |
| 124 | std::int16_t, |
| 125 | std::int32_t, |
| 126 | std::int64_t, |
| 127 | float, |
| 128 | double, |
| 129 | long double>(*p_dda, rArray)) |
| 130 | { |
| 131 | KRATOS_ERROR |
| 132 | << "NDData cannot be assigned an numpy array with \"" |
| 133 | << rArray.dtype() << "\". They can be only set with numpy arrays having following dtypes:" |
| 134 | << "\n\t numpy.bool" |
| 135 | << "\n\t numpy.uint8" |
| 136 | << "\n\t numpy.uint16" |
| 137 | << "\n\t numpy.uint32" |
| 138 | << "\n\t numpy.uint64" |
| 139 | << "\n\t numpy.int8" |
| 140 | << "\n\t numpy.int16" |
| 141 | << "\n\t numpy.int32" |
| 142 | << "\n\t numpy.int64" |
| 143 | << "\n\t numpy.float32" |
| 144 | << "\n\t numpy.float64" |
| 145 | << "\n\t numpy.float128"; |
| 146 | } |
| 147 | return p_dda; |
| 148 | } |