-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFT_routines.c
406 lines (322 loc) · 12.2 KB
/
FFT_routines.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
/****************************************************************************************************
NAME: conf2dump
FUNCTION: Reads the input file with parameters
INPUT: Parameters file
RETURN: 0
****************************************************************************************************/
int conf2dump( char filename[] )
{
char cmd[1000];
int nread;
/*
sprintf( cmd, "grep -v \"#\" %s | grep -v \"^$\" | gawk -F\"=\" '{print $2}' > %s.dump",
filename, filename );
*/
sprintf( cmd, "grep -v \"#\" %s | grep -v \"^$\" | awk -F\"=\" '{print $2}' > %s.dump",
filename, filename );
nread = system( cmd );
return 0;
}
/****************************************************************************************************
NAME: read_parameters
FUNCTION: Reads the parameters
INPUT: Parameters file
RETURN: 0
****************************************************************************************************/
int read_parameters( char filename[] )
{
int nread;
char cmd[1000], filenamedump[1000];
FILE *file;
/*+++++ Loading the file +++++*/
file = fopen( filename, "r" );
if( file==NULL )
{
printf( " * The file '%s' doesn't exist!\n", filename );
return 1;
}
fclose(file);
/*+++++ Converting to plain text +++++*/
conf2dump( filename );
sprintf( filenamedump, "%s.dump", filename );
file = fopen( filenamedump, "r" );
/*+++++ Parameters for binary data +++++*/
#ifdef BINARYPARAMS
nread = fscanf(file, "%d", &GV.NCELLS);
nread = fscanf(file, "%s", GV.FILENAME);
#endif
#ifdef SUPERCIC
nread = fscanf(file, "%s", GV.FILENAMEVELS);
#endif
/*+++++ Parameters for ASCII data +++++*/
#ifdef ASCIIDATA
/*+++++ Simulation parameters +++++*/
nread = fscanf(file, "%d", &GV.NCELLS);
nread = fscanf(file, "%lf", &GV.BoxSize);
nread = fscanf(file, "%s", GV.FILENAME);
/*+++++ Cosmological parameters +++++*/
nread = fscanf(file, "%lf", &GV.Omega_M0);
nread = fscanf(file, "%lf", &GV.Omega_L0);
nread = fscanf(file, "%lf", &GV.z_RS);
nread = fscanf(file, "%lf", &GV.h_Hubble);
GV.H0 = 100.0;// * GV.h_Hubble;
GV.a_SF = 1.0/(1.0 + GV.z_RS);
#endif
fclose( file );
printf( " * The file '%s' has been loaded!\n", filename );
printf("File to read is %s ", GV.FILENAME);
printf("File to read is %s ", GV.FILENAMEVELS);
printf("with %d cells per axis\n", GV.NCELLS);
sprintf( cmd, "rm -rf %s.dump", filename );
nread = system( cmd );
return 0;
}
/**************************************************************************
NAME: read_data
FUNCTION: reads the input file
INPUT: data file: data_complete_NCELLSXXX.dat (densities data file)
RETURN: 0
*****************************************************************************/
#ifdef CIC_MDR
int read_data(char *infile, double *DenConCell)
{
int m, nread;
FILE *pf=NULL;
char buff[1000];
double dummy;
int dummy1;
pf = fopen(infile, "r");
/*+++ Ignoring the first line +++*/
//nread = fgets(buff, 1000, pf);
fgets(buff, 1000, pf);
/*+++ Reading from the second line +++*/
for(m=0; m<GV.NTOTALCELLS; m++)
{
nread = fscanf(pf,"%d%*[,] %d%*[,] %d%*[,] %d%*[,] %lf%*[,]",
&dummy1, &dummy1, &dummy1, &dummy1, &DenConCell[m]);
if(m%5000000==0)
{
printf("%d %lf\n",
m, DenConCell[m]);
}//if
}//for m
/*
for(m=0; m<GV.NTOTALCELLS; m++)
{
nread=fscanf(pf,"%d %lf %lf %lf %lf",
&dummy1, &dummy, &dummy, &dummy,
&DenConCell[m]);
if(m%5000000==0)
{
printf("%d %lf\n",
m, DenConCell[m]);
}//if
}//for m
*/
fclose(pf);
return 0;
}//read_data
#endif
/****************************************************************************************************
NAME: read_binary
FUNCTION: Reads the binary data file
INPUT: None
RETURN: 0
****************************************************************************************************/
int read_binary(double *DenConCell, double **p_r)
{
int m, nread;
double pos_aux[3];
double dummy;
FILE *inFile=NULL;
inFile = fopen(GV.FILENAME, "r");
/*+++++ Saving Simulation parameters +++++*/
nread = fread(&GV.BoxSize, sizeof(double), 1, inFile); //Box Size
nread = fread(&GV.Omega_M0, sizeof(double), 1, inFile); //Matter density parameter
nread = fread(&GV.Omega_L0, sizeof(double), 1, inFile); //Cosmological constant density parameter
nread = fread(&GV.z_RS, sizeof(double), 1, inFile); //Redshift
nread = fread(&GV.h_Hubble, sizeof(double), 1, inFile); //Hubble parameter
GV.H0 = 100.0;// * GV.h_Hubble;
GV.a_SF = 1.0 / (1.0 + GV.z_RS);
printf("-----------------------------------------------\n");
printf("Cosmological parameters:\n");
printf("OmegaM0=%lf OmegaL0=%lf redshift=%lf HubbleParam=%lf H0=%lf\n",
GV.Omega_M0,
GV.Omega_L0,
GV.z_RS,
GV.h_Hubble,
GV.H0);
printf("-----------------------------------------------\n");
printf("Simulation parameters:\n");
printf("L=%lf\n",
GV.BoxSize);
printf("-----------------------------------------------\n");
#ifdef CIC_400
for(m=0; m<GV.NTOTALCELLS; m++ )
{
nread = fread(&pos_aux[0], sizeof(double), 3, inFile);
nread = fread(&p_r[m][0], sizeof(double), 3, inFile);
nread = fread(&DenConCell[m], sizeof(double), 1, inFile);
if(m%5000000==0)
{
printf("Reading m=%d x=%lf y=%lf z=%lf px=%lf py=%lf pz=%lf DenCon=%lf\n",
m, pos_aux[X], pos_aux[Y], pos_aux[Z],
p_r[m][X], p_r[m][Y], p_r[m][Z],
DenConCell[m]);
}//if
}//for m
#endif
#ifdef NGP_400
for(m=0; m<GV.NTOTALCELLS; m++ )
{
nread = fread(&pos_aux[0], sizeof(double), 3, inFile);
nread = fread(&p_r[m][0], sizeof(double), 3, inFile);
nread = fread(&DenConCell[m], sizeof(double), 1, inFile);
if(m%5000000==0)
{
printf("Reading m=%d x=%lf y=%lf z=%lf px=%lf py=%lf pz=%lf DenCon=%lf\n",
m, pos_aux[X], pos_aux[Y], pos_aux[Z],
p_r[m][X], p_r[m][Y], p_r[m][Z],
DenConCell[m]);
}//if
}//for m
#endif
#ifdef CIC_MDR
free(p_r);
for(m=0; m<GV.NTOTALCELLS; m++ )
{
nread = fread(&pos_aux[0], sizeof(double), 3, inFile);
nread = fread(&DenConCell[m], sizeof(double), 1, inFile);
if(m%5000000==0)
{
printf("Reading m=%d x=%lf y=%lf z=%lf DenCon=%lf\n",
m,
pos_aux[X], pos_aux[Y], pos_aux[Z],
DenConCell[m]);
}//if
}//for m
#endif
fclose(inFile);
return 0;
}//read_binary
/****************************************************************************************************
NAME: read_binary_super_CIC
FUNCTION: Reads the binary data file
INPUT: None
RETURN: 0
****************************************************************************************************/
int read_binary_super_CIC(double *DenConCell)
{
int m, nread;
double pos_aux[3];
double dummy_d;
int dummy_int;
char dummy_char;
FILE *inFile=NULL;
inFile = fopen(GV.FILENAME, "r");
/* Saving cosmological parameters of the simulation */
nread = fread( &(GV.Omega_M0), sizeof(double), 1, inFile);
nread = fread( &(GV.Omega_L0), sizeof(double), 1, inFile);
nread = fread( &(GV.z_RS), sizeof(double), 1, inFile);
nread = fread( &(GV.h_Hubble), sizeof(double), 1, inFile);
/* Saving simulation parameters */
nread = fread( &(GV.NCELLS), sizeof(int), 1, inFile);
nread = fread( &(dummy_int), sizeof(int), 1, inFile);
nread = fread( &(GV.BoxSize), sizeof(double), 1, inFile);
nread = fread( &(GV.Total_NParts), sizeof(unsigned long), 1, inFile);
nread = fread( &(dummy_d), sizeof(double), 1, inFile);
nread = fread( &(GV.MeanDen), sizeof(double), 1, inFile);
nread = fread( &(dummy_d), sizeof(double), 1, inFile);
nread = fread( &(dummy_d), sizeof(double), 1, inFile);
nread = fread( &(dummy_char), sizeof(char), 1, inFile);
nread = fread( &(dummy_char), sizeof(char), 1, inFile);
nread = fread( &(dummy_char), sizeof(char), 1, inFile);
GV.NTOTALCELLS = POW3(GV.NCELLS);
/*+++++ Saving Simulation parameters +++++*/
GV.H0 = 100.0; // * GV.h_Hubble;
GV.a_SF = 1.0 / (1.0 + GV.z_RS);
printf("-----------------------------------------------\n");
printf("Cosmological parameters:\n");
printf("OmegaM0=%lf OmegaL0=%lf redshift=%lf HubbleParam=%lf H0=%lf\n",
GV.Omega_M0,
GV.Omega_L0,
GV.z_RS,
GV.h_Hubble,
GV.H0);
printf("-----------------------------------------------\n");
printf("Simulation parameters:\n");
printf("L=%lf\n",
GV.BoxSize);
printf("-----------------------------------------------\n");
//fwrite( &(denCon[index_cell]), sizeof(double), 1, outfile );
//fwrite( &(vx[index_cell]), sizeof(double), 1, outfile_vel );
//fwrite( &(vy[index_cell]), sizeof(double), 1, outfile_vel );
//fwrite( &(vz[index_cell]), sizeof(double), 1, outfile_vel );
for(m=0; m<GV.NTOTALCELLS; m++ )
{
nread = fread(&DenConCell[m], sizeof(double), 1, inFile);
if(m%100000000==0)
{
printf("Reading m=%d DenCon=%lf\n", m, DenConCell[m]);
}//if
}//for m
fclose(inFile);
return 0;
}//read_binary
/****************************************************************************************************
NAME: write_binary
FUNCTION: Writes binary data file
INPUT: none
RETURN: 0
****************************************************************************************************/
/*
int write_binary(void)
{
int i, nread;
double pos_aux[3];
FILE *outFile=NULL;
outFile = fopen("./../Processed_data/DenCon_Pot_PotDot_fields_256.bin", "w");
//+++++ Saving Simulation parameters +++++
fwrite(&GV.BoxSize, sizeof(double), 1, outFile); // Box Size
fwrite(&GV.Omega_M0, sizeof(double), 1, outFile); // Matter density parameter
fwrite(&GV.Omega_L0, sizeof(double), 1, outFile); // Cosmological constant density parameter
fwrite(&GV.z_RS, sizeof(double), 1, outFile); // Redshift
fwrite(&GV.H0, sizeof(double), 1, outFile); // Hubble parameter
for(i=0; i<GV.NTOTALCELLS; i++ )
{
fwrite(&gp[i].GID, sizeof(int), 1, outFile);
//----- Positions -----
pos_aux[X] = gp[i].pos[X];
pos_aux[Y] = gp[i].pos[Y];
pos_aux[Z] = gp[i].pos[Z];
fwrite(&pos_aux[0], sizeof(double), 3, outFile); // Cell position
fwrite(&gp[i].DenConCell, sizeof(double), 1, outFile); // Density contrast in cell
fwrite(&gp[i].poten_r[0], sizeof(double), 1, outFile); // Gravitational potential in cell
fwrite(&gp[i].potDot_r[0], sizeof(double), 1, outFile); // PotDot exact
fwrite(&gp[i].potDot_r_l_app1[0], sizeof(double), 1, outFile); // PotDot in first approximation
fwrite(&gp[i].potDot_r_l_app2[0], sizeof(double), 1, outFile); // PotDot in second approximation
}//for i
fclose(outFile);
return 0;
}//write_binary
*/
/*
* Function: mod
* --------------------
* Calculate the module operation for two numbers a and b (a%b)
* including negative numbers.
*
* a: Numerator of the division.
* b: Denominator of the division.
*
* returns: The module a%b including the option for negative numbers.
*/
int mod(int a, int b)
{
int mod = a%b;
while(mod<0)
{
mod += b;
}
return mod;
}