-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulate_GW.py
674 lines (599 loc) · 25.4 KB
/
Simulate_GW.py
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
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 29 15:23:05 2018
@author: qijingzhao
"""
__package__ = 'qcosmc'
import numpy as np
from .cos_models import *
from scipy.integrate import quad
from scipy.interpolate import InterpolatedUnivariateSpline
import scipy.constants as sc
import scipy.stats as stats
import pandas as pd
import matplotlib.pyplot as plt
import os
from .tools import simp_err,savetxt
from astroML.density_estimation import FunctionDistribution
# np.random.seed(6)
dataDir=os.path.dirname(os.path.abspath(__file__))+'/data/'
#====================constants=================================
Mpc=sc.parsec*1e6
Msun=1.98847*1e30
#--------------------------------------------------------------
f0=200.0;S0=1.449*1e-52
p1,p2=-4.05,-0.69;a1,a2=185.62,232.56
b1,b2,b3,b4,b5,b6=31.18,-64.72,52.24,-42.16,10.17,11.53
c1,c2,c3,c4=13.58,-36.46,18.56,27.43
f_lower=1.0
#-------------------------------------------------------------
#==============================================================
class ET(object):
def __init__(self,model_name='LCDM', params=[0.315,0.674],GW_type='NSNS'):
self.ll=globals().get('%s'%model_name)(*params)
self._GW_type=GW_type
self._gw_choice()
def _gw_choice(self):
if self._GW_type=='BHNS':
print('The type of GW events you choose is %s'%self._GW_type)
elif self._GW_type=='NSNS':
print('The type of GW events you choose is %s'%self._GW_type)
elif self._GW_type=='BHBH':
print('The type of GW events you choose is %s'%self._GW_type)
elif 0<self._GW_type<1:
print('The ratio between BHNS and BNS events = %s'%self._GW_type)
else:
raise NameError('The type of GW your choice is wrong!\n\n\
Please choose from ["BHNS","NSNS","BHBH"]')
#=====================================================
#=====================================================
@staticmethod
def F1plus(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*phi)*np.cos(2*psi)/2.0
b=np.cos(theta)*np.sin(2*phi)*np.sin(2*psi)
return np.sqrt(3)/2.0*(a-b)
@staticmethod
def F1mul(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*phi)*np.sin(2*psi)/2.0
b=np.cos(theta)*np.sin(2*phi)*np.cos(2*psi)
return np.sqrt(3)/2.0*(a+b)
@staticmethod
def F2plus(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*(phi+2*np.pi/3))*np.cos(2*psi)/2.0
b=np.cos(theta)*np.sin(2*(phi+2*np.pi/3))*np.sin(2*psi)
return np.sqrt(3)/2.0*(a-b)
@staticmethod
def F2mul(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*(phi+2*np.pi/3))*np.sin(2*psi)/2.0
b=np.cos(theta)*np.sin(2*(phi+2*np.pi/3))*np.cos(2*psi)
return np.sqrt(3)/2.0*(a+b)
@staticmethod
def F3plus(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*(phi+4*np.pi/3))*np.cos(2*psi)/2.0
b=np.cos(theta)*np.sin(2*(phi+4*np.pi/3))*np.sin(2*psi)
return np.sqrt(3)/2.0*(a-b)
@staticmethod
def F3mul(theta,phi,psi):
a=(1+np.cos(theta)**2)*np.cos(2*(phi+4*np.pi/3))*np.sin(2*psi)/2.0
b=np.cos(theta)*np.sin(2*(phi+4*np.pi/3))*np.cos(2*psi)
return np.sqrt(3)/2.0*(a+b)
@staticmethod
def Sh(f):
x=f/f0
fz=1+b1*x+b2*x**2+b3*x**3+b4*x**4+b5*x**5+b6*x**6
fm=1+c1*x+c2*x**2+c3*x**3+c4*x**4
return S0*(x**p1+a1*x**p2+a2*fz/fm)
def Int_sh(self,f):
return f**(-7/3)/self.Sh(f)
def plotSh(self,ls='-'):
f_upper=2e3
ff=np.linspace(f_lower, f_upper,10000)
plt.plot(ff,np.sqrt(self.Sh(ff)),ls=ls,label='ET')
plt.yscale('log')
plt.xscale('log')
plt.xlabel('$f~[\mathrm{Hz}]$')
plt.ylabel('$\sqrt{S_h(f)}~[\mathrm{Hz^{-1/2}}]$')
plt.legend(frameon=False)
# =============================================================================
#=============================================================================
def __snr(self,z,event='BHNS'):
if event=='BHNS':
m1_range=[1,2]
m2_range=[3,10]
elif event=='NSNS':
m1_range=[1,2]
m2_range=[1,2]
elif event=='BHBH':
m1_range=[3,10]
m2_range=[3,10]
rho=0.0
while rho<=8.0:
m1=np.random.uniform(m1_range[0],m1_range[1])*Msun
m2=np.random.uniform(m2_range[0],m2_range[1])*Msun
M=m1+m2
eta=m1*m2/M**2
Mc_phys=M*eta**(3.0/5.0)
Mc_obs=(1+z)*Mc_phys
theta=np.random.uniform(0,np.pi)
phi=np.random.uniform(0,2*np.pi)
iota=0.0
psi=np.pi/4
M_obs=(1+z)*(m1+m2)
f_upper=2/np.power(6,1.5)/2/np.pi/M_obs/sc.G*sc.c**3
A1=np.sqrt(self.F1plus(theta,phi,psi)**2*(1+np.cos(iota)**2)**2+4*self.F1mul(theta,phi,psi)**2*
np.cos(iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc*\
np.power(sc.G,5/6)*np.power(sc.c,-1.5)
A2=np.sqrt(self.F2plus(theta,phi,psi)**2*(1+np.cos(iota)**2)**2+4*self.F2mul(theta,phi,psi)**2*
np.cos(iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc*\
np.power(sc.G,5/6)*np.power(sc.c,-1.5)
A3=np.sqrt(self.F3plus(theta,phi,psi)**2*(1+np.cos(iota)**2)**2+4*self.F3mul(theta,phi,psi)**2*
np.cos(iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc*\
np.power(sc.G,5/6)*np.power(sc.c,-1.5)
ET1=4*quad(self.Int_sh,f_lower,f_upper)[0]*A1**2
# ET1=2*np.sqrt(sc.G**(5/3)*sc.c**(-3)*A1**2*Nconst)
ET2=4*quad(self.Int_sh,f_lower,f_upper)[0]*A2**2
ET3=4*quad(self.Int_sh,f_lower,f_upper)[0]*A3**2
rho=np.sqrt(ET1+ET2+ET3)
# self.count+=1
# print(self.count)
return z,m1/Msun,m2/Msun,theta,phi,rho
#====================分布函数=================================
def Pz(self,z):
def Rz(z):
if z<=1:
r=1.+2.*z
elif 1<z<5:
r=3.*(5.-z)/4.
else:
r=0.0
return r
R=np.vectorize(Rz)
return 4.*np.pi*self.ll.d_z(z)**2*R(z)/self.ll.hubz(z)/(1.0+z)
def ET_z(self,zz,rand='normal'):
Dd=np.zeros(len(zz))-1
while (Dd<=0).any():
if type(self._GW_type)==float:
N=len(zz)
n_BHNS=round(self._GW_type*N)
index_BHNS=np.random.choice(np.where(zz<5)[0],n_BHNS, replace=False)
BNS_zz=np.delete(zz,index_BHNS)
print('The number of BHNS events is %s.'%n_BHNS)
print('The number of BNS events is %s.'%len(BNS_zz))
zh,m1h,m2h,thetah,phih,rhoh=np.vectorize(self.__snr)(zz[index_BHNS],event='BHNS')
zn,m1n,m2n,thetan,phin,rhon=np.vectorize(self.__snr)(BNS_zz,event='NSNS')
self.z=np.append(zh,zn)
self.m1=np.append(m1h,m1n)
self.m2=np.append(m2h,m2n)
self.theta=np.append(thetah,thetan)
self.phi=np.append(phih,phin)
self.rho=np.append(rhoh,rhon)
else:
self.z,self.m1,self.m2,self.theta,self.phi,self.rho=np.vectorize(self.__snr)(zz,self._GW_type)
DL=self.ll.lum_dis_z(self.z)
DL_err=np.sqrt((2.*DL/self.rho)**2+(0.05*self.z*DL)**2)
if rand=='normal':
DL_mean=np.random.normal(DL,DL_err)
elif rand=='1sigma':
DL_mean=stats.truncnorm(-1,1,loc=DL,scale=DL_err).rvs()
elif rand=='None':
DL_mean=DL
else:
print('The input of random is wrong.')
Dd=DL_mean-DL_err
self.DL=DL_mean
self.DL_err = DL_err
return self.z ,DL_mean,DL_err
def GW_DL(self,zlow=0,zup=2,num=1000,rand='normal'):
# self.count=0
# if type(self._GW_type)==str:
# if zup>self.z_max: zup=self.z_max
zzn=FunctionDistribution(self.Pz,zlow,zup,num*2).rvs(num)
zz,DL_mean,DL_err = self.ET_z(zzn,rand)
return zz,DL_mean,DL_err
def save_fulldata(self,path_file_name):
st=['#z','m1','m2','theta','phi','snr']
try:
if self.z.any():
data=(self.z,self.m1,self.m2,self.theta,self.phi,self.rho)
dc=dict(zip(st,data))
df=pd.DataFrame(dc)
if path_file_name[-3:]=='lsx':
df.to_excel(path_file_name,index=False)
elif path_file_name[-3:]=='txt':
df.to_csv(path_file_name,index=False,sep=' ')
else:
df.to_excel(path_file_name+'.xlsx',index=False)
except AttributeError:
print('Please run the GW_z function firstly!')
def save_DL(self,path_file_name):
# st=['#z','DL','DL_err']
try:
if self.z.any():
savetxt(path_file_name,[self.z,self.DL,self.DL_err])
# data=(self.z,self.DL,self.DL_err)
## dc=dict(zip(st,data))
# dc=dict(data)
# df=pd.DataFrame(dc)
# if path_file_name[-3:]=='lsx':
# df.to_excel(path_file_name,index=False)
# elif path_file_name[-3:]=='txt':
# df.to_csv(path_file_name,index=False,sep=' ')
# else:
# df.to_csv(path_file_name,index=False,sep=' ')
except AttributeError:
print('Please run the GW_z function firstly!')
class LIGO(object):
def __init__(self,Gam,Lam,ll,kesi=np.pi/2,psi=np.pi/4,iota=0):
self.Gam = Gam
self.Lam = Lam
self.kesi = kesi
self.psi = psi
self.iota = iota
self.ll=ll
def at(self,alpha,delta,ang):
Gam,Lam = self.Gam,self.Lam
a=np.sin(2*Gam)*(3-np.cos(2*Lam))*(3-np.cos(2*delta))*np.cos(2*(alpha-ang))/16
b=np.cos(2*Gam)*np.sin(Lam)*(3-np.cos(2*delta))*np.sin(2*(alpha-ang))/4
c=np.sin(2*Gam)*np.sin(2*Lam)*np.sin(2*delta)*np.cos(alpha-ang)/4
d=np.cos(2*Gam)*np.cos(Lam)*np.sin(2*delta)*np.sin(alpha-ang)/2
e=np.sin(2*Gam)*np.cos(Lam)**2*np.cos(delta)**2*3/4
return a-b+c-d+e
def bt(self,alpha,delta,ang):
Gam,Lam = self.Gam,self.Lam
a=np.cos(2*Gam)*np.sin(Lam)*np.sin(delta)*np.cos(2*(alpha-ang))
b=np.sin(2*Gam)*(3-np.cos(2*Lam))*np.sin(delta)*np.sin(2*(alpha-ang))/4
c=np.cos(2*Gam)*np.cos(Lam)*np.cos(delta)*np.cos(alpha-ang)
d=np.sin(2*Gam)*np.sin(2*Lam)*np.cos(delta)*np.sin(alpha-ang)/2
return a+b+c+d
def Fplus(self,alpha,delta,ang):
kesi = self.kesi
psi = self.psi
return np.sin(kesi)*(self.at(alpha,delta,ang)*np.cos(2*psi)+
self.bt(alpha,delta,ang)*np.sin(2*psi))
def Fmul(self,alpha,delta,ang):
kesi = self.kesi
psi = self.psi
return np.sin(kesi)*(self.bt(alpha,delta,ang)*np.cos(2*psi)-
self.at(alpha,delta,ang)*np.sin(2*psi))
def AA(self,alpha,delta,ang,Mc_obs,z):
return np.sqrt(self.Fplus(alpha,delta,ang)**2*(1+np.cos(self.iota)**2)**2+4*self.Fmul(alpha,delta,ang)**2*
np.cos(self.iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc
@staticmethod
def Sh(f):
f0,S0=215,1e-49
x=f/f0
return S0*(x**(-4.14)-5*x**(-2)+111*(1-x**2+0.5*x**4)/(1+0.5*x**2))
def plotSh(self,ls='-'):
ff=np.linspace(10, 2e3,10000)
plt.plot(ff,np.sqrt(self.Sh(ff)),ls=ls,label='LIGO')
plt.yscale('log')
plt.xscale('log')
plt.xlabel('$f~[Hz]$')
plt.ylabel('$\sqrt{S_h(f)}~[Hz^{-1/2}]$')
plt.legend(frameon=False)
def Ncont(self):
Nint=lambda f:f**(-7/3)/self.Sh(f)
return quad(Nint,20,2000)[0]
def snr(self,alpha,delta,ang,Mc_obs,z):
return 2*np.sqrt(sc.G**(5/3)*sc.c**(-3)*self.AA(alpha,delta,ang,Mc_obs,z)**2*self.Ncont())
class Virgo(LIGO):
def __init__(self,Gam,Lam,ll,kesi=np.pi/2,psi=np.pi/4,iota=0):
self.Gam = Gam
self.Lam = Lam
self.psi = psi
self.kesi = kesi
self.iota = iota
self.ll=ll
@staticmethod
def Sh(f):
f0,S0=720,1e-47
x=f/f0
a=np.log(x)**2*(3.2+1.08*np.log(x)+0.13*np.log(x)**2)
b=0.73*np.log(x)**2
c=2.67e-7*x**(-5.6)+0.59*x**(-4.1)*np.exp(-a)+0.68*x**(5.34)*np.exp(-b)
return S0*c
class LgVg(object):
def __init__(self,model_name='LCDM', params=[0.308,0.678],m1_range=[3,100],m2_range=[3,100]):
self.ll=globals().get('%s'%model_name)(*params)
# self._GW_type=GW_type
# self._gw_choice()
self.model_name=model_name
self.params=params
self._m1_range=m1_range
self._m2_range=m2_range
# def _gw_choice(self):
# if self._GW_type=='BHNS':
# self._m1_range=[1,2]
# self._m2_range=[3,10]
# elif self._GW_type=='NSNS':
# self._m1_range=[1,2]
# self._m2_range=[1,2]
# elif self._GW_type=='BHBH':
# self._m1_range=[3,10]
# self._m2_range=[3,10]
# else:
# raise NameError('The type of GW your choice is wrong!\n\n\
# Please choose from ["BHNS","NSNS","BHBH"]')
# =============================================================================
#=============================================================================
def __snr(self,z):
H1=LIGO(171.8*np.pi/180,46.45*np.pi/180,self.ll)
H1longitude = (119+24/60+27.6/60**2)*np.pi/180
L1=LIGO(243*np.pi/180,30.56*np.pi/180,self.ll)
L1longitude = (90+46/60+27.3/60**2)*np.pi/180
V1=Virgo(116.5*np.pi/180,43.63*np.pi/180,self.ll)
V1longitude = (10+30/60+16/60**2)*np.pi/180
snrH1,snrL1,snrV1=0.0,0.0,0.0
mr=0
while snrH1<=8.0 or snrL1<=8.0 or snrV1<=8.0 or mr<0.5 or mr>2:
m1=np.random.uniform(self._m1_range[0],self._m1_range[1])*Msun
m2=np.random.uniform(self._m2_range[0],self._m2_range[1])*Msun
M=m1+m2
eta=m1*m2/M**2
Mc_phys=M*eta**(3.0/5.0)
Mc_obs=(1+z)*Mc_phys
mr=m1/m2
alpha=np.random.uniform(0,np.pi)
delta=np.random.uniform(0,2*np.pi)
ang=np.random.uniform(0,2*np.pi)
snrH1=H1.snr(alpha,delta,ang,Mc_obs,z)
L1_ang=ang+(L1longitude-H1longitude)
snrL1=L1.snr(alpha,delta,L1_ang,Mc_obs,z)
V1_ang=ang-(V1longitude+H1longitude)
snrV1=V1.snr(alpha,delta,V1_ang,Mc_obs,z)
rho=np.sqrt(snrH1**2+snrL1**2+snrV1**2)
self.count+=1
# print(self.count)
return z,m1/Msun,m2/Msun,alpha,delta,snrH1,snrL1,snrV1,rho
#====================分布函数=================================
def Pz(self,z):
def Rz(z):
if z<=1:
r=1.+2.*z
elif 1<z<5:
r=3.*(5.-z)/4.
else:
r=0.0
return r
R=np.vectorize(Rz)
return 4.*np.pi*self.ll.d_z(z)**2*R(z)/self.ll.hubz(z)/(1.0+z)
def GW_z(self,zz,rand='normal'):
self.count=0
self.z,self.m1,self.m2,self.alpha,self.delta,self.snrH1,self.snrL1,self.snrV1,self.rho\
=np.vectorize(self.__snr)(zz)
DL=np.vectorize(self.ll.lum_dis_z)(zz)
DL_err=np.sqrt((2.*DL/self.rho)**2+(0.05*zz*DL)**2)
if rand=='normal':
DL_mean=np.random.normal(DL,DL_err)
elif rand=='1sigma':
DL_mean=stats.truncnorm(-1,1,loc=DL,scale=DL_err).rvs()
elif rand=='None':
DL_mean=DL
else:
print('The input of random is wrong.')
self.DL=DL_mean
self.DL_err=DL_err
return DL_mean,DL_err
def save_fulldata(self,path_file_name):
st=['#z','m1','m2','alpha','delta','snrH1','snrL1','snrV','snr']
try:
if self.z.any():
data=(self.z,self.m1,self.m2,self.alpha,self.delta,self.snrH1,self.snrL1,self.snrV1,self.rho)
dc=dict(zip(st,data))
df=pd.DataFrame(dc)
if path_file_name[-3:]=='lsx':
df.to_excel(path_file_name,index=False)
elif path_file_name[-3:]=='txt':
df.to_csv(path_file_name,index=False,sep=' ')
else:
df.to_excel(path_file_name+'.xlsx',index=False)
except AttributeError:
print('Please run the GW_z function firstly!')
def save_DL(self,path_file_name):
st=['#z','DL','DL_err']
try:
if self.z.any():
data=(self.z,self.DL,self.DL_err)
dc=dict(zip(st,data))
df=pd.DataFrame(dc)
if path_file_name[-3:]=='lsx':
df.to_excel(path_file_name,index=False)
elif path_file_name[-3:]=='txt':
df.to_csv(path_file_name,index=False,sep=' ')
else:
df.to_csv(path_file_name,index=False,sep=' ')
except AttributeError:
print('Please run the GW_z function firstly!')
def Ligo_default(self,zlow=0.0,zup=1.0,num=800,rand='normal'):
zzn=zzn=FunctionDistribution(self.Pz,zlow,zup,num).rvs(num)
DL,DL_s=self.GW_z(zzn,rand)
return zzn,DL,DL_s
class DECIGO(object):
def __init__(self,model_name='LCDM', params=[0.315,0.674],T=1):
self.ll=globals().get('%s'%model_name)(*params)
self.Tobs=T
@staticmethod
def Sh2(f):
'''
Physical Review D 95, 109901(E) 2017
'''
fp=7.36
a=7.05*1e-48*(1+(f/fp)**2)
b=4.8*1e-51*f**(-4)/(1+(f/fp)**2)
c=5.33*1e-52*f**(-4)
return a+b+c
@staticmethod
def Sh(f):
'''
Physical Review D 83,044011 2011
'''
fp=7.36
a=6.53*1e-49*(1+(f/fp)**2)
b=4.45*1e-51*f**(-4)/(1+(f/fp)**2)
c=4.94*1e-52*f**(-4)
return a+b+c
def plotSh(self,ls='-'):
ff=np.linspace(1e-3,1e2,10000)
plt.plot(ff,np.sqrt(self.Sh(ff)),ls=ls,label='DECIGO')
plt.yscale('log')
plt.xscale('log')
plt.xlabel('$f~[\mathrm{Hz}]$')
plt.ylabel('$\sqrt{S_h(f)}~[\mathrm{Hz^{-1/2}}]$')
plt.legend(frameon=False)
@staticmethod
def t_f_normal(f,m1,m2,z):
'''
Physical Review D 83,044011 2011
Equation (30)
'''
M=m1+m2
eta=m1*m2/M**2
Mc_phys=M*eta**(3.0/5.0)
Mc_obs=(1+z)*Mc_phys
x=np.power(np.pi*(1+z)*M*f,2/3)
tc=0
a=5/256*Mc_obs*(np.pi*Mc_obs*f)**(-8/3)
b=(1+4/3*(743/336+11/4*eta))*x-32/5*np.pi*np.power(x,1.5)
c=2*(3058673/1016064+5429/1008*eta+617/144*eta**2)*x**2
return tc-a*(b+c)
@staticmethod
def t_f(f,m1,m2,z):
'''
Physical Review D 83,044011 2011
Equation (31)
'''
M=m1+m2
eta=m1*m2/M**2
Mc_phys=M*eta**(3.0/5.0)
return 1.04*1e7*np.power((1+z)/2,-5/3)*np.power(Mc_phys/1.22/Msun,-5/3)*np.power(f/0.2,-8/3)
def _F1plus(self,cos_theta,phi,psi):
a=(1+cos_theta**2)*np.cos(2*phi)*np.cos(2*psi)/2.0
b=cos_theta*np.sin(2*phi)*np.sin(2*psi)
return a-b
def _F1mul(self,cos_theta,phi,psi):
a=(1+cos_theta**2)*np.cos(2*phi)*np.sin(2*psi)/2.0
b=cos_theta*np.sin(2*phi)*np.cos(2*psi)
return a+b
def _F2plus(self,theta,phi,psi):
return self._F1plus(theta,phi-np.pi/4,psi)
def _F2mul(self,theta,phi,psi):
return self._F1mul(theta,phi-np.pi/4,psi)
def Int_sh(self,f):
return f**(-7/3)/self.Sh(f)
# =============================================================================
#=============================================================================
def __snr(self,z):
rho=0.0
while rho<=8.0:
# m1=np.random.uniform(m1_range[0],m1_range[1])*Msun
# m2=np.random.uniform(m2_range[0],m2_range[1])*Msun
m1=m2=1.4*Msun
M=m1+m2
eta=m1*m2/M**2
Mc_phys=M*eta**(3.0/5.0)
Mc_obs=(1+z)*Mc_phys
theta=np.random.uniform(0,np.pi)
phi=np.random.uniform(0,2*np.pi)
iota=0.0
psi=np.pi/4
f_lower=0.233*np.power(Msun/Mc_obs,5/8)*np.power(1/self.Tobs,3/8) #Physical Review D 83,084045 2011 Eq.(15)
f_upper=100
t=self.t_f(f_lower,m1,m2,z)-self.t_f(f_upper,m1,m2,z)
phi_t=2*np.pi*t/sc.year
cosTS=np.cos(theta)/2-np.sqrt(3)/2*np.sin(theta)*np.cos(phi_t-phi) #Physical Review D 83,084045 2011 Eq.(A1)
phiSt=np.pi/12+np.arctan((np.sqrt(3)*np.cos(theta)+np.sin(theta)*np.cos(phi_t-phi))/(2*np.sin(theta)*np.sin(phi_t-phi))) #Physical Review D 83,084045 2011 Eq.(A2)
A1=np.sqrt(self._F1plus(cosTS,phiSt,psi)**2*(1+np.cos(iota)**2)**2+4*self._F1mul(cosTS,phiSt,psi)**2*
np.cos(iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc*\
np.power(sc.G,5/6)*sc.c**(-1.5)
A2=np.sqrt(self._F2plus(cosTS,phiSt,psi)**2*(1+np.cos(iota)**2)**2+4*self._F2mul(cosTS,phiSt,psi)**2*
np.cos(iota)**2)*np.sqrt(5*np.pi/96)*np.pi**(-7/6)*Mc_obs**(5/6)/self.ll.lum_dis_z(z)/Mpc*\
np.power(sc.G,5/6)*sc.c**(-1.5)
ET1=4*quad(self.Int_sh,f_lower,f_upper)[0]*A1**2
# ET1=2*np.sqrt(sc.G**(5/3)*sc.c**(-3)*A1**2*Nconst)
ET2=4*quad(self.Int_sh,f_lower,f_upper)[0]*A2**2
rho=np.sqrt(ET1+ET2)
# self.count+=1
# print(self.count)
return z,m1/Msun,m2/Msun,theta,phi,rho
#====================分布函数=================================
def Pz(self,z):
def Rz(z):
if z<=1:
r=1.+2.*z
elif 1<z<5:
r=3.*(5.-z)/4.
else:
r=0.0
return r
R=np.vectorize(Rz)
return 4.*np.pi*self.ll.d_z(z)**2*R(z)/self.ll.hubz(z)/(1.0+z)
def GWz(self,zz,rand='normal'):
Dd=np.zeros(len(zz))-1
while (Dd<=0).any():
self.z,self.m1,self.m2,self.theta,self.phi,self.rho=np.vectorize(self.__snr)(zz)
DL=self.ll.lum_dis_z(self.z)
self.inst=DL/self.rho
self.lens=DL*0.066*np.power((1-(1+self.z)**(-0.25))/0.25,1.8)
self.pv=DL*np.abs(1/sc.c*1e3-(1+self.z)**2/self.ll.Hz(self.z)/DL)*300
DL_err=np.sqrt((self.inst)**2+(self.lens)**2+(self.pv)**2)
# DL_err=np.sqrt((DL/self.rho)**2+(DL*0.066*np.power((1-(1+self.z)**(-0.25))/0.25,1.8))**2+
# (DL*np.abs(1/sc.c*1e3-(1+self.z)**2/self.ll.Hz(self.z)/DL)*300)**2)
if rand=='normal':
DL_mean=np.random.normal(DL,DL_err)
elif rand=='1sigma':
DL_mean=stats.truncnorm(-1,1,loc=DL,scale=DL_err).rvs()
elif rand=='None':
DL_mean=DL
else:
print('The input of random is wrong.')
Dd=DL_mean-DL_err
self.DL=DL_mean
self.DL_err = DL_err
return self.z ,DL_mean,DL_err
def GW_DL(self,zlow=0,zup=5,num=1000,rand='normal'):
# self.count=0
# if type(self._GW_type)==str:
# if zup>self.z_max: zup=self.z_max
z,snh,snl=np.loadtxt(dataDir+'NSNSrates.dat',usecols=(1, 2,3),skiprows=1,unpack=True)
Pz=InterpolatedUnivariateSpline(z[::-1],snh[::-1])
zzn=FunctionDistribution(Pz,zlow,zup,num).rvs(num)
zz,DL_mean,DL_err = self.GWz(zzn,rand)
return zz,DL_mean,DL_err
def Hz(self):
v0=369.1
dL1=v0*(1+self.z)**2/self.ll.Hz(self.z)
Hz_s=self.ll.Hz(self.z)*np.sqrt(3)*(self.DL_err/self.DL)/(dL1/self.DL)
Hz=self.ll.Hz(self.z)
return self.z,Hz,Hz_s
def save_fulldata(self,path_file_name):
st=['#z','m1','m2','theta','phi','snr']
try:
if self.z.any():
data=(self.z,self.m1,self.m2,self.theta,self.phi,self.rho)
dc=dict(zip(st,data))
df=pd.DataFrame(dc)
if path_file_name[-3:]=='lsx':
df.to_excel(path_file_name,index=False)
elif path_file_name[-3:]=='txt':
df.to_csv(path_file_name,index=False,sep=' ')
else:
df.to_excel(path_file_name+'.xlsx',index=False)
except AttributeError:
print('Please run the GW_z function firstly!')
def save_DL(self,path_file_name):
# st=['#z','DL','DL_err']
try:
if self.z.any():
savetxt(path_file_name,[self.z,self.DL,self.DL_err])
# data=(self.z,self.DL,self.DL_err)
## dc=dict(zip(st,data))
# dc=dict(data)
# df=pd.DataFrame(dc)
# if path_file_name[-3:]=='lsx':
# df.to_excel(path_file_name,index=False)
# elif path_file_name[-3:]=='txt':
# df.to_csv(path_file_name,index=False,sep=' ')
# else:
# df.to_csv(path_file_name,index=False,sep=' ')
except AttributeError:
print('Please run the GW_z function firstly!')