30#include "system/System.hpp"
36#include <boost/mpi/collectives/all_gather.hpp>
37#include <boost/mpi/collectives/all_reduce.hpp>
38#include <boost/mpi/collectives/gather.hpp>
39#include <boost/mpi/collectives/reduce.hpp>
40#include <boost/mpi/collectives/scatter.hpp>
50#include <unordered_map>
51#include <unordered_set>
75 boost::mpi::broadcast(
::comm_cart, is_rebuild_needed, 0);
76 return is_rebuild_needed;
94 auto const found = p and not p->is_ghost();
95 assert(1 == boost::mpi::all_reduce(
::comm_cart,
static_cast<int>(found),
97 "Particle not found");
110 assert(p !=
nullptr);
124 ::comm_cart.recv(boost::mpi::any_source, boost::mpi::any_tag, result);
129 std::vector<int> local_ids;
130 boost::mpi::scatter(
comm_cart, local_ids, 0);
132 std::vector<Particle> parts(local_ids.size());
133 std::ranges::transform(local_ids, parts.begin(), [](
int p_id) {
134 auto const p = get_cell_structure().get_local_particle(p_id);
135 assert(p != nullptr);
157 std::vector<Particle> parts(ids.size());
160 static std::vector<std::vector<int>> node_ids(
comm_cart.size());
161 for (
auto &per_node : node_ids) {
165 for (
auto const &p_id : ids) {
167 node_ids[p_node].emplace_back(p_id);
174 static std::vector<int> ignore;
175 boost::mpi::scatter(
comm_cart, node_ids, ignore, 0);
176 assert(ignore.empty());
179 static std::vector<int> node_sizes(
comm_cart.size());
181 std::transform(node_ids.begin(), node_ids.end(), node_sizes.begin(),
182 std::size<std::vector<int>>);
185 parts.data(), node_sizes.data(), 0);
196 static std::vector<int> ids;
198 auto out_ids = std::back_inserter(ids);
201 std::ranges::copy_if(in_ids, out_ids, [](
int id) {
217 static std::vector<int> sendbuf;
220 auto const n_part =
static_cast<int>(local_particles.size());
221 boost::mpi::gather(
comm_cart, n_part, 0);
228 sendbuf.resize(n_part);
230 std::transform(local_particles.begin(), local_particles.end(),
231 sendbuf.begin(), [](
Particle const &p) { return p.id(); });
242 static std::vector<int> n_parts;
243 boost::mpi::gather(
comm_cart,
static_cast<int>(local_particles.size()),
246 static std::vector<int> pdata;
251 for (
int pnode = 0; pnode < n_nodes; pnode++) {
253 for (
auto const &p : local_particles) {
257 }
else if (n_parts[pnode] > 0) {
258 pdata.resize(n_parts[pnode]);
260 for (
int i = 0; i < n_parts[pnode]; i++) {
290 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
300 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
303 return needle->second;
308 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
323 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
326 return needle->second;
343 return std::accumulate(
345 [](
int max,
auto const &kv) { return std::max(max, kv.first); });
356 auto folded_pos = pos;
358 box_geo.fold_position(folded_pos, image_box);
361 new_part.id() = p_id;
362 new_part.pos() = folded_pos;
363 new_part.image_box() = image_box;
377 auto const &box_geo = *system.box_geo;
378 auto p = system.cell_structure->get_local_particle(p_id);
382 auto folded_pos = pos;
384 box_geo.fold_position(folded_pos, image_box);
385 p->pos() = folded_pos;
386 p->image_box() = image_box;
421 auto const node_local = (has_created) ?
::comm_cart.rank() : 0;
422 boost::mpi::reduce(
::comm_cart, node_local, node, std::plus<int>{}, 0);
426 assert(not has_created or node == 0);
436 auto success =
false;
437 boost::mpi::reduce(
::comm_cart, has_moved, success, std::plus<bool>{}, 0);
439 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
454 std::vector<int> pids{};
455 std::ranges::copy(std::views::keys(
particle_node), std::back_inserter(pids));
456 std::ranges::sort(pids);
465 std::vector<int> pids{};
466 std::ranges::copy(std::views::keys(
particle_node), std::back_inserter(pids));
#define REGISTER_CALLBACK(cb)
Register a static callback without return value.
Vector implementation and trait types for boost qvm interoperability.
This file contains everything related to the global cell structure / cell system.
auto call_all(void(*fp)(Args...), ArgRef &&...args) const
Call a callback on all nodes.
void on_particle_change()
Called every time a particle property changes.
std::shared_ptr< CellStructure > cell_structure
std::shared_ptr< BoxGeometry > box_geo
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
MpiCallbacks & mpiCallbacks()
Returns a reference to the global callback class instance.
void gatherv(const boost::mpi::communicator &comm, const T *in_values, int in_size, T *out_values, const int *sizes, const int *displs, int root)
std::size_t const max_cache_size
Utils::Cache< int, Particle > particle_fetch_cache(max_cache_size)
Various procedures concerning interactions between particles.
std::vector< int > get_particle_ids_parallel()
static int max_seen_pid
Keep track of the largest particle id.
static bool maybe_move_particle(int p_id, Utils::Vector3d const &pos)
Move particle to a new position.
static void build_particle_node()
Rebuild the particle index.
void make_new_particle(int p_id, Utils::Vector3d const &pos)
Create a new particle and attach it to a cell.
const Particle & get_particle_data(int p_id)
Get particle data.
static std::unordered_map< int, int > particle_node
Mapping particle ids to MPI ranks.
int get_particle_node(int p_id)
Get the MPI rank which owns the a specific particle.
void set_particle_pos(int p_id, Utils::Vector3d const &pos)
Move particle to a new position.
static void mpi_synchronize_max_seen_pid_local()
static int calculate_max_seen_id()
Calculate the largest particle id.
void remove_particle(int p_id)
Remove particle with a given identity.
static void mpi_get_particles_local()
static void mpi_who_has_head()
static std::vector< Particle > mpi_get_particles(std::span< const int > ids)
Get multiple particles at once.
int get_maximal_particle_id()
Get maximal particle id.
int get_particle_node_parallel(int p_id)
std::vector< int > get_particle_ids()
Get all particle ids.
std::size_t fetch_cache_max_size()
Return the maximal number of particles that are kept in the fetch cache.
void prefetch_particle_data(std::span< const int > in_ids)
Fetch a range of particle into the fetch cache.
void invalidate_fetch_cache()
Invalidate the fetch cache for get_particle_data.
static auto rebuild_needed()
static auto & get_cell_structure()
static void build_particle_node_parallel()
Rebuild the particle index.
static bool maybe_insert_particle(int p_id, Utils::Vector3d const &pos)
Create a new particle and attach it to a cell.
void clear_particle_node()
Invalidate particle_node.
static void mpi_who_has_local()
static void mpi_send_particle_data_local(int p_id)
int get_n_part()
Get number of particles.
bool particle_exists(int p_id)
Check if particle exists.
Particles creation and deletion.
Struct holding all information for one particle.