-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclass_tools.hpp
420 lines (343 loc) · 14.7 KB
/
class_tools.hpp
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
418
419
//////////////////////////
// class_tools.hpp
//////////////////////////
//
// interface to linear Boltzmann code CLASS
//
// Author: Julian Adamek (Université de Genève & Observatoire de Paris)
//
// Last modified: March 2017
//
//////////////////////////
#ifndef CLASS_TOOLS_HEADER
#define CLASS_TOOLS_HEADER
#ifdef HAVE_CLASS
#include <gsl/gsl_spline.h>
using namespace std;
using namespace LATfield2;
//////////////////////////
// initializeCLASSstructures
//////////////////////////
// Description:
// initializes CLASS structures containing interpolation tables for various transfer functions
//
// Arguments:
// sim simulation metadata structure
// ic settings for IC generation
// cosmo cosmological parameter structure
// class_background CLASS structure that will contain the background
// class_perturbs CLASS structure that will contain perturbations
// class_spectra CLASS structure that will contain spectra
// output_value CLASS parameter value specifying the output (optional)
//
// Returns:
//
//////////////////////////
void initializeCLASSstructures(metadata & sim, icsettings & ic, cosmology & cosmo, background & class_background, perturbs & class_perturbs, spectra & class_spectra, const char * output_value = "dTk, vTk")
{
precision class_precision;
thermo class_thermo;
transfers class_transfers;
primordial class_primordial;
nonlinear class_nonlinear;
lensing class_lensing;
output class_output;
file_content class_filecontent;
ErrorMsg class_errmsg;
char filename[] = "initializeCLASSstructures";
char tmp[8 * PARAM_MAX_LENGTH];
int i;
int num_entries = 16;
#ifdef CLASS_K_PER_DECADE_FOR_PK
num_entries += 1;
#endif
if (cosmo.num_ncdm > 0) num_entries += 3;
if (parallel.isRoot()) num_entries += 7;
if ((1.015 * ic.z_ic + 0.01) > 9999.)
num_entries += 2;
parser_init(&class_filecontent, num_entries, filename, class_errmsg);
for (i = 0; i < num_entries; i++)
class_filecontent.read[i] = _FALSE_;
i = 0;
sprintf(class_filecontent.name[i], "root");
sprintf(class_filecontent.value[i++], "%s%s_class", sim.output_path, sim.basename_generic);
sprintf(class_filecontent.name[i], "k_pivot");
sprintf(class_filecontent.value[i++], "%e", ic.k_pivot);
sprintf(class_filecontent.name[i], "A_s");
sprintf(class_filecontent.value[i++], "%e", ic.A_s);
sprintf(class_filecontent.name[i], "n_s");
sprintf(class_filecontent.value[i++], "%f", ic.n_s);
sprintf(class_filecontent.name[i], "z_pk");
if (ic.z_ic > sim.z_in)
sprintf(class_filecontent.value[i++], "%f, %f, 0", 1.015 * ic.z_ic + 0.01, sim.z_in);
else
sprintf(class_filecontent.value[i++], "%f, 0", 1.015 * ic.z_ic + 0.01);
sprintf(class_filecontent.name[i], "output");
sprintf(class_filecontent.value[i++], "%s", output_value);
sprintf(class_filecontent.name[i], "gauge");
sprintf(class_filecontent.value[i++], "Newtonian");
sprintf(class_filecontent.name[i], "P_k_ini type");
sprintf(class_filecontent.value[i++], "analytic_Pk");
sprintf(class_filecontent.name[i], "P_k_max_h/Mpc");
sprintf(class_filecontent.value[i++], "%f", 2. * M_PI * (double) sim.numpts / sim.boxsize);
sprintf(class_filecontent.name[i], "h");
sprintf(class_filecontent.value[i++], "%f", cosmo.h);
sprintf(class_filecontent.name[i], "Omega_cdm");
sprintf(class_filecontent.value[i++], "%e", cosmo.Omega_cdm);
sprintf(class_filecontent.name[i], "Omega_b");
sprintf(class_filecontent.value[i++], "%e", cosmo.Omega_b);
sprintf(class_filecontent.name[i], "Omega_g");
sprintf(class_filecontent.value[i++], "%e", cosmo.Omega_g);
sprintf(class_filecontent.name[i], "Omega_ur");
sprintf(class_filecontent.value[i++], "%e", cosmo.Omega_ur);
sprintf(class_filecontent.name[i], "N_ncdm");
sprintf(class_filecontent.value[i++], "%d", cosmo.num_ncdm);
sprintf(class_filecontent.name[i], "perturb_sampling_stepsize");
sprintf(class_filecontent.value[i++], "0.01");
#ifdef CLASS_K_PER_DECADE_FOR_PK
sprintf(class_filecontent.name[i], "k_per_decade_for_pk");
sprintf(class_filecontent.value[i++], "%d", CLASS_K_PER_DECADE_FOR_PK);
#endif
if ((1.015 * ic.z_ic + 0.01) > 9999.)
{
sprintf(class_filecontent.name[i], "recfast_z_initial");
sprintf(class_filecontent.value[i++], "%e", ic.z_ic * 1.02);
sprintf(class_filecontent.name[i], "recfast_Nz0");
sprintf(class_filecontent.value[i++], "%d", 2 * (int) ceil(ic.z_ic * 1.02));
}
if (parallel.isRoot())
{
sprintf(class_filecontent.name[i], "background_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "perturbations_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "spectra_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "thermodynamics_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "transfer_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "primordial_verbose");
sprintf(class_filecontent.value[i++], "1");
sprintf(class_filecontent.name[i], "nonlinear_verbose");
sprintf(class_filecontent.value[i++], "1");
}
if (cosmo.num_ncdm > 0)
{
sprintf(class_filecontent.name[num_entries-3], "m_ncdm");
sprintf(class_filecontent.value[num_entries-3], "%f", cosmo.m_ncdm[0]);
for (i = 1; i < cosmo.num_ncdm; i++)
{
sprintf(tmp, "%s, %f", class_filecontent.value[num_entries-3], cosmo.m_ncdm[i]);
sprintf(class_filecontent.value[num_entries-3], "%s", tmp);
}
sprintf(class_filecontent.name[num_entries-2], "T_ncdm");
sprintf(class_filecontent.value[num_entries-2], "%f", cosmo.T_ncdm[0]);
for (i = 1; i < cosmo.num_ncdm; i++)
{
sprintf(tmp, "%s, %f", class_filecontent.value[num_entries-2], cosmo.T_ncdm[i]);
sprintf(class_filecontent.value[num_entries-2], "%s", tmp);
}
sprintf(class_filecontent.name[num_entries-1], "deg_ncdm");
sprintf(class_filecontent.value[num_entries-1], "%f", cosmo.deg_ncdm[0]);
for (i = 1; i < cosmo.num_ncdm; i++)
{
sprintf(tmp, "%s, %f", class_filecontent.value[num_entries-1], cosmo.deg_ncdm[i]);
sprintf(class_filecontent.value[num_entries-1], "%s", tmp);
}
}
COUT << " gevolution is calling CLASS..." << endl << endl;
if (input_init(&class_filecontent, &class_precision, &class_background, &class_thermo, &class_perturbs, &class_transfers, &class_primordial, &class_spectra, &class_nonlinear, &class_lensing, &class_output, class_errmsg) == _FAILURE_)
{
COUT << " error: calling input_init from CLASS library failed!" << endl << " following error message was passed: " << class_errmsg << endl;
parallel.abortForce();
}
parser_free(&class_filecontent);
if (background_init(&class_precision, &class_background) == _FAILURE_)
{
COUT << " error: calling background_init from CLASS library failed!" << endl << " following error message was passed: " << class_background.error_message << endl;
parallel.abortForce();
}
if (thermodynamics_init(&class_precision, &class_background, &class_thermo) == _FAILURE_)
{
COUT << " error: calling thermodynamics_init from CLASS library failed!" << endl << " following error message was passed: " << class_thermo.error_message << endl;
parallel.abortForce();
}
if (perturb_init(&class_precision, &class_background, &class_thermo, &class_perturbs) == _FAILURE_)
{
COUT << " error: calling perturb_init from CLASS library failed!" << endl << " following error message was passed: " << class_perturbs.error_message << endl;
parallel.abortForce();
}
if (primordial_init(&class_precision, &class_perturbs, &class_primordial) == _FAILURE_)
{
COUT << " error: calling primordial_init from CLASS library failed!" << endl << " following error message was passed: " << class_primordial.error_message << endl;
parallel.abortForce();
}
if (nonlinear_init(&class_precision, &class_background, &class_thermo, &class_perturbs, &class_primordial, &class_nonlinear) == _FAILURE_)
{
COUT << " error: calling nonlinear_init from CLASS library failed!" << endl << " following error message was passed: " << class_nonlinear.error_message << endl;
parallel.abortForce();
}
if (transfer_init(&class_precision, &class_background, &class_thermo, &class_perturbs, &class_nonlinear, &class_transfers) == _FAILURE_)
{
COUT << " error: calling transfer_init from CLASS library failed!" << endl << " following error message was passed: " << class_transfers.error_message << endl;
parallel.abortForce();
}
if (spectra_init(&class_precision, &class_background, &class_perturbs, &class_primordial, &class_nonlinear, &class_transfers, &class_spectra) == _FAILURE_)
{
COUT << " error: calling spectra_init from CLASS library failed!" << endl << " following error message was passed: " << class_spectra.error_message << endl;
parallel.abortForce();
}
// Now, free unneeded structures
if (lensing_free(&class_lensing) == _FAILURE_)
{
COUT << " error: calling lensing_free from CLASS library failed!" << endl << " following error message was passed: " << class_lensing.error_message << endl;
parallel.abortForce();
}
if (transfer_free(&class_transfers) == _FAILURE_)
{
COUT << " error: calling transfer_free from CLASS library failed!" << endl << " following error message was passed: " << class_transfers.error_message << endl;
parallel.abortForce();
}
if (nonlinear_free(&class_nonlinear) == _FAILURE_)
{
COUT << " error: calling nonlinear_free from CLASS library failed!" << endl << " following error message was passed: " << class_nonlinear.error_message << endl;
parallel.abortForce();
}
if (primordial_free(&class_primordial) == _FAILURE_)
{
COUT << " error: calling primordial_free from CLASS library failed!" << endl << " following error message was passed: " << class_primordial.error_message << endl;
parallel.abortForce();
}
if (thermodynamics_free(&class_thermo) == _FAILURE_)
{
COUT << " error: calling thermodynamics_free from CLASS library failed!" << endl << " following error message was passed: " << class_thermo.error_message << endl;
parallel.abortForce();
}
COUT << endl << " CLASS structures initialized successfully." << endl;
}
//////////////////////////
// freeCLASSstructures
//////////////////////////
// Description:
// frees CLASS structures containing interpolation tables for various transfer functions
//
// Arguments:
// class_background CLASS structure that contains the background
// class_perturbs CLASS structure that contains perturbations
// class_spectra CLASS structure that contains spectra
//
// Returns:
//
//////////////////////////
void freeCLASSstructures(background & class_background, perturbs & class_perturbs, spectra & class_spectra)
{
if (spectra_free(&class_spectra) == _FAILURE_)
{
COUT << " error: calling spectra_free from CLASS library failed!" << endl << " following error message was passed: " << class_spectra.error_message << endl;
parallel.abortForce();
}
if (perturb_free(&class_perturbs) == _FAILURE_)
{
COUT << " error: calling perturb_free from CLASS library failed!" << endl << " following error message was passed: " << class_perturbs.error_message << endl;
parallel.abortForce();
}
if (background_free(&class_background) == _FAILURE_)
{
COUT << " error: calling background_free from CLASS library failed!" << endl << " following error message was passed: " << class_background.error_message << endl;
parallel.abortForce();
}
}
//////////////////////////
// loadTransferFunctions (2)
//////////////////////////
// Description:
// loads a set of tabulated transfer functions from some precomputed CLASS structures
//
// Arguments:
// class_background CLASS structure that contains the background
// class_perturbs CLASS structure that contains the perturbations
// class_spectra CLASS structure that contains the spectra
// tk_delta will point to the gsl_spline which holds the tabulated
// transfer function for delta (memory will be allocated)
// tk_theta will point to the gsl_spline which holds the tabulated
// transfer function for theta (memory will be allocated)
// qname string containing the name of the component (e.g. "cdm"); if no string is
// specified, the transfer functions for phi and psi are returned instead!
// boxsize comoving box size (in the same units as used in the CLASS output)
// z redshift at which the transfer functions are to be obtained
// h conversion factor between 1/Mpc and h/Mpc (theta is in units of 1/Mpc)
//
// Returns:
//
//////////////////////////
void loadTransferFunctions(background & class_background, perturbs & class_perturbs, spectra & class_spectra, gsl_spline * & tk_delta, gsl_spline * & tk_theta, const char * qname, const double boxsize, const double z, double h)
{
int cols = 0, dcol = -1, tcol = -1, kcol = -1;
double * k;
double * tk_d;
double * tk_t;
double * data;
char coltitles[_MAXTITLESTRINGLENGTH_] = {0};
char dname[16];
char tname[16];
char kname[8];
char * ptr;
spectra_output_tk_titles(&class_background, &class_perturbs, class_format, coltitles);
if (qname != NULL)
{
sprintf(dname, "d_%s", qname);
sprintf(tname, "t_%s", qname);
h /= boxsize;
}
else
{
sprintf(dname, "phi");
sprintf(tname, "psi");
h = 1.;
}
sprintf(kname, "k");
ptr = strtok(coltitles, _DELIMITER_);
while (ptr != NULL)
{
if (strncmp(ptr, dname, strlen(dname)) == 0) dcol = cols;
else if (strncmp(ptr, tname, strlen(tname)) == 0) tcol = cols;
else if (strncmp(ptr, kname, strlen(kname)) == 0) kcol = cols;
cols++;
ptr = strtok(NULL, _DELIMITER_);
}
if (dcol < 0 || tcol < 0 || kcol < 0)
{
COUT << " error in loadTransferFunctions (HAVE_CLASS)! Unable to identify requested columns!" << endl;
parallel.abortForce();
}
data = (double *) malloc(sizeof(double) * cols * class_spectra.ln_k_size);
k = (double *) malloc(sizeof(double) * class_spectra.ln_k_size);
tk_d = (double *) malloc(sizeof(double) * class_spectra.ln_k_size);
tk_t = (double *) malloc(sizeof(double) * class_spectra.ln_k_size);
spectra_output_tk_data(&class_background, &class_perturbs, &class_spectra, class_format, z, cols, data);
for (int i = 0; i < class_spectra.ln_k_size; i++)
{
k[i] = data[i*cols + kcol] * boxsize;
tk_d[i] = data[i*cols + dcol];
tk_t[i] = data[i*cols + tcol] / h;
if (i > 0)
{
if (k[i] < k[i-1])
{
COUT << " error in loadTransferFunctions (HAVE_CLASS)! k-values are not strictly ordered." << endl;
parallel.abortForce();
}
}
}
free(data);
tk_delta = gsl_spline_alloc(gsl_interp_cspline, class_spectra.ln_k_size);
tk_theta = gsl_spline_alloc(gsl_interp_cspline, class_spectra.ln_k_size);
gsl_spline_init(tk_delta, k, tk_d, class_spectra.ln_k_size);
gsl_spline_init(tk_theta, k, tk_t, class_spectra.ln_k_size);
free(k);
free(tk_d);
free(tk_t);
}
#endif
#endif