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
22
24#include "communication.hpp"
25#include "errorhandling.hpp"
26#include "system/System.hpp"
27
30
31#include <boost/mpi.hpp>
32#include <boost/serialization/access.hpp>
33
34#include <algorithm>
35#include <cassert>
36#include <memory>
37#include <span>
38#include <unordered_set>
39#include <utility>
40#include <variant>
41#include <vector>
42
43namespace BondBreakage {
44
45// Variant holding any of the actions
46using Action = std::variant<DeleteBond, DeleteAngleBond, DeleteAllBonds>;
47
48// Set of actions
49using ActionSet = std::unordered_set<Action>;
50
51/** Add a particle+bond combination to the breakage queue */
52void BondBreakage::queue_breakage(int particle_id,
53 BondPartners const &bond_partners,
54 int bond_type) {
55 m_queue.emplace_back(QueueEntry{particle_id, bond_partners, bond_type});
56}
57
58/** @brief Gathers combined queue from all mpi ranks */
61 if (comm_cart.size() > 1) {
63 boost::mpi::broadcast(comm_cart, res, 0);
64 }
65 return res;
66}
67
68/** @brief Constructs the actions to take for a breakage queue entry */
69static ActionSet actions_for_breakage(CellStructure const &cell_structure,
70 QueueEntry const &e,
71 BreakageSpec const &spec) {
72 auto is_angle_bond = [](auto const &bond_partners) {
73 return bond_partners[1];
74 }; // optional for second partner engaged
75
76 // Handle different action types
77 if (spec.action_type == ActionType::DELETE_BOND) {
78 if (is_angle_bond(e.bond_partners)) {
80 {{*(e.bond_partners[0]), *(e.bond_partners[1])}},
81 e.bond_type}};
82 }
83 return {DeleteBond{e.particle_id, *(e.bond_partners[0]), e.bond_type}};
84 }
85#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
86 // revert bind at point of collision for pair bonds
88 not is_angle_bond(e.bond_partners)) {
89 // We need to find the base particles for the two virtual sites
90 // between which the bond broke.
91 auto p1 = cell_structure.get_local_particle(e.particle_id);
92 auto p2 = cell_structure.get_local_particle(*(e.bond_partners[0]));
93 if (p1 and p2) {
94 if (not p1->is_virtual() or not p2->is_virtual()) {
95 runtimeErrorMsg() << "The REVERT_BIND_AT_POINT_OF_COLLISION bond "
96 "breakage action has to be configured for the "
97 "bond on the virtual site. Encountered a particle "
98 "that is not virtual.";
99 return {};
100 }
101
102 return {
103 // Bond between virtual sites
104 DeleteBond{e.particle_id, *(e.bond_partners[0]), e.bond_type},
105 // Bond between base particles. We do not know, on which of these
106 // the bond is defined, since bonds are stored only on one partner
107 DeleteAllBonds{p1->vs_relative().to_particle_id,
108 p2->vs_relative().to_particle_id},
109 DeleteAllBonds{p2->vs_relative().to_particle_id,
110 p1->vs_relative().to_particle_id},
111 };
112 }
113 } else {
114 // revert bind at point of collision for angle bonds
115 auto vs = cell_structure.get_local_particle(e.particle_id);
116 auto p1 = cell_structure.get_local_particle(*(e.bond_partners[0]));
117 auto p2 = cell_structure.get_local_particle(*(e.bond_partners[1]));
118 if (p1 and p2) {
119 if (not vs->is_virtual()) {
120 runtimeErrorMsg() << "The REVERT_BIND_AT_POINT_OF_COLLISION bond "
121 "breakage action has to be configured for the "
122 "bond on the virtual site. Encountered a particle "
123 "that is not virtual.";
124 return {};
125 }
126
127 return {// Angle bond on the virtual site
128 DeleteAngleBond{e.particle_id, {p1->id(), p2->id()}, e.bond_type},
129 // Bond between base particles. We do not know, on which of these
130 // the bond is defined, since bonds are stored only on one partner
131 DeleteAllBonds{p1->id(), p2->id()},
132 DeleteAllBonds{p2->id(), p1->id()}};
133 }
134 }
135#endif // ESPRESSO_VIRTUAL_SITES_RELATIVE
136 return {};
137}
138
139/**
140 * @brief Delete specific bond.
141 */
142static void remove_bond(Particle &p, BondView const &view) {
143 auto &bond_list = p.bonds();
144 auto it = std::find(bond_list.begin(), bond_list.end(), view);
145 if (it != bond_list.end()) {
146 bond_list.erase(it);
147 }
148}
149
150/**
151 * @brief Delete pair bonds to a specific partner
152 */
154 std::vector<std::pair<int, int>> to_delete;
155 for (auto b : p.bonds()) {
156 if (b.partner_ids().size() == 1 and b.partner_ids()[0] == other_pid)
157 to_delete.emplace_back(b.bond_id(), other_pid);
158 }
159 for (auto const &b : to_delete) {
160 remove_bond(p, BondView(b.first, {&b.second, 1}));
161 }
162}
163
164// Handler for the different delete events
165class execute {
166 CellStructure &cell_structure;
167
168public:
169 explicit execute(CellStructure &cell_structure)
170 : cell_structure{cell_structure} {}
171
172 void operator()(DeleteBond const &d) const {
173 if (auto p = cell_structure.get_local_particle(d.particle_id)) {
174 remove_bond(*p, BondView(d.bond_type, {&d.bond_partner_id, 1}));
175 }
176 }
177 void operator()(DeleteAngleBond const &d) const {
178 if (auto p = cell_structure.get_local_particle(d.particle_id)) {
179 remove_bond(*p, BondView(d.bond_type, {&d.bond_partner_id[0], 2}));
180 }
181 }
182 void operator()(DeleteAllBonds const &d) const {
183 if (auto p = cell_structure.get_local_particle(d.particle_id_1)) {
185 }
186 }
187};
188
189void BondBreakage::process_queue_impl(System::System &system) {
190 auto global_queue = gather_global_queue(m_queue);
191 auto &cell_structure = *system.cell_structure;
192
193 // Construct delete actions from breakage queue
194 ActionSet actions = {};
195 for (auto const &e : global_queue) {
196 // Retrieve relevant breakage spec
197 assert(breakage_specs.contains(e.bond_type));
198 auto const &spec = breakage_specs.at(e.bond_type);
199 actions.merge(actions_for_breakage(cell_structure, e, *spec));
200 }
201
202 // Execute actions
203 for (auto const &a : actions) {
204 std::visit(execute(cell_structure), a);
205 system.on_particle_change();
206 }
207}
208
209static bool bond_handler(BondBreakage &bond_breakage, Particle &p,
210 std::span<Particle *> partners, int bond_id,
211 BoxGeometry const &box_geo) {
212 auto retval = false;
213 if (partners.size() == 1u) { // pair bonds
214 auto d = box_geo.get_mi_vector(p.pos(), partners[0]->pos()).norm();
215 retval = bond_breakage.check_and_handle_breakage(
216 p.id(), {{partners[0]->id(), std::nullopt}}, bond_id, d);
217 } else if (partners.size() == 2u) { // angle bond
218 auto d =
219 box_geo.get_mi_vector(partners[0]->pos(), partners[1]->pos()).norm();
220 retval = bond_breakage.check_and_handle_breakage(
221 p.id(), {{partners[0]->id(), partners[1]->id()}}, bond_id, d);
222 }
223 return retval;
224}
225
226void BondBreakage::execute_bond_breakage(System::System &system) {
227 system.cell_structure->update_ghosts_and_resort_particle(
228 system.get_global_ghost_flags());
229
230 // Clear the bond breakage queue
231 clear_queue();
232
233 // Create the bond kernel function (the bond handler)
234 auto bond_kernel = [&](Particle &p, int bond_id,
235 std::span<Particle *> partners) {
236 bond_handler(*this, p, partners, bond_id, *system.box_geo);
237 return false;
238 };
239
240 // Use the CellStructure::bond_loop to process bonds
241 system.cell_structure->bond_loop(bond_kernel);
242
243 // Process the bond breakage queue
244 process_queue(system);
245}
246
247} // 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.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
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