forked from MethodicalAcceleratorDesign/MAD-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
madx_ptc_track_run.f90
3857 lines (3197 loc) · 195 KB
/
madx_ptc_track_run.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
!Comments from Piotr
! The module was not documented by FRS & Kapin, below my comments from reverse engineering
!
! The loop over turns is in the main routine ptc_track_run
!
! There 2 modes it operates with:
!
! mode1: element_by_element=TRUE - normal ray tracking of PTC element to element
! if an element is an observation point coordinates are stored in a table
! implemented in routine One_turn_track_with_PTC
!
! mode2: element_by_element=FALSE - ray tracking over one turn
! if other than START observation points are present then
! the coordinates are transformed with transfer map that is calculated at the end (routine Observation_with_PTC)
! implemented in track_beam_elementwise_with_PTC
!END Comments from Piotr
MODULE madx_ptc_track_run_module
! This module serve as a COMMON block as in F77
! It contains variables which exchange data between
! SUBROUTINE ptc_track_run and called from it
! external subroutines calculating particle interactions
use file_handler
USE madx_ptc_module , ONLY: dp, lp, lnv, &
! shorts for <double precision>, <logical>, 0D0 etc.
doublenum, & ! am temprorary double number for I/O with C-procedures
propagate, clocks, nclocks, acdipoleramping, get_length
USE madx_ptc_intstate_module, ONLY: getdebug ! new debug control by PS (from 2006.03.20)
use name_lenfi
use definition
implicit none
SAVE
PRIVATE
TYPE(INTERNAL_STATE) MYSTATE
PUBLIC :: ptc_track_run ! Subroutine inside the module
!------------------------------------------------------------------!
! Variables from input files and probably corrected by this code: !
!------------------------------------------------------------------!
INTEGER, PUBLIC :: icase_ptc ! Phase-space (4, 5, 56 or 6) in input command
INTEGER, PUBLIC :: nvariables ! actul number of variables
INTEGER, PUBLIC :: turns ! The current turn and Total number of turns
LOGICAL(lp), PUBLIC :: &
closed_orbit, & ! Switch to turn on closed orbit calculation
element_by_element, & ! element-by-element tracking (not over-one-turn one
Radiation_PTC, & ! Radiation is internally done by PTC
Radiation_model1_FZ, & ! Radiation according to model 1 (done by FZ)
Radiation_Energy_Loss, & !
Radiation_Quad, &
Space_Charge, &
beam_envelope, &
recloss, &
NORM_OUT ! IF true THEN output tables with normalizad coord,
! ELSE (default) => output tables with standard coord.
INTEGER :: Normal_Order_n0 ! The order of Normal Form ( Normal_Order_n0=1 => for Linear)
integer :: ptc_ffile ! periodicity of printing coordinates (every ffile-th turn)
! ! integer for keyword 'ffile' in 'ptc_track' command
logical(lp) :: ptc_onetable ! logical for keyword 'onetable'
! ! in 'ptc_track' command
!--------------------------------------------------!
! variables calculated or called inside this code !
!--------------------------------------------------!
INTEGER, PUBLIC :: npara ! icase_PTC is NOT actul number of variables
! ! out from PTC subr. <init> called in subr.
! ! Get_map_from_NormalForm
INTEGER, PUBLIC :: i_th_turn ! The current turn number
Real (dp), public :: &
Energy_rest_MeV, & ! the rest and total energy of particles
Energy_total_MeV, & !
deltap, & ! The relative momentum deviation for off-momentum particles (5D case)
dt ! Energy error, divided by the ref.momentum times the velocity of light
integer, PUBLIC :: &
jmax_numb_particl_at_i_th_turn, & ! "jmax" may be reduced by particle loss
j_tot_numb_starting_particles ! "j_tot" saves initial number of particles
! ! meanining: <j_total_number_starting_particles>
integer, PUBLIC,allocatable :: jmax_all_turns_numb_part(:) ! save jmax for all turns including init.
INTEGER, allocatable :: particle_ID(:) !(1:N_particle_max) ! => part_id (in TRRUN.F)
INTEGER, allocatable :: part_ID_turns(:,:) ! save particle ID for every i-th turns (i_turn,j_part)
integer :: tot_segm_one_table ! for table printing (like in trrun)
integer :: segment_one_table !
real(dp), PUBLIC :: x_coord_co(1:6) ! Closed orbit at Start of ring
! ! => x0(1:6) in ptc_track; orbit0(1:6) in TRRUN.F
real(dp), PRIVATE, allocatable :: x_co_at_all_observ(:,:)
! ! to keep closed orbit at all observations for
! ! <element_by_element> tracking
real(dp), PRIVATE, allocatable :: x_all_incl_co_at0(:,:,:) ! save x at START of the ring
! ! (1:6,0:turns,1:jmax_numb_particl_at_i_th_turn)
logical(lp), public :: ptc_track_debug=.False. !.TRUE. .False.
logical(lp) :: last_table_line_out ! = last_out => flag to avoid double entry of last line
INTEGER, ALLOCATABLE :: elem_number_at_observ(:) ! the sequent number of a ring element at
! ! the observation point
REAL(dp), allocatable :: sum_length_at_observ(:) ! the sum length for the current observ.
! ! point from the ring start
CHARACTER(name_len), ALLOCATABLE :: name_el_at_obsrv(:) ! the name of an element at observation point
! ! contrary to <character(16)> in c-code
character(1000), private :: whymsg
type(probe), private :: savedProbe ! probe at last step
! the tracking loop logic is inversed, so need to do cumbersome gymnastic with probes
! variables for keeping 6th variable within limits whet totalpath=true
! totpath_maxwavelen is value to subtract every (few) turns, it must be multiple of any RF wavelength present in the machine
REAL(dp) , private :: totpath_maxwavelen
REAL(dp) , private :: totpath_ct_subtracted
integer , private :: totpath_turncount ! counts how many turns passed from last substraction
! flag to decide if totalpath subtraction is done
logical , private :: dototpathsubtraction ! true if all the frequencies are multiples of the lowest one
REAL(dp) , private :: circumference
!real(KIND(1d0)) :: dble_num_C ! to use as a temprorary double number for I/O with C-procedures
CONTAINS
SUBROUTINE ptc_track_run(max_obs)
!USE MADX_PTC_MODULE ====================================================================!
USE madx_ptc_module, ONLY: universe, EXCEPTION, my_ring, default, index_mad, c_ !
! !
USE madx_ptc_module, ONLY: & ! "LAYOUT type (ring) => double linked list, !
FIBRE, & ! whose nodes (elements=magnets) of type FIBRE" !
C_NORMAL_FORM, & ! type for normalform !
REAL_8, & ! type for map !
damap, & ! type for diff algebra !
RADIATION ! STOCH_IN_REC, & ! type for radiation with quadrupoles in PTC !
! ======== functions ==================================================================!
USE madx_ptc_module, ONLY: & !
print, find_orbit,FIND_ORBIT_x, track,track_probe_x,UPDATE_STATES, my_state, & !
PRODUCE_APERTURE_FLAG, ANALYSE_APERTURE_FLAG, & !
kill, daprint, alloc, Get_one, & !
assignment(=), operator(+),operator(-), operator(*), operator(.sub.), & !
! Coord_MAD_to_PTC, Coord_PTC_to_MAD, & => at the end of this module !
write_closed_orbit,Convert_dp_to_dt,mytime !
!======================================================================================!
USE madx_ptc_module, ONLY: & !
c_1d_7,c_1D3,one, twopi, zero !, two !
!======================================================================================!
USE Inf_NaN_Detection !VK20070328 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
use bbfi ! integer bbd_loc,bbd_cnt,bbd_flag,bbd_pos,bbd_max;
! !uses bbd_pos parameter(bbd_max=200)
! !real(dp) bb_kick
! ! common/bbi/bbd_loc(bbd_max),bbd_cnt,bbd_flag,bbd_pos
! !common/bbr/bb_kick(2,bbd_max)
use name_lenfi ! integer name_len; parameter(name_len=24)
IMPLICIT NONE
integer, intent (IN) :: max_obs ! the maximum number of observation points >=1
! ! one point at the end (beginning)+
! ! points given in input file by the command
! ! "ptc_observe,place=mark";
integer ::flag_index_ptc_aperture, why_ptc_aperture(9)
real(dp) :: deltap0
! INTEGER, parameter :: N_particle_max=1000, & ! boundary of particle arrays
! This parameter is not used anymore, instead allocatable arrays are used in
! in the int.subr <ptc_track_ini_conditions> to define number of tracks (particles)
! INTEGER, parameter :: N_elements_max=1000 ! boundary of element arrays
! is not used, it is known in PTC as <my_ring%n>
real(dp),allocatable :: &
x_coord_incl_co (:,:) ! (1:6,1:N_particle_max) ! z(6,npart) in TRRUN.F
real (dp):: current_x_coord_incl_co(1:6)
! buffer for coordinates of the curent particle VK
real (dp):: x_input_0_by_ptc_track_command(1:6)
! buffer for input coordinates of the track command line VK
!integer :: mad8_code_current_node ! = code <in trrun.F>
! code_of_the_current_node_as_mad8
! REAL(dp) :: el_element_length ! = el
Integer, allocatable :: last_turn_of_lost_particle(:)! (1:N_particle_max) != last_turn
REAL(dp) , allocatable :: &
last_position_of_lost_particle(:), & !(1:N_particle_max), & != last_pos
last_orbit_of_lost_particle (:,:) !(1:6,1:N_particle_max) != last_orbit
! /* C routines called from Fortran and C */
real(KIND(1d0)), external :: get_value, get_variable ! external c-functions
INTEGER, external :: get_option, & ! int get_option(char*);
restart_sequ, & ! restart beamline and return number of beamline node
advance_node ! advance to the next node in expanded sequence
! ! =0 (end of range), =1 (else)
REAL(KIND(1d0)), external :: node_value !/*returns value for parameter par of current element */
EXTERNAL :: comm_para ! subroutine needed for LF95
!k character(12) char_a
! variables added by V.Kapin ! = in <trrun.F>
integer :: ptc_switch ! = switch
real(dp) :: maxaper(1:6) ! moved from int.subr.ST
integer :: k_th_coord !, j_th_particle
logical(lp) :: return_from_subr_ptc_track
REAL(dp) :: sum_length ! = sum <in trrun.F>
TYPE(fibre), POINTER :: current ! F90 pointer to the CURRENT beamline (fibre) element
TYPE(c_normal_form) :: Normal_Form_N ! n => Normal_Form_N - local name
TYPE(real_8) :: Map_Y(6) ! y => Map_Y - local name
TYPE (damap) :: A_t_map ! x_stdt=A_t_map*x_norm
TYPE (damap) :: A_t_map_rev ! x_norm=A_t_map_rev*x_norm
! ! A_t_map_rev=A_t_map^(-1)
!integer, dimension(4) :: iia ! iia(2) is the number of variables in tracking (4,..,6)
!hbu
real(dp) :: spos_current_position ! s-position =spos
REAL(dp) :: summ_ring_length ! saved ring-length calculated before tracking
! at the last element in subr. Prepare_Observation_points
integer :: nlm_current_element_number ! line position =nlm
character(name_len) el_name
!hbu
character(4) vec_names(7)
!hbu
data vec_names / 'x', 'px', 'y', 'py', 't', 'pt','s' / ! MADX
!data vec_names / 'x', 'px', 'y', 'py', 'pt', 't','s' / ! PTC has a reverse order for pt and t
logical(lp) rplot
integer mft ! debug output file
!k data char_a / ' ' /
!-------------------------------------------------------------------------
if (getdebug() > 2) then
ptc_track_debug = .true.
call kanalnummer(mft)
open(unit=mft,file='ptc_track.log')
endif
rplot = get_value('ptc_track ','rootntuple ') .ne. 0
if (rplot) then
call newrplot()
endif
! default value
return_from_subr_ptc_track=.FALSE. ! used to RETURN from this subr.
element_by_element =.FALSE. ! default is one-turn tracking
last_table_line_out = .false. ! flag to avoid double entry of last line
NORM_OUT=.False. ! IF true THEN output tables with normalizad coord,
! ELSE (default) output tables with standard coord.
Normal_Order_n0=1 !
x_coord_co(:)=zero
x_input_0_by_ptc_track_command(:)=zero
CALL Values_from_ptc_track_command ! Int. proc. in this subr.(see below in the file)
! read command line from input file :
! Example: ptc_track,icase=5, coord=0.01,0.,0.01,0.,0.,0.;
! Keywords: "icase", "turns", "closed_orbit", "deltap", "coord"
! Values for: icase, turns , closed_orbit, deltap0, coord.
! NORM_OUT=.TRUE. ! IF true THEN output tables with normalizad coord,
! ! ELSE (default) output tables with standard coord.
ALLOCATE (jmax_all_turns_numb_part(0:turns)) ! to save save jmax for all turns including init.
CALL Check_ptc_universe_and_layout ! (before_tracking) => Int.Proc.(this subr.)
if (return_from_subr_ptc_track) RETURN ! (see below in this file)
Call Call_my_state_and_update_states ! parameter "deltap" is defined now
! ! icase_ptc is changed to correct value
if (getdebug() > 1) then
Print *; Print *,' ================================================================'
Print *, ' ptc_track: The current dimensionality of the problem is icase=', icase_ptc
Print *, ' ptc_track: nvariables=', nvariables
Print *,' ================================================================'; Print *;
endif
warn_coordinate_system_changed: IF((.NOT. mytime) .AND.(nvariables.gt.4) ) THEN
CALL FORT_WARN('time=false => coord. system: {-pathlength, delta_p} ', &
'the table headers mean: PT -> delta_p, T -> pathlength')
ENDIF warn_coordinate_system_changed
call get_length(my_ring,circumference)
! initialize the closed orbit coordinates at START of the ring
! Closed_orbit_at_START:
IF(closed_orbit) then
CALL Find_Closed_Orbit ! Calculates x_coord_co(1:6)
if ( .not. check_stable) then
write(whymsg,*) 'DA got unstable during closed orbit search: PTC msg: ',messagelost(:len_trim(messagelost))
call fort_warn('ptc_track: ',whymsg(:len_trim(whymsg)))
call seterrorflag(10,"ptc_track ",whymsg);
return
endif
endif
! needed to transfrom coordinates in case closed_orbit=true AND element_by_element=false
Normal_forms: IF(closed_orbit) THEN !-----------------------!
! !
! !
call Get_map_from_NormalForm & !
(ptc_track_debug, Normal_Order_n0, x_coord_co, & !
! INTENT: IN, IN, IN !
! 1=> Normal_Order_n0 for Linear !
Map_Y, Normal_Form_N, A_t_map, A_t_map_rev) !
! OUT, OUT, OUT, OUT !
! !
END IF Normal_forms !----------------------------------------!
ptc_switch = 1 ! set up the "switch"=1 as for "RUN" option
! ! In subr. trrun.F, it is input parameter equal to
! ! 1 (RUN); 2 (DYNAP fastune); 3 (DYNAP aperture)
Call ffile_and_segm_for_switch !Int. proc. in this subr.(see below)
CALL Prepare_Observation_points (max_obs, x_coord_co)
! getting parameters at observation points and
! finding the closed orbits at observations for element_by_element tracking
call ptc_track_ini_conditions
call ptc_track_ini_modulation
Call Set_initial_particle_ID ! Int.subr. below in this subr.
ALLOCATE ( last_turn_of_lost_particle(1:j_tot_numb_starting_particles), &
last_position_of_lost_particle(1:j_tot_numb_starting_particles), &
last_orbit_of_lost_particle (1:6,1:j_tot_numb_starting_particles))
last_turn_of_lost_particle=zero
last_position_of_lost_particle=zero
last_orbit_of_lost_particle=zero
Call Particle_Interactions_Ini ! Int.subr. - intialize data for int.subr. Particle_Interactions
Call Init_info_for_tables ! Int.subr. below in this subr.
Call Start_Coord_to_TrackSum_Table
! The name of the table "tracksum".
! At debug stage it is printed out with the command <write> in input file,
! e.g. "write,table=tracksumm, file=track_summary.txt;"
Call Enter_0st_turn_in_tables
! Subroutines "tt_putone" and "tt_puttab" from the file "trrun.F" are used.
! (renamed as "tt_putone_coord" & "tt_puttab_coord"
! The name of the tables are "trackone" and "track.obs$$$$.p$$$$"
! At debug stage it is printed out with the command <write> in input file,
! e.g. ??????
! Call Initialize_kinematics_and_orbit ! removed subr.
c_%watch_user=.true.
debug_print_1: IF (ptc_track_debug) then
print *; print*,'Start loop over turns: '
Print *, ' make tracking with element_by_element=', element_by_element
print *,'The line <MY_RING> consists of <MY_RING%n>=', MY_RING%n, ' nodes'
print *, "Number of observation points max_obs =", max_obs
END IF debug_print_1
if (rplot) call rplotstartcoord()
totpath_maxwavelen = 0.0
totpath_ct_subtracted = 0.0
totpath_turncount = 0
dototpathsubtraction = .false.
if ( (nvariables .gt. 5) .and. (MYSTATE%TOTALPATH .gt. 0) ) then ! !
! must be after ptc_track_ini_modulation, clocks must be already initialized
call calculate_nrfwavelen()
endif
loop_over_turns: do i_th_turn=1,turns ! ================loop over turn =======!
debug_print_2: IF (ptc_track_debug) then !
print *; print*, 'start ',i_th_turn, '-th turn ' !
end if debug_print_2 ! !
!
call acdipoleramping(i_th_turn)
!
track_over_total_ring: if ( .NOT.element_by_element) then !++++++++++++! t
! ! u
call One_turn_track_with_PTC ! r
! ! n
else ! +++++track ring element-by-element +++++++++++++++++++++++++++++! s
! ! !
call track_beam_elementwise_with_PTC ! !
! ! !
!if (ptc_track_debug) pause ! STOP ! !
! ! !
end if track_over_total_ring ! ++++++++++++++++++++++++++++++++++++++++! !
! !
switch_1: if (ptc_switch .eq. 1) then ! switch=1 => for RUN command ===! l
! ! o
debug_print_3: if (ptc_track_debug) then ! o
Print *; Print *, ' ptc_switch= ',ptc_switch ! p
Print *, ' Call Write_tables_after_total_turn' ! !
end if debug_print_3 ! !
! ! o
call Write_tables_after_total_turn ! v
! ! e
else ! === ENDIF (switch .ne. 1) => DYNAP (fastune,aperture)===========! r
! ! !
debug_print_4: if (ptc_track_debug) then !------------------! ! !
Print *; Print *, ' ptc_switch= ',ptc_switch ! ! !
Print *, ' Call Save_Coord_for_DYNAP_after_total_turn' ! ! t
end if debug_print_4 !--------------------------------------! ! u
! ! r
! ? Call Save_Coord_for_DYNAP_after_total_turn ! n
! ! !
END IF switch_1!=== END if (switch) ==== RUN or DYNAP =================! !
!
IF (jmax_numb_particl_at_i_th_turn.eq.0) EXIT Loop_over_turns !
! all particles are lost !
!
if ( (nvariables .gt. 5) .and. (MYSTATE%TOTALPATH .gt. 0) ) then ! !
call subtract_nrfwavelen(i_th_turn)
endif
end do Loop_over_turns !===========loop over turn ============================!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
!!
!! WRITE DOWN
!!
Call Final_Coord_to_tables ! Complete all tables by one subroutine:
!'trackone' filled before by tt_puttab_coord,
!'track.obs$$$$.p$$$$' by tt_putone_coord
!summary table by 'tracksumm'
Output_observ_with_PTC: IF(closed_orbit .AND. &
(.NOT.element_by_element).AND.(.NOT. Radiation_PTC)) THEN !-!
debug_print_5: if (ptc_track_debug) then !----------------! !
Print *, 'element_by_element=', element_by_element, & ! !
' Radiation_PTC=',Radiation_PTC ! !
Print *, ' Call Observation_with_PTC' ! !
end if debug_print_5 !------------------------------------! !
Call Observation_with_PTC(max_obs, x_coord_co, Map_Y) !
ENDIF Output_observ_with_PTC !---------------------------------------------------------!
if (ptc_track_debug) then
Print *, 'Come to : <! Calculate beam envelope with PTC>'
endif
if (rplot) call rplotfinish()
Beam_envelope_with_PTC: IF (beam_envelope) THEN
call fort_warn('ptc_track: ',' Calculation of Equilibrium emittance was moved to ptc_twiss')
endif Beam_envelope_with_PTC
! Beam_envelope_with_PTC: IF (beam_envelope) THEN !###############################!
! Radiat_PTC: IF ( Radiation_PTC) THEN !====================================! !
! icase_6: IF (icase_ptc .EQ. 6 .AND.closed_orbit) THEN !--! ! !
! Call beam_enevelope_with_PTC ! ! !
! ELSE !---------------------------------------------------! ! !
! Print *, ' Warning !: Option BEAM_ENVELOPE', & ! ! !
! ' require option ICASE=6', & ! ! !
! ' and option CLOSED_ORBIT ' ! ! !
! Print *, ' The code ignores BEAM_ENVELOPE option ' ! ! !
! ENDIF icase_6 !------------------------------------------! ! !
! ELSE !====================================================================! !
! Print *, ' Warning !!!: Option BEAM_ENVELOPE require option Radiation' ! !
! Print *, ' The code ignores BEAM_ENVELOPE option ' ! !
! ENDIF Radiat_PTC !========================================================! !
! ENDIF Beam_envelope_with_PTC !##################################################!
c_%watch_user=.false.
Call DeAllocate_local_and_PTC_arrays ! Internal subroutine
!=============================================================================
CONTAINS ! Internal procedures for the HOST subr. "ptc_track"
! (a new fortran-90/95 feature):
! 1) Int.p. is entirely contained within the HOST.
! 2) It is compiled together with the HOST;
! 3) It can only be invoked from the host program unit;
!
! No other procedure within the program can access it !!!
!
! 4) Int.p. may not CONTAIN futher subprograms;
! 5) Int.p. automatically has a access to all the host entiteles;
! 6) Int.p. is able to call other int.procedures of the same HOST ;
! 7) the HOST does not have access to the local entities (declared
! inside int.p.) of any int.subroutine that it contains.
! 8) The absense of IMPLICIT NONE within int.proc, because one in the
! host applies to the intern. proc. as well
! Internal procedures are used for this ptc_track_run in order to
! use modified subroutines imported from trrun.F with the same names
!=============================================================================
SUBROUTINE Check_ptc_universe_and_layout ! before_tracking => Int. proc. (f95)
! USE madx_ptc_module, ONLY: universe, index_mad
implicit none
! It checks that the following commands has been performed
! before calling subr. ptc_track
! If not, then return_from_subr_ptc_track =>.TRUE.
! (accessible from the HOST subr.)
! 1) ptc_create_universe: Needed to set-up PTC
if (ptc_track_debug) then
print *
print *, '<subr. Check_ptc_universe_and_layout (before_tracking)>:'
print *, 'PTC Universe universe=', universe
print *, 'PTC Layout index=', index_mad
end if
if(universe.le.0.or.EXCEPTION.ne.0) then
call fort_warn('return from ptc_track: ',' no universe created')
! return
return_from_subr_ptc_track=.TRUE.
endif
! 2) ptc_create_layout: creates PTC layout and fills it
! with current MAD-X sequence
if(index_mad.le.0.or.EXCEPTION.ne.0) then
call fort_warn('return from ptc_track: ',' no layout created')
! return
return_from_subr_ptc_track=.TRUE.
endif
END SUBROUTINE Check_ptc_universe_and_layout ! before_tracking => Int. proc. (f95)
!=============================================================================
SUBROUTINE Values_from_ptc_track_command ! Internal procedure (f95)
USE madx_ptc_intstate_module, ONLY: getdebug ! new debug control by PS (from 2006.03.20)
implicit none
! local variables
character(4) text
character(12) tol_a, char_a
integer :: nint,ndble, nchar, int_arr(1),char_l
data tol_a,char_a / 'maxaper ', ' ' /
real(dp) :: tmpr
!defaults
int_arr(:)=1; char_l=1
! Values of "ptc_track" command: (PTC thick lens tracking module)
! DEFAULT
! icase: Phase-space (4, 5 or 6) (4)
! turns: Number of turns (1)
! closed_orbit: Switch to turn on closed orbit calculation (false)
! deltap: The relative momentum deviation for
! off-momentum particles (5D case) ???? (4)
!
! madxdict.h:408:"ptc_track: ptc_track none 0 0 "
! "ptc_track: ptc_track none 0 0 "
! "icase = [i, 4], "
! "turns = [i, 1], "
! "closed_orbit = [l, false, true], "
! "deltap = [r, 0], "
! ! exluded: "coord = [r, {0,0,0,0,0,0}]; "
!
! added by VKapin
!
! "element_by_element = [l, false, true], " => element_by_element
! "radiation = [l, false, true], " => radiation_PTC
! "radiation_model1 = [l, false, true], " => radiation_model1_FZ
! "radiation_energy_loss = [l, false, true], "
! "radiation_quad = [l, false, true], "
! "space_charge = [l, false, true], "
! "beam_envelope = [l, false, true], "
!
! "dump = [l, false, true], " - c-procedure for input
! "onetable = [l, false, true], "
! "file = [s, track, track], "
! "extension= [s, none, none], "
! "maxaper= [r, {0.1, 0.01, 0.1, 0.01, 1., 0.1}], "
! "ffile = [i, 1], "
! "norm_out = [l, false, true], "
! "norm_no = [i, 1], "
! "debug = [l, false, true]; " - only for developer (not for User)
icase_ptc = get_value('ptc_track ','icase ')
if(icase_ptc.eq.56) nvariables=6
if(icase_ptc.ne.4.and.icase_ptc.ne.5.and.icase_ptc.ne.6.and.icase_ptc.ne.56) then
write(text, '(i4)') icase_ptc
call aafail('Values_from_ptc_track_command: ',' ICASE not 4, 5, 6 or 56 found: ' // text)
endif
turns = get_value('ptc_track ','turns ')
closed_orbit = get_value('ptc_track ','closed_orbit ') .ne. 0
deltap0 = get_value('ptc_track ','deltap ')
element_by_element = get_value('ptc_track ', 'element_by_element ') .ne. 0
IF(max_obs.gt.1.AND.(.NOT.CLOSED_ORBIT).AND.(.NOT. ELEMENT_BY_ELEMENT)) THEN
Print *, ' '
Print *, '===================================================================='
Print *,'To perform tracking with observation points the option'
Print *,'ELEMENT_BY_ELEMENT must be ON, if the option CLOSED_ORBIT is OFF'
call fort_warn(' ELEMENT_BY_ELEMENT',' has been switched ON by the code')
Print *, '===================================================================='
Print *, ' '
ELEMENT_BY_ELEMENT=.TRUE.
ENDIF
Radiation_PTC = get_value('ptc_track ','radiation ') .ne. 0
radiation_model1_FZ = get_value('ptc_track ','radiation_model1 ') .ne. 0
IF ( Radiation_PTC .AND. radiation_model1_FZ ) THEN
radiation_model1_FZ=.FALSE.
Print *, ' '
Print *, '===================================================================='
Print *,'Both options RADIATION and RADIATION_MODEL1 are entered as TRUE !? '
Print *,'RADIATION has a higher priority. RADIATION_MODEL1 set to be OFF.'
call fort_warn(' RADIATION_MODEL1',' has been switched OFF by the code')
Print *, '===================================================================='
Print *, ' '
ENDIF
Radiation_Energy_Loss = get_value('ptc_track ','radiation_energy_loss ') .ne. 0
Radiation_Quad = get_value('ptc_track ','radiation_quad ') .ne. 0
Space_Charge = get_value('ptc_track ','space_charge ') .ne. 0
recloss = get_value('ptc_track ','recloss ') .ne. 0
IF (radiation_model1_FZ .OR. Space_Charge ) THEN
IF(.NOT.element_by_element) THEN
element_by_element=.TRUE.
Print *, ' '
Print *, '===================================================================='
call fort_warn(' ELEMENT_BY_ELEMENT',' has been switched ON by the code')
Print *,' Only element-by-element tracking can be performed with '
Print *,' the options RADIATION_MODEL1 or SPACE_CHARGE.'
Print *, '===================================================================='
Print *, ' '
ENDIF
ENDIF
beam_envelope = get_value('ptc_track ','beam_envelope ') .ne. 0
! 'dump ' is done in c-code
ptc_onetable = get_value('ptc_track ','onetable ') .ne. 0
! 'file ' and 'extension ' are done in c-code
ptc_ffile = get_value('ptc_track ', 'ffile ')
norm_out = get_value('ptc_track ','norm_out ') .ne. 0
norm_out_wrong: IF (norm_out .AND. (.NOT.closed_orbit) ) THEN
Print *, '!!! Warning: The input options norm_out=', norm_out, ' and closed_orbit=', &
closed_orbit, ' are uncompatible !!!'
norm_out=.FALSE.
Print *, ' The input option is set to norm_out=', norm_out, ' and tracking is continued'
END IF norm_out_wrong
Normal_Order_n0 = get_value('ptc_track ','norm_no ')
!ptc_track_debug = get_value('ptc_track ','debug ') .ne. 0 ! before 2006.03.20
if (getdebug()>3) ptc_track_debug=.true. ! ptc_track_debug=.T., if debuglevel.ge.3 ,i.e.,
! in the madx input: ptc_setswitch, debuglevel=4
debug_print_1: if (ptc_track_debug) then
print *,' '
print*, '====================================================='
print *, 'Start subr. <ptc_track(max_obs)> in a debugging regime '
print *, 'Int.Subr <Values_from_ptc_track_command>:'
print *, 'ptc_track_debug=', ptc_track_debug
end if debug_print_1
! get_value =>
! file "madxd.h": getting values from INPUT-user file !
! double get_value(char*, char*) !
!/* C routines called from Fortran and C */ !
!--- 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)
!swap 5 and 6
tmpr = maxaper(5)
maxaper(5) = maxaper(6)
maxaper(6) = tmpr
debug_print_2: if (ptc_track_debug) then
print *; print *, "<subr. Values_from_ptc_track_command>:"
print *, "icase_ptc=get_value('ptc_track ','icase ')= ", icase_ptc
print *, "turns=get_value('ptc_track ','turns ')= ", turns
print *, "closed_orbit = ", closed_orbit
print *, &
"deltap0 = get_value('ptc_track ','deltap ')= ", deltap0
print *, " Radiation_PTC =", Radiation_PTC
print *, " radiation_model1_FZ =", radiation_model1_FZ
print *, " Radiation_Energy_Loss =", Radiation_Energy_Loss
print *, " Radiation_Quad =", Radiation_Quad
print *, " Space_Charge =", Space_Charge
print *, " recloss =", recloss
print *, " maxaper =", maxaper
print *, " ptc_onetable =", ptc_onetable
print *, " ptc_ffile =", ptc_ffile
print *, " norm_out =", norm_out
print *, " Normal_Order_n0 =", Normal_Order_n0
print *, " ptc_track_debug =", ptc_track_debug
end if debug_print_2
! getting particle coordinates with 'coord' keyword
debug_print_3: if (ptc_track_debug) then
print *; print*, '----------------------------------', &
'----------------------------------'
print *, "before call comm_para('coord ',", &
"nint,ndble,nchar,int_arr,x,char_a,char_l)"
print*, "x=", x_input_0_by_ptc_track_command, &
"IS NOT DEFINED YET"; print *
end if debug_print_3
call comm_para('coord ',nint,ndble,nchar,int_arr, &
x_input_0_by_ptc_track_command,char_a,char_l)
! returns the value for command parameter "name"
debug_print_4: if (ptc_track_debug) then
print *, "after call comm_para('coord ',", &
"nint,ndble,nchar,int_arr,x,char_a,char_l)"
print*, "nint =", nint, "not used later"
print*, "ndble=", ndble, "not used later"
print*, "nchar=", nchar, "not used later"
print*, "int_arr=", int_arr, "not used later"
print*, "x=", x_input_0_by_ptc_track_command, "MIGHT BE USED LATER"
print*, "char_a=", char_a, "not used later"
print*, "char_l=", char_l, "not used later"; print *
end if debug_print_4
! The KeyWord 'coord ' from the "ptc_track"command is not used here
! any more..., instead of it the command "ptc_start" is used
END SUBROUTINE Values_from_ptc_track_command
!=============================================================================
SUBROUTINE Call_my_state_and_update_states
USE madx_ptc_module, ONLY: radiation0
implicit none
character(4) text
debug_print_1: if (ptc_track_debug) then
print *,"before <call my_state(icase,deltap,deltap0)>", &
"with parameters: "
print *, 'icase_ptc=', icase_ptc, 'deltap=', deltap
print *, 'deltap0=', deltap0 ; print *
print*, 'my_state printing:'
end if debug_print_1
call my_state(icase_ptc,deltap,deltap0, silent=my_true)
nvariables = icase_ptc
!icase 56:
if (nvariables .gt.6 ) nvariables = 6
IF (Radiation_PTC) then
DEFAULT = DEFAULT + RADIATION0
else
DEFAULT = DEFAULT - RADIATION0 ! switch off RADIATION even if true in ptc_setswitch
endif
if(getdebug() > 1) then
Print *, ' Radiation_PTC = ', Radiation_PTC
endif
debug_print_2: if (ptc_track_debug) then
print *, &
"after call my_state(icase,deltap,deltap0)"
print *, 'icase_ptc=',icase_ptc, 'deltap=', deltap
print *, 'deltap0=', deltap0
print *; print*, '----------------------------------'
print *, "before CALL UPDATE_STATES"
end if debug_print_2
CALL UPDATE_STATES
MYSTATE=DEFAULT
if (getdebug()>0) then
call print(MYSTATE,6)
endif
if ( MYSTATE%TOTALPATH .gt. 0 ) then
print *, "********************************************************************"
print *, "MAXAPER check of T variable not to be done because TOTALPATH is used"
print *, "********************************************************************"
endif
END SUBROUTINE Call_my_state_and_update_states
!=============================================================================
!=============================================================================
SUBROUTINE Find_Closed_Orbit
! USE madx_ptc_module, ONLY: dp, zero, find_orbit, my_ring,default
implicit none
logical isstochastic
!
!====================================================================!
! initialize the closed orbit coordinates !
x_coord_co(1)=get_value('ptc_track ','x ')
x_coord_co(2)=get_value('ptc_track ','px ')
x_coord_co(3)=get_value('ptc_track ','y ')
x_coord_co(4)=get_value('ptc_track ','py ')
x_coord_co(6)=-get_value('ptc_track ','t ') ! swap of t sign
x_coord_co(5)=get_value('ptc_track ','pt ')
if (ptc_track_debug) THEN !---------------------------! !
print *, "Start point for closed orbit search: " ! !
CALL write_closed_orbit(nvariables,x_coord_co) ! !
end if !----------------------------------------------! !
! !
if(nvariables.ge.5) THEN !------------------------! !
if(mytime) then !----------------------! ! !
call Convert_dp_to_dt (deltap, dt) ! ! !
else
print*, 'Converted deltap ', deltap, ' to ', dt ! ! !
dt=deltap ! ! !
endif !--------------------------------! ! !
x_coord_co(5)=dt ! !
ENDIF !------------------------------------------! !
! !
if (ptc_track_debug) then !------------------------! !
print *, " if(icase.eq.5) ,x_coord_co(5)=deltap" ! !
print *, " ,x_coord_co(5),deltap=", & ! !
x_coord_co(5),deltap ! !
global_verbose = .true.
end if !--------------------------------------------! !
! !
if(closed_orbit) then !------------------------------------! !
! temporarly disbale stochastic, otherwise the search is doomed to fail
isstochastic = MYSTATE%stochastic
MYSTATE%stochastic = .false.
CALL FIND_ORBIT_x(my_ring,x_coord_co,MYSTATE,c_1d_7,fibre1=1) !
! call find_orbit(my_ring,x_coord_co,1,MYSTATE,c_1d_7) !
if ( .not. check_stable) then
! the routine which calls this function should also
! check the flag and report the eventual error
return
endif
MYSTATE%stochastic = isstochastic
print*,"===== ptc_track ============================" ! !
CALL write_closed_orbit(nvariables,x_coord_co) ! !
print*,"============================================" ! !
endif !---------------------------------------------------! !
! !
if (ptc_track_debug) then
global_verbose = .false.
print*,"After closed_orbit"; print *; !
endif !
! !
!END closed_orbit which is logically (.not.ONEPASS) !
!====================================================================!
END SUBROUTINE Find_Closed_Orbit
!=============================================================================
!=============================================================================
SUBROUTINE DeAllocate_local_and_PTC_arrays
! USE madx_ptc_module, ONLY: kill
implicit none
if(closed_orbit) then
call kill (Map_Y)
call kill (Normal_Form_N)
CALL kill (A_t_map)
CALL kill (A_t_map_rev)
endif
Deallocate (particle_ID) ! OK
Deallocate (jmax_all_turns_numb_part) ! OK
Deallocate (x_coord_incl_co) ! OK
Deallocate (last_turn_of_lost_particle) !OK
Deallocate (last_position_of_lost_particle) !OK
Deallocate (last_orbit_of_lost_particle) !OK
IF (.NOT. element_by_element) THEN
Deallocate ( x_all_incl_co_at0, part_ID_turns)
ELSE
Deallocate (x_co_at_all_observ)
END IF
Deallocate (elem_number_at_observ, sum_length_at_observ, name_el_at_obsrv)
END SUBROUTINE DeAllocate_local_and_PTC_arrays
!=============================================================================
!=============================================================================
SUBROUTINE rplotstartcoord()
implicit none
integer j
DO j=1, jmax_numb_particl_at_i_th_turn
!x_coord_incl_co(:,j)
call plottrack(j, 1, i_th_turn, &
x_coord_incl_co(1,j), &
x_coord_incl_co(2,j), &
x_coord_incl_co(3,j), &
x_coord_incl_co(4,j), &
x_coord_incl_co(5,j), &
sqrt(MY_RING%end%mag%p%p0c**2 + (MY_RING%end%mass)**2), &
x_coord_incl_co(6,j))
enddo
END SUBROUTINE rplotstartcoord
!=============================================================================
! must be called after ptc_track_ini_modulation, clocks must be already initialized
SUBROUTINE calculate_nrfwavelen()
USE madx_keywords, only: kind4, kind21
implicit none
integer, parameter :: maxnwavelens = 100
real(dp) :: wavelens(maxnwavelens)
real(dp) :: nominators(maxnwavelens)
real(dp) :: denominators(maxnwavelens)
real(dp) :: wavelen, maxwavelen
integer i,j, nwavelens
type(fibre), pointer:: p
logical :: found
integer(kind=8) :: d,n
real(dp) :: f
real(dp) :: MASS_GeV,ENERGY,KINETIC,BRHO,BETA0,P0C,gamma0I,gambet
Call GET_ONE(MASS_GeV,ENERGY,KINETIC,BRHO,BETA0,P0C,gamma0I,gambet) !
nwavelens = 0
p=>my_ring%start
! Search form all frequencies
! 1. RF cavities (including RF multipoles) and TW cavities
DO j=1, my_ring%n
!print*, "Element ", p%mag%name, " kind ", p%mag%kind
if( (p%mag%kind /= kind21) .and. (p%mag%kind/=kind4) ) then
!print*, "Not a cavity"
p=>p%next
cycle
endif
if(p%mag%freq==zero) then
!print*, "Frequency is zero"
p=>p%next
cycle
endif
!print*, "Getting wavelength"