 |
Version: SMASH-2.0
|
|
Go to the documentation of this file.
10 #ifndef SRC_INCLUDE_SMASH_INTEGRATE_H_
11 #define SRC_INCLUDE_SMASH_INTEGRATE_H_
14 #include <gsl/gsl_integration.h>
15 #include <gsl/gsl_monte_plain.h>
16 #include <gsl/gsl_monte_vegas.h>
42 void operator()(gsl_integration_cquad_workspace *ptr)
const {
46 gsl_integration_cquad_workspace_free(ptr);
54 class Result :
public std::pair<double, double> {
56 using Base = std::pair<double, double>;
63 operator double()
const {
return Base::first; }
66 double value()
const {
return Base::first; }
69 double error()
const {
return Base::second; }
80 double relative_tolerance = 5e-4,
81 double absolute_tolerance = 1e-9)
const {
82 const double allowed_error =
83 std::max(absolute_tolerance,
value() * relative_tolerance);
84 if (
error() > allowed_error) {
85 std::stringstream error_msg;
86 error_msg << integration_name <<
" resulted in I = " <<
value() <<
" ± "
88 <<
", but the required precision is either absolute error < "
89 << absolute_tolerance <<
" or relative error < "
90 << relative_tolerance << std::endl;
91 throw std::runtime_error(error_msg.str());
123 :
workspace_(gsl_integration_cquad_workspace_alloc(workspace_size)) {}
138 template <
typename F>
141 const gsl_function gslfun{
144 [](
double x,
void *type_erased) ->
double {
145 auto &&f = *static_cast<F *>(type_erased);
151 const int error_code = gsl_integration_cquad(
153 &result.first, &result.second,
156 std::stringstream err;
157 err <<
"GSL 1D deterministic integration: " << gsl_strerror(error_code);
158 throw std::runtime_error(err.str());
175 std::unique_ptr<gsl_integration_cquad_workspace, GslWorkspaceDeleter>
191 template <
typename F>
233 double epsabs = 1e-9)
252 template <
typename F>
256 if (max1 < min1 || max2 < min2) {
259 bool tolerable = (max1 - min1 > -1.e-15) && (max2 - min2 > -1.e-15);
263 std::stringstream err;
264 err <<
"Integrator2d got wrong integration limits: [" << min1 <<
", "
265 << max1 <<
"], [" << min2 <<
", " << max2 <<
"]";
266 throw std::invalid_argument(err.str());
269 Integrand2d<F> f_with_limits = {min1, max1 - min1, min2, max2 - min2, fun};
271 const integrand_t cuhre_fun{[](
const int * ,
const cubareal xx[],
272 const int * , cubareal ff[],
273 void *userdata) ->
int {
277 ff[0] = (i->f)(i->min1 + i->diff1 * xx[0], i->min2 + i->diff2 * xx[1]) *
284 void *userdata = &f_with_limits;
287 const int mineval = 0;
290 const char *statefile =
nullptr;
291 void *spin =
nullptr;
293 Cuhre(ndim, ncomp, cuhre_fun, userdata, nvec,
epsrel_,
epsabs_, flags,
295 &result.first, &result.second, &
prob_);
298 std::stringstream err;
299 err <<
"After " <<
neval_ <<
" evaluations "
300 <<
"Cuhre integration from Cuba reports error code " <<
fail_;
301 throw std::runtime_error(err.str());
336 #endif // SRC_INCLUDE_SMASH_INTEGRATE_H_
double error() const
Access the second entry in the pair as the absolute error.
A C++ interface for numerical integration in two dimensions with the Cuba Cuhre integration function.
void set_precision(double absolute, double relative)
Set precision for the absolute and relative error of the integration.
A C++ interface for numerical integration in one dimension with the GSL CQUAD integration functions.
This is a wrapper for the integrand, so we can pass the limits as well for renormalizing to the unit ...
double accuracy_absolute_
Parameter to the GSL integration function: desired absolute error limit.
double epsabs_
Requested absolute accuracy.
std::pair< double, double > Base
The data type to store the value and the error of the integration.
double value() const
Access the first entry in the pair as the value.
double min1
the lower bound of the first integrated variable
Guard type that safely disables floating point traps for the scope in which it is placed.
void check_error(const std::string &integration_name, double relative_tolerance=5e-4, double absolute_tolerance=1e-9) const
Check whether the error is small and alert if it is not.
std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleter > workspace_
Holds the workspace pointer.
F f
the integrated function
A deleter type for std::unique_ptr to be used with gsl_integration_workspace pointers.
Integrator2d(int num_calls=1e6, double epsrel=5e-4, double epsabs=1e-9)
Construct an integration functor.
int maxeval_
The (approximate) maximum number of integrand evaluations allowed.
The result type returned from integrations, containing the value and an error.
double min2
the lower bound of the second integrated variable
int nregions_
Actual number of subregions needed.
double diff1
the integration range of the first integrated variable
Integrator(size_t workspace_size=1000)
Construct an integration functor with the given workspace_size.
int neval_
Actual number of integrand evaluations needed.
double accuracy_relative_
Parameter to the GSL integration function: desired relative error limit.
void operator()(gsl_integration_cquad_workspace *ptr) const
Frees the gsl_integration_cquad_workspace resource if it is non-zero.
double diff2
the integration range of the second integrated variable
constexpr GslWorkspaceDeleter()=default
The class has no members, so this is a noop.
double epsrel_
Requested relative accuracy.
Result operator()(double a, double b, F &&fun)
The function call operator implements the integration functionality.
Result operator()(double min1, double max1, double min2, double max2, F fun)
The function call operator implements the integration functionality.
double prob_
The chi^2 probability that the error is not a reliable estimate of the true integration error.