deal.II version GIT relicensing-2250-g88cb8ba3cb 2024-12-13 12:20:00+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
particle_handler.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2017 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_particles_particle_handler_h
16#define dealii_particles_particle_handler_h
17
18#include <deal.II/base/config.h>
19
25
27
28#include <deal.II/fe/mapping.h>
29
31
36
37#include <boost/range/iterator_range.hpp>
38#include <boost/serialization/map.hpp>
39
41
42namespace Particles
43{
61 template <int dim, int spacedim = dim>
63 {
64 public:
69
73 using particle_iterator_range = boost::iterator_range<particle_iterator>;
74
80
85
96 const unsigned int n_properties = 0);
97
101 virtual ~ParticleHandler();
102
108 void
111 const unsigned int n_properties = 0);
112
133 void
135
139 void
140 clear();
141
147 void
149
164 void
165 reserve(const std::size_t n_particles);
166
175 void
177
182 begin() const;
183
188 begin();
189
194 end() const;
195
200 end();
201
206 begin_ghost() const;
207
212 begin_ghost();
213
218 end_ghost() const;
219
224 end_ghost();
225
232 const;
233
245
257 const;
258
264 void
266
272 void
273 remove_particles(const std::vector<particle_iterator> &particles);
274
285
303 const Point<spacedim> &position,
305 const types::particle_index particle_index,
307 const ArrayView<const double> &properties = {});
308
315 void
317 const std::multimap<
320
330 void
331 insert_particles(const std::vector<Point<spacedim>> &positions);
332
393 std::map<unsigned int, IndexSet>
395 const std::vector<Point<spacedim>> &positions,
396 const std::vector<std::vector<BoundingBox<spacedim>>>
398 const std::vector<std::vector<double>> &properties = {},
399 const std::vector<types::particle_index> &ids = {});
400
431 std::map<unsigned int, IndexSet>
433 const std::vector<Particle<dim, spacedim>> &particles,
434 const std::vector<std::vector<BoundingBox<spacedim>>>
436
480 template <typename VectorType>
481 std::enable_if_t<
482 std::is_convertible_v<VectorType *, Function<spacedim> *> == false>
483 set_particle_positions(const VectorType &input_vector,
484 const bool displace_particles = true);
485
506 void
508 const bool displace_particles = true);
509
510
529 void
531 const bool displace_particles = true);
532
560 template <typename VectorType>
561 void
563 const bool add_to_output_vector = false);
564
579 void
581 const bool add_to_output_vector = false);
582
609 void
611 const std::function<std::size_t()> &size_callback,
612 const std::function<void *(const particle_iterator &, void *)>
614 const std::function<const void *(const particle_iterator &, const void *)>
616
626 n_global_particles() const;
627
636
643
651
670
674 unsigned int
676
692
698 get_property_pool() const;
699
714 void
716
722 void
724
732 void
734
744 void
746
765 void
767
779 void
781
790 void
791 deserialize();
792
799 template <class Archive>
800 void
801 serialize(Archive &ar, const unsigned int version);
802
815 struct Signals
816 {
830 boost::signals2::signal<void(
833 &cell)>
835 };
836
842
843 private:
852 const void *&data,
854
864
874 void
876
883
890
899 std::unique_ptr<PropertyPool<dim, spacedim>> property_pool;
900
905
911 const typename particle_container::iterator owned_particles_end;
912
918 std::vector<typename particle_container::iterator> cells_to_particle_cache;
919
925
931
941
947
959 std::function<std::size_t()> size_callback;
960
971 std::function<void *(const particle_iterator &, void *)> store_callback;
972
983 std::function<const void *(const particle_iterator &, const void *)>
985
993
997 const double tolerance_inside_cell = 1e-12;
998
1008 std::unique_ptr<GridTools::Cache<dim, spacedim>> triangulation_cache;
1009
1010#ifdef DEAL_II_WITH_MPI
1034 void
1036 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1038 const std::map<
1040 std::vector<
1042 &new_cells_for_particles = std::map<
1044 std::vector<
1046 const bool enable_cache = false);
1047
1059 void
1061 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1063
1064#endif
1065
1073
1078 void
1080
1085 std::vector<boost::signals2::connection> tria_listeners;
1086
1095 void
1097
1103 void
1105
1113 void
1115
1121 std::vector<char>
1124 const CellStatus status) const;
1125
1130 void
1133 const CellStatus status,
1134 const boost::iterator_range<std::vector<char>::const_iterator>
1135 &data_range);
1136
1141 typename particle_container::iterator
1143
1148 typename particle_container::iterator
1150
1155 typename particle_container::iterator
1157
1162 typename particle_container::iterator
1164 };
1165
1166
1167
1168 /* ---------------------- inline and template functions ------------------
1169 */
1170
1171 template <int dim, int spacedim>
1174 {
1175 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->begin();
1176 }
1177
1178
1179
1180 template <int dim, int spacedim>
1183 {
1184 return particle_iterator(particle_container_owned_begin(),
1185 *property_pool,
1186 0);
1187 }
1188
1189
1190
1191 template <int dim, int spacedim>
1194 {
1195 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->end();
1196 }
1197
1198
1199
1200 template <int dim, int spacedim>
1203 {
1204 return particle_iterator(particle_container_owned_end(), *property_pool, 0);
1205 }
1206
1207
1208
1209 template <int dim, int spacedim>
1212 {
1213 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->begin_ghost();
1214 }
1215
1216
1217
1218 template <int dim, int spacedim>
1221 {
1222 return particle_iterator(particle_container_ghost_begin(),
1223 *property_pool,
1224 0);
1225 }
1226
1227
1228
1229 template <int dim, int spacedim>
1232 {
1233 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->end_ghost();
1234 }
1235
1236
1237
1238 template <int dim, int spacedim>
1241 {
1242 return particle_iterator(particle_container_ghost_end(), *property_pool, 0);
1243 }
1244
1245
1246
1247 template <int dim, int spacedim>
1250 {
1251 // We should always have at least the three anchor entries in the list of
1252 // particles
1253 Assert(!particles.empty(), ExcInternalError());
1254 typename particle_container::iterator begin =
1255 const_cast<particle_container &>(particles).begin();
1256 return ++begin;
1257 }
1258
1259
1260
1261 template <int dim, int spacedim>
1264 {
1265 Assert(!particles.empty(), ExcInternalError());
1266 return owned_particles_end;
1267 }
1268
1269
1270
1271 template <int dim, int spacedim>
1274 {
1275 Assert(!particles.empty(), ExcInternalError());
1276 typename particle_container::iterator begin = owned_particles_end;
1277 return ++begin;
1278 }
1279
1280
1281
1282 template <int dim, int spacedim>
1285 {
1286 Assert(!particles.empty(), ExcInternalError());
1287 typename particle_container::iterator end =
1288 const_cast<particle_container &>(particles).end();
1289 return --end;
1290 }
1291
1292
1293
1294 template <int dim, int spacedim>
1295 template <class Archive>
1296 inline void
1298 {
1299 // Note that we do not serialize the particle data itself (i.e., the
1300 // 'particles' member variable). Instead we
1301 // use the serialization functionality of the triangulation class, because
1302 // this guarantees that data is immediately shipped to new processes if
1303 // the domain is distributed differently after resuming from a checkpoint.
1304 //
1305 // See @ref step_83 "step-83" for how to serialize ParticleHandler objects.
1306 ar &global_number_of_particles &global_max_particles_per_cell
1307 &next_free_particle_index;
1308 }
1309
1310
1311
1312 template <int dim, int spacedim>
1313 template <typename VectorType>
1314 inline std::enable_if_t<
1315 std::is_convertible_v<VectorType *, Function<spacedim> *> == false>
1317 const VectorType &input_vector,
1318 const bool displace_particles)
1319 {
1321 get_next_free_particle_index() * spacedim);
1322 for (auto &p : *this)
1323 {
1324 Point<spacedim> &position = p.get_location();
1325 const auto id = p.get_id();
1326
1328 for (unsigned int i = 0; i < spacedim; ++i)
1329 position[i] += input_vector[id * spacedim + i];
1330 else
1331 for (unsigned int i = 0; i < spacedim; ++i)
1332 position[i] = input_vector[id * spacedim + i];
1333 }
1334 sort_particles_into_subdomains_and_cells();
1335 }
1336
1337
1338
1339 template <int dim, int spacedim>
1340 template <typename VectorType>
1341 inline void
1343 VectorType &output_vector,
1344 const bool add_to_output_vector)
1345 {
1347 get_next_free_particle_index() * spacedim);
1348 for (const auto &p : *this)
1349 {
1350 auto point = p.get_location();
1351 const auto id = p.get_id();
1353 for (unsigned int i = 0; i < spacedim; ++i)
1354 output_vector[id * spacedim + i] += point[i];
1355 else
1356 for (unsigned int i = 0; i < spacedim; ++i)
1357 output_vector[id * spacedim + i] = point[i];
1358 }
1361 else
1363 }
1364
1365} // namespace Particles
1366
1368
1369#endif
CellStatus
Definition cell_status.h:31
std::list< ParticlesInCell > particle_container
std::function< void *(const particle_iterator &, void *)> store_callback
void register_additional_store_load_functions(const std::function< std::size_t()> &size_callback, const std::function< void *(const particle_iterator &, void *)> &store_callback, const std::function< const void *(const particle_iterator &, const void *)> &load_callback)
void exchange_ghost_particles(const bool enable_ghost_cache=false)
types::particle_index n_global_particles() const
particle_iterator end_ghost() const
unsigned int global_max_particles_per_cell
internal::GhostParticlePartitioner< dim, spacedim > ghost_particles_cache
particle_container::iterator particle_container_ghost_begin() const
unsigned int n_properties_per_particle() const
types::particle_index global_number_of_particles
void get_particle_positions(VectorType &output_vector, const bool add_to_output_vector=false)
particle_container::iterator particle_container_owned_end() const
particle_container::iterator particle_container_ghost_end() const
boost::iterator_range< particle_iterator > particle_iterator_range
void send_recv_particles(const std::map< types::subdomain_id, std::vector< particle_iterator > > &particles_to_send, const std::map< types::subdomain_id, std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > > &new_cells_for_particles=std::map< types::subdomain_id, std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > >(), const bool enable_cache=false)
ParticleIterator< dim, spacedim > particle_iterator
ObserverPointer< const Mapping< dim, spacedim >, ParticleHandler< dim, spacedim > > mapping
void send_recv_particles_properties_and_location(const std::map< types::subdomain_id, std::vector< particle_iterator > > &particles_to_send)
types::particle_index number_of_locally_owned_particles
particle_iterator begin_ghost() const
PropertyPool< dim, spacedim > & get_property_pool() const
void initialize(const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping, const unsigned int n_properties=0)
void serialize(Archive &ar, const unsigned int version)
std::unique_ptr< PropertyPool< dim, spacedim > > property_pool
types::particle_index get_max_local_particle_index() const
void reserve(const std::size_t n_particles)
particle_iterator begin() const
std::map< unsigned int, IndexSet > insert_global_particles(const std::vector< Point< spacedim > > &positions, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bounding_boxes, const std::vector< std::vector< double > > &properties={}, const std::vector< types::particle_index > &ids={})
void notify_ready_to_unpack(const bool serialization)
std::enable_if_t< std::is_convertible_v< VectorType *, Function< spacedim > * >==false > set_particle_positions(const VectorType &input_vector, const bool displace_particles=true)
std::function< std::size_t()> size_callback
std::vector< char > pack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status) const
void insert_particles(const std::multimap< typename Triangulation< dim, spacedim >::active_cell_iterator, Particle< dim, spacedim > > &particles)
types::particle_index next_free_particle_index
const particle_container::iterator owned_particles_end
particle_iterator end() const
void remove_particle(const particle_iterator &particle)
types::particle_index n_locally_owned_particles() const
void reset_particle_container(particle_container &particles)
types::particle_index n_particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell) const
typename ParticleAccessor< dim, spacedim >::particle_container particle_container
ObserverPointer< const Triangulation< dim, spacedim >, ParticleHandler< dim, spacedim > > triangulation
particle_iterator_range particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
particle_iterator insert_particle(const Particle< dim, spacedim > &particle, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
void remove_particles(const std::vector< particle_iterator > &particles)
types::particle_index n_global_max_particles_per_cell() const
std::vector< boost::signals2::connection > tria_listeners
particle_container::iterator particle_container_owned_begin() const
void unpack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range)
std::vector< typename particle_container::iterator > cells_to_particle_cache
void copy_from(const ParticleHandler< dim, spacedim > &particle_handler)
std::function< const void *(const particle_iterator &, const void *)> load_callback
std::unique_ptr< GridTools::Cache< dim, spacedim > > triangulation_cache
types::particle_index get_next_free_particle_index() const
IndexSet locally_owned_particle_ids() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
std::vector< index_type > data
Definition mpi.cc:735
boost::signals2::signal< void(const typename Particles::ParticleIterator< dim, spacedim > &particle, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)> particle_lost