ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
rattle.cpp
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#include "rattle.hpp"
23
24#ifdef ESPRESSO_BOND_CONSTRAINT
25
26#include "BoxGeometry.hpp"
27#include "Particle.hpp"
28#include "ParticleRange.hpp"
32#include "communication.hpp"
33#include "errorhandling.hpp"
34
35#include <boost/mpi/collectives/all_reduce.hpp>
36#include <boost/range/algorithm.hpp>
37
38#include <cmath>
39#include <functional>
40#include <span>
41#include <variant>
42
43/** Maximal number of iterations before the RATTLE algorithm bails out. */
44static constexpr auto shake_max_iterations = 1000;
45
46static void check_convergence(int cnt, char const *const name) {
47 static constexpr char const *const msg = " failed to converge after ";
48 if (cnt >= shake_max_iterations) {
49 runtimeErrorMsg() << name << msg << cnt << " iterations";
50 }
51}
52
53/**
54 * @brief copy current position
55 *
56 * @param particles particle range
57 * @param ghost_particles ghost particle range
58 */
59void save_old_position(const ParticleRange &particles,
60 const ParticleRange &ghost_particles) {
61 auto save_pos = [](Particle &p) { p.pos_last_time_step() = p.pos(); };
62
63 boost::for_each(particles, save_pos);
64 boost::for_each(ghost_particles, save_pos);
65}
66
67/**
68 * @brief reset correction vectors to zero
69 *
70 * @param particles particle range
71 * @param ghost_particles ghost particle range
72 */
73static void init_correction_vector(const ParticleRange &particles,
74 const ParticleRange &ghost_particles) {
75 auto reset_force = [](Particle &p) { p.rattle_params().correction.fill(0); };
76
77 boost::for_each(particles, reset_force);
78 boost::for_each(ghost_particles, reset_force);
79}
80
81/**
82 * @brief Calculate the positional correction for the particles.
83 *
84 * @param ia_params Parameters
85 * @param box_geo Box geometry.
86 * @param p1 First particle.
87 * @param p2 Second particle.
88 * @return True if there was a correction.
89 */
90static bool calculate_positional_correction(RigidBond const &ia_params,
91 BoxGeometry const &box_geo,
92 Particle &p1, Particle &p2) {
93 auto const r_ij = box_geo.get_mi_vector(p1.pos(), p2.pos());
94 auto const r_ij2 = r_ij.norm2();
95
96 if (std::abs(1.0 - r_ij2 / ia_params.d2) > ia_params.p_tol) {
97 auto const r_ij_t =
99 auto const r_ij_dot = r_ij_t * r_ij;
100 auto const G =
101 0.50 * (ia_params.d2 - r_ij2) / r_ij_dot / (p1.mass() + p2.mass());
102
103 auto const pos_corr = G * r_ij_t;
104 p1.rattle_params().correction += pos_corr * p2.mass();
105 p2.rattle_params().correction -= pos_corr * p1.mass();
106
107 return true;
108 }
109
110 return false;
111}
112
113/**
114 * @brief Compute the correction vectors using given kernel.
115 *
116 * @param cs cell structure
117 * @param box_geo Box geometry
118 * @param bonded_ias Bonded interactions
119 * @param kernel kernel function
120 * @return True if correction is necessary
121 */
122template <typename Kernel>
124 BoxGeometry const &box_geo,
125 BondedInteractionsMap const &bonded_ias,
126 Kernel kernel) {
127 bool correction = false;
128 cs.bond_loop([&correction, &kernel, &box_geo, &bonded_ias](
129 Particle &p1, int bond_id, std::span<Particle *> partners) {
130 auto const &iaparams = *bonded_ias.at(bond_id);
131
132 if (auto const *bond = std::get_if<RigidBond>(&iaparams)) {
133 auto const corrected = kernel(*bond, box_geo, p1, *partners[0]);
134 if (corrected)
135 correction = true;
136 }
137
138 /* Rigid bonds cannot break */
139 return false;
140 });
141
142 return correction;
143}
144
145/**
146 * @brief Apply positional corrections
147 *
148 * @param particles particle range
149 */
150static void apply_positional_correction(const ParticleRange &particles) {
151 boost::for_each(particles, [](Particle &p) {
152 p.pos() += p.rattle_params().correction;
153 p.v() += p.rattle_params().correction;
154 });
155}
156
158 BondedInteractionsMap const &bonded_ias) {
161
162 auto particles = cs.local_particles();
163 auto ghost_particles = cs.ghost_particles();
164
165 int cnt;
166 for (cnt = 0; cnt < shake_max_iterations; ++cnt) {
167 init_correction_vector(particles, ghost_particles);
168 bool const repeat_ = compute_correction_vector(
169 cs, box_geo, bonded_ias, calculate_positional_correction);
170 bool const repeat =
171 boost::mpi::all_reduce(comm_cart, repeat_, std::logical_or<bool>());
172
173 // no correction is necessary, skip communication and bail out
174 if (!repeat)
175 break;
176
178
181 }
182 check_convergence(cnt, "RATTLE");
183
184 auto const resort_level =
186 cs.set_resort_particles(resort_level);
187}
188
189/**
190 * @brief Calculate the velocity correction for the particles.
191 *
192 * The position correction is accumulated in the forces
193 * of the particles so that it can be reduced over the ghosts.
194 *
195 * @param ia_params Parameters
196 * @param box_geo Box geometry.
197 * @param p1 First particle.
198 * @param p2 Second particle.
199 * @return True if there was a correction.
200 */
201static bool calculate_velocity_correction(RigidBond const &ia_params,
202 BoxGeometry const &box_geo,
203 Particle &p1, Particle &p2) {
204 auto const v_ij = p1.v() - p2.v();
205 auto const r_ij = box_geo.get_mi_vector(p1.pos(), p2.pos());
206
207 auto const v_proj = v_ij * r_ij;
208 if (std::abs(v_proj) > ia_params.v_tol) {
209 auto const K = v_proj / ia_params.d2 / (p1.mass() + p2.mass());
210
211 auto const vel_corr = K * r_ij;
212
213 p1.rattle_params().correction -= vel_corr * p2.mass();
214 p2.rattle_params().correction += vel_corr * p1.mass();
215
216 return true;
217 }
218
219 return false;
220}
221
222/**
223 * @brief Apply velocity corrections
224 *
225 * @param particles particle range
226 */
227static void apply_velocity_correction(ParticleRange const &particles) {
228 boost::for_each(particles,
229 [](Particle &p) { p.v() += p.rattle_params().correction; });
230}
231
233 BondedInteractionsMap const &bonded_ias) {
235
236 auto particles = cs.local_particles();
237 auto ghost_particles = cs.ghost_particles();
238
239 int cnt;
240 for (cnt = 0; cnt < shake_max_iterations; ++cnt) {
241 init_correction_vector(particles, ghost_particles);
242 bool const repeat_ = compute_correction_vector(
243 cs, box_geo, bonded_ias, calculate_velocity_correction);
244 bool const repeat =
245 boost::mpi::all_reduce(comm_cart, repeat_, std::logical_or<bool>());
246
247 // no correction is necessary, skip communication and bail out
248 if (!repeat)
249 break;
250
252
253 apply_velocity_correction(particles);
255 }
256 check_convergence(cnt, "VEL RATTLE");
257}
258
259#endif
Data structures for bonded interactions.
container for bonded interactions.
mapped_type at(key_type const &key) const
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.
Describes a cell structure / cell system.
ParticleRange ghost_particles() const
void update_ghosts_and_resort_particle(unsigned data_parts)
Update ghost particles, with particle resort if needed.
void ghosts_update(unsigned data_parts)
Update ghost particles.
bool check_resort_required(Utils::Vector3d const &additional_offset={}) const
Check whether a particle has moved further than half the skin since the last Verlet list update,...
void bond_loop(BondKernel const &bond_kernel)
Bonded pair loop.
void set_resort_particles(Cells::Resort level)
Increase the local resort level at least to level.
ParticleRange local_particles() const
void ghosts_reduce_rattle_correction()
Add rattle corrections from ghost particles to real particles.
A range of particles.
boost::mpi::communicator comm_cart
The communicator.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
@ DATA_PART_MOMENTUM
Particle::m.
@ DATA_PART_PROPERTIES
Particle::p.
@ DATA_PART_POSITION
Particle::r.
void correct_velocity_shake(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias)
Correction of current velocities using RATTLE algorithm.
Definition rattle.cpp:232
void save_old_position(const ParticleRange &particles, const ParticleRange &ghost_particles)
copy current position
Definition rattle.cpp:59
void correct_position_shake(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias)
Propagate velocity and position while using SHAKE algorithm for bond constraint.
Definition rattle.cpp:157
static constexpr auto shake_max_iterations
Maximal number of iterations before the RATTLE algorithm bails out.
Definition rattle.cpp:44
static void init_correction_vector(const ParticleRange &particles, const ParticleRange &ghost_particles)
reset correction vectors to zero
Definition rattle.cpp:73
static void apply_positional_correction(const ParticleRange &particles)
Apply positional corrections.
Definition rattle.cpp:150
static bool calculate_velocity_correction(RigidBond const &ia_params, BoxGeometry const &box_geo, Particle &p1, Particle &p2)
Calculate the velocity correction for the particles.
Definition rattle.cpp:201
static void check_convergence(int cnt, char const *const name)
Definition rattle.cpp:46
static void apply_velocity_correction(ParticleRange const &particles)
Apply velocity corrections.
Definition rattle.cpp:227
static bool compute_correction_vector(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias, Kernel kernel)
Compute the correction vectors using given kernel.
Definition rattle.cpp:123
static bool calculate_positional_correction(RigidBond const &ia_params, BoxGeometry const &box_geo, Particle &p1, Particle &p2)
Calculate the positional correction for the particles.
Definition rattle.cpp:90
RATTLE algorithm ().
Definition of the rigid bond data type for the Rattle algorithm.
Struct holding all information for one particle.
Definition Particle.hpp:450
auto const & rattle_params() const
Definition Particle.hpp:658
auto const & mass() const
Definition Particle.hpp:507
auto const & pos_last_time_step() const
Definition Particle.hpp:656
auto const & v() const
Definition Particle.hpp:488
auto const & pos() const
Definition Particle.hpp:486
Parameters for the rigid_bond/SHAKE/RATTLE ALGORITHM.
double d2
Square of the length of Constrained Bond.
double v_tol
Velocity Tolerance/Accuracy for termination of RATTLE/SHAKE iterations during velocity corrections.
double p_tol
Positional Tolerance/Accuracy value for termination of RATTLE/SHAKE iterations during position correc...