ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
energy_inline.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24/** \file
25 * Energy calculation.
26 */
27
28#include "config/config.hpp"
29
50
51#include "BoxGeometry.hpp"
52#include "Observable_stat.hpp"
53#include "Particle.hpp"
54#include "bond_error.hpp"
55#include "errorhandling.hpp"
56#include "exclusions.hpp"
57
58#include <utils/Vector.hpp>
59
60#include <optional>
61#include <span>
62#include <string>
63#include <variant>
64
65inline double calc_central_radial_energy(IA_parameters const &ia_params,
66 double const dist) {
67
68 [[maybe_unused]] auto const mask = ia_params.active_pair_mask;
69 double ret = 0.;
70
71#ifdef ESPRESSO_LENNARD_JONES
73 ret += lj_pair_energy(ia_params, dist);
74#endif
75
76#ifdef ESPRESSO_WCA
78 ret += wca_pair_energy(ia_params, dist);
79#endif
80
81#ifdef ESPRESSO_LENNARD_JONES_GENERIC
83 ret += ljgen_pair_energy(ia_params, dist);
84#endif
85
86#ifdef ESPRESSO_SMOOTH_STEP
88 ret += SmSt_pair_energy(ia_params, dist);
89#endif
90
91#ifdef ESPRESSO_HERTZIAN
93 ret += hertzian_pair_energy(ia_params, dist);
94#endif
95
96#ifdef ESPRESSO_GAUSSIAN
98 ret += gaussian_pair_energy(ia_params, dist);
99#endif
100
101#ifdef ESPRESSO_BMHTF_NACL
103 ret += BMHTF_pair_energy(ia_params, dist);
104#endif
105
106#ifdef ESPRESSO_MORSE
108 ret += morse_pair_energy(ia_params, dist);
109#endif
110
111#ifdef ESPRESSO_BUCKINGHAM
113 ret += buck_pair_energy(ia_params, dist);
114#endif
115
116#ifdef ESPRESSO_SOFT_SPHERE
118 ret += soft_pair_energy(ia_params, dist);
119#endif
120
121#ifdef ESPRESSO_HAT
123 ret += hat_pair_energy(ia_params, dist);
124#endif
125
126#ifdef ESPRESSO_LJCOS2
128 ret += ljcos2_pair_energy(ia_params, dist);
129#endif
130
131#ifdef ESPRESSO_TABULATED
133 ret += tabulated_pair_energy(ia_params, dist);
134#endif
135
136#ifdef ESPRESSO_LJCOS
138 ret += ljcos_pair_energy(ia_params, dist);
139#endif
140
141 return ret;
142}
143
144/** Calculate non-bonded energies between a pair of particles.
145 * @param p1 particle 1.
146 * @param p2 particle 2.
147 * @param ia_params the interaction parameters between the two particles
148 * @param d vector between p1 and p2.
149 * @param dist distance between p1 and p2.
150 * @param bonded_ias bonded interaction kernels.
151 * @param coulomb Electrostatics solver.
152 * @param coulomb_kernel Coulomb energy kernel.
153 * @return the short-range interaction energy between the two particles
154 */
156 Particle const &p1, Particle const &p2, IA_parameters const &ia_params,
157 Utils::Vector3d const &d, double const dist,
158 [[maybe_unused]] BondedInteractionsMap const &bonded_ias,
159 [[maybe_unused]] Coulomb::Solver const &coulomb,
161 *coulomb_kernel) {
162
163 double ret = 0.;
164
165 ret += calc_central_radial_energy(ia_params, dist);
166
167#ifdef ESPRESSO_THOLE
168 /* Thole damping */
169 ret += thole_pair_energy(p1, p2, ia_params, d, dist, bonded_ias, coulomb,
170 coulomb_kernel);
171#endif
172
173#ifdef ESPRESSO_GAY_BERNE
174 /* Gay-Berne */
175 ret += gb_pair_energy(p1.quat(), p2.quat(), ia_params, d, dist);
176#endif
177
178 return ret;
179}
180
181inline std::optional<double> calc_pair_bonded_energy(
182 Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &dx,
183 Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, double q1q2,
185
186 if (auto const *iap = std::get_if<FeneBond>(&iaparams)) {
187 return iap->energy(dx);
188 }
189 if (auto const *iap = std::get_if<HarmonicBond>(&iaparams)) {
190 return iap->energy(dx);
191 }
192 if (auto const *iap = std::get_if<QuarticBond>(&iaparams)) {
193 return iap->energy(dx);
194 }
195#ifdef ESPRESSO_ELECTROSTATICS
196 if (auto const *iap = std::get_if<BondedCoulomb>(&iaparams)) {
197 return iap->energy(q1q2, dx);
198 }
199 if (auto const *iap = std::get_if<BondedCoulombSR>(&iaparams)) {
200 return iap->energy(pos1, pos2, dx, *kernel);
201 }
202#endif
203#ifdef ESPRESSO_BOND_CONSTRAINT
204 if (std::get_if<RigidBond>(&iaparams)) {
205 return {0.};
206 }
207#endif
208#ifdef ESPRESSO_TABULATED
209 if (auto const *iap = std::get_if<TabulatedDistanceBond>(&iaparams)) {
210 return iap->energy(dx);
211 }
212#endif
213 if (std::get_if<VirtualBond>(&iaparams)) {
214 return {0.};
215 }
216 throw BondUnknownTypeError();
217}
218
219inline std::optional<double>
221 Utils::Vector3d const &vec1,
222 Utils::Vector3d const &vec2) {
223 if (auto const *iap = std::get_if<AngleHarmonicBond>(&iaparams)) {
224 return iap->energy(vec1, vec2);
225 }
226 if (auto const *iap = std::get_if<AngleCosineBond>(&iaparams)) {
227 return iap->energy(vec1, vec2);
228 }
229 if (auto const *iap = std::get_if<AngleCossquareBond>(&iaparams)) {
230 return iap->energy(vec1, vec2);
231 }
232 if (auto const *iap = std::get_if<TabulatedAngleBond>(&iaparams)) {
233 return iap->energy(vec1, vec2);
234 }
235 if (std::get_if<IBMTriel>(&iaparams)) {
236 runtimeWarningMsg() << "Unsupported bond type " +
237 std::to_string(iaparams.index()) +
238 " in energy calculation.";
239 return 0.;
240 }
241 throw BondUnknownTypeError();
242}
243
244inline std::optional<double> calc_dihedral_bonded_energy(
245 Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &v12,
246 Utils::Vector3d const &v23, Utils::Vector3d const &v34) {
247 if (auto const *iap = std::get_if<DihedralBond>(&iaparams)) {
248 return iap->energy(v12, v23, v34);
249 }
250 if (auto const *iap = std::get_if<TabulatedDihedralBond>(&iaparams)) {
251 return iap->energy(v12, v23, v34);
252 }
253 if (std::get_if<IBMTribend>(&iaparams)) {
254 runtimeWarningMsg() << "Unsupported bond type " +
255 std::to_string(iaparams.index()) +
256 " in energy calculation.";
257 return 0.;
258 }
259 throw BondUnknownTypeError();
260}
261
262inline std::optional<double>
264 std::span<Particle *> partners, BoxGeometry const &box_geo,
266 auto const n_partners = static_cast<int>(partners.size());
267
268 auto p2 = (n_partners > 0) ? partners[0] : nullptr;
269 auto p3 = (n_partners > 1) ? partners[1] : nullptr;
270 auto p4 = (n_partners > 2) ? partners[2] : nullptr;
271
272 if (n_partners == 1) {
273 auto const dx = box_geo.get_mi_vector(p1.pos(), p2->pos());
274 return calc_pair_bonded_energy(iaparams, dx, p1.pos(), p2->pos(),
275#ifdef ESPRESSO_ELECTROSTATICS
276 p1.q() * p2->q(), kernel
277#else
278 0.0, nullptr
279#endif
280 );
281 } // 1 partner
282 if (n_partners == 2) {
283 auto const vec1 = box_geo.get_mi_vector(p2->pos(), p1.pos());
284 auto const vec2 = box_geo.get_mi_vector(p3->pos(), p1.pos());
285 return calc_angle_bonded_energy(iaparams, vec1, vec2);
286 } // 2 partners
287 if (n_partners == 3) {
288 // note: particles in a dihedral bond are ordered as p2-p1-p3-p4
289 auto const v12 = box_geo.get_mi_vector(p1.pos(), p2->pos());
290 auto const v23 = box_geo.get_mi_vector(p3->pos(), p1.pos());
291 auto const v34 = box_geo.get_mi_vector(p4->pos(), p3->pos());
292 return calc_dihedral_bonded_energy(iaparams, v12, v23, v34);
293 } // 3 partners
294 if (n_partners == 0) {
295 return 0.;
296 }
297
298 throw BondInvalidSizeError(n_partners);
299}
300
301/** Calculate kinetic energies from translation for one particle.
302 * @param p particle for which to calculate energies
303 */
304inline double translational_kinetic_energy(Particle const &p) {
305 return p.is_virtual() ? 0. : 0.5 * p.mass() * p.v().norm2();
306}
307
308/** Calculate kinetic energies from rotation for one particle.
309 * @param p particle for which to calculate energies
310 */
311inline double rotational_kinetic_energy([[maybe_unused]] Particle const &p) {
312#ifdef ESPRESSO_ROTATION
313 return (p.can_rotate() and not p.is_virtual())
314 ? 0.5 * (hadamard_product(p.omega(), p.omega()) * p.rinertia())
315 : 0.0;
316#else
317 return 0.0;
318#endif
319}
Vector implementation and trait types for boost qvm interoperability.
Routines to calculate the Born-Meyer-Huggins-Tosi-Fumi potential between particle pairs.
double BMHTF_pair_energy(IA_parameters const &ia_params, double dist)
Calculate BMHTF potential energy.
Data structures for bonded interactions.
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.
Routines to calculate the Buckingham potential between particle pairs.
double buck_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Buckingham energy.
container for bonded interactions.
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.
Routines to calculate the Gaussian potential between particle pairs.
double gaussian_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Gaussian energy.
std::optional< double > calc_bonded_energy(Bonded_IA_Parameters const &iaparams, Particle const &p1, std::span< Particle * > partners, BoxGeometry const &box_geo, Coulomb::ShortRangeEnergyKernel::kernel_type const *kernel)
double translational_kinetic_energy(Particle const &p)
Calculate kinetic energies from translation for one particle.
std::optional< double > calc_dihedral_bonded_energy(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &v12, Utils::Vector3d const &v23, Utils::Vector3d const &v34)
double calc_central_radial_energy(IA_parameters const &ia_params, double const dist)
std::optional< double > calc_pair_bonded_energy(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &dx, Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, double q1q2, Coulomb::ShortRangeEnergyKernel::kernel_type const *kernel)
std::optional< double > calc_angle_bonded_energy(Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &vec1, Utils::Vector3d const &vec2)
double rotational_kinetic_energy(Particle const &p)
Calculate kinetic energies from rotation for one particle.
double calc_non_bonded_pair_energy(Particle const &p1, Particle const &p2, IA_parameters const &ia_params, Utils::Vector3d const &d, double const dist, BondedInteractionsMap const &bonded_ias, Coulomb::Solver const &coulomb, Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_kernel)
Calculate non-bonded energies between a pair of particles.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeWarningMsg()
Routines to calculate the Gay-Berne potential between particle pairs.
double gb_pair_energy(Utils::Vector3d const &ui, Utils::Vector3d const &uj, IA_parameters const &ia_params, Utils::Vector3d const &d, double dist)
Calculate Gay-Berne energy.
Routines to calculate the hat potential between particle pairs.
double hat_pair_energy(IA_parameters const &ia_params, double dist)
Calculate hat energy.
Definition hat.hpp:46
Routines to calculate the Hertzian potential between particle pairs.
double hertzian_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Hertzian energy.
Definition hertzian.hpp:49
Routines to calculate the Lennard-Jones potential between particle pairs.
double lj_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Lennard-Jones energy.
Definition lj.hpp:52
Routines to calculate the Lennard-Jones with cosine tail potential between particle pairs.
double ljcos2_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Lennard-Jones cosine squared energy.
Definition ljcos2.hpp:66
Routines to calculate the Lennard-Jones+cosine potential between particle pairs.
double ljcos_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Lennard-Jones cosine energy.
Definition ljcos.hpp:64
Routines to calculate the generalized Lennard-Jones potential between particle pairs.
double ljgen_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Lennard-Jones energy.
Definition ljgen.hpp:81
Routines to calculate the Morse potential between particle pairs.
double morse_pair_energy(IA_parameters const &ia_params, double dist)
Calculate Morse energy.
Definition morse.hpp:53
Various procedures concerning interactions between particles.
constexpr unsigned pair_potential_bit(PairPotential p)
Bitmask for a pair potential.
Routines to calculate the energy and/or force for particle pairs via interpolation of lookup tables.
double tabulated_pair_energy(IA_parameters const &ia_params, double dist)
Calculate a non-bonded pair energy by linear interpolation from a table.
Routines to calculate the smooth step potential between particle pairs.
double SmSt_pair_energy(IA_parameters const &ia_params, double dist)
Calculate smooth step energy.
Routines to calculate the soft-sphere potential between particle pairs.
double soft_pair_energy(IA_parameters const &ia_params, double dist)
Calculate soft-sphere energy.
Exception indicating that a bond with an unexpected number of partners was encountered.
Exception indicating that a bond type was unknown.
Solver::ShortRangeEnergyKernel kernel_type
Parameters for non-bonded interactions.
unsigned active_pair_mask
Bitmask of pair potentials active for this type pair.
Struct holding all information for one particle.
Definition Particle.hpp:435
auto is_virtual() const
Definition Particle.hpp:588
auto const & mass() const
Definition Particle.hpp:492
auto const & quat() const
Definition Particle.hpp:517
auto const & q() const
Definition Particle.hpp:578
auto const & v() const
Definition Particle.hpp:473
auto const & pos() const
Definition Particle.hpp:471
Routines to calculate the Thole damping potential between particle pairs.
double thole_pair_energy(Particle const &p1, Particle const &p2, IA_parameters const &ia_params, Utils::Vector3d const &d, double dist, BondedInteractionsMap const &bonded_ias, Coulomb::Solver const &coulomb, Coulomb::ShortRangeEnergyKernel::kernel_type const *kernel)
Calculate Thole energy.
Definition thole.hpp:68
Routines to calculate the Weeks-Chandler-Andersen potential between particle pairs.
double wca_pair_energy(IA_parameters const &ia_params, double dist)
Calculate WCA energy.
Definition wca.hpp:50