ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bond_breakage.hpp
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#pragma once
21
22#include <config/config.hpp>
23
24#include "system/System.hpp"
25
26#include <boost/serialization/access.hpp>
27
28#include <array>
29#include <memory>
30#include <mutex>
31#include <optional>
32#include <unordered_map>
33#include <vector>
34
35namespace BondBreakage {
36
37/** Stores one or two bond partners for pair/angle bonds */
38using BondPartners = std::array<std::optional<int>, 2>;
39
40enum class ActionType {
41 NONE = 0,
42 DELETE_BOND = 1,
44};
45
50
51// Broken bond record
52struct QueueEntry {
56
57 // Serialization for synchronization across mpi ranks
59 template <typename Archive>
60 void serialize(Archive &ar, unsigned int const /* version */) {
61 ar & particle_id;
62 ar & bond_partners;
63 ar & bond_type;
64 }
65};
66
67/** @brief Record bonds broken during a time step. */
68using Queue = std::vector<QueueEntry>;
69
71 Queue m_queue;
72
73public:
74 /** @brief Bond breakage specifications. */
75 std::unordered_map<int, std::shared_ptr<BreakageSpec>> breakage_specs;
76
77 BondBreakage() : m_queue{}, breakage_specs{} {}
78
79 /** @brief Check if the bond between the particles should break, if yes, queue
80 * it.
81 */
82 bool check_and_handle_breakage(int particle_id,
83 BondPartners const &bond_partners,
84 int bond_type, double distance) {
85 if (not breakage_specs.contains(bond_type)) {
86 return false; // No breakage rule for this bond type
87 }
88
89 // Retrieve relevant breakage spec
90 auto const &spec = *(breakage_specs.at(bond_type));
91
92 // Is the bond length longer than the breakage length?
93 if (distance >= spec.breakage_length) {
94 queue_breakage(particle_id, bond_partners, bond_type);
95 return true;
96 }
97 return false;
98 }
99
100 void clear_queue() { m_queue.clear(); }
101
102 void execute_bond_breakage(System::System &system);
103
105 if (not breakage_specs.empty()) {
106 process_queue_impl(system);
107 }
108 }
109
110private:
111 std::mutex queue_mtx;
112
113 void process_queue_impl(System::System &system);
114
115 /** Add a particle+bond combination to the breakage queue */
116 void queue_breakage(int particle_id, BondPartners const &bond_partners,
117 int bond_type);
118};
119
120} // namespace BondBreakage
std::unordered_map< int, std::shared_ptr< BreakageSpec > > breakage_specs
Bond breakage specifications.
void process_queue(System::System &system)
bool check_and_handle_breakage(int particle_id, BondPartners const &bond_partners, int bond_type, double distance)
Check if the bond between the particles should break, if yes, queue it.
Main system class.
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.
void serialize(Archive &ar, unsigned int const)
friend class boost::serialization::access