35#include "communication.hpp"
42#include "system/System.hpp"
49#ifdef ESPRESSO_CALIPER
50#include <caliper/cali.h>
53#include <boost/mpi/collectives/all_reduce.hpp>
70#include <unordered_set>
78 m_kokkos_handle.reset();
82 m_scatter_force.reset();
83 m_local_force.reset();
84#ifdef ESPRESSO_ROTATION
85 m_scatter_torque.reset();
86 m_local_torque.reset();
88#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
89 m_scatter_dip_fld.reset();
90 m_local_dip_fld.reset();
93 m_scatter_virial.reset();
94 m_local_virial.reset();
96 m_id_to_index.reset();
98 m_verlet_list_cabana.reset();
99 m_bond_state->clear();
100 m_rebuild_verlet_list_cabana =
true;
105 m_kokkos_handle = std::move(
handle);
106 m_bond_state = std::make_unique<LocalBondState>();
127 (4. / 3.) * std::numbers::pi * Utils::int_pow<3>(
pair_cutoff);
140#ifdef ESPRESSO_CALIPER
149#ifdef ESPRESSO_COLLISION_DETECTION
150 if (
system.has_collision_detection_enabled()) {
158 m_scatter_force.emplace(
160#ifdef ESPRESSO_ROTATION
163 m_scatter_torque.emplace(
166#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
169 m_scatter_dip_fld.emplace(
179 m_local_force = std::make_unique<ForceType>(
"local_force",
num_part);
180 m_scatter_force.emplace(
181 Kokkos::Experimental::create_scatter_view(*m_local_force));
182#ifdef ESPRESSO_ROTATION
183 m_local_torque = std::make_unique<ForceType>(
"local_torque",
num_part);
184 m_scatter_torque.emplace(
185 Kokkos::Experimental::create_scatter_view(*m_local_torque));
187#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
188 m_local_dip_fld = std::make_unique<ForceType>(
"local_dip_fld",
num_part);
189 m_scatter_dip_fld.emplace(
190 Kokkos::Experimental::create_scatter_view(*m_local_dip_fld));
192 m_id_to_index = std::make_unique<Kokkos::View<int *, memory_space>>(
198 m_aosoa = std::make_unique<AoSoA_pack>();
202 m_verlet_list_cabana =
206 m_local_virial = std::make_unique<VirialType>(
"local_virial");
207 m_scatter_virial.emplace(
208 Kokkos::Experimental::create_scatter_view(*m_local_virial));
213#ifdef ESPRESSO_CALIPER
217 m_scatter_force->reset();
218#ifdef ESPRESSO_ROTATION
220 m_scatter_torque->reset();
222#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
224 m_scatter_dip_fld->reset();
230 m_scatter_force->reset();
231#ifdef ESPRESSO_ROTATION
233 m_scatter_torque->reset();
235#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
237 m_scatter_dip_fld->reset();
241 m_scatter_virial->reset();
249 auto &pair_list = m_bond_state->pair_list;
250 auto &pair_ids = m_bond_state->pair_ids;
251 auto &angle_list = m_bond_state->angle_list;
252 auto &angle_ids = m_bond_state->angle_ids;
253 auto &dihedral_list = m_bond_state->dihedral_list;
254 auto &dihedral_ids = m_bond_state->dihedral_ids;
256 auto const partner_ids =
bond.partner_ids();
260 auto p_index = Kokkos::atomic_fetch_add(&pair_count, 1);
265 auto a_index = Kokkos::atomic_fetch_add(&angle_count, 1);
271 auto d_index = Kokkos::atomic_fetch_add(&dihedral_count, 1);
285#ifdef ESPRESSO_CALIPER
288 auto &unique_particles = m_unique_particles;
289 unique_particles.clear();
296 m_bond_state->reset_counts();
304 unique_particles[index] = &p;
308 if (
not bond.partner_ids().empty()) {
309 auto const partner_ids =
bond.partner_ids();
310 if (partner_ids.size() == 1u) {
312 }
else if (partner_ids.size() == 2u) {
314 }
else if (partner_ids.size() == 3u) {
327 m_bond_state->allocate();
342 unique_particles.emplace_back(&p);
346 m_cached_max_local_particle_id =
max_id;
347 m_num_local_particles_cached = unique_particles.size();
357 auto const id = p.id();
360 throw std::runtime_error(
"Particle id out of bounds.");
364 throw std::runtime_error(
"Invalid local particle index entry.");
374 throw std::runtime_error(
"local_particles part has corrupted id.");
380 throw std::runtime_error(
388 for (
auto const &p : cell->particles()) {
389 if (particle_to_cell(p) != cell) {
390 throw std::runtime_error(
"misplaced particle with id " +
391 std::to_string(p.id()));
399 for (
auto it = bl.begin();
it != bl.end();) {
409 auto &
parts = cell->particles();
411 if (
it->id() == id) {
424 auto const sort_cell = particle_to_cell(p);
426 return std::addressof(
427 append_indexed_particle(
sort_cell->particles(), std::move(p)));
434 auto const sort_cell = particle_to_cell(p);
443 return std::addressof(
444 append_indexed_particle(cell->particles(), std::move(p)));
448 auto it = std::ranges::find_if(std::ranges::views::reverse(m_particle_index),
449 [](
auto const *p) {
return p !=
nullptr; });
451 return (
it != m_particle_index.rend()) ? (*it)->id() : -1;
455 return m_bond_state->pair_count;
458 return m_bond_state->angle_count;
461 return m_bond_state->dihedral_count;
467#ifdef ESPRESSO_COLLISION_DETECTION
478 cell->particles().clear();
481 m_particle_index.clear();
488 using namespace Cells;
518#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
524#ifdef ESPRESSO_BOND_CONSTRAINT
548 std::vector<ParticleChange>
diff;
552 for (
auto d :
diff) {
553 std::visit(UpdateParticleIndexVisitor{
this}, d);
557 m_rebuild_verlet_list =
true;
558 m_rebuild_verlet_list_cabana =
true;
559 m_le_pos_offset_at_last_resort =
lebc.pos_offset;
561#ifdef ESPRESSO_ADDITIONAL_CHECKS
569 auto &local_geo = *
system.local_geo;
570 auto const &box_geo = *
system.box_geo;
571 set_particle_decomposition(
572 std::make_unique<AtomDecomposition>(
::comm_cart, box_geo));
574 local_geo.set_cell_structure_type(m_type);
575 system.on_cell_structure_change();
579 double range, std::optional<std::pair<int, int>> fully_connected_boundary) {
581 auto &local_geo = *
system.local_geo;
582 auto const &box_geo = *
system.box_geo;
583 set_particle_decomposition(std::make_unique<RegularDecomposition>(
586 local_geo.set_cell_structure_type(m_type);
587 system.on_cell_structure_change();
593 auto &local_geo = *
system.local_geo;
594 auto const &box_geo = *
system.box_geo;
595 set_particle_decomposition(std::make_unique<HybridDecomposition>(
597 [&
system]() {
return system.get_global_ghost_flags(); }, box_geo,
600 local_geo.set_cell_structure_type(m_type);
601 system.on_cell_structure_change();
606 m_verlet_skin = value;
607 m_verlet_skin_set =
true;
608 m_rebuild_verlet_list_cabana =
true;
614 auto const max_cut =
get_system().maximal_cutoff();
616 throw std::runtime_error(
617 "cannot automatically determine skin, please set it manually");
632 ::comm_cart, m_resort_particles, std::bit_or<unsigned>());
663 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
Kokkos::DefaultHostExecutionSpace execution_space
void resort_particles(bool global_flag)
Resort particles.
void check_particle_index() const
Check that particle index is commensurate with particles.
void ghosts_reduce_dipole_field()
Add dipole fields from ghost particles to real 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.
auto & get_local_dip_fld()
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.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
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_DIPFLD
transfer dipole field tracking data
@ 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
@ GHOSTTRANS_BONDS
transfer bonds data
ESPRESSO_ATTR_ALWAYS_INLINE void kokkos_deep_copy(auto const &exec_space, auto const &view, auto const &value)
Wrapper for Kokkos::deep_copy that skips fork/join when the number of threads is 1.
@ DATA_PART_PROPERTIES
Particle::p.
@ DATA_PART_BONDS
Particle::bonds.
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.
Exception indicating that a particle id could not be resolved.
Struct holding all information for one particle.
constexpr auto const & bonds() const
constexpr auto const & id() const
Apply a ParticleChange to a particle index.
void operator()(RemovedParticle rp) const
void operator()(ModifiedList mp) const