Skip to content

Commit

Permalink
Test that a warning is properly raised
Browse files Browse the repository at this point in the history
  • Loading branch information
cphyc committed Feb 20, 2024
1 parent 50157ca commit e88d310
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 11 additions & 6 deletions yt/frontends/ramses/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from yt import units
from yt._typing import KnownFieldsT
from yt.fields.field_detector import FieldDetector
from yt.fields.field_info_container import FieldInfoContainer
from yt.frontends.ramses.io import convert_ramses_conformal_time_to_physical_age
from yt.utilities.cython_fortran_utils import FortranFile
Expand Down Expand Up @@ -218,12 +219,16 @@ def _temperature(field, data):
else:

def _temperature(field, data):
warnings.warn(
"Trying to calculate temperature but the cooling tables couldn't be found or read. "
"yt will return T/µ instead of T — this is equivalent to assuming µ=1.0",
category=RuntimeWarning,
stacklevel=1,
)
if not isinstance(data, FieldDetector):
warnings.warn(
"Trying to calculate temperature but the cooling tables "
"couldn't be found or read. yt will return T/µ instead of "
"T — this is equivalent to assuming µ=1.0. To suppress this, "
"derive the temperature from temperature_over_mu with "
"some values for mu.",
category=RuntimeWarning,
stacklevel=1,
)
return data["gas", "temperature_over_mu"]

self.add_field(
Expand Down
25 changes: 25 additions & 0 deletions yt/frontends/ramses/tests/test_outputs_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,28 @@ def test_field_config_2(custom_ramses_fields_conf):
assert ("ramses", f) in ds.field_list
for f in custom_grav:
assert ("gravity", f) in ds.field_list


@requires_file(output_00080)
@requires_file(ramses_new_format)
def test_warning_T2():
ds1 = yt.load(output_00080)
ds2 = yt.load(ramses_new_format)

# Should not raise warnings
ds1.r["gas", "temperature_over_mu"]
ds2.r["gas", "temperature_over_mu"]

# We cannot read the cooling tables of output_00080
# so this should raise a warning
with pytest.warns(
RuntimeWarning,
match=(
"Trying to calculate temperature but the cooling tables couldn't be "
r"found or read\. yt will return T/µ instead of T.*"
),
):
ds1.r["gas", "temperature"]

# But this one should not
ds2.r["gas", "temperature"]

0 comments on commit e88d310

Please sign in to comment.