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 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#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
25
26#include "aosoa_pack.hpp"
27#include "forces_inline.hpp"
28
29#include <utils/Vector.hpp>
30
31#include <Cabana_Core.hpp>
32
33#include <omp.h>
34
35#include <cstddef>
36#include <memory>
37#include <optional>
38#include <variant>
39#include <vector>
40
41#if defined(__GNUG__) or defined(__clang__)
42#define ESPRESSO_ATTR_ALWAYS_INLINE [[gnu::always_inline]]
43#else
44#define ESPRESSO_ATTR_ALWAYS_INLINE
45#endif
46
56 std::vector<Particle *> const &unique_particles;
58#ifdef ESPRESSO_ROTATION
60#endif
61#ifdef ESPRESSO_NPT
64#endif
66#ifdef ESPRESSO_P3M
68#endif
69
71 BondedInteractionsMap const &bonded_ias_,
72 InteractionsNonBonded const &nonbonded_ias_,
76 Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_u_kernel_,
77 Coulomb::Solver const &coulomb_,
78 Thermostat::Thermostat const &thermostat_, BoxGeometry const &box_geo_,
79 std::vector<Particle *> const &unique_particles_,
80 CellStructure::ForceType const &local_force_,
81#ifdef ESPRESSO_ROTATION
82 CellStructure::ForceType const &local_torque_,
83#endif
84#ifdef ESPRESSO_NPT
85 Utils::Vector3d *const global_virial_,
86 CellStructure::VirialType const &local_virial_,
87#endif
88 CellStructure::AoSoA_pack const &aosoa_)
89 : bonded_ias(bonded_ias_), nonbonded_ias(nonbonded_ias_),
90 coulomb_kernel(coulomb_kernel_), dipoles_kernel(dipoles_kernel_),
91 elc_kernel(elc_kernel_), coulomb_u_kernel(coulomb_u_kernel_),
92 thermostat(thermostat_), box_geo(box_geo_),
93 unique_particles(unique_particles_), local_force(local_force_),
94#ifdef ESPRESSO_ROTATION
95 local_torque(local_torque_),
96#endif
97#ifdef ESPRESSO_NPT
98 global_virial(global_virial_), local_virial(local_virial_),
99#endif
100 aosoa(aosoa_) {
101#ifdef ESPRESSO_P3M
102 p3m = nullptr;
103 if (auto &solver = coulomb_.impl->solver; solver.has_value()) {
104 if (std::holds_alternative<std::shared_ptr<CoulombP3M>>(*solver)) {
105 p3m = std::get<std::shared_ptr<CoulombP3M>>(*solver).get();
106 }
107 }
108#endif
109 }
110
111 // Helper functions to check if specific algorithms are active
112 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool
113 gay_berne_active(double dist, IA_parameters const &ia_params) const {
114#ifdef ESPRESSO_GAY_BERNE
115 return dist < ia_params.gay_berne.cut;
116#else
117 return false;
118#endif
119 }
120
121#ifdef ESPRESSO_NPT
122 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool npt_active() const {
123 return global_virial != nullptr;
124 }
125#endif
126
127#ifdef ESPRESSO_THOLE
128 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool
129 thole_active(IA_parameters const &ia_params) const {
130 return (ia_params.thole.scaling_coeff != 0. and
131 ia_params.thole.q1q2 != 0. and coulomb_kernel != nullptr);
132 }
133#endif
134
135#ifdef ESPRESSO_DIPOLES
136 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool
138 return dipoles_kernel != nullptr;
139 }
140#endif
141
142 ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION void
143 operator()(std::size_t i, std::size_t j) const {
144
145 auto const &ia_params =
147
148 ParticleForce pf{};
149
150 // calc distance
151 auto const pos1 = aosoa.get_vector_at(aosoa.position, i);
152 auto const pos2 = aosoa.get_vector_at(aosoa.position, j);
153 auto const d = box_geo.get_mi_vector(pos1, pos2);
154 auto const dist = d.norm();
155
156 // Determine which data needs to be loaded based on active algorithms
157#if defined(ESPRESSO_DIPOLES) or defined(ESPRESSO_GAY_BERNE)
158 bool const need_directors =
159 gay_berne_active(dist, ia_params) or dipoles_active();
160#endif
161#if defined(ESPRESSO_EXCLUSIONS) or defined(ESPRESSO_THOLE)
162 bool const need_particle_pointers = aosoa.has_exclusion(i) or
163 aosoa.has_exclusion(j) or
164 thole_active(ia_params);
165 Particle const *p1_ptr = nullptr;
166 Particle const *p2_ptr = nullptr;
167 if (need_particle_pointers) {
168 p1_ptr = unique_particles.at(i);
169 p2_ptr = unique_particles.at(j);
170 }
171#endif
172
173 // Load directors only if needed
174#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
175 Utils::Vector3d dir1{}, dir2{};
176 if (need_directors) {
179 }
180#endif
181
182 /***********************************************/
183 /* non-bonded pair potentials */
184 /***********************************************/
185
186 if (dist < ia_params.max_cut) {
187#ifdef ESPRESSO_EXCLUSIONS
188 bool skip_non_bonded = false;
189 if (aosoa.has_exclusion(i) or aosoa.has_exclusion(j)) {
190 skip_non_bonded = not do_nonbonded(*p1_ptr, *p2_ptr);
191 }
192#else
193 constexpr bool skip_non_bonded = false;
194#endif
195 if (not skip_non_bonded) {
196 pf.f += calc_central_radial_force(ia_params, d, dist);
197
198 // Only call Thole force kernel if active
199#ifdef ESPRESSO_THOLE
200 if (thole_active(ia_params)) {
201 pf.f += thole_pair_force(*p1_ptr, *p2_ptr, ia_params, d, dist,
203 }
204#endif
205 // Only call Gay-Berne force kernel if active
206#ifdef ESPRESSO_GAY_BERNE
207 if (gay_berne_active(dist, ia_params)) {
208 pf += gb_pair_force(dir1, dir2, ia_params, d, dist);
209 }
210#endif
211 } // not skip_non_bonded
212 }
213
214 /*********************************************************************/
215 /* everything before this contributes to the virial pressure in NpT, */
216 /* but nothing afterwards, since the contribution to pressure from */
217 /* electrostatic is calculated by energy */
218 /*********************************************************************/
219#ifdef ESPRESSO_NPT
220 Utils::Vector3d virial{};
221 if (npt_active()) {
222 virial = hadamard_product(pf.f, d);
223 }
224#endif // ESPRESSO_NPT
225
226 /***********************************************/
227 /* thermostat */
228 /***********************************************/
229
230 /* The inter dpd force should not be part of the virial */
231#ifdef ESPRESSO_DPD
233 auto const dist2 = dist * dist;
234 auto const vel1 = aosoa.get_vector_at(aosoa.velocity, i);
235 auto const vel2 = aosoa.get_vector_at(aosoa.velocity, j);
236 auto const force =
237 dpd_pair_force(pos1, vel1, aosoa.id(i), pos2, vel2, aosoa.id(j),
238 *thermostat.dpd, box_geo, ia_params, d, dist, dist2);
239 pf += force;
240 }
241#endif // ESPRESSO_DPD
242
243#ifdef ESPRESSO_ELECTROSTATICS
244 Utils::Vector3d f1_asym{};
245 Utils::Vector3d f2_asym{};
246 // real-space electrostatic charge-charge interaction
247 if (coulomb_kernel != nullptr) {
248 auto const q1q2 = aosoa.charge(i) * aosoa.charge(j);
249 if (q1q2 != 0) {
250#ifdef ESPRESSO_P3M
251 if (p3m) [[likely]] {
252 pf.f += p3m->pair_force(q1q2, d, dist);
253 } else
254#endif
255 {
256 pf.f += (*coulomb_kernel)(q1q2, d, dist);
257 }
258 if (elc_kernel) {
259 (*elc_kernel)(pos1, pos2, f1_asym, f2_asym, q1q2);
260 }
261#ifdef ESPRESSO_NPT
262 if (npt_active()) {
263 virial[0] += (*coulomb_u_kernel)(pos1, pos2, q1q2, d, dist);
264 }
265#endif // ESPRESSO_NPT
266 }
267 }
268#endif // ESPRESSO_ELECTROSTATICS
269
270 // Only call dipole force kernel if active
271#ifdef ESPRESSO_DIPOLES
272 if (dipoles_active()) {
273 auto const d1d2 = aosoa.dipm(i) * aosoa.dipm(j);
274 if (d1d2 != 0.) {
275 pf += (*dipoles_kernel)(d1d2, aosoa.dipm(i) * dir1,
276 aosoa.dipm(j) * dir2, d, dist, dist * dist);
277 }
278 }
279#endif // ESPRESSO_DIPOLES
280
281 auto opf = calc_opposing_force(pf, d);
282#ifdef ESPRESSO_ELECTROSTATICS
283 pf.f += f1_asym;
284 opf.f += f2_asym;
285#endif // ESPRESSO_ELECTROSTATICS
286
287 auto const thread_id = omp_get_thread_num();
288
289 local_force(i, thread_id, 0) += pf.f[0];
290 local_force(i, thread_id, 1) += pf.f[1];
291 local_force(i, thread_id, 2) += pf.f[2];
292#ifdef ESPRESSO_ROTATION
293 local_torque(i, thread_id, 0) += pf.torque[0];
294 local_torque(i, thread_id, 1) += pf.torque[1];
295 local_torque(i, thread_id, 2) += pf.torque[2];
296#endif
297
298 local_force(j, thread_id, 0) += opf.f[0];
299 local_force(j, thread_id, 1) += opf.f[1];
300 local_force(j, thread_id, 2) += opf.f[2];
301#ifdef ESPRESSO_ROTATION
302 local_torque(j, thread_id, 0) += opf.torque[0];
303 local_torque(j, thread_id, 1) += opf.torque[1];
304 local_torque(j, thread_id, 2) += opf.torque[2];
305#endif
306#ifdef ESPRESSO_NPT
307 if (npt_active()) {
308 local_virial(thread_id, 0) += virial[0];
309 local_virial(thread_id, 1) += virial[1];
310 local_virial(thread_id, 2) += virial[2];
311 }
312#endif
313 }
314};
315
316#endif // ESPRESSO_SHARED_MEMORY_PARALLELISM
@ THERMO_DPD
Vector implementation and trait types for boost qvm interoperability.
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.
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:79
bool do_nonbonded(Particle const &p1, Particle const &p2)
Determine if the non-bonded interactions between p1 and p2 should be calculated.
#define ESPRESSO_ATTR_ALWAYS_INLINE
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
Utils::Vector3d get_vector_at(Kokkos::View< double *[3], array_layout, Kokkos::HostSpace > const &view, std::size_t i) const
bool has_exclusion(std::size_t i) const
PositionViewType position
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
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool thole_active(IA_parameters const &ia_params) const
std::vector< Particle * > const & unique_particles
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool gay_berne_active(double dist, IA_parameters const &ia_params) const
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
ESPRESSO_ATTR_ALWAYS_INLINE KOKKOS_INLINE_FUNCTION bool dipoles_active() const
Dipoles::ShortRangeForceKernel::kernel_type const *const dipoles_kernel
CellStructure::AoSoA_pack const & aosoa
CellStructure::ForceType const & local_torque
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_)
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
Parameters for non-bonded interactions.
GayBerne_Parameters gay_berne
Force information on a particle.
Definition Particle.hpp:345
Struct holding all information for one particle.
Definition Particle.hpp:450
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