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
24
#include "
CepGen/Core/Exception.h
"
25
#include "
CepGen/Modules/RandomGeneratorFactory.h
"
26
#include "
CepGen/Utils/RandomGenerator.h
"
27
28
namespace
cepgen
{
29
class
GSLRandomGenerator
:
public
utils::RandomGenerator
{
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
55
static
ParametersDescription
description
() {
56
auto
desc =
utils::RandomGenerator::description
();
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
90
REGISTER_RANDOM_GENERATOR
(
"gsl"
, GSLRandomGenerator);
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
RandomGeneratorFactory.h
REGISTER_RANDOM_GENERATOR
#define REGISTER_RANDOM_GENERATOR(name, obj)
Add a generic random number generator definition to the list of handled modules.
Definition
RandomGeneratorFactory.h:25
RandomGenerator.h
cepgen::GSLRandomGenerator
Definition
GSLRandomGenerator.cpp:29
cepgen::GSLRandomGenerator::normal
double normal(double mean, double rms) override
Definition
GSLRandomGenerator.cpp:71
cepgen::GSLRandomGenerator::poisson
int poisson(double mean) override
Definition
GSLRandomGenerator.cpp:75
cepgen::GSLRandomGenerator::description
static ParametersDescription description()
Definition
GSLRandomGenerator.cpp:55
cepgen::GSLRandomGenerator::uniformInt
int uniformInt(int min, int max) override
Definition
GSLRandomGenerator.cpp:69
cepgen::GSLRandomGenerator::uniform
double uniform(double min, double max) override
Definition
GSLRandomGenerator.cpp:70
cepgen::GSLRandomGenerator::exponential
double exponential(double exponent) override
Definition
GSLRandomGenerator.cpp:72
cepgen::GSLRandomGenerator::breitWigner
double breitWigner(double mean, double scale) override
Definition
GSLRandomGenerator.cpp:73
cepgen::GSLRandomGenerator::landau
double landau(double location, double width) override
Definition
GSLRandomGenerator.cpp:74
cepgen::GSLRandomGenerator::GSLRandomGenerator
GSLRandomGenerator(const ParametersList ¶ms)
Definition
GSLRandomGenerator.cpp:31
cepgen::Limits
Validity interval for a variable.
Definition
Limits.h:28
cepgen::Limits::x
double x(double v) const
Find the [0,1] value scaled between minimum and maximum.
Definition
Limits.cpp:97
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::utils::RandomGenerator
A random number generator.
Definition
RandomGenerator.h:31
cepgen::utils::RandomGenerator::description
static ParametersDescription description()
Definition
RandomGenerator.cpp:53
cepgen::utils::RandomGenerator::RandomGenerator
RandomGenerator(const ParametersList &)
Default constructor.
Definition
RandomGenerator.cpp:24
cepgen::utils::RandomGenerator::seed_
unsigned long long seed_
Definition
RandomGenerator.h:56
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGen
Utils
GSLRandomGenerator.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7