forked from kcroker/Gadget-2.0.7-ngravs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
potential.c
354 lines (286 loc) · 8.35 KB
/
potential.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <mpi.h>
#include "allvars.h"
#include "proto.h"
/*! \file potential.c
* \brief Computation of the gravitational potential of particles
*/
/*! This function computes the gravitational potential for ALL the particles.
* First, the (short-range) tree potential is computed, and then, if needed,
* the long range PM potential is added.
*/
void compute_potential(void)
{
int i;
#ifndef NOGRAVITY
long long ntot, ntotleft;
int j, k, level, sendTask, recvTask;
int ndone;
int maxfill, ngrp, place, nexport;
int *nsend, *noffset, *nsend_local, *nbuffer, *ndonelist, *numlist;
double fac;
double t0, t1, tstart, tend;
MPI_Status status;
double r2;
t0 = second();
if(All.ComovingIntegrationOn)
set_softenings();
if(ThisTask == 0)
{
printf("Start computation of potential for all particles...\n");
fflush(stdout);
}
tstart = second();
if(TreeReconstructFlag)
{
if(ThisTask == 0)
printf("Tree construction.\n");
force_treebuild(NumPart);
TreeReconstructFlag = 0;
if(ThisTask == 0)
printf("Tree construction done.\n");
}
tend = second();
All.CPU_TreeConstruction += timediff(tstart, tend);
numlist = malloc(NTask * sizeof(int) * NTask);
MPI_Allgather(&NumPart, 1, MPI_INT, numlist, 1, MPI_INT, MPI_COMM_WORLD);
for(i = 0, ntot = 0; i < NTask; i++)
ntot += numlist[i];
free(numlist);
noffset = malloc(sizeof(int) * NTask); /* offsets of bunches in common list */
nbuffer = malloc(sizeof(int) * NTask);
nsend_local = malloc(sizeof(int) * NTask);
nsend = malloc(sizeof(int) * NTask * NTask);
ndonelist = malloc(sizeof(int) * NTask);
i = 0; /* beginn with this index */
ntotleft = ntot; /* particles left for all tasks together */
while(ntotleft > 0)
{
for(j = 0; j < NTask; j++)
nsend_local[j] = 0;
/* do local particles and prepare export list */
for(nexport = 0, ndone = 0; i < NumPart && nexport < All.BunchSizeForce - NTask; i++)
{
ndone++;
for(j = 0; j < NTask; j++)
Exportflag[j] = 0;
#ifndef PMGRID
force_treeevaluate_potential(i, 0);
#else
force_treeevaluate_potential_shortrange(i, 0);
#endif
for(j = 0; j < NTask; j++)
{
if(Exportflag[j])
{
for(k = 0; k < 3; k++)
GravDataGet[nexport].u.Pos[k] = P[i].Pos[k];
#ifdef UNEQUALSOFTENINGS
GravDataGet[nexport].Type = P[i].Type;
#ifdef ADAPTIVE_GRAVSOFT_FORGAS
if(P[i].Type == 0)
GravDataGet[nexport].Soft = SphP[i].Hsml;
#endif
#endif
GravDataGet[nexport].w.OldAcc = P[i].OldAcc;
GravDataIndexTable[nexport].Task = j;
GravDataIndexTable[nexport].Index = i;
GravDataIndexTable[nexport].SortIndex = nexport;
nexport++;
nsend_local[j]++;
}
}
}
qsort(GravDataIndexTable, nexport, sizeof(struct gravdata_index), grav_tree_compare_key);
for(j = 0; j < nexport; j++)
GravDataIn[j] = GravDataGet[GravDataIndexTable[j].SortIndex];
for(j = 1, noffset[0] = 0; j < NTask; j++)
noffset[j] = noffset[j - 1] + nsend_local[j - 1];
MPI_Allgather(nsend_local, NTask, MPI_INT, nsend, NTask, MPI_INT, MPI_COMM_WORLD);
/* now do the particles that need to be exported */
for(level = 1; level < (1 << PTask); level++)
{
for(j = 0; j < NTask; j++)
nbuffer[j] = 0;
for(ngrp = level; ngrp < (1 << PTask); ngrp++)
{
maxfill = 0;
for(j = 0; j < NTask; j++)
{
if((j ^ ngrp) < NTask)
if(maxfill < nbuffer[j] + nsend[(j ^ ngrp) * NTask + j])
maxfill = nbuffer[j] + nsend[(j ^ ngrp) * NTask + j];
}
if(maxfill >= All.BunchSizeForce)
break;
sendTask = ThisTask;
recvTask = ThisTask ^ ngrp;
if(recvTask < NTask)
{
if(nsend[ThisTask * NTask + recvTask] > 0 || nsend[recvTask * NTask + ThisTask] > 0)
{
/* get the particles */
MPI_Sendrecv(&GravDataIn[noffset[recvTask]],
nsend_local[recvTask] * sizeof(struct gravdata_in), MPI_BYTE,
recvTask, TAG_POTENTIAL_A,
&GravDataGet[nbuffer[ThisTask]],
nsend[recvTask * NTask + ThisTask] * sizeof(struct gravdata_in), MPI_BYTE,
recvTask, TAG_POTENTIAL_A, MPI_COMM_WORLD, &status);
}
}
for(j = 0; j < NTask; j++)
if((j ^ ngrp) < NTask)
nbuffer[j] += nsend[(j ^ ngrp) * NTask + j];
}
for(j = 0; j < nbuffer[ThisTask]; j++)
{
#ifndef PMGRID
force_treeevaluate_potential(j, 1);
#else
force_treeevaluate_potential_shortrange(j, 1);
#endif
}
/* get the result */
for(j = 0; j < NTask; j++)
nbuffer[j] = 0;
for(ngrp = level; ngrp < (1 << PTask); ngrp++)
{
maxfill = 0;
for(j = 0; j < NTask; j++)
{
if((j ^ ngrp) < NTask)
if(maxfill < nbuffer[j] + nsend[(j ^ ngrp) * NTask + j])
maxfill = nbuffer[j] + nsend[(j ^ ngrp) * NTask + j];
}
if(maxfill >= All.BunchSizeForce)
break;
sendTask = ThisTask;
recvTask = ThisTask ^ ngrp;
if(recvTask < NTask)
{
if(nsend[ThisTask * NTask + recvTask] > 0 || nsend[recvTask * NTask + ThisTask] > 0)
{
/* send the results */
MPI_Sendrecv(&GravDataResult[nbuffer[ThisTask]],
nsend[recvTask * NTask + ThisTask] * sizeof(struct gravdata_in),
MPI_BYTE, recvTask, TAG_POTENTIAL_B,
&GravDataOut[noffset[recvTask]],
nsend_local[recvTask] * sizeof(struct gravdata_in),
MPI_BYTE, recvTask, TAG_POTENTIAL_B, MPI_COMM_WORLD, &status);
/* add the result to the particles */
for(j = 0; j < nsend_local[recvTask]; j++)
{
place = GravDataIndexTable[noffset[recvTask] + j].Index;
P[place].Potential += GravDataOut[j + noffset[recvTask]].u.Potential;
}
}
}
for(j = 0; j < NTask; j++)
if((j ^ ngrp) < NTask)
nbuffer[j] += nsend[(j ^ ngrp) * NTask + j];
}
level = ngrp - 1;
}
MPI_Allgather(&ndone, 1, MPI_INT, ndonelist, 1, MPI_INT, MPI_COMM_WORLD);
for(j = 0; j < NTask; j++)
ntotleft -= ndonelist[j];
}
free(ndonelist);
free(nsend);
free(nsend_local);
free(nbuffer);
free(noffset);
/* add correction to exclude self-potential */
for(i = 0; i < NumPart; i++)
{
/* remove self-potential */
P[i].Potential += P[i].Mass / All.SofteningTable[P[i].Type];
// KC 1/27/15
// Again, this is the LatticeZero
if(All.ComovingIntegrationOn)
if(All.PeriodicBoundariesOn) {
j = TypeToGrav[P[i].Type];
P[i].Potential -= /* 2.8372975 */ LatticeZero[j][j] * pow(P[i].Mass, 2.0 / 3) *
pow(All.Omega0 * 3 * All.Hubble * All.Hubble / (8 * M_PI * All.G), 1.0 / 3);
}
}
/* multiply with the gravitational constant */
for(i = 0; i < NumPart; i++)
P[i].Potential *= All.G;
#ifdef PMGRID
#ifdef PERIODIC
pmpotential_periodic();
#ifdef PLACEHIGHRESREGION
i = pmpotential_nonperiodic(1);
if(i == 1) /* this is returned if a particle lied outside allowed range */
{
pm_init_regionsize();
pm_setup_nonperiodic_kernel();
i = pmpotential_nonperiodic(1); /* try again */
}
if(i == 1)
endrun(88686);
#endif
#else
i = pmpotential_nonperiodic(0);
if(i == 1) /* this is returned if a particle lied outside allowed range */
{
pm_init_regionsize();
pm_setup_nonperiodic_kernel();
i = pmpotential_nonperiodic(0); /* try again */
}
if(i == 1)
endrun(88687);
#ifdef PLACEHIGHRESREGION
i = pmpotential_nonperiodic(1);
if(i == 1) /* this is returned if a particle lied outside allowed range */
{
pm_init_regionsize();
i = pmpotential_nonperiodic(1);
}
if(i != 0)
endrun(88688);
#endif
#endif
#endif
if(All.ComovingIntegrationOn)
{
#ifndef PERIODIC
fac = -0.5 * All.Omega0 * All.Hubble * All.Hubble;
for(i = 0; i < NumPart; i++)
{
for(k = 0, r2 = 0; k < 3; k++)
r2 += P[i].Pos[k] * P[i].Pos[k];
P[i].Potential += fac * r2;
}
#endif
}
else
{
fac = -0.5 * All.OmegaLambda * All.Hubble * All.Hubble;
if(fac != 0)
{
for(i = 0; i < NumPart; i++)
{
for(k = 0, r2 = 0; k < 3; k++)
r2 += P[i].Pos[k] * P[i].Pos[k];
P[i].Potential += fac * r2;
}
}
}
if(ThisTask == 0)
{
printf("potential done.\n");
fflush(stdout);
}
t1 = second();
All.CPU_Potential += timediff(t0, t1);
#else
for(i = 0; i < NumPart; i++)
P[i].Potential = 0;
#endif
}