Skip to content

Commit

Permalink
ENH: fix compatibility issues with newest versions of SWIFT (#4921)
Browse files Browse the repository at this point in the history
  • Loading branch information
smsutherland authored Jun 25, 2024
1 parent 01db599 commit 7f0f4b9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
32 changes: 26 additions & 6 deletions yt/frontends/swift/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from yt.data_objects.static_output import ParticleFile
from yt.frontends.sph.data_structures import SPHDataset, SPHParticleIndex
from yt.frontends.sph.fields import SPHFieldInfo
from yt.funcs import only_on_root
from yt.utilities.logger import ytLogger as mylog
from yt.utilities.on_demand_imports import _h5py as h5py

from .fields import SwiftFieldInfo


class SwiftParticleFile(ParticleFile):
pass
Expand All @@ -15,7 +16,7 @@ class SwiftParticleFile(ParticleFile):
class SwiftDataset(SPHDataset):
_load_requirements = ["h5py"]
_index_class = SPHParticleIndex
_field_info_class = SPHFieldInfo
_field_info_class = SwiftFieldInfo
_file_class = SwiftParticleFile

_particle_mass_name = "Masses"
Expand Down Expand Up @@ -97,7 +98,15 @@ def _parse_parameter_file(self):
# Read from the HDF5 file, this gives us all the info we need. The rest
# of this function is just parsing.
header = self._get_info_attributes("Header")
runtime_parameters = self._get_info_attributes("RuntimePars")
# RuntimePars were removed from snapshots at SWIFT commit 6271388
# between SWIFT versions 0.8.5 and 0.9.0
with h5py.File(self.filename, mode="r") as handle:
has_runtime_pars = "RuntimePars" in handle.keys()

if has_runtime_pars:
runtime_parameters = self._get_info_attributes("RuntimePars")
else:
runtime_parameters = {}

policy = self._get_info_attributes("Policy")
# These are the parameterfile parameters from *.yml at runtime
Expand All @@ -113,7 +122,10 @@ def _parse_parameter_file(self):
self.dimensionality = int(header["Dimension"])

# SWIFT is either all periodic, or not periodic at all
periodic = int(runtime_parameters["PeriodicBoundariesOn"])
if has_runtime_pars:
periodic = int(runtime_parameters["PeriodicBoundariesOn"])
else:
periodic = int(parameters["InitialConditions:periodic"])

if periodic:
self._periodicity = [True] * self.dimensionality
Expand All @@ -131,7 +143,14 @@ def _parse_parameter_file(self):
self.current_redshift = float(header["Redshift"])
# These won't be present if self.cosmological_simulation is false
self.omega_lambda = float(parameters["Cosmology:Omega_lambda"])
self.omega_matter = float(parameters["Cosmology:Omega_m"])
# Cosmology:Omega_m parameter deprecated at SWIFT commit d2783c2
# Between SWIFT versions 0.9.0 and 1.0.0
if "Cosmology:Omega_cdm" in parameters:
self.omega_matter = float(parameters["Cosmology:Omega_b"]) + float(
parameters["Cosmology:Omega_cdm"]
)
else:
self.omega_matter = float(parameters["Cosmology:Omega_m"])
# This is "little h"
self.hubble_constant = float(parameters["Cosmology:h"])
except KeyError:
Expand All @@ -155,9 +174,10 @@ def _parse_parameter_file(self):
# Store the un-parsed information should people want it.
self.parameters = {
"header": header,
"runtime_parameters": runtime_parameters,
"policy": policy,
"parameters": parameters,
# NOTE: runtime_parameters may be empty
"runtime_parameters": runtime_parameters,
"hydro": hydro,
"subgrid": subgrid,
}
Expand Down
27 changes: 27 additions & 0 deletions yt/frontends/swift/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from yt.frontends.sph.fields import SPHFieldInfo


class SwiftFieldInfo(SPHFieldInfo):
def __init__(self, ds, field_list, slice_info=None):
self.known_particle_fields += (
(
"InternalEnergies",
("code_specific_energy", ["specific_thermal_energy"], None),
),
("Densities", ("code_mass / code_length**3", ["density"], None)),
("SmoothingLengths", ("code_length", ["smoothing_length"], None)),
)
super().__init__(ds, field_list, slice_info)

def setup_particle_fields(self, ptype, *args, **kwargs):
super().setup_particle_fields(ptype, *args, **kwargs)

if ptype in ("PartType0", "Gas"):
self.setup_gas_particle_fields(ptype)

def setup_gas_particle_fields(self, ptype):
self.alias((ptype, "temperature"), (ptype, "Temperatures"))
self.alias(("gas", "temperature"), (ptype, "Temperatures"))

for ax in ("x", "y", "z"):
self.alias((ptype, ax), (ptype, "particle_position_" + ax))
8 changes: 7 additions & 1 deletion yt/frontends/swift/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ def _get_smoothing_length(self, sub_file, pdtype=None, pshape=None):
with h5py.File(sub_file.filename, mode="r") as f:
pcount = f["/Header"].attrs["NumPart_ThisFile"][ind].astype("int64")
pcount = np.clip(pcount - si, 0, ei - si)
keys = f[ptype].keys()
# SWIFT commit a94cc81 changed from "SmoothingLength" to "SmoothingLengths"
# between SWIFT versions 0.8.2 and 0.8.3
if "SmoothingLengths" in keys:
hsml = f[ptype]["SmoothingLengths"][si:ei, ...]
else:
hsml = f[ptype]["SmoothingLength"][si:ei, ...]
# we upscale to float64
hsml = f[ptype]["SmoothingLength"][si:ei, ...]
hsml = hsml.astype("float64", copy=False)
return hsml

Expand Down

0 comments on commit 7f0f4b9

Please sign in to comment.