cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
GSLDerivator.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2021-2024 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_deriv.h>
20
#include <gsl/gsl_errno.h>
21
22
#include "
CepGen/Modules/DerivatorFactory.h
"
23
#include "
CepGen/Utils/Derivator.h
"
24
#include "
CepGen/Utils/GSLFunctionsWrappers.h
"
25
#include "
CepGen/Utils/Message.h
"
26
27
namespace
cepgen
{
28
namespace
utils {
29
class
GSLDerivator
:
public
Derivator
{
30
public
:
31
explicit
GSLDerivator
(
const
ParametersList
& params) :
Derivator
(params), mode_(
steerAs
<int,
Mode
>(
"mode"
)) {}
32
33
static
ParametersDescription
description
() {
34
auto
desc =
Derivator::description
();
35
desc.setDescription(
"GSL numerical differentiation algorithm"
);
36
desc.addAs<int,
Mode
>(
"mode"
,
Mode::central
)
37
.setDescription(
"mode used for the adaptive difference algorithm"
)
38
.allow(0,
"central"
)
39
.allow(1,
"forward"
)
40
.allow(2,
"backward"
);
41
return
desc;
42
}
43
48
double
derivate
(
const
Function1D
& func,
double
x,
double
h = -1.)
const override
{
49
int
res{GSL_SUCCESS};
50
double
val, val_unc;
51
const
double
step_size = h > 0. ? h :
h_
;
52
auto
gfunc =
utils::GSLFunctionWrapper::build
(func);
53
switch
(mode_) {
54
case
Mode::central
:
55
res = gsl_deriv_central(gfunc.get(), x, step_size, &val, &val_unc);
56
break
;
57
case
Mode::forward
:
58
res = gsl_deriv_forward(gfunc.get(), x, step_size, &val, &val_unc);
59
break
;
60
case
Mode::backward
:
61
res = gsl_deriv_backward(gfunc.get(), x, step_size, &val, &val_unc);
62
break
;
63
}
64
if
(res != GSL_SUCCESS)
65
CG_WARNING
(
"GSLDerivator"
) <<
"Failed to evaluate the derivative. GSL error: "
<< gsl_strerror(res) <<
"."
;
66
return
val;
67
}
68
69
enum struct
Mode
{
70
central
,
71
forward
,
72
backward
73
};
74
75
private
:
76
const
Mode
mode_;
77
};
78
}
// namespace utils
79
}
// namespace cepgen
80
81
using
cepgen::utils::GSLDerivator
;
82
REGISTER_DERIVATOR
(
"gsl"
,
GSLDerivator
);
DerivatorFactory.h
REGISTER_DERIVATOR
#define REGISTER_DERIVATOR(name, obj)
Add a generic derivator object builder definition.
Definition
DerivatorFactory.h:25
Derivator.h
GSLFunctionsWrappers.h
Message.h
CG_WARNING
#define CG_WARNING(mod)
Definition
Message.h:228
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::Steerable::steerAs
U steerAs(const std::string &key) const
Retrieve a recasted parameters as previously steered.
Definition
Steerable.h:44
cepgen::utils::Derivator
Definition
Derivator.h:27
cepgen::utils::Derivator::description
static ParametersDescription description()
Definition
Derivator.h:31
cepgen::utils::Derivator::h_
const double h_
Definition
Derivator.h:51
cepgen::utils::Function1D
Wrapper to a 1-dimensional function with optional parameters.
Definition
FunctionsWrappers.h:29
cepgen::utils::GSLDerivator
Definition
GSLDerivator.cpp:29
cepgen::utils::GSLDerivator::Mode
Mode
Definition
GSLDerivator.cpp:69
cepgen::utils::GSLDerivator::Mode::backward
@ backward
adaptive backward difference algorithm
cepgen::utils::GSLDerivator::Mode::forward
@ forward
adaptive forward difference algorithm
cepgen::utils::GSLDerivator::Mode::central
@ central
adaptive central difference algorithm
cepgen::utils::GSLDerivator::derivate
double derivate(const Function1D &func, double x, double h=-1.) const override
Evaluate the derivative of a function at a given value.
Definition
GSLDerivator.cpp:48
cepgen::utils::GSLDerivator::GSLDerivator
GSLDerivator(const ParametersList ¶ms)
Definition
GSLDerivator.cpp:31
cepgen::utils::GSLDerivator::description
static ParametersDescription description()
Definition
GSLDerivator.cpp:33
cepgen::utils::GSLFunctionWrapper::build
static std::unique_ptr< gsl_function > build(const Function1D &func, void *obj)
Utility to build a gsl_function pointer from a functional.
Definition
GSLFunctionsWrappers.h:35
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGen
Utils
GSLDerivator.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7