forked from AFD-Illinois/igrmonty2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jnu_mixed.c
472 lines (364 loc) · 10.2 KB
/
jnu_mixed.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "decs.h"
//#include "gsl_sf_gamma.h"
//#pragma omp threadprivate(r)
/*
"mixed" emissivity formula
interpolates between Petrosian limit and
classical thermal synchrotron limit
good for Thetae > 1
*/
// For kappa = 5
//#define GAM1 (4.0122013020041507)
//#define GAM2 (2.)
//#define GAM3 (1.0555465648134663)
//#define GAM4 (1.411932800087401)
double GAM1, GAM2, GAM3, GAM4;
double jnu_synch(double nu, double Ne, double Thetae, double B, double theta);
double jnu_kappa(double nu, double Ne, double Thetae, double B, double theta);
double jnu_bremss(double nu, double Ne, double Thetae);
double int_jnu_synch(double Ne, double Thetae, double Bmag, double nu);
double int_jnu_kappa(double Ne, double Thetae, double Bmag, double nu);
double int_jnu_bremss(double Ne, double Thetae, double nu);
double jnu(double nu, double Ne, double Thetae, double B, double theta)
{
double j = 0.;
#if SYNCHROTRON
#if DIST_KAPPA
j += jnu_kappa(nu, Ne, Thetae, B, theta);
#else
j += jnu_synch(nu, Ne, Thetae, B, theta);
#endif
#endif
#if BREMSSTRAHLUNG
j += jnu_bremss(nu, Ne, Thetae);
#endif
return j;
}
double int_jnu(double Ne, double Thetae, double B, double nu)
{
double intj = 0.;
#if SYNCHROTRON
#if DIST_KAPPA
intj += int_jnu_kappa(Ne, Thetae, B, nu);
#else
intj += int_jnu_synch(Ne, Thetae, B, nu);
#endif
#endif
#if BREMSSTRAHLUNG
intj += int_jnu_bremss(Ne, Thetae, nu);
#endif
return intj;
}
double jnu_bremss(double nu, double Ne, double Thetae)
{
if (Thetae < THETAE_MIN)
return 0;
double Te = Thetae*ME*CL*CL/KBOL;
double rel = (1. + 4.4e-10*Te);
double x, efac;
double gff = 1.2;
x = HPL*nu/(KBOL*Te);
if (x < 1.e-3) {
efac = (24 - 24*x + 12*x*x - 4.*x*x*x + x*x*x*x)/24.;
} else {
efac = exp(-x);
}
double jv = 1./(4.*M_PI)*pow(2,5)*M_PI*pow(EE,6)/(3.*ME*pow(CL,3));
jv *= pow(2.*M_PI/(3.*KBOL*ME),1./2.);
jv *= pow(Te,-1./2.)*Ne*Ne;
jv *= efac*rel*gff;
return jv;
}
#define CST 1.88774862536 /* 2^{11/12} */
double jnu_synch(double nu, double Ne, double Thetae, double B,
double theta)
{
double K2, nuc, nus, x, f, j, sth, xp1, xx;
double K2_eval(double Thetae);
if (Thetae < THETAE_MIN)
return 0.;
K2 = K2_eval(Thetae);
nuc = EE * B / (2. * M_PI * ME * CL);
sth = sin(theta);
nus = (2. / 9.) * nuc * Thetae * Thetae * sth;
if (nu > 1.e12 * nus)
return (0.);
x = nu / nus;
xp1 = pow(x, 1. / 3.);
xx = sqrt(x) + CST * sqrt(xp1);
f = xx * xx;
j = (M_SQRT2 * M_PI * EE * EE * Ne * nus / (3. * CL * K2)) * f *
exp(-xp1);
return (j);
}
#include <gsl/gsl_sf_gamma.h>
double jnu_kappa(double nu, double Ne, double Thetae, double B, double theta)
{
if (Thetae < THETAE_MIN)
return 0.;
if (theta < SMALL || theta > M_PI-SMALL)
return 0.;
double kap = KAPPA;
double nuc = EE * B / (2. * M_PI * ME * CL);
double js = Ne*pow(EE,2)*nuc/CL;
double x = 3.*pow(kap,-3./2.);
double Jslo, Jshi;
double nuk = nuc*pow(Thetae*kap,2)*sin(theta);
double Xk = nu/nuk;
Jslo = pow(Xk,1./3.)*sin(theta)*4.*M_PI*gsl_sf_gamma(kap-4./3.)/(pow(3.,7./3.)*gsl_sf_gamma(kap-2.));
Jshi = pow(Xk,-(kap-2.)/2.)*sin(theta)*pow(3.,(kap-1.)/2.);
Jshi *= (kap-2.)*(kap-1.)/4.*gsl_sf_gamma(kap/4.-1./3.)*gsl_sf_gamma(kap/4.+4./3.);
double Js = pow(pow(Jslo,-x) + pow(Jshi,-x),-1./x);
if (isnan(js*Js) || js*Js < 0. || js*Js > 1.e200 || js*Js < 1.e-100) {
printf("BAD jkap! %e\n", js*Js);
printf("nu Ne Thetae B theta = %e %e %e %e %e\n", nu, Ne, Thetae, B, theta);
exit(-1);
}
if (isnan(Jslo) || isinf(Jslo) || Jslo < 0. || Jslo > 1.e100) {
printf("JSLO ERROR! %e\n", Jslo);
}
double cut = exp(-nu/NUCUT);
return js*Js*cut;
}
#undef CST
#define JCST (M_SQRT2*EE*EE*EE/(27*ME*CL*CL))
double int_jnu_synch(double Ne, double Thetae, double Bmag, double nu)
{
/* Returns energy per unit time at *
* frequency nu in cgs */
double j_fac, K2;
double F_eval(double Thetae, double B, double nu);
double K2_eval(double Thetae);
if (Thetae < THETAE_MIN)
return 0.;
K2 = K2_eval(Thetae);
if (K2 == 0.)
return 0.;
j_fac = Ne * Bmag * Thetae * Thetae / K2;
return JCST * j_fac * F_eval(Thetae, Bmag, nu);
}
double jnu_kappa_integrand(double th, void *params)
{
double K = *(double *) params;
double sth = sin(th);
double Xk = K / sth;
double kap = KAPPA;
if (sth < 1.e-150 || Xk > 2.e8)
return 0.;
double Jslo, Jshi, Js;
//Jslo = pow(Xk,1./3.)*sth*4.*M_PI*gsl_sf_gamma(kap-4./3.)/(pow(3.,7./3.)*gsl_sf_gamma(kap-2.));
//Jshi = pow(Xk,-(kap-2.)/2.)*sth*pow(3.,(kap-1.)/2.)*(kap-2.)*(kap-1.)/4.*gsl_sf_gamma(kap/4.-1./3.)*gsl_sf_gamma(kap/4.+4./3.);
Jslo = pow(Xk,1./3.)*sth*4.*M_PI*GAM1/(pow(3.,7./3.)*GAM2);
Jshi = pow(Xk,-(kap-2.)/2.)*sth*pow(3.,(kap-1.)/2.)*(kap-2.)*(kap-1.)/4.*GAM3*GAM4;
double x = 3.*pow(kap,-3./2.);
Js = pow(pow(Jslo,-x) + pow(Jshi,-x),-1./x);
return Js;
}
//#define EPSABS (0.)
//#define EPSREL (1.e-6)
double int_jnu_kappa(double Ne, double Thetae, double B, double nu)
{
/* Returns energy per unit time at *
* frequency nu in cgs */
double G_eval(double Thetae, double B, double nu);
if (Thetae < THETAE_MIN)
return 0.;
double nuc = EE*B/(2.*M_PI*ME*CL);
double js = Ne*EE*EE*nuc/CL;
double cut = exp(-nu/NUCUT);
return js*G_eval(Thetae, B, nu)*cut;
/*
double K = 2.*M_PI*ME*CL*nu/(EE*B*pow(Thetae*KAPPA,2));
double result, err;
gsl_function func;
gsl_integration_workspace *w;
func.function = &jnu_kappa_integrand;
func.params = &K;
w = gsl_integration_workspace_alloc(1000);
gsl_integration_qag(&func, 0., M_PI / 2., EPSABS, EPSREL, 1000,
GSL_INTEG_GAUSS61, w, &result, &err);
gsl_integration_workspace_free(w);
return 4.*M_PI*result;*/
}
//#undef EPSABS
//#undef EPSREL
#undef JCST
double int_jnu_bremss(double Ne, double Thetae, double nu)
{
return 4.*M_PI*jnu_bremss(nu, Ne, Thetae);
}
#define CST 1.88774862536 /* 2^{11/12} */
double jnu_integrand(double th, void *params)
{
double K = *(double *) params;
double sth = sin(th);
double x = K / sth;
if (sth < 1.e-150 || x > 2.e8)
return 0.;
return sth * sth * pow(sqrt(x) + CST * pow(x, 1. / 6.),
2.) * exp(-pow(x, 1. / 3.));
}
#undef CST
/* Tables */
double F[N_ESAMP + 1], G[N_ESAMP + 1], K2[N_ESAMP + 1];
double lK_min, dlK;
double lL_min, dlL;
double lT_min, dlT;
#define EPSABS 0.
#define EPSREL 1.e-6
#define KMIN (0.002)
#define KMAX (1.e7)
#define LMIN (0.002)
#define LMAX (1.e7)
#define TMIN (THETAE_MIN)
#define TMAX (1.e2)
void init_emiss_tables(void)
{
int k;
double result, err, K, T;
// Thermal synchrotron lookup table
{
gsl_function func;
gsl_integration_workspace *w;
func.function = &jnu_integrand;
func.params = &K;
lK_min = log(KMIN);
dlK = log(KMAX / KMIN) / (N_ESAMP);
/* build table for F(K) where F(K) is given by
\int_0^\pi ( (K/\sin\theta)^{1/2} + 2^{11/12}(K/\sin\theta)^{1/6})^2 \exp[-(K/\sin\theta)^{1/3}]
so that J_{\nu} = const.*F(K)
*/
w = gsl_integration_workspace_alloc(1000);
for (k = 0; k <= N_ESAMP; k++) {
K = exp(k * dlK + lK_min);
gsl_integration_qag(&func, 0., M_PI / 2., EPSABS, EPSREL,
1000, GSL_INTEG_GAUSS61, w, &result,
&err);
F[k] = log(4 * M_PI * result);
}
gsl_integration_workspace_free(w);
}
// Kappa synchrotron lookup table
{
// Store & evaluate Gamma functions
GAM1 = gsl_sf_gamma(KAPPA - 4./3.);
GAM2 = gsl_sf_gamma(KAPPA - 2.);
GAM3 = gsl_sf_gamma(KAPPA/4. - 1./3.);
GAM4 = gsl_sf_gamma(KAPPA/4. + 4./3.);
double L;
gsl_function func;
gsl_integration_workspace *w;
func.function = &jnu_kappa_integrand;
func.params = &L;
lL_min = log(LMIN);
dlL = log(LMAX / LMIN) / (N_ESAMP);
/* build table for G(L) where G(L) is given by
2 \pi \int_0^\pi ...( (K/\sin\theta)^{1/2} + 2^{11/12}(K/\sin\theta)^{1/6})^2 \exp[-(K/\sin\theta)^{1/3}]
so that J_{\nu} = const.*G(L)
*/
w = gsl_integration_workspace_alloc(1000);
for (k = 0; k <= N_ESAMP; k++) {
L = exp(k * dlL + lL_min);
gsl_integration_qag(&func, 0., M_PI / 2., EPSABS, EPSREL, 1000,
GSL_INTEG_GAUSS61, w, &result, &err);
G[k] = log(4*M_PI*result);
}
gsl_integration_workspace_free(w);
}
// Bessel K2 lookup table
{
lT_min = log(TMIN);
dlT = log(TMAX / TMIN) / (N_ESAMP);
for (k = 0; k <= N_ESAMP; k++) {
T = exp(k * dlT + lT_min);
K2[k] = log(gsl_sf_bessel_Kn(2, 1. / T));
}
}
/* Avoid doing divisions later */
//dlK = 1. / dlK;
//dlT = 1. / dlT;
fprintf(stderr, "done.\n\n");
return;
}
/* rapid evaluation of K_2(1/\Thetae) */
double K2_eval(double Thetae)
{
double linear_interp_K2(double);
if (Thetae < THETAE_MIN)
return 0.;
if (Thetae > TMAX)
return 2. * Thetae * Thetae;
return linear_interp_K2(Thetae);
}
#define KFAC (9*M_PI*ME*CL/EE)
double F_eval(double Thetae, double Bmag, double nu)
{
double K, x;
double linear_interp_F(double);
K = KFAC * nu / (Bmag * Thetae * Thetae);
if (K > KMAX) {
return 0.;
} else if (K < KMIN) {
/* use a good approximation */
x = pow(K, 0.333333333333333333);
return (x * (37.67503800178 + 2.240274341836 * x));
} else {
return linear_interp_F(K);
}
}
#define GFAC (2.*M_PI*ME*CL/EE)
double G_eval(double Thetae, double Bmag, double nu)
{
double L;
double linear_interp_G(double);
L = GFAC*nu/(Bmag*pow(Thetae*KAPPA,2.));
//K = KFAC * nu / (Bmag * Thetae * Thetae);
if (L > LMAX) {
return 0.;
} else if (L < LMIN) {
/* use a good approximation */
//x = pow(K, 0.333333333333333333);
//return (x * (37.67503800178 + 2.240274341836 * x));
return 0.;
} else {
return linear_interp_G(L);
}
}
#undef KFAC
#undef KMIN
#undef KMAX
#undef GFAC
#undef LMIN
#undef LMAX
#undef EPSABS
#undef EPSREL
double linear_interp_K2(double Thetae)
{
int i;
double di, lT;
lT = log(Thetae);
di = (lT - lT_min)/dlT;
i = (int) di;
di = di - i;
return exp((1. - di) * K2[i] + di * K2[i + 1]);
}
double linear_interp_F(double K)
{
int i;
double di, lK;
lK = log(K);
di = (lK - lK_min)/dlK;
i = (int) di;
di = di - i;
return exp((1. - di) * F[i] + di * F[i + 1]);
}
double linear_interp_G(double L)
{
int i;
double di, lL;
lL = log(L);
di = (lL - lL_min)/dlL;
i = (int) di;
di = di - i;
return exp((1. - di) * G[i] + di * G[i + 1]);
}