-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintegrate.c
executable file
·328 lines (270 loc) · 11.7 KB
/
integrate.c
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
/***************************************************************************
integrate.c - do Kramers-Kronig integration
-------------------
begin : Sat July 6 12:01:02 GMT 2002
copyright : (C) 2002 by Gwyndaf Evans
email : gwyndaf@gwyndafevans.co.uk
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include "chooch.h"
extern char *sElement;
gsl_interp_accel *acc;
gsl_spline *spline;
void Integrate(int nDataPoints, int *nPoints, double fEdge, double *fXraw, double *fXfpp,
double *fYspline, double *fYfpp, double *fD1, double *fD2,
double *fD3, double *fYfp)
{
extern int verbose;
extern double fEres;
int i;
/* double fp, error=0.0, E0, dE; changed for next line for v5.0.8*/
double error=0.0, E0, dE;
double fElo, fEhi;
fElo=fEdge/1000.0;
fEhi=fEdge*50.0;
acc=gsl_interp_accel_alloc();
// spline=gsl_spline_alloc(gsl_interp_akima, nDataPoints);
spline=gsl_spline_alloc(gsl_interp_linear, nDataPoints);
gsl_spline_init(spline, fXraw, fYfpp, nDataPoints);
/*
*dE=(fXraw[nDataPoints-1]-fXraw[0])/(nDataPoints-1);
*/
if(verbose>0){
printf("****************************\n");
printf(" Integrate.c \n");
printf("****************************\n");
}
dE=0.01;
// dE=fEres*fXraw[0]/5.0; // Changed so we define it as a fixed, very small, interval. See below **
if(verbose>0)printf("Energy interval used for integrating around singularity = %f\n", dE);
// for (i=0, E0=fXraw[0]+dE; E0<=fXraw[nDataPoints-1]-dE; E0+=dE, i++)
/******************************************************************
* First calculate for all points on spectrum except first and last
******************************************************************/
if(verbose>2)printf("/******************************************************************\n");
if(verbose>2)printf(" * First calculate for all points on spectrum except first and last\n");
if(verbose>2)printf(" ******************************************************************\n");
for (i=1; i<nDataPoints-1; i++)
{
E0=fXraw[i];
if(verbose>2)printf("\n\n=====\nPoint No. i=%d x=%f y=%f\n", i, E0, fYfpp[i]);
fYfp[i]=0.0;
/* Exrapolate to low energy */
if(verbose>2)printf("\n**************\nIntegration 1 E0=%f a=%f b=%f \n**************\n", E0, fElo, fXraw[0]);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, fElo, fXraw[0], error);
if(verbose>2)printf(" Sum of fp so far = %f \n", fYfp[i]);
/* From first data point up to singularity E0-dE */
if(verbose>2)printf("\n**************\nIntegration 2 E0=%f a=%f b=%f \n**************\n", E0, fXraw[0], E0-dE);
fYfp[i]+=IntegrateCurve(nDataPoints, E0, fXraw[0], E0-dE);
if(verbose>2)printf(" Sum of fp so far = %f \n", fYfp[i]);
/* Singularity */
if(verbose>2)printf("**************\nSingularity\n**************\n");
fYfp[i]+=Singularity(E0, E0-dE, E0+dE, fYfpp[i], fYfpp[i-1], fYfpp[i+1], fD1[i], fD2[i], fD3[i]);
if(verbose>2)printf(" Final SUM of fp so far = %f \n", fYfp[i]);
/* From singularity E0+dE up to last data point */
if(verbose>2)printf("\n**************\nIntegration 3 E0=%f a=%f b=%f \n**************\n", E0, E0+dE, fXraw[nDataPoints-1]);
fYfp[i]+=IntegrateCurve(nDataPoints, E0, E0+dE, fXraw[nDataPoints-1]);
if(verbose>2)printf(" Sum of fp so far = %f \n",fYfp[i]);
/* Extrapolate to high energy */
if(verbose>2)printf("\n**************\nIntegration 4 E0=%f a=%f b=%f \n**************\n", E0, fXraw[nDataPoints-1], fEhi);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, fXraw[nDataPoints-1], fEhi, error);
if(verbose>2)printf(" Final value of fp = %f \n", fYfp[i]);
fXfpp[i]=E0;
fYspline[i]=gsl_spline_eval(spline, E0, acc);
}
/*******************************
* Now calculate for first point
*******************************/
if(verbose>2)printf(" *******************************\n");
if(verbose>2)printf(" * Now calculate for first point\n");
if(verbose>2)printf(" *******************************\n");
i=0;
fYfp[i]=0.0;
/* **
Because the spline fit used by Singularity routine is only defined within data
range fXraw[0] to fXraw[nDataPoints-1] we must cheat here and define E0 as E0 plus a very small number
equal to the integration range either side of the singularity, so that we only call spline values
within the total range of our data. dE is this value and we have set it to be 0.01 eV
*/
E0=fXraw[0]+dE;
/* Exrapolate to low energy */
if(verbose>2)printf("Integration 1 E0=%f a=%f b=%f \n", E0, fElo, fXraw[0]-dE);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, fElo, fXraw[0]-dE, error);
if(verbose>2)printf(" Sum of fp so far = %f \n", fYfp[i]);
/* Singularity */
if(verbose>2)printf("Singularity input variables E0=%f %f %f %f %f %f %f %f %f \n",E0, E0-dE, E0+dE, fYfpp[i], fYfpp[i], fYfpp[i], fD1[i], fD2[i], fD3[i] );
fYfp[i]+=Singularity(E0, E0-dE, E0+dE, fYfpp[i], fYfpp[i], fYfpp[i], fD1[i], fD2[i], fD3[i]);
if(verbose>2)printf(" Final SUM of fp so far = %f \n", fYfp[i]);
/* From singularity E0 up to last data point */
if(verbose>2)printf("Integration 3 E0=%f a=%f b=%f \n", E0, E0+dE, fXraw[nDataPoints-1]);
fYfp[i]+=IntegrateCurve(nDataPoints, E0, E0+dE, fXraw[nDataPoints-1]);
// if(verbose>2)printf(" Sum of fp so far = %f \n",fYfp[i]);
/* Extrapolate to high energy */
// if(verbose>2)printf("Integration 4 E0=%f a=%f b=%f \n", E0, fXraw[nDataPoints-1], fEhi);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, fXraw[nDataPoints-1], fEhi, error);
// if(verbose>2)printf(" Final value of fp = %f \n", fYfp[i]);
fXfpp[i]=E0;
fYspline[i]=gsl_spline_eval(spline, E0, acc);
fXfpp[i]=E0;
fYspline[i]=gsl_spline_eval(spline, E0, acc);
/*********************************
* Now calculate for last point
*********************************/
if(verbose>2)printf(" *******************************\n");
if(verbose>2)printf(" * Now calculate for last point\n");
if(verbose>2)printf(" *******************************\n");
i=nDataPoints-1;
fYfp[i]=0.0;
/* We used the same trick as above for the last point. Define E0 as a little bit less that max data range. */
E0=fXraw[i]-dE;
/* Exrapolate to low energy */
if(verbose>2)printf("Integration 1 E0=%f a=%f b=%f \n", E0, fElo, fXraw[0]-dE);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, fElo, fXraw[0], error);
if(verbose>2)printf(" Sum of fp so far = %f \n", fYfp[i]);
/* From first data point up to singularity E0 */
if(verbose>2)printf("Integration 2 E0=%f a=%f b=%f \n", E0, fXraw[0], E0-dE);
fYfp[i]+=IntegrateCurve(nDataPoints, E0, fXraw[0], E0-dE);
if(verbose>2)printf(" Sum of fp so far = %f \n", fYfp[i]);
/* Singularity */
if(verbose>2)printf("Singularity\n");
fYfp[i]+=Singularity(E0, E0-dE, E0+dE, fYfpp[i], fYfpp[i], fYfpp[i], fD1[i], fD2[i], fD3[i]);
if(verbose>2)printf(" Final SUM of fp so far = %f \n", fYfp[i]);
/* Extrapolate to high energy */
if(verbose>2)printf("Integration 4 E0=%f a=%f b=%f \n", E0, fXraw[nDataPoints-1], fEhi);
fYfp[i]+=IntegrateExtrap(nDataPoints, E0, E0+dE, fEhi, error);
if(verbose>2)printf(" Final value of fp = %f \n", fYfp[i]);
fXfpp[i]=E0;
fYspline[i]=gsl_spline_eval(spline, E0, acc);
fXfpp[i]=E0;
fYspline[i]=gsl_spline_eval(spline, E0, acc);
/* *nPoints=i-1;*/
/* Changed 16/9/2004 to test bug from Chuck Farrah */
*nPoints=i;
/*
*/
gsl_spline_free (spline);
gsl_interp_accel_free(acc);
}
double IntegrateExtrap(int N, double E0, double a, double b, double error)
{
extern int verbose, status;
double result;
gsl_integration_workspace * w=gsl_integration_workspace_alloc(3000);
gsl_function F;
F.function = &f;
F.params = &E0;
if(verbose>2)printf("Integrating Extrap\n");
status=gsl_integration_qag(&F, a, b, 1e-3, 1e-3, 3000, 3, w, &result, &error);
if(status != 0){
printf ("gsl error: %d : %s\n", status, gsl_strerror (status));
exit(EXIT_FAILURE);
}
result=2.0*result/PI;
if(verbose>2){
printf("result = % .18f\n", result);
printf("estimated error = % .18f\n", error);
printf("intervals = %lu\n", w->size);
}
gsl_integration_workspace_free(w);
return result;
}
double IntegrateCurve(int N, double E0, double a, double b){
extern int verbose, status;
double result, error;
gsl_integration_workspace * w
= gsl_integration_workspace_alloc(3000);
gsl_function F;
F.function = &fc;
F.params = &E0;
if(verbose>1)printf("Integrating curve\n");
status=gsl_integration_qag (&F, a, b, 1e-3, 1e-3, 3000, 5, w, &result, &error);
if(status != 0){
printf ("gsl error: %d : %s\n", status, gsl_strerror (status));
exit(EXIT_FAILURE);
}
result=2.0*result/PI;
if(verbose>2){
printf("result = % .18f\n", result);
printf("estimated error = % .18f\n", error);
printf("intervals are = %lu \n", w->size);
}
gsl_integration_workspace_free(w);
return result;
}
double Singularity(double E0, double a, double b,
double fppE0, double fppa, double fppb,
double fD1, double fD2, double fD3){
extern int verbose, status;
double result, error;
gsl_integration_workspace * w
= gsl_integration_workspace_alloc(50);
double d1, d2, test;
double term1, term2, term3, term4, term5;
gsl_function F;
F.function = &fs;
F.params = &E0;
if(verbose>2)printf("Integrating Singularity\n");
// if(verbose>2)printf("E0=%f a=%f b=%f w=%d\n", E0, a, b, w);
status=gsl_integration_qag (&F, a, b, 1.0e-3, 1.0e-3, 50, 6, w, &term1, &error);
if(status != 0){
printf ("gsl error: %d : %s\n", status, gsl_strerror (status));
exit(EXIT_FAILURE);
}
d1 = a-E0;
d2 = b-E0;
term2 = -1.0*(log(fabs(d2)) - log(fabs(d1)));
term3= -1.0*fD1*(b-a);
term4 = -0.25*fD2*(d2*d2-d1*d1);
term5= -1.0*fD3*(d2*d2*d2-d1*d1*d1)/18.0;
result = (term1+term2+term3+term4+term5)/PI;
if(verbose>2)printf("%f + %f + %f + %f + %f / PI = %f\n", term1, term2, term3, term4, term5, result);
return result;
}
/*
* Functions for QAG integrator
*
*
*
*/
double f(double E, void * params) {
double answer;
double E0 = *(double *) params;
answer = E*get_fpp(sElement, E/1000.0)/(E0*E0-E*E);
return answer;
}
double fc(double E, void * params) {
extern gsl_spline *spline;
extern gsl_interp_accel *acc;
double answer,fdp;
double E0 = *(double *) params;
fdp = gsl_spline_eval(spline, E, acc);
answer = E*fdp/(E0*E0-E*E);
return answer;
}
double fs(double E, void * params) {
extern gsl_spline *spline;
extern gsl_interp_accel *acc;
double answer,fdp;
double E0 = *(double *) params;
fdp = gsl_spline_eval(spline, E, acc);
answer = -1.0*fdp/(E0+E);
// if(E<E0)printf("out of range ");
// printf("E = %f E0 = %f fdp = %f answer = %f \n", E, E0, fdp, answer);
return answer;
}