forked from MethodicalAcceleratorDesign/MAD-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trrun.f90
7491 lines (6579 loc) · 263 KB
/
trrun.f90
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
subroutine trrun(switch, turns, orbit0, rt, part_id, last_turn, last_pos, &
z, dxt, dyt, last_orbit, eigen, coords, e_flag, code_buf, &
l_buf)
use twtrrfi
use bbfi
use time_varfi
use spch_bbfi
use twiss0fi
use name_lenfi
use trackfi
use fasterror
use matrices, only : EYE
use math_constfi, only : zero, one, two
use code_constfi
use SpaceCharge
use track_enums
use SCdat, only: mytracksumm_maxlineso
implicit none
!----------------------------------------------------------------------*
! Purpose: *
! Interface RUN and DYNAP command to tracking routine *
! *
!-- Input: *
! switch (int) 1: RUN, 2: DYNAP fastune f *
! turns (int) number of turns to track *
! orbit0 (dp. array) start of closed orbit *
! rt (dp. matrix) one-turn matrix *
!-- Output: *
! eigen dp(6,6) eigenvector matrix *
! coords dp(6,0:turns,npart) (only switch > 1) particle coords. *
! e_flag (int) (only switch > 1) 0: OK, else part. lost *
!-- Buffers: *
! part_id int(npart) *
! last_turn int(npart) *
! last_pos dp(npart) *
! z dp(6,npart) *
! last_orbit dp(6,npart) *
! code_buf int(nelem) local mad-8 code storage *
! l_buf dp(nelem) local length storage *
!----------------------------------------------------------------------*
integer, intent(IN) :: switch, turns
integer, intent(OUT) :: e_flag
double precision, intent(IN) :: orbit0(6)
double precision, intent(IN OUT) :: rt(6,6)
double precision, intent(OUT) :: eigen(6,6), coords(6,0:turns,*)
integer mytracksumm_maxlines ! mytracksumm lines print limit hrr Sep 2021
integer :: part_id(*), last_turn(*), code_buf(*)
double precision :: last_pos(*), z(6,*), dxt(*), dyt(*)
double precision :: last_orbit(6,*), l_buf(*)
double precision :: theta
double precision, dimension (:), allocatable :: theta_buf
logical :: onepass, onetable, last_out, info, aperflag, doupdate, debug
logical :: run=.false.,dynap=.false., thin_foc, onlyaver
logical, save :: first=.true.
logical :: fast_error_func
integer :: i, j, k, code, ffile, n_perm_align
integer :: n_align, nlm, j_tot, turn, nobs, lobs
integer :: nint, ndble, nchar, char_l, tot_segm, int_arr(1)
double precision :: tmp_d,orbit(6), el, re(6,6), deltap, sum, spos
double precision :: al_errors(align_max), zz(6), maxaper(6), obs_orb(6)
character(len=12) :: tol_a='maxaper ', char_a=' '
character(len=name_len) :: el_name
character(len=4) :: vec_names(7)
data vec_names /'x', 'px', 'y', 'py', 't', 'pt', 's'/
character(len=20) :: text
integer range(2) ! hrr Sep 2021 for number of lines in mytracksumm table
integer mytracksumm_per_turns ! hrr Sep 2021 fill table rate
integer mytracksumm_start_particle,mytracksumm_end_particle ! hrr Sep 2021
integer mytracksumm_turns ! hrr Sep 2021 used for mytracksumm table fill rate
save mytracksumm_turns ! hrr Sep 2021
data mytracksumm_turns / 0 / ! hrr Sep 2021
integer N_uni ! hrr Aug 2021 to read frs checkpoint file
logical is_checkpoint !hrr Sep 2021
integer, external :: restart_sequ, advance_node, get_option, node_al_errors, get_nnodes, is_permalign
double precision, external :: node_value, get_variable, get_value, node_obs_point
external :: set_tt_attrib, alloc_tt_attrib, set_tt_multipoles, get_tt_multipoles
! 2015-Jul-08 19:16:53 ghislain: make code more readable
run = switch .eq. 1
dynap = switch .eq. 2
allocate ( theta_buf(get_nnodes()) )
!--- Initialize
deltap = get_value('probe ','deltap ')
betas = get_value('probe ','beta ')
gammas = get_value('probe ','gamma ')
dtbyds = get_value('probe ','dtbyds ')
arad = get_value('probe ','arad ')
radiate = get_value('probe ','radiate ') .ne. zero
thin_cf = get_option('thin_cf ').ne.zero
bet0 = get_value('beam ','beta ')
bet0i = one / bet0
beti = one / betas
deltas = get_variable('track_deltap ')
damp = get_option('damp ') .ne. 0
quantum = get_option('quantum ') .ne. 0
debug = get_option('debug ') .ne. 0
thin_foc = get_option('thin_foc ').eq.1
onlyaver = get_option('only_average ') .ne. 0
call init_elements()
!-------added by Yipeng SUN 01-12-2008--------------
if (deltap .eq. zero) then
onepass = get_option('onepass ') .ne. 0
if (.not.onepass) call trclor(switch, orbit0)
endif
!---- Set fast_error_func flag to use faster error function
!---- including tables. Thanks to late G. Erskine
fast_error_func = get_option('fast_error_func ') .ne. 0
if (fast_error_func .and. .not.fasterror_on) then
call wzset
fasterror_on = .true.
endif
!---- get options for space charge variables
! add if (sc_3d_kick.and..not.bb_sxy_update) failure test into SC_Init later !hrr Sep 2021
call SC_Init(first, run, dynap, turns);
if (fsecarb) then
call fort_warn('TRRUN: ','Second order terms of arbitrary Matrix not allowed for tracking.')
return
endif
aperflag = get_option('aperture ') .ne. 0
e_flag = 0
! flag to avoid double entry of last line
last_out = .false.
onepass = get_option('onepass ') .ne. 0
onetable = get_option('onetable ') .ne. 0
info = get_option('info ') * get_option('warn ') .gt. 0 ! why not just respect info and warn ?
if (onepass) RT = EYE
call trbegn(rt,eigen)
if (run) then
ffile = get_value('run ', 'ffile ')
else if (dynap) then
ffile = 1
endif
! for one_table case
if (first) segment = 0
tot_segm = turns / ffile + 1
if (mod(turns, ffile) .ne. 0) tot_segm = tot_segm + 1
if (first) then
! checkpoint_restart file existence check is in SC_Init !hrr Feb 2022
if (checkpnt_restart) then
read(unit_chpt,END=100) jmax
read(unit_chpt,END=100) Ex_rms0
read(unit_chpt,END=100) Ey_rms0
do i = 1, jmax
do j=1,6
read(unit_chpt,END=100) z(j,i)
enddo
enddo
!frs on 07.06.2016 - fixing
! longitudinal plane must be frozen too!
read(unit_chpt,END=100) sigma_t
read(unit_chpt,END=100) mean_t
! this could be coded to allow either format hee Sep 2021
read(unit_chpt,END=100) N_ini ! hrr Dec 2021 checkpfile stream is unit_chpt not 90
else
call trinicmd(switch,orbit0,eigen,jmax,z,turns,coords)
N_ini = jmax !hrr Sep 2021
endif
R_part = dble(jmax)/dble(N_ini) !hrr Sep 2021
!--- set particle id
!!$OMP PARALLEL PRIVATE(k)
!!$OMP DO
do k=1,jmax
part_id(k) = k
enddo
!!$OMP END DO
!!$OMP END PARALLEL
else
if (jmax .eq. 0) then
call fort_warn('TRRUN: ','Particle Number equals zero: exit from trrun')
return
endif
if (jmax .gt. max_part) then
write(text,'(a,d20.12,a)') "Maximum number of particles (",max_part,") exceeded"
call fort_fail('TRRUN: ',text)
endif
!!$OMP PARALLEL PRIVATE(i,j)
!!$OMP DO
do i = 1, jmax
last_turn(i) = last_turn_keep(i)
part_id(i) = part_id_keep(i)
do j = 1,6
z(j,i) = z_keep(j,i)
enddo
enddo
!!$OMP END DO
!!$OMP END PARALLEL
endif
!--- jmax may be reduced by particle loss - keep number in j_tot
j_tot = jmax
!--- get vector of six coordinate maxapers (both RUN and DYNAP)
call comm_para(tol_a, nint, ndble, nchar, int_arr, maxaper, char_a, char_l)
!hbu--- init info for tables initial s position is 0
spos=0 ; nlm=0 ; el_name='start '
mytracksumm_maxlines = get_option('sc_mytracksumm_maxlines ') ! hrr Jan 2022
mytracksumm_per_turns = get_option('sc_mytracksumm_per_turns ') ! hrr Sep 2021
mytracksumm_turns= mytracksumm_turns + 1 ! hrr Sep 2021
if (mytracksumm_turns.ge.mytracksumm_per_turns) mytracksumm_turns= 0 ! hrr Sep 2021
mytracksumm_start_particle = get_option('sc_mytracksumm_start_particle ') ! hrr Sep 2021
if(mytracksumm_start_particle.lt.1.or.mytracksumm_start_particle.gt.jmax)then !hrr Sep 2021
mytracksumm_start_particle= 1 ! Sep 2021
endif ! hrr Sep 2021
mytracksumm_end_particle = get_option('sc_mytracksumm_end_particle ') ! hrr Sep 2021
if(mytracksumm_end_particle.lt.1.or.mytracksumm_end_particle.gt.jmax) then ! hrr Sep 2021
mytracksumm_end_particle= jmax ! hrr Sep 2021
endif ! hrr Sep 2021
!if (get_option('info ') .ne. 0) then ! hrr Feb 2022
!print *," Info: mytracksumm_maxlines=", mytracksumm_maxlines ! hrr Jan 2022
!print *," Info: mytracksumm_per_turns=", mytracksumm_per_turns ! hrr Sep 2021
!print *," Info: mytracksumm_start/end_particle=", &
!mytracksumm_start_particle,mytracksumm_end_particle ! hrr Sep 2021
!endif
if (first) then
!--- enter start coordinates in summary table
if (run) then
t_max = get_value('probe ','circ ') / get_value('run ', 'track_harmon ') / betas
pt_max = get_value('run ', 'deltap_max ')
pt_max = (sqrt( (betas*(pt_max + one))**2 + one/gammas**2) - one) * beti
else if (dynap) then
t_max = 1d20
pt_max = 1d20
endif
do i = 1, j_tot
if (abs(z(5,i)) .gt. t_max) then
write(text, '(1p,d13.5,a1,i6)') t_max,"p",i
call fort_fail('TRACK_INITIAL: ','Fatal: T-Coordinate larger than' // text)
endif
if (abs(z(6,i)).gt.pt_max) then
write(text, '(1p,d13.5,a1,i6)') pt_max,"p",i
call fort_fail('TRACK_INITIAL: ','Fatal: PT-Coordinate larger than' // text)
endif
enddo
! hrr Sep 2021 apply tracksumm start and end particles also to turn 0
do i = 1,j_tot
tmp_d = i
call double_to_table_curr('tracksumm ', 'number ', tmp_d)
tmp_d = tot_turn
call double_to_table_curr('tracksumm ', 'turn ', tmp_d)
do j = 1, 6
tmp_d = z(j,i) - orbit0(j)
call double_to_table_curr('tracksumm ', vec_names(j), tmp_d)
enddo
!hbu add s
call double_to_table_curr('tracksumm ',vec_names(7),spos)
call augment_count('tracksumm ')
enddo
do i = mytracksumm_start_particle,mytracksumm_end_particle ! hrr Sep 2021
call double_to_table_curr('mytracksumm ', 'number ', dble(i)) ! hrr Sep 2021
call double_to_table_curr('mytracksumm ', 'turn ', dble(tot_turn)) ! hrr Sep 2021
do j = 1, 6
call double_to_table_curr('mytracksumm ', vec_names(j), z(j,i) - orbit0(j)) ! hrr Sep 2021)
enddo
call double_to_table_curr('mytracksumm ', vec_names(7), spos) ! hrr Sep 2021
call augment_count('mytracksumm ') ! hrr Sep 2021
enddo
endif
!--- enter first turn, and possibly eigen in tables
if (run) then
if (onetable) then
if (first) then
call track_pteigen(eigen)
call tt_putone(jmax, tot_turn, tot_segm, segment, part_id, &
z, orbit0, spos, nlm, el_name, onlyaver)
endif
else
do i = 1, jmax
call tt_puttab(part_id(i), 0, 1, z(1,i), orbit0, spos)
enddo
endif
endif
ORBIT = ORBIT0
doupdate = get_option('update ') .ne. 0
!--- loop over turns
nobs = 0
call BB_Init(first);
turnloop: do turn = 1, turns
!--- Write checkpoint_restart data - disable for speed reasons
! rewind 90
! write(90) jmax
! write(90) Ex_rms
! write(90) Ey_rms
! do i = 1, jmax
! do j=1,6
! write(90) z(j,i)
! enddo
! enddo
if (doupdate) call trupdate(turn)
j = restart_sequ()
call BB_Update(jmax, orbit0, z);
nlm = 0
sum = zero
do !--- loop over nodes
bbd_pos = j
if (turn .eq. 1) then
code = node_value('mad8_type ')
! if (code .eq. code_tkicker) code = code_kicker
if (code .eq. code_placeholder) code = code_instrument
el = node_value('l ')
theta = node_value('tilt ')
theta_buf(nlm+1) = theta
code_buf(nlm+1) = code
l_buf(nlm+1) = el
!param(nlm+1, enum_bvk) =
!param(nlm+1, enum_lrad) -=
!param(nlm+1, enum_bvk)
!param(nlm+1, enum_bvk)
if ((code.eq.code_sextupole .or. &
code.eq.code_octupole .or. &
code.eq.code_elseparator .or. &
code.eq.code_crabcavity) .and. el.ne.zero) then
!if (.not. (is_drift() .or. is_thin() .or. is_quad() .or. is_dipole() .or. is_matrix()) ) then
call element_name(el_name,len(el_name))
print *," "
print *,el_name, "code: ",code," el: ",el," THICK ELEMENT FOUND"
print *," "
print *,"Track dies nicely"
print *,"Thick lenses will get nowhere"
print *,"MAKETHIN will save you"
print *," "
print *," "
sum = node_value('name ')
call fort_fail('TRRUN: Fatal ','----element with length found : CONVERT STRUCTURE WITH MAKETHIN')
endif
else
el = l_buf(nlm+1)
code = code_buf(nlm+1)
theta = theta_buf(nlm+1)
endif
if (run) nobs = node_obs_point()
!-------- Misalignment at beginning of element (from twissfs.f)
if (code .ne. code_drift) then
AL_ERRORS = zero
n_align = node_al_errors(al_errors)
n_perm_align = is_permalign()
if (n_perm_align .ne. 0) then
al_errors(1) = al_errors(1) + node_value('dx ')
al_errors(2) = al_errors(2) + node_value('dy ')
al_errors(3) = al_errors(3) + node_value('ds ')
al_errors(5) = al_errors(5) + node_value('dtheta ')
al_errors(4) = al_errors(4) + node_value('dphi ')
al_errors(6) = al_errors(6) + node_value('dpsi ')
n_align = 1
endif
if (n_align .ne. 0) then
do i = 1, jmax
ZZ(:) = Z(:,i)
call tmali1(zz, al_errors, betas, gammas, z(1,i), re)
enddo
endif
endif
!-------- Track through element // suppress dxt 13.12.04
call ttmap(switch, code, el, z, jmax, dxt, dyt, sum, tot_turn+turn, part_id, &
last_turn, last_pos, last_orbit, aperflag, maxaper, al_errors, onepass, debug, theta, thin_foc,.false.)
!-------- Space Charge update
call SC_Update(orbit0, z);
!-------- Misalignment at end of element (from twissfs.f)
if (code .ne. code_drift .and. n_align.ne.0) then
do i = 1, jmax
ZZ(:) = Z(:,i)
call tmali2(el, zz, al_errors, betas, gammas, z(1,i), re)
enddo
endif
nlm = nlm + 1
if (nobs .gt. 0) then
OBS_ORB = zero
call get_node_vector('obs_orbit ', lobs, obs_orb)
if (lobs .lt. 6) &
call fort_fail('TRACK: Fatal ', 'obs. point orbit not found')
if (onetable) then
spos = sum
call element_name(el_name,len(el_name))
if (mod(turn, ffile) .eq. 0) then
call tt_putone(jmax, tot_turn+turn, tot_segm, segment, part_id, &
z, obs_orb,spos,nlm,el_name,onlyaver)
endif
else
if (mod(turn, ffile) .eq. 0) then
do i = 1, jmax
call tt_puttab(part_id(i), turn, nobs, z(1,i), obs_orb,spos)
enddo
endif
endif
endif
if (advance_node() .eq. 0) exit
j=j+1
end do !--- end of loop over nodes
if (run) then
if (mod(turn, ffile) .eq. 0) then
if (turn .eq. turns) last_out = .true.
if (onetable) then
spos=sum
call element_name(el_name,len(el_name))
call tt_putone(jmax, tot_turn+turn, tot_segm, segment, part_id, &
z, orbit0,spos,nlm,el_name,onlyaver)
else
do i = 1, jmax
call tt_puttab(part_id(i), turn, 1, z(1,i), orbit0, spos)
enddo
endif
endif
else if (dynap) then
!!$OMP PARALLEL PRIVATE(i,j)
!!$OMP DO
do i = 1, jmax
do j = 1, 6
coords(j,turn,i) = z(j,i) - orbit0(j)
enddo
enddo
!!$OMP END DO
!!$OMP END PARALLEL
endif
if (jmax .eq. 0 .or. (switch .gt. 1 .and. jmax .lt. j_tot)) goto 20 ! break loop over turns
if (dynap .and. info .and. mod(turn,100) .eq. 0) print *, 'turn :', turn
if (exit_loss_turn .and. lost_in_turn) then
lost_in_turn = .false.
write(text, '(i6)') turn
call fort_warn('TRRUN Frozen SC: ', 'Stop in Loss Turn: '//text)
goto 20 ! break loop over turns
endif
end do turnloop !--- end of loop over turns
20 continue
if (dynap .and. jmax.lt.j_tot) then
e_flag = 1
return
endif
do i = 1, jmax
last_turn(part_id(i)) = min(turns+tot_turn, turn+tot_turn)
last_pos(part_id(i)) = sum
last_orbit(1:6,part_id(i)) = z(1:6,i)
enddo
turn = min(turn, turns)
! call BB_Update2(jmax, orbit0, z, part_id, last_turn);
call BB_Update2(turn, orbit0, z, part_id, last_turn); ! hrr bug fix jmax should be turn Aug 2021
!--- enter last turn in tables if not done already
if (run .and. .not.last_out) then
if (onetable) then
spos = sum
call element_name(el_name,len(el_name))
call tt_putone(jmax, tot_turn+turn, tot_segm, segment, part_id, &
z, orbit0,spos,nlm,el_name,onlyaver)
else
do i = 1, jmax
call tt_puttab(part_id(i), turn, 1, z(1,i), orbit0,spos)
enddo
endif
endif
!--- Write checkpoint_restart data
call BB_Write(jmax, orbit0, z, dynap);
!--- enter last turn in summary table.
! Add as well as tracksumm recreated each RUN call new permanent table mytracksumm. ! hrr Feb 2022
do i = 1, j_tot
! change index i to part_id(i) in this loop hrr Feb 2022
call double_to_table_curr('tracksumm ', 'number ', dble(part_id(i)))
call double_to_table_curr('tracksumm ', 'turn ', dble(last_turn(part_id(i))))
do j = 1, 6
call double_to_table_curr('tracksumm ', vec_names(j), last_orbit(j,part_id(i)) - orbit0(j))
enddo
spos = last_pos(part_id(i))
call double_to_table_curr('tracksumm ',vec_names(7),spos)
call augment_count('tracksumm ')
enddo
!--- Enter last turn in summary table - mytracksumm is kept between trrun calls. hrr Sep 2021
! For the event of a lost particle in trkill must use part_id(i) not i and i=1,jmax. hrr Sep 2021
!--- For case of RUN with turns >1 each last turn in mytracksumm_per_turns is kept. hrr Sep 2021
if(mytracksumm_turns.eq.0) then ! hrr April 2020
mytracksumm_end_particle= min(mytracksumm_end_particle,jmax) ! hrr Sep 2021 allow for lost particle
do i = mytracksumm_start_particle,mytracksumm_end_particle ! hrr Sep 2021
! hrr Sep 2021 change index i to part_id(i) in this loop
call double_to_table_curr('mytracksumm ', 'number ', dble(part_id(i))) !hrr Sep 2021
call double_to_table_curr('mytracksumm ', 'turn ', dble(last_turn(part_id(i)))) !hrr Sep 2021
do j = 1, 6
call double_to_table_curr('mytracksumm ', vec_names(j), last_orbit(j,part_id(i)) - orbit0(j)) !hrr Sep 2021
enddo
spos = last_pos(part_id(i)) !hrr Sep 2021
call double_to_table_curr('mytracksumm ',vec_names(7),spos) !hrr Sep 2021
call augment_count('mytracksumm ') !hrr Sep 2021
enddo
endif
! mytracksumm_maxlineso is defined as 16000008 in util.f90. hrr Sep 2021
if(mytracksumm_maxlines.gt.mytracksumm_maxlineso) then ! hrr Sep 2021
write(text, '(1p,2i8)') mytracksumm_maxlines,mytracksumm_maxlineso ! hrr Sep 2021
call aafail('trrun SCdat:', 'mytracksumm_maxlines > mytracksumm_maxlineso =' // text) ! hrr Sep 2021
endif ! hrr Sep 2021
return
100 call fort_fail('TRACK: Fatal ', 'checkpoint_restart file corrupted')
end subroutine trrun
subroutine init_elements
use track_enums
use trackfi
use twtrrfi
use code_constfi
implicit none
logical:: aperflag
integer:: j, code
integer, external :: restart_sequ, advance_node, get_option
double precision, external :: node_value
external :: update_node_aperture
aperflag = get_option('aperture ') .ne. 0
j = restart_sequ()
do !---- loop over nodes
code = node_value('mad8_type ')
! if (code .eq. code_tkicker) code = code_kicker
if(code .eq. code_multipole) then
call alloc_tt_attrib(total_enums)
call set_tt_attrib(enum_other_bv, node_value('other_bv '))
call set_tt_attrib(enum_lrad, node_value('lrad '))
call set_tt_attrib(enum_noise, node_value('noise '))
call set_tt_attrib(enum_angle, node_value('angle '))
call set_tt_attrib(enum_time_var, node_value('time_var '))
call set_tt_multipoles(maxmul)
endif
if(code.eq.code_hkicker .or. code.eq.code_vkicker .or. &
code.eq.code_kicker .or. code.eq.code_tkicker) then
call alloc_tt_attrib(total_enums)
call set_tt_attrib(enum_other_bv, node_value('other_bv '))
call set_tt_attrib(enum_sinkick, node_value('sinkick '))
call set_tt_attrib(enum_kick, node_value('kick '))
call set_tt_attrib(enum_chkick, node_value('chkick '))
call set_tt_attrib(enum_cvkick, node_value('cvkick '))
call set_tt_attrib(enum_hkick, node_value('hkick '))
call set_tt_attrib(enum_vkick, node_value('vkick '))
endif
if(aperflag .and. code .ne. code_drift) then
call update_node_aperture()
endif
if (advance_node() .eq. 0) exit
end do !--- end of loop over nodes to set upt things
end subroutine init_elements
subroutine ttmap(switch,code,el,track,ktrack,dxt,dyt,sum,turn,part_id, &
last_turn,last_pos,last_orbit,aperflag,maxaper,al_errors,onepass, debug, theta, thin_foc, isFirst)
use twtrrfi
use twiss0fi
use name_lenfi
use math_constfi, only : zero, one
use code_constfi
use aperture_enums
use trackfi
implicit none
!----------------------------------------------------------------------*
! Purpose: *
! - Test apertures *
! - Track through thin lenses ONLY. *
! *
! Input/output: *
! switch (integer) 1: RUN, 2: DYNAP fastune *
! SUM (double) Accumulated length. *
! TRACK(6,*)(double) Track coordinates: (X, PX, Y, PY, T, PT). *
! NUMBER(*) (integer) Number of current track. *
! KTRACK (integer) number of surviving tracks. *
!----------------------------------------------------------------------*
integer :: switch, code, ktrack
integer :: turn, part_id(*), last_turn(*)
double precision :: el, sum
double precision :: track(6,*), dxt(*), dyt(*), last_pos(*)
double precision :: last_orbit(6,*), maxaper(6), al_errors(align_max)
logical :: aperflag, onepass, lost_global
logical :: fmap, debug, isFirst
logical :: run=.false., dynap=.false., thin_foc
integer :: i, nn, jtrk, apint
double precision :: ct, tmp, st, theta
double precision :: ap1, ap2, ap3, ap4, aperture(maxnaper)
double precision :: offset(2), offx, offy
double precision :: ek(6), re(6,6), te(6,6,6), craporb(6)
character(len=name_len) :: aptype
integer, external :: get_option, node_apertype
double precision, external :: get_value, node_value
double precision, parameter :: min_double = 1.e-36
external :: node_aperture_vector, node_aperture_offset
! 2015-Jul-08 19:16:53 ghislain: make code more readable
run = switch .eq. 1
dynap = switch .eq. 2
fmap=.false.
!---- Drift space; no rotation or aperture check, go straight to tracking and return
if (code .eq. code_drift) then
call ttdrf(el,track,ktrack)
sum = sum + el
return
endif
!---- Rotate trajectory before entry
if (theta .ne. zero) then
st = sin(theta)
ct = cos(theta)
do jtrk = 1,ktrack
tmp = track(1,jtrk)
track(1,jtrk) = ct * tmp + st * track(3,jtrk)
track(3,jtrk) = ct * track(3,jtrk) - st * tmp
tmp = track(2,jtrk)
track(2,jtrk) = ct * tmp + st * track(4,jtrk)
track(4,jtrk) = ct * track(4,jtrk) - st * tmp
enddo
endif
!---- Test aperture. ALL ELEMENTS BUT DRIFTS and BEAMBEAM
if (aperflag .and. code.ne.code_beambeam) then
nn=name_len
apint=node_apertype()
if(apint .eq. ap_notset) then
! make global check even if aperture is not defined
lost_global =.false.
do jtrk = 1,ktrack
lost_global = ISNAN(track(2,jtrk)) .or. ISNAN(track(4,jtrk)) .or. &
ISNAN(track(5,jtrk)) .or. ISNAN(track(6,jtrk)) .or. &
abs(track(1, jtrk)) .gt. maxaper(1) .or. abs(track(2, jtrk)) .gt. maxaper(2) .or. &
abs(track(3, jtrk)) .gt. maxaper(3) .or. abs(track(4, jtrk)) .gt. maxaper(4) .or. &
abs(track(5, jtrk)) .gt. maxaper(5) .or. abs(track(6, jtrk)) .gt. maxaper(6)
if(lost_global) then
APERTURE(:maxnaper) = zero
call get_node_vector('aperture ',nn,aperture)
OFFSET = zero
call get_node_vector('aper_offset ',nn,offset)
call trcoll(apint, aperture, offset, al_errors, maxaper, &
turn, sum, part_id, last_turn, last_pos, last_orbit, track, ktrack, debug)
EXIT ! They are anyway checked against all the particles so no need to continue to loop
endif
enddo
else
!APERTURE(:maxnaper) = zero
!call get_node_vector('aperture ',nn,aperture)
call node_aperture_vector(aperture)
call node_aperture_offset(offset)
!OFFSET = zero
!call get_node_vector('aper_offset ',nn,offset)
if (debug) then
print *, " aperture type ", apint
print *, " parameters ", (aperture(i),i=1,4)
print *, " offsets ", (offset(i),i=1,2)
print *, " alignment errors ", (al_errors(i),i=11,12)
print *, " maximum aperture ", (offset(i),i=1,2)
print *, " "
endif
call trcoll(apint, aperture, offset, al_errors, maxaper, &
turn, sum, part_id, last_turn, last_pos, last_orbit, track, ktrack, debug)
endif
endif ! Test aperture
! Switch based on element code for element specific tracking
select case (code)
case (code_rbend, code_sbend)
call tttdipole(track,ktrack, code)
case (code_matrix)
EK(:6) = zero
RE(:6,:6) = zero
TE(:6,:6,:6) = zero
CRAPORB(:6) = zero
call tmarb(.false.,.false.,craporb,fmap,ek,re,te)
call tttrak(ek,re,track,ktrack)
case (code_quadrupole)
call tttquad(track,ktrack)
case (code_multipole)
if(thin_cf .and. node_value('lrad ') .gt. zero ) then
call ttmult_cf_mini(track,ktrack,dxt,dyt,turn,thin_foc)
else
call ttmult(track,ktrack,dxt,dyt,turn,thin_foc)
endif
case (code_solenoid)
call trsol(track, ktrack,dxt,dyt)
case (code_rfcavity)
call ttrf(track,ktrack)
if (run) &
call ttrfloss(turn,sum,part_id,last_turn,last_pos,last_orbit,track,ktrack)
case (code_elseparator)
call ttsep(track, ktrack)
case (code_srotation)
call ttsrot(track, ktrack)
case (code_yrotation)
call ttyrot(track, ktrack)
case (code_xrotation)
call ttxrot(track, ktrack)
case (code_hkicker, code_vkicker, code_kicker, code_tkicker)
call ttcorr(el, track, ktrack, turn, code)
!case (code_ecollimator)
! call fort_warn('TRRUN: ','found deprecated ECOLLIMATOR element; should be replaced by COLLIMATOR')
! case (code_rcollimator)
! call fort_warn('TRRUN: ','found deprecated RCOLLIMATOR element; should be replaced by COLLIMATOR')
case (code_beambeam)
call ttbb(track, ktrack)
case (code_twcavity)
! call ttlcav(el, track, ktrack)
case (code_dipedge)
call ttdpdg(track,ktrack)
case (code_translation)
if (onepass) call tttrans(track,ktrack)
case (code_crabcavity)
call ttcrabrf(track,ktrack,turn)
case (code_hacdipole)
call tthacdip(track,ktrack,turn)
case (code_vacdipole)
call ttvacdip(track,ktrack,turn)
case (code_nllens)
call ttnllens(track,ktrack)
case (code_rfmultipole)
call ttrfmult(track,ktrack,turn)
case (code_wire)
call ttwire(track,ktrack, isFirst)
case(code_changerefp0)
call ttchangep0(track,ktrack)
case (code_hmonitor:code_rcollimator, code_instrument, &
code_slmonitor:code_imonitor, code_placeholder)
if(el .gt. 0) call ttdrf(el,track,ktrack)
case default ! The rest: do nothing
end select
! This is where we should Test Aperture at exit of elements
! by calling trcoll again
!---- Rotate trajectory at exit
if (theta .ne. zero) then
do jtrk = 1,ktrack
tmp = track(1,jtrk)
track(1,jtrk) = ct * tmp - st * track(3,jtrk)
track(3,jtrk) = ct * track(3,jtrk) + st * tmp
tmp = track(2,jtrk)
track(2,jtrk) = ct * tmp - st * track(4,jtrk)
track(4,jtrk) = ct * track(4,jtrk) + st * tmp
enddo
endif
!---- Accumulate length.
sum = sum + el
return
end subroutine ttmap
SUBROUTINE ttmult_cf_mini(track,ktrack,dxt,dyt,turn, thin_foc)
use twtrrfi
use twissbeamfi, only : deltap
use math_constfi, only : zero, one, two, three
use time_varfi
use trackfi
use time_varfi
use track_enums
implicit none
!----------------------------------------------------------------------*
! Purpose: *
! Computes thin-lens kick through combined-function magnet. *
! Input: *
! fsec (logical) if true, return second order terms. *
! ftrk (logical) if true, track orbit. *
! Input/output: *
! orbit(6) (double) closed orbit. *
! Output: *
! fmap (logical) if true, element has a map. *
! re(6,6) (double) transfer matrix. *
! te(6,6,6) (double) second-order terms. *
! Detailed description: *
! Phys. Rev. AccelBeams 19.054002 by M. Titze
!----------------------------------------------------------------------*
double precision :: track(6,*), dxt(*), dyt(*), ttt
logical :: time_var,thin_foc
integer :: ktrack, turn
logical :: fsec, ftrk, fmap
integer :: nord, k, j, nn, ns, bvk, iord, n_ferr, jtrk, nd
integer, external :: Factorial
double precision :: dpx, dpy, tilt, kx, ky, elrad, bp1, h0, ktap
double precision :: dipr, dipi, dbr, dbi, dtmp, an, angle, tilt2
double precision :: gstr, sstr, x, px0, y, py0, t0, pt0, deltapp
double precision :: normal(0:maxmul), skew(0:maxmul), f_errors(0:maxferr)
!double precision :: orbit(6),
double complex :: kappa, barkappa, sum0, del_p_g, pkick, dxdpg, dydpg, &
dxx, dxy, dyy, rp, rm
double complex :: lambda(0:maxmul)
double complex :: g(0:maxmul, 0:maxmul)
double precision, external :: node_value, get_tt_attrib, get_value
integer, external :: node_fd_errors
fmap = .true.
! Read magnetic field components & fill lambda's according to field
! components relative to given plane
F_ERRORS(0:maxferr) = zero
n_ferr = node_fd_errors(f_errors)
bvk = get_tt_attrib(enum_other_bv)
!---- Multipole length for radiation.
elrad = get_tt_attrib(enum_lrad)
an = get_tt_attrib(enum_angle)
time_var = get_tt_attrib(enum_time_var) .ne. 0
!----Tapering factor
ktap = get_tt_attrib(enum_ktap)
!dbr = bvk * f_errors(0) !field(1,0)
!dbi = bvk * f_errors(1) !field(2,0)
!---- Nominal dipole strength.
! dipr = bvk * vals(1,0) / (one + deltas)
! dipi = bvk * vals(2,0) / (one + deltas)
!bet0 = get_value('probe ','beta ')
!---- Multipole components.
NORMAL(0:maxmul) = zero! ; call get_node_vector('knl ',nn,normal)
SKEW(0:maxmul) = zero ! ; call get_node_vector('ksl ',ns,skew)
call get_tt_multipoles(nn,normal,ns,skew,ktap)
dipr = bvk * normal(0) !vals(1,0)
dipi = bvk * skew(0)
! cf magnet with quadrupole & sextupole
gstr = normal(1)/elrad
sstr = normal(2)/elrad
do jtrk = 1,ktrack
x = track(1,jtrk)
px0 = track(2,jtrk)
y = track(3,jtrk)
py0 = track(4,jtrk)
t0 = track(5,jtrk)
pt0 = track(6,jtrk)
! get \Delta p/p + 1 (which we denote by the variable
! deltapp here) out of orbit(6), for the corresponding
! particle
deltapp = sqrt(pt0*pt0 + two*pt0*beti + one);
! orbit transformation:
! attention: The following formulas constitute only the kick part
! of the CF in a drift-kick-drift decomposition.
!speedup hrr Nov 2021
! track(2, jtrk) = (elrad**2*(-gstr*x - 0.5*x**2*sstr +&
! &0.5*y**2*sstr) + elrad*px0 +&
! &elrad*(dipi*y**3*sstr/6.0d0 - dipr*gstr*x**2 +&
! &0.5*dipr*gstr*y**2 - 0.5*dipr*x**3*sstr + dipr*x*y**2*sstr +&
! &dipr*deltapp - dipr) -&
! &dipr*(-dipi*gstr*y**3/6.0d0 + dipi*y +&
! &dipr*x))/elrad
track(2, jtrk) = (elrad*elrad*(-gstr*x - 0.5*x*x*sstr +&
&0.5*y*y*sstr) + elrad*px0 +&
&elrad*(dipi*y*y*y*sstr/6.0d0 - dipr*gstr*x*x +&
&0.5*dipr*gstr*y*y - 0.5*dipr*x*x*x*sstr + dipr*x*y*y*sstr +&
&dipr*deltapp - dipr) -&
&dipr*(-dipi*gstr*y*y*y/6.0d0 + dipi*y +&
&dipr*x))/elrad
! speedup hrr Nov 2021
! track(4, jtrk) = (elrad**2*y*(gstr + x*sstr) + elrad*py0 +&
! &elrad*(0.5*dipi*gstr*y**2 + 0.5*dipi*x*y**2*sstr + dipi*deltapp&
! & - dipi + dipr*gstr*x*y + dipr*x**2*y*sstr -&
! &dipr*y**3*sstr/6.0d0) -&
! &dipi**2*gstr*y**3/6.0d0 - dipi**2*y +&
! &0.5*dipi*dipr*gstr*x*y**2 - dipi*dipr*x +&
! &dipr**2*gstr*y**3/6.0d0)/elrad
track(4, jtrk) = (elrad*elrad*y*(gstr + x*sstr) + elrad*py0 +&
&elrad*(0.5*dipi*gstr*y*y + 0.5*dipi*x*y*y*sstr + dipi*deltapp&
& - dipi + dipr*gstr*x*y + dipr*x*x*y*sstr -&
&dipr*y*y*y*sstr/6.0d0) -&
&dipi*dipi*gstr*y*y*y/6.0d0 - dipi*dipi*y +&
&0.5*dipi*dipr*gstr*x*y*y - dipi*dipr*x +&
&dipr*dipr*gstr*y*y*y/6.0d0)/elrad
track(5, jtrk) = (betas*t0*deltapp - (betas*pt0 +&
&1.0)*(dipi*y + dipr*x))/(betas*deltapp)
enddo
end SUBROUTINE ttmult_cf_mini
SUBROUTINE ttmult_cf(track,ktrack,dxt,dyt,turn, thin_foc)
use twtrrfi
use twissbeamfi, only : beta
use math_constfi, only : zero, one, two, three
use time_varfi
use trackfi
use time_varfi
use track_enums
implicit none
!----------------------------------------------------------------------*
! Purpose: *
! Computes thin-lens kick through combined-function magnet. *
! Input: *
! fsec (logical) if true, return second order terms. *
! ftrk (logical) if true, track orbit. *
! Input/output: *
! orbit(6) (double) closed orbit. *
! Output: *