forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathmath_integral.cpp
More file actions
454 lines (402 loc) · 19.3 KB
/
math_integral.cpp
File metadata and controls
454 lines (402 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "math_integral.h"
#include <stddef.h> // use size_t
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include "constants.h"
namespace ModuleBase
{
Integral::Integral(){}
Integral::~Integral(){}
// Peize Lin accelerate 2017-10-02
/*
void Integral::Simpson_Integral
(
const int mesh,
const double *func,
const double *rab,
double &asum
)
{
// simpson's rule integration. On input:
// mesh = mhe number of grid points (should be odd)
// func(i)= function to be integrated
// rab(i) = r(i) * dr(i)/di * di
// For the logarithmic grid not including r=0 :
// r(i) = r_0*exp((i-1)*dx) ==> rab(i)=r(i)*dx
// For the logarithmic grid including r=0 :
// r(i) = a(exp((i-1)*dx)-1) ==> rab(i)=(r(i)+a)*dx
// Output in asum = \sum_i c_i f(i)*rab(i) = \int_0^\infty f(r) dr
// where c_i are alternativaly 2/3, 4/3 except c_1 = c_mesh = 1/3
// simpson's rule integrator for function stored on the
// radial logarithmic mesh
// routine assumes that mesh is an odd number so run check
if (mesh % 2 == 0)
{
std::cout << "\n error in subroutine simpson ";
std::cout << "\n routine assumes mesh is odd but mesh = "
<< mesh << std::endl;
return;
}
asum = 0.00;
const double r12 = 1.00 / 12.00;
double f3 = func [0] * rab [0] * r12;
for (int i = 1;i < mesh;i += 2)
{
const double f1 = f3;
const double f2 = func [i] * rab [i] * r12;
f3 = func [i + 1] * rab [i + 1] * r12;
asum += 4.00 * f1 + 16.00 * f2 + 4.00 * f3;
}
return;
}// end subroutine simpson
*/
// Peize Lin accelerate 2017-10-02
void Integral::Simpson_Integral
(
const int mesh,
const double * const func,
const double * const rab,
double &asum
)
{
/* simpson's rule integration. On input:
! mesh = mhe number of grid points (should be odd)
! func(i)= function to be integrated
! rab(i) = r(i) * dr(i)/di * di
! For the logarithmic grid not including r=0 :
! r(i) = r_0*exp((i-1)*dx) ==> rab(i)=r(i)*dx
! For the logarithmic grid including r=0 :
! r(i) = a(exp((i-1)*dx)-1) ==> rab(i)=(r(i)+a)*dx
! Output in asum = \sum_i c_i f(i)*rab(i) = \int_0^\infty f(r) dr
! where c_i are alternativaly 2/3, 4/3 except c_1 = c_mesh = 1/3
*/
// simpson's rule integrator for function stored on the
// radial logarithmic mesh
// routine assumes that mesh is an odd number so run check
assert(mesh&1);
asum = 0.00;
const size_t end = mesh-2;
for( size_t i=1; i!=end; i+=2 )
{
const double f1 = func[i]*rab[i];
asum += f1 + f1 + func[i+1]*rab[i+1];
}
const double f1 = func[mesh-2]*rab[mesh-2];
asum += f1+f1;
asum += asum;
asum += func[0]*rab[0] + func[mesh-1]*rab[mesh-1];
asum /= 3.0;
return;
}// end subroutine simpson
// Peize Lin accelerate 2017-10-02
void Integral::Simpson_Integral
(
const int mesh,
const double * const func,
const double dr,
double &asum
)
{
/* simpson's rule integration. On input:
! mesh = mhe number of grid points (should be odd)
! func(i)= function to be integrated
! rab(i) = r(i) * dr(i)/di * di
! For the logarithmic grid not including r=0 :
! r(i) = r_0*exp((i-1)*dx) ==> rab(i)=r(i)*dx
! For the logarithmic grid including r=0 :
! r(i) = a(exp((i-1)*dx)-1) ==> rab(i)=(r(i)+a)*dx
! Output in asum = \sum_i c_i f(i)*rab(i) = \int_0^\infty f(r) dr
! where c_i are alternativaly 2/3, 4/3 except c_1 = c_mesh = 1/3
*/
// simpson's rule integrator for function stored on the
// radial logarithmic mesh
// routine assumes that mesh is an odd number so run check
assert(mesh&1);
asum = 0.00;
const size_t end = mesh-2;
for(size_t i=1; i!=end; i+=2 )
{
const double f1 = func[i];
asum += f1 + f1 + func[i+1];
}
const double f1 = func[mesh-2];
asum += f1+f1;
asum += asum;
asum += func[0] + func[mesh-1];
asum *= dr/3.0;
return;
}// end subroutine simpson
// Peize Lin add 2016-02-14
void Integral::Simpson_Integral_0toall
(
const int mesh,
const double * const func,
const double * const rab,
double * const asum
)
{
// asum(r) = \int_{r'=0}^{r} dr' f(r')
const double r2=1.00/2.00, r3=1.00/3.00;
asum[0] = 0.00;
double f3 = func [0] * rab [0];
for( int i=1; i<mesh; i+=2)
{
const double f1 = f3;
const double f2 = func[i] * rab[i] ;
f3 = func[i+1] * rab[i+1] ;
asum[i] = asum[i-1] + r2*( f1 + f2);
if(i+1<mesh)
{
asum[i+1] = asum[i-1] + r3*( f1 + 4.00*f2 + f3 );
}
}
return;
}
// Peize Lin add 2016-02-14
// faster but still have bug
/*void Integral::Simpson_Integral_alltoinf
(
const int mesh,
const double *func,
const double *rab,
double *asum
)
{
// asum(r) = \int_{r'=r}^{+\infty} dr' f(r')
// = \inf_{r'=r}^{mesh} dr' f(r')
const double r2=1.00/2.00, r3=1.00/3.00;
asum[mesh-1] = 0.00;
const int odd_mesh = (mesh-1)^~1;
double f1 = func[odd_mesh] * rab[odd_mesh];
for( size_t i=(mesh-3)|1; i>0; i-=2)
{
const double f3 = f1;
if( i+3==mesh )
{
const double f4 = func[mesh-1] * rab[mesh-1];
asum[mesh-2] = r2*(f3 + f4);
}
const double f2 = func[i] * rab[i] ;
f1 = func[i-1] * rab[i-1] ;
asum[i-1] = asum[i+1] + r3*( f1 + 4.00*f2 + f3 );
asum[i] = asum[i-1] - r2*( f1 + f2);
}
return;
}*/
// Peize Lin add 2016-06-11
// a little lower
void Integral::Simpson_Integral_alltoinf
(
const int mesh,
const double * const func,
const double * const rab,
double * const asum
)
{
Integral::Simpson_Integral_0toall( mesh, func, rab, asum );
const double asum_all = asum[mesh-1];
for (int i = 0;i < mesh; ++i)
{
asum[i] = asum_all - asum[i];
}
return;
}
double Integral::simpson(const int n, const double* const f, const double dx)
{
assert(n >= 2);
if (n == 4)
{ // Simpson's 3/8 rule
return 3.0 * dx / 8 * (f[0] + 3.0 * f[1] + 3.0 * f[2] + f[3]);
}
if (n == 2)
{
return 0.5 * dx * (f[0] + f[1]);
}
if (n % 2 == 1)
{ // composite Simpson's 1/3 rule
double sum = 0.0;
for (int i = 1; i != n-2; i += 2)
{
sum += 2.0 * f[i] + f[i+1];
}
sum += 2.0 * f[n-2];
sum *= 2.0;
sum += f[0] + f[n-1];
return sum * dx / 3.0;
}
else
{ // composite Simpson's 1/3 rule for the first n-4 intervals plus Simpson's 3/8 rule for the last 3 intervals
return simpson(n-3, f, dx) + simpson(4, &f[n-4], dx);
}
}
double Integral::simpson(const int n, const double* const f, const double* const h)
{
// Simpson's rule for irregularly-spaced grid
// The treatment for even number of grid points is the same as that of the regularly-spaced grid case.
assert( n >= 2 );
assert( std::all_of(h, h+(n-1), [](double x){return x > 0.0;}) );
if (n == 4)
{
double w = h[0] + h[1] + h[2];
return w / 12.0 * ( 2.0 + ((h[1]+h[2])/h[0]-1.0) * (h[2]/(h[0]+h[1])-1.0) ) * f[0]
+ std::pow(w,3) / 12.0 * (h[0]+h[1]-h[2]) / (h[0]*h[1]*(h[1]+h[2])) * f[1]
+ std::pow(w,3) / 12.0 * (h[2]+h[1]-h[0]) / (h[2]*h[1]*(h[1]+h[0])) * f[2]
+ w / 12.0 * ( 2.0 + ((h[1]+h[0])/h[2]-1.0) * (h[0]/(h[2]+h[1])-1.0) ) * f[3];
}
if (n == 2)
{
return 0.5 * h[0] * (f[0] + f[1]);
}
if (n % 2 == 1)
{
double sum = 0.0;
for (int i = 0; i < n/2; ++i)
{
double hrp = h[2*i+1] / h[2*i];
double hrm = h[2*i] / h[2*i+1];
sum += (h[2*i+1] + h[2*i]) / 6.0 * ( (2.0-hrp)*f[2*i] + (2.0+hrp+hrm)*f[2*i+1] + (2.0-hrm) * f[2*i+2]);
}
return sum;
}
else
{
return simpson(n-3, f, h) + simpson(4, &f[n-4], &h[n-4]);
}
}
void Integral::Gauss_Legendre_grid_and_weight(const int n, double *x, double *weights)
{
assert( n >= 1 );
double z = 0.0;
double z1 = 0.0;
double p1 = 1.0;
double p2 = 0.0;
double p3 = 0.0;
double pp = 0.0;
int half_grid_num = static_cast<int>((n+1)/2);
for(int i = 1; i <= half_grid_num; i++)
{
z = cos(ModuleBase::PI * (i - 0.25) / (n + 0.5));
while(true)
{
p1 = 1.0;
p2 = 0.0;
for(int j = 1; j <= n; j++)
{
p3 = p2;
p2 = p1;
p1 = ((2.0 * j - 1.0) * z * p2 - (j - 1.0) * p3) / j;
}
pp = n * (p2 - z * p1) / (1.0 - z*z);
z1 = z;
z = z1 - p1 / pp;
if (std::abs(z - z1) < 1e-13) break;
}
x[i-1] = -z;
x[n-i] = z;
weights[i-1] = 2.0 / ((1.0 - z * z) * pp * pp);
weights[n-i] = weights[i-1];
}
}
void Integral::Gauss_Legendre_grid_and_weight(const double xmin, const double xmax, const int n, double *x, double *weights)
{
assert( n >= 1 );
double xl = (xmax - xmin) * 0.5;
double xmean = (xmax + xmin) * 0.5;
double z = 0.0;
double z1 = 0.0;
double p1 = 1.0;
double p2 = 0.0;
double p3 = 0.0;
double pp = 0.0;
int half_grid_num = static_cast<int>((n+1)/2);
for(int i = 1; i <= half_grid_num; i++)
{
z = cos(ModuleBase::PI * (i - 0.25) / (n + 0.5));
while(true)
{
p1 = 1.0;
p2 = 0.0;
for(int j = 1; j <= n; j++)
{
p3 = p2;
p2 = p1;
p1 = ((2.0 * j - 1.0) * z * p2 - (j - 1.0) * p3) / j;
}
pp = n * (p2 - z * p1) / (1.0 - z*z);
z1 = z;
z = z1 - p1 / pp;
if (std::abs(z - z1) < 1e-13) break;
}
x[i-1] = xmean - xl * z;
x[n-i] = xmean + xl * z;
weights[i-1] = 2.0 * xl / ((1.0 - z * z) * pp * pp);
weights[n-i] = weights[i-1];
}
}
const double Integral::Lebedev_Laikov_grid110_x[110] = {
1.000000000000000, -1.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.577350269189626, -0.577350269189626,
0.577350269189626, -0.577350269189626, 0.577350269189626, -0.577350269189626, 0.577350269189626, -0.577350269189626, 0.185115635344736, -0.185115635344736,
0.185115635344736, -0.185115635344736, 0.185115635344736, -0.185115635344736, 0.185115635344736, -0.185115635344736, 0.185115635344736, -0.185115635344736,
0.185115635344736, -0.185115635344736, 0.185115635344736, -0.185115635344736, 0.185115635344736, -0.185115635344736, 0.965124035086594, -0.965124035086594,
0.965124035086594, -0.965124035086594, 0.965124035086594, -0.965124035086594, 0.965124035086594, -0.965124035086594, 0.690421048382292, -0.690421048382292,
0.690421048382292, -0.690421048382292, 0.690421048382292, -0.690421048382292, 0.690421048382292, -0.690421048382292, 0.690421048382292, -0.690421048382292,
0.690421048382292, -0.690421048382292, 0.690421048382292, -0.690421048382292, 0.690421048382292, -0.690421048382292, 0.215957291845848, -0.215957291845848,
0.215957291845848, -0.215957291845848, 0.215957291845848, -0.215957291845848, 0.215957291845848, -0.215957291845848, 0.395689473055942, -0.395689473055942,
0.395689473055942, -0.395689473055942, 0.395689473055942, -0.395689473055942, 0.395689473055942, -0.395689473055942, 0.395689473055942, -0.395689473055942,
0.395689473055942, -0.395689473055942, 0.395689473055942, -0.395689473055942, 0.395689473055942, -0.395689473055942, 0.828769981252592, -0.828769981252592,
0.828769981252592, -0.828769981252592, 0.828769981252592, -0.828769981252592, 0.828769981252592, -0.828769981252592, 0.478369028812150, -0.478369028812150,
0.478369028812150, -0.478369028812150, 0.878158910604066, -0.878158910604066, 0.878158910604066, -0.878158910604066, 0.478369028812150, -0.478369028812150,
0.478369028812150, -0.478369028812150, 0.878158910604066, -0.878158910604066, 0.878158910604066, -0.878158910604066, 0.000000000000000, 0.000000000000000,
0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000
};
const double Integral::Lebedev_Laikov_grid110_y[110] = {
0.000000000000000, 0.000000000000000, 1.000000000000000, -1.000000000000000, 0.000000000000000, 0.000000000000000, 0.577350269189626, 0.577350269189626,
-0.577350269189626, -0.577350269189626, 0.577350269189626, 0.577350269189626, -0.577350269189626, -0.577350269189626, 0.185115635344736, 0.185115635344736,
-0.185115635344736, -0.185115635344736, 0.185115635344736, 0.185115635344736, -0.185115635344736, -0.185115635344736, 0.965124035086594, 0.965124035086594,
-0.965124035086594, -0.965124035086594, 0.965124035086594, 0.965124035086594, -0.965124035086594, -0.965124035086594, 0.185115635344736, 0.185115635344736,
-0.185115635344736, -0.185115635344736, 0.185115635344736, 0.185115635344736, -0.185115635344736, -0.185115635344736, 0.690421048382292, 0.690421048382292,
-0.690421048382292, -0.690421048382292, 0.690421048382292, 0.690421048382292, -0.690421048382292, -0.690421048382292, 0.215957291845848, 0.215957291845848,
-0.215957291845848, -0.215957291845848, 0.215957291845848, 0.215957291845848, -0.215957291845848, -0.215957291845848, 0.690421048382292, 0.690421048382292,
-0.690421048382292, -0.690421048382292, 0.690421048382292, 0.690421048382292, -0.690421048382292, -0.690421048382292, 0.395689473055942, 0.395689473055942,
-0.395689473055942, -0.395689473055942, 0.395689473055942, 0.395689473055942, -0.395689473055942, -0.395689473055942, 0.828769981252592, 0.828769981252592,
-0.828769981252592, -0.828769981252592, 0.828769981252592, 0.828769981252592, -0.828769981252592, -0.828769981252592, 0.395689473055942, 0.395689473055942,
-0.395689473055942, -0.395689473055942, 0.395689473055942, 0.395689473055942, -0.395689473055942, -0.395689473055942, 0.878158910604066, 0.878158910604066,
-0.878158910604066, -0.878158910604066, 0.478369028812150, 0.478369028812150, -0.478369028812150, -0.478369028812150, 0.000000000000000, 0.000000000000000,
0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.478369028812150, -0.478369028812150,
0.478369028812150, -0.478369028812150, 0.878158910604066, -0.878158910604066, 0.878158910604066, -0.878158910604066
};
const double Integral::Lebedev_Laikov_grid110_z[110] = {
0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 1.000000000000000, -1.000000000000000, 0.577350269189626, 0.577350269189626,
0.577350269189626, 0.577350269189626, -0.577350269189626, -0.577350269189626, -0.577350269189626, -0.577350269189626, 0.965124035086594, 0.965124035086594,
0.965124035086594, 0.965124035086594, -0.965124035086594, -0.965124035086594, -0.965124035086594, -0.965124035086594, 0.185115635344736, 0.185115635344736,
0.185115635344736, 0.185115635344736, -0.185115635344736, -0.185115635344736, -0.185115635344736, -0.185115635344736, 0.185115635344736, 0.185115635344736,
0.185115635344736, 0.185115635344736, -0.185115635344736, -0.185115635344736, -0.185115635344736, -0.185115635344736, 0.215957291845848, 0.215957291845848,
0.215957291845848, 0.215957291845848, -0.215957291845848, -0.215957291845848, -0.215957291845848, -0.215957291845848, 0.690421048382292, 0.690421048382292,
0.690421048382292, 0.690421048382292, -0.690421048382292, -0.690421048382292, -0.690421048382292, -0.690421048382292, 0.690421048382292, 0.690421048382292,
0.690421048382292, 0.690421048382292, -0.690421048382292, -0.690421048382292, -0.690421048382292, -0.690421048382292, 0.828769981252592, 0.828769981252592,
0.828769981252592, 0.828769981252592, -0.828769981252592, -0.828769981252592, -0.828769981252592, -0.828769981252592, 0.395689473055942, 0.395689473055942,
0.395689473055942, 0.395689473055942, -0.395689473055942, -0.395689473055942, -0.395689473055942, -0.395689473055942, 0.395689473055942, 0.395689473055942,
0.395689473055942, 0.395689473055942, -0.395689473055942, -0.395689473055942, -0.395689473055942, -0.395689473055942, 0.000000000000000, 0.000000000000000,
0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, 0.878158910604066, 0.878158910604066,
-0.878158910604066, -0.878158910604066, 0.478369028812150, 0.478369028812150, -0.478369028812150, -0.478369028812150, 0.878158910604066, 0.878158910604066,
-0.878158910604066, -0.878158910604066, 0.478369028812150, 0.478369028812150, -0.478369028812150, -0.478369028812150
};
const double Integral::Lebedev_Laikov_grid110_w[110] = {
0.048107465851397, 0.048107465851397, 0.048107465851397, 0.048107465851397, 0.048107465851397, 0.048107465851397, 0.123071735281670, 0.123071735281670,
0.123071735281670, 0.123071735281670, 0.123071735281670, 0.123071735281670, 0.123071735281670, 0.123071735281670, 0.103191734088330, 0.103191734088330,
0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330,
0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330,
0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.103191734088330, 0.124945096872513, 0.124945096872513,
0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513,
0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513,
0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.124945096872513, 0.120580249028528, 0.120580249028528,
0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528,
0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528,
0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.120580249028528, 0.121830917385521, 0.121830917385521,
0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521,
0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521,
0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521, 0.121830917385521
};
}