-
Notifications
You must be signed in to change notification settings - Fork 39
/
mad_6track.c
executable file
·3493 lines (3255 loc) · 113 KB
/
mad_6track.c
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
#include "madx.h"
#include "mad_6track_name_mangler.h"
/*---------------------------------------------------------------------*
* *
* CERN *
* *
* European Organization for Nuclear Research *
* *
* Program name: c6t: MAD-Sixtrack Format Converter *
* *
* Author and contact: Hans GROTE *
* SL Division *
* CERN *
* CH-1211 GENEVA 23 *
* SWITZERLAND *
* Tel. [041] (022) 767 49 61 *
* Hans.Grote@cern.ch *
* *
* Converted to MAD-X by Mark HAYES *
* Followed up by Frank Schmidt *
* *
* Copyright CERN, Geneva 2000 - Copyright and any other *
* appropriate legal protection of this computer program and *
* associated documentation reserved in all countries of the *
* world. *
* *
* Organizations collaborating with CERN may receive this program *
* and documentation freely and without charge. *
* *
* CERN undertakes no obligation for the maintenance of this *
* program, nor responsibility for its correctness, and accepts *
* no liability whatsoever resulting from its use. *
* *
* Program and documentation are provided solely for the use of *
* the organization to which they are distributed. *
* *
* This program may not be copied or otherwise distributed *
* without permission. This message must be retained on this and *
* any other authorized copies. *
* *
* The material cannot be sold. CERN should be given credit in *
* all references. *
* *
*---------------------------------------------------------------------*/
/* 17.08.2004 - FS fix print-out of special f34 file needed as input file
for the sodd program. */
/* 15/03/2004 - FS fixing faulty variable passing to "create_aperture" */
/* 01/07/2003 - FS added the "arbitrary matrix" element */
/* 21/03/2003 - FS fixed segmentation fault which was due to a faulty
free-ing of object that had already been freed before */
/* 10/07/2002 - MH fixed missing mcdo bug, caused by recursion up
element tree to unexpanded double_array */
/* 20/06/2002 - MH fixed double declarations and memory leaks because the
original c6t was only ment to be run once - but not this one! */
/* 19/06/2002 - MH found last 'bug' in rhic sequence... due to micron
length quadrupole and rounding errors caused by them */
/* 29/04/2002 - MH&HG made it copy all the collimators across */
/* 23/04/2002 - MH changed ref_delta so that =0 for quads and higher
this directly effects fort.3 */
/* 14/04/2002 - MH changed calloc to mycalloc for HG error checking */
/* extract Sixtrack input files from DOOM */
/* question 1: change BEAM defaults to those of MAD8 */
/* question 2: att_lcavity ? */
/* #define _call_tree_ */
/* JMJ, 7/11/2002 commenting out the following
to see if it helps for Visual Fortran ....
already in madxn.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
and I moved
#include "c6t.h"
to madxn.c
*/
#define BASE_TYPES 100 /* maximum no. of element types allowed */
#define EL_COUNT 100 /* initial length of element type list */
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502
#endif
// types
/* MADX name and internal codes :
circle=1=CR, rectangle=2=RE, ellipse=3=EL, rectcircle=lhcscreen=4=RC,
rectellipse=5=RL, racetrack=6=RT, octagon=7=OC */
struct aper_struct {
int apply;
char name[255];
char style[255];
double value[8]; // 2015-Jul-31 ghislain: adding more parameters to aperture
};
struct c6t_element
{
char name[48], /* name including occurrence count if > 1 */
org_name[48], /* original name */
base_name[48]; /* basic type */
struct c6t_element* previous;
struct c6t_element* next;
struct c6t_element* equiv; /* pointer to first identical element */
int flag; /* treatment flag_1 or _2 or _3 (see type_info) */
int force; /* magnet flag (flag_5) (see type_info) */
int c_drift; /* treatment flag_4 (see type_info) */
int split; /* treatment flag_6 (see type_info) */
int n_values; /* length of value */
int w_flag; /* 0 if not, 1 if written on fort.2 */
int out_1; /* output parameter 1, fort.2 */
int na_err; /* current no. of alignment errors */
int nf_err; /* current no. of field errors */
int nc_pos; /* component count, only multipoles */
int npole_sign; /* sign inversion flag for even (created) npoles */
int keep_in; /* if not 0, do not yank */
int mult_order; /* error reference comp., only multipoles */
int f3_flag; /* for multipole def. on fc.3 */
int occ_cnt; /* occurrence count */
int twtab_row; /* row number in twiss table */
double position; /* s position in sequence [m] */
double rad_length; /* radiation length of multipoles [m] */
double ref_radius; /* reference radius for multipole errors [m] */
double ref_delta; /* reference delta for multipole errors */
double out_2; /* output parameter 2, fort.2 */
double out_3; /* output parameter 3, fort.2 */
double out_4; /* output parameter 4, fort.2 */
double out_5; /* output parameter 5, fort.2 */
double out_6; /* output parameter 6, fort.2 */
double out_7; /* output parameter 7, fort.2 */
double* value; /* element strength etc. values */
struct object* p_al_err; /* pointer to alignment error object */
struct object* p_fd_err; /* pointer to field error object */
struct object* p_ph_err; /* pointer to field phase error array AL: */
double rfm_freq; /* frequency of the rf-multipole fields AL: */
int tilt_err; /* allow write_f8 to dump tilt as well */
int do_not_free; /* avoid free crash */
};
struct c6t_el_list /* contains list of element pointers */
{
int max, /* max. pointer array size */
curr; /* current occupation */
char base_name[48];
struct c6t_element** elem; /* element pointer list */
};
struct block
{
char name[48];
double length;
int flag; /* if 0 take element, else block */
struct c6t_element* first;
struct c6t_element* last;
struct block* previous;
struct block* next;
struct block* equiv;
struct c6t_el_list* elements;
};
struct li_list /* contains list of list pointers */
{
int curr; /* current occupation */
struct c6t_el_list* member[BASE_TYPES]; /* list pointer list */
};
struct type_info /* info about types */
{
char name[48]; /* base_type */
/* flag meanings - 0: skip, 1: linear, >1: non-linear,
2: convert to multipole (temporarily), 3: cavity
4: make 2 if in explicit list, else skip
5: only split */
int flag_1, /* for length = 0 */
flag_2, /* for length > 0, normal */
flag_3, /* for length > 0, skew */
flag_4, /* if > 0: make drift, print warning when encountered */
flag_5, /* if > 0: magnet (for k0n * l) */
flag_6; /* if length > 0: 0 = no split
1 = split; if flag_2(_3) = 1: two identical + zero m.
if flag_2(_3) = 2: two drift + full m. */
};
/* (some) constants and structure definitions for DOOM */
/*
element definition: (F: Fortran, C: C/C++)
d.p. array
word / e_type = 1 = 2 = 3
C F
0 1 l [m] l [m] l [m]
1 2 rhoinv [1/m] volt [MV] kick
2 3 e1 ex [MV/m] .
3 4 e2 ey [MV/m] .
4 5 h1 freq [MHz] .
5 6 h2 lag [2 Pi] .
6 7 tilt tilt kick
7 8 ks betrf C:7-42 F:8-43: rm
8 9 hgap [m] pg {MW] C:43-258 F:44-259: tm
9 10 fint [Tm] shunt [MOhm/m]
10 11 angle = K_0*l tfill [micro sec]
11 12 lrad harmon
12 13 k0 or k0*l (l=0) xsize (coll.) or xma (beam-beam) or x (mon.)
13 14 k0s or k0s*l ysize (coll.) or yma (beam_beam) or y (mon.)
14 15 k1 or k1*l (l=0) sigx
15 16 k1s or k1s*l sigy
16 17 k2 or k2*l charge
17 18 k2s etc. npart (# particles in opposite beam)
int array: as d.p. array, containing expression flag:
ex_flag = 1 value
ex_flag > 1 expression
name array:as d.p. array, pointers to parameter names if ex_flag > 0
*/
/*
parameter definition:
int array:
1 exflag 1 if value, > 1 if expression
d.p. array:
1 value (always)
char array:
string expression as read if exflag > 1
*/
struct object
{
char key[48]; /* d.b. key */
/* The order of the first 11 variables below is FIXED */
int ma_time, /* start of control part =
major time at creation or last modification */
mi_time, /* minor time at creation or last modification */
l_int, /* length of integer array */
l_dble, /* length of double array */
l_char, /* length of string */
l_obj, /* length of object pointer array */
c_int, /* occupation of integer array */
c_dble, /* occupation of double array */
c_char, /* occupation of string */
c_obj; /* occupation of object and names pointer array */
char par_name[24], /* parent name */
base_name[24], /* basic type name (e.g. QUADRUPOLE, DRIFT,..) */
obj_type[24]; /* object type such as ELEMENT, TWISS_SUMMARY etc. */
int* a_int; /* integer array */
double* a_dble; /* d.p. array */
char* a_char; /* string */
struct object* parent; /* pointer to parent object */
struct object** p_obj; /* object pointer array */
char** names; /* name pointers into a_char */
};
/* already defined as 42 in fulll.h */
/*#define FIELD_MAX 40*/ /* field error array length */
#define KEY_LENGTH 48 /* from DOOM */
#define MM_KEEP 2 /* no. of element name starts to keep */
#define MULTI_MAX 24 /* element array length for multipoles */
#define NT34 5 /* no. of element types in special fort.34 */
#define LINES_MAX 3 /* structure output line max. names */
#define SEQ_DUMP_LEVEL 0 /* chooses amount of dumped output */
static void add_c6t_drifts(void);
static void add_split_list(struct c6t_element*);
static void add_to_ellist(struct c6t_element*);
static void app_factor(double, double*, int);
// static void arr_print(double*, int); // not used
static void assign_att(void);
static void att_aperture(struct c6t_element*);
static void att_beambeam(struct c6t_element*);
static void att_colli(struct c6t_element*);
static void att_decapole(struct c6t_element*);
static void att_drift(struct c6t_element*);
static int f34_values(struct c6t_element*, int*, double*);
static void att_hkicker(struct c6t_element*);
static void att_kicker(struct c6t_element*);
static void att_lcavity(struct c6t_element*);
static void att_marker(struct c6t_element*);
static void att_matrix(struct c6t_element*);
static void att_multipole(struct c6t_element*);
static void att_octupole(struct c6t_element*);
static void att_quadrupole(struct c6t_element*);
static void att_rbend(struct c6t_element*);
static void att_rfcavity(struct c6t_element*);
static void att_crabcavity(struct c6t_element*);
static void att_dipedge(struct c6t_element*);
static void att_solenoid(struct c6t_element*);
static void att_hacdipole(struct c6t_element*);
static void att_vacdipole(struct c6t_element*);
static void att_sbend(struct c6t_element*);
static void att_sextupole(struct c6t_element*);
static void att_vkicker(struct c6t_element*);
static void att_rfmultipole(struct c6t_element*);
static void att_undefined(struct c6t_element*);
static void clean_c6t_element(struct c6t_element*);
static struct c6t_element* create_aperture(const char* ,const char* ,double, double, double, double, double, double, double,
struct double_array*);
static void concat_drifts(void);
static void conv_elem(void);
static void c6t_finish(void);
static void c6t_init(void);
static struct c6t_element* convert_madx_to_c6t(struct node*);
// static void dump_c6t_element(struct c6t_element*); // not used
// static void dump_c6t_sequ(int); // not used
// static void dump_types(int); // not used
static void equiv_elem(void);
static struct block* get_block_equiv(struct block*);
static void get_args(struct in_cmd*);
static void get_error_refs(struct c6t_element*);
static int get_flag(struct c6t_element*, struct type_info*);
// static struct c6t_element* get_from_ellist(char*, char*); // not used
static void get_multi_refs(void);
static int get_next_name(char*, char);
// static void gnu_file(struct c6t_element*); // not used
static void grow_ellist(struct c6t_el_list*);
static int ident_el(struct c6t_element*, struct c6t_element*);
static int ident_zero(struct c6t_element*);
static int in_keep_list(struct c6t_element*);
static void invert_normal(int, double*);
// static void invert_skew(int, double*); // not used
static void link_behind(struct c6t_element*, struct c6t_element*);
static void link_c6t_in_front(struct c6t_element*, struct c6t_element*);
static struct c6t_element* make_c6t_element(struct node*);
static struct object* make_obj(const char*, int, int, int, int);
static void make_multipole(struct c6t_element*);
static void mod_errors(void);
static void mod_lcavity(struct c6t_element*);
static void mod_multipole(struct c6t_element*);
static void mod_octupole(struct c6t_element*);
static void mod_quadrupole(struct c6t_element*);
static void mod_rbend(struct c6t_element*);
static void mod_rfcavity(struct c6t_element*);
static void mod_crabcavity(struct c6t_element*);
// static void mod_dipedge(struct c6t_element*); // not defined
// static void mod_solenoid(struct c6t_element*); // not defined
// static void mod_hacdipole(struct c6t_element*); // not defined
// static void mod_vacdipole(struct c6t_element*); // not defined
static void mod_sextupole(struct c6t_element*);
static void multi_loop(void);
static struct c6t_element* new_c6t_element(int, const char*, const char*);
static struct block* new_block(void);
static void post_multipoles(void);
static double power_of(double, int);
static void pre_multipole(struct c6t_element*);
static void pro_elem(struct node*);
static void process_c6t(void);
static void read_sequ(void);
static void remove_from_ellist(struct c6t_element*);
static void replace_c6t(struct c6t_element*, struct c6t_element*);
static void split(void);
static void split_kicker(struct c6t_element*);
static void split_other(struct c6t_element*);
static void split_special(struct c6t_element*);
static void supp_elem(void);
static void supp_small_comp(struct c6t_element*);
static void treat_split(struct c6t_element*);
static void yank(struct c6t_element*);
static void write_all_el(void);
static void write_blocks(void);
static void write_c6t_element(struct c6t_element*);
static void write_f16_errors(void);
static void write_f8_errors(void);
static void write_f3_aper(void);
static void write_f3_aux(void);
static void write_f3_matrix(void);
static void write_f3_entry(const char*, struct c6t_element*);
static void write_f3_mult(struct c6t_element*);
static void write_f34_special(void);
static void write_struct(void);
static int my_table_row(struct table*, char*);
/* routines used from makethin.c */
static struct li_list types;
static struct block *first_block; //, *last_block; not used
static struct block* prev_block;
static struct block* current_block = NULL;
static int virgin_c6t = 1;
static struct c6t_element *first_in_sequ, *last_in_sequ_org; // *last_in_sequ, // not used
static struct c6t_element* prev_element;
static struct c6t_element* current_element = NULL;
// static struct c6t_element* debug_element = NULL; // not used
static struct c6t_el_list* split_list = NULL;
static struct aper_struct tag_aperture;
static struct object *p_err_zero; /* pointer to error object with all zeroes */
static int last_row = 0;
static char el_info[][60] = /* see type_info definition */
/* l=0 l>0,normal l>0,skew ->drift make_k*l split */
{"aperture 2 2 2 0 0 0",
"beambeam 2 2 2 0 0 0",
"beamint 0 1 1 1 0 0",
"collimator 2 1 1 0 0 0",
"drift 0 1 1 0 0 0",
"decapole 2 2 2 0 1 2",
"ecollimator 2 1 1 0 0 0",
"elseparator 0 1 1 1 0 0",
"hkicker 5 5 5 1 0 3",
"hmonitor 0 1 1 1 0 0",
"instrument 0 1 1 1 0 0",
"placeholder 0 1 1 1 0 0",
"kicker 6 6 6 1 0 3",
"tkicker 6 6 6 1 0 3",
"lcavity 3 3 3 0 0 2",
"marker 4 0 0 0 0 0",
"matrix 2 2 2 0 0 0",
"monitor 0 1 1 1 0 0",
"multipole 2 2 2 0 0 0",
"octupole 2 2 2 0 1 2",
"quadrupole 2 1 2 0 1 1",
"rbend 2 1 1 0 1 1",
"rcollimator 2 1 1 0 0 0",
"rfcavity 3 3 3 0 0 2",
"sbend 2 1 1 0 1 1",
"sextupole 2 2 2 0 1 2",
"vkicker 5 5 5 1 0 3",
"vmonitor 0 1 1 1 0 0",
"crabcavity 3 3 3 0 0 2",
"dipedge 2 2 2 0 0 0",
"solenoid 2 2 2 0 0 0",
"hacdipole 3 3 3 0 0 2",
"vacdipole 3 3 3 0 0 2",
"rfmultipole 2 0 2 0 0 2",
"rfdipole 2 0 2 0 0 2",
"rfquadrupole 2 0 2 0 0 2",
"rfsextupole 2 0 2 0 0 2",
"rfoctupole 2 0 2 0 0 2"
};
/* no. of valid element types */
enum { N_TYPES = sizeof el_info / sizeof *el_info };
static struct type_info* t_info[N_TYPES];
static char mpole_names[][16] = {"dipole", "quadrupole", "sextupole",
"octupole", "decapole", "multipole"};
static char acro_list[20]; /* list for name starts */
static int acro_cnt[20]; /* counters for name starts */
static char tmp_name[KEY_LENGTH];
static int
block_count = 0, /* current block count for naming */
elem_cnt = 0, /* element count */
acro_occ = 0, /* acro list occupation */
align_cnt = 0, /* element with align errors count */
field_cnt = 0, /* element with field errors count */
f3_cnt = 0, /* f3 write flag */
f3aux_cnt = 0, /* f3aux write flag */
f8_cnt = 0, /* f8 write count */
f16_cnt = 0, /* f16 write count */
f34_cnt = 0, /* f34 write count */
special_flag = 1, /* produce special output file from twiss */
cavall_flag = 0, /* if 0 dump all cavities into first */
markall_flag = 0, /* if 1 dump all markers into first */
aperture_flag = 0, /* if 1 insert apertures into structure */
// radius_flag = 0, // not used /* change the default reference radius */
split_flag = 0, /* if 1 keep zero multipoles after split */
mangle_flag = 0, /* if 1 truncate to 14 chars and mangle names */
mult_auto_off = 1, /* if 1 code does not process zero value
multipoles;
if 0 process up to order max_mult_ord */
max_mult_ord = 20, /* Process up to this order for mult_auto_off = 0 */
multi_type = -1, /* is set to multipole type if any found */
cavity_count = 0; /* count cavities in output */
static double
sequ_length, /* length of sequence */
sequ_start,
sequ_end,
total_voltage = 0,
harmon = 0,
// freq = 0, // not used
error_matrix[FIELD_MAX],
tmp_buff[FIELD_MAX];
static const double ten = 10;
static const double c1p3 = 1.e3;
static const double eps_6 = 1.e-6;
static const double eps_9 = 1.e-9;
static const double eps_12 = 1.e-12;
static double ref_def = 0.017;
static FILE *f2, *f3, *f3aux, *f3aper, *f8, *f16, *f34;
// private functions
static void
add_c6t_drifts(void)
{
int af;
struct c6t_element *d1;
double pos = sequ_start, dl, el2;
char c[24];
current_element = first_in_sequ;
while (current_element != NULL)
{
el2 = current_element->value[0] / two;
dl = current_element->position - el2 - pos;
if (dl + eps_9 < zero)
{
printf(
"+=+=+= c6t fatal - negative drift in front of %s, length %f\n",
current_element->name, dl);
exit(1);
}
else if (dl > eps_9) // create an appropriate drift
{
af = get_next_name(c, 'd');
d1 = new_c6t_element(1, c, "drift");
d1->value[0] = dl; d1->flag = 1;
link_c6t_in_front(d1, current_element);
d1->position = pos + dl / two;
if (af != 0) add_to_ellist(d1);
}
pos = current_element->position + el2;
current_element = current_element->next;
}
}
static void
add_split_list(struct c6t_element* el)
{
int i;
const char *rout_name = "c6t:add_split_list";
if (split_list == NULL)
{
split_list = mycalloc(rout_name,1, sizeof *split_list);
split_list->elem = mycalloc(rout_name,EL_COUNT, sizeof *split_list->elem);
split_list->max = EL_COUNT;
}
else if (split_list->curr == split_list->max) grow_ellist(split_list);
for (i = 0; i < split_list->curr; i++) if (split_list->elem[i] == el) return;
split_list->elem[split_list->curr++] = el;
}
static void
add_to_ellist( /* adds element to correct object list */
struct c6t_element* p_elem)
{
int j;
const char *rout_name = "c6t:add_to_ellist";
#ifdef _call_tree_
puts("+++++++ add_to_ellist");
#endif
for (j = 0; j < types.curr; j++)
{
if (strcmp(types.member[j]->base_name, p_elem->base_name) == 0)
{
if (types.member[j]->curr == types.member[j]->max)
grow_ellist(types.member[j]);
types.member[j]->elem[types.member[j]->curr++] = p_elem;
return;
}
}
/* type list does not exist - create */
if (types.curr == BASE_TYPES)
{
printf("+++ fatal - %s overruns type buffer of %d types\n",
p_elem->base_name, types.curr);
exit(1);
}
types.member[types.curr] = mycalloc(rout_name,1,sizeof *types.member[0]);
types.member[types.curr]->elem = mycalloc(rout_name,EL_COUNT, sizeof *types.member[0]->elem);
types.member[types.curr]->elem[types.member[types.curr]->curr++] = p_elem;
types.member[types.curr]->max = EL_COUNT;
strcpy(types.member[types.curr]->base_name, p_elem->base_name);
types.curr++;
}
static void
app_factor(double fact, double* array, int count)
{
int i;
for (i = 0; i < count; i++) array[i] *= fact;
}
#if 0 // not used
static void
arr_print(double array[], int occ)
{
int i;
for (i = 0; i < occ; i++)
{
printf(" %12.4e", array[i]); if ((i+1)%5 == 0) printf("\n");
}
printf("\n");
}
#endif
static void
assign_att(void)
{
struct c6t_element *el;
int i, j;
for (i = 0; i < types.curr; i++) /* loop over base types */
{
for (j = 0; j < types.member[i]->curr; j++) /* loop over el. in type */
{
el = types.member[i]->elem[j];
if (el->flag > 0 && el->equiv == el) /* all others ignored */
{
if (strcmp(el->base_name, "aperture") == 0) att_aperture(el);
else if (strcmp(el->base_name, "beambeam") == 0) att_beambeam(el);
else if (strcmp(el->base_name, "collimator") == 0) att_colli(el);
else if (strcmp(el->base_name, "decapole") == 0) att_decapole(el);
else if (strcmp(el->base_name, "drift") == 0) att_drift(el);
else if (strcmp(el->base_name, "ecollimator") == 0) att_colli(el);
else if (strcmp(el->base_name, "hkicker") == 0) att_hkicker(el);
else if (strcmp(el->base_name, "kicker") == 0) att_kicker(el);
else if (strcmp(el->base_name, "tkicker") == 0) att_kicker(el);
else if (strcmp(el->base_name, "lcavity") == 0) att_lcavity(el);
else if (strcmp(el->base_name, "marker") == 0) att_marker(el);
else if (strcmp(el->base_name, "matrix") == 0) att_matrix(el);
else if (strcmp(el->base_name, "multipole") == 0) att_multipole(el);
else if (strcmp(el->base_name, "octupole") == 0) att_octupole(el);
else if (strcmp(el->base_name, "quadrupole") == 0) att_quadrupole(el);
else if (strcmp(el->base_name, "rbend") == 0) att_rbend(el);
else if (strcmp(el->base_name, "rcollimator") == 0) att_colli(el);
else if (strcmp(el->base_name, "rfcavity") == 0) att_rfcavity(el);
else if (strcmp(el->base_name, "crabcavity") == 0) att_crabcavity(el);
else if (strcmp(el->base_name, "dipedge") == 0) att_dipedge(el);
else if (strcmp(el->base_name, "solenoid") == 0) att_solenoid(el);
else if (strcmp(el->base_name, "hacdipole") == 0) att_hacdipole(el);
else if (strcmp(el->base_name, "vacdipole") == 0) att_vacdipole(el);
else if (strcmp(el->base_name, "sbend") == 0) att_sbend(el);
else if (strcmp(el->base_name, "sextupole") == 0) att_sextupole(el);
else if (strcmp(el->base_name, "vkicker") == 0) att_vkicker(el);
else if (strcmp(el->base_name, "rfmultipole") == 0) att_rfmultipole(el);
else att_undefined(el);
}
}
}
}
static void
att_aperture(struct c6t_element* el)
{
el->out_1 = 3;
el->out_2 = 1e-8;
el->out_3 = 0.0;
el->out_4 = 0.0;
}
static void
att_beambeam(struct c6t_element* el)
{
double beamx=zero,beamy=zero;
if (double_from_table_row("twiss","x",&(el->twtab_row),&beamx) != 0 ||
double_from_table_row("twiss","y",&(el->twtab_row),&beamy) != 0)
{
warning("c6t: beambeam element not found in twiss table","");
}
el->out_1 = 20;
el->out_2 = - c1p3*(el->value[12] - beamx);
el->out_3 = - c1p3*(el->value[13] - beamy);
el->out_4 = el->value[16];
el->out_5 = pow(c1p3*el->value[14], 2);
el->out_6 = pow(c1p3*el->value[15], 2);
el->out_7 = 0;
}
static void
att_colli(struct c6t_element* el)
/* collimator, ecollimator, rcollimator - make drift, do not concatenate */
{
el->out_1 = 0; el->out_4 = el->value[0];
}
static void
att_decapole(struct c6t_element* el)
{
if (el->value[20] != zero)
{
el->out_1 = 5; el->out_2 = -el->value[20]/24;
}
else if (el->value[21] != zero)
{
el->out_1 = -5; el->out_2 = el->value[21]/24;
}
else el->out_1 = 0;
}
static void
att_drift(struct c6t_element* el)
{
el->out_4 = el->value[0];
}
static void
att_hkicker(struct c6t_element* el)
{
el->out_1 = 1; el->out_2 = el->value[12];
}
static void
att_kicker(struct c6t_element* el)
{
(void)el;
}
static void
att_lcavity(struct c6t_element* el)
{
double lag = -el->value[5];
el->out_1 = 12;
el->out_2 = cavall_flag == 0 ? total_voltage : el->value[1];
el->out_3 = 0; /* ??? harmon = p_beam->a_dble[41] / el->value[11]; */
printf("harmon: %e\n", harmon);
if (lag < -0.5) lag +=1.;
else if (lag > 0.5) lag -=1.;
el->out_4 = 360. * lag;
}
static void
att_marker(struct c6t_element* el)
{
(void)el;
}
static void
att_matrix(struct c6t_element* el)
{
el->out_1 = 22;
el->out_2 = 0;
el->out_3 = 0;
el->out_4 = el->value[0];
}
static void
att_multipole(struct c6t_element* el)
{
el->out_1 = 11;
if (el->nc_pos == 0)
{
el->out_2 = el->out_3 = 1;
}
else
{
el->out_3 = el->rad_length;
if (el->nc_pos == 12)
{
el->out_2 = -el->value[12]; el->out_4 = -1;
}
else if (el->nc_pos == 13)
{
el->out_2 = el->value[13]; el->out_4 = -2;
}
}
}
static void
att_octupole(struct c6t_element* el)
{
if (el->value[18] != zero)
{
el->out_1 = 4; el->out_2 = -el->value[18]/6;
}
else if (el->value[19] != zero)
{
el->out_1 = -4; el->out_2 = el->value[19]/6;
}
else el->out_1 = 0;
}
static void
att_quadrupole(struct c6t_element* el)
{
el->out_4 = el->value[0];
if (el->value[14] != zero)
{
el->out_1 = 2;
if (el->value[0] == zero) el->out_2 = -el->value[14];
else el->out_3 = -el->value[14];
}
else if (el->value[15] != zero)
{
el->out_1 = -2; el->out_2 = el->value[15];
}
else el->out_1 = 0;
}
static void
att_rbend(struct c6t_element* el)
{
el->out_4 = el->value[0];
if (el->value[12] != zero)
{
el->out_2 = -el->value[1];
if (el->value[14] == zero) el->out_1 = 1;
else
{
el->out_1 = 6;
el->out_3 = -el->value[14];
}
}
else if (el->value[13] != zero)
{
el->out_2 = el->value[1];
if (el->value[15] == zero) el->out_1 = 4;
else
{
el->out_1 = 4;
el->out_3 = el->value[15];
}
}
else el->out_1 = 0;
}
static void
att_rfcavity(struct c6t_element* el)
{
double lag = 0.5 - el->value[5];
el->out_1 = 12;
if (cavall_flag == 0)
{
el->out_2 = total_voltage;
strcpy(el->name, "CAV");
}
else el->out_2 = el->value[1];
el->out_3 = harmon = el->value[11];
if (lag < -0.5) lag +=1.;
else if (lag > 0.5) lag -=1.;
el->out_4 = 360. * lag;
}
static void
att_crabcavity(struct c6t_element* el)
{
double lag = -el->value[5];
double tilt = el->value[12];
if (fabs(tilt - M_PI/2)<eps_9)
el->out_1 = -23;
else
el->out_1 = 23;
/* AL: Discussions with RdM lead to the conclusions that a CrabCavity shouldn't be considered as a regular accelerating cavity */
/* if (cavall_flag == 0) */
/* { */
/* el->out_2 = total_voltage; */
/* strcpy(el->name, "CAV"); */
/* } */
/* else */
el->out_2 = el->value[1];
el->out_3 = el->value[4]; // freq = // not used
if (lag < -0.5) lag +=1.;
else if (lag > 0.5) lag -=1.;
el->out_4 = 2 * M_PI * lag;
}
static void
att_dipedge(struct c6t_element* el)
{
double corr;
corr = 2*el->value[1]*el->value[8]*el->value[9];
if (el->value[1] != zero && (el->value[2] != zero || corr != zero))
{
el->out_1 = 24;
el->out_2 = el->value[1]*tan(el->value[2]);
el->out_3 = -el->value[1]*tan(el->value[2]-corr/cos(el->value[2])*
(one+sin(el->value[2])*sin(el->value[2])));
}
else
{
el->out_1 = 0;
el->out_2 = 0;
el->out_3 = 0;
}
el->out_4 = 0;
}
static void
att_solenoid(struct c6t_element* el)
{
el->out_1 = 25;
el->out_2 = el->value[2];
el->out_3 = el->value[3];
el->out_4 = el->value[0];
}
static void
att_hacdipole(struct c6t_element* el)
{
el->out_1 = 16;
el->out_2 = el->value[2];
el->out_3 = el->value[3];
el->out_4 = el->value[4];
}
static void
att_vacdipole(struct c6t_element* el)
{
el->out_1 = -16;
el->out_2 = el->value[2];
el->out_3 = el->value[3];
el->out_4 = el->value[4];
}
static void
att_sbend(struct c6t_element* el)
{
el->out_4 = el->value[0];
if (el->value[12] != zero)
{
el->out_2 = -el->value[1];
if (el->value[14] == zero) el->out_1 = 3;
else
{
el->out_1 = 6;
el->out_3 = -el->value[14];
}
}
else if (el->value[13] != zero)
{
el->out_1 = 5;
el->out_2 = el->value[1];
el->out_3 = el->value[15];
}
else el->out_1 = 0;
}
static void
att_sextupole(struct c6t_element* el)
{
if (el->value[16] != zero)
{
el->out_1 = 3; el->out_2 = -el->value[16]/two;
}
else if (el->value[17] != zero)
{
el->out_1 = -3; el->out_2 = el->value[17]/two;
}
else el->out_1 = 0;
}
static void
att_vkicker(struct c6t_element* el)
{
el->out_1 = -1; el->out_2 = el->value[13];
}
static void
att_undefined(struct c6t_element* el)
{
el->out_4 = el->value[0];
}
static void
att_rfmultipole(struct c6t_element* el)
{
(void)el;
}
static void
block_it(void)
{
struct c6t_element* el;
const char *rout_name = "c6t:block_it";
current_element = first_in_sequ;
while ((el = current_element) != NULL)
{
current_block = new_block();
current_block->previous = prev_block;
current_block->next = NULL;
if (prev_block == NULL) first_block = current_block;
else prev_block->next = current_block;
current_block->elements = mycalloc(rout_name,1,sizeof *current_block->elements);
current_block->elements->elem = mycalloc(rout_name,EL_COUNT, sizeof *current_block->elements->elem);
current_block->elements->max = EL_COUNT;
current_block->first = el;
current_block->length = el->equiv->value[0];
current_block->elements->elem[0] = el;
current_block->elements->curr = 1;
if (el->flag < 2)
{
while (el->next != NULL && el->next->flag < 2)
{
el = el->next;
current_block->length += el->equiv->value[0];
if (current_block->elements->curr == current_block->elements->max)
grow_ellist(current_block->elements);
current_block->elements->elem[current_block->elements->curr++]
= el;
}