Skip to content

Bmad conversion bugfixes -- take 2 for #663 - #683

Open
jank324 wants to merge 16 commits into
masterfrom
redo-663-bmad-conversion-fixes
Open

Bmad conversion bugfixes -- take 2 for #663#683
jank324 wants to merge 16 commits into
masterfrom
redo-663-bmad-conversion-fixes

Conversation

@jank324

@jank324 jank324 commented Aug 14, 2026

Copy link
Copy Markdown
Member

Description

Redo #663 to fix issue with that PR.

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code and checked that formatting passes (required).
  • I have have fixed all issues found by flake8 (required).
  • I have ensured that all pytest tests pass (required).
  • I have run pytest on a machine with a CUDA GPU and made sure all tests pass (required).
  • I have checked that the documentation builds (required).

Note: We are using a maximum length of 88 characters per line.

@jank324
jank324 marked this pull request as draft August 14, 2026 09:33
- Move alias and type metadata extraction to bmad.py to preserve generic namelist parser separation
- Parse quoted string literals in evaluate_expression to prevent spurious PhysicsWarnings
- Simplify overlay and group skipping with a unified control regex and remove dead code
- Fix resolution of typed non-wildcard property assignments (type::name[prop])
- Use standard Bmad attribute phi0 for crab cavity phase
- Add and refine docstrings and test assertions across test suite
Comment on lines -313 to -354
def define_overlay(line: str, context: dict) -> dict:
"""
Define an overlay in the context.

:param line: Line of an overlay definition to be parsed.
:param context: Dictionary of variables to define the overlay in and from which to
read variables.
:return: Updated context.
"""

expression_match = re.fullmatch(OVERLAY_EXPRESSION_BASED_PATTERN, line)
knot_match = re.fullmatch(OVERLAY_KNOT_BASED_PATTERN, line)

if knot_match:
overlay_name = knot_match.group(1).strip()
overlay_definition = knot_match.group(2).strip()
overlay_variable = knot_match.group(3).strip()
overlay_x_knot = knot_match.group(4).strip()

context[overlay_name] = {
"overlay_definition": overlay_definition,
"overlay_variable": overlay_variable,
"overlay_x_knot": overlay_x_knot,
}
elif expression_match:
overlay_name = expression_match.group(1).strip()
overlay_definition = expression_match.group(2).strip()
overlay_variables = expression_match.group(3).strip()
if expression_match.group(4) is not None:
overlay_parameters = expression_match.group(4).strip()[1:].strip()
else:
overlay_parameters = None

context[overlay_name] = {
"overlay_definition": overlay_definition,
"overlay_variables": overlay_variables,
"overlay_parameters": overlay_parameters,
}
else:
raise ValueError(f"Overlay definition {line} not understood.")

return context

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure how good of an idea it is to remove this now, but it seems like currently this is dead code ... and who knows if it actually works. I don't remember.

Comment thread cheetah/converters/utils/infix.py Outdated
Comment thread CHANGELOG.md Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR redoes the Bmad conversion bugfix work from #663, focusing on more robust parsing of Bmad/Fortran-style expressions and property assignments, and on preserving alias/type metadata on converted Cheetah elements.

Changes:

  • Extend infix expression parsing to support unary +/- and scientific-notation tokens (e.g. 0.750e-3).
  • Update the Fortran namelist/Bmad line parser to (a) skip overlay/group control definitions and (b) better resolve wildcard + typed property assignments (e.g. lcavity::l0a[...], l0*[...]), plus support quoted string literals.
  • Improve Bmad element conversion by propagating alias/type into element metadata, handling missing e1 on bends, and adding crab_cavity conversion; add/extend test fixtures and changelog entry.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cheetah/converters/utils/infix.py Adds unary sign operators and scientific-notation tokenization improvements.
cheetah/converters/utils/fortran_namelist.py Skips overlay/group definitions, supports quoted strings, and improves wildcard/typed property assignment resolution.
cheetah/converters/bmad.py Propagates alias/type into metadata, makes sbend e1 optional, and adds crab_cavity conversion.
tests/test_infix.py Adds tests for unary-minus-before-function-call and scientific notation parsing.
tests/test_fortran_namelist.py New tests covering context eval, quoted strings, typed assignments, and skipping control definitions.
tests/test_bmad_conversion.py Adds an end-to-end conversion test for the reduced CU_HXR fixture.
tests/resources/lcls/cu_hxr.lat.bmad New reduced CU_HXR-like Bmad lattice fixture used by conversion tests.
tests/resources/lcls/cu_hxr_overlays_and_fixers.bmad New include file containing representative overlay/group/superimpose constructs.
CHANGELOG.md Documents the Bmad conversion bugfixes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cheetah/converters/utils/infix.py Outdated
Comment thread tests/test_infix.py
@jank324
jank324 marked this pull request as ready for review August 18, 2026 14:23
@jank324 jank324 added the bug Something isn't working label Aug 18, 2026
@jank324
jank324 requested review from Hespe, cr-xu and jp-ga August 18, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants