gwlens 0.1__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.
- gwlens/__init__.py +11 -0
- gwlens/lens/__init__.py +0 -0
- gwlens/lens/base.py +41 -0
- gwlens/lens/general.py +225 -0
- gwlens/lens/microlensing.py +40 -0
- gwlens/lens/nfw.py +271 -0
- gwlens/lens/nfw_old.py +74 -0
- gwlens/lens/nfwparallel.py +69 -0
- gwlens/lens/pbh.py +75 -0
- gwlens/lens/point.py +164 -0
- gwlens/lens/pointparallel.py +100 -0
- gwlens/lens/powerlaw.py +164 -0
- gwlens/lens/sis.py +128 -0
- gwlens/lens/test.py +26 -0
- gwlens/lensing.py +0 -0
- gwlens/pe/__init__.py +0 -0
- gwlens/pe/priors.py +109 -0
- gwlens/pe/waveform.py +85 -0
- gwlens/tools/__init__.py +1 -0
- gwlens/tools/executor.py +111 -0
- gwlens/tools/generate_ini.py +90 -0
- gwlens/tools/halo.py +115 -0
- gwlens/tools/interpolator.py +105 -0
- gwlens/tools/plotting.py +35 -0
- gwlens/tools/utilities.py +250 -0
- gwlens-0.1.dist-info/METADATA +14 -0
- gwlens-0.1.dist-info/RECORD +31 -0
- gwlens-0.1.dist-info/WHEEL +5 -0
- gwlens-0.1.dist-info/entry_points.txt +4 -0
- gwlens-0.1.dist-info/licenses/LICENSE +21 -0
- gwlens-0.1.dist-info/top_level.txt +1 -0
gwlens/__init__.py
ADDED
gwlens/lens/__init__.py
ADDED
|
File without changes
|
gwlens/lens/base.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#Base class for lens models in GWLENS
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
class Lens(ABC):
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Base class for Lens models.
|
|
9
|
+
Abstract methods are defined here...
|
|
10
|
+
|
|
11
|
+
Creating a subclass requires implementing the abstract methdos in base class
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self,**params):
|
|
17
|
+
self.params = params
|
|
18
|
+
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def potential(self,x):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def time_delay(self,x1,x2):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def deflection(self,x):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def Image_count(self):
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def Hessian(self,x1,x2):
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def Dimensionless_Frequency(self):
|
|
41
|
+
pass
|
gwlens/lens/general.py
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import time
|
|
3
|
+
import multiprocessing as mpr
|
|
4
|
+
import h5py
|
|
5
|
+
import os
|
|
6
|
+
import configparser
|
|
7
|
+
import logging
|
|
8
|
+
from mpmath import mp,mpf, mpc, sqrt, exp, besselj, quad, log
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger("nfw_interpolator")
|
|
12
|
+
logger.setLevel(logging.INFO)
|
|
13
|
+
if not logger.handlers:
|
|
14
|
+
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")
|
|
15
|
+
console_handler = logging.StreamHandler()
|
|
16
|
+
console_handler.setFormatter(formatter)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
mp.dps = 50 # set precision to 50 decimal places (arbitrary precision)
|
|
21
|
+
eps = 1e-3 # relative accuracy tolerance
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#Define it when running the code...
|
|
25
|
+
def func(x, w, y, psi, **kwargs):
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
sqrt2x = sqrt(2 * x)
|
|
29
|
+
arg_bessel = w * y * sqrt2x
|
|
30
|
+
bessel_val = besselj(0, arg_bessel)
|
|
31
|
+
psi_val = psi(sqrt2x,**kwargs)
|
|
32
|
+
return bessel_val * exp(-1j * w * psi_val)
|
|
33
|
+
|
|
34
|
+
def func2(x, w, y, psi,**kwargs):
|
|
35
|
+
return func(x, w, y,psi,**kwargs) * exp(1j * w * x)
|
|
36
|
+
|
|
37
|
+
def dfunc(x, w, y,psi,**kwargs):
|
|
38
|
+
sqrt2x = sqrt(2 * x)
|
|
39
|
+
psi_val = psi(sqrt2x, **kwargs)
|
|
40
|
+
J1 = besselj(1, w * y * sqrt2x)
|
|
41
|
+
prefactor = -w * y / sqrt2x
|
|
42
|
+
return prefactor * J1 * exp(-1j * w * psi_val) - (1j * w / (2 * x)) * func(x, w, y,psi,**kwargs)
|
|
43
|
+
|
|
44
|
+
def ddfunc(x, w, y,psi,**kwargs):
|
|
45
|
+
sqrt2x = sqrt(2 * x)
|
|
46
|
+
psi_val = psi(sqrt2x,**kwargs)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
term1 = (w * y) / (2 * x * sqrt2x) * (2 + 1j * w) * besselj(1, w * y * sqrt2x) * exp(-1j * w * psi_val)
|
|
50
|
+
term2 = -1 / (2 * x) * (w**2 * y**2 - 1j * w / x) * func(x, w, y,psi,**kwargs)
|
|
51
|
+
term3 = -1j * w / (2 * x) * dfunc(x, w, y,psi,**kwargs)
|
|
52
|
+
return term1 + term2 + term3
|
|
53
|
+
|
|
54
|
+
def Solver_AS(w, y, psi,**kwargs):
|
|
55
|
+
w = mpf(w)
|
|
56
|
+
y = mpf(y)
|
|
57
|
+
|
|
58
|
+
# Convert each kwargs to mpf (or mpc if needed)
|
|
59
|
+
kwargs_mpf = {}
|
|
60
|
+
for k, v in kwargs.items():
|
|
61
|
+
if isinstance(v, complex):
|
|
62
|
+
kwargs_mpf[k] = mpc(v)
|
|
63
|
+
else:
|
|
64
|
+
kwargs_mpf[k] = mpf(v)
|
|
65
|
+
|
|
66
|
+
a = 0.00001
|
|
67
|
+
|
|
68
|
+
if w < 1 :
|
|
69
|
+
b = 100 / w
|
|
70
|
+
elif 1 < w < 500:
|
|
71
|
+
b = 1000/w
|
|
72
|
+
else: b = 10000/w
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# mpmath.quad with complex integrand
|
|
76
|
+
zz = quad(lambda x: func2(x, w, y, psi,**kwargs_mpf), [a, b], error=True, maxdegree=10)
|
|
77
|
+
zz_val = zz[0] # integral value
|
|
78
|
+
|
|
79
|
+
# Add tail correction terms (at b)
|
|
80
|
+
tail = (-func(b, w, y,psi,**kwargs_mpf) / (1j * w) * exp(1j * w * b)
|
|
81
|
+
- dfunc(b, w, y,psi,**kwargs_mpf) / (w**2) * exp(1j * w * b)
|
|
82
|
+
+ ddfunc(b, w, y, psi,**kwargs_mpf) / (1j * w**3) * exp(1j * w * b))
|
|
83
|
+
|
|
84
|
+
zz_val += tail
|
|
85
|
+
|
|
86
|
+
ampfac= -1j * w * exp(0.5 * 1j * w * y**2) * zz_val
|
|
87
|
+
return complex(round(ampfac.real, 6), round(ampfac.imag, 6))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def amplification(w_values,y_values,psi,num_processes=4,**kwargs):
|
|
91
|
+
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
W_len = len(w_values)
|
|
98
|
+
Y_len = len(y_values)
|
|
99
|
+
total_points = W_len * Y_len
|
|
100
|
+
|
|
101
|
+
logger.info(f" Grid size: {W_len} * {Y_len} → {total_points} points")
|
|
102
|
+
logger.info(f"Launching parallel computation with {num_processes} tasks")
|
|
103
|
+
results = np.zeros((W_len, Y_len, 2), dtype=np.float64)
|
|
104
|
+
tasks = [(i, j, w_values[i], y_values[j],psi,kwargs ) for i in range(W_len) for j in range(Y_len)]
|
|
105
|
+
|
|
106
|
+
with mpr.Pool(processes=num_processes or mpr.cpu_count()) as pool:
|
|
107
|
+
for count, (i, j, re, im) in enumerate(pool.imap_unordered(_worker, tasks, chunksize=100), 1):
|
|
108
|
+
results[i, j, 0] = re
|
|
109
|
+
results[i, j, 1] = im
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
print("Completed")
|
|
113
|
+
return results
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
## Below code is for python multiprocessing...
|
|
121
|
+
|
|
122
|
+
def _worker(args):
|
|
123
|
+
i, j, w, y,psi,kwargs = args
|
|
124
|
+
val = amplification_factor(w, y, psi,**kwargs)
|
|
125
|
+
return i, j, val.real, val.imag
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def compute_lens_grid(w_values, y_values,psi, hdf5_filename,num_processes=None,**kwargs):
|
|
130
|
+
"""
|
|
131
|
+
Computes the Point(w, y) complex amplification factor over a grid
|
|
132
|
+
and stores the result in an HDF5 file.
|
|
133
|
+
|
|
134
|
+
Parameters:
|
|
135
|
+
- w_values (1D np.array): dimensionless frequency array
|
|
136
|
+
- y_values (1D np.array): impact parameter array
|
|
137
|
+
- hdf5_filename (str): path to output HDF5 file
|
|
138
|
+
- num_processes (int or None): number of CPU cores to use (default: all)
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
#Read the config file
|
|
142
|
+
config = configparser.ConfigParser()
|
|
143
|
+
config.read('parameters.ini')
|
|
144
|
+
|
|
145
|
+
# Setup logging
|
|
146
|
+
logger = logging.getLogger("compute_logger")
|
|
147
|
+
logger.setLevel(logging.INFO)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
#Store result
|
|
151
|
+
output_folder = config['Output']['outdir']
|
|
152
|
+
|
|
153
|
+
# Subfolder for interpolation outputs
|
|
154
|
+
log_folder = os.path.join(output_folder, 'logs')
|
|
155
|
+
os.makedirs(log_folder, exist_ok=True) # Create if it doesn't exist
|
|
156
|
+
|
|
157
|
+
log_file = os.path.join(log_folder,'interpolation.log')
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
file_handler = logging.FileHandler(log_file)
|
|
161
|
+
file_handler.setLevel(logging.INFO)
|
|
162
|
+
|
|
163
|
+
stream_handler = logging.StreamHandler()
|
|
164
|
+
stream_handler.setLevel(logging.INFO)
|
|
165
|
+
|
|
166
|
+
formatter = logging.Formatter(
|
|
167
|
+
"%(asctime)s [%(levelname)s] %(message)s",
|
|
168
|
+
"%Y-%m-%d %H:%M:%S"
|
|
169
|
+
)
|
|
170
|
+
file_handler.setFormatter(formatter)
|
|
171
|
+
stream_handler.setFormatter(formatter)
|
|
172
|
+
|
|
173
|
+
logger.addHandler(file_handler)
|
|
174
|
+
logger.addHandler(stream_handler)
|
|
175
|
+
|
|
176
|
+
W_len = len(w_values)
|
|
177
|
+
Y_len = len(y_values)
|
|
178
|
+
total_points = W_len * Y_len
|
|
179
|
+
|
|
180
|
+
# Create task list
|
|
181
|
+
tasks = [(i, j, w_values[i], y_values[j], amp, core, p ) for i in range(W_len) for j in range(Y_len)]
|
|
182
|
+
|
|
183
|
+
logger.info(f" Output file: {hdf5_filename}")
|
|
184
|
+
logger.info(f" Grid size: {W_len} × {Y_len} → {total_points} points")
|
|
185
|
+
logger.info(f"Launching parallel computation with {num_processes} tasks")
|
|
186
|
+
|
|
187
|
+
start_time = time.time()
|
|
188
|
+
|
|
189
|
+
with h5py.File(hdf5_filename, "w") as h5file:
|
|
190
|
+
h5file.create_dataset("w_values", data=w_values)
|
|
191
|
+
h5file.create_dataset("y_values", data=y_values)
|
|
192
|
+
dset = h5file.create_dataset(
|
|
193
|
+
"results",
|
|
194
|
+
shape=(W_len, Y_len, 2),
|
|
195
|
+
dtype="f8",
|
|
196
|
+
chunks=(1, Y_len, 2) # optimize for row-wise access
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
with mpr.Pool(processes=num_processes or mpr.cpu_count()) as pool:
|
|
200
|
+
for count, (i, j, re, im) in enumerate(pool.imap_unordered(_worker, tasks, chunksize=100), 1):
|
|
201
|
+
dset[i, j, 0] = re
|
|
202
|
+
dset[i, j, 1] = im
|
|
203
|
+
|
|
204
|
+
if count % 100 == 0 or count == total_points:
|
|
205
|
+
logger.info(f" {count}/{total_points} results written...")
|
|
206
|
+
|
|
207
|
+
h5file.flush()
|
|
208
|
+
|
|
209
|
+
end_time = time.time()
|
|
210
|
+
logger.info(f" Done in {end_time - start_time:.2f} seconds")
|
|
211
|
+
logger.info(f" Saved to: {hdf5_filename}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""
|
|
2
|
+
author: Sreeakanth Harikumar
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from astropy.constants import c,G,M_sun,kpc
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
#Define constants
|
|
10
|
+
c = c.value
|
|
11
|
+
G = G.value
|
|
12
|
+
M_sun = M_sun.value
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def EinsteinCrossingTime(Dl,Ds,v,M):
|
|
16
|
+
"""
|
|
17
|
+
Parameters:
|
|
18
|
+
|
|
19
|
+
Dl : Distance to lens in kpc
|
|
20
|
+
Ds : Distance to lens in kpc
|
|
21
|
+
v : velocity of the lens (not relative velocity) in 1 km/s
|
|
22
|
+
M : Mass of the lens in Solar Mass
|
|
23
|
+
|
|
24
|
+
returns: einstein crossing time in days
|
|
25
|
+
"""
|
|
26
|
+
prefactor = np.sqrt(1*kpc.value)*np.sqrt(M_sun)* 1/(1000)
|
|
27
|
+
oneday = 60*60*24
|
|
28
|
+
x = Dl/Ds
|
|
29
|
+
te = (1/oneday ) * prefactor*np.sqrt(G/c**2) * np.sqrt(4*x * (1 -x )) * np.sqrt(Ds) * np.sqrt(M) * (v)**(-1)
|
|
30
|
+
return te
|
|
31
|
+
|
|
32
|
+
def RelativeVelocity(Dl,Ds,vS,vL,vO):
|
|
33
|
+
x = Ds/Dl
|
|
34
|
+
return vS - x*vL + (x-1)*vO
|
|
35
|
+
|
|
36
|
+
def MicrolensingImpactparameter(t,y0,t0,tE):
|
|
37
|
+
x = (t-t0)/tE
|
|
38
|
+
y = np.sqrt(y0**2 + x**2)
|
|
39
|
+
return y
|
|
40
|
+
|
gwlens/lens/nfw.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import time
|
|
3
|
+
import multiprocessing as mpr
|
|
4
|
+
import h5py
|
|
5
|
+
import os
|
|
6
|
+
import configparser
|
|
7
|
+
import logging
|
|
8
|
+
from joblib import Parallel, delayed
|
|
9
|
+
from mpmath import mp,mpf, mpc, sqrt, exp, besselj, quad,log, atan,atanh
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Setup logger (you can customize the path or pass it in)
|
|
14
|
+
logger = logging.getLogger("nfw_interpolator")
|
|
15
|
+
logger.setLevel(logging.INFO)
|
|
16
|
+
if not logger.handlers:
|
|
17
|
+
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")
|
|
18
|
+
console_handler = logging.StreamHandler()
|
|
19
|
+
console_handler.setFormatter(formatter)
|
|
20
|
+
logger.addHandler(console_handler)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
mp.dps = 200 # set precision to 50 decimal places (arbitrary precision)
|
|
24
|
+
eps = 1e-3 # relative accuracy tolerance
|
|
25
|
+
|
|
26
|
+
def psi(x,rs):
|
|
27
|
+
"""
|
|
28
|
+
NFW lensing potential defined as per Villarrubia-Rojo et. al. 2024
|
|
29
|
+
The constant k = xi_0/r_s
|
|
30
|
+
|
|
31
|
+
To avoid replacing all the r_s with k. I have used k = rs in this form of lensing potential. This is only for testing purpose and hence will be replaced later.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
u = rs*x
|
|
37
|
+
if u <= 1:
|
|
38
|
+
|
|
39
|
+
return 0.5*( (log(u/2))**2 - (atanh(sqrt(1-u**2)))**2 )
|
|
40
|
+
else:
|
|
41
|
+
|
|
42
|
+
return 0.5*( (log(u/2))**2 + (atan(sqrt(u**2 -1)))**2 )
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def psi_2(x, rs):
|
|
46
|
+
if x == 0:
|
|
47
|
+
return 0
|
|
48
|
+
elif x < 1:
|
|
49
|
+
return rs/2 * ((log(x/2))**2 - (atanh(sqrt(1 - x**2)))**2)
|
|
50
|
+
else:
|
|
51
|
+
return rs/2 * ((log(x/2))**2 + (atan(sqrt(x**2 - 1)))**2)
|
|
52
|
+
|
|
53
|
+
#def psi(x,rs):
|
|
54
|
+
#
|
|
55
|
+
# bterm = mpf('2.01')
|
|
56
|
+
|
|
57
|
+
# return rs*( (log(x/2*bterm))**2 - (atanh(sqrt(1 - ((x**2) /bterm**2)**2)))**2)
|
|
58
|
+
|
|
59
|
+
def func(x, w, y,rs):
|
|
60
|
+
# x: real integration variable (mpf)
|
|
61
|
+
# w, y, amp, core, p: mpc or mpf
|
|
62
|
+
sqrt2x = sqrt(2 * x)
|
|
63
|
+
arg_bessel = w * y * sqrt2x
|
|
64
|
+
bessel_val = besselj(0, arg_bessel)
|
|
65
|
+
psi_val = psi(sqrt2x, rs)
|
|
66
|
+
return bessel_val * exp(-1j * w * psi_val)
|
|
67
|
+
|
|
68
|
+
def func2(x, w, y, rs):
|
|
69
|
+
return func(x, w, y, rs) * exp(1j * w * x)
|
|
70
|
+
|
|
71
|
+
def dfunc(x, w, y, rs):
|
|
72
|
+
sqrt2x = sqrt(2 * x)
|
|
73
|
+
psi_val = psi(sqrt2x, rs)
|
|
74
|
+
J1 = besselj(1, w * y * sqrt2x)
|
|
75
|
+
prefactor = -w * y / sqrt2x
|
|
76
|
+
return prefactor * J1 * exp(-1j * w * psi_val) - (1j * w / (2 * x)) * func(x, w, y,rs)
|
|
77
|
+
|
|
78
|
+
def ddfunc(x, w, y,rs):
|
|
79
|
+
sqrt2x = sqrt(2 * x)
|
|
80
|
+
psi_val = psi(sqrt2x, rs)
|
|
81
|
+
#denom = (sqrt2x**2 + core**2)**(1 - p/2)
|
|
82
|
+
#dpsi = amp * p * sqrt2x / denom
|
|
83
|
+
|
|
84
|
+
# derivative squared term:
|
|
85
|
+
#d2psi = amp * p * (1 - p / 2) * (2 * x)**(-0.5) * (1 - 2 * x / (2 * x + core**2)) / (2 * x + core**2)**(1 - p/2)
|
|
86
|
+
|
|
87
|
+
term1 = (w * y) / (2 * x * sqrt2x) * (2 + 1j * w) * besselj(1, w * y * sqrt2x) * exp(-1j * w * psi_val)
|
|
88
|
+
term2 = -1 / (2 * x) * (w**2 * y**2 - 1j * w / x) * func(x, w, y, rs)
|
|
89
|
+
term3 = -1j * w / (2 * x) * dfunc(x, w, y,rs)
|
|
90
|
+
return term1 + term2 + term3
|
|
91
|
+
|
|
92
|
+
def NFW(w, y, rs):
|
|
93
|
+
|
|
94
|
+
# Convert inputs to mpmath types
|
|
95
|
+
w = mpf(w) # real number
|
|
96
|
+
y = mpf(y) # real number
|
|
97
|
+
rs = mpf(rs) # real number
|
|
98
|
+
a = mpf('0.00001')
|
|
99
|
+
|
|
100
|
+
if w <= 1:
|
|
101
|
+
b = mpf('100') / w
|
|
102
|
+
elif 1 < w < 400000:
|
|
103
|
+
b = mpf('1000') / w
|
|
104
|
+
elif 1000 < w < 500000:
|
|
105
|
+
b = mpf('100000') / w
|
|
106
|
+
elif 500000 < w < 10000000:
|
|
107
|
+
b = mpf('1000000') / w
|
|
108
|
+
else:
|
|
109
|
+
b = mpf('100000000') / w
|
|
110
|
+
|
|
111
|
+
# mpmath.quad with complex integrand
|
|
112
|
+
zz = quad(lambda x: func2(x, w, y,rs), [a, b], error=True, maxdegree=10)
|
|
113
|
+
zz_val = zz[0] # integral value
|
|
114
|
+
|
|
115
|
+
# Add tail correction terms (at b)
|
|
116
|
+
tail = (-func(b, w, y, rs) / (1j * w) * exp(1j * w * b)
|
|
117
|
+
- dfunc(b, w, y, rs) / (w**2) * exp(1j * w * b)
|
|
118
|
+
+ ddfunc(b, w, y, rs) / (1j * w**3) * exp(1j * w * b))
|
|
119
|
+
|
|
120
|
+
zz_val += tail
|
|
121
|
+
|
|
122
|
+
return -1j * w * exp(0.5 * 1j * w * y**2) * zz_val
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _worker(args):
|
|
126
|
+
i, j, w, y, rs = args
|
|
127
|
+
val = NFW(w, y, rs)
|
|
128
|
+
return i, j, val.real, val.imag
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def compute_nfw_lens_grid(w_values, y_values, rs , hdf5_filename,num_processes=None):
|
|
133
|
+
"""
|
|
134
|
+
Computes the Point(w, y) complex amplification factor over a grid
|
|
135
|
+
and stores the result in an HDF5 file.
|
|
136
|
+
|
|
137
|
+
Parameters:
|
|
138
|
+
- w_values (1D np.array): dimensionless frequency array
|
|
139
|
+
- y_values (1D np.array): impact parameter array
|
|
140
|
+
- hdf5_filename (str): path to output HDF5 file
|
|
141
|
+
- num_processes (int or None): number of CPU cores to use (default: all)
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
#Read the config file
|
|
145
|
+
config = configparser.ConfigParser()
|
|
146
|
+
config.read('parameters.ini')
|
|
147
|
+
|
|
148
|
+
# Setup logging
|
|
149
|
+
logger = logging.getLogger("compute_logger")
|
|
150
|
+
logger.setLevel(logging.INFO)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
#Store result
|
|
154
|
+
output_folder = config['Output']['outdir']
|
|
155
|
+
|
|
156
|
+
# Subfolder for interpolation outputs
|
|
157
|
+
log_folder = os.path.join(output_folder, 'logs')
|
|
158
|
+
os.makedirs(log_folder, exist_ok=True) # Create if it doesn't exist
|
|
159
|
+
|
|
160
|
+
log_file = os.path.join(log_folder,'interpolation.log')
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
file_handler = logging.FileHandler(log_file)
|
|
164
|
+
file_handler.setLevel(logging.INFO)
|
|
165
|
+
|
|
166
|
+
stream_handler = logging.StreamHandler()
|
|
167
|
+
stream_handler.setLevel(logging.INFO)
|
|
168
|
+
|
|
169
|
+
formatter = logging.Formatter(
|
|
170
|
+
"%(asctime)s [%(levelname)s] %(message)s",
|
|
171
|
+
"%Y-%m-%d %H:%M:%S"
|
|
172
|
+
)
|
|
173
|
+
file_handler.setFormatter(formatter)
|
|
174
|
+
stream_handler.setFormatter(formatter)
|
|
175
|
+
|
|
176
|
+
logger.addHandler(file_handler)
|
|
177
|
+
logger.addHandler(stream_handler)
|
|
178
|
+
|
|
179
|
+
W_len = len(w_values)
|
|
180
|
+
Y_len = len(y_values)
|
|
181
|
+
total_points = W_len * Y_len
|
|
182
|
+
|
|
183
|
+
w_values = [mpc(w) for w in w_values]
|
|
184
|
+
y_values = [mpc(y) for y in y_values]
|
|
185
|
+
|
|
186
|
+
# Create task list
|
|
187
|
+
tasks = [(i, j, w_values[i], y_values[j], rs ) for i in range(W_len) for j in range(Y_len)]
|
|
188
|
+
|
|
189
|
+
logger.info(f" Output file: {hdf5_filename}")
|
|
190
|
+
logger.info(f" Grid size: {W_len} × {Y_len} → {total_points} points")
|
|
191
|
+
logger.info(f"Launching parallel computation with {num_processes} tasks")
|
|
192
|
+
|
|
193
|
+
start_time = time.time()
|
|
194
|
+
w_array = np.array([float(w) for w in w_values], dtype='f8')
|
|
195
|
+
y_array = np.array([float(y) for y in y_values], dtype='f8')
|
|
196
|
+
with h5py.File(hdf5_filename, "w") as h5file:
|
|
197
|
+
h5file.create_dataset("w_values", data=w_array)
|
|
198
|
+
h5file.create_dataset("y_values", data=y_array)
|
|
199
|
+
dset = h5file.create_dataset(
|
|
200
|
+
"results",
|
|
201
|
+
shape=(W_len, Y_len, 2),
|
|
202
|
+
dtype="f8",
|
|
203
|
+
chunks=(1, Y_len, 2) # optimize for row-wise access
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
with mpr.Pool(processes=num_processes or mpr.cpu_count()) as pool:
|
|
207
|
+
for count, (i, j, re, im) in enumerate(pool.imap_unordered(_worker, tasks, chunksize=100), 1):
|
|
208
|
+
dset[i, j, 0] = re
|
|
209
|
+
dset[i, j, 1] = im
|
|
210
|
+
|
|
211
|
+
if count % 1000 == 0 or count == total_points:
|
|
212
|
+
logger.info(f" {count}/{total_points} results written...")
|
|
213
|
+
|
|
214
|
+
h5file.flush()
|
|
215
|
+
|
|
216
|
+
end_time = time.time()
|
|
217
|
+
logger.info(f" Done in {end_time - start_time:.2f} seconds")
|
|
218
|
+
logger.info(f" Saved to: {hdf5_filename}")
|
|
219
|
+
|
|
220
|
+
def compute_nfw_lens_test(w_values, y_values, rs_values, hdf5_filename, num_jobs=None):
|
|
221
|
+
"""
|
|
222
|
+
Computes the NFW(w, y, rs) complex amplification factor over a 3D grid,
|
|
223
|
+
and stores real and imaginary parts in an HDF5 file.
|
|
224
|
+
|
|
225
|
+
Parameters:
|
|
226
|
+
- w_values (np.ndarray): 1D array of dimensionless frequency
|
|
227
|
+
- y_values (np.ndarray): 1D array of impact parameter
|
|
228
|
+
- rs_values (np.ndarray): 1D array of scale radii
|
|
229
|
+
- hdf5_filename (str): Path to output HDF5 file
|
|
230
|
+
- num_jobs (int or None): Number of parallel jobs to use (default: all cores)
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
W_len, Y_len, R_len = len(w_values), len(y_values), len(rs_values)
|
|
234
|
+
total = W_len * Y_len * R_len
|
|
235
|
+
logger.info(f"Preparing {total} NFW evaluations ({W_len}×{Y_len}×{R_len})")
|
|
236
|
+
logger.info(f"Saving to: {hdf5_filename}")
|
|
237
|
+
|
|
238
|
+
# Start timer
|
|
239
|
+
t0 = time.time()
|
|
240
|
+
|
|
241
|
+
# Flatten all grid points as tasks
|
|
242
|
+
tasks = [(i, j, k, w_values[i], y_values[j], rs_values[k])
|
|
243
|
+
for i in range(W_len)
|
|
244
|
+
for j in range(Y_len)
|
|
245
|
+
for k in range(R_len)]
|
|
246
|
+
|
|
247
|
+
def _compute_point(i, j, k, w, y, rs):
|
|
248
|
+
val = NFW(w, y, rs)
|
|
249
|
+
return (i, j, k, val.real, val.imag)
|
|
250
|
+
|
|
251
|
+
# Run in parallel
|
|
252
|
+
logger.info(f"Running in parallel with {num_jobs or os.cpu_count()} cores...")
|
|
253
|
+
results = Parallel(n_jobs=num_jobs or -1, backend="loky", verbose=10)(
|
|
254
|
+
delayed(_compute_point)(i, j, k, w, y, rs) for (i, j, k, w, y, rs) in tasks
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
# Allocate result array
|
|
258
|
+
result_array = np.zeros((W_len, Y_len, R_len, 2), dtype=np.float64)
|
|
259
|
+
for i, j, k, re, im in results:
|
|
260
|
+
result_array[i, j, k, 0] = re
|
|
261
|
+
result_array[i, j, k, 1] = im
|
|
262
|
+
|
|
263
|
+
# Save everything to HDF5
|
|
264
|
+
with h5py.File(hdf5_filename, "w") as f:
|
|
265
|
+
f.create_dataset("w_values", data=w_values)
|
|
266
|
+
f.create_dataset("y_values", data=y_values)
|
|
267
|
+
f.create_dataset("rs_values", data=rs_values)
|
|
268
|
+
f.create_dataset("results", data=result_array, chunks=True, compression="gzip")
|
|
269
|
+
|
|
270
|
+
logger.info(f"✅ Completed in {time.time() - t0:.2f} seconds.")
|
|
271
|
+
logger.info("💾 Interpolation grid saved and ready.")
|
gwlens/lens/nfw_old.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy.special import jv
|
|
4
|
+
from scipy.integrate import quad
|
|
5
|
+
import warnings
|
|
6
|
+
#from functools import lru_cache
|
|
7
|
+
|
|
8
|
+
warnings.simplefilter('ignore')
|
|
9
|
+
|
|
10
|
+
eps = 1.e-4 # Relative accuracy of F(w, y)
|
|
11
|
+
|
|
12
|
+
def psi(x, rs):
|
|
13
|
+
if x == 0:
|
|
14
|
+
return 0
|
|
15
|
+
elif x < 1:
|
|
16
|
+
return rs/2 * ((np.log(x/2))**2 - np.arctanh(np.sqrt(1 - x**2))**2)
|
|
17
|
+
else:
|
|
18
|
+
return rs/2 * ((np.log(x/2))**2 + np.arctan(np.sqrt(x**2 - 1))**2)
|
|
19
|
+
|
|
20
|
+
# def psi(x, rs):
|
|
21
|
+
# if x == 0:
|
|
22
|
+
# return 0
|
|
23
|
+
# elif x<=1
|
|
24
|
+
# return rs/2 * ((np.log(x/2))**2 - 4 * np.arctanh(np.sqrt((1 - x) / (1 + x)))**2)
|
|
25
|
+
# else:
|
|
26
|
+
# return rs/2 * ((np.log(x/2))**2 + 4 * np.arctan(np.sqrt((x - 1) / (1 + x)))**2)
|
|
27
|
+
|
|
28
|
+
def func(x, w, y, rs):
|
|
29
|
+
return jv(0, w * y * np.sqrt(2 * x)) * np.exp(-1j * w * psi(np.sqrt(2 * x), rs))
|
|
30
|
+
|
|
31
|
+
def func2(x, w, y, rs):
|
|
32
|
+
return func(x, w, y, rs) * np.exp(1j * w * x)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def dfunc(x, w, y, rs):
|
|
36
|
+
return (-w * y / np.sqrt(2 * x) * jv(1, w * y * np.sqrt(2 * x)) * np.exp(-1j * w * psi(np.sqrt(2 * x), rs)) -
|
|
37
|
+
1j * w / (2 * x) * func(x, w, y, rs))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def ddfunc(x, w, y, rs):
|
|
41
|
+
return (w * y / (2 * x * np.sqrt(2 * x)) * (2 + 1j * w) * jv(1, w * y * np.sqrt(2 * x)) *
|
|
42
|
+
np.exp(-1j * w * psi(np.sqrt(2 * x), rs)) - 1 / (2 * x) * (w * w * y * y - 1j * w / x) *
|
|
43
|
+
func(x, w, y, rs) - 1j * w / (2 * x) * dfunc(x, w, y, rs))
|
|
44
|
+
|
|
45
|
+
def NFW(w, y, rs):
|
|
46
|
+
a = 0.0001
|
|
47
|
+
b = 1000. / w
|
|
48
|
+
zzp = -1.
|
|
49
|
+
while True:
|
|
50
|
+
zz = quad(func2, a, b, args=(w, y, rs), limit=int(1e+7), epsrel=eps/3., complex_func=True)[0]
|
|
51
|
+
zz += (-func(b, w, y, rs) / (1j * w) * np.exp(1j * w * b) -
|
|
52
|
+
dfunc(b, w, y, rs) / (w * w) * np.exp(1j * w * b) +
|
|
53
|
+
ddfunc(b, w, y, rs) / (1j * w * w * w) * np.exp(1j * w * b))
|
|
54
|
+
if np.abs(zz / zzp - 1) < eps:
|
|
55
|
+
break
|
|
56
|
+
zzp = zz
|
|
57
|
+
b = min(b * 10, 10**5 / w) # Limit b to a maximum to avoid excessive ranges
|
|
58
|
+
return -1j * w * np.exp(0.5 * 1j * w * y * y) * zz
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# def NFW(w, y, rs):
|
|
62
|
+
# a = 0.
|
|
63
|
+
# b = 1000
|
|
64
|
+
# zzp = -1.
|
|
65
|
+
# while True:
|
|
66
|
+
# zz = quad(func2, a, b, args=(w, y, rs), limit=int(1e+5), epsrel=eps/3., complex_func=True)[0]
|
|
67
|
+
# zz += (-func(b, w, y, rs) / (1j * w) * np.exp(1j * w * b) -
|
|
68
|
+
# dfunc(b, w, y, rs) / (w * w) * np.exp(1j * w * b) +
|
|
69
|
+
# ddfunc(b, w, y, rs) / (1j * w * w * w) * np.exp(1j * w * b))
|
|
70
|
+
# if np.abs(zz / zzp - 1) < eps:
|
|
71
|
+
# break
|
|
72
|
+
# zzp = zz
|
|
73
|
+
|
|
74
|
+
# return -1j * w * np.exp(0.5 * 1j * w * y * y) * zz
|