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 double ret = 0.;
69
70#ifdef ESPRESSO_LENNARD_JONES
71 /* Lennard-Jones */
72 ret += lj_pair_energy(ia_params, dist);
73#endif
74
75#ifdef ESPRESSO_WCA
76 /* WCA */
77 ret += wca_pair_energy(ia_params, dist);
78#endif
79
80#ifdef ESPRESSO_LENNARD_JONES_GENERIC
81 /* Generic Lennard-Jones */
82 ret += ljgen_pair_energy(ia_params, dist);
83#endif
84
85#ifdef ESPRESSO_SMOOTH_STEP
86 /* smooth step */
87 ret += SmSt_pair_energy(ia_params, dist);
88#endif
89
90#ifdef ESPRESSO_HERTZIAN
91 /* Hertzian potential */
92 ret += hertzian_pair_energy(ia_params, dist);
93#endif
94
95#ifdef ESPRESSO_GAUSSIAN
96 /* Gaussian potential */
97 ret += gaussian_pair_energy(ia_params, dist);
98#endif
99
100#ifdef ESPRESSO_BMHTF_NACL
101 /* BMHTF NaCl */
102 ret += BMHTF_pair_energy(ia_params, dist);
103#endif
104
105#ifdef ESPRESSO_MORSE
106 /* Morse */
107 ret += morse_pair_energy(ia_params, dist);
108#endif
109
110#ifdef ESPRESSO_BUCKINGHAM
111 /* Buckingham */
112 ret += buck_pair_energy(ia_params, dist);
113#endif
114
115#ifdef ESPRESSO_SOFT_SPHERE
116 /* soft-sphere */
117 ret += soft_pair_energy(ia_params, dist);
118#endif
119
120#ifdef ESPRESSO_HAT
121 /* hat */
122 ret += hat_pair_energy(ia_params, dist);
123#endif
124
125#ifdef ESPRESSO_LJCOS2
126 /* Lennard-Jones */
127 ret += ljcos2_pair_energy(ia_params, dist);
128#endif
129
130#ifdef ESPRESSO_TABULATED
131 /* tabulated */
132 ret += tabulated_pair_energy(ia_params, dist);
133#endif
134
135#ifdef ESPRESSO_LJCOS
136 /* Lennard-Jones cosine */
137 ret += ljcos_pair_energy(ia_params, dist);
138#endif
139
140 return ret;
141}
142
143/** Calculate non-bonded energies between a pair of particles.
144 * @param p1 particle 1.
145 * @param p2 particle 2.
146 * @param ia_params the interaction parameters between the two particles
147 * @param d vector between p1 and p2.
148 * @param dist distance between p1 and p2.
149 * @param bonded_ias bonded interaction kernels.
150 * @param coulomb Electrostatics solver.
151 * @param coulomb_kernel Coulomb energy kernel.
152 * @return the short-range interaction energy between the two particles
153 */
155 Particle const &p1, Particle const &p2, IA_parameters const &ia_params,
156 Utils::Vector3d const &d, double const dist,
157 [[maybe_unused]] BondedInteractionsMap const &bonded_ias,
158 [[maybe_unused]] Coulomb::Solver const &coulomb,
160 *coulomb_kernel) {
161
162 double ret = 0.;
163
164 ret += calc_central_radial_energy(ia_params, dist);
165
166#ifdef ESPRESSO_THOLE
167 /* Thole damping */
168 ret += thole_pair_energy(p1, p2, ia_params, d, dist, bonded_ias, coulomb,
169 coulomb_kernel);
170#endif
171
172#ifdef ESPRESSO_GAY_BERNE
173 /* Gay-Berne */
174 ret += gb_pair_energy(p1.quat(), p2.quat(), ia_params, d, dist);
175#endif
176
177 return ret;
178}
179
180inline std::optional<double> calc_pair_bonded_energy(
181 Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &dx,
182 Utils::Vector3d const &pos1, Utils::Vector3d const &pos2, double q1q2,
184
185 if (auto const *iap = std::get_if<FeneBond>(&iaparams)) {
186 return iap->energy(dx);
187 }
188 if (auto const *iap = std::get_if<HarmonicBond>(&iaparams)) {
189 return iap->energy(dx);
190 }
191 if (auto const *iap = std::get_if<QuarticBond>(&iaparams)) {
192 return iap->energy(dx);
193 }
194#ifdef ESPRESSO_ELECTROSTATICS
195 if (auto const *iap = std::get_if<BondedCoulomb>(&iaparams)) {
196 return iap->energy(q1q2, dx);
197 }
198 if (auto const *iap = std::get_if<BondedCoulombSR>(&iaparams)) {
199 return iap->energy(pos1, pos2, dx, *kernel);
200 }
201#endif
202#ifdef ESPRESSO_BOND_CONSTRAINT
203 if (std::get_if<RigidBond>(&iaparams)) {
204 return {0.};
205 }
206#endif
207#ifdef ESPRESSO_TABULATED
208 if (auto const *iap = std::get_if<TabulatedDistanceBond>(&iaparams)) {
209 return iap->energy(dx);
210 }
211#endif
212 if (std::get_if<VirtualBond>(&iaparams)) {
213 return {0.};
214 }
215 throw BondUnknownTypeError();
216}
217
218inline std::optional<double>
220 Utils::Vector3d const &vec1,
221 Utils::Vector3d const &vec2) {
222 if (auto const *iap = std::get_if<AngleHarmonicBond>(&iaparams)) {
223 return iap->energy(vec1, vec2);
224 }
225 if (auto const *iap = std::get_if<AngleCosineBond>(&iaparams)) {
226 return iap->energy(vec1, vec2);
227 }
228 if (auto const *iap = std::get_if<AngleCossquareBond>(&iaparams)) {
229 return iap->energy(vec1, vec2);
230 }
231 if (auto const *iap = std::get_if<TabulatedAngleBond>(&iaparams)) {
232 return iap->energy(vec1, vec2);
233 }
234 if (std::get_if<IBMTriel>(&iaparams)) {
235 runtimeWarningMsg() << "Unsupported bond type " +
236 std::to_string(iaparams.index()) +
237 " in energy calculation.";
238 return 0.;
239 }
240 throw BondUnknownTypeError();
241}
242
243inline std::optional<double> calc_dihedral_bonded_energy(
244 Bonded_IA_Parameters const &iaparams, Utils::Vector3d const &v12,
245 Utils::Vector3d const &v23, Utils::Vector3d const &v34) {
246 if (auto const *iap = std::get_if<DihedralBond>(&iaparams)) {
247 return iap->energy(v12, v23, v34);
248 }
249 if (auto const *iap = std::get_if<TabulatedDihedralBond>(&iaparams)) {
250 return iap->energy(v12, v23, v34);
251 }
252 if (std::get_if<IBMTribend>(&iaparams)) {
253 runtimeWarningMsg() << "Unsupported bond type " +
254 std::to_string(iaparams.index()) +
255 " in energy calculation.";
256 return 0.;
257 }
258 throw BondUnknownTypeError();
259}
260
261inline std::optional<double>
263 std::span<Particle *> partners, BoxGeometry const &box_geo,
265 auto const n_partners = static_cast<int>(partners.size());
266
267 auto p2 = (n_partners > 0) ? partners[0] : nullptr;
268 auto p3 = (n_partners > 1) ? partners[1] : nullptr;
269 auto p4 = (n_partners > 2) ? partners[2] : nullptr;
270
271 if (n_partners == 1) {
272 auto const dx = box_geo.get_mi_vector(p1.pos(), p2->pos());
273 return calc_pair_bonded_energy(iaparams, dx, p1.pos(), p2->pos(),
274#ifdef ESPRESSO_ELECTROSTATICS
275 p1.q() * p2->q(), kernel
276#else
277 0.0, nullptr
278#endif
279 );
280 } // 1 partner
281 if (n_partners == 2) {
282 auto const vec1 = box_geo.get_mi_vector(p2->pos(), p1.pos());
283 auto const vec2 = box_geo.get_mi_vector(p3->pos(), p1.pos());
284 return calc_angle_bonded_energy(iaparams, vec1, vec2);
285 } // 2 partners
286 if (n_partners == 3) {
287 // note: particles in a dihedral bond are ordered as p2-p1-p3-p4
288 auto const v12 = box_geo.get_mi_vector(p1.pos(), p2->pos());
289 auto const v23 = box_geo.get_mi_vector(p3->pos(), p1.pos());
290 auto const v34 = box_geo.get_mi_vector(p4->pos(), p3->pos());
291 return calc_dihedral_bonded_energy(iaparams, v12, v23, v34);
292 } // 3 partners
293 if (n_partners == 0) {
294 return 0.;
295 }
296
297 throw BondInvalidSizeError(n_partners);
298}
299
300/** Calculate kinetic energies from translation for one particle.
301 * @param p particle for which to calculate energies
302 */
303inline double translational_kinetic_energy(Particle const &p) {
304 return p.is_virtual() ? 0. : 0.5 * p.mass() * p.v().norm2();
305}
306
307/** Calculate kinetic energies from rotation for one particle.
308 * @param p particle for which to calculate energies
309 */
310inline double rotational_kinetic_energy([[maybe_unused]] Particle const &p) {
311#ifdef ESPRESSO_ROTATION
312 return (p.can_rotate() and not p.is_virtual())
313 ? 0.5 * (hadamard_product(p.omega(), p.omega()) * p.rinertia())
314 : 0.0;
315#else
316 return 0.0;
317#endif
318}
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.
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.
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