Skip to content

Commit

Permalink
Manusally Merge branch 'Varunvaruns9-faster'
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbapat committed Sep 5, 2019
2 parents a3bf6ac + 79ec505 commit 9ccf6d6
Show file tree
Hide file tree
Showing 10 changed files with 575 additions and 208 deletions.
5 changes: 5 additions & 0 deletions src/einsteinpy/coordinates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from .conversion import (
BoyerLindquistConversion,
CartesianConversion,
SphericalConversion,
)
from .core import BoyerLindquist, Cartesian, Spherical
from .velocity import (
BoyerLindquistDifferential,
Expand Down
127 changes: 127 additions & 0 deletions src/einsteinpy/coordinates/conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import numpy as np

from .utils import (
bl_to_cartesian_fast,
cartesian_to_bl_fast,
cartesian_to_spherical_fast,
spherical_to_cartesian_fast,
)


class CartesianConversion:
def __init__(self, x, y, z, v_x=None, v_y=None, v_z=None):
self.x_si = x
self.y_si = y
self.z_si = z
self.vx_si = v_x
self.vy_si = v_y
self.vz_si = v_z
self._velocities_provided = not (
(v_x is None) or (v_y is None) or (v_z is None)
)

def values(self):
if self._velocities_provided:
return self.x_si, self.y_si, self.z_si, self.vx_si, self.vy_si, self.vz_si
return self.x_si, self.y_si, self.z_si

def convert_spherical(self):
return cartesian_to_spherical_fast(
self.x_si,
self.y_si,
self.z_si,
self.vx_si,
self.vy_si,
self.vz_si,
self._velocities_provided,
)

def convert_bl(self, a):
return cartesian_to_bl_fast(
self.x_si,
self.y_si,
self.z_si,
a,
self.vx_si,
self.vy_si,
self.vz_si,
self._velocities_provided,
)


class SphericalConversion:
def __init__(self, r, theta, phi, v_r=None, v_t=None, v_p=None):
self.r_si = r
self.t_si = theta
self.p_si = phi
self.vr_si = v_r
self.vt_si = v_t
self.vp_si = v_p
self._velocities_provided = not (
(v_r is None) or (v_t is None) or (v_p is None)
)

def values(self):
if self._velocities_provided:
return self.r_si, self.t_si, self.p_si, self.vr_si, self.vt_si, self.vp_si
return self.r_si, self.t_si, self.p_si

def convert_cartesian(self):
return spherical_to_cartesian_fast(
self.r_si,
self.t_si,
self.p_si,
self.vr_si,
self.vt_si,
self.vp_si,
self._velocities_provided,
)

def convert_bl(self, a):
transformed_cartesian = self.convert_cartesian()
cart = CartesianConversion(*transformed_cartesian)
return cart.convert_bl(a)


class BoyerLindquistConversion:
def __init__(self, r, theta, phi, v_r=None, v_t=None, v_p=None, a=0.0):
self.r_si = r
self.t_si = theta
self.p_si = phi
self.vr_si = v_r
self.vt_si = v_t
self.vp_si = v_p
self.a_si = a
self._velocities_provided = not (
(v_r is None) or (v_t is None) or (v_p is None)
)

def values(self):
if self._velocities_provided:
return (
self.r_si,
self.t_si,
self.p_si,
self.vr_si,
self.vt_si,
self.vp_si,
self.a_si,
)
return self.r_si, self.t_si, self.p_si, self.a_si

def convert_cartesian(self):
return bl_to_cartesian_fast(
self.r_si,
self.t_si,
self.p_si,
self.a_si,
self.vr_si,
self.vt_si,
self.vp_si,
self._velocities_provided,
)

def convert_spherical(self):
transformed_cartesian = self.convert_cartesian()
cart = CartesianConversion(*transformed_cartesian)
return cart.convert_spherical()
53 changes: 24 additions & 29 deletions src/einsteinpy/coordinates/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import astropy.units as u
import numpy as np

from einsteinpy.coordinates import (
BoyerLindquistConversion,
CartesianConversion,
SphericalConversion,
)

class Cartesian:

class Cartesian(CartesianConversion):
"""
Class for Cartesian Coordinates and related transformations.
"""
Expand All @@ -22,6 +28,7 @@ def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
super().__init__(x.si.value, y.si.value, z.si.value)
self.system = "Cartesian"

def __repr__(self):
Expand Down Expand Up @@ -86,11 +93,8 @@ def to_spherical(self):
Spherical representation of the Cartesian Coordinates.
"""
r = self.norm()
theta = np.arccos(self.z / r)
phi = np.arctan2(self.y, self.x)

return Spherical(r, theta, phi)
r, theta, phi = self.convert_spherical()
return Spherical(r * u.m, theta * u.rad, phi * u.rad)

@u.quantity_input(a=u.km)
def to_bl(self, a):
Expand All @@ -108,15 +112,11 @@ def to_bl(self, a):
BL representation of the Cartesian Coordinates.
"""
w = self.norm() ** 2 - a ** 2
r = np.sqrt(0.5 * (w + np.sqrt((w ** 2) + (4 * (a ** 2) * (self.z ** 2)))))
theta = np.arccos(self.z / r)
phi = np.arctan2(self.y, self.x)

return BoyerLindquist(r, theta, phi, a)
r, theta, phi, a = self.convert_bl(a.si.value)
return BoyerLindquist(r * u.m, theta * u.rad, phi * u.rad, a * u.m)


class Spherical:
class Spherical(SphericalConversion):
"""
Class for Spherical Coordinates and related transformations.
"""
Expand All @@ -136,6 +136,7 @@ def __init__(self, r, theta, phi):
self.r = r
self.theta = theta
self.phi = phi
super().__init__(r.si.value, theta.si.value, phi.si.value)
self.system = "Spherical"

def __repr__(self):
Expand Down Expand Up @@ -169,11 +170,8 @@ def to_cartesian(self):
Cartesian representation of the Spherical Coordinates.
"""
x = self.r * np.cos(self.phi) * np.sin(self.theta)
y = self.r * np.sin(self.phi) * np.sin(self.theta)
z = self.r * np.cos(self.theta)

return Cartesian(x, y, z)
x, y, z = self.convert_cartesian()
return Cartesian(x * u.m, y * u.m, z * u.m)

@u.quantity_input(a=u.km)
def to_bl(self, a):
Expand All @@ -191,11 +189,11 @@ def to_bl(self, a):
BL representation of the Spherical Coordinates.
"""
cart = self.to_cartesian()
return cart.to_bl(a)
r, theta, phi, a = self.convert_bl(a.si.value)
return BoyerLindquist(r * u.m, theta * u.rad, phi * u.rad, a * u.m)


class BoyerLindquist:
class BoyerLindquist(BoyerLindquistConversion):
"""
Class for Spherical Coordinates and related transformations.
"""
Expand All @@ -217,6 +215,7 @@ def __init__(self, r, theta, phi, a):
self.theta = theta
self.phi = phi
self.a = a
super().__init__(r.si.value, theta.si.value, phi.si.value, a=a.si.value)
self.system = "BoyerLindquist"

def __repr__(self):
Expand Down Expand Up @@ -250,12 +249,8 @@ def to_cartesian(self):
Cartesian representation of the BL Coordinates.
"""
sin_norm = np.sqrt(self.r ** 2 + self.a ** 2) * np.sin(self.theta)
x = sin_norm * np.cos(self.phi)
y = sin_norm * np.sin(self.phi)
z = self.r * np.cos(self.theta)

return Cartesian(x, y, z)
x, y, z = self.convert_cartesian()
return Cartesian(x * u.m, y * u.m, z * u.m)

def to_spherical(self):
"""
Expand All @@ -267,5 +262,5 @@ def to_spherical(self):
Spherical representation of the BL Coordinates.
"""
cart = self.to_cartesian()
return cart.to_spherical()
r, theta, phi = self.convert_spherical()
return Spherical(r * u.m, theta * u.rad, phi * u.rad)

0 comments on commit 9ccf6d6

Please sign in to comment.