-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
302 lines (243 loc) · 7.99 KB
/
main.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
/*
kmonty, based on grmonty
Using monte carlo method, estimate spectrum of an appropriately
scaled axisymmetric GRMHD simulation as a function of
latitudinal viewing angle.
Input simulation data is assumed to be in dump format provided by
HARM code. Location of input file is, at present, hard coded
(see init_sim_data.c).
Nph super-photons are generated in total and then allowed
to propagate. They are weighted according to the emissivity.
The photons are pushed by the geodesic equation.
Their weight decays according to the local absorption coefficient.
The photons also scatter with probability related to the local
scattering opacity.
The electrons are assumed to have a thermal distribution
function, and to be at the same temperature as the protons.
CFG 8-17-06
Implemented synchrotron sampling, 22 Jan 07
fixed bugs in tetrad/compton scattering routines, 31 Jan 07
Implemented normalization for output, 6 Feb 07
Separated out different synchrotron sampling routines
into separate files, 8 Mar 07
fixed bug in energy recording; bug used Kcon[0] rather than
Kcov[0] as energy, 18 Mar 07
major reorganization to encapsulate problem-dependent parts 5-6 Nov 07
added accelerated paticles J. Davelaar 2017-2018
*/
#include "decs.h"
/* defining declarations for global variables */
struct of_geom **geom;
struct of_spectrum ***shared_spect;
double ***shared_Xi_spec;
double ***shared_ispec;
double F_th[N_ESAMP + 1], F_nth[N_ESAMP + 1], wgt[N_ESAMP + 1];
long int Ns, N_scatt, N_superph_recorded, N_superph_recorded_total;
/* some coordinate parameters */
double a;
double R0, Rin, Rh, Rout, Rms;
double hslope;
double startx[NDIM], stopx[NDIM], dx[NDIM];
double dlE, lE0;
double gam;
double dMsim;
double M_unit, L_unit, T_unit;
double RHO_unit, U_unit, B_unit, Ne_unit, Thetae_unit;
double max_tau_scatt, Ladv, dMact, bias_norm;
double stopx[4], startx[4], dx[4];
double a;
double th_len;
double *neqpar;
double xprobmin[3], xprobmax[3];
int ng[3], nxlone[3], nleafs;
double *eqpar;
int count_leaf, count_node;
int *forest; //= NULL;
struct block *block_info;
int block_size;
int forest_size;
int n1, n2, n3;
int index;
double Ladv, dMact;
double **Xcoord;
double ***Xgrid;
double ***Xbar;
gsl_rng *r;
gsl_integration_workspace *w;
#include <time.h>
#pragma omp threadprivate(r)
int main(int argc, char *argv[]) {
double Ntot;
long int N_superph_made_local, N_superph_made;
int quit_flag, myid;
struct of_photon ph;
time_t currtime, starttime;
if (argc < 4) {
fprintf(stderr, "usage: grmonty Ns infilename M_unit\n");
exit(0);
}
sscanf(argv[1], "%lf", &Ntot);
Ns = (long int)Ntot;
/* initialize random number generator */
sscanf(argv[4], "%d", &index);
#if MPI
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Ns /= world_size;
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
if (world_rank == 0) {
fprintf(stderr, "\nBOOTING UP kmonty\n\n");
}
MPI_Barrier(MPI_COMM_WORLD);
// fprintf(stderr, "Initialized processor %s, rank %d out of %d processors\n",
// processor_name, world_rank, world_size);
fflush(stderr);
MPI_Barrier(MPI_COMM_WORLD);
if (world_rank == 0) {
fprintf(stderr, "\n kmonty running\n\n");
}
myid = world_rank;
init_monty_rand(139 * myid +
time(NULL) * index); /* Arbitrarily picked initial seed */
#endif
#if OPENMP
#pragma omp parallel private(myid)
{
myid = omp_get_thread_num();
init_monty_rand(
139 * myid +
time(NULL) *
(1337 + index + myid)); /* Arbitrarily picked initial seed */
fprintf(stderr, "id is %d\n", myid);
}
#endif
long int Ns_total = Ns;
// Ns /=(world_size);
fflush(stderr);
/* spectral bin parameters */
dlE = 0.25; /* bin width */
lE0 = log(1.e-12); /* location of first bin, in electron rest-mass units */
/* initialize model data, auxiliary variables */
init_model(argv);
#if MPI
MPI_Barrier(MPI_COMM_WORLD);
#endif
/** main loop **/
N_superph_made_local = 0;
N_superph_recorded = 0;
N_scatt = 0;
starttime = time(NULL);
quit_flag = 0;
#if MPI
if (world_rank == 0) {
#endif
fprintf(stderr, "\nMAIN LOOP\n");
fflush(stderr);
#if MPI
}
#endif
#if OPENMP
#pragma omp parallel private(ph)
{
#endif
#if MPI
{
#endif
while (1) {
/* get pseudo-quanta */
#if OPENMP
#pragma omp critical(MAKE_SPHOT)
{
#endif
#if MPI
{
#endif
if (!quit_flag)
make_super_photon(&ph, &quit_flag);
}
if (quit_flag)
break;
/* push them around */
int igrid = -1;
track_super_photon(&ph, &N_superph_recorded, igrid);
/* step */
#if OPENMP
#pragma omp atomic
#endif
N_superph_made_local += 1;
/* give interim reports on rates */
#if MPI
if (((int)(N_superph_made_local)) % 10000 == 0 &&
N_superph_recorded * world_size > 0 &&
world_rank == 0) {
currtime = time(NULL);
fprintf(stderr, "time %g, rate %g ph/s\n",
(double)(currtime - starttime),
(double)N_superph_made_local * world_size /
(currtime - starttime));
fflush(stderr);
}
#endif
/* give interim reports on rates */
#if OPENMP
if (((int)(N_superph_made_local)) % 10000 == 0 &&
N_superph_made_local > 0) {
currtime = time(NULL);
fprintf(stderr, "time %g, rate %g ph/s\n",
(double)(currtime - starttime),
(double)N_superph_made_local / (currtime - starttime));
}
#endif
}
}
currtime = time(NULL);
#if MPI
MPI_Barrier(MPI_COMM_WORLD);
if (world_rank == 0) {
fprintf(stderr, "reducing spetra\n");
fflush(stderr);
}
#endif
// #ifdef _OPENMP
// #pragma omp parallel
#if MPI
{ mpi_reduce_spect(); }
if (world_rank == 0) {
fprintf(stderr, "done\n");
fflush(stderr);
}
MPI_Allreduce(&N_superph_made_local, &N_superph_made, 1, MPI_LONG,
MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&N_superph_recorded, &N_superph_recorded_total, 1,
MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
if (world_rank == 0) {
fprintf(stderr, "Final time %g, record rate %g ph/s generated rate %g ph/s\n",
(double)(currtime - starttime),
(double)N_superph_recorded_total /
(currtime - starttime), (double)N_superph_made /
(currtime - starttime));
fprintf(stderr, "Made %d Recorded %d\n", N_superph_made,
N_superph_recorded_total);
report_spectrum((int)N_superph_made);
}
// Finalize the MPI environment.
MPI_Finalize();
#endif
#if OPENMP
#pragma omp barrier
#pragma omp parallel
{ omp_reduce_spect(); }
report_spectrum((int)N_superph_made_local);
#endif
/* done! */
return (0);
}