TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_generic.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-2023 Simons Foundation
4// Copyright (c) 2017 Hugo U.R. Strand
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Michel Ferrero, Henri Menke, Olivier Parcollet, Priyanka Seth, Hugo U. R. Strand, Nils Wentzell, Thomas Ayral
20
25
26#pragma once
27
28#include "./concepts.hpp"
30#include "./mc_measure_set.hpp"
31#include "./mc_move_set.hpp"
34#include "../utility/timer.hpp"
36
37#include <h5/h5.hpp>
38
39#include <functional>
40#include <iostream>
41#include <map>
42#include <memory>
43#include <string>
44#include <utility>
45#include <vector>
46
47namespace triqs::mc_tools {
48
56 enum class mc_phase { warmup, accumulation };
57
96 template <DoubleOrComplex MCSignType> class mc_generic {
97 private:
98 // Value to indicate that the initial sign of the user-provided parameters should not be used.
99 static constexpr MCSignType default_initial_sign = std::numeric_limits<double>::infinity();
100
101 public:
103 struct run_param_t {
105 std::int64_t ncycles = -1;
106
108 std::int64_t cycle_length = 1;
109
114 std::function<bool()> stop_callback = []() { return false; };
115
120 MCSignType initial_sign = default_initial_sign;
121
123 mpi::communicator comm = mpi::communicator{};
124
131 std::function<void()> after_cycle_duty = []() {};
132
135
137 bool enable_measures = true;
138
140 bool enable_calibration = false;
141
144 mc_phase phase = mc_phase::accumulation;
145
159
162
165 };
166
174 mc_generic(const std::string &rng_name, int rng_seed, int verbosity_lvl)
175 : rng_(rng_name, rng_seed), moves_(rng_), report_(&std::cout, verbosity_lvl), verbosity_lvl_(verbosity_lvl) {}
176
178 void set_verbosity(int v) {
179 verbosity_lvl_ = v;
180 report_ = utility::report_stream(&std::cout, v);
181 }
182
184 [[nodiscard]] MCSignType get_sign() const { return sign_; }
185
194 template <typename T> void add_move(T &&m, std::string name, double weight = 1.0) { moves_.add(std::forward<T>(m), name, weight); }
195
206 template <typename T> auto add_measure(T &&m, std::string name, bool enable_timer = true, bool enable_report = false) {
207 return measures_.insert(std::forward<T>(m), name, enable_timer, enable_report);
208 }
209
216 template <typename T> void add_measure_aux(std::shared_ptr<T> const &m_ptr) { measures_aux_.emplace_back(m_ptr); }
217
222 void rm_measure(typename measure_set<MCSignType>::measure_itr_t const &it) { measures_.remove(it); }
223
225 void clear_measures() { measures_.clear(); }
226
234 int run(run_param_t const &params);
235
245 int warmup(run_param_t const &params);
246
257 int accumulate(run_param_t const &params);
258
271 int run(std::int64_t ncycles, std::int64_t cycle_length, std::function<bool()> stop_callback, bool enable_measures,
272 mpi::communicator c = mpi::communicator{}, bool enable_calibration = false);
273
285 int warmup(std::int64_t ncycles, std::int64_t cycle_length, std::function<bool()> stop_callback, MCSignType initial_sign,
286 mpi::communicator c = mpi::communicator{});
287
290 int warmup(std::int64_t ncycles, std::int64_t cycle_length, std::function<bool()> stop_callback, mpi::communicator c = mpi::communicator{});
291
302 int accumulate(std::int64_t ncycles, std::int64_t cycle_length, std::function<bool()> stop_callback, mpi::communicator c = mpi::communicator{});
303
316 int warmup_and_accumulate(std::int64_t ncycles_warmup, std::int64_t ncycles_acc, std::int64_t cycle_length, std::function<bool()> stop_callback,
317 MCSignType initial_sign, mpi::communicator c = mpi::communicator{});
318
321 int warmup_and_accumulate(std::int64_t ncycles_warmup, std::int64_t ncycles_acc, std::int64_t cycle_length, std::function<bool()> stop_callback,
322 mpi::communicator c = mpi::communicator{});
323
328 void collect_results(mpi::communicator const &c);
329
331 [[nodiscard]] auto get_run_params() const { return run_param_t{}; }
332
334 [[nodiscard]] std::map<std::string, double> get_acceptance_rates() const { return moves_.get_acceptance_rates(); }
335
337 [[nodiscard]] double get_percent() const { return percentage_done_; }
338
340 random_generator &get_rng() { return rng_; }
341
343 [[nodiscard]] int get_current_cycle_number() const { return ncycles_done_; }
344
346 [[nodiscard]] int get_config_id() const { return config_id_; }
347
349 [[nodiscard]] double get_duration() const { return get_total_time(); }
350
352 [[nodiscard]] int get_nmeasures() const { return nmeasures_done_; }
353
355 [[nodiscard]] double get_total_time() const { return get_warmup_time() + get_accumulation_time(); }
356
358 [[nodiscard]] double get_warmup_time() const { return static_cast<double>(warmup_timer_); }
359
361 [[nodiscard]] auto get_warmup_time_HHMMSS() const { return hours_minutes_seconds_from_seconds(warmup_timer_); }
362
364 [[nodiscard]] double get_accumulation_time() const { return static_cast<double>(acc_timer_); }
365
367 [[nodiscard]] std::string get_accumulation_time_HHMMSS() const { return hours_minutes_seconds_from_seconds(acc_timer_); }
368
379 friend void h5_write(h5::group g, std::string const &name, mc_generic const &mc) {
380 auto gr = g.create_group(name);
381 h5_write(gr, "moves", mc.moves_);
382 h5_write(gr, "measures", mc.measures_);
383 h5_write(gr, "number_cycle_done", mc.ncycles_done_);
384 h5_write(gr, "number_measure_done", mc.nmeasures_done_);
385 h5_write(gr, "sign", mc.sign_);
386 }
387
398 friend void h5_read(h5::group g, std::string const &name, mc_generic &mc) {
399 auto gr = g.open_group(name);
400 h5_read(gr, "moves", mc.moves_);
401 h5_read(gr, "measures", mc.measures_);
402 h5_read(gr, "number_cycle_done", mc.ncycles_done_);
403 h5_read(gr, "number_measure_done", mc.nmeasures_done_);
404 h5_read(gr, "sign", mc.sign_);
405 }
406
407 private:
408 // Print simulation info.
409 void print_sim_info(run_param_t const &params, std::int64_t cycle_counter);
410
411 // Do a single Metropolis accept/reject step.
412 void metropolis_step();
413
414 // Perform after-cycle duties: the user-provided duty and move calibration. Both may communicate,
415 // so this must be called the same number of times on every rank (it is skipped in overtime cycles).
416 void after_cycle_duties(run_param_t const &params);
417
418 // Perform all measurements and update their counter.
419 void do_measurements();
420
421 private:
422 random_generator rng_;
424 measure_set<MCSignType> measures_;
425 std::vector<measure_aux> measures_aux_;
427 utility::timer run_timer_;
428 utility::timer acc_timer_;
429 utility::timer warmup_timer_;
430 MCSignType sign_{1};
431 std::int64_t nmeasures_done_{0};
432 std::int64_t ncycles_done_{0};
433 double percentage_done_{0};
434 std::int64_t config_id_{0};
435 int verbosity_lvl_{0};
436 };
437
438 // Explicit template instantiation declarations.
439 extern template class mc_generic<double>;
440 extern template class mc_generic<std::complex<double>>;
441
442} // namespace triqs::mc_tools
Generic Monte Carlo class.
auto get_warmup_time_HHMMSS() const
Get the time spent in the warmup phase in hours, minutes, and seconds.
double get_duration() const
Get the total time, i.e. the sum of the warmup and accumulation times, in seconds.
double get_total_time() const
Get the total time, i.e. the sum of the warmup and accumulation times, in seconds.
int warmup_and_accumulate(std::int64_t ncycles_warmup, std::int64_t ncycles_acc, std::int64_t cycle_length, std::function< bool()> stop_callback, MCSignType initial_sign, mpi::communicator c=mpi::communicator{})
Run the warumup and accumulation phases of the MC simulation.
auto get_run_params() const
Get the default run parameters.
random_generator & get_rng()
Get a reference to the random number generator.
void add_measure_aux(std::shared_ptr< T > const &m_ptr)
Register a new auxiliary MC measure.
auto add_measure(T &&m, std::string name, bool enable_timer=true, bool enable_report=false)
Register a new MC measure.
void clear_measures()
Remove all registered measures.
double get_percent() const
Get the percentage of the requested number of cycles done.
void add_move(T &&m, std::string name, double weight=1.0)
Register a new MC move.
double get_accumulation_time() const
Get the time spent in the accumulation phase in seconds.
double get_warmup_time() const
Get the time spent in the warmup phase in seconds.
void set_verbosity(int v)
Set the verbosity level.
mc_generic(const std::string &rng_name, int rng_seed, int verbosity_lvl)
Construct a generic Monte Carlo class.
MCSignType get_sign() const
Get the current sign of the MC configuration.
int run(run_param_t const &params)
Run a generic MC simulation.
friend void h5_write(h5::group g, std::string const &name, mc_generic const &mc)
Write the MC simulation object to HDF5.
int get_config_id() const
Get the ID of the current MC configuration.
friend void h5_read(h5::group g, std::string const &name, mc_generic &mc)
Read the MC simulation object from HDF5.
void rm_measure(typename measure_set< MCSignType >::measure_itr_t const &it)
Remove a registered MC measure.
int warmup(run_param_t const &params)
Run the warumup phase of the MC simulation.
int accumulate(run_param_t const &params)
Run the accumulations phase of the MC simulation.
int get_current_cycle_number() const
Get the number of cycles done.
int get_nmeasures() const
Get the number of measurements done.
void collect_results(mpi::communicator const &c)
Collect results from multiple MPI processes.
std::map< std::string, double > get_acceptance_rates() const
Get the acceptance rates of all MC moves (see move_set::get_acceptance_rates).
std::string get_accumulation_time_HHMMSS() const
Get the time spent in the accumulation phase in hours, minutes and seconds.
measure_map_t::iterator measure_itr_t
Iterator type for the measure set.
Random number generator with a selectable underlying engine.
Output stream with a configurable verbosity level.
Accumulating wall-clock timer based on std::chrono::high_resolution_clock.
Definition timer.hpp:39
mc_phase
Phase of a Monte Carlo simulation.
Provides type erasure for auxiliary MC measurements.
Provides a set of MC measurements.
Provides a set of MC moves.
Provides concepts for the MC tools.
Provides a type erased random number generator.
Verbosity-controlled output stream and an auto-indenting std::ostream.
User-provided MC simulation parameters for the run() method.
std::int64_t ncycles
Number of MC cycles to run (< 1 to run indefinitely).
bool continue_after_ncycles_done
Should we continue the simulation on the current rank after the given number of cycles is done and wa...
std::function< void()> after_cycle_duty
Callback function that is executed after each of the first ncycles cycles.
MCSignType initial_sign
The sign of the MC weight is initialized to this value before the simulation starts (if it is !...
double check_exception_interval
Time interval (in seconds) after which the simulation checks for exceptions on other nodes.
std::function< bool()> stop_callback
Optional callback function that returns a boolean value indicating if we should stop the simulation....
mpi::communicator comm
MPI communicator.
std::int64_t cycle_length
Number of MC steps/moves per cycle.
bool propagate_exception
Should we propagate exceptions to all MPI ranks or abort the simulation immediately?
bool enable_calibration
Should we calibrate the moves during the simulation? Usually false during the accumulation phase.
double check_cycles_interval
Time interval (in seconds) after which the simulation checks if all other nodes have finished their c...
bool enable_measures
Should we take measurements during the simulation? Usually false during the warmup phase.
A wall-clock timer that accumulates elapsed seconds across start/stop intervals.
Small helpers that format wall-clock timestamps and durations for human-readable logs.