IntegrationTools.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
6
7  Author: Steve Boyd ( s.b.boyd \at warwick.ac.uk)
8  University of Warwick
9
10 */
11 //____________________________________________________________________________
12
13 #include <cstdlib>
14 #include <complex>
15
16 #include <TMath.h>
17
21
22 namespace genie {
23 namespace alvarezruso {
24
25 typedef std::complex<double> cdouble;
26
27 // All numerical values from Abscissas and weights for Gaussian quadratures of high order (1956)
28 // https://archive.org/details/jresv56n1p35
29
30 //
31 // Routine to work out the evaluation points for a Gaussian integral given the
32 // end points, and the number of sampling points
33 //
34 void integrationtools::SG20R(const double a, const double b, const unsigned int n, const unsigned int nsamp,
35  double* x, unsigned int& np, double* /*w*/)
36 {
37  static const double y[10] = {.9931285991, .9639719272, .9122344282, .8391169718, .7463319064, .6360536807, .5108670019, .3737060887, .2277858511, .0765265211 };
38  np = 20 * n;
39  double dint = (b - a) / double(n);
40  double delt = dint * 0.5;
41  double orig = a - delt;
42  int i1 = -nsamp;
43  int i2, j1, j2;
44  double dorig;
45  for(unsigned int i = 1; i <= n; i++)
46  {
47  orig += dint;
48  dorig = orig + orig;
49  i1 += nsamp;
50  i2 = i1 + nsamp+1;
51  for(unsigned int j = 1; j <= 10; j++)
52  {
53  j1 = i1 + j;
54  j2 = i2 - j;
55  x[j1-1] = orig - delt * y[j-1];
56  x[j2-1] = dorig - x[j1-1];
57  }
58  }
59 }
60
61 //-----------------------------------------------------------------------------------------------------------
62 // Gaussian-Legendre integration of the function defined by CF
63 cdouble integrationtools::RG201D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const cdouble CF[])
64 {
65  // Gaussian-Legendre integration of the function defined by CF
66  const double W[10] = {.0176140071,.0406014298,.0626720483,.0832767415,.1019301198,.1181945319,.1316886384,.1420961093,.1491729864,.1527533871};
67  cdouble CR(0.0, 0.0);
68  int I1 = -nsamp;
69  int I2, J1, J2;
70  for(unsigned int i = 1; i <= N; ++i)
71  {
72  I1 += nsamp;
73  I2 = I1 + nsamp+1;
74  for(unsigned int j = 1; j <= 10; ++j)
75  {
76  J1 = I1 + j;
77  J2 = I2 - j;
78  CR += W[j-1] * (CF[J1-1]+CF[J2-1]);
79  }
80  }
81  //CRES=CR*0.5*(B-A)/float(N)
82  cdouble CRES = CR*0.5*(B-A)/Double_t(N);
83  return CRES;
84 }
85 //-----------------------------------------------------------------------------------------------------------
86 // Gaussian-Legendre integration of the function defined by CF
87 void integrationtools::RG202D(const double a, const double b, unsigned int n, unsigned int l,
88  unsigned int m, std::vector< std::vector<cdouble> >& cf,
89  const unsigned int nsamp, std::vector<cdouble>& cres)
90 {
91  // This is a fast integrator based on a Gauss-Legendre method. This only support two-dimensional integration
92  n = 2; l = 0; m = 3;
93
94  static const double w[10] = {.0176140071,.0406014298,.0626720483,.0832767415,.1019301198,.1181945319,.1316886384,.1420961093,.1491729864,.1527533871};
95
96  std::vector<cdouble> cr(4, cdouble(0.0,0.0));
97
98  int i1 = -nsamp;
99  int i2;
100  int j1;
101  int j2;
102
103  for(unsigned int i = 0; i != n; ++i)
104  {
105  i1 += nsamp;
106  i2 = i1 + nsamp-1;
107  for(unsigned int j = 0; j != 10; ++j)
108  {
109  j1 = i1 + j;
110  j2 = i2 - j;
111
112  for(unsigned int ll = l; ll <= m; ++ll)
113  {
114  cr[ll] += w[j] * ( cf[ll][j1] + cf[ll][j2] );
115  }
116  }
117  }
118
119  for(unsigned int i = 0; i != 4; ++i)
120  {
121  cres[i] = cr[i] * 0.5 * (b-a) / static_cast<double>(n);
122  }
123 }
124 //-----------------------------------------------------------------------------------------------------------
125 //
126 // Routine to work out the evaluation points for a Gaussian integral given the
127 // end points, and the number of sampling points
128 //
129 void integrationtools::SG48R(const double a, const double b, const unsigned int n, const unsigned int nsamp,
130  double* x, unsigned int& np, double* /*w*/)
131 {
132  static const double y[24] = {0.9987710072, 0.9935301722, 0.9841245873, 0.9705915925, 0.9529877031,
133  0.9313866907, 0.9058791367, 0.8765720202, 0.8435882616, 0.8070662040, 0.7671590325, 0.7240341309,
134  0.6778723796, 0.6288673967, 0.5772247260, 0.5231609747, 0.4669029047, 0.4086864819, 0.3487558862,
135  0.2873624873, 0.2247637903, 0.1612223560, 0.0970046992, 0.0323801709};
136  np = 48 * n;
137  double dint = (b - a) / double(n);
138  double delt = dint * 0.5;
139  double orig = a - delt;
140  int i1 = -nsamp;
141  int i2, j1, j2;
142  double dorig;
143  for(unsigned int i = 1; i <= n; i++)
144  {
145  orig += dint;
146  dorig = orig + orig;
147  i1 += nsamp;
148  i2 = i1 + nsamp+1;
149  for(unsigned int j = 1; j <= 24; j++)
150  {
151  j1 = i1 + j;
152  j2 = i2 - j;
153  x[j1-1] = orig - delt * y[j-1];
154  x[j2-1] = dorig - x[j1-1];
155  }
156  }
157 }
158 //-----------------------------------------------------------------------------------------------------------
159 // Gaussian-Legendre integration of the function defined by CF
160 cdouble integrationtools::RG481D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const cdouble CF[])
161 {
162  // Gaussian-Legendre integration of the function defined by CF
163  static const double W[24] = {0.0031533460, 0.0073275539, 0.0114772345, 0.0155793157, 0.0196161604,
164  0.0235707608, 0.0274265097, 0.0311672278, 0.0347772225, 0.0382413510, 0.0415450829, 0.0446745608,
165  0.0476166584, 0.0503590355, 0.0528901894, 0.0551995036, 0.0572772921, 0.0591148396, 0.0607044391,
166  0.0620394231, 0.0631141922, 0.0639242385, 0.0644661644, 0.0647376968 };
167  cdouble CR(0.0, 0.0);
168  int I1 = -nsamp;
169  int I2, J1, J2;
170  for(unsigned int i = 1; i <= N; ++i)
171  {
172  I1 += nsamp;
173  I2 = I1 + nsamp+1;
174  for(unsigned int j = 1; j <= 24; ++j)
175  {
176  J1 = I1 + j;
177  J2 = I2 - j;
178  CR += W[j-1] * (CF[J1-1]+CF[J2-1]);
179  }
180  }
181  //CRES=CR*0.5*(B-A)/float(N)
182  cdouble CRES = CR*0.5*(B-A)/Double_t(N);
183  return CRES;
184 }
185 //-----------------------------------------------------------------------------------------------------------
186 // Gaussian-Legendre integration of the function defined by CF
187 void integrationtools::RG482D(const double a, const double b, unsigned int n, unsigned int l,
188  unsigned int m, std::vector< std::vector<cdouble> >& cf,
189  const unsigned int nsamp, std::vector<cdouble>& cres)
190 {
191  // This is a fast integrator based on a Gauss-Legendre method. This only support two-dimensional integration
192  n = 2; l = 0; m = 3;
193
194  static const double w[24] = {0.0031533460, 0.0073275539, 0.0114772345, 0.0155793157, 0.0196161604,
195  0.0235707608, 0.0274265097, 0.0311672278, 0.0347772225, 0.0382413510, 0.0415450829, 0.0446745608,
196  0.0476166584, 0.0503590355, 0.0528901894, 0.0551995036, 0.0572772921, 0.0591148396, 0.0607044391,
197  0.0620394231, 0.0631141922, 0.0639242385, 0.0644661644, 0.0647376968 };
198
199  std::vector<cdouble> cr(4, cdouble(0.0,0.0));
200
201  int i1 = -nsamp;
202  int i2;
203  int j1;
204  int j2;
205
206  for(unsigned int i = 0; i != n; ++i)
207  {
208  i1 += nsamp;
209  i2 = i1 + nsamp-1;
210  for(unsigned int j = 0; j != 24; ++j)
211  {
212  j1 = i1 + j;
213  j2 = i2 - j;
214
215  for(unsigned int ll = l; ll <= m; ++ll)
216  {
217  cr[ll] += w[j] * ( cf[ll][j1] + cf[ll][j2] );
218  }
219  }
220  }
221
222  for(unsigned int i = 0; i != 4; ++i)
223  {
224  cres[i] = cr[i] * 0.5 * (b-a) / static_cast<double>(n);
225  }
226 }
227
228 //______________________________________________________________________
229 // Calls correct integration tool for current sampling
230 void integrationtools::SGNR(const double a, const double b, const unsigned int n, const unsigned int nsamp,
231  double* x, unsigned int& np, double* w)
232 {
233  if (nsamp==20) {
234  integrationtools::SG20R(a, b, n, nsamp, x, np, w);
235  }
236  else if (nsamp==48) {
237  integrationtools::SG48R(a, b, n, nsamp, x, np, w);
238  }
239  else {
240  std::cerr<<"IntegrationTools/SGNR >> Unsupported nuclear sampling: "<<nsamp<<std::endl;
241  exit(-1);
242  }
243 }
244
245 cdouble integrationtools::RGN1D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const cdouble CF[])
246 {
247  if (nsamp==20) {
248  return integrationtools::RG201D(A, B, N, nsamp, CF);
249  }
250  else if (nsamp==48) {
251  return integrationtools::RG481D(A, B, N, nsamp, CF);
252  }
253  else {
254  std::cerr<<"IntegrationTools/RGN1D >> Unsupported nuclear sampling: "<<nsamp<<std::endl;
255  exit(-1);
256  }
257 }
258
259 void integrationtools::RGN2D (const double a, const double b, unsigned int n, unsigned int l,
260  unsigned int m, std::vector< std::vector<cdouble> >& cf,
261  const unsigned int nsamp, std::vector<cdouble>& cres)
262 {
263  if (nsamp==20) {
264  integrationtools::RG202D(a, b, n, l, m, cf, nsamp, cres);
265  }
266  else if (nsamp==48) {
267  integrationtools::RG482D(a, b, n, l, m, cf, nsamp, cres);
268  }
269  else {
270  std::cerr<<"IntegrationTools/RGN2D >> Unsupported nuclear sampling: "<<nsamp<<std::endl;
271  exit(-1);
272  }
273 }
274
275 } // alvarezruso namespace
276 } // genie namespace
std::complex< double > cdouble
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
void SG20R(const double a, const double b, const unsigned int n, const unsigned int nsamp, double *x, unsigned int &np, double *w)
std::complex< double > RG201D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const std::complex< double > CF[])
void RGN2D(const double a, const double b, unsigned int n, unsigned int l, unsigned int m, std::vector< std::vector< std::complex< double > > > &cf, const unsigned int nsamp, std::vector< std::complex< double > > &cres)
OStream cerr
Definition: OStream.cxx:7
const double a
void SG48R(const double a, const double b, const unsigned int n, const unsigned int nsamp, double *x, unsigned int &np, double *w)
const double j
Definition: BetheBloch.cxx:29
static const double A
Definition: Units.h:82
exit(0)
const hit & b
Definition: hits.cxx:21
std::complex< double > RG481D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const std::complex< double > CF[])
std::complex< double > RGN1D(const double A, const double B, const unsigned int N, const unsigned int nsamp, const std::complex< double > CF[])
void SGNR(const double a, const double b, const unsigned int n, const unsigned int nsamp, double *x, unsigned int &np, double *w)
void RG202D(const double a, const double b, unsigned int n, unsigned int l, unsigned int m, std::vector< std::vector< std::complex< double > > > &cf, const unsigned int nsamp, std::vector< std::complex< double > > &cres)
Float_t w
Definition: plot.C:20
void RG482D(const double a, const double b, unsigned int n, unsigned int l, unsigned int m, std::vector< std::vector< std::complex< double > > > &cf, const unsigned int nsamp, std::vector< std::complex< double > > &cres)
#define W(x)