Skip to content

Conversation

@lcarver
Copy link
Contributor

@lcarver lcarver commented Dec 5, 2025

There has been a major bug present in the beam loading module since it was first written. To summarise, the beam induced voltage used in the cavity feedback was taken at the wrong point. For a single harmonic system, (like the work published in IPAC : https://inspirehep.net/files/cc5691837fadff23276bd1022058a71e ) the passmethod was working as expected and this bug was not visible, however when a harmonic cavity was present, the phase of the beam induced voltage became incorrect, causing unstable results. This was not the correct behaviour.

This PR completely remodels the computation to compute the beam induced voltage identically to mbtrack.

This PR is a work in progress. Still to do:

  1. In the PassMethod I need the full fill pattern and the total number of buckets for the main RF as I also want to record the vbeam for the empty buckets. At the moment all I can pass is bunch_spos and bunch_currents which have length of nbunch. For now I do a dirty reconstruction of the fill pattern and I compute the ring harmonic number from the RF frequency, but needs to be cleaned up.

  2. Benchmark uniform, 16b and asymmetric fill pattern with MBTRACK. Check with multiple slices per bunch and 1 slice per bunch.

  3. Check the passive cavity cases including the passive cavity feedback.

  4. Check buffers and windowing.

  5. Documentation.

  6. Update examples.

Other relevant major changes:

BLMode.WAKE has been removed in its entirety, it was not useful, slow and not needed. The tests have been updated and the default and now only method is PHASOR.

Vgen was an array of two [Vgen, psi], now it is an array of four [Vgen, thetag, psi, Vgr]. This has been updated everywhere, but breaks a bit some old scripts.

The ring harmonic number and the filling pattern are now passed to the passmethod through the ring param.

@lcarver lcarver added WIP work in progress Python For python AT code bug fix C For C code / pass methods labels Dec 5, 2025
@lcarver lcarver requested review from lfarv and swhite2401 and removed request for lfarv December 5, 2025 15:00
@lcarver
Copy link
Contributor Author

lcarver commented Jan 19, 2026

Comparing MBTRACK and PyAT for uniform filling, Nslice=200 per bunch (in pyat, Nslices=1001 but fixed in mbtrack), Nparts_per_bunch=100k, Single mode only.

image image image

The FBOff case agrees very well, this shows that the vbeam kick is correctly computed and applied to the slices.
The dynamic behaviour of the FBWeak and FBStrong cases agree nicely. There is an offset present in both cases, which is very small (0.2ps!), but nonetheless it is there. There are a few differences between PyAT and MBTRACK that can account for this. The first is that MBTRACK uses Fixed Slicing (Nslices=1001 over the range +- 50sigma, where sigma=3mm. This gives a bin width of 0.01ps in mbtrack, compared to PyAT which uses dynamic slicing. Also in MBTRACK they track a frozen beam distribution to initialise the generator voltage settings, this means the start point is not quite identical. I don't rule out some small difference in the implementation that I am not yet aware of, but it is very close and the dynamic behaviour is the same.

@lcarver
Copy link
Contributor Author

lcarver commented Jan 20, 2026

Below: Main cavity + 4HC, 200mA uniform mode, ~100k particles per bunch in pyat, ~50k particles per bunch in mbtrack, 200 slices per bunch in pyat, 1001 slices from +- 50*3mm in mbtrack, strong feedback (Vgain=1, PhaseGain=0.001 on main, PhaseGain=0.01 on 4HC)

image

Below is TBL for 7/8 filling, starting with bucket 0 filled.
image

Below is TBL for 7/8 filling, starting with bucket 0 empty.
image

Starting with bucket 0 empty seems to cause a problem in the loop. I will check.
TBL case now agrees beautifully.

Below:
16b, the reduced bunch length comes from the short range wake of the cavities.
image

All good!

I do not plan to test 16b mode with a short range wakefield, I will save it for a later date as I do not currently know how to do it in MBTRACK. So it will take some more time. However, I know the individual components work (SR wake alone, and beam loading alone) I just need to verify the case with both together.

@lcarver
Copy link
Contributor Author

lcarver commented Jan 20, 2026

Fully ready for review and merging.

@swhite2401
Copy link
Contributor

Very nice, only few minor comments!
I trust you on the physics / formulas, benchmarking looks good.

@lfarv
Copy link
Contributor

lfarv commented Jan 21, 2026

@lcarver : Nice job, and thanks @swhite2401 for reviewing. I trust you both.
I suggest running ruff: it suggests a number of code improvements, and also mypy for typing errors (there are also quite some…).
But none of those will change the behaviour nor the results, so it's ok for me as it is.

@lcarver
Copy link
Contributor Author

lcarver commented Jan 21, 2026

I fixed the TBL case and made the changes suggest by @swhite2401. Please read the docs again, if you think it is not clear, feel free to make changes yourself.

Given that I found a bug in the TBL case, I plan to check a quick 16b case too to make sure I haven't missed anything.

@lcarver
Copy link
Contributor Author

lcarver commented Jan 21, 2026

@lcarver : Nice job, and thanks @swhite2401 for reviewing. I trust you both. I suggest running ruff: it suggests a number of code improvements, and also mypy for typing errors (there are also quite some…). But none of those will change the behaviour nor the results, so it's ok for me as it is.

ruff was nice, and i used it. mypy has made many many suggestions and many of them I do not understand. I'm sorry, I am not so knowledgable on the type formatting.
image

@lfarv
Copy link
Contributor

lfarv commented Jan 21, 2026

@lcarver : Agreed, forget typing !

But running ruff with the configuration I propose in #1048 still gives many more suggestions:

(pyat) laurent@garou at % ruff check pyat/at/collective/beam_loading.py 
pyat/at/collective/beam_loading.py:5:1: UP035 [*] Import from `collections.abc` instead: `Sequence`
pyat/at/collective/beam_loading.py:40:1: W293 Blank line contains whitespace
pyat/at/collective/beam_loading.py:51:14: UP007 [*] Use `X | Y` for type annotations
pyat/at/collective/beam_loading.py:52:13: UP007 [*] Use `X | Y` for type annotations
pyat/at/collective/beam_loading.py:54:11: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:57:5: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:83:26: B905 `zip()` without an explicit `strict=` parameter
pyat/at/collective/beam_loading.py:93:24: B905 `zip()` without an explicit `strict=` parameter
pyat/at/collective/beam_loading.py:103:59: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:104:5: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:120:26: B905 `zip()` without an explicit `strict=` parameter
pyat/at/collective/beam_loading.py:150:5: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:185:17: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:186:21: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:187:17: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:188:21: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:191:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:261:17: ISC003 [*] Explicitly concatenated string should be implicitly concatenated
pyat/at/collective/beam_loading.py:283:21: ISC003 [*] Explicitly concatenated string should be implicitly concatenated
pyat/at/collective/beam_loading.py:292:17: ISC003 [*] Explicitly concatenated string should be implicitly concatenated
pyat/at/collective/beam_loading.py:318:14: UP008 Use `super()` instead of `super(__class__, self)`
pyat/at/collective/beam_loading.py:376:21: ISC003 [*] Explicitly concatenated string should be implicitly concatenated
pyat/at/collective/beam_loading.py:379:17: B028 No explicit `stacklevel` keyword argument found
pyat/at/collective/beam_loading.py:385:14: PLR1714 Consider merging multiple comparisons: `self._cavitymode in {2, 3}`.
pyat/at/collective/beam_loading.py:421:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:426:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:431:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:436:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:446:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:456:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:461:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:473:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:478:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:483:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:488:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:501:21: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:502:21: UP045 [*] Use `X | None` for type annotations
pyat/at/collective/beam_loading.py:505:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:536:17: B028 No explicit `stacklevel` keyword argument found
pyat/at/collective/beam_loading.py:551:9: D415 First line should end with a period, question mark, or exclamation point
pyat/at/collective/beam_loading.py:554:15: C402 Unnecessary generator (rewrite as a dict comprehension)
pyat/at/collective/beam_loading.py:555:16: UP030 Use implicit references for positional format fields
pyat/at/collective/beam_loading.py:555:16: UP032 [*] Use f-string instead of `format` call
Found 43 errors.
[*] 16 fixable with the `--fix` option (27 hidden fixes can be enabled with the `--unsafe-fixes` option).

@lcarver
Copy link
Contributor Author

lcarver commented Jan 23, 2026

I ignore the following ones:

cluster-access-03:/machfs/carver/pyat_dev/at % ruff check pyat/at/collective/beam_loading.py 
pyat/at/collective/beam_loading.py:5:1: UP035 [*] Import from `collections.abc` instead: `Sequence`
pyat/at/collective/beam_loading.py:283:21: EM101 Exception must not use a string literal, assign to variable first
pyat/at/collective/beam_loading.py:292:17: EM101 Exception must not use a string literal, assign to variable first
pyat/at/collective/beam_loading.py:310:30: EM101 Exception must not use a string literal, assign to variable first
pyat/at/collective/beam_loading.py:319:14: UP008 Use `super()` instead of `super(__class__, self)`
pyat/at/collective/beam_loading.py:377:21: ISC003 [*] Explicitly concatenated string should be implicitly concatenated
pyat/at/collective/beam_loading.py:380:17: B028 No explicit `stacklevel` keyword argument found
pyat/at/collective/beam_loading.py:537:17: B028 No explicit `stacklevel` keyword argument found
pyat/at/collective/beam_loading.py:555:15: C402 Unnecessary generator (rewrite as a dict comprehension)
pyat/at/collective/beam_loading.py:556:16: UP030 Use implicit references for positional format fields
pyat/at/collective/beam_loading.py:556:16: UP032 [*] Use f-string instead of `format` call

@lcarver
Copy link
Contributor Author

lcarver commented Jan 23, 2026

OK to merge?

@lfarv
Copy link
Contributor

lfarv commented Jan 23, 2026

ignore the following ones:

Ok for me. But if you look at the documentation for those messages, you will see that the suggestions are really beneficial !

@lcarver
Copy link
Contributor Author

lcarver commented Jan 23, 2026

ignore the following ones:

Ok for me. But if you look at the documentation for those messages, you will see that the suggestions are really beneficial !

I needed convincing by Simon, a few of them to be seemed arbitrary but I have been convinced. I put all of the suggestions in.

@lcarver
Copy link
Contributor Author

lcarver commented Jan 23, 2026

all implemented

@lcarver lcarver merged commit 0c4a00c into master Jan 23, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix C For C code / pass methods Python For python AT code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants