TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_move.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 "../utility/timer.hpp"
29
30#include <h5/h5.hpp>
31#include <mpi/communicator.hpp>
32
33#include <complex>
34#include <cstdint>
35#include <map>
36#include <memory>
37#include <string>
38#include <type_traits>
39#include <utility>
40
41namespace triqs::mc_tools {
42
43 // Forward declaration.
44 template <DoubleOrComplex MCSignType> class move_set;
45
68 template <DoubleOrComplex MCSignType> class move {
69 private:
70 // MC move concept defines the interface for MC moves.
71 struct move_concept {
72 virtual ~move_concept() = default;
73 virtual MCSignType attempt() = 0;
74 virtual MCSignType accept() = 0;
75 virtual void reject() = 0;
76 virtual void calibrate(mpi::communicator const &) = 0;
77 virtual void collect_statistics(mpi::communicator const &) = 0;
78 virtual void h5write(h5::group, std::string const &) const = 0;
79 virtual void h5read(h5::group, std::string const &) = 0;
80
81 // Methods only implemented in move_set.
82 virtual void ms_clear_statistics() = 0;
83 [[nodiscard]] virtual std::string ms_get_statistics(std::string const &) const = 0;
84 [[nodiscard]] virtual std::map<std::string, double> ms_get_acceptance_rates() const = 0;
85 [[nodiscard]] virtual std::string ms_get_timings(std::string const &) const = 0;
86 };
87
88 // MC move model implements the MC move concept by calling the appropriate methods of the type erased object.
89 template <typename T>
91 struct move_model : public move_concept {
92 static constexpr bool is_move_set = std::is_same_v<T, move_set<MCSignType>>;
93 T move_;
94 move_model(T m) : move_{std::move(m)} {}
95 MCSignType attempt() override { return move_.attempt(); }
96 MCSignType accept() override { return move_.accept(); }
97 void reject() override {
98 if constexpr (requires { move_.reject(); }) move_.reject();
99 }
100 void calibrate(mpi::communicator const &c) override {
101 if constexpr (requires { move_.calibrate(c); }) move_.calibrate(c);
102 }
103 void collect_statistics(mpi::communicator const &c) override {
104 if constexpr (requires { move_.collect_statistics(c); }) move_.collect_statistics(c);
105 }
106 void h5write(h5::group g, std::string const &name) const override {
107 if constexpr (h5::Storable<T>) h5::write(g, name, move_);
108 }
109 void h5read(h5::group g, std::string const &name) override {
110 if constexpr (h5::Storable<T>) h5::read(g, name, move_);
111 }
112
113 // Methods only implemented in move_set.
114 void ms_clear_statistics() override {
115 if constexpr (is_move_set) move_.clear_statistics();
116 }
117 [[nodiscard]] std::string ms_get_statistics(std::string const &prefix) const override {
118 if constexpr (is_move_set) return move_.get_statistics(prefix);
119 return {};
120 }
121 [[nodiscard]] std::map<std::string, double> ms_get_acceptance_rates() const override {
122 if constexpr (is_move_set) return move_.get_acceptance_rates();
123 return {};
124 }
125 [[nodiscard]] std::string ms_get_timings(std::string const &prefix) const override {
126 if constexpr (is_move_set) return move_.get_timings(prefix);
127 return {};
128 }
129 };
130
131 public:
138 template <typename T>
139 requires(MCMove<T, MCSignType> && !std::is_same_v<T, move>)
140 move(T m) : ptr_{std::make_unique<move_model<T>>(std::move(m))}, is_move_set_{std::is_same_v<T, move_set<MCSignType>>} {}
141
143 move(move const &) = delete;
144
146 move &operator=(move const &) = delete;
147
149 move(move &&) = default;
150
152 move &operator=(move &&) = default;
153
158 MCSignType attempt() {
159 ++nprop_;
160 timer_.start();
161 auto result = ptr_->attempt();
162 timer_.stop();
163 return result;
164 }
165
170 MCSignType accept() {
171 ++nacc_;
172 timer_.start();
173 auto result = ptr_->accept();
174 timer_.stop();
175 return result;
176 }
177
182 void reject() {
183 timer_.start();
184 ptr_->reject();
185 timer_.stop();
186 }
187
193 void calibrate(mpi::communicator const &c) { ptr_->calibrate(c); }
194
203 void collect_statistics(mpi::communicator const &c);
204
206 [[nodiscard]] auto acceptance_rate() const { return acc_rate_; }
207
209 [[nodiscard]] auto n_proposed_config() const { return nprop_; }
210
212 [[nodiscard]] auto n_accepted_config() const { return nacc_; }
213
218 void clear_statistics();
219
227 [[nodiscard]] std::string get_statistics(std::string const &name, std::string const &prefix = "") const;
228
236 [[nodiscard]] std::string get_timings(std::string const &name, std::string const &prefix = "") const;
237
239 [[nodiscard]] double duration() const { return static_cast<double>(timer_); }
240
246 [[nodiscard]] std::map<std::string, double> get_acceptance_rates() const { return ptr_->ms_get_acceptance_rates(); }
247
249 [[nodiscard]] auto is_set() const { return is_move_set_; }
250
260 friend void h5_write(h5::group g, std::string const &name, move const &m) { m.ptr_->h5write(g, name); }
261
271 friend void h5_read(h5::group g, std::string const &name, move &m) { m.ptr_->h5read(g, name); }
272
273 private:
274 std::unique_ptr<move_concept> ptr_;
275 std::uint64_t nprop_{0};
276 std::uint64_t nacc_{0};
277 double acc_rate_{-1};
278 bool is_move_set_{false};
279 triqs::utility::timer timer_;
280 };
281
282 // Explicit template instantiation declarations.
283 extern template class move<double>;
284 extern template class move<std::complex<double>>;
285
286} // namespace triqs::mc_tools
Type erasure class for MC moves.
Definition mc_move.hpp:68
std::string get_timings(std::string const &name, std::string const &prefix="") const
Get a formatted string showing the runtime of the move.
Definition mc_move.cpp:61
auto acceptance_rate() const
Get the acceptance rate of the move. You need to call collect_statistics() first.
Definition mc_move.hpp:206
MCSignType accept()
Accept the new MC configuration.
Definition mc_move.hpp:170
void clear_statistics()
Reset the gathered statistics to their initial states.
Definition mc_move.cpp:44
std::string get_statistics(std::string const &name, std::string const &prefix="") const
Get a formatted string showing the statistics of the move (and other moves if it is a move set).
Definition mc_move.cpp:52
MCSignType attempt()
Propose a new MC configuration.
Definition mc_move.hpp:158
friend void h5_read(h5::group g, std::string const &name, move &m)
Read the move object from HDF5.
Definition mc_move.hpp:271
double duration() const
Get the duration of the cumulative attempt(), accept(), and reject() calls.
Definition mc_move.hpp:239
auto n_accepted_config() const
Get the number of accepted configurations, i.e. the number of times accept() has been called.
Definition mc_move.hpp:212
void reject()
Optional callback function if the proposed move is rejected.
Definition mc_move.hpp:182
move(move const &)=delete
Deleted copy constructor.
std::map< std::string, double > get_acceptance_rates() const
Get the acceptance rates of all moves in case it is a move set.
Definition mc_move.hpp:246
auto is_set() const
Is the move object a move set?
Definition mc_move.hpp:249
move(T m)
Constructor takes an object that models the triqs::mc_tools::MCMove concept and erases its type.
Definition mc_move.hpp:140
auto n_proposed_config() const
Get the number of proposed configurations, i.e. the number of times attempt() has been called.
Definition mc_move.hpp:209
move & operator=(move &&)=default
Default move assignment operator leaves the moved from object in an empty state.
void collect_statistics(mpi::communicator const &c)
Collect statistics from multiple MPI processes.
Definition mc_move.cpp:37
move & operator=(move const &)=delete
Deleted copy assignment operator.
move(move &&)=default
Default move constructor leaves the moved from object in an empty state.
void calibrate(mpi::communicator const &c)
Optional callback function to calibrate the move.
Definition mc_move.hpp:193
friend void h5_write(h5::group g, std::string const &name, move const &m)
Write the move object to HDF5.
Definition mc_move.hpp:260
Check if a type can be used as a MC move.
Definition concepts.hpp:59
Provides concepts for the MC tools.
A wall-clock timer that accumulates elapsed seconds across start/stop intervals.