ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
forces.cpp
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#include <config/config.hpp>
23
24#include "BoxGeometry.hpp"
25#include "Particle.hpp"
26#include "PropagationMode.hpp"
29#include "cells.hpp"
30#include "collision_detection/CollisionDetection.hpp"
31#include "communication.hpp"
32#include "constraints/Constraints.hpp"
34#include "forces_inline.hpp"
35#include "galilei/ComFixed.hpp"
42#include "npt.hpp"
43#include "rotation.hpp"
45#include "short_range_loop.hpp"
47#include "system/System.hpp"
48#include "thermostat.hpp"
50#include "virtual_sites/com.hpp"
52
53#include <utils/Vector.hpp>
54#include <utils/math/sqr.hpp>
55
56#ifdef ESPRESSO_CALIPER
57#include <caliper/cali.h>
58#endif
59
60#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
61#include <Cabana_Core.hpp>
62#endif
63
64#include <cassert>
65#include <cmath>
66#include <cstddef>
67#include <memory>
68#include <span>
69#include <variant>
70
71/** External particle forces */
73 ParticleForce f = {};
74
75#ifdef ESPRESSO_EXTERNAL_FORCES
76 f.f += p.ext_force();
77#ifdef ESPRESSO_ROTATION
78 f.torque += p.ext_torque();
79#endif
80#endif
81
82#ifdef ESPRESSO_ENGINE
83 // apply a swimming force in the direction of
84 // the particle's orientation axis
85 if (p.swimming().swimming and !p.swimming().is_engine_force_on_fluid) {
86 f.f += p.swimming().f_swim * p.calc_director();
87 }
88#endif
89
90 return f;
91}
92
93/** Combined force initialization and Langevin noise application */
95#ifdef ESPRESSO_CALIPER
97#endif
98
99 auto &cell_structure = *system.cell_structure;
100 auto const &propagation = *system.propagation;
101 auto const &thermostat = *system.thermostat;
102 auto const kT = thermostat.kT;
103 auto const time_step = system.get_time_step();
104
105 // Check if Langevin thermostat is active
106 bool const langevin_active =
107 thermostat.langevin &&
108 (propagation.used_propagations &
110
111 // Single pass over all local particles
112 cell_structure.for_each_local_particle([&](Particle &p) {
113 // Initialize force with external forces
115
116 // Apply Langevin noise if thermostat is active
117 if (langevin_active) {
118 auto const &langevin = *thermostat.langevin;
119 if (propagation.should_propagate_with(p, PropagationMode::TRANS_LANGEVIN))
120 p.force() += friction_thermo_langevin(langevin, p, time_step, kT);
121#ifdef ESPRESSO_ROTATION
122 if (propagation.should_propagate_with(p, PropagationMode::ROT_LANGEVIN))
124 p, friction_thermo_langevin_rotation(langevin, p, time_step, kT));
125#endif
126 }
127 });
128#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
129 cell_structure.reset_local_force();
130#endif
131
132 // Initialize ghost forces (unchanged)
133 cell_structure.ghosts_reset_forces();
134}
135
136static void force_capping(CellStructure &cell_structure, double force_cap) {
137 if (force_cap > 0.) {
138 auto const force_cap_sq = Utils::sqr(force_cap);
139 cell_structure.for_each_local_particle(
140 [&force_cap, &force_cap_sq](Particle &p) {
141 auto const force_sq = p.force().norm2();
142 if (force_sq > force_cap_sq) {
143 p.force() *= force_cap / std::sqrt(force_sq);
144 }
145 });
146 }
147}
148
149#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
150static void reinit_dip_fld(CellStructure const &cell_structure) {
151 cell_structure.for_each_local_particle(
152 [](Particle &p) { p.dip_fld() = {0., 0., 0.}; });
153}
154#endif
155
156#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
159 auto const &elc_kernel, auto const &coulomb_kernel,
160 auto const &dipoles_kernel, auto const &coulomb_u_kernel) {
161
162 auto const &unique_particles = system.cell_structure->get_unique_particles();
163 auto const &local_force = system.cell_structure->get_local_force();
164#ifdef ESPRESSO_ROTATION
165 auto const &local_torque = system.cell_structure->get_local_torque();
166#endif
167#ifdef ESPRESSO_NPT
168 auto const &local_virial = system.cell_structure->get_local_virial();
169#endif
170 auto const &aosoa = system.cell_structure->get_aosoa();
171
172 return /* ForcesKernel */ {*system.bonded_ias,
173 *system.nonbonded_ias,
174 get_ptr(coulomb_kernel),
175 get_ptr(dipoles_kernel),
176 get_ptr(elc_kernel),
177 get_ptr(coulomb_u_kernel),
178 system.coulomb,
179 *system.thermostat,
180 *system.box_geo,
181 unique_particles,
182 local_force,
183#ifdef ESPRESSO_ROTATION
184 local_torque,
185#endif
186#ifdef ESPRESSO_NPT
187 virial,
188 local_virial,
189#endif
190 aosoa,
192}
193
196 auto const &unique_particles = system.cell_structure->get_unique_particles();
197 auto const &local_force = system.cell_structure->get_local_force();
198#ifdef ESPRESSO_ROTATION
199 auto const &local_torque = system.cell_structure->get_local_torque();
200#endif
201#ifdef ESPRESSO_NPT
202 auto const &local_virial = system.cell_structure->get_local_virial();
203#endif
204
205 using execution_space = Kokkos::DefaultExecutionSpace;
206 int num_threads = execution_space().concurrency();
207 Kokkos::RangePolicy<execution_space> policy(std::size_t{0},
208 unique_particles.size());
209 Kokkos::parallel_for("reduction", policy,
210 [&local_force,
212 &local_torque,
213#endif
214 &unique_particles, num_threads](std::size_t const i) {
215 Utils::Vector3d force{};
216#ifdef ESPRESSO_ROTATION
217 Utils::Vector3d torque{};
218#endif
219 for (int tid = 0; tid < num_threads; ++tid) {
220 force[0] += local_force(i, tid, 0);
221 force[1] += local_force(i, tid, 1);
222 force[2] += local_force(i, tid, 2);
223#ifdef ESPRESSO_ROTATION
224 torque[0] += local_torque(i, tid, 0);
225 torque[1] += local_torque(i, tid, 1);
226 torque[2] += local_torque(i, tid, 2);
227#endif
228 }
229 unique_particles.at(i)->force() += force;
230#ifdef ESPRESSO_ROTATION
231 unique_particles.at(i)->torque() += torque;
232#endif
233 });
234 Kokkos::fence();
235
236#ifdef ESPRESSO_NPT
237 if (virial) {
238 for (int tid = 0; tid < num_threads; ++tid) {
239 (*virial)[0] += local_virial(tid, 0);
240 (*virial)[1] += local_virial(tid, 1);
241 (*virial)[2] += local_virial(tid, 2);
242 }
243 }
244#endif
245}
246#endif // ESPRESSO_SHARED_MEMORY_PARALLELISM
247
249#ifdef ESPRESSO_CALIPER
251#endif
252#ifdef ESPRESSO_CUDA
253 {
254#ifdef ESPRESSO_CALIPER
255 CALI_MARK_BEGIN("copy_particles_to_GPU");
256#endif
257 gpu->update();
258#ifdef ESPRESSO_CALIPER
259 CALI_MARK_END("copy_particles_to_GPU");
260#endif
261 }
262#endif // ESPRESSO_CUDA
263
264#ifdef ESPRESSO_COLLISION_DETECTION
265 collision_detection->clear_queue();
266 auto const collision_detection_cutoff = collision_detection->cutoff();
267#else
269#endif
270 bond_breakage->clear_queue();
271 auto particles = cell_structure->local_particles();
272#ifdef ESPRESSO_NPT
273 if (propagation->used_propagations & PropagationMode::TRANS_LANGEVIN_NPT) {
274 // reset virial part of instantaneous pressure
275 npt_inst_pressure->p_vir = Utils::Vector3d{};
276 }
277#endif
278#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
279 // reset dipole field
280 reinit_dip_fld(*cell_structure);
281#endif
282
283 // Use combined function instead of two separate calls
284
285 auto const elc_kernel = coulomb.pair_force_elc_kernel();
286 auto const coulomb_kernel = coulomb.pair_force_kernel();
287 auto const dipoles_kernel = dipoles.pair_force_kernel();
288 auto const coulomb_u_kernel = coulomb.pair_energy_kernel();
289 auto *const virial = get_npt_virial();
290
291 // interaction kernel is defined
292 auto bond_kernel = [coulomb_kernel_ptr = get_ptr(coulomb_kernel),
293 &bonded_ias = *bonded_ias,
294 &bond_breakage = *bond_breakage, virial,
295 &box_geo = *box_geo](Particle &p1, int bond_id,
296 std::span<Particle *> partners) {
297 return add_bonded_force(p1, bond_id, partners, bonded_ias, bond_breakage,
298 box_geo, virial, coulomb_kernel_ptr);
299 };
300
302 cell_structure->get_verlet_skin(),
303 get_interaction_range(),
304 coulomb.cutoff(),
305 dipoles.cutoff(),
307
308#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
309 update_cabana_state(*cell_structure, verlet_criterion,
310 get_interaction_range(), propagation->integ_switch);
311#endif
312#ifdef ESPRESSO_ELECTROSTATICS
313 if (coulomb.impl->extension) {
314 update_icc_particles();
315 }
316#endif // ESPRESSO_ELECTROSTATICS
318#ifdef ESPRESSO_CALIPER
319 CALI_MARK_BEGIN("calc_long_range_forces");
320#endif
321#ifdef ESPRESSO_ELECTROSTATICS
322 coulomb.calc_long_range_force();
323#endif
324#ifdef ESPRESSO_DIPOLES
325 dipoles.calc_long_range_force();
326#endif
327#ifdef ESPRESSO_CALIPER
328 CALI_MARK_END("calc_long_range_forces");
329#endif
330
331#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
332#ifdef ESPRESSO_CALIPER
333 CALI_MARK_BEGIN("cabana_short_range");
334#endif
335
337 create_cabana_neighbor_kernel(*this, virial, elc_kernel, coulomb_kernel,
338 dipoles_kernel, coulomb_u_kernel);
339
341 get_interaction_range(), bonded_ias->maximal_cutoff(),
342 verlet_criterion, propagation->integ_switch);
343
344 // Force and Torque reduction
346
347#ifdef ESPRESSO_COLLISION_DETECTION
348 auto collision_kernel = [&collision_detection = *collision_detection](
349 Particle const &p1, Particle const &p2,
350 Distance const &d) {
351 collision_detection.detect_collision(p1, p2, d.dist2);
352 };
353 if (not collision_detection->is_off()) {
354 cell_structure->non_bonded_loop(collision_kernel, verlet_criterion);
355 }
356#endif
357
358#ifdef ESPRESSO_CALIPER
359 CALI_MARK_END("cabana_short_range");
360#endif
361
362#else // ESPRESSO_SHARED_MEMORY_PARALLELISM
363
364#ifdef ESPRESSO_CALIPER
365 CALI_MARK_BEGIN("serial_short_range");
366#endif
367
368 auto pair_kernel = [coulomb_kernel_ptr = get_ptr(coulomb_kernel),
369 dipoles_kernel_ptr = get_ptr(dipoles_kernel),
370 elc_kernel_ptr = get_ptr(elc_kernel),
371 coulomb_u_kernel_ptr = get_ptr(coulomb_u_kernel),
372 &nonbonded_ias = *nonbonded_ias,
373 &thermostat = *thermostat, &bonded_ias = *bonded_ias,
374 virial,
375#ifdef ESPRESSO_COLLISION_DETECTION
376 &collision_detection = *collision_detection,
377#endif
378 &box_geo = *box_geo,
379 system_max_cutoff2 = Utils::sqr(maximal_cutoff())](
380 Particle &p1, Particle &p2, Distance const &d) {
381 if (d.dist2 > system_max_cutoff2)
382 return;
383 auto const &ia_params = nonbonded_ias.get_ia_param(p1.type(), p2.type());
385 p1, p2, d.vec21, sqrt(d.dist2), d.dist2, p1.q() * p2.q(), ia_params,
386 thermostat, box_geo, bonded_ias, virial, coulomb_kernel_ptr,
388#ifdef ESPRESSO_COLLISION_DETECTION
389 if (not collision_detection.is_off()) {
390 collision_detection.detect_collision(p1, p2, d.dist2);
391 }
392#endif
393 };
394
395 short_range_loop(bond_kernel, pair_kernel, *cell_structure, maximal_cutoff(),
396 bonded_ias->maximal_cutoff(), verlet_criterion);
397
398#ifdef ESPRESSO_CALIPER
399 CALI_MARK_END("serial_short_range");
400#endif
401
402#endif // ESPRESSO_SHARED_MEMORY_PARALLELISM
403
404 constraints->add_forces(particles, get_sim_time());
405 oif_global->calculate_forces();
406
407 // Must be done here. Forces need to be ghost-communicated
408 immersed_boundaries->volume_conservation(*cell_structure);
409
410 if (thermostat->lb and (propagation->used_propagations &
412#ifdef ESPRESSO_CALIPER
413 CALI_MARK_BEGIN("lb_particle_coupling");
414#endif
415 lb_couple_particles();
416#ifdef ESPRESSO_CALIPER
417 CALI_MARK_END("lb_particle_coupling");
418#endif
419 }
420
421#ifdef ESPRESSO_CUDA
422 {
423#ifdef ESPRESSO_CALIPER
424 CALI_MARK_BEGIN("copy_forces_from_GPU");
425#endif
426 gpu->copy_forces_to_host(particles, this_node);
427
428#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
429 gpu->copy_dip_fld_to_host(particles, this_node);
430#endif
431
432#ifdef ESPRESSO_CALIPER
433 CALI_MARK_END("copy_forces_from_GPU");
434#endif
435 }
436#endif // ESPRESSO_CUDA
437
438#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
439 if (propagation->used_propagations &
443 }
444#endif
445#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
446 if (propagation->used_propagations &
449 }
450#endif
451
452 // Communication step: ghost forces
453 cell_structure->ghosts_reduce_forces();
454
455 // should be pretty late, since it needs to zero out the total force
456 comfixed->apply(particles);
457
458 // Needs to be the last one to be effective
459 force_capping(*cell_structure, force_cap);
460
461 // mark that forces are now up-to-date
462 propagation->recalc_forces = false;
463}
Vector implementation and trait types for boost qvm interoperability.
This file contains everything related to the global cell structure / cell system.
double maximal_cutoff() const
Calculate the maximal cutoff of bonded interactions, required to determine the cell size for communic...
Describes a cell structure / cell system.
void for_each_local_particle(ParticleUnaryOp &&f, bool parallel=true) const
Run a kernel on all local particles.
Main system class.
void calculate_forces()
Calculate all forces.
Definition forces.cpp:248
Returns true if the particles are to be considered for short range interactions.
void vs_com_back_transfer_forces_and_torques(CellStructure &cell_structure)
Definition com.cpp:231
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
int this_node
The number of this node.
constexpr double inactive_cutoff
Special cutoff value for an inactive interaction.
Definition config.hpp:53
const T * get_ptr(std::optional< T > const &opt)
static void reinit_dip_fld(CellStructure const &cell_structure)
Definition forces.cpp:150
static void force_capping(CellStructure &cell_structure, double force_cap)
Definition forces.cpp:136
static void init_forces_and_thermostat(System::System const &system)
Combined force initialization and Langevin noise application.
Definition forces.cpp:94
static ParticleForce external_force(Particle const &p)
External particle forces.
Definition forces.cpp:72
static ForcesKernel create_cabana_neighbor_kernel(System::System const &system, Utils::Vector3d *virial, auto const &elc_kernel, auto const &coulomb_kernel, auto const &dipoles_kernel, auto const &coulomb_u_kernel)
Definition forces.cpp:157
static void reduce_cabana_forces_and_torques(System::System const &system, Utils::Vector3d *virial)
Definition forces.cpp:194
Force calculation.
void add_non_bonded_pair_force(Particle &p1, Particle &p2, Utils::Vector3d const &d, double dist, double dist2, double q1q2, IA_parameters const &ia_params, Thermostat::Thermostat const &thermostat, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias, Utils::Vector3d *const virial, 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)
Calculate non-bonded forces between a pair of particles and update their forces and torques.
bool add_bonded_force(Particle &p1, int bond_id, std::span< Particle * > partners, BondedInteractionsMap const &bonded_ia_params, BondBreakage::BondBreakage &bond_breakage, BoxGeometry const &box_geo, Utils::Vector3d *const virial, Coulomb::ShortRangeForceKernel::kernel_type const *kernel)
ICC is a method that allows to take into account the influence of arbitrarily shaped dielectric inter...
Utils::Vector3d friction_thermo_langevin(LangevinThermostat const &langevin, Particle const &p, double time_step, double kT)
Langevin thermostat for particle translational velocities.
Utils::Vector3d friction_thermo_langevin_rotation(LangevinThermostat const &langevin, Particle const &p, double time_step, double kT)
Langevin thermostat for particle angular velocities.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28
Various procedures concerning interactions between particles.
Exports for the NpT code.
void vs_relative_back_transfer_forces_and_torques(CellStructure &cell_structure)
Definition relative.cpp:163
This file contains all subroutines required to process rotational motion.
Utils::Vector3d convert_vector_body_to_space(const Particle &p, const Utils::Vector3d &vec)
Definition rotation.hpp:58
ESPRESSO_ATTR_ALWAYS_INLINE void update_cabana_state(CellStructure &cell_structure, auto const &verlet_criterion, double const pair_cutoff, auto const integ_switch)
void cabana_short_range(auto const &bond_kernel, auto const &forces_kernel, CellStructure &cell_structure, double pair_cutoff, double bond_cutoff, auto const &verlet_criterion, auto const integ_switch)
void short_range_loop(BondKernel bond_kernel, PairKernel pair_kernel, CellStructure &cell_structure, double pair_cutoff, double bond_cutoff, VerletCriterion const &verlet_criterion={})
Distance vector and length handed to pair kernels.
BondedInteractionsMap const & bonded_ias
Force information on a particle.
Definition Particle.hpp:330
Utils::Vector3d torque
torque.
Definition Particle.hpp:360
Utils::Vector3d f
force.
Definition Particle.hpp:356
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & dip_fld() const
Definition Particle.hpp:568
auto const & swimming() const
Definition Particle.hpp:633
auto const & force_and_torque() const
Definition Particle.hpp:477
auto const & torque() const
Definition Particle.hpp:519
auto const & ext_force() const
Definition Particle.hpp:626
auto const & ext_torque() const
Definition Particle.hpp:524
auto const & force() const
Definition Particle.hpp:475
auto calc_director() const
Definition Particle.hpp:527