Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gauss.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * *
3  * / / / / __ \ \ / / *
4  * / /__ / / / _ \ \ \/ / *
5  * / ___ / | |/_/ / /\ \ *
6  * / / / / \_\ / / \ \ *
7  * *
8  * Jakub Benda (c) 2014 *
9  * Charles University in Prague *
10  * *
11 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HEX_GAUSS
14 #define HEX_GAUSS
15 
16 #include <map>
17 #include <vector>
18 
19 #include "bspline.h"
20 
34 {
35  public:
36 
37  // constructor
38  GaussLegendre(Bspline const & bspline)
39  : bspline_(bspline) {}
40 
52  int gauss_nodes_and_weights (int points, const double* &vx, const double* &vw) const;
53 
64  cArray p_points (int points, Complex x1, Complex x2) const;
65 
76  cArray p_weights (int points, Complex x1, Complex x2) const;
77 
101  template <class Functor, class... Data> Complex quad (
102  Functor f,
103  int points, int iknot, Complex x1, Complex x2,
104  Data... data
105  ) const {
106  // check boundaries
107  if (x1.real() < bspline_.t(iknot).real() or bspline_.t(iknot+1).real() < x1.real() or
108  x2.real() < bspline_.t(iknot).real() or bspline_.t(iknot+1).real() < x2.real())
109  {
110  throw exception ("[quad] Error: boundaries not for this iknot!");
111  }
112 
113  // get evaluation points and weights
114  cArray xs = p_points(points, x1, x2);
115  cArray ws = p_weights(points, x1, x2);
116  cArray values (points);
117 
118  // evaluate the function
119  f(points, xs.data(), values.data(), data...);
120 
121  // pointers for fast restricted access
122  Complex const * const restrict pw = ws.data();
123  Complex const * const restrict py = values.data();
124 
125  // sum the results
126  Complex result = 0.;
127  for (int ipt = 0; ipt < points; ipt++)
128  result += pw[ipt] * py[ipt];
129 
130  return result;
131  }
132 
144  template <class ClassPtr, class Functor, class... Data> Complex quadMFP (
145  ClassPtr ptr, Functor f, int points, int iknot, Complex x1, Complex x2, Data... data
146  ) const {
147  // check boundaries
148  if (x1.real() < bspline_.t(iknot).real() or bspline_.t(iknot+1).real() < x1.real() or
149  x2.real() < bspline_.t(iknot).real() or bspline_.t(iknot+1).real() < x2.real())
150  {
151  throw exception ("[quad] Error: boundaries not for this iknot!");
152  }
153 
154  // get evaluation points and weights
155  cArray xs = p_points(points, x1, x2);
156  cArray ws = p_weights(points, x1, x2);
157  cArray values (points);
158 
159  // evaluate the function
160  (ptr->*f)(points, xs.data(), values.data(), data...);
161 
162  // pointers for fast restricted access
163  Complex const * const restrict pw = ws.data();
164  Complex const * const restrict py = values.data();
165 
166  // sum the results
167  Complex result = 0.;
168  for (int ipt = 0; ipt < points; ipt++)
169  result += pw[ipt] * py[ipt];
170 
171  return result;
172  }
173 
174  private:
175 
176  // B-spline environment
177  Bspline const & bspline_;
178 
179  // precomputed nodes and weights, common to all instances
180  static std::vector<std::pair<double*,double*>> data_;
181 };
182 
183 #endif
virtual T * data()
Data pointer.
Definition: arrays.h:757
cArray p_weights(int points, Complex x1, Complex x2) const
Get Gauss-Legendre quadrature weights in interval.
Definition: gauss.cpp:94
int gauss_nodes_and_weights(int points, const double *&vx, const double *&vw) const
Retrieve Gauss-Legendre data.
Definition: gauss.cpp:29
A comfortable number array class.
Definition: arrays.h:171
Complex const & t(int i) const
B-spline knot sequence.
Definition: bspline.h:186
B-spline environment.
Definition: bspline.h:35
#define restrict
Definition: misc.h:88
Gauss-Legendre quadrature.
Definition: gauss.h:33
Complex quad(Functor f, int points, int iknot, Complex x1, Complex x2, Data...data) const
Gauss-Legendre integrator.
Definition: gauss.h:101
Complex quadMFP(ClassPtr ptr, Functor f, int points, int iknot, Complex x1, Complex x2, Data...data) const
Gauss-Legendre integrator.
Definition: gauss.h:144
CLArrayView exception
GaussLegendre(Bspline const &bspline)
Definition: gauss.h:38
std::complex< double > Complex
Definition: complex.h:20
cArray p_points(int points, Complex x1, Complex x2) const
Get Gauss-Legendre quadrature points in interval.
Definition: gauss.cpp:73