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