Reference documentation for deal.II version 9.2.0
\(\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\}}\)
particle_handler.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2020 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_particles_particle_handler_h
17 #define dealii_particles_particle_handler_h
18 
19 #include <deal.II/base/config.h>
20 
23 #include <deal.II/base/function.h>
24 #include <deal.II/base/mpi.h>
27 
29 
30 #include <deal.II/fe/mapping.h>
31 
33 
37 
38 #include <boost/range/iterator_range.hpp>
39 #include <boost/serialization/map.hpp>
40 
42 
43 namespace Particles
44 {
59  template <int dim, int spacedim = dim>
60  class ParticleHandler : public Subscriptor
61  {
62  public:
67 
71  using particle_iterator_range = boost::iterator_range<particle_iterator>;
72 
77 
88  const unsigned int n_properties = 0);
89 
93  virtual ~ParticleHandler() override = default;
94 
100  void
103  const unsigned int n_properties = 0);
104 
108  void
109  clear();
110 
116  void
117  clear_particles();
118 
127  void
129 
134  begin() const;
135 
140  begin();
141 
146  end() const;
147 
152  end();
153 
158  begin_ghost() const;
159 
164  begin_ghost();
165 
170  end_ghost() const;
171 
176  end_ghost();
177 
186 
187 
196  const;
197 
201  void
202  remove_particle(const particle_iterator &particle);
203 
212  const Particle<dim, spacedim> &particle,
214 
221  void
223  const std::multimap<
226 
236  void
237  insert_particles(const std::vector<Point<spacedim>> &positions);
238 
287  std::map<unsigned int, IndexSet>
289  const std::vector<Point<spacedim>> &positions,
290  const std::vector<std::vector<BoundingBox<spacedim>>>
291  & global_bounding_boxes,
292  const std::vector<std::vector<double>> &properties = {});
293 
339  template <class VectorType>
340  typename std::enable_if<
341  std::is_convertible<VectorType *, Function<spacedim> *>::value ==
342  false>::type
343  set_particle_positions(const VectorType &input_vector,
344  const bool displace_particles = true);
345 
368  void
369  set_particle_positions(const std::vector<Point<spacedim>> &new_positions,
370  const bool displace_particles = true);
371 
372 
393  void
395  const bool displace_particles = true);
396 
426  template <class VectorType>
427  void
428  get_particle_positions(VectorType &output_vector,
429  const bool add_to_output_vector = false);
430 
448  void
449  get_particle_positions(std::vector<Point<spacedim>> &positions,
450  const bool add_to_output_vector = false);
451 
478  void
480  const std::function<std::size_t()> &size_callback,
481  const std::function<void *(const particle_iterator &, void *)>
483  const std::function<const void *(const particle_iterator &, const void *)>
484  &load_callback);
485 
495  n_global_particles() const;
496 
505 
512 
520 
539  IndexSet
540  locally_relevant_ids() const;
541 
545  unsigned int
547 
552  PropertyPool &
553  get_property_pool() const;
554 
558  unsigned int
561  const;
562 
571  void
573 
579  void
581 
588  void
590 
597  void
598  register_load_callback_function(const bool serialization);
599 
603  template <class Archive>
604  void
605  serialize(Archive &ar, const unsigned int version);
606 
607  private:
614 
620 
625  std::multimap<internal::LevelInd, Particle<dim, spacedim>> particles;
626 
632  std::multimap<internal::LevelInd, Particle<dim, spacedim>> ghost_particles;
633 
639 
649 
655 
660  std::unique_ptr<PropertyPool> property_pool;
661 
673  std::function<std::size_t()> size_callback;
674 
685  std::function<void *(const particle_iterator &, void *)> store_callback;
686 
697  std::function<const void *(const particle_iterator &, const void *)>
699 
706  unsigned int handle;
707 
717  std::unique_ptr<GridTools::Cache<dim, spacedim>> triangulation_cache;
718 
719 #ifdef DEAL_II_WITH_MPI
720 
741  void
743  const std::map<types::subdomain_id, std::vector<particle_iterator>>
744  &particles_to_send,
746  &received_particles,
747  const std::map<
749  std::vector<
751  &new_cells_for_particles = std::map<
753  std::vector<
755 #endif
756 
762  std::vector<char>
764  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
765  const typename Triangulation<dim, spacedim>::CellStatus status) const;
766 
771  void
773  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
774  const typename Triangulation<dim, spacedim>::CellStatus status,
775  const boost::iterator_range<std::vector<char>::const_iterator>
776  &data_range);
777  };
778 
779 
780 
781  /* ---------------------- inline and template functions ------------------
782  */
783 
784 
785 
786  template <int dim, int spacedim>
787  template <class Archive>
788  void
789  ParticleHandler<dim, spacedim>::serialize(Archive &ar, const unsigned int)
790  {
791  // Note that we do not serialize the particle data itself. Instead we
792  // use the serialization functionality of the triangulation class, because
793  // this guarantees that data is immediately shipped to new processes if
794  // the domain is distributed differently after resuming from a checkpoint.
795  ar //&particles
796  &global_number_of_particles &global_max_particles_per_cell
797  & next_free_particle_index;
798  }
799 
800 
801 
802  template <int dim, int spacedim>
803  template <class VectorType>
804  typename std::enable_if<
805  std::is_convertible<VectorType *, Function<spacedim> *>::value ==
806  false>::type
808  const VectorType &input_vector,
809  const bool displace_particles)
810  {
811  AssertDimension(input_vector.size(),
812  get_next_free_particle_index() * spacedim);
813  for (auto &p : *this)
814  {
815  auto new_point(displace_particles ? p.get_location() :
816  Point<spacedim>());
817  const auto id = p.get_id();
818  for (unsigned int i = 0; i < spacedim; ++i)
819  new_point[i] += input_vector[id * spacedim + i];
820  p.set_location(new_point);
821  }
822  sort_particles_into_subdomains_and_cells();
823  }
824 
825 
826 
827  template <int dim, int spacedim>
828  template <class VectorType>
829  void
831  VectorType &output_vector,
832  const bool add_to_output_vector)
833  {
834  AssertDimension(output_vector.size(),
835  get_next_free_particle_index() * spacedim);
836  for (const auto &p : *this)
837  {
838  auto point = p.get_location();
839  const auto id = p.get_id();
840  if (add_to_output_vector)
841  for (unsigned int i = 0; i < spacedim; ++i)
842  output_vector[id * spacedim + i] += point[i];
843  else
844  for (unsigned int i = 0; i < spacedim; ++i)
845  output_vector[id * spacedim + i] = point[i];
846  }
847  if (add_to_output_vector)
848  output_vector.compress(VectorOperation::add);
849  else
850  output_vector.compress(VectorOperation::insert);
851  }
852 
853 } // namespace Particles
854 
856 
857 #endif
array_view.h
Particles::ParticleHandler::size_callback
std::function< std::size_t()> size_callback
Definition: particle_handler.h:673
IndexSet
Definition: index_set.h:74
bounding_box.h
Particles::ParticleHandler::particles_in_cell
particle_iterator_range particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
Definition: particle_handler.cc:313
Particles::ParticleHandler::ParticleHandler
ParticleHandler()
Definition: particle_handler.cc:89
Particles::ParticleHandler::register_store_callback_function
void register_store_callback_function()
Definition: particle_handler.cc:1410
Particles::ParticleHandler::set_particle_positions
std::enable_if< std::is_convertible< VectorType *, Function< spacedim > * >::value==false >::type set_particle_positions(const VectorType &input_vector, const bool displace_particles=true)
Definition: particle_handler.h:807
Particles::ParticleHandler::store_callback
std::function< void *(const particle_iterator &, void *)> store_callback
Definition: particle_handler.h:685
Particles::ParticleHandler::n_properties_per_particle
unsigned int n_properties_per_particle() const
Definition: particle_handler.cc:682
Particles::ParticleHandler::global_number_of_particles
types::particle_index global_number_of_particles
Definition: particle_handler.h:638
Particles::ParticleHandler::insert_particles
void insert_particles(const std::multimap< typename Triangulation< dim, spacedim >::active_cell_iterator, Particle< dim, spacedim >> &particles)
Definition: particle_handler.cc:371
Particles::ParticleHandler::begin_ghost
particle_iterator begin_ghost() const
Definition: particle_handler.cc:265
VectorOperation::insert
@ insert
Definition: vector_operation.h:49
Particles::ParticleHandler::get_next_free_particle_index
types::particle_index get_next_free_particle_index() const
Definition: particle_handler.cc:712
Triangulation
Definition: tria.h:1109
Particles::ParticleHandler::particle_iterator
ParticleIterator< dim, spacedim > particle_iterator
Definition: particle_handler.h:66
VectorType
Particles::ParticleHandler::property_pool
std::unique_ptr< PropertyPool > property_pool
Definition: particle_handler.h:660
Particles::PropertyPool
Definition: property_pool.h:43
VectorOperation::add
@ add
Definition: vector_operation.h:53
Particles
Definition: data_out.h:27
Particles::ParticleHandler::particle_iterator_range
boost::iterator_range< particle_iterator > particle_iterator_range
Definition: particle_handler.h:71
Particles::ParticleHandler::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: particle_handler.h:789
Particles::ParticleHandler::send_recv_particles
void send_recv_particles(const std::map< types::subdomain_id, std::vector< particle_iterator >> &particles_to_send, std::multimap< internal::LevelInd, Particle< dim, spacedim >> &received_particles, 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 >>())
Definition: particle_handler.cc:1161
Particles::ParticleHandler::particles
std::multimap< internal::LevelInd, Particle< dim, spacedim > > particles
Definition: particle_handler.h:625
Particles::ParticleIterator
Definition: particle_iterator.h:39
Particles::ParticleHandler::load_particles
void load_particles(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const typename Triangulation< dim, spacedim >::CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range)
Definition: particle_handler.cc:1603
Particles::ParticleHandler::triangulation_cache
std::unique_ptr< GridTools::Cache< dim, spacedim > > triangulation_cache
Definition: particle_handler.h:717
Particles::ParticleHandler::insert_global_particles
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={})
Definition: particle_handler.cc:457
Particles::ParticleHandler::register_additional_store_load_functions
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)
Definition: particle_handler.cc:1395
OpenCASCADE::point
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
BoundingBox
Definition: bounding_box.h:128
Subscriptor
Definition: subscriptor.h:62
Triangulation::CellStatus
CellStatus
Definition: tria.h:1969
Particles::ParticleHandler::get_property_pool
PropertyPool & get_property_pool() const
Definition: particle_handler.cc:805
Particles::Particle
Definition: particle.h:146
Particles::ParticleHandler::end_ghost
particle_iterator end_ghost() const
Definition: particle_handler.cc:283
Mapping
Abstract base class for mapping classes.
Definition: mapping.h:302
Particles::ParticleHandler::n_particles_in_cell
unsigned int n_particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell) const
Definition: particle_handler.cc:691
mpi.h
Particles::ParticleHandler::mapping
SmartPointer< const Mapping< dim, spacedim >, ParticleHandler< dim, spacedim > > mapping
Definition: particle_handler.h:619
subscriptor.h
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
property_pool.h
Particles::ParticleHandler::get_particle_positions
void get_particle_positions(VectorType &output_vector, const bool add_to_output_vector=false)
Definition: particle_handler.h:830
Particles::ParticleHandler::n_global_particles
types::particle_index n_global_particles() const
Definition: particle_handler.cc:655
Particles::ParticleHandler::sort_particles_into_subdomains_and_cells
void sort_particles_into_subdomains_and_cells()
Definition: particle_handler.cc:845
AssertDimension
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
Particles::ParticleHandler::next_free_particle_index
types::particle_index next_free_particle_index
Definition: particle_handler.h:654
smartpointer.h
Particles::ParticleHandler::end
particle_iterator end() const
Definition: particle_handler.cc:247
Particles::ParticleHandler::n_locally_owned_particles
types::particle_index n_locally_owned_particles() const
Definition: particle_handler.cc:673
TriaActiveIterator
Definition: tria_iterator.h:759
Particles::ParticleHandler::store_particles
std::vector< char > store_particles(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const typename Triangulation< dim, spacedim >::CellStatus status) const
Definition: particle_handler.cc:1512
SmartPointer
Definition: smartpointer.h:68
Particles::ParticleHandler::remove_particle
void remove_particle(const particle_iterator &particle)
Definition: particle_handler.cc:337
unsigned int
Particles::ParticleHandler::ghost_particles
std::multimap< internal::LevelInd, Particle< dim, spacedim > > ghost_particles
Definition: particle_handler.h:632
value
static const bool value
Definition: dof_tools_constraints.cc:433
tria.h
function.h
Particles::ParticleHandler::locally_relevant_ids
IndexSet locally_relevant_ids() const
Definition: particle_handler.cc:721
mapping.h
particle.h
Particles::ParticleHandler::insert_particle
particle_iterator insert_particle(const Particle< dim, spacedim > &particle, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
Definition: particle_handler.cc:347
Particles::internal::LevelInd
std::pair< int, int > LevelInd
Definition: particle.h:92
particle_iterator.h
grid_tools_cache.h
Particles::ParticleHandler
Definition: data_out.h:30
Particles::ParticleHandler::triangulation
SmartPointer< const Triangulation< dim, spacedim >, ParticleHandler< dim, spacedim > > triangulation
Definition: particle_handler.h:613
Particles::ParticleHandler::update_cached_numbers
void update_cached_numbers()
Definition: particle_handler.cc:175
Particles::ParticleHandler::~ParticleHandler
virtual ~ParticleHandler() override=default
Particles::ParticleHandler::clear_particles
void clear_particles()
Definition: particle_handler.cc:166
Point< spacedim >
Particles::ParticleHandler::load_callback
std::function< const void *(const particle_iterator &, const void *)> load_callback
Definition: particle_handler.h:698
config.h
Particles::ParticleHandler::n_global_max_particles_per_cell
types::particle_index n_global_max_particles_per_cell() const
Definition: particle_handler.cc:664
Function
Definition: function.h:151
Particles::ParticleHandler::register_load_callback_function
void register_load_callback_function(const bool serialization)
Definition: particle_handler.cc:1447
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Particles::ParticleHandler::handle
unsigned int handle
Definition: particle_handler.h:706
Particles::ParticleHandler::global_max_particles_per_cell
unsigned int global_max_particles_per_cell
Definition: particle_handler.h:648
Particles::ParticleHandler::begin
particle_iterator begin() const
Definition: particle_handler.cc:229
Particles::ParticleHandler::exchange_ghost_particles
void exchange_ghost_particles()
Definition: particle_handler.cc:1083
TriaIterator
Definition: tria_iterator.h:578
Particles::ParticleHandler::initialize
void initialize(const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping, const unsigned int n_properties=0)
Definition: particle_handler.cc:132
Particles::ParticleHandler::clear
void clear()
Definition: particle_handler.cc:154