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