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 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 <cassert>
61#include <functional>
62#include <optional>
63#include <ranges>
64#include <unordered_map>
65#include <unordered_set>
66#include <vector>
67
68/**
69 * @brief Center of mass information for a molecule.
70 *
71 * Hold the total mass and the mass-weighted position sum for a molecule,
72 * used in center of mass calculations and MPI communication.
73 */
74struct ComInfo {
75 /** @brief Sum of the masses of all constituent particles. */
76 double total_mass = 0.;
77 /** @brief Sum of (mass * position) for all constituent particles. */
79
81 template <class Archive>
82 void serialize(Archive &ar, unsigned int const version) {
83 ar & total_mass;
85 }
86};
87
90
91static bool is_vs_com(Particle const &p) {
93}
94
95std::optional<int> get_pid_for_vs_com(CellStructure &cell_structure,
96 int mol_id) {
97 auto constexpr parallel_execution_policy = false;
98 std::vector<int> pids;
99 cell_structure.for_each_local_particle(
100 [&](Particle const &p) {
101 if (is_vs_com(p) and p.mol_id() == mol_id) {
102 pids.emplace_back(p.id());
103 }
104 },
105 parallel_execution_policy);
107 std::optional<int> result = std::nullopt;
108 if (not pids.empty()) {
109 assert(pids.size() == 1ul);
110 result = pids.front();
111 }
112 return result;
113}
114
115/**
116 * @brief Synchronize a map across all MPI ranks.
117 *
118 * Gathers all key-value pairs from all ranks, broadcasts the full set to every
119 * rank, and reconstructs the map so all processes have the same data.
120 */
121template <typename K, typename V>
122void gather_buffer_map(std::unordered_map<K, V> &map_data) {
123 std::vector<std::pair<K, V>> flat_map{map_data.begin(), map_data.end()};
125 boost::mpi::broadcast(comm_cart, flat_map, 0);
126 map_data = {flat_map.begin(), flat_map.end()};
127}
128
130 BoxGeometry const &box_geo) {
131
132 auto constexpr parallel_execution_policy = false;
133 // Update ghost positions before computing centers of mass
135
136 // Store virtual site center of mass particles
137 // (mol_id: vs_com_id)
138 std::unordered_map<ParticleId, MoleculeId> virtual_site_id_for_mol_id;
139 // Store com information for each molecule id
140 // (mol_id: com_info)
141 std::unordered_map<MoleculeId, ComInfo> m_com_by_mol_id;
142
143 cell_structure.for_each_local_particle(
144 [&](Particle const &p) {
145 if (is_vs_com(p)) {
146 assert(not virtual_site_id_for_mol_id.contains(p.mol_id()));
147 virtual_site_id_for_mol_id[p.mol_id()] = p.id();
148 } else if (not p.is_virtual()) {
149 if (not m_com_by_mol_id.contains(p.mol_id())) {
150 m_com_by_mol_id[p.mol_id()] = ComInfo{};
151 }
152 auto const pos_unfolded =
153 box_geo.unfolded_position(p.pos(), p.image_box());
154 m_com_by_mol_id[p.mol_id()].total_mass += p.mass();
155 m_com_by_mol_id[p.mol_id()].weighted_position +=
156 p.mass() * pos_unfolded;
157 }
158 },
159 parallel_execution_policy);
160
161 // Reduction of m_com_by_mol_id across all processes
162 // get a list of all molids that need to be communicated
163 std::unordered_set<MoleculeId> local_mol_ids;
164 for (auto const &mol_id : m_com_by_mol_id | std::views::keys) {
165 local_mol_ids.insert(mol_id);
166 }
167
168 // Communicate the list of molids to all processes
169 std::vector<std::unordered_set<MoleculeId>> global_mol_ids{};
170 boost::mpi::all_gather(comm_cart, local_mol_ids, global_mol_ids);
171 std::unordered_set<MoleculeId> unique_mol_ids{};
172 for (auto const &mol_id_set : global_mol_ids) {
173 for (auto const &mol_id : mol_id_set) {
174 unique_mol_ids.insert(mol_id);
175 }
176 }
177 std::vector<MoleculeId> flattened_mol_ids{unique_mol_ids.begin(),
178 unique_mol_ids.end()};
179 std::ranges::sort(flattened_mol_ids);
180
181 // MPI-Allreduce for total mass and weighted position
182 for (auto const mol_id : flattened_mol_ids) {
183 double local_total_mass = 0.;
184 Utils::Vector3d local_weighted_position = {0., 0., 0.};
185
186 if (m_com_by_mol_id.contains(mol_id)) {
187 local_total_mass = m_com_by_mol_id[mol_id].total_mass;
188 local_weighted_position = m_com_by_mol_id[mol_id].weighted_position;
189 }
190
191 auto const total_mass =
192 boost::mpi::all_reduce(comm_cart, local_total_mass, std::plus{});
193 auto const weighted_position =
194 boost::mpi::all_reduce(comm_cart, local_weighted_position, std::plus{});
195
196 if (m_com_by_mol_id.contains(mol_id)) {
197 m_com_by_mol_id[mol_id].total_mass = total_mass;
198 m_com_by_mol_id[mol_id].weighted_position = weighted_position;
199 }
200 }
201
202 // communicate m_com_by_mol_id across all processes
203 gather_buffer_map(m_com_by_mol_id);
204
205 // communicate the virtual_site_id_for_mol_id across all processes
206 gather_buffer_map(virtual_site_id_for_mol_id);
207
208 // Iterate over all the molecules and set the position and mass of the
209 // associated virtual site com particle
210 for (auto const &[mol_id, com_info] : m_com_by_mol_id) {
211 if (not virtual_site_id_for_mol_id.contains(mol_id)) {
212 continue;
213 }
214 auto const vs_id = virtual_site_id_for_mol_id[mol_id];
215 if (auto const vs_ptr = cell_structure.get_local_particle(vs_id)) {
216 auto folded_pos = com_info.weighted_position / com_info.total_mass;
217 auto image_box = Utils::Vector3i{};
218
219 box_geo.fold_position(folded_pos, image_box);
220
221 vs_ptr->image_box() = image_box;
222 vs_ptr->mass() = com_info.total_mass;
223 vs_ptr->pos() = folded_pos;
224 }
225 }
226}
227
228// Distribute forces that have accumulated on virtual particles to the
229// associated real particles
231
232 auto constexpr parallel_execution_policy = false;
233 cell_structure.ghosts_reduce_forces();
234 cell_structure.ghosts_reset_forces();
235
236 // Store forces for virtual site com particles
237 // (vs_com_id: force)
238 std::unordered_map<ParticleId, Utils::Vector3d> force_for_vs_id;
239 // (vs_com_id: mass)
240 std::unordered_map<ParticleId, double> mass_for_vs_id;
241
242 // Store virtual site center of mass particles
243 // (mol_id: vs_com_id)
244 std::unordered_map<MoleculeId, ParticleId> virtual_site_id_for_mol_id;
245 cell_structure.for_each_local_particle(
246 [&](Particle const &p) {
247 if (is_vs_com(p)) { // get vs_com particle
248 assert(not virtual_site_id_for_mol_id.contains(p.mol_id()));
249 virtual_site_id_for_mol_id[p.mol_id()] = p.id();
250 force_for_vs_id[p.id()] = p.force();
251 mass_for_vs_id[p.id()] = p.mass();
252 }
253 },
254 parallel_execution_policy);
255
256 // communicate virtual_site_id_for_mol_id across all processes
257 gather_buffer_map(virtual_site_id_for_mol_id);
258 // communicate force_for_vs_id, namely the force acting on the virtual site
259 // com particles to all processes
260 gather_buffer_map(force_for_vs_id);
261 // communicate mass_for_vs_id, namely the mass of the virtual site com
262 // particles to all processes
263 gather_buffer_map(mass_for_vs_id);
264
265 // Iterate over all the particles in the local cells
266 cell_structure.for_each_local_particle(
267 [&](Particle &p) {
268 if (is_vs_com(p) or
269 not virtual_site_id_for_mol_id.contains(p.mol_id())) {
270 return;
271 }
272 auto const vs_id = virtual_site_id_for_mol_id.at(p.mol_id());
273 p.force() +=
274 (p.mass() / mass_for_vs_id.at(vs_id)) * force_for_vs_id.at(vs_id);
275 },
276 parallel_execution_policy);
277}
278
279#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:91
void vs_com_back_transfer_forces_and_torques(CellStructure &cell_structure)
Definition com.cpp:230
std::optional< int > get_pid_for_vs_com(CellStructure &cell_structure, int mol_id)
Definition com.cpp:95
decltype(ParticleProperties::mol_id) MoleculeId
Definition com.cpp:89
void gather_buffer_map(std::unordered_map< K, V > &map_data)
Synchronize a map across all MPI ranks.
Definition com.cpp:122
void vs_com_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition com.cpp:129
decltype(ParticleProperties::identity) ParticleId
Definition com.cpp:88
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:74
void serialize(Archive &ar, unsigned int const version)
Definition com.cpp:82
double total_mass
Sum of the masses of all constituent particles.
Definition com.cpp:76
friend class boost::serialization::access
Definition com.cpp:80
Utils::Vector3d weighted_position
Sum of (mass * position) for all constituent particles.
Definition com.cpp:78
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