ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bond_breakage.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-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#include <config/config.hpp>
21
24
26#include "communication.hpp"
27#include "errorhandling.hpp"
28#include "system/System.hpp"
29
32
33#include <boost/mpi.hpp>
34#include <boost/serialization/access.hpp>
35
36#include <algorithm>
37#include <cassert>
38#include <memory>
39#include <mutex>
40#include <span>
41#include <unordered_set>
42#include <utility>
43#include <variant>
44#include <vector>
45
46namespace BondBreakage {
47
48// Variant holding any of the actions
49using Action = std::variant<DeleteBond, DeleteAngleBond, DeleteAllBonds>;
50
51// Set of actions
52using ActionSet = std::unordered_set<Action>;
53
54/** Add a particle+bond combination to the breakage queue */
55void BondBreakage::queue_breakage(int particle_id,
56 BondPartners const &bond_partners,
57 int bond_type) {
58 {
59 std::lock_guard<std::mutex> lock(queue_mtx);
60 m_queue.emplace_back(QueueEntry{particle_id, bond_partners, bond_type});
61 }
62}
63
64/** @brief Gathers combined queue from all mpi ranks */
65static auto gather_global_queue(Queue const &local_queue) {
66 Queue res = local_queue;
67 if (comm_cart.size() > 1) {
69 boost::mpi::broadcast(comm_cart, res, 0);
70 }
71 return res;
72}
73
74/** @brief Constructs the actions to take for a breakage queue entry */
75static ActionSet actions_for_breakage(CellStructure const &cell_structure,
76 QueueEntry const &e,
77 BreakageSpec const &spec) {
78 auto is_angle_bond = [](auto const &bond_partners) {
79 return bond_partners[1];
80 }; // optional for second partner engaged
81
82 // Handle different action types
84 if (is_angle_bond(e.bond_partners)) {
86 {{*(e.bond_partners[0]), *(e.bond_partners[1])}},
87 e.bond_type}};
88 }
89 return {DeleteBond{e.particle_id, *(e.bond_partners[0]), e.bond_type}};
90 }
91#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
92 // revert bind at point of collision for pair bonds
94 not is_angle_bond(e.bond_partners)) {
95 // We need to find the base particles for the two virtual sites
96 // between which the bond broke.
97 auto p1 = cell_structure.get_local_particle(e.particle_id);
98 auto p2 = cell_structure.get_local_particle(*(e.bond_partners[0]));
99 if (p1 and p2) {
100 if (not p1->is_virtual() or not p2->is_virtual()) {
101 runtimeErrorMsg() << "The REVERT_BIND_AT_POINT_OF_COLLISION bond "
102 "breakage action has to be configured for the "
103 "bond on the virtual site. Encountered a particle "
104 "that is not virtual.";
105 return {};
106 }
107
108 return {
109 // Bond between virtual sites
111 // Bond between base particles. We do not know, on which of these
112 // the bond is defined, since bonds are stored only on one partner
113 DeleteAllBonds{p1->vs_relative().to_particle_id,
114 p2->vs_relative().to_particle_id},
115 DeleteAllBonds{p2->vs_relative().to_particle_id,
116 p1->vs_relative().to_particle_id},
117 };
118 }
119 } else {
120 // revert bind at point of collision for angle bonds
121 auto vs = cell_structure.get_local_particle(e.particle_id);
122 auto p1 = cell_structure.get_local_particle(*(e.bond_partners[0]));
123 auto p2 = cell_structure.get_local_particle(*(e.bond_partners[1]));
124 if (p1 and p2) {
125 if (not vs->is_virtual()) {
126 runtimeErrorMsg() << "The REVERT_BIND_AT_POINT_OF_COLLISION bond "
127 "breakage action has to be configured for the "
128 "bond on the virtual site. Encountered a particle "
129 "that is not virtual.";
130 return {};
131 }
132
133 return {// Angle bond on the virtual site
134 DeleteAngleBond{e.particle_id, {p1->id(), p2->id()}, e.bond_type},
135 // Bond between base particles. We do not know, on which of these
136 // the bond is defined, since bonds are stored only on one partner
137 DeleteAllBonds{p1->id(), p2->id()},
138 DeleteAllBonds{p2->id(), p1->id()}};
139 }
140 }
141#endif // ESPRESSO_VIRTUAL_SITES_RELATIVE
142 return {};
143}
144
145/**
146 * @brief Delete specific bond.
147 */
148static void remove_bond(Particle &p, BondView const &view) {
149 auto &bond_list = p.bonds();
150 auto it = std::find(bond_list.begin(), bond_list.end(), view);
151 if (it != bond_list.end()) {
152 bond_list.erase(it);
153 }
154}
155
156/**
157 * @brief Delete pair bonds to a specific partner
158 */
159static void remove_pair_bonds_to(Particle &p, int other_pid) {
160 std::vector<std::pair<int, int>> to_delete;
161 for (auto b : p.bonds()) {
162 if (b.partner_ids().size() == 1 and b.partner_ids()[0] == other_pid)
163 to_delete.emplace_back(b.bond_id(), other_pid);
164 }
165 for (auto const &b : to_delete) {
166 remove_bond(p, BondView(b.first, {&b.second, 1}));
167 }
168}
169
170// Handler for the different delete events
171class execute {
172 CellStructure &cell_structure;
173
174public:
175 explicit execute(CellStructure &cell_structure)
176 : cell_structure{cell_structure} {}
177
178 void operator()(DeleteBond const &d) const {
179 if (auto p = cell_structure.get_local_particle(d.particle_id)) {
180 remove_bond(*p, BondView(d.bond_type, {&d.bond_partner_id, 1}));
181 }
182 }
183 void operator()(DeleteAngleBond const &d) const {
184 if (auto p = cell_structure.get_local_particle(d.particle_id)) {
185 remove_bond(*p, BondView(d.bond_type, {&d.bond_partner_id[0], 2}));
186 }
187 }
188 void operator()(DeleteAllBonds const &d) const {
189 if (auto p = cell_structure.get_local_particle(d.particle_id_1)) {
191 }
192 }
193};
194
195void BondBreakage::process_queue_impl(System::System &system) {
196 auto global_queue = gather_global_queue(m_queue);
197 auto &cell_structure = *system.cell_structure;
198
199 // Construct delete actions from breakage queue
200 ActionSet actions = {};
201 for (auto const &e : global_queue) {
202 // Retrieve relevant breakage spec
203 assert(breakage_specs.contains(e.bond_type));
204 auto const &spec = breakage_specs.at(e.bond_type);
205 actions.merge(actions_for_breakage(cell_structure, e, *spec));
206 }
207
208 // Execute actions
209 for (auto const &a : actions) {
210 std::visit(execute(cell_structure), a);
211 system.on_particle_change();
212 }
213}
214
215static bool bond_handler(BondBreakage &bond_breakage, Particle &p,
216 std::span<Particle *> partners, int bond_id,
217 BoxGeometry const &box_geo) {
218 auto retval = false;
219 if (partners.size() == 1u) { // pair bonds
220 auto d = box_geo.get_mi_vector(p.pos(), partners[0]->pos()).norm();
221 retval = bond_breakage.check_and_handle_breakage(
222 p.id(), {{partners[0]->id(), std::nullopt}}, bond_id, d);
223 } else if (partners.size() == 2u) { // angle bond
224 auto d =
225 box_geo.get_mi_vector(partners[0]->pos(), partners[1]->pos()).norm();
226 retval = bond_breakage.check_and_handle_breakage(
227 p.id(), {{partners[0]->id(), partners[1]->id()}}, bond_id, d);
228 }
229 return retval;
230}
231
232void BondBreakage::execute_bond_breakage(System::System &system) {
233 system.cell_structure->update_ghosts_and_resort_particle(
234 system.get_global_ghost_flags());
235
236 // Clear the bond breakage queue
237 clear_queue();
238
239 // Create the bond kernel function (the bond handler)
240 auto bond_kernel = [&](Particle &p, int bond_id,
241 std::span<Particle *> partners) {
242 bond_handler(*this, p, partners, bond_id, *system.box_geo);
243 return false;
244 };
245
246 // Use the CellStructure::bond_loop to process bonds
247 system.cell_structure->bond_loop(bond_kernel);
248
249 // Process the bond breakage queue
250 process_queue(system);
251}
252
253} // namespace BondBreakage
execute(CellStructure &cell_structure)
void operator()(DeleteAngleBond const &d) const
void operator()(DeleteBond const &d) const
void operator()(DeleteAllBonds const &d) const
Immutable view on a bond.
Definition BondList.hpp:44
ESPRESSO_ATTR_ALWAYS_INLINE Utils::Vector3< T > get_mi_vector(Utils::Vector3< T > const &a, Utils::Vector3< T > const &b) const
Get the minimum-image vector between two coordinates.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
Main system class.
unsigned get_global_ghost_flags() const
Returns the ghost flags required for running pair kernels for the global state, e....
void on_particle_change()
Called every time a particle property changes.
std::shared_ptr< CellStructure > cell_structure
std::shared_ptr< BoxGeometry > box_geo
boost::mpi::communicator comm_cart
The communicator.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
static void remove_pair_bonds_to(Particle &p, int other_pid)
Delete pair bonds to a specific partner.
static void remove_bond(Particle &p, BondView const &view)
Delete specific bond.
static ActionSet actions_for_breakage(CellStructure const &cell_structure, QueueEntry const &e, BreakageSpec const &spec)
Constructs the actions to take for a breakage queue entry.
static auto gather_global_queue(Queue const &local_queue)
Gathers combined queue from all mpi ranks.
std::variant< DeleteBond, DeleteAngleBond, DeleteAllBonds > Action
static bool bond_handler(BondBreakage &bond_breakage, Particle &p, std::span< Particle * > partners, int bond_id, BoxGeometry const &box_geo)
std::array< std::optional< int >, 2 > BondPartners
Stores one or two bond partners for pair/angle bonds.
std::vector< QueueEntry > Queue
Record bonds broken during a time step.
std::unordered_set< Action > ActionSet
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & bonds() const
Definition Particle.hpp:468
auto const & pos() const
Definition Particle.hpp:471
auto const & id() const
Definition Particle.hpp:454