Skip to content

Multipole implementation - #399

Draft
ThorstenHellert wants to merge 7 commits into
desy-ml:masterfrom
ThorstenHellert:master
Draft

Multipole implementation#399
ThorstenHellert wants to merge 7 commits into
desy-ml:masterfrom
ThorstenHellert:master

Conversation

@ThorstenHellert

Copy link
Copy Markdown

WIP: Add multipole element implementation with tests and benchmarks

Description

This PR adds a general multipole element to Cheetah's accelerator elements. It includes:

  • A new Multipole class in cheetah/accelerator/multipole.py that implements arbitrary-order normal and skew multipole fields
  • Comprehensive unit tests in tests/test_multipole.py verifying that the multipole implementation correctly reproduces the behavior of drift elements, dipoles, and quadrupoles when configured with appropriate coefficients
  • Benchmarking code in benchmarks/benchmark_multipole_pyat.py that compares Cheetah's multipole implementation against pyAT for validation and performance analysis

Motivation and Context

Adding a general multipole element enhances Cheetah's capabilities for accurate accelerator simulations. This implementation:

  • Allows simulation of higher-order magnetic field components
  • Provides a unified framework for representing various magnetic elements
  • Enables more realistic modeling of real-world accelerator components with field imperfections
  • Helps with realistic simulations that include fringe field effects

The implementation includes comprehensive benchmark tests against pyAT's multipole element which all work reasonably well. I've cross-checked it with Cheetah's corrector magnet and quadrupole magnet implementations using the beam matrix tracking approach.


Note that vectorization is not yet included in this implementation.


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).

Comment thread tests/test_multipole.py Outdated
outgoing_drift = drift.track(incoming)

assert torch.allclose(
outgoing_multipole.particles, outgoing_drift.particles, atol=1e-5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this tolerance way too high?

Comment thread tests/test_multipole.py Outdated
Comment on lines +51 to +57
# Both should cause focusing/defocusing, but exact values will differ due to
# different tracking methods. Just verify they both affect the beam.
assert abs(outgoing_multipole.mu_px) > 1e-9
assert abs(outgoing_quadrupole.mu_px) > 1e-9

# The sign of px change should be the same for both
assert torch.sign(outgoing_multipole.mu_px) == torch.sign(outgoing_quadrupole.mu_px)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These assertions also seem odd to me. Wouldn't it be better to compare particle cloud to particle cloud?

Comment thread tests/test_multipole.py
k1 = torch.tensor(4.2)

multipole = cheetah.Multipole(
length=torch.tensor(1.0), polynom_b=torch.tensor([0.0, k1]) # B1 = k1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure with the terminology, but I feel like it should be made to match Cheetah's existing terminology.

Comment thread tests/test_multipole.py Outdated
Comment on lines +81 to +85
# Verify they produce similar results
assert torch.allclose(outgoing_corrector.mu_px, outgoing_multipole.mu_px, atol=1e-9)
assert torch.allclose(outgoing_corrector.mu_py, outgoing_multipole.mu_py, atol=1e-9)
assert torch.allclose(outgoing_corrector.mu_x, outgoing_multipole.mu_x, atol=1e-9)
assert torch.allclose(outgoing_corrector.mu_y, outgoing_multipole.mu_y, atol=1e-9)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same question here: This feels like comparing the particle arrays would be much better. The current version doesn't capture everything and yet requires a lot more code.

Comment thread tests/test_multipole.py
assert torch.allclose(outgoing_corrector.mu_y, outgoing_multipole.mu_y, atol=1e-9)


def test_multipole_with_misalignment():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test seems superfluous to me, if we are already testing against a quadrupole.

Comment thread tests/test_multipole.py
)


def test_skew_multipole_behavior():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here: If we already trust the multipole to behave like a quadrupole, why do this?

@jank324
jank324 marked this pull request as draft April 10, 2025 14:26
@jank324 jank324 changed the title WIP: Add multipole implementation and benchmarking Multipole implementation Apr 10, 2025
@jank324 jank324 mentioned this pull request Apr 15, 2025
5 tasks
@jank324 jank324 added the enhancement New feature or request label Apr 22, 2025
@jank324 jank324 mentioned this pull request Apr 22, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants