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 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/**
39 * @brief copy current position
40 *
41 * @param particles particle range
42 * @param ghost_particles ghost particle range
43 */
44void save_old_position(const ParticleRange &particles,
45 const ParticleRange &ghost_particles) {
46 auto save_pos = [](Particle &p) { p.pos_last_time_step() = p.pos(); };
47
48 boost::for_each(particles, save_pos);
49 boost::for_each(ghost_particles, save_pos);
50}
51
52/**
53 * @brief reset correction vectors to zero
54 *
55 * @param particles particle range
56 * @param ghost_particles ghost particle range
57 */
58static void init_correction_vector(const ParticleRange &particles,
59 const ParticleRange &ghost_particles) {
60 auto reset_force = [](Particle &p) { p.rattle_params().correction.fill(0); };
61
62 boost::for_each(particles, reset_force);
63 boost::for_each(ghost_particles, reset_force);
64}
65
66/**
67 * @brief Calculate the positional correction for the particles.
68 *
69 * @param ia_params Parameters
70 * @param box_geo Box geometry.
71 * @param p1 First particle.
72 * @param p2 Second particle.
73 * @return True if there was a correction.
74 */
75static bool calculate_positional_correction(RigidBond const &ia_params,
76 BoxGeometry const &box_geo,
77 Particle &p1, Particle &p2) {
78 auto const r_ij = box_geo.get_mi_vector(p1.pos(), p2.pos());
79 auto const r_ij2 = r_ij.norm2();
80
81 if (std::abs(1.0 - r_ij2 / ia_params.d2) > ia_params.p_tol) {
82 auto const r_ij_t =
84 auto const r_ij_dot = r_ij_t * r_ij;
85 auto const G =
86 0.50 * (ia_params.d2 - r_ij2) / r_ij_dot / (p1.mass() + p2.mass());
87
88 auto const pos_corr = G * r_ij_t;
89 p1.rattle_params().correction += pos_corr * p2.mass();
90 p2.rattle_params().correction -= pos_corr * p1.mass();
91
92 return true;
93 }
94
95 return false;
96}
97
98/**
99 * @brief Compute the correction vectors using given kernel.
100 *
101 * @param cs cell structure
102 * @param box_geo Box geometry
103 * @param kernel kernel function
104 * @return True if correction is necessary
105 */
106template <typename Kernel>
108 BoxGeometry const &box_geo,
109 Kernel kernel) {
110 bool correction = false;
111 cs.bond_loop(
112 [&correction, &kernel, &box_geo](Particle &p1, int bond_id,
113 Utils::Span<Particle *> partners) {
114 auto const &iaparams = *bonded_ia_params.at(bond_id);
115
116 if (auto const *bond = boost::get<RigidBond>(&iaparams)) {
117 auto const corrected = kernel(*bond, box_geo, p1, *partners[0]);
118 if (corrected)
119 correction = true;
120 }
121
122 /* Rigid bonds cannot break */
123 return false;
124 });
125
126 return correction;
127}
128
129/**
130 * @brief Apply positional corrections
131 *
132 * @param particles particle range
133 */
134static void apply_positional_correction(const ParticleRange &particles) {
135 boost::for_each(particles, [](Particle &p) {
136 p.pos() += p.rattle_params().correction;
137 p.v() += p.rattle_params().correction;
138 });
139}
140
144
145 auto particles = cs.local_particles();
146 auto ghost_particles = cs.ghost_particles();
147
148 int cnt;
149 for (cnt = 0; cnt < SHAKE_MAX_ITERATIONS; ++cnt) {
150 init_correction_vector(particles, ghost_particles);
151 bool const repeat_ =
153 bool const repeat =
154 boost::mpi::all_reduce(comm_cart, repeat_, std::logical_or<bool>());
155
156 // no correction is necessary, skip communication and bail out
157 if (!repeat)
158 break;
159
161
164 }
165 if (cnt >= SHAKE_MAX_ITERATIONS) {
166 runtimeErrorMsg() << "RATTLE failed to converge after " << cnt
167 << " iterations";
168 }
169
170 auto const resort_level =
172 cs.set_resort_particles(resort_level);
173}
174
175/**
176 * @brief Calculate the velocity correction for the particles.
177 *
178 * The position correction is accumulated in the forces
179 * of the particles so that it can be reduced over the ghosts.
180 *
181 * @param ia_params Parameters
182 * @param box_geo Box geometry.
183 * @param p1 First particle.
184 * @param p2 Second particle.
185 * @return True if there was a correction.
186 */
187static bool calculate_velocity_correction(RigidBond const &ia_params,
188 BoxGeometry const &box_geo,
189 Particle &p1, Particle &p2) {
190 auto const v_ij = p1.v() - p2.v();
191 auto const r_ij = box_geo.get_mi_vector(p1.pos(), p2.pos());
192
193 auto const v_proj = v_ij * r_ij;
194 if (std::abs(v_proj) > ia_params.v_tol) {
195 auto const K = v_proj / ia_params.d2 / (p1.mass() + p2.mass());
196
197 auto const vel_corr = K * r_ij;
198
199 p1.rattle_params().correction -= vel_corr * p2.mass();
200 p2.rattle_params().correction += vel_corr * p1.mass();
201
202 return true;
203 }
204
205 return false;
206}
207
208/**
209 * @brief Apply velocity corrections
210 *
211 * @param particles particle range
212 */
213static void apply_velocity_correction(ParticleRange const &particles) {
214 boost::for_each(particles,
215 [](Particle &p) { p.v() += p.rattle_params().correction; });
216}
217
220
221 auto particles = cs.local_particles();
222 auto ghost_particles = cs.ghost_particles();
223
224 int cnt;
225 for (cnt = 0; cnt < SHAKE_MAX_ITERATIONS; ++cnt) {
226 init_correction_vector(particles, ghost_particles);
227 bool const repeat_ =
229 bool const repeat =
230 boost::mpi::all_reduce(comm_cart, repeat_, std::logical_or<bool>());
231
232 // no correction is necessary, skip communication and bail out
233 if (!repeat)
234 break;
235
237
238 apply_velocity_correction(particles);
240 }
241
242 if (cnt >= SHAKE_MAX_ITERATIONS) {
243 runtimeErrorMsg() << "VEL RATTLE failed to converge after " << cnt
244 << " iterations";
245 }
246}
247
248#endif
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
mapped_type at(key_type const &key) const
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.
A range of particles.
A stripped-down version of std::span from C++17.
Definition Span.hpp:38
boost::mpi::communicator comm_cart
The communicator.
#define SHAKE_MAX_ITERATIONS
Maximal number of iterations in the RATTLE algorithm before it bails out.
Definition config.hpp:88
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 save_old_position(const ParticleRange &particles, const ParticleRange &ghost_particles)
copy current position
Definition rattle.cpp:44
static bool compute_correction_vector(CellStructure &cs, BoxGeometry const &box_geo, Kernel kernel)
Compute the correction vectors using given kernel.
Definition rattle.cpp:107
void correct_velocity_shake(CellStructure &cs, BoxGeometry const &box_geo)
Correction of current velocities using RATTLE algorithm.
Definition rattle.cpp:218
static void init_correction_vector(const ParticleRange &particles, const ParticleRange &ghost_particles)
reset correction vectors to zero
Definition rattle.cpp:58
void correct_position_shake(CellStructure &cs, BoxGeometry const &box_geo)
Propagate velocity and position while using SHAKE algorithm for bond constraint.
Definition rattle.cpp:141
static void apply_positional_correction(const ParticleRange &particles)
Apply positional corrections.
Definition rattle.cpp:134
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:187
static void apply_velocity_correction(ParticleRange const &particles)
Apply velocity corrections.
Definition rattle.cpp:213
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:75
RATTLE algorithm ().
Definition of the rigid bond data type for the Rattle algorithm.
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.
Struct holding all information for one particle.
Definition Particle.hpp:393
auto const & rattle_params() const
Definition Particle.hpp:565
auto const & mass() const
Definition Particle.hpp:450
auto const & pos_last_time_step() const
Definition Particle.hpp:563
auto const & v() const
Definition Particle.hpp:431
auto const & pos() const
Definition Particle.hpp:429
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...