Skip to content

allow amrex substantance fields to have an "_" #5115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zingale
Copy link
Member

@zingale zingale commented Feb 11, 2025

this allows for NSE protons in our output ("P_nse")

PR Summary

PR Checklist

  • New features are documented, with docstrings and narrative docs
  • Adds a test for any bugs fixed. Adds tests for new features.

this allows for NSE protons in our output ("P_nse")
@zingale zingale added enhancement Making something better frontend: amrex labels Feb 11, 2025
@zingale zingale requested a review from yut23 February 11, 2025 14:21
@yut23
Copy link
Member

yut23 commented Feb 11, 2025

@zingale Do you mind if I fix those problems and add some tests?

@zingale
Copy link
Member Author

zingale commented Feb 12, 2025

I'm happy with the changes

@chrishavlin
Copy link
Contributor

nose test failure was from an aborted run after timing out:

Build timed out (after 240 minutes). Marking the build as aborted.

probably a fluke (since other tests ran recently no problem), will trigger it again.

@chrishavlin
Copy link
Contributor

@yt-fido test this please

@chrishavlin
Copy link
Contributor

Is this one waiting on a review?

@zingale
Copy link
Member Author

zingale commented Feb 24, 2025

yes. I think that since I started it and @yut23 then modified it, someone else should review.

Copy link
Member

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, and mostly I have questions more than demands, though there's one point I'd really want to see taken into account about making escaping idempotent.

@@ -504,8 +504,8 @@ def setup_fluid_fields(self):
)


substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9]*)\)")
substance_elements_re = re.compile(r"(?P<element>[a-zA-Z]+)(?P<digits>\d*)")
substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9_]*)\)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you guys prefer the verbose style, in which case don't mind this nit, but I couldn't resist pointing out that this expression is now 1-1 equivalent

Suggested change
substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9_]*)\)")
substance_expr_re = re.compile(r"\(([a-zA-Z]\w*)\)")

for element, count in self._spec
)

def _to_tex_descriptive(self) -> str:
return str(self)
return self._tex_escape(str(self))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to update the corresponding __str__ implementation instead ?

@@ -539,18 +539,22 @@ def __str__(self) -> str:
f"{element}{count if count > 1 else ''}" for element, count in self._spec
)

@staticmethod
def _tex_escape(s: str) -> str:
return s.replace("_", r"\_")
Copy link
Member

@neutrinoceros neutrinoceros Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this snowballing into a subtle bug where a single underscore would be escaped multiple times instead of once. I'm sure we make this method idempotent using a regexp:

Suggested change
return s.replace("_", r"\_")
# replace '_' with '\_' once, but leave already-escaped occurrences alone
return re.sub(r"[^\\]_", r"\\_", s)

@@ -12,6 +12,7 @@
pytest.param("X(C12H24)", [("C", 12), ("H", 24)], id="molecule_2"),
pytest.param("X(H2O)", [("H", 2), ("O", 1)], id="molecule_3"),
pytest.param("X(ash)", [("ash", 0)], id="descriptive_name"),
pytest.param("X(P_nse)", [("P_nse", 0)], id="descriptive_underscore"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a double take here: the underscore isn't descriptive by itself, or is it ?

Suggested change
pytest.param("X(P_nse)", [("P_nse", 0)], id="descriptive_underscore"),
pytest.param("X(P_nse)", [("P_nse", 0)], id="with_an_underscore"),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the same applies to the other two test cases)

@neutrinoceros neutrinoceros added this to the 4.5.0 milestone Mar 9, 2025
@neutrinoceros
Copy link
Member

I'm adding this to the 4.5.0 milestone as it's not currently labeled as a bugfix, but if you guys would like it backported to yt 4.4.x, this seems fine to me, so don't hesitate to changie the milestone accordingly and update the labels (enhancement -> bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making something better frontend: amrex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants