GWFIT 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gwfit/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """GWFIT: Gravitational Waveform Integration Tools."""
2
+
3
+ __version__ = "0.1.0"
@@ -0,0 +1 @@
1
+ """Legacy GWFIT implementations."""
@@ -0,0 +1,182 @@
1
+ import numpy as np
2
+ from ..physics.constants import MTsun, time_fac, strain_fac
3
+ from ..waveforms import waveform_functions as wfn
4
+ from pycbc import types, waveform, fft
5
+ from ..waveforms import inspiral_model as insp
6
+
7
+ def inspiral_TH(f_ini=10.,f_final=None, delta_t=None, m1=10, m2=10, chi1=0, chi2=0, H1=1, H2=1, DL=200, tc=0, phic=0, iota=0, amp_phase_only=False, amp_phase_hp_hc=False,approximant='SEOBNRv2',TH1=False,TH2=False,amp_corr=False):
8
+
9
+ # convert to sec
10
+ Ms = m1+m2
11
+ M = Ms * time_fac
12
+ DLt = DL * time_fac/strain_fac
13
+ # f_isco_KBH = wfn.f_isco_Msolar_KBH(m1,m2,chi1,chi2)
14
+
15
+ # if f_final is None: f_final = 0.03/M #f_isco_KBH
16
+ if delta_t is None: raise Exception('Specify delta_t')
17
+
18
+ hps, hcs = waveform.get_td_waveform(approximant=approximant,
19
+ mass1=m1, mass2=m2, spin1z=chi1, spin2z=chi2, distance = DL,
20
+ delta_t=delta_t, f_lower = f_ini-2, f_final = f_final)
21
+
22
+ f0 = waveform.utils.frequency_from_polarizations(hps,hcs)
23
+
24
+ ## Find the TD tapering needed
25
+ index = np.where(f0>f_ini)[0][0]
26
+ tini = hps.sample_times[0]
27
+ tfin = hps.sample_times[index]
28
+
29
+
30
+
31
+ ## Apply tapering to TD data
32
+ hps = waveform.utils.td_taper(hps,tini,tfin)
33
+ hcs = waveform.utils.td_taper(hcs,tini,tfin)
34
+ # if f_final is not None:
35
+ # index2 = np.where(f0>(f_final-2))[0][0]
36
+ # index3 = np.where(f0>f_final)[0][0]
37
+ # tini2 = hps.sample_times[index2]
38
+ # tfin2 = hps.sample_times[index3]
39
+ # hps = waveform.utils.td_taper(hps,tini2,tfin2,side='right')
40
+ # hcs = waveform.utils.td_taper(hcs,tini2,tfin2,side='right')
41
+
42
+ ## Take FFT
43
+ hpstilde = hps.to_frequencyseries()
44
+ hcstilde = hcs.to_frequencyseries()
45
+
46
+ f = hpstilde.sample_frequencies
47
+ index0 = np.where(f>f_ini-2)[0][0]
48
+
49
+ finsp = f[index0:]
50
+ patch = np.zeros(index0)
51
+
52
+ ## Extract amplitude & phase
53
+ amps = waveform.utils.amplitude_from_frequencyseries(hpstilde)
54
+ amps=np.asarray(amps)
55
+ phases = waveform.utils.phase_from_frequencyseries(hpstilde)
56
+ phases=np.asarray(phases)
57
+
58
+ ## Compute TH corrections
59
+ if TH1==1: psi_TH = insp.psiTH(finsp,m1,m2,chi1,chi2,H1,H2)
60
+ if TH2==1: psi_TH = insp.psiTH_new(finsp,m1,m2,chi1,chi2)
61
+ if TH1==0 and TH2==0: psi_TH = np.zeros_like(finsp)
62
+ amp_TH = insp.ampTH(finsp,m1,m2,chi1,chi2,DL,H1,H2)
63
+
64
+ psi_TH = np.concatenate((patch,psi_TH))
65
+ amp_TH = np.concatenate((patch,amp_TH))
66
+
67
+ psi_TH = types.FrequencySeries(psi_TH,delta_f=hpstilde.delta_f)
68
+ amp_TH = types.FrequencySeries(amp_TH,delta_f=hpstilde.delta_f)
69
+
70
+ ## Tapering starts from f_ini-2 to f_ini; ends from f_peak to 1.2f_peak
71
+ psi_TH = waveform.utils.fd_taper(psi_TH,f_ini-2,f_ini)
72
+ amp_TH = waveform.utils.fd_taper(amp_TH,f_ini-2,f_ini)
73
+
74
+ fpeak = wfn.f_peak(m1,m2,chi1,chi2)
75
+ amp_TH = waveform.utils.fd_taper(amp_TH,fpeak,1.2*fpeak,side='right')
76
+
77
+ if amp_corr==True:
78
+ hptilde = 0.5*(1+np.cos(iota)**2) * (amps+amp_TH) * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
79
+ hctilde = -1j*np.cos(iota) * (amps+amp_TH) * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
80
+ else:
81
+ hptilde = 0.5*(1+np.cos(iota)**2) * amps * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
82
+ hctilde = -1j*np.cos(iota) * amps * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
83
+
84
+ hptilde = types.FrequencySeries(hptilde,delta_f=hpstilde.delta_f)
85
+ hctilde = types.FrequencySeries(hctilde,delta_f=hpstilde.delta_f)
86
+
87
+ hp = hptilde.to_timeseries(delta_t=delta_t)
88
+ hc = hctilde.to_timeseries(delta_t=delta_t)
89
+
90
+ hp.start_time = 0
91
+ hc.start_time = 0
92
+
93
+ return hp, hc
94
+
95
+
96
+ def inspiral_TH_fd(f_ini=10.,f_final=None, delta_t=None, m1=10, m2=10, chi1=0, chi2=0, H1=1, H2=1, DL=200, tc=0, phic=0, iota=0, amp_phase_only=False, amp_phase_hp_hc=False,approximant='SEOBNRv2',TH1=True,TH2=False,amp_corr=True):
97
+
98
+ # convert to sec
99
+ Ms = m1+m2
100
+ M = Ms * time_fac
101
+ DLt = DL * time_fac/strain_fac
102
+ # f_isco_KBH = wfn.f_isco_Msolar_KBH(m1,m2,chi1,chi2)
103
+
104
+ # if f_final is None: f_final = 0.03/M #f_isco_KBH
105
+ if delta_t is None: raise Exception('Specify delta_t')
106
+
107
+ hps, hcs = waveform.get_td_waveform(approximant=approximant,
108
+ mass1=m1, mass2=m2, spin1z=chi1, spin2z=chi2, distance = DL,
109
+ delta_t=delta_t, f_lower = f_ini-2, f_final = f_final)
110
+
111
+ f0 = waveform.utils.frequency_from_polarizations(hps,hcs)
112
+
113
+ ## Find the TD tapering needed
114
+ index = np.where(f0>f_ini)[0][0]
115
+ tini = hps.sample_times[0]
116
+ tfin = hps.sample_times[index]
117
+
118
+
119
+
120
+ ## Apply tapering to TD data
121
+ hps = waveform.utils.td_taper(hps,tini,tfin)
122
+ hcs = waveform.utils.td_taper(hcs,tini,tfin)
123
+ if f_final is not None:
124
+ index2 = np.where(f0>(f_final-2))[0][0]
125
+ index3 = np.where(f0>f_final)[0][0]
126
+ tini2 = hps.sample_times[index2]
127
+ tfin2 = hps.sample_times[index3]
128
+ hps = waveform.utils.td_taper(hps,tini2,tfin2,side='right')
129
+ hcs = waveform.utils.td_taper(hcs,tini2,tfin2,side='right')
130
+
131
+ ## Take FFT
132
+ hpstilde = hps.to_frequencyseries()
133
+ hcstilde = hcs.to_frequencyseries()
134
+
135
+ f = hpstilde.sample_frequencies
136
+ index0 = np.where(f>f_ini-2)[0][0]
137
+
138
+ finsp = f[index0:]
139
+ patch = np.zeros(index0)
140
+
141
+ ## Extract amplitude & phase
142
+ amps = waveform.utils.amplitude_from_frequencyseries(hpstilde)
143
+ amps=np.asarray(amps)
144
+ phases = waveform.utils.phase_from_frequencyseries(hpstilde)
145
+ phases=np.asarray(phases)
146
+
147
+ ## Compute TH corrections
148
+ if TH1==True: psi_TH = insp.psiTH(finsp,m1,m2,chi1,chi2,H1,H2)
149
+ if TH2==True: psi_TH = insp.psiTH_new(finsp,m1,m2,chi1,chi2,H1,H2)
150
+ else: psi_TH = np.zeros_like(finsp)
151
+ amp_TH = insp.ampTH(finsp,m1,m2,chi1,chi2,DL,H1,H2)
152
+
153
+ psi_TH = np.concatenate((patch,psi_TH))
154
+ amp_TH = np.concatenate((patch,amp_TH))
155
+
156
+ psi_TH = types.FrequencySeries(psi_TH,delta_f=hpstilde.delta_f)
157
+ amp_TH = types.FrequencySeries(amp_TH,delta_f=hpstilde.delta_f)
158
+
159
+ ## Tapering starts from f_ini-2 to f_ini; ends from f_peak to 1.2f_peak
160
+ psi_TH = waveform.utils.fd_taper(psi_TH,f_ini-2,f_ini)
161
+ amp_TH = waveform.utils.fd_taper(amp_TH,f_ini-2,f_ini)
162
+
163
+ fpeak = wfn.f_peak(m1,m2,chi1,chi2)
164
+ amp_TH = waveform.utils.fd_taper(amp_TH,fpeak,1.2*fpeak,side='right')
165
+
166
+ ## Add TH corrections
167
+ if TH1 or TH2 is True:
168
+ if amp_corr==True:
169
+ hptilde = 0.5*(1+np.cos(iota)**2) * (amps+amp_TH) * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
170
+ hctilde = -1j*np.cos(iota) * (amps+amp_TH) * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
171
+ else:
172
+ hptilde = 0.5*(1+np.cos(iota)**2) * amps * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
173
+ hctilde = -1j*np.cos(iota) * amps * np.exp(1j*(phases + psi_TH + 2.*np.pi*f*tc + phic))
174
+ else:
175
+ hptilde = 0.5*(1+np.cos(iota)**2)*hpstilde
176
+ hctilde = -1j*np.cos(iota)*hpstilde
177
+
178
+ hptilde = types.FrequencySeries(hptilde,delta_f=hpstilde.delta_f)
179
+ hctilde = types.FrequencySeries(hctilde,delta_f=hpstilde.delta_f)
180
+
181
+
182
+ return hptilde, hctilde
@@ -0,0 +1,294 @@
1
+ # Copyright (c) 2026 Samanwaya Mukherjee
2
+
3
+ import sxs
4
+ import numpy as np
5
+ import h5py as h5
6
+ from ..physics import useful_relations as urel
7
+ from scipy.interpolate import interp1d
8
+
9
+
10
+ def nr_resampled(path_to_file,Ms,DL=200,merger_time=None,time_fix=False,fine_resample=False,geo_units=False):
11
+
12
+
13
+ M = Ms*urel.MTsun
14
+ MbyDL = (Ms/DL)*urel.strain_fac
15
+
16
+ # f = h5.File(path_to_file,'r')
17
+ # dataset = f['Extrapolated_N2.dir']
18
+ # data = dataset['Y_l2_m2.dat']
19
+
20
+ with h5.File(path_to_file, "r") as f:
21
+ data = f["Extrapolated_N2.dir"]["Y_l2_m2.dat"][:]
22
+
23
+ time0 = data[:,0]
24
+ strainp0 = data[:,1]
25
+ strainc0 = -data[:,2]
26
+
27
+ time = time0*M
28
+ strainp, strainc = strainp0*MbyDL, strainc0*MbyDL
29
+
30
+
31
+ ### DEFINING THE CHIRP TIME AS 0 BY FINDING THE MAXIMUM STRAIN ####
32
+ if time_fix==True:
33
+ if merger_time is None:
34
+ j = np.argmax(strainp)
35
+ maximt = time[j]
36
+
37
+ else: maximt = merger_time*M
38
+
39
+ time = time - maximt
40
+
41
+ flinp = interp1d(time,strainp,kind='cubic')
42
+ flinc = interp1d(time,strainc,kind='cubic')
43
+
44
+
45
+ # finding maximum and minimum time spacing ---------
46
+ deltat=[]
47
+ for i in range(len(time)-1):
48
+ deltat.append(time[i+1]-time[i])
49
+
50
+ deltat.append(deltat[-1])
51
+
52
+ coarse_dt, fine_dt = max(deltat), min(deltat)
53
+ #--------------------------------------------------
54
+
55
+ if fine_resample == True:
56
+ dt = fine_dt
57
+ else: dt = coarse_dt
58
+
59
+ time_resamp = np.arange(time[0],time[-1],dt)
60
+ strainp_resamp = flinp(time_resamp)
61
+ strainc_resamp = flinc(time_resamp)
62
+
63
+ if geo_units == True: return time0, strainp0, strainc0, time_resamp, strainp_resamp, strainc_resamp
64
+ return time_resamp, strainp_resamp, strainc_resamp
65
+
66
+ def nr_resampled_geo(path_to_file,merger_time=None,fine_resample=False):
67
+
68
+ # f = h5.File(path_to_file,'r')
69
+ # dataset = f['Extrapolated_N2.dir']
70
+ # data = dataset['Y_l2_m2.dat']
71
+
72
+ with h5.File(path_to_file, "r") as f:
73
+ data = f["Extrapolated_N2.dir"]["Y_l2_m2.dat"][:]
74
+
75
+ time = data[:,0]
76
+ strainp = data[:,1]
77
+ strainc = -data[:,2]
78
+
79
+
80
+ ### DEFINING THE CHIRP TIME AS 0 BY FINDING THE MAXIMUM STRAIN ####
81
+
82
+ if merger_time is None:
83
+
84
+ maximstr = strainp[0]
85
+
86
+ for i in range(len(time)):
87
+ if strainp[i] > maximstr:
88
+ maximstr = strainp[i]
89
+ j=i
90
+ maximt = time[j]
91
+
92
+ else: maximt = merger_time
93
+
94
+
95
+ # for k in range(len(time)):
96
+ # time[k]=time[k]-maximt
97
+
98
+ flinp = interp1d(time,strainp,kind='cubic')
99
+ flinc = interp1d(time,strainc,kind='cubic')
100
+
101
+
102
+ # finding maximum and minimum time spacing ---------
103
+ deltat=[]
104
+ for i in range(len(time)-1):
105
+ deltat.append(time[i+1]-time[i])
106
+
107
+ deltat.append(deltat[-1])
108
+
109
+ coarse_dt, fine_dt = max(deltat), min(deltat)
110
+ #--------------------------------------------------
111
+
112
+ if fine_resample == True:
113
+ dt = fine_dt
114
+ else: dt = coarse_dt
115
+
116
+ time_resamp = np.arange(time[0],time[-1],dt)
117
+ strainp_resamp = flinp(time_resamp)
118
+ strainc_resamp = flinc(time_resamp)
119
+
120
+ return time_resamp, strainp_resamp, strainc_resamp
121
+
122
+
123
+ def nr_resampled_rit(path_to_file,Ms,DL=200,geo_units=False):
124
+
125
+
126
+ M = Ms*urel.MTsun
127
+ MbyDL = (Ms/DL)*urel.strain_fac
128
+
129
+ file = h5.File(path_to_file,'r')
130
+ phasedataset=file['phase_l2_m2']
131
+ phasex=phasedataset['X'][:,]
132
+ phasey=phasedataset['Y'][:,]
133
+ ampdataset=file['amp_l2_m2']
134
+ ampx=ampdataset['X'][:,]
135
+ ampy=ampdataset['Y'][:,]
136
+ timedata=file['NRTimes'][:,]
137
+
138
+ phaseint = interp1d(phasex,phasey,kind='cubic')
139
+ phase = phaseint(timedata)
140
+
141
+ ampint = interp1d(ampx,ampy,kind='cubic')
142
+ amp = ampint(timedata)
143
+
144
+ sp0 = amp*np.cos(phase)
145
+ sc0 = amp*np.sin(phase)
146
+
147
+ time = timedata*M
148
+ sp, sc = sp0*MbyDL, sc0*MbyDL
149
+
150
+ if geo_units==True: return timedata, sp0, sc0, time, sp , sc
151
+ else: return time, sp, sc
152
+
153
+ def nr_resampled_horizon(path_to_file,quantity=None,delta_tbyM=None,relaxtime=None):
154
+
155
+ if quantity is None: raise Exception('Provide the quantity you need. Allowed entries are: ChristodoulouMass, ArealMass, CoordCenterInertial, DimensionfulInertialSpin, DimensionfulInertialSpinMag, chiInertial, chiMagInertial')
156
+ if delta_tbyM is None: raise Exception('Provide delta_tbyM')
157
+ # if relaxtime is None: raise Exception('Provide relaxtime')
158
+ file2 = h5.File(path_to_file,'r')
159
+ datasetA=file2['AhA.dir']
160
+ dataA=datasetA['{}.dat'.format(quantity)]
161
+ datasetB=file2['AhB.dir']
162
+ dataB=datasetB['{}.dat'.format(quantity)]
163
+ time=dataA[:,0]
164
+ # index1 = np.where(time>relaxtime)[0][0]
165
+ # time2 = time[index1:]
166
+ A=dataA[:,1]
167
+ # A2=A[index1:]
168
+ B=dataB[:,1]
169
+ # B2=B[index1:]
170
+
171
+ flinA = interp1d(time,A,kind='cubic')
172
+ flinB = interp1d(time,B,kind='cubic')
173
+
174
+ time_resamp = np.arange(time[0],time[-1],delta_tbyM)
175
+ A_resamp = flinA(time_resamp)
176
+ B_resamp = flinB(time_resamp)
177
+
178
+ return time_resamp, A_resamp, B_resamp
179
+
180
+ def nr_resampled_horizon_with_sxs(simulation_label='SXS:BBH:0001',sampling='coarse',delta_t=None,geo_units=True):
181
+
182
+ simulation = sxs.load(simulation_label)
183
+ horizons = simulation.horizons
184
+ metadata = simulation.metadata
185
+ reference_time = metadata.reference_time
186
+
187
+ reference_index = np.searchsorted(horizons.A.time, reference_time)
188
+
189
+ time_sliced = horizons.A.time[reference_index:]
190
+ mass_sliced_A = horizons.A.christodoulou_mass[reference_index:]
191
+ mass_sliced_B = horizons.B.christodoulou_mass[reference_index:]
192
+
193
+ if geo_units:
194
+ time = time_sliced
195
+ massA = mass_sliced_A
196
+ massB = mass_sliced_B
197
+ else:
198
+ time = time_sliced * urel.MTsun
199
+ massA = mass_sliced_A * urel.MTsun
200
+ massB = mass_sliced_B * urel.MTsun
201
+
202
+ flinA = interp1d(time,massA,kind='cubic')
203
+ flinB = interp1d(time,massB,kind='cubic')
204
+
205
+ if delta_t is None:
206
+ if sampling == "fine":
207
+ delta_t = np.diff(time).min()
208
+ elif sampling == "coarse":
209
+ delta_t = np.diff(time).max()
210
+ else:
211
+ raise ValueError("sampling must be 'coarse' or 'fine'")
212
+
213
+ time_sliced_even = np.arange(time[0],time[-1],delta_t)
214
+
215
+ mass_A_even = flinA(time_sliced_even)
216
+ mass_B_even = flinB(time_sliced_even)
217
+
218
+ return time_sliced_even, mass_A_even, mass_B_even
219
+
220
+
221
+ def nr_resampled_geo_with_sxs(simulation_label='SXS:BBH:0001',mode=[2,2],sampling=None,delta_t=None,junk_remove=True,merger_cutoff=False):
222
+ '''
223
+ simulation_label => just the 4-digit sxs label.
224
+ mode = an array of two elements: [l,m]
225
+ sampling => if none is specified, the resampling will be with the coarsest delta_t (sometimes it's enough). Otherwise, you can pass the string 'fine' to resample with the finest delta_t.
226
+ delta_t => you can choose delta_t of your own (optional)
227
+ junk_remove => trim the starting part of the waveform up to the reference time to remove junk.
228
+ merger_cutoff => cutoff the waveform at the common horizon time.
229
+
230
+ Returns
231
+ resampled (l,m) mode: time, real and -imag parts.
232
+ '''
233
+ simulation = sxs.load(simulation_label)
234
+ ell, em = mode
235
+ n = ell+ em
236
+
237
+ for i in range(2,ell):
238
+ n += 2*i+1
239
+
240
+ h = simulation.h[:,n] #for the (l,m) mode
241
+ metadata = simulation.metadata
242
+ reference_time = metadata.reference_time
243
+ merger_time = metadata.common_horizon_time
244
+
245
+ reference_index = np.searchsorted(h.t, reference_time)
246
+
247
+ start = reference_index if junk_remove else 0
248
+ stop = np.searchsorted(h.t, merger_time) if merger_cutoff else None
249
+
250
+ time = h.t[start:stop]
251
+ strainp = h.real[start:stop]
252
+ strainc = -h.imag[start:stop]
253
+
254
+ flinp = interp1d(time,strainp,kind='cubic')
255
+ flinc = interp1d(time,strainc,kind='cubic')
256
+
257
+ dt_array = np.diff(time)
258
+ if delta_t is None:
259
+ if sampling == "fine":
260
+ dt = dt_array.min()
261
+ else:
262
+ dt = dt_array.max()
263
+ else:
264
+ dt = delta_t
265
+
266
+ time_resamp = np.arange(time[0],time[-1],dt)
267
+ strainp_resamp = flinp(time_resamp)
268
+ strainc_resamp = flinc(time_resamp)
269
+
270
+ return time_resamp, strainp_resamp, strainc_resamp
271
+
272
+ def gw_flux(simulation_label='SXS:BBH:0001',merger_cutoff=True,delta_t=None,ell_max=8):
273
+ i=0
274
+ M = urel.MTsun
275
+ for ell in range(2,ell_max+1):
276
+ for em in range(-ell,ell+1):
277
+ t, hlm_real, hlm_imag = nr_resampled_geo_with_sxs(simulation_label=simulation_label,mode=[ell,em],merger_cutoff=merger_cutoff,delta_t=delta_t)
278
+ if i==0:
279
+ gw_flux = np.zeros_like(t)
280
+ t_nr = t
281
+ hlm_real_dot = urel.dydx(t*M,hlm_real)
282
+ hlm_imag_dot = urel.dydx(t*M,hlm_imag)
283
+
284
+ hlm_real_dot *= M
285
+ hlm_imag_dot *= M
286
+
287
+ gw_flux += np.abs(hlm_real_dot**2+hlm_imag_dot**2)
288
+ i += 1
289
+
290
+ gw_flux *= (1./(16.*np.pi))
291
+
292
+ return t_nr, gw_flux
293
+
294
+