-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps with GM changes.c
5778 lines (4927 loc) · 161 KB
/
ps with GM changes.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
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include "ps.h"
#include "lib.h"
extern cospar *cosmopar;
extern zdistr_par *bg_zdistrpar;
double s1glob,s2glob;
extern double thetaglob;
extern double theta_min;
extern double theta_max;
extern double xi_plus_zero;
double thetaglob;
double theta_min=4.8e-6;//3e-5;
double theta_max=0.25; //0.1;
double xi_plus_zero;
/* global variable thetaglob is used here */
/*************************************************************************************/
/* Functions for shear correlation functions */
/*************************************************************************************/
double int_for_xi_plus(double x, void *args)
{
//printf("%g\n",x*P_2(x/thetaglob)*ps_bessj0(x));
return x*P_2(x/thetaglob)*ps_bessj0(x);
}
/* global variable thetaglob is used here */
double int_for_xi_minus(double x, void *args)
{
return x*P_2(x/thetaglob)*ps_bessj(4, x);
}
/* for theta=0 */
double int_for_xi_0(double s, void *args)
{
return s*P_2(s);
}
/* integral for xi_plus */
double int_over_p2_j0(double theta)
{
double f, g, res, lower, upper;
double RMAX, step;
RMAX = 20.*theta*s_max;
if (RMAX <= twopi)
RMAX = twopi+epsilon2;
if (theta < epsilon2)
{
f = int_GSL_integrate_qag(int_for_xi_0,NULL,0.0,1e8,NULL,2048)/twopi;
//f = ps_qromb(int_for_xi_0, 0.0, 1.e8)/twopi;
return f;
}
thetaglob = theta;
step = pi;
lower = 2.35723;
upper = step;
f = int_GSL_integrate_qag(int_for_xi_plus,NULL,0.0,lower,NULL,2048);
//f = ps_qromb1(int_for_xi_plus, 0.0, lower);
while (1)
{
g = int_GSL_integrate_qag(int_for_xi_plus,NULL,lower,upper,NULL,2048);
//g=ps_qromb1(int_for_xi_plus, lower, upper);
if (fabs(g/f) < 1.e-6)
break;
f += g;
lower = upper;
upper += step;
}
res = f/(theta*theta)/twopi;
return res;
}
/* integral for xi_minus */
double int_over_p2_j4(double theta)
{
double RMAX = 20.*theta*1.e6;
double lower, upper, f, step, g, res;
thetaglob = theta;
if (theta<0.004) RMAX = 5.*0.004*1.e7;
if (RMAX<twopi) { RMAX = twopi+epsilon2; }
step = pi;
lower = 2.35723;
upper = step;
f = int_GSL_integrate_qag(int_for_xi_minus,NULL,0.0,lower,NULL,2048);
//f = ps_qromb1(int_for_xi_minus, 0.0, lower);
while (1)
{
g = int_GSL_integrate_qag(int_for_xi_minus,NULL,lower,upper,NULL,2048);
//g = ps_qromb1(int_for_xi_minus, lower, upper);
if (fabs(g/f) < 1.e-6) {
/* printf("- %e %e\n", theta, upper); */
break;
}
f += g;
lower = upper;
upper += step;
}
res = f/(twopi*(theta*theta));
return res;
}
double xi(int pm, double theta)
{
static double OMEGA_M = -42.;
static double OMEGA_V = -42.;
static double W0 = -42.;
static double WA = -42.;
static double SIGMA_8 = -42.;
static double GAMMA = -42.;
static double N_SPEC = -42.;
static double BETA_P = -42.;
static double Z0 = -42.;
static double NONLINEAR = -42;
static int PSNLTYPE=-42;
static int EH=-42;
static double table_p[N_theta];
static double table_m[N_theta];
static double dtheta = .0, logthetamin = 0., logthetamax = 0.;
double res, thetalog, t;
int i;
FILE *F;
char *xi_name;
if (OMEGA_M != cosmopar->omm || OMEGA_V != cosmopar->omv || W0 != cosmopar->w0 || WA != cosmopar->wa || SIGMA_8 != cosmopar->sigma8 || N_SPEC != cosmopar->n || GAMMA != cosmopar->Gamma || BETA_P != bg_zdistrpar->beta || Z0 != bg_zdistrpar->z0 || NONLINEAR != cosmopar->nonlinear || PSNLTYPE != cosmopar->psnltype || EH != cosmopar->transfer_EH)
{
OMEGA_M = cosmopar->omm;
OMEGA_V = cosmopar->omv;
W0 = cosmopar->w0;
WA = cosmopar->wa;
SIGMA_8 = cosmopar->sigma8;
N_SPEC = cosmopar->n;
GAMMA = cosmopar->Gamma;
BETA_P = bg_zdistrpar->beta;
Z0 = bg_zdistrpar->z0;
NONLINEAR = cosmopar->nonlinear;
PSNLTYPE = cosmopar->psnltype;
EH = cosmopar->transfer_EH;
logthetamin = log(theta_min);
logthetamax = log(theta_max);
dtheta = (logthetamax - logthetamin)/(N_theta - 1.);
//printf("Func Xi: dtheta=%f\n",dtheta);
xi_name = (char*)malloc(200*sizeof(char));
sprintf(xi_name, "th/xi/xi-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f", cosmopar->omm, cosmopar->omv, cosmopar->Gamma, cosmopar->sigma8, cosmopar->n, bg_zdistrpar->beta, bg_zdistrpar->z0);
/* read xi from file */
if ((F=fopen(xi_name, "r")))
{
fscanf(F, "0.0 %lf\n", &xi_plus_zero);
thetalog = logthetamin;
i = 0;
while (fscanf(F, "%lf %lf %lf\n", &t, table_p+i, table_m+i) != EOF)
{
if (fabs(t - exp(thetalog)) > epsilon3)
{
printf("xi: t:%g, tlog: %g\n",t,exp(thetalog));
printf("Inconsistency with xi file %s!\n",xi_name); exit(1);
}
thetalog += dtheta;
i++;
}
fileclose(F);
}
else
{
/* calculate xi and write to disk */
F = fileopen(xi_name, "w");
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
t = exp(thetalog);
table_p[i] = int_over_p2_j0(t);
table_m[i] = int_over_p2_j4(t);
printf("Th. cf: %1.1f%% done.\r",1.0*i/(1.0*N_theta)*100.); fflush(stdout);
}
xi_plus_zero = int_over_p2_j0(0.0);
printf("Th. cf: 100.0%% done.\n");
fprintf(F, "0.0 %5.20e\n", xi_plus_zero);
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
fprintf(F, "%5.10e %5.20e %5.20e\n", exp(thetalog), table_p[i], table_m[i]);
}
fileclose(F);
free((void*)xi_name);
}
}
/* interpolate xi */
if (theta>=theta_min)
{
if (theta>theta_max)
{
printf("theta=%e larger than theta_max=%e in xi\n", theta, theta_max);
error("theta too large in xi");
}
thetalog = log(theta);
t = (thetalog-logthetamin)/dtheta;
i = (int)(floor(t));
if (pm == +1)
{
res = (t-i)*(table_p[i+1] - table_p[i]) + table_p[i];
}
else
{
res = (t-i)*(table_m[i+1] - table_m[i]) + table_m[i];
}
return res;
}
else if (theta < epsilon2)
{
if (pm == +1) { return xi_plus_zero; }
else { return 0.0; }
}
else
{
t = theta/theta_min;
if (pm == +1) { return t*(table_p[0]-xi_plus_zero) + xi_plus_zero; }
else { return t*(table_m[0]); }
}
}
double xi2(int pm, double theta,char *path)
{
static double OMEGA_M = -42.;
static double OMEGA_V = -42.;
static double W0 = -42.;
static double WA = -42.;
static double SIGMA_8 = -42.;
static double GAMMA = -42.;
static double N_SPEC = -42.;
static double BETA_P = -42.;
static double Z0 = -42.;
static double NONLINEAR = -42;
static int PSNLTYPE=-42;
static int EH=-42;
static double table_p[N_theta];
static double table_m[N_theta];
static double dtheta = .0, logthetamin = 0., logthetamax = 0.;
double res, thetalog, t;
int i;
FILE *F;
char *xi_name;
if (OMEGA_M != cosmopar->omm || OMEGA_V != cosmopar->omv || W0 != cosmopar->w0 || WA != cosmopar->wa || SIGMA_8 != cosmopar->sigma8 || N_SPEC != cosmopar->n || GAMMA != cosmopar->Gamma || BETA_P != bg_zdistrpar->beta || Z0 != bg_zdistrpar->z0 || NONLINEAR != cosmopar->nonlinear || PSNLTYPE != cosmopar->psnltype || EH != cosmopar->transfer_EH)
{
OMEGA_M = cosmopar->omm;
OMEGA_V = cosmopar->omv;
W0 = cosmopar->w0;
WA = cosmopar->wa;
SIGMA_8 = cosmopar->sigma8;
N_SPEC = cosmopar->n;
GAMMA = cosmopar->Gamma;
BETA_P = bg_zdistrpar->beta;
Z0 = bg_zdistrpar->z0;
NONLINEAR = cosmopar->nonlinear;
PSNLTYPE = cosmopar->psnltype;
EH = cosmopar->transfer_EH;
logthetamin = log(theta_min);
logthetamax = log(theta_max);
dtheta = (logthetamax - logthetamin)/(N_theta - 1.);
//printf("Func Xi: dtheta=%f\n",dtheta);
xi_name = (char*)malloc(200*sizeof(char));
sprintf(xi_name, "%s/xi-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f",path,cosmopar->omm, cosmopar->omv, cosmopar->Gamma, cosmopar->sigma8, cosmopar->n, bg_zdistrpar->beta, bg_zdistrpar->z0);
/* read xi from file */
if ((F=fopen(xi_name, "r")))
{
fscanf(F, "0.0 %lf\n", &xi_plus_zero);
thetalog = logthetamin;
i = 0;
while (fscanf(F, "%lf %lf %lf\n", &t, table_p+i, table_m+i) != EOF)
{
if (fabs(t - exp(thetalog)) > epsilon3)
{
printf("xi: t:%g, tlog: %g\n",t,exp(thetalog));
printf("Inconsistency with xi file %s!\n",xi_name); exit(1);
}
thetalog += dtheta;
i++;
}
fileclose(F);
}
else
{
/* calculate xi and write to disk */
F = fileopen(xi_name, "w");
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
t = exp(thetalog);
table_p[i] = int_over_p2_j0(t);
table_m[i] = int_over_p2_j4(t);
printf("Th. cf: %1.1f%% done.\r",1.0*i/(1.0*N_theta)*100.); fflush(stdout);
}
xi_plus_zero = int_over_p2_j0(0.0);
printf("Th. cf: 100.0%% done.\n");
fprintf(F, "0.0 %5.20e\n", xi_plus_zero);
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
fprintf(F, "%5.10e %5.20e %5.20e\n", exp(thetalog), table_p[i], table_m[i]);
}
fileclose(F);
free((void*)xi_name);
}
}
/* interpolate xi */
if (theta>=theta_min)
{
if (theta>theta_max)
{
printf("theta=%e larger than theta_max=%e in xi\n", theta, theta_max);
error("theta too large in xi");
}
thetalog = log(theta);
t = (thetalog-logthetamin)/dtheta;
i = (int)(floor(t));
if (pm == +1)
{
res = (t-i)*(table_p[i+1] - table_p[i]) + table_p[i];
}
else
{
res = (t-i)*(table_m[i+1] - table_m[i]) + table_m[i];
}
return res;
}
else if (theta < epsilon2)
{
if (pm == +1) { return xi_plus_zero; }
else { return 0.0; }
}
else
{
t = theta/theta_min;
if (pm == +1) { return t*(table_p[0]-xi_plus_zero) + xi_plus_zero; }
else { return t*(table_m[0]); }
}
}
/*************************************************************************************/
/* Tomography: Functions for shear cross-correlation functions */
/*************************************************************************************/
double int_for_xi_plusx(double x, void *args)
{
int b1,b2;
double *arg = (double*)args;
b1=(int)arg[0];
b2=(int)arg[1];
return x*P_2x(x/thetaglob,b1,b2)*ps_bessj0(x);
}
double int_for_xi_minusx(double x, void *args)
{
int b1,b2;
double *arg = (double*)args;
b1=(int)arg[0];
b2=(int)arg[1];
return x*P_2x(x/thetaglob,b1,b2)*ps_bessj(4, x);
}
/* for theta=0 */
double int_for_xi_0x(double s, void *args)
{
double *arg = (double*)args;
int b1,b2;
b1=(int)arg[0];
b2=(int)arg[1];
return s*P_2x(s,b1,b2);
}
/* integral for xi_plus */
double int_over_p2_j0x(double theta, void *args)
{
//printf("int_over_p2_j0\n");
double f, g, res, lower, upper;
double RMAX, step;
double *arg = (double*)args;
RMAX = 20.*theta*s_max;
if (RMAX <= twopi) { RMAX = twopi+epsilon2; }
if (theta < epsilon2)
{
f = int_GSL_integrate_qag(int_for_xi_0x,NULL,0.0,1e8,NULL,2048)/twopi;
//f = int_qrombn(int_for_xi_0x, 0.0, 1.e8,arg,argc)/twopi;
return f;
}
thetaglob = theta;
step = pi;
lower = 2.35723;
upper = step;
f = int_GSL_integrate_qag(int_for_xi_plusx,NULL,0.0,lower,NULL,2048);//int_qrombn(int_for_xi_plusx, 0.0, lower,arg,argc);
while (1)
{
g = int_GSL_integrate_qag(int_for_xi_plusx,NULL,lower,upper,NULL,2048);//int_qrombn(int_for_xi_plusx, lower, upper,arg,argc);
if (fabs(g/f) < 1.e-6)
break;
f += g;
lower = upper;
upper += step;
}
res = f/(theta*theta)/twopi;
return res;
}
/* integral for xi_minus */
double int_over_p2_j4x(double theta,void *args)
{
double RMAX = 20.*theta*1.e6;
double lower, upper, f, step, g, res;
double *arg = (double*)args;
thetaglob = theta;
if (theta<0.004) RMAX = 5.*0.004*1.e7;
if (RMAX<twopi) { RMAX = twopi+epsilon2; }
step = pi;
lower = 2.35723;
upper = step;
f = int_GSL_integrate_qag(int_for_xi_minusx,NULL,0.0,lower,NULL,2048);//int_qrombn(int_for_xi_minusx, 0.0, lower,arg,argc);
while (1) {
g = int_GSL_integrate_qag(int_for_xi_minusx,NULL,lower,upper,NULL,2048);//int_qrombn(int_for_xi_minusx, lower, upper,arg,argc);
if (fabs(g/f) < 1.e-6)
break;
f += g;
lower = upper;
upper += step;
}
res = f/(twopi*(theta*theta));
return res;
}
double xix(int pm, double theta, int bin1, int bin2)
{
static double OMEGA_M = -42.;
static double OMEGA_V = -42.;
static double W0 = -42.;
static double WA = -42.;
static double SIGMA_8 = -42.;
static double GAMMA = -42.;
static double N_SPEC = -42.;
static double BETA_P = -42.;
static double Z0 = -42.;
static double NONLINEAR = -42;
static int PSNLTYPE=-42;
static int EH=-42;
static int NBIN1=-42;
static int NBIN2=-42;
static double table_p[N_theta];
static double table_m[N_theta];
static double dtheta = .0, logthetamin = 0., logthetamax = 0.;
double arg[2];
double res, thetalog, t;
int i;
FILE *F;
char *xi_name;
if (OMEGA_M != cosmopar->omm || OMEGA_V != cosmopar->omv || W0 != cosmopar->w0 || WA != cosmopar->wa || SIGMA_8 != cosmopar->sigma8 || N_SPEC != cosmopar->n || GAMMA != cosmopar->Gamma || BETA_P != bg_zdistrpar->beta || Z0 != bg_zdistrpar->z0 || NONLINEAR != cosmopar->nonlinear || NBIN1 != bin1 || NBIN2 != bin2 || PSNLTYPE != cosmopar->psnltype || EH != cosmopar->transfer_EH)
{
OMEGA_M = cosmopar->omm;
OMEGA_V = cosmopar->omv;
W0 = cosmopar->w0;
WA = cosmopar->wa;
SIGMA_8 = cosmopar->sigma8;
N_SPEC = cosmopar->n;
GAMMA = cosmopar->Gamma;
BETA_P = bg_zdistrpar->beta;
Z0 = bg_zdistrpar->z0;
NONLINEAR = cosmopar->nonlinear;
NBIN1 = bin1;
NBIN2 = bin2;
PSNLTYPE = cosmopar->psnltype;
EH = cosmopar->transfer_EH;
logthetamin = log(theta_min);
logthetamax = log(theta_max);
dtheta = (logthetamax - logthetamin)/(N_theta - 1.);
arg[0]=bin1;
arg[1]=bin2;
//printf("Func Xi: dtheta=%f\n",dtheta);
xi_name = (char*)malloc(300*sizeof(char));
sprintf(xi_name, "th/xi/tomo/xi_%d-%d_-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f", bin1, bin2, cosmopar->omm, cosmopar->omv, cosmopar->Gamma, cosmopar->sigma8, cosmopar->n, bg_zdistrpar->beta, bg_zdistrpar->z0);
/* read xi from file */
if ((F=fopen(xi_name, "r")))
{
fscanf(F, "0.0 %lf\n", &xi_plus_zero);
thetalog = logthetamin;
i = 0;
while (fscanf(F, "%lf %lf %lf\n", &t, table_p+i, table_m+i) != EOF)
{
if (fabs(t - exp(thetalog)) > epsilon3)
{
printf("xi: t:%g, tlog: %g\n",t,exp(thetalog));
printf("Inconsistency with xi file %s!\n",xi_name); exit(1);
}
thetalog += dtheta;
i++;
}
fileclose(F);
}
else
{
/* calculate xi and write to disk */
F = fileopen(xi_name, "w");
thetalog = logthetamin;
//printf("172\n");
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
//printf("i=%d\n",i);
t = exp(thetalog);
table_p[i] = int_over_p2_j0x(t,arg);
table_m[i] = int_over_p2_j4x(t,arg);
printf("Th. cf: %1.1f%% done.\r",1.0*i/(1.0*N_theta)*100.); fflush(stdout);
//printf("theta: %g, p: %g, m: %g\n",t,table_p[i],table_m[i]);
}
xi_plus_zero = int_over_p2_j0x(0.0,arg);
printf("Th. cf: 100.0%% done.\n");
fprintf(F, "0.0 %5.20e\n", xi_plus_zero);
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
fprintf(F, "%5.10e %5.20e %5.20e\n", exp(thetalog), table_p[i], table_m[i]);
}
fileclose(F);
free((void*)xi_name);
}
}
/* interpolate xi */
if (theta>=theta_min)
{
if (theta>theta_max)
{
printf("theta=%e larger than theta_max=%e in xi\n", theta, theta_max);
error("theta too large in xi");
}
thetalog = log(theta);
t = (thetalog-logthetamin)/dtheta;
i = (int)(floor(t));
if (pm == +1)
{
res = (t-i)*(table_p[i+1] - table_p[i]) + table_p[i];
}
else
{
res = (t-i)*(table_m[i+1] - table_m[i]) + table_m[i];
}
return res;
}
else if (theta < epsilon2)
{
if (pm == +1) { return xi_plus_zero; }
else { return 0.0; }
}
else
{
t = theta/theta_min;
if (pm == +1) { return t*(table_p[0]-xi_plus_zero) + xi_plus_zero; }
else { return t*(table_m[0]); }
}
}
double xix2(int pm, double theta, int bin1, int bin2,char *path)
{
static double OMEGA_M = -42.;
static double OMEGA_V = -42.;
static double W0 = -42.;
static double WA = -42.;
static double SIGMA_8 = -42.;
static double GAMMA = -42.;
static double N_SPEC = -42.;
static double BETA_P = -42.;
static double Z0 = -42.;
static double NONLINEAR = -42;
static int NBIN1=-42;
static int NBIN2=-42;
static int PSNLTYPE=-42;
static int EH=-42;
static double table_p[N_theta];
static double table_m[N_theta];
static double dtheta = .0, logthetamin = 0., logthetamax = 0.;
double arg[2];
double res, thetalog, t;
int i;
FILE *F;
char *xi_name;
if (OMEGA_M != cosmopar->omm || OMEGA_V != cosmopar->omv || W0 != cosmopar->w0 || WA != cosmopar->wa || SIGMA_8 != cosmopar->sigma8 || N_SPEC != cosmopar->n || GAMMA != cosmopar->Gamma || BETA_P != bg_zdistrpar->beta || Z0 != bg_zdistrpar->z0 || NONLINEAR != cosmopar->nonlinear || NBIN1 != bin1 || NBIN2 != bin2 || PSNLTYPE != cosmopar->psnltype || EH != cosmopar->transfer_EH)
{
OMEGA_M = cosmopar->omm;
OMEGA_V = cosmopar->omv;
W0 = cosmopar->w0;
WA = cosmopar->wa;
SIGMA_8 = cosmopar->sigma8;
N_SPEC = cosmopar->n;
GAMMA = cosmopar->Gamma;
BETA_P = bg_zdistrpar->beta;
Z0 = bg_zdistrpar->z0;
NONLINEAR = cosmopar->nonlinear;
NBIN1 = bin1;
NBIN2 = bin2;
PSNLTYPE = cosmopar->psnltype;
EH = cosmopar->transfer_EH;
logthetamin = log(theta_min);
logthetamax = log(theta_max);
dtheta = (logthetamax - logthetamin)/(N_theta - 1.);
arg[0]=bin1;
arg[1]=bin2;
//printf("Func Xi: dtheta=%f\n",dtheta);
xi_name = (char*)malloc(300*sizeof(char));
mkdir(path,0777);
sprintf(xi_name, "%s/xi_%d-%d_-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f-%.6f", path, bin1, bin2, cosmopar->omm, cosmopar->omv, cosmopar->Gamma, cosmopar->sigma8, cosmopar->n, bg_zdistrpar->beta, bg_zdistrpar->z0);
/* read xi from file */
if ((F=fopen(xi_name, "r")))
{
fscanf(F, "0.0 %lf\n", &xi_plus_zero);
thetalog = logthetamin;
i = 0;
while (fscanf(F, "%lf %lf %lf\n", &t, table_p+i, table_m+i) != EOF)
{
if (fabs(t - exp(thetalog)) > epsilon3)
{
printf("xi: t:%g, tlog: %g\n",t,exp(thetalog));
printf("Inconsistency with xi file %s!\n",xi_name); exit(1);
}
thetalog += dtheta;
i++;
}
fileclose(F);
}
else
{
/* calculate xi and write to disk */
F = fileopen(xi_name, "w");
thetalog = logthetamin;
//printf("172\n");
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
//printf("i=%d\n",i);
t = exp(thetalog);
table_p[i] = int_over_p2_j0x(t,arg);
table_m[i] = int_over_p2_j4x(t,arg);
printf("Th. cf: %1.1f%% done.\r",1.0*i/(1.0*N_theta)*100.); fflush(stdout);
//printf("theta: %g, p: %g, m: %g\n",t,table_p[i],table_m[i]);
}
xi_plus_zero = int_over_p2_j0x(0.0,arg);
printf("Th. cf: 100.0%% done.\n");
fprintf(F, "0.0 %5.20e\n", xi_plus_zero);
thetalog = logthetamin;
for (i=0; i<N_theta; i++, thetalog+=dtheta)
{
fprintf(F, "%5.10e %5.20e %5.20e\n", exp(thetalog), table_p[i], table_m[i]);
}
fileclose(F);
free((void*)xi_name);
}
}
/* interpolate xi */
if (theta>=theta_min)
{
if (theta>theta_max)
{
printf("theta=%e larger than theta_max=%e in xi\n", theta, theta_max);
error("theta too large in xi");
}
thetalog = log(theta);
t = (thetalog-logthetamin)/dtheta;
i = (int)(floor(t));
if (pm == +1)
{
res = (t-i)*(table_p[i+1] - table_p[i]) + table_p[i];
}
else
{
res = (t-i)*(table_m[i+1] - table_m[i]) + table_m[i];
}
return res;
}
else if (theta < epsilon2)
{
if (pm == +1) { return xi_plus_zero; }
else { return 0.0; }
}
else
{
t = theta/theta_min;
if (pm == +1) { return t*(table_p[0]-xi_plus_zero) + xi_plus_zero; }
else { return t*(table_m[0]); }
}
}
/* ============================================================ *
* Global variables needed in integrand functions int_for_p_2 *
* and int_for_g *
* ============================================================ */
double sglob, aglob;
/* CPT 9 */
double da_dtau(double a)
{
double res, det;
if (a < epsilon3) error("Division by 0 in da_dtau");
det = 1. + cosmopar->omm*(1./a - 1.) + OMV_MACRO(a)*(a*a - 1.);
res = sqrt(det);
return res;
}
double da_dtau_m3(double a, void *args)
{
double res;
if (a < epsilon3)
return 0.;
res = da_dtau(a);
if (res < epsilon3)
error("Division by 0 in da_dtau_m3");
return 1./(res*res*res);
}
double D_plus(double a)
{
static double OMEGA_M = -42.;
static double OMEGA_V = -42.;
static double W0 = -42.;
static double WA = -42.;
static double da = 0.;
static double table[N_a];
double delta, aa, delta0;
int i;
if (fabs(cosmopar->omm-OMEGA_M)>epsilon3 || fabs(cosmopar->omv-OMEGA_V)>epsilon3 || W0 != cosmopar->w0 || WA != cosmopar->wa) {
delta0 = int_GSL_integrate_qag(da_dtau_m3,NULL,0.,1.,NULL,2048);;
da = (1. - a_min)/(N_a-1.);
aa = a_min;
for (i = 0; i<N_a; i++, aa += da)
{
delta = int_GSL_integrate_qag(da_dtau_m3,NULL,0.,aa,NULL,2048);
table[i] = 1./aa * da_dtau(aa) * delta / delta0;
}
OMEGA_M = cosmopar->omm;
OMEGA_V = cosmopar->omv;
W0 = cosmopar->w0;
WA = cosmopar->wa;
}
return interpol(table, N_a, a_min, 1., da, a, .0, .0);
}
/* BBKS G3, see also PD2 51, 52. Returns k^n * T^2(k) */
double Tsqr(double k) //Transferfunktn^2 // k in Hubble length-1
{
double q, f1, f2;
const double coverh=ckms/100.;
q = k/(coverh*cosmopar->Gamma); // k in Hubble length-1, k/3000 in hMpc-1
f1 = log(1 + 2.34*q)/(2.34*q);
f2 = 1 + q*(3.89 + q*(259.21 + q*(162.771336 + q*2027.16958081)));
/* polynomial: 1 + 3.89q + (16.1q)^2 + (5.46q)^3 + (6.71q)^4 */
//assert(finite(f1)&&finite(f2));
return pow(k, cosmopar->n)*(f1*f1)/sqrt(f2);
}
/*Eisenstein &Hu no-wiggle version*/
/*
double Tsqr_EH(double k) // from Martin White, approximation for s as in EH98, (26)
{
double q, theta, ommh2, a, s, gamma, L0, C0;
double tmp;
double omegam, ombh2, hubble;
// other input parameters
hubble = cosmopar->h;
omegam = cosmopar->omm;
ombh2 = cosmopar->omb* cosmopar->h *cosmopar->h ;
if(cosmopar->omb == 0)
ombh2 = 0.04 * cosmopar->h *cosmopar->h ;
k *= 1./3000.;//(3.085678e24 / UnitLength_in_cm); // convert to h/Mpc
theta = 2.728 / 2.7;
ommh2 = omegam * hubble * hubble;
s = 44.5 * log(9.83 / ommh2) / sqrt(1. + 10. * exp(0.75 * log(ombh2))) * hubble;
a = 1. - 0.328 * log(431. * ommh2) * ombh2 / ommh2
+ 0.380 * log(22.3 * ommh2) * (ombh2 / ommh2) * (ombh2 / ommh2);
gamma = a + (1. - a) / (1. + exp(4 * log(0.43 * k * s)));
gamma *= omegam * hubble;
q = k * theta * theta / gamma;
L0 = log(2. * exp(1.) + 1.8 * q);
C0 = 14.2 + 731. / (1. + 62.5 * q);
tmp = L0 / (L0 + C0 * q * q);
return (tmp*tmp)*pow(k,cosmopar->n);
}
*/
double Tsqr_EH(double k) // as in EH98
{
double q, theta, thetasq, ommh2, a, s, gamma, L0, C0;
double tmp;
double omegam, ombh2, hubble;
const double coverh=ckms/100.;
hubble = cosmopar->h;
omegam = cosmopar->omm;
ombh2 = cosmopar->omb* cosmopar->h *cosmopar->h ;
if(cosmopar->omb == 0)
ombh2 = 0.04 * cosmopar->h *cosmopar->h ;
k *= 1./coverh;//(3.085678e24 / UnitLength_in_cm); // convert to h/Mpc
theta = 2.728 / 2.7;
thetasq=theta*theta;
ommh2 = omegam * hubble * hubble;
// compute s
double z_eq,z_d,R_eq,R_d,k_eq,b_1,b_2;
k_eq=7.46e-2*ommh2/thetasq;
b_1=0.313*pow(ommh2,-0.419)*(1.+0.607*pow(ommh2,0.674));
b_2=0.238*pow(ommh2,0.223);
z_eq=2.5e4*ommh2/(thetasq*thetasq);
z_d=1291.*pow(ommh2,0.251)*(1.+b_1*pow(ombh2,b_2))/(1.+0.659*pow(ommh2,0.828));
R_eq=3.15e4*ombh2/(z_eq*thetasq*thetasq);
R_d=3.15e4*ombh2/(z_d*thetasq*thetasq);
s = 2./(3.*k_eq)*sqrt(6./R_eq)*log((sqrt(1.+R_d)+sqrt(R_d+R_eq))/(1.+sqrt(R_eq)));
a = 1. - 0.328 * log(431. * ommh2) * ombh2 / ommh2
+ 0.380 * log(22.3 * ommh2) * (ombh2 / ommh2) * (ombh2 / ommh2);
gamma = a + (1. - a) / (1. + exp(4 * log(0.43 * k*hubble * s))); //this k is 1/Mpc
gamma *= omegam * hubble;
q = k * thetasq / gamma;
L0 = log(2. * exp(1.) + 1.8 * q);
C0 = 14.2 + 731. / (1. + 62.5 * q);
tmp = L0 / (L0 + C0 * q * q);
return (tmp*tmp)*pow(k,cosmopar->n);
}
// Eh transfer function fit with wiggles
double T_tilde(double k,double a, double b)
{
double L0,C0,q,theta;
theta = 2.728 / 2.7;
q=k*theta*theta/(cosmopar->omm*cosmopar->h);
L0=log(exp(1.)+1.8*b*q);
C0=14.2/a+386./(1.+69.9*pow(q,1.08));
return(L0/(L0+C0*q*q));
}
double Gfunc_EH98(double x)
{
double sx=sqrt(1.+x);
return(x*((2.+3.*x)*log((sx+1.)/(sx-1.))-6.*sx));
}
double Tsqr_EH_wiggle(double k) // as in EH98, eqs. 11-15,17-24
{
double theta, thetasq, ombh2, ommh2, fb, fc, T_cdm,T_baryon,T_total;
const double coverh=ckms/100.;
if(cosmopar->omb == 0) {
printf("Error: set Omega_b>0 for EH transfer function with wiggles.\n");
exit(-1);
}
ombh2 = cosmopar->omb* cosmopar->h *cosmopar->h;
ommh2 = cosmopar->omm* cosmopar->h *cosmopar->h;
k *= 1./coverh; // convert to h/Mpc
theta = 2.728 / 2.7;
thetasq=theta*theta;
fb=cosmopar->omb/cosmopar->omm;
fc=(cosmopar->omm-cosmopar->omb)/cosmopar->omm;
// compute sound horizon s
double z_eq,z_d,R_eq,R_d,k_eq,b_1,b_2,s;
k_eq=7.46e-2*ommh2/thetasq/cosmopar->h; // divide by h to get units [h/Mpc]
b_1=0.313*pow(ommh2,-0.419)*(1.+0.607*pow(ommh2,0.674));
b_2=0.238*pow(ommh2,0.223);
z_eq=2.5e4*ommh2/(thetasq*thetasq);
z_d=1291.*pow(ommh2,0.251)*(1.+b_1*pow(ombh2,b_2))/(1.+0.659*pow(ommh2,0.828));
R_eq=3.15e4*ombh2/(z_eq*thetasq*thetasq);
R_d=3.15e4*ombh2/(z_d*thetasq*thetasq);
s = 2./(3.*k_eq)*sqrt(6./R_eq)*log((sqrt(1.+R_d)+sqrt(R_d+R_eq))/(1.+sqrt(R_eq))); // in units because of k_eq [Mpc/h]
// CDM TF parameters
double a1,a2,alpha_c,b1,b2,beta_c,f;
a1=pow(46.9*ommh2,0.670)*(1.+pow(32.1*ommh2,-0.532));
a2=pow(12.0*ommh2,0.424)*(1.+pow(45.0*ommh2,-0.582));
alpha_c=pow(a1,(-1.)*fb)*pow(a2,(-1.)*pow(fb,3.));
b1=0.944/(1.+pow(458.*ommh2,-0.708));
b2=pow(0.395*ommh2,-0.0266);
beta_c=1./(1.+b1*(pow(fc,b2)-1.));
f=1./(1.+pow(k*s/5.4,4.));
T_cdm=f*T_tilde(k,1.,beta_c)+(1.-f)*T_tilde(k,alpha_c,beta_c);
// baryon TF parameters
double alpha_b,beta_b,beta_node,s_tilde,k_silk;
alpha_b=2.07*k_eq*s*pow(1.+R_d,-0.75)*Gfunc_EH98((1.+z_eq)/(1.+z_d));
beta_b=0.5+fb+(3.-2.*fb)*sqrt(pow(17.2*ommh2,2.)+1.);
beta_node=8.41*pow(ommh2,0.435);
s_tilde=s/pow(1.+pow(beta_node/(k*s),3.),1./3.);
k_silk=1.6*pow(ombh2,0.52)*pow(ommh2,0.73)*(1.+pow(10.4*ommh2,-0.95))/cosmopar->h; // divide by h to get units [h/Mpc]
T_baryon=sin(k*s_tilde)/(k*s_tilde)*(T_tilde(k,1.,1.)/(1.+pow(k*s/5.2,2.))+alpha_b/(1.+pow(beta_b/(k*s),3.))*exp((-1.)*pow(k/k_silk,1.4)));
T_total=fb*T_baryon+fc*T_cdm;
return(T_total*T_total)*pow(k,cosmopar->n);
}
void LineFit(double x1, double x2, double y1, double y2, double *m, double *b)
{
*m = (y2-y1)/(x2-x1);
*b = y1-*m*x1;
return;
}
double Tsqr_tabulated(double k)
{
static int firststart=1,nbin=0;
static double *kk,*T, lm, lb, um, ub;
double kint,Tint;
int i,j;
const double coverh=ckms/100.;
k *= 1./coverh; /* convert to h/Mpc */
if(firststart)
{
printf(" Using tabulateted tranfer function\n");
kk = calloc(1,sizeof(double));
T = calloc(1,sizeof(double));
FILE *fp = fopen(cosmopar->tffile,"r");
if(!fp)
{
printf(" Error: Couldn't open %s!\n",cosmopar->tffile);
exit(1);
}