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