ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
pressure_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#include <config/config.hpp>
25
29
30#include "BoxGeometry.hpp"
31#include "Observable_stat.hpp"
32#include "Particle.hpp"
33#include "errorhandling.hpp"
34#include "exclusions.hpp"
35#include "forces_inline.hpp"
36
37#include <utils/Vector.hpp>
39
40#include <cstdio>
41#include <optional>
42#include <span>
43#include <string>
44#include <tuple>
45#include <variant>
46
47/** Calculate non-bonded energies between a pair of particles.
48 * @param p1 pointer to particle 1.
49 * @param p2 pointer to particle 2.
50 * @param d vector between p1 and p2.
51 * @param dist distance between p1 and p2.
52 * @param ia_params non-bonded interaction kernels.
53 * @param bonded_ias bonded interaction kernels.
54 * @param kernel_forces Coulomb force kernel.
55 * @param kernel_pressure Coulomb pressure kernel.
56 * @param[in,out] obs_pressure pressure observable.
57 */
59 Particle const &p1, Particle const &p2, Utils::Vector3d const &d,
60 double dist, IA_parameters const &ia_params,
61 [[maybe_unused]] BondedInteractionsMap const &bonded_ias,
64 Observable_stat &obs_pressure) {
65#ifdef ESPRESSO_EXCLUSIONS
66 if (do_nonbonded(p1, p2))
67#endif
68 {
69 auto f = calc_non_central_force(p1, p2, ia_params, d, dist).f;
70 f += calc_central_radial_force(ia_params, d, dist);
71#ifdef ESPRESSO_THOLE
72 f +=
73 thole_pair_force(p1, p2, ia_params, d, dist, bonded_ias, kernel_forces);
74#endif
75 auto const stress = Utils::tensor_product(d, f);
76 obs_pressure.add_non_bonded_contribution(p1.type(), p2.type(), p1.mol_id(),
77 p2.mol_id(), flatten(stress));
78 }
79
80#ifdef ESPRESSO_ELECTROSTATICS
81 if (!obs_pressure.coulomb.empty() and kernel_pressure != nullptr) {
82 /* real space Coulomb */
83 auto const p_coulomb = (*kernel_pressure)(p1.q() * p2.q(), d, dist);
84
85 for (std::size_t i = 0u; i < 3u; i++) {
86 for (std::size_t j = 0u; j < 3u; j++) {
87 obs_pressure.coulomb[i * 3u + j] += p_coulomb(i, j);
88 }
89 }
90 }
91#endif // ESPRESSO_ELECTROSTATICS
92
93#ifdef ESPRESSO_DIPOLES
94 /* real space magnetic dipole-dipole */
95 if (Dipoles::get_dipoles().impl->solver) {
96 fprintf(stderr, "calculating pressure for magnetostatics which doesn't "
97 "have it implemented\n");
98 }
99#endif // ESPRESSO_DIPOLES
100}
101
102inline std::optional<Utils::Matrix<double, 3, 3>>
104 Bonded_IA_Parameters const &iaparams, Particle const &p1,
105 Particle const &p2, BoxGeometry const &box_geo,
107 auto const dx = box_geo.get_mi_vector(p1.pos(), p2.pos());
108 auto const pair_force = calc_bond_pair_force(iaparams, p1, p2, dx, kernel);
109 std::optional<Utils::Matrix<double, 3, 3>> pressure{std::nullopt};
110 if (pair_force) {
111 pressure = Utils::tensor_product(*pair_force, dx);
112 }
113 return pressure;
114}
115
116inline std::optional<Utils::Matrix<double, 3, 3>>
118 Particle const &p1, Particle const &p2,
119 Particle const &p3,
120 BoxGeometry const &box_geo) {
121 if (std::holds_alternative<AngleHarmonicBond>(iaparams) or
122 std::holds_alternative<AngleCosineBond>(iaparams) or
123#ifdef ESPRESSO_TABULATED
124 std::holds_alternative<TabulatedAngleBond>(iaparams) or
125#endif
126 std::holds_alternative<AngleCossquareBond>(iaparams)) {
127 auto const dx21 = -box_geo.get_mi_vector(p1.pos(), p2.pos());
128 auto const dx31 = box_geo.get_mi_vector(p3.pos(), p1.pos());
129
130 auto const result =
131 calc_bonded_three_body_force(iaparams, box_geo, p1, p2, p3);
132 if (result) {
133 Utils::Vector3d force2, force3;
134 std::tie(std::ignore, force2, force3) = result.value();
135
136 return Utils::tensor_product(force2, dx21) +
137 Utils::tensor_product(force3, dx31);
138 }
139 } else {
140 runtimeWarningMsg() << "Unsupported bond type " +
141 std::to_string(iaparams.index()) +
142 " in pressure calculation.";
144 }
145
146 return {};
147}
148
149inline std::optional<Utils::Matrix<double, 3, 3>> calc_bonded_pressure_tensor(
150 Bonded_IA_Parameters const &iaparams, Particle const &p1,
151 std::span<Particle *> partners, BoxGeometry const &box_geo,
153 switch (number_of_partners(iaparams)) {
154 case 1:
155 return calc_bonded_virial_pressure_tensor(iaparams, p1, *partners[0],
156 box_geo, kernel);
157 case 2:
158 return calc_bonded_three_body_pressure_tensor(iaparams, p1, *partners[0],
159 *partners[1], box_geo);
160 default:
161 runtimeWarningMsg() << "Unsupported bond type " +
162 std::to_string(iaparams.index()) +
163 " in pressure calculation.";
165 }
166}
167
168/** Calculate kinetic pressure (aka energy) for one particle.
169 * @param[in] p1 particle for which to calculate pressure
170 * @param[out] obs_pressure pressure observable
171 */
172inline void add_kinetic_virials(Particle const &p1,
173 Observable_stat &obs_pressure) {
174 if (p1.is_virtual())
175 return;
176
177 /* kinetic pressure */
178 for (std::size_t k = 0u; k < 3u; k++)
179 for (std::size_t l = 0u; l < 3u; l++)
180 obs_pressure.kinetic_lin[k * 3u + l] += p1.v()[k] * p1.v()[l] * p1.mass();
181}
Vector implementation and trait types for boost qvm interoperability.
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.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Get the number of bonded partners for the specified bond.
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 > kinetic_lin
Contribution from linear kinetic energy.
static auto pair_force(Utils::Vector3d const &d, Utils::Vector3d const &m1, Utils::Vector3d const &m2)
Pair force of two interacting dipoles.
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.
Force calculation.
std::optional< Utils::Vector3d > calc_bond_pair_force(Bonded_IA_Parameters const &iaparams, Particle const &p1, Particle const &p2, Utils::Vector3d const &dx, Coulomb::ShortRangeForceKernel::kernel_type const *kernel)
Compute the bonded interaction force between particle pairs.
ParticleForce calc_non_central_force(Particle const &p1, Particle const &p2, IA_parameters const &ia_params, Utils::Vector3d const &d, double const dist)
std::optional< std::tuple< Utils::Vector3d, Utils::Vector3d, Utils::Vector3d > > calc_bonded_three_body_force(Bonded_IA_Parameters const &iaparams, BoxGeometry const &box_geo, Particle const &p1, Particle const &p2, Particle const &p3)
ESPRESSO_ATTR_ALWAYS_INLINE Utils::Vector3d calc_central_radial_force(IA_parameters const &ia_params, Utils::Vector3d const &d, double const dist)
Solver const & get_dipoles()
Definition dipoles.cpp:49
Matrix< T, N, M > tensor_product(const Vector< T, N > &x, const Vector< T, M > &y)
Various procedures concerning interactions between particles.
void add_non_bonded_pair_virials(Particle const &p1, Particle const &p2, Utils::Vector3d const &d, double dist, IA_parameters const &ia_params, BondedInteractionsMap const &bonded_ias, Coulomb::ShortRangeForceKernel::kernel_type const *kernel_forces, Coulomb::ShortRangePressureKernel::kernel_type const *kernel_pressure, Observable_stat &obs_pressure)
Calculate non-bonded energies between a pair of particles.
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_pressure_tensor(Bonded_IA_Parameters const &iaparams, Particle const &p1, std::span< Particle * > partners, BoxGeometry const &box_geo, Coulomb::ShortRangeForceKernel::kernel_type const *kernel)
void add_kinetic_virials(Particle const &p1, Observable_stat &obs_pressure)
Calculate kinetic pressure (aka energy) for one particle.
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_three_body_pressure_tensor(Bonded_IA_Parameters const &iaparams, Particle const &p1, Particle const &p2, Particle const &p3, BoxGeometry const &box_geo)
std::optional< Utils::Matrix< double, 3, 3 > > calc_bonded_virial_pressure_tensor(Bonded_IA_Parameters const &iaparams, Particle const &p1, Particle const &p2, BoxGeometry const &box_geo, Coulomb::ShortRangeForceKernel::kernel_type const *kernel)
Solver::ShortRangeForceKernel kernel_type
Solver::ShortRangePressureKernel kernel_type
Parameters for non-bonded interactions.
Utils::Vector3d f
force.
Definition Particle.hpp:371
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 & 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
Matrix representation with static size.
Definition matrix.hpp:65
Utils::Vector3d thole_pair_force(Particle const &p1, Particle const &p2, IA_parameters const &ia_params, Utils::Vector3d const &d, double dist, BondedInteractionsMap const &bonded_ias, Coulomb::ShortRangeForceKernel::kernel_type const *kernel)
Calculate Thole force.
Definition thole.hpp:44