deal.II version GIT relicensing-3718-gc13d52846a 2025-07-09 19:30:01+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.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2017 - 2025 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
16
19
21
23
24#include <limits>
25#include <memory>
26#include <utility>
27
29
30namespace Particles
31{
32 namespace
33 {
34 template <int dim, int spacedim>
35 std::vector<char>
36 pack_particles(std::vector<ParticleIterator<dim, spacedim>> &particles)
37 {
38 std::vector<char> buffer;
39
40 if (particles.empty())
41 return buffer;
42
43 buffer.resize(particles.size() *
44 particles.front()->serialized_size_in_bytes());
45 void *current_data = buffer.data();
46
47 for (const auto &particle : particles)
48 {
49 current_data = particle->write_particle_data_to_memory(current_data);
50 }
51
52 return buffer;
53 }
54 } // namespace
55
56
57
58 template <int dim, int spacedim>
61 , mapping()
62 , property_pool(std::make_unique<PropertyPool<dim, spacedim>>(0))
63 , global_number_of_particles(0)
64 , number_of_locally_owned_particles(0)
65 , global_max_particles_per_cell(0)
66 , next_free_particle_index(0)
67 , size_callback()
68 , store_callback()
69 , load_callback()
70 , tria_attached_data_index(numbers::invalid_unsigned_int)
71 , tria_listeners()
72 {
74 }
75
76
77
78 template <int dim, int spacedim>
81 const Mapping<dim, spacedim> &mapping,
82 const unsigned int n_properties)
83 : triangulation(&triangulation, typeid(*this).name())
84 , mapping(&mapping, typeid(*this).name())
85 , property_pool(std::make_unique<PropertyPool<dim, spacedim>>(n_properties))
86 , cells_to_particle_cache(triangulation.n_active_cells(), particles.end())
87 , global_number_of_particles(0)
88 , number_of_locally_owned_particles(0)
89 , global_max_particles_per_cell(0)
90 , next_free_particle_index(0)
91 , size_callback()
92 , store_callback()
93 , load_callback()
94 , tria_attached_data_index(numbers::invalid_unsigned_int)
95 , triangulation_cache(
96 std::make_unique<GridTools::Cache<dim, spacedim>>(triangulation,
97 mapping))
98 , tria_listeners()
99 {
102 }
103
104
105
106 template <int dim, int spacedim>
108 {
109 clear_particles();
110
111 for (const auto &connection : tria_listeners)
112 connection.disconnect();
113 }
114
115
116
117 template <int dim, int spacedim>
118 void
120 const Triangulation<dim, spacedim> &new_triangulation,
121 const Mapping<dim, spacedim> &new_mapping,
122 const unsigned int n_properties)
123 {
124 clear();
125
126 triangulation = &new_triangulation;
127 mapping = &new_mapping;
128
129 reset_particle_container(particles);
130
131 // Create the memory pool that will store all particle properties
132 property_pool = std::make_unique<PropertyPool<dim, spacedim>>(n_properties);
133
134 // Create the grid cache to cache the information about the triangulation
135 // that is used to locate the particles into subdomains and cells
136 triangulation_cache =
137 std::make_unique<GridTools::Cache<dim, spacedim>>(new_triangulation,
138 new_mapping);
139
140 cells_to_particle_cache.resize(triangulation->n_active_cells(),
141 particles.end());
142
143 connect_to_triangulation_signals();
144 }
145
146
147
148 template <int dim, int spacedim>
149 void
151 const ParticleHandler<dim, spacedim> &particle_handler)
152 {
153 const unsigned int n_properties =
154 particle_handler.property_pool->n_properties_per_slot();
155 initialize(*particle_handler.triangulation,
156 *particle_handler.mapping,
157 n_properties);
158
159 property_pool = std::make_unique<PropertyPool<dim, spacedim>>(
160 *(particle_handler.property_pool));
161
162 // copy static members
163 global_number_of_particles = particle_handler.global_number_of_particles;
164 number_of_locally_owned_particles =
165 particle_handler.number_of_locally_owned_particles;
166
167 global_max_particles_per_cell =
168 particle_handler.global_max_particles_per_cell;
169 next_free_particle_index = particle_handler.next_free_particle_index;
170
171 // Manually copy over the particles because we do not want to touch the
172 // anchor iterators set by initialize()
173 particles.insert(particle_container_owned_end(),
174 particle_handler.particle_container_owned_begin(),
175 particle_handler.particle_container_owned_end());
176 particles.insert(particle_container_ghost_end(),
177 particle_handler.particle_container_ghost_begin(),
178 particle_handler.particle_container_ghost_end());
179
180 for (auto it = particles.begin(); it != particles.end(); ++it)
181 if (!it->particles.empty())
182 cells_to_particle_cache[it->cell->active_cell_index()] = it;
183
184 ghost_particles_cache.ghost_particles_by_domain =
185 particle_handler.ghost_particles_cache.ghost_particles_by_domain;
186 tria_attached_data_index = particle_handler.tria_attached_data_index;
187 }
188
189
190
191 template <int dim, int spacedim>
192 void
194 {
195 clear_particles();
196 global_number_of_particles = 0;
197 number_of_locally_owned_particles = 0;
198 next_free_particle_index = 0;
199 global_max_particles_per_cell = 0;
200 }
201
202
203
204 template <int dim, int spacedim>
205 void
207 {
208 for (auto &particles_in_cell : particles)
209 for (auto &particle : particles_in_cell.particles)
211 property_pool->deregister_particle(particle);
212
213 cells_to_particle_cache.clear();
214 reset_particle_container(particles);
215 if (triangulation != nullptr)
216 cells_to_particle_cache.resize(triangulation->n_active_cells(),
217 particles.end());
218
219 // the particle properties have already been deleted by their destructor,
220 // but the memory is still allocated. Return the memory as well.
221 property_pool->clear();
222 }
223
224
225
226 template <int dim, int spacedim>
227 void
228 ParticleHandler<dim, spacedim>::reserve(const std::size_t n_particles)
229 {
230 property_pool->reserve(n_particles);
231 }
232
233
234
235 template <int dim, int spacedim>
236 void
238 particle_container &given_particles)
239 {
240 // Make sure to set a valid past-the-end iterator also in case we have no
241 // triangulation
243 past_the_end_iterator =
244 triangulation != nullptr ?
245 triangulation->end() :
246 typename Triangulation<dim, spacedim>::cell_iterator(nullptr, -1, -1);
247
248 given_particles.clear();
249 for (unsigned int i = 0; i < 3; ++i)
250 given_particles.emplace_back(
251 std::vector<typename PropertyPool<dim, spacedim>::Handle>(),
252 past_the_end_iterator);
253
254 // Set the end of owned particles to the middle of the three elements
255 const_cast<typename particle_container::iterator &>(owned_particles_end) =
256 ++given_particles.begin();
257 }
258
259
260
261 template <int dim, int spacedim>
262 void
264 {
265 // first sort the owned particles by the active cell index
266 bool sort_is_necessary = false;
267 {
268 auto previous = particle_container_owned_begin();
269 for (auto next = previous; next != particle_container_owned_end(); ++next)
270 {
271 if (previous->cell.state() == IteratorState::valid &&
272 next->cell.state() == IteratorState::valid &&
273 previous->cell > next->cell)
274 {
275 sort_is_necessary = true;
276 break;
277 }
278 previous = next;
279 }
280 }
281 if (sort_is_necessary)
282 {
283 // we could have tried to call std::list::sort with a custom
284 // comparator to get things sorted, but things are complicated by the
285 // three anchor entries that we do not want to move, and we would
286 // hence pay an O(N log(N)) algorithm with a large constant in front
287 // of it (on the order of 20+ instructions with many
288 // difficult-to-predict branches). Therefore, we simply copy the list
289 // into a new one (keeping alive the possibly large vectors with
290 // particles on cells) into a new container.
291 particle_container sorted_particles;
292
293 // note that this call updates owned_particles_end, so that
294 // particle_container_owned_end() below already points to the
295 // new container
296 reset_particle_container(sorted_particles);
297
298 // iterate over cells and insert the entries in the new order
299 for (const auto &cell : triangulation->active_cell_iterators())
300 if (!cell->is_artificial())
301 if (cells_to_particle_cache[cell->active_cell_index()] !=
302 particles.end())
303 {
304 // before we move the sorted_particles into particles
305 // particle_container_ghost_end() still points to the
306 // old particles container. Therefore this condition looks
307 // quirky.
308 typename particle_container::iterator insert_position =
309 cell->is_locally_owned() ? particle_container_owned_end() :
310 --sorted_particles.end();
311 typename particle_container::iterator new_entry =
312 sorted_particles.insert(
313 insert_position, typename particle_container::value_type());
314 new_entry->cell = cell;
315 new_entry->particles =
316 std::move(cells_to_particle_cache[cell->active_cell_index()]
317 ->particles);
318 }
319 particles = std::move(sorted_particles);
320
321 // refresh cells_to_particle_cache
322 cells_to_particle_cache.clear();
323 cells_to_particle_cache.resize(triangulation->n_active_cells(),
324 particles.end());
325 for (auto it = particles.begin(); it != particles.end(); ++it)
326 if (!it->particles.empty())
327 cells_to_particle_cache[it->cell->active_cell_index()] = it;
328 }
329
330 // Ensure that we did not accidentally modify the anchor entries with
331 // special purpose.
332 Assert(particles.front().cell.state() == IteratorState::past_the_end &&
333 particles.front().particles.empty() &&
334 particles.back().cell.state() == IteratorState::past_the_end &&
335 particles.back().particles.empty() &&
336 owned_particles_end->cell.state() == IteratorState::past_the_end &&
337 owned_particles_end->particles.empty(),
339
340 if constexpr (running_in_debug_mode())
341 {
342 // check that no cache element hits the three anchor states in the list
343 // of particles
344 for (const auto &it : cells_to_particle_cache)
345 Assert(it != particles.begin() && it != owned_particles_end &&
346 it != --(particles.end()),
348
349 // check that we only have locally owned particles in the first region
350 // of cells; note that we skip the very first anchor element
351 for (auto it = particle_container_owned_begin();
352 it != particle_container_owned_end();
353 ++it)
354 Assert(it->cell->is_locally_owned(), ExcInternalError());
355
356 // check that the cache is consistent with the iterators
357 std::vector<typename particle_container::iterator> verify_cache(
358 triangulation->n_active_cells(), particles.end());
359 for (auto it = particles.begin(); it != particles.end(); ++it)
360 if (!it->particles.empty())
361 verify_cache[it->cell->active_cell_index()] = it;
362
363 for (unsigned int i = 0; i < verify_cache.size(); ++i)
364 Assert(verify_cache[i] == cells_to_particle_cache[i],
366 }
367
368 // now compute local result with the function above and then compute the
369 // collective results
370 number_of_locally_owned_particles = 0;
371
372 types::particle_index result[2] = {};
373 for (const auto &particles_in_cell : particles)
374 {
375 const types::particle_index n_particles_in_cell =
376 particles_in_cell.particles.size();
377
378 // local_max_particles_per_cell
379 result[0] = std::max(result[0], n_particles_in_cell);
380
381 // number of locally owned particles
382 if (n_particles_in_cell > 0 &&
383 particles_in_cell.cell->is_locally_owned())
384 number_of_locally_owned_particles += n_particles_in_cell;
385
386 // local_max_particle_index
387 for (const auto &particle : particles_in_cell.particles)
388 result[1] = std::max(result[1], property_pool->get_id(particle));
389 }
390
391 global_number_of_particles =
392 ::Utilities::MPI::sum(number_of_locally_owned_particles,
393 triangulation->get_mpi_communicator());
394
395 if (global_number_of_particles == 0)
396 {
397 next_free_particle_index = 0;
398 global_max_particles_per_cell = 0;
399 }
400 else
401 {
402 Utilities::MPI::max(result,
403 triangulation->get_mpi_communicator(),
404 result);
405
406 next_free_particle_index = result[1] + 1;
407 global_max_particles_per_cell = result[0];
408 }
409 }
410
411
412
413 template <int dim, int spacedim>
417 const
418 {
419 if (cells_to_particle_cache.empty())
420 return 0;
421
422 if (cell->is_artificial() == false)
423 {
424 return cells_to_particle_cache[cell->active_cell_index()] !=
425 particles.end() ?
426 cells_to_particle_cache[cell->active_cell_index()]
427 ->particles.size() :
428 0;
429 }
430 else
431 AssertThrow(false,
432 ExcMessage("You can't ask for the particles on an artificial "
433 "cell since we don't know what exists on these "
434 "kinds of cells."));
435
437 }
438
439
440
441 template <int dim, int spacedim>
445 const
446 {
447 return (const_cast<ParticleHandler<dim, spacedim> *>(this))
448 ->particles_in_cell(cell);
449 }
450
451
452
453 template <int dim, int spacedim>
457 {
458 const unsigned int active_cell_index = cell->active_cell_index();
459
460 if (cell->is_artificial() == false)
461 {
462 if (cells_to_particle_cache[active_cell_index] == particles.end())
463 {
464 return boost::make_iterator_range(
465 particle_iterator(particles.begin(), *property_pool, 0),
466 particle_iterator(particles.begin(), *property_pool, 0));
467 }
468 else
469 {
470 const typename particle_container::iterator
471 particles_in_current_cell =
472 cells_to_particle_cache[active_cell_index];
473 typename particle_container::iterator particles_in_next_cell =
474 particles_in_current_cell;
475 ++particles_in_next_cell;
476 return boost::make_iterator_range(
477 particle_iterator(particles_in_current_cell, *property_pool, 0),
478 particle_iterator(particles_in_next_cell, *property_pool, 0));
479 }
480 }
481 else
482 AssertThrow(false,
483 ExcMessage("You can't ask for the particles on an artificial "
484 "cell since we don't know what exists on these "
485 "kinds of cells."));
486
487 return {};
488 }
489
490
491
492 template <int dim, int spacedim>
493 void
496 {
497 auto &particles_on_cell = particle->particles_in_cell->particles;
498
499 // if the particle has an invalid handle (e.g. because it has
500 // been duplicated before calling this function) do not try
501 // to deallocate its memory again
502 auto handle = particle->get_handle();
504 property_pool->deregister_particle(handle);
505
506 // need to reduce the cached number before deleting, because the iterator
507 // may be invalid after removing the particle even if only
508 // accessing the cell
509 const auto cell = particle->get_surrounding_cell();
510 const bool owned_cell = cell->is_locally_owned();
511 if (owned_cell)
512 --number_of_locally_owned_particles;
513
514 if (particles_on_cell.size() > 1)
515 {
516 particles_on_cell[particle->particle_index_within_cell] =
517 std::move(particles_on_cell.back());
518 particles_on_cell.resize(particles_on_cell.size() - 1);
519 }
520 else
521 {
522 particles.erase(particle->particles_in_cell);
523 cells_to_particle_cache[cell->active_cell_index()] = particles.end();
524 }
525 }
526
527
528
529 template <int dim, int spacedim>
530 void
533 &particles_to_remove)
534 {
535 // We need to remove particles backwards on each cell to keep the particle
536 // iterators alive as we keep removing particles on the same cell. To
537 // ensure that this is safe, we either check if we already have sorted
538 // iterators or if we need to manually sort
539 const auto check_greater = [](const particle_iterator &a,
540 const particle_iterator &b) {
541 return a->particles_in_cell->cell > b->particles_in_cell->cell ||
542 (a->particles_in_cell->cell == b->particles_in_cell->cell &&
543 a->particle_index_within_cell > b->particle_index_within_cell);
544 };
545
546 bool particles_are_sorted = true;
547 auto previous = particles_to_remove.begin();
548 for (auto next = previous; next != particles_to_remove.end(); ++next)
549 {
550 if (check_greater(*previous, *next))
551 {
552 particles_are_sorted = false;
553 break;
554 }
555 previous = next;
556 }
557 if (particles_are_sorted)
558 {
559 // pass along backwards in array
560 for (auto it = particles_to_remove.rbegin();
561 it != particles_to_remove.rend();
562 ++it)
563 remove_particle(*it);
564 }
565 else
566 {
567 std::vector<ParticleHandler<dim, spacedim>::particle_iterator>
568 sorted_particles(particles_to_remove);
569 std::sort(sorted_particles.begin(),
570 sorted_particles.end(),
571 check_greater);
572
573 for (const auto &particle : sorted_particles)
574 remove_particle(particle);
575 }
576
577 update_cached_numbers();
578 }
579
580
581
582 template <int dim, int spacedim>
585 const Particle<dim, spacedim> &particle,
587 {
588 return insert_particle(particle.get_location(),
589 particle.get_reference_location(),
590 particle.get_id(),
591 cell,
592 particle.get_properties());
593 }
594
595
596
597 template <int dim, int spacedim>
600 const typename PropertyPool<dim, spacedim>::Handle handle,
602 {
603 const unsigned int active_cell_index = cell->active_cell_index();
604 typename particle_container::iterator &cache =
605 cells_to_particle_cache[active_cell_index];
606 if (cache == particles.end())
607 {
608 const typename particle_container::iterator insert_position =
609 cell->is_locally_owned() ? particle_container_owned_end() :
610 particle_container_ghost_end();
611 cache = particles.emplace(
612 insert_position,
613 std::vector<typename PropertyPool<dim, spacedim>::Handle>{handle},
614 cell);
615 }
616 else
617 {
618 cache->particles.push_back(handle);
619 Assert(cache->cell == cell, ExcInternalError());
620 }
621 return particle_iterator(cache,
622 *property_pool,
623 cache->particles.size() - 1);
624 }
625
626
627
628 template <int dim, int spacedim>
631 const void *&data,
633 {
635 Assert(cells_to_particle_cache.size() == triangulation->n_active_cells(),
637 Assert(cell->is_locally_owned(),
638 ExcMessage("You tried to insert particles into a cell that is not "
639 "locally owned. This is not supported."));
640
641 particle_iterator particle_it =
642 insert_particle(property_pool->register_particle(), cell);
643
644 data = particle_it->read_particle_data_from_memory(data);
645
646 ++number_of_locally_owned_particles;
647
648 return particle_it;
649 }
650
651
652
653 template <int dim, int spacedim>
656 const Point<spacedim> &position,
657 const Point<dim> &reference_position,
658 const types::particle_index particle_index,
660 const ArrayView<const double> &properties)
661 {
663 Assert(cells_to_particle_cache.size() == triangulation->n_active_cells(),
666 Assert(cell->is_locally_owned(),
667 ExcMessage("You tried to insert particles into a cell that is not "
668 "locally owned. This is not supported."));
669
670 particle_iterator particle_it =
671 insert_particle(property_pool->register_particle(), cell);
672
673 particle_it->set_location(position);
674 particle_it->set_reference_location(reference_position);
675 particle_it->set_id(particle_index);
676
677 if (properties.size() != 0)
678 particle_it->set_properties(properties);
679
680 ++number_of_locally_owned_particles;
681
682 return particle_it;
683 }
684
685
686
687 template <int dim, int spacedim>
688 void
690 const std::multimap<
692 Particle<dim, spacedim>> &new_particles)
693 {
694 reserve(n_locally_owned_particles() + new_particles.size());
695 for (const auto &cell_and_particle : new_particles)
696 insert_particle(cell_and_particle.second, cell_and_particle.first);
697
698 update_cached_numbers();
699 }
700
701
702
703 template <int dim, int spacedim>
704 void
706 const std::vector<Point<spacedim>> &positions)
707 {
709
710 update_cached_numbers();
711 reserve(n_locally_owned_particles() + positions.size());
712
713 // Determine the starting particle index of this process, which
714 // is the highest currently existing particle index plus the sum
715 // of the number of newly generated particles of all
716 // processes with a lower rank if in a parallel computation.
717 const types::particle_index local_next_particle_index =
718 get_next_free_particle_index();
719 types::particle_index local_start_index = 0;
720
721#ifdef DEAL_II_WITH_MPI
722 if (const auto parallel_triangulation =
723 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
724 &*triangulation))
725 {
726 types::particle_index particles_to_add_locally = positions.size();
727 const int ierr =
728 MPI_Scan(&particles_to_add_locally,
729 &local_start_index,
730 1,
731 Utilities::MPI::mpi_type_id_for_type<types::particle_index>,
732 MPI_SUM,
733 parallel_triangulation->get_mpi_communicator());
734 AssertThrowMPI(ierr);
735 local_start_index -= particles_to_add_locally;
736 }
737#endif
738
739 local_start_index += local_next_particle_index;
740
741 auto point_locations =
743 positions);
744
745 auto &cells = std::get<0>(point_locations);
746 auto &local_positions = std::get<1>(point_locations);
747 auto &index_map = std::get<2>(point_locations);
748 auto &missing_points = std::get<3>(point_locations);
749 // If a point was not found, throwing an error, as the old
750 // implementation of compute_point_locations would have done
751 AssertThrow(missing_points.empty(),
753
754 (void)missing_points;
755
756 for (unsigned int i = 0; i < cells.size(); ++i)
757 for (unsigned int p = 0; p < local_positions[i].size(); ++p)
758 insert_particle(positions[index_map[i][p]],
759 local_positions[i][p],
760 local_start_index + index_map[i][p],
761 cells[i]);
762
763 update_cached_numbers();
764 }
765
766
767
768 template <int dim, int spacedim>
769 std::map<unsigned int, IndexSet>
771 const std::vector<Point<spacedim>> &positions,
772 const std::vector<std::vector<BoundingBox<spacedim>>>
773 &global_bounding_boxes,
774 const std::vector<std::vector<double>> &properties,
775 const std::vector<types::particle_index> &ids)
776 {
777 if (!properties.empty())
778 {
779 AssertDimension(properties.size(), positions.size());
780 if constexpr (running_in_debug_mode())
781 {
782 for (const auto &p : properties)
783 AssertDimension(p.size(), n_properties_per_particle());
784 }
785 }
786
787 if (!ids.empty())
788 AssertDimension(ids.size(), positions.size());
789
790 const auto comm = triangulation->get_mpi_communicator();
791
792 const auto n_mpi_processes = Utilities::MPI::n_mpi_processes(comm);
793
794 // Compute the global number of properties
795 const auto n_global_properties =
796 Utilities::MPI::sum(properties.size(), comm);
797
798 // Gather the number of points per processor
799 const auto n_particles_per_proc =
800 Utilities::MPI::all_gather(comm, positions.size());
801
802 // Calculate all starting points locally
803 std::vector<unsigned int> particle_start_indices(n_mpi_processes);
804
805 unsigned int particle_start_index = get_next_free_particle_index();
806 for (unsigned int process = 0; process < particle_start_indices.size();
807 ++process)
808 {
809 particle_start_indices[process] = particle_start_index;
810 particle_start_index += n_particles_per_proc[process];
811 }
812
813 // Get all local information
814 const auto cells_positions_and_index_maps =
816 positions,
817 global_bounding_boxes);
818
819 // Unpack the information into several vectors:
820 // All cells that contain at least one particle
821 const auto &local_cells_containing_particles =
822 std::get<0>(cells_positions_and_index_maps);
823
824 // The reference position of every particle in the local part of the
825 // triangulation.
826 const auto &local_reference_positions =
827 std::get<1>(cells_positions_and_index_maps);
828 // The original index in the positions vector for each particle in the
829 // local part of the triangulation
830 const auto &original_indices_of_local_particles =
831 std::get<2>(cells_positions_and_index_maps);
832 // The real spatial position of every particle in the local part of the
833 // triangulation.
834 const auto &local_positions = std::get<3>(cells_positions_and_index_maps);
835 // The MPI process that inserted each particle
836 const auto &calling_process_indices =
837 std::get<4>(cells_positions_and_index_maps);
838
839 // Create the map of cpu to indices, indicating who sent us what particle
840 std::map<unsigned int, std::vector<unsigned int>>
841 original_process_to_local_particle_indices_tmp;
842 for (unsigned int i_cell = 0;
843 i_cell < local_cells_containing_particles.size();
844 ++i_cell)
845 {
846 for (unsigned int i_particle = 0;
847 i_particle < local_positions[i_cell].size();
848 ++i_particle)
849 {
850 const unsigned int local_id_on_calling_process =
851 original_indices_of_local_particles[i_cell][i_particle];
852 const unsigned int calling_process =
853 calling_process_indices[i_cell][i_particle];
854
855 original_process_to_local_particle_indices_tmp[calling_process]
856 .push_back(local_id_on_calling_process);
857 }
858 }
859 std::map<unsigned int, IndexSet> original_process_to_local_particle_indices;
860 for (auto &process_and_particle_indices :
861 original_process_to_local_particle_indices_tmp)
862 {
863 const unsigned int calling_process = process_and_particle_indices.first;
864 original_process_to_local_particle_indices.insert(
865 {calling_process, IndexSet(n_particles_per_proc[calling_process])});
866 std::sort(process_and_particle_indices.second.begin(),
867 process_and_particle_indices.second.end());
868 original_process_to_local_particle_indices[calling_process].add_indices(
869 process_and_particle_indices.second.begin(),
870 process_and_particle_indices.second.end());
871 original_process_to_local_particle_indices[calling_process].compress();
872 }
873
874 // A map from mpi process to properties, ordered as in the IndexSet.
875 // Notice that this ordering may be different from the ordering in the
876 // vectors above, since no local ordering is guaranteed by the
877 // distribute_compute_point_locations() call.
878 // This is only filled if n_global_properties is > 0
879 std::map<unsigned int, std::vector<std::vector<double>>>
880 locally_owned_properties_from_other_processes;
881
882 // A map from mpi process to ids, ordered as in the IndexSet.
883 // Notice that this ordering may be different from the ordering in the
884 // vectors above, since no local ordering is guaranteed by the
885 // distribute_compute_point_locations() call.
886 // This is only filled if ids.size() is > 0
887 std::map<unsigned int, std::vector<types::particle_index>>
888 locally_owned_ids_from_other_processes;
889
890 if (n_global_properties > 0 || !ids.empty())
891 {
892 // Gather whom I sent my own particles to, to decide whom to send
893 // the particle properties or the ids
894 auto send_to_cpu = Utilities::MPI::some_to_some(
895 comm, original_process_to_local_particle_indices);
896
897 // Prepare the vector of properties to send
898 if (n_global_properties > 0)
899 {
900 std::map<unsigned int, std::vector<std::vector<double>>>
901 non_locally_owned_properties;
902
903 for (const auto &it : send_to_cpu)
904 {
905 std::vector<std::vector<double>> properties_to_send(
906 it.second.n_elements(),
907 std::vector<double>(n_properties_per_particle()));
908 unsigned int index = 0;
909 for (const auto el : it.second)
910 properties_to_send[index++] = properties[el];
911 non_locally_owned_properties.insert(
912 {it.first, properties_to_send});
913 }
914
915 // Send the non locally owned properties to each mpi process
916 // that needs them
917 locally_owned_properties_from_other_processes =
918 Utilities::MPI::some_to_some(comm, non_locally_owned_properties);
919
921 locally_owned_properties_from_other_processes.size(),
922 original_process_to_local_particle_indices.size());
923 }
924
925 if (!ids.empty())
926 {
927 std::map<unsigned int, std::vector<types::particle_index>>
928 non_locally_owned_ids;
929 for (const auto &it : send_to_cpu)
930 {
931 std::vector<types::particle_index> ids_to_send(
932 it.second.n_elements());
933 unsigned int index = 0;
934 for (const auto el : it.second)
935 ids_to_send[index++] = ids[el];
936 non_locally_owned_ids.insert({it.first, ids_to_send});
937 }
938
939 // Send the non locally owned ids to each mpi process
940 // that needs them
941 locally_owned_ids_from_other_processes =
942 Utilities::MPI::some_to_some(comm, non_locally_owned_ids);
943
944 AssertDimension(locally_owned_ids_from_other_processes.size(),
945 original_process_to_local_particle_indices.size());
946 }
947 }
948
949 // Now fill up the actual particles
950 for (unsigned int i_cell = 0;
951 i_cell < local_cells_containing_particles.size();
952 ++i_cell)
953 {
954 for (unsigned int i_particle = 0;
955 i_particle < local_positions[i_cell].size();
956 ++i_particle)
957 {
958 const unsigned int local_id_on_calling_process =
959 original_indices_of_local_particles[i_cell][i_particle];
960
961 const unsigned int calling_process =
962 calling_process_indices[i_cell][i_particle];
963
964 const unsigned int index_within_set =
965 original_process_to_local_particle_indices[calling_process]
966 .index_within_set(local_id_on_calling_process);
967
968 const unsigned int particle_id =
969 ids.empty() ?
970 local_id_on_calling_process +
971 particle_start_indices[calling_process] :
972 locally_owned_ids_from_other_processes[calling_process]
973 [index_within_set];
974
975 auto particle_it =
976 insert_particle(local_positions[i_cell][i_particle],
977 local_reference_positions[i_cell][i_particle],
978 particle_id,
979 local_cells_containing_particles[i_cell]);
980
981 if (n_global_properties > 0)
982 {
983 particle_it->set_properties(
984 locally_owned_properties_from_other_processes
985 [calling_process][index_within_set]);
986 }
987 }
988 }
989
990 update_cached_numbers();
991
992 return original_process_to_local_particle_indices;
993 }
994
995
996
997 template <int dim, int spacedim>
998 std::map<unsigned int, IndexSet>
1000 const std::vector<Particle<dim, spacedim>> &particles,
1001 const std::vector<std::vector<BoundingBox<spacedim>>>
1002 &global_bounding_boxes)
1003 {
1004 // Store the positions in a vector of points, the ids in a vector of ids,
1005 // and the properties, if any, in a vector of vector of properties.
1006 std::vector<Point<spacedim>> positions;
1007 std::vector<std::vector<double>> properties;
1008 std::vector<types::particle_index> ids;
1009 positions.resize(particles.size());
1010 ids.resize(particles.size());
1011 if (n_properties_per_particle() > 0)
1012 properties.resize(particles.size(),
1013 std::vector<double>(n_properties_per_particle()));
1014
1015 unsigned int i = 0;
1016 for (const auto &p : particles)
1017 {
1018 positions[i] = p.get_location();
1019 ids[i] = p.get_id();
1020 if (p.has_properties())
1021 properties[i] = {p.get_properties().begin(),
1022 p.get_properties().end()};
1023 ++i;
1024 }
1025
1026 return insert_global_particles(positions,
1027 global_bounding_boxes,
1028 properties,
1029 ids);
1030 }
1031
1032
1033
1034 template <int dim, int spacedim>
1037 {
1038 return global_number_of_particles;
1039 }
1040
1041
1042
1043 template <int dim, int spacedim>
1046 {
1047 return global_max_particles_per_cell;
1048 }
1049
1050
1051
1052 template <int dim, int spacedim>
1055 {
1056 return number_of_locally_owned_particles;
1057 }
1058
1059
1060
1061 template <int dim, int spacedim>
1062 unsigned int
1064 {
1065 return property_pool->n_properties_per_slot();
1066 }
1067
1068
1069
1070 template <int dim, int spacedim>
1073 {
1074 return next_free_particle_index;
1075 }
1076
1077
1078
1079 template <int dim, int spacedim>
1080 IndexSet
1082 {
1083 IndexSet set(get_next_free_particle_index());
1084 std::vector<types::particle_index> indices;
1085 indices.reserve(n_locally_owned_particles());
1086 for (const auto &p : *this)
1087 indices.push_back(p.get_id());
1088 set.add_indices(indices.begin(), indices.end());
1089 set.compress();
1090 return set;
1091 }
1092
1093
1094
1095 template <int dim, int spacedim>
1098 {
1099 return property_pool->n_slots();
1100 }
1101
1102
1103
1104 template <int dim, int spacedim>
1105 void
1107 std::vector<Point<spacedim>> &positions,
1108 const bool add_to_output_vector)
1109 {
1110 // There should be one point per particle to gather
1111 AssertDimension(positions.size(), n_locally_owned_particles());
1112
1113 unsigned int i = 0;
1114 for (auto it = begin(); it != end(); ++it, ++i)
1115 {
1116 if (add_to_output_vector)
1117 positions[i] = positions[i] + it->get_location();
1118 else
1119 positions[i] = it->get_location();
1120 }
1121 }
1122
1123
1124
1125 template <int dim, int spacedim>
1126 void
1128 const std::vector<Point<spacedim>> &new_positions,
1129 const bool displace_particles)
1130 {
1131 // There should be one point per particle to fix the new position
1132 AssertDimension(new_positions.size(), n_locally_owned_particles());
1133
1134 unsigned int i = 0;
1135 for (auto it = begin(); it != end(); ++it, ++i)
1136 {
1137 Point<spacedim> &location = it->get_location();
1138 if (displace_particles)
1139 location += new_positions[i];
1140 else
1141 location = new_positions[i];
1142 }
1143 sort_particles_into_subdomains_and_cells();
1144 }
1145
1146
1147
1148 template <int dim, int spacedim>
1149 void
1151 const Function<spacedim> &function,
1152 const bool displace_particles)
1153 {
1154 // The function should have sufficient components to displace the
1155 // particles
1156 AssertDimension(function.n_components, spacedim);
1157
1158 Vector<double> new_position(spacedim);
1159 for (auto &particle : *this)
1160 {
1161 Point<spacedim> &particle_location = particle.get_location();
1162 function.vector_value(particle_location, new_position);
1163 if (displace_particles)
1164 for (unsigned int d = 0; d < spacedim; ++d)
1165 particle_location[d] += new_position[d];
1166 else
1167 for (unsigned int d = 0; d < spacedim; ++d)
1168 particle_location[d] = new_position[d];
1169 }
1170 sort_particles_into_subdomains_and_cells();
1171 }
1172
1173
1174
1175 template <int dim, int spacedim>
1178 {
1179 return *property_pool;
1180 }
1181
1182
1183
1184 namespace
1185 {
1195 template <int dim>
1196 bool
1197 compare_particle_association(
1198 const unsigned int a,
1199 const unsigned int b,
1200 const Tensor<1, dim> &particle_direction,
1201 const std::vector<Tensor<1, dim>> &center_directions)
1202 {
1203 const double scalar_product_a = center_directions[a] * particle_direction;
1204 const double scalar_product_b = center_directions[b] * particle_direction;
1205
1206 // The function is supposed to return if a is before b. We are looking
1207 // for the alignment of particle direction and center direction,
1208 // therefore return if the scalar product of a is larger.
1209 return (scalar_product_a > scalar_product_b);
1210 }
1211 } // namespace
1212
1213
1214
1215 template <int dim, int spacedim>
1216 void
1218 {
1219 Assert(triangulation != nullptr, ExcInternalError());
1220 Assert(cells_to_particle_cache.size() == triangulation->n_active_cells(),
1222
1223 // TODO: The current algorithm only works for particles that are in
1224 // the local domain or in ghost cells, because it only knows the
1225 // subdomain_id of ghost cells, but not of artificial cells. This
1226 // can be extended using the distributed version of compute point
1227 // locations.
1228 // TODO: Extend this function to allow keeping particles on other
1229 // processes around (with an invalid cell).
1230
1231 std::vector<particle_iterator> particles_out_of_cell;
1232
1233 // Reserve some space for particles that need sorting to avoid frequent
1234 // re-allocation. Guess 25% of particles need sorting. Balance memory
1235 // overhead and performance.
1236 particles_out_of_cell.reserve(n_locally_owned_particles() / 4);
1237
1238 // Now update the reference locations of the moved particles
1239 std::vector<Point<spacedim>> real_locations;
1240 std::vector<Point<dim>> reference_locations;
1241 real_locations.reserve(global_max_particles_per_cell);
1242 reference_locations.reserve(global_max_particles_per_cell);
1243
1244 for (const auto &cell : triangulation->active_cell_iterators())
1245 {
1246 // Particles can be inserted into arbitrary cells, e.g. if their cell is
1247 // not known. However, for artificial cells we can not evaluate
1248 // the reference position of particles. Do not sort particles that are
1249 // not locally owned, because they will be sorted by the process that
1250 // owns them.
1251 if (cell->is_locally_owned() == false)
1252 {
1253 continue;
1254 }
1255
1256 const unsigned int n_pic = n_particles_in_cell(cell);
1257 auto pic = particles_in_cell(cell);
1258
1259 real_locations.clear();
1260 for (const auto &particle : pic)
1261 real_locations.push_back(particle.get_location());
1262
1263 reference_locations.resize(n_pic);
1264 mapping->transform_points_real_to_unit_cell(cell,
1265 real_locations,
1266 reference_locations);
1267
1268 auto particle = pic.begin();
1269 for (const auto &p_unit : reference_locations)
1270 {
1271 if (numbers::is_finite(p_unit[0]) &&
1272 cell->reference_cell().contains_point(p_unit,
1273 tolerance_inside_cell))
1274 particle->set_reference_location(p_unit);
1275 else
1276 particles_out_of_cell.push_back(particle);
1277
1278 ++particle;
1279 }
1280 }
1281
1282 // There are three reasons why a particle is not in its old cell:
1283 // It moved to another cell, to another subdomain or it left the mesh.
1284 // Particles that moved to another cell are updated and moved inside the
1285 // particles vector, particles that moved to another domain are
1286 // collected in the moved_particles_domain vector. Particles that left
1287 // the mesh completely are ignored and removed.
1288 std::map<types::subdomain_id, std::vector<particle_iterator>>
1289 moved_particles;
1290 std::map<
1292 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>>
1293 moved_cells;
1294
1295 // We do not know exactly how many particles are lost, exchanged between
1296 // domains, or remain on this process. Therefore we pre-allocate
1297 // approximate sizes for these vectors. If more space is needed an
1298 // automatic and relatively fast (compared to other parts of this
1299 // algorithm) re-allocation will happen.
1300 std::set<types::subdomain_id> ghost_owners;
1301 if (const auto parallel_triangulation =
1302 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1303 &*triangulation))
1304 ghost_owners = parallel_triangulation->ghost_owners();
1305
1306 // Reserve some space for particles that need communication to avoid
1307 // frequent re-allocation. Guess 25% of particles out of their old cell need
1308 // communication. Balance memory overhead and performance.
1309 for (const auto &ghost_owner : ghost_owners)
1310 moved_particles[ghost_owner].reserve(particles_out_of_cell.size() / 4);
1311 for (const auto &ghost_owner : ghost_owners)
1312 moved_cells[ghost_owner].reserve(particles_out_of_cell.size() / 4);
1313
1314 {
1315 // Create a map from vertices to adjacent cells using grid cache
1316 const std::vector<
1317 std::set<typename Triangulation<dim, spacedim>::active_cell_iterator>>
1318 &vertex_to_cells = triangulation_cache->get_vertex_to_cell_map();
1319
1320 // Create a corresponding map of vectors from vertex to cell center
1321 // using grid cache
1322 const std::vector<std::vector<Tensor<1, spacedim>>>
1323 &vertex_to_cell_centers =
1324 triangulation_cache->get_vertex_to_cell_centers_directions();
1325
1326 std::vector<unsigned int> search_order;
1327
1328 // Reuse these vectors below, but only with a single element.
1329 // Avoid resizing for every particle.
1330 reference_locations.resize(1, numbers::signaling_nan<Point<dim>>());
1331 real_locations.resize(1, numbers::signaling_nan<Point<spacedim>>());
1332
1333 // Find the cells that the particles moved to.
1334 for (auto &out_particle : particles_out_of_cell)
1335 {
1336 // make a copy of the current cell, since we will modify the
1337 // variable current_cell below, but we need the original in
1338 // the case the particle is not found
1339 auto current_cell = out_particle->get_surrounding_cell();
1340
1341 real_locations[0] = out_particle->get_location();
1342
1343 // Record if the new cell was found
1344 bool found_cell = false;
1345
1346 // Check if the particle is in one of the old cell's neighbors
1347 // that are adjacent to the closest vertex
1348 const unsigned int closest_vertex =
1349 GridTools::find_closest_vertex_of_cell<dim, spacedim>(
1350 current_cell, out_particle->get_location(), *mapping);
1351 const unsigned int closest_vertex_index =
1352 current_cell->vertex_index(closest_vertex);
1353
1354 const auto &candidate_cells = vertex_to_cells[closest_vertex_index];
1355 const unsigned int n_candidate_cells = candidate_cells.size();
1356
1357 // The order of searching through the candidate cells matters for
1358 // performance reasons. Start with a simple order.
1359 search_order.resize(n_candidate_cells);
1360 for (unsigned int i = 0; i < n_candidate_cells; ++i)
1361 search_order[i] = i;
1362
1363 // If the particle is not on a vertex, we can do better by
1364 // sorting the candidate cells by alignment with
1365 // the vertex_to_particle direction.
1366 Tensor<1, spacedim> vertex_to_particle =
1367 out_particle->get_location() - current_cell->vertex(closest_vertex);
1368
1369 // Only do this if the particle is not on a vertex, otherwise we
1370 // cannot normalize
1371 if (vertex_to_particle.norm_square() >
1372 1e4 * std::numeric_limits<double>::epsilon() *
1373 std::numeric_limits<double>::epsilon() *
1374 vertex_to_cell_centers[closest_vertex_index][0].norm_square())
1375 {
1376 vertex_to_particle /= vertex_to_particle.norm();
1377 const auto &vertex_to_cells =
1378 vertex_to_cell_centers[closest_vertex_index];
1379
1380 std::sort(search_order.begin(),
1381 search_order.end(),
1382 [&vertex_to_particle,
1383 &vertex_to_cells](const unsigned int a,
1384 const unsigned int b) {
1385 return compare_particle_association(
1386 a, b, vertex_to_particle, vertex_to_cells);
1387 });
1388 }
1389
1390 // Search all of the candidate cells according to the determined
1391 // order. Most likely we will find the particle in them.
1392 for (unsigned int i = 0; i < n_candidate_cells; ++i)
1393 {
1394 typename std::set<
1396 const_iterator candidate_cell = candidate_cells.begin();
1397
1398 std::advance(candidate_cell, search_order[i]);
1399 mapping->transform_points_real_to_unit_cell(*candidate_cell,
1400 real_locations,
1401 reference_locations);
1402
1403 if ((*candidate_cell)
1404 ->reference_cell()
1405 .contains_point(reference_locations[0],
1406 tolerance_inside_cell))
1407 {
1408 current_cell = *candidate_cell;
1409 found_cell = true;
1410 break;
1411 }
1412 }
1413
1414 // If we did not find a cell the particle is not in a neighbor of
1415 // its old cell. Look for the new cell in the whole local domain.
1416 // This case should be rare.
1417 if (!found_cell)
1418 {
1419 // For some clang-based compilers and boost versions the call to
1420 // RTree::query doesn't compile. We use a slower implementation as
1421 // workaround.
1422 // This is fixed in boost in
1423 // https://github.com/boostorg/numeric_conversion/commit/50a1eae942effb0a9b90724323ef8f2a67e7984a
1424#if defined(DEAL_II_WITH_BOOST_BUNDLED) || \
1425 !(defined(__clang_major__) && __clang_major__ >= 16) || \
1426 BOOST_VERSION >= 108100
1427
1428 std::vector<std::pair<Point<spacedim>, unsigned int>>
1429 closest_vertex_in_domain;
1430 triangulation_cache->get_used_vertices_rtree().query(
1431 boost::geometry::index::nearest(out_particle->get_location(),
1432 1),
1433 std::back_inserter(closest_vertex_in_domain));
1434
1435 // We should have one and only one result
1436 AssertDimension(closest_vertex_in_domain.size(), 1);
1437 const unsigned int closest_vertex_index_in_domain =
1438 closest_vertex_in_domain[0].second;
1439#else
1440 const unsigned int closest_vertex_index_in_domain =
1443 out_particle->get_location());
1444#endif
1445
1446 // Search all of the cells adjacent to the closest vertex of the
1447 // domain. Most likely we will find the particle in them.
1448 for (const auto &cell :
1449 vertex_to_cells[closest_vertex_index_in_domain])
1450 {
1451 mapping->transform_points_real_to_unit_cell(
1452 cell, real_locations, reference_locations);
1453
1454 if (cell->reference_cell().contains_point(
1455 reference_locations[0], tolerance_inside_cell))
1456 {
1457 current_cell = cell;
1458 found_cell = true;
1459 break;
1460 }
1461 }
1462 }
1463
1464 if (!found_cell)
1465 {
1466 // We can find no cell for this particle. It has left the
1467 // domain due to an integration error or an open boundary.
1468 // Signal the loss and move on.
1469 signals.particle_lost(out_particle,
1470 out_particle->get_surrounding_cell());
1471 continue;
1472 }
1473
1474 // If we are here, we found a cell and reference position for this
1475 // particle
1476 out_particle->set_reference_location(reference_locations[0]);
1477
1478 // Reinsert the particle into our domain if we own its cell.
1479 // Mark it for MPI transfer otherwise
1480 if (current_cell->is_locally_owned())
1481 {
1483 out_particle->particles_in_cell
1484 ->particles[out_particle->particle_index_within_cell];
1485
1486 // Avoid deallocating the memory of this particle
1487 const auto old_value = old;
1489
1490 // Allocate particle with the old handle
1491 insert_particle(old_value, current_cell);
1492 }
1493 else
1494 {
1495 moved_particles[current_cell->subdomain_id()].push_back(
1496 out_particle);
1497 moved_cells[current_cell->subdomain_id()].push_back(current_cell);
1498 }
1499 }
1500 }
1501
1502 // Exchange particles between processors if we have more than one process
1503#ifdef DEAL_II_WITH_MPI
1504 if (const auto parallel_triangulation =
1505 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1506 &*triangulation))
1507 {
1509 parallel_triangulation->get_mpi_communicator()) > 1)
1510 send_recv_particles(moved_particles, moved_cells);
1511 }
1512#endif
1513
1514 // remove_particles also calls update_cached_numbers()
1515 remove_particles(particles_out_of_cell);
1516
1517 // now make sure particle data is sorted in order of iteration
1518 std::vector<typename PropertyPool<dim, spacedim>::Handle> unsorted_handles;
1519 unsorted_handles.reserve(property_pool->n_registered_slots());
1520
1521 typename PropertyPool<dim, spacedim>::Handle sorted_handle = 0;
1522 for (auto &particles_in_cell : particles)
1523 for (auto &particle : particles_in_cell.particles)
1524 {
1525 unsorted_handles.push_back(particle);
1526 particle = sorted_handle++;
1527 }
1528
1529 property_pool->sort_memory_slots(unsorted_handles);
1530
1531 } // namespace Particles
1532
1533
1534
1535 template <int dim, int spacedim>
1536 void
1538 const bool enable_cache)
1539 {
1540 // Nothing to do in serial computations
1541 const auto parallel_triangulation =
1542 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1543 &*triangulation);
1544 if (parallel_triangulation != nullptr)
1545 {
1547 parallel_triangulation->get_mpi_communicator()) == 1)
1548 return;
1549 }
1550 else
1551 return;
1552
1553#ifndef DEAL_II_WITH_MPI
1554 (void)enable_cache;
1555#else
1556 // Clear ghost particles and their properties
1557 for (const auto &cell : triangulation->active_cell_iterators())
1558 if (cell->is_ghost() &&
1559 cells_to_particle_cache[cell->active_cell_index()] != particles.end())
1560 {
1561 Assert(cells_to_particle_cache[cell->active_cell_index()]->cell ==
1562 cell,
1564 // Clear particle properties
1565 for (auto &ghost_particle :
1566 cells_to_particle_cache[cell->active_cell_index()]->particles)
1567 property_pool->deregister_particle(ghost_particle);
1568
1569 // Clear particles themselves
1570 particles.erase(cells_to_particle_cache[cell->active_cell_index()]);
1571 cells_to_particle_cache[cell->active_cell_index()] = particles.end();
1572 }
1573
1574 // Clear ghost particles cache and invalidate it
1575 ghost_particles_cache.ghost_particles_by_domain.clear();
1576 ghost_particles_cache.valid = false;
1577
1578 // In the case of a parallel simulation with periodic boundary conditions
1579 // the vertices associated with periodic boundaries are not directly
1580 // connected to the ghost cells but they are connected to the ghost cells
1581 // through their coinciding vertices. We gather this information using the
1582 // vertices_with_ghost_neighbors map
1583 const std::map<unsigned int, std::set<types::subdomain_id>>
1585 triangulation_cache->get_vertices_with_ghost_neighbors();
1586
1587 const std::set<types::subdomain_id> ghost_owners =
1588 parallel_triangulation->ghost_owners();
1589 for (const auto ghost_owner : ghost_owners)
1590 ghost_particles_cache.ghost_particles_by_domain[ghost_owner].reserve(
1591 n_locally_owned_particles() / 4);
1592
1593 const std::vector<std::set<unsigned int>> vertex_to_neighbor_subdomain =
1594 triangulation_cache->get_vertex_to_neighbor_subdomain();
1595
1596 for (const auto &cell : triangulation->active_cell_iterators())
1597 {
1598 if (cell->is_locally_owned())
1599 {
1600 std::set<unsigned int> cell_to_neighbor_subdomain;
1601 for (const unsigned int v : cell->vertex_indices())
1602 {
1603 const auto vertex_ghost_neighbors =
1604 vertices_with_ghost_neighbors.find(cell->vertex_index(v));
1605 if (vertex_ghost_neighbors !=
1607 {
1608 cell_to_neighbor_subdomain.insert(
1609 vertex_ghost_neighbors->second.begin(),
1610 vertex_ghost_neighbors->second.end());
1611 }
1612 }
1613
1614 if (cell_to_neighbor_subdomain.size() > 0)
1615 {
1616 const particle_iterator_range particle_range =
1617 particles_in_cell(cell);
1618
1619 for (const auto domain : cell_to_neighbor_subdomain)
1620 {
1621 for (typename particle_iterator_range::iterator particle =
1622 particle_range.begin();
1623 particle != particle_range.end();
1624 ++particle)
1625 ghost_particles_cache.ghost_particles_by_domain[domain]
1626 .push_back(particle);
1627 }
1628 }
1629 }
1630 }
1631
1632 send_recv_particles(
1633 ghost_particles_cache.ghost_particles_by_domain,
1634 std::map<
1636 std::vector<
1638 enable_cache);
1639#endif
1640 }
1641
1642
1643
1644 template <int dim, int spacedim>
1645 void
1647 {
1648 // Nothing to do in serial computations
1649 const auto parallel_triangulation =
1650 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1651 &*triangulation);
1652 if (parallel_triangulation == nullptr ||
1654 parallel_triangulation->get_mpi_communicator()) == 1)
1655 {
1656 return;
1657 }
1658
1659
1660#ifdef DEAL_II_WITH_MPI
1661 // First clear the current ghost_particle information
1662 // ghost_particles.clear();
1663 Assert(ghost_particles_cache.valid,
1664 ExcMessage(
1665 "Ghost particles cannot be updated if they first have not been "
1666 "exchanged at least once with the cache enabled"));
1667
1668
1669 send_recv_particles_properties_and_location(
1670 ghost_particles_cache.ghost_particles_by_domain);
1671#endif
1672 }
1673
1674
1675
1676#ifdef DEAL_II_WITH_MPI
1677 template <int dim, int spacedim>
1678 void
1680 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1681 &particles_to_send,
1682 const std::map<
1685 &send_cells,
1686 const bool build_cache)
1687 {
1688 Assert(triangulation != nullptr, ExcInternalError());
1689 Assert(cells_to_particle_cache.size() == triangulation->n_active_cells(),
1691
1692 ghost_particles_cache.valid = build_cache;
1693
1694 const auto parallel_triangulation =
1695 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1696 &*triangulation);
1697 Assert(parallel_triangulation,
1698 ExcMessage("This function is only implemented for "
1699 "parallel::TriangulationBase objects."));
1700
1701 // Determine the communication pattern
1702 const std::set<types::subdomain_id> ghost_owners =
1703 parallel_triangulation->ghost_owners();
1704 const std::vector<types::subdomain_id> neighbors(ghost_owners.begin(),
1705 ghost_owners.end());
1706 const unsigned int n_neighbors = neighbors.size();
1707
1708 if (send_cells.size() != 0)
1709 Assert(particles_to_send.size() == send_cells.size(), ExcInternalError());
1710
1711 // If we do not know the subdomain this particle needs to be send to,
1712 // throw an error
1713 Assert(particles_to_send.find(numbers::artificial_subdomain_id) ==
1714 particles_to_send.end(),
1716
1717 // TODO: Implement the shipping of particles to processes that are not
1718 // ghost owners of the local domain
1719 for (auto send_particles = particles_to_send.begin();
1720 send_particles != particles_to_send.end();
1721 ++send_particles)
1722 Assert(ghost_owners.find(send_particles->first) != ghost_owners.end(),
1724
1725 std::size_t n_send_particles = 0;
1726 for (auto send_particles = particles_to_send.begin();
1727 send_particles != particles_to_send.end();
1728 ++send_particles)
1729 n_send_particles += send_particles->second.size();
1730
1731 const unsigned int cellid_size = sizeof(CellId::binary_type);
1732
1733 // Containers for the amount and offsets of data we will send
1734 // to other processors and the data itself.
1735 std::vector<unsigned int> n_send_data(n_neighbors, 0);
1736 std::vector<unsigned int> send_offsets(n_neighbors, 0);
1737 std::vector<char> send_data;
1738
1739 Particle<dim, spacedim> test_particle;
1740 test_particle.set_property_pool(*property_pool);
1741
1742 const unsigned int individual_particle_data_size =
1743 test_particle.serialized_size_in_bytes() +
1744 (size_callback ? size_callback() : 0);
1745
1746 const unsigned int individual_total_particle_data_size =
1747 individual_particle_data_size + cellid_size;
1748
1749 // Only serialize things if there are particles to be send.
1750 // We can not return early even if no particles
1751 // are send, because we might receive particles from other processes
1752 if (n_send_particles > 0)
1753 {
1754 // Allocate space for sending particle data
1755 send_data.resize(n_send_particles *
1756 individual_total_particle_data_size);
1757
1758 void *data = static_cast<void *>(&send_data.front());
1759
1760 // Serialize the data sorted by receiving process
1761 for (unsigned int i = 0; i < n_neighbors; ++i)
1762 {
1763 send_offsets[i] = reinterpret_cast<std::size_t>(data) -
1764 reinterpret_cast<std::size_t>(&send_data.front());
1765
1766 const unsigned int n_particles_to_send =
1767 particles_to_send.at(neighbors[i]).size();
1768
1769 Assert(static_cast<std::size_t>(n_particles_to_send) *
1770 individual_total_particle_data_size ==
1771 static_cast<std::size_t>(
1772 n_particles_to_send *
1773 individual_total_particle_data_size),
1774 ExcMessage("Overflow when trying to send particle "
1775 "data"));
1776
1777 for (unsigned int j = 0; j < n_particles_to_send; ++j)
1778 {
1779 // If no target cells are given, use the iterator
1780 // information
1782 cell;
1783 if (send_cells.empty())
1784 cell = particles_to_send.at(neighbors[i])[j]
1785 ->get_surrounding_cell();
1786 else
1787 cell = send_cells.at(neighbors[i])[j];
1788
1789 const CellId::binary_type cellid =
1790 cell->id().template to_binary<dim>();
1791 memcpy(data, &cellid, cellid_size);
1792 data = static_cast<char *>(data) + cellid_size;
1793
1794 data = particles_to_send.at(neighbors[i])[j]
1795 ->write_particle_data_to_memory(data);
1796 if (store_callback)
1797 data =
1798 store_callback(particles_to_send.at(neighbors[i])[j], data);
1799 }
1800 n_send_data[i] = n_particles_to_send;
1801 }
1802 }
1803
1804 // Containers for the data we will receive from other processors
1805 std::vector<unsigned int> n_recv_data(n_neighbors);
1806 std::vector<unsigned int> recv_offsets(n_neighbors);
1807
1808 {
1809 const int mpi_tag = Utilities::MPI::internal::Tags::
1811
1812 std::vector<MPI_Request> n_requests(2 * n_neighbors);
1813 for (unsigned int i = 0; i < n_neighbors; ++i)
1814 {
1815 const int ierr =
1816 MPI_Irecv(&(n_recv_data[i]),
1817 1,
1818 MPI_UNSIGNED,
1819 neighbors[i],
1820 mpi_tag,
1821 parallel_triangulation->get_mpi_communicator(),
1822 &(n_requests[2 * i]));
1823 AssertThrowMPI(ierr);
1824 }
1825 for (unsigned int i = 0; i < n_neighbors; ++i)
1826 {
1827 const int ierr =
1828 MPI_Isend(&(n_send_data[i]),
1829 1,
1830 MPI_UNSIGNED,
1831 neighbors[i],
1832 mpi_tag,
1833 parallel_triangulation->get_mpi_communicator(),
1834 &(n_requests[2 * i + 1]));
1835 AssertThrowMPI(ierr);
1836 }
1837 const int ierr =
1838 MPI_Waitall(2 * n_neighbors, n_requests.data(), MPI_STATUSES_IGNORE);
1839 AssertThrowMPI(ierr);
1840 }
1841
1842 // Determine how many particles and data we will receive
1843 unsigned int total_recv_data = 0;
1844 for (unsigned int neighbor_id = 0; neighbor_id < n_neighbors; ++neighbor_id)
1845 {
1846 recv_offsets[neighbor_id] = total_recv_data;
1847 total_recv_data +=
1848 n_recv_data[neighbor_id] * individual_total_particle_data_size;
1849 }
1850
1851 // Set up the space for the received particle data
1852 std::vector<char> recv_data(total_recv_data);
1853
1854 // Exchange the particle data between domains
1855 {
1856 std::vector<MPI_Request> requests(2 * n_neighbors);
1857 unsigned int send_ops = 0;
1858 unsigned int recv_ops = 0;
1859
1860 const int mpi_tag = Utilities::MPI::internal::Tags::
1862
1863 for (unsigned int i = 0; i < n_neighbors; ++i)
1864 if (n_recv_data[i] > 0)
1865 {
1866 const int ierr =
1867 MPI_Irecv(&(recv_data[recv_offsets[i]]),
1868 n_recv_data[i] * individual_total_particle_data_size,
1869 MPI_CHAR,
1870 neighbors[i],
1871 mpi_tag,
1872 parallel_triangulation->get_mpi_communicator(),
1873 &(requests[send_ops]));
1874 AssertThrowMPI(ierr);
1875 ++send_ops;
1876 }
1877
1878 for (unsigned int i = 0; i < n_neighbors; ++i)
1879 if (n_send_data[i] > 0)
1880 {
1881 const int ierr =
1882 MPI_Isend(&(send_data[send_offsets[i]]),
1883 n_send_data[i] * individual_total_particle_data_size,
1884 MPI_CHAR,
1885 neighbors[i],
1886 mpi_tag,
1887 parallel_triangulation->get_mpi_communicator(),
1888 &(requests[send_ops + recv_ops]));
1889 AssertThrowMPI(ierr);
1890 ++recv_ops;
1891 }
1892 const int ierr =
1893 MPI_Waitall(send_ops + recv_ops, requests.data(), MPI_STATUSES_IGNORE);
1894 AssertThrowMPI(ierr);
1895 }
1896
1897 // Put the received particles into the domain if they are in the
1898 // triangulation
1899 const void *recv_data_it = static_cast<const void *>(recv_data.data());
1900
1901 // Store the particle iterators in the cache
1902 auto &ghost_particles_iterators =
1903 ghost_particles_cache.ghost_particles_iterators;
1904
1905 if (build_cache)
1906 {
1907 ghost_particles_iterators.clear();
1908
1909 auto &send_pointers_particles = ghost_particles_cache.send_pointers;
1910 send_pointers_particles.assign(n_neighbors + 1, 0);
1911
1912 for (unsigned int i = 0; i < n_neighbors; ++i)
1913 send_pointers_particles[i + 1] =
1914 send_pointers_particles[i] +
1915 n_send_data[i] * individual_particle_data_size;
1916
1917 auto &recv_pointers_particles = ghost_particles_cache.recv_pointers;
1918 recv_pointers_particles.assign(n_neighbors + 1, 0);
1919
1920 for (unsigned int i = 0; i < n_neighbors; ++i)
1921 recv_pointers_particles[i + 1] =
1922 recv_pointers_particles[i] +
1923 n_recv_data[i] * individual_particle_data_size;
1924
1925 ghost_particles_cache.neighbors = neighbors;
1926
1927 ghost_particles_cache.send_data.resize(
1928 ghost_particles_cache.send_pointers.back());
1929 ghost_particles_cache.recv_data.resize(
1930 ghost_particles_cache.recv_pointers.back());
1931 }
1932
1933 while (reinterpret_cast<std::size_t>(recv_data_it) -
1934 reinterpret_cast<std::size_t>(recv_data.data()) <
1935 total_recv_data)
1936 {
1937 CellId::binary_type binary_cellid;
1938 memcpy(&binary_cellid, recv_data_it, cellid_size);
1939 const CellId id(binary_cellid);
1940 recv_data_it = static_cast<const char *>(recv_data_it) + cellid_size;
1941
1943 triangulation->create_cell_iterator(id);
1944
1945 insert_particle(property_pool->register_particle(), cell);
1946 const typename particle_container::iterator &cache =
1947 cells_to_particle_cache[cell->active_cell_index()];
1948 Assert(cache->cell == cell, ExcInternalError());
1949
1950 particle_iterator particle_it(cache,
1951 *property_pool,
1952 cache->particles.size() - 1);
1953
1954 recv_data_it =
1955 particle_it->read_particle_data_from_memory(recv_data_it);
1956
1957 if (load_callback)
1958 recv_data_it = load_callback(particle_it, recv_data_it);
1959
1960 if (build_cache) // TODO: is this safe?
1961 ghost_particles_iterators.push_back(particle_it);
1962 }
1963
1964 AssertThrow(recv_data_it == recv_data.data() + recv_data.size(),
1965 ExcMessage(
1966 "The amount of data that was read into new particles "
1967 "does not match the amount of data sent around."));
1968 }
1969#endif
1970
1971
1972
1973#ifdef DEAL_II_WITH_MPI
1974 template <int dim, int spacedim>
1975 void
1977 const std::map<types::subdomain_id, std::vector<particle_iterator>>
1978 &particles_to_send)
1979 {
1980 const auto parallel_triangulation =
1981 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1982 &*triangulation);
1983 Assert(
1984 parallel_triangulation,
1985 ExcMessage(
1986 "This function is only implemented for parallel::TriangulationBase "
1987 "objects."));
1988
1989 const auto &neighbors = ghost_particles_cache.neighbors;
1990 const auto &send_pointers = ghost_particles_cache.send_pointers;
1991 const auto &recv_pointers = ghost_particles_cache.recv_pointers;
1992
1993 std::vector<char> &send_data = ghost_particles_cache.send_data;
1994
1995 // Fill data to send
1996 if (send_pointers.back() > 0)
1997 {
1998 void *data = static_cast<void *>(&send_data.front());
1999
2000 // Serialize the data sorted by receiving process
2001 for (const auto i : neighbors)
2002 for (const auto &p : particles_to_send.at(i))
2003 {
2004 data = p->write_particle_data_to_memory(data);
2005 if (store_callback)
2006 data = store_callback(p, data);
2007 }
2008 }
2009
2010 std::vector<char> &recv_data = ghost_particles_cache.recv_data;
2011
2012 // Exchange the particle data between domains
2013 {
2014 std::vector<MPI_Request> requests(2 * neighbors.size());
2015 unsigned int send_ops = 0;
2016 unsigned int recv_ops = 0;
2017
2018 const int mpi_tag = Utilities::MPI::internal::Tags::
2020
2021 for (unsigned int i = 0; i < neighbors.size(); ++i)
2022 if ((recv_pointers[i + 1] - recv_pointers[i]) > 0)
2023 {
2024 const int ierr =
2025 MPI_Irecv(recv_data.data() + recv_pointers[i],
2026 recv_pointers[i + 1] - recv_pointers[i],
2027 MPI_CHAR,
2028 neighbors[i],
2029 mpi_tag,
2030 parallel_triangulation->get_mpi_communicator(),
2031 &(requests[send_ops]));
2032 AssertThrowMPI(ierr);
2033 ++send_ops;
2034 }
2035
2036 for (unsigned int i = 0; i < neighbors.size(); ++i)
2037 if ((send_pointers[i + 1] - send_pointers[i]) > 0)
2038 {
2039 const int ierr =
2040 MPI_Isend(send_data.data() + send_pointers[i],
2041 send_pointers[i + 1] - send_pointers[i],
2042 MPI_CHAR,
2043 neighbors[i],
2044 mpi_tag,
2045 parallel_triangulation->get_mpi_communicator(),
2046 &(requests[send_ops + recv_ops]));
2047 AssertThrowMPI(ierr);
2048 ++recv_ops;
2049 }
2050 const int ierr =
2051 MPI_Waitall(send_ops + recv_ops, requests.data(), MPI_STATUSES_IGNORE);
2052 AssertThrowMPI(ierr);
2053 }
2054
2055 // Put the received particles into the domain if they are in the
2056 // triangulation
2057 const void *recv_data_it = static_cast<const void *>(recv_data.data());
2058
2059 // Gather ghost particle iterators from the cache
2060 auto &ghost_particles_iterators =
2061 ghost_particles_cache.ghost_particles_iterators;
2062
2063 for (auto &recv_particle : ghost_particles_iterators)
2064 {
2065 // Update particle data using previously allocated memory space
2066 // for efficiency reasons
2067 recv_data_it =
2068 recv_particle->read_particle_data_from_memory(recv_data_it);
2069
2070 Assert(recv_particle->particles_in_cell->cell->is_ghost(),
2072
2073 if (load_callback)
2074 recv_data_it = load_callback(
2075 particle_iterator(recv_particle->particles_in_cell,
2076 *property_pool,
2077 recv_particle->particle_index_within_cell),
2078 recv_data_it);
2079 }
2080
2081 AssertThrow(recv_data_it == recv_data.data() + recv_data.size(),
2082 ExcMessage(
2083 "The amount of data that was read into new particles "
2084 "does not match the amount of data sent around."));
2085 }
2086#endif
2087
2088 template <int dim, int spacedim>
2089 void
2091 const std::function<std::size_t()> &size_callb,
2092 const std::function<void *(const particle_iterator &, void *)> &store_callb,
2093 const std::function<const void *(const particle_iterator &, const void *)>
2094 &load_callb)
2095 {
2096 size_callback = size_callb;
2097 store_callback = store_callb;
2098 load_callback = load_callb;
2099 }
2100
2101
2102 template <int dim, int spacedim>
2103 void
2105 {
2106 // First disconnect existing connections
2107 for (const auto &connection : tria_listeners)
2108 connection.disconnect();
2109
2110 tria_listeners.clear();
2111
2112 tria_listeners.push_back(triangulation->signals.create.connect([&]() {
2113 this->initialize(*(this->triangulation),
2114 *(this->mapping),
2115 this->property_pool->n_properties_per_slot());
2116 }));
2117
2118 this->tria_listeners.push_back(
2119 this->triangulation->signals.clear.connect([&]() { this->clear(); }));
2120
2121 // for distributed triangulations, connect to distributed signals
2123 *>(&(*triangulation)) != nullptr)
2124 {
2125 tria_listeners.push_back(
2126 triangulation->signals.post_distributed_refinement.connect(
2127 [&]() { this->post_mesh_change_action(); }));
2128 tria_listeners.push_back(
2129 triangulation->signals.post_distributed_repartition.connect(
2130 [&]() { this->post_mesh_change_action(); }));
2131 tria_listeners.push_back(
2132 triangulation->signals.post_distributed_load.connect(
2133 [&]() { this->post_mesh_change_action(); }));
2134 }
2135 else
2136 {
2137 tria_listeners.push_back(triangulation->signals.post_refinement.connect(
2138 [&]() { this->post_mesh_change_action(); }));
2139 }
2140 }
2141
2142
2143
2144 template <int dim, int spacedim>
2145 void
2147 {
2148 Assert(triangulation != nullptr, ExcInternalError());
2149
2150 const bool distributed_triangulation =
2151 dynamic_cast<
2153 &(*triangulation)) != nullptr;
2154 (void)distributed_triangulation;
2155
2156 Assert(
2157 distributed_triangulation || number_of_locally_owned_particles == 0,
2158 ExcMessage(
2159 "Mesh refinement in a non-distributed triangulation is not supported "
2160 "by the ParticleHandler class. Either insert particles after mesh "
2161 "creation, or use a distributed triangulation."));
2162
2163 // Resize the container if it is possible without
2164 // transferring particles
2165 if (number_of_locally_owned_particles == 0)
2166 cells_to_particle_cache.resize(triangulation->n_active_cells(),
2167 particles.end());
2168 }
2169
2170
2171
2172 template <int dim, int spacedim>
2173 void
2178
2179
2180
2181 template <int dim, int spacedim>
2182 void
2184 {
2185 register_data_attach();
2186 }
2187
2188
2189
2190 template <int dim, int spacedim>
2191 void
2193 {
2194 const auto callback_function =
2196 &cell_iterator,
2197 const CellStatus cell_status) {
2198 return this->pack_callback(cell_iterator, cell_status);
2199 };
2200
2201 tria_attached_data_index =
2203 ->register_data_attach(callback_function,
2204 /*returns_variable_size_data=*/true);
2205 }
2206
2207
2208
2209 template <int dim, int spacedim>
2210 void
2212 {
2213 const bool serialization = false;
2214 notify_ready_to_unpack(serialization);
2215 }
2216
2217
2218
2219 template <int dim, int spacedim>
2220 void
2222 {
2223 const bool serialization = true;
2224 notify_ready_to_unpack(serialization);
2225 }
2226
2227
2228 template <int dim, int spacedim>
2229 void
2231 const bool serialization)
2232 {
2233 // First prepare container for insertion
2234 clear();
2235
2236 // If we are resuming from a checkpoint, we first have to register the
2237 // store function again, to set the triangulation to the same state as
2238 // before the serialization. Only afterwards we know how to deserialize the
2239 // data correctly.
2240 if (serialization)
2241 register_data_attach();
2242
2243 // Check if something was stored and load it
2244 if (tria_attached_data_index != numbers::invalid_unsigned_int)
2245 {
2246 const auto callback_function =
2248 &cell_iterator,
2249 const CellStatus cell_status,
2250 const boost::iterator_range<std::vector<char>::const_iterator>
2251 &range_iterator) {
2252 this->unpack_callback(cell_iterator, cell_status, range_iterator);
2253 };
2254
2256 ->notify_ready_to_unpack(tria_attached_data_index, callback_function);
2257
2258 // Reset handle and update global numbers.
2259 tria_attached_data_index = numbers::invalid_unsigned_int;
2260 update_cached_numbers();
2261 }
2262 }
2263
2264
2265
2266 template <int dim, int spacedim>
2267 std::vector<char>
2270 const CellStatus status) const
2271 {
2272 std::vector<particle_iterator> stored_particles_on_cell;
2273
2274 switch (status)
2275 {
2278 // If the cell persist or is refined store all particles of the
2279 // current cell.
2280 {
2281 const unsigned int n_particles = n_particles_in_cell(cell);
2282 stored_particles_on_cell.reserve(n_particles);
2283
2284 for (unsigned int i = 0; i < n_particles; ++i)
2285 stored_particles_on_cell.push_back(particle_iterator(
2286 cells_to_particle_cache[cell->active_cell_index()],
2287 *property_pool,
2288 i));
2289 }
2290 break;
2291
2293 // If this cell is the parent of children that will be coarsened,
2294 // collect the particles of all children.
2295 {
2296 for (const auto &child : cell->child_iterators())
2297 {
2298 const unsigned int n_particles = n_particles_in_cell(child);
2299
2300 stored_particles_on_cell.reserve(
2301 stored_particles_on_cell.size() + n_particles);
2302
2303 const typename particle_container::iterator &cache =
2304 cells_to_particle_cache[child->active_cell_index()];
2305 for (unsigned int i = 0; i < n_particles; ++i)
2306 stored_particles_on_cell.push_back(
2307 particle_iterator(cache, *property_pool, i));
2308 }
2309 }
2310 break;
2311
2312 default:
2314 break;
2315 }
2316
2317 return pack_particles(stored_particles_on_cell);
2318 }
2319
2320
2321
2322 template <int dim, int spacedim>
2323 void
2326 const CellStatus status,
2327 const boost::iterator_range<std::vector<char>::const_iterator> &data_range)
2328 {
2329 if (data_range.begin() == data_range.end())
2330 return;
2331
2332 const auto cell_to_store_particles =
2333 (status != CellStatus::cell_will_be_refined) ? cell : cell->child(0);
2334
2335 // deserialize particles and insert into local storage
2336 if (data_range.begin() != data_range.end())
2337 {
2338 const void *data = static_cast<const void *>(&(*data_range.begin()));
2339 const void *end = static_cast<const void *>(
2340 &(*data_range.begin()) + (data_range.end() - data_range.begin()));
2341
2342 while (data < end)
2343 {
2344 const void *old_data = data;
2345 const auto x = insert_particle(data, cell_to_store_particles);
2346
2347 // Ensure that the particle read exactly as much data as
2348 // it promised it needs to store its data
2349 const void *new_data = data;
2350 (void)old_data;
2351 (void)new_data;
2352 (void)x;
2353 AssertDimension((const char *)new_data - (const char *)old_data,
2354 x->serialized_size_in_bytes());
2355 }
2356
2357 Assert(data == end,
2358 ExcMessage(
2359 "The particle data could not be deserialized successfully. "
2360 "Check that when deserializing the particles you expect "
2361 "the same number of properties that were serialized."));
2362 }
2363
2364 auto loaded_particles_on_cell = particles_in_cell(cell_to_store_particles);
2365
2366 // now update particle storage location and properties if necessary
2367 switch (status)
2368 {
2370 {
2371 // all particles are correctly inserted
2372 }
2373 break;
2374
2376 {
2377 // all particles are in correct cell, but their reference location
2378 // has changed
2379 for (auto &particle : loaded_particles_on_cell)
2380 {
2381 const Point<dim> p_unit =
2382 mapping->transform_real_to_unit_cell(cell_to_store_particles,
2383 particle.get_location());
2384 particle.set_reference_location(p_unit);
2385 }
2386 }
2387 break;
2388
2390 {
2391 // we need to find the correct child to store the particles and
2392 // their reference location has changed
2393 typename particle_container::iterator &cache =
2394 cells_to_particle_cache[cell_to_store_particles
2395 ->active_cell_index()];
2396
2397 // make sure that the call above has inserted an entry
2398 Assert(cache != particles.end(), ExcInternalError());
2399
2400 // Cannot use range-based loop, because number of particles in cell
2401 // is going to change
2402 auto particle = loaded_particles_on_cell.begin();
2403 for (unsigned int i = 0; i < cache->particles.size();)
2404 {
2405 bool found_new_cell = false;
2406
2407 for (const auto &child : cell->child_iterators())
2408 {
2409 Assert(child->is_locally_owned(), ExcInternalError());
2410
2411 try
2412 {
2413 const Point<dim> p_unit =
2414 mapping->transform_real_to_unit_cell(
2415 child, particle->get_location());
2416 if (cell->reference_cell().contains_point(
2417 p_unit, tolerance_inside_cell))
2418 {
2419 found_new_cell = true;
2420 particle->set_reference_location(p_unit);
2421
2422 // if the particle is not in the cell we stored it
2423 // in above, its handle is in the wrong place
2424 if (child != cell_to_store_particles)
2425 {
2426 // move handle into correct cell
2427 insert_particle(cache->particles[i], child);
2428 // remove handle by replacing it with last one
2429 cache->particles[i] = cache->particles.back();
2430 cache->particles.pop_back();
2431 // no loop increment, we need to process
2432 // the new i-th particle.
2433 }
2434 else
2435 {
2436 // move on to next particle
2437 ++i;
2438 ++particle;
2439 }
2440 break;
2441 }
2442 }
2443 catch (typename Mapping<dim>::ExcTransformationFailed &)
2444 {}
2445 }
2446
2447 if (found_new_cell == false)
2448 {
2449 // If we get here, we did not find the particle in any
2450 // child. This case may happen for particles that are at the
2451 // boundary for strongly curved cells. We apply a tolerance
2452 // in the call to ReferenceCell::contains_point() to
2453 // account for this, but if that is not enough, we still
2454 // need to prevent an endless loop here. Delete the particle
2455 // and move on.
2456 signals.particle_lost(particle,
2457 particle->get_surrounding_cell());
2458 if (cache->particles[i] !=
2460 property_pool->deregister_particle(cache->particles[i]);
2461 cache->particles[i] = cache->particles.back();
2462 cache->particles.pop_back();
2463 }
2464 }
2465 // clean up in case child 0 has no particle left
2466 if (cache->particles.empty())
2467 {
2468 particles.erase(cache);
2469 cache = particles.end();
2470 }
2471 }
2472 break;
2473
2474 default:
2476 break;
2477 }
2478 }
2479} // namespace Particles
2480
2481#include "particles/particle_handler.inst"
2482
CellStatus
Definition cell_status.h:31
@ cell_will_be_refined
@ children_will_be_coarsened
std::size_t size() const
Definition array_view.h:689
std::array< unsigned int, 4 > binary_type
Definition cell_id.h:80
const unsigned int n_components
Definition function.h:164
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const
void compress() const
Definition index_set.h:1799
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
Definition index_set.h:1846
Abstract base class for mapping classes.
Definition mapping.h:320
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
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)
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
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)
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)
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::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
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
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)
void copy_from(const ParticleHandler< dim, spacedim > &particle_handler)
types::particle_index get_next_free_particle_index() const
IndexSet locally_owned_particle_ids() const
void set_property_pool(PropertyPool< dim, spacedim > &property_pool)
Definition particle.h:610
const Point< dim > & get_reference_location() const
Definition particle.h:583
const Point< spacedim > & get_location() const
Definition particle.h:556
std::size_t serialized_size_in_bytes() const
Definition particle.cc:286
types::particle_index get_id() const
Definition particle.h:592
ArrayView< double > get_properties()
Definition particle.cc:330
Definition point.h:113
numbers::NumberTraits< Number >::real_type norm() const
constexpr numbers::NumberTraits< Number >::real_type norm_square() const
IteratorState::IteratorStates state() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:35
constexpr bool running_in_debug_mode()
Definition config.h:73
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:36
#define DEAL_II_ASSERT_UNREACHABLE()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcPointNotAvailableHere()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
TriaIterator< CellAccessor< dim, spacedim > > cell_iterator
Definition tria.h:1557
std::vector< index_type > data
Definition mpi.cc:750
const MPI_Comm comm
Definition mpi.cc:928
unsigned int find_closest_vertex(const std::map< unsigned int, Point< spacedim > > &vertices, const Point< spacedim > &p)
return_type compute_point_locations_try_all(const Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &points, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell_hint=typename Triangulation< dim, spacedim >::active_cell_iterator())
return_type distributed_compute_point_locations(const GridTools::Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &local_points, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const double tolerance=1e-10, const std::vector< bool > &marked_vertices={}, const bool enforce_unique_mapping=true)
@ past_the_end
Iterator reached end of container.
@ valid
Iterator points to a valid object.
@ particle_handler_send_recv_particles_send
ParticleHandler<dim, spacedim>::send_recv_particles.
Definition mpi_tags.h:114
@ particle_handler_send_recv_particles_setup
ParticleHandler<dim, spacedim>::send_recv_particles.
Definition mpi_tags.h:110
T sum(const T &t, const MPI_Comm mpi_communicator)
std::map< unsigned int, T > some_to_some(const MPI_Comm comm, const std::map< unsigned int, T > &objects_to_send)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:105
T max(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
constexpr unsigned int invalid_unsigned_int
Definition types.h:238
T signaling_nan()
constexpr types::subdomain_id artificial_subdomain_id
Definition types.h:402
bool is_finite(const double x)
Definition numbers.h:508
STL namespace.
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
unsigned int subdomain_id
Definition types.h:52
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
std::map< unsigned int, std::set<::types::subdomain_id > > * vertices_with_ghost_neighbors