pyAMReX
ParticleTile.H
Go to the documentation of this file.
1 /* Copyright 2022 The AMReX Community
2  *
3  * Authors: Ryan Sandberg, Axel Huebl
4  * License: BSD-3-Clause-LBNL
5  */
6 #pragma once
7 
8 #include "pyAMReX.H"
9 
10 #include <AMReX_BoxArray.H>
11 #include <AMReX_GpuAllocators.H>
12 #include <AMReX_IntVect.H>
13 #include <AMReX_ParticleTile.H>
14 #include <AMReX_Particle.H>
15 
16 #include <sstream>
17 
18 
19 template <typename T_ParticleType, int NArrayReal, int NArrayInt>
20 void make_ParticleTileData(py::module &m)
21 {
22  using namespace amrex;
23 
24  using ParticleType = T_ParticleType;
25  constexpr int NStructReal = ParticleType::NReal;
26  constexpr int NStructInt = ParticleType::NInt;
27 
30 
31  auto particle_tile_data_type = std::string("ParticleTileData_");
32  if (T_ParticleType::is_soa_particle) {
33  particle_tile_data_type += "pureSoA_";
34  }
35  else {
36  particle_tile_data_type +=
37  std::to_string(NStructReal) + "_" +
38  std::to_string(NStructInt) + "_";
39  }
40  particle_tile_data_type +=
41  std::to_string(NArrayReal) + "_" +
42  std::to_string(NArrayInt);
43 
44  py::class_<ParticleTileDataType>(m, particle_tile_data_type.c_str())
45  .def(py::init())
46 
47  .def_property_readonly("m_size", [](ParticleTileDataType const & ptd){ return ptd.m_size; })
48  .def_property_readonly("m_num_runtime_real", [](ParticleTileDataType const & ptd){ return ptd.m_num_runtime_real; })
49  .def_property_readonly("m_num_runtime_int", [](ParticleTileDataType const & ptd){ return ptd.m_num_runtime_int; })
50 
51  .def("get_super_particle", &ParticleTileDataType::template getSuperParticle<ParticleType>)
52  .def("set_super_particle", &ParticleTileDataType::template setSuperParticle<ParticleType>)
53  // setter & getter
54  .def("__setitem__", [](ParticleTileDataType &pdt, int const v,
55  SuperParticleType const value) { pdt.setSuperParticle(value, v); })
56  .def("__getitem__",
57  [](ParticleTileDataType &pdt, int const v) { return pdt.getSuperParticle(v); })
58 
59  ;
60 }
61 
62 template <typename T_ParticleType, int NArrayReal, int NArrayInt,
63  template<class> class Allocator=amrex::DefaultAllocator>
64 void make_ParticleTile(py::module &m, std::string allocstr)
65 {
66  using namespace amrex;
67 
68  using ParticleType = T_ParticleType;
69  constexpr int NStructReal = ParticleType::NReal;
70  constexpr int NStructInt = ParticleType::NInt;
71 
74 
75  auto particle_tile_type = std::string("ParticleTile_");
76  if (T_ParticleType::is_soa_particle) {
77  particle_tile_type += "pureSoA_";
78  }
79  else {
80  particle_tile_type +=
81  std::to_string(NStructReal) + "_" +
82  std::to_string(NStructInt) + "_";
83  }
84  particle_tile_type += std::to_string(NArrayReal) + "_" +
85  std::to_string(NArrayInt) + "_" + allocstr;
86 
87  auto py_particle_tile = py::class_<ParticleTileType>(m, particle_tile_type.c_str())
88  .def(py::init())
89  .def_readonly_static("NAR", &ParticleTileType::NAR)
90  .def_readonly_static("NAI", &ParticleTileType::NAI)
91  .def("define", &ParticleTileType::define)
92  .def("get_struct_of_arrays", py::overload_cast<>(&ParticleTileType::GetStructOfArrays),
93  py::return_value_policy::reference_internal)
94 
95  .def_property_readonly("empty", &ParticleTileType::empty)
96  .def_property_readonly("size", &ParticleTileType::size)
97  .def_property_readonly("num_particles", &ParticleTileType::numParticles)
98  .def_property_readonly("num_real_particles", &ParticleTileType::numRealParticles)
99  .def_property_readonly("num_neighbor_particles", &ParticleTileType::numNeighborParticles)
100  .def_property_readonly("num_total_particles", &ParticleTileType::numTotalParticles)
101 
102  .def("set_num_neighbors", &ParticleTileType::setNumNeighbors)
103  .def("get_num_neighbors", &ParticleTileType::getNumNeighbors)
104  .def("resize", &ParticleTileType::resize)
105  ;
106 
107  if constexpr (!T_ParticleType::is_soa_particle) {
108  py_particle_tile
109  .def("push_back",
110  [](ParticleTileType& ptile, const ParticleType &p) { ptile.push_back(p); },
111  "Add one particle to this tile.")
112  ;
113  }
114 
115  py_particle_tile
116  .def("push_back",
117  [](ParticleTileType& ptile, const SuperParticleType &p) {ptile.push_back(p);},
118  "Add one particle to this tile.")
119  .def("push_back_real", [](ParticleTileType& ptile, int comp, ParticleReal v) {ptile.push_back_real(comp, v);})
120  .def("push_back_real", [](ParticleTileType& ptile,
121  const std::array<ParticleReal, NArrayReal>& v) {ptile.push_back_real(v);})
122  .def("push_back_real", [](ParticleTileType&ptile,
123  int comp,
124  std::size_t npar,
125  ParticleReal v)
126  {ptile.push_back_real(comp, npar, v);})
127  .def("push_back_int", [](ParticleTileType& ptile, int comp, int v) {ptile.push_back_int(comp, v);})
128  .def("push_back_int", [](ParticleTileType& ptile,
129  const std::array<int, NArrayInt>& v) {ptile.push_back_int(v);})
130  .def("push_back_int", [](ParticleTileType&ptile,
131  int comp,
132  std::size_t npar,
133  int v)
134  {ptile.push_back_int(comp, npar, v);})
135 
136  .def_property_readonly("num_real_comps", &ParticleTileType::NumRealComps)
137  .def_property_readonly("num_int_comps", &ParticleTileType::NumIntComps)
138  .def_property_readonly("num_runtime_real_comps", &ParticleTileType::NumRuntimeRealComps)
139  .def_property_readonly("num_runtime_int_comps", &ParticleTileType::NumRuntimeIntComps)
140 
141  .def("shrink_to_fit", &ParticleTileType::shrink_to_fit)
142  .def("capacity", &ParticleTileType::capacity)
143  .def("swap",&ParticleTileType::swap)
144  .def("get_particle_tile_data", &ParticleTileType::getParticleTileData)
145  .def("__setitem__", [](ParticleTileType & pt, int const v, SuperParticleType const value){ pt.getParticleTileData().setSuperParticle( value, v); })
146  .def("__getitem__", [](ParticleTileType & pt, int const v){ return pt.getParticleTileData().getSuperParticle(v); })
147  ;
148 
149  if constexpr (!T_ParticleType::is_soa_particle) {
150  py_particle_tile
151  .def("get_array_of_structs",
152  py::overload_cast<>(&ParticleTileType::GetArrayOfStructs),
153  py::return_value_policy::reference_internal)
154  ;
155  }
156 }
157 
158 template <typename T_ParticleType, int NArrayReal, int NArrayInt>
159 void make_ParticleTile(py::module &m)
160 {
161  if constexpr (T_ParticleType::is_soa_particle) {
162  make_ParticleTileData<amrex::SoAParticleBase, NArrayReal, NArrayInt>(m);
163  }
164  else {
165  make_ParticleTileData<T_ParticleType, NArrayReal, NArrayInt>(m);
166  }
167 
168  // first, because used as copy target in methods in containers with other allocators
169  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
170  amrex::PinnedArenaAllocator>(m, "pinned");
171 
172  // see Src/Base/AMReX_GpuContainers.H
173  // !AMREX_USE_GPU: DefaultAllocator = std::allocator
174  // AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator
175 
176  // work-around for https://github.com/pybind/pybind11/pull/4581
177  //make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
178  // std::allocator>(m, "std");
179  //make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
180  // amrex::ArenaAllocator>(m, "arena");
181 #ifdef AMREX_USE_GPU
182  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
183  std::allocator>(m, "std");
184  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
185  amrex::DefaultAllocator>(m, "default"); // amrex::ArenaAllocator
186 #else
187  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
188  amrex::DefaultAllocator>(m, "default"); // std::allocator
189  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
190  amrex::ArenaAllocator>(m, "arena");
191 #endif
192  // end work-around
193 #ifdef AMREX_USE_GPU
194  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
195  amrex::DeviceArenaAllocator>(m, "device");
196  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
197  amrex::ManagedArenaAllocator>(m, "managed");
198  make_ParticleTile<T_ParticleType, NArrayReal, NArrayInt,
199  amrex::AsyncArenaAllocator>(m, "async");
200 #endif
201 }
void make_ParticleTileData(py::module &m)
Definition: ParticleTile.H:20
void make_ParticleTile(py::module &m, std::string allocstr)
Definition: ParticleTile.H:64