Skip to content

Commit

Permalink
Move _particle_mass_is_mass attribute to io handler and implement cor…
Browse files Browse the repository at this point in the history
…rection function.
  • Loading branch information
brittonsmith committed May 25, 2022
1 parent a5de9e8 commit adcdcf9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
7 changes: 0 additions & 7 deletions yt/frontends/enzo_e/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,6 @@ def _parse_parameter_file(self):
with open(lcfn) as lf:
self.parameters = libconf.load(lf)

# Determine if particle masses are actually densities by the
# existence of the "mass_is_mass" particles parameter.
mass_flag = nested_dict_get(
self.parameters, ("Particle", "mass_is_mass"), default=None
)
self._particle_mass_is_mass = mass_flag is not None

cosmo = nested_dict_get(self.parameters, ("Physics", "cosmology"))
if cosmo is not None:
self.cosmological_simulation = 1
Expand Down
12 changes: 5 additions & 7 deletions yt/frontends/enzo_e/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from yt.fields.field_info_container import FieldInfoContainer
from yt.fields.magnetic_field import setup_magnetic_field_aliases
from yt.fields.particle_fields import add_union_field
from yt.frontends.enzo_e.misc import nested_dict_get
from yt.frontends.enzo_e.misc import \
get_particle_mass_correction, \
nested_dict_get

rho_units = "code_mass / code_length**3"
vel_units = "code_velocity"
Expand Down Expand Up @@ -149,12 +151,8 @@ def setup_particle_mass_field(self, ptype):
return

val = constants[names.index("mass")][2] * self.ds.mass_unit
if not getattr(self.ds, "_particle_mass_is_mass", False):
val = (
val
* (self.ds.domain_width / self.ds.domain_dimensions).prod()
/ self.ds.length_unit**3
)
if not self.ds.index.io._particle_mass_is_mass:
val = val * get_particle_mass_correction(self.ds)

def _pmass(field, data):
return val * data[ptype, "particle_ones"]
Expand Down
19 changes: 12 additions & 7 deletions yt/frontends/enzo_e/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np

from yt.frontends.enzo_e.misc import \
get_particle_mass_correction, \
nested_dict_get
from yt.utilities.exceptions import YTException
from yt.utilities.io_handler import BaseIOHandler
from yt.utilities.on_demand_imports import _h5py as h5py
Expand All @@ -18,6 +21,13 @@ def __init__(self, *args, **kwargs):
slice(self.ds.ghost_zones, -self.ds.ghost_zones),
)

# Determine if particle masses are actually densities by the
# existence of the "mass_is_mass" particles parameter.
mass_flag = nested_dict_get(
self.ds.parameters, ("Particle", "mass_is_mass"), default=None
)
self._particle_mass_is_mass = mass_flag is not None

def _read_field_names(self, grid):
if grid.filename is None:
return []
Expand Down Expand Up @@ -116,13 +126,8 @@ def _read_particle_fields(self, chunks, ptf, selector):
continue
for field in field_list:
data = np.asarray(group.get(pn % field)[()], "=f8")
if field == "mass" and not getattr(
self.ds, "_particle_mass_is_mass", False
):
mfac = (
self.ds.domain_width / self.ds.domain_dimensions
).prod() / self.ds.length_unit**3
data[mask] *= mfac
if field == "mass" and not self._particle_mass_is_mass:
data[mask] *= get_particle_mass_correction(self.ds)
yield (ptype, field), data[mask]
if f:
f.close()
Expand Down
10 changes: 10 additions & 0 deletions yt/frontends/enzo_e/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,13 @@ def nested_dict_get(pdict, keys, default=None):
except KeyError:
return default
return val

def get_particle_mass_correction(ds):
"""
Normalize particle masses by the root grid cell volume.
This correction is used for Enzo-E datasets where particle
masses are stored as densities.
"""

return (ds.domain_width / ds.domain_dimensions).prod() / ds.length_unit**3

0 comments on commit adcdcf9

Please sign in to comment.