This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_glass.c
417 lines (338 loc) · 10.3 KB
/
read_glass.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
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include <inttypes.h>
#include <limits.h>
#include "allvars.h"
#include "proto.h"
#include "save.h"
#include "read_glass.h"
int find_files(char *fname);
void read_glass(char *fname)
{
int64_t i, j, k, n;
int slab, count, type;
uint32_t dummy, dummy2, m;
float *pos = 0;
float x, y, z;
FILE *fd = 0;
size_t bytes;
int *npart_Task;
int num, numfiles, skip, nlocal;
char buf[500];
#define SKIP {my_fread(&dummy, sizeof(int), 1, fd);}
#define SKIP2 {my_fread(&dummy2, sizeof(int), 1, fd);}
if(ThisTask == 0)
{
printf("\nreading Lagrangian glass file...\n");
fflush(stdout);
numfiles = find_files(fname);
for(num = 0, skip = 0; num < numfiles; num++)
{
if(numfiles > 1)
sprintf(buf, "%s.%d", fname, num);
else
sprintf(buf, "%s", fname);
fd = fopen(buf, "r");
if(fd == NULL)
{
printf("can't open file `%s' for reading glass file.\n", buf);
FatalError(1);
}
SKIP;
my_fread(&header1, sizeof(header1), 1, fd);
SKIP2;
if(dummy != sizeof(header1) || dummy2 != sizeof(header1))
{
printf("incorrect header size!\n");
FatalError(2);
}
nlocal = 0;
for(k = 0; k < 6; k++)
nlocal += header1.npart[k];
printf("reading '%s' with %d particles\n", fname, nlocal);
if(num == 0)
{
Nglass = 0;
for(k = 0; k < 6; k++)
Nglass += header1.npartTotal[k];
printf("\nNglass= %d\n\n", Nglass);
pos = malloc(sizeof(*pos) * Nglass * 3);
if(pos == NULL)
{
printf("failed to allocate %g Mbyte on Task %d for glass file\n",
sizeof(float) * Nglass * 3.0 / (1024.0 * 1024.0), ThisTask);
FatalError(112);
}
}
SKIP;
my_fread(&pos[3 * skip], sizeof(float), 3 * nlocal, fd);
SKIP2;
if(dummy != sizeof(float) * 3 * nlocal || dummy2 != sizeof(float) * 3 * nlocal)
{
printf("incorrect block structure in positions block!\n");
FatalError(3);
}
skip += nlocal;
fclose(fd);
}
}
MPI_Bcast(&Nglass, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&header1, sizeof(header1), MPI_BYTE, 0, MPI_COMM_WORLD);
if(ThisTask != 0)
{
pos = malloc(sizeof(*pos) * Nglass * 3);
if(pos == NULL)
{
printf("failed to allocate %g Mbyte on Task %d for glass file\n",
sizeof(float) * Nglass * 3.0 / (1024.0 * 1024.0), ThisTask);
FatalError(112);
}
}
MPI_Bcast(&pos[0], sizeof(float) * Nglass * 3, MPI_BYTE, 0, MPI_COMM_WORLD);
bytes = sizeof(int) * NTask;
npart_Task = malloc(bytes);
ASSERT_ALLOC(npart_Task);
for(i = 0; i < NTask; i++)
npart_Task[i] = 0;
#if defined(MULTICOMPONENTGLASSFILE) && defined(DIFFERENT_TRANSFER_FUNC)
MinType = 7;
MaxType = -2;
for(type = 0; type < 6; type++)
if(header1.npartTotal[type])
{
if(MinType > type - 1)
MinType = type - 1;
if(MaxType < type - 1)
MaxType = type - 1;
}
#endif
#if defined(PRODUCE_CONSISTENT_IDS)
/* Code from Greg Poole to generate same IDs across different simulations.
Makes matching halos trivial afterwards. */
for(i = 0; i < GlassTileFac; i++){
if(i % GlassTileFacSampleDenom < GlassTileFacSampleNumerator) {
for(type = 0, n = 0; type < 6; type++)
{
for(m = 0; m < header1.npartTotal[type]; m++, n++)
{
x = pos[3 * n] / header1.BoxSize * (Box / GlassTileFac) + i * (Box / GlassTileFac);
slab = x / Box * Nmesh;
if(slab >= Nmesh)
slab = Nmesh - 1;
npart_Task[Slab_to_task[slab]] += 1;
}
}
}
}
const int64_t subsample_oneaxis = (GlassTileFac * GlassTileFacSampleNumerator)/GlassTileFacSampleDenom;
const int64_t sqr_GlassTileFac = subsample_oneaxis * subsample_oneaxis;
for(i=0;i<NTask;i++) {
const int64_t this_npart = npart_Task[i];
if(this_npart * sqr_GlassTileFac > INT_MAX) {
printf("On Task %d number of particles = %"PRId64" exceeds INT_MAX. Please increase the number of cpus (currently running on %d cpus) and rerun..aborting\n",
ThisTask, this_npart * sqr_GlassTileFac, NTask);
FatalError(3142);
}
npart_Task[i] = this_npart * sqr_GlassTileFac;
}
#else
/* Old code from standard 2LPTic */
for(i = 0; i < GlassTileFac; i++)
{
for(type = 0, n = 0; type < 6; type++)
{
for(m = 0; m < header1.npartTotal[type]; m++, n++)
{
x = pos[3 * n] / header1.BoxSize * (Box / GlassTileFac) + i * (Box / GlassTileFac);
slab = x / Box * Nmesh;
if(slab >= Nmesh)
slab = Nmesh - 1;
npart_Task[Slab_to_task[slab]] += 1;
}
}
}
const int64_t sqr_GlassTileFac = GlassTileFac * GlassTileFac;
for(i=0;i<NTask;i++) {
const int64_t this_npart = npart_Task[i];
if(this_npart * sqr_GlassTileFac > INT_MAX) {
printf("On Task %d number of particles = %"PRId64" exceeds INT_MAX. Please increase the number of cpus and rerun..aborting\n",
ThisTask, this_npart * sqr_GlassTileFac);
FatalError(3143);
}
npart_Task[i] = this_npart * sqr_GlassTileFac;
}
#endif
TotNumPart = 0; /* note: This is a 64 bit integer */
NTaskWithN = 0;
NumPart = npart_Task[ThisTask];
#if defined(PRODUCE_CONSISTENT_IDS)
/* Code from Greg Poole to generate consistent IDs across
simulations of different resolutions */
ThisTaskFileNumber=0;
for(i = 0; i < NTask; i++)
{
TotNumPart += npart_Task[i];
if(npart_Task[i] > 0){
NTaskWithN++;
if(i<ThisTask && npart_Task[i]>0)
ThisTaskFileNumber++;
}
}
if(npart_Task[ThisTask]<=0)
ThisTaskFileNumber=-1;
#else
/* alternate code from old 2LPTic - does not preserve IDs */
for(i = 0; i < NTask; i++)
{
TotNumPart += npart_Task[i];
if(npart_Task[i] > 0)
NTaskWithN++;
}
#endif
if(ThisTask == 0)
{
for(i = 0; i < NTask; i++)
printf("%d particles on task=%"PRId64" (slabs=%d)\n", npart_Task[i], i, Local_nx_table[i]);
printf("\nTotal number of particles = %d%09d\n\n",
(int) (TotNumPart / 1000000000), (int) (TotNumPart % 1000000000));
fflush(stdout);
}
free(npart_Task);
if(NumPart)
{
bytes = sizeof(struct part_data) * NumPart;
P = (struct part_data *) malloc(bytes);
if(P == NULL)
{
printf("failed to allocate %g Mbyte (%d particles) on Task %d\n", bytes / (1024.0 * 1024.0),
NumPart, ThisTask);
FatalError(9891);
}
}
count = 0;
#if defined(PRODUCE_CONSISTENT_IDS)
/* Code from Greg Poole to generate consistent particle IDs
across simulations of different resolutions */
long long GTF1=(long long)GlassTileFac;
long long GTF2=(long long)GlassTileFac*(long long)GlassTileFac;
for(i = 0; i < GlassTileFac; i++){
if(i % GlassTileFacSampleDenom < GlassTileFacSampleNumerator){
for(j = 0; j < GlassTileFac; j++){
if(j % GlassTileFacSampleDenom < GlassTileFacSampleNumerator){
for(k = 0; k < GlassTileFac; k++){
if(k % GlassTileFacSampleDenom < GlassTileFacSampleNumerator) {
for(type = 0, n = 0; type < 6; type++)
{
for(m = 0; m < header1.npartTotal[type]; m++, n++)
{
x = pos[3 * n] / header1.BoxSize * (Box / GlassTileFac) + i * (Box / GlassTileFac);
slab = x / Box * Nmesh;
if(slab >= Nmesh)
slab = Nmesh - 1;
if(Slab_to_task[slab] == ThisTask)
{
y = pos[3 * n + 1] / header1.BoxSize * (Box / GlassTileFac) + j * (Box / GlassTileFac);
z = pos[3 * n + 2] / header1.BoxSize * (Box / GlassTileFac) + k * (Box / GlassTileFac);
P[count].Pos[0] = x;
P[count].Pos[1] = y;
P[count].Pos[2] = z;
#ifdef MULTICOMPONENTGLASSFILE
P[count].Type = type - 1;
#endif
P[count].ID = 1+(((long long)i)*GTF2+((long long)j)*GTF1)+((long long)k);
#ifndef USE_64BITID
/*If 32bit IDs are requested, then check that these generated IDs will fit.
Simulations are wasted no more. */
if(P[count].ID > INT_MAX) {
printf("On task %d: Can not represent particles ID = %lld with 32 bit integers. Enabled the compile time options USE_64BITID\n",
ThisTask, P[count].ID);
FatalError(10000);
}
#endif
count++;
}
}
}
}
}
}
}
}
}
#else //else for PRODUCE_CONSISTENT_IDS
/* Alternate code from old 2LPTic */
IDStart = 1;
for(i = 0; i < GlassTileFac; i++)
for(j = 0; j < GlassTileFac; j++)
for(k = 0; k < GlassTileFac; k++)
{
for(type = 0, n = 0; type < 6; type++)
{
for(m = 0; m < header1.npartTotal[type]; m++, n++)
{
x = pos[3 * n] / header1.BoxSize * (Box / GlassTileFac) + i * (Box / GlassTileFac);
slab = x / Box * Nmesh;
if(slab >= Nmesh)
slab = Nmesh - 1;
if(Slab_to_task[slab] == ThisTask)
{
y = pos[3 * n + 1] / header1.BoxSize * (Box / GlassTileFac) + j * (Box / GlassTileFac);
z = pos[3 * n + 2] / header1.BoxSize * (Box / GlassTileFac) + k * (Box / GlassTileFac);
P[count].Pos[0] = x;
P[count].Pos[1] = y;
P[count].Pos[2] = z;
#ifdef MULTICOMPONENTGLASSFILE
P[count].Type = type - 1;
#endif
P[count].ID = IDStart;
#ifndef USE_64BITID
/*If 32bit IDs are requested, then check that these generated IDs will fit.
Simulations are wasted no more. */
if(P[count].ID > INT_MAX) {
printf("On task %d: Can not represent particles ID = %lld with 32 bit integers. Enabled the compile time options USE_64BITID\n",
ThisTask, P[count].ID);
FatalError(10000);
}
#endif
count++;
}
IDStart++;
}
}
}
#endif //end of #if 0 -> alternate code for non-preserving IDs from original 2LPTic
if(count != NumPart)
{
printf("fatal mismatch (%d %d) on Task %d\n", count, NumPart, ThisTask);
FatalError(1);
}
free(pos);
}
int find_files(char *fname)
{
FILE *fd;
char buf[200], buf1[200];
int32_t dummy;
sprintf(buf, "%s.%d", fname, 0);
sprintf(buf1, "%s", fname);
if((fd = fopen(buf, "r")))
{
my_fread(&dummy, sizeof(dummy), 1, fd);
my_fread(&header, sizeof(header), 1, fd);
my_fread(&dummy, sizeof(dummy), 1, fd);
fclose(fd);
return header.num_files;
}
if((fd = fopen(buf1, "r")))
{
my_fread(&dummy, sizeof(dummy), 1, fd);
my_fread(&header, sizeof(header), 1, fd);
my_fread(&dummy, sizeof(dummy), 1, fd);
fclose(fd);
header.num_files = 1;
return header.num_files;
}
FatalError(121);
return 0;
}