-
-
Notifications
You must be signed in to change notification settings - Fork 215
/
utils.py
178 lines (149 loc) · 5.13 KB
/
utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
from ..ijit import jit
def cartesian_to_spherical_fast(
x, y, z, v_x=None, v_y=None, v_z=None, velocities_provided=False
):
if velocities_provided:
return cartesian_to_spherical(x, y, z, v_x, v_y, v_z)
return cartesian_to_spherical_novel(x, y, z)
@jit
def cartesian_to_spherical(x, y, z, v_x, v_y, v_z):
"""
Utility function (jitted) to convert cartesian to spherical.
This function should eventually result in Coordinate Transformation Graph!
"""
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
theta = np.arccos(z / r)
phi = np.arctan2(y, x)
n1 = x ** 2 + y ** 2
n2 = n1 + z ** 2
v_r = (x * v_x + y * v_y + z * v_z) / np.sqrt(n2)
v_t = (z * (x * v_x + y * v_y) - n1 * v_z) / (n2 * np.sqrt(n1))
v_p = -1 * (v_x * y - x * v_y) / n1
return r, theta, phi, v_r, v_t, v_p
@jit
def cartesian_to_spherical_novel(x, y, z):
"""
Utility function (jitted) to convert cartesian to spherical.
This function should eventually result in Coordinate Transformation Graph!
"""
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
theta = np.arccos(z / r)
phi = np.arctan2(y, x)
return r, theta, phi
def cartesian_to_bl_fast(
x, y, z, a, v_x=None, v_y=None, v_z=None, velocities_provided=False
):
if velocities_provided:
return cartesian_to_bl(x, y, z, a, v_x, v_y, v_z)
return cartesian_to_bl_novel(x, y, z, a)
@jit
def cartesian_to_bl(x, y, z, a, v_x, v_y, v_z):
"""
Utility function (jitted) to convert cartesian to boyer lindquist.
This function should eventually result in Coordinate Transformation Graph!
"""
w = (x ** 2 + y ** 2 + z ** 2) - (a ** 2)
r = np.sqrt(0.5 * (w + np.sqrt((w ** 2) + (4 * (a ** 2) * (z ** 2)))))
theta = np.arccos(z / r)
phi = np.arctan2(y, x)
dw_dt = 2 * (x * v_x + y * v_y + z * v_z)
v_r = (1 / (2 * r)) * (
(dw_dt / 2)
+ (
(w * dw_dt + 4 * (a ** 2) * z * v_z)
/ (2 * np.sqrt((w ** 2) + (4 * (a ** 2) * (z ** 2))))
)
)
v_t = (-1 / np.sqrt(1 - np.square(z / r))) * ((v_z * r - v_r * z) / (r ** 2))
v_p = (1 / (1 + np.square(y / x))) * ((v_y * x - v_x * y) / (x ** 2))
return r, theta, phi, v_r, v_t, v_p, a
@jit
def cartesian_to_bl_novel(x, y, z, a):
"""
Utility function (jitted) to convert cartesian to boyer lindquist.
This function should eventually result in Coordinate Transformation Graph!
"""
w = (x ** 2 + y ** 2 + z ** 2) - (a ** 2)
r = np.sqrt(0.5 * (w + np.sqrt((w ** 2) + (4 * (a ** 2) * (z ** 2)))))
theta = np.arccos(z / r)
phi = np.arctan2(y, x)
return r, theta, phi, a
def spherical_to_cartesian_fast(
r, t, p, v_r=None, v_t=None, v_p=None, velocities_provided=False
):
if velocities_provided:
return spherical_to_cartesian(r, t, p, v_r, v_t, v_p)
return spherical_to_cartesian_novel(r, t, p)
@jit
def spherical_to_cartesian(r, t, p, v_r, v_t, v_p):
"""
Utility function (jitted) to convert spherical to cartesian.
This function should eventually result in Coordinate Transformation Graph!
"""
x = r * np.cos(p) * np.sin(t)
y = r * np.sin(p) * np.sin(t)
z = r * np.cos(t)
v_x = (
np.sin(t) * np.cos(p) * v_r
- r * np.sin(t) * np.sin(p) * v_p
+ r * np.cos(t) * np.cos(p) * v_t
)
v_y = (
np.sin(t) * np.sin(p) * v_r
+ r * np.cos(t) * np.sin(p) * v_t
+ r * np.sin(t) * np.cos(p) * v_p
)
v_z = np.cos(t) * v_r - r * np.sin(t) * v_t
return x, y, z, v_x, v_y, v_z
@jit
def spherical_to_cartesian_novel(r, t, p):
"""
Utility function (jitted) to convert spherical to cartesian.
This function should eventually result in Coordinate Transformation Graph!
"""
x = r * np.cos(p) * np.sin(t)
y = r * np.sin(p) * np.sin(t)
z = r * np.cos(t)
return x, y, z
def bl_to_cartesian_fast(
r, t, p, a, v_r=None, v_t=None, v_p=None, velocities_provided=False
):
if velocities_provided:
return bl_to_cartesian(r, t, p, a, v_r, v_t, v_p)
return bl_to_cartesian_novel(r, t, p, a)
@jit
def bl_to_cartesian(r, t, p, a, v_r, v_t, v_p):
"""
Utility function (jitted) to convert bl to cartesian.
This function should eventually result in Coordinate Transformation Graph!
"""
xa = np.sqrt(r ** 2 + a ** 2)
sin_norm = xa * np.sin(t)
x = sin_norm * np.cos(p)
y = sin_norm * np.sin(p)
z = r * np.cos(t)
v_x = (
(r * v_r * np.sin(t) * np.cos(p) / xa)
+ (xa * np.cos(t) * np.cos(p) * v_t)
- (xa * np.sin(t) * np.sin(p) * v_p)
)
v_y = (
(r * v_r * np.sin(t) * np.sin(p) / xa)
+ (xa * np.cos(t) * np.sin(p) * v_t)
+ (xa * np.sin(t) * np.cos(p) * v_p)
)
v_z = (v_r * np.cos(t)) - (r * np.sin(t) * v_t)
return x, y, z, v_x, v_y, v_z
@jit
def bl_to_cartesian_novel(r, t, p, a):
"""
Utility function (jitted) to convert bl to cartesian.
This function should eventually result in Coordinate Transformation Graph!
"""
xa = np.sqrt(r ** 2 + a ** 2)
sin_norm = xa * np.sin(t)
x = sin_norm * np.cos(p)
y = sin_norm * np.sin(p)
z = r * np.cos(t)
return x, y, z