KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
numpy_utils.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Suneth Warnakulasuriya
11 //
12 
13 // System includes
14 #include <numeric>
15 
16 // External includes
17 #include <pybind11/numpy.h>
18 
19 // Project includes
20 
21 namespace Kratos::Python
22 {
23 
24 template <class TDataType>
25 using ContiguousNumpyArray = pybind11::array_t<
26  TDataType,
27  /*column-major*/ pybind11::array::c_style
28 >;
29 
30 template <class TDataType>
31 pybind11::array_t<TDataType> AllocateNumpyArray(
32  const std::size_t NumberOfEntities,
33  const std::vector<std::size_t>& rShape)
34 {
35  const std::size_t size = std::accumulate(rShape.begin(),
36  rShape.end(),
37  1UL,
38  [](std::size_t left, std::size_t right) {return left * right;});
39 
40  TDataType* array = new TDataType[size * NumberOfEntities];
41  pybind11::capsule release(array, [](void* a) {
42  delete[] reinterpret_cast<TDataType*>(a);
43  });
44 
45  // we allocate one additional dimension for the shape to hold the
46  // number of entities. So if the shape within kratos is [2, 3] with 70 entities,
47  // then the final shape of the numpy array will be [70,2,3] so it can keep the
48  // existing shape preserved for each entitiy.
49  std::vector<std::size_t> c_shape(rShape.size() + 1);
50  c_shape[0] = NumberOfEntities;
51  std::copy(rShape.begin(), rShape.end(), c_shape.begin() + 1);
52 
53  // we have to allocate number of bytes to be skipped to reach next element
54  // in each dimension. So this is calculated backwards.
55  std::vector<std::size_t> strides(c_shape.size());
56  std::size_t stride_items = 1;
57  for (int i = c_shape.size() - 1; i >= 0; --i) {
58  strides[i] = sizeof(double) * stride_items;
59  stride_items *= c_shape[i];
60  }
61 
63  c_shape,
64  strides,
65  array,
66  release
67  );
68 }
69 
70 template <class TDataType>
71 pybind11::array_t<TDataType> MakeNumpyArray(
72  TDataType const* pBegin,
73  TDataType const* pEnd,
74  const std::vector<std::size_t>& rShape)
75 {
76  auto array = AllocateNumpyArray<TDataType>(rShape);
77  KRATOS_ERROR_IF_NOT(std::distance(pBegin, pEnd) == array.size()) << "Size mismatch.";
78  std::copy(pBegin,
79  pEnd,
80  array.mutable_data());
81  return array;
82 }
83 
84 } // namespace Kratos::Python
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
Definition: add_distributed_sparse_matrices_to_python.cpp:28
pybind11::array_t< TDataType > MakeNumpyArray(TDataType const *pBegin, TDataType const *pEnd, const std::vector< std::size_t > &rShape)
Definition: numpy_utils.h:71
pybind11::array_t< TDataType, pybind11::array::c_style > ContiguousNumpyArray
Definition: numpy_utils.h:28
pybind11::array_t< TDataType > AllocateNumpyArray(const std::size_t NumberOfEntities, const std::vector< std::size_t > &rShape)
Definition: numpy_utils.h:31
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
left
Definition: exact_hinsberg_test.py:140
a
Definition: generate_stokes_twofluid_element.py:77
integer i
Definition: TensorModule.f:17