34#include "communication.hpp"
40#include "system/System.hpp"
47#ifdef ESPRESSO_CALIPER
48#include <caliper/cali.h>
51#include <boost/mpi/collectives/all_reduce.hpp>
53#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
54#include <Cabana_Core.hpp>
55#include <Cabana_NeighborList.hpp>
56#include <Kokkos_Core.hpp>
76#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
79 m_kokkos_handle.reset();
83#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
85 m_local_force.reset();
86#ifdef ESPRESSO_ROTATION
87 m_local_torque.reset();
90 m_local_virial.reset();
92 m_id_to_index.reset();
94 m_verlet_list_cabana.reset();
95 m_rebuild_verlet_list_cabana =
true;
99 m_kokkos_handle = std::move(handle);
103 std::size_t number_of_unique_particles) {
104 if (std::isinf(pair_cutoff)) {
105 return number_of_unique_particles;
107 if (pair_cutoff < 0.) {
110 auto const volume = Utils::int_pow<3>(pair_cutoff);
111 auto max_counts =
static_cast<std::size_t
>(std::ceil(8. * volume));
112 std::size_t
constexpr threshold_num = 16;
113 if (max_counts < threshold_num) {
114 max_counts = std::min(threshold_num, number_of_unique_particles);
120#ifdef ESPRESSO_CALIPER
121 CALI_CXX_MARK_FUNCTION;
123 assert(m_kokkos_handle);
124 using execution_space = Kokkos::DefaultExecutionSpace;
125 auto const num_threads = execution_space().concurrency();
128#ifdef ESPRESSO_COLLISION_DETECTION
130 if (system.has_collision_detection_enabled()) {
132 max_counts = num_part * 2ul;
137#ifdef ESPRESSO_ROTATION
143 m_aosoa->resize(num_part);
144 Kokkos::deep_copy(m_aosoa->flags, uint8_t{0});
145 m_verlet_list_cabana->reallocData(num_part, max_counts);
148 std::make_unique<ForceType>(
"local_force", num_part, num_threads);
149#ifdef ESPRESSO_ROTATION
151 std::make_unique<ForceType>(
"local_torque", num_part, num_threads);
153 m_id_to_index = std::make_unique<Kokkos::View<int *>>(
154 Kokkos::ViewAllocateWithoutInitializing(
"id_to_index"),
158 m_aosoa = std::make_unique<AoSoA_pack>();
159 m_aosoa->resize(num_part);
160 Kokkos::deep_copy(m_aosoa->flags, uint8_t{0});
162 m_verlet_list_cabana =
163 std::make_unique<ListType>(0ul, num_part, max_counts);
166 m_local_virial = std::make_unique<VirialType>(
"local_virial", num_threads);
171#ifdef ESPRESSO_CALIPER
172 CALI_CXX_MARK_FUNCTION;
179#ifdef ESPRESSO_ROTATION
185 Kokkos::deep_copy(
get_aosoa().flags, uint8_t{0});
189#ifdef ESPRESSO_CALIPER
190 CALI_CXX_MARK_FUNCTION;
192 auto &unique_particles = m_unique_particles;
193 unique_particles.clear();
195 std::unordered_set<int> registered_index{};
196 using execution_space = Kokkos::DefaultExecutionSpace;
197 int n_threads = execution_space().concurrency();
198 std::vector<int> max_ids(n_threads);
200 *
this, [&unique_particles, &max_ids](std::size_t index,
Particle &p) {
201 unique_particles[index] = &p;
202 const int thread_num = omp_get_thread_num();
203 max_ids[thread_num] = std::max(p.
id(), max_ids[thread_num]);
205 int max_id = *(std::max_element(max_ids.begin(), max_ids.end()));
208 if (not local_particle) {
211 if (not local_particle->is_ghost()) {
214 if (registered_index.contains(p.
id())) {
217 registered_index.insert(p.
id());
218 unique_particles.emplace_back(&p);
219 max_id = std::max(p.
id(), max_id);
221 registered_index.clear();
222 m_cached_max_local_particle_id = max_id;
234 auto const id = p.id();
236 if (id < 0 || id > max_id) {
237 throw std::runtime_error(
"Particle id out of bounds.");
241 throw std::runtime_error(
"Invalid local particle index entry.");
246 std::size_t local_part_cnt = 0u;
251 throw std::runtime_error(
"local_particles part has corrupted id.");
257 throw std::runtime_error(
259 std::to_string(local_part_cnt) +
" parts in local_particles");
265 for (
auto const &p : cell->particles()) {
266 if (particle_to_cell(p) != cell) {
267 throw std::runtime_error(
"misplaced particle with id " +
268 std::to_string(p.id()));
275 auto remove_all_bonds_to = [id](
BondList &bl) {
276 for (
auto it = bl.begin(); it != bl.end();) {
286 auto &parts = cell->particles();
287 for (
auto it = parts.begin(); it != parts.end();) {
288 if (it->id() == id) {
289 it = parts.erase(it);
293 remove_all_bonds_to(it->bonds());
301 auto const sort_cell = particle_to_cell(p);
303 return std::addressof(
304 append_indexed_particle(sort_cell->particles(), std::move(p)));
311 auto const sort_cell = particle_to_cell(p);
320 return std::addressof(
321 append_indexed_particle(cell->particles(), std::move(p)));
325 auto it = std::ranges::find_if(std::ranges::views::reverse(m_particle_index),
326 [](
auto const *p) {
return p !=
nullptr; });
328 return (it != m_particle_index.rend()) ? (*it)->id() : -1;
333 cell->particles().clear();
336 m_particle_index.clear();
342 using namespace Cells;
350#ifdef ESPRESSO_BOND_CONSTRAINT
369#ifdef ESPRESSO_BOND_CONSTRAINT
393 static std::vector<ParticleChange> diff;
396 m_decomposition->resort(global_flag, diff);
398 for (
auto d : diff) {
399 std::visit(UpdateParticleIndexVisitor{
this}, d);
402 auto const &lebc =
get_system().box_geo->lees_edwards_bc();
403 m_rebuild_verlet_list =
true;
404 m_rebuild_verlet_list_cabana =
true;
405 m_le_pos_offset_at_last_resort = lebc.pos_offset;
407#ifdef ESPRESSO_ADDITIONAL_CHECKS
415 auto &local_geo = *system.local_geo;
416 auto const &box_geo = *system.box_geo;
417 set_particle_decomposition(
418 std::make_unique<AtomDecomposition>(
::comm_cart, box_geo));
420 local_geo.set_cell_structure_type(m_type);
421 system.on_cell_structure_change();
425 double range, std::optional<std::pair<int, int>> fully_connected_boundary) {
427 auto &local_geo = *system.local_geo;
428 auto const &box_geo = *system.box_geo;
429 set_particle_decomposition(std::make_unique<RegularDecomposition>(
430 ::comm_cart, range, box_geo, local_geo, fully_connected_boundary));
432 local_geo.set_cell_structure_type(m_type);
433 system.on_cell_structure_change();
437 std::set<int> n_square_types) {
439 auto &local_geo = *system.local_geo;
440 auto const &box_geo = *system.box_geo;
441 set_particle_decomposition(std::make_unique<HybridDecomposition>(
443 [&system]() {
return system.get_global_ghost_flags(); }, box_geo,
444 local_geo, n_square_types));
446 local_geo.set_cell_structure_type(m_type);
447 system.on_cell_structure_change();
452 m_verlet_skin = value;
453 m_verlet_skin_set =
true;
454 m_rebuild_verlet_list_cabana =
true;
460 auto const max_cut =
get_system().maximal_cutoff();
462 throw std::runtime_error(
463 "cannot automatically determine skin, please set it manually");
468 auto const new_skin = std::min(0.4 * max_cut,
max_range - max_cut);
474 auto constexpr resort_only_parts =
477 auto const global_resort = boost::mpi::all_reduce(
478 ::comm_cart, m_resort_particles, std::bit_or<unsigned>());
504#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
505void CellStructure::parallel_for_each_particle_impl(
507 if (cells.size() > 1) {
508 Kokkos::parallel_for(
509 "for_each_local_particle", cells.size(), [&](
auto cell_idx) {
510 for (auto &p : cells[cell_idx]->particles())
513 }
else if (cells.size() == 1) {
514 auto &
particles = cells.front()->particles();
515 Kokkos::parallel_for(
517 [&](
auto part_idx) { f(*(particles.begin() + part_idx)); });
524 auto const lim =
Utils::sqr(m_verlet_skin / 2.) - additional_offset.
norm2();
527 [lim](
bool &result,
Particle const &p) {
528 if ((p.pos() - p.pos_at_last_verlet_update()).norm2() > lim) {
@ NSQUARE
Atom decomposition (N-square).
@ HYBRID
Hybrid decomposition.
@ REGULAR
Regular decomposition.
static auto estimate_max_counts(double pair_cutoff, std::size_t number_of_unique_particles)
unsigned map_data_parts(unsigned data_parts)
Map the data parts flags from cells to those used internally by the ghost communication.
unsigned map_data_parts(unsigned data_parts)
Map the data parts flags from cells to those used internally by the ghost communication.
std::function< void(Particle &)> ParticleUnaryOp
Vector implementation and trait types for boost qvm interoperability.
Atom decomposition cell system.
Describes a cell structure / cell system.
ParticleRange ghost_particles() const
Particle * get_local_particle(int id)
Get a local particle by id.
void set_kokkos_handle(std::shared_ptr< KokkosHandle > handle)
void check_particle_sorting() const
Check that particles are in the correct cell.
std::size_t count_local_particles() const
void clear_resort_particles()
Set the resort level to sorted.
auto is_verlet_skin_set() const
Whether the Verlet skin is set.
void clear_local_properties()
ParticleDecomposition const & decomposition() const
Get the underlying particle decomposition.
void update_ghosts_and_resort_particle(unsigned data_parts)
Update ghost particles, with particle resort if needed.
Particle * add_local_particle(Particle &&p)
Add a particle.
void set_verlet_skin_heuristic()
Set the Verlet skin using a heuristic.
void set_verlet_skin(double value)
Set the Verlet skin.
void ghosts_update(unsigned data_parts)
Update ghost particles.
int get_cached_max_local_particle_id() const
CellStructure(BoxGeometry const &box)
auto & get_local_torque()
auto & get_local_virial()
void update_particle_index(int id, Particle *p)
Update local particle index.
void ghosts_reduce_forces()
Add forces from ghost particles to real particles.
auto const & get_unique_particles() const
void rebuild_local_properties(double pair_cutoff)
Utils::Vector3d max_range() const
Maximal pair range supported by current cell system.
bool check_resort_required(Utils::Vector3d const &additional_offset={}) const
Check whether a particle has moved further than half the skin since the last Verlet list update,...
void ghosts_count()
Synchronize number of ghosts.
void set_resort_particles(Cells::Resort level)
Increase the local resort level at least to level.
void remove_particle(int id)
Remove a particle.
Particle * add_particle(Particle &&p)
Add a particle.
void resort_particles(bool global_flag)
Resort particles.
void check_particle_index() const
Check that particle index is commensurate with particles.
void set_regular_decomposition(double range, std::optional< std::pair< int, int > > fully_connected_boundary)
Set the particle decomposition to RegularDecomposition.
void set_atom_decomposition()
Set the particle decomposition to AtomDecomposition.
void remove_all_particles()
Remove all particles from the cell system.
ParticleRange local_particles() const
void ghosts_reduce_rattle_correction()
Add rattle corrections from ghost particles to real particles.
void set_hybrid_decomposition(double cutoff_regular, std::set< int > n_square_types)
Set the particle decomposition to HybridDecomposition.
int get_max_local_particle_id() const
Get the maximal particle id on this node.
Utils::Vector3d max_cutoff() const
Maximal cutoff supported by current cell system.
void reset_local_properties()
virtual std::span< Cell *const > local_cells() const =0
Get pointer to local cells.
base_type::size_type size() const
constexpr T norm2() const
boost::mpi::communicator comm_cart
The communicator.
void ghost_communicator(GhostCommunicator const &gcr, BoxGeometry const &box_geo, unsigned int data_parts)
Do a ghost communication with the specified data parts.
@ GHOSTTRANS_MOMENTUM
transfer ParticleMomentum
@ GHOSTTRANS_RATTLE
transfer ParticleRattle
@ GHOSTTRANS_PARTNUM
resize the receiver particle arrays to the size of the senders
@ GHOSTTRANS_POSITION
transfer ParticlePosition
@ GHOSTTRANS_PROPRTS
transfer ParticleProperties
@ GHOSTTRANS_FORCE
transfer ParticleForce
@ DATA_PART_PROPERTIES
Particle::p.
@ DATA_PART_BONDS
Particle::bonds.
ParticleRange particles(std::span< Cell *const > cells)
std::function< void(ResultType &, ResultType const &)> ReductionOp
Join two partial reduction results.
std::function< void(ResultType &, Particle const &)> AddPartialResultKernel
Kernel that adds the result from a single particle to a reduction.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
bool contains(Range &&rng, T const &value)
Check whether a range contains a value.
void enumerate_local_particles(CellStructure const &cs, Kernel &&kernel)
Run a kernel on all local particles with enumeration.
ResultType reduce_over_local_particles(CellStructure const &cs, Reduction::AddPartialResultKernel< ResultType > add_partial, Reduction::ReductionOp< ResultType > reduce_op)
performs a reduction over all particles
Struct holding all information for one particle.
Apply a ParticleChange to a particle index.
void operator()(RemovedParticle rp) const
void operator()(ModifiedList mp) const