Skip to content

Commit 364748e

Browse files
authored
Merge pull request #20 from SWIFTSIM/add-cosmology
Added cosmology reading
2 parents 6a68203 + 86b0242 commit 364748e

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

docs/source/basic_usage/index.rst

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ To extract properties, you need to instantiate a
5959
Here, we have the values of ``M_200crit`` stored in kgs, correctly applied
6060
based on the unit metadata in the file.
6161

62+
There is also full unit information available in the ``data.units`` object, with
63+
an ``astropy`` cosmology object provided as ``data.units.cosmology``.
64+
6265
Creating your first plot
6366
------------------------
6467

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
2424
"Operating System :: OS Independent",
2525
],
26-
install_requires=["numpy", "unyt>=2.6.0", "h5py"],
26+
install_requires=["numpy", "unyt>=2.6.0", "h5py", "astropy"],
2727
)

velociraptor/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.10.0"
1+
__version__ = "0.10.1"

velociraptor/units.py

+64
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import unyt
88
import h5py
99

10+
from astropy.cosmology.core import Cosmology
11+
from astropy.cosmology import wCDM, FlatLambdaCDM
12+
13+
14+
15+
1016

1117
class VelociraptorUnits(object):
1218
"""
@@ -37,6 +43,7 @@ class VelociraptorUnits(object):
3743
star_formation_rate: unyt.unyt_quantity
3844
period: unyt.unyt_quantity
3945
box_volume: unyt.unyt_quantity
46+
cosmology: Cosmology
4047

4148
def __init__(self, filename: str, disregard_units: bool = False):
4249
"""
@@ -126,6 +133,7 @@ def get_unit_dictionary(self):
126133

127134
self.cosmological = bool(attributes["Cosmological_Sim"])
128135
self.comoving = bool(attributes["Comoving_or_Physical"])
136+
self.cosmology = self.load_cosmology(handle)
129137

130138
# Period is comoving.
131139
self.period = unyt.unyt_quantity(
@@ -142,3 +150,59 @@ def get_unit_dictionary(self):
142150
setattr(self, name, unit)
143151

144152
return
153+
154+
def load_cosmology(self, handle: h5py.File) -> Cosmology:
155+
"""
156+
Save the (astropy) cosmology to a HDF5 dataset.
157+
158+
Parameters
159+
----------
160+
161+
handle: h5py.File
162+
h5py file handle for the catalogue file.
163+
164+
Returns
165+
-------
166+
167+
astropy.cosmology.Cosmology:
168+
Astropy cosmology instance extracted from the HDF5 file.
169+
Also sets ``self.cosmology``.
170+
171+
"""
172+
173+
try:
174+
group = handle["Configuration"].attrs
175+
except:
176+
return None
177+
178+
# Note that some of these are unused - commented out if not.
179+
H0 = 100.0 * float(group["h_val"])
180+
w_of_DE = float(group["w_of_DE"])
181+
Omega_DE = float(group["Omega_DE"])
182+
# Omega_Lambda = float(group["Omega_Lambda"])
183+
Omega_b = float(group["Omega_b"])
184+
# Omega_cdm = float(group["Omega_cdm"])
185+
# Omega_k = float(group["Omega_k"])
186+
Omega_m = float(group["Omega_m"])
187+
# Omega_nu = float(group["Omega_nu"])
188+
# Omega_r = float(group["Omega_r"])
189+
190+
if w_of_DE != -1.0:
191+
cosmology = wCDM(
192+
H0=H0,
193+
Om0=Omega_m,
194+
Ode0=Omega_DE,
195+
w0=w_of_DE,
196+
Ob0=Omega_b,
197+
)
198+
else:
199+
# No EoS
200+
cosmology = FlatLambdaCDM(
201+
H0=H0,
202+
Om0=Omega_m,
203+
Ob0=Omega_b,
204+
)
205+
206+
self.cosmology = cosmology
207+
208+
return cosmology

0 commit comments

Comments
 (0)