forked from pseudotensor/grmonty
-
Notifications
You must be signed in to change notification settings - Fork 6
/
geodesics.c
291 lines (237 loc) · 7.46 KB
/
geodesics.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
/***********************************************************************************
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
***********************************************************************************/
#include "decs.h"
/*
this is the main photon orbit integrator
*/
#define FAST_CPY(in,out) {out[0] = in[0]; out[1] = in[1]; out[2] = in[2]; out[3] = in[3];}
#define ETOL 1.e-3
#define MAX_ITER 2
void push_photon(double X[NDIM], double Kcon[NDIM], double dKcon[NDIM],
double dl, double *E0, int n)
{
double lconn[NDIM][NDIM][NDIM];
double Kcont[NDIM], K[NDIM], dK;
double Xcpy[NDIM], Kcpy[NDIM], dKcpy[NDIM];
double Gcov[NDIM][NDIM], E1;
double dl_2, err, errE;
int i, k, iter;
if (X[1] < startx[1])
return;
FAST_CPY(X, Xcpy);
FAST_CPY(Kcon, Kcpy);
FAST_CPY(dKcon, dKcpy);
dl_2 = 0.5 * dl;
/* Step the position and estimate new wave vector */
for (i = 0; i < NDIM; i++) {
dK = dKcon[i] * dl_2;
Kcon[i] += dK;
K[i] = Kcon[i] + dK;
X[i] += Kcon[i] * dl;
}
get_connection(X, lconn);
/* We're in a coordinate basis so take advantage of symmetry in the connection */
iter = 0;
do {
iter++;
FAST_CPY(K, Kcont);
err = 0.;
for (k = 0; k < 4; k++) {
dKcon[k] =
-2. * (Kcont[0] *
(lconn[k][0][1] * Kcont[1] +
lconn[k][0][2] * Kcont[2] +
lconn[k][0][3] * Kcont[3])
+
Kcont[1] * (lconn[k][1][2] * Kcont[2] +
lconn[k][1][3] * Kcont[3])
+ lconn[k][2][3] * Kcont[2] * Kcont[3]
);
dKcon[k] -=
(lconn[k][0][0] * Kcont[0] * Kcont[0] +
lconn[k][1][1] * Kcont[1] * Kcont[1] +
lconn[k][2][2] * Kcont[2] * Kcont[2] +
lconn[k][3][3] * Kcont[3] * Kcont[3]
);
K[k] = Kcon[k] + dl_2 * dKcon[k];
err += fabs((Kcont[k] - K[k]) / (K[k] + SMALL));
}
} while (err > ETOL && iter < MAX_ITER);
FAST_CPY(K, Kcon);
gcov_func(X, Gcov);
E1 = -(Kcon[0] * Gcov[0][0] + Kcon[1] * Gcov[0][1] +
Kcon[2] * Gcov[0][2] + Kcon[3] * Gcov[0][3]);
errE = fabs((E1 - (*E0)) / (*E0));
if (n < 7
&& (errE > 1.e-4 || err > ETOL || isnan(err) || isinf(err))) {
FAST_CPY(Xcpy, X);
FAST_CPY(Kcpy, Kcon);
FAST_CPY(dKcpy, dKcon);
push_photon(X, Kcon, dKcon, 0.5 * dl, E0, n + 1);
push_photon(X, Kcon, dKcon, 0.5 * dl, E0, n + 1);
E1 = *E0;
}
*E0 = E1;
/* done! */
}
/* spare photon integrator: 4th order Runge-Kutta */
void push_photon4(double X[], double K[], double dK[], double dl)
{
int k;
double lconn[NDIM][NDIM][NDIM];
double Kt[NDIM], Xt[NDIM];
double f1x[NDIM], f2x[NDIM], f3x[NDIM], f4x[NDIM];
double f1k[NDIM], f2k[NDIM], f3k[NDIM], f4k[NDIM];
double dl_2 = 0.5 * dl;
for (k = 0; k < NDIM; k++)
f1x[k] = K[k];
get_connection(X, lconn);
for (k = 0; k < NDIM; k++) {
f1k[k] =
-2. * (K[0] *
(lconn[k][0][1] * K[1] + lconn[k][0][2] * K[2] +
lconn[k][0][3] * K[3]) +
K[1] * (lconn[k][1][2] * K[2] +
lconn[k][1][3] * K[3]) +
lconn[k][2][3] * K[2] * K[3]
);
f1k[k] -=
(lconn[k][0][0] * K[0] * K[0] +
lconn[k][1][1] * K[1] * K[1] +
lconn[k][2][2] * K[2] * K[2] +
lconn[k][3][3] * K[3] * K[3]
);
}
for (k = 0; k < NDIM; k++) {
Kt[k] = K[k] + dl_2 * f1k[k];
f2x[k] = Kt[k];
Xt[k] = X[k] + dl_2 * f1x[k];
}
get_connection(Xt, lconn);
for (k = 0; k < NDIM; k++) {
f2k[k] =
-2. * (Kt[0] *
(lconn[k][0][1] * Kt[1] +
lconn[k][0][2] * Kt[2] +
lconn[k][0][3] * Kt[3]) +
Kt[1] * (lconn[k][1][2] * Kt[2] +
lconn[k][1][3] * Kt[3]) +
lconn[k][2][3] * Kt[2] * Kt[3]
);
f2k[k] -=
(lconn[k][0][0] * Kt[0] * Kt[0] +
lconn[k][1][1] * Kt[1] * Kt[1] +
lconn[k][2][2] * Kt[2] * Kt[2] +
lconn[k][3][3] * Kt[3] * Kt[3]
);
}
for (k = 0; k < NDIM; k++) {
Kt[k] = K[k] + dl_2 * f2k[k];
f3x[k] = Kt[k];
Xt[k] = X[k] + dl_2 * f2x[k];
}
get_connection(Xt, lconn);
for (k = 0; k < NDIM; k++) {
f3k[k] =
-2. * (Kt[0] *
(lconn[k][0][1] * Kt[1] +
lconn[k][0][2] * Kt[2] +
lconn[k][0][3] * Kt[3]) +
Kt[1] * (lconn[k][1][2] * Kt[2] +
lconn[k][1][3] * Kt[3]) +
lconn[k][2][3] * Kt[2] * Kt[3]
);
f3k[k] -=
(lconn[k][0][0] * Kt[0] * Kt[0] +
lconn[k][1][1] * Kt[1] * Kt[1] +
lconn[k][2][2] * Kt[2] * Kt[2] +
lconn[k][3][3] * Kt[3] * Kt[3]
);
}
for (k = 0; k < NDIM; k++) {
Kt[k] = K[k] + dl * f3k[k];
f4x[k] = Kt[k];
Xt[k] = X[k] + dl * f3x[k];
}
get_connection(Xt, lconn);
for (k = 0; k < NDIM; k++) {
f4k[k] =
-2. * (Kt[0] *
(lconn[k][0][1] * Kt[1] +
lconn[k][0][2] * Kt[2] +
lconn[k][0][3] * Kt[3]) +
Kt[1] * (lconn[k][1][2] * Kt[2] +
lconn[k][1][3] * Kt[3]) +
lconn[k][2][3] * Kt[2] * Kt[3]
);
f4k[k] -=
(lconn[k][0][0] * Kt[0] * Kt[0] +
lconn[k][1][1] * Kt[1] * Kt[1] +
lconn[k][2][2] * Kt[2] * Kt[2] +
lconn[k][3][3] * Kt[3] * Kt[3]
);
}
for (k = 0; k < NDIM; k++) {
X[k] +=
0.166666666666667 * dl * (f1x[k] +
2. * (f2x[k] + f3x[k]) +
f4x[k]);
K[k] +=
0.166666666666667 * dl * (f1k[k] +
2. * (f2k[k] + f3k[k]) +
f4k[k]);
}
init_dKdlam(X, K, dK);
/* done */
}
void init_dKdlam(double X[], double Kcon[], double dK[])
{
int k;
double lconn[NDIM][NDIM][NDIM];
get_connection(X, lconn);
for (k = 0; k < 4; k++) {
dK[k] =
-2. * (Kcon[0] *
(lconn[k][0][1] * Kcon[1] +
lconn[k][0][2] * Kcon[2] +
lconn[k][0][3] * Kcon[3])
+ Kcon[1] * (lconn[k][1][2] * Kcon[2] +
lconn[k][1][3] * Kcon[3])
+ lconn[k][2][3] * Kcon[2] * Kcon[3]
);
dK[k] -=
(lconn[k][0][0] * Kcon[0] * Kcon[0] +
lconn[k][1][1] * Kcon[1] * Kcon[1] +
lconn[k][2][2] * Kcon[2] * Kcon[2] +
lconn[k][3][3] * Kcon[3] * Kcon[3]
);
}
return;
}