ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bonded_interaction_data.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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/** @file
23 * Data structures for bonded interactions.
24 * For more information on how to add new interactions, see @ref bondedIA_new.
25 */
26
27#include <config/config.hpp>
28
29#include "angle_cosine.hpp"
30#include "angle_cossquare.hpp"
31#include "angle_harmonic.hpp"
32#include "bonded_coulomb.hpp"
33#include "bonded_coulomb_sr.hpp"
34#include "bonded_tab.hpp"
35#include "dihedral.hpp"
36#include "fene.hpp"
37#include "harmonic.hpp"
43#include "quartic.hpp"
44#include "rigid_bond.hpp"
45#include "thermalized_bond.hpp"
46
47#include "BondList.hpp"
48#include "Particle.hpp"
49#include "system/Leaf.hpp"
50
51#include <utils/Vector.hpp>
53
54#include <algorithm>
55#include <cassert>
56#include <cmath>
57#include <memory>
58#include <optional>
59#include <unordered_map>
60#include <variant>
61#include <vector>
62
63/** Interaction type for unused bonded interaction slots */
64struct NoneBond {
65 static constexpr int num = 0;
66 double cutoff() const { return bonded_inactive_cutoff; }
67};
68
69/** Interaction type for virtual bonds */
71 static constexpr int num = 1;
72 double cutoff() const { return bonded_inactive_cutoff; }
73};
74
75/** Variant in which to store the parameters of an individual bonded
76 * interaction
77 */
85
86/**
87 * @brief container for bonded interactions.
88 */
89class BondedInteractionsMap : public System::Leaf<BondedInteractionsMap> {
90 using container_type =
91 std::unordered_map<int, std::shared_ptr<Bonded_IA_Parameters>>;
92
93public:
94 using key_type = container_type::key_type;
95 using mapped_type = container_type::mapped_type;
96 using value_type = container_type::value_type;
97 using iterator = container_type::iterator;
98 using const_iterator = container_type::const_iterator;
99
101 virtual ~BondedInteractionsMap() = default;
102
103 iterator begin() { return m_params.begin(); }
104 iterator end() { return m_params.end(); }
105 const_iterator begin() const { return m_params.begin(); }
106 const_iterator end() const { return m_params.end(); }
107
108 void insert(key_type const &key, mapped_type const &ptr) {
109 if (m_params.contains(key)) {
110 deactivate_bond(m_params.at(key));
111 }
112 activate_bond(ptr);
113 next_key = std::max(next_key, key + 1);
114 m_params[key] = ptr;
115 on_ia_change();
116 }
118 activate_bond(ptr);
119 auto const key = next_key++;
120 m_params[key] = ptr;
121 on_ia_change();
122 return key;
123 }
124 auto erase(key_type const &key) {
125 if (m_params.contains(key)) {
126 deactivate_bond(m_params.at(key));
127 }
128 auto &&obj = m_params.erase(key);
129 on_ia_change();
130 return obj;
131 }
132 virtual void activate_bond(mapped_type const &ptr);
133 virtual void deactivate_bond(mapped_type const &ptr);
134 mapped_type const &at(key_type const &key) const { return m_params.at(key); }
135 bool contains(key_type const &key) const { return m_params.contains(key); }
136 bool empty() const { return m_params.empty(); }
137 auto size() const { return m_params.size(); }
139 auto get_next_key() const { return next_key; }
140 auto get_zero_based_type(int bond_id) const {
141 return contains(bond_id) ? static_cast<int>(at(bond_id)->index()) : 0;
142 }
145 assert(n_thermalized_bonds >= 0);
146 return n_thermalized_bonds;
147 }
148#ifdef ESPRESSO_BOND_CONSTRAINT
150 auto get_n_rigid_bonds() const {
151 assert(n_rigid_bonds >= 0);
152 return n_rigid_bonds;
153 }
154 /**
155 * @brief Per-bond-type RATTLE constraint virial.
156 *
157 * Accumulated bond-by-bond inside @ref correct_position_shake() (indexed by
158 * bond id, same convention as @ref Observable_stat::bonded_contribution()),
159 * where the pairwise correction/mass/bond-vector are all unambiguous,
160 * regardless of how many rigid bonds a particle participates in.
161 * Reset at the start of every SHAKE call; consumed by
162 * @ref System::System::calculate_pressure().
163 */
164 std::vector<Utils::Vector9d> rigid_bond_virial;
165#endif // ESPRESSO_BOND_CONSTRAINT
166 std::optional<key_type> find_bond_id(mapped_type const &target_bond) const {
167 for (auto const &[bond_id, bond] : m_params) {
168 if (bond == target_bond) {
169 return bond_id;
170 }
171 }
172 return std::nullopt;
173 }
174
175 /**
176 * @brief Calculate the maximal cutoff of bonded interactions, required to
177 * determine the cell size for communication.
178 *
179 * Bond angle and dihedral potentials do not contain a cutoff intrinsically.
180 * The cutoff for these potentials depends on the bond length potentials
181 * (it is assumed that particles participating in a bond angle or dihedral
182 * potential are bound to each other by some bond length potential). For bond
183 * angle potentials nothing has to be done. For dihedral potentials the cutoff
184 * is set to twice the maximal cutoff because the particle in which the bond
185 * is stored is only bonded to the first two partners, one of which has an
186 * additional bond to the third partner.
187 */
188 double maximal_cutoff() const;
189
190 /**
191 * @brief Checks both particles for a specific bond, even on ghost particles.
192 *
193 * @param p particle to check for the bond
194 * @param p_partner possible bond partner
195 * @tparam BondType Bond type to check for. Must be of one of the types in
196 * @ref Bonded_IA_Parameters.
197 */
198 template <typename BondType>
199 bool pair_bond_exists_on(Particle const &p, Particle const &p_partner) const {
200 auto const &bonds = p.bonds();
201 return std::any_of(
202 bonds.begin(), bonds.end(),
203 [this, partner_id = p_partner.id()](BondView const &bond) {
204 auto const &bond_ptr = at(bond.bond_id());
205 return std::holds_alternative<BondType>(*bond_ptr.get()) and
206 (bond.partner_ids()[0] == partner_id);
207 });
208 }
209
210 /**
211 * @brief Checks both particles for a specific bond, even on ghost particles.
212 *
213 * @param p1 particle on which the bond may be stored
214 * @param p2 particle on which the bond may be stored
215 * @tparam BondType Bond type to check for.
216 */
217 template <typename BondType>
218 bool pair_bond_exists_between(Particle const &p1, Particle const &p2) const {
219 if (&p1 == &p2)
220 return false;
221
222 // Check if particles have bonds and search for the bond of interest.
223 // Could be saved on either particle, so we need to check both.
226 }
227
228 void on_ia_change();
229
230private:
231 container_type m_params = {};
232 key_type next_key = static_cast<key_type>(0);
233 int n_thermalized_bonds = 0;
234#ifdef ESPRESSO_BOND_CONSTRAINT
235 int n_rigid_bonds = 0;
236#endif
237};
238
239/** @brief Get the number of bonded partners for the specified bond. */
241 return std::visit([]<typename T>(T const &) { return T::num; }, iaparams);
242}
Vector implementation and trait types for boost qvm interoperability.
Routines to calculate the angle energy or/and and force for a particle triple using the potential des...
Routines to calculate the angle energy or/and and force for a particle triple using the potential des...
Routines to calculate the angle energy or/and and force for a particle triple using the potential des...
Routines to calculate the bonded Coulomb potential between particle pairs.
Routines to calculate the short-range part of the bonded Coulomb potential between particle pairs.
std::variant< NoneBond, FeneBond, HarmonicBond, QuarticBond, BondedCoulomb, BondedCoulombSR, AngleHarmonicBond, AngleCosineBond, AngleCossquareBond, DihedralBond, TabulatedDistanceBond, TabulatedAngleBond, TabulatedDihedralBond, ThermalizedBond, RigidBond, IBMTriel, IBMVolCons, IBMTribend, OifGlobalForcesBond, OifLocalForcesBond, VirtualBond > Bonded_IA_Parameters
Variant in which to store the parameters of an individual bonded interaction.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Get the number of bonded partners for the specified bond.
Routines to calculate the energy and/or force for particle bonds, angles and dihedrals via interpolat...
Immutable view on a bond.
Definition BondList.hpp:44
container for bonded interactions.
DEVICE_QUALIFIER auto get_n_rigid_bonds() const
virtual void activate_bond(mapped_type const &ptr)
key_type insert(mapped_type const &ptr)
virtual void deactivate_bond(mapped_type const &ptr)
auto get_zero_based_type(int bond_id) const
container_type::value_type value_type
bool contains(key_type const &key) const
mapped_type const & at(key_type const &key) const
virtual ~BondedInteractionsMap()=default
void insert(key_type const &key, mapped_type const &ptr)
container_type::const_iterator const_iterator
container_type::iterator iterator
DEVICE_QUALIFIER auto get_n_thermalized_bonds() const
std::optional< key_type > find_bond_id(mapped_type const &target_bond) const
bool pair_bond_exists_between(Particle const &p1, Particle const &p2) const
Checks both particles for a specific bond, even on ghost particles.
bool pair_bond_exists_on(Particle const &p, Particle const &p_partner) const
Checks both particles for a specific bond, even on ghost particles.
double maximal_cutoff() const
Calculate the maximal cutoff of bonded interactions, required to determine the cell size for communic...
BondedInteractionsMap()=default
container_type::key_type key_type
std::vector< Utils::Vector9d > rigid_bond_virial
Per-bond-type RATTLE constraint virial.
container_type::mapped_type mapped_type
DEVICE_QUALIFIER auto get_next_key() const
auto erase(key_type const &key)
Abstract class that represents a component of the system.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
constexpr double bonded_inactive_cutoff
Special cutoff value for an inactive bond.
Definition config.hpp:59
#define DEVICE_QUALIFIER
Routines to calculate the dihedral energy or/and force for a particle quadruple.
Routines to calculate the FENE potential between particle pairs.
Routines to calculate the harmonic bond potential between particle pairs.
Routines to calculate the OIF local forces for a particle quadruple (two neighboring triangles with c...
Routines to calculate the quartic potential between particle pairs.
Definition of the rigid bond data type for the Rattle algorithm.
Parameters for three-body angular potential (cosine).
Parameters for three-body angular potential (cossquare).
Parameters for three-body angular potential (harmonic).
Parameters for Coulomb bond short-range Potential.
Parameters for Coulomb bond Potential.
Parameters for four-body angular potential (dihedral-angle potentials).
Definition dihedral.hpp:47
Parameters for FENE bond Potential.
Definition fene.hpp:38
Parameters for harmonic bond Potential.
Definition harmonic.hpp:37
Parameters for IBM tribend.
Parameters for IBM elastic triangle (triel)
Definition ibm_triel.hpp:35
Parameters for IBM volume conservation bond.
Interaction type for unused bonded interaction slots.
double cutoff() const
static constexpr int num
Parameters for OIF global forces.
Parameters for OIF local forces.
Struct holding all information for one particle.
Definition Particle.hpp:436
constexpr auto const & bonds() const
Definition Particle.hpp:473
Parameters for quartic bond Potential.
Definition quartic.hpp:36
Parameters for the rigid_bond/SHAKE/RATTLE ALGORITHM.
Parameters for 3-body tabulated potential.
Parameters for 4-body tabulated potential.
Parameters for 2-body tabulated potential.
Parameters for Thermalized bond.
Interaction type for virtual bonds.
static constexpr int num
Routines to thermalize the center of mass and distance of a particle pair.