MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
Integrator.cc
1
4//
5// This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6//
7// MARLEY is free software: you can redistribute it and/or modify it under the
8// terms of version 3 of the GNU General Public License as published by the
9// Free Software Foundation.
10//
11// For the full text of the license please see COPYING or
12// visit http://opensource.org/licenses/GPL-3.0
13//
14// Please respect the MCnet academic usage guidelines. See GUIDELINES
15// or visit https://www.montecarlonet.org/GUIDELINES for details.
16
17#include <cmath>
18
19#include "marley/marley_utils.hh"
20#include "marley/Integrator.hh"
21
22marley::Integrator::Integrator(size_t num) : N_(num), weights_(num + 1, 0.),
23 offsets_(num - 1)
24{
27
28 // Precompute the N_ - 1 offsets for speed
29 double arg = 0.;
30 for (size_t n = 0; n < N_ - 1; ++n) {
31 arg += marley_utils::half_pi / N_;
32 offsets_[n] = std::cos(arg);
33 }
34
35 // Also precompute the N_ + 1 weights
36 for (size_t n = 0; n <= N_; ++n) {
37 for (size_t k = 0; k <= N_; ++k) {
38 double weight_piece = std::cos(n * k * marley_utils::pi / N_)
39 / (1. - std::pow(2*k, 2));
40 if (k != 0 && k != N_) weight_piece *= 2.;
41 weights_[n] += weight_piece;
42 }
43 weights_[n] /= N_;
44 }
45}
46
47double marley::Integrator::num_integrate(const std::function<double(double)>& f,
48 double a, double b) const
49{
50 double A = (b - a) / 2.;
51 double B = (b + a) / 2.;
52 double C = (f(a) + f(b)) / 2.;
53
54 double integral = weights_[0] * C; // n = 0 term
55 integral += weights_[N_] * f(B); // n = N_ term
56
57 // n = 1 to n = N_ - 1 terms
58 for (size_t n = 1; n < N_; ++n) {
59 double epoint = A * offsets_[n - 1];
60 integral += weights_[n] * (f(B + epoint) + f(B - epoint));
61 }
62
63 return A * integral;
64}
double num_integrate(const std::function< double(double)> &f, double a, double b) const
Numerically integrate an arbitrary 1D function.
Definition Integrator.cc:47
Integrator(size_t num=N_DEFAULT_)
Create a Clenshaw-Curtis quadrature integrator that uses 2*num sampling points.
Definition Integrator.cc:22