-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgas_model.h
2042 lines (1750 loc) · 74.4 KB
/
gas_model.h
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
// class for the gas model
// code originally by Suman Bhattacharya, modified by Samuel Flender
#ifndef _GAS_MODEL_
#define _GAS_MODEL_
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_sf.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_multimin.h>
#include <gsl/gsl_dht.h>
#include <gsl/gsl_interp.h>
#include <gsl/gsl_spline.h>
#include "xray.h"
struct Shaw_model_param{
float conc_norm = 1.0, conc_mass_norm = 1.0;
//static float conc_norm, conc_mass_norm;
float delta_rel, delta_rel_n, delta_rel_zslope;
float ad_index;
float eps_fb, eps_dm;
float fs_0, fs_alpha;
int pturbrad = 2;
//static int pturbrad;
bool verbose = false;
//static bool verbose;
float overden_id = -1.0;
//static float overden_id;
int relation = 3;
//static int relation;
float rcutoff = 2.0;
//static float rcutoff;
};
/*
float Shaw_model_param::conc_norm = 1.0;
float Shaw_model_param::conc_mass_norm = 1.0;
int Shaw_model_param::pturbrad = 2;
bool Shaw_model_param::verbose = false;
float Shaw_model_param::overden_id = -1.0;
int Shaw_model_param::relation = 3;
float Shaw_model_param::rcutoff = 2.0;
*/
double sx_func (double x, void * params);
double ss_func (double x, void * params);
double fx_func (double x, void * params);
double ftx_func (double x, void * params);
double tx_func (double x, void * params);
double tx_func_p (double x, void * params);
double ttx_func (double x, void * params);
double gasmod_apply_bc(const gsl_vector * x, void *p);
double yproj_func(double x, void * params);
double yint_func(double x, void * p);
double yint_func_sam(double x, void * p);
double arnaud_func(double x, void * p);
double proj_arnaud_func(double x, void * p);
double yfft_func(double x, void * p);
double kappa_integrant(double x, void* p);
double mgas500_func(double x, void * p);
double mgas500_func_mod(double x, void * p);
double mgas500_func_mod_clumped(double x, void * p);
double arnaud_func_k(double x, void * p);
double yfft_func_k(double x, void * p);
double proj_KS02_func(double x, void * p);
double gasproj_func(double x, void * p);
double gasproj_func_mod(double x, void * p);
double NFWproj_func(double x, void * p);
using namespace std;
struct my_func_params { float a; float b; float c; double d; int e;float f;};
class gas_model {
friend double sx_func (double x, void * params);
friend double ss_func (double x, void * params);
friend double fx_func (double x, void * params);
friend double ftx_func (double x, void * params);
friend double tx_func (double x, void * params);
friend double tx_func_p (double x, void * params);
friend double ttx_func (double x, void * params);
friend double gasmod_apply_bc(const gsl_vector * x, void *p);
friend double yproj_func(double x, void * params);
friend double yint_func(double x, void * p);
friend double yint_func_sam(double x, void * p);
friend double yfft_func(double x, void * p);
friend double kappa_integrant(double x, void* p);
friend double arnaud_func(double x, void * p);
friend double proj_arnaud_func(double x, void * p);
friend double mgas500_func(double x, void * p);
friend double mgas500_func_mod(double x, void * p);
friend double mgas500_func_mod_clumped(double x, void * p);
friend double yfft_func_k(double x, void * p);
friend double gasproj_func(double x, void * p);
friend double gasproj_func_mod(double x, void * p);
friend double NFWproj_func(double x, void * p);
protected:
float delta_rel, delta_rel_n, n, eps, eps_dm, fs_0, fs_alpha, f_s, Mpiv, chi_turb, delta_rel_zslope;
int pturbrad;
float C, ri, rhoi, mass, radius, vcmax, mgas, Ytot, pressurebound, R500toRvir;
double xs;
double final_beta, final_Cf, p0, rho0, T0; // need to define these
double PI, m_sun, G, mpc, mu_e, mmw, m_p, clight, eV, sigma_T, me_csq, m_e, q;
double Aprime, Bprime;
float *x, *k, Tau_d, Tau_b, bulge_frac;
double *ysz, *fft_ysz, *ell;
double *rhogas, *rr, *Tsz, *Ksz, *clumpf;
int nrads, nell;
double clump0, alpha_clump1, alpha_clump2, x_clump;
public:
gas_model(float inp1, float inp2, float inp3, double Ap, double Bp, float inp5, int inp4, float inp9) { // SF: don't know what this does
Mpiv = 3.0e14; //in Msol
set_constants();
delta_rel = inp1;
n = inp2;
C = inp3;
pturbrad = inp4;
if (pturbrad==1) chi_turb = (n-1.0)/(-1.0*(n+1.0));
else chi_turb = 0.0;
Aprime = Ap;
Bprime = Bp;
f_s = inp5;
delta_rel_n = inp9; //0.8;
pressurebound = 1.0;
}
gas_model(float inp1, float inp2, float inp3, float inp4, float inp5, float inp6, int inp7, float inp8, float inp9) {
delta_rel = inp1;
delta_rel_zslope = inp8;
n = inp2;
eps = inp3;
eps_dm = inp4;
fs_0 = inp5;
fs_alpha = inp6;
pturbrad = inp7;
Mpiv = 3.0e14; // in Msol
if (pturbrad==1) chi_turb = (n-1.0)/(-1.0*(n+1.0));
else chi_turb = 0.0;
set_constants();
// stellar evolution parameters (c.f. Nagamine et al. (2006)
pressurebound = 1.0;
delta_rel_n = inp9;//0.8;
bulge_frac = 0.9;
Tau_d = 4.5;//4.5; // in Gyr
Tau_b = 1.5;//1.5; // in Gyr
}
gas_model(Shaw_model_param params){
delta_rel = params.delta_rel;
delta_rel_zslope = params.delta_rel_zslope;
n = params.ad_index;
eps = params.eps_fb;
eps_dm = params.eps_dm;
fs_0 = params.fs_0;
fs_alpha = params.fs_alpha;
pturbrad = params.pturbrad;
Mpiv = 3.0e14; // in Msol
if (pturbrad==1) chi_turb = (n-1.0)/(-1.0*(n+1.0));
else chi_turb = 0.0;
set_constants();
// stellar evolution parameters (c.f. Nagamine et al. (2006)
pressurebound = 1.0;
delta_rel_n = params.delta_rel_n;//0.8
bulge_frac = 0.9;
Tau_d = 4.5;// in Gyr
Tau_b = 1.5;// in Gyr
}
void set_constants() {
PI = 4.*atan(1.);
m_sun = 1.98892e30; //kg
clight = 3.0e5; // in km/s
mpc = 3.0857e22; // in m
G = 6.67e-11*m_sun/pow(mpc,3); // in mpc^3/Msun/s^2
//mu_e = 1.143; // SF: mean molecular weight per electron
mu_e = 1.136; // X=0.76 assumed
//mmw = 0.59; // mean molecular weight
mmw = 0.58824; // X=0.76 assumed
m_p = 1.6726e-27;// mass proton, kg
eV = 1.602e-19; // 1eV in J
sigma_T = 6.652e-25/(1.0e4); // now in m^2.
m_e = 9.11e-31; // mass electron, kg
q = 1.60217646e-19;// joules
me_csq = m_e*clight*clight*1.0e6/(1000.0*q); // in KeV
}
/* SF: origin of mu_e and mmw:
for the tSZ we have DeltaT/T = sigma_T/me_csq int P_e dl
now we need to convert P_e ~ n_e to P_gas ~ n_gas.
n_gas = rho_gas/mu, where mu = 4/(3+5X_H) = 0.59 is the mean atomic weight in a fully ionized gas (Suman calls it mmw)
Now,
n_e = zeta * rho_gas, with zeta = (X_H/m_H + 2*Y_He/m_He).
The factor of 2 comes from the fact that Helium is doubly ionized!
Putting everything together, we have
n_e = zeta * mu * n_gas and thus
P_e = zeta * mu * P_gas.
With Y_He=0.2477 we have zeta=0.87 (in atomic units) and mu=0.59 (in atomic units)
Suman calls mu = mmw and zeta = 1/mu_e.
Then the tSZ normalization is
zeta*mu = mmw/mu_e = 0.5133
For the kSZ the normalization is different (integrated density instead of integrated pressure)
DeltaT/T = sigma_T/c * int n_e v_los dl
now n_e = zeta * rho_gas, as above. Then we have simply
DeltaT/T = sigma_T/c * v_los * zeta * int dl rho_gas
*/
void evolve_pturb_norm(float z, float outer_radius) { //compute alpha(z)
float fmax, evo_power, evo_converge;
if (delta_rel == 0.0) {
delta_rel = 0.0;
}
else if (delta_rel_zslope<=0.0) {
delta_rel *= pow(1.0 + z, delta_rel_zslope);
}
else {
// This is the old power-law evolution
evo_power = pow(1.0 + z, delta_rel_zslope);
// This is the new version that asymptotes to a maximum value, preventing thermal pressure from going negative
fmax = 1.0 / (delta_rel * pow(outer_radius*2.0, delta_rel_n)); // factor of two converts rvir-> r500
//fmax = 1.0 / (delta_rel * pow(4.0, delta_rel_n)); // factor of two converts rvir-> r500
// ---!!!
// outer_radius HAS to be =2 in order to match the equations in Shaw et al (2010)
// ---!!!
evo_converge = (fmax - 1.0)*tanh(delta_rel_zslope*z) + 1.0;
delta_rel *= min(evo_power, evo_converge); //v2 of model
//delta_rel *= evo_power; //v1 of model
}
//cout << "delta_rel at z = " << delta_rel << endl;
}
void set_nfw_params(float bmass, float bradius, float conc, float brhoi, float r500) { // set the NFW parameters
mass = bmass; // Mvir [Msol]
radius = bradius; // Rvir [Mpc]
C = conc; // cvir_Mpc
rhoi = brhoi; // NFW density at NFW scale radius [Msol/Mpc^3]
ri = radius/C; // NFW scale radius [Mpc]
//cout<< mass<<" "<<ri<<endl;
ri = ri*mpc/1000.0; //in km (for later units)
// SO after calling this, ri is always in units km!
vcmax = sqrt(4.0*PI*G*rhoi*ri*ri*Gmax()); //% in km/s
//findxs();// now can calculation radius within which stellar mass is contained
R500toRvir= r500/radius;
}
void set_mgas_init(float baryon_frac_univ) {
mgas = (baryon_frac_univ)*mass/(1.0+f_s);
}
void set_fs(float bfs) {
f_s = bfs;
}
float get_fs() {
return f_s;
}
void set_Mpiv(float bMpiv) {
Mpiv = bMpiv;
}
void calc_fs(float M500, float baryon_frac_univ, float cosm_t0, float cosm_tz) { // compute the star fraction
//---note M500 must be in Msol
f_s = min(fs_0 * pow(M500/Mpiv,-1.0*fs_alpha), 0.8*baryon_frac_univ); //
f_s = f_s / (baryon_frac_univ - f_s); // f_s is now the star formation efficiency
//---uncomment this line for z-evolution:
//f_s = f_s*calc_fstarz(cosm_t0, cosm_tz);
}
void set_stellar_evo_params(float inp1, float inp2, float inp3) {
// reset stellar evolution parameters as in Nagamine et al. 2006
Tau_b = inp1;
Tau_d = inp2;
bulge_frac = inp3;
}
float calc_fstarz(float cosm_t0, float cosm_tz) {
// calculates fraction of stars formed at z = 0 that have formed by redshift z
// Assumes Nagamine et al 06 evolution of disk and bulge populations by default
// Use the Nagamine values for Tau_d, Tau_b and bulge_frac;
float chi_b, chi_d, fb, fd, fstarz;
chi_b = (1.0 - (cosm_t0/Tau_b + 1.0)*exp(-1.0*cosm_t0/Tau_b));
chi_d = (1.0 - (cosm_t0/Tau_d + 1.0)*exp(-1.0*cosm_t0/Tau_d));
fb = bulge_frac/chi_b*(1.0 - (cosm_tz/Tau_b + 1.0)*exp(-1.0*cosm_tz/Tau_b));
fd = (1.0-bulge_frac)/chi_d*(1.0 - (cosm_tz/Tau_d + 1.0)*exp(-1.0*cosm_tz/Tau_d));
fstarz = fb + fd;
//cout<<cosm_t0<<" "<<cosm_tz<<" "<<chi_b<<" "<<chi_d<<" "<<fb<<" "<<fd<<endl;
return fstarz;
}
// now put solvep0rho0 functions in here
double H(double x) { // eqn 6b
return (1/((1+x)*g(x)))*(-1*log(1+x) + (x*(1+x/2))/(1+x));
}
double g(double x) { // eqn 2b
return log(1.0+x) - x/(1.0+x);
}
double findxs() { // solve for x_s (sec 3.1)
// start here!
vector<float> x(2000), gg(2000);
int i;
float mingg;
for (i=0;i<2000;i++) {
x[i] = 0.0 + float(i)*(2.0*C/2000.0);
gg[i] = fabs(g(x[i]) - g(C)*f_s/(1+f_s));
}
mingg = *min_element(gg.begin(), gg.end());
i = 0;
if (mingg>2e-3) cout << "Convergence xs error " << mingg << endl;
while (gg[i]!=mingg) {i++;}
xs = x[i];
return xs;
}
double f(double x) { // eqn 5
if (x<=C) return log(1+x)/x - (1/(1+C));
else if (x>C) return (C/x)*((log(1+C)/C) - (1/(1+C)));
else return -1;
}
double Gmax() { // eqn 3b
float xmax = 2.163; // check this number!
return g(xmax)/xmax;
}
double delta_s() { // eqn 15
return S_C(C) / (S_C(C) + H(C)*g(C)/pow(C,3.0));
}
double S_C(double x) { // eqn 11
double SC;
SC = pow(PI,2)/2.0 - log(x)/2.0 - 1.0/(2.0*x) - 1.0/(2.0*pow(1.0+x,2)) - 3.0/(1+x);
SC += (0.5 + 1.0/(2.0*pow(x,2)) - 2.0/x - 1.0/(1.0+x))*log(1.0+x);
SC += (3.0/2.0)*log(1.0+x)*log(1.0+x);
SC += 3.0*gsl_sf_dilog(-1.0*x); // gsl dilog function
//SC = SC*g(x)*g(x);// this is the correction made to Ostriker et al 05.
// (Mar 10) Don't think it should be here.
return (double)SC;
}
void test_dilog() {
cout << gsl_sf_dilog(-3.0) << endl;
}
double S_cx(double x) { // % eqn 7b
gsl_integration_workspace * w = gsl_integration_workspace_alloc (2000);
double result, error, Sx, alpha = 0.0;
gsl_function F;
F.function = &sx_func;
if (x==0) x = 1e-7; //% diverges at x = 0
gsl_integration_qags (&F, x, C, 0, 1e-7, 2000, w, &result, &error);
Sx = S_C(C) - result;
gsl_integration_workspace_free (w);
return (double)Sx;
}
double K(double x) { //% eqn 16
double Kx = (1.0/3.0)*H(x)*(1./(Gmax()*(1.0-delta_s())));
return Kx;
}
double K_s() { // % eqn 21
gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
double resulta, resultb, error, Ks, tempC = C;
gsl_function F,FF;
F.function = &ss_func;
FF.function = &fx_func;
F.params = &tempC;
FF.params = &tempC;
gsl_integration_qags (&F, 0.0e-4, xs, 0, 1e-7, 10000, w, &resulta, &error);
gsl_integration_qags (&FF, 0.0e-4, xs, 0, 1e-7, 10000, w, &resultb, &error);
Ks = (1.0/g(C))*(resulta - (2.0/3.0)*resultb);
gsl_integration_workspace_free (w);
return Ks;
}
double theta(double x, double beta) { // % eqn 26b
double th;
if (pturbrad==1) {
th = (-1.0*beta*j(x)/(n+1.0) + 1.0 + chi_turb*delta_rel)/2.0;
th = th + 0.5*sqrt(pow(1.0 + chi_turb*delta_rel - beta*j(x)/(n+1.0),2) - 4.0*chi_turb*delta_rel);
}
else if (pturbrad==2) th = (double)(1.0 - (beta*j(x)/(1.0+n)));
else th = (double)(1.0 - (beta*j(x)/((1.0+n)*(1.0+delta_rel))));
return (double)fabs(th);
}
double theta_mod(double x, double beta, float x_break, float npoly_mod) {
// ---
// modified theta (broken power-law model)
// x_break is here in units of the NFW scale radius
// ---
double th;
if (pturbrad!=2){cout<<"ERROR! -- pturbrad should be 2 for computing theta_mod!"; return -1;}
if (x>=x_break){
th = (double)(1.0 - (beta*j(x)/(1.0+n)));
}
else if (x<x_break){
th = (double)(1.0 - (beta*j(x)/(1.0+npoly_mod))) * (1.0 - (beta*j(x_break)/(1.0+n)))/(1.0 - (beta*j(x_break)/(1.0+npoly_mod)));
}
return (double)fabs(th);
}
double j(double x) { //% eqn 25b
double jj;
if (x==0.0) jj = 0.0;
else if (x<=C) jj = 1.0 - log(1.0+x)/x;
else jj = 1.0 - 1.0/(1.0+C) - (log(1.0+C) - C/(1.0+C))/x;
return jj;
}
double I2(double Cf, double beta) {// % eqn 28a
gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
double result, error;
struct my_func_params params = { delta_rel, n, C, beta, pturbrad };
gsl_function F;
F.function = &ftx_func;
F.params = ¶ms;
gsl_integration_qags (&F, 0.0, Cf, 0, 1e-5, 10000, w, &result, &error);
gsl_integration_workspace_free (w);
return result;
}
double I2spline(double Cf, double beta) {// % eqn 27
int nxbins = 1000, i;
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nxbins);
double *xx, *ftx, result;
xx = new double [nxbins];
ftx = new double [nxbins];
// first need to make an array of values
if (Cf<0) cout << "Cf error! " << Cf << endl;
for (i=0;i<nxbins;i++) {
xx[i] = (double)i * Cf / ((double)(nxbins-1));
ftx[i] = f(xx[i])*pow(theta(xx[i], beta),n)*pow(xx[i],2);
}
gsl_spline_init (spline, xx, ftx, nxbins);
result = gsl_spline_eval_integ (spline, xx[0], xx[nxbins-1], acc);
gsl_spline_free (spline);
gsl_interp_accel_free (acc);
delete xx;
delete ftx;
return result;
}
double I3(double Cf, double beta) {// % eqn 28b
gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
double result, error;
struct my_func_params params = { delta_rel, n, C, beta, pturbrad };
gsl_function F;
F.function = &tx_func;
F.params = ¶ms;
gsl_integration_qags (&F, 0.0, Cf, 0, 1e-5, 10000, w, &result, &error);
//cout << "I3: " << result << endl;
gsl_integration_workspace_free (w);
return result;
}
double I3spline(double Cf, double beta) {// % eqn 27 {
int nxbins = 100, i;
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nxbins);
double *xx, *tx, result;
xx = new double [nxbins];
tx = new double [nxbins];
// first need to make an array of values
if (Cf<0) cout << "Cf error! " << Cf << endl;
for (i=0;i<nxbins;i++) {
xx[i] = (double)i * Cf / ((double)(nxbins-1));
tx[i] = pow(theta(xx[i], beta),n+1.0)*pow(xx[i],2);
}
gsl_spline_init (spline, xx, tx, nxbins);
result = gsl_spline_eval_integ (spline, xx[0], xx[nxbins-1], acc);
gsl_spline_free (spline);
gsl_interp_accel_free (acc);
delete xx;
delete tx;
return result;
}
double I3p(double Cf, double beta) {// % eqn 28b
gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
double result, error;
struct my_func_params params = { delta_rel, n, C, beta, pturbrad };
gsl_function F;
if (pturbrad==1) F.function = &tx_func_p;
else F.function = &tx_func;
F.params = ¶ms;
gsl_integration_qags (&F, 0.0, Cf, 0, 1e-5, 10000, w, &result, &error);
//cout << "I3: " << result << endl;
gsl_integration_workspace_free (w);
if (pturbrad==0) result = result*delta_rel*2.0; // check factor of 2!
else if (pturbrad==2) result = 0.0;
return result;
}
double I3p_spline(double Cf, double beta) {// % eqn 27 {
// DO NOT USE THIS FOR NOW
int nxbins = 100, i;
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nxbins);
double *xx, *tx, result;
xx = new double [nxbins];
tx = new double [nxbins];
// first need to make an array of values
if (Cf<0) cout << "Cf error! " << Cf << endl;
for (i=0;i<nxbins;i++) {
xx[i] = (double)i * Cf / ((double)(nxbins-1));
if (pturbrad==1) tx[i] = delta_rel*pow(theta(xx[i],beta),n-1.0)*pow(xx[i],2);
else tx[i] = pow(theta(xx[i], beta),n+1.0)*pow(xx[i],2);
}
gsl_spline_init (spline, xx, tx, nxbins);
result = gsl_spline_eval_integ (spline, xx[0], xx[nxbins-1], acc);
gsl_spline_free (spline);
gsl_interp_accel_free (acc);
delete xx;
delete tx;
return result;
}
double L(double Cf, double beta) {// % eqn 27
gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
double result, error, Sx;
struct my_func_params params = {delta_rel, n, C, beta, pturbrad };
gsl_function F;
F.function = &ttx_func;
F.params = ¶ms;
gsl_integration_qags (&F, 0.0, Cf, 0, 1e-5, 10000, w, &result, &error);
//cout << "L: " << result << endl;
gsl_integration_workspace_free (w);
return result;
}
double Lspline(double Cf, double beta) {// % eqn 27 {
int nxbins = 100, i;
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nxbins);
double *xx, *ttl, result;
xx = new double [nxbins];
ttl = new double [nxbins];
// first need to make an array of values
if (Cf<0) cout << "Cf error! " << Cf << endl;
for (i=0;i<nxbins;i++) {
xx[i] = (double)i * Cf / ((double)(nxbins-1));
ttl[i] = pow(theta(xx[i], beta), n)*pow(xx[i],2);
}
gsl_spline_init (spline, xx, ttl, nxbins);
result = gsl_spline_eval_integ (spline, xx[0], xx[nxbins-1], acc);
gsl_spline_free (spline);
gsl_interp_accel_free (acc);
delete xx;
delete ttl;
return result;
}
double Lvar(double Cf, double beta) {
// allows different outer pressure boundaries
float gfact, Cfp, Lc, Lp;
gfact = g(pressurebound*C) / g(C);
if (gfact<1.0) cout << "gfact error" << endl;
Cfp = Cf;
Lc = Lspline(Cf,beta);
Lp = Lc;
// do this in three steps to speed it up
while (Lp < gfact*Lc) {
Cfp *= 1.2;
Lp = Lspline(Cfp,beta);
}
Cfp = Cfp / 1.2;
while (Lp < gfact*Lc) {
Cfp *= 1.1;
Lp = Lspline(Cfp,beta);
}
Cfp = Cfp / 1.1;
while (Lp < gfact*Lc) {
Cfp *= 1.02;
Lp = Lspline(Cfp,beta);
}
Cfp *= 1.01/1.02; // settle on half way in between
return Cfp;
}
double Edm(float aniso_beta) {
//% Calculation of T + W for dark matter energy transfer (see Bode et al. 09)
// integrate the kinetic + potential energy
//w0 = -1*vcmax^2*mass*H(C)/Gmax; % Ostriker et al (05) Eq6a
double winf, W0Lokas, E;
winf = (G*pow(mpc/1000.0,3)*pow(mass,2)/ri)*pow(g(C),-2)/2.0; // Lokas & Mamon (01) eq 21
W0Lokas = -1.0*winf*(1.0 - (1.0/pow(1.0+C,2)) - (2.0*log(1.0+C)/(1.0+C)));
E = Ek(C, aniso_beta, winf);
//abs(2*E/W0Lokas) % 2|T|/W (i.e. virial ratio)
return (W0Lokas + E);
}
double Ek(double x, float aniso_beta, double Winf) {
//% calculate total kinetic energy T in NFW halo using Lokas & Mamon 01
double K;
if (aniso_beta==0.0) {
K = -3.0 + 3.0/(1.0+x) - 2.0*log(1.0+x) + x*(5.0 + 3.0*log(1.0+x));
K = K - pow(x,2)*(7.0 + 6.0*log(1.0+x));
K = K + pow(x,3)*(PI*PI - log(C) - log(x/C) + log(1.0+x) + 3.0*pow(log(1+x),2) + 6.0* gsl_sf_dilog(-1.0*x));
K = K*0.5;
}
else if (aniso_beta==0.5) {
K = -3.0 + 3.0/(1.0+x) - 3.0*log(1.0+x);
K = K + 6.0*x*(1.0+log(1.0+x));
K = K - pow(x,2)*(PI*PI + 3.0*pow(log(1.0+x),2) + 6.0*gsl_sf_dilog(-1.0*x));
K = K/3;
}
else {
K = -2.0*log(1.0+x);
K = K + x*(PI*PI/3.0 - 1.0/(1.0+x) + pow(log(1.0+x),2) + 2.0*gsl_sf_dilog(-1.0*x));
K = K/2.0;
}
return K*Winf;
}
double setAprime() {
Aprime = 1.5*(1.0+f_s)*(Gmax()*K(C)*(3.0-4.0*delta_s()) + K_s());
Aprime += -1.0*(Gmax()*eps*f_s*pow(clight/vcmax,2)) - (Gmax()*eps_dm*fabs(Edm(0.0))/(mgas*pow(vcmax,2)));
return Aprime;
}
double setBprime() {
Bprime = (1.0+f_s)*(S_C(C)/g(C));
return Bprime;
}
double energy_constraint(double beta, double Cf) {
double f, Lval;
if (Cf<=0.0) Cf = C/10.0; // (C<0) is unphysical
f = Aprime + Bprime*(pow(Cf,3) - pow(C,3))/3.0;
Lval = Lspline(Cf,beta);
f += -1.0*I2(Cf,beta)/Lval + (1.5*(I3spline(Cf,beta) + I3p(Cf,beta))/(beta*Lval));
if (f != f ) return 100.0;
return (f);
}
double pressure_constraint(double beta, double Cf) {
double f, Cfp;
if (beta<=0.0) beta = 0.1;
if (Cf<=0.0) Cf = C/10.0;
Cfp = Lvar(Cf, beta);
f = pow((1.0+f_s)*(S_C(C*pressurebound)/g(C))*beta*Lspline(Cf,beta),(1.0/(1.0+n)));
if (pturbrad==1) f += -1.0*pow(1.0 + delta_rel*pow(theta(Cfp,beta),-2),1.0/(1.0+n))*theta(Cfp,beta);
else if (pturbrad==2) f += -1.0* pow(1.0,(1.0/(1.0+n)))*(1.0 - beta*j(Cfp)/((1.0+n)));
else f += -1.0* pow(1.0+delta_rel,(1.0/(1.0+n)))*(1.0 - beta*j(Cfp)/((1.0+n)*(1.0+delta_rel)));
if (f != f) {
return 100.0;
cout << "Pressure constraint failed! " << endl;
}
return (f);
}
int solve_gas_model(bool verbose, float tolerance) {
const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
gsl_multimin_fminimizer *s = NULL;
gsl_vector *ss, *x;
gsl_multimin_function F;
size_t iter = 0;
int status;
double size;
float adiabat_n = n;
double pt = 0.0;
pt = (double)pturbrad;
setAprime();
setBprime();
double p[8] = {delta_rel, adiabat_n, C, 0.0, Aprime, Bprime, f_s, pt}; // \beta,_f, C, delta_rel, Ap, Bp
F.f = gasmod_apply_bc;
F.n = 2;
F.params = p;
/* Starting point */
x = gsl_vector_alloc (2);
gsl_vector_set (x, 0, 1.0);
gsl_vector_set (x, 1, C/2.0);
/* Set initial step sizes to 1 */
ss = gsl_vector_alloc (2);
gsl_vector_set_all (ss, 1.0);
/* Initialize method and iterate */
s = gsl_multimin_fminimizer_alloc (T, 2);
gsl_multimin_fminimizer_set (s, &F, x, ss);
do {
iter++;
status = gsl_multimin_fminimizer_iterate(s);
if (status)
break;
size = gsl_multimin_fminimizer_size (s);
status = gsl_multimin_test_size (size, tolerance);
if ((status == GSL_SUCCESS) & verbose) {
printf ("converged to minimum at\n");
printf ("%5d %10.4e %10.4e f() = %7.3f size = %.3f\n",
(int)iter,
gsl_vector_get (s->x, 0),
gsl_vector_get (s->x, 1),
s->fval, size);
}
} while (status == GSL_CONTINUE && iter < 100);
final_beta = gsl_vector_get (s->x, 0);
final_Cf = gsl_vector_get (s->x, 1);
setp0rho0(verbose);
gsl_vector_free(x);
gsl_vector_free(ss);
gsl_multimin_fminimizer_free (s);
return status;
}
void setp0rho0(bool verbose) {
if (verbose) {
cout << "final_Cf " << final_Cf << endl;
cout << "final_beta " << final_beta << endl;
}
rho0 = mgas / (4.0*PI*pow(ri*1000/mpc,3)*Lspline(final_Cf, final_beta)); // in Msol/Mpc^3
p0 = rho0*vcmax*vcmax/(final_beta*Gmax()); // in Msol/Mpc^3 (km/s)^2
T0 = (mmw*m_p*p0/rho0)*(1000.0/eV); // this is in keV
if (verbose) {
cout << "final model solution:" << endl;
cout << "P0 " << "rho0 " << "TO " << endl;
cout << p0 << " " << rho0 << " " << T0 << " "<<mgas<<endl;
}
p0 = p0*1.0e6*m_sun/(eV*1000.0*pow(mpc,3)); // now in keV/m^3
}
void initialize_profiles(float minr, float maxr, float dr, float ellmin, float ellmax, float dlfrac) {
int i;
float dtheta, dl_ell_10;
float c500=1.177;
minr = minr*radius; //in mpc
maxr = maxr*radius; //in mpc
dr = dr*radius; //in mpc
if (dlfrac < 1) dl_ell_10 = dlfrac/log(10.0);
nrads = (int) ceil((maxr-minr)/dr)+1;
if (dlfrac == 0.0) nell = 1;
if(dlfrac < 1 && dlfrac > 0.0) nell = (int)ceil((log10(ellmax/ellmin)/dl_ell_10))+1;
if(dlfrac >= 1) nell= (int) ceil((ellmax-ellmin)/dlfrac)+1;
x = new float [nrads];
ysz = new double [nrads];
rhogas= new double [nrads];
Tsz= new double [nrads];
Ksz= new double [nrads];
rr= new double [nrads];
k= new float [nrads];
clumpf = new double [nrads];
for (i=0;i<nrads;i++) {
x[i] = minr + (float)i*dr; //in Mpc
k[i]= 2.0*PI/x[i]*radius/c500;
}
fft_ysz = new double [nell];
ell = new double [nell];
for (i=0;i<nell;i++) {
if (dlfrac<1) {
ell[i] = log10(ellmin) + ((float)i*dl_ell_10);
ell[i] = ceil(pow(10.0, ell[i]));
}
if(dlfrac >= 1) ell[i] = ellmin + (float) i*dlfrac;
//cout<<i<<" "<<ell[i]<<endl;
}
}
int get_nrads() {
return nrads;
}
int get_nell() {
return nell;
}
void clear_profiles() {
delete[] x;
delete[] ysz;
delete[] fft_ysz;
delete[] ell;
delete[] rhogas;
delete[] rr;
delete[] Tsz;
delete[] Ksz;
delete[] clumpf;
}
double* get_ell() {
return &ell[0];
}
double* get_yfft() {
return &fft_ysz[0];
}
double* get_ysz() {
return &ysz[0];
}
float* get_k(){ return &k[0];}
double* get_x() { return (double*)&x[0];}
double* get_P() { return &ysz[0]; }
double* get_T(){ return &Tsz[0];}
double* get_rhogas(){ return &rhogas[0];}
double* get_K() { return &Ksz[0]; }
double* get_xx(){ return &rr[0]; }
double* get_clumpf(){ return &clumpf[0]; }
double* calc_3d_sz_profile(float R500) {
float units = mpc*sigma_T/me_csq;
double xx;
int i;
for (i=0;i<nrads;i++) {
xx = (double)x[i]/(1000.0*ri/mpc); // need to sort out the stupid units on ri
ysz[i] = (double)units*p0*pow(theta(xx,final_beta),(n+1.0))/mu_e; // should the mu_e be here?
if (pturbrad==2) ysz[i] = ysz[i]*(1.0 - delta_rel*pow(x[i]*(1000.0*ri/mpc) / R500, delta_rel_n));
}
return &ysz[0];
}
double* calc_electron_pressure_profile(float R500) {
double xx;
int i;
for (i=0;i<nrads;i++) {
xx = (double)x[i]/(1000.0*ri/mpc); // need to sort out the stupid units on ri
ysz[i] = (double)mmw*p0*pow(theta(xx,final_beta),(n+1.0))/mu_e;
if (pturbrad==2) ysz[i] = ysz[i]*(1.0 - delta_rel*pow(x[i]*(1000.0*ri/mpc) / R500, delta_rel_n));
}
return &ysz[0];
}
/* Pressure profile from Arnaud et al. (2010) */
double calc_pressure_Arnaud(double r, float r500, double h, double E){
double M500, R500, pi, rho_cr, rho_cr0, h70, XH, b_HSE;
double P, P_gnfw, P0, c500, alpha, beta, gamma, Mpiv, xi;
pi = 4.0*atan(1.0);
XH = 0.76;
b_HSE = 0.2; // Hydrostatic bias
rho_cr0 = 2.77536627e11*h*h; //[Msun/Mpc^3]
rho_cr = rho_cr0*E*E;
h70 = h/0.7;
R500 = (double) r500/pow(1.0+b_HSE, 1./3.);
M500 = 4.0*pi/3.0*500.0*rho_cr*pow(R500, 3.0);
/* Arnaud et al. (2010) */
/*
P0 = 8.403*pow(h70, -3./2.);
c500 = 1.177;
gamma = 0.3081;
alpha = 1.0510;
beta = 5.4905;
*/
/* Planck Collaboration (2013) */
P0 = 6.41;
c500 = 1.81;
gamma = 0.31;
alpha = 1.33;
beta = 4.13;
//Mpiv = 3e14*0.7; //[Msun/h]
Mpiv = 3e14; //[Msun]
xi = r/R500;
P_gnfw = P0/(pow(c500*xi, gamma)*pow(1.0+pow(c500*xi, alpha), (beta-gamma)/alpha));
P = 1.65e-3*pow(E, 8./3.)*pow(M500/Mpiv, 2./3.+0.12)*P_gnfw*h70*h70;
P = P/((2.0+2.0*XH)/(3.0+5.0*XH));//convert electron pressure to thermal pressure
return P;
}
double calc_gas_density(double r, float R500){
double xx, rhogas;
xx = r/(1000.0*ri/mpc);
rhogas = rho0*pow(theta(xx,final_beta), n)/pow(mpc,3)/1.e3*m_sun; // g/cm^3
return rhogas;
}
/*
returns thermal gas pressure (not electron pressure) in the unit of keV/cm^3.
Note that the unit does not include hubble parameter (h).
*/
double calc_gas_pressure(double r, float R500){
double xx, ngas, T, Pgas;
xx = r/(1000.0*ri/mpc);
ngas = rho0*pow(theta(xx,final_beta), n)/m_p/mmw/pow(mpc,3)/1.e6*m_sun; // cm^-3
T = T0*theta(xx,final_beta)*(1.0 - delta_rel*pow(xx*(1000.0*ri/mpc)/R500, delta_rel_n)); //keV
Pgas = ngas*T; // keV/cm^3
return Pgas;
}
double calc_gas_num_density(double r, float R500){
double xx, ngas;
xx = r/(1000.0*ri/mpc);
ngas = rho0*pow(theta(xx,final_beta), n)/m_p/mmw/pow(mpc,3)/1.e6*m_sun; // cm^-3
return ngas;
}
double calc_clumped_gas_density(double r, float R500){
double xx, rhogas;
double xb, clump;
xx = r/(1000.0*ri/mpc); //r in Mpc, ri in km!, xx is r/ri
xb = x_clump * R500; //x_clump in R500, xb in units of Mpc (same as r)
rhogas = rho0*pow(theta(xx,final_beta), n)/pow(mpc,3)/1.e3*m_sun; // g/cm^3
if ( r < x_clump ) {
clump = 1.0 + clump0*pow (r/xb, alpha_clump1);
}
else {
clump = 1.0 + clump0*pow (r/xb, alpha_clump2);
}
if ( clump < 1.0 ) clump = 1.0;
rhogas *= sqrt(clump);
return rhogas;
}
double calc_clumped_gas_num_density(double r, float R500){
double xx, ngas;
xx = r/(1000.0*ri/mpc);
ngas = rho0*pow(theta(xx,final_beta), n)/m_p/mmw/pow(mpc,3)/1.e6*m_sun; // cm^-3
double clump;
double xb = x_clump * R500; //x_clump in R500, xb in units of Mpc (same as r)
if ( r < x_clump ) {
clump = 1.0 + clump0*pow (r/xb, alpha_clump1);
} else {
clump = 1.0 + clump0*pow (r/xb, alpha_clump2);
}
if ( clump < 1.0 ) clump = 1.0;
ngas *= sqrt(clump);
return ngas;
}
double calc_gas_temperature(double r, float R500){
double xx, T;
xx = r/(1000.0*ri/mpc);
T = T0*theta(xx,final_beta)*(1.0 - delta_rel*pow(xx*(1000.0*ri/mpc)/R500, delta_rel_n)); //keV
if ( T < 0.0 ) T = 0.0;
return T;
}
double calc_Y(float R500, float Rvir, double Rmax){
int nx = 1000;
double res, x, w;
gsl_integration_glfixed_table *t;
t = gsl_integration_glfixed_table_alloc(nx);
res = 0.0;
for(int i=0;i<nx;i++){
gsl_integration_glfixed_point(0.0, 1.0, i, &x, &w, t);
res += w*4.0*PI*x*x*calc_gas_pressure(x*Rmax, R500);
}
res *= pow(Rmax*mpc*1e2, 3.0);
gsl_integration_glfixed_table_free(t);
return res;
}