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