ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
particle_node.cpp File Reference
#include "particle_node.hpp"
#include "BoxGeometry.hpp"
#include "Particle.hpp"
#include "cell_system/CellStructure.hpp"
#include "cells.hpp"
#include "communication.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "system/System.hpp"
#include <utils/Cache.hpp>
#include <utils/Span.hpp>
#include <utils/Vector.hpp>
#include <utils/keys.hpp>
#include <utils/mpi/gatherv.hpp>
#include <boost/mpi/collectives/all_gather.hpp>
#include <boost/mpi/collectives/all_reduce.hpp>
#include <boost/mpi/collectives/gather.hpp>
#include <boost/mpi/collectives/reduce.hpp>
#include <boost/mpi/collectives/scatter.hpp>
#include <boost/optional.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/numeric.hpp>
#include <algorithm>
#include <cmath>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
+ Include dependency graph for particle_node.cpp:

Go to the source code of this file.

Namespaces

namespace  anonymous_namespace{particle_node.cpp}
 

Functions

static auto & get_cell_structure ()
 
static auto rebuild_needed ()
 
static void mpi_synchronize_max_seen_pid_local ()
 
void init_type_map (int type)
 
static void remove_id_from_map (int p_id, int type)
 
static void add_id_to_type_map (int p_id, int type)
 
void on_particle_type_change (int p_id, int old_type, int new_type)
 
void invalidate_fetch_cache ()
 Invalidate the fetch cache for get_particle_data.
 
std::size_t fetch_cache_max_size ()
 Return the maximal number of particles that are kept in the fetch cache.
 
static void mpi_send_particle_data_local (int p_id)
 
const Particleget_particle_data (int p_id)
 Get particle data.
 
static void mpi_get_particles_local ()
 
static std::vector< Particlempi_get_particles (Utils::Span< const int > ids)
 Get multiple particles at once.
 
void prefetch_particle_data (Utils::Span< const int > in_ids)
 Fetch a range of particle into the fetch cache.
 
static void mpi_who_has_local ()
 
static void mpi_who_has_head ()
 
static void build_particle_node ()
 Rebuild the particle index.
 
static void build_particle_node_parallel ()
 Rebuild the particle index.
 
int get_particle_node (int p_id)
 Get the MPI rank which owns the a specific particle.
 
int get_particle_node_parallel (int p_id)
 
void clear_particle_node ()
 Invalidate particle_node.
 
static void clear_particle_type_map ()
 
static int calculate_max_seen_id ()
 Calculate the largest particle id.
 
static bool maybe_insert_particle (int p_id, Utils::Vector3d const &pos)
 Create a new particle and attach it to a cell.
 
static bool maybe_move_particle (int p_id, Utils::Vector3d const &pos)
 Move particle to a new position.
 
void remove_all_particles ()
 Remove all particles.
 
void remove_particle (int p_id)
 Remove particle with a given identity.
 
void make_new_particle (int p_id, Utils::Vector3d const &pos)
 Create a new particle and attach it to a cell.
 
void set_particle_pos (int p_id, Utils::Vector3d const &pos)
 Move particle to a new position.
 
int get_random_p_id (int type, int random_index_in_type_map)
 Find a particle of given type and return its id.
 
int number_of_particles_with_type (int type)
 
bool particle_exists (int p_id)
 Check if particle exists.
 
std::vector< int > get_particle_ids ()
 Get all particle ids.
 
std::vector< int > get_particle_ids_parallel ()
 
int get_maximal_particle_id ()
 Get maximal particle id.
 
int get_n_part ()
 Get number of particles.
 

Variables

constexpr auto some_tag = 42
 
static bool type_list_enable
 Enable particle type tracking in particle_type_map.
 
static std::unordered_map< int, std::unordered_set< int > > particle_type_map
 Mapping particle types to lists of particle ids.
 
static std::unordered_map< int, int > particle_node
 Mapping particle ids to MPI ranks.
 
static int max_seen_pid = -1
 Keep track of the largest particle id.
 
std::size_t const anonymous_namespace{particle_node.cpp}::max_cache_size = (100ul * 1048576ul) / sizeof(Particle)
 
Utils::Cache< int, Particleanonymous_namespace{particle_node.cpp}::particle_fetch_cache (max_cache_size)
 

Function Documentation

◆ add_id_to_type_map()

static void add_id_to_type_map ( int  p_id,
int  type 
)
static

Definition at line 123 of file particle_node.cpp.

References particle_type_map.

Referenced by on_particle_type_change().

◆ build_particle_node()

static void build_particle_node ( )
static

Rebuild the particle index.

Definition at line 348 of file particle_node.cpp.

References mpi_call(), mpi_who_has_head(), and mpi_who_has_local().

Referenced by get_n_part(), get_particle_ids(), get_particle_node(), and particle_exists().

◆ build_particle_node_parallel()

static void build_particle_node_parallel ( )
static

◆ calculate_max_seen_id()

static int calculate_max_seen_id ( )
static

Calculate the largest particle id.

Traversing the particle_node to find the largest particle id scales with O(N). Consider using the cached value in max_seen_pid if possible. This function is only necessary when the cached value is invalidated, for example when removing the particle which has the largest id.

Definition at line 424 of file particle_node.cpp.

References particle_node.

Referenced by remove_particle().

◆ clear_particle_node()

void clear_particle_node ( )

Invalidate particle_node.

This has to be done at the beginning of the integration.

Definition at line 405 of file particle_node.cpp.

References max_seen_pid, and particle_node.

Referenced by System::System::on_cell_structure_change(), System::System::on_observable_calc(), and remove_all_particles().

◆ clear_particle_type_map()

static void clear_particle_type_map ( )
static

Definition at line 410 of file particle_node.cpp.

References particle_type_map.

Referenced by remove_all_particles().

◆ fetch_cache_max_size()

std::size_t fetch_cache_max_size ( )

Return the maximal number of particles that are kept in the fetch cache.

Definition at line 160 of file particle_node.cpp.

References anonymous_namespace{particle_node.cpp}::particle_fetch_cache.

◆ get_cell_structure()

◆ get_maximal_particle_id()

◆ get_n_part()

int get_n_part ( )

Get number of particles.

Definition at line 611 of file particle_node.cpp.

References build_particle_node(), and particle_node.

Referenced by ScriptInterface::Particles::ParticleList::do_call_method().

◆ get_particle_data()

◆ get_particle_ids()

std::vector< int > get_particle_ids ( )

◆ get_particle_ids_parallel()

std::vector< int > get_particle_ids_parallel ( )

◆ get_particle_node()

int get_particle_node ( int  p_id)

Get the MPI rank which owns the a specific particle.

Parameters
p_ididentity of the particle
Returns
The MPI rank the particle is on.

Definition at line 364 of file particle_node.cpp.

References build_particle_node(), and particle_node.

Referenced by get_particle_data(), mpi_get_particles(), and ScriptInterface::Particles::ParticleHandle::ParticleHandle().

◆ get_particle_node_parallel()

int get_particle_node_parallel ( int  p_id)

◆ get_random_p_id()

int get_random_p_id ( int  type,
int  random_index_in_type_map 
)

Find a particle of given type and return its id.

Definition at line 550 of file particle_node.cpp.

References comm_cart, and particle_type_map.

Referenced by ReactionMethods::ReactionAlgorithm::displacement_mc_move(), and ReactionMethods::ReactionAlgorithm::make_reaction_attempt().

◆ init_type_map()

◆ invalidate_fetch_cache()

void invalidate_fetch_cache ( )

Invalidate the fetch cache for get_particle_data.

Definition at line 159 of file particle_node.cpp.

References anonymous_namespace{particle_node.cpp}::particle_fetch_cache.

Referenced by System::System::on_integration_start(), and System::System::on_particle_change().

◆ make_new_particle()

void make_new_particle ( int  p_id,
Utils::Vector3d const &  pos 
)

Create a new particle and attach it to a cell.

Parameters
p_idThe identity of the particle to create.
posThe particle position.

Definition at line 519 of file particle_node.cpp.

References build_particle_node_parallel(), comm_cart, System::get_system(), max_seen_pid, maybe_insert_particle(), mpi_synchronize_max_seen_pid_local(), node, System::System::on_particle_change(), particle_node, pos, rebuild_needed(), and this_node.

Referenced by ScriptInterface::Particles::ParticleHandle::do_construct().

◆ maybe_insert_particle()

static bool maybe_insert_particle ( int  p_id,
Utils::Vector3d const &  pos 
)
static

Create a new particle and attach it to a cell.

Parameters
p_idThe identity of the particle to create.
posThe particle position.
Returns
Whether the particle was created on that node.

Definition at line 437 of file particle_node.cpp.

References System::System::box_geo, get_cell_structure(), System::get_system(), and pos.

Referenced by make_new_particle().

◆ maybe_move_particle()

static bool maybe_move_particle ( int  p_id,
Utils::Vector3d const &  pos 
)
static

Move particle to a new position.

Parameters
p_idThe identity of the particle to move.
posThe new particle position.
Returns
Whether the particle was moved from that node.

Definition at line 458 of file particle_node.cpp.

References System::get_system(), and pos.

Referenced by set_particle_pos().

◆ mpi_get_particles()

static std::vector< Particle > mpi_get_particles ( Utils::Span< const int >  ids)
static

Get multiple particles at once.

WARNING Particles are returned in an arbitrary order.

Parameters
idsThe ids of the particles that should be returned.
Returns
The particle list.

Definition at line 224 of file particle_node.cpp.

References comm_cart, Utils::Mpi::gatherv(), get_particle_node(), mpi_call(), mpi_get_particles_local(), and this_node.

Referenced by prefetch_particle_data().

◆ mpi_get_particles_local()

static void mpi_get_particles_local ( )
static

Definition at line 198 of file particle_node.cpp.

References comm_cart, and Utils::Mpi::gatherv().

Referenced by mpi_get_particles().

◆ mpi_send_particle_data_local()

static void mpi_send_particle_data_local ( int  p_id)
static

Definition at line 162 of file particle_node.cpp.

References comm_cart, and get_cell_structure().

Referenced by get_particle_data().

◆ mpi_synchronize_max_seen_pid_local()

static void mpi_synchronize_max_seen_pid_local ( )
static

Definition at line 88 of file particle_node.cpp.

References comm_cart, and max_seen_pid.

Referenced by make_new_particle(), mpi_who_has_head(), mpi_who_has_local(), and remove_particle().

◆ mpi_who_has_head()

static void mpi_who_has_head ( )
static

◆ mpi_who_has_local()

static void mpi_who_has_local ( )
static

◆ number_of_particles_with_type()

◆ on_particle_type_change()

◆ particle_exists()

bool particle_exists ( int  p_id)

Check if particle exists.

Parameters
p_ididentity of the particle
Returns
True iff the particle exists.

Definition at line 578 of file particle_node.cpp.

References build_particle_node(), and particle_node.

Referenced by ScriptInterface::Particles::ParticleList::do_call_method(), and ScriptInterface::Particles::ParticleSlice::do_call_method().

◆ prefetch_particle_data()

void prefetch_particle_data ( Utils::Span< const int >  ids)

Fetch a range of particle into the fetch cache.

If the range is larger than the cache size, only the particle that fit into the cache are fetched.

The particles have to exist, an exception it throw if one of the the particles can not be found.

Parameters
idsIds of the particles that should be fetched.

Definition at line 267 of file particle_node.cpp.

References Utils::Span< T >::begin(), comm_cart, Utils::Span< T >::end(), mpi_get_particles(), and anonymous_namespace{particle_node.cpp}::particle_fetch_cache.

Referenced by ScriptInterface::Particles::ParticleSlice::do_call_method().

◆ rebuild_needed()

static auto rebuild_needed ( )
static

◆ remove_all_particles()

◆ remove_id_from_map()

static void remove_id_from_map ( int  p_id,
int  type 
)
static

Definition at line 117 of file particle_node.cpp.

References particle_type_map.

Referenced by on_particle_type_change(), and remove_particle().

◆ remove_particle()

◆ set_particle_pos()

void set_particle_pos ( int  p_id,
Utils::Vector3d const &  pos 
)

Move particle to a new position.

Parameters
p_idThe identity of the particle to move.
posThe new particle position.

Definition at line 537 of file particle_node.cpp.

References comm_cart, get_cell_structure(), System::get_system(), maybe_move_particle(), System::System::on_particle_change(), pos, Cells::RESORT_GLOBAL, and this_node.

Referenced by ReactionMethods::ReactionAlgorithm::displacement_mc_move(), and ScriptInterface::Particles::ParticleHandle::ParticleHandle().

Variable Documentation

◆ max_seen_pid

int max_seen_pid = -1
static

Keep track of the largest particle id.

This book-keeping variable is necessary to make particle insertion run in constant time. Traversing the particle_node to find the largest particle id scales with O(N) and traversing the local cells in parallel followed by a reduction scales with O(N^2).

Definition at line 80 of file particle_node.cpp.

Referenced by clear_particle_node(), get_maximal_particle_id(), make_new_particle(), mpi_synchronize_max_seen_pid_local(), mpi_who_has_head(), and remove_particle().

◆ particle_node

◆ particle_type_map

std::unordered_map<int, std::unordered_set<int> > particle_type_map
static

◆ some_tag

constexpr auto some_tag = 42
constexpr

Definition at line 58 of file particle_node.cpp.

Referenced by mpi_who_has_head(), and mpi_who_has_local().

◆ type_list_enable

bool type_list_enable
static

Enable particle type tracking in particle_type_map.

Definition at line 61 of file particle_node.cpp.

Referenced by init_type_map(), on_particle_type_change(), and remove_particle().