Skip to content

Commit 2d7d250

Browse files
authored
Merge pull request #5155 from mabruzzo/cholla-mu-fix
BUG: Better behavior when Cholla doesn't write "mu" attribute
2 parents 60abc14 + 2fed009 commit 2d7d250

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

yt/frontends/cholla/data_structures.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from yt.funcs import setdefaultattr
99
from yt.geometry.api import Geometry
1010
from yt.geometry.grid_geometry_handler import GridIndex
11+
from yt.utilities.logger import ytLogger as mylog
1112
from yt.utilities.on_demand_imports import _h5py as h5py
1213

1314
from .fields import ChollaFieldInfo
@@ -111,7 +112,19 @@ def _parse_parameter_file(self):
111112
self.current_time = attrs["t"][:]
112113
self._periodicity = tuple(attrs.get("periodicity", (False, False, False)))
113114
self.gamma = attrs.get("gamma", 5.0 / 3.0)
114-
self.mu = attrs.get("mu", 1.0)
115+
if (self.default_species_fields is not None) and "mu" in attrs:
116+
raise ValueError(
117+
'default_species_fields must be None when "mu" is an hdf5 attribute'
118+
)
119+
elif "mu" in attrs:
120+
self.mu = attrs["mu"]
121+
elif self.default_species_fields is None:
122+
# other yt-machinery can't handle ds.mu == None, so we simply
123+
# avoid defining the mu attribute if we don't know its value
124+
mylog.info(
125+
'add the "mu" hdf5 attribute OR use the default_species_fields kwarg '
126+
"to compute temperature"
127+
)
115128
self.refine_by = 1
116129

117130
# If header specifies code units, default to those (in CGS)

yt/frontends/cholla/fields.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,23 @@ def _specific_total_energy(field, data):
8787
)
8888

8989
# Add temperature field
90-
def _temperature(field, data):
91-
return (
92-
data.ds.mu
93-
* data["gas", "pressure"]
94-
/ data["gas", "density"]
95-
* mh
96-
/ kboltz
97-
)
90+
if hasattr(self.ds, "mu"):
91+
92+
def _temperature(field, data):
93+
return (
94+
data.ds.mu
95+
* data["gas", "pressure"]
96+
/ data["gas", "density"]
97+
* mh
98+
/ kboltz
99+
)
98100

99-
self.add_field(
100-
("gas", "temperature"),
101-
sampling_type="cell",
102-
function=_temperature,
103-
units=unit_system["temperature"],
104-
)
101+
self.add_field(
102+
("gas", "temperature"),
103+
sampling_type="cell",
104+
function=_temperature,
105+
units=unit_system["temperature"],
106+
)
105107

106108
# Add color field if present (scalar0 / density)
107109
if ("cholla", "scalar0") in self.field_list:

0 commit comments

Comments
 (0)