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