forked from pseudotensor/grmonty
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrack_super_photon.c
312 lines (264 loc) · 8.27 KB
/
track_super_photon.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
/***********************************************************************************
Copyright 2013 Joshua C. Dolence, Charles F. Gammie, Monika Mo\'scibrodzka,
and Po Kin Leung
GRMONTY version 1.0 (released February 1, 2013)
This file is part of GRMONTY. GRMONTY v1.0 is a program that calculates the
emergent spectrum from a model using a Monte Carlo technique.
This version of GRMONTY is configured to use input files from the HARM code
available on the same site. It assumes that the source is a plasma near a
black hole described by Kerr-Schild coordinates that radiates via thermal
synchrotron and inverse compton scattering.
You are morally obligated to cite the following paper in any
scientific literature that results from use of any part of GRMONTY:
Dolence, J.C., Gammie, C.F., Mo\'scibrodzka, M., \& Leung, P.-K. 2009,
Astrophysical Journal Supplement, 184, 387
Further, we strongly encourage you to obtain the latest version of
GRMONTY directly from our distribution website:
http://rainman.astro.illinois.edu/codelib/
GRMONTY 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.
GRMONTY is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GRMONTY; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************************/
/*
main transport subroutine for tracking, absorbing,
and scattering superphotons
assumes superphotons do not step out of simulation then back in
*/
#include "decs.h"
#define MAXNSTEP 1280000
void track_super_photon(struct of_photon *ph)
{
int bound_flag;
double dtau_scatt, dtau_abs, dtau;
double bi, bf;
double alpha_scatti, alpha_scattf;
double alpha_absi, alpha_absf;
double dl, x1;
double nu, Thetae, Ne, B, theta;
struct of_photon php;
double dtauK, frac;
double bias = 0.;
double Xi[NDIM], Ki[NDIM], dKi[NDIM], E0;
double Gcov[NDIM][NDIM], Ucon[NDIM], Ucov[NDIM], Bcon[NDIM],
Bcov[NDIM];
int nstep = 0;
/* quality control */
if (isnan(ph->X[0]) ||
isnan(ph->X[1]) ||
isnan(ph->X[2]) ||
isnan(ph->X[3]) ||
isnan(ph->K[0]) ||
isnan(ph->K[1]) ||
isnan(ph->K[2]) || isnan(ph->K[3]) || ph->w == 0.) {
fprintf(stderr, "track_super_photon: bad input photon.\n");
fprintf(stderr,
"X0,X1,X2,X3,K0,K1,K2,K3,w,nscatt: %g %g %g %g %g %g %g %g %g %d\n",
ph->X[0], ph->X[1], ph->X[2], ph->X[3], ph->K[0],
ph->K[1], ph->K[2], ph->K[3], ph->w, ph->nscatt);
return;
}
dtauK = 2. * M_PI * L_unit / (ME * CL * CL / HBAR);
/* Initialize opacities */
gcov_func(ph->X, Gcov);
get_fluid_params(ph->X, Gcov, &Ne, &Thetae, &B, Ucon, Ucov, Bcon,
Bcov);
theta = get_bk_angle(ph->X, ph->K, Ucov, Bcov, B);
nu = get_fluid_nu(ph->X, ph->K, Ucov);
alpha_scatti = alpha_inv_scatt(nu, Thetae, Ne);
alpha_absi = alpha_inv_abs(nu, Thetae, Ne, B, theta);
bi = bias_func(Thetae, ph->w);
/* Initialize dK/dlam */
init_dKdlam(ph->X, ph->K, ph->dKdlam);
while (!stop_criterion(ph)) {
/* Save initial position/wave vector */
Xi[0] = ph->X[0];
Xi[1] = ph->X[1];
Xi[2] = ph->X[2];
Xi[3] = ph->X[3];
Ki[0] = ph->K[0];
Ki[1] = ph->K[1];
Ki[2] = ph->K[2];
Ki[3] = ph->K[3];
dKi[0] = ph->dKdlam[0];
dKi[1] = ph->dKdlam[1];
dKi[2] = ph->dKdlam[2];
dKi[3] = ph->dKdlam[3];
E0 = ph->E0s;
/* evaluate stepsize */
dl = stepsize(ph->X, ph->K);
/* step the geodesic */
push_photon(ph->X, ph->K, ph->dKdlam, dl, &(ph->E0s), 0);
if (stop_criterion(ph))
break;
/* allow photon to interact with matter, */
gcov_func(ph->X, Gcov);
get_fluid_params(ph->X, Gcov, &Ne, &Thetae, &B, Ucon, Ucov,
Bcon, Bcov);
if (alpha_absi > 0. || alpha_scatti > 0. || Ne > 0.) {
bound_flag = 0;
if (Ne == 0.)
bound_flag = 1;
if (!bound_flag) {
theta =
get_bk_angle(ph->X, ph->K, Ucov, Bcov,
B);
nu = get_fluid_nu(ph->X, ph->K, Ucov);
if (isnan(nu)) {
fprintf(stderr,
"isnan nu: track_super_photon dl,E0 %g %g\n",
dl, E0);
fprintf(stderr,
"Xi, %g %g %g %g\n", Xi[0],
Xi[1], Xi[2], Xi[3]);
fprintf(stderr,
"Ki, %g %g %g %g\n", Ki[0],
Ki[1], Ki[2], Ki[3]);
fprintf(stderr,
"dKi, %g %g %g %g\n",
dKi[0], dKi[1], dKi[2],
dKi[3]);
exit(1);
}
}
/* scattering optical depth along step */
if (bound_flag || nu < 0.) {
dtau_scatt =
0.5 * alpha_scatti * dtauK * dl;
dtau_abs = 0.5 * alpha_absi * dtauK * dl;
alpha_scatti = alpha_absi = 0.;
bias = 0.;
bi = 0.;
} else {
alpha_scattf =
alpha_inv_scatt(nu, Thetae, Ne);
dtau_scatt =
0.5 * (alpha_scatti +
alpha_scattf) * dtauK * dl;
alpha_scatti = alpha_scattf;
/* absorption optical depth along step */
alpha_absf =
alpha_inv_abs(nu, Thetae, Ne, B,
theta);
dtau_abs =
0.5 * (alpha_absi +
alpha_absf) * dtauK * dl;
alpha_absi = alpha_absf;
bf = bias_func(Thetae, ph->w);
bias = 0.5 * (bi + bf);
bi = bf;
}
x1 = -log(monty_rand());
php.w = ph->w / bias;
if (bias * dtau_scatt > x1 && php.w > WEIGHT_MIN) {
if (isnan(php.w) || isinf(php.w)) {
fprintf(stderr,
"w isnan in track_super_photon: Ne, bias, ph->w, php.w %g, %g, %g, %g\n",
Ne, bias, ph->w, php.w);
}
frac = x1 / (bias * dtau_scatt);
/* Apply absorption until scattering event */
dtau_abs *= frac;
if (dtau_abs > 100)
return; /* This photon has been absorbed before scattering */
dtau_scatt *= frac;
dtau = dtau_abs + dtau_scatt;
if (dtau_abs < 1.e-3)
ph->w *=
(1. -
dtau / 24. * (24. -
dtau * (12. -
dtau *
(4. -
dtau))));
else
ph->w *= exp(-dtau);
/* Interpolate position and wave vector to scattering event */
push_photon(Xi, Ki, dKi, dl * frac, &E0,
0);
ph->X[0] = Xi[0];
ph->X[1] = Xi[1];
ph->X[2] = Xi[2];
ph->X[3] = Xi[3];
ph->K[0] = Ki[0];
ph->K[1] = Ki[1];
ph->K[2] = Ki[2];
ph->K[3] = Ki[3];
ph->dKdlam[0] = dKi[0];
ph->dKdlam[1] = dKi[1];
ph->dKdlam[2] = dKi[2];
ph->dKdlam[3] = dKi[3];
ph->E0s = E0;
/* Get plasma parameters at new position */
gcov_func(ph->X, Gcov);
get_fluid_params(ph->X, Gcov, &Ne, &Thetae,
&B, Ucon, Ucov, Bcon,
Bcov);
if (Ne > 0.) {
scatter_super_photon(ph, &php, Ne,
Thetae, B,
Ucon, Bcon,
Gcov);
if (ph->w < 1.e-100) { /* must have been a problem popping k back onto light cone */
return;
}
track_super_photon(&php);
}
theta =
get_bk_angle(ph->X, ph->K, Ucov, Bcov,
B);
nu = get_fluid_nu(ph->X, ph->K, Ucov);
if (nu < 0.) {
alpha_scatti = alpha_absi = 0.;
} else {
alpha_scatti =
alpha_inv_scatt(nu, Thetae,
Ne);
alpha_absi =
alpha_inv_abs(nu, Thetae, Ne,
B, theta);
}
bi = bias_func(Thetae, ph->w);
ph->tau_abs += dtau_abs;
ph->tau_scatt += dtau_scatt;
} else {
if (dtau_abs > 100)
return; /* This photon has been absorbed */
ph->tau_abs += dtau_abs;
ph->tau_scatt += dtau_scatt;
dtau = dtau_abs + dtau_scatt;
if (dtau < 1.e-3)
ph->w *=
(1. -
dtau / 24. * (24. -
dtau * (12. -
dtau *
(4. -
dtau))));
else
ph->w *= exp(-dtau);
}
}
nstep++;
/* signs that something's wrong w/ the integration */
if (nstep > MAXNSTEP) {
fprintf(stderr,
"X1,X2,K1,K2,bias: %g %g %g %g %g\n",
ph->X[1], ph->X[2], ph->K[1], ph->K[2],
bias);
break;
}
}
/* accumulate result in spectrum on escape */
if (record_criterion(ph) && nstep < MAXNSTEP)
record_super_photon(ph);
/* done! */
return;
}