TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_move_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_move.hpp"
30
31#include <fmt/format.h>
32#include <h5/h5.hpp>
33
34#include <cassert>
35#include <cstddef>
36#include <complex>
37#include <functional>
38#include <map>
39#include <stdexcept>
40#include <string>
41#include <utility>
42#include <vector>
43
44namespace triqs::mc_tools {
45
63 template <DoubleOrComplex MCSignType> class move_set {
64 public:
69 move_set(random_generator &rng) : rng_(rng) {}
70
72 move_set(move_set const &) = delete;
73
75 move_set &operator=(move_set const &) = delete;
76
78 move_set(move_set &&) = default;
79
81 move_set &operator=(move_set &&) = default;
82
96 template <typename T>
97 requires(MCMove<T, MCSignType>)
98 void add(T &&m, std::string name, double weight) {
99 if (weight < 0.0) throw std::runtime_error(fmt::format("Error in move_set: Weight of move {} is negative: {} < 0", name, weight));
100 moves_.emplace_back(std::forward<T>(m));
101 weights_.emplace_back(weight);
102 names_.emplace_back(std::move(name));
103 initialize();
104 }
105
112 double attempt();
113
119 MCSignType accept() { return moves_[current_].accept() * attempt_sign_; }
120
125 void reject() { moves_[current_].reject(); }
126
131 void clear_statistics();
132
138 void collect_statistics(mpi::communicator const &c);
139
145 void calibrate(mpi::communicator const &c);
146
151 [[nodiscard]] std::map<std::string, double> get_acceptance_rates() const;
152
159 [[nodiscard]] std::string get_statistics(std::string const &prefix = "") const;
160
166 [[nodiscard]] std::string get_timings(std::string const &prefix = "") const;
167
169 [[nodiscard]] double total_duration() const {
170 double total = 0.0;
171 for (auto const &m : moves_) total += m.duration();
172 return total;
173 }
174
176 [[nodiscard]] auto current() const { return current_; }
177
179 [[nodiscard]] auto const &probabilities() const { return probs_; }
180
182 [[nodiscard]] auto attempt_sign() const { return attempt_sign_; }
183
185 [[nodiscard]] static std::string hdf5_format() { return "move_set"; }
186
196 friend void h5_write(h5::group g, std::string const &name, move_set const &ms) {
197 auto gr = g.create_group(name);
198 h5::write_hdf5_format(gr, ms);
199 for (std::size_t i = 0; i < ms.moves_.size(); ++i) h5::write(gr, ms.names_[i], ms.moves_[i]);
200 }
201
211 friend void h5_read(h5::group g, std::string const &name, move_set &ms) {
212 auto gr = g.open_group(name);
213 h5::assert_hdf5_format(gr, ms);
214 for (std::size_t i = 0; i < ms.moves_.size(); ++i) h5::read(gr, ms.names_[i], ms.moves_[i]);
215 }
216
217 private:
218 // Normalize the weights and calculate the accumulated probabilities.
219 void initialize();
220
221 // Check the acceptance ratio, handle possible infinites, set the attemped sign and return the absolute value.
222 double check_ratio(MCSignType ratio);
223
224 private:
225 std::vector<move<MCSignType>> moves_;
226 std::vector<std::string> names_;
227 std::size_t current_{0};
228 std::reference_wrapper<random_generator> rng_;
229 std::vector<double> weights_;
230 std::vector<double> probs_;
231 std::vector<double> acc_probs_;
232 MCSignType attempt_sign_{1.0};
233 };
234
235 // Explicit template instantiation declarations.
236 extern template class move_set<double>;
237 extern template class move_set<std::complex<double>>;
238
239} // namespace triqs::mc_tools
static std::string hdf5_format()
Get the HDF5 format tag.
friend void h5_write(h5::group g, std::string const &name, move_set const &ms)
Write the move set object to HDF5.
std::map< std::string, double > get_acceptance_rates() const
Get the acceptance rates of all moves in the set.
move_set(move_set &&)=default
Default move constructor.
move_set & operator=(move_set &&)=default
Default move assignment operator.
std::string get_statistics(std::string const &prefix="") const
Get a formatted string showing the acceptance rates of all moves.
auto current() const
Get the index of the current move.
void clear_statistics()
Clear the statistics of all the moves in the set.
move_set(random_generator &rng)
Construct a move set with a given random number generator (stored in a std::reference_wrapper).
move_set & operator=(move_set const &)=delete
Deleted copy assignment operator.
auto const & probabilities() const
Get the probabilities with which the moves are selected.
void calibrate(mpi::communicator const &c)
Calibrate all the moves in the set.
friend void h5_read(h5::group g, std::string const &name, move_set &ms)
Read the move set object from HDF5.
auto attempt_sign() const
Get the sign of the acceptance ratio of the last attempt.
move_set(move_set const &)=delete
Deleted copy constructor.
MCSignType accept()
Accept the new MC configuration.
void reject()
Reject the new MC configuration.
void collect_statistics(mpi::communicator const &c)
Collect statistics for all the moves in the set from multiple MPI processes.
double attempt()
Propose a new MC configuration.
std::string get_timings(std::string const &prefix="") const
Get a formatted string with the timings of all moves.
double total_duration() const
Get the total duration of all moves.
void add(T &&m, std::string name, double weight)
Add a new move to the set with a given name and weight.
Random number generator with a selectable underlying engine.
Check if a type can be used as a MC move.
Definition concepts.hpp:59
Provides type erasure for MC moves.
Provides concepts for the MC tools.
Provides a type erased random number generator.