-
Notifications
You must be signed in to change notification settings - Fork 7
/
lgadgetio.c
237 lines (196 loc) · 6.28 KB
/
lgadgetio.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <mpi.h>
#include "raytrace.h"
#include "lgadgetio.h"
//LGadget-2 I/O header for Lightcones
struct io_header_1 {
unsigned int npart[6]; /*!< npart[1] gives the number of particles in the present file, other particle types are ignored */
double mass[6]; /*!< mass[1] gives the particle mass */
double time; /*!< time (=cosmological scale factor) of snapshot */
double redshift; /*!< redshift of snapshot */
int flag_sfr; /*!< flags whether star formation is used (not available in L-Gadget2) */
int flag_feedback; /*!< flags whether feedback from star formation is included */
unsigned int npartTotal[6]; /*!< npart[1] gives the total number of particles in the run. If this number exceeds 2^32, the npartTotal[2] stores
the result of a division of the particle number by 2^32, while npartTotal[1] holds the remainder. */
int flag_cooling; /*!< flags whether radiative cooling is included */
int num_files; /*!< determines the number of files that are used for a snapshot */
double BoxSize; /*!< Simulation box size (in code units) */
double Omega0; /*!< matter density */
double OmegaLambda; /*!< vacuum energy density */
double HubbleParam; /*!< little 'h' */
int flag_stellarage; /*!< flags whether the age of newly formed stars is recorded and saved */
int flag_metals; /*!< flags whether metal enrichment is included */
int hashtabsize; /*!< gives the size of the hashtable belonging to this snapshot file */
//char fill[84]; /*!< fills to 256 Bytes */
unsigned int npartTotalHighWord[6]; /*!< High word of the total number of par
ticles of each type */
char fill[60];
};
long get_numparts_LGADGET(char fname[])
{
FILE *fp;
struct io_header_1 header1;
long np;
int dummy;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
fclose(fp);
np = (long) (header1.npartTotal[1]) + (((long) (header1.npartTotal[2])) << 32);
return np;
}
int get_numfiles_LGADGET(char fname[])
{
FILE *fp;
struct io_header_1 header1;
int dummy;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
fclose(fp);
return header1.num_files;
}
float get_omegam_LGADGET(char fname[])
{
FILE *fp;
struct io_header_1 header1;
int dummy;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
fclose(fp);
return header1.Omega0;
}
float get_scale_factor_LGADGET(char fname[])
{
FILE *fp;
struct io_header_1 header1;
int dummy;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
fclose(fp);
return header1.time;
}
float get_period_length_LGADGET(char fname[])
{
FILE *fp;
struct io_header_1 header1;
int dummy;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
fclose(fp);
return header1.BoxSize;
}
void read_LGADGET(char fname[], float **px, float **py, float **pz, long **id, int *Np)
{
FILE *fp;
struct io_header_1 header1;
int dummy,k;
float *partChunk;
#define SKIP fread(&dummy,sizeof(dummy),(size_t) 1,fp);
fp = fopen_retry(fname,"r");
if(fp == NULL)
{
fprintf(stderr,"%d: could not open file '%s'!\n",ThisTask,fname);
assert(fp != NULL);
}
SKIP;
fread(&header1,sizeof(header1),(size_t) 1,fp);
SKIP;
*Np = 0;
for(k=0;k<5;k++)
*Np += header1.npart[k];
*px = (float*)malloc(sizeof(float)*(*Np));
assert((*px) != NULL);
*py = (float*)malloc(sizeof(float)*(*Np));
assert((*py) != NULL);
*pz = (float*)malloc(sizeof(float)*(*Np));
assert((*pz) != NULL);
if(id != NULL)
{
*id = (long*)malloc(sizeof(long)*(*Np));
assert((*id) != NULL);
}
//read positions
partChunk = (float*)malloc(sizeof(float)*(*Np)*3);
assert(partChunk != NULL);
SKIP;
assert(dummy == (*Np)*3*sizeof(float));
fread(partChunk,(size_t) (*Np),3*sizeof(float),fp);
SKIP;
assert(dummy == (*Np)*3*sizeof(float));
for(k=0;k<(*Np);++k)
{
(*px)[k] = partChunk[k*3 + 0];
(*py)[k] = partChunk[k*3 + 1];
(*pz)[k] = partChunk[k*3 + 2];
while((*px)[k] < 0.0)
(*px)[k] += header1.BoxSize;
while((*px)[k] > header1.BoxSize)
(*px)[k] -= header1.BoxSize;
while((*py)[k] < 0.0)
(*py)[k] += header1.BoxSize;
while((*py)[k] > header1.BoxSize)
(*py)[k] -= header1.BoxSize;
while((*pz)[k] < 0.0)
(*pz)[k] += header1.BoxSize;
while((*pz)[k] > header1.BoxSize)
(*pz)[k] -= header1.BoxSize;
}
free(partChunk);
if(id != NULL)
{
SKIP;
assert(dummy == (*Np)*3*sizeof(float));
fseek(fp,3*sizeof(float)*(*Np),SEEK_CUR);
SKIP;
assert(dummy == (*Np)*3*sizeof(float));
SKIP;
assert(dummy == (*Np)*sizeof(long));
fread(*id,(size_t) (*Np),sizeof(long),fp);
SKIP;
assert(dummy == (*Np)*sizeof(long));
}
#undef SKIP
fclose(fp);
}