ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
steepest_descent.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 <config/config.hpp>
23
25
26#include "Particle.hpp"
28#include "communication.hpp"
29#include "rotation.hpp"
30
31#include <utils/Vector.hpp>
32#include <utils/math/sqr.hpp>
33
34#include <boost/mpi/collectives/all_reduce.hpp>
35#include <boost/mpi/operations.hpp>
36
37#include <algorithm>
38#include <cmath>
39#include <limits>
40#include <stdexcept>
41
42bool SteepestDescent::propagate(CellStructure &cell_structure) const {
43 // Maximal force encountered on node
44 auto f_max_local = -std::numeric_limits<double>::max();
45
46 // Iteration over all local particles
47 for (auto &p : cell_structure.local_particles()) {
48 auto f = 0.0;
49
50 // For all Cartesian coordinates
51 for (auto j = 0u; j < 3u; ++j) {
52 // Skip, if coordinate is fixed
53 if (!p.is_fixed_along(j)) {
54 // Square of force on particle
55 f += Utils::sqr(p.force()[j]);
56
57 // Positional increment, crop to maximum allowed by user
58 auto const dp = std::clamp(gamma * p.force()[j], -max_displacement,
60
61 // Move particle
62 p.pos()[j] += dp;
63 }
64 }
65#ifdef ESPRESSO_ROTATION
66 {
67 // Rotational increment
68 auto const dq = gamma * p.torque(); // Vector parallel to torque
69 auto const t = p.torque().norm2();
70
71 // Normalize rotation axis and compute amount of rotation
72 auto const l = dq.norm();
73 if (l > 0.0) {
74 auto const axis = dq / l;
75 auto const angle = std::clamp(l, -max_displacement, max_displacement);
76
77 // Rotate the particle around axis dq by amount l
78 local_rotate_particle(p, axis, angle);
79 }
80
81 f_max_local = std::max(f_max_local, t);
82 }
83#endif
84 // Note maximum force/torque encountered
85 f_max_local = std::max(f_max_local, f);
86 }
87
89
90 // Synchronize maximum force/torque encountered
91 auto const f_max_global = boost::mpi::all_reduce(
92 comm_cart, f_max_local, boost::mpi::maximum<double>());
93
94 return std::sqrt(f_max_global) < f_max;
95}
96
97SteepestDescent::SteepestDescent(double f_max, double gamma,
98 double max_displacement)
99 : f_max{f_max}, gamma{gamma}, max_displacement{max_displacement} {
100 if (f_max < 0.0) {
101 throw std::runtime_error("The maximal force must be positive.");
102 }
103 if (gamma < 0.0) {
104 throw std::runtime_error("The dampening constant must be positive.");
105 }
106 if (max_displacement < 0.0) {
107 throw std::runtime_error("The maximal displacement must be positive.");
108 }
109}
Vector implementation and trait types for boost qvm interoperability.
Describes a cell structure / cell system.
void set_resort_particles(Cells::Resort level)
Increase the local resort level at least to level.
ParticleRange local_particles() const
boost::mpi::communicator comm_cart
The communicator.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28
This file contains all subroutines required to process rotational motion.
void local_rotate_particle(Particle &p, const Utils::Vector3d &axis_space_frame, const double phi)
Rotate the particle p around the NORMALIZED axis aSpaceFrame by amount phi.
Definition rotation.hpp:134
SteepestDescent()=default
double max_displacement
Maximal particle displacement or rotation.
double gamma
Dampening constant.
double f_max
Maximal particle force or torque.
bool propagate(CellStructure &cell_structure) const
Run steepest descent algorithm.