TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
random_generator.cpp
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//
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#include "./MersenneRNG.hpp"
28
29#include <boost/preprocessor/control/if.hpp>
30#include <boost/preprocessor/seq.hpp>
31#include <boost/random/lagged_fibonacci.hpp>
32#include <boost/random/mersenne_twister.hpp>
33#include <boost/random/ranlux.hpp>
34#include <boost/random/uniform_real.hpp>
35#include <boost/random/variate_generator.hpp>
36#include <fmt/format.h>
37#include <nda/macros.hpp>
38
39#include <cstdint>
40#include <stdexcept>
41#include <string>
42#include <vector>
43
44// List of all supported Boost random number generators.
45#define RNG_LIST \
46 (mt19937)(mt11213b)( \
47 lagged_fibonacci607)(lagged_fibonacci1279)(lagged_fibonacci2281)(lagged_fibonacci3217)(lagged_fibonacci4423)(lagged_fibonacci9689)(lagged_fibonacci19937)(lagged_fibonacci23209)(lagged_fibonacci44497)(ranlux3)
48
49namespace triqs::mc_tools {
50
51 random_generator::random_generator(std::string name, std::uint32_t seed, std::size_t buffer_size) : buffer_(buffer_size), name_(std::move(name)) {
52 initialize_rng(name_, seed);
53 refill();
54 }
55
56 void random_generator::initialize_rng(std::string const &name, std::uint32_t seed) {
57 // empty string corresponds to RandMT
58 if (name.empty()) {
59 using rng_t = RandomGenerators::RandMT;
60 ptr_ = std::make_unique<rng_model<rng_t>>(seed);
61 return;
62 }
63
64 // now boost random number generators
65#define DRNG(r, data, XX) \
66 if (name == AS_STRING(XX)) { \
67 using rng_t = boost::variate_generator<boost::XX, boost::uniform_real<double>>; \
68 ptr_ = std::make_unique<rng_model<rng_t>>(rng_t{boost::XX{seed}, boost::uniform_real<>{}}); \
69 return; \
70 }
71 BOOST_PP_SEQ_FOR_EACH(DRNG, ~, RNG_LIST)
72
73 // throw an exception if the given name is not recognized
74 throw std::runtime_error(fmt::format("Error in random_generator::initialize_rng: RNG with name {} is not supported", name));
75 }
76
77 std::string random_generator_names(std::string const &sep) {
78#define PR(r, sep, p, XX) BOOST_PP_IF(p, +(sep) +, ) std::string(AS_STRING(XX))
79 return BOOST_PP_SEQ_FOR_EACH_I(PR, sep, RNG_LIST);
80 }
81
82 std::vector<std::string> random_generator_names_list() {
83 std::vector<std::string> res;
84#define PR2(r, sep, p, XX) res.push_back(AS_STRING(XX));
85 BOOST_PP_SEQ_FOR_EACH_I(PR2, sep, RNG_LIST);
86 return res;
87 }
88
89} // namespace triqs::mc_tools
Provides a Mersenne Twister random number generator.
Type erasure class for MC moves.
Definition mc_move.hpp:68
random_generator()
Default constructor uses the mt19937 engine with the default seed.
std::string name() const
Get the name of the underlying RNG.
Compiler / platform glue and the dcomplex alias (must be included before any Boost header).
std::string random_generator_names(std::string const &sep)
Get a string containing the names of all available RNGs.
std::vector< std::string > random_generator_names_list()
Get a list of all available RNG names.
Provides a type erased random number generator.