TRIQS/triqs_cthyb 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
auto_corr_time.hpp
1/*******************************************************************************
2 *
3 * TRIQS: a Toolbox for Research in Interacting Quantum Systems
4 *
5 * Copyright (C) 2021, Simons Foundation
6 * author: N. Wentzell
7 *
8 * TRIQS is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software
10 * Foundation, either version 3 of the License, or (at your option) any later
11 * version.
12 *
13 * TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * TRIQS. If not, see <http://www.gnu.org/licenses/>.
20 *
21 ******************************************************************************/
22#pragma once
23
24#include <triqs/stat/log_binning.hpp>
25
26#include "../qmc_data.hpp"
27
28#include <cmath>
29
30namespace triqs_cthyb {
31
33 struct measure_auto_corr_time {
34
35 measure_auto_corr_time(qmc_data const &_data, double &_auto_corr_time, bool &_auto_corr_time_converged)
36 : data(_data), auto_corr_time(_auto_corr_time), auto_corr_time_converged(_auto_corr_time_converged) {}
37
38 void accumulate(mc_weight_t sign) {
39 log_accs[0] << sign;
40 log_accs[1] << data.config.size();
41 }
42
43 void collect_results(mpi::communicator const &comm) {
44
45 // tau saturates at large bin size. We report it at the deepest bin level with >= min_samples effective
46 // samples and flag it as a lower bound if its finer neighbour (half the bin size) is more than n_sigma
47 // errors below it. The error of tau from M samples is d_tau = (tau + 1/2) * sqrt(2 / (M - 1)) (variance-
48 // of-a-variance); the deeper level dominates the uncertainty, so we use its d_tau as the yardstick.
49 constexpr int min_samples = 64; // report level: ~9% error on the error bar
50 constexpr double n_sigma = 2.0; // flag as "rising" if tau grows by > n_sigma errors per bin doubling (~95%)
51
52 // mean_errors_and_taus all-reduces internally, so every rank obtains the same result.
53 auto_corr_time = 0.0;
54 auto_corr_time_converged = true;
55
56 auto d_tau = [](double tau, long M) { return (tau + 0.5) * std::sqrt(2.0 / static_cast<double>(M - 1)); };
57
58 for (auto &log_acc : log_accs) {
59 auto [mean, errs, taus, effs] = log_acc.mean_errors_and_taus(comm, min_samples);
60
61 // No bin level reached min_samples effective samples: the run is too short to estimate tau at all,
62 // so we cannot claim it has saturated.
63 if (taus.empty()) {
64 auto_corr_time_converged = false;
65 continue;
66 }
67
68 // Zero variance (e.g. the sign in a sign-problem-free run): tau is genuinely ~0, treat as saturated.
69 if (!std::isfinite(taus.back())) continue;
70
71 double const tau_a = taus.back();
72 auto_corr_time = std::max(auto_corr_time, tau_a);
73
74 // Compare against the finer neighbour to check whether tau has saturated.
75 auto const n = taus.size();
76 if (n < 2 || !std::isfinite(taus[n - 2])) {
77 auto_corr_time_converged = false; // no neighbour to confirm saturation
78 continue;
79 }
80 double const tau_b = taus[n - 2];
81 if (tau_a - tau_b > n_sigma * d_tau(tau_a, effs[n - 1])) auto_corr_time_converged = false; // still rising -> lower bound
82 }
83 }
84
85 private:
86 qmc_data const &data;
87 double &auto_corr_time;
88 bool &auto_corr_time_converged;
89
90 // One complex log-binning accumulator per observable (partition-function sign and perturbation order).
91 std::vector<triqs::stat::log_binning<dcomplex>> log_accs = {2, {dcomplex{0.0}, -1}};
92 };
93
94} // namespace triqs_cthyb