From 1a0bd9394088001767c224ad181de21b17b81c8a Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Fri, 4 Nov 2022 08:48:42 -0400 Subject: [PATCH 1/6] Added neutrinos and Tcmb0 read from snapshots --- swiftsimio/conversions.py | 48 +++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index 9c57881b..4c7a4fe4 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -42,19 +42,38 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: w_0 = cosmo["w_0"][0] w_a = cosmo["w_a"][0] - # expressions taken directly from astropy, since they do no longer - # allow access to these attributes (since version 5.1+) - critdens_const = (3.0 / (8.0 * np.pi * const.G)).cgs.value - a_B_c2 = (4.0 * const.sigma_sb / const.c ** 3).cgs.value - - # SWIFT provides Omega_r, but we need a consistent Tcmb0 for astropy. - # This is an exact inversion of the procedure performed in astropy. - critical_density_0 = astropy_units.Quantity( - critdens_const * H0.to("1/s").value ** 2, - astropy_units.g / astropy_units.cm ** 3, - ) - - Tcmb0 = (Omega_r * critical_density_0.value / a_B_c2) ** (1.0 / 4.0) + # For backwards compatibility with previous cosmology constructs + # in snapshots + Tcmb0 = None + Neff = None + m_nu = None + + try: + Tcmb0 = cosmo["T_CMB_0"][0] + except (IndexError, KeyError, AttributeError): + # expressions taken directly from astropy, since they do no longer + # allow access to these attributes (since version 5.1+) + critdens_const = (3.0 / (8.0 * np.pi * const.G)).cgs.value + a_B_c2 = (4.0 * const.sigma_sb / const.c ** 3).cgs.value + + # SWIFT provides Omega_r, but we need a consistent Tcmb0 for astropy. + # This is an exact inversion of the procedure performed in astropy. + critical_density_0 = astropy_units.Quantity( + critdens_const * H0.to("1/s").value ** 2, + astropy_units.g / astropy_units.cm ** 3, + ) + + Tcmb0 = (Omega_r * critical_density_0.value / a_B_c2) ** (1.0 / 4.0) + + try: + Neff = cosmo["N_eff"][0] + except (IndexError, KeyError, AttributeError): + Neff = 3.04 # Astropy default + + try: + m_nu = cosmo["M_nu_eV"][0] + except (IndexError, KeyError, AttributeError): + m_nu = 0.0 return w0waCDM( H0=H0.to_astropy(), @@ -64,9 +83,10 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: wa=w_a, Tcmb0=Tcmb0, Ob0=Omega_b, + Neff=Neff, + m_nu=m_nu, ) - else: def swift_cosmology_to_astropy(cosmo: dict, units) -> dict: From c8d8b6b250ed0d0b233429d91029b1edbe4b71c1 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Fri, 4 Nov 2022 08:51:01 -0400 Subject: [PATCH 2/6] Formatting... --- swiftsimio/conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index 4c7a4fe4..a30d28c9 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -87,6 +87,7 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: m_nu=m_nu, ) + else: def swift_cosmology_to_astropy(cosmo: dict, units) -> dict: From 9fe33d02f08b70bd46d3bf2a76f38113009f7ee5 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Fri, 4 Nov 2022 12:34:07 -0400 Subject: [PATCH 3/6] Added [K] to CMB temperature as required --- swiftsimio/conversions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index a30d28c9..2092ff09 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -49,7 +49,7 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: m_nu = None try: - Tcmb0 = cosmo["T_CMB_0"][0] + Tcmb0 = cosmo["T_CMB_0 [K]"][0] except (IndexError, KeyError, AttributeError): # expressions taken directly from astropy, since they do no longer # allow access to these attributes (since version 5.1+) @@ -87,7 +87,6 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: m_nu=m_nu, ) - else: def swift_cosmology_to_astropy(cosmo: dict, units) -> dict: From e0b01e5b362de511e6ed0e4ea13557ec90354632 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Fri, 4 Nov 2022 12:34:56 -0400 Subject: [PATCH 4/6] Formatting" --- swiftsimio/conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index 2092ff09..d137bb4c 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -87,6 +87,7 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: m_nu=m_nu, ) + else: def swift_cosmology_to_astropy(cosmo: dict, units) -> dict: From 31f01e59e907c477dd9385f46ceca88a176e8c40 Mon Sep 17 00:00:00 2001 From: Willem Elbers Date: Tue, 13 Dec 2022 04:16:38 +0000 Subject: [PATCH 5/6] Convert SWIFT neutrino mass and degeneracy format to astropy format --- swiftsimio/conversions.py | 59 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index d137bb4c..4e4cd0d7 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -14,6 +14,43 @@ import astropy.units as astropy_units import numpy as np + def swift_neutrinos_to_astropy(N_eff, N_ur, M_nu_eV, deg_nu): + """ + Parameters + ---------- + + N_eff: float + Fractional number of effective massless neutrinos at high redshift + + N_ur: float + Fractional number of massless neutrino species + + M_nu_eV: array of floats + Masses in eV of massive species only, up to degeneracy + + deg_nu: array of floats + Fractional degeneracies of the massive neutrino species + + Returns + ------- + + ap_m_nu + Array of neutrino masses in eV, replicated according to degeneracy, + including massless species, as desired by astropy + """ + if np.isscalar(deg_nu): + deg_nu = np.array([deg_nu]) + if np.isscalar(M_nu_eV): + M_nu_eV = np.array([M_nu_eV]) + if not (deg_nu == deg_nu.astype(int)).all(): + raise AttributeError("SWIFTsimIO uses astropy, which cannot handle this cosmological model.") + if not int(N_eff) == deg_nu.astype(int).sum() + int(N_ur): + raise AttributeError("SWIFTsimIO uses astropy, which cannot handle this cosmological model.") + ap_m_nu = [[m] * int(d) for m,d in zip(M_nu_eV, deg_nu)] # replicate + ap_m_nu = sum(ap_m_nu, []) + [0.] * int(N_ur) # flatten + add massless + ap_m_nu = np.array(ap_m_nu) * astropy_units.eV + return ap_m_nu + def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: """ Parameters @@ -46,7 +83,9 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: # in snapshots Tcmb0 = None Neff = None - m_nu = None + N_ur = None + M_nu_eV = None + deg_nu = None try: Tcmb0 = cosmo["T_CMB_0 [K]"][0] @@ -71,9 +110,21 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: Neff = 3.04 # Astropy default try: - m_nu = cosmo["M_nu_eV"][0] + M_nu_eV = cosmo["M_nu_eV"] + except (IndexError, KeyError, AttributeError): + M_nu_eV = 0.0 + + try: + deg_nu = cosmo["deg_nu"] except (IndexError, KeyError, AttributeError): - m_nu = 0.0 + deg_nu = 0.0 + + try: + N_ur = cosmo["N_ur"] + except (IndexError, KeyError, AttributeError): + N_ur = 3.04 # Astropy default + + ap_m_nu = swift_neutrinos_to_astropy(Neff, N_ur, M_nu_eV, deg_nu) return w0waCDM( H0=H0.to_astropy(), @@ -84,7 +135,7 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: Tcmb0=Tcmb0, Ob0=Omega_b, Neff=Neff, - m_nu=m_nu, + m_nu=ap_m_nu, ) From 1f2071f96615054169b346073be30f626e1e12eb Mon Sep 17 00:00:00 2001 From: Matthieu Schaller Date: Wed, 18 Jan 2023 22:33:50 +0800 Subject: [PATCH 6/6] Applied code formatting script --- swiftsimio/conversions.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/swiftsimio/conversions.py b/swiftsimio/conversions.py index 4e4cd0d7..d246c1a1 100644 --- a/swiftsimio/conversions.py +++ b/swiftsimio/conversions.py @@ -43,11 +43,15 @@ def swift_neutrinos_to_astropy(N_eff, N_ur, M_nu_eV, deg_nu): if np.isscalar(M_nu_eV): M_nu_eV = np.array([M_nu_eV]) if not (deg_nu == deg_nu.astype(int)).all(): - raise AttributeError("SWIFTsimIO uses astropy, which cannot handle this cosmological model.") + raise AttributeError( + "SWIFTsimIO uses astropy, which cannot handle this cosmological model." + ) if not int(N_eff) == deg_nu.astype(int).sum() + int(N_ur): - raise AttributeError("SWIFTsimIO uses astropy, which cannot handle this cosmological model.") - ap_m_nu = [[m] * int(d) for m,d in zip(M_nu_eV, deg_nu)] # replicate - ap_m_nu = sum(ap_m_nu, []) + [0.] * int(N_ur) # flatten + add massless + raise AttributeError( + "SWIFTsimIO uses astropy, which cannot handle this cosmological model." + ) + ap_m_nu = [[m] * int(d) for m, d in zip(M_nu_eV, deg_nu)] # replicate + ap_m_nu = sum(ap_m_nu, []) + [0.0] * int(N_ur) # flatten + add massless ap_m_nu = np.array(ap_m_nu) * astropy_units.eV return ap_m_nu @@ -122,7 +126,7 @@ def swift_cosmology_to_astropy(cosmo: dict, units) -> Cosmology: try: N_ur = cosmo["N_ur"] except (IndexError, KeyError, AttributeError): - N_ur = 3.04 # Astropy default + N_ur = 3.04 # Astropy default ap_m_nu = swift_neutrinos_to_astropy(Neff, N_ur, M_nu_eV, deg_nu)