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
26
27namespace 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
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,
73 };
74
75 private:
76 const Mode mode_;
77 };
78 } // namespace utils
79} // namespace cepgen
80
#define REGISTER_DERIVATOR(name, obj)
Add a generic derivator object builder definition.
#define CG_WARNING(mod)
Definition Message.h:228
A description object for parameters collection.
U steerAs(const std::string &key) const
Retrieve a recasted parameters as previously steered.
Definition Steerable.h:44
static ParametersDescription description()
Definition Derivator.h:31
Wrapper to a 1-dimensional function with optional parameters.
@ backward
adaptive backward difference algorithm
@ forward
adaptive forward difference algorithm
@ central
adaptive central difference algorithm
double derivate(const Function1D &func, double x, double h=-1.) const override
Evaluate the derivative of a function at a given value.
GSLDerivator(const ParametersList &params)
static ParametersDescription description()
static std::unique_ptr< gsl_function > build(const Function1D &func, void *obj)
Utility to build a gsl_function pointer from a functional.
Common namespace for this Monte Carlo generator.