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