forked from Alkistis/class_IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.c
executable file
·2713 lines (2246 loc) · 104 KB
/
background.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file background.c Documented background module
*
* * Julien Lesgourgues, 17.04.2011
* * routines related to ncdm written by T. Tram in 2011
*
* Deals with the cosmological background evolution.
* This module has two purposes:
*
* - at the beginning, to initialize the background, i.e. to integrate
* the background equations, and store all background quantities
* as a function of conformal time inside an interpolation table.
*
* - to provide routines which allow other modules to evaluate any
* background quantity for a given value of the conformal time (by
* interpolating within the interpolation table), or to find the
* correspondence between redshift and conformal time.
*
*
* The overall logic in this module is the following:
*
* 1. most background parameters that we will call {A}
* (e.g. rho_gamma, ..) can be expressed as simple analytical
* functions of a few variables that we will call {B} (in simplest
* models, of the scale factor 'a'; in extended cosmologies, of 'a'
* plus e.g. (phi, phidot) for quintessence, or some temperature for
* exotic particles, etc...).
*
* 2. in turn, quantities {B} can be found as a function of conformal
* time by integrating the background equations.
*
* 3. some other quantities that we will call {C} (like e.g. the
* sound horizon or proper time) also require an integration with
* respect to time, that cannot be inferred analytically from
* parameters {B}.
*
* So, we define the following routines:
*
* - background_functions() returns all background
* quantities {A} as a function of quantities {B}.
*
* - background_solve() integrates the quantities {B} and {C} with
* respect to conformal time; this integration requires many calls
* to background_functions().
*
* - the result is stored in the form of a big table in the background
* structure. There is one column for conformal time 'tau'; one or
* more for quantities {B}; then several columns for quantities {A}
* and {C}.
*
* Later in the code, if we know the variables {B} and need some
* quantity {A}, the quickest and most precise way is to call directly
* background_functions() (for instance, in simple models, if we want
* H at a given value of the scale factor). If we know 'tau' and want
* any other quantity, we can call background_at_tau(), which
* interpolates in the table and returns all values. Finally it can be
* useful to get 'tau' for a given redshift 'z': this can be done with
* background_tau_of_z(). So if we are somewhere in the code, knowing
* z and willing to get background quantities, we should call first
* background_tau_of_z() and then background_at_tau().
*
*
* In order to save time, background_at_tau() can be called in three
* modes: short_info, normal_info, long_info (returning only essential
* quantities, or useful quantities, or rarely useful
* quantities). Each line in the interpolation table is a vector whose
* first few elements correspond to the short_info format; a larger
* fraction contribute to the normal format; and the full vector
* corresponds to the long format. The guideline is that short_info
* returns only geometric quantities like a, H, H'; normal format
* returns quantities strictly needed at each step in the integration
* of perturbations; long_info returns quantities needed only
* occasionally.
*
* In summary, the following functions can be called from other modules:
*
* -# background_init() at the beginning
* -# background_at_tau(), background_tau_of_z() at any later time
* -# background_free() at the end, when no more calls to the previous functions are needed
*/
#include "background.h"
/**
* Background quantities at given conformal time tau.
*
* Evaluates all background quantities at a given value of
* conformal time by reading the pre-computed table and interpolating.
*
* @param pba Input: pointer to background structure (containing pre-computed table)
* @param tau Input: value of conformal time
* @param return_format Input: format of output vector (short, normal, long)
* @param intermode Input: interpolation mode (normal or closeby)
* @param last_index Input/Output: index of the previous/current point in the interpolation array (input only for closeby mode, output for both)
* @param pvecback Output: vector (assumed to be already allocated)
* @return the error status
*/
int background_at_tau(
struct background *pba,
double tau,
short return_format,
short intermode,
int * last_index,
double * pvecback /* vector with argument pvecback[index_bg] (must be already allocated with a size compatible with return_format) */
) {
/** Summary: */
/** - define local variables */
/* size of output vector, controlled by input parameter return_format */
int pvecback_size;
/** - check that tau is in the pre-computed range */
class_test(tau < pba->tau_table[0],
pba->error_message,
"out of range: tau=%e < tau_min=%e, you should decrease the precision parameter a_ini_over_a_today_default\n",tau,pba->tau_table[0]);
class_test(tau > pba->tau_table[pba->bt_size-1],
pba->error_message,
"out of range: tau=%e > tau_max=%e\n",tau,pba->tau_table[pba->bt_size-1]);
/** - deduce length of returned vector from format mode */
if (return_format == pba->normal_info) {
pvecback_size=pba->bg_size_normal;
}
else {
if (return_format == pba->short_info) {
pvecback_size=pba->bg_size_short;
}
else {
pvecback_size=pba->bg_size;
}
}
/** - interpolate from pre-computed table with array_interpolate()
or array_interpolate_growing_closeby() (depending on
interpolation mode) */
if (intermode == pba->inter_normal) {
class_call(array_interpolate_spline(
pba->tau_table,
pba->bt_size,
pba->background_table,
pba->d2background_dtau2_table,
pba->bg_size,
tau,
last_index,
pvecback,
pvecback_size,
pba->error_message),
pba->error_message,
pba->error_message);
}
if (intermode == pba->inter_closeby) {
class_call(array_interpolate_spline_growing_closeby(
pba->tau_table,
pba->bt_size,
pba->background_table,
pba->d2background_dtau2_table,
pba->bg_size,
tau,
last_index,
pvecback,
pvecback_size,
pba->error_message),
pba->error_message,
pba->error_message);
}
return _SUCCESS_;
}
/**
* Conformal time at given redshift.
*
* Returns tau(z) by interpolation from pre-computed table.
*
* @param pba Input: pointer to background structure
* @param z Input: redshift
* @param tau Output: conformal time
* @return the error status
*/
int background_tau_of_z(
struct background *pba,
double z,
double * tau
) {
/** Summary: */
/** - define local variables */
/* necessary for calling array_interpolate(), but never used */
int last_index;
/** - check that \f$ z \f$ is in the pre-computed range */
class_test(z < pba->z_table[pba->bt_size-1],
pba->error_message,
"out of range: z=%e < z_min=%e\n",z,pba->z_table[pba->bt_size-1]);
class_test(z > pba->z_table[0],
pba->error_message,
"out of range: a=%e > a_max=%e\n",z,pba->z_table[0]);
/** - interpolate from pre-computed table with array_interpolate() */
class_call(array_interpolate_spline(
pba->z_table,
pba->bt_size,
pba->tau_table,
pba->d2tau_dz2_table,
1,
z,
&last_index,
tau,
1,
pba->error_message),
pba->error_message,
pba->error_message);
return _SUCCESS_;
}
/**
* Background quantities at given \f$ a \f$.
*
* Function evaluating all background quantities which can be computed
* analytically as a function of {B} parameters such as the scale factor 'a'
* (see discussion at the beginning of this file). In extended
* cosmological models, the pvecback_B vector contains other input parameters than
* just 'a', e.g. (phi, phidot) for quintessence, some temperature of
* exotic relics, etc...
*
* @param pba Input: pointer to background structure
* @param pvecback_B Input: vector containing all {B} type quantities (scale factor, ...)
* @param return_format Input: format of output vector
* @param pvecback Output: vector of background quantities (assumed to be already allocated)
* @return the error status
*/
int background_functions(
struct background *pba,
double * pvecback_B, /* Vector containing all {B} quantities. */
short return_format,
double * pvecback /* vector with argument pvecback[index_bg] (must be already allocated with a size compatible with return_format) */
) {
/** Summary: */
/** - define local variables */
/* total density */
double rho_tot;
/* critical density */
double rho_crit;
/* total pressure */
double p_tot;
/* total relativistic density */
double rho_r;
/* total non-relativistic density */
double rho_m;
/* scale factor relative to scale factor today */
double a_rel;
/* background ncdm quantities */
double rho_ncdm,p_ncdm,pseudo_p_ncdm;
/* index for n_ncdm species */
int n_ncdm;
/* fluid's time-dependent equation of state parameter */
double w_fld, dw_over_da, integral_fld;
/* scale factor */
double a;
/* scalar field quantities */
double phi, phi_prime;
double F,Fz,Fzz,Zbg;
/* Since we only know a_prime_over_a after we have rho_tot,
it is not possible to simply sum up p_tot_prime directly.
Instead we sum up dp_dloga = p_prime/a_prime_over_a. The formula is
p_prime = a_prime_over_a * dp_dloga = a_prime_over_a * Sum [ (w_prime/a_prime_over_a -3(1+w)w)rho].
Note: The scalar field contribution must be added in the end, as an exception!*/
double dp_dloga;
/** - initialize local variables */
a = pvecback_B[pba->index_bi_a];
rho_tot = 0.;
p_tot = 0.;
dp_dloga = 0.;
rho_r=0.;
rho_m=0.;
a_rel = a / pba->a_today;
class_test(a_rel <= 0.,
pba->error_message,
"a = %e instead of strictly positive",a_rel);
/** - pass value of \f$ a\f$ to output */
pvecback[pba->index_bg_a] = a;
/** - compute each component's density and pressure */
/* photons */
pvecback[pba->index_bg_rho_g] = pba->Omega0_g * pow(pba->H0,2) / pow(a_rel,4);
rho_tot += pvecback[pba->index_bg_rho_g];
p_tot += (1./3.) * pvecback[pba->index_bg_rho_g];
dp_dloga += -(4./3.) * pvecback[pba->index_bg_rho_g];
rho_r += pvecback[pba->index_bg_rho_g];
/* baryons */
pvecback[pba->index_bg_rho_b] = pba->Omega0_b * pow(pba->H0,2) / pow(a_rel,3);
rho_tot += pvecback[pba->index_bg_rho_b];
p_tot += 0;
rho_m += pvecback[pba->index_bg_rho_b];
/* cdm */
if (pba->has_cdm == _TRUE_) {
pvecback[pba->index_bg_rho_cdm] = pba->Omega0_cdm * pow(pba->H0,2) / pow(a_rel,3);
rho_tot += pvecback[pba->index_bg_rho_cdm];
p_tot += 0.;
rho_m += pvecback[pba->index_bg_rho_cdm];
}
/* dcdm */
if (pba->has_dcdm == _TRUE_) {
/* Pass value of rho_dcdm to output */
pvecback[pba->index_bg_rho_dcdm] = pvecback_B[pba->index_bi_rho_dcdm];
rho_tot += pvecback[pba->index_bg_rho_dcdm];
p_tot += 0.;
rho_m += pvecback[pba->index_bg_rho_dcdm];
}
/* dr */
if (pba->has_dr == _TRUE_) {
/* Pass value of rho_dr to output */
pvecback[pba->index_bg_rho_dr] = pvecback_B[pba->index_bi_rho_dr];
rho_tot += pvecback[pba->index_bg_rho_dr];
p_tot += (1./3.)*pvecback[pba->index_bg_rho_dr];
dp_dloga += -(4./3.) * pvecback[pba->index_bg_rho_dr];
rho_r += pvecback[pba->index_bg_rho_dr];
}
/* Scalar field */
if (pba->has_scf == _TRUE_) {
phi = pvecback_B[pba->index_bi_phi_scf];
phi_prime = pvecback_B[pba->index_bi_phi_prime_scf];
pvecback[pba->index_bg_phi_scf] = phi; // value of the scalar field phi
pvecback[pba->index_bg_phi_prime_scf] = phi_prime; // value of the scalar field phi derivative wrt conformal time
pvecback[pba->index_bg_V_scf] = V_scf(pba,phi); //V_scf(pba,phi); //write here potential as function of phi
pvecback[pba->index_bg_dV_scf] = dV_scf(pba,phi); // dV_scf(pba,phi); //potential' as function of phi
//printf(" phi=%g, dV=%g, dVanal=%g\n",phi,dV_scf(pba,phi),-1.2247*V_scf(pba,phi));
pvecback[pba->index_bg_ddV_scf] = ddV_scf(pba,phi); // ddV_scf(pba,phi); //potential'' as function of phi
Zbg = -phi_prime/a;
F=(pba->scf_veta)*Zbg*Zbg;
Fz=2.*(pba->scf_veta)*Zbg;
Fzz=2*(pba->scf_veta);
pvecback[pba->index_bg_rho_scf] = (((Zbg*Zbg)/2.)+F-Zbg*Fz + V_scf(pba,phi))/3.; // energy of the scalar field. The field units are set automatically by setting the initial conditions
pvecback[pba->index_bg_p_scf] =(((Zbg*Zbg)/2.)-F - V_scf(pba,phi))/3.; // pressure of the scalar field
rho_tot += pvecback[pba->index_bg_rho_scf];
p_tot += pvecback[pba->index_bg_p_scf];
//divide relativistic & nonrelativistic (not very meaningful for oscillatory models)
rho_r += 3.*pvecback[pba->index_bg_p_scf]; //field pressure contributes radiation
rho_m += pvecback[pba->index_bg_rho_scf] - 3.* pvecback[pba->index_bg_p_scf]; //the rest contributes matter
//printf(" a= %e, Omega_scf = %f, \n ",a_rel, pvecback[pba->index_bg_rho_scf]/rho_tot );
}
/* ncdm */
if (pba->has_ncdm == _TRUE_) {
/* Loop over species: */
for(n_ncdm=0; n_ncdm<pba->N_ncdm; n_ncdm++){
/* function returning background ncdm[n_ncdm] quantities (only
those for which non-NULL pointers are passed) */
class_call(background_ncdm_momenta(
pba->q_ncdm_bg[n_ncdm],
pba->w_ncdm_bg[n_ncdm],
pba->q_size_ncdm_bg[n_ncdm],
pba->M_ncdm[n_ncdm],
pba->factor_ncdm[n_ncdm],
1./a_rel-1.,
NULL,
&rho_ncdm,
&p_ncdm,
NULL,
&pseudo_p_ncdm),
pba->error_message,
pba->error_message);
pvecback[pba->index_bg_rho_ncdm1+n_ncdm] = rho_ncdm;
rho_tot += rho_ncdm;
pvecback[pba->index_bg_p_ncdm1+n_ncdm] = p_ncdm;
p_tot += p_ncdm;
pvecback[pba->index_bg_pseudo_p_ncdm1+n_ncdm] = pseudo_p_ncdm;
/** See e.g. Eq. A6 in 1811.00904. */
dp_dloga += (pseudo_p_ncdm - 5*p_ncdm);
/* (3 p_ncdm1) is the "relativistic" contribution to rho_ncdm1 */
rho_r += 3.* p_ncdm;
/* (rho_ncdm1 - 3 p_ncdm1) is the "non-relativistic" contribution
to rho_ncdm1 */
rho_m += rho_ncdm - 3.* p_ncdm;
}
}
/* Lambda */
if (pba->has_lambda == _TRUE_) {
pvecback[pba->index_bg_rho_lambda] = pba->Omega0_lambda * pow(pba->H0,2);
rho_tot += pvecback[pba->index_bg_rho_lambda];
p_tot -= pvecback[pba->index_bg_rho_lambda];
}
/* fluid with w(a) and constant cs2 */
if (pba->has_fld == _TRUE_) {
/* get rho_fld from vector of integrated variables */
pvecback[pba->index_bg_rho_fld] = pvecback_B[pba->index_bi_rho_fld];
/* get w_fld from dedicated function */
class_call(background_w_fld(pba,a,&w_fld,&dw_over_da,&integral_fld), pba->error_message, pba->error_message);
pvecback[pba->index_bg_w_fld] = w_fld;
// Obsolete: at the beginning, we had here the analytic integral solution corresponding to the case w=w0+w1(1-a/a0):
// pvecback[pba->index_bg_rho_fld] = pba->Omega0_fld * pow(pba->H0,2) / pow(a_rel,3.*(1.+pba->w0_fld+pba->wa_fld)) * exp(3.*pba->wa_fld*(a_rel-1.));
// But now everthing is integrated numerically for a given w_fld(a) defined in the function background_w_fld.
rho_tot += pvecback[pba->index_bg_rho_fld];
p_tot += w_fld * pvecback[pba->index_bg_rho_fld];
dp_dloga += (a*dw_over_da-3*(1+w_fld)*w_fld)*pvecback[pba->index_bg_rho_fld];
}
/* relativistic neutrinos (and all relativistic relics) */
if (pba->has_ur == _TRUE_) {
pvecback[pba->index_bg_rho_ur] = pba->Omega0_ur * pow(pba->H0,2) / pow(a_rel,4);
rho_tot += pvecback[pba->index_bg_rho_ur];
p_tot += (1./3.) * pvecback[pba->index_bg_rho_ur];
dp_dloga += -(4./3.) * pvecback[pba->index_bg_rho_ur];
rho_r += pvecback[pba->index_bg_rho_ur];
}
/* interacting dark matter */
if (pba->has_idm_dr == _TRUE_) {
pvecback[pba->index_bg_rho_idm_dr] = pba->Omega0_idm_dr * pow(pba->H0,2) / pow(a_rel,3);
rho_tot += pvecback[pba->index_bg_rho_idm_dr];
p_tot += 0.;
rho_m += pvecback[pba->index_bg_rho_idm_dr];
}
/* interacting dark radiation */
if (pba->has_idr == _TRUE_) {
pvecback[pba->index_bg_rho_idr] = pba->Omega0_idr * pow(pba->H0,2) / pow(a_rel,4);
rho_tot += pvecback[pba->index_bg_rho_idr];
p_tot += (1./3.) * pvecback[pba->index_bg_rho_idr];
rho_r += pvecback[pba->index_bg_rho_idr];
}
/** - compute expansion rate H from Friedmann equation: this is the
only place where the Friedmann equation is assumed. Remember
that densities are all expressed in units of \f$ [3c^2/8\pi G] \f$, ie
\f$ \rho_{class} = [8 \pi G \rho_{physical} / 3 c^2]\f$ */
pvecback[pba->index_bg_H] = sqrt(rho_tot-pba->K/a/a);
/** - compute derivative of H with respect to conformal time */
pvecback[pba->index_bg_H_prime] = - (3./2.) * (rho_tot + p_tot) * a + pba->K/a;
/* Total energy density*/
pvecback[pba->index_bg_rho_tot] = rho_tot;
/* Total pressure */
pvecback[pba->index_bg_p_tot] = p_tot;
/* Derivative of total pressure w.r.t. conformal time */
pvecback[pba->index_bg_p_tot_prime] = a*pvecback[pba->index_bg_H]*dp_dloga;
if (pba->has_scf == _TRUE_){
/** The contribution of scf was not added to dp_dloga, add p_scf_prime here: */
pvecback[pba->index_bg_p_prime_scf] = pvecback[pba->index_bg_phi_prime_scf]*
(-(1.-2.*pba->scf_veta)*pvecback[pba->index_bg_phi_prime_scf]*pvecback[pba->index_bg_H]/a-2./3.*pvecback[pba->index_bg_dV_scf]);
pvecback[pba->index_bg_p_tot_prime] += pvecback[pba->index_bg_p_prime_scf];
}
/** - compute critical density */
rho_crit = rho_tot-pba->K/a/a;
class_test(rho_crit <= 0.,
pba->error_message,
"rho_crit = %e instead of strictly positive",rho_crit);
/** - compute relativistic density to total density ratio */
pvecback[pba->index_bg_Omega_r] = rho_r / rho_crit;
/** - compute other quantities in the exhaustive, redundant format */
if (return_format == pba->long_info) {
/** - store critical density */
pvecback[pba->index_bg_rho_crit] = rho_crit;
/** - compute Omega_m */
pvecback[pba->index_bg_Omega_m] = rho_m / rho_crit;
/* one can put other variables here */
/* */
/* */
}
return _SUCCESS_;
}
/**
* Single place where the fluid equation of state is
* defined. Parameters of the function are passed through the
* background structure. Generalisation to arbitrary functions should
* be simple.
*
* @param pba Input: pointer to background structure
* @param a Input: current value of scale factor
* @param w_fld Output: equation of state parameter w_fld(a)
* @param dw_over_da_fld Output: function dw_fld/da
* @param integral_fld Output: function \f$ \int_{a}^{a_0} da 3(1+w_{fld})/a \f$
* @return the error status
*/
int background_w_fld(
struct background * pba,
double a,
double * w_fld,
double * dw_over_da_fld,
double * integral_fld) {
double Omega_ede = 0.;
double dOmega_ede_over_da = 0.;
double d2Omega_ede_over_da2 = 0.;
double a_eq, Omega_r, Omega_m;
/** - first, define the function w(a) */
switch (pba->fluid_equation_of_state) {
case CLP:
*w_fld = pba->w0_fld + pba->wa_fld * (1. - a / pba->a_today);
break;
case EDE:
// Omega_ede(a) taken from eq. (10) in 1706.00730
Omega_ede = (pba->Omega0_fld - pba->Omega_EDE*(1.-pow(a,-3.*pba->w0_fld)))
/(pba->Omega0_fld+(1.-pba->Omega0_fld)*pow(a,3.*pba->w0_fld))
+ pba->Omega_EDE*(1.-pow(a,-3.*pba->w0_fld));
// d Omega_ede / d a taken analytically from the above
dOmega_ede_over_da = - pba->Omega_EDE* 3.*pba->w0_fld*pow(a,-3.*pba->w0_fld-1.)/(pba->Omega0_fld+(1.-pba->Omega0_fld)*pow(a,3.*pba->w0_fld))
- (pba->Omega0_fld - pba->Omega_EDE*(1.-pow(a,-3.*pba->w0_fld)))*(1.-pba->Omega0_fld)*3.*pba->w0_fld*pow(a,3.*pba->w0_fld-1.)/pow(pba->Omega0_fld+(1.-pba->Omega0_fld)*pow(a,3.*pba->w0_fld),2)
+ pba->Omega_EDE*3.*pba->w0_fld*pow(a,-3.*pba->w0_fld-1.);
// find a_equality (needed because EDE tracks first radiation, then matter)
Omega_r = pba->Omega0_g * (1. + 3.046 * 7./8.*pow(4./11.,4./3.)); // assumes LambdaCDM + eventually massive neutrinos so light that they are relativistic at equality; needs to be generalised later on.
Omega_m = pba->Omega0_b;
if (pba->has_cdm == _TRUE_) Omega_m += pba->Omega0_cdm;
if (pba->has_idm_dr == _TRUE_) Omega_m += pba->Omega0_idm_dr;
if (pba->has_dcdm == _TRUE_)
class_stop(pba->error_message,"Early Dark Energy not compatible with decaying Dark Matter because we omitted to code the calculation of a_eq in that case, but it would not be difficult to add it if necessary, should be a matter of 5 minutes");
a_eq = Omega_r/Omega_m; // assumes a flat universe with a=1 today
// w_ede(a) taken from eq. (11) in 1706.00730
*w_fld = - dOmega_ede_over_da*a/Omega_ede/3./(1.-Omega_ede)+a_eq/3./(a+a_eq);
break;
}
/** - then, give the corresponding analytic derivative dw/da (used
by perturbation equations; we could compute it numerically,
but with a loss of precision; as long as there is a simple
analytic expression of the derivative of the previous
function, let's use it! */
switch (pba->fluid_equation_of_state) {
case CLP:
*dw_over_da_fld = - pba->wa_fld / pba->a_today;
break;
case EDE:
d2Omega_ede_over_da2 = 0.;
*dw_over_da_fld = - d2Omega_ede_over_da2*a/3./(1.-Omega_ede)/Omega_ede
- dOmega_ede_over_da/3./(1.-Omega_ede)/Omega_ede
+ dOmega_ede_over_da*dOmega_ede_over_da*a/3./(1.-Omega_ede)/(1.-Omega_ede)/Omega_ede
+ a_eq/3./(a+a_eq)/(a+a_eq);
break;
}
/** - finally, give the analytic solution of the following integral:
\f$ \int_{a}^{a0} da 3(1+w_{fld})/a \f$. This is used in only
one place, in the initial conditions for the background, and
with a=a_ini. If your w(a) does not lead to a simple analytic
solution of this integral, no worry: instead of writing
something here, the best would then be to leave it equal to
zero, and then in background_initial_conditions() you should
implement a numerical calculation of this integral only for
a=a_ini, using for instance Romberg integration. It should be
fast, simple, and accurate enough. */
switch (pba->fluid_equation_of_state) {
case CLP:
*integral_fld = 3.*((1.+pba->w0_fld+pba->wa_fld)*log(pba->a_today/a) + pba->wa_fld*(a/pba->a_today-1.));
break;
case EDE:
class_stop(pba->error_message,"EDE implementation not finished: to finish it, read the comments in background.c just before this line\n");
break;
}
/** note: of course you can generalise these formulas to anything,
defining new parameters pba->w..._fld. Just remember that so
far, HyRec explicitely assumes that w(a)= w0 + wa (1-a/a0); but
Recfast does not assume anything */
return _SUCCESS_;
}
/**
* Initialize the background structure, and in particular the
* background interpolation table.
*
* @param ppr Input: pointer to precision structure
* @param pba Input/Output: pointer to initialized background structure
* @return the error status
*/
int background_init(
struct precision * ppr,
struct background * pba
) {
/** Summary: */
/** - define local variables */
int n_ncdm;
double rho_ncdm_rel,rho_nu_rel;
double Neff, N_dark;
double w_fld, dw_over_da, integral_fld;
int filenum=0;
/** - in verbose mode, provide some information */
if (pba->background_verbose > 0) {
printf("Running CLASS version %s\n",_VERSION_);
printf("Computing background\n");
/* below we want to inform the user about ncdm species and/or the total N_eff */
if ((pba->N_ncdm > 0) || (pba->Omega0_idr != 0.)) {
/* contribution of ultra-relativistic species _ur to N_eff */
Neff = pba->Omega0_ur/7.*8./pow(4./11.,4./3.)/pba->Omega0_g;
/* contribution of ncdm species to N_eff*/
if (pba->N_ncdm > 0){
/* loop over ncdm species */
for (n_ncdm=0;n_ncdm<pba->N_ncdm; n_ncdm++) {
/* inform if p-s-d read in files */
if (pba->got_files[n_ncdm] == _TRUE_) {
printf(" -> ncdm species i=%d read from file %s\n",n_ncdm+1,pba->ncdm_psd_files+filenum*_ARGUMENT_LENGTH_MAX_);
filenum++;
}
/* call this function to get rho_ncdm */
background_ncdm_momenta(pba->q_ncdm_bg[n_ncdm],
pba->w_ncdm_bg[n_ncdm],
pba->q_size_ncdm_bg[n_ncdm],
0.,
pba->factor_ncdm[n_ncdm],
0.,
NULL,
&rho_ncdm_rel,
NULL,
NULL,
NULL);
/* inform user of the contribution of each species to
radiation density (in relativistic limit): should be
between 1.01 and 1.02 for each active neutrino species;
evaluated as rho_ncdm/rho_nu_rel where rho_nu_rel is the
density of one neutrino in the instantaneous decoupling
limit, i.e. assuming T_nu=(4/11)^1/3 T_gamma (this comes
from the definition of N_eff) */
rho_nu_rel = 56.0/45.0*pow(_PI_,6)*pow(4.0/11.0,4.0/3.0)*_G_/pow(_h_P_,3)/pow(_c_,7)*
pow(_Mpc_over_m_,2)*pow(pba->T_cmb*_k_B_,4);
printf(" -> ncdm species i=%d sampled with %d (resp. %d) points for purpose of background (resp. perturbation) integration. In the relativistic limit it gives Delta N_eff = %g\n",
n_ncdm+1,
pba->q_size_ncdm_bg[n_ncdm],
pba->q_size_ncdm[n_ncdm],
rho_ncdm_rel/rho_nu_rel);
Neff += rho_ncdm_rel/rho_nu_rel;
}
}
/* contribution of interacting dark radiation _idr to N_eff */
if (pba->Omega0_idr != 0.) {
N_dark = pba->Omega0_idr/7.*8./pow(4./11.,4./3.)/pba->Omega0_g;
Neff += N_dark;
printf(" -> dark radiation Delta Neff %e\n",N_dark);
}
printf(" -> total N_eff = %g (sumed over ultra-relativistic species, ncdm and dark radiation)\n",Neff);
}
}
/** - if shooting failed during input, catch the error here */
class_test_except(pba->shooting_failed == _TRUE_,
pba->error_message,
background_free_input(pba),
"Shooting failed, try optimising input_get_guess(). Error message:\n\n%s",
pba->shooting_error);
/** - assign values to all indices in vectors of background quantities with background_indices()*/
class_call(background_indices(pba),
pba->error_message,
pba->error_message);
/* fluid equation of state */
if (pba->has_fld == _TRUE_) {
class_call(background_w_fld(pba,0.,&w_fld,&dw_over_da,&integral_fld), pba->error_message, pba->error_message);
class_test(w_fld >= 1./3.,
pba->error_message,
"Your choice for w(a--->0)=%g is suspicious, since it is bigger than -1/3 there cannot be radiation domination at early times\n",
w_fld);
}
/* in verbose mode, inform the user about the value of the ncdm
masses in eV and about the ratio [m/omega_ncdm] in eV (the usual
93 point something)*/
if ((pba->background_verbose > 0) && (pba->has_ncdm == _TRUE_)) {
for (n_ncdm=0; n_ncdm < pba->N_ncdm; n_ncdm++) {
printf(" -> non-cold dark matter species with i=%d has m_i = %e eV (so m_i / omega_i =%e eV)\n",
n_ncdm+1,
pba->m_ncdm_in_eV[n_ncdm],
pba->m_ncdm_in_eV[n_ncdm]*pba->deg_ncdm[n_ncdm]/pba->Omega0_ncdm[n_ncdm]/pba->h/pba->h);
}
}
/* check other quantities which would lead to segmentation fault if zero */
class_test(pba->a_today <= 0,
pba->error_message,
"input a_today = %e instead of strictly positive",pba->a_today);
class_test(_Gyr_over_Mpc_ <= 0,
pba->error_message,
"_Gyr_over_Mpc = %e instead of strictly positive",_Gyr_over_Mpc_);
/** - this function integrates the background over time, allocates
and fills the background table */
class_call(background_solve(ppr,pba),
pba->error_message,
pba->error_message);
/** - this function finds and stores a few derived parameters at radiation-matter equality */
class_call(background_find_equality(ppr,pba),
pba->error_message,
pba->error_message);
class_call(background_output_budget(pba),
pba->error_message,
pba->error_message);
return _SUCCESS_;
}
/**
* Free all memory space allocated by background_init().
*
*
* @param pba Input: pointer to background structure (to be freed)
* @return the error status
*/
int background_free(
struct background *pba
) {
class_call(background_free_noinput(pba),
pba->error_message,
pba->error_message);
class_call(background_free_input(pba),
pba->error_message,
pba->error_message);
return _SUCCESS_;
}
/**
* Free only the memory space NOT allocated through input_read_parameters()
*
* @param pba Input: pointer to background structure (to be freed)
* @return the error status
*/
int background_free_noinput(
struct background *pba
) {
free(pba->tau_table);
free(pba->z_table);
free(pba->d2tau_dz2_table);
free(pba->background_table);
free(pba->d2background_dtau2_table);
return _SUCCESS_;
}
/**
* Free pointers inside background structure which were
* allocated in input_read_parameters()
*
* @param pba Input: pointer to background structure
* @return the error status
*/
int background_free_input(
struct background *pba
) {
int k;
if (pba->Omega0_ncdm_tot != 0.){
for(k=0; k<pba->N_ncdm; k++){
free(pba->q_ncdm[k]);
free(pba->w_ncdm[k]);
free(pba->q_ncdm_bg[k]);
free(pba->w_ncdm_bg[k]);
free(pba->dlnf0_dlnq_ncdm[k]);
}
free(pba->ncdm_quadrature_strategy);
free(pba->ncdm_input_q_size);
free(pba->ncdm_qmax);
free(pba->q_ncdm);
free(pba->w_ncdm);
free(pba->q_ncdm_bg);
free(pba->w_ncdm_bg);
free(pba->dlnf0_dlnq_ncdm);
free(pba->q_size_ncdm);
free(pba->q_size_ncdm_bg);
free(pba->M_ncdm);
free(pba->T_ncdm);
free(pba->ksi_ncdm);
free(pba->deg_ncdm);
free(pba->Omega0_ncdm);
free(pba->m_ncdm_in_eV);
free(pba->factor_ncdm);
if(pba->got_files!=NULL)
free(pba->got_files);
if(pba->ncdm_psd_files!=NULL)
free(pba->ncdm_psd_files);
if(pba->ncdm_psd_parameters!=NULL)
free(pba->ncdm_psd_parameters);
}
if (pba->Omega0_scf != 0.){
if (pba->scf_parameters != NULL)
free(pba->scf_parameters);
}
return _SUCCESS_;
}
/**
* Assign value to each relevant index in vectors of background quantities.
*
* @param pba Input: pointer to background structure
* @return the error status
*/
int background_indices(
struct background *pba
) {
/** Summary: */
/** - define local variables */
/* a running index for the vector of background quantities */
int index_bg;
/* a running index for the vector of background quantities to be integrated */
int index_bi;
/** - initialize all flags: which species are present? */
pba->has_cdm = _FALSE_;
pba->has_ncdm = _FALSE_;
pba->has_dcdm = _FALSE_;
pba->has_dr = _FALSE_;
pba->has_scf = _FALSE_;
pba->has_lambda = _FALSE_;
pba->has_fld = _FALSE_;
pba->has_ur = _FALSE_;
pba->has_idr = _FALSE_;
pba->has_idm_dr = _FALSE_;
pba->has_curvature = _FALSE_;
if (pba->Omega0_cdm != 0.)
pba->has_cdm = _TRUE_;
if (pba->Omega0_ncdm_tot != 0.)
pba->has_ncdm = _TRUE_;
if (pba->Omega0_dcdmdr != 0.){
pba->has_dcdm = _TRUE_;
if (pba->Gamma_dcdm != 0.)
pba->has_dr = _TRUE_;
}
if (pba->Omega0_scf != 0.)
pba->has_scf = _TRUE_;
if (pba->Omega0_lambda != 0.)
pba->has_lambda = _TRUE_;
if (pba->Omega0_fld != 0.)
pba->has_fld = _TRUE_;
if (pba->Omega0_ur != 0.)
pba->has_ur = _TRUE_;
if (pba->Omega0_idr != 0.)
pba->has_idr = _TRUE_;
if (pba->Omega0_idm_dr != 0.)
pba->has_idm_dr = _TRUE_;
if (pba->sgnK != 0)
pba->has_curvature = _TRUE_;
/** - initialize all indices */
index_bg=0;
/* index for scale factor */
class_define_index(pba->index_bg_a,_TRUE_,index_bg,1);
/* - indices for H and its conformal-time-derivative */
class_define_index(pba->index_bg_H,_TRUE_,index_bg,1);
class_define_index(pba->index_bg_H_prime,_TRUE_,index_bg,1);
/* - end of indices in the short vector of background values */
pba->bg_size_short = index_bg;
/* - index for rho_g (photon density) */
class_define_index(pba->index_bg_rho_g,_TRUE_,index_bg,1);
/* - index for rho_b (baryon density) */
class_define_index(pba->index_bg_rho_b,_TRUE_,index_bg,1);
/* - index for rho_cdm */
class_define_index(pba->index_bg_rho_cdm,pba->has_cdm,index_bg,1);
/* - indices for ncdm. We only define the indices for ncdm1
(density, pressure, pseudo-pressure), the other ncdm indices
are contiguous */
class_define_index(pba->index_bg_rho_ncdm1,pba->has_ncdm,index_bg,pba->N_ncdm);
class_define_index(pba->index_bg_p_ncdm1,pba->has_ncdm,index_bg,pba->N_ncdm);
class_define_index(pba->index_bg_pseudo_p_ncdm1,pba->has_ncdm,index_bg,pba->N_ncdm);
/* - index for dcdm */
class_define_index(pba->index_bg_rho_dcdm,pba->has_dcdm,index_bg,1);
/* - index for dr */
class_define_index(pba->index_bg_rho_dr,pba->has_dr,index_bg,1);
/* - indices for scalar field */
class_define_index(pba->index_bg_phi_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_phi_prime_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_V_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_dV_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_ddV_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_rho_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_p_scf,pba->has_scf,index_bg,1);
class_define_index(pba->index_bg_p_prime_scf,pba->has_scf,index_bg,1);
/* - index for Lambda */
class_define_index(pba->index_bg_rho_lambda,pba->has_lambda,index_bg,1);
/* - index for fluid */
class_define_index(pba->index_bg_rho_fld,pba->has_fld,index_bg,1);
class_define_index(pba->index_bg_w_fld,pba->has_fld,index_bg,1);
/* - index for ultra-relativistic neutrinos/species */
class_define_index(pba->index_bg_rho_ur,pba->has_ur,index_bg,1);
/* - index for total density */
class_define_index(pba->index_bg_rho_tot,_TRUE_,index_bg,1);
/* - index for total pressure */
class_define_index(pba->index_bg_p_tot,_TRUE_,index_bg,1);
/* - index for derivative of total pressure */
class_define_index(pba->index_bg_p_tot_prime,_TRUE_,index_bg,1);
/* - index for Omega_r (relativistic density fraction) */
class_define_index(pba->index_bg_Omega_r,_TRUE_,index_bg,1);
/* - index interacting for dark radiation */
class_define_index(pba->index_bg_rho_idr,pba->has_idr,index_bg,1);