cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
GSLRandomGenerator.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2023 Laurent Forthomme
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 * 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 should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <gsl/gsl_randist.h>
20#include <gsl/gsl_rng.h>
21
22#include <memory>
23
27
28namespace cepgen {
30 public:
31 explicit GSLRandomGenerator(const ParametersList& params) : utils::RandomGenerator(params) {
32 gsl_rng_env_setup();
33 gsl_rng_type* rng_engine{nullptr};
34 const auto& type = steer<std::string>("type");
35 if (type == "mt19937")
36 rng_engine = const_cast<gsl_rng_type*>(gsl_rng_mt19937);
37 else if (type == "taus")
38 rng_engine = const_cast<gsl_rng_type*>(gsl_rng_taus);
39 else if (type == "taus2")
40 rng_engine = const_cast<gsl_rng_type*>(gsl_rng_taus2);
41 else if (type == "gfsr4")
42 rng_engine = const_cast<gsl_rng_type*>(gsl_rng_gfsr4);
43 else if (type == "ranlxs0")
44 rng_engine = const_cast<gsl_rng_type*>(gsl_rng_ranlxs0);
45 else
46 throw CG_FATAL("GSLRandomGenerator") << "Random number generator engine invalid: '" << type << "'.";
47
48 rng_.reset(gsl_rng_alloc(rng_engine));
49 gsl_rng_set(rng_.get(), seed_);
50
51 CG_DEBUG("GSLRandomGenerator") << "Random numbers generator: " << gsl_rng_name(rng_.get()) << ".\n\t"
52 << "Seed: " << seed_ << ".";
53 }
54
57 desc.setDescription("GSL random number generator engine");
58 desc.add<std::string>("type", "mt19937")
59 .allow("mt19937", "Mersenne-Twister generator")
60 .allow("taus", "maximally equidistributed combined Tausworthe generator by L’Ecuyer")
61 .allow("taus2",
62 "maximally equidistributed combined Tausworthe generator by L’Ecuyer (w/ improved seeding procedure)")
63 .allow("gfsr4", "lagged-fibonacci generator")
64 .allow("ranlxs0", "second-generation version of the RANLUX algorithm of Luscher")
65 .setDescription("random number engine");
66 return desc;
67 }
68
69 int uniformInt(int min, int max) override { return min + gsl_rng_uniform_int(rng_.get(), max - min + 1); }
70 double uniform(double min, double max) override { return Limits{min, max}.x(gsl_rng_uniform(rng_.get())); }
71 double normal(double mean, double rms) override { return gsl_ran_gaussian(rng_.get(), rms) + mean; }
72 double exponential(double exponent) override { return gsl_ran_exponential(rng_.get(), exponent); }
73 double breitWigner(double mean, double scale) override { return gsl_ran_cauchy(rng_.get(), scale) + mean; }
74 double landau(double location, double width) override { return width * gsl_ran_landau(rng_.get()) + location; }
75 int poisson(double mean) override { return gsl_ran_poisson(rng_.get(), mean); }
76
77 private:
79 struct gsl_rng_deleter {
81 inline void operator()(gsl_rng* rng) { gsl_rng_free(rng); }
82 };
84 std::unique_ptr<gsl_rng, gsl_rng_deleter> rng_;
85
86 void* enginePtr() override { return rng_.get(); }
87 };
88} // namespace cepgen
89
90REGISTER_RANDOM_GENERATOR("gsl", GSLRandomGenerator);
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG(mod)
Definition Message.h:220
#define REGISTER_RANDOM_GENERATOR(name, obj)
Add a generic random number generator definition to the list of handled modules.
double normal(double mean, double rms) override
int poisson(double mean) override
static ParametersDescription description()
int uniformInt(int min, int max) override
double uniform(double min, double max) override
double exponential(double exponent) override
double breitWigner(double mean, double scale) override
double landau(double location, double width) override
GSLRandomGenerator(const ParametersList &params)
Validity interval for a variable.
Definition Limits.h:28
double x(double v) const
Find the [0,1] value scaled between minimum and maximum.
Definition Limits.cpp:97
A description object for parameters collection.
A random number generator.
static ParametersDescription description()
RandomGenerator(const ParametersList &)
Default constructor.
Common namespace for this Monte Carlo generator.