ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
TuningAlgorithm.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#if defined(P3M) || defined(DP3M)
25
27#include "p3m/common.hpp"
28
29#include "tuning.hpp"
30
31#include "BoxGeometry.hpp"
32#include "LocalBox.hpp"
34#include "communication.hpp"
35#include "system/System.hpp"
36
37#include <boost/range/algorithm/min_element.hpp>
38
39#include <algorithm>
40#include <cassert>
41#include <cmath>
42#include <string>
43#include <tuple>
44#include <utility>
45
46/** @name Error codes for tuning. */
47/**@{*/
48/** charge assignment order too large for mesh size */
49static auto constexpr P3M_TUNE_CAO_TOO_LARGE = 1.;
50/** conflict with ELC gap size */
51static auto constexpr P3M_TUNE_ELC_GAP_SIZE = 2.;
52/** could not achieve target accuracy */
53static auto constexpr P3M_TUNE_ACCURACY_TOO_LARGE = 3.;
54/**@}*/
55
56/** @brief Precision threshold for a non-zero real-space cutoff. */
57static auto constexpr P3M_RCUT_PREC = 1e-3;
58
60 auto const &box_geo = *m_system.box_geo;
61 auto const &local_geo = *m_system.local_geo;
62 auto const verlet_skin = m_system.cell_structure->get_verlet_skin();
63 auto const r_cut_iL = get_params().r_cut_iL;
64 if (r_cut_iL == 0.) {
65 auto const min_box_l = *boost::min_element(box_geo.length());
66 auto const min_local_box_l = *boost::min_element(local_geo.length());
67 m_r_cut_iL_min = 0.;
68 m_r_cut_iL_max = std::min(min_local_box_l, min_box_l / 2.) - verlet_skin;
69 m_r_cut_iL_min *= box_geo.length_inv()[0];
70 m_r_cut_iL_max *= box_geo.length_inv()[0];
71 } else {
72 m_r_cut_iL_min = m_r_cut_iL_max = r_cut_iL;
73 m_logger->report_fixed_r_cut_iL(r_cut_iL);
74 }
75}
76
78 assert(initial_cao >= 1 and initial_cao <= 7);
79 auto const cao = get_params().cao;
80 if (cao == -1) {
81 cao_min = 1;
82 cao_max = 7;
83 cao_best = initial_cao;
84 } else {
85 cao_min = cao_max = cao_best = cao;
86 m_logger->report_fixed_cao(cao);
87 }
88}
89
90void TuningAlgorithm::commit(Utils::Vector3i const &mesh, int cao,
91 double r_cut_iL, double alpha_L) {
92 auto const &box_geo = *m_system.box_geo;
93 auto &p3m_params = get_params();
94 p3m_params.r_cut = r_cut_iL * box_geo.length()[0];
95 p3m_params.r_cut_iL = r_cut_iL;
96 p3m_params.cao = cao;
97 p3m_params.alpha_L = alpha_L;
98 p3m_params.alpha = alpha_L * box_geo.length_inv()[0];
99 p3m_params.mesh = mesh;
100}
101
102/**
103 * @brief Get the optimal alpha and the corresponding computation time
104 * for a fixed @p mesh and @p cao.
105 *
106 * The @p tuned_r_cut_iL is determined via a simple bisection.
107 *
108 * @param[in] mesh @copybrief P3MParameters::mesh
109 * @param[in] cao @copybrief P3MParameters::cao
110 * @param[in,out] tuned_r_cut_iL @copybrief P3MParameters::r_cut_iL
111 * @param[in,out] tuned_alpha_L @copybrief P3MParameters::alpha_L
112 * @param[in,out] tuned_accuracy @copybrief P3MParameters::accuracy
113 *
114 * @returns The integration time in case of success, otherwise
115 * -@ref P3M_TUNE_ACCURACY_TOO_LARGE,
116 * -@ref P3M_TUNE_CAO_TOO_LARGE, or -@ref P3M_TUNE_ELC_GAP_SIZE
117 */
119 double &tuned_r_cut_iL,
120 double &tuned_alpha_L,
121 double &tuned_accuracy) {
122 auto const &box_geo = *m_system.box_geo;
123 auto const &local_geo = *m_system.local_geo;
124 auto const verlet_skin = m_system.cell_structure->get_verlet_skin();
125 auto const target_accuracy = get_params().accuracy;
126 double rs_err, ks_err;
127 double r_cut_iL_min = m_r_cut_iL_min;
128 double r_cut_iL_max = m_r_cut_iL_max;
129
130 /* initial checks. */
131 auto const k_cut_per_dir = (static_cast<double>(cao) / 2.) *
132 Utils::hadamard_division(box_geo.length(), mesh);
133 auto const k_cut = *boost::min_element(k_cut_per_dir);
134 auto const min_box_l = *boost::min_element(box_geo.length());
135 auto const min_local_box_l = *boost::min_element(local_geo.length());
136 auto const k_cut_max = std::min(min_box_l, min_local_box_l) - verlet_skin;
137
138 if (cao >= *boost::min_element(mesh) or k_cut >= k_cut_max) {
139 m_logger->log_cao_too_large(mesh[0], cao);
141 }
142
143 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
144 calculate_accuracy(mesh, cao, r_cut_iL_max);
145
146 /* Either low and high boundary are equal (for fixed cut), or the low border
147 is initially 0 and therefore
148 has infinite error estimate, as required. Therefore if the high boundary
149 fails, there is no possible r_cut */
150 if (tuned_accuracy > target_accuracy) {
151 m_logger->log_skip("accuracy not achieved", mesh[0], cao, r_cut_iL_max,
152 tuned_alpha_L, tuned_accuracy, rs_err, ks_err);
154 }
155
156 double r_cut_iL, accuracy;
157 for (;;) {
158 r_cut_iL = 0.5 * (r_cut_iL_min + r_cut_iL_max);
159
160 if (r_cut_iL_max - r_cut_iL_min < P3M_RCUT_PREC)
161 break;
162
163 /* bisection */
164 std::tie(accuracy, rs_err, ks_err, tuned_alpha_L) =
165 calculate_accuracy(mesh, cao, r_cut_iL);
166 if (accuracy > target_accuracy)
167 r_cut_iL_min = r_cut_iL;
168 else
169 r_cut_iL_max = r_cut_iL;
170 }
171
172 /* final result is always the upper interval boundary, since only there
173 * we know that the desired minimal accuracy is obtained */
174 tuned_r_cut_iL = r_cut_iL = r_cut_iL_max;
175
176 /* if we are running P3M+ELC, check that r_cut is compatible */
177 auto const r_cut = r_cut_iL * box_geo.length()[0];
178 auto const veto = layer_correction_veto_r_cut(r_cut);
179 if (veto) {
180 m_logger->log_skip(*veto, mesh[0], cao, r_cut_iL, tuned_alpha_L,
181 tuned_accuracy, rs_err, ks_err);
182 return -P3M_TUNE_ELC_GAP_SIZE;
183 }
184
185 commit(mesh, cao, r_cut_iL, tuned_alpha_L);
187 auto const int_time = benchmark_integration_step(m_system, m_timings);
188
189 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
190 calculate_accuracy(mesh, cao, r_cut_iL);
191
192 m_logger->log_success(int_time, mesh[0], cao, r_cut_iL, tuned_alpha_L,
193 tuned_accuracy, rs_err, ks_err);
195 return int_time;
196}
197
198/**
199 * @brief Get the optimal alpha and the corresponding computation time
200 * for a fixed @p mesh.
201 *
202 * @p _cao should contain an initial guess, which is then adapted by stepping
203 * up and down.
204 *
205 * @param[in] mesh @copybrief P3MParameters::mesh
206 * @param[in,out] tuned_cao initial guess for the
207 * @copybrief P3MParameters::cao
208 * @param[out] tuned_r_cut_iL @copybrief P3MParameters::r_cut_iL
209 * @param[out] tuned_alpha_L @copybrief P3MParameters::alpha_L
210 * @param[out] tuned_accuracy @copybrief P3MParameters::accuracy
211 *
212 * @returns The integration time in case of success, otherwise
213 * -@ref P3M_TUNE_CAO_TOO_LARGE
214 */
215double TuningAlgorithm::get_m_time(Utils::Vector3i const &mesh, int &tuned_cao,
216 double &tuned_r_cut_iL,
217 double &tuned_alpha_L,
218 double &tuned_accuracy) {
219 double best_time = -1., tmp_r_cut_iL = 0., tmp_alpha_L = 0.,
220 tmp_accuracy = 0.;
221 /* in which direction improvement is possible. Initially, we don't know it
222 * yet. */
223 int final_dir = 0;
224 int cao = tuned_cao;
225
226 /* the initial step sets a timing mark. If there is no valid r_cut, we can
227 * only try to increase cao to increase the obtainable precision of the far
228 * formula. */
229 double tmp_time;
230 do {
231 tmp_time = get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
232 /* cao is too large for this grid, but still the accuracy cannot be
233 * achieved, give up */
234 if (tmp_time == -P3M_TUNE_CAO_TOO_LARGE) {
235 return tmp_time;
236 }
237 /* we have a valid time, start optimising from there */
238 if (tmp_time >= 0.) {
239 best_time = tmp_time;
240 tuned_r_cut_iL = tmp_r_cut_iL;
241 tuned_alpha_L = tmp_alpha_L;
242 tuned_accuracy = tmp_accuracy;
243 tuned_cao = cao;
244 break;
245 }
246 /* the required accuracy could not be obtained, try higher caos */
247 cao++;
248 final_dir = 1;
249 } while (cao <= cao_max);
250 /* with this mesh, the required accuracy cannot be obtained. */
251 if (cao > cao_max)
253
254 /* at the boundaries, only the opposite direction can be used for
255 * optimisation
256 */
257 if (cao == cao_min)
258 final_dir = 1;
259 else if (cao == cao_max)
260 final_dir = -1;
261
262 if (final_dir == 0) {
263 /* check in which direction we can optimise. Both directions are possible */
264 double dir_times[3];
265 for (final_dir = -1; final_dir <= 1; final_dir += 2) {
266 dir_times[final_dir + 1] = tmp_time = get_mc_time(
267 mesh, cao + final_dir, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
268 /* in this direction, we cannot optimise, since we get into precision
269 * trouble */
270 if (tmp_time < 0.)
271 continue;
272
273 if (tmp_time < best_time) {
274 best_time = tmp_time;
275 tuned_r_cut_iL = tmp_r_cut_iL;
276 tuned_alpha_L = tmp_alpha_L;
277 tuned_accuracy = tmp_accuracy;
278 tuned_cao = cao + final_dir;
279 }
280 }
281 /* choose the direction which was optimal, if any of the two */
282 if (dir_times[0] == best_time) {
283 final_dir = -1;
284 } else if (dir_times[2] == best_time) {
285 final_dir = 1;
286 } else {
287 /* no improvement in either direction, however if one is only marginally
288 * worse, we can still try; down is possible and not much worse, while
289 * up is either illegal or even worse */
290 if ((dir_times[0] >= 0 && dir_times[0] < best_time + time_granularity) &&
291 (dir_times[2] < 0 || dir_times[2] > dir_times[0]))
292 final_dir = -1;
293 /* same for up */
294 else if ((dir_times[2] >= 0 &&
295 dir_times[2] < best_time + time_granularity) &&
296 (dir_times[0] < 0 || dir_times[0] > dir_times[2]))
297 final_dir = 1;
298 else {
299 /* really no chance for optimisation */
300 return best_time;
301 }
302 }
303 /* we already checked the initial cao and its neighbor */
304 cao += 2 * final_dir;
305 } else {
306 /* here some constraint is active, and we only checked the initial cao
307 * itself */
308 cao += final_dir;
309 }
310
311 /* move cao into the optimisation direction until we do not gain anymore. */
312 for (; cao >= cao_min && cao <= cao_max; cao += final_dir) {
313 tmp_time = get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
314 /* if we cannot meet the precision anymore, give up */
315 if (tmp_time < 0.)
316 break;
317
318 if (tmp_time < best_time) {
319 best_time = tmp_time;
320 tuned_r_cut_iL = tmp_r_cut_iL;
321 tuned_alpha_L = tmp_alpha_L;
322 tuned_accuracy = tmp_accuracy;
323 tuned_cao = cao;
324 } else if (tmp_time > best_time + time_granularity) {
325 /* no hope of further optimisation */
326 break;
327 }
328 }
329 return best_time;
330}
331
332#endif // P3M or DP3M
static auto constexpr P3M_TUNE_ACCURACY_TOO_LARGE
could not achieve target accuracy
static auto constexpr P3M_RCUT_PREC
Precision threshold for a non-zero real-space cutoff.
static auto constexpr P3M_TUNE_ELC_GAP_SIZE
conflict with ELC gap size
static auto constexpr P3M_TUNE_CAO_TOO_LARGE
charge assignment order too large for mesh size
std::shared_ptr< LocalBox > local_geo
std::shared_ptr< CellStructure > cell_structure
std::shared_ptr< BoxGeometry > box_geo
double get_m_time(Utils::Vector3i const &mesh, int &tuned_cao, double &tuned_r_cut_iL, double &tuned_alpha_L, double &tuned_accuracy)
Get the optimal alpha and the corresponding computation time for a fixed mesh.
System::System & m_system
virtual void on_solver_change() const =0
Re-initialize the currently active solver.
double get_mc_time(Utils::Vector3i const &mesh, int cao, double &tuned_r_cut_iL, double &tuned_alpha_L, double &tuned_accuracy)
Get the optimal alpha and the corresponding computation time for a fixed mesh and cao.
virtual std::tuple< double, double, double, double > calculate_accuracy(Utils::Vector3i const &mesh, int cao, double r_cut_iL) const =0
Get the minimal error for this combination of parameters.
virtual std::optional< std::string > layer_correction_veto_r_cut(double r_cut) const =0
Veto real-space cutoffs larger than the layer correction gap.
void commit(Utils::Vector3i const &mesh, int cao, double r_cut_iL, double alpha_L)
Write tuned parameters to the P3M parameter struct.
void determine_cao_limits(int initial_cao)
Determine a sensible range for the charge assignment order.
void determine_r_cut_limits()
Determine a sensible range for the real-space cutoff.
virtual P3MParameters & get_params()=0
Get the P3M parameters.
std::unique_ptr< TuningLogger > m_logger
static auto constexpr time_granularity
Granularity of the time measurement (milliseconds).
Common functions for dipolar and charge P3M.
This file contains the defaults for ESPResSo.
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
Definition Vector.hpp:407
double r_cut_iL
cutoff radius for real space electrostatics (>0), rescaled to r_cut_iL = r_cut * box_l_i.
Definition common.hpp:75
int cao
charge assignment order ([0,7]).
Definition common.hpp:82
double accuracy
accuracy of the actual parameter set.
Definition common.hpp:84
double benchmark_integration_step(System::System &system, int int_steps)
Benchmark the integration loop.
Definition tuning.cpp:75