ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
electrostatics/scafacos_impl.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
24#ifdef SCAFACOS
25
28
29#include "BoxGeometry.hpp"
30#include "LocalBox.hpp"
32#include "communication.hpp"
33#include "system/System.hpp"
34#include "tuning.hpp"
35
36#include <utils/Vector.hpp>
37
38#include <algorithm>
39#include <cassert>
40#include <cmath>
41#include <iterator>
42#include <limits>
43#include <span>
44#include <string>
45
46std::shared_ptr<CoulombScafacos>
47make_coulomb_scafacos(std::string const &method,
48 std::string const &parameters) {
49 return std::make_shared<CoulombScafacosImpl>(comm_cart, method, parameters);
50}
51
53 auto const &system = get_system();
54 auto const &box_geo = *system.box_geo;
55 auto const &cell_structure = *system.cell_structure;
56
57 positions.clear();
58 charges.clear();
59
60 for (auto const &p : cell_structure.local_particles()) {
61 auto const pos = box_geo.folded_position(p.pos());
62 positions.push_back(pos[0]);
63 positions.push_back(pos[1]);
64 positions.push_back(pos[2]);
65 charges.push_back(p.q());
66 }
67}
68
70 if (positions.empty())
71 return;
72
73 auto const &cell_structure = *get_system().cell_structure;
74
75 auto it_fields = fields.begin();
76 for (auto &p : cell_structure.local_particles()) {
77 p.force() += prefactor * p.q() *
78 Utils::Vector3d(std::span<const double>(&*it_fields, 3ul));
79 std::advance(it_fields, 3);
80 }
81
82 /* Check that the particle number did not change */
83 assert(it_fields == fields.end());
84}
85
86double CoulombScafacosImpl::time_r_cut(double r_cut) {
87 set_r_cut_and_tune(r_cut);
88 auto &system = get_system();
89 return benchmark_integration_step(system, 10);
90}
91
92void CoulombScafacosImpl::tune_r_cut() {
93 auto constexpr convergence_threshold = 1e-3;
94 auto const &system = get_system();
95 auto const &box_geo = *system.box_geo;
96 auto const &local_geo = *system.local_geo;
97 auto const verlet_skin = system.cell_structure->get_verlet_skin();
98
99 auto const min_box_l = std::ranges::min(box_geo.length());
100 auto const min_local_box_l = std::ranges::min(local_geo.length());
101
102 /* The bisection code breaks down when r_min < 1 for several methods
103 * (e.g. p2nfft, p3m, ewald) if the mesh size is not fixed (ScaFaCoS
104 * either hangs or allocates too much memory) */
105 auto r_min = 1.0;
106 auto r_max = std::min(min_local_box_l, min_box_l / 2.0) - verlet_skin;
107 assert(r_max >= r_min);
108 auto t_min = 0.0;
109 auto t_max = std::numeric_limits<double>::max();
110 auto r_opt = -1.0;
111
112 /* Run bisection */
113 while (std::fabs(r_min - r_max) > convergence_threshold) {
114 r_opt = (r_max + r_min) / 2.;
115 auto const dr = 0.5 * (r_max - r_min);
116 auto const t_mid = time_r_cut(r_min + dr);
117 t_min = time_r_cut(r_min);
118 t_max = time_r_cut(r_max);
119
120 if (t_min <= 0.0 or t_max <= 0.0) {
121 break;
122 }
123
124 if (t_mid > t_min) {
125 r_max = r_min += dr;
126 } else {
127 r_min += dr;
128 }
129 }
130 assert(r_opt >= 0.);
131 set_r_cut(r_opt);
132}
133
136
137 // Check whether we have to do a bisection for the short-range cutoff
138 // Check if there is a user-supplied cutoff
139 if (ScafacosContext::get_near_field_delegation() and
140 ScafacosContext::r_cut() <= 0.0) {
141 tune_r_cut();
142 } else {
143 // ESPResSo is not affected by a short-range cutoff -> tune in parallel
144 ScafacosContext::tune(charges, positions);
145 }
146 get_system().on_coulomb_change();
147}
148
149#endif // SCAFACOS
Vector implementation and trait types for boost qvm interoperability.
double prefactor
Electrostatics prefactor.
boost::mpi::communicator comm_cart
The communicator.
This file contains the defaults for ESPResSo.
std::shared_ptr< CoulombScafacos > make_coulomb_scafacos(std::string const &method, std::string const &parameters)
VectorXd< 3 > Vector3d
Definition Vector.hpp:164
void update_particle_forces() const override
double benchmark_integration_step(System::System &system, int int_steps)
Benchmark the integration loop.
Definition tuning.cpp:73