Repository navigation
Description
Under suspicion that BunchTwissAnalysis::computeBunchMoments is not working correctly, I produced some tests to compare the results of BunchTwissAnalysis to bunch statistics computed via numpy, the results of which are outlined below:
tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py:322: AssertionError
=================================================== short test summary info ===================================================
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_order2_M02 - assert 0.0 == 3.19521707947...e-08 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_order2_emitnorm_M02 - assert 0.0 == 1.0007118962927186 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_order2_dispersion_flag - assert 9.331838635982362e-08 == 9.33183590281...e-08 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_order3_M03 - assert 0.0 == 4.13235583192...e-13 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_macrosize_order2_M00_M10_M01 - assert -7.968925040526781e-05 == -0.0002851747...7959 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_macrosize_order2_M20 - assert 9.368853867319752e-08 == 8.91593940978...e-08 ± 1.0e-12
================================================ 6 failed, 24 passed in 0.16s =================================================
Moments Formula
The principle issue is that, for a 2D distribution, the central moments of overall order
where
Manually evaluating some of the low order terms yields the following:
| j | k | |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 0 |
|
| 0 | 1 |
|
| 1 | 1 |
|
| 2 | 0 |
|
| 0 | 2 |
|
Instead, these lines:
PyORBIT3/src/orbit/BunchDiagnostics/BunchTwissAnalysis.cc
Lines 250 to 261 in 51f0568
| for(i = 0; i < _order; i++) | |
| momX[i+1] = momX[i]*m_size*((part_coord_arr[ip][0] - dispterm) - xAvg); | |
| for(i = 0; i< _order; i++) | |
| momY[i+1] = momY[i]*m_size*(part_coord_arr[ip][2] - yAvg); | |
| for(j = 0; j<_order; j++) | |
| for(i=0 ; i< _order+1-j; i++){ | |
| momentXY[i][j] += momX[i]/pow(xbetaterm, double(i)) * momY[j]/pow(ybetaterm, double(j)); | |
| momentXY[i][j] += momX[i] * momY[j]; | |
| } | |
| } |
are essentially calculating
which, evaluates to:
| j | k | |
|---|---|---|
| 0 | 0 | |
| 1 | 0 | |
| 1 | 1 |
and so forth.
This mixing of normalized and raw terms leads to tests on
tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_macrosize_order2_M00_M10_M01 - assert -7.968925040526781e-05 == -0.0002851747...7959 ± 1.0e-15
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_macrosize_order2_M20 - assert 9.368853867319752e-08 == 8.91593940978...e-08 ± 1.0e-12
Tests for _order+1, so the element at the last index is always 0
FAILED tests/py/orbit/bunch_utils/test_bunch_twiss_analysis.py::test_computeBunchMoments_order3_M03 - assert 0.0 == 4.13235583192...e-13 ± 1.0e-15
| for(j = 0; j<_order; j++) |
The branch where macrosize is not set has a bug due to missing braces, but I'm not sure this branch is ever used in-practice.
PyORBIT3/src/orbit/BunchDiagnostics/BunchTwissAnalysis.cc
Lines 286 to 289 in 51f0568
| for(j = 0; j<_order; j++) | |
| for(i=0 ; i< _order+1-j; i++) | |
| momentXY[i][j] += momX[i]/pow(xbetaterm, double(i)) * momY[j]/pow(ybetaterm, double(j)); | |
| momentXY[i][j] += momX[i] * momY[j]; |
dispersionflag doesn't do anything
The dispersionflag branch is inaccessible because dispterm is explicitly initialized to 0 here:
| double dispterm = 0.; |
and is never modified, so the argument to the if-statement on L167 is always false.
Performance Implications
Lastly, analyzeBunch correctly computes means/covariances. computeBunchMoments itself calls analyzeBunch, which essentially triggers a double loop over the entire bunch every time this method is called. For the time being, users should prefer to call analyzeBunch directly rather than computeBunchMoments for computing the covariance matrix, averages, etc.
Activity
austin-hoover commentedon Jun 15, 2026
I agree that the macrosize needs to be moved outside the product sum when calculating the moments.
Maybe there should have been an option to compute normalized or unnormalized moments?
Looks like$\sqrt{\beta}$ or $\sqrt{\varepsilon \beta}$ (depending on $\varepsilon = \sqrt{\langle xx \rangle \langle x'x' \rangle - \langle xx' \rangle^2}$ and $\beta = \langle xx \rangle / \varepsilon$ .
xbetatermis actuallyemitnormflag), wherewoodtp commentedon Jun 15, 2026
Making it optional might have been the original intent, and I think that makes good sense. I could take the time to refactor a bit some point as I would also like to see the behavior of
analyzeBunchandcomputeBunchMomentsmerged to avoid unnecessarily iterating the bunch twice.Another idea that could potentially simplify things a bit is to use the methods introduced in #119 to reimplement
BunchTwissAnalysisin pure Python and offload stats computation to numpy/scipy. Maybe even bring the functionality ofBunchExtremaCalculatorinto a singleBunchAnalyzerclass or something.