33 template <
int dim,
int spacedim>
37 std::vector<char> buffer;
39 if (particles.empty())
42 buffer.resize(particles.size() *
43 particles.front()->serialized_size_in_bytes());
44 void *current_data = buffer.data();
46 for (
const auto &particle : particles)
48 current_data = particle->write_particle_data_to_memory(current_data);
57 template <
int dim,
int spacedim>
77 template <
int dim,
int spacedim>
81 const unsigned int n_properties)
105 template <
int dim,
int spacedim>
111 connection.disconnect();
116 template <
int dim,
int spacedim>
121 const unsigned int n_properties)
131 property_pool = std::make_unique<PropertyPool<dim, spacedim>>(n_properties);
136 std::make_unique<GridTools::Cache<dim, spacedim>>(new_triangulation,
147 template <
int dim,
int spacedim>
152 const unsigned int n_properties =
158 property_pool = std::make_unique<PropertyPool<dim, spacedim>>(
180 if (!it->particles.empty())
190 template <
int dim,
int spacedim>
203 template <
int dim,
int spacedim>
225 template <
int dim,
int spacedim>
234 template <
int dim,
int spacedim>
242 past_the_end_iterator =
247 given_particles.clear();
248 for (
unsigned int i = 0; i < 3; ++i)
249 given_particles.emplace_back(
251 past_the_end_iterator);
255 ++given_particles.begin();
260 template <
int dim,
int spacedim>
265 bool sort_is_necessary =
false;
272 previous->cell > next->cell)
274 sort_is_necessary =
true;
280 if (sort_is_necessary)
298 for (
const auto &cell :
triangulation->active_cell_iterators())
299 if (!cell->is_artificial())
307 typename particle_container::iterator insert_position =
309 --sorted_particles.end();
310 typename particle_container::iterator new_entry =
311 sorted_particles.insert(
312 insert_position,
typename particle_container::value_type());
313 new_entry->cell = cell;
314 new_entry->particles =
325 if (!it->particles.empty())
356 std::vector<typename particle_container::iterator> verify_cache(
359 if (!it->particles.empty())
360 verify_cache[it->cell->active_cell_index()] = it;
362 for (
unsigned int i = 0; i < verify_cache.size(); ++i)
412 template <
int dim,
int spacedim>
421 if (cell->is_artificial() ==
false)
431 ExcMessage(
"You can't ask for the particles on an artificial "
432 "cell since we don't know what exists on these "
440 template <
int dim,
int spacedim>
447 ->particles_in_cell(cell);
452 template <
int dim,
int spacedim>
457 const unsigned int active_cell_index = cell->active_cell_index();
459 if (cell->is_artificial() ==
false)
463 return boost::make_iterator_range(
469 const typename particle_container::iterator
470 particles_in_current_cell =
472 typename particle_container::iterator particles_in_next_cell =
473 particles_in_current_cell;
474 ++particles_in_next_cell;
475 return boost::make_iterator_range(
482 ExcMessage(
"You can't ask for the particles on an artificial "
483 "cell since we don't know what exists on these "
491 template <
int dim,
int spacedim>
509 const bool owned_cell = cell->is_locally_owned();
513 if (particles_on_cell.size() > 1)
516 std::move(particles_on_cell.back());
517 particles_on_cell.resize(particles_on_cell.size() - 1);
528 template <
int dim,
int spacedim>
532 &particles_to_remove)
545 bool particles_are_sorted =
true;
546 auto previous = particles_to_remove.begin();
547 for (
auto next = previous; next != particles_to_remove.end(); ++next)
549 if (check_greater(*previous, *next))
551 particles_are_sorted =
false;
556 if (particles_are_sorted)
559 for (
auto it = particles_to_remove.rbegin();
560 it != particles_to_remove.rend();
566 std::vector<ParticleHandler<dim, spacedim>::particle_iterator>
567 sorted_particles(particles_to_remove);
568 std::sort(sorted_particles.begin(),
569 sorted_particles.end(),
572 for (
const auto &particle : sorted_particles)
581 template <
int dim,
int spacedim>
596 template <
int dim,
int spacedim>
603 typename particle_container::iterator &cache =
607 const typename particle_container::iterator insert_position =
617 cache->particles.push_back(handle);
622 cache->particles.size() - 1);
627 template <
int dim,
int spacedim>
636 Assert(cell->is_locally_owned(),
637 ExcMessage(
"You tried to insert particles into a cell that is not "
638 "locally owned. This is not supported."));
652 template <
int dim,
int spacedim>
665 Assert(cell->is_locally_owned(),
666 ExcMessage(
"You tried to insert particles into a cell that is not "
667 "locally owned. This is not supported."));
674 particle_it->
set_id(particle_index);
676 if (properties.
size() != 0)
686 template <
int dim,
int spacedim>
694 for (
const auto &cell_and_particle : new_particles)
702 template <
int dim,
int spacedim>
720#ifdef DEAL_II_WITH_MPI
721 if (
const auto parallel_triangulation =
727 MPI_Scan(&particles_to_add_locally,
732 parallel_triangulation->get_mpi_communicator());
734 local_start_index -= particles_to_add_locally;
738 local_start_index += local_next_particle_index;
740 auto point_locations =
744 auto &cells = std::get<0>(point_locations);
745 auto &local_positions = std::get<1>(point_locations);
746 auto &index_map = std::get<2>(point_locations);
747 auto &missing_points = std::get<3>(point_locations);
753 (void)missing_points;
755 for (
unsigned int i = 0; i < cells.size(); ++i)
756 for (
unsigned int p = 0; p < local_positions[i].size(); ++p)
758 local_positions[i][p],
759 local_start_index + index_map[i][p],
767 template <
int dim,
int spacedim>
768 std::map<unsigned int, IndexSet>
772 &global_bounding_boxes,
773 const std::vector<std::vector<double>> &properties,
774 const std::vector<types::particle_index> &ids)
776 if (!properties.empty())
781 for (
const auto &p : properties)
794 const auto n_global_properties =
798 const auto n_particles_per_proc =
802 std::vector<unsigned int> particle_start_indices(n_mpi_processes);
805 for (
unsigned int process = 0; process < particle_start_indices.size();
808 particle_start_indices[process] = particle_start_index;
809 particle_start_index += n_particles_per_proc[process];
813 const auto cells_positions_and_index_maps =
816 global_bounding_boxes);
820 const auto &local_cells_containing_particles =
821 std::get<0>(cells_positions_and_index_maps);
825 const auto &local_reference_positions =
826 std::get<1>(cells_positions_and_index_maps);
829 const auto &original_indices_of_local_particles =
830 std::get<2>(cells_positions_and_index_maps);
833 const auto &local_positions = std::get<3>(cells_positions_and_index_maps);
835 const auto &calling_process_indices =
836 std::get<4>(cells_positions_and_index_maps);
839 std::map<unsigned int, std::vector<unsigned int>>
840 original_process_to_local_particle_indices_tmp;
841 for (
unsigned int i_cell = 0;
842 i_cell < local_cells_containing_particles.size();
845 for (
unsigned int i_particle = 0;
846 i_particle < local_positions[i_cell].size();
849 const unsigned int local_id_on_calling_process =
850 original_indices_of_local_particles[i_cell][i_particle];
851 const unsigned int calling_process =
852 calling_process_indices[i_cell][i_particle];
854 original_process_to_local_particle_indices_tmp[calling_process]
855 .push_back(local_id_on_calling_process);
858 std::map<unsigned int, IndexSet> original_process_to_local_particle_indices;
859 for (
auto &process_and_particle_indices :
860 original_process_to_local_particle_indices_tmp)
862 const unsigned int calling_process = process_and_particle_indices.first;
863 original_process_to_local_particle_indices.insert(
864 {calling_process,
IndexSet(n_particles_per_proc[calling_process])});
865 std::sort(process_and_particle_indices.second.begin(),
866 process_and_particle_indices.second.end());
867 original_process_to_local_particle_indices[calling_process].add_indices(
868 process_and_particle_indices.second.begin(),
869 process_and_particle_indices.second.end());
870 original_process_to_local_particle_indices[calling_process].compress();
878 std::map<unsigned int, std::vector<std::vector<double>>>
879 locally_owned_properties_from_other_processes;
886 std::map<unsigned int, std::vector<types::particle_index>>
887 locally_owned_ids_from_other_processes;
889 if (n_global_properties > 0 || !ids.empty())
894 comm, original_process_to_local_particle_indices);
897 if (n_global_properties > 0)
899 std::map<unsigned int, std::vector<std::vector<double>>>
900 non_locally_owned_properties;
902 for (
const auto &it : send_to_cpu)
904 std::vector<std::vector<double>> properties_to_send(
905 it.second.n_elements(),
907 unsigned int index = 0;
908 for (
const auto el : it.second)
909 properties_to_send[index++] = properties[el];
910 non_locally_owned_properties.insert(
911 {it.first, properties_to_send});
916 locally_owned_properties_from_other_processes =
920 locally_owned_properties_from_other_processes.size(),
921 original_process_to_local_particle_indices.size());
926 std::map<unsigned int, std::vector<types::particle_index>>
927 non_locally_owned_ids;
928 for (
const auto &it : send_to_cpu)
930 std::vector<types::particle_index> ids_to_send(
931 it.second.n_elements());
932 unsigned int index = 0;
933 for (
const auto el : it.second)
934 ids_to_send[index++] = ids[el];
935 non_locally_owned_ids.insert({it.first, ids_to_send});
940 locally_owned_ids_from_other_processes =
944 original_process_to_local_particle_indices.size());
949 for (
unsigned int i_cell = 0;
950 i_cell < local_cells_containing_particles.size();
953 for (
unsigned int i_particle = 0;
954 i_particle < local_positions[i_cell].size();
957 const unsigned int local_id_on_calling_process =
958 original_indices_of_local_particles[i_cell][i_particle];
960 const unsigned int calling_process =
961 calling_process_indices[i_cell][i_particle];
963 const unsigned int index_within_set =
964 original_process_to_local_particle_indices[calling_process]
965 .index_within_set(local_id_on_calling_process);
967 const unsigned int particle_id =
969 local_id_on_calling_process +
970 particle_start_indices[calling_process] :
971 locally_owned_ids_from_other_processes[calling_process]
976 local_reference_positions[i_cell][i_particle],
978 local_cells_containing_particles[i_cell]);
980 if (n_global_properties > 0)
982 particle_it->set_properties(
983 locally_owned_properties_from_other_processes
984 [calling_process][index_within_set]);
991 return original_process_to_local_particle_indices;
996 template <
int dim,
int spacedim>
997 std::map<unsigned int, IndexSet>
1001 &global_bounding_boxes)
1005 std::vector<Point<spacedim>> positions;
1006 std::vector<std::vector<double>> properties;
1007 std::vector<types::particle_index> ids;
1017 positions[i] = p.get_location();
1018 ids[i] = p.get_id();
1019 if (p.has_properties())
1020 properties[i] = {p.get_properties().begin(),
1021 p.get_properties().end()};
1026 global_bounding_boxes,
1033 template <
int dim,
int spacedim>
1042 template <
int dim,
int spacedim>
1051 template <
int dim,
int spacedim>
1060 template <
int dim,
int spacedim>
1069 template <
int dim,
int spacedim>
1078 template <
int dim,
int spacedim>
1083 std::vector<types::particle_index> indices;
1085 for (
const auto &p : *
this)
1086 indices.push_back(p.get_id());
1087 set.add_indices(indices.begin(), indices.end());
1094 template <
int dim,
int spacedim>
1103 template <
int dim,
int spacedim>
1107 const bool add_to_output_vector)
const
1113 for (
auto it =
begin(); it !=
end(); ++it, ++i)
1115 if (add_to_output_vector)
1116 positions[i] = positions[i] + it->get_location();
1118 positions[i] = it->get_location();
1124 template <
int dim,
int spacedim>
1128 const bool displace_particles)
1134 for (
auto it =
begin(); it !=
end(); ++it, ++i)
1137 if (displace_particles)
1138 location += new_positions[i];
1140 location = new_positions[i];
1147 template <
int dim,
int spacedim>
1151 const bool displace_particles)
1158 for (
auto &particle : *
this)
1161 function.
vector_value(particle_location, new_position);
1162 if (displace_particles)
1163 for (
unsigned int d = 0; d < spacedim; ++d)
1164 particle_location[d] += new_position[d];
1166 for (
unsigned int d = 0; d < spacedim; ++d)
1167 particle_location[d] = new_position[d];
1174 template <
int dim,
int spacedim>
1196 compare_particle_association(
1197 const unsigned int a,
1198 const unsigned int b,
1202 const double scalar_product_a = center_directions[a] * particle_direction;
1203 const double scalar_product_b = center_directions[b] * particle_direction;
1208 return (scalar_product_a > scalar_product_b);
1214 template <
int dim,
int spacedim>
1230 std::vector<particle_iterator> particles_out_of_cell;
1242 struct StageOne_CopyData
1244 std::vector<particle_iterator> local_particles_out_of_cell;
1246 StageOne_CopyData(
const unsigned int size)
1248 local_particles_out_of_cell.reserve(size);
1255 struct StageOne_ScratchData
1257 std::vector<Point<spacedim>> real_locations;
1258 std::vector<Point<dim>> reference_locations;
1259 StageOne_ScratchData(
const unsigned int size)
1261 real_locations.reserve(size);
1262 reference_locations.reserve(size);
1266 const auto stage_one_worker =
1269 StageOne_ScratchData &scratch,
1270 StageOne_CopyData ©) {
1276 copy.local_particles_out_of_cell.clear();
1278 if (cell->is_locally_owned() ==
false)
1283 scratch.real_locations.
clear();
1288 for (
const auto &particle : pic)
1289 scratch.real_locations.push_back(particle.get_location());
1291 scratch.reference_locations.resize(n_pic);
1292 mapping->transform_points_real_to_unit_cell(
1293 cell, scratch.real_locations, scratch.reference_locations);
1295 auto particle = pic.begin();
1296 for (
const auto &p_unit : scratch.reference_locations)
1299 cell->reference_cell().contains_point(p_unit,
1301 particle->set_reference_location(p_unit);
1303 copy.local_particles_out_of_cell.push_back(particle);
1309 const auto stage_one_copier = [&](
const StageOne_CopyData ©) {
1310 particles_out_of_cell.insert(particles_out_of_cell.end(),
1311 copy.local_particles_out_of_cell.begin(),
1312 copy.local_particles_out_of_cell.end());
1327 std::map<types::subdomain_id, std::vector<particle_iterator>>
1331 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>>
1339 std::set<types::subdomain_id> ghost_owners;
1340 if (
const auto parallel_triangulation =
1343 ghost_owners = parallel_triangulation->ghost_owners();
1348 for (
const auto &ghost_owner : ghost_owners)
1349 moved_particles[ghost_owner].reserve(particles_out_of_cell.size() / 4);
1350 for (
const auto &ghost_owner : ghost_owners)
1351 moved_cells[ghost_owner].reserve(particles_out_of_cell.size() / 4);
1356 std::set<typename Triangulation<dim, spacedim>::active_cell_iterator>>
1361 const std::vector<std::vector<Tensor<1, spacedim>>>
1362 &vertex_to_cell_centers =
1372 struct StageTwo_CopyData
1380 struct StageTwo_ScratchData
1382 std::vector<Point<spacedim>> real_locations;
1383 std::vector<Point<dim>> reference_locations;
1384 std::vector<unsigned int> search_order;
1386 StageTwo_ScratchData()
1393 struct StageTwo_QueuedData
1399 std::vector<StageTwo_QueuedData> data_queue;
1403 const auto stage_two_worker =
1405 const typename std::vector<particle_iterator>::iterator out_particle,
1406 StageTwo_ScratchData &scratch,
1407 StageTwo_CopyData ©) {
1411 copy.current_cell = (*out_particle)->get_surrounding_cell();
1412 copy.found_cell =
false;
1414 scratch.real_locations[0] = (*out_particle)->get_location();
1418 const unsigned int closest_vertex =
1420 copy.current_cell, (*out_particle)->get_location(), *
mapping);
1421 const unsigned int closest_vertex_index =
1422 copy.current_cell->vertex_index(closest_vertex);
1424 const auto &candidate_cells = vertex_to_cells[closest_vertex_index];
1425 const unsigned int n_candidate_cells = candidate_cells.size();
1429 scratch.search_order.resize(n_candidate_cells);
1430 for (
unsigned int i = 0; i < n_candidate_cells; ++i)
1431 scratch.search_order[i] = i;
1437 (*out_particle)->get_location() -
1438 copy.current_cell->vertex(closest_vertex);
1443 1e4 * std::numeric_limits<double>::epsilon() *
1444 std::numeric_limits<double>::epsilon() *
1445 vertex_to_cell_centers[closest_vertex_index][0].norm_square())
1447 vertex_to_particle /= vertex_to_particle.
norm();
1448 const auto &vertex_to_cells_center =
1449 vertex_to_cell_centers[closest_vertex_index];
1451 std::sort(scratch.search_order.begin(),
1452 scratch.search_order.end(),
1453 [&vertex_to_particle,
1454 &vertex_to_cells_center](
const unsigned int a,
1455 const unsigned int b) {
1456 return compare_particle_association<spacedim>(
1457 a, b, vertex_to_particle, vertex_to_cells_center);
1463 for (
unsigned int i = 0; i < n_candidate_cells; ++i)
1467 const_iterator candidate_cell = candidate_cells.begin();
1469 std::advance(candidate_cell, scratch.search_order[i]);
1470 mapping->transform_points_real_to_unit_cell(
1472 scratch.real_locations,
1473 scratch.reference_locations);
1478 copy.current_cell = *candidate_cell;
1479 copy.found_cell =
true;
1487 if (!copy.found_cell)
1494#if defined(DEAL_II_WITH_BOOST_BUNDLED) || \
1495 !(defined(__clang_major__) && __clang_major__ >= 16) || \
1496 BOOST_VERSION >= 108100
1497 std::vector<std::pair<Point<spacedim>,
unsigned int>>
1498 closest_vertex_in_domain;
1500 boost::geometry::index::nearest((*out_particle)->get_location(),
1502 std::back_inserter(closest_vertex_in_domain));
1506 const unsigned int closest_vertex_index_in_domain =
1507 closest_vertex_in_domain[0].second;
1509 const unsigned int closest_vertex_index_in_domain =
1512 (*out_particle)->get_location());
1517 for (
const auto &cell :
1518 vertex_to_cells[closest_vertex_index_in_domain])
1520 mapping->transform_points_real_to_unit_cell(
1521 cell, scratch.real_locations, scratch.reference_locations);
1526 copy.current_cell = cell;
1527 copy.found_cell =
true;
1532 copy.out_particle = (*out_particle);
1533 copy.reference_location = scratch.reference_locations[0];
1536 const auto stage_two_copier = [&](
const StageTwo_CopyData ©) {
1537 auto local_out_particle = copy.out_particle;
1539 if (!copy.found_cell)
1544 signals.particle_lost(copy.out_particle,
1545 copy.out_particle->get_surrounding_cell());
1550 local_out_particle->set_reference_location(copy.reference_location);
1554 StageTwo_QueuedData data_entry;
1555 data_entry.current_cell = copy.current_cell;
1556 data_entry.out_particle = copy.out_particle;
1557 data_queue.push_back(data_entry);
1561 particles_out_of_cell.end(),
1564 StageTwo_ScratchData(),
1565 StageTwo_CopyData(),
1570 for (
const auto &data_entry : data_queue)
1574 if (data_entry.current_cell->is_locally_owned())
1577 data_entry.out_particle->particles_in_cell->particles
1578 [data_entry.out_particle->particle_index_within_cell];
1581 const auto old_value = old;
1589 moved_particles[data_entry.current_cell->subdomain_id()]
1590 .push_back(data_entry.out_particle);
1591 moved_cells[data_entry.current_cell->subdomain_id()].push_back(
1592 data_entry.current_cell);
1598#ifdef DEAL_II_WITH_MPI
1599 if (
const auto parallel_triangulation =
1604 parallel_triangulation->get_mpi_communicator()) > 1)
1613 std::vector<typename PropertyPool<dim, spacedim>::Handle> unsorted_handles;
1614 unsorted_handles.reserve(
property_pool->n_registered_slots());
1620 unsorted_handles.push_back(particle);
1621 particle = sorted_handle++;
1630 template <
int dim,
int spacedim>
1633 const bool enable_cache)
1636 const auto parallel_triangulation =
1639 if (parallel_triangulation !=
nullptr)
1642 parallel_triangulation->get_mpi_communicator()) == 1)
1648#ifndef DEAL_II_WITH_MPI
1652 for (
const auto &cell :
triangulation->active_cell_iterators())
1653 if (cell->is_ghost() &&
1660 for (
auto &ghost_particle :
1678 const std::map<unsigned int, std::set<types::subdomain_id>>
1679 &vertices_with_ghost_neighbors =
1682 const std::set<types::subdomain_id> ghost_owners =
1683 parallel_triangulation->ghost_owners();
1684 for (
const auto ghost_owner : ghost_owners)
1688 const std::vector<std::set<unsigned int>> vertex_to_neighbor_subdomain =
1691 for (
const auto &cell :
triangulation->active_cell_iterators())
1693 if (cell->is_locally_owned())
1695 std::set<unsigned int> cell_to_neighbor_subdomain;
1696 for (
const unsigned int v : cell->vertex_indices())
1698 const auto vertex_ghost_neighbors =
1699 vertices_with_ghost_neighbors.find(cell->vertex_index(v));
1700 if (vertex_ghost_neighbors !=
1701 vertices_with_ghost_neighbors.end())
1703 cell_to_neighbor_subdomain.insert(
1704 vertex_ghost_neighbors->second.begin(),
1705 vertex_ghost_neighbors->second.end());
1709 if (cell_to_neighbor_subdomain.size() > 0)
1714 for (
const auto domain : cell_to_neighbor_subdomain)
1716 for (
typename particle_iterator_range::iterator particle =
1717 particle_range.begin();
1718 particle != particle_range.end();
1721 .push_back(particle);
1739 template <
int dim,
int spacedim>
1744 const auto parallel_triangulation =
1747 if (parallel_triangulation ==
nullptr ||
1749 parallel_triangulation->get_mpi_communicator()) == 1)
1755#ifdef DEAL_II_WITH_MPI
1760 "Ghost particles cannot be updated if they first have not been "
1761 "exchanged at least once with the cache enabled"));
1771#ifdef DEAL_II_WITH_MPI
1772 template <
int dim,
int spacedim>
1781 const bool build_cache)
1789 const auto parallel_triangulation =
1792 Assert(parallel_triangulation,
1793 ExcMessage(
"This function is only implemented for "
1794 "parallel::TriangulationBase objects."));
1797 const std::set<types::subdomain_id> ghost_owners =
1798 parallel_triangulation->ghost_owners();
1799 const std::vector<types::subdomain_id> neighbors(ghost_owners.begin(),
1800 ghost_owners.end());
1801 const unsigned int n_neighbors = neighbors.size();
1803 if (send_cells.size() != 0)
1809 particles_to_send.end(),
1814 for (
auto send_particles = particles_to_send.begin();
1815 send_particles != particles_to_send.end();
1817 Assert(ghost_owners.find(send_particles->first) != ghost_owners.end(),
1820 std::size_t n_send_particles = 0;
1821 for (
auto send_particles = particles_to_send.begin();
1822 send_particles != particles_to_send.end();
1824 n_send_particles += send_particles->second.size();
1830 std::vector<unsigned int> n_send_data(n_neighbors, 0);
1831 std::vector<unsigned int> send_offsets(n_neighbors, 0);
1832 std::vector<char> send_data;
1837 const unsigned int individual_particle_data_size =
1841 const unsigned int individual_total_particle_data_size =
1842 individual_particle_data_size + cellid_size;
1847 if (n_send_particles > 0)
1850 send_data.resize(n_send_particles *
1851 individual_total_particle_data_size);
1853 void *data =
static_cast<void *
>(&send_data.front());
1856 for (
unsigned int i = 0; i < n_neighbors; ++i)
1858 send_offsets[i] =
reinterpret_cast<std::size_t
>(data) -
1859 reinterpret_cast<std::size_t
>(&send_data.front());
1861 const unsigned int n_particles_to_send =
1862 particles_to_send.at(neighbors[i]).size();
1864 Assert(
static_cast<std::size_t
>(n_particles_to_send) *
1865 individual_total_particle_data_size ==
1866 static_cast<std::size_t
>(
1867 n_particles_to_send *
1868 individual_total_particle_data_size),
1869 ExcMessage(
"Overflow when trying to send particle "
1872 for (
unsigned int j = 0; j < n_particles_to_send; ++j)
1878 if (send_cells.empty())
1879 cell = particles_to_send.at(neighbors[i])[j]
1880 ->get_surrounding_cell();
1882 cell = send_cells.at(neighbors[i])[j];
1885 cell->id().template to_binary<dim>();
1886 memcpy(data, &cellid, cellid_size);
1887 data =
static_cast<char *
>(data) + cellid_size;
1889 data = particles_to_send.at(neighbors[i])[j]
1890 ->write_particle_data_to_memory(data);
1895 n_send_data[i] = n_particles_to_send;
1900 std::vector<unsigned int> n_recv_data(n_neighbors);
1901 std::vector<unsigned int> recv_offsets(n_neighbors);
1904 const int mpi_tag = Utilities::MPI::internal::Tags::
1905 particle_handler_send_recv_particles_setup;
1907 std::vector<MPI_Request> n_requests(2 * n_neighbors);
1908 for (
unsigned int i = 0; i < n_neighbors; ++i)
1911 MPI_Irecv(&(n_recv_data[i]),
1916 parallel_triangulation->get_mpi_communicator(),
1917 &(n_requests[2 * i]));
1920 for (
unsigned int i = 0; i < n_neighbors; ++i)
1923 MPI_Isend(&(n_send_data[i]),
1928 parallel_triangulation->get_mpi_communicator(),
1929 &(n_requests[2 * i + 1]));
1933 MPI_Waitall(2 * n_neighbors, n_requests.data(), MPI_STATUSES_IGNORE);
1938 unsigned int total_recv_data = 0;
1939 for (
unsigned int neighbor_id = 0; neighbor_id < n_neighbors; ++neighbor_id)
1941 recv_offsets[neighbor_id] = total_recv_data;
1943 n_recv_data[neighbor_id] * individual_total_particle_data_size;
1947 std::vector<char> recv_data(total_recv_data);
1951 std::vector<MPI_Request> requests(2 * n_neighbors);
1952 unsigned int send_ops = 0;
1953 unsigned int recv_ops = 0;
1955 const int mpi_tag = Utilities::MPI::internal::Tags::
1956 particle_handler_send_recv_particles_send;
1958 for (
unsigned int i = 0; i < n_neighbors; ++i)
1959 if (n_recv_data[i] > 0)
1962 MPI_Irecv(&(recv_data[recv_offsets[i]]),
1963 n_recv_data[i] * individual_total_particle_data_size,
1967 parallel_triangulation->get_mpi_communicator(),
1968 &(requests[send_ops]));
1973 for (
unsigned int i = 0; i < n_neighbors; ++i)
1974 if (n_send_data[i] > 0)
1977 MPI_Isend(&(send_data[send_offsets[i]]),
1978 n_send_data[i] * individual_total_particle_data_size,
1982 parallel_triangulation->get_mpi_communicator(),
1983 &(requests[send_ops + recv_ops]));
1988 MPI_Waitall(send_ops + recv_ops, requests.data(), MPI_STATUSES_IGNORE);
1994 const void *recv_data_it =
static_cast<const void *
>(recv_data.data());
1997 auto &ghost_particles_iterators =
2002 ghost_particles_iterators.clear();
2005 send_pointers_particles.assign(n_neighbors + 1, 0);
2007 for (
unsigned int i = 0; i < n_neighbors; ++i)
2008 send_pointers_particles[i + 1] =
2009 send_pointers_particles[i] +
2010 n_send_data[i] * individual_particle_data_size;
2013 recv_pointers_particles.assign(n_neighbors + 1, 0);
2015 for (
unsigned int i = 0; i < n_neighbors; ++i)
2016 recv_pointers_particles[i + 1] =
2017 recv_pointers_particles[i] +
2018 n_recv_data[i] * individual_particle_data_size;
2028 while (
reinterpret_cast<std::size_t
>(recv_data_it) -
2029 reinterpret_cast<std::size_t
>(recv_data.data()) <
2033 memcpy(&binary_cellid, recv_data_it, cellid_size);
2034 const CellId id(binary_cellid);
2035 recv_data_it =
static_cast<const char *
>(recv_data_it) + cellid_size;
2041 const typename particle_container::iterator &cache =
2047 cache->particles.size() - 1);
2056 ghost_particles_iterators.push_back(particle_it);
2059 AssertThrow(recv_data_it == recv_data.data() + recv_data.size(),
2061 "The amount of data that was read into new particles "
2062 "does not match the amount of data sent around."));
2068#ifdef DEAL_II_WITH_MPI
2069 template <
int dim,
int spacedim>
2075 const auto parallel_triangulation =
2079 parallel_triangulation,
2081 "This function is only implemented for parallel::TriangulationBase "
2091 if (send_pointers.back() > 0)
2093 void *data =
static_cast<void *
>(&send_data.front());
2096 for (
const auto i : neighbors)
2097 for (
const auto &p : particles_to_send.at(i))
2099 data = p->write_particle_data_to_memory(data);
2109 std::vector<MPI_Request> requests(2 * neighbors.size());
2110 unsigned int send_ops = 0;
2111 unsigned int recv_ops = 0;
2113 const int mpi_tag = Utilities::MPI::internal::Tags::
2114 particle_handler_send_recv_particles_send;
2116 for (
unsigned int i = 0; i < neighbors.size(); ++i)
2117 if ((recv_pointers[i + 1] - recv_pointers[i]) > 0)
2120 MPI_Irecv(recv_data.data() + recv_pointers[i],
2121 recv_pointers[i + 1] - recv_pointers[i],
2125 parallel_triangulation->get_mpi_communicator(),
2126 &(requests[send_ops]));
2131 for (
unsigned int i = 0; i < neighbors.size(); ++i)
2132 if ((send_pointers[i + 1] - send_pointers[i]) > 0)
2135 MPI_Isend(send_data.data() + send_pointers[i],
2136 send_pointers[i + 1] - send_pointers[i],
2140 parallel_triangulation->get_mpi_communicator(),
2141 &(requests[send_ops + recv_ops]));
2146 MPI_Waitall(send_ops + recv_ops, requests.data(), MPI_STATUSES_IGNORE);
2152 const void *recv_data_it =
static_cast<const void *
>(recv_data.data());
2155 auto &ghost_particles_iterators =
2158 for (
auto &recv_particle : ghost_particles_iterators)
2163 recv_particle->read_particle_data_from_memory(recv_data_it);
2165 Assert(recv_particle->particles_in_cell->cell->is_ghost(),
2172 recv_particle->particle_index_within_cell),
2176 AssertThrow(recv_data_it == recv_data.data() + recv_data.size(),
2178 "The amount of data that was read into new particles "
2179 "does not match the amount of data sent around."));
2183 template <
int dim,
int spacedim>
2186 const std::function<std::size_t()> &size_callb,
2197 template <
int dim,
int spacedim>
2203 connection.disconnect();
2208 this->initialize(*(this->triangulation),
2210 this->property_pool->n_properties_per_slot());
2213 this->tria_listeners.push_back(
2214 this->
triangulation->signals.clear.connect([&]() { this->clear(); }));
2222 [&]() { this->post_mesh_change_action(); }));
2224 triangulation->signals.post_distributed_repartition.connect(
2225 [&]() { this->post_mesh_change_action(); }));
2228 [&]() { this->post_mesh_change_action(); }));
2233 [&]() { this->post_mesh_change_action(); }));
2239 template <
int dim,
int spacedim>
2245 const bool distributed_triangulation =
2248 &(*triangulation)) !=
nullptr;
2249 (void)distributed_triangulation;
2254 "Mesh refinement in a non-distributed triangulation is not supported "
2255 "by the ParticleHandler class. Either insert particles after mesh "
2256 "creation, or use a distributed triangulation."));
2267 template <
int dim,
int spacedim>
2276 template <
int dim,
int spacedim>
2285 template <
int dim,
int spacedim>
2289 const auto callback_function =
2304 template <
int dim,
int spacedim>
2308 const bool serialization =
false;
2314 template <
int dim,
int spacedim>
2318 const bool serialization =
true;
2323 template <
int dim,
int spacedim>
2326 const bool serialization)
2341 const auto callback_function =
2345 const boost::iterator_range<std::vector<char>::const_iterator>
2361 template <
int dim,
int spacedim>
2367 std::vector<particle_iterator> stored_particles_on_cell;
2377 stored_particles_on_cell.reserve(n_particles);
2379 for (
unsigned int i = 0; i < n_particles; ++i)
2395 stored_particles_on_cell.reserve(
2396 stored_particles_on_cell.size() + n_particles);
2398 const typename particle_container::iterator &cache =
2400 for (
unsigned int i = 0; i < n_particles; ++i)
2401 stored_particles_on_cell.push_back(
2412 return pack_particles(stored_particles_on_cell);
2417 template <
int dim,
int spacedim>
2422 const boost::iterator_range<std::vector<char>::const_iterator> &data_range)
2424 if (data_range.begin() == data_range.end())
2427 const auto cell_to_store_particles =
2431 if (data_range.begin() != data_range.end())
2433 const void *data =
static_cast<const void *
>(&(*data_range.begin()));
2434 const void *
end =
static_cast<const void *
>(
2435 &(*data_range.begin()) + (data_range.end() - data_range.begin()));
2439 const void *old_data = data;
2444 const void *new_data = data;
2449 x->serialized_size_in_bytes());
2454 "The particle data could not be deserialized successfully. "
2455 "Check that when deserializing the particles you expect "
2456 "the same number of properties that were serialized."));
2474 for (
auto &particle : loaded_particles_on_cell)
2477 mapping->transform_real_to_unit_cell(cell_to_store_particles,
2478 particle.get_location());
2479 particle.set_reference_location(p_unit);
2488 typename particle_container::iterator &cache =
2490 ->active_cell_index()];
2497 auto particle = loaded_particles_on_cell.begin();
2498 for (
unsigned int i = 0; i < cache->particles.size();)
2500 bool found_new_cell =
false;
2509 mapping->transform_real_to_unit_cell(
2510 child, particle->get_location());
2514 found_new_cell =
true;
2515 particle->set_reference_location(p_unit);
2519 if (child != cell_to_store_particles)
2524 cache->particles[i] = cache->particles.back();
2525 cache->particles.pop_back();
2542 if (found_new_cell ==
false)
2551 signals.particle_lost(particle,
2552 particle->get_surrounding_cell());
2553 if (cache->particles[i] !=
2556 cache->particles[i] = cache->particles.back();
2557 cache->particles.pop_back();
2561 if (cache->particles.empty())
2576#include "particles/particle_handler.inst"
*Â x_component_mask set(0, true)
@ children_will_be_coarsened
TriaIterator< CellAccessor< dim, spacedim > > child(const unsigned int i) const
bool is_locally_owned() const
boost::container::small_vector< TriaIterator< CellAccessor< dim, spacedim > >, ReferenceCells::max_n_children< dim >()> child_iterators() const
unsigned int active_cell_index() const
std::array< std::uint64_t, 3 > binary_type
const unsigned int n_components
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const
Abstract base class for mapping classes.
static unsigned int n_threads()
particle_container::iterator particles_in_cell
unsigned int particle_index_within_cell
const void * read_particle_data_from_memory(const void *data)
const Triangulation< dim, spacedim >::cell_iterator & get_surrounding_cell() const
void set_location(const Point< spacedim > &new_location)
void set_reference_location(const Point< dim > &new_reference_location)
const PropertyPool< dim, spacedim >::Handle & get_handle() const
void set_properties(const std::vector< double > &new_properties)
void set_id(const types::particle_index &new_id)
std::function< void *(const particle_iterator &, void *)> store_callback
void register_additional_store_load_functions(const std::function< std::size_t()> &size_callback, const std::function< void *(const particle_iterator &, void *)> &store_callback, const std::function< const void *(const particle_iterator &, const void *)> &load_callback)
void exchange_ghost_particles(const bool enable_ghost_cache=false)
unsigned int tria_attached_data_index
types::particle_index n_global_particles() const
unsigned int global_max_particles_per_cell
void unpack_after_coarsening_and_refinement()
void register_data_attach()
internal::GhostParticlePartitioner< dim, spacedim > ghost_particles_cache
particle_container::iterator particle_container_ghost_begin() const
void prepare_for_serialization()
unsigned int n_properties_per_particle() const
types::particle_index global_number_of_particles
particle_container::iterator particle_container_owned_end() const
const double tolerance_inside_cell
void update_cached_numbers()
void prepare_for_coarsening_and_refinement()
particle_container::iterator particle_container_ghost_end() const
boost::iterator_range< particle_iterator > particle_iterator_range
void post_mesh_change_action()
void send_recv_particles(const std::map< types::subdomain_id, std::vector< particle_iterator > > &particles_to_send, const std::map< types::subdomain_id, std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > > &new_cells_for_particles=std::map< types::subdomain_id, std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > >(), const bool enable_cache=false)
ParticleIterator< dim, spacedim > particle_iterator
ObserverPointer< const Mapping< dim, spacedim >, ParticleHandler< dim, spacedim > > mapping
void send_recv_particles_properties_and_location(const std::map< types::subdomain_id, std::vector< particle_iterator > > &particles_to_send)
void get_particle_positions(VectorType &output_vector, const bool add_to_output_vector=false) const
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)
typename ParticleAccessor< dim, spacedim >::particle_container particle_container
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)
void connect_to_triangulation_signals()
std::enable_if_t< std::is_convertible_v< VectorType *, Function< spacedim > * >==false > set_particle_positions(const VectorType &input_vector, const bool displace_particles=true)
std::function< std::size_t()> size_callback
std::vector< char > pack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status) const
void insert_particles(const std::multimap< typename Triangulation< dim, spacedim >::active_cell_iterator, Particle< dim, spacedim > > &particles)
types::particle_index next_free_particle_index
const particle_container::iterator owned_particles_end
particle_iterator end() const
void remove_particle(const particle_iterator &particle)
types::particle_index n_locally_owned_particles() const
void reset_particle_container(particle_container &particles)
types::particle_index n_particles_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell) const
void update_ghost_particles()
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 sort_particles_into_subdomains_and_cells()
void remove_particles(const std::vector< particle_iterator > &particles)
types::particle_index n_global_max_particles_per_cell() const
std::vector< boost::signals2::connection > tria_listeners
particle_container::iterator particle_container_owned_begin() const
void unpack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range)
virtual ~ParticleHandler()
std::vector< typename particle_container::iterator > cells_to_particle_cache
void copy_from(const ParticleHandler< dim, spacedim > &particle_handler)
std::function< const void *(const particle_iterator &, const void *)> load_callback
std::unique_ptr< GridTools::Cache< dim, spacedim > > triangulation_cache
types::particle_index get_next_free_particle_index() const
particle_container particles
IndexSet locally_owned_particle_ids() const
void set_property_pool(PropertyPool< dim, spacedim > &property_pool)
const Point< dim > & get_reference_location() const
const Point< spacedim > & get_location() const
std::size_t serialized_size_in_bytes() const
types::particle_index get_id() const
ArrayView< double > get_properties()
static const Handle invalid_handle
numbers::NumberTraits< Number >::real_type norm() const
constexpr numbers::NumberTraits< Number >::real_type norm_square() const
ReferenceCell< structdim > reference_cell() const
IteratorState::IteratorStates state() const
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ASSERT_UNREACHABLE()
static ::ExceptionBase & ExcTransformationFailed()
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)
TriaActiveIterator< CellAccessor< dim, spacedim > > active_cell_iterator
TriaIterator< CellAccessor< dim, spacedim > > cell_iterator
@ past_the_end
Iterator reached end of container.
@ valid
Iterator points to a valid object.
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)
T max(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
const MPI_Datatype mpi_type_id_for_type
void run(const std::vector< std::vector< Iterator > > &colored_iterators, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
constexpr unsigned int invalid_unsigned_int
constexpr types::subdomain_id artificial_subdomain_id
bool is_finite(const double x)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
unsigned int subdomain_id
unsigned int particle_index
*braid_SplitCommworld & comm
static bool is_inside_unit_cell(const Point< dim > &p)