24#if defined(P3M) || defined(DP3M)
34#include "communication.hpp"
35#include "system/System.hpp"
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());
66 m_r_cut_iL_max = std::min(min_local_box_l, min_box_l / 2.) - verlet_skin;
71 m_logger->report_fixed_r_cut_iL(r_cut_iL);
76 assert(initial_cao >= 1 and initial_cao <= 7);
89 double r_cut_iL,
double alpha_L) {
92 p3m_params.r_cut = r_cut_iL * box_geo.length()[0];
93 p3m_params.r_cut_iL = r_cut_iL;
95 p3m_params.alpha_L = alpha_L;
96 p3m_params.alpha = alpha_L * box_geo.length_inv()[0];
97 p3m_params.mesh = mesh;
117 double &tuned_r_cut_iL,
118 double &tuned_alpha_L,
119 double &tuned_accuracy) {
124 double rs_err, ks_err;
129 auto const k_cut_per_dir = (
static_cast<double>(cao) / 2.) *
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;
136 if (cao >= std::ranges::min(mesh) or k_cut >= k_cut_max) {
137 m_logger->log_cao_too_large(mesh[0], cao);
141 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
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);
154 double r_cut_iL, accuracy;
156 r_cut_iL = 0.5 * (r_cut_iL_min + r_cut_iL_max);
162 std::tie(accuracy, rs_err, ks_err, tuned_alpha_L) =
164 if (accuracy > target_accuracy)
165 r_cut_iL_min = r_cut_iL;
167 r_cut_iL_max = r_cut_iL;
172 tuned_r_cut_iL = r_cut_iL = r_cut_iL_max;
175 auto const r_cut = r_cut_iL * box_geo.length()[0];
178 m_logger->log_skip(*veto, mesh[0], cao, r_cut_iL, tuned_alpha_L,
179 tuned_accuracy, rs_err, ks_err);
183 commit(mesh, cao, r_cut_iL, tuned_alpha_L);
187 std::tie(tuned_accuracy, rs_err, ks_err, tuned_alpha_L) =
190 m_logger->log_success(int_time, mesh[0], cao, r_cut_iL, tuned_alpha_L,
191 tuned_accuracy, rs_err, ks_err);
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.,
229 tmp_time =
get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
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;
260 if (final_dir == 0) {
263 for (final_dir = -1; final_dir <= 1; final_dir += 2) {
265 mesh, cao + final_dir, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
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;
280 if (dir_times[0] == best_time) {
282 }
else if (dir_times[2] == best_time) {
289 (dir_times[2] < 0 || dir_times[2] > dir_times[0]))
292 else if ((dir_times[2] >= 0 &&
294 (dir_times[0] < 0 || dir_times[0] > dir_times[2]))
302 cao += 2 * final_dir;
311 tmp_time =
get_mc_time(mesh, cao, tmp_r_cut_iL, tmp_alpha_L, tmp_accuracy);
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;
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.
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.