24#if defined(P3M) || defined(DP3M)
34#include "communication.hpp"
35#include "system/System.hpp"
65 auto const min_box_l = std::ranges::min(box_geo.length());
66 auto const min_local_box_l = std::ranges::min(local_geo.length());
68 m_r_cut_iL_max = std::min(min_local_box_l, min_box_l / 2.) - verlet_skin;
73 m_logger->report_fixed_r_cut_iL(r_cut_iL);
78 assert(initial_cao >= 1 and initial_cao <= 7);
91 double r_cut_iL,
double alpha_L) {
94 p3m_params.r_cut = r_cut_iL * box_geo.length()[0];
95 p3m_params.r_cut_iL = r_cut_iL;
97 p3m_params.alpha_L = alpha_L;
98 p3m_params.alpha = alpha_L * box_geo.length_inv()[0];
99 p3m_params.mesh = mesh;
119 double &tuned_r_cut_iL,
120 double &tuned_alpha_L,
121 double &tuned_accuracy) {
126 double rs_err, ks_err;
131 auto const k_cut_per_dir = (
static_cast<double>(cao) / 2.) *
133 auto const k_cut = std::ranges::min(k_cut_per_dir);
134 auto const min_box_l = std::ranges::min(box_geo.length());
135 auto const min_local_box_l = std::ranges::min(local_geo.length());
136 auto const k_cut_max = std::min(min_box_l, min_local_box_l) - verlet_skin;
138 if (cao >= std::ranges::min(mesh) or k_cut >= k_cut_max) {
139 m_logger->log_cao_too_large(mesh[0], cao);
143 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
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);
156 double r_cut_iL, accuracy;
158 r_cut_iL = 0.5 * (r_cut_iL_min + r_cut_iL_max);
164 std::tie(accuracy, rs_err, ks_err, tuned_alpha_L) =
166 if (accuracy > target_accuracy)
167 r_cut_iL_min = r_cut_iL;
169 r_cut_iL_max = r_cut_iL;
174 tuned_r_cut_iL = r_cut_iL = r_cut_iL_max;
176 auto const report_veto = [&](
auto const &veto) {
178 m_logger->log_skip(*veto, mesh[0], cao, r_cut_iL, tuned_alpha_L,
179 tuned_accuracy, rs_err, ks_err);
181 return static_cast<bool>(veto);
185 auto const r_cut = r_cut_iL * box_geo.length()[0];
190 commit(mesh, cao, r_cut_iL, tuned_alpha_L);
197 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
200 m_logger->log_success(int_time, mesh[0], cao, r_cut_iL, tuned_alpha_L,
201 tuned_accuracy, rs_err, ks_err);
224 double &tuned_r_cut_iL,
225 double &tuned_alpha_L,
226 double &tuned_accuracy) {
227 double best_time = -1., tmp_r_cut_iL = 0., tmp_alpha_L = 0.,
239 tmp_time =
get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
246 if (tmp_time >= 0.) {
247 best_time = tmp_time;
248 tuned_r_cut_iL = tmp_r_cut_iL;
249 tuned_alpha_L = tmp_alpha_L;
250 tuned_accuracy = tmp_accuracy;
270 if (final_dir == 0) {
273 for (final_dir = -1; final_dir <= 1; final_dir += 2) {
275 mesh, cao + final_dir, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
281 if (tmp_time < best_time) {
282 best_time = tmp_time;
283 tuned_r_cut_iL = tmp_r_cut_iL;
284 tuned_alpha_L = tmp_alpha_L;
285 tuned_accuracy = tmp_accuracy;
286 tuned_cao = cao + final_dir;
290 if (dir_times[0] == best_time) {
292 }
else if (dir_times[2] == best_time) {
299 (dir_times[2] < 0 || dir_times[2] > dir_times[0]))
302 else if ((dir_times[2] >= 0 &&
304 (dir_times[0] < 0 || dir_times[0] > dir_times[2]))
312 cao += 2 * final_dir;
321 tmp_time =
get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
326 if (tmp_time < best_time) {
327 best_time = tmp_time;
328 tuned_r_cut_iL = tmp_r_cut_iL;
329 tuned_alpha_L = tmp_alpha_L;
330 tuned_accuracy = tmp_accuracy;
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_FFT_MESH_SIZE
conflict with FFT domain decomposition
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.
virtual std::optional< std::string > fft_decomposition_veto(Utils::Vector3i const &) const
Veto FFT decomposition in non-cubic boxes.
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.
void increment_n_trials()
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)
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.