TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_measure_set.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./concepts.hpp"
28#include "./mc_measure.hpp"
29
30#include <fmt/format.h>
31#include <h5/h5.hpp>
32#include <mpi/communicator.hpp>
33
34#include <cassert>
35#include <complex>
36#include <map>
37#include <stdexcept>
38#include <string>
39#include <utility>
40#include <vector>
41
42namespace triqs::mc_tools {
43
58 template <DoubleOrComplex MCSignType> class measure_set {
59 public:
61 using measure_map_t = std::map<std::string, measure<MCSignType>>;
62
64 using measure_itr_t = measure_map_t::iterator;
65
67 measure_set() = default;
68
70 measure_set(measure_set const &) = delete;
71
73 measure_set &operator=(measure_set const &) = delete;
74
76 measure_set(measure_set &&) = default;
77
80
92 template <typename T>
94 measure_itr_t insert(T &&m, std::string name, bool enable_timer, bool enable_report) {
95 if (has(name)) throw std::runtime_error(fmt::format("Error in measure_set: Measure with name {} already exists", name));
96 return measures_.emplace(name, measure<MCSignType>{std::forward<T>(m), enable_timer, enable_report}).first;
97 }
98
103 void remove(measure_itr_t const &it) { measures_.erase(it); }
104
106 void clear() { measures_.clear(); }
107
113 [[nodiscard]] bool has(std::string const &name) const { return measures_.find(name) != measures_.end(); }
114
119 [[nodiscard]] std::vector<std::string> names() const;
120
126 void accumulate(MCSignType sign) {
127 for (auto &[name, m] : measures_) m.accumulate(sign);
128 }
129
135 void collect_results(const mpi::communicator &c);
136
142 [[nodiscard]] std::string report() const;
143
152 [[nodiscard]] std::string get_timings(std::string const &prefix = "") const;
153
155 [[nodiscard]] double total_duration() const {
156 double total = 0.0;
157 for (auto const &[name, m] : measures_) total += m.duration();
158 return total;
159 }
160
162 [[nodiscard]] static std::string hdf5_format() { return "measure_set"; }
163
173 friend void h5_write(h5::group g, std::string const &key, measure_set const &ms) {
174 auto gr = g.create_group(key);
175 h5::write_hdf5_format(gr, ms);
176 for (auto const &[name, m] : ms.measures_) h5::write(gr, name, m);
177 }
178
188 friend void h5_read(h5::group g, std::string const &key, measure_set &ms) {
189 auto gr = g.open_group(key);
190 h5::assert_hdf5_format(gr, ms);
191 for (auto &[name, m] : ms.measures_) h5::read(gr, name, m);
192 }
193
194 private:
195 measure_map_t measures_;
196 };
197
198 // Explicit template instantiation declarations.
199 extern template class measure_set<double>;
200 extern template class measure_set<std::complex<double>>;
201
202} // namespace triqs::mc_tools
friend void h5_write(h5::group g, std::string const &key, measure_set const &ms)
Write the measure set object to HDF5.
friend void h5_read(h5::group g, std::string const &key, measure_set &ms)
Read the measure set object from HDF5.
std::vector< std::string > names() const
Get a vector of all the measure names.
void clear()
Remove all measures from the set.
measure_set(measure_set &&)=default
Default move constructor.
measure_itr_t insert(T &&m, std::string name, bool enable_timer, bool enable_report)
Add a new measure to the set with a given name.
void collect_results(const mpi::communicator &c)
Collect results from all the measures in the measure set from multiple MPI processes.
measure_set(measure_set const &)=delete
Deleted copy constructor.
measure_set & operator=(measure_set const &)=delete
Deleted copy assignment operator.
std::string get_timings(std::string const &prefix="") const
Get a formatted string with the timings of all measures.
static std::string hdf5_format()
Get the HDF5 format tag.
measure_set & operator=(measure_set &&)=default
Default move assignment operator.
double total_duration() const
Get the total duration of all measures.
measure_set()=default
Default constructor.
bool has(std::string const &name) const
Check if a measure with the given name is registered.
measure_map_t::iterator measure_itr_t
Iterator type for the measure set.
void remove(measure_itr_t const &it)
Remove the measure at the given iterator from the set.
std::string report() const
Report information about the measures in the set.
void accumulate(MCSignType sign)
Perform all measurements in the set.
std::map< std::string, measure< MCSignType > > measure_map_t
Map type used for storing the measures.
Type erasure class for MC measures.
Check if a type can be used as a MC measure.
Definition concepts.hpp:77
Provides type erasure for MC measurements.
Provides concepts for the MC tools.