-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas_model.h
2245 lines (1902 loc) · 78.7 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_HEADER_INCLUDED
#define GAS_MODEL_HEADER_INCLUDED
#include <stdio.h>
#include <math.h>
#include <assert.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_multiroots.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_dht.h>
#include <gsl/gsl_interp.h>
#include <gsl/gsl_spline.h>
#include "xray.h"
using namespace std;
struct parameters {
double A_nt;
double B_nt;
double gamma_nt;
//double alpha0; // fiducial : 0.18
//double n_nt; // fiducial : 0.80
//double beta; // fiducial : 0.50
double eps_f; // fiducial : 3.97e-6
double eps_DM; // fiducial : 0.00
double f_star; // fiducial : 0.026
double S_star; // fiducial : 0.12
double A_C; // fiducial : 1.00
double gamma_mod0; // fiducial : 0.10
double gamma_mod_zslope; // fiducial : 1.72
double x_break; // fiducial : 0.195
double x_smooth; // fiducial : 0.01
double n_nt_mod; // fiducial : 0.80
double clump0;
double alpha_clump;
double beta_clump;
double gamma_clump;
};
void set_fiducial_parameters (struct parameters *params);
void set_parameters (double pzero, double p1, double p2, double p3, double p4, double p5, double p6, double p7, double p8, double p9, double p10, double p11, double p12, double p13, double p14, double p15, double p16, double p17, struct parameters *params);
struct my_func_params { float a; float b; float c; double d; int e;float f;};
/*
double Shaw_model_param::conc_norm = 1.0;
double Shaw_model_param::conc_mass_norm = 1.0;
int Shaw_model_param::pturbrad = 2;
bool Shaw_model_param::verbose = false;
double Shaw_model_param::overden_id = -1.0;
int Shaw_model_param::relation = 3;
double 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);
int gasmod_constraints(const gsl_vector * x, void *p, gsl_vector * f);
int gasmod_constraints_df(const gsl_vector *x, void *p, gsl_matrix *J);
int gasmod_constraints_fdf(const gsl_vector *x, void *p, gsl_vector *f, gsl_matrix *J);
double gxs (double x, void *p);
double dgxs (double x, void *p);
void gxs_fdf (double x, void *p, double *y, double *dy);
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 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);
friend int gasmod_constraints(const gsl_vector * x, void *p, gsl_vector * f);
friend int gasmod_constraints_df(const gsl_vector * x, void *p, gsl_matrix * J);
friend int gasmod_constraints_fdf(const gsl_vector * x, void *p, gsl_vector * f, gsl_matrix * J);
friend double gxs (double x, void *p);
friend double dgxs (double x, void *p);
friend void quadratic_fdf (double x, void *p, double *y, double *dy);
protected:
//double delta_rel, delta_rel_n, n, eps, eps_dm, fs_0, fs_alpha, f_s, Mpiv, chi_turb, delta_rel_zslope;
double A_nt, B_nt, n, eps, eps_dm, fs_0, fs_alpha, f_s, Mpiv, chi_turb, gamma_nt;
double C, ri, rhoi, mass, radius, vcmax, mgas, Ytot, pressurebound, R500toRvir, R500toR200m;
double xs;
double final_beta, final_Cf, p0, rho0, T0; // need to define these
//double Aprime, Bprime;
double Tau_d, Tau_b, bulge_frac;
double *x, *k;
double *ysz, *fft_ysz, *ell;
double *rhogas, *rr, *Tsz, *Ksz, *clumpf;
int nrads, nell;
double clump0, alpha_clump, beta_clump, gamma_clump;
double m_sun, clight, mpc, G, mu_e, mmw, m_p, eV, sigma_T, m_e, charge, me_csq;
public:
gas_model() {
set_constants();
}
gas_model(double *p) {
n = p[0];
eps = p[1];
eps_dm = p[2];
fs_0 = p[3];
fs_alpha = p[4];
//delta_rel_zslope = p[6];
//delta_rel_n = p[7];
A_nt = p[5];
B_nt = p[6];
gamma_nt = p[7];
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;
bulge_frac = 0.9;
Tau_d = 4.5;//4.5; // in Gyr
Tau_b = 1.5;//1.5; // in Gyr
//assert(eps > 0);
}
/*
void evolve_pturb_norm(double z, double outer_radius) { //compute alpha(z)
double 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(double bmass, double bradius, double conc, double brhoi, double 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*M_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_constants() {
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
//charge = 1.60217646e-19;// joules
//me_csq = m_e*clight*clight*1.0e6/(1000.0*charge); // in KeV
me_csq = 511.0; // in KeV
}
void set_mgas_init(double baryon_frac_univ) {
mgas = (baryon_frac_univ)*mass/(1.0+f_s);
}
void set_mass(double inp) {
mass = inp;
}
void set_ri(double inp) {
ri = inp;
}
void set_vcmax(double inp) {
vcmax = inp;
}
void set_mgas(double inp) {
mgas = inp;
}
//void set_delta_rel(double inp) {
// delta_rel = inp;
//}
//void set_delta_rel_n(double inp) {
// delta_rel_n = inp;
//}
void set_A_nt(double inp) {
A_nt = inp;
}
void set_B_nt(double inp) {
B_nt = inp;
}
void set_n(double inp) {
n = inp;
}
void set_C(double inp){
C = inp;
}
void set_xs(double inp){
xs = inp;
}
void set_f_s(double inp){
f_s = inp;
}
void set_R500toR200m(double inp){
R500toR200m = inp;
}
double get_C(){
return C;
}
double get_n(){
return n;
}
//double get_delta_rel(){
// return delta_rel;
//}
double get_A_nt(){
return A_nt;
}
double get_f_s(){
return f_s;
}
void calc_fs(double M500, double baryon_frac_univ, double cosm_t0, double 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); //
if (f_s <= 0) {
cout << f_s << endl;
cout << fs_0 << " " << M500/Mpiv << " " << fs_alpha << endl;
f_s = 1.e-4;
}
f_s = f_s / (baryon_frac_univ - f_s); // f_s is now the star formation efficiency
//assert (f_s >= 0);
//---uncomment this line for z-evolution:
//f_s = f_s*calc_fstarz(cosm_t0, cosm_tz);
}
void set_stellar_evo_params(double inp1, double inp2, double inp3) {
// reset stellar evolution parameters as in Nagamine et al. 2006
Tau_b = inp1;
Tau_d = inp2;
bulge_frac = inp3;
}
double calc_fstarz(double cosm_t0, double 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;
double 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)
int status;
int iter = 0, max_iter = 100;
const gsl_root_fsolver_type *T;
gsl_root_fsolver *s;
double x_lo = 0.0, x_hi = 10.*C, x_guess;
double p[2] = { C, f_s };
gsl_function F;
F.function = &gxs;
F.params = &p;
T = gsl_root_fsolver_brent;
s = gsl_root_fsolver_alloc(T);
status = gsl_root_fsolver_set (s, &F, x_lo, x_hi);
//printf ("using %s method\n", gsl_root_fsolver_name (s));
//assert( f_s >= 0);
do {
iter++;
status = gsl_root_fsolver_iterate (s);
x_guess = gsl_root_fsolver_root (s);
x_lo = gsl_root_fsolver_x_lower (s);
x_hi = gsl_root_fsolver_x_upper (s);
status = gsl_root_test_interval (x_lo, x_hi, 0, 1.e-5);
//printf ("%5d [%.7f, %.7f] %.7f\n", iter, x_lo, x_hi, x_guess);
} while (status == GSL_CONTINUE && iter < max_iter);
if (status != GSL_SUCCESS ) {
cout << "xs did not converge! Setting xs to C." << endl;
xs = C;
} else {
xs = x_guess;
}
gsl_root_fsolver_free (s);
//cout << "In findxs: " << endl;
//cout << " C = " << C << endl;
//cout << " f_s = " << f_s << endl;
//cout << " xs = " << xs << endl;
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
double 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 = M_PI*M_PI/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 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 = 1.e-7; //% diverges at x = 0
gsl_integration_qags (&F, x, C, 0, 1.e-7, 2000, w, &result, &error);
Sx = S_C(C) - result;
gsl_integration_workspace_free (w);
return 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
if (xs <= 0 ) {
cout << "In K_s: xs = "<< xs << " is less than zero! " << endl;
return 0.0;
}
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
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;
//cout << "In K_s: xs= "<< xs << endl;
gsl_integration_qags (&F, 0.0, xs, 0, 1.e-7, 1000, w, &resulta, &error);
gsl_integration_qags (&FF, 0.0, xs, 0, 1.e-7, 1000, 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;
th = (1.0 - (beta*j(x)/(1.0+n)));
return fabs(th);
}
double theta_mod(double x, double beta, double x_break, double npoly_mod) {
// ---
// modified theta (broken power-law model)
// x_break is here in units of the NFW scale radius
// ---
double th;
if (npoly_mod >= 1.e7) {
th = 1.0;
}
else if (x>=x_break){
th = (1.0 - (beta*j(x)/(1.0+n)));
}
else if (x<x_break){
th = (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 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;
double params[5] = {delta_rel, n, pturbrad, C, beta};
gsl_function F;
F.function = &ftx_func;
F.params = ¶ms;
if (Cf<=0) {
cout << "Cf error! in I2" << Cf << endl;
return 0.0;
}
gsl_integration_qags (&F, 1e-7, Cf, 0, 1.e-5, 10000, w, &result, &error);
gsl_integration_workspace_free (w);
return result;
*/
int nxbins = 1000, i;
double *xx, *ftx, result, dlogx;
double lowlim = 1e-30;
if (Cf<=lowlim) {
cout << "Cf error! in I2, Cf=" << Cf << endl;
return 0.0;
}
xx = new double [nxbins];
ftx = new double [nxbins];
// first need to make an array of values
result = 0.0;
dlogx = (log10(Cf)-log10(lowlim))/((double)(nxbins-1));
for (i=0;i<nxbins;i++) {
xx[i] = pow(10.0, log10(lowlim) + (double)i * dlogx);
ftx[i] = f(xx[i])*pow(theta(xx[i], beta),n)*pow(xx[i],2);
result += ftx[i]*xx[i]*dlogx;
}
delete xx;
delete ftx;
return result;
}
double I2spline(double Cf, double beta) {// % eqn 27
int nxbins = 100, i;
if (Cf<=0) {
cout << "Cf error! in I2spline, Cf=" << Cf << endl;
return 0.0;
}
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_steffen, nxbins);
double *xx, *ftx, result;
xx = new double [nxbins];
ftx = new double [nxbins];
// first need to make an array of values
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;
double params[5] = {delta_rel, n, pturbrad, C, beta};
gsl_function F;
F.function = &tx_func;
F.params = ¶ms;
//cout << "In I3: Cf, beta= "<< Cf << " " << beta << endl;
if (Cf<=0) {
cout << "Cf error! in I3" << Cf << endl;
return 0.0;
}
gsl_integration_qags (&F, 1e-7, Cf, 0, 1.e-5, 10000, w, &result, &error);
//cout << "I3: " << result << endl;
gsl_integration_workspace_free (w);
return result;
*/
int nxbins = 1000, i;
double *xx, *tx, result, dlogx;
double lowlim = 1e-30;
if (Cf<=lowlim) {
cout << "Cf error! in I3, Cf=" << Cf << endl;
return 0.0;
}
xx = new double [nxbins];
tx = new double [nxbins];
// first need to make an array of values
result = 0.0;
dlogx = (log10(Cf)-log10(lowlim))/((double)(nxbins-1));
for (i=0;i<nxbins;i++) {
xx[i] = pow(10.0, log10(lowlim) + (double)i * dlogx);
tx[i] = pow(theta(xx[i], beta),n+1.0)*pow(xx[i],2);
result += tx[i]*xx[i]*dlogx;
}
delete xx;
delete tx;
return result;
}
double I3spline(double Cf, double beta) {// % eqn 27 {
int nxbins = 1000, i;
double lowlim = 1e-30;
if (Cf<=lowlim) {
cout << "Cf error! in I3spline, Cf=" << Cf << endl;
return 0.0;
}
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_steffen, nxbins);
double *xx, *tx, result, dlogx;
xx = new double [nxbins];
tx = new double [nxbins];
result = 0.0;
dlogx = (log10(Cf)-log10(lowlim))/((double)(nxbins-1));
for (i=0;i<nxbins;i++) {
//xx[i] = ((double)i * Cf / ((double)(nxbins-1)));
xx[i] = pow(10.0, log10(lowlim) + (double)i * dlogx);
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;
double params[5] = {delta_rel, n, pturbrad, C, beta};
gsl_function F;
if ((int)pturbrad==1) F.function = &tx_func_p;
else F.function = &tx_func;
F.params = ¶ms;
double lowlim = 1e-7;
if (Cf<=lowlim) {
cout << "Cf error! in I3p" << Cf << endl;
return 0.0;
}
gsl_integration_qags (&F, 1e-7, Cf, 0, 1.e-5, 10000, w, &result, &error);
//cout << "I3: " << result << endl;
gsl_integration_workspace_free (w);
if ((int)pturbrad==0) result = result*delta_rel*2.0; // check factor of 2!
else if ((int)pturbrad==2) result = 0.0;
return result;
*/
int nxbins = 1000, i;
double *xx, *tx, result, dlogx;
double lowlim = 1e-30;
if (Cf<=lowlim) {
cout << "Cf error! in I3p, Cf=" << Cf << endl;
return 0.0;
}
xx = new double [nxbins];
tx = new double [nxbins];
result = 0.0;
dlogx = (log10(Cf)-log10(lowlim))/((double)(nxbins-1));
for (i=0;i<nxbins;i++) {
xx[i] = pow(10.0, log10(lowlim) + (double)i * dlogx);
//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);
tx[i] = pow(theta((xx[i]), beta),n+1.0)*pow((xx[i]),2);
result += tx[i]*xx[i]*dlogx;
}
delete xx;
delete tx;
return result;
}
double I3p_spline(double Cf, double beta) {// % eqn 27 {
// DO NOT USE THIS FOR NOW
int nxbins = 1000, i;
if (Cf<=0) {
cout << "Cf error! in I3p_spline, Cf=" << Cf << endl;
return 0.0;
}
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_steffen, nxbins);
double *xx, *tx, result;
xx = new double [nxbins];
tx = new double [nxbins];
// first need to make an array of values
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);
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;
double params[5] = {delta_rel, n, pturbrad, C, beta};
gsl_function F;
F.function = &ttx_func;
F.params = ¶ms;
if (Cf<=lowlim) {
cout << "Cf error! in L" << Cf << endl;
return 0.0;
}
gsl_integration_qags (&F, 1e-7, Cf, 0, 1.e-5, 10000, w, &result, &error);
//cout << "L: " << result << endl;
gsl_integration_workspace_free (w);
return result;
*/
int nxbins = 1000, i;
double lowlim = 1e-30;
if (Cf<=lowlim) {
cout << "Cf error! in L, Cf=" << Cf << endl;
return 0.0;
}
double *xx, *ttl, result, dlogx;
xx = new double [nxbins];
ttl = new double [nxbins];
result = 0.0;
dlogx = (log10(Cf)-log10(lowlim))/((double)(nxbins-1));
for (i=0;i<nxbins;i++) {
xx[i] = pow(10.0, log10(lowlim) + (double)i * dlogx);
ttl[i] = pow(theta((xx[i]), beta), n)*pow((xx[i]),2);
result += ttl[i]*xx[i]*dlogx;
}
delete xx;
delete ttl;
return result;
}
double Lspline(double Cf, double beta) {// % eqn 27 {
int nxbins = 1000, i;
if (Cf<=0) {
cout << "Cf error! in Lspline, Cf = " << Cf << endl;
return 0.0;
}
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_steffen, nxbins);
double *xx, *ttl, result;
xx = new double [nxbins];
ttl = new double [nxbins];
// first need to make an array of values
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
double gfact, Cfp, Lc, Lp;
gfact = g(pressurebound*C) / g(C);
if (gfact<1.0) cout << "gfact error" << endl;
Cfp = Cf;
//Lc = Lspline(Cf,beta);
Lc = L(Cf,beta);
Lp = Lc;
// do this in three steps to speed it up
while (Lp < gfact*Lc) {
Cfp *= 1.2;
//Lp = Lspline(Cfp,beta);
Lp = L(Cfp,beta);
}
Cfp = Cfp / 1.2;
while (Lp < gfact*Lc) {
Cfp *= 1.1;
//Lp = Lspline(Cfp,beta);
Lp = L(Cfp,beta);
}
Cfp = Cfp / 1.1;
while (Lp < gfact*Lc) {
Cfp *= 1.02;
//Lp = Lspline(Cfp,beta);
Lp = L(Cfp,beta);
}
Cfp *= 1.01/1.02; // settle on half way in between
return Cfp;
}
double Edm(double 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, double 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)*(M_PI*M_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)*(M_PI*M_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*(M_PI*M_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 val, Lval;
double Aprime, Bprime;
double Ks = K_s();
Aprime = 1.5*(1.0+f_s)*(Gmax()*K(C)*(3.0-4.0*delta_s()) + Ks);
Aprime += -1.0*(Gmax()*eps*f_s*pow(clight/vcmax,2)) - (Gmax()*eps_dm*fabs(Edm(0.0))/(mgas*pow(vcmax,2)));
Bprime = (1.0+f_s)*(S_C(C)/g(C));
if (beta<=0.0 || beta != beta) beta = 0.1;
if (Cf<=0.0 || Cf != Cf ) Cf = C; // (C<0) is unphysical
val = Aprime + Bprime*(pow(Cf,3) - pow(C,3))/3.0;
//Lval = Lspline(Cf,beta);
Lval = L(Cf,beta);
//val += -1.0*I2(Cf,beta)/Lval + (1.5*(I3spline(Cf,beta) + I3p(Cf,beta))/(beta*Lval));
val += -1.0*I2(Cf,beta)/Lval + (1.5*(I3(Cf,beta) + I3p(Cf,beta))/(beta*Lval));
if (val != val ) {
cout << "Energy constraint failed! " << endl;
cout << " eps, eps_dm, fs_0, fs_alpha = " << eps << " " << eps_dm << " " << fs_0 << " " << fs_alpha << endl;
cout << " beta, Cf, C, xs = " << beta << " " << Cf << " " << C << " " << xs << endl;
cout << " A, B, Lval = " << Aprime << " " << Bprime << " " << Lval << endl;
//cout << "f_s, Gmax(), K(C), delta_s(), K_s() = " << f_s << " " << Gmax() << " " << K(C) << " " << delta_s() << " " << Ks << endl;
//cout << "eps, vcmax, eps_dm, |Edm|, mgas =" << eps << " " << vcmax << " " << eps_dm << " " << fabs(Edm(0.0)) << " " << mgas << endl;
return 0.0;
}
return (val);
}
double pressure_constraint(double beta, double Cf) {
double val, Cfp;
if (beta<=0.0) beta = 0.1;
if (Cf<=0.0) Cf = C;
//if (beta<=0.0) beta = 1.e-7;
//if (Cf<=0.0) Cf = 1.e-7;
Cfp = Lvar(Cf, beta);
//val = pow((1.0+f_s)*(S_C(C*pressurebound)/g(C))*beta*Lspline(Cf,beta),(1.0/(1.0+n)));
val = pow((1.0+f_s)*(S_C(C*pressurebound)/g(C))*beta*L(Cf,beta),(1.0/(1.0+n)));
//if (pturbrad==1) val += -1.0*pow(1.0 + delta_rel*pow(theta(Cfp,beta),-2),1.0/(1.0+n))*theta(Cfp,beta);
//else if (pturbrad==2) val += -1.0* pow(1.0,(1.0/(1.0+n)))*(1.0 - beta*j(Cfp)/(1.0+n));
//else val += -1.0* pow(1.0+delta_rel,(1.0/(1.0+n)))*(1.0 - beta*j(Cfp)/((1.0+n)*(1.0+delta_rel)));
val += -1.0* pow(1.0,(1.0/(1.0+n)))*(1.0 - beta*j(Cfp)/(1.0+n));
if (val != val) {
return 0.0;
cout << "Pressure constraint failed! " << endl;
}
return (val);
}
int solve_gas_model (bool verbose, double tolerance) {
const gsl_multiroot_fsolver_type *T;
gsl_multiroot_fsolver *s;
gsl_vector *v;
size_t i, iter = 0, max_iter=100;
const size_t ndim = 2;
int status;
double size;
double p[15] = {n, eps, eps_dm, fs_0, fs_alpha, A_nt, B_nt, gamma_nt, C, mass, vcmax, ri, mgas, xs, f_s};
//assert(eps > 0);
gsl_multiroot_function func = {&gasmod_constraints, ndim, &p};
/* Starting point */
v = gsl_vector_alloc (2);
gsl_vector_set (v, 0, 1.0);
gsl_vector_set (v, 1, 10*C);
/* Initialize method and iterate */
T = gsl_multiroot_fsolver_hybrids;
s = gsl_multiroot_fsolver_alloc (T,2);
gsl_multiroot_fsolver_set (s, &func, v);
do {
iter++;
status = gsl_multiroot_fsolver_iterate(s);
if (status)
break;
status = gsl_multiroot_test_residual (s->f, tolerance);
if ((status == GSL_SUCCESS) & verbose) {
printf ("converged to minimum at\n");
printf ("%5d %10.4e %10.4e\n",
(int)iter,
gsl_vector_get (s->x, 0),
gsl_vector_get (s->x, 1));
}
} while (status == GSL_CONTINUE && iter < max_iter);
final_beta = gsl_vector_get (s->x, 0);
final_Cf = gsl_vector_get (s->x, 1);
if (final_Cf < 0 ) {
cout << "final Cf < 0! Setting Cf to C/10" << endl;
final_Cf = C/10.;
setp0rho0(true);
} else {
setp0rho0(verbose);
}
gsl_vector_free(v);
gsl_multiroot_fsolver_free(s);
return status;
}
void setp0rho0(bool verbose) {
if (verbose) cout << "eps, eps_dm, fs_0, fs_alpha = " << eps << " " << eps_dm << " " << fs_0 << " " << fs_alpha << endl;
if (verbose) cout << "mass, conc = " << mass << " " << C << endl;
if (verbose) {
cout << " final_Cf " << final_Cf << endl;
cout << " final_beta " << final_beta << endl;
}
rho0 = mgas / (4.0*M_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