-
Notifications
You must be signed in to change notification settings - Fork 11
/
madl_track.mad
1892 lines (1519 loc) · 55.5 KB
/
madl_track.mad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[=[
o-----------------------------------------------------------------------------o
|
| Track module
|
| Methodical Accelerator Design - Copyright CERN 2016+
| Support: http://cern.ch/mad - mad at cern.ch
| Authors: L. Deniau, laurent.deniau at cern.ch
| Contrib: I. Shreyber, irina.tecker at cern.ch
|
o-----------------------------------------------------------------------------o
| You can redistribute this file and/or modify it under the terms of the GNU
| General Public License GPLv3 (or later), as published by the Free Software
| Foundation. This file is distributed in the hope that it will be useful, but
| WITHOUT ANY WARRANTY OF ANY KIND. See http://gnu.org/licenses for details.
o-----------------------------------------------------------------------------o
Purpose:
- TODO
o-----------------------------------------------------------------------------o
]=]
-- help -----------------------------------------------------------------------o
local __help = {}
__help.track = [=[
NAME
track -- track
SYNOPSIS
TODO
DESCRIPTION
The module track provides...
RETURN VALUES
The track command
SEE ALSO
element, sequence, survey
]=]
__help['track: references'] = [=[
[Wolski14] A. Wolski, "Beam Dynamics in High Energy Particle Accelerators".
[Forest98] E. Forest, "Beam Dynamics, A New Attitude and Framework".
[Forest02] E. Forest, "Introduction to Polymorphic Tracking Code",
KEK report 2002-3.
[Forest06] E. Forest, "Geometric Integration for Particle Accelerators",
J.Phys. A, Math.Gen. 39 (2006) 5321-5377.
[Ripken95] K. Heinemann, G. Ripken, and F. Schmidt. "Construction of
Nonlinear Symplectic Six-dimensional Thin-lens Maps by
Exponentiation", Technical Report 95-189, DESY, 1995.
[SixTrack] R. De. Maria et al., "SixTrack Physics Manual", CERN,
http://cern.ch/SixTrack/doc/physics_manual/sixphys.pdf, Sep. 2016.
[Yoshida90] H. Yoshida, "Construction of higher order symplectic integrators",
Phys.Let. A, Volume 150, number 5,6,7.
[Burkhardt13] H. Burkhardt et al., "Improved Teapot Method and Tracking with
Thick Quadrupoles for the LHC and its Upgrade", IPAC 2013.
[MADX-PTC] MAD-X PTC Code, rev. 6099
]=]
__help['track: straight maps'] = [[
[2] p.352, ch.12.1
DKD: ch.12.1.3, p.353
MKM: ch.12.1.4, p.354
[3] DKD: p.72, Section K.4.2,
PTC, Function INTER_STREX in file Sh_def_kind.f90)
MKM: p.75, Section K.4.7,
PTC, Function INTER_TKTF in file Sh_def_kind.f90)
]]
__help['track: curved maps'] = [[
[2] p.357, ch.12.2
[3] PTC, p.76, Section K.4.9,Function INTER_TEAPOT in file Sh_def_kind.f90)
]]
__help['track: pushtkt'] = [[
[2] MKM: p.75, Section K.4.7,
[3] PTC, Function INTER_TKTF in file Sh_def_kind.f90)
]]
__help['track: straight matrix-kick'] = [[
[2] p.358, ch.12.2.2 Drift-Kick Split, drift in polar coordinates
[3] p.76, PTC, Function SPROTR in file Sh_def_kind.f90)
]]
--[[TODO:
-Add charge, direction, mass(?)
-Deltap (recalculation of pt)
- KICKEX: how to read properly strength of thick element (i.e. convert
k1,k1s, k2 etc to knl,ksl)
-????KICKEX: add a check for thick element that there is only one strength,
i.e. quad only has k1 and not k0, k2 or???
]]
-- add patch, rotation + translation
--[[ METHOD:
2 - 2nd order, 1 kick per integration step
4 - Ruth-Neri-Yoshida 4th order, 3 kicks per step
6 - Yoshida 6th order, 7kicks per step
]]
--[[
frame of integration:
angle == 0 and k0 ~= 0 rbend on (??? rbend with the straight ref. frame ??)
STRAIGHT REF. FRAME FOR RBEND : STREX
angle == 0 and k0 == 0 rbend off (i.e. drift)
DRIFT-TRACK
angle ~= 0 and k0 ~= 0 sbend on
SBEND, default RBEND: TEAPOT
angle ~= 0 and k0 == 0 sbend off( useless, what kind of element is this? broken sbend?)
<=> rbend with l = l_arc and k0 = angle, what about errors?
angle == 0 abd k0 == 0 but k[n] ~= 0: straight magnet
DKD (model = 1 ): STREX ; MKM (model = 2): TKTF
]]
--[[
Documentation....
List of top level frame_track methods:
--------------------------------------
- patch_frame
- drift_frame
+ l = 0 -> sub element only
- thin_frame ignore l and angle but not patch and tilt
+ nmul == 0 and #elm > 0 -> drift_frame
- thick_frames (all-in-one)
+ l = 0 -> thin_frame
+ nmul = 0 -> drift_frame
+ l > 0 and nmul > 0 and angle == 0 -> strait_frame
+ l > 0 and nmul > 0 and angle ~= 0 -> curved_frame
- do we need curved and strait differenciation?
- decompose entry and exit in more steps?
- do we perform radiation and aperture check after each nst?
+ what about thin lens?
- no more guards about lengths in thin, allow to store non-zero length
- no more guards about lengths in thick, allow to be compatible with madx
- check for angle and length to switch between integration schemes
]]
__help['track: classification of drift-kick models versus thick elements'] = [=[
drift kind kick kind fringe kind
- angle=0
rbend: rbend_drift rfcavity_kick straight_fringe
quadrupole: quadrupole_drift quadrupole_kick straight_fringe
solenoid: solenoid_drift rfcavity_kick solenoid_fringe
rfcavity: solenoid_drift rfcavity_kick rfcavity_fringe
twcavity: twcavity_drift twcavity_kick twcavity_fringe
- angle~=0
sbend: sbend_drift polar_kick sbend_fringe sbend
combined: combined_drift combined_kick sbend_fringe KTK, TKTF
- precedences (ordered):
if ks ~=0 solenoid_drift rfcavity_kick solenoid_fringe
if ang~=0 sbend_drift polar_kick sbend_fringe
if k0 ~=0 rbend_drift rfcavity_kick straight_fringe
if v ~=0 rbend_drift rfcavity_kick rfcavity_fringe
if k1 ~=0 quadrupole_drift quadrupole_kick straight_fringe
default straight_drift straight_kick straight_fringe
- fallbacks:
straight_drift(l=0) => (none)
rbend_drift(k0=0) => straight_drift
solenoid_drift(ks=0) => rbend_drift
quadrupole_drift(k1=0) => rbend_drift
polar_drift(ang=0) => rbend_drift (*)
sbend_drift(k0=ang) => polar_drift
combined_drift(k1=0) => sbend_drift
straight_kick(nmul=0) => (none)
rfcavity_kick(v=0) => straight_kick
quadrupole_kick(k1=0) => rfcavity_kick (*)
polar_kick(ang=0) => straight_kick (*)
combined_kick(k1=0) => polar_kick
straight_fringe(nmul=0) => (none)
solenoid_fringe(ks=0) => straight_fringe (*)
rfcavity_fringe(v=0) => straight_fringe (*)
sbend_fringe(ang=0) => straight_fringe (*)
(*) fallbacks that should never occur according to precedences
- remarks:
+ fringe kind should be driven by element kind and not by precedence
+ quadrupole_drift = quadrupole matrix (linear) + drift correction (nonlinear)
quadrupole_kick = straight_kick - k1_kick
+ no solution for combined ang and/or k0 and ks ??
]=]
__help['track: drift-kick models and references'] = [=[
PTC KIND References
straight_drift: DRIFT{R/P}
rbend_drift: SPAR{R/P}
solenoid_drift: KICK_SOL{R/P}
polar_drift: SPROT{R/P}
sbend_drift: SSEC{R/P}
quadrupole_kick: PUSHTKT7{R/P}
combined_drift: PUSHTKT7{R/P} ? KTK, TKTF
straight_kick: KICKEX{R/P}
rfcavity_kick: KICK_CAV{R/P}
polar_kick: SKICK{R/P} (GETELECTRIC)
quadrupole_kick: KICKPATH{R/P}+KICKTKT7{R/P}
combined_kick: KICKPATH{R/P}+KICKTKT7{R/P}
twcavity(drift+kick): CAVE{R/P}_TRAV => RK{2,4,6}_CAV{R/P}+...
straight_fringe: FRINGE_STRAIGHT{R/P}
sbend_fringe: FRINGE_TEAPOT{R/P}
solenoid_fringe: FRINGE_SOL{R/P}
rfcavity_fringe: FRINGECAV{R/P}
twcavity_fringe: FRINGE_CAV_TRAV{R/P}
]=]
-- locals ---------------------------------------------------------------------o
local vector, matrix, element, mtable, Command in MAD
local is_nil, is_number, is_sequence, is_beam in MAD.typeid
local minlen, minang, clight in MAD.constant
local observe, ismoved in element.flags
local abs, sqrt, max, sin, cos, tan, asin, acos, atan, atan2,
sinh, cosh, sinc, fact, twopi in MAD.gmath
local minvolt, volt_c = 1e-6, 1e-3
-- forward declarations -------------------------------------------------------o
-- patches
local entry_patch, exit_patch -- patches
local xrotation, yrotation, srotation -- rotations
local translate, changedir, changeref -- specials
-- drifts
local straight_drift , rbend_drift, quadrupole_drift -- straight
local polar_drift , sbend_drift, combined_drift -- curved
local solenoid_drift -- solenoid
local twcavity_drift -- cavities
-- kicks
local thin_kick -- thin
local straight_kick , quadrupole_kick -- straight
local polar_kick , combined_kick -- curved
local rfcavity_kick , twcavity_kick -- cavities
-- fringes
local straight_fringe -- straight
local polar_fringe , sbend_fringe -- curved
local solenoid_fringe -- solenoid
local rfcavity_fringe, twcavity_fringe -- cavities
-- special --------------------------------------------------------------------o
local function empty_track () end
local function invalid_track (elm)
error(string.format("invalid element %s of kind %s with length %s",
elm.name, elm.kind, elm.l))
end
element. extra_element :set_functions { track = invalid_track,
backtrack = invalid_track } -- not valid
element.special_element :set_functions { track = empty_track,
backtrack = empty_track } -- default
-- patches --------------------------------------------------------------------o
-- X-rotation (pitch)
function xrotation (_, m, angle) -- ROT_YZR <-> Rx(angle)
if abs(angle) < minang then return end -- XZrot with x,px <-> y,py)
local x, px, y, py, t, pt, sdir in m
local sa, ca, ta = sin(angle*sdir), cos(angle*sdir), tan(angle*sdir)
local _beta = 1/m.beam.beta
local pz = sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
local _pz = 1/pz
local ptt = 1 - ta*py*_pz
local _ptt = 1/ptt
m.y = y/(ca*ptt)
m.py = ca*py + sa*pz
m.x = x + ta*y*px*_pz*_ptt
m.t = t - ta*y *_pz*_ptt*(_beta+pt)
end
-- Y-rotation (yaw)
function yrotation (_, m, angle) -- ROT_XZR (Dragt's PROT) <-> Ry(angle)
if abs(angle) < minang then return end
local x, px, y, py, t, pt, sdir in m
local sa, ca, ta = sin(angle*sdir), cos(angle*sdir), tan(angle*sdir)
local _beta = 1/m.beam.beta
local pz = sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
local _pz = 1/pz
local ptt = 1 - ta*px*_pz
local _ptt = 1/ptt
m.x = x/(ca*ptt)
m.px = ca*px + sa*pz
m.y = y + ta*x*py*_pz*_ptt
m.t = t - ta*x *_pz*_ptt*(_beta+pt)
end
-- S-rotation (roll, tilt)
function srotation (_, m, angle) -- XYrot <-> Rz(-angle)?
if abs(angle) < minang then return end
local x, px, y, py, bdir in m
local sa, ca = sin(angle*bdir), cos(angle*bdir)
m.x = ca*x + sa*y
m.y = ca*y - sa*x
m.px = ca*px + sa*py
m.py = ca*py - sa*px
end
-- Translation (dz treated as drift length)
function translate (_, m, dx, dy, dz)
local bdir in m
straight_drift(nil, m, dz)
m.x = m.x - dx*bdir
m.y = m.y - dy*bdir
end
-- Changedir (reverse x and angles)
function changedir(_, m)
m.sdir = -m.sdir
m.bdir = -m.bdir
end
-- Changeref (change reference frame)
function changeref (elm, m)
local x, y, z, theta, phi, psi in elm
xrotation(nil, m, phi or 0) -- phi : Elevation angle.
yrotation(nil, m, theta or 0) -- theta: Azimuthal angle.
srotation(nil, m, psi or 0) -- psi : Roll angle.
translate(nil, m, x or 0, y or 0, z or 0)
end
-- Entry/Exit patches
-- not correct, need local survey patch
function entry_patch (elm, m)
local theta, phi, psi, dtheta, dphi, dpsi, tilt in elm
local x, y, z, dx, dy, dz in elm
local mdir in m
-- TODO: survey m.S (mdir = -1)
xrotation(nil, m, (phi or 0)+(dphi or 0)) -- phi : Elevation angle.
yrotation(nil, m, (theta or 0)+(dtheta or 0)) -- theta: Azimuthal angle.
srotation(nil, m, (psi or 0)+(dpsi or 0)) -- psi : Roll angle.
translate(nil, m, (x or 0)+(dx or 0),
(y or 0)+(dy or 0),
((z or 0)+(dz or 0))*mdir )
srotation(nil, m, tilt)
end
function exit_patch (elm, m)
local theta, phi, psi, dtheta, dphi, dpsi, tilt in elm
local x, y, z, dx, dy, dz in elm
local mdir in m
-- TODO: survey m.S (mdir = 1)
srotation(nil, m, -tilt)
translate(nil, m, (x or 0)+(dx or 0),
(y or 0)+(dy or 0),
((z or 0)+(dz or 0))*mdir )
srotation(nil, m, (psi or 0)+(dpsi or 0)) -- psi : Roll angle.
yrotation(nil, m, (theta or 0)+(dtheta or 0)) -- theta: Azimuthal angle.
xrotation(nil, m, (phi or 0)+(dphi or 0)) -- phi : Elevation angle.
end
-- strengths and phases -------------------------------------------------------o
-- Load element phases in mflow
local function get_phas (elm, m, nmul) -- l=0 (or ignored)
local pnl, dpnl = elm.pnl or {}, elm.dpnl or {}
local psl, dpsl = elm.psl or {}, elm.dpsl or {}
for i=1,nmul do
m.pnl[i] = (pnl[i] or 0) + (dpnl[i] or 0)
m.psl[i] = (psl[i] or 0) + (dpsl[i] or 0)
end
end
-- Load element multipoles and volts in mflow
local function get_mult (elm, m) -- l=0 (or ignored)
local knl, dknl = elm.knl or {}, elm.dknl or {}
local ksl, dksl = elm.ksl or {}, elm.dksl or {}
local nmul = max(#knl, #ksl, #dknl, #dksl)
m.ksi = (elm.ksi or 0) + (elm.dksi or 0) -- solenoid
m.krf = (elm.krf or 0) + (elm.dkrf or 0) -- rf cavity (krf*z = omega_rf*t)
m.vrf = (elm.volt or 0) + (elm.dvrf or 0) -- rf volts
m.ktot = nmul + abs(m.ksi) + abs(m.krf) + abs(m.vrf)
if nmul >= 1 then
for i=1,nmul do
m.knl[i] = (knl[i] or 0) + (dknl[i] or 0)
m.ksl[i] = (ksl[i] or 0) + (dksl[i] or 0)
end
if abs(m.krf) >= minang then
get_phas(elm, m, nmul)
end
m.nmul = nmul
else
m.nmul, m.knl[1], m.ksl[1], m.knl[2] = 0, 0, 0, 0
end
end
-- Load element strengths, multipoles and volts in mflow
local function get_kmult (elm, m) -- l~=0
local l in elm
if abs(l) < minlen then return get_mult(elm, m) end
local k0 , k1 , k2 , k3 in elm
local k0s, k1s, k2s, k3s in elm
local nkn = k3 and 4 or k2 and 3 or k1 and 2 or k0 and 1 or 0
local nks = k3s and 4 or k2s and 3 or k1s and 2 or k0s and 1 or 0
local knl, dknl = elm.knl or {}, elm.dknl or {}
local ksl, dksl = elm.ksl or {}, elm.dksl or {}
local nmul = max(#knl, #ksl, #dknl, #dksl, nkn, nks)
m.ksi = (elm.ks or 0)*l + (elm.ksi or 0) + (elm.dksi or 0)
m.krf = (elm.krf or 0) + (elm.dkrf or 0)
m.vrf = (elm.volt or 0) + (elm.dvrf or 0)
m.ktot = nmul + abs(m.ksi) + abs(m.krf) + abs(m.vrf)
if nmul >= 1 then
local kn = { k0 , k1 , k2 , k3 }
local ks = { k0s, k1s, k2s, k3s }
for i=1,nmul do
m.knl[i] = (kn[i] or 0)*l + (knl[i] or 0) + (dknl[i] or 0)
m.ksl[i] = (ks[i] or 0)*l + (ksl[i] or 0) + (dksl[i] or 0)
end
if abs(m.krf) >= minang then
get_phas(elm, m, nmul)
end
m.nmul = nmul
else
m.nmul, m.knl[1], m.ksl[1], m.knl[2] = 0, 0, 0, 0
end
end
-- Compute weighted multipoles
local function get_bxby (nmul, knl, ksl, x, y, lw)
if nmul == 0 then return 0, 0 end
local by = lw*knl[nmul] / fact(nmul-1)
local bx = lw*ksl[nmul] / fact(nmul-1)
local byt
for i=nmul-1,1,-1 do
byt = x*by - y*bx + lw*knl[i] / fact(i-1)
bx = y*by + x*bx + lw*ksl[i] / fact(i-1)
by = byt
end
return bx, by
end
-- drifts ---------------------------------------------------------------------o
__help['track: exact straight drift'] = [=[
[Wolski14] p.86, ch. 3.1, eq. 3.13-3.18
[Forest98] p.306, ch. 10.4.3.1, eq. 10.23a-10.23c
[MADX-PTC] Function DRIFTR in file Sh_def_kind.f90
]=]
function straight_drift (_, m, l) -- exact, l~=0
if abs(l) < minlen then return end
local x, px, y, py, t, pt, T in m
local _beta = 1/m.beam.beta
local l_pz = l/sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
m.x = x + px*l_pz
m.y = y + py*l_pz
m.t = t - (_beta+pt)*l_pz + (1-T)*l*_beta
end
__help['track: exact rbend drift'] = [=[
[Forest98] p.367, ch. 12.3.1, eq. 12.39a,12.39c, 12.39f
[MADX-PTC] Function SPARR in file Sh_def_kind.f90
]=]
function rbend_drift (elm, m, l) -- exact, l~=0
local knl in m
if abs(knl[1]) < minang then
return straight_drift(nil,m,l)
end
local x, px, y, py, t, pt, bdir, T in m
local _beta = 1/m.beam.beta
local lw = l/elm.l
local k0 = bdir*lw*knl[1]
local pz = sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
local pt = sqrt(1 + 2*_beta*pt + pt^2 - py^2)
local xp = px - k0
local ps = sqrt(1 + 2*_beta*pt + pt^2 - xp^2 - py^2)
local xs = (asin(px/pt) - asin(xp/pt))/k0
m.x = x + (ps-pz)/k0
m.px = xp
m.y = y + py*xs
m.t = t - (_beta+pt)*xs + (1-T)*l*_beta
end
__help['track: exact solenoid drift'] = [=[
[Forest98] p.377, ch. 12.5.2, eq. 12.50 and solution
[MADX-PTC] Function KICK_SOL in file Sh_def_kind.f90
]=]
function solenoid_drift (elm, m, l) -- exact, l~=0 TO BE REVIEWED
if abs(m.ksi) < minang then
return rbend_drift(elm,m,l)
end
local x, px, y, py, t, pt, T, bdir in m
local _beta = 1/m.beam.beta
local _el = 1/elm.l
local bsol = m.ksi*bdir*_el/2
local xp = px + bsol*y
local yp = py - bsol*x
local l_pz = l/sqrt(1 + 2*_beta*pt + pt^2 - xp^2 - yp^2)
local angle = l_pz*bsol
local ca, sa, sc = cos(angle), sin(angle), sinc(angle)
local xt = ca*x + l_pz*sc*px
local pxt = ca*px - l_pz*sc*x *bsol^2
local yt = ca*y + l_pz*sc*py
local pyt = ca*py - l_pz*sc*y *bsol^2
m.x = ca*xt + sa*yt
m.px = ca*pxt + sa*pyt
m.y = ca*yt - sa*xt
m.py = ca*pyt - sa*pxt
m.t = t - (_beta+pt)*l_pz + (1-T)*l*_beta
end
__help['track: drift to correct pseudo-exact combined kick'] = [=[
[MADX-PTC] Function KICKPATHR in file Sh_def_kind.f90
]=]
local function combadj_drift (_, m, l) -- l~=0, adjust combined_drift
local x, px, y, py, t, pt, T in m
local _beta = 1/m.beam.beta
local l_pz = l/sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
m.x = x + px*(l_pz-l)
m.y = y + py*(l_pz-l)
m.t = t - (_beta+pt)*l_pz + (1-T)*l*_beta
end
__help['track: pseudo-exact quad drift'] = [=[
[MADX-PTC] Function PUSHTKT7 in file Sh_def_kind.f90
]=]
function quadrupole_drift (elm, m, l) -- comb_drift with angle == 0
local _el = 1/elm.l
local _beta = 1/m.beam.beta
local l_bet2_gam2 = l/(m.beam.gamma^2-1)
local knl, bdir in m
local k0, k1 = bdir*knl[1]*_el, bdir*knl[2]*_el
local w2 = abs(k1)
local w = sqrt(w2)
-- TODO: case w=0...
local cw, sw = cos (w*l), sin (w*l)/w
local ch, sh = cosh(w*l), sinh(w*l)/w
if k1 < 0 then
cw, sw, ch, sh = ch, sh, cw, sw
end
local npos in m
if is_nil(npos) or npos == 'last' then
local dl = is_nil(pos) and l/2 or l
combadj_drift(_, m, dl)
end
local x, px, y, py, t, pt in m
local m16 = (1-cw)/w2
m.x = cw*x + sw*px -k0*m16
m.px = -w*sw*x + cw*px -k0*sw
m.y = ch*y + sh*py
m.py = w*sh*y + ch*py
m.t = t + l_bet2_gam2*pt
if is_nil(npos) or npos == 'first' then
local dl = is_nil(pos) and l/2 or l
combadj_drift(_, m, l)
end
m.npos = nil
end
__help['track: pseudo-exact combined drift'] = [=[
[MADX-PTC] Function PUSHTKT7 in file Sh_def_kind.f90
]=]
function combined_drift (elm, m, l)
local _el = 1/elm.l
local _beta = 1/m.beam.beta
local l_bet2_gam2 = l/(m.beam.gamma^2-1)
local angle = elm.angle
local lw = l*_el
local h = lw*angle
local hbar = h*_beta
local knl, bdir in m
local k0, k1 = bdir*knl[1]*_el, bdir*knl[2]*_el
local hk0pk1 = h*k0+k1
local wx2, wy2 = abs(hk0pk1), abs(k1)
local wx , wy = sqrt(wx2), sqrt(wy2)
local cx, sx, cy, sy
if hk0pk1 < 0
then cx, sx = cosh(wx*l), sinh(wx*l)/wx -- check wx == 0
else cx, sx = cos (wx*l), sin (wx*l)/wx -- check wx == 0
end
if k1 < 0
then cy, sy = cosh(wy*l), sinh(wy*l)/wy -- check wy == 0
else cy, sy = cos (wy*l), sin (wy*l)/wy -- check wy == 0
end
local npos in m
if is_nil(npos) or npos == 'last' then
local dl = is_nil(pos) and l/2 or l
combadj_drift(_, m, dl)
end
local x, px, y, py, t, pt in m
local m16, m56 = (1-cx)/wx2, hbar^2*(l-sx)/wx2
m.x = cx*x + sx*px + hbar*m16*pt + (h-k0)*m16
m.px = -wx2*sx*x + cx*px + hbar*sx*pt + (h-k0)*sx
m.y = cy*y + sy*py
m.py = wy2*sy*y + cy*py
m.t = t - hbar*(sx*x+m16*px) + (l_bet2_gam2-m56)*pt
if is_nil(npos) or npos == 'first' then
local dl = is_nil(pos) and l/2 or l
combadj_drift(_, m, l)
end
m.npos = nil
end
__help['track: exact polar drift'] = [=[
[Forest02] p.76, ch. K.4.9, (general explaination)
[Forest06] p.5365, eq. 127, exact drift -- WRONG: ref is an yrotation
[MADX-PTC] Function SPROTR in file Sh_def_kind.f90
]=]
function polar_drift (elm, m, l) -- exact, l~=0, k0=angle
local rho = elm.l/(elm.angle*m.sdir)
local ang = l/rho
local sa, ca, ta, sa2 = sin(ang), cos(ang), tan(ang), sin(ang/2)
local x, px, y, py, t, pt, T in m
local _beta = 1/m.beam.beta
local pz = sqrt(1 + 2*_beta*pt + pt^2 - px^2 - py^2)
local _pz = 1/pz
local ptt = 1 - ta*px*_pz
local xr = (x+rho)*ta*_pz
m.x = (x + rho*(2*sa2^2 + sa*px*_pz))/(ca*ptt)
m.px = ca*px + sa*pz
m.y = y + xr*py/ptt
m.t = t - xr*(_beta+pt)/ptt + (1-T)*l*_beta
end
__help['track: exact sbend drift'] = [=[
[Forest06] p.5365, eq. 126, exact drift
]=]
function sbend_drift (elm, m, l) -- exact, l~=0, k0~=angle
local angle in elm
local knl in m
if abs(angle - knl[1]) < minang then
return polar_drift(elm,m,l)
end
error("NYI")
end
__help['track: pseudo-exact combined drift'] = [=[
[MADX-PTC] Function PUSHTKT7 in file Sh_def_kind.f90
]=]
-- PRECEDENCE SHOULD BE OUTSIDE INTEGRATORS
--local function combined_drift (elm, m, l) -- angle, k0, k1 (tunes)
-- if abs(m.knl[2]) < minang then -- Put these checks outside the integrator
-- if abs(angle) < minang then
-- return rbend_drift(elm,m,l)
-- else
-- return sbend_drift(elm,m,l)
-- end
-- else
-- if abs(angle) < minang then
-- return quadrupole_drift(elm,m,l)
-- else
-- return combined_drift (elm,m,l)
-- end
-- end
--end
-- kicks ----------------------------------------------------------------------o
__help['track: generic thin kick'] = [[
[MADX-PTC] Function KICKTR in file Sh_def_kind.f90
]]
-- Special case TO BE REVIEWED !!!
function thin_kick (elm, m) -- l=0 (ignored)
local nmul in m
if nmul == 0 then return end
-- local knlt, kslt = elm.knl or {}, elm.ksl or {} -- BUG: not retrieved by getmul
-- local knl1, ksl1 = (knlt[1] or 0), (kslt[1] or 0)
local x, px, y, py, bdir, knl, ksl in m
local bx, by = get_bxby(nmul, knl, ksl, x, y, 1)
m.px = px - bdir*(by + knl[1])
m.py = py + bdir*(bx - ksl[1])
if knl1 ~= 0 or ksl1 ~= 0 then
local t, pt in m
local _beta = 1/m.beam.beta
local pz = sqrt(1 + 2*_beta*pt + pt^2)
local l = elm.lrad or 0 -- TODO: check if lrad or l?
if l ~= 0 then -- dipole focusing and deltap
m.px = m.px - knl[1]^2*x/l + bdir*knl[1]*(pz-1)
m.py = m.py - ksl[1]^2*y/l + bdir*ksl[1]*(pz-1)
end
-- m.t = t + bdir*(knl1*x - ksl1*y) * (_beta+pt)/pz
m.t = t - bdir*(knl[1]*x - ksl[1]*y) * (_beta+pt)/pz
end
end
__help['track: straight kick'] = [[
[Forest98] p.354, ch. 12.1.3, eq. 12.6
[MADX-PTC] Function KICKEXR in file Sh_def_kind.f90
[SixTrack] p.12, ch. 3.5 (thin multipole), eq. 91-94
[Ripken95] p.26, ch. 4.6.2, eq. 4.23 (and solution)
]]
function straight_kick (elm, m, l) -- l~=0
local nmul in m
if nmul == 0 then return end
local x, px, y, py, knl, ksl, bdir in m
local lw = abs(elm.l)>minlen and l/elm.l or 1
local bx, by = get_bxby(nmul, knl, ksl, x, y, lw)
m.px = px - bdir*by
m.py = py + bdir*bx
end
__help['track: exact quad kick'] = [=[
[Forest98] p.354, ch. 12.1.3, eq. 12.6
[MADX-PTC] Function KICKTKT7R in file Sh_def_kind.f90
]=]
function quadrupole_kick (elm, m, l)
if abs(m.knl[2]) < minang then -- should never occur due to precedences
return rfcavity_kick(elm,m,l)
end
local x, px, y, py, t, pt, bdir, T, nmul, knl, ksl in m
local _beta = 1/m.beam.beta
local pz = sqrt(1 + 2*_beta*pt + pt^2) - 1
local lw = l/elm.l
local bx, by = get_bxby(nmul, knl, ksl, x, y, lw)
m.px = px + lw*knl[1]*(pz-pt)*_beta
m.t = t - lw*knl[1]*((pt+_beta)/(1+pz) - _beta)*x
m.px = m.px - bdir*(by - lw*(knl[1] - knl[2]*x))
m.py = m.py + bdir*(bx - lw* knl[2]*y )
end
__help['track: polar kick'] = [[
[Forest98] p. 358-361, ch. 12.2.2, eq. 12.18a-12.18f
[Forest02] p. 76
[MADX-PTC] Function SKICKR in file Sh_def_kind.f90
[Ripken95] p. 22, ch. 4.3 (bending magnet) eq. 4.12 (and solution)
[SixTrack] Thin Dipole, ch. 3.2.2, eq. 48-50
]]
function polar_kick (elm, m, l) -- [PTC] SKICK
local angle in elm
if abs(angle) < minang then -- should never occur due to precedences
return straight_kick(elm,m,l)
end
local x, px, y, py, nmul, knl, ksl, bdir in m
local lw = l/elm.l
local bx, by = get_bxby(nmul, knl, ksl, x, y, lw) -- TODO: GETELECTRIC
local h = lw*angle
m.px = px - bdir*by*(1 + h*x)
m.py = py + bdir*bx*(1 + h*x)
end
__help['track: pseudo-exact combined kick'] = [=[
Ref??
]=]
function combined_kick (elm, m, l) -- angle, k0, k1 (tunes)
-- local knl in m
-- if abs(knl[2]) < minang then
return polar_kick(elm,m,l)
-- end
-- error("NYI") -- TODO
end
__help['track: exact cavity kick'] = [=[
[MADX-PTC] Function KICCAVR in file Sh_def_kind.f90
]=]
function rfcavity_kick (elm, m, l) --RF CAV, CRAB, RFMULT with l>0 kick (kickcavr)
--local vrf in m
--if abs(vrf) < minvolt then
return straight_kick(elm,m,l)
--end
-- missing input: tt, phase, phase0, ff, ph, a, r, nbessel, freq, volt, lag
-- local x, px, y, py, t, pt, bdir, nmul, knl, ksl in m
-- local volt, freq, lag, nbessel in elm
--
-- if freq == 0 then
-- error("Frequency number has to be defined for RF Cavity (i.e. harmon NYI) "
-- .. elm.name)
-- end
--
-- local pc = m.beam.pc
-- local omega = twopi*freq / clight
-- local vl = bdir*l*volt*volt_c/pc
-- local df, f, r2 = 0, 1, 1
--
-- do i=1,nbessel
-- r2 = -r2*(ko*omega)^2/(4*(i+1)^2)
-- dr2 = r2*i
-- df = df + 2*dr2
-- r2 = r2*(x^2+y^2)
-- f = f + r2
-- end
--
-- local ff, ph = elm.f or {}, elm.ph or {}
-- local a , r = elm.a or 0, elm.r or 0
-- local arg = omega*(t + tt) + phasе + ph + phase0
--
-- if nbessel > 0 then
-- m.px = px-x*ff[ko]* df * vl * cos(arg)/(omega)
-- m.py = py-y*ff[ko]* df * vl * cos(arg)/(omega)
-- end
--
-- m.pt = m.pt -ff*f*vl*sin(arg)
--
-- local lw = elm.l>0 and l/elm.l or 1
-- local bx, by = get_bxby(nmul, knl, ksl, x, y, lw)
--
-- m.px = m.px - ff[ko]* bdir*by/pc*(a + r*cos(arg))
-- m.py = m.py + ff[ko]* bdir*bx/pc*(a + r*cos(arg))
-- m.pt = m.pt + ff[ko]*(ko*omega)*bdir*by/pc* r*sin(arg)
--[[ useful???
by = -lw*knl[nmul] / fact(nmul-1) / nmul
bx = -lw*ksl[nmul] / fact(nmul-1) / nmul
for i=nmul-1,1,-1 do
byt = x*by - y*bx - lw*knl[i] / fact(i-1) / i
bx = y*by + x*bx - lw*ksl[i] / fact(i-1) / i
by = byt
end
byt = x*by - y*bx
bx = y*by + x*bx
by = byt
]]
end
-- fringes --------------------------------------------------------------------o
__help['track: straight fringe'] = [=[
[Forest98] p. 389, ch. 13.2.2, eq. 13.31a-13.31f
]=]
local function mult_fringe_field (elm, m, pos)
if elm.kill_fringe or
elm.kill_ent_fringe and pos == 'entry' or
elm.kill_exi_fringe and pos == 'exit' then return end
local x, px, y, py, bdir, nmul, knl, ksl in m
local _beta = 1/m.beam.beta
local _l = 1/elm.l
local rx, ix, fx, fy, fxx, fxy, fyx, fyy = 1, 0, 0, 0, 0, 0, 0, 0
for i=1, nmul do
local drx, dix = rx, ix
rx = drx*x - dix*y
ix = drx*y + dix*x
local ni, nf = -bdir/(4*(i+1)), i+2/i
local lkn, lks = knl[i]*_l, ksl[i]*_l
local u = ni*(lkn*rx - lks*ix )
local v = ni*(lkn*ix + lks*rx )
local du = ni*(lkn*drx - lks*dix)
local dv = ni*(lkn*dix + lks*drx)
local dux = i*du
local dvx = i*dv
local duy = -i*dv
local dvy = i*du
fx = fx + u*x + nf* v*y
fy = fy + u*y - nf* v*x
fxx = fxx + dux*x + nf* dvx*y + u
fyy = fyy + duy*y - nf* dvy*x + u
fxy = fxy + duy*x + nf*(v + dvy*y)
fyx = fyx + dux*y - nf*(v - dvx*x)
end
local _pz = 1/sqrt(1 + 2*pt*_beta + pt^2)
local a = 1 - fxx*_pz
local d = 1 - fyy*_pz
local b = - fyx*_pz
local c = - fxy*_pz
local _det = 1/(a*d - b*c)
m.x = x - fx*_pz
m.y = y - fy*_pz
m.px = (d*px - b*py)*_det
m.py = (a*py - c*px)*_det
m.t = t + (_beta+pt)*(m.px*fx + m.py*fy)*_pz^3
end
local function straight_fringe_field (elm, m, pos)
if elm.kill_fringe or
elm.kill_ent_fringe and pos == 'entry' or
elm.kill_exi_fringe and pos == 'exit' then return end
-- Lee-Whiting formula, E. Forest ch 13.2.3, eq 13.33
local x, px, y, py, t, pt, T, bdir in m
local k0 = elm.k0 or 0
local _beta = 1/m.beam.beta
-- soft edge
local fint = elm.fint or 0 -- fringe field integral at entrance and exit of the bend
local hgap = elm.hgap or 0 -- half gap of the magnet
local _pz = 1/sqrt(1 + 2*_beta*pt + pt^2 )
local time_fac = (_beta + pt)*_pz
local a2, b2 = elm.ksl[2]/elm.l or 0, elm.knl[2]/elm.l or 0
local b = sqrt(b2^2 + a2^2)
local f1 = -bdir*fint*abs(fint)*b*_pz/24.0
local f2 = hgap*b*_pz
local ang=-atan2(a2,b2)/2
srotation(nil, m, -ang or 0)
m.t = t - time_fac*(f1*x + f2*(1+f1/2)*px*_pz*exp(-f1))*px*_pz
+ time_fac*(f1*y + f2*(1-f1/2)*py*_pz*exp( f1))*py*_pz
m.x = x*exp( f1) + px*f2*_pz
m.y = y*exp(-f1) - py*f2*_pz
m.px = px*exp(-f1)