35#include "communication.hpp"
41#include "system/System.hpp"
48#ifdef ESPRESSO_CALIPER
49#include <caliper/cali.h>
52#include <boost/mpi/collectives/all_reduce.hpp>
69#include <unordered_set>
77 m_kokkos_handle.reset();
81 m_scatter_force.reset();
82 m_local_force.reset();
83#ifdef ESPRESSO_ROTATION
84 m_scatter_torque.reset();
85 m_local_torque.reset();
88 m_scatter_virial.reset();
89 m_local_virial.reset();
91 m_id_to_index.reset();
93 m_verlet_list_cabana.reset();
94 m_bond_state->clear();
95 m_rebuild_verlet_list_cabana =
true;
100 m_kokkos_handle = std::move(handle);
101 m_bond_state = std::make_unique<LocalBondState>();
105 std::size_t number_of_unique_particles,
106 double local_box_volume,
107 std::size_t num_local_particles) {
108 if (std::isinf(pair_cutoff)) {
109 return number_of_unique_particles;
111 if (pair_cutoff < 0.) {
117 auto const local_density =
118 (local_box_volume > 0. && num_local_particles > 0)
119 ?
static_cast<double>(num_local_particles) / local_box_volume
121 auto const cutoff_sphere_volume =
122 (4. / 3.) * std::numbers::pi * Utils::int_pow<3>(pair_cutoff);
124 auto const fluctuation_factor = 2.;
125 auto max_counts =
static_cast<std::size_t
>(
126 std::ceil(fluctuation_factor * local_density * cutoff_sphere_volume));
127 std::size_t
constexpr threshold_num = 16;
128 if (max_counts < threshold_num) {
129 max_counts = std::min(threshold_num, number_of_unique_particles);
135#ifdef ESPRESSO_CALIPER
136 CALI_CXX_MARK_FUNCTION;
138 assert(m_kokkos_handle);
141 auto const local_box_volume = system.local_geo->volume();
144#ifdef ESPRESSO_COLLISION_DETECTION
145 if (system.has_collision_detection_enabled()) {
147 max_counts = num_part * 2ul;
153 m_scatter_force.emplace(
155#ifdef ESPRESSO_ROTATION
158 m_scatter_torque.emplace(
164 m_aosoa->resize(num_part);
165 Kokkos::deep_copy(m_aosoa->flags, uint8_t{0});
166 m_verlet_list_cabana->reallocData(num_part, max_counts);
168 m_local_force = std::make_unique<ForceType>(
"local_force", num_part);
169 m_scatter_force.emplace(
170 Kokkos::Experimental::create_scatter_view(*m_local_force));
171#ifdef ESPRESSO_ROTATION
172 m_local_torque = std::make_unique<ForceType>(
"local_torque", num_part);
173 m_scatter_torque.emplace(
174 Kokkos::Experimental::create_scatter_view(*m_local_torque));
176 m_id_to_index = std::make_unique<Kokkos::View<int *>>(
177 Kokkos::ViewAllocateWithoutInitializing(
"id_to_index"),
181 m_aosoa = std::make_unique<AoSoA_pack>();
182 m_aosoa->resize(num_part);
183 Kokkos::deep_copy(m_aosoa->flags, uint8_t{0});
185 m_verlet_list_cabana =
186 std::make_unique<ListType>(0ul, num_part, max_counts);
189 m_local_virial = std::make_unique<VirialType>(
"local_virial");
190 m_scatter_virial.emplace(
191 Kokkos::Experimental::create_scatter_view(*m_local_virial));
196#ifdef ESPRESSO_CALIPER
197 CALI_CXX_MARK_FUNCTION;
200 m_scatter_force->reset();
201#ifdef ESPRESSO_ROTATION
203 m_scatter_torque->reset();
209 m_scatter_force->reset();
210#ifdef ESPRESSO_ROTATION
212 m_scatter_torque->reset();
216 m_scatter_virial->reset();
218 Kokkos::deep_copy(
get_aosoa().flags, uint8_t{0});
224 auto &pair_list = m_bond_state->pair_list;
225 auto &pair_ids = m_bond_state->pair_ids;
226 auto &angle_list = m_bond_state->angle_list;
227 auto &angle_ids = m_bond_state->angle_ids;
228 auto &dihedral_list = m_bond_state->dihedral_list;
229 auto &dihedral_ids = m_bond_state->dihedral_ids;
230 for (
auto const bond : p.
bonds()) {
231 auto const partner_ids = bond.partner_ids();
234 if (partners.size() == 1u) {
235 auto p_index = Kokkos::atomic_fetch_add(&pair_count, 1);
236 pair_list(p_index, 0) = p.
id();
237 pair_list(p_index, 1) = partners[0]->id();
238 pair_ids(p_index) = bond.bond_id();
239 }
else if (partners.size() == 2u) {
240 auto a_index = Kokkos::atomic_fetch_add(&angle_count, 1);
241 angle_list(a_index, 0) = p.
id();
242 angle_list(a_index, 1) = partners[0]->id();
243 angle_list(a_index, 2) = partners[1]->id();
244 angle_ids(a_index) = bond.bond_id();
245 }
else if (partners.size() == 3u) {
246 auto d_index = Kokkos::atomic_fetch_add(&dihedral_count, 1);
247 dihedral_list(d_index, 0) = p.
id();
248 dihedral_list(d_index, 1) = partners[0]->id();
249 dihedral_list(d_index, 2) = partners[1]->id();
250 dihedral_list(d_index, 3) = partners[2]->id();
251 dihedral_ids(d_index) = bond.bond_id();
260#ifdef ESPRESSO_CALIPER
261 CALI_CXX_MARK_FUNCTION;
263 auto &unique_particles = m_unique_particles;
264 unique_particles.clear();
266 std::unordered_set<int> registered_index{};
267 using execution_space = Kokkos::DefaultExecutionSpace;
268 int n_threads = execution_space().concurrency();
269 std::vector<int> max_ids(n_threads);
271 m_bond_state->reset_counts();
272 std::vector<int> pair_counts(n_threads, 0);
273 std::vector<int> angle_counts(n_threads, 0);
274 std::vector<int> dihedral_counts(n_threads, 0);
277 *
this, [&unique_particles, &max_ids, &pair_counts, &angle_counts,
278 &dihedral_counts](std::size_t index,
Particle &p) {
279 unique_particles[index] = &p;
280 auto const thread_num = omp_get_thread_num();
281 max_ids[thread_num] = std::max(p.
id(), max_ids[thread_num]);
282 for (
auto const bond : p.
bonds()) {
283 if (not bond.partner_ids().empty()) {
284 auto const partner_ids = bond.partner_ids();
285 if (partner_ids.size() == 1u) {
286 pair_counts[thread_num] += 1;
287 }
else if (partner_ids.size() == 2u) {
288 angle_counts[thread_num] += 1;
289 }
else if (partner_ids.size() == 3u) {
290 dihedral_counts[thread_num] += 1;
296 int pair_count = std::reduce(std::begin(pair_counts), std::end(pair_counts));
298 std::reduce(std::begin(angle_counts), std::end(angle_counts));
300 std::reduce(std::begin(dihedral_counts), std::end(dihedral_counts));
302 m_bond_state->allocate();
304 int max_id = *(std::max_element(max_ids.begin(), max_ids.end()));
307 if (not local_particle) {
310 if (not local_particle->is_ghost()) {
313 if (registered_index.contains(p.
id())) {
316 registered_index.insert(p.
id());
317 unique_particles.emplace_back(&p);
318 max_id = std::max(p.
id(), max_id);
320 registered_index.clear();
321 m_cached_max_local_particle_id = max_id;
322 m_num_local_particles_cached = unique_particles.size();
332 auto const id = p.id();
334 if (id < 0 || id > max_id) {
335 throw std::runtime_error(
"Particle id out of bounds.");
339 throw std::runtime_error(
"Invalid local particle index entry.");
344 std::size_t local_part_cnt = 0u;
349 throw std::runtime_error(
"local_particles part has corrupted id.");
355 throw std::runtime_error(
357 std::to_string(local_part_cnt) +
" parts in local_particles");
363 for (
auto const &p : cell->particles()) {
364 if (particle_to_cell(p) != cell) {
365 throw std::runtime_error(
"misplaced particle with id " +
366 std::to_string(p.id()));
373 auto remove_all_bonds_to = [id](
BondList &bl) {
374 for (
auto it = bl.begin(); it != bl.end();) {
384 auto &parts = cell->particles();
385 for (
auto it = parts.begin(); it != parts.end();) {
386 if (it->id() == id) {
387 it = parts.erase(it);
391 remove_all_bonds_to(it->bonds());
399 auto const sort_cell = particle_to_cell(p);
401 return std::addressof(
402 append_indexed_particle(sort_cell->particles(), std::move(p)));
409 auto const sort_cell = particle_to_cell(p);
418 return std::addressof(
419 append_indexed_particle(cell->particles(), std::move(p)));
423 auto it = std::ranges::find_if(std::ranges::views::reverse(m_particle_index),
424 [](
auto const *p) {
return p !=
nullptr; });
426 return (it != m_particle_index.rend()) ? (*it)->id() : -1;
430 return m_bond_state->pair_count;
433 return m_bond_state->angle_count;
436 return m_bond_state->dihedral_count;
439 int dihedral_value) {
440 m_bond_state->set_counts(pair_value, angle_value, dihedral_value);
442#ifdef ESPRESSO_COLLISION_DETECTION
445 std::vector<int>
const &particle_ids) {
453 cell->particles().clear();
456 m_particle_index.clear();
463 using namespace Cells;
471#ifdef ESPRESSO_BOND_CONSTRAINT
490#ifdef ESPRESSO_BOND_CONSTRAINT
514 std::vector<ParticleChange> diff;
516 m_decomposition->resort(global_flag, diff);
518 for (
auto d : diff) {
519 std::visit(UpdateParticleIndexVisitor{
this}, d);
522 auto const &lebc =
get_system().box_geo->lees_edwards_bc();
523 m_rebuild_verlet_list =
true;
524 m_rebuild_verlet_list_cabana =
true;
525 m_le_pos_offset_at_last_resort = lebc.pos_offset;
527#ifdef ESPRESSO_ADDITIONAL_CHECKS
535 auto &local_geo = *system.local_geo;
536 auto const &box_geo = *system.box_geo;
537 set_particle_decomposition(
538 std::make_unique<AtomDecomposition>(
::comm_cart, box_geo));
540 local_geo.set_cell_structure_type(m_type);
541 system.on_cell_structure_change();
545 double range, std::optional<std::pair<int, int>> fully_connected_boundary) {
547 auto &local_geo = *system.local_geo;
548 auto const &box_geo = *system.box_geo;
549 set_particle_decomposition(std::make_unique<RegularDecomposition>(
550 ::comm_cart, range, box_geo, local_geo, fully_connected_boundary));
552 local_geo.set_cell_structure_type(m_type);
553 system.on_cell_structure_change();
557 std::set<int> n_square_types) {
559 auto &local_geo = *system.local_geo;
560 auto const &box_geo = *system.box_geo;
561 set_particle_decomposition(std::make_unique<HybridDecomposition>(
563 [&system]() {
return system.get_global_ghost_flags(); }, box_geo,
564 local_geo, n_square_types));
566 local_geo.set_cell_structure_type(m_type);
567 system.on_cell_structure_change();
572 m_verlet_skin = value;
573 m_verlet_skin_set =
true;
574 m_rebuild_verlet_list_cabana =
true;
580 auto const max_cut =
get_system().maximal_cutoff();
582 throw std::runtime_error(
583 "cannot automatically determine skin, please set it manually");
588 auto const new_skin = std::min(0.4 * max_cut,
max_range - max_cut);
594 auto constexpr resort_only_parts =
597 auto const global_resort = boost::mpi::all_reduce(
598 ::comm_cart, m_resort_particles, std::bit_or<unsigned>());
626 auto const lim =
Utils::sqr(m_verlet_skin / 2.) - additional_offset.
norm2();
629 [lim](
bool &result,
Particle const &p) {
630 if ((p.pos() - p.pos_at_last_verlet_update()).norm2() > lim) {
@ NSQUARE
Atom decomposition (N-square).
@ HYBRID
Hybrid decomposition.
@ REGULAR
Regular decomposition.
unsigned map_data_parts(unsigned data_parts)
Map the data parts flags from cells to those used internally by the ghost communication.
static auto estimate_max_counts(double pair_cutoff, std::size_t number_of_unique_particles, double local_box_volume, std::size_t num_local_particles)
unsigned map_data_parts(unsigned data_parts)
Map the data parts flags from cells to those used internally by the ghost communication.
Vector implementation and trait types for boost qvm interoperability.
void bond_resolution_error(std::span< const int > partner_ids)
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
int get_local_angle_bond_numbers() 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.
int get_local_pair_bond_numbers() const
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_local_dihedral_bond_numbers() const
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 set_local_bond_numbers(int p, int a, int d)
void ghosts_reduce_forces()
Add forces and torques 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.
void add_new_bond(int bond_id, std::vector< int > const &particle_ids)
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,...
auto resolve_bond_partners(std::span< const int > partner_ids)
Resolve ids to particles.
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.
std::size_t get_num_local_particles_cached() const
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 reset_local_force_and_torque()
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 update_bond_storage(int &pair_count, int &angle_count, int &dihedral_count, Particle const &p)
Update bond storage(m_*_bond_list_kokkos and m_*_bond_id_kokkos).
void clear_bond_properties()
void reset_local_properties()
virtual std::span< Cell *const > local_cells() const =0
Get pointer to local cells.
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.
Ghost particles and particle exchange.
@ 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.
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
Exception indicating that a particle id could not be resolved.
Struct holding all information for one particle.
auto const & bonds() const
Apply a ParticleChange to a particle index.
void operator()(RemovedParticle rp) const
void operator()(ModifiedList mp) const