/
DMcalcLUT1.cpp
795 lines (501 loc) · 17 KB
/
DMcalcLUT1.cpp
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
// 4-19-16
//
// Diffusion Equation with spatial diffusion, diffusion const independent of spatial coordinate -> D(E). Uses GSL and Darksusy
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <cmath>
#include <ctime>
#include <vector>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>
#include <iomanip>
#include "Constants.h"
/* to compile and run, use command
g++ -o DMcalcLUT1 DMcalcLUT1.cpp -I/home/alex/research/darksusy-5.1.2/include -L/home/alex/research/darksusy-5.1.2/lib -lgsl -lgslcblas -ldarksusy -lFH -lHB -lgfortran
*/
//////////////////////////// routines for calling fortran/darksusy stuff /////////////////////////
extern"C" { //interface with fortran code to initialize darksusy
void dsinit_();
}
extern"C" { // dshayield gives injection spectrum
double dshayield_(double *mwimp, double *emuthr, int *ch, int *yieldk, int *istat);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// consider putting this in its own header?? problem is that we need to access and change member variables
class Cluster{
public:
std::string name;
double z ;
double rh; //halo radius
double B0 ; //microGauss
double rcore ; //0.291; //core radius in Mpc for coma
double root_dv;
double alpha; // D(E) ~ E^alpha
double size;
double scale;
std::vector<double> vlookup;
Cluster() : name(""),
z(0), //redshift
rh(0), //halo radius Mpc
B0(4.7), rcore(0) , //Bfield Params
root_dv(0.035) ,
alpha(3.0) , //DIffusion parameter, not really a cluster thing but easy access is good
size(1000000), //MUST CHANGE CONSTANT n->nele
vlookup(size),
scale(1)
{ //everything labelled or Coma, sgould work in user options
std::cout << "creating cluster... " << std::endl;
}
double bfield_model (double r) {
double beta = 0.75;
double eta = 0.5;
double B_field = B0 * pow(( 1 + r*r/(rcore*rcore)),(-1.5*beta*eta)); // Storm et al 2013
return B_field;
};
double bloss(double E , double r){
double ne = 1e-3;
double bloss = 0.0253*pow(bfield_model(r), 2)*E*E //bsyn bfield_model(r)
+ 0.265 * pow(1 + z, 4 )*E*E //bIC
+ 1.51*ne*(0.36 + log(E/me/ne) ) //+ 1.51*n*(0.36 + log(E/me) )*E //bbrem Note this is most likely incorrect, no factor of E, and should be E/me/nin log
+ 6.13*ne*( 1 + log(E/me/ne)/75); //bcoul
bloss = bloss *1e-16;
return bloss;
};
double bloss(double E ){ //overload so that we can just have bloss(E), could also have same as b(E, r) but set r=0 ?? kinda sloppy
double ne = 1.3e-3;
double Bmu = 1.; //should us average or something lat
double bloss = 0.0254*pow(Bmu, 2.0)*E*E //bsyn bfield_model(r)
+ 0.25 /** pow(1 + c.z, 4 )*/*E*E //bIC
+ 1.51*ne*(0.36 + log(E/me/ne) ) //brem , in Emma's code has + 1.51*n*(0.36 + log(E/me) )*E
+ 6.13*ne*( 1 + log(E/me/ne)/75); //bcoul
bloss = bloss ;// *1e-16;
return bloss;
};
double DM_profile(double r){
double rhos = 0.039974 ;//DM char density in Gev /cm^3 Storm13 (Mathematica)
double rs = 0.404*mpc2cm; //DM scale radius in Mpc Storm13 (Mathematica)
double rho = rhos / ( r/rs * pow(1 + r/rs , 2 )); //x? + 1e-100
return rho;
};
double D(double E){
//double Bmu = 0.5;
//double alpha = 1.0/3.0; //close to 1/3??
double db = pow(20.0 , 2.0/3.0); //just a scaling factor
double D0 = 100*3.1e28; // cm/s
double D = db * D0 * pow(E, alpha)/pow(bloss(E) , 1.0/3.0);
return D;
};
};
class Particle {
public:
int ch;
double mx;
double sv;
Particle() : ch(0) , mx(0), sv(0){}
};
Cluster c;
Particle p;
/////////////////// root_dv ////////////////////////////////
double dv(double E , void * params){
double dv = c.D(E)/c.bloss(E);
return dv;
}
double v( double E ){
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &dv;
gsl_integration_qags (&F, E, p.mx, 0, 1e-8, 1000,
w, &result, &error);
gsl_integration_workspace_free (w);
result *= 1e16;
return result;
}
double root_dv(double Ep, double vE){
double root_dv = sqrt( ( vE ) - v(Ep) ) ;
return root_dv;
}
////////////////////////////////////////////////
double ddist(double z , void * params){
double distint = mpc2cm * clight / ( H0 * sqrt( OmegaM * pow(1 + z , 3) + OmegaL ) );
return distint;
}
//distance as a function of redshift
double Dist(){
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &ddist;
gsl_integration_qags (&F, 0, c.z, 0, 1e-3, 1000,
w, &result, &error);
gsl_integration_workspace_free (w);
return result;
}
double rconst(double rcm){
double thetaB = 25; // beam size in arcsec
double dist_z = Dist() / (1 + c.z);
double rb = 0.5 * dist_z *thetaB/(3600) * (pi/180); //beam size in cm
double rconst;
if( rb < rcm){
rconst = rcm;
}
else if(rb > rcm){
rconst = rb;
};
return rconst;
}
double dGreens(double rp, void * params){
std::vector<double> greenParam = *(std::vector<double> *)params;
double ri = greenParam[0] ;
double root_dv = greenParam[1];
//std::cout << "rdv = " << root_dv/mpc2cm*1000 << std::endl;
double dGreens = rp/ri* pow( c.DM_profile(rp) , 2.0) *(exp( - pow( (rp-ri)/( 2*root_dv ) , 2)) - exp( - pow( ( rp + ri)/(2*root_dv) , 2)) ) ;//
//std::cout << "root_dv = " << root_dv << std::endl;
return dGreens;
}
/*
double GreenSum (double rp, void * params) { //called by ddsyn
std::vector<double> greenParam = *(std::vector<double> *)params;
double root_dv = greenParam[1];
double rh = c.rh * mpc2cm ;
int imNum = 7; //number of image pairs + 1
double Gsum = 0 ;
for (int i = - imNum; i < imNum + 1; ++i ){
double ri;
if (i == 0)
ri = r;
else
ri = (pow(-1 , i)*r + 2*i*rh);
Gsum += pow(-1, i) * dGreens(rp, ri, root_dv);
}
Gsum *= ;
return Gsum;
}*/
double gslInt_Green(double ri, double root_dv){
/* ///////////
std::clock_t start;
double duration;
start = std::clock();
int a ;
*/ ///////////
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
std::vector<double> greenParam (2);
double rh = c.rh*mpc2cm;
greenParam[0] = ri ;
greenParam[1] = root_dv ;
gsl_function F;
F.function = &dGreens;
F.params = &greenParam;
gsl_integration_qags (&F, 1e-16, rh, 0, 1e-1, 1000, //x?
w, &result, &error);
gsl_integration_workspace_free (w);
/* ///////after algorithm
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
std::cout << "greens duration: " << duration << std::endl;
*/ ///////
return result;
}
double darksusy (double Ep){
int yieldk = 151;
int istat;
double ds = dshayield_(&p.mx, &Ep, &p.ch, &yieldk, &istat);
return ds;
}
double ddiffusion(double Ep, void * params){
std::vector<double> diffusionParams = *(std::vector<double> *)params;
double E = diffusionParams[0];
double ri = diffusionParams[1] ;
double vE = diffusionParams[2];
double r = diffusionParams[3];
double Ep_scaled = (int)(Ep/c.scale) ;
double rootdv = sqrt( std::abs(vE - c.vlookup[Ep_scaled]) ); // 0.035*mpc2cm ; // //
if (c.vlookup[Ep_scaled] > vE)
{
std::cout << "v(E = "<<E<<", E_scaled = "<< (int)(E/c.scale)<<") = "<< vE << ", v(Ep = ";
std::cout<<Ep <<" Ep_scaled = " << Ep_scaled<<") = " ;
std::cout << c.vlookup[Ep_scaled] << ", Dv = " << vE - c.vlookup[Ep_scaled] <<" rootdv = "<< rootdv/mpc2cm *1000<<std::endl;;
};
//else
// rootdv = sqrt( abs(vE - c.vlookup[Ep_scaled]) );
//if (rootdv < 0.005*mpc2cm)
//std::cout << "v(E = "<<E<<") = "<< vE << ", v(Ep = "<<Ep<<") = " <<c.vlookup[Ep*c.n/p.mx] << ", rootdv = "<< rootdv/mpc2cm *1000<<std::endl;
//std::cout << " rootdv = "<< rootdv/mpc2cm *1000<<std::endl;
//std::cout << "root _dv(E = "<<E<<", Ep = "<<Ep<<"): " << rootdv/mpc2cm *1000<<std::endl;
double a = gslInt_Green(ri, rootdv);
double ddiffusion;
if (rootdv == 0)
ddiffusion = darksusy(Ep)/9;//divide by imNum to cancel out sum * (1.0/rootdv) * gslInt_Green(ri, rootdv);
else
ddiffusion = pow(4*pi , -1.0/2.0)*darksusy(Ep) * (1.0/rootdv) * pow(c.DM_profile(r) , -2.0)*a;
//std::cout << "ddiffusion(E = "<<E << ", Ep = "<< Ep <<") = " << ddiffusion <<" rdv = "<< rootdv/mpc2cm*1000<<std::endl;
//std::cout << "Greens(E = "<<E << ", Ep = "<< Ep <<") = " << a <<" rdv = "<< rootdv/mpc2cm*1000<<std::endl;
if(isnan(a) == 1){
std::cout << "v(E = "<<E<<") = "<< vE << ", v(Ep = ";
std::cout<<Ep <<" Ep_scaled = " << Ep_scaled<<") = " ;
std::cout << c.vlookup[Ep_scaled] << ", Dv = " << std::abs(vE - c.vlookup[Ep_scaled]) <<" rootdv = "<< rootdv/mpc2cm *1000<<std::endl;;
};
return ddiffusion;
}
double gslInt_diffusion( double E, double ri, double r){ // int over Ep
double E_scaled = (int)(E/c.scale);
double vE = c.vlookup[E_scaled];
//std::cout <<"E_scaled = " << E_scaled << std::endl;
//std::cout << c.n << " , " <<p.mx <<" ";
//double Ep = (p.mx + E) /2;
//std::cout <<E <<" -> vE = "<< vE << std::endl;
//double scale = 0.001;
//double Es = (int)(E*c.n/p.mx);
//std::cout << sqrt(vE)/mpc2cm*1000 << " "<<sqrt(c.vlookup[ E*c.n/p.mx ])/mpc2cm*1000 <<std::endl;;
//std::cout << Es << " LUT = " << sqrt(c.vlookup[ Es ])/mpc2cm*1000 << std::endl;
//double rootdv =sqrt(c.vlookup[E*c.n/p.mx]) ;//0.035*mpc2cm ;//sqrt(c.vlookup[ Es ]); //gives max value for rootdv//root_dv(Ep, vE); //
//std::cout << "umax: " << umax << std::endl;
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
std::vector<double> diffusionParams (4);
diffusionParams[0] = E;
diffusionParams[1] = ri;
diffusionParams[2] = vE;
diffusionParams[3] = r;
gsl_function F;
F.function = &ddiffusion;
F.params = &diffusionParams; //pass Ep to rootdv(), pass r from dndeeq as well,
gsl_set_error_handler_off();
gsl_integration_qags (&F, E, p.mx, 0, 1e-1, 1000,
w, &result, &error);
//std::cout << "result = " << result <<std::endl;
gsl_integration_workspace_free (w);
return result;
}
double dndeeq(double E, double r ){
//total time timer start
std::clock_t start;
double duration;
start = std::clock();
///////before algorithm
double rh = c.rh * mpc2cm ;
int imNum = 4; //number of image pairs + 1, total points = 2*imNum + 1
double diffsum = 0 ;
for (int i = -imNum; i < imNum + 1; ++i ){
double ri;
if (i == 0)
ri = r;
else
ri = (pow(-1 , i)*r + 2*i*rh);
diffsum += pow(-1, i) * gslInt_diffusion(E, ri, r);
};
double dndeeq = (1 / c.bloss(E,r))* diffsum ; //gslInt_diffusion(E, r);
////////after algorithm
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
if ( isnan(dndeeq) == 1)
std::cout << "dndeeq(E = "<< E << " , r = " << r/mpc2cm*1000 << " ) = "<< dndeeq <<" --> " << duration << std::endl;
return dndeeq;
}
//SYnchrotron emmission spectral function form Cola2006.
double fff(double x){
double fff = 1.25 * pow( x , 1.0/3.0) * exp( -x )* pow((648 + x*x) , 1.0/12.0);
return fff;
}
double dpsyn(double theta, void * params ){
double nu = 1.4; //Ghz
double psyn0 = 1.46323e-25 ; // Gev/s/Hz
double x0 = 62.1881 ; // dimensionless constant
double nu_em = ( 1 + c.z )* nu; // (observing freq)*(1+z)
std::vector<double> psynParams = *(std::vector<double> *)params;
double x = x0 *nu_em / ( c.bfield_model( psynParams[1] ) * pow( psynParams[0] , 2) );
//std::cout << "x = " << x << " B = " << c.bfield_model(psynParams[1]) << std::endl;
double dpsyn = psyn0 * c.bfield_model(psynParams[1]) * 0.5 * pow( sin(theta) , 2)* fff( x /sin(theta) );
return dpsyn;
}
double gslInt_psyn( double E, double r){ //int over theta
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
std::vector<double> psynParams (2);
psynParams[0] = E;
psynParams[1] = r;
gsl_function F;
F.function = &dpsyn;
F.params = &psynParams;
gsl_integration_qags (&F, 1e-16, pi, 0, 1e-3, 1000, //x?
w, &result, &error);
gsl_integration_workspace_free (w);
return result;
}
double djsyn(double E , void * params){
double r = *(double *)params;
double djsyn = 2* gslInt_psyn(E, r)* dndeeq(E , r);
return djsyn;
}
double gslInt_jsyn(double r){ // int over E
///////////
std::clock_t start;
double duration;
start = std::clock();
int a ;
///////////
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &djsyn;
F.params = &r;
gsl_integration_qags (&F, me, p.mx, 0, 1e-2, 1000,
w, &result, &error);
gsl_integration_workspace_free (w);
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
std::cout << "alpha = "<< c.alpha << ", jsyn( r = " << r/mpc2cm*1000 <<" ) = "<< result <<" duration: " << duration <<std::endl; // ~30s-60s
return result;
}
double dssyn( double r, void * params ){
double dist_z = Dist() / (1+c.z);
double ssynIntegrand = 4 *pi /pow(dist_z , 2) *pow(r,2) * pow(c.DM_profile(r) , 2)*gslInt_jsyn(r);
return ssynIntegrand;
}
double gslInt_ssyn( double r ){ // int over r
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &dssyn;
//F.params = &mx;
gsl_integration_qags (&F, 1e-16 , r, 0, 1e-2, 1000,
w, &result, &error);
return result;
}
double min_flux(double r){
double thetaB = 25.0; // beam size in arcsec
double frms = 1e-5; //noise per beam in Jy
double dist_z = Dist() / (1.0 + c.z);
double thetaH = r/dist_z * 180.0/pi * 3600.0;
double min_flux = 4.0 * log(2.0) * frms * pow(thetaH/thetaB, 2.0);
return min_flux;
}
double Calc_sv(double r){ // potentially add ch, z here?
double Sin = gslInt_ssyn(r) * GeVJy ;
double Sout = min_flux(r);
double sv = 8*pi * pow(p.mx, 2) * (Sout/Sin);
return sv ;
}
void createLUT(){
// iteration timer start
std::clock_t vstart;
double vduration;
vstart = std::clock();
int va ;
///////before algorithm
std::cout << "creating LUT..." <<std::endl;
//std::cout << root_dv(E, 5 , mx) << std::endl;
for (int j = 0 ; j < c.size ; ++j ){
c.scale = p.mx/c.size;
//std::cout << j << " " << j*dE<< std::endl;
c.vlookup[j] = v(j*c.scale);
/*
if(c.vlookup[j] > c.vlookup[0])
std::cout <<j << " rootv(E = " << j*c.scale << ") = " << sqrt(c.vlookup[j])/mpc2cm*1000 << std::endl;*/
}
//std::cout << "vlookup created..." << c.vlookup[] <<std::endl;
////////after algorithm
vduration = (std::clock() - vstart)/(double) CLOCKS_PER_SEC;
std::cout << "vlookup time = " << vduration <<std::endl;
}
void runComa(int ch){
p.ch = ch; //darksusy channel
c.name = "Coma";
c.z = 0.0232; //redshift
c.rh = 0.415; //halo radius Mpc
c.B0 = 4.7;
//c.alpha = 1.0/3.0; //
c.rcore = 0.291*mpc2cm; //
double rcm = c.rh * mpc2cm ;
double rmax = rconst(rcm);
double mx_min = 300;
double mx_max = 1000;
double data;
std::string channel;
if(p.ch == 13){
channel = "WW";
}
else if(p.ch == 15){
channel = "ee";
}
else if(p.ch == 17){
channel = "mumu";
}
else if(p.ch == 19){
channel = "tt";
}
else if(p.ch == 25){
channel = "bb";
};
std::ostringstream makefilename;
makefilename << "061316_"<<c.name << "_NONZERO_" << channel << "_alpha_" <<c.alpha <<"TEST.txt" ;
std::string filename = makefilename.str();
std::ofstream file(filename.c_str());
int n_mx = 15 ;//number of mx values used
/*
p.mx = 500;
createLUT();
std::cout << Calc_sv(rmax)<<std::endl;*/
//createLUT();
//double rt = 6.9984/1000*mpc2cm;f
//std::cout << dndeeq(0.000511, rt)<<std::endl;
for (int i = 0 ; i < n_mx + 1 ; ++i){
// iteration timer start
std::clock_t start;
double duration;
start = std::clock();
int a ;
///////before algorithm
p.mx = mx_min * ( exp( (log(mx_max) - log(mx_min))/ n_mx * i));
createLUT();
data = Calc_sv(rmax);
file << p.mx << "\t" << data <<std::endl;
std::cout << "sv( " << p.mx << " ) = " << data << std::endl;
////////after algorithm
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
std::cout << i << "/"<< n_mx << " ";
std::cout << p.ch << ", alpha = " << c.alpha << ", time = " << duration <<std::endl;
};
//end runComa()
}
void alphaPrint(){
std::cout << c.alpha << std::endl;
}
main(){
//total time timer start
std::clock_t start;
double duration;
start = std::clock();
int a ;
///////before algorithm
dsinit_(); //initialixe DarkSUSY
//runComa(13);
//runComa(15);
//runComa(17);
//runComa(19);
//runComa(25);
runComa(25);
////////after algorithm
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
std::cout << "Total time: " << duration <<std::endl;
}
/*
//total time timer start
std::clock_t start;
double duration;
start = std::clock();
int a ;
///////before algorithm
////////after algorithm
duration = (std::clock() - start)/(double) CLOCKS_PER_SEC;
std::cout << "Total time: " << duration <<std::endl;
*/