Skip to content

Commit

Permalink
Modules now depend on astropy units and constants
Browse files Browse the repository at this point in the history
Fix #3
  • Loading branch information
sambit-giri committed Dec 9, 2024
1 parent 4ade669 commit 2faefed
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion notebooks/model_universes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
requirements = f.read().splitlines()

setup(name='AstronomyCalc',
version='1.0.0',
version='1.0.1',
author='Sambit Giri',
author_email='[email protected]',
packages=find_packages("src"),
Expand Down
4 changes: 0 additions & 4 deletions src/AstronomyCalc/constants.py

This file was deleted.

5 changes: 2 additions & 3 deletions src/AstronomyCalc/cosmo_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from .basic_functions import *
from .cosmo_equations import *
from . import constants as const

def age_estimator(param, z):
'''
Expand All @@ -19,8 +18,8 @@ def age_estimator(param, z):
Feq = FriedmannEquation(param)
a = z_to_a(z)
I = lambda a: 1/a/Feq.H(a=a)
t = lambda a: quad(I, 0, a)[0]*const.Mpc_to_km/const.Gyr_to_s
return np.vectorize(t)(a)
t = lambda a: quad(lambda x: I(x).value, 0, a)[0]
return (np.vectorize(t)(a)*I(1).unit).to('Gyr')

def Hubble(param, z=None, a=None):
"""
Expand Down
19 changes: 10 additions & 9 deletions src/AstronomyCalc/cosmo_equations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from scipy.integrate import quad
from astropy import units as u
from astropy import constants as const

from .basic_functions import *
from . import constants as const

class FriedmannEquation:
'''
Expand Down Expand Up @@ -33,15 +34,15 @@ def create_functions(self):

def H(self, z=None, a=None):
assert z is not None or a is not None
if z is not None: return self.Hz(z)
else: return self.Ha(a)
if z is not None: return self.Hz(z)*u.km/u.Mpc/u.s
else: return self.Ha(a)*u.km/u.Mpc/u.s

def age(self, z=None, a=None):
assert z is not None or a is not None
if a is None: a = z_to_a(z)
I = lambda a: 1/a/self.H(a=a)
t = lambda a: quad(I, 0, a)[0]*const.Mpc_to_km/const.Gyr_to_s
return np.vectorize(t)(a)
t = lambda a: quad(lambda x: I(x).value, 0, a)[0]
return (np.vectorize(t)(a)*I(1).unit).to('Gyr')

class CosmoDistances(FriedmannEquation):
'''
Expand All @@ -65,11 +66,11 @@ def __init__(self, param):
self.create_functions()

def Hubble_dist(self):
return const.c_kmps/self.H(z=0)
return const.c/self.H(z=0)

def _comoving_dist(self, z):
I = lambda z: const.c_kmps/self.H(z=z)
return quad(I, 0, z)[0] # Mpc
I = lambda z: const.c/self.H(z=z)
return (quad(lambda x: I(x).value, 0, z)[0]*I(0).unit).to('Mpc') # Mpc

def comoving_dist(self, z=None, a=None):
if a is not None: z = a_to_z(a)
Expand All @@ -82,7 +83,7 @@ def proper_dist(self, z=None, a=None):
def light_travel_dist(self, z=None, a=None):
t0 = self.age(z=0)
te = self.age(z=z, a=a)
return const.c_kmps*(t0-te)*const.Gyr_to_s/const.Mpc_to_km
return (const.c*(t0-te)).to('Mpc')

def angular_dist(self, z=None, a=None):
dc = self.comoving_dist(z=z, a=a)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cosmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def test_param():

def test_age_estimator():
param = astro.param()
t0 = astro.age_estimator(param, 0)
t0 = astro.age_estimator(param, 0).value
assert np.abs(t0-13.74)<0.01

def test_cosmo_dist():
param = astro.param()
dist = astro.CosmoDistances(param)
cdist = dist.comoving_dist(1) #3380.72792719
ldist = dist.luminosity_dist(1) #6761.45585438037
cdist = dist.comoving_dist(1).value #3380.72792719
ldist = dist.luminosity_dist(1).value #6761.45585438037
assert np.abs(cdist-3380.73)<0.1 and np.abs(ldist-6761.46)<0.1

def test_hubble():
Expand Down

0 comments on commit 2faefed

Please sign in to comment.