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>
81 boost::mpi::broadcast(
::comm_cart, is_rebuild_needed, 0);
82 return is_rebuild_needed;
91 throw std::runtime_error(
"Types may not be negative");
95 nonbonded_ias.make_particle_type_exist(type);
97 std::vector<int> local_pids;
99 if (p.type() == type) {
100 local_pids.emplace_back(p.id());
104 std::vector<std::vector<int>> global_pids;
105 boost::mpi::all_gather(
::comm_cart, local_pids, global_pids);
107 for (
auto const &vec : global_pids) {
108 for (
auto const &p_id : vec) {
117 it->second.erase(p_id);
123 it->second.insert(p_id);
130 auto it = kv.second.find(p_id);
131 if (it != kv.second.end()) {
135 assert(p->type() == kv.first);
142 if (old_type != new_type) {
161 auto const found = p and not p->is_ghost();
162 assert(1 == boost::mpi::all_reduce(
::comm_cart,
static_cast<int>(found),
164 "Particle not found");
177 assert(p !=
nullptr);
191 ::comm_cart.recv(boost::mpi::any_source, boost::mpi::any_tag, result);
196 std::vector<int> local_ids;
197 boost::mpi::scatter(
comm_cart, local_ids, 0);
199 std::vector<Particle> parts(local_ids.size());
200 std::ranges::transform(local_ids, parts.begin(), [](
int p_id) {
201 auto const p = get_cell_structure().get_local_particle(p_id);
202 assert(p != nullptr);
224 std::vector<Particle> parts(ids.size());
227 static std::vector<std::vector<int>> node_ids(
comm_cart.size());
228 for (
auto &per_node : node_ids) {
232 for (
auto const &p_id : ids) {
234 node_ids[p_node].emplace_back(p_id);
241 static std::vector<int> ignore;
242 boost::mpi::scatter(
comm_cart, node_ids, ignore, 0);
243 assert(ignore.empty());
246 static std::vector<int> node_sizes(
comm_cart.size());
247 std::ranges::transform(std::as_const(node_ids), node_sizes.begin(),
248 std::size<std::vector<int>>);
251 parts.data(), node_sizes.data(), 0);
262 static std::vector<int> ids;
264 auto out_ids = std::back_inserter(ids);
267 std::ranges::copy_if(in_ids, out_ids, [](
int id) {
283 static std::vector<int> sendbuf;
286 auto const n_part =
static_cast<int>(local_particles.size());
287 boost::mpi::gather(
comm_cart, n_part, 0);
294 sendbuf.resize(n_part);
296 std::transform(local_particles.begin(), local_particles.end(),
297 sendbuf.begin(), [](
Particle const &p) { return p.id(); });
308 static std::vector<int> n_parts;
309 boost::mpi::gather(
comm_cart,
static_cast<int>(local_particles.size()),
312 static std::vector<int> pdata;
317 for (
int pnode = 0; pnode < n_nodes; pnode++) {
319 for (
auto const &p : local_particles) {
323 }
else if (n_parts[pnode] > 0) {
324 pdata.resize(n_parts[pnode]);
326 for (
int i = 0; i < n_parts[pnode]; i++) {
356 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
366 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
369 return needle->second;
374 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
389 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
392 return needle->second;
415 return std::accumulate(
417 [](
int max,
auto const &kv) { return std::max(max, kv.first); });
428 auto folded_pos = pos;
430 box_geo.fold_position(folded_pos, image_box);
433 new_part.id() = p_id;
434 new_part.pos() = folded_pos;
435 new_part.image_box() = image_box;
449 auto const &box_geo = *system.box_geo;
450 auto p = system.cell_structure->get_local_particle(p_id);
454 auto folded_pos = pos;
456 box_geo.fold_position(folded_pos, image_box);
457 p->pos() = folded_pos;
458 p->image_box() = image_box;
473 if (p !=
nullptr and not p->is_ghost()) {
480 ::comm_cart.recv(boost::mpi::any_source, 42, p_type);
516 auto const node_local = (has_created) ?
::comm_cart.rank() : 0;
517 boost::mpi::reduce(
::comm_cart, node_local, node, std::plus<int>{}, 0);
521 assert(not has_created or node == 0);
531 auto success =
false;
532 boost::mpi::reduce(
::comm_cart, has_moved, success, std::plus<bool>{}, 0);
534 throw std::runtime_error(
"Particle node for id " + std::to_string(p_id) +
542 throw std::runtime_error(
"The provided particle type " +
543 std::to_string(type) +
544 " is currently not tracked by the system.");
547 if (random_index_in_type_map + 1 > it->second.size())
548 throw std::runtime_error(
"The provided index exceeds the number of "
549 "particle types listed in the particle_type_map");
551 auto p_id = *std::next(it->second.begin(), random_index_in_type_map);
559 throw std::runtime_error(
"The provided particle type " +
560 std::to_string(type) +
561 " is currently not tracked by the system.");
564 return static_cast<int>(it->second.size());
577 std::vector<int> pids{};
578 std::ranges::copy(std::views::keys(
particle_node), std::back_inserter(pids));
579 std::ranges::sort(pids);
588 std::vector<int> pids{};
589 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 -> std::enable_if_t< std::is_void_v< decltype(fp(args...))> >
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
std::shared_ptr< InteractionsNonBonded > nonbonded_ias
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
void mpi_call(void(*fp)(Args...), ArgRef &&...args)
Call a local function.
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 void add_id_to_type_map(int p_id, int type)
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.
int number_of_particles_with_type(int type)
void set_particle_pos(int p_id, Utils::Vector3d const &pos)
Move particle to a new position.
void init_type_map(int type)
static void remove_id_from_map(int p_id, int type)
static void mpi_synchronize_max_seen_pid_local()
void remove_all_particles()
Remove all particles.
int get_random_p_id(int type, int random_index_in_type_map)
Find a particle of given type and return its id.
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 bool type_list_enable
Enable particle type tracking in particle_type_map.
static void mpi_who_has_local()
void on_particle_type_change(int p_id, int old_type, int new_type)
static void mpi_send_particle_data_local(int p_id)
int get_n_part()
Get number of particles.
static void clear_particle_type_map()
static std::unordered_map< int, std::unordered_set< int > > particle_type_map
Mapping particle types to lists of particle ids.
bool particle_exists(int p_id)
Check if particle exists.
Particles creation and deletion.
Struct holding all information for one particle.