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 <algorithm>
52#include <cassert>
53#include <cmath>
54#include <memory>
55#include <optional>
56#include <unordered_map>
57#include <variant>
58
59/** Interaction type for unused bonded interaction slots */
60struct NoneBond {
61 static constexpr int num = 0;
62 double cutoff() const { return bonded_inactive_cutoff; }
63};
64
65/** Interaction type for virtual bonds */
67 static constexpr int num = 1;
68 double cutoff() const { return bonded_inactive_cutoff; }
69};
70
71/** Variant in which to store the parameters of an individual bonded
72 * interaction
73 */
81
82/**
83 * @brief container for bonded interactions.
84 */
85class BondedInteractionsMap : public System::Leaf<BondedInteractionsMap> {
86 using container_type =
87 std::unordered_map<int, std::shared_ptr<Bonded_IA_Parameters>>;
88
89public:
90 using key_type = container_type::key_type;
91 using mapped_type = container_type::mapped_type;
92 using value_type = container_type::value_type;
93 using iterator = container_type::iterator;
94 using const_iterator = container_type::const_iterator;
95
97 virtual ~BondedInteractionsMap() = default;
98
99 iterator begin() { return m_params.begin(); }
100 iterator end() { return m_params.end(); }
101 const_iterator begin() const { return m_params.begin(); }
102 const_iterator end() const { return m_params.end(); }
103
104 void insert(key_type const &key, mapped_type const &ptr) {
105 if (m_params.contains(key)) {
106 deactivate_bond(m_params.at(key));
107 }
108 activate_bond(ptr);
109 next_key = std::max(next_key, key + 1);
110 m_params[key] = ptr;
111 on_ia_change();
112 }
114 activate_bond(ptr);
115 auto const key = next_key++;
116 m_params[key] = ptr;
117 on_ia_change();
118 return key;
119 }
120 auto erase(key_type const &key) {
121 if (m_params.contains(key)) {
122 deactivate_bond(m_params.at(key));
123 }
124 auto &&obj = m_params.erase(key);
125 on_ia_change();
126 return obj;
127 }
128 virtual void activate_bond(mapped_type const &ptr);
129 virtual void deactivate_bond(mapped_type const &ptr);
130 mapped_type const &at(key_type const &key) const { return m_params.at(key); }
131 bool contains(key_type const &key) const { return m_params.contains(key); }
132 bool empty() const { return m_params.empty(); }
133 auto size() const { return m_params.size(); }
134 auto get_next_key() const { return next_key; }
135 auto get_zero_based_type(int bond_id) const {
136 return contains(bond_id) ? static_cast<int>(at(bond_id)->index()) : 0;
137 }
139 assert(n_thermalized_bonds >= 0);
140 return n_thermalized_bonds;
141 }
142#ifdef ESPRESSO_BOND_CONSTRAINT
143 auto get_n_rigid_bonds() const {
144 assert(n_rigid_bonds >= 0);
145 return n_rigid_bonds;
146 }
147#endif
148 std::optional<key_type> find_bond_id(mapped_type const &target_bond) const {
149 for (auto const &[bond_id, bond] : m_params) {
150 if (bond == target_bond) {
151 return bond_id;
152 }
153 }
154 return std::nullopt;
155 }
156
157 /**
158 * @brief Calculate the maximal cutoff of bonded interactions, required to
159 * determine the cell size for communication.
160 *
161 * Bond angle and dihedral potentials do not contain a cutoff intrinsically.
162 * The cutoff for these potentials depends on the bond length potentials
163 * (it is assumed that particles participating in a bond angle or dihedral
164 * potential are bound to each other by some bond length potential). For bond
165 * angle potentials nothing has to be done. For dihedral potentials the cutoff
166 * is set to twice the maximal cutoff because the particle in which the bond
167 * is stored is only bonded to the first two partners, one of which has an
168 * additional bond to the third partner.
169 */
170 double maximal_cutoff() const;
171
172 /**
173 * @brief Checks both particles for a specific bond, even on ghost particles.
174 *
175 * @param p particle to check for the bond
176 * @param p_partner possible bond partner
177 * @tparam BondType Bond type to check for. Must be of one of the types in
178 * @ref Bonded_IA_Parameters.
179 */
180 template <typename BondType>
181 bool pair_bond_exists_on(Particle const &p, Particle const &p_partner) const {
182 auto const &bonds = p.bonds();
183 return std::any_of(
184 bonds.begin(), bonds.end(),
185 [this, partner_id = p_partner.id()](BondView const &bond) {
186 auto const &bond_ptr = at(bond.bond_id());
187 return std::holds_alternative<BondType>(*bond_ptr.get()) and
188 (bond.partner_ids()[0] == partner_id);
189 });
190 }
191
192 /**
193 * @brief Checks both particles for a specific bond, even on ghost particles.
194 *
195 * @param p1 particle on which the bond may be stored
196 * @param p2 particle on which the bond may be stored
197 * @tparam BondType Bond type to check for.
198 */
199 template <typename BondType>
200 bool pair_bond_exists_between(Particle const &p1, Particle const &p2) const {
201 if (&p1 == &p2)
202 return false;
203
204 // Check if particles have bonds and search for the bond of interest.
205 // Could be saved on either particle, so we need to check both.
208 }
209
210 void on_ia_change();
211
212private:
213 container_type m_params = {};
214 key_type next_key = static_cast<key_type>(0);
215 int n_thermalized_bonds = 0;
216#ifdef ESPRESSO_BOND_CONSTRAINT
217 int n_rigid_bonds = 0;
218#endif
219};
220
221/** @brief Get the number of bonded partners for the specified bond. */
223 return std::visit([]<typename T>(T const &) { return T::num; }, iaparams);
224}
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.
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
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
container_type::mapped_type mapped_type
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
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:435
auto const & bonds() const
Definition Particle.hpp:468
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.