-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
86 lines (72 loc) · 2.18 KB
/
test.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""test.py
Run
-------
python test.py
Author
------
Chong-Chong He ([email protected])
"""
import sys, os
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import cKDTree
from time import time
import csv
import tpcf
def testnumba():
N = 3000
print(f"Testing numba acceleration by running dr_cuboid on {N} particles.")
d = np.random.random([N, 3])
dpart = d[:100, :] # compile the numba machine code
rs = np.logspace(-3, 0, 20)
tpcf.dr_cuboid(dpart, rs, 1, 1, 1)
t1 = time()
tpcf.dr_cuboid(d, rs, 1, 1, 1)
dt = time() - t1
print(f"Time elapsed: {dt} sec")
print(f"If numba is installed and working properly, the time shown above "
"should be under 0.05 seconds.\n")
def get_eagle_data(NDIM=3, choose="1"):
data_dir = "mock-data"
sim_name = {"1": "RefL0050N0752_Subhalo",
"2": "RefL0100N1504_Subhalo"}[choose]
fn_data = os.path.join(data_dir, f"eagle_data_{sim_name}.npy")
fn_info = os.path.join(data_dir, f"eagle_data_{sim_name}.info")
with open(fn_data, "rb") as f:
data = np.load(f)
with open(fn_info, "r") as f:
d = csv.reader(f)
for l in d:
boxlen = float(l[1])
break
assert np.all(data <= boxlen) and np.all(data >= 0.)
if NDIM == 2:
return data[:2, :].T, (boxlen, boxlen)
elif NDIM == 3:
return data.T, (boxlen, boxlen, boxlen)
def plot_test_tpcf():
""" Plot analytically calculated TPCF
"""
sample0, dim = get_eagle_data()
N = 10000
sample1 = sample0[:N, :]
print(f"Doing {N} data particles")
a, b, c = dim
# rbins = np.logspace(-1, np.log10(a/2.0000001), 20)
rbins = np.logspace(-1, np.log10(a), 20)
rbins2 = (rbins[1:] + rbins[:-1]) / 2 / a
est = 'LS'
xi = tpcf.tpcf_ana(sample1, rbins, shape='cuboid', bound=dim, est=est)
plt.plot(rbins2, xi, 'k',)
plt.gca().set(xscale='log', yscale='log',
ylabel=r"$\xi(r)$",
xlabel="Normalized radius $r$",
)
plt.savefig("test.png", dpi=300)
print("test.png saved")
plt.show()
if __name__ == "__main__":
testnumba()
plot_test_tpcf()