TRIQS/triqs_ctseg 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
pert_order.cpp
1// Copyright (c) 2024--present, The Simons Foundation
2// Copyright (c) 2024--present, Max Planck Institute for Polymer Research, Mainz, Germany
3// This file is part of TRIQS/ctseg and is licensed under the terms of GPLv3 or later.
4// SPDX-License-Identifier: GPL-3.0-or-later
5// See LICENSE in the root of this distribution for details.
6
7#include "./pert_order.hpp"
8#include "../logs.hpp"
9
10namespace triqs_ctseg::measures {
11
12 pert_order::pert_order(std::function<int()> get_order, std::optional<std::vector<double>> &hist_opt,
13 std::optional<double> &average_order_opt)
14 : get_order{get_order}, hist{hist_opt.emplace(4, 0.0)}, average_order{average_order_opt.emplace(0.0)} {}
15
16 // -------------------------------------
17
18 void pert_order::accumulate(double) {
19 auto order = get_order();
20 while (order >= hist.size()) hist.resize(2 * hist.size());
21 hist[order] += 1;
22 ++N;
23 }
24
25 // -------------------------------------
26
27 void pert_order::collect_results(mpi::communicator const &c) {
28 N = mpi::all_reduce(N, c);
29
30 // Make sure that all mpi threads have an equally sized hist
31 auto max_size = mpi::all_reduce(hist.size(), c, MPI_MAX);
32 hist.resize(max_size, 0.0);
33
34 // Reduce hist over mpi threads
35 hist = mpi::all_reduce(hist, c);
36
37 // Normalize and Calculate average order
38 for (int order : range(hist.size())) {
39 hist[order] /= N;
40 average_order += hist[order] * order;
41 }
42 }
43
44} // namespace triqs_ctseg::measures