ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
forces_cabana.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025-2026 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#include <config/config.hpp>
23
24#include "aosoa_pack.hpp"
25#include "forces_inline.hpp"
27
28#include <utils/Vector.hpp>
29
30#include <Cabana_Core.hpp>
31
32#include <omp.h>
33
34#include <cstddef>
35#include <memory>
36#include <optional>
37#include <variant>
38#include <vector>
39
49 std::vector<Particle *> const &unique_particles;
51#ifdef ESPRESSO_ROTATION
53#endif
54#ifdef ESPRESSO_NPT
57#endif
59#ifdef ESPRESSO_P3M
61#endif
63
65 BondedInteractionsMap const &bonded_ias_,
66 InteractionsNonBonded const &nonbonded_ias_,
70 Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_u_kernel_,
71 Coulomb::Solver const &coulomb_,
72 Thermostat::Thermostat const &thermostat_, BoxGeometry const &box_geo_,
73 std::vector<Particle *> const &unique_particles_,
74 CellStructure::ForceType const &local_force_,
75#ifdef ESPRESSO_ROTATION
76 CellStructure::ForceType const &local_torque_,
77#endif
78#ifdef ESPRESSO_NPT
79 Utils::Vector3d *const global_virial_,
80 CellStructure::VirialType const &local_virial_,
81#endif
82 CellStructure::AoSoA_pack const &aosoa_, double system_max_cutoff_)
83 : bonded_ias(bonded_ias_), nonbonded_ias(nonbonded_ias_),
84 coulomb_kernel(coulomb_kernel_), dipoles_kernel(dipoles_kernel_),
85 elc_kernel(elc_kernel_), coulomb_u_kernel(coulomb_u_kernel_),
86 thermostat(thermostat_), box_geo(box_geo_),
87 unique_particles(unique_particles_), local_force(local_force_),
88#ifdef ESPRESSO_ROTATION
89 local_torque(local_torque_),
90#endif
91#ifdef ESPRESSO_NPT
92 global_virial(global_virial_), local_virial(local_virial_),
93#endif
94 aosoa(aosoa_), system_max_cutoff(system_max_cutoff_) {
95#ifdef ESPRESSO_P3M
96 p3m = nullptr;
97 if (auto &solver = coulomb_.impl->solver; solver.has_value()) {
98 if (std::holds_alternative<std::shared_ptr<CoulombP3M>>(*solver)) {
99 p3m = std::get<std::shared_ptr<CoulombP3M>>(*solver).get();
100 }
101 }
102#endif
103 }
104
105// Helper functions to check if NPT algorithms are active
106#ifdef ESPRESSO_NPT
107 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool npt_active() const {
108 return global_virial != nullptr;
109 }
110#endif
111
112 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void
113 operator()(std::size_t i, std::size_t j) const {
114
115 // calc distance
116 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
117 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
118 auto const d = box_geo.get_mi_vector(pos1, pos2);
119 auto const dist = d.norm();
120
121 // Early exit if distance > maximal global cutoff
122 if (dist > system_max_cutoff)
123 return;
124
125 auto const &ia_params =
127
128 ParticleForce pf{};
129
130 // Determine which data needs to be loaded based on active algorithms
131#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES) or \
132 defined(ESPRESSO_EXCLUSIONS) or defined(ESPRESSO_THOLE)
133 auto const flag =
134 compute_pair_data_flags(dist, ia_params, coulomb_kernel != nullptr,
135 dipoles_kernel != nullptr, aosoa, i, j);
136#endif
137
138#if defined(ESPRESSO_EXCLUSIONS) or defined(ESPRESSO_THOLE)
139 Particle const *p1_ptr = nullptr;
140 Particle const *p2_ptr = nullptr;
141 if (flag.need_particle_pointers) {
142 p1_ptr = unique_particles.at(i);
143 p2_ptr = unique_particles.at(j);
144 }
145#endif
146
147 // Load directors only if needed
148#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
149 Utils::Vector3d dir1{}, dir2{};
150 if (flag.need_directors) {
153 }
154#endif
155
156 /***********************************************/
157 /* non-bonded pair potentials */
158 /***********************************************/
159
160 if (dist <= ia_params.max_cut) {
161#ifdef ESPRESSO_EXCLUSIONS
162 bool skip_non_bonded = false;
163 if (aosoa.has_exclusion(i) or aosoa.has_exclusion(j)) {
164 skip_non_bonded = not do_nonbonded(*p1_ptr, *p2_ptr);
165 }
166#else
167 constexpr bool skip_non_bonded = false;
168#endif
169 if (not skip_non_bonded) {
170 pf.f += calc_central_radial_force(ia_params, d, dist);
171
172 // Only call Thole force kernel if active
173#ifdef ESPRESSO_THOLE
174 if (thole_active(ia_params, coulomb_kernel != nullptr)) {
175 pf.f += thole_pair_force(*p1_ptr, *p2_ptr, ia_params, d, dist,
177 }
178#endif
179 // Only call Gay-Berne force kernel if active
180#ifdef ESPRESSO_GAY_BERNE
181 if (gay_berne_active(dist, ia_params)) {
182 pf += gb_pair_force(dir1, dir2, ia_params, d, dist);
183 }
184#endif
185 } // not skip_non_bonded
186 } // not dist > ia_params.max_cut
187
188 /*********************************************************************/
189 /* everything before this contributes to the virial pressure in NpT, */
190 /* but nothing afterwards, since the contribution to pressure from */
191 /* electrostatic is calculated by energy */
192 /*********************************************************************/
193#ifdef ESPRESSO_NPT
194 Utils::Vector3d virial{};
195 if (npt_active()) {
196 virial = hadamard_product(pf.f, d);
197 }
198#endif // ESPRESSO_NPT
199
200 /***********************************************/
201 /* thermostat */
202 /***********************************************/
203
204 /* The inter dpd force should not be part of the virial */
205#ifdef ESPRESSO_DPD
207 auto const dist2 = dist * dist;
208 auto const vel1 = aosoa.get_vector_at(aosoa.velocity, i);
209 auto const vel2 = aosoa.get_vector_at(aosoa.velocity, j);
210 auto const force =
211 dpd_pair_force(pos1, vel1, aosoa.id(i), pos2, vel2, aosoa.id(j),
212 *thermostat.dpd, box_geo, ia_params, d, dist, dist2);
213 pf += force;
214 }
215#endif // ESPRESSO_DPD
216
217#ifdef ESPRESSO_ELECTROSTATICS
218 Utils::Vector3d f1_asym{};
219 Utils::Vector3d f2_asym{};
220 // real-space electrostatic charge-charge interaction
221 if (coulomb_kernel != nullptr) {
222 if ((aosoa.charge(i) != 0.) and (aosoa.charge(j) != 0.)) {
223 auto const q1q2 = aosoa.charge(i) * aosoa.charge(j);
224#ifdef ESPRESSO_P3M
225 if (p3m) [[likely]] {
226 pf.f += p3m->pair_force(q1q2, d, dist);
227 } else
228#endif
229 {
230 pf.f += (*coulomb_kernel)(q1q2, d, dist);
231 }
232 if (elc_kernel) {
233 (*elc_kernel)(pos1, pos2, f1_asym, f2_asym, q1q2);
234 }
235#ifdef ESPRESSO_NPT
236 if (npt_active()) {
237 virial[0] += (*coulomb_u_kernel)(pos1, pos2, q1q2, d, dist);
238 }
239#endif // ESPRESSO_NPT
240 }
241 }
242#endif // ESPRESSO_ELECTROSTATICS
243
244 // Only call dipole force kernel if active
245#ifdef ESPRESSO_DIPOLES
246 if (dipoles_kernel != nullptr) {
247 auto const d1d2 = aosoa.dipm(i) * aosoa.dipm(j);
248 if (d1d2 != 0.) {
249 pf += (*dipoles_kernel)(d1d2, aosoa.dipm(i) * dir1,
250 aosoa.dipm(j) * dir2, d, dist, dist * dist);
251 }
252 }
253#endif // ESPRESSO_DIPOLES
254
255 auto opf = calc_opposing_force(pf, d);
256#ifdef ESPRESSO_ELECTROSTATICS
257 pf.f += f1_asym;
258 opf.f += f2_asym;
259#endif // ESPRESSO_ELECTROSTATICS
260
261 auto const thread_id = omp_get_thread_num();
262
263 local_force(i, thread_id, 0) += pf.f[0];
264 local_force(i, thread_id, 1) += pf.f[1];
265 local_force(i, thread_id, 2) += pf.f[2];
266#ifdef ESPRESSO_ROTATION
267 local_torque(i, thread_id, 0) += pf.torque[0];
268 local_torque(i, thread_id, 1) += pf.torque[1];
269 local_torque(i, thread_id, 2) += pf.torque[2];
270#endif
271
272 local_force(j, thread_id, 0) += opf.f[0];
273 local_force(j, thread_id, 1) += opf.f[1];
274 local_force(j, thread_id, 2) += opf.f[2];
275#ifdef ESPRESSO_ROTATION
276 local_torque(j, thread_id, 0) += opf.torque[0];
277 local_torque(j, thread_id, 1) += opf.torque[1];
278 local_torque(j, thread_id, 2) += opf.torque[2];
279#endif
280#ifdef ESPRESSO_NPT
281 if (npt_active()) {
282 local_virial(thread_id, 0) += virial[0];
283 local_virial(thread_id, 1) += virial[1];
284 local_virial(thread_id, 2) += virial[2];
285 }
286#endif
287 }
288};
@ THERMO_DPD
Vector implementation and trait types for boost qvm interoperability.
#define ESPRESSO_ATTR_ALWAYS_INLINE
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.
auto & get_ia_param(int i, int j)
Get interaction parameters between particle types i and j.
std::shared_ptr< DPDThermostat > dpd
int thermo_switch
Bitmask of currently active thermostats.
Utils::Vector3d dpd_pair_force(Utils::Vector3d const &p1_position, Utils::Vector3d const &p1_velocity, int const &p1_id, Utils::Vector3d const &p2_position, Utils::Vector3d const &p2_velocity, int const &p2_id, DPDThermostat const &dpd, BoxGeometry const &box_geo, IA_parameters const &ia_params, Utils::Vector3d const &d, double dist, double dist2)
Definition dpd.cpp:76
bool do_nonbonded(Particle const &p1, Particle const &p2)
Determine if the non-bonded interactions between p1 and p2 should be calculated.
Force calculation.
ParticleForce calc_opposing_force(ParticleForce const &pf, Utils::Vector3d const &d)
ESPRESSO_ATTR_ALWAYS_INLINE Utils::Vector3d calc_central_radial_force(IA_parameters const &ia_params, Utils::Vector3d const &d, double const dist)
ParticleForce gb_pair_force(Utils::Vector3d const &ui, Utils::Vector3d const &uj, IA_parameters const &ia_params, Utils::Vector3d const &d, double dist)
Calculate Gay-Berne force and torques.
Definition gay_berne.hpp:49
KOKKOS_INLINE_FUNCTION PairDataFlags compute_pair_data_flags(double dist, IA_parameters const &ia_params, bool has_coulomb, bool has_dipoles, auto const &aosoa, std::size_t i, std::size_t j)
KOKKOS_INLINE_FUNCTION bool thole_active(IA_parameters const &ia_params, bool has_coulomb_kernel)
KOKKOS_INLINE_FUNCTION bool gay_berne_active(double dist, IA_parameters const &ia_params)
bool has_exclusion(std::size_t i) const
PositionViewType position
Utils::Vector< T, N > get_vector_at(Kokkos::View< T *[N], array_layout, Kokkos::HostSpace > const &view, std::size_t i) const
DirectorViewType director
VelocityViewType velocity
P3M solver.
Definition p3m.hpp:55
Utils::Vector3d pair_force(double q1q2, Utils::Vector3d const &d, double dist) const
Calculate real-space contribution of p3m Coulomb pair forces.
Definition p3m.hpp:145
Solver::ShortRangeEnergyKernel kernel_type
Solver::ShortRangeForceCorrectionsKernel kernel_type
Solver::ShortRangeForceKernel kernel_type
std::unique_ptr< Implementation > impl
Pointer-to-implementation.
Solver::ShortRangeForceKernel kernel_type
std::vector< Particle * > const & unique_particles
Coulomb::ShortRangeEnergyKernel::kernel_type const *const coulomb_u_kernel
Coulomb::ShortRangeForceCorrectionsKernel::kernel_type const * elc_kernel
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool npt_active() const
BoxGeometry const & box_geo
CellStructure::VirialType const & local_virial
ForcesKernel(BondedInteractionsMap const &bonded_ias_, InteractionsNonBonded const &nonbonded_ias_, Coulomb::ShortRangeForceKernel::kernel_type const *coulomb_kernel_, Dipoles::ShortRangeForceKernel::kernel_type const *dipoles_kernel_, Coulomb::ShortRangeForceCorrectionsKernel::kernel_type const *elc_kernel_, Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_u_kernel_, Coulomb::Solver const &coulomb_, Thermostat::Thermostat const &thermostat_, BoxGeometry const &box_geo_, std::vector< Particle * > const &unique_particles_, CellStructure::ForceType const &local_force_, CellStructure::ForceType const &local_torque_, Utils::Vector3d *const global_virial_, CellStructure::VirialType const &local_virial_, CellStructure::AoSoA_pack const &aosoa_, double system_max_cutoff_)
Dipoles::ShortRangeForceKernel::kernel_type const *const dipoles_kernel
CellStructure::AoSoA_pack const & aosoa
CellStructure::ForceType const & local_torque
double system_max_cutoff
InteractionsNonBonded const & nonbonded_ias
Utils::Vector3d *const global_virial
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void operator()(std::size_t i, std::size_t j) const
BondedInteractionsMap const & bonded_ias
CellStructure::ForceType const & local_force
CoulombP3M const * p3m
Coulomb::ShortRangeForceKernel::kernel_type const *const coulomb_kernel
Thermostat::Thermostat const & thermostat
Force information on a particle.
Definition Particle.hpp:330
Struct holding all information for one particle.
Definition Particle.hpp:435
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:45