-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathic_relic.hpp
More file actions
333 lines (243 loc) · 10.7 KB
/
ic_relic.hpp
File metadata and controls
333 lines (243 loc) · 10.7 KB
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
//////////////////////////
// ic_relic.hpp
//////////////////////////
//
// initial condition generator for gevolution using input files in the format as provided by RELIC
// [see J. Adamek, J. Calles, T. Montandon, J. Noreña, and C. Stahl, JCAP 2204, 001 (2022)]
//
// Authors: Julian Adamek (Université de Genève & Observatoire de Paris & Queen Mary University of London & Universität Zürich)
// Thomas Montandon (Universität Wien & Université de Montpellier)
//
// Last modified: August 2024
//
//////////////////////////
#ifndef IC_RELIC_HEADER
#define IC_RELIC_HEADER
using namespace LATfield2;
void generateIC_relic(metadata & sim, icsettings & ic, cosmology & cosmo, const double fourpiG, Particles<part_simple,part_simple_info,part_simple_dataType> * pcls_cdm, Particles<part_simple,part_simple_info,part_simple_dataType> * pcls_b, Particles<part_simple,part_simple_info,part_simple_dataType> * pcls_ncdm, double * maxvel, Field<Real> * phi, Field<Real> * chi, Field<Real> * Bi, Field<Real> * source, Field<Real> * Sij, Field<Cplx> * zetaFT, Field<Cplx> * scalarFT, Field<Cplx> * BiFT, Field<Cplx> * SijFT, PlanFFT<Cplx> * plan_phi, PlanFFT<Cplx> * plan_chi, PlanFFT<Cplx> * plan_Bi, PlanFFT<Cplx> * plan_source, PlanFFT<Cplx> * plan_Sij, parameter * params, int & numparam)
{
double a = 1. / (1. + sim.z_in);
float * pcldata = NULL;
double * sinc = NULL;
Site x(phi->lattice());
rKSite kFT(scalarFT->lattice());
double max_displacement;
part_simple_info pcls_cdm_info;
part_simple_dataType pcls_cdm_dataType;
part_simple_info pcls_b_info;
part_simple_dataType pcls_b_dataType;
Real boxSize[3] = {1.,1.,1.};
Field<Real> * ic_fields[2];
string filename;
int reduce = MAX;
ic_fields[0] = chi;
ic_fields[1] = chi;
// generate the kernel for the 1st order displacement field
loadHomogeneousTemplate(ic.pclfile[0], sim.numpcl[0], pcldata);
if (pcldata == NULL)
{
COUT << " error: particle data was empty!" << endl;
parallel.abortForce();
}
if (ic.flags & ICFLAG_CORRECT_DISPLACEMENT)
generateCICKernel(*source, sim.numpcl[0], pcldata, ic.numtile[0]);
else
generateCICKernel(*source);
plan_source->execute(FFT_FORWARD);
// use BiFT as temporary storage for the kernel
for (kFT.first(); kFT.test(); kFT.next())
(*BiFT)(kFT, 0) = (*scalarFT)(kFT);
// precompute sinc function
sinc = (double *) malloc(sim.numpts * sizeof(double));
sinc[0] = 1.;
for (int i = 1; i <= sim.numpts/2; i++)
sinc[i] = sin(M_PI * i / sim.numpts) / (M_PI * i / sim.numpts);
for (int i = 1; i < sim.numpts/2; i++)
sinc[sim.numpts-i] = sinc[i];
if (sim.gr_flag == 1 || sim.radiation_flag > 0 || sim.fluid_flag > 0)
{
filename.assign(ic.metricfile[0]);
COUT << " reading " << filename << "..." << endl;
phi->loadHDF5(filename);
phi->updateHalo();
// toma
plan_phi->execute(FFT_FORWARD);
#ifdef HAVE_CLASS
if (zetaFT == NULL && (sim.radiation_flag > 0 || sim.fluid_flag > 0))
{
zetaFT = new Field<Cplx>;
zetaFT->initialize(scalarFT->lattice(), 1);
zetaFT->alloc();
for (kFT.first(); kFT.test(); kFT.next())
(*zetaFT)(kFT) = (*scalarFT)(kFT);
}
#endif
for (kFT.first(); kFT.test(); kFT.next())
{
double W = sinc[kFT.coord(0)] * sinc[kFT.coord(1)] * sinc[kFT.coord(2)];
(*scalarFT)(kFT) *= W*W/sim.numpts/sim.numpts/sim.numpts;
}
plan_phi->execute(FFT_BACKWARD);
phi->updateHalo();
// end toma
}
filename.assign(ic.densityfile[0]);
COUT << " reading " << filename << "..." << endl;
source->loadHDF5(filename);
// toma
source->updateHalo();
plan_source->execute(FFT_FORWARD);
for (kFT.first(); kFT.test(); kFT.next())
{
double W = sinc[kFT.coord(0)] * sinc[kFT.coord(1)] * sinc[kFT.coord(2)];
(*scalarFT)(kFT) *= W*W/sim.numpts/sim.numpts/sim.numpts;
}
plan_source->execute(FFT_BACKWARD);
source->updateHalo();
// end toma
COUT << " computing 1st order displacement..." << endl;
if (sim.gr_flag == 1)
{
for (x.first(); x.test(); x.next())
(*chi)(x) = (*source)(x) - 3. * (*phi)(x);
}
else
{
for (x.first(); x.test(); x.next())
(*chi)(x) = (*source)(x);
}
plan_chi->execute(FFT_FORWARD);
for (kFT.first(); kFT.test(); kFT.next())
{
if ((*BiFT)(kFT, 0).norm() > 1.0e-16)
(*scalarFT)(kFT) = (*scalarFT)(kFT) / (*BiFT)(kFT, 0);
}
plan_chi->execute(FFT_BACKWARD);
chi->updateHalo();
//filename.assign("1st_order_displacement.h5");
//chi->saveHDF5(filename);
strcpy(pcls_cdm_info.type_name, "part_simple");
if (sim.baryon_flag == 1)
pcls_cdm_info.mass = cosmo.Omega_cdm / (Real) (sim.numpcl[0]*(long)ic.numtile[0]*(long)ic.numtile[0]*(long)ic.numtile[0]);
else
pcls_cdm_info.mass = (cosmo.Omega_cdm + cosmo.Omega_b) / (Real) (sim.numpcl[0]*(long)ic.numtile[0]*(long)ic.numtile[0]*(long)ic.numtile[0]);
pcls_cdm_info.relativistic = false;
pcls_cdm->initialize(pcls_cdm_info, pcls_cdm_dataType, &(phi->lattice()), boxSize);
initializeParticlePositions(sim.numpcl[0], pcldata, ic.numtile[0], *pcls_cdm);
if (sim.baryon_flag == 3) // baryon treatment = hybrid; displace particles using both displacement fields
pcls_cdm->moveParticles(displace_pcls_ic_basic, 1./sim.numpts/sim.numpts/sim.numpts, ic_fields, 2, NULL, &max_displacement, &reduce, 1);
else
pcls_cdm->moveParticles(displace_pcls_ic_basic, 1./sim.numpts/sim.numpts/sim.numpts, &chi, 1, NULL, &max_displacement, &reduce, 1); // displace CDM particles
sim.numpcl[0] *= (long) ic.numtile[0] * (long) ic.numtile[0] * (long) ic.numtile[0];
COUT << " " << sim.numpcl[0] << " cdm particles initialized: maximum displacement (1st order) = " << max_displacement * sim.numpts << " lattice units." << endl;
filename.assign(ic.velocityfile[0]); // not useful for Newton
COUT << " reading " << filename << "..." << endl;
chi->loadHDF5(filename);
chi->updateHalo();
// toma
plan_chi->execute(FFT_FORWARD);
for (kFT.first(); kFT.test(); kFT.next())
{
double W = sinc[kFT.coord(0)] * sinc[kFT.coord(1)] * sinc[kFT.coord(2)];
(*scalarFT)(kFT) *= W*W/sim.numpts/sim.numpts/sim.numpts;
}
plan_chi->execute(FFT_BACKWARD);
chi->updateHalo();
// end toma
maxvel[0] = pcls_cdm->updateVel(initialize_q_ic_basic, -a/sim.boxsize, &chi, 1) / a;
COUT << " computing 1st order density..." << endl;
projection_init(chi);
projection_T00_project(pcls_cdm, chi, a, sim.gr_flag ? phi : NULL); // gr_flag=1 -> GR, else Newton
scalarProjectionCIC_comm(chi);
for (x.first(); x.test(); x.next())
(*chi)(x) = (*chi)(x) / (cosmo.Omega_cdm + cosmo.Omega_b) - 1.;
//filename.assign("1st_order_density.h5");
//chi->saveHDF5(filename);
for (x.first(); x.test(); x.next())
(*chi)(x) = (*source)(x) - (*chi)(x);
plan_chi->execute(FFT_FORWARD);
for (kFT.first(); kFT.test(); kFT.next())
{
if ((*BiFT)(kFT, 0).norm() > 1.0e-16)
(*scalarFT)(kFT) = (*scalarFT)(kFT) / (*BiFT)(kFT, 0);
}
plan_chi->execute(FFT_BACKWARD);
chi->updateHalo();
//filename.assign("2nd_order_displacement.h5");
//chi->saveHDF5(filename);
pcls_cdm->moveParticles(displace_pcls_ic_basic, 1./sim.numpts/sim.numpts/sim.numpts, &chi, 1, NULL, &max_displacement, &reduce, 1);
COUT << " " << sim.numpcl[0] << " cdm particles initialized: maximum displacement (2nd order) = " << max_displacement * sim.numpts << " lattice units." << endl;
free(pcldata);
COUT << " computing 2nd order density..." << endl;
projection_init(chi);
projection_T00_project(pcls_cdm, chi, a, sim.gr_flag ? phi : NULL); // toma
scalarProjectionCIC_comm(chi);
for (x.first(); x.test(); x.next())
(*chi)(x) = (*source)(x) - ((*chi)(x) / (cosmo.Omega_cdm + cosmo.Omega_b) - 1.);
plan_chi->execute(FFT_FORWARD);
for (kFT.first(); kFT.test(); kFT.next())
{
if ((*BiFT)(kFT, 0).norm() > 1.0e-16)
(*scalarFT)(kFT) = (*scalarFT)(kFT) / (*BiFT)(kFT, 0);
}
plan_chi->execute(FFT_BACKWARD);
chi->updateHalo();
//filename.assign("3rd_order_displacement.h5");
//chi->saveHDF5(filename);
pcls_cdm->moveParticles(displace_pcls_ic_basic, 1./sim.numpts/sim.numpts/sim.numpts, &chi, 1, NULL, &max_displacement, &reduce, 1);
COUT << " " << sim.numpcl[0] << " cdm particles initialized: maximum displacement (3rd order) = " << max_displacement * sim.numpts << " lattice units." << endl;
if (sim.baryon_flag == 1)
{
loadHomogeneousTemplate(ic.pclfile[1], sim.numpcl[1], pcldata);
if (pcldata == NULL)
{
COUT << " error: particle data was empty!" << endl;
parallel.abortForce();
}
strcpy(pcls_b_info.type_name, "part_simple");
pcls_b_info.mass = cosmo.Omega_b / (Real) (sim.numpcl[1]*(long)ic.numtile[1]*(long)ic.numtile[1]*(long)ic.numtile[1]);
pcls_b_info.relativistic = false;
pcls_b->initialize(pcls_b_info, pcls_b_dataType, &(phi->lattice()), boxSize);
initializeParticlePositions(sim.numpcl[1], pcldata, ic.numtile[1], *pcls_b);
pcls_b->moveParticles(displace_pcls_ic_basic, 1./sim.boxsize/sim.boxsize, &phi, 1, NULL, &max_displacement, &reduce, 1); // displace baryon particles
sim.numpcl[1] *= (long) ic.numtile[1] * (long) ic.numtile[1] * (long) ic.numtile[1];
COUT << " " << sim.numpcl[1] << " baryon particles initialized: maximum displacement = " << max_displacement * sim.numpts << " lattice units." << endl;
free(pcldata);
}
if (ic.pkfile[0] == '\0') // set velocities using transfer functions
{
filename.assign(ic.velocityfile[1]);
chi->loadHDF5(filename);
chi->updateHalo();
if (sim.baryon_flag == 3) // baryon treatment = hybrid; set velocities using both velocity potentials
maxvel[0] = pcls_cdm->updateVel(initialize_q_ic_basic, -a/sim.boxsize, ic_fields, 2) / a;
else
maxvel[0] = pcls_cdm->updateVel(initialize_q_ic_basic, -a/sim.boxsize, &chi, 1) / a; // set CDM velocities
if (sim.baryon_flag == 1)
maxvel[1] = pcls_b->updateVel(initialize_q_ic_basic, -a/sim.boxsize, &phi, 1) / a; // set baryon velocities
}
if (sim.baryon_flag > 1) sim.baryon_flag = 0;
projection_init(Bi);
projection_T0i_project(pcls_cdm, Bi, phi);
if (sim.baryon_flag)
projection_T0i_project(pcls_b, Bi, phi);
projection_T0i_comm(Bi);
prepareFTsource(*Bi, *phi, 3. * a * a * Hconf(a, fourpiG, cosmo) * (double) sim.numpts / fourpiG);
plan_Bi->execute(FFT_FORWARD);
projectFTvector(*BiFT, *BiFT, fourpiG / (double) sim.numpts / (double) sim.numpts);
plan_Bi->execute(FFT_BACKWARD);
Bi->updateHalo(); // B initialized
ic_fields[1] = Bi;
projection_init(Sij);
projection_Tij_project(pcls_cdm, Sij, a, phi);
if (sim.baryon_flag)
projection_Tij_project(pcls_b, Sij, a, phi);
projection_Tij_comm(Sij);
prepareFTsource<Real>(*phi, *Sij, *Sij, 2. * fourpiG / a / (double) sim.numpts / (double) sim.numpts);
plan_Sij->execute(FFT_FORWARD);
projectFTscalar(*SijFT, *scalarFT);
plan_chi->execute(FFT_BACKWARD);
chi->updateHalo(); // chi now finally contains chi
free(sinc);
}
#endif