-
Notifications
You must be signed in to change notification settings - Fork 0
/
pspace.py
126 lines (79 loc) · 2.42 KB
/
pspace.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
from include import *
from utils import *
def get_pspace(self, xfield, yfield, npixels):
# Some additional settings
if xfield == 'gamma':
log_plot_x = 0
else:
log_plot_x = 1
if yfield == 'gamma':
log_plot_y = 0
else:
log_plot_y = 1
# Read selected fields
flag, x = self.get_refined_field(xfield)
if flag:
return
flag, y = self.get_refined_field(yfield)
if flag:
return
# Read mass
flag, mass = self.get_refined_field('mass')
if flag:
return
# Select only valid entries
if log_plot_x or log_plot_y:
if log_plot_x and log_plot_y:
idx = np.logical_and(x > 0., y > 0.)
elif log_plot_x:
idx = x > 0.
else:
idx = y > 0.
x = x[idx]
y = y[idx]
mass = mass[idx]
npart = mass.size
# Take logarithm if required
if log_plot_x:
x = np.log10(x)
if log_plot_x:
y = np.log10(y)
# Get minimum and maximum values
xmin = np.min(x)
xmax = np.max(x)
ymin = np.min(y)
ymax = np.max(y)
# Get indices for all cells
xidx = (x - xmin) * npixels / (xmax - xmin)
yidx = (y - ymin) * npixels / (ymax - ymin)
# Convert to integer
xidx = xidx.astype(dtype = np.dtype('int32'))
yidx = yidx.astype(dtype = np.dtype('int32'))
# Limit integers
xidx = np.minimum(np.maximum(xidx, 0), npixels - 1)
yidx = np.minimum(np.maximum(yidx, 0), npixels - 1)
# Intitialize results array
vals = np.zeros(npixels**2)
# Load binning library
arr_i = npct.ndpointer(dtype = np.dtype('int32'), ndim = 1, flags = 'CONTIGUOUS')
arr_d = npct.ndpointer(dtype = np.dtype('float64'), ndim = 1, flags = 'CONTIGUOUS')
lib = npct.load_library("binning", ".")
lib.restype = None
lib.binning_2d.argtypes = [arr_i, arr_i, arr_d, c_int, arr_d, c_int]
# Bin particles into vals array
lib.binning_2d(xidx, yidx, mass, npart, vals, npixels)
# Divide by total mass
tot_mass = np.sum(vals)
idx = vals > 0.
vals[idx] /= tot_mass
# Take logarithm of fractional mass
vals[idx] = np.log10(vals[idx])
# Set empty pixels to nan
idx = vals == 0.
vals[idx] = np.nan
# Reshape
vals = np.reshape(vals, (npixels, npixels))
vals = np.swapaxes(vals, 0, 1)
# Get boundaries
bds = [xmin, xmax, ymin, ymax]
return vals, bds