BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Integrator.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Utils/Integrator.cpp
6 //! @brief Implements classes RealIntegrator, ComplexIntegrator.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #include "Base/Utils/Integrator.h"
16 
18  : m_gsl_f{m_Cfunction, nullptr}, m_workspace{gsl_integration_workspace_alloc(200)}
19 {
20 }
21 
23 {
24  gsl_integration_workspace_free(m_workspace);
25 }
26 
27 double RealIntegrator::integrate(const std::function<double(double)>& f, double lmin, double lmax)
28 {
29  m_gsl_f.params = (void*)&f;
30  double result, error;
31  gsl_integration_qag(&m_gsl_f, lmin, lmax, 1e-9, 1e-7, 200, 3, m_workspace, &result, &error);
32  // TODO check error
33  return result;
34 }
35 
36 complex_t ComplexIntegrator::integrate(const std::function<complex_t(double)>& f, double lmin,
37  double lmax)
38 {
39  return {realPart.integrate([f](double x) { return f(x).real(); }, lmin, lmax),
40  imagPart.integrate([f](double x) { return f(x).imag(); }, lmin, lmax)};
41 }
std::complex< double > complex_t
Definition: Complex.h:20
Defines classes RealIntegrator, ComplexIntegrator.
complex_t integrate(const std::function< complex_t(double)> &f, double lmin, double lmax)
Definition: Integrator.cpp:36
RealIntegrator realPart
Definition: Integrator.h:46
RealIntegrator imagPart
Definition: Integrator.h:47
double integrate(const std::function< double(double)> &f, double lmin, double lmax)
Definition: Integrator.cpp:27
gsl_integration_workspace * m_workspace
Definition: Integrator.h:36
gsl_function m_gsl_f
Definition: Integrator.h:34