ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
com.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * @file
22 * This file contains routine to handle virtual sites at the center of mass of
23 * a bunch of other particles (say, a molecule). Forces acting on the center of
24 * mass are distributed back onto the constituent particles.
25 * The position/velocity/mass of the virtual site at center of mass
26 * is calculated from the positions/velocities/masses of many particles.
27 *
28 * Virtual sites are like particles, but they will not be integrated.
29 * Step performed for virtual sites:
30 * - update virtual sites
31 * - calculate forces
32 * - distribute forces
33 * - move non-virtual particles
34 * - update virtual sites
35 */
36
37#include <config/config.hpp>
38
39#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
40
41#include "BoxGeometry.hpp"
42#include "Particle.hpp"
43#include "PropagationMode.hpp"
45#include "cells.hpp"
46#include "communication.hpp"
47
48#include <utils/Vector.hpp>
50
51#include <boost/archive/text_iarchive.hpp>
52#include <boost/archive/text_oarchive.hpp>
53#include <boost/mpi/collectives/all_gather.hpp>
54#include <boost/mpi/collectives/all_reduce.hpp>
55#include <boost/mpi/collectives/broadcast.hpp>
56#include <boost/serialization/unordered_set.hpp>
57#include <boost/serialization/utility.hpp>
58#include <boost/serialization/vector.hpp>
59
60#include <algorithm>
61#include <cassert>
62#include <functional>
63#include <optional>
64#include <ranges>
65#include <unordered_map>
66#include <unordered_set>
67#include <vector>
68
69/**
70 * @brief Center of mass information for a molecule.
71 *
72 * Hold the total mass and the mass-weighted position sum for a molecule,
73 * used in center of mass calculations and MPI communication.
74 */
75struct ComInfo {
76 /** @brief Sum of the masses of all constituent particles. */
77 double total_mass = 0.;
78 /** @brief Sum of (mass * position) for all constituent particles. */
80
82 template <class Archive>
83 void serialize(Archive &ar, unsigned int const version) {
84 ar & total_mass;
86 }
87};
88
91
92static bool is_vs_com(Particle const &p) {
94}
95
96std::optional<int> get_pid_for_vs_com(CellStructure &cell_structure,
97 int mol_id) {
98 auto constexpr parallel_execution_policy = false;
99 std::vector<int> pids;
100 cell_structure.for_each_local_particle(
101 [&](Particle const &p) {
102 if (is_vs_com(p) and p.mol_id() == mol_id) {
103 pids.emplace_back(p.id());
104 }
105 },
108 std::optional<int> result = std::nullopt;
109 if (not pids.empty()) {
110 assert(pids.size() == 1ul);
111 result = pids.front();
112 }
113 return result;
114}
115
116/**
117 * @brief Synchronize a map across all MPI ranks.
118 *
119 * Gathers all key-value pairs from all ranks, broadcasts the full set to every
120 * rank, and reconstructs the map so all processes have the same data.
121 */
122template <typename K, typename V>
123void gather_buffer_map(std::unordered_map<K, V> &map_data) {
124 std::vector<std::pair<K, V>> flat_map{map_data.begin(), map_data.end()};
126 boost::mpi::broadcast(comm_cart, flat_map, 0);
127 map_data = {flat_map.begin(), flat_map.end()};
128}
129
131 BoxGeometry const &box_geo) {
132
133 auto constexpr parallel_execution_policy = false;
134 // Update ghost positions before computing centers of mass
136
137 // Store virtual site center of mass particles
138 // (mol_id: vs_com_id)
139 std::unordered_map<ParticleId, MoleculeId> virtual_site_id_for_mol_id;
140 // Store com information for each molecule id
141 // (mol_id: com_info)
142 std::unordered_map<MoleculeId, ComInfo> m_com_by_mol_id;
143
144 cell_structure.for_each_local_particle(
145 [&](Particle const &p) {
146 if (is_vs_com(p)) {
149 } else if (not p.is_virtual()) {
150 if (not m_com_by_mol_id.contains(p.mol_id())) {
152 }
153 auto const pos_unfolded =
154 box_geo.unfolded_position(p.pos(), p.image_box());
155 m_com_by_mol_id[p.mol_id()].total_mass += p.mass();
156 m_com_by_mol_id[p.mol_id()].weighted_position +=
157 p.mass() * pos_unfolded;
158 }
159 },
161
162 // Reduction of m_com_by_mol_id across all processes
163 // get a list of all molids that need to be communicated
164 std::unordered_set<MoleculeId> local_mol_ids;
165 for (auto const &mol_id : m_com_by_mol_id | std::views::keys) {
166 local_mol_ids.insert(mol_id);
167 }
168
169 // Communicate the list of molids to all processes
170 std::vector<std::unordered_set<MoleculeId>> global_mol_ids{};
171 boost::mpi::all_gather(comm_cart, local_mol_ids, global_mol_ids);
172 std::unordered_set<MoleculeId> unique_mol_ids{};
173 for (auto const &mol_id_set : global_mol_ids) {
174 for (auto const &mol_id : mol_id_set) {
175 unique_mol_ids.insert(mol_id);
176 }
177 }
178 std::vector<MoleculeId> flattened_mol_ids{unique_mol_ids.begin(),
179 unique_mol_ids.end()};
180 std::ranges::sort(flattened_mol_ids);
181
182 // MPI-Allreduce for total mass and weighted position
183 for (auto const mol_id : flattened_mol_ids) {
184 double local_total_mass = 0.;
186
187 if (m_com_by_mol_id.contains(mol_id)) {
188 local_total_mass = m_com_by_mol_id[mol_id].total_mass;
189 local_weighted_position = m_com_by_mol_id[mol_id].weighted_position;
190 }
191
192 auto const total_mass =
193 boost::mpi::all_reduce(comm_cart, local_total_mass, std::plus{});
194 auto const weighted_position =
195 boost::mpi::all_reduce(comm_cart, local_weighted_position, std::plus{});
196
197 if (m_com_by_mol_id.contains(mol_id)) {
198 m_com_by_mol_id[mol_id].total_mass = total_mass;
199 m_com_by_mol_id[mol_id].weighted_position = weighted_position;
200 }
201 }
202
203 // communicate m_com_by_mol_id across all processes
205
206 // communicate the virtual_site_id_for_mol_id across all processes
208
209 // Iterate over all the molecules and set the position and mass of the
210 // associated virtual site com particle
211 for (auto const &[mol_id, com_info] : m_com_by_mol_id) {
212 if (not virtual_site_id_for_mol_id.contains(mol_id)) {
213 continue;
214 }
215 auto const vs_id = virtual_site_id_for_mol_id[mol_id];
216 if (auto const vs_ptr = cell_structure.get_local_particle(vs_id)) {
217 auto folded_pos = com_info.weighted_position / com_info.total_mass;
218 auto image_box = Utils::Vector3i{};
219
220 box_geo.fold_position(folded_pos, image_box);
221
222 vs_ptr->image_box() = image_box;
223 vs_ptr->mass() = com_info.total_mass;
224 vs_ptr->pos() = folded_pos;
225 }
226 }
227}
228
229// Distribute forces that have accumulated on virtual particles to the
230// associated real particles
232
233 auto constexpr parallel_execution_policy = false;
234 cell_structure.ghosts_reduce_forces();
235 cell_structure.ghosts_reset_forces();
236
237 // Store forces for virtual site com particles
238 // (vs_com_id: force)
239 std::unordered_map<ParticleId, Utils::Vector3d> force_for_vs_id;
240 // (vs_com_id: mass)
241 std::unordered_map<ParticleId, double> mass_for_vs_id;
242
243 // Store virtual site center of mass particles
244 // (mol_id: vs_com_id)
245 std::unordered_map<MoleculeId, ParticleId> virtual_site_id_for_mol_id;
246 cell_structure.for_each_local_particle(
247 [&](Particle const &p) {
248 if (is_vs_com(p)) { // get vs_com particle
251 force_for_vs_id[p.id()] = p.force();
252 mass_for_vs_id[p.id()] = p.mass();
253 }
254 },
256
257 // communicate virtual_site_id_for_mol_id across all processes
259 // communicate force_for_vs_id, namely the force acting on the virtual site
260 // com particles to all processes
262 // communicate mass_for_vs_id, namely the mass of the virtual site com
263 // particles to all processes
265
266 // Iterate over all the particles in the local cells
267 cell_structure.for_each_local_particle(
268 [&](Particle &p) {
269 if (is_vs_com(p) or
270 not virtual_site_id_for_mol_id.contains(p.mol_id())) {
271 return;
272 }
273 auto const vs_id = virtual_site_id_for_mol_id.at(p.mol_id());
274 p.force() +=
276 },
278}
279
280#endif // ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
Vector implementation and trait types for boost qvm interoperability.
This file contains everything related to the global cell structure / cell system.
auto unfolded_position(Utils::Vector3d const &pos, Utils::Vector3i const &image_box) const
Unfold particle coordinates to image box.
void fold_position(Utils::Vector3d &pos, Utils::Vector3i &image_box) const
Fold coordinates to primary simulation box in-place.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
void ghosts_update(unsigned data_parts)
Update ghost particles.
void ghosts_reset_forces()
Set forces and torques on all ghosts to zero.
void ghosts_reduce_forces()
Add forces and torques from ghost particles to real particles.
void for_each_local_particle(ParticleUnaryOp &&f, bool parallel=true) const
Run a kernel on all local particles.
static bool is_vs_com(Particle const &p)
Definition com.cpp:92
void vs_com_back_transfer_forces_and_torques(CellStructure &cell_structure)
Definition com.cpp:231
std::optional< int > get_pid_for_vs_com(CellStructure &cell_structure, int mol_id)
Definition com.cpp:96
decltype(ParticleProperties::mol_id) MoleculeId
Definition com.cpp:90
void gather_buffer_map(std::unordered_map< K, V > &map_data)
Synchronize a map across all MPI ranks.
Definition com.cpp:123
void vs_com_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition com.cpp:130
decltype(ParticleProperties::identity) ParticleId
Definition com.cpp:89
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
@ DATA_PART_POSITION
Particle::r.
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
Center of mass information for a molecule.
Definition com.cpp:75
void serialize(Archive &ar, unsigned int const version)
Definition com.cpp:83
double total_mass
Sum of the masses of all constituent particles.
Definition com.cpp:77
friend class boost::serialization::access
Definition com.cpp:81
Utils::Vector3d weighted_position
Sum of (mass * position) for all constituent particles.
Definition com.cpp:79
int identity
unique identifier for the particle.
Definition Particle.hpp:109
int mol_id
Molecule identifier.
Definition Particle.hpp:111
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & propagation() const
Definition Particle.hpp:461
auto is_virtual() const
Definition Particle.hpp:588
auto const & mass() const
Definition Particle.hpp:492
auto const & image_box() const
Definition Particle.hpp:484
auto const & mol_id() const
Definition Particle.hpp:456
auto const & pos() const
Definition Particle.hpp:471
auto const & force() const
Definition Particle.hpp:475
auto const & id() const
Definition Particle.hpp:454