Coverage for planit/utils.py: 90%
88 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-11 15:42 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-11 15:42 +0000
1"""
2 planit utilities
3"""
5import numpy as npy
6import numba
7from .globaldefs import *
10def calc_hmax(rho_lim, part_mass):
11 """
12 calculate maximum smoothing length corresponding to the
13 density floor (rho_lim) for a given particle mass (part_mass)
15 returns h_max in Earth radii (default format used for SWIFT param)
16 """
17 h = (part_mass/(4/3.*npy.pi*rho_lim))**(1./3.)
18 return h/Rearth * 1.206
21def rad(x, y, z):
22 """
23 Calculate radial distance from center
24 """
25 return npy.sqrt(x**2 + y**2 + z**2)
28def com(m,x,y,z):
29 """
30 Calculate centre-of-mass
31 """
32 cx = (m*x).sum()/m.sum()
33 cy = (m*y).sum()/m.sum()
34 cz = (m*z).sum()/m.sum()
36 return cx,cy,cz
39def com_v(m,vx,vy,vz):
40 """
41 Calculate centre-of-mass velocity
42 """
43 cvx = (m*vx).sum()/m.sum()
44 cvy = (m*vy).sum()/m.sum()
45 cvz = (m*vz).sum()/m.sum()
47 return cvx,cvy,cvz
50class Node:
51 def __init__(self, width, origin, pos, mass, ids, leaves=None):
52 self.size = width
53 self.origin = origin
54 self.children = []
55 if leaves is None:
56 leaves = []
58 N = len(mass)
59 #if N == 1:
60 # self.m = mass[0]
61 # self.com = pos[0]
62 # self.ids = ids
63 # self.pot = 0
64 # leaves.append(self)
65 if N <= 8:
66 self.m = mass.sum()
67 self.com = pos.sum(axis=0)/len(mass)
68 self.ids = ids
69 self.pot = 0
70 leaves.append(self)
71 #elif N == 2 and npy.sqrt(((pos[0]-pos[1])**2).sum()) < (self.size/2e50):
72 # self.m = mass.sum()
73 # self.com = (pos[0]+pos[1])/2.
74 # self.ids = ids
75 # self.pot = 0
76 # leaves.append(self)
77 elif self.size == 0.0:
78 if len(ids) > 2:
79 print('WARNING: ', len(ids), ' overlapping particles: ', ids)
80 self.m = mass.sum()
81 self.com = pos[0]
82 self.ids = ids
83 self.pot = 0
84 leaves.append(self)
85 else:
86 self.genChild(width, origin, pos, mass, ids, leaves)
88 m = [c.m for c in self.children]
89 com = [c.com for c in self.children]
90 m = npy.array(m)
91 com = npy.array(com)
92 self.m = m.sum()
93 self.com = (m*com.T).T.sum(axis=0)/self.m
95 def genChild(self, width, origin, pos, mass, ids, leaves):
96 oct_choose = pos > self.origin
97 oct_choose = oct_choose.astype(int)
98 for i in [0,1]:
99 for j in [0,1]:
100 for k in [0,1]:
101 this_oct = (oct_choose[:,0] == i)*(oct_choose[:,1] == j)*(oct_choose[:,2] == k)
102 if npy.any(this_oct):
103 dw = 0.5*self.size*(npy.array([i,j,k])-0.5)
104 self.children.append(Node(0.5*self.size, self.origin+dw, pos[this_oct], mass[this_oct], ids[this_oct], leaves))
107def Walk_Pot(node, leaf, theta2):
108 dr = node.com - leaf.com
109 r2 = (dr**2).sum()
110 if (len(node.children) == 0) or (node.size**2/r2 < theta2):
111 if r2 != 0:
112 leaf.pot += - G * node.m / npy.sqrt(r2)
113 else:
114 for c in node.children:
115 Walk_Pot(c, leaf, theta2)
118@numba.njit(parallel=False)
119def _calc_potential_direct(m, x, y, z):
120 pot = npy.zeros(len(m))
121 for j in numba.prange(len(m)):
122 pdist = npy.sqrt((x-x[j])**2 + (y-y[j])**2 + (z-z[j])**2)
123 pot[j] = npy.nansum(npy.where(npy.isinf(-G * m / pdist), 0, -G * m / pdist))
124 return pot
127def _calc_potential_tree(m, x, y, z):
128 theta = 1.1
129 theta2 = theta**2
130 treeorigin = (npy.max((x, y, z), axis=1) + npy.min((x, y, z), axis=1))/2.
131 treewidth = (npy.max((x, y, z)) - npy.min((x, y, z)))
132 leaves = []
133 tree = Node(treewidth, treeorigin, npy.array((x, y, z)).T, m, npy.arange(len(m)), leaves)
135 pot = npy.zeros(len(m))
136 for leaf in leaves:
137 Walk_Pot(tree, leaf, theta2)
138 pot[leaf.ids] = leaf.pot
139 return pot
142def calc_potential(m, x, y, z):
143 if len(m) > 200:
144 return _calc_potential_tree(m, x, y, z)
145 else:
146 return _calc_potential_direct(m, x, y, z)