-
Notifications
You must be signed in to change notification settings - Fork 39
/
trrun.f90
5838 lines (5137 loc) · 210 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 track_enums
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 :: 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
logical, save :: first=.true.
logical :: bb_sxy_update, virgin_state, emittance_update
logical :: checkpnt_restart, fast_error_func, exit_loss_turn
integer :: i, j, k, code, ffile
integer :: n_align, nlm, j_tot, turn, nobs, lobs
integer :: nint, ndble, nchar, char_l, tot_segm, int_arr(1)
integer, save :: jmax, segment
integer, save :: tot_turn=0
integer, parameter :: max_part=20000
integer, save :: part_id_keep(max_part), last_turn_keep(max_part)
double precision :: orbit(6), el, re(6,6), deltap, sum, spos
double precision :: al_errors(align_max), zz(6), maxaper(6), obs_orb(6)
double precision, save :: betx_start=one, bety_start=one
double precision, save :: alfx_start=zero, alfy_start=zero
double precision, save :: gamx_start=zero, gamy_start=zero
double precision, save :: dx_start=zero, dpx_start=zero
double precision, save :: dy_start=zero, dpy_start=zero
double precision, save :: ex_rms0=zero, ey_rms0=zero, sigma_p0=zero, sigma_z0=zero
double precision, save :: N_ions_in_beam, Npart_gain, N_ions_ini, n_ions_macro, N_ions_for_bb
double precision, save :: sigma_z_ini, z_factor, t_rms, pt_rms, z_keep(6,max_part)
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
!VVK 20100321 -------------------------------------------------
integer :: i_part ! local counter
double precision :: Summ_t_mean ! local for mean value
double precision :: Summ_t_square ! local for rms value
!-------------------------------------------------------------------
integer, external :: restart_sequ, advance_node, get_option, node_al_errors, get_nnodes
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
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
exit_loss_turn = get_option('exit_loss_turn ') .ne. 0
bb_sxy_update = get_option('bb_sxy_update ') .ne. 0
checkpnt_restart = get_value('run ', 'checkpnt_restart ') .ne. zero
emittance_update = get_option('emittance_update ') .ne. 0
virgin_state = get_value('run ', 'virgin_state ') .ne. zero
if (run .and. bb_sxy_update) then
open(90,file='checkpoint_restart.dat',form='unformatted',status='unknown')
else if (dynap) then
bb_sxy_update = .false.
checkpnt_restart = .false.
endif
if (bb_sxy_update) then
if (virgin_state) first=.true.
call table_input( betx_start, bety_start, &
alfx_start, alfy_start, &
gamx_start, gamy_start, &
dx_start, dpx_start, &
dy_start, dpy_start)
if (first) call make_bb6d_ixy(turns)
endif
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
if (checkpnt_restart) then
read(90,END=100) jmax
read(90,END=100) Ex_rms0
read(90,END=100) Ey_rms0
do i = 1, jmax
do j=1,6
read(90,END=100) z(j,i)
enddo
enddo
!frs on 07.06.2016 - fixing
! longitudinal plane must be frozen too!
read(90,END=100) sigma_t
read(90,END=100) mean_t
else
call trinicmd(switch,orbit0,eigen,jmax,z,turns,coords)
endif
!--- 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 '
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
call double_to_table_curr('tracksumm ', 'number ', dble(i))
call double_to_table_curr('tracksumm ', 'turn ', dble(tot_turn))
do j = 1, 6
call double_to_table_curr('tracksumm ', vec_names(j), z(j,i) - orbit0(j))
enddo
call double_to_table_curr('tracksumm ', vec_names(7), spos)
call augment_count('tracksumm ')
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)
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
if (bb_sxy_update) then
trrun_nt = 0
if (first) then
time_var_m_cnt = 0 ; time_var_p_cnt = 0 ; time_var_c_cnt = 0
! <<N_macro_part_ini = N_macro_surv + N_macro_lost>>
N_ions_in_beam = get_value('probe ', 'npart ') !BEAM->NPART
if (N_ions_in_beam .lt. zero) call fort_fail('TRRUN: ','N_ions_in_beam .lt. zero')
Npart_gain = get_value('run ', 'n_part_gain ')
N_ions_ini = Npart_gain * N_ions_in_beam
N_macro_surv = jmax ! = number of START lines submitted
n_ions_macro = N_ions_ini/N_macro_surv
N_for_I = N_macro_surv ! at start (to be redefined in Ixy)
if (N_macro_surv .gt. N_macro_max) &
call fort_fail('TRRUN: ', 'Number N_macro_surv exceeds N_macro_max (array size)')
! 2015-Jul-03 18:07:00 ghislain: BUG or voluntary ?
if (N_macro_surv .gt. N_macro_surv) &
call fort_fail('TRRUN: ', 'Number START-lines exceeds the initial number of macroparticles N_macro_surv')
t_rms = get_value('run ', 'sigma_z ') * beti
pt_rms = get_value('run ', 'deltap_rms ')
pt_rms = (sqrt((betas * (pt_rms + one))**2 + one/gammas**2) - one) * beti
sigma_z_ini = t_rms !betas: BEAM->BETA
sigma_z = sigma_z_ini !at start (to be redefined in Ixy)
sigma_p = pt_rms !default
z_factor = one !at start sigma_z_ini/sigma_z
Ex_rms = get_value('probe ', 'ex ') !BEAM->Ex
Ey_rms = get_value('probe ', 'ey ') !BEAM->Ey
if (checkpnt_restart.and.emittance_update) then
Ex_rms = Ex_rms0
Ey_rms = Ey_rms0
endif
! write(8,'(4(g16.9,1x))') Ex_rms, Ey_rms,sigma_z,sigma_p
first = .false.
endif
endif
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()
if (bb_sxy_update) then
trrun_nt = turn
time_var_m_lnt = 0 ; time_var_p_lnt = 0 ; time_var_c_lnt = 0
N_macro_surv = jmax
i_spch = 0 !a special spch-update counter
ex_rms0 = ex_rms
ey_rms0 = ey_rms
sigma_z0 = sigma_z
sigma_p0=sigma_p
!sigma_p0 = sigma_p !CM, 3/11/14
!fill, table=Ixy_unsorted; column=i_macro_part, Ix, Iy, dpi, z_part;
!new on 3/31/14:
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
! if (emittance_update) then
call ixy_calcs(betas, orbit0, z, &
betx_start, bety_start, &
alfx_start, alfy_start, &
gamx_start, gamy_start, &
dx_start, dpx_start, &
dy_start, dpy_start)
call ixy_fitting()
call double_to_table_curr('bb6d_ixy ', 'turn ', dble(tot_turn+turn))
call double_to_table_curr('bb6d_ixy ', 'n_macro_surv ', dble(n_macro_surv))
call double_to_table_curr('bb6d_ixy ', 'n_for_i ', dble(n_for_i))
call double_to_table_curr('bb6d_ixy ', 'ex_rms ', ex_rms)
call double_to_table_curr('bb6d_ixy ', 'ey_rms ', ey_rms)
call double_to_table_curr('bb6d_ixy ', 'sigma_p ', sigma_p)
call double_to_table_curr('bb6d_ixy ', 'sigma_z ', sigma_z)
call augment_count('bb6d_ixy ')
if (sigma_p0 .eq. zero) sigma_p0 = sigma_p
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
!new on 3/31/14:
! endif
if (.not.emittance_update) then
ex_rms = ex_rms0
ey_rms = ey_rms0
sigma_z = sigma_z0
sigma_p = sigma_p0
endif
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
! sigma_p=sigma_p0
z_factor = one
if ( sigma_z.gt.zero .and. sigma_z_ini.gt.zero) z_factor = sigma_z_ini/sigma_z
N_ions_for_bb = n_ions_macro * N_for_I * z_factor
if (N_ions_in_beam .le. zero) then
rat_bb_n_ions = zero
else
rat_bb_n_ions = N_ions_for_bb/N_ions_in_beam
endif
time_var_m = .false. ; time_var_p = .false. ; time_var_c = .false.
if (idnint(time_var_m_nt(time_var_m_cnt+1)) .eq. tot_turn+turn) time_var_m=.true.
if (idnint(time_var_p_nt(time_var_p_cnt+1)) .eq. tot_turn+turn) time_var_p=.true.
if (idnint(time_var_c_nt(time_var_c_cnt+1)) .eq. tot_turn+turn) time_var_c=.true.
endif ! bb_sxy_update
nlm = 0
sum = zero
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
!frs on 07.06.2016 - fixing
! longitudinal plane must be frozen too!
if (bb_sxy_update) then
if(emittance_update.or.(.not.emittance_update.and.mean_t.eq.0d0.and.sigma_t.eq.0d0)) then
!VVK 20100321 -------- Find RMS-value of t ----------------------
! if we do 1-turn tracking, orbit0(5)=0 always
Summ_t_mean = zero
do i_part = 1, jmax
if (abs(z(5,i_part)) .ge. zero) then
Summ_t_mean = Summ_t_mean + z(5,i_part)
else
print *, 'NaN z(5,i) ? :', i_part, z(5,i_part)
endif
enddo
mean_t = Summ_t_mean/dble(jmax)
Summ_t_square = zero
do i_part = 1, jmax
if (abs(z(5,i_part)) .ge. zero) &
Summ_t_square = Summ_t_square + (z(5,i_part) - mean_t)**2
enddo
sigma_t = sqrt(Summ_t_square/dble(jmax))
if (abs(sigma_t) .eq. zero) then
sigma_t=t_max/two
call fort_warn('TTRUN Frozen SC: sigma_t = zero: ','sigma_t set to L/track_harmon/betas/2')
endif
endif
!-----------------------------------------------------------------
endif
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_rfcavity .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)
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)
!-------- Space Charge update
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
ex_rms0=ex_rms
ey_rms0=ey_rms
sigma_z0=sigma_z
sigma_p0=sigma_p
if (bb_sxy_update .and. is_lost) then
call ixy_calcs(betas, orbit0, z, &
betx_start, bety_start, &
alfx_start, alfy_start, &
gamx_start, gamy_start, &
dx_start, dpx_start, &
dy_start, dpy_start)
call ixy_fitting()
is_lost = .false.
endif
if( .not. emittance_update) then
ex_rms=ex_rms0
ey_rms=ey_rms0
sigma_z=sigma_z0
sigma_p=sigma_p0
endif
!frs on 04.06.2016 - fixing
!a) bug concerning sigma_p
!b) Filling data in file bb6d_ixy.txt even for "emittance_update = .false.",
! obviously without update!
!c) Fixing checkpnt_restart for "emittance_update = .false." which
! worked for ".true." alright.
!-------- 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))
call tt_putone(jmax, tot_turn+turn, tot_segm, segment, part_id, &
z, obs_orb,spos,nlm,el_name)
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)
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)
if (bb_sxy_update) then
tot_turn = tot_turn + turn
!$OMP PARALLEL PRIVATE(i,j)
!$OMP DO
do i = 1, jmax
part_id_keep(i) = part_id(i)
last_turn_keep(i) = last_turn(i)
do j=1,6
z_keep(j,i) = z(j,i)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
endif
!--- 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)
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
if (bb_sxy_update) then
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
!frs on 07.06.2016 - fixing
! longitudinal plane must be frozen too!
write(90) sigma_t
write(90) mean_t
endif
!--- enter last turn in summary table
do i = 1, j_tot
call double_to_table_curr('tracksumm ', 'number ', dble(i))
call double_to_table_curr('tracksumm ', 'turn ', dble(last_turn(i)))
do j = 1, 6
call double_to_table_curr('tracksumm ', vec_names(j), last_orbit(j,i) - orbit0(j))
enddo
spos = last_pos(i)
call double_to_table_curr('tracksumm ',vec_names(7),spos)
call augment_count('tracksumm ')
enddo
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)
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
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
! print *, "apint", apint, "ap_notset", ap_notset
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 anway 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(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_hmonitor:code_rcollimator, code_instrument, &
code_slmonitor:code_imonitor, code_placeholder, code_collimator)
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(track,ktrack,dxt,dyt,turn, thin_foc)
use twtrrfi
use twissbeamfi, only : deltap, beta
use math_constfi, only : zero, one, two, three
use time_varfi
use trackfi
use time_varfi
use track_enums