Skip to content
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

TWISS drift with EXACT flag has errors in the time variable #1123

Merged
3 changes: 2 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MAD-X master
* [PR 1081](https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1108) Fix potential buffer overrun when node_name calls stoupper (S. Berg)
* (https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1123) Fix errors in the time variable with EXACT flag to TWISS (J. S. Berg)
* [PR 1081](https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1108) Fix potential buffer overrun when node_name calls stoupper (J. S. Berg)
* [PR 1107](https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1107) Stabizes few tests due to compiler dependent numerical noise (R. De Maria)
* [PR 1095](https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1095) Implement more robust, optional, PTC DA map output (L. Deniau)
* [PR 1088](https://github.com/MethodicalAcceleratorDesign/MAD-X/pull/1088) Additional explanation bv flag (J. Dilly)
Expand Down
4 changes: 3 additions & 1 deletion Makefile_test
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# added test-twiss-12 for sigma matrix calculation
# 2021-Sep-02 20:59:00 ghislain:
# added test-taper
# 2022-Jun-21 jsberg-bnl:
# added test-twiss-16 for exact TWISS drift compared to PTC
#################################################
# testsuite

Expand All @@ -55,7 +57,7 @@ test-track-7 test-track-8 test-track-9 test-track-10 test-track-11 test-track-12
test-track-acd test-track-rotations test-beambeam-npart \
test-twiss test-twiss-2 test-twiss-3 test-twiss-4 test-twiss-5 \
test-twiss-6 test-twiss-8 test-twiss-9 test-twiss-10 test-twiss-11 \
test-twiss-12 test-twiss-13 test-twiss-14 test-twiss-15 test-twiss-exact test-translation test-crabcavity \
test-twiss-12 test-twiss-13 test-twiss-14 test-twiss-15 test-twiss-16 test-twiss-exact test-translation test-crabcavity \
test-xrotation test-yrotation test-rotations test-interpolate test-rf-fringe \
test-cororbit test-cororbit-2 test-cororbit-3 test-cororbit-4 \
test-emit test-emit-2 \
Expand Down
105 changes: 58 additions & 47 deletions src/twiss.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7159,7 +7159,8 @@ SUBROUTINE tmdrf(fsec,ftrk,orbit,fmap,dl,ek,re,te)
logical :: fsec, ftrk, fmap
double precision :: dl
double precision :: orbit(6), ek(6), re(6,6), te(6,6,6)
double precision :: px, py, pt, csq, l_pz, c3sq, c52sq
double precision :: px, py, pt, csq, l_pz
real(kind(1d0)) :: beti, dl3, dl5, pp2, ptb, ptot2, px2, py2, pz

!---- Initialize.
EK = zero
Expand All @@ -7171,56 +7172,66 @@ SUBROUTINE tmdrf(fsec,ftrk,orbit,fmap,dl,ek,re,te)
px = orbit(2)
py = orbit(4)
pt = orbit(6)

csq = 1 + 2*pt/beta + pt**2 - px**2 - py**2
l_pz = dl / sqrt(csq)
c3sq = csq**(3d0/2d0)
c52sq =csq**(5d0/2d0)

re(1,2) = dl/sqrt(csq) + dl*px**2/c3sq
re(1,4) = dl*px*py/c3sq
re(1,6) = dl*px*(-pt - 1d0/beta)/c3sq
re(3,2) = dl*px*py/c3sq
re(3,4) = dl/sqrt(csq) + dl*py**2/c3sq
re(3,6) = dl*py*(-pt - 1d0/beta)/c3sq
re(5,2) = -dl*px*(beta + pt)/c3sq
re(5,4) = -dl*py*(beta + pt)/c3sq
re(5,6) = -dl/sqrt(csq) - dl*(beta + pt)*(-pt - 1d0/beta)/c3sq
px2 = px*px
py2 = py*py

beti = 1/beta
ptb = beti + pt
ptot2 = 1 + pt*(2*beti + pt)
pp2 = (beta*gamma)**(-2)+px2+py2
csq = ptot2 - px2 - py2
pz = sqrt(csq)
l_pz = dl / pz
dl3 = l_pz / csq


re(1,2) = dl3*(ptot2 - py2)
re(1,4) = dl3*px*py
re(1,6) = -dl3*px*ptb
re(3,2) = re(1,4)
re(3,4) = dl3*(ptot2 - px2)
re(3,6) = -dl3*py*ptb
re(5,2) = re(1,6)
re(5,4) = re(3,6)
re(5,6) = dl3*pp2

if (fsec) then
te(1,2,2) = 3d0*dl*px/(2d0*c3sq) + 3d0*dl*px**3d0/(2d0*c52sq)
te(1,2,4) = dl*py/(2d0*c3sq) + 3d0*dl*px**2d0*py/(2d0*c52sq)
te(1,2,6) = dl*(-pt - 1d0/beta)/(2d0*c3sq) + dl*px**2d0*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(1,4,2) = dl*py/(2d0*c3sq) + 3d0*dl*px**2d0*py/(2d0*c52sq)
te(1,4,4) = dl*px/(2d0*c3sq) + 3d0*dl*px*py**2d0/(2d0*c52sq)
te(1,4,6) = dl*px*py*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(1,6,2) = dl*(-pt - 1d0/beta)/(2d0*c3sq) + 3d0*dl*px**2d0*(-pt - 1d0/beta)/(2d0*c52sq)
te(1,6,4) = 3d0*dl*px*py*(-pt - 1d0/beta)/(2d0*c52sq)
te(1,6,6) = -dl*px/(2d0*c3sq) + dl*px*(-3d0*pt - 3d0/beta)*(-pt - 1d0/beta)/(2d0*c52sq)
te(3,2,2) = dl*py/(2d0*c3sq) + 3d0*dl*px**2d0*py/(2d0*c52sq)
te(3,2,4) = dl*px/(2d0*c3sq) + 3d0*dl*px*py**2d0/(2d0*c52sq)
te(3,2,6) = dl*px*py*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(3,4,2) = dl*px/(2d0*c3sq) + 3d0*dl*px*py**2d0/(2d0*c52sq)
te(3,4,4) = 3d0*dl*py/(2d0*c3sq) + 3d0*dl*py**3d0/(2d0*c52sq)
te(3,4,6) = dl*(-pt - 1d0/beta)/(2d0*c3sq) + dl*py**2d0*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(3,6,2) = 3d0*dl*px*py*(-pt - 1d0/beta)/(2d0*c52sq)
te(3,6,4) = dl*(-pt - 1d0/beta)/(2d0*c3sq) + 3d0*dl*py**2d0*(-pt - 1d0/beta)/(2d0*c52sq)
te(3,6,6) = -dl*py/(2d0*c3sq) + dl*py*(-3d0*pt - 3d0/beta)*(-pt - 1d0/beta)/(2d0*c52sq)
te(5,2,2) = -dl*(beta + pt)/(2d0*c3sq) - 3d0*dl*px**2d0*(beta + pt)/(2d0*c52sq)
te(5,2,4) = -3d0*dl*px*py*(beta + pt)/(2d0*c52sq)
te(5,2,6) = -dl*px/(2d0*c3sq) - dl*px*(beta + pt)*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(5,4,2) = -3d0*dl*px*py*(beta + pt)/(2d0*c52sq)
te(5,4,4) = -dl*(beta + pt)/(2d0*c3sq) - 3d0*dl*py**2d0*(beta + pt)/(2d0*c52sq)
te(5,4,6) = -dl*py/(2d0*c3sq) - dl*py*(beta + pt)*(-3d0*pt - 3d0/beta)/(2d0*c52sq)
te(5,6,2) = -dl*px/(2d0*c3sq) - 3d0*dl*px*(beta + pt)*(-pt - 1d0/beta)/(2d0*c52sq)
te(5,6,4) = -dl*py/(2d0*c3sq) - 3d0*dl*py*(beta + pt)*(-pt - 1d0/beta)/(2d0*c52sq)
te(5,6,6) = dl*(beta + pt)/(2d0*c3sq) - dl*(-pt - 1d0/beta)/c3sq - &
dl*(beta + pt)*(-3d0*pt - 3d0/beta)*(-pt - 1d0/beta)/(2d0*c52sq)
dl5 = 0.5d0*dl3/csq
te(1,2,2) = 3*dl5*px*(ptot2 - py2)
te(1,2,4) = dl5*py*(ptot2 + 2*px2 - py2)
te(1,2,6) = -dl5*ptb*(ptot2 + 2*px2 - py2)
te(1,4,2) = te(1,2,4)
te(1,4,4) = dl5*px*(ptot2 - px2 + 2*py2)
te(1,4,6) = -3*dl5*px*py*ptb
te(1,6,2) = te(1,2,6)
te(1,6,4) = te(1,4,6)
te(1,6,6) = dl5*px*(2*ptb*ptb + pp2)
te(3,2,2) = te(1,2,4)
te(3,2,4) = te(1,4,4)
te(3,2,6) = te(1,4,6)
te(3,4,2) = te(3,2,4)
te(3,4,4) = 3*dl5*py*(ptot2 - px2)
te(3,4,6) = -dl5*ptb*(ptot2 - px2 + 2*py2)
te(3,6,2) = te(3,2,6)
te(3,6,4) = te(3,4,6)
te(3,6,6) = dl5*py*(2*ptb*ptb + pp2)
te(5,2,2) = te(1,2,6)
te(5,2,4) = te(1,4,6)
te(5,2,6) = te(1,6,6)
te(5,4,2) = te(5,2,4)
te(5,4,4) = te(3,4,6)
te(5,4,6) = te(3,6,6)
te(5,6,2) = te(5,2,6)
te(5,6,4) = te(5,4,6)
te(5,6,6) = -3*dl5*pp2*ptb
endif

orbit(1) = orbit(1) + px*l_pz
orbit(3) = orbit(3) + py*l_pz
orbit(5) = orbit(5) + (dl/beta - (1d0/beta + pt) * l_pz)
ek(1) = px*l_pz
ek(3) = py*l_pz
ek(5) = dl*dtbyds + l_pz*(pt*(2*beti+pt)/(gamma*gamma) - px2 - py2)/(beta*beta*(ptb + beti*pz))

if (ftrk) orbit(1:5:2) = orbit(1:5:2) + ek(1:5:2)

else

re(1,2) = dl
Expand Down
3 changes: 3 additions & 0 deletions tests/test-twiss-16/test-twiss-16-mad3.tfs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1-9 * skip
10 * rel=1e-14
11-$ * skip
11 changes: 11 additions & 0 deletions tests/test-twiss-16/test-twiss-16-mad3.tfs.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@ NAME %04s "MAD3"
@ TYPE %09s "SECTORMAP"
@ TITLE %08s "no-title"
@ ORIGIN %16s "5.08.01 Linux 64"
@ DATE %08s "21/06/22"
@ TIME %08s "13.30.36"
* NAME T122 T124 T126 T144 T146 T166 T322 T324 T326 T344 T346 T366 T522 T524 T526 T544 T546 T566
$ %s %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le
"L$START" 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00
"O" 7.95552195596275907e-02 3.44810984420141561e-02 -2.95832272960501763e+00 2.65296302746198774e-02 -1.19954398173697098e-03 7.63998146067667028e-02 3.44810984420141561e-02 2.65296302746198774e-02 -1.19954398173697098e-03 1.03429206342507532e-01 -2.95895941064147783e+00 9.93197589887966914e-02 -2.95832272960501763e+00 -1.19954398173697098e-03 7.63998146067667028e-02 -2.95895941064147783e+00 9.93197589887966914e-02 -2.60638736213083666e+00
"L$END" 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00
4 changes: 4 additions & 0 deletions tests/test-twiss-16/test-twiss-16-ptc.tfs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1-91 * skip
92 2-8 rel=1e-14
92 10-18 rel=1e-14
93-$ * skip
93 changes: 93 additions & 0 deletions tests/test-twiss-16/test-twiss-16-ptc.tfs.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@ NAME %09s "PTC_TWISS"
@ TYPE %05s "TWISS"
@ SEQUENCE %01s "L"
@ PARTICLE %06s "PROTON"
@ MASS %le 9.38272088159999962e-01
@ CHARGE %le 1.00000000000000000e+00
@ ENERGY %le 2.00000000000000000e+00
@ PC %le 1.76625181912995433e+00
@ GAMMA %le 2.13157784957890328e+00
@ KBUNCH %le 1.00000000000000000e+00
@ BCURRENT %le 7.49214795614770220e-12
@ SIGE %le 1.00000000000000002e-03
@ SIGT %le 1.00000000000000000e+00
@ NPART %le 1.00000000000000000e+00
@ EX %le 1.00000000000000000e+00
@ EY %le 1.00000000000000000e+00
@ ET %le 1.00000000000000002e-03
@ DELTAP %le 0.00000000000000000e+00
@ LENGTH %le 5.00000000000000000e+00
@ ALPHA_C %le -1.00000000000000000e+06
@ ALPHA_C_P %le -1.00000000000000000e+06
@ ALPHA_C_P2 %le -1.00000000000000000e+06
@ ALPHA_C_P3 %le -1.00000000000000000e+06
@ ETA_C %le -1.00000000000000000e+06
@ GAMMA_TR %le -1.00000000000000000e+06
@ Q1 %le 2.18662318680467038e-01
@ Q2 %le 2.18664509460172629e-01
@ DQ1 %le -1.00000000000000000e+06
@ DQ2 %le -1.00000000000000000e+06
@ QS %le -1.00000000000000000e+06
@ BETA_X_MIN %le 9.97668990890940632e-01
@ BETA_X_MAX %le 2.60683049153817414e+01
@ BETA_Y_MIN %le 9.97668990890940632e-01
@ BETA_Y_MAX %le 2.60719028755598323e+01
@ BETA11MIN %le 9.97668990890940632e-01
@ BETA11MAX %le 2.60683048258232652e+01
@ BETA12MIN %le 0.00000000000000000e+00
@ BETA12MAX %le 4.58189068032043954e-07
@ BETA13MIN %le 0.00000000000000000e+00
@ BETA13MAX %le 3.49033090966918268e-03
@ BETA21MIN %le 0.00000000000000000e+00
@ BETA21MAX %le 4.58189068032043954e-07
@ BETA22MIN %le 9.97668990890940632e-01
@ BETA22MAX %le 2.60719027859889927e+01
@ BETA23MIN %le 0.00000000000000000e+00
@ BETA23MAX %le 5.89865923734092003e-03
@ BETA31MIN %le 0.00000000000000000e+00
@ BETA31MAX %le 3.37266836437332459e-03
@ BETA32MIN %le 0.00000000000000000e+00
@ BETA32MAX %le 5.69980953579091783e-03
@ BETA33MIN %le 9.64036571539015785e-01
@ BETA33MAX %le 3.20267140640042713e+00
@ DISP1MIN %le -5.96572604168680853e-02
@ DISP1MAX %le 0.00000000000000000e+00
@ DISP2MIN %le 0.00000000000000000e+00
@ DISP2MAX %le 0.00000000000000000e+00
@ DISP3MIN %le -7.75544385419285193e-02
@ DISP3MAX %le 0.00000000000000000e+00
@ DISP4MIN %le 0.00000000000000000e+00
@ DISP4MAX %le 0.00000000000000000e+00
@ ORBIT_X %le 0.00000000000000000e+00
@ ORBIT_PX %le 1.00000000000000002e-02
@ ORBIT_Y %le 0.00000000000000000e+00
@ ORBIT_PY %le 1.29999999999999994e-02
@ ORBIT_PT %le -1.70000000000000012e-02
@ ORBIT_T %le 0.00000000000000000e+00
@ XCORMS %le 4.16337111554961822e-02
@ PXCORMS %le 1.00000000000000002e-02
@ YCORMS %le 5.41238245021450348e-02
@ PYCORMS %le 1.29999999999999994e-02
@ TCORMS %le 2.08157583830212087e-02
@ PTCORMS %le 1.70000000000000012e-02
@ XCOMIN %le 0.00000000000000000e+00
@ XCOMAX %le 5.09906742146927355e-02
@ PXCOMIN %le 1.00000000000000002e-02
@ PXCOMAX %le 1.00000000000000002e-02
@ YCOMIN %le 0.00000000000000000e+00
@ YCOMAX %le 6.62878764791005604e-02
@ PYCOMIN %le 1.29999999999999994e-02
@ PYCOMAX %le 1.29999999999999994e-02
@ TCOMIN %le -2.54939933237317007e-02
@ TCOMAX %le -0.00000000000000000e+00
@ PTCOMIN %le -1.70000000000000012e-02
@ PTCOMAX %le -1.70000000000000012e-02
@ TITLE %08s "no-title"
@ ORIGIN %16s "5.08.01 Linux 64"
@ DATE %08s "21/06/22"
@ TIME %08s "13.30.36"
* NAME KEYWORD S X PX Y PY T PT DISP1 DISP2 RE12 RE14 RE16 RE32 RE34 RE36 RE52 RE54 RE56
$ %s %s %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le
"L$START" "MARKER" 0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000002e-02 0.00000000000000000e+00 1.29999999999999994e-02 -0.00000000000000000e+00 -1.70000000000000012e-02 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00
"O" "DRIFT" 5.00000000000000000e+00 5.09906742146927355e-02 1.00000000000000002e-02 6.62878764791005604e-02 1.29999999999999994e-02 -2.54939933237317007e-02 -1.70000000000000012e-02 -5.96572604168680853e-02 0.00000000000000000e+00 5.09959773444608544e+00 6.89406869855017164e-04 -5.91480000693044086e-02 6.89406869855017164e-04 5.09996365040008470e+00 -7.68924000900957361e-02 -5.91480000693044086e-02 -7.68924000900957222e-02 1.49795370600283739e+00
"L$END" "MARKER" 5.00000000000000000e+00 5.09906742146927355e-02 1.00000000000000002e-02 6.62878764791005604e-02 1.29999999999999994e-02 -2.54939933237317007e-02 -1.70000000000000012e-02 -5.96572604168680853e-02 0.00000000000000000e+00 5.09959773444608544e+00 6.89406869855017164e-04 -5.91480000693044086e-02 6.89406869855017164e-04 5.09996365040008470e+00 -7.68924000900957361e-02 -5.91480000693044086e-02 -7.68924000900957222e-02 1.49795370600283739e+00
3 changes: 3 additions & 0 deletions tests/test-twiss-16/test-twiss-16-ptc3.tfs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1-9 * skip
10 * rel=1e-14
11-$ * skip
11 changes: 11 additions & 0 deletions tests/test-twiss-16/test-twiss-16-ptc3.tfs.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@ NAME %04s "PTC3"
@ TYPE %04s "USER"
@ TITLE %08s "no-title"
@ ORIGIN %16s "5.08.01 Linux 64"
@ DATE %08s "21/06/22"
@ TIME %08s "13.30.36"
* NAME TXPP TXPQ TXPD TXQQ TXQD TXDD TYPP TYPQ TYPD TYQQ TYQD TYDD TTPP TTPQ TTPD TTQQ TTQD TTDD
$ %s %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le %le
"L$START" 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 0.00000000000000000e+00 -0.00000000000000000e+00 -0.00000000000000000e+00 -0.00000000000000000e+00 -0.00000000000000000e+00 -0.00000000000000000e+00 -0.00000000000000000e+00
"O" 7.95552195596276046e-02 6.89621968840283123e-02 -5.91664545921003615e+00 2.65296302746198809e-02 -2.39908796347394282e-03 7.63998146067667305e-02 3.44810984420141631e-02 5.30592605492397548e-02 -2.39908796347394282e-03 1.03429206342507546e-01 -5.91791882128295654e+00 9.93197589887967469e-02 -2.95832272960501808e+00 -2.39908796347394282e-03 1.52799629213533406e-01 -2.95895941064147827e+00 1.98639517977593438e-01 -2.60638736213083977e+00
"L$END" 7.95552195596276046e-02 6.89621968840283123e-02 -5.91664545921003615e+00 2.65296302746198809e-02 -2.39908796347394282e-03 7.63998146067667305e-02 3.44810984420141631e-02 5.30592605492397548e-02 -2.39908796347394282e-03 1.03429206342507546e-01 -5.91791882128295654e+00 9.93197589887967469e-02 -2.95832272960501808e+00 -2.39908796347394282e-03 1.52799629213533406e-01 -2.95895941064147827e+00 1.98639517977593438e-01 -2.60638736213083977e+00
4 changes: 4 additions & 0 deletions tests/test-twiss-16/test-twiss-16-twiss.tfs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1-53 * skip
54 2-8 rel=1e-14
54 10-18 rel=1e-14
55-$ * skip
Loading