Reference documentation for deal.II version GIT relicensing-897-g3473bfcca7 2024-06-24 11:40:02+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{
63 template <int dim, int spacedim = dim>
65 {
66 public:
71
75 using particle_iterator_range = boost::iterator_range<particle_iterator>;
76
82
87
98 const unsigned int n_properties = 0);
99
103 virtual ~ParticleHandler();
104
110 void
113 const unsigned int n_properties = 0);
114
135 void
136 copy_from(const ParticleHandler<dim, spacedim> &particle_handler);
137
141 void
142 clear();
143
149 void
151
166 void
167 reserve(const std::size_t n_particles);
168
177 void
179
184 begin() const;
185
190 begin();
191
196 end() const;
197
202 end();
203
208 begin_ghost() const;
209
214 begin_ghost();
215
220 end_ghost() const;
221
226 end_ghost();
227
234 const;
235
247
259 const;
260
266 void
267 remove_particle(const particle_iterator &particle);
268
274 void
275 remove_particles(const std::vector<particle_iterator> &particles);
276
285 const Particle<dim, spacedim> &particle,
287
305 const Point<spacedim> &position,
306 const Point<dim> &reference_position,
307 const types::particle_index particle_index,
309 const ArrayView<const double> &properties = {});
310
317 void
319 const std::multimap<
322
332 void
333 insert_particles(const std::vector<Point<spacedim>> &positions);
334
395 std::map<unsigned int, IndexSet>
397 const std::vector<Point<spacedim>> &positions,
398 const std::vector<std::vector<BoundingBox<spacedim>>>
399 &global_bounding_boxes,
400 const std::vector<std::vector<double>> &properties = {},
401 const std::vector<types::particle_index> &ids = {});
402
433 std::map<unsigned int, IndexSet>
435 const std::vector<Particle<dim, spacedim>> &particles,
436 const std::vector<std::vector<BoundingBox<spacedim>>>
437 &global_bounding_boxes);
438
482 template <typename VectorType>
483 std::enable_if_t<
484 std::is_convertible_v<VectorType *, Function<spacedim> *> == false>
485 set_particle_positions(const VectorType &input_vector,
486 const bool displace_particles = true);
487
508 void
509 set_particle_positions(const std::vector<Point<spacedim>> &new_positions,
510 const bool displace_particles = true);
511
512
531 void
533 const bool displace_particles = true);
534
562 template <typename VectorType>
563 void
564 get_particle_positions(VectorType &output_vector,
565 const bool add_to_output_vector = false);
566
581 void
582 get_particle_positions(std::vector<Point<spacedim>> &positions,
583 const bool add_to_output_vector = false);
584
611 void
613 const std::function<std::size_t()> &size_callback,
614 const std::function<void *(const particle_iterator &, void *)>
616 const std::function<const void *(const particle_iterator &, const void *)>
618
628 n_global_particles() const;
629
638
645
653
672
676 unsigned int
678
694
699 PropertyPool<dim, spacedim> &
700 get_property_pool() const;
701
716 void
718
724 void
725 exchange_ghost_particles(const bool enable_ghost_cache = false);
726
734 void
736
746 void
748
767 void
769
781 void
783
792 void
793 deserialize();
794
806 void
808
820 void
821 register_load_callback_function(const bool serialization);
822
827 template <class Archive>
828 void
829 serialize(Archive &ar, const unsigned int version);
830
843 struct Signals
844 {
858 boost::signals2::signal<void(
859 const typename Particles::ParticleIterator<dim, spacedim> &particle,
861 &cell)>
863 };
864
870
871 private:
880 const void *&data,
882
891
901 void
903
910
916
925 std::unique_ptr<PropertyPool<dim, spacedim>> property_pool;
926
931
937 const typename particle_container::iterator owned_particles_end;
938
944 std::vector<typename particle_container::iterator> cells_to_particle_cache;
945
951
957
967
973
985 std::function<std::size_t()> size_callback;
986
997 std::function<void *(const particle_iterator &, void *)> store_callback;
998
1009 std::function<const void *(const particle_iterator &, const void *)>
1011
1018 unsigned int handle;
1019
1023 const double tolerance_inside_cell = 1e-12;
1024
1034 std::unique_ptr<GridTools::Cache<dim, spacedim>> triangulation_cache;
1035
1036#ifdef DEAL_II_WITH_MPI
1060 void
1062 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1063 &particles_to_send,
1064 const std::map<
1066 std::vector<
1068 &new_cells_for_particles = std::map<
1070 std::vector<
1072 const bool enable_cache = false);
1073
1085 void
1087 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1088 &particles_to_send);
1089
1090#endif
1091
1099
1104 void
1106
1111 std::vector<boost::signals2::connection> tria_listeners;
1112
1121 void
1123
1129 void
1131
1139 void
1140 notify_ready_to_unpack(const bool serialization);
1141
1147 std::vector<char>
1150 const CellStatus status) const;
1151
1156 void
1159 const CellStatus status,
1160 const boost::iterator_range<std::vector<char>::const_iterator>
1161 &data_range);
1162
1167 typename particle_container::iterator
1169
1174 typename particle_container::iterator
1176
1181 typename particle_container::iterator
1183
1188 typename particle_container::iterator
1190 };
1191
1192
1193
1194 /* ---------------------- inline and template functions ------------------
1195 */
1196
1197 template <int dim, int spacedim>
1200 {
1201 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->begin();
1202 }
1203
1204
1205
1206 template <int dim, int spacedim>
1209 {
1210 return particle_iterator(particle_container_owned_begin(),
1211 *property_pool,
1212 0);
1213 }
1214
1215
1216
1217 template <int dim, int spacedim>
1220 {
1221 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->end();
1222 }
1223
1224
1225
1226 template <int dim, int spacedim>
1229 {
1230 return particle_iterator(particle_container_owned_end(), *property_pool, 0);
1231 }
1232
1233
1234
1235 template <int dim, int spacedim>
1238 {
1239 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->begin_ghost();
1240 }
1241
1242
1243
1244 template <int dim, int spacedim>
1247 {
1248 return particle_iterator(particle_container_ghost_begin(),
1249 *property_pool,
1250 0);
1251 }
1252
1253
1254
1255 template <int dim, int spacedim>
1258 {
1259 return (const_cast<ParticleHandler<dim, spacedim> *>(this))->end_ghost();
1260 }
1261
1262
1263
1264 template <int dim, int spacedim>
1267 {
1268 return particle_iterator(particle_container_ghost_end(), *property_pool, 0);
1269 }
1270
1271
1272
1273 template <int dim, int spacedim>
1276 {
1277 // We should always have at least the three anchor entries in the list of
1278 // particles
1279 Assert(!particles.empty(), ExcInternalError());
1280 typename particle_container::iterator begin =
1281 const_cast<particle_container &>(particles).begin();
1282 return ++begin;
1283 }
1284
1285
1286
1287 template <int dim, int spacedim>
1290 {
1291 Assert(!particles.empty(), ExcInternalError());
1292 return owned_particles_end;
1293 }
1294
1295
1296
1297 template <int dim, int spacedim>
1300 {
1301 Assert(!particles.empty(), ExcInternalError());
1302 typename particle_container::iterator begin = owned_particles_end;
1303 return ++begin;
1304 }
1305
1306
1307
1308 template <int dim, int spacedim>
1311 {
1312 Assert(!particles.empty(), ExcInternalError());
1313 typename particle_container::iterator end =
1314 const_cast<particle_container &>(particles).end();
1315 return --end;
1316 }
1317
1318
1319
1320 template <int dim, int spacedim>
1321 template <class Archive>
1322 inline void
1323 ParticleHandler<dim, spacedim>::serialize(Archive &ar, const unsigned int)
1324 {
1325 // Note that we do not serialize the particle data itself. Instead we
1326 // use the serialization functionality of the triangulation class, because
1327 // this guarantees that data is immediately shipped to new processes if
1328 // the domain is distributed differently after resuming from a checkpoint.
1329 ar //&particles
1330 &global_number_of_particles &global_max_particles_per_cell
1331 &next_free_particle_index;
1332 }
1333
1334
1335
1336 template <int dim, int spacedim>
1337 template <typename VectorType>
1338 inline std::enable_if_t<
1339 std::is_convertible_v<VectorType *, Function<spacedim> *> == false>
1341 const VectorType &input_vector,
1342 const bool displace_particles)
1343 {
1344 AssertDimension(input_vector.size(),
1345 get_next_free_particle_index() * spacedim);
1346 for (auto &p : *this)
1347 {
1348 Point<spacedim> &position = p.get_location();
1349 const auto id = p.get_id();
1350
1351 if (displace_particles)
1352 for (unsigned int i = 0; i < spacedim; ++i)
1353 position[i] += input_vector[id * spacedim + i];
1354 else
1355 for (unsigned int i = 0; i < spacedim; ++i)
1356 position[i] = input_vector[id * spacedim + i];
1357 }
1358 sort_particles_into_subdomains_and_cells();
1359 }
1360
1361
1362
1363 template <int dim, int spacedim>
1364 template <typename VectorType>
1365 inline void
1367 VectorType &output_vector,
1368 const bool add_to_output_vector)
1369 {
1370 AssertDimension(output_vector.size(),
1371 get_next_free_particle_index() * spacedim);
1372 for (const auto &p : *this)
1373 {
1374 auto point = p.get_location();
1375 const auto id = p.get_id();
1376 if (add_to_output_vector)
1377 for (unsigned int i = 0; i < spacedim; ++i)
1378 output_vector[id * spacedim + i] += point[i];
1379 else
1380 for (unsigned int i = 0; i < spacedim; ++i)
1381 output_vector[id * spacedim + i] = point[i];
1382 }
1383 if (add_to_output_vector)
1384 output_vector.compress(VectorOperation::add);
1385 else
1386 output_vector.compress(VectorOperation::insert);
1387 }
1388
1389} // namespace Particles
1390
1392
1393#endif
CellStatus
Definition cell_status.h:31
Abstract base class for mapping classes.
Definition mapping.h:318
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
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)
void register_load_callback_function(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
particle_iterator_range particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
SmartPointer< const Triangulation< dim, spacedim >, ParticleHandler< dim, spacedim > > triangulation
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
SmartPointer< const Mapping< dim, spacedim >, ParticleHandler< dim, spacedim > > mapping
std::unique_ptr< GridTools::Cache< dim, spacedim > > triangulation_cache
types::particle_index get_next_free_particle_index() const
IndexSet locally_owned_particle_ids() const
Definition point.h:111
#define DEAL_II_DEPRECATED
Definition config.h:207
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
boost::signals2::signal< void(const typename Particles::ParticleIterator< dim, spacedim > &particle, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)> particle_lost