source: PSPA/madxPSPA/src/mad_6track.c @ 445

Last change on this file since 445 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 107.0 KB
Line 
1#include "madx.h"
2
3/*---------------------------------------------------------------------*
4 *                                                                      *
5 *                           CERN                                       *
6 *                                                                      *
7 *     European Organization for Nuclear Research                       *
8 *                                                                      *
9 *     Program name: c6t: MAD-Sixtrack Format Converter                 *
10 *                                                                      *
11 *     Author and contact:   Hans GROTE                                 *
12 *                           SL Division                                *
13 *                           CERN                                       *
14 *                           CH-1211 GENEVA 23                          *
15 *                           SWITZERLAND                                *
16 *                      Tel. [041] (022) 767 49 61                      *
17 *                           Hans.Grote@cern.ch                         *
18 *                                                                      *
19 *     Converted to MAD-X by Mark HAYES                                 *
20 *     Followed up by Frank Schmidt                                     *
21 *                                                                      *
22 *     Copyright  CERN,  Geneva  2000  -  Copyright  and  any   other   *
23 *     appropriate  legal  protection  of  this  computer program and   *
24 *     associated documentation reserved  in  all  countries  of  the   *
25 *     world.                                                           *
26 *                                                                      *
27 *     Organizations collaborating with CERN may receive this program   *
28 *     and documentation freely and without charge.                     *
29 *                                                                      *
30 *     CERN undertakes no obligation  for  the  maintenance  of  this   *
31 *     program,  nor responsibility for its correctness,  and accepts   *
32 *     no liability whatsoever resulting from its use.                  *
33 *                                                                      *
34 *     Program  and documentation are provided solely for the use  of   *
35 *     the organization to which they are distributed.                  *
36 *                                                                      *
37 *     This program  may  not  be  copied  or  otherwise  distributed   *
38 *     without  permission. This message must be retained on this and   *
39 *     any other authorized copies.                                     *
40 *                                                                      *
41 *     The material cannot be sold. CERN should be  given  credit  in   *
42 *     all references.                                                  *
43 *                                                                      *
44 *---------------------------------------------------------------------*/
45
46/* 17.08.2004 - FS fix print-out of special f34 file needed as input file
47   for the sodd program. */
48/* 15/03/2004 - FS fixing faulty variable passing to "create_aperture" */
49/* 01/07/2003 - FS added the "arbitrary matrix" element */
50/* 21/03/2003 - FS fixed segmentation fault which was due to a faulty
51   free-ing of object that had already been freed before */
52/* 10/07/2002 - MH fixed missing mcdo bug, caused by recursion up
53   element tree to unexpanded double_array */
54/* 20/06/2002 - MH fixed double declarations and memory leaks because the
55   original c6t was only ment to be run once - but not this one! */
56/* 19/06/2002 - MH found last 'bug' in rhic sequence... due to micron
57   length quadrupole and rounding errors caused by them */
58/* 29/04/2002 - MH&HG made it copy all the collimators across */
59/* 23/04/2002 - MH changed ref_delta so that =0 for quads and higher
60   this directly effects fort.3 */
61/* 14/04/2002 - MH changed calloc to mycalloc for HG error checking */
62/* extract Sixtrack input files from DOOM */
63/* question 1: change BEAM defaults to those of MAD8 */
64/* question 2: att_lcavity ? */
65/* #define _call_tree_ */
66
67/* JMJ, 7/11/2002 commenting out the following
68   to see if it helps  for Visual Fortran ....
69   already in madxn.c
70
71   #include <string.h>
72   #include <stdio.h>
73   #include <stdlib.h>
74   #include <sys/types.h>
75   #include <ctype.h>
76   #include <math.h>
77   #include <time.h>
78
79   and I moved
80   #include "c6t.h"
81   to madxn.c
82
83*/
84
85#define BASE_TYPES 100    /* maximum no. of element types allowed */
86#define EL_COUNT   100    /* initial length of element type list */
87
88#ifndef M_PI
89#define M_PI 3.141592653589793238462643383279502
90#endif
91
92// types
93
94/* MADX name     : circle, ellipse, rectangle, lhcscreen */
95/* internal code :    1       2         3          4     */
96struct aper_struct {
97    int apply;
98    char name[255];
99    char style[255];
100    double value[3];
101};
102
103struct c6t_element
104{
105    char name[48],           /* name including occurrence count if > 1 */
106      org_name[48],            /* original name */
107      base_name[48];           /* basic type */
108    struct c6t_element* previous;
109    struct c6t_element* next;
110    struct c6t_element* equiv;   /* pointer to first identical element */
111    int flag;                /* treatment flag_1 or _2 or _3 (see type_info) */
112    int force;               /* magnet flag (flag_5) (see type_info) */
113    int c_drift;             /* treatment flag_4 (see type_info) */
114    int split;               /* treatment flag_6 (see type_info) */
115    int n_values;            /* length of value */
116    int w_flag;              /* 0 if not, 1 if written on fort.2 */
117    int out_1;               /* output parameter 1, fort.2 */
118    int na_err;              /* current no. of alignment errors */
119    int nf_err;              /* current no. of field errors */
120    int nc_pos;              /* component count, only multipoles */
121    int npole_sign;          /* sign inversion flag for even (created) npoles */
122    int keep_in;             /* if not 0, do not yank */
123    int mult_order;          /* error reference comp., only multipoles */
124    int f3_flag;             /* for multipole def. on fc.3 */
125    int occ_cnt;             /* occurrence count */
126    int twtab_row;           /* row number in twiss table */
127    double position;         /* s position in sequence [m] */
128    double rad_length;       /* radiation length of multipoles [m] */
129    double ref_radius;       /* reference radius for multipole errors [m] */
130    double ref_delta;        /* reference delta for multipole errors */
131    double out_2;            /* output parameter 2, fort.2 */
132    double out_3;            /* output parameter 3, fort.2 */
133    double out_4;            /* output parameter 4, fort.2 */
134    double out_5;            /* output parameter 5, fort.2 */
135    double out_6;            /* output parameter 6, fort.2 */
136    double out_7;            /* output parameter 7, fort.2 */
137    double* value;           /* element strength etc. values */
138    struct object* p_al_err; /* pointer to alignment error object */
139    struct object* p_fd_err; /* pointer to field error object */
140    struct object* p_ph_err; /* pointer to field phase error array AL: */
141    double rfm_freq;         /* frequency of the rf-multipole fields  AL: */
142    int tilt_err;            /* allow write_f8 to dump tilt as well */
143    int do_not_free;         /* avoid free crash */
144};
145
146struct c6t_el_list /* contains list of element pointers */
147{
148    int   max,                /* max. pointer array size */
149          curr;               /* current occupation */
150    char  base_name[48];
151    struct c6t_element** elem; /* element pointer list */
152};
153
154struct block
155{
156    char    name[48];
157    double  length;
158    int     flag;              /* if 0 take element, else block */
159    struct c6t_element* first;
160    struct c6t_element* last;
161    struct block* previous;
162    struct block* next;
163    struct block* equiv;
164    struct c6t_el_list* elements;
165};
166
167struct li_list /* contains list of list pointers */
168{
169    int curr;               /* current occupation */
170    struct c6t_el_list* member[BASE_TYPES]; /* list pointer list */
171};
172
173struct type_info /* info about types */
174{
175    char name[48];    /* base_type */
176    /* flag meanings - 0: skip, 1: linear, >1: non-linear,
177       2: convert to multipole (temporarily), 3: cavity
178       4: make 2 if in explicit list, else skip
179       5: only split */
180    int flag_1,  /* for length = 0 */
181        flag_2,  /* for length > 0, normal */
182        flag_3,  /* for length > 0, skew */
183        flag_4,  /* if > 0: make drift, print warning when encountered */
184        flag_5,  /* if > 0: magnet (for k0n * l) */
185        flag_6;  /* if length > 0: 0 = no split
186                    1 = split; if flag_2(_3) = 1: two identical + zero m.
187                    if flag_2(_3) = 2: two drift + full m. */
188};
189
190/* (some) constants and structure definitions for DOOM */
191
192/*
193  element definition: (F: Fortran, C: C/C++)
194  d.p. array
195  word / e_type = 1            = 2                               = 3
196  C    F
197  0    1    l      [m]          l [m]                             l [m]
198  1    2    rhoinv [1/m]        volt [MV]                          kick
199  2    3    e1                  ex [MV/m]                           .
200  3    4    e2                  ey [MV/m]                           .
201  4    5    h1                  freq [MHz]                          .
202  5    6    h2                  lag [2 Pi]                          .
203  6    7    tilt                tilt                               kick
204  7    8    ks                  betrf                       C:7-42 F:8-43: rm
205  8    9    hgap [m]            pg {MW]                 C:43-258 F:44-259: tm
206  9   10    fint [Tm]           shunt [MOhm/m]
207  10   11    angle = K_0*l       tfill [micro sec]
208  11   12    lrad                harmon
209  12   13    k0 or k0*l (l=0)    xsize (coll.) or xma (beam-beam) or x (mon.)
210  13   14    k0s or k0s*l        ysize (coll.) or yma (beam_beam) or y (mon.)
211  14   15    k1 or k1*l (l=0)    sigx
212  15   16    k1s or k1s*l        sigy
213  16   17    k2 or k2*l          charge
214  17   18    k2s  etc.           npart (# particles in opposite beam)
215
216  int array: as d.p. array, containing expression flag:
217  ex_flag = 1   value
218  ex_flag > 1   expression
219
220  name array:as d.p. array, pointers to parameter names if ex_flag > 0
221*/
222
223/*
224  parameter definition:
225  int  array:
226  1    exflag            1 if value, > 1 if expression
227  d.p. array:
228  1    value             (always)
229  char array:
230  string            expression as read if exflag > 1
231*/
232
233struct object
234{
235  char key[48];          /* d.b. key */
236/* The order of the first 11 variables below is FIXED */
237  int ma_time,            /* start of control part =
238                             major time at creation or last modification */
239      mi_time,            /* minor time at creation or last modification */
240      l_int,              /* length of integer array */
241      l_dble,             /* length of double array */
242      l_char,             /* length of string */
243      l_obj,              /* length of object pointer array */
244      c_int,              /* occupation of integer array */
245      c_dble,             /* occupation of double array */
246      c_char,             /* occupation of string */
247      c_obj;              /* occupation of object and names pointer array */
248
249  char par_name[24],      /* parent name */
250       base_name[24],     /* basic type name (e.g. QUADRUPOLE, DRIFT,..) */
251       obj_type[24];      /* object type such as ELEMENT, TWISS_SUMMARY etc. */
252
253  int*    a_int;          /* integer array */
254  double* a_dble;         /* d.p. array */
255  char*   a_char;         /* string */
256  struct object* parent;  /* pointer to parent object */
257  struct object** p_obj;  /* object pointer array */
258  char**  names;          /* name pointers into a_char */
259};
260
261/* already defined as 42 in fulll.h */
262/*#define FIELD_MAX 40*/        /* field error array length */
263#define KEY_LENGTH 48       /* from DOOM */
264#define MM_KEEP 2           /* no. of element name starts to keep */
265#define N_TYPES 37          /* no. of valid element types */
266#define MULTI_MAX 24        /* element array length for multipoles */
267#define NT34 5              /* no. of element types in special fort.34 */
268#define LINES_MAX 3         /* structure output line max. names */
269#define SEQ_DUMP_LEVEL 0    /* chooses amount of dumped output */
270
271static void add_c6t_drifts(void);
272static void add_split_list(struct c6t_element*);
273static void add_to_ellist(struct c6t_element*);
274static void app_factor(double, double*, int);
275// static void arr_print(double*, int); // not used
276static void assign_att(void);
277static void att_aperture(struct c6t_element*);
278static void att_beambeam(struct c6t_element*);
279static void att_colli(struct c6t_element*);
280static void att_decapole(struct c6t_element*);
281static void att_drift(struct c6t_element*);
282static int f34_values(struct c6t_element*, int*, double*);
283static void att_hkicker(struct c6t_element*);
284static void att_kicker(struct c6t_element*);
285static void att_lcavity(struct c6t_element*);
286static void att_marker(struct c6t_element*);
287static void att_matrix(struct c6t_element*);
288static void att_multipole(struct c6t_element*);
289static void att_octupole(struct c6t_element*);
290static void att_quadrupole(struct c6t_element*);
291static void att_rbend(struct c6t_element*);
292static void att_rfcavity(struct c6t_element*);
293static void att_crabcavity(struct c6t_element*);
294static void att_dipedge(struct c6t_element*);
295static void att_solenoid(struct c6t_element*);
296static void att_hacdipole(struct c6t_element*);
297static void att_vacdipole(struct c6t_element*);
298static void att_sbend(struct c6t_element*);
299static void att_sextupole(struct c6t_element*);
300static void att_vkicker(struct c6t_element*);
301static void att_rfquadrupole(struct c6t_element*);
302static void att_rfsextupole(struct c6t_element*);
303static void att_rfoctupole(struct c6t_element*);
304static void att_undefined(struct c6t_element*);
305static void clean_c6t_element(struct c6t_element*);
306static struct c6t_element* create_aperture(char* ,char* ,double , double , struct double_array*);
307static void concat_drifts(void);
308static void conv_elem(void);
309static void c6t_finish(void);
310static void c6t_init(void);
311static struct c6t_element* convert_madx_to_c6t(struct node*);
312// static void dump_c6t_element(struct c6t_element*); // not used
313// static void dump_c6t_sequ(int); // not used
314// static void dump_types(int); // not used
315static void equiv_elem(void);
316static struct block* get_block_equiv(struct block*);
317static void get_args(struct in_cmd*);
318static void get_error_refs(struct c6t_element*);
319static int get_flag(struct c6t_element*, struct type_info*);
320// static struct c6t_element* get_from_ellist(char*, char*); // not used
321static void get_multi_refs(void);
322static int get_next_name(char*, char);
323// static void gnu_file(struct c6t_element*); // not used
324static void grow_ellist(struct c6t_el_list*);
325static int ident_el(struct c6t_element*, struct c6t_element*);
326static int ident_zero(struct c6t_element*);
327static int in_keep_list(struct c6t_element*);
328static void invert_normal(int, double*);
329// static void invert_skew(int, double*); // not used
330static void link_behind(struct c6t_element*, struct c6t_element*);
331static void link_c6t_in_front(struct c6t_element*, struct c6t_element*);
332static struct c6t_element* make_c6t_element(struct node*);
333static struct object* make_obj(char*, int, int, int, int);
334static void make_multipole(struct c6t_element*);
335static void mod_errors(void);
336static void mod_lcavity(struct c6t_element*);
337static void mod_multipole(struct c6t_element*);
338static void mod_octupole(struct c6t_element*);
339static void mod_quadrupole(struct c6t_element*);
340static void mod_rbend(struct c6t_element*);
341static void mod_rfcavity(struct c6t_element*);
342static void mod_crabcavity(struct c6t_element*);
343// static void mod_dipedge(struct c6t_element*); // not defined
344// static void mod_solenoid(struct c6t_element*); // not defined
345// static void mod_hacdipole(struct c6t_element*); // not defined
346// static void mod_vacdipole(struct c6t_element*); // not defined
347static void mod_sextupole(struct c6t_element*);
348static void multi_loop(void);
349static struct c6t_element* new_c6t_element(int, char*, char*);
350static struct block* new_block(void);
351static void post_multipoles(void);
352static double power_of(double, int);
353static void pre_multipole(struct c6t_element*);
354static void pro_elem(struct node*);
355static void process_c6t(void);
356static void read_sequ(void);
357static void remove_from_ellist(struct c6t_element*);
358static void replace_c6t(struct c6t_element*, struct c6t_element*);
359static void split(void);
360static void split_kicker(struct c6t_element*);
361static void split_other(struct c6t_element*);
362static void split_special(struct c6t_element*);
363static void supp_elem(void);
364static void supp_small_comp(struct c6t_element*);
365static void treat_split(struct c6t_element*);
366static void yank(struct c6t_element*);
367static void write_all_el(void);
368static void write_blocks(void);
369static void write_c6t_element(struct c6t_element*);
370static void write_f16_errors(void);
371static void write_f8_errors(void);
372static void write_f3_aper(void);
373static void write_f3aux(void);
374static void write_f3_matrix(void);
375static void write_f3_entry(char*, struct c6t_element*);
376static void write_f3_mult(struct c6t_element*);
377static void write_f34_special(void);
378static void write_struct(void);
379static int my_table_row(struct table*, char*);
380
381/* routines used from makethin.c */
382static struct li_list types;
383
384static struct type_info* t_info[N_TYPES];
385
386static struct block   *first_block; //, *last_block; not used
387static struct block*   prev_block;
388static struct block*   current_block = NULL;
389
390static int virgin_c6t = 1;
391
392static struct c6t_element *first_in_sequ, *last_in_sequ_org; // *last_in_sequ, // not used
393static struct c6t_element* prev_element;
394static struct c6t_element* current_element = NULL;
395// static struct c6t_element* debug_element = NULL; // not used
396static struct c6t_el_list* split_list = NULL;
397static struct aper_struct tag_aperture;
398
399static struct object *p_err_zero;  /* pointer to error object with all zeroes */
400
401static int last_row = 0;
402
403static char el_info[N_TYPES][60] = /* see type_info definition */
404/*           l=0 l>0,normal l>0,skew ->drift make_k*l split */
405{"aperture     2       2       2       0       0       0",
406 "beambeam     2       2       2       0       0       0",
407 "beamint      0       1       1       1       0       0",
408 "drift        0       1       1       0       0       0",
409 "decapole     2       2       2       0       1       2",
410 "ecollimator  2       1       1       0       0       0",
411 "elseparator  0       1       1       1       0       0",
412 "gbend        1       1       1       2       1       1",
413 "hkicker      5       5       5       1       0       3",
414 "hmonitor     0       1       1       1       0       0",
415 "instrument   0       1       1       1       0       0",
416 "placeholder  0       1       1       1       0       0",
417 "kicker       6       6       6       1       0       3",
418 "tkicker      6       6       6       1       0       3",
419 "lcavity      3       3       3       0       0       2",
420 "marker       4       0       0       0       0       0",
421 "matrix       2       2       2       0       0       0",
422 "monitor      0       1       1       1       0       0",
423 "multipole    2       2       2       0       0       0",
424 "octupole     2       2       2       0       1       2",
425 "quadrupole   2       1       2       0       1       1",
426 "rbend        2       1       1       0       1       1",
427 "rcollimator  2       1       1       0       0       0",
428 "rfcavity     3       3       3       0       0       2",
429 "sbend        2       1       1       0       1       1",
430 "sextupole    2       2       2       0       1       2",
431 "vkicker      5       5       5       1       0       3",
432 "vmonitor     0       1       1       1       0       0",
433 "crabcavity   3       3       3       0       0       2",
434 "dipedge      2       2       2       0       0       0",
435 "solenoid     2       2       2       0       0       0",
436 "hacdipole    3       3       3       0       0       2",
437 "vacdipole    3       3       3       0       0       2",
438 "rfmultipole  2       0       2       0       0       2",
439 "rfquadrupole 2       0       2       0       0       2",
440 "rfsextupole  2       0       2       0       0       2",
441 "rfoctupole   2       0       2       0       0       2"
442};
443
444static char keep_these[MM_KEEP][24] = {"ip", "mt_"};
445static char mpole_names[][16] = {"dipole", "quadrupole", "sextupole",
446                          "octupole", "decapole", "multipole"};
447static char acro_list[20];   /* list for name starts */
448static int acro_cnt[20];    /* counters for name starts */
449static char tmp_name[KEY_LENGTH];
450
451static int 
452  block_count = 0,     /* current block count for naming */
453  elem_cnt = 0,        /* element count */
454  acro_occ = 0,        /* acro list occupation */
455  align_cnt = 0,       /* element with align errors count */
456  field_cnt = 0,       /* element with field errors count */
457  f3_cnt = 0,          /* f3 write flag */
458  f3aux_cnt = 0,       /* f3aux write flag */
459  f3_matrix_cnt = 0,   /* f3_matrix write flag */
460  f8_cnt = 0,          /* f8 write count */
461  f16_cnt = 0,         /* f16 write count */
462  f34_cnt = 0,         /* f34 write count */
463  special_flag = 1,    /* produce special output file from twiss */
464  cavall_flag = 0,     /* if 0 lump all cavities into first */
465  aperture_flag = 0,   /* if 1 insert apertures into structure */
466//  radius_flag = 0, // not used    /* change the default reference radius */
467  split_flag = 0,      /* if 1 keep zero multipoles after split */
468  mult_auto_off = 1,   /* if 1 code does not process zero value
469                          multipoles;
470                          if 0 process up to order max_mult_ord */
471  max_mult_ord = 20,   /* Process up to this order for mult_auto_off = 0 */
472  multi_type = -1,     /* is set to multipole type if any found */
473  cavity_count = 0;    /* count cavities in output */
474
475static double
476  sequ_length,         /* length of  sequence */
477  sequ_start,
478  sequ_end,
479  total_voltage = 0,
480  harmon = 0,
481//  freq = 0, // not used
482  error_matrix[FIELD_MAX],
483  tmp_buff[FIELD_MAX];
484
485static const double ten   = 10;
486static const double c1p3 = 1.e3;
487static const double eps_6 = 1.e-6;
488static const double eps_9 = 1.e-9;
489static const double eps_12 = 1.e-12;
490static double ref_def = 0.017;
491
492static FILE *f2, *f3, *f3aux, *f3matrix, *f3aper, *f8, *f16, *f34;
493
494// private functions
495
496static void
497add_c6t_drifts(void)
498{
499  int af;
500  struct c6t_element *d1;
501  double pos = sequ_start, dl, el2;
502  char c[24];
503  current_element = first_in_sequ;
504  while (current_element != NULL)
505  {
506    el2 = current_element->value[0] / two;
507    dl = current_element->position - el2 - pos;
508    if (dl + eps_9 < zero)
509    {
510      printf(
511        "+=+=+= c6t fatal - negative drift in front of %s, length %f\n",
512        current_element->name, dl);
513      exit(1);
514    }
515    else if (dl > eps_9)
516    {
517      af = get_next_name(c, 'd');
518      d1 = new_c6t_element(1, c, "drift");
519      d1->value[0] = dl; d1->flag = 1;
520      link_c6t_in_front(d1, current_element);
521      d1->position = pos + dl / two;
522      if (af != 0)  add_to_ellist(d1);
523    }
524    pos = current_element->position + el2;
525    current_element = current_element->next;
526  }
527}
528
529static void
530add_split_list(struct c6t_element* el)
531{
532  int i;
533  char rout_name[] = "c6t:add_split_list";
534  if (split_list == NULL)
535  {
536    split_list = (struct c6t_el_list*) mycalloc(rout_name,1, sizeof(struct c6t_el_list));
537    split_list->elem =
538      (struct c6t_element**) mycalloc(rout_name,EL_COUNT, sizeof(struct elem*));
539    split_list->max = EL_COUNT;
540  }
541  else if (split_list->curr == split_list->max) grow_ellist(split_list);
542  for (i = 0; i < split_list->curr; i++) if (split_list->elem[i] == el) return;
543  split_list->elem[split_list->curr++] = el;
544}
545
546static void
547add_to_ellist( /* adds element to correct object list */
548  struct c6t_element* p_elem)
549{
550  int j;
551  char rout_name[] = "c6t:add_to_ellist";
552
553#ifdef _call_tree_
554  puts("+++++++ add_to_ellist");
555#endif
556  for (j = 0; j < types.curr; j++)
557  {
558    if (strcmp(types.member[j]->base_name, p_elem->base_name) == 0)
559    {
560      if (types.member[j]->curr == types.member[j]->max)
561        grow_ellist(types.member[j]);
562      types.member[j]->elem[types.member[j]->curr++] = p_elem;
563      return;
564    }
565  }
566  /* type list does not exist - create */
567  if (types.curr == BASE_TYPES)
568  {
569    printf("+++ fatal - %s overruns type buffer of %d types\n",
570           p_elem->base_name, types.curr);
571    exit(1);
572  }
573  types.member[types.curr]
574    = (struct c6t_el_list*) mycalloc(rout_name,1,sizeof(struct c6t_el_list));
575  types.member[types.curr]->elem
576    = (struct c6t_element**) mycalloc(rout_name,EL_COUNT, sizeof(struct c6t_element*));
577  types.member[types.curr]->elem[types.member[types.curr]->curr++] = p_elem;
578  types.member[types.curr]->max = EL_COUNT;
579  strcpy(types.member[types.curr]->base_name, p_elem->base_name);
580  types.curr++;
581}
582
583static void
584app_factor(double fact, double* array, int count)
585{
586  int i;
587  for (i = 0; i < count; i++) array[i] *= fact;
588}
589
590#if 0 // not used
591static void
592arr_print(double array[], int occ)
593{
594  int i;
595  for (i = 0; i < occ; i++)
596  {
597    printf(" %12.4e", array[i]); if ((i+1)%5 == 0) printf("\n");
598  }
599  printf("\n");
600}
601#endif
602
603static void
604assign_att(void)
605{
606  struct c6t_element *el;
607  int i, j;
608
609  for (i = 0; i < types.curr; i++)  /* loop over base types */
610  {
611    for (j = 0; j < types.member[i]->curr; j++) /* loop over el. in type */
612    {
613      el = types.member[i]->elem[j];
614      if (el->flag > 0 && el->equiv == el)  /* all others ignored */
615      {
616        if (strcmp(el->base_name, "aperture") == 0) att_aperture(el);
617        else if (strcmp(el->base_name, "beambeam") == 0) att_beambeam(el);
618        else if (strcmp(el->base_name, "decapole") == 0) att_decapole(el);
619        else if (strcmp(el->base_name, "drift") == 0) att_drift(el);
620        else if (strcmp(el->base_name, "ecollimator") == 0) att_colli(el);
621        else if (strcmp(el->base_name, "hkicker") == 0) att_hkicker(el);
622        else if (strcmp(el->base_name, "kicker") == 0) att_kicker(el);
623        else if (strcmp(el->base_name, "tkicker") == 0) att_kicker(el);
624        else if (strcmp(el->base_name, "lcavity") == 0) att_lcavity(el);
625        else if (strcmp(el->base_name, "marker") == 0) att_marker(el);
626        else if (strcmp(el->base_name, "matrix") == 0) att_matrix(el);
627        else if (strcmp(el->base_name, "multipole") == 0) att_multipole(el);
628        else if (strcmp(el->base_name, "octupole") == 0) att_octupole(el);
629        else if (strcmp(el->base_name, "quadrupole") == 0) att_quadrupole(el);
630        else if (strcmp(el->base_name, "rbend") == 0) att_rbend(el);
631        else if (strcmp(el->base_name, "rcollimator") == 0) att_colli(el);
632        else if (strcmp(el->base_name, "rfcavity") == 0) att_rfcavity(el);
633        else if (strcmp(el->base_name, "crabcavity") == 0) att_crabcavity(el);
634        else if (strcmp(el->base_name, "dipedge") == 0) att_dipedge(el);
635        else if (strcmp(el->base_name, "solenoid") == 0) att_solenoid(el);
636        else if (strcmp(el->base_name, "hacdipole") == 0) att_hacdipole(el);
637        else if (strcmp(el->base_name, "vacdipole") == 0) att_vacdipole(el);
638        else if (strcmp(el->base_name, "sbend") == 0) att_sbend(el);
639        else if (strcmp(el->base_name, "sextupole") == 0) att_sextupole(el);
640        else if (strcmp(el->base_name, "vkicker") == 0) att_vkicker(el);
641        else if (strcmp(el->base_name, "rfquadrupole") == 0) att_rfquadrupole(el);
642        else if (strcmp(el->base_name, "rfsextupole") == 0) att_rfsextupole(el);
643        else if (strcmp(el->base_name, "rfoctupole") == 0) att_rfoctupole(el);
644        else att_undefined(el);
645      }
646    }
647  }
648}
649
650static void
651att_aperture(struct c6t_element* el)
652{
653  el->out_1 = 3;
654  el->out_2 = 1e-8;
655  el->out_3 = 0.0;
656  el->out_4 = 0.0;
657}
658
659static void
660att_beambeam(struct c6t_element* el)
661{
662
663  double beamx=zero,beamy=zero;
664  if (double_from_table_row("twiss","x",&(el->twtab_row),&beamx) != 0 ||
665      double_from_table_row("twiss","y",&(el->twtab_row),&beamy) != 0)
666  {
667    warning("c6t: beambeam element not found in twiss table","");
668  }
669  el->out_1 = 20;
670  el->out_2 = c1p3*(el->value[12] - beamx);
671  el->out_3 = c1p3*(el->value[13] - beamy);
672  el->out_4 = el->value[16];
673  el->out_5 = pow(c1p3*el->value[14], 2);
674  el->out_6 = pow(c1p3*el->value[15], 2);
675  el->out_7 = 0;
676
677}
678
679static void
680att_colli(struct c6t_element* el)
681/* ecollim. + rcollim. - make drift, do not concatenate */
682{
683  el->out_1 = 0; el->out_4 = el->value[0];
684}
685
686static void
687att_decapole(struct c6t_element* el)
688{
689  if (el->value[20] != zero)
690  {
691    el->out_1 = 5; el->out_2 = -el->value[20]/24;
692  }
693  else if (el->value[21] != zero)
694  {
695    el->out_1 = -5; el->out_2 = el->value[21]/24;
696  }
697  else el->out_1 = 0;
698}
699
700static void
701att_drift(struct c6t_element* el)
702{
703  el->out_4 = el->value[0];
704}
705
706static void
707att_hkicker(struct c6t_element* el)
708{
709  el->out_1 = 1; el->out_2 = el->value[12];
710}
711
712static void
713att_kicker(struct c6t_element* el)
714{
715  (void)el;
716}
717
718static void
719att_lcavity(struct c6t_element* el)
720{
721  double lag = -el->value[5];
722  el->out_1 = 12;
723  el->out_2 = cavall_flag == 0 ? total_voltage : el->value[1];
724  el->out_3 = 0; /* ??? harmon = p_beam->a_dble[41] / el->value[11]; */
725  printf("harmon: %e\n", harmon);
726  if (lag < -0.5) lag +=1.;
727  else if (lag > 0.5) lag -=1.;
728  el->out_4 = 360. * lag;
729}
730
731static void
732att_marker(struct c6t_element* el)
733{
734  (void)el;
735}
736
737static void
738att_matrix(struct c6t_element* el)
739{
740  el->out_1 = 22;
741  el->out_2 = 0;
742  el->out_3 = 0;
743  el->out_4 = el->value[0];
744}
745
746static void
747att_multipole(struct c6t_element* el)
748{
749  el->out_1 = 11;
750  if (el->nc_pos == 0)
751  {
752    el->out_2 = el->out_3 = 1;
753  }
754  else
755  {
756    el->out_3 = el->rad_length;
757    if (el->nc_pos == 12)
758    {
759      el->out_2 = -el->value[12]; el->out_4 = -1;
760    }
761    else if (el->nc_pos == 13)
762    {
763      el->out_2 = el->value[13]; el->out_4 = -2;
764    }
765  }
766}
767
768static void
769att_octupole(struct c6t_element* el)
770{
771  if (el->value[18] != zero)
772  {
773    el->out_1 = 4; el->out_2 = -el->value[18]/6;
774  }
775  else if (el->value[19] != zero)
776  {
777    el->out_1 = -4; el->out_2 = el->value[19]/6;
778  }
779  else el->out_1 = 0;
780}
781
782static void
783att_quadrupole(struct c6t_element* el)
784{
785  el->out_4 = el->value[0];
786  if (el->value[14] != zero)
787  {
788    el->out_1 = 2;
789    if (el->value[0] == zero) el->out_2 = -el->value[14];
790    else                      el->out_3 = -el->value[14];
791  }
792  else if (el->value[15] != zero)
793  {
794    el->out_1 = -2; el->out_2 = el->value[15];
795  }
796  else el->out_1 = 0;
797}
798
799static void
800att_rbend(struct c6t_element* el)
801{
802  el->out_4 = el->value[0];
803  if (el->value[12] != zero)
804  {
805    el->out_2 = -el->value[1];
806    if (el->value[14] == zero)  el->out_1 = 1;
807    else
808    {
809      el->out_1 = 6;
810      el->out_3 = -el->value[14];
811    }
812  }
813  else if (el->value[13] != zero)
814  {
815    el->out_2 = el->value[1];
816    if (el->value[15] == zero)  el->out_1 = 4;
817    else
818    {
819      el->out_1 = 4;
820      el->out_3 = el->value[15];
821    }
822  }
823  else el->out_1 = 0;
824}
825
826static void
827att_rfcavity(struct c6t_element* el)
828{
829  double lag = 0.5 - el->value[5];
830  el->out_1 = 12;
831  if (cavall_flag == 0)
832  {
833    el->out_2 = total_voltage;
834    strcpy(el->name, "CAV");
835  }
836  else el->out_2 = el->value[1];
837  el->out_3 = harmon = el->value[11];
838  if (lag < -0.5) lag +=1.;
839  else if (lag > 0.5) lag -=1.;
840  el->out_4 = 360. * lag;
841}
842
843static void
844att_crabcavity(struct c6t_element* el)
845{
846  double lag = el->value[5];
847  double tilt = el->value[12];
848  if (fabs(tilt - M_PI/2)<eps_9)
849    el->out_1 = -23;
850  else
851    el->out_1 = 23;
852  if (cavall_flag == 0)
853  {
854    el->out_2 = total_voltage;
855    strcpy(el->name, "CAV");
856  }
857  else el->out_2 = el->value[1];
858  el->out_3 = el->value[4]; // freq = // not used
859  if (lag < -0.5) lag +=1.;
860  else if (lag > 0.5) lag -=1.;
861  el->out_4 = 360. * lag;
862}
863
864static void
865att_dipedge(struct c6t_element* el)
866{
867  double corr;
868  corr = 2*el->value[1]*el->value[8]*el->value[9];
869  if (el->value[1] != zero && (el->value[2] != zero || corr != zero))
870  {
871    el->out_1 = 24;
872    el->out_2 =  el->value[1]*tan(el->value[2]);
873    el->out_3 = -el->value[1]*tan(el->value[2]-corr/cos(el->value[2])*
874                                  (one+sin(el->value[2])*sin(el->value[2])));
875  }
876  else
877  {
878    el->out_1 = 0;
879    el->out_2 = 0;
880    el->out_3 = 0;
881  }
882  el->out_4 = 0;
883}
884
885static void
886att_solenoid(struct c6t_element* el)
887{
888  el->out_1 = 25;
889  el->out_2 = el->value[2];
890  el->out_3 = el->value[3];
891  el->out_4 = el->value[0];
892}
893
894static void
895att_hacdipole(struct c6t_element* el)
896{
897  el->out_1 = 16;
898  el->out_2 = el->value[2];
899  el->out_3 = el->value[3];
900  el->out_4 = el->value[4];
901}
902
903static void
904att_vacdipole(struct c6t_element* el)
905{
906  el->out_1 = -16;
907  el->out_2 = el->value[2];
908  el->out_3 = el->value[3];
909  el->out_4 = el->value[4];
910}
911
912static void
913att_sbend(struct c6t_element* el)
914{
915  el->out_4 = el->value[0];
916  if (el->value[12] != zero)
917  {
918    el->out_2 = -el->value[1];
919    if (el->value[14] == zero)  el->out_1 = 3;
920    else
921    {
922      el->out_1 = 6;
923      el->out_3 = -el->value[14];
924    }
925  }
926  else if (el->value[13] != zero)
927  {
928    el->out_1 = 5;
929    el->out_2 = el->value[1];
930    el->out_3 = el->value[15];
931  }
932  else el->out_1 = 0;
933}
934
935static void
936att_sextupole(struct c6t_element* el)
937{
938  if (el->value[16] != zero)
939  {
940    el->out_1 = 3; el->out_2 = -el->value[16]/two;
941  }
942  else if (el->value[17] != zero)
943  {
944    el->out_1 = -3; el->out_2 = el->value[17]/two;
945  }
946  else el->out_1 = 0;
947}
948
949static void
950att_vkicker(struct c6t_element* el)
951{
952  el->out_1 = -1; el->out_2 = el->value[13];
953}
954
955static void 
956att_undefined(struct c6t_element* el)
957{
958  el->out_4 = el->value[0];
959}
960
961static void
962att_rfquadrupole(struct c6t_element* el)
963{
964  /*
965  ** out_1 = 26
966  ** out_2 = coefficient
967  ** out_3 = frequency
968  ** out_4 = phase
969  ** out_5 = tilt
970  */
971
972  if (fabs(el->value[3])>eps_9) {
973    el->out_1 = 26;
974    el->out_2 = el->value[3];
975  } else if (fabs(el->value[9])>eps_9) {
976    el->out_1 = -26;
977    el->out_2 = el->value[9];
978  }
979  el->out_3 = el->value[2];
980  el->out_4 = el->value[1];
981  el->out_5 = el->value[6];
982}
983
984static void
985att_rfsextupole(struct c6t_element* el)
986{
987  /*
988  ** out_1 = 27
989  ** out_2 = coefficient
990  ** out_3 = frequency
991  ** out_4 = phase
992  ** out_5 = tilt
993  */
994
995  if (fabs(el->value[4])>eps_9) {
996    el->out_1 = 27;
997    el->out_2 = el->value[4];
998  } else if (fabs(el->value[10])>eps_9) {
999    el->out_1 = -27;
1000    el->out_2 = el->value[10];
1001  }
1002  el->out_3 = el->value[2];
1003  el->out_4 = el->value[7];
1004  el->out_5 = el->value[6];
1005}
1006
1007static void
1008att_rfoctupole(struct c6t_element* el)
1009{
1010  /*
1011  ** out_1 = 28
1012  ** out_2 = coefficient
1013  ** out_3 = frequency
1014  ** out_4 = phase
1015  ** out_5 = tilt
1016  */
1017
1018  if (fabs(el->value[5])>eps_9) {
1019    el->out_1 = 28;
1020    el->out_2 = el->value[5];
1021  } else if (fabs(el->value[11])>eps_9) {
1022    el->out_1 = -28;
1023    el->out_2 = el->value[11];
1024  }
1025  el->out_3 = el->value[2];
1026  el->out_4 = el->value[8];
1027  el->out_5 = el->value[6];
1028}
1029
1030static void
1031block_it(void)
1032{
1033  struct c6t_element* el;
1034  char rout_name[] = "c6t:block_it";
1035
1036  current_element = first_in_sequ;
1037  while ((el = current_element) != NULL)
1038  {
1039    current_block = new_block();
1040    current_block->previous = prev_block;
1041    current_block->next = NULL;
1042    if (prev_block == NULL) first_block = current_block;
1043    else                    prev_block->next = current_block;
1044    current_block->elements
1045      = (struct c6t_el_list*) mycalloc(rout_name,1,sizeof(struct c6t_el_list));
1046    current_block->elements->elem
1047      = (struct c6t_element**) mycalloc(rout_name,EL_COUNT, sizeof(struct c6t_element*));
1048    current_block->elements->max = EL_COUNT;
1049    current_block->first = el;
1050    current_block->length = el->equiv->value[0];
1051    current_block->elements->elem[0] = el;
1052    current_block->elements->curr = 1;
1053    if (el->flag < 2)
1054    {
1055      while (el->next != NULL && el->next->flag < 2)
1056      {
1057        el = el->next;
1058        current_block->length += el->equiv->value[0];
1059        if (current_block->elements->curr == current_block->elements->max)
1060          grow_ellist(current_block->elements);
1061        current_block->elements->elem[current_block->elements->curr++]
1062          = el;
1063      }
1064      current_element = el;
1065    }
1066    current_block->last = current_element;
1067    if (current_block->first == current_block->last &&
1068        current_block->last->flag >= 2)  current_block->flag = 0;
1069    else current_block->flag = 1;
1070    current_block->equiv = get_block_equiv(current_block);
1071    current_element = current_element->next;
1072    prev_block = current_block;
1073  }
1074  // last_block = current_block; // not used
1075}
1076
1077static void
1078concat_drifts(void)
1079{
1080  struct c6t_element *d1, *temp, *nk;
1081  int flag, cnt;
1082  double suml, pos;
1083  char c[24];
1084  current_element = first_in_sequ;
1085  while (current_element != NULL)
1086  {
1087    cnt = 0;
1088    suml = current_element->value[0];
1089    pos = current_element->position - suml / two;
1090    if (strcmp(current_element->base_name, "drift") == 0)
1091    {
1092      temp = current_element->next;
1093      while (temp != NULL && strcmp(temp->base_name, "drift") == 0)
1094      {
1095        suml += temp->value[0]; cnt++;
1096        temp = temp->next;
1097      }
1098    }
1099    if (cnt > 0) /* actually concatenated something */
1100    {
1101      flag = get_next_name(c, 'd');
1102      d1 = new_c6t_element(1, c, "drift");  d1->flag = 1;
1103      d1->value[0] = suml; d1->position = pos + suml / two;
1104      if (flag != 0) add_to_ellist(d1);
1105      temp = current_element->next;
1106      while (temp != NULL && strcmp(temp->base_name, "drift") == 0)
1107      {
1108        nk = temp->next;
1109        yank(temp);
1110        temp = nk;
1111      }
1112      if (current_element == first_in_sequ) first_in_sequ = d1;
1113      replace_c6t(current_element, d1); current_element = d1;
1114    }
1115    current_element = current_element->next;
1116  }
1117}
1118
1119static void
1120conv_elem(void)
1121{
1122  int i, j, nup;
1123  struct type_info* type = NULL;
1124  struct c6t_element* el;
1125
1126  for (i = 0; i < types.curr; i++)  /* loop over base types */
1127  {
1128    for (j = 0; j < N_TYPES; j++)
1129    {
1130      if (strcmp(types.member[i]->base_name, t_info[j]->name) == 0)
1131      {
1132        type = t_info[j]; break;
1133      }
1134    }
1135    if (type == NULL)
1136    {
1137      printf("+=+=+= c6t fatal - type %s not defined\n",
1138             types.member[i]->base_name);
1139      exit(1);
1140    }
1141    nup = types.member[i]->curr;
1142    for (j = 0; j < nup; j++) /* loop over el. in type */
1143    {
1144      el = types.member[i]->elem[j];
1145      if (type->flag_4 > 1)
1146        printf("+++ warning - treated as drift: %s\n", el->name);
1147      el->flag = get_flag(el, type);
1148      if (el->flag > 0)  /* all others ignored */
1149      {
1150        if (el->value[0] < eps_9)
1151        {
1152          el->value[0] = zero;
1153          if (el->flag == 1)  el->flag = 0;
1154        }
1155        if (el->flag > 0)
1156        {
1157          el->c_drift = type->flag_4;
1158          el->force = type->flag_5;
1159          el->split = type->flag_6;
1160          if (el->split > 0) add_split_list(el);
1161        }
1162      }
1163    }
1164  }
1165}
1166
1167static void
1168clean_c6t_element(struct c6t_element* cleanme)
1169{
1170  int i;
1171  for(i=0; i<cleanme->n_values; i++) { cleanme->value[i]=0; }
1172}
1173
1174static struct c6t_element*
1175create_aperture(char* name,char* type,double a, double b, struct double_array* p_al_err)
1176{
1177  struct c6t_element* aper_element;
1178  aper_element = new_c6t_element(4,name,"aperture");
1179  clean_c6t_element(aper_element);
1180  strcpy(aper_element->org_name,name);
1181  aper_element->value[0] = 0.0;
1182  aper_element->value[1] = a;
1183  aper_element->value[2] = b;
1184  if (strcmp(type,"RE")==0)
1185  {
1186    aper_element->value[3] = 1;
1187  }
1188  else
1189  {
1190    aper_element->value[3] = 2;
1191  }
1192  aper_element->keep_in=1;
1193  /* alignment errors of aperture are to be copied toalignment errors of element */
1194  if (p_al_err && p_al_err->curr>11)
1195  {
1196    align_cnt++;
1197    aper_element->na_err = p_al_err->curr;
1198    aper_element->p_al_err = make_obj("ALDUM",0,ALIGN_MAX,0,0);
1199    aper_element->p_al_err->c_dble = p_al_err->curr;
1200    aper_element->p_al_err->a_dble[0] = p_al_err->a[10];
1201    aper_element->p_al_err->a_dble[1] = p_al_err->a[11];
1202  }
1203  return aper_element;
1204}
1205
1206static struct c6t_element*
1207convert_madx_to_c6t(struct node* p)
1208{
1209  struct command_parameter *kn_param = NULL,*ks_param = NULL,*aper_param = NULL;
1210  struct command_parameter *pn_param = NULL,*ps_param = NULL;
1211  struct c6t_element* c6t_elem = NULL;
1212  char t_name[255];
1213  int i,j;
1214  char* cp;
1215  int index=-1;
1216
1217  strcpy(t_name, p->name);
1218  if ((cp = strchr(t_name, ':')) != NULL) *cp = '\0';
1219  if ((strcmp(p->base_name,"rbend") == 0)      ||
1220      (strcmp(p->base_name,"sbend") == 0)      ||
1221      (strcmp(p->base_name,"quadrupole") == 0) ||
1222      (strcmp(p->base_name,"sextupole") == 0)  ||
1223      (strcmp(p->base_name,"octupole") == 0)   ||
1224      (strcmp(p->base_name,"vkicker") == 0)    ||
1225      (strcmp(p->base_name,"hkicker") == 0)    ||
1226      (strcmp(p->base_name,"tkicker") == 0)    ||
1227      (strcmp(p->base_name,"kicker") == 0))
1228  {
1229    c6t_elem = new_c6t_element(20,t_name,p->base_name);
1230    clean_c6t_element(c6t_elem);
1231    strcpy(c6t_elem->org_name,t_name);
1232
1233    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1234    c6t_elem->value[1] = el_par_value("rhoinv",p->p_elem);  /* not intrinsic */
1235    c6t_elem->value[2] = el_par_value_recurse("e1",p->p_elem);
1236    c6t_elem->value[3] = el_par_value_recurse("e2",p->p_elem);
1237    c6t_elem->value[4] = el_par_value_recurse("h1",p->p_elem);
1238    c6t_elem->value[5] = el_par_value_recurse("h2",p->p_elem);
1239    c6t_elem->value[6] = el_par_value_recurse("tilt",p->p_elem);
1240    c6t_elem->value[7] = el_par_value_recurse("k0s",p->p_elem);
1241    c6t_elem->value[8] = el_par_value_recurse("hgap",p->p_elem);
1242    c6t_elem->value[9] = el_par_value_recurse("fint",p->p_elem);
1243    c6t_elem->value[10] = el_par_value_recurse("angle",p->p_elem);
1244    c6t_elem->value[11] = el_par_value_recurse("lrad",p->p_elem);
1245    c6t_elem->value[12] = el_par_value_recurse("k0",p->p_elem);
1246    if (c6t_elem->value[12] == zero && c6t_elem->value[0] > zero)
1247    {
1248      c6t_elem->value[12] = c6t_elem->value[10]/c6t_elem->value[0];
1249    }
1250    c6t_elem->value[13] = el_par_value_recurse("k0s",p->p_elem);
1251    c6t_elem->value[14] = el_par_value_recurse("k1",p->p_elem);
1252    c6t_elem->value[15] = el_par_value_recurse("k1s",p->p_elem);
1253    c6t_elem->value[16] = el_par_value_recurse("k2",p->p_elem);
1254    c6t_elem->value[17] = el_par_value_recurse("k2s",p->p_elem);
1255    c6t_elem->value[18] = el_par_value_recurse("k3",p->p_elem);
1256    c6t_elem->value[19] = el_par_value_recurse("k3s",p->p_elem);
1257  }
1258  else if ((strcmp(p->base_name,"multipole") == 0))
1259  {
1260    int maxkn=0, maxks=0;
1261    /*      if ((kn_param = return_param_recurse("knl",p->p_elem))) maxkn=kn_param->double_array->curr; */
1262    /*      if ((ks_param = return_param_recurse("ksl",p->p_elem))) maxks=ks_param->double_array->curr; */
1263    if ((index = name_list_pos("knl",p->p_elem->def->par_names))>-1)
1264    {
1265      kn_param = p->p_elem->def->par->parameters[index];
1266      maxkn=kn_param->double_array->curr;
1267    }
1268    if ((index = name_list_pos("ksl",p->p_elem->def->par_names))>-1)
1269    {
1270      ks_param = p->p_elem->def->par->parameters[index];
1271      maxks=ks_param->double_array->curr;
1272    }
1273    if (maxkn > maxks) {j=maxkn;} else {j=maxks;}
1274    i=j*2+12+1;
1275    c6t_elem = new_c6t_element(i,t_name,p->base_name);
1276    clean_c6t_element(c6t_elem);
1277    strcpy(c6t_elem->org_name,t_name);
1278    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1279    c6t_elem->value[6] = el_par_value_recurse("tilt",p->p_elem);
1280    c6t_elem->value[11] = el_par_value_recurse("lrad",p->p_elem);
1281    for (i=0; i<j; i++)
1282    {
1283      if (i<maxkn) c6t_elem->value[i*2+12] = kn_param->double_array->a[i];
1284      if (i<maxks) c6t_elem->value[i*2+13] = ks_param->double_array->a[i];
1285    }
1286  }
1287  else if ((strcmp(p->base_name,"matrix") == 0))
1288  {
1289    c6t_elem = new_c6t_element(43,t_name,p->base_name);
1290    clean_c6t_element(c6t_elem);
1291    strcpy(c6t_elem->org_name,t_name);
1292    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1293    c6t_elem->value[1] = el_par_value_recurse("kick1",p->p_elem);
1294    c6t_elem->value[2] = el_par_value_recurse("kick2",p->p_elem);
1295    c6t_elem->value[3] = el_par_value_recurse("kick3",p->p_elem);
1296    c6t_elem->value[4] = el_par_value_recurse("kick4",p->p_elem);
1297    c6t_elem->value[5] = el_par_value_recurse("kick5",p->p_elem);
1298    c6t_elem->value[6] = el_par_value_recurse("kick6",p->p_elem);
1299    c6t_elem->value[7] = el_par_value_recurse("rm11",p->p_elem);
1300    c6t_elem->value[8] = el_par_value_recurse("rm12",p->p_elem);
1301    c6t_elem->value[9] = el_par_value_recurse("rm13",p->p_elem);
1302    c6t_elem->value[10] = el_par_value_recurse("rm14",p->p_elem);
1303    c6t_elem->value[11] = el_par_value_recurse("rm15",p->p_elem);
1304    c6t_elem->value[12] = el_par_value_recurse("rm16",p->p_elem);
1305    c6t_elem->value[13] = el_par_value_recurse("rm21",p->p_elem);
1306    c6t_elem->value[14] = el_par_value_recurse("rm22",p->p_elem);
1307    c6t_elem->value[15] = el_par_value_recurse("rm23",p->p_elem);
1308    c6t_elem->value[16] = el_par_value_recurse("rm24",p->p_elem);
1309    c6t_elem->value[17] = el_par_value_recurse("rm25",p->p_elem);
1310    c6t_elem->value[18] = el_par_value_recurse("rm26",p->p_elem);
1311    c6t_elem->value[19] = el_par_value_recurse("rm31",p->p_elem);
1312    c6t_elem->value[20] = el_par_value_recurse("rm32",p->p_elem);
1313    c6t_elem->value[21] = el_par_value_recurse("rm33",p->p_elem);
1314    c6t_elem->value[22] = el_par_value_recurse("rm34",p->p_elem);
1315    c6t_elem->value[23] = el_par_value_recurse("rm35",p->p_elem);
1316    c6t_elem->value[24] = el_par_value_recurse("rm36",p->p_elem);
1317    c6t_elem->value[25] = el_par_value_recurse("rm41",p->p_elem);
1318    c6t_elem->value[26] = el_par_value_recurse("rm42",p->p_elem);
1319    c6t_elem->value[27] = el_par_value_recurse("rm43",p->p_elem);
1320    c6t_elem->value[28] = el_par_value_recurse("rm44",p->p_elem);
1321    c6t_elem->value[29] = el_par_value_recurse("rm45",p->p_elem);
1322    c6t_elem->value[30] = el_par_value_recurse("rm46",p->p_elem);
1323    c6t_elem->value[31] = el_par_value_recurse("rm51",p->p_elem);
1324    c6t_elem->value[32] = el_par_value_recurse("rm52",p->p_elem);
1325    c6t_elem->value[33] = el_par_value_recurse("rm53",p->p_elem);
1326    c6t_elem->value[34] = el_par_value_recurse("rm54",p->p_elem);
1327    c6t_elem->value[35] = el_par_value_recurse("rm55",p->p_elem);
1328    c6t_elem->value[36] = el_par_value_recurse("rm56",p->p_elem);
1329    c6t_elem->value[37] = el_par_value_recurse("rm61",p->p_elem);
1330    c6t_elem->value[38] = el_par_value_recurse("rm62",p->p_elem);
1331    c6t_elem->value[39] = el_par_value_recurse("rm63",p->p_elem);
1332    c6t_elem->value[40] = el_par_value_recurse("rm64",p->p_elem);
1333    c6t_elem->value[41] = el_par_value_recurse("rm65",p->p_elem);
1334    c6t_elem->value[42] = el_par_value_recurse("rm66",p->p_elem);
1335  }
1336  else if ((strcmp(p->base_name,"rfcavity") == 0))
1337  {
1338    c6t_elem = new_c6t_element(11,t_name,p->base_name);
1339    clean_c6t_element(c6t_elem);
1340    strcpy(c6t_elem->org_name,t_name);
1341    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1342    c6t_elem->value[1] = el_par_value_recurse("volt",p->p_elem);
1343    c6t_elem->value[4] = el_par_value_recurse("freq",p->p_elem);
1344    c6t_elem->value[5] = el_par_value_recurse("lag",p->p_elem);
1345    c6t_elem->value[7] = el_par_value_recurse("betrf",p->p_elem);
1346    c6t_elem->value[8] = el_par_value_recurse("pg",p->p_elem);
1347    c6t_elem->value[9] = el_par_value_recurse("shunt",p->p_elem);
1348    c6t_elem->value[10] = el_par_value_recurse("tfill",p->p_elem);
1349    c6t_elem->value[11] = el_par_value_recurse("harmon",p->p_elem);
1350  }
1351  else if ((strcmp(p->base_name,"crabcavity") == 0))
1352  {
1353    c6t_elem = new_c6t_element(12,t_name,p->base_name);
1354    clean_c6t_element(c6t_elem);
1355    strcpy(c6t_elem->org_name,t_name);
1356    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1357    c6t_elem->value[1] = el_par_value_recurse("volt",p->p_elem);
1358    c6t_elem->value[4] = el_par_value_recurse("freq",p->p_elem);
1359    c6t_elem->value[5] = el_par_value_recurse("lag",p->p_elem);
1360    c6t_elem->value[7] = el_par_value_recurse("betrf",p->p_elem);
1361    c6t_elem->value[8] = el_par_value_recurse("pg",p->p_elem);
1362    c6t_elem->value[9] = el_par_value_recurse("shunt",p->p_elem);
1363    c6t_elem->value[10] = el_par_value_recurse("tfill",p->p_elem);
1364    c6t_elem->value[11] = el_par_value_recurse("harmon",p->p_elem);
1365    c6t_elem->value[12] = el_par_value_recurse("tilt",p->p_elem);
1366  }
1367  else if ((strcmp(p->base_name,"dipedge") == 0))
1368  {
1369    c6t_elem = new_c6t_element(11,t_name,p->base_name);
1370    clean_c6t_element(c6t_elem);
1371    strcpy(c6t_elem->org_name,t_name);
1372    c6t_elem->value[1] = el_par_value("h",p->p_elem);
1373    c6t_elem->value[2] = el_par_value_recurse("e1",p->p_elem);
1374    c6t_elem->value[8] = el_par_value_recurse("hgap",p->p_elem);
1375    c6t_elem->value[9] = el_par_value_recurse("fint",p->p_elem);
1376  }
1377  else if ((strcmp(p->base_name,"solenoid") == 0))
1378  {
1379    c6t_elem = new_c6t_element(11,t_name,p->base_name);
1380    clean_c6t_element(c6t_elem);
1381    strcpy(c6t_elem->org_name,t_name);
1382    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1383    c6t_elem->value[2] = el_par_value_recurse("ks",p->p_elem);
1384    c6t_elem->value[3] = el_par_value_recurse("ksi",p->p_elem);
1385  }
1386  else if ((strcmp(p->base_name,"hacdipole") == 0))
1387  {
1388    c6t_elem = new_c6t_element(11,t_name,p->base_name);
1389    clean_c6t_element(c6t_elem);
1390    strcpy(c6t_elem->org_name,t_name);
1391    c6t_elem->value[2] = el_par_value_recurse("volt",p->p_elem);
1392    c6t_elem->value[3] = el_par_value_recurse("freq",p->p_elem);
1393    c6t_elem->value[4] = el_par_value_recurse("lag",p->p_elem);
1394  }
1395  else if ((strcmp(p->base_name,"vacdipole") == 0))
1396  {
1397    c6t_elem = new_c6t_element(11,t_name,p->base_name);
1398    clean_c6t_element(c6t_elem);
1399    strcpy(c6t_elem->org_name,t_name);
1400    c6t_elem->value[2] = el_par_value_recurse("volt",p->p_elem);
1401    c6t_elem->value[3] = el_par_value_recurse("freq",p->p_elem);
1402    c6t_elem->value[4] = el_par_value_recurse("lag",p->p_elem);
1403  }
1404  else if ((strcmp(p->base_name,"marker") == 0)   ||
1405           (strcmp(p->base_name,"instrument") == 0)    ||
1406           (strcmp(p->base_name,"placeholder") == 0)    ||
1407           (strcmp(p->base_name,"hmonitor") == 0) ||
1408           (strcmp(p->base_name,"vmonitor") == 0) ||
1409           (strcmp(p->base_name,"monitor") == 0))
1410  {
1411    c6t_elem = new_c6t_element(0,t_name,p->base_name);
1412    clean_c6t_element(c6t_elem);
1413    strcpy(c6t_elem->org_name,t_name);
1414    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1415  }
1416  else if ((strcmp(p->base_name,"rcollimator") == 0) ||
1417           (strcmp(p->base_name,"ecollimator") == 0))
1418  {
1419    c6t_elem = new_c6t_element(13,t_name,p->base_name);
1420    clean_c6t_element(c6t_elem);
1421    strcpy(c6t_elem->org_name,t_name);
1422    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1423    c6t_elem->value[12] = el_par_value_recurse("xsize",p->p_elem);
1424    c6t_elem->value[13] = el_par_value_recurse("ysize",p->p_elem);
1425  }
1426  else if((strcmp(p->base_name,"beambeam") == 0  ))
1427  {
1428    c6t_elem = new_c6t_element(17,t_name,p->base_name);
1429    clean_c6t_element(c6t_elem);
1430    strcpy(c6t_elem->org_name,t_name);
1431    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1432    c6t_elem->value[12] = el_par_value_recurse("xma",p->p_elem);
1433    c6t_elem->value[13] = el_par_value_recurse("yma",p->p_elem);
1434    c6t_elem->value[14] = el_par_value_recurse("sigx",p->p_elem);
1435    c6t_elem->value[15] = el_par_value_recurse("sigy",p->p_elem);
1436    c6t_elem->value[16] = el_par_value_recurse("charge",p->p_elem);
1437    c6t_elem->value[17] = 0.0; /* npart */
1438  }
1439  else if((strcmp(p->base_name,"elseparator") == 0  ))
1440  {
1441    c6t_elem = new_c6t_element(3,t_name,p->base_name);
1442    clean_c6t_element(c6t_elem);
1443    strcpy(c6t_elem->org_name,t_name);
1444    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1445    c6t_elem->value[2] = el_par_value_recurse("ex",p->p_elem);
1446    c6t_elem->value[3] = el_par_value_recurse("ey",p->p_elem);
1447  }
1448  else if (strcmp(p->base_name,"drift") == 0)
1449  {
1450    c6t_elem = new_c6t_element(0,t_name,p->base_name);
1451    clean_c6t_element(c6t_elem);
1452    strcpy(c6t_elem->org_name,t_name);
1453    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1454  }
1455  else if (strcmp(p->base_name,"rfmultipole") == 0)
1456  {
1457    int maxkn=0, maxks=0, maxpn=0, maxps=0;
1458    if ((index = name_list_pos("knl",p->p_elem->def->par_names))>-1)
1459    {
1460      kn_param = p->p_elem->def->par->parameters[index];
1461      maxkn=kn_param->double_array->curr;
1462    }
1463    if ((index = name_list_pos("ksl",p->p_elem->def->par_names))>-1)
1464    {
1465      ks_param = p->p_elem->def->par->parameters[index];
1466      maxks=ks_param->double_array->curr;
1467    }
1468    if ((index = name_list_pos("pnl",p->p_elem->def->par_names))>-1)
1469    {
1470      pn_param = p->p_elem->def->par->parameters[index];
1471      maxpn=kn_param->double_array->curr;
1472    }
1473    if ((index = name_list_pos("psl",p->p_elem->def->par_names))>-1)
1474    {
1475      ps_param = p->p_elem->def->par->parameters[index];
1476      maxps=ks_param->double_array->curr;
1477    }
1478    if (maxkn>3 || maxks>3) {
1479        printf("warning while converting rfmultipole: components beyond octupole are ignored\n");
1480    }
1481   
1482    /*
1483    ** In particular, for the RF multipoles:
1484    ** Name: any name
1485    ** type: 26, 27 or 28 for quadrupole, sextupole and octupole, respectively (note there are
1486    ** no skew elements yet).
1487    ** n1: Integrated multipole strength in the same units as the normal sixtrack elements
1488    ** n2: Frequency in MHz
1489    ** n3: RF phase in radians
1490    */ 
1491
1492    /*
1493    ** we have 9 parameters
1494    ** value[0] = el_par_value_recurse("l",p->p_elem);
1495    ** value[6] = el_par_value_recurse("tilt",p->p_elem);
1496    ** value[2] = frequency in MhZ
1497    ** value[3] = integrated quadrupole strength
1498    ** value[4] = integrated sextupole strength
1499    ** value[5] = integrated octupole strength
1500    ** value[1] = phase for quadrupole
1501    ** value[7] = phase for sextupole
1502    ** value[8] = phase for octupole
1503    */ 
1504   
1505    c6t_elem = new_c6t_element(14,t_name,p->base_name);
1506    clean_c6t_element(c6t_elem);
1507    strcpy(c6t_elem->org_name,t_name);
1508
1509    c6t_elem->value[0] = el_par_value_recurse("l",p->p_elem);
1510    c6t_elem->value[6] = el_par_value_recurse("tilt",p->p_elem);
1511    c6t_elem->value[2] = el_par_value_recurse("freq",p->p_elem);
1512    // normal components
1513    c6t_elem->value[3] = maxkn>1?(-kn_param->double_array->a[1]/1.0):0.0;
1514    c6t_elem->value[4] = maxkn>2?(-kn_param->double_array->a[2]/2.0):0.0;
1515    c6t_elem->value[5] = maxkn>3?(-kn_param->double_array->a[3]/6.0):0.0;
1516    c6t_elem->value[1] = maxpn>1?(pn_param->double_array->a[1]*2*M_PI):0.0;
1517    c6t_elem->value[7] = maxpn>2?(pn_param->double_array->a[2]*2*M_PI):0.0;
1518    c6t_elem->value[8] = maxpn>3?(pn_param->double_array->a[3]*2*M_PI):0.0;
1519    // skew component
1520    c6t_elem->value[9]  = maxks>1?(-ks_param->double_array->a[1]/1.0):0.0;
1521    c6t_elem->value[10] = maxks>2?(-ks_param->double_array->a[2]/2.0):0.0;
1522    c6t_elem->value[11] = maxks>3?(-ks_param->double_array->a[3]/6.0):0.0;
1523    c6t_elem->value[12] = maxps>1?(ps_param->double_array->a[1]*2*M_PI):0.0;
1524    c6t_elem->value[13] = maxps>2?(ps_param->double_array->a[2]*2*M_PI):0.0;
1525    c6t_elem->value[14] = maxps>3?(ps_param->double_array->a[3]*2*M_PI):0.0;
1526  }
1527  else
1528  {
1529    printf("Element not convertible! name= %s, basename = %s\n",p->name,p->base_name);
1530  }
1531
1532
1533  if (c6t_elem)
1534  {
1535    for (j = 0; j < c6t_elem->n_values; j++)
1536      if (fabs(c6t_elem->value[j]) < eps_12)
1537        c6t_elem->value[j] = 0.0;
1538    /* check to see if this has an aperture assigned, check for aperture flag */
1539    if ((aperture_flag)
1540        && (aper_param = return_param_recurse("apertype", p->p_elem)))
1541    {
1542      tag_aperture.apply=1;
1543      strcpy(tag_aperture.style,aper_param->string);
1544      strcpy(tag_aperture.name,t_name);
1545      strcat(tag_aperture.name,"_AP");
1546      if ((aper_param = return_param_recurse("aperture", p->p_elem)))
1547      {
1548        if (aper_param->expr_list != NULL)
1549          update_vector(aper_param->expr_list, aper_param->double_array);
1550        j=3;
1551        if (aper_param->double_array->curr<3) j=aper_param->double_array->curr;
1552        for(i=0;i<j;i++)
1553        {
1554          tag_aperture.value[i] = aper_param->double_array->a[i];
1555        }
1556      }
1557    }
1558
1559    /* name used has to be without occ_cnt as this is added
1560       (only 1) in tab_name_code
1561       !!! fixed FS 17.08.2004 !!! */
1562    c6t_elem->twtab_row = my_table_row(current_sequ->tw_table,t_name);
1563  }
1564
1565  return c6t_elem;
1566}
1567
1568#if 0 // not used
1569static void
1570dump_c6t_element(struct c6t_element* el)
1571{
1572  int j;
1573  char pname[24] = "NULL", nname[24] = "NULL";
1574  if (el->previous != NULL) strcpy(pname, el->previous->name);
1575  if (el->next != NULL) strcpy(nname, el->next->name);
1576  printf("name: %s  base: %s  position: %f\n", el->name, el->base_name,
1577         el->position);
1578  printf("  names of - previous: %s  next: %s  equiv: %s\n",
1579         pname, nname, el->equiv->name);
1580  printf("  flag: %d  force: %d  split: %d keep: %d values: %d f_errors: %d\n",
1581         el->flag, el->force, el->split, el->keep_in,el->n_values, el->nf_err);
1582  for (j = 0; j < el->n_values; j++)
1583  {
1584    printf("%e ", el->value[j]);
1585    if ((j+1)%5 == 0 || j+1 == el->n_values)  printf("\n");
1586  }
1587  if (el->nf_err)  puts("field errors");
1588  for (j = 0; j < el->nf_err; j++)
1589  {
1590    printf("%e ", el->p_fd_err->a_dble[j]);
1591    if ((j+1)%5 == 0 || j+1 == el->nf_err)  printf("\n");
1592  }
1593  if (el->na_err)  puts("alignment errors");
1594  for (j = 0; j < el->na_err; j++)
1595  {
1596    printf("%e ", el->p_al_err->a_dble[j]);
1597    if ((j+1)%5 == 0 || j+1 == el->na_err)  printf("\n");
1598  }
1599}
1600#endif
1601
1602#if 0 // not used
1603static void
1604dump_c6t_sequ(int level)
1605{
1606  double suml = zero;
1607  puts("+++++++++ dump sequence +++++++++");
1608  current_element = first_in_sequ;
1609  while (current_element != NULL)
1610  {
1611    suml += current_element->value[0];
1612    if (level > 2)  dump_c6t_element(current_element);
1613    else if (level > 1)  gnu_file(current_element);
1614    else if (level > 0 && strcmp(current_element->base_name, "drift") != 0)
1615      printf("%s: %s at = %f\n", current_element->name,
1616             current_element->equiv->name, current_element->position);
1617    current_element = current_element->next;
1618  }
1619  printf("=== sum of element length: %f\n", suml);
1620}
1621#endif
1622
1623#if 0 // not used
1624static void
1625dump_types(int flag)
1626{
1627  int i, j;
1628
1629  puts("+++++++++ dump types +++++++++");
1630  for (i = 0; i < types.curr; i++)
1631  {
1632    puts(types.member[i]->base_name);
1633    for (j = 0; j < types.member[i]->curr; j++)
1634      printf("       %s  %f\n", types.member[i]->elem[j]->name,
1635             types.member[i]->elem[j]->value[0]);
1636    if (flag > 0) dump_c6t_element(types.member[i]->elem[j]);
1637  }
1638}
1639#endif
1640
1641static void
1642equiv_elem(void)
1643{
1644  int i, j, k;
1645  struct c6t_element *el, *eln;
1646
1647  for (i = 0; i < types.curr; i++)  /* loop over base types */
1648  {
1649    for (j = 0; j < types.member[i]->curr; j++) /* loop over el. in type */
1650    {
1651      el = types.member[i]->elem[j];
1652      if (el->flag > 0)  /* all others ignored */
1653      {
1654        if (el->equiv == el /* not yet equivalenced */
1655            && strcmp(el->base_name,"marker") != 0) /* do not touch markers */
1656        {
1657          for (k = j+1; k < types.member[i]->curr; k++)
1658          {
1659            eln = types.member[i]->elem[k];
1660            if (eln->flag > 0
1661                && eln->equiv == eln
1662                && ident_el(el, eln) == 0
1663                && eln->nf_err == el->nf_err
1664                && strcmp(eln->base_name,"beambeam") != 0
1665                && strcmp(eln->base_name,"marker") != 0
1666                && strstr(eln->base_name,"colli") == NULL)
1667              eln->equiv = el;
1668          }
1669        }
1670      }
1671    }
1672  }
1673}
1674
1675static int
1676f34_values(struct c6t_element* el, int* flags, double* values)
1677{
1678  int i, j, np, nd, cnt = 0;
1679  double pow, tmp[FIELD_MAX];
1680  for (i = 0; i < FIELD_MAX; i++)
1681  {
1682    tmp[i] = zero;
1683    j = i + 12;
1684    if (j < el->n_values && el->value[j] != zero)
1685    {
1686      if (el->value[0] != zero) tmp[i] += el->value[0] * el->value[j];
1687      else tmp[i] += el->value[j];
1688    }
1689    if (i < el->nf_err && el->p_fd_err->a_dble[i] != zero)
1690      tmp[i] += el->p_fd_err->a_dble[i];
1691  }
1692  for (i = 3; i < FIELD_MAX; i++)
1693  {
1694    if (tmp[i] != zero)
1695    {
1696      np = i / 2 + 1;
1697      nd = 1; for (j = 2; j < np; j++)  nd *= j;
1698      pow = nd;
1699      pow = power_of(ten, 6-3*np) / pow;
1700      if (i%2 == 0)
1701      {
1702        flags[cnt] = np; if (el->npole_sign) pow = -pow;
1703      }
1704      else           flags[cnt] = -np;
1705      values[cnt++] = pow * tmp[i];
1706    }
1707  }
1708  return cnt;
1709}
1710
1711static struct block*
1712get_block_equiv(struct block* current)
1713{
1714  struct block* p = first_block;
1715  int i, k;
1716  while (p != current)
1717  {
1718    if (current->elements->curr == p->elements->curr)
1719    {
1720      k = 0;
1721      for (i = 0; i < current->elements->curr; i++)
1722      {
1723        if (strcmp(current->elements->elem[i]->equiv->name,
1724                   p->elements->elem[i]->equiv->name) == 0) k++;
1725      }
1726      if (k == current->elements->curr)  return p;
1727    }
1728    p = p->next;
1729  }
1730  return p;
1731}
1732
1733static void
1734get_args(struct in_cmd* my_cmd)
1735{
1736  int tmp_max_mult_ord;
1737  double tmp_ref_def;
1738  if ((aperture_flag = command_par_value("aperture", my_cmd->clone)))
1739    put_info("c6t - aperture flag selected","");
1740  if ((cavall_flag = command_par_value("cavall", my_cmd->clone)))
1741    put_info("c6t - cavall flag selected","");
1742  if ((mult_auto_off = command_par_value("mult_auto_off", my_cmd->clone)))
1743    put_info("c6t - mult_auto_off flag selected","");
1744  if ((split_flag = command_par_value("split", my_cmd->clone)))
1745    put_info("c6t - split flag selected","");
1746  if ((tmp_max_mult_ord = command_par_value("max_mult_ord", my_cmd->clone))>0)
1747  {
1748    max_mult_ord = tmp_max_mult_ord;
1749    printf("max_mult_ord set to : %d\n",max_mult_ord);
1750  }
1751  if ((tmp_ref_def = command_par_value("radius", my_cmd->clone))>0.)
1752  {
1753//    radius_flag = 1; // not_used
1754    ref_def = tmp_ref_def;
1755    printf("Reference radius set to : %f\n",ref_def);
1756  }
1757}
1758
1759static void
1760get_error_refs(struct c6t_element* el)
1761{
1762  int i;
1763  double tmp;
1764  i = 12 + 2 * el->mult_order;
1765  if (i+1 < el->n_values)
1766  {
1767    tmp = fabs(el->value[i]) > fabs(el->value[i+1]) ?
1768      fabs(el->value[i]) : fabs(el->value[i+1]);
1769  }
1770  else if(i < el->n_values) tmp = fabs(el->value[i]);
1771  else tmp = 1;
1772  if (tmp == zero) tmp = 1;
1773  if (el->mult_order==0)
1774  {
1775    el->ref_delta = c1p3 * tmp * power_of(el->ref_radius, el->mult_order);
1776  }
1777  else
1778  {
1779    el->ref_delta = 0;
1780  }
1781}
1782
1783static int
1784get_flag(struct c6t_element* el, struct type_info* type)
1785{
1786
1787  if (el->value[0] == zero)
1788  {
1789    if (type->flag_1 == 4) return in_keep_list(el);
1790    else return type->flag_1;
1791  }
1792  if (el->n_values < 7) return type->flag_2;
1793  else return (el->value[6] == 0 ? type->flag_2 : type->flag_3);
1794}
1795
1796#if 0 // not used
1797static struct c6t_element*
1798get_from_ellist(char* name, char* type)
1799{
1800  int i, j;
1801
1802#ifdef _call_tree_
1803  puts("+++++++ get_from_ellist");
1804#endif
1805  for (i = 0; i < types.curr; i++)
1806  {
1807    if (strcmp(types.member[i]->base_name, type) == 0)
1808    {
1809      for (j = 0; j < types.member[i]->curr; j++) /* loop over el. in type */
1810      {
1811        if (strcmp(types.member[i]->elem[j]->name, name) == 0)
1812          return types.member[i]->elem[j];
1813      }
1814    }
1815  }
1816  return NULL;
1817}
1818#endif
1819
1820static void
1821get_multi_refs(void)
1822{
1823  int i;
1824  for (i = 0; i < types.curr; i++)  /* loop over base types */
1825  {
1826    if (strcmp(types.member[i]->base_name, "multipole") == 0)
1827    {
1828      multi_type = i;  break;
1829    }
1830  }
1831}
1832
1833static int
1834get_next_name(char* name, char acro)
1835{
1836  int j, k = -1;
1837
1838  for (j = 0; j < acro_occ; j++)
1839    if (acro_list[j] == acro)  k = j;
1840
1841  if (k < 0) {
1842    k = acro_occ++; acro_list[k] = acro; acro_cnt[k] = 0;
1843  }
1844
1845  sprintf(name, "%c_c6t_%d", acro, ++acro_cnt[k]);
1846
1847  return 1;
1848}
1849
1850#if 0 // not used
1851static void
1852gnu_file(struct c6t_element* el)
1853{
1854  double el_start, el_end;
1855
1856  el_start = el->position - el->value[0] / two;
1857  el_end   = el->position + el->value[0] / two;
1858  printf("%s %e 0.\n", el->name, el_start);
1859  printf("%s %e 1.\n", el->name, el_start);
1860  printf("%s %e 1.\n", el->name, el_end);
1861  printf("%s %e 0.\n", el->name, el_end);
1862}
1863#endif
1864
1865static void
1866grow_ellist( /* doubles object list size */
1867  struct c6t_el_list* p)
1868{
1869  struct c6t_element** p_loc = p->elem;
1870  int j, new = 2*p->max;
1871  char rout_name[] = "c6t:grow_ellist";
1872
1873#ifdef _call_tree_
1874  puts("+++++++ grow_ellist");
1875#endif
1876  p->max = new;
1877  p->elem = (struct c6t_element**) mycalloc(rout_name,new, sizeof(struct c6t_element*));
1878  for (j = 0; j < p->curr; j++) p->elem[j] = p_loc[j];
1879  myfree(rout_name, p_loc);
1880}
1881
1882static int
1883ident_el(struct c6t_element* el1, struct c6t_element* el2)
1884{
1885  double s, tolerance = eps_9;
1886  int j, m = el1->n_values < el2->n_values ? el1->n_values : el2->n_values;
1887  if (el1->mult_order != el2->mult_order)  return 1;
1888  if (el1->ref_radius != el2->ref_radius)  return 2;
1889  if (el1->ref_delta  != el2->ref_delta)   return 2;
1890  if (el1->keep_in  != el2->keep_in)       return 6;
1891  for (j = 0; j < m; j++)
1892  {
1893    s = fabs(el1->value[j]) + fabs(el2->value[j]);
1894    if (s > zero
1895        && fabs(el1->value[j] - el2->value[j])/s > tolerance) return 3;
1896  }
1897  if (m != el1->n_values)
1898  {
1899    for (j = m; j < el1->n_values; j++)
1900      if (el1->value[j] != zero) return 4;
1901  }
1902  else if (m != el2->n_values)
1903  {
1904    for (j = m; j < el2->n_values; j++)
1905      if (el2->value[j] != zero) return 5;
1906  }
1907  return 0;
1908}
1909
1910static int
1911ident_zero(struct c6t_element* el)
1912{
1913  int j;
1914  for (j = 12; j < el->n_values; j++)
1915    if (el->value[j] != zero) return 1;
1916  return 0;
1917}
1918
1919static int
1920in_keep_list(struct c6t_element* el)
1921{
1922  char temp[24];
1923  int j;
1924
1925  strcpy(temp, el->name); stolower(temp);
1926  for (j = 0; j < MM_KEEP; j++)
1927  {
1928    if (strncmp(temp, keep_these[j], strlen(keep_these[j])) == 0) return 2;
1929  }
1930  return 0;
1931}
1932
1933static void
1934invert_normal(int count, double array[])
1935{
1936  int i;
1937  for (i = 0; i < (count+1)/2; i++)  array[2*i] = -array[2*i];
1938}
1939
1940#if 0 // not used
1941static void
1942invert_skew(int count, double array[])
1943{
1944  int i;
1945  for (i = 0; i < count/2; i++)  array[2*i+1] = -array[2*i+1];
1946}
1947#endif
1948
1949static void
1950link_c6t_in_front(struct c6t_element* new, struct c6t_element* el)
1951{
1952  if (el->previous == NULL) first_in_sequ = new;
1953  else el->previous->next = new;
1954  new->previous = el->previous; new->next = el;
1955  el->previous = new;
1956}
1957
1958static void
1959link_behind(struct c6t_element* new, struct c6t_element* el)
1960{
1961  if (el->next == NULL)
1962  {
1963//    last_in_sequ = new; // not used
1964    last_in_sequ_org = new;
1965  }
1966  else el->next->previous = new;
1967  new->previous = el; new->next = el->next;
1968  el->next = new;
1969}
1970
1971/* removed, replaced by stolower from mad_str.h
1972void lower(char* s)
1973{
1974  char* cp = s;
1975  while(*cp != '\0')
1976  {
1977    *cp = (char) tolower((int)*cp); cp++;
1978  }
1979}
1980*/
1981
1982static struct c6t_element*
1983make_c6t_element(struct node* p)
1984{
1985  struct c6t_element *tmp_element;
1986  if ((tmp_element = convert_madx_to_c6t(p)))
1987  {
1988    prev_element = current_element;
1989    current_element = tmp_element;
1990    if (elem_cnt++ == 0) first_in_sequ = current_element;
1991    else                 prev_element->next = current_element;
1992    current_element->previous = prev_element;
1993    current_element->next = NULL;
1994  }
1995  return tmp_element;
1996}
1997
1998/* this is taken from doom but all it does is malloc the structure and fill
1999   it in */
2000static struct object*
2001make_obj(   /* creates a new object */
2002  char* key,
2003  int vlint,       /* length of integer array */
2004  int vldble,      /* length of double array */
2005  int vlchar,      /* length of char array */
2006  int vlpobj)      /* length of object pointer array */
2007{
2008  struct object* p;
2009  char rout_name[] = "c6t:make_obj";
2010
2011#ifdef _call_tree_
2012  put_info("+++++++ make_object","");
2013#endif
2014  p = (struct object*)  mycalloc(rout_name, 1, sizeof(struct object));
2015  mycpy(p->key, key);
2016  if ((p->l_int = vlint) > 0)
2017    p->a_int = (int*) mymalloc(rout_name, p->l_int * sizeof(int));
2018  if ((p->l_dble = vldble) > 0)
2019    p->a_dble = (double*) mymalloc(rout_name, p->l_dble * sizeof(double));
2020  if ((p->l_char = vlchar) > 0)
2021    p->a_char = (char*) mymalloc(rout_name, p->l_char);
2022  if ((p->l_obj  = vlpobj) > 0)
2023  {
2024    p->p_obj = (struct object**) mycalloc(rout_name, p->l_obj, sizeof(struct object*));
2025    p->names = (char**) mycalloc(rout_name, p->l_obj, sizeof(char*));
2026  }
2027  p->parent = NULL;
2028  /*      my_time(); */
2029  /*      p->ma_time = major_time; p->mi_time = minor_time; */
2030  return p;
2031}
2032
2033static void
2034make_multipole(struct c6t_element* el)
2035{
2036  if (el->force > 0) /* multiply forces with length */
2037    app_factor(el->value[0], &el->value[12], el->n_values-12);
2038  el->value[0] = zero; /* set element length to zero */
2039  el->flag = 2;
2040  remove_from_ellist(el);
2041  strcpy(el->base_name, "multipole");
2042  add_to_ellist(el);
2043}
2044
2045static void
2046mod_errors(void)
2047{
2048  current_element = first_in_sequ;
2049  while (current_element != NULL)
2050  {
2051    if (current_element->nf_err > 0)
2052      invert_normal(current_element->nf_err, current_element->p_fd_err->a_dble);
2053    current_element = current_element->next;
2054  }
2055}
2056
2057static void
2058mod_lcavity(struct c6t_element* p)
2059{
2060  total_voltage += p->value[1];   /* accumulate voltage */
2061}
2062
2063static void
2064mod_multipole(struct c6t_element* p)
2065{
2066  supp_small_comp(p);
2067}
2068
2069static void
2070mod_rfmultipole(struct c6t_element* p)
2071{
2072  supp_small_comp(p);
2073}
2074
2075static void
2076mod_octupole(struct c6t_element* p)
2077{
2078  supp_small_comp(p);
2079}
2080
2081static void
2082mod_quadrupole(struct c6t_element* p)
2083{
2084  supp_small_comp(p);
2085}
2086
2087static void
2088mod_rbend(struct c6t_element* p)
2089{
2090  supp_small_comp(p);
2091  /* commented out in the original c6t (MEH)
2092     set length to arc length
2093     if (fabs(p->a_dble[0]) > eps_9 && fabs(p->a_dble[10]) > eps_9)
2094     p->a_dble[0] *= p->a_dble[10] / (two * sin(p->a_dble[10]/two));
2095  */
2096  /* change 1/r to value for straight line */
2097  p->value[1] = p->value[10] / p->value[0];
2098}
2099
2100static void
2101mod_rfcavity(struct c6t_element* p)
2102{
2103  total_voltage += p->value[1];  /* accumulate voltage */
2104}
2105
2106static void
2107mod_crabcavity(struct c6t_element* p)
2108{
2109  total_voltage += p->value[1];  /* accumulate voltage */
2110}
2111
2112static void
2113mod_sextupole(struct c6t_element* p)
2114{
2115  supp_small_comp(p);
2116  /* supress tilt angle (not component !) */
2117  /*    p->value[6] = zero; */
2118}
2119
2120static void
2121multi_loop(void)
2122{
2123  int i, j, nup;
2124  struct c6t_element* el;
2125  for (i = 0; i < types.curr; i++)  /* loop over base types */
2126  {
2127    if (strcmp(types.member[i]->base_name, "multipole") == 0)
2128    {
2129      nup = types.member[i]->curr;
2130      for (j = 0; j < nup; j++) /* loop over mutipoles */
2131      {
2132        el = types.member[i]->elem[j];
2133        pre_multipole(el);
2134      }
2135    }
2136  }
2137}
2138
2139static struct block*
2140new_block(void)
2141{
2142  struct block* p;
2143  char rout_name[] = "c6t:new_block";
2144  p = (struct block*) mycalloc(rout_name, 1, sizeof(struct block));
2145  sprintf(p->name, "BLOC%d", block_count++);
2146  return p;
2147}
2148
2149static struct c6t_element*
2150new_c6t_element(int size, char* name, char* base)
2151{
2152  struct c6t_element* p;
2153  char rout_name[] = "c6t:new_c6t_element";
2154  p = (struct c6t_element*) mycalloc(rout_name, 1, sizeof(struct c6t_element));
2155  strcpy(p->name, name);
2156  p->equiv = p;
2157  strcpy(p->base_name, base);
2158  p->value = (double*) mycalloc(rout_name,++size,sizeof(double));
2159  p->n_values = size;
2160  p->do_not_free = 0;
2161  return p;
2162}
2163
2164static void
2165post_multipoles(void) /* post equiv. treatment of multipoles */
2166{
2167  int i, j;
2168  struct c6t_element *el, *eln;
2169  struct object *p;
2170
2171  if (multi_type > -1) /* there are multipoles */
2172  {
2173    for (j = 0; j < types.member[multi_type]->curr; j++)
2174    {
2175      el = types.member[multi_type]->elem[j]; eln = el->equiv;
2176      if (el->nf_err > 0)
2177      {
2178        eln->mult_order = el->mult_order;
2179        eln->ref_radius = el->ref_radius;
2180        if (eln->p_fd_err == NULL)
2181        {
2182          eln->p_fd_err = p_err_zero;
2183          eln->nf_err = FIELD_MAX;
2184        }
2185        if (eln->nf_err < el->nf_err)
2186        {
2187          strcpy(tmp_name, eln->p_fd_err->key);
2188          p = eln->p_fd_err;
2189          eln->p_fd_err = make_obj(tmp_name, 0, el->nf_err, 0, 0);
2190          /* first initialise */
2191          for (i = 0; i < el->nf_err; i++)
2192            eln->p_fd_err->a_dble[i] = 0.0;
2193          for (i = 0; i < eln->nf_err; i++)
2194            eln->p_fd_err->a_dble[i] = p->a_dble[i];
2195          eln->nf_err = el->nf_err;
2196        }
2197      }
2198    }
2199  }
2200}
2201
2202static double
2203power_of(double d, int i)
2204{
2205  int j;
2206  double tmp = 1;
2207  if (i > 0)
2208  {
2209    for (j = 0; j < i; j++)  tmp *= d;
2210    return tmp;
2211  }
2212  else if(i < 0)
2213  {
2214    for (j = 0; j < -i; j++)  tmp *= d;
2215    return 1./tmp;
2216  }
2217  else return 1.;
2218}
2219
2220static void
2221pre_multipole(struct c6t_element* el) /* pre-process multipoles */
2222{
2223  /*
2224    1. first count multipole components < decapole (cnt)
2225    if   cnt == 1:
2226    if    dipole, set el->nc_pos
2227    else  make n_pole, insert in front, zero el->component
2228    2. add all comp. > dipole + errors
2229    if all zero:
2230    straight quad, yank element
2231    else  set error count to zero, keep n-pole
2232    else
2233    put sum into errors, zero all el->components
2234  */
2235  int i, last_nzero = -1, nz_cnt = 0, cnt = 0, s_pole = 0, ndmax;
2236  // int n_pole, // not used
2237  int low, new_el_t;
2238  struct c6t_element *new_el = NULL;
2239  char t_list[5][12] = {"dipole", "quadrupole", "sextupole", "octupole",
2240                        "decapole"};
2241
2242  ndmax = el->n_values > 22 ? 22 : el->n_values;
2243  for (i = 12; i < ndmax; i++)
2244  {
2245    if (el->value[i] != zero)
2246    {
2247      s_pole = i; cnt++;
2248    }
2249  }
2250  if ((cnt == 1) || (el->value[12]!=zero) || (el->value[13]!=zero))
2251  {
2252    if (el->value[12]!=zero) { s_pole=12; cnt=1; }
2253    if (el->value[13]!=zero) { s_pole=13; cnt=1; }
2254    if ((new_el_t = (s_pole-12)/2) == 0)  el->nc_pos = s_pole;
2255    else
2256    {
2257      get_next_name(tmp_name, t_list[new_el_t][0]);
2258      new_el = new_c6t_element(s_pole+1, tmp_name, t_list[new_el_t]);
2259      new_el->do_not_free = 1;
2260      for (i = 0; i <= s_pole; i++) new_el->value[i] = el->value[i];
2261      for (i = 12; i <= s_pole; i++) el->value[i] = 0;
2262      new_el->flag = s_pole > 13 ? 2 : 1; new_el->npole_sign = 1;
2263      new_el->keep_in = el->keep_in;
2264      new_el->position = el->position;
2265      new_el->twtab_row = el->twtab_row;
2266      new_el->na_err = el->na_err; /* el->na_err = 0; */
2267      new_el->p_al_err = el->p_al_err; /*  el->p_al_err = NULL; */
2268      new_el->tilt_err = el->tilt_err; /*  keep tilt info */
2269      link_c6t_in_front(new_el, el);
2270      add_to_ellist(new_el);
2271      strcpy(tmp_name, el->name); strcpy(el->name, new_el->name);
2272      strcpy(new_el->name, tmp_name);
2273    }
2274  }
2275  for (i = 0; i < FIELD_MAX; i++) tmp_buff[i] = zero;
2276  low = cnt == 1 ? 2 : 0;
2277  for (i = low; i < el->n_values-14; i++)
2278    tmp_buff[i] = el->value[12+i];
2279  for (i = 0; i < el->nf_err; i++) tmp_buff[i] += el->p_fd_err->a_dble[i];
2280  for (i = 0; i < FIELD_MAX; i++) if (tmp_buff[i] != zero)
2281  {
2282    last_nzero = i; nz_cnt++;
2283  }
2284  // n_pole = last_nzero / 2; // not used
2285  el->rad_length = el->value[11];
2286  if (last_nzero < 0)  /* all quad+ components and all errors = zero */
2287  {
2288    el->nf_err = 0;
2289    if (el->keep_in == 0 && (s_pole == 0 || s_pole > 13)) yank(el);
2290    else                            el->nc_pos = s_pole;
2291  }
2292  else  /* element becomes multipole, all comp. above dipole -> errors */
2293  {
2294    el->nc_pos = s_pole > 13 ? 0 : s_pole;
2295    if (el->ref_delta == zero)
2296    {
2297      el->ref_delta = c1p3;
2298      el->ref_radius = ref_def;
2299      el->mult_order = 1;
2300    }
2301    if (++last_nzero > el->nf_err)
2302    {
2303      if (el->p_fd_err != NULL) strcpy(tmp_name, el->p_fd_err->key);
2304      else  sprintf(tmp_name,"%s_arfa", el->name);
2305      el->nf_err = last_nzero;
2306      el->p_fd_err = make_obj(tmp_name, 0, el->nf_err, 0, 0);
2307    }
2308    for (i = 0; i < el->nf_err; i++) el->p_fd_err->a_dble[i] = tmp_buff[i];
2309    for (i = 14; i < el->n_values; i++)  el->value[i] = zero;
2310  }
2311}
2312
2313static void
2314pro_elem(struct node* cnode)
2315/* processes one element, makes linked list */
2316/* converts MADX linked list to c6t internal linked list */
2317{
2318  int i;
2319  char t_key[KEY_LENGTH];
2320  char ap_name[255];
2321  struct c6t_element *tag_element;
2322  double tmp_vk,tmp_hk;
2323
2324  tag_aperture.apply=0;
2325  /* do the fiddly conversion but skip element if not needed */
2326  if (make_c6t_element(cnode) == NULL) return;
2327
2328  if (strcmp(cnode->base_name, "rbend") == 0) mod_rbend(current_element);
2329  else if (strcmp(cnode->base_name, "lcavity") == 0) mod_lcavity(current_element);
2330  else if (strcmp(cnode->base_name, "multipole") == 0) mod_multipole(current_element);
2331  else if (strcmp(cnode->base_name, "octupole") == 0) mod_octupole(current_element);
2332  else if (strcmp(cnode->base_name, "quadrupole") == 0) mod_quadrupole(current_element);
2333  else if (strcmp(cnode->base_name, "sextupole") == 0) mod_sextupole(current_element);
2334  else if (strcmp(cnode->base_name, "rfcavity") == 0) mod_rfcavity(current_element);
2335  else if (strcmp(cnode->base_name, "crabcavity") == 0) mod_crabcavity(current_element);
2336  else if (strcmp(cnode->base_name, "rfmultipole") == 0) mod_rfmultipole(current_element);
2337 
2338  if (strstr(cnode->base_name, "kicker") || strstr(cnode->base_name, "tkicker"))
2339  {
2340    if (cnode->p_elem)
2341    {
2342      tmp_hk = el_par_value("hkick",cnode->p_elem); current_element->value[12] += tmp_hk;
2343      tmp_vk = el_par_value("vkick",cnode->p_elem); current_element->value[13] += tmp_vk;
2344    }
2345    current_element->value[12] += cnode->chkick;
2346    current_element->value[13] += cnode->cvkick;
2347  }
2348
2349  strcpy(current_element->org_name, current_element->name);
2350  current_element->occ_cnt = cnode->occ_cnt;
2351  if (cnode->occ_cnt > 1)  /* add occurence count to name */
2352  {
2353    sprintf(t_key, "%s+%d", current_element->name,cnode->occ_cnt);
2354    strcpy(current_element->name, t_key);
2355  }
2356  current_element->position = cnode->position;
2357  add_to_ellist(current_element);
2358
2359  /* errors in MADX are stored in the same way as c6t */
2360  if (cnode->p_fd_err)
2361  {
2362    field_cnt++;
2363    current_element->nf_err = cnode->p_fd_err->curr;
2364    current_element->p_fd_err = make_obj("FDDUM",0,FIELD_MAX,0,0);
2365    current_element->p_fd_err->c_dble = cnode->p_fd_err->curr;
2366    for (i=0;i<cnode->p_fd_err->curr;i++)
2367      current_element->p_fd_err->a_dble[i] = cnode->p_fd_err->a[i];
2368    for (i=12;((i<current_element->n_values) && (current_element->value[i]==0));i+=2);
2369    if (i>current_element->n_values) i-=2;
2370    current_element->mult_order = i-12;
2371    current_element->ref_radius = ref_def;
2372    get_error_refs(current_element);
2373  }
2374  if (cnode->p_al_err)
2375  {
2376    align_cnt++;
2377    current_element->na_err = cnode->p_al_err->curr;
2378    current_element->p_al_err = make_obj("ALDUM",0,ALIGN_MAX,0,0);
2379    current_element->p_al_err->c_dble = cnode->p_al_err->curr;
2380    for (i=0;i<cnode->p_al_err->curr;i++)
2381      current_element->p_al_err->a_dble[i] = cnode->p_al_err->a[i];
2382  }
2383  /* if we have a tilt set the flag */
2384  ///// AL: WARNING: NOT ALL ELEMENTS STORE "TILT" in value[6]
2385  if (current_element->n_values >= 7 && fabs(current_element->value[6]) > zero)
2386  {
2387    align_cnt++;
2388    current_element->tilt_err = 1;
2389  }
2390  else
2391  {
2392    current_element->tilt_err = 0;
2393  }
2394  /* add aperture element if necessary */
2395  if (tag_aperture.apply==1)
2396  {
2397    if (strstr(tag_aperture.style,"circle")!=NULL)
2398    {
2399      tag_element = create_aperture(tag_aperture.name,"EL",
2400                                    tag_aperture.value[0],tag_aperture.value[0],cnode->p_al_err);
2401      tag_element->previous = current_element;
2402      tag_element->next = current_element->next;
2403      current_element->next = tag_element;
2404      prev_element = current_element;
2405      current_element = tag_element;
2406      current_element->position = cnode->position;
2407      add_to_ellist(current_element);
2408    }
2409    else if (strstr(tag_aperture.style,"ellipse")!=NULL)
2410    {
2411      tag_element = create_aperture(tag_aperture.name,"EL",
2412                                    tag_aperture.value[0],tag_aperture.value[1],cnode->p_al_err);
2413      tag_element->previous = current_element;
2414      tag_element->next = current_element->next;
2415      current_element->next = tag_element;
2416      prev_element = current_element;
2417      current_element = tag_element;
2418      current_element->position = cnode->position;
2419      add_to_ellist(current_element);
2420    }
2421    else if (strstr(tag_aperture.style,"rectangle")!=NULL)
2422    {
2423      tag_element = create_aperture(tag_aperture.name,"RE",
2424                                    tag_aperture.value[0],tag_aperture.value[1],cnode->p_al_err);
2425      tag_element->previous = current_element;
2426      tag_element->next = current_element->next;
2427      current_element->next = tag_element;
2428      prev_element = current_element;
2429      current_element = tag_element;
2430      current_element->position = cnode->position;
2431      add_to_ellist(current_element);
2432    }
2433    else if (strstr(tag_aperture.style,"lhcscreen")!=NULL)
2434    {
2435      strcpy(ap_name,tag_aperture.name); strcat(ap_name,"1");
2436      tag_element = create_aperture(ap_name,"EL",
2437                                    tag_aperture.value[0],tag_aperture.value[0],cnode->p_al_err);
2438      tag_element->previous = current_element;
2439      tag_element->next = current_element->next;
2440      current_element->next = tag_element;
2441      prev_element = current_element;
2442      current_element = tag_element;
2443      current_element->position = cnode->position;
2444      add_to_ellist(current_element);
2445      strcpy(ap_name,tag_aperture.name); strcat(ap_name,"2");
2446      tag_element = create_aperture(ap_name,"RE",
2447                                    tag_aperture.value[1],tag_aperture.value[2],cnode->p_al_err);
2448      tag_element->previous = current_element;
2449      tag_element->next = current_element->next;
2450      current_element->next = tag_element;
2451      prev_element = current_element;
2452      current_element = tag_element;
2453      current_element->position = cnode->position;
2454      add_to_ellist(current_element);
2455    }
2456    else
2457    {
2458      warning("general aperture element not supported in sixtrack",tag_aperture.name);
2459    }
2460  }
2461}
2462
2463static void
2464read_sequ(void)
2465{
2466  struct node* cnode;
2467  if ((current_sequ->n_nodes) > 0)  sequ_start = current_sequ->ex_start->position;
2468  cnode=current_sequ->ex_start;
2469  while(cnode && cnode!=current_sequ->ex_end)
2470  {
2471    if (strstr(cnode->name,"$")==NULL) pro_elem(cnode);
2472    cnode=cnode->next;
2473  }
2474  sequ_end = current_sequ->ex_end->position;
2475  sequ_length = sequ_end - sequ_start;
2476//  last_in_sequ = current_element; // not used
2477  last_in_sequ_org = current_element;
2478  put_info("MADX sequence converted to c6t internal.","");
2479}
2480
2481/* removes element from correct object list */
2482static void
2483remove_from_ellist(struct c6t_element* p_elem)
2484{
2485  int i, j;
2486
2487#ifdef _call_tree_
2488  puts("+++++++ remove_from_ellist");
2489#endif
2490  for (i = 0; i < types.curr; i++)
2491  {
2492    if (strcmp(types.member[i]->base_name, p_elem->base_name) == 0)
2493    {
2494      for (j = 0; j < types.member[i]->curr; j++) /* loop over el. in type */
2495      {
2496        if (types.member[i]->elem[j] == p_elem)
2497        {
2498          types.member[i]->elem[j]
2499            = types.member[i]->elem[--types.member[i]->curr];
2500          return;
2501        }
2502      }
2503    }
2504  }
2505}
2506
2507static void
2508replace_c6t(struct c6t_element* old, struct c6t_element* new)
2509{
2510  if (old->previous != NULL)  old->previous->next = new;
2511  new->previous = old->previous;
2512  if (old->next != NULL)      old->next->previous = new;
2513  new->next = old->next;
2514  old->flag = 0;
2515}
2516
2517static void
2518split(void)
2519{
2520  if (split_list != NULL)
2521  {
2522    int i;
2523    for (i = 0; i < split_list->curr; i++)
2524    {
2525      struct c6t_element *el = split_list->elem[i];
2526      if (el->flag == 1
2527          && (split_flag != 0 || el->nf_err > 0)) split_special(el);
2528      else if (el->flag == 2 || el->flag == 3)  split_other(el);
2529      else if (el->split == 3)  split_kicker(el);
2530    }
2531  }
2532}
2533
2534
2535static void
2536split_kicker(struct c6t_element* el)
2537{
2538  struct c6t_element *k1, *k2;
2539  char c[24];
2540
2541  if (el->value[0] > zero) split_other(el);
2542  if (el->flag == 6) /*split kicker into h + v */
2543  {
2544    get_next_name(c, 'h');
2545    k1 = new_c6t_element(13, c, "hkicker");
2546    get_next_name(c, 'v');
2547    k2 = new_c6t_element(14, c, "vkicker");
2548    k1->force = k2->force = el->force;
2549    k1->value[12] = el->value[12];
2550    k2->value[13] = el->value[13];
2551    k1->flag = k2->flag = 5;
2552    k1->position = el->position;
2553    k2->position = el->position;
2554    replace_c6t(el, k1);
2555    link_behind(k2, k1);
2556    add_to_ellist(k1); add_to_ellist(k2);
2557  }
2558}
2559
2560static void
2561split_other(struct c6t_element* el)
2562{
2563  /* -> two drifts with non-lin. thin lens at centre */
2564  struct c6t_element *d1, *d2;
2565  double length = el->value[0] / two;
2566  char c[24];
2567
2568  get_next_name(c, 'd');
2569  d1 = new_c6t_element(1, c, "drift");
2570  get_next_name(c, 'd');
2571  d2 = new_c6t_element(1, c, "drift");
2572  d1->value[0] = d2->value[0] = length;
2573  d1->flag = d2->flag = 1;
2574  d1->position = el->position - d1->value[0] / two;
2575  d2->position = el->position + d2->value[0] / two;
2576  treat_split(el);
2577  link_c6t_in_front(d1, el);
2578  link_behind(d2, el);
2579  add_to_ellist(d1); add_to_ellist(d2);
2580}
2581
2582static void
2583split_special(struct c6t_element* el)
2584/* -> two lin. halves with multipole at centre */
2585{
2586  struct c6t_element *d1, *mt;
2587  double length = el->value[0] / two, mt_position = el->position;
2588  char c[24];
2589  int j;
2590
2591  if (el->nf_err > 0)
2592    app_factor(el->value[0], el->p_fd_err->a_dble, el->nf_err);
2593  get_next_name(c, *el->base_name);
2594  d1 = new_c6t_element(el->n_values, c, el->base_name);
2595  d1->value[0] = el->value[0] = length;
2596  for (j = 1; j < el->n_values; j++)
2597    d1->value[j] = el->value[j];
2598  d1->flag = el->flag = 1;
2599  d1->force = el->force = 1;
2600  d1->position = el->position + d1->value[0] / two;
2601  el->position = el->position - el->value[0] / two;
2602  get_next_name(c, 'm');
2603  mt = new_c6t_element(MULTI_MAX, c, "multipole");
2604  mt->force = 1;
2605  mt->flag = 2; mt->position = mt_position;
2606  mt->n_values = el->n_values; /* multipole forces remain zero */
2607  mt->p_fd_err = el->p_fd_err; el->p_fd_err = NULL;
2608  mt->nf_err = el->nf_err; el->nf_err = 0;
2609  mt->p_al_err = el->p_al_err; mt->na_err = el->na_err;
2610  /* el->p_al_err = NULL; el->na_err = 0; */
2611  mt->keep_in = split_flag;
2612  add_to_ellist(mt);
2613  add_to_ellist(d1);
2614  link_behind(mt, el);
2615  link_behind(d1, mt);
2616}
2617
2618static void
2619supp_elem(void)
2620{
2621  struct c6t_element *d1, *el;
2622  char c[24];
2623  int af;
2624
2625  current_element = first_in_sequ;
2626  while (current_element != NULL)
2627  {
2628    el = current_element;
2629    if (el->value[0] == zero)  /* zero length */
2630    {
2631      if (el->flag == 0)  yank(el);
2632      else if (el->keep_in == 0 && el->npole_sign == 0
2633               && (el->flag == 1 || el->flag > 4)
2634               && ident_zero(el) == 0) yank(el);
2635      else if (el->flag == 3) /* cavity */
2636      {
2637        cavity_count++;
2638        if (cavall_flag == 0 && cavity_count > 1) yank(el);
2639      }
2640    }
2641    else if(el->c_drift > 0)
2642    {
2643      af = get_next_name(c, 'd');
2644      d1 = new_c6t_element(1, c, "drift");
2645      d1->value[0] = el->value[0];
2646      d1->flag = 1; d1->position = el->position;
2647      replace_c6t(el, d1);
2648      if (af != 0)  add_to_ellist(d1);
2649    }
2650    current_element = current_element->next;
2651  }
2652}
2653
2654static void
2655supp_small_comp(struct c6t_element* p)
2656{
2657  int i;
2658  for (i = 12; i < p->n_values-1; i+=2)
2659  {
2660    if (fabs(p->value[i]) > fabs(p->value[i+1]))
2661    {
2662      if (fabs(p->value[i+1]) / fabs(p->value[i]) < eps_6)
2663        p->value[i+1] = zero;
2664    }
2665    else if (fabs(p->value[i+1]) > fabs(p->value[i]))
2666    {
2667      if (fabs(p->value[i]) / fabs(p->value[i+1]) < eps_6)
2668        p->value[i] = zero;
2669    }
2670  }
2671}
2672
2673static void
2674treat_split(struct c6t_element* el)
2675{
2676  if (el->flag == 2)  el->keep_in = 1;
2677  if (el->nf_err > 0)
2678  {
2679    make_multipole(el);
2680  }
2681  else
2682  {
2683    if (el->force != 0)
2684      app_factor(el->value[0], &el->value[12], el->n_values-12);
2685    el->value[0] = zero; /* set element length to zero */
2686  }
2687}
2688
2689static void
2690yank(struct c6t_element* el)
2691{
2692  if (el->previous != NULL)  el->previous->next = el->next;
2693  else                       first_in_sequ      = el->next;
2694  if (el->next != NULL)      el->next->previous = el->previous;
2695//  else                       last_in_sequ       = el->previous; // not used
2696  el->flag = 0;
2697}
2698
2699static void
2700write_all_el(void)
2701{
2702  char title[] =
2703    "SINGLE ELEMENTS---------------------------------------------------------";
2704  f2 = fopen("fc.2", "w");
2705  fprintf(f2, "%s\n", title);
2706  current_element = first_in_sequ;
2707  while (current_element != NULL)
2708  {
2709    if (current_element->flag > 0
2710        && current_element == current_element->equiv
2711        && current_element->w_flag == 0)
2712      write_c6t_element(current_element);
2713    current_element = current_element->next;
2714  }
2715  fprintf(f2, "NEXT\n");
2716}
2717
2718static void write_rfmultipole(struct c6t_element* el)
2719{
2720  char name[48];
2721  if (fabs(el->value[3])>eps_9) {
2722    /* att_rfquadrupole(el); */
2723    strcpy(name, el->name);
2724    strcat(name, "_q");
2725    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2726            name, 26, el->value[3], el->value[2], el->value[1], el->value[6], 0.0, 0.0);
2727  }
2728  if (fabs(el->value[4])>eps_9) {
2729    /* att_rfsextupole(el); */
2730    strcpy(name, el->name);
2731    strcat(name, "_s");
2732    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2733            name, 27, el->value[4], el->value[2], el->value[7], el->value[6], 0.0, 0.0);
2734  }
2735  if (fabs(el->value[5])>eps_9) {
2736    /* att_rfoctupole(el); */
2737    strcpy(name, el->name);
2738    strcat(name, "_o");
2739    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2740            name, 28, el->value[5], el->value[2], el->value[8], el->value[6], 0.0, 0.0);
2741  }
2742  if (fabs(el->value[9])>eps_9) {
2743    /* att_rfquadrupole(el); */
2744    strcpy(name, el->name);
2745    strcat(name, "_q");
2746    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2747            name, -26, el->value[9], el->value[2], el->value[12], el->value[6], 0.0, 0.0);
2748  }
2749  if (fabs(el->value[10])>eps_9) {
2750    /* att_rfsextupole(el); */
2751    strcpy(name, el->name);
2752    strcat(name, "_s");
2753    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2754            name, -27, el->value[10], el->value[2], el->value[13], el->value[6], 0.0, 0.0);
2755  }
2756  if (fabs(el->value[11])>eps_9) {
2757    /* att_rfoctupole(el); */
2758    strcpy(name, el->name);
2759    strcat(name, "_o");
2760    fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2761            name, -28, el->value[11], el->value[2], el->value[14], el->value[6], 0.0, 0.0);
2762  }
2763}
2764
2765static void
2766write_c6t_element(struct c6t_element* el)
2767{
2768  if (strcmp(el->name, "CAV") != 0) {
2769    if (strcmp(el->base_name, "rfmultipole")==0) { 
2770      write_rfmultipole(el);
2771    } else {
2772      fprintf(f2, "%-16s %2d  %16.9e %17.9e  %17.9e  %17.9e  %17.9e  %17.9e\n",
2773              el->name, el->out_1, el->out_2, el->out_3, el->out_4, el->out_5, el->out_6, el->out_7);
2774    }
2775  }
2776  el->w_flag = 1;
2777}
2778
2779static void
2780write_blocks(void)
2781{
2782  char title1[] =
2783    "BLOCK DEFINITIONS-------------------------------------------------------";
2784  char title2[] = "1  1";
2785  struct block* p = first_block;
2786  int i, lc = 0, nbct = 0;
2787  double sum = 0;
2788
2789  fprintf(f2, "%s\n", title1); fprintf(f2, "%s\n", title2);
2790  while (p != NULL)
2791  {
2792    if (p->equiv == p)
2793    {
2794      if (p->flag != 0)
2795      {
2796        sprintf(p->name, "BLOC%d", ++nbct);
2797        fprintf(f2, "%-18s", p->name); lc++;
2798        for (i = 0; i < p->elements->curr; i++)
2799        {
2800          if (lc++ == LINES_MAX)
2801          {
2802            fprintf(f2,"\n"); fprintf(f2,"                  "); lc = 2;
2803          }
2804          fprintf(f2, "%-18s",p->elements->elem[i]->equiv->name);
2805        }
2806      }
2807      if (lc > 0)
2808      {
2809        fprintf(f2,"\n"); lc = 0;
2810      }
2811    }
2812    sum += p->length;
2813    p = p->next;
2814  }
2815  printf("\ntotal block length: %f\n", sum);
2816  fprintf(f2, "NEXT\n");
2817}
2818
2819static void
2820write_f8_errors(void)
2821{
2822  double tiltval;
2823  if (align_cnt == 0)  return;
2824  current_element = first_in_sequ;
2825  while (current_element != NULL)
2826  {
2827    if (current_element->tilt_err > 0)
2828    {
2829      tiltval = current_element->value[6];
2830    }
2831    else {tiltval=0.0;}
2832    if (current_element->na_err > 0)
2833    {
2834      if (f8_cnt++ == 0)    f8 = fopen("fc.8", "w");
2835      fprintf(f8, "%-16s  %14.6e%14.6e%17.9e\n",current_element->equiv->name,
2836              1000*current_element->p_al_err->a_dble[0],
2837              1000*current_element->p_al_err->a_dble[1],
2838              1000*(current_element->p_al_err->a_dble[5]+tiltval));
2839    }
2840    else if (current_element->tilt_err > 0)
2841    {
2842      if (f8_cnt++ == 0)    f8 = fopen("fc.8", "w");
2843      fprintf(f8, "%-16s  %14.6e%14.6e%17.9e\n",current_element->equiv->name,
2844              0.0,
2845              0.0,
2846              1000*tiltval);
2847    }
2848    current_element = current_element->next;
2849  }
2850}
2851
2852static void
2853write_f16_errors(void)
2854{
2855  int i;
2856  double factor;
2857
2858  current_element = first_in_sequ;
2859  while (current_element != NULL)
2860  {
2861    if (current_element->nf_err > 0 && current_element->ref_delta != zero)
2862    {
2863      if (f16_cnt++ == 0)    f16 = fopen("fc.16", "w");
2864      if (current_element->equiv->f3_flag++ == 0)
2865        write_f3_entry("multipole", current_element->equiv);
2866      fprintf(f16,"%s\n", current_element->equiv->name);
2867      for (i = 0; i < current_element->nf_err; i++)
2868        tmp_buff[i] = current_element->p_fd_err->a_dble[i];
2869      for (i = current_element->nf_err; i < FIELD_MAX; i++)
2870        tmp_buff[i] = zero;
2871      factor = c1p3 / current_element->ref_delta;
2872      /* commented out because FIELD_MAX in MAD-X is larger than in original c6t */
2873      /*          for (i = 0; i < FIELD_MAX/2; i++) */
2874      for (i = 0; i < FIELD_MAX/2-1; i++)
2875      {
2876        fprintf(f16, "%23.15e", factor*tmp_buff[2*i]);
2877        factor *= current_element->ref_radius / (i+1);
2878        if ((i+1)%3 == 0) fprintf(f16,"\n");
2879      }
2880      if (i%3 != 0) fprintf(f16,"\n");
2881      factor = c1p3 / current_element->ref_delta;
2882      /*          for (i = 0; i < FIELD_MAX/2; i++) */
2883      for (i = 0; i < FIELD_MAX/2-1; i++)
2884      {
2885        fprintf(f16, "%23.15e", factor*tmp_buff[2*i+1]);
2886        factor *= current_element->ref_radius / (i+1);
2887        if ((i+1)%3 == 0) fprintf(f16,"\n");
2888      }
2889      if (i%3 != 0) fprintf(f16,"\n");
2890    }
2891    current_element = current_element->next;
2892  }
2893}
2894
2895static void
2896write_f34_special(void)
2897{
2898  int i, j, n, flags[FIELD_MAX];
2899  double values[FIELD_MAX];
2900  char* t_list[NT34];
2901  char t_name[NAME_L];
2902  char* cp;
2903  double spos=zero,betx=zero,bety=zero,mux=zero,muy=zero;
2904  int err;
2905
2906  t_list[0] = &mpole_names[1][0];
2907  t_list[1] = &mpole_names[2][0];
2908  t_list[2] = &mpole_names[3][0];
2909  t_list[3] = &mpole_names[4][0];
2910  t_list[4] = &mpole_names[5][0];
2911
2912  if (special_flag == 0)  return;
2913
2914  n = 0;
2915  if(f34_cnt++ == 0)    f34 = fopen("fc.34", "w");
2916  current_element = first_in_sequ;
2917  while (current_element != NULL)
2918  {
2919    for (i = 0; i < NT34; i++)
2920    {
2921      if (strcmp(current_element->base_name, t_list[i]) == 0)
2922      {
2923        n = f34_values(current_element, flags, values);
2924        for (j = 0; j < n; j++)
2925        {
2926          strcpy(t_name, current_element->name);
2927          if ((cp = strchr(t_name, '+')) != NULL) *cp = '\0';
2928          if ((err=double_from_table_row("twiss","s",&(current_element->twtab_row),&spos)))
2929            printf ("Not found double_from table = %i\n",err);
2930          if ((err=double_from_table_row("twiss","betx",&(current_element->twtab_row),&betx)))
2931            printf ("Not found double_from table = %i\n",err);
2932          if ((err=double_from_table_row("twiss","bety",&(current_element->twtab_row),&bety)))
2933            printf ("Not found double_from table = %i\n",err);
2934          if ((err=double_from_table_row("twiss","mux",&(current_element->twtab_row),&mux)))
2935            printf ("Not found double_from table = %i\n",err);
2936          if ((err=double_from_table_row("twiss","muy",&(current_element->twtab_row),&muy)))
2937            printf ("Not found double_from table = %i\n",err);
2938          fprintf(f34,
2939                  " %20.13e  %-16s %3d %20.13e %20.13e %20.13e %20.13e %20.13e\n",
2940                  spos,t_name,flags[j],values[j],betx,bety,mux,muy);
2941        }
2942      }
2943    }
2944    current_element = current_element->next;
2945  }
2946  if (last_in_sequ_org->twtab_row > 0)
2947  {
2948    if ((err=double_from_table_row("twiss","s",&(last_in_sequ_org->twtab_row),&spos)))
2949      printf ("Not found double_from table = %i\n",err);
2950    if ((err=double_from_table_row("twiss","betx",&(last_in_sequ_org->twtab_row),&betx)))
2951      printf ("Not found double_from table = %i\n",err);
2952    if ((err=double_from_table_row("twiss","bety",&(last_in_sequ_org->twtab_row),&bety)))
2953      printf ("Not found double_from table = %i\n",err);
2954    if ((err=double_from_table_row("twiss","mux",&(last_in_sequ_org->twtab_row),&mux)))
2955      printf ("Not found double_from table = %i\n",err);
2956    if ((err=double_from_table_row("twiss","muy",&(last_in_sequ_org->twtab_row),&muy)))
2957      printf ("Not found double_from table = %i\n",err);
2958  }
2959  fprintf(f34,
2960          " %20.13e  %-16s %3d %20.13e %20.13e %20.13e %20.13e %20.13e\n",
2961          spos,"end_marker",100,zero,betx,bety,mux,muy);
2962}
2963
2964static void
2965write_f3_aper(void)
2966{
2967  int f3aper_cnt = 0;
2968  current_element = first_in_sequ;
2969  while (current_element != NULL)
2970  {
2971    if (strstr(current_element->name,"_AP")!=NULL
2972        && (current_element->equiv == current_element))
2973    {
2974      if (f3aper_cnt++ == 0)
2975      {
2976        f3aper  = fopen("fc.3.aper", "w");
2977        fprintf(f3aper,"LIMI\n");
2978      }
2979      if (current_element->value[3] == 1)
2980      {
2981        fprintf(f3aper,"%s %s %f %f\n",current_element->name,"RE",
2982                current_element->value[1], current_element->value[2]);
2983      }
2984      else
2985      {
2986        fprintf(f3aper,"%s %s %f %f\n",current_element->name,"EL",
2987                current_element->value[1], current_element->value[2]);
2988      }
2989    }
2990    current_element = current_element->next;
2991  }
2992  if (f3aper_cnt > 0) fprintf(f3aper,"NEXT\n");
2993}
2994
2995static void
2996write_f3aux(void)
2997{
2998  double aux_val[4] = {-1.e20, -1.e20, -1.e20, -1.e20};
2999  double tw_alfa;
3000  int row=1;
3001  if ((double_from_table_row("summ","q1", &row, &(aux_val[0])) !=0) ||
3002      (double_from_table_row("summ","q2",  &row, &(aux_val[1])) !=0) ||
3003      (double_from_table_row("summ","dq1", &row, &(aux_val[2])) !=0) ||
3004      (double_from_table_row("summ","dq2", &row, &(aux_val[3])) !=0))
3005  {
3006    printf("c6t error: tunes or chromaticities not found!\n");
3007  }
3008  if (current_beam != NULL)
3009  {
3010    if (f3aux_cnt++ == 0)     f3aux  = fopen("fc.3.aux", "w");
3011    if (double_from_table_row("summ","alfa", &row, &tw_alfa) !=0)
3012      printf("c6t warning: alfa not found in twiss\n");
3013    fprintf(f3aux, "SYNC\n");
3014    fprintf(f3aux,"%12.0f%10.6f%10.3f 0.  %12.6f%12.6f  1\n",
3015            harmon, tw_alfa, total_voltage, sequ_length,
3016            c1p3*command_par_value("mass", current_beam));
3017    fprintf(f3aux,"      1.        1.\n");
3018    fprintf(f3aux, "NEXT\n");
3019    fprintf(f3aux, "BEAM\n");
3020    fprintf(f3aux, "%12.4e%14.6g%14.6g%12.4e%12.4e  1  0\n",
3021            command_par_value("npart", current_beam),
3022            0.25e6*command_par_value("exn", current_beam),
3023            0.25e6*command_par_value("eyn", current_beam),
3024            command_par_value("sigt", current_beam),
3025            command_par_value("sige", current_beam));
3026    fprintf(f3aux, "NEXT\n");
3027  }
3028  if (aux_val[0] > -1.e10 && aux_val[1] > -1.e10)
3029  {
3030    fprintf(f3aux, "TUNE\n");
3031    fprintf(f3aux, "QF%12.5f\n", aux_val[0]);
3032    fprintf(f3aux, "QD%12.5f\n", aux_val[1]);
3033    fprintf(f3aux, "NEXT\n");
3034  }
3035  if (aux_val[2] > -1.e10 && aux_val[3] > -1.e10)
3036  {
3037    fprintf(f3aux, "CHRO\n");
3038    fprintf(f3aux, "SXF%12.5f\n", aux_val[2]);
3039    fprintf(f3aux, "SXD%12.5f\n", aux_val[3]);
3040    fprintf(f3aux, "NEXT\n");
3041  }
3042}
3043
3044static void
3045write_f3_matrix(void)
3046{
3047  int i, i_max = 43;
3048  current_element = first_in_sequ;
3049  while (current_element != NULL)
3050  {
3051    if (strcmp(current_element->base_name, "matrix") == 0)
3052    {
3053      if (f3_matrix_cnt++ == 0)
3054      {
3055        f3matrix = fopen("fc.3", "w");
3056        fprintf(f3matrix,"TROM\n");
3057        fprintf(f3matrix,"%-16s\n",current_element->name);
3058      }
3059      for (i = 1; i < i_max; i++)
3060      {
3061        fprintf(f3matrix,"%23.15e", current_element->value[i]);
3062        if (i%3 == 0) fprintf(f3matrix,"\n");
3063      }
3064      fprintf(f3matrix,"NEXT\n");
3065    }
3066    current_element = current_element->next;
3067  }
3068}
3069
3070static void
3071write_f3_entry(char* option, struct c6t_element* el)
3072{
3073  if (f3_cnt++ == 0) f3 = fopen("fc.3", "w");
3074  if (strcmp(option, "multipole") == 0) write_f3_mult(el);
3075}
3076
3077static void
3078write_f3_mult(struct c6t_element* el)
3079{
3080  int i, j, i_max = -1;
3081  struct c6t_element* eln;
3082  if (multi_type < 0)  return;
3083  fprintf(f3,"MULT\n");
3084  fprintf(f3,"%-16s%20.10e%20.10e\n", el->name, c1p3*el->ref_radius,
3085          el->ref_delta);
3086  /* find non-zero errors in all elements equiv. to this, print error matrix */
3087  for (i = 0; i < FIELD_MAX; i++) error_matrix[i] = zero;
3088  for (j = 0; j < types.member[multi_type]->curr; j++)
3089  {
3090    eln = types.member[multi_type]->elem[j];
3091    if (eln->equiv == el)
3092    {
3093      for (i = 0; i < eln->nf_err; i++)
3094      {
3095        if (mult_auto_off)
3096        {
3097          error_matrix[i] = 1.;
3098        }
3099        else
3100        {
3101          if (eln->p_fd_err->a_dble[i] != zero)
3102          {
3103            i_max = i; error_matrix[i] = 1.;
3104          }
3105        }
3106      }
3107    }
3108  }
3109  if (mult_auto_off)
3110  {
3111    i_max = max_mult_ord * 2;
3112  }
3113  else
3114  {
3115    if (++i_max > 0)  i_max += i_max%2;
3116  }
3117  for (i = 0; i < i_max; i++)
3118  {
3119    fprintf(f3,"%4.0f.%4.0f.", 0., error_matrix[i]);
3120    if ((i+1)%2 == 0) fprintf(f3,"\n");
3121  }
3122  fprintf(f3,"NEXT\n");
3123}
3124
3125static void
3126rfmultipole_name(char *name, struct c6t_element* el)
3127{
3128  char tmp[256] = "";
3129  int n = 0;
3130
3131  if (fabs(el->value[3])>eps_9 || fabs(el->value[9])>eps_9) {
3132    strcpy(tmp, el->name);
3133    strcat(tmp, "_q");
3134    n += sprintf(name+n, "%-18s", tmp);
3135  }
3136  if (fabs(el->value[4])>eps_9 || fabs(el->value[10])>eps_9) {
3137    strcpy(tmp, el->name);
3138    strcat(tmp, "_s");
3139    n += sprintf(name+n, "%-18s", tmp);
3140  }
3141  if (fabs(el->value[5])>eps_9 || fabs(el->value[11])>eps_9) {
3142    strcpy(tmp, el->name);
3143    strcat(tmp, "_o");
3144    n += sprintf(name+n, "%-18s", tmp);
3145  }
3146}
3147
3148static void
3149write_struct(void)
3150{
3151  struct block* p = first_block;
3152  int lc = 0;
3153  char title[] =
3154    "STRUCTURE INPUT---------------------------------------------------------";
3155
3156  fprintf(f2, "%s\n", title);
3157  while (p != NULL)
3158  {
3159    char name[256] = "";
3160
3161    if (p->flag == 0) {
3162      if (strcmp(p->first->equiv->base_name,"rfmultipole") == 0)
3163        rfmultipole_name(name, p->first->equiv);
3164      else
3165        strcpy(name, p->first->equiv->name);
3166    }
3167    else
3168      strcpy(name,p->equiv->name);
3169
3170    if (lc++ == LINES_MAX)
3171    {
3172      fprintf(f2,"\n"); lc = 1;
3173    }
3174    fprintf(f2, "%-18s", name);
3175    p = p->next;
3176  }
3177  if (lc > 0)
3178  {
3179    fprintf(f2,"\n");
3180  }
3181  fprintf(f2, "NEXT\n");
3182}
3183
3184static int
3185my_table_row(struct table* table, char* name)
3186{
3187  int i, j, ret = 0;
3188  char t_name[255];
3189  char* cp;
3190  for (i = 0; i < table->num_cols; i++)
3191    if(table->columns->inform[i] == 3) break;
3192  if (i < table->num_cols && last_row < table->curr)
3193  {
3194    for (j = last_row; j < table->curr; j++)
3195    {
3196      strcpy(t_name, table->s_cols[i][j]);
3197      if ((cp = strchr(t_name, ':')) != NULL) *cp = '\0';
3198      if (strcmp(name, t_name) == 0) break;
3199    }
3200    if (j < table->curr)
3201    {
3202      ret = j+1;
3203      last_row = j+1;
3204    }
3205  }
3206  return ret;
3207}
3208
3209static void
3210c6t_finish(void)
3211{
3212  char rout_name[] = "c6t_finish";
3213  int i,j;
3214  struct block* p;
3215  /* remove elements and elements list */
3216  for(i=0; i<types.curr; i++)
3217  {
3218    for(j=0; j<types.member[i]->curr; j++)
3219    {
3220      if (types.member[i]->elem[j]->value)
3221        myfree(rout_name, types.member[i]->elem[j]->value);
3222      if (types.member[i]->elem[j]->p_al_err &&
3223          types.member[i]->elem[j]->do_not_free != 1)
3224      {
3225        if (types.member[i]->elem[j]->p_al_err->a_dble)
3226          myfree(rout_name, types.member[i]->elem[j]->p_al_err->a_dble);
3227        myfree(rout_name, types.member[i]->elem[j]->p_al_err);
3228        types.member[i]->elem[j]->p_al_err = NULL;
3229      }
3230      if (types.member[i]->elem[j]->p_fd_err &&
3231          types.member[i]->elem[j]->do_not_free != 1)
3232      {
3233        if (types.member[i]->elem[j]->p_fd_err->a_dble)
3234          myfree(rout_name, types.member[i]->elem[j]->p_fd_err->a_dble);
3235        myfree(rout_name, types.member[i]->elem[j]->p_fd_err);
3236        types.member[i]->elem[j]->p_fd_err = NULL;
3237      }
3238      myfree(rout_name, types.member[i]->elem[j]);
3239      types.member[i]->elem[j]=NULL;
3240    }
3241    myfree(rout_name, types.member[i]);
3242  }
3243  types.curr=0; first_in_sequ = NULL; last_in_sequ_org = NULL; // last_in_sequ = NULL; // not used
3244  current_element=NULL;
3245  /* remove blocks */
3246  p = first_block;
3247  while (p != NULL)
3248  {
3249    p = p->next;
3250    if (p) myfree(rout_name, p->previous);
3251  }
3252  first_block = NULL; prev_block=NULL; // last_block=NULL; not used
3253  current_block = NULL;
3254  /* remove split_list */
3255  if (split_list)
3256  {
3257    myfree(rout_name, split_list); split_list = NULL;
3258  }
3259  /* clear acro_cnt and acro_list */
3260  for(i=0; i<20; i++)
3261  {
3262    acro_list[i]='\0';
3263    acro_cnt[i]=0;
3264  }
3265  /* remember that this is not the first time we run */
3266  virgin_c6t=0;
3267
3268  /* added by LD 2011-10-18 */
3269  if (f2) { fclose(f2); f2 = 0; }
3270  if (f3) { fclose(f3); f3 = 0; }
3271  if (f3aux) { fclose(f3aux); f3aux = 0; }
3272  if (f3matrix) { fclose(f3matrix); f3matrix = 0; }
3273  if (f3aper) { fclose(f3aper); f3aper = 0; }
3274  if (f8) { fclose(f8); f8 = 0; }
3275  if (f16) { fclose(f16); f16 = 0; }
3276  if (f34) { fclose(f34); f34 = 0; }
3277}
3278
3279static void
3280c6t_init(void)
3281{
3282  int j;
3283  char rout_name[] = "c6t_init";
3284
3285  if (virgin_c6t)
3286  {
3287    p_err_zero = make_obj("zero_errors", 0, FIELD_MAX, 0, 0);
3288    for (j = 0; j < FIELD_MAX; j++)
3289    {
3290      p_err_zero->a_dble[j]=0.0;
3291    }
3292
3293    for (j = 0; j < N_TYPES; j++)
3294    {
3295      t_info[j] = (struct type_info*) mymalloc(rout_name,sizeof(struct type_info));
3296      sscanf(el_info[j],"%s%d%d%d%d%d%d",t_info[j]->name, &t_info[j]->flag_1,
3297             &t_info[j]->flag_2, &t_info[j]->flag_3, &t_info[j]->flag_4,
3298             &t_info[j]->flag_5, &t_info[j]->flag_6);
3299    }
3300  }
3301  if (current_sequ == NULL)
3302    fatal_error("c6t - no current sequence.","");
3303  if (current_sequ->ex_start == NULL)
3304    fatal_error("c6t - sequence not expanded.","");
3305  if (current_sequ->tw_table == NULL)
3306    fatal_error("c6t - twiss table not found.","");
3307  if (attach_beam(current_sequ) == 0)
3308    fatal_error("c6t - sequence without beam command.","");
3309
3310  /* initialise everything */
3311  block_count = 0;     /* current block count for naming */
3312  elem_cnt = 0;        /* element count */
3313  acro_occ = 0;        /* acro list occupation */
3314  align_cnt = 0;       /* element with align errors count */
3315  field_cnt = 0;       /* element with field errors count */
3316  f3_cnt = 0;          /* f3 write flag */
3317  f3aux_cnt = 0;       /* f3aux write flag */
3318  f3_matrix_cnt = 0;   /* f3_matrix write flag */
3319  f8_cnt = 0;          /* f8 write count */
3320  f16_cnt = 0;         /* f16 write count */
3321  f34_cnt = 0;         /* f34 write count */
3322  special_flag = 1;    /* produce special output file from twiss */
3323  aperture_flag = 0;   /* if 1 insert apertures into structure */
3324  cavall_flag = 0;     /* if 0 lump all cavities into first */
3325//  radius_flag = 0; // not used    /* change the default reference radius */
3326  split_flag = 0;      /* if 1 keep zero multipoles after split */
3327  multi_type = -1;     /* is set to multipole type if any found */
3328  cavity_count = 0;    /* count cavities in output */
3329
3330  total_voltage = 0;
3331  harmon = 0;
3332
3333  /* added by LD 2011-10-18 */
3334  f2 = 0;
3335  f3 = 0;
3336  f3aux = 0;
3337  f3matrix = 0;
3338  f3aper = 0;
3339  f8 = 0;
3340  f16 = 0;
3341  f34 = 0;
3342}
3343
3344static void
3345process_c6t(void)  /* steering routine */
3346{
3347  read_sequ();   /* from db read sequence, store all elements */
3348  add_c6t_drifts();
3349  conv_elem();   /* tag elements */
3350  split();       /* convert to thin */
3351  multi_loop();
3352  supp_elem();   /* suppress/replace zero force, most markers,
3353                    and possibly some cavities */
3354  concat_drifts();
3355  get_multi_refs();  /* get multipole flag */
3356  equiv_elem();  /* find first equivalent for all elements */
3357  post_multipoles();  /* give errors to all equiv. multipoles */
3358  block_it();    /* group linear elements into blocks */
3359  assign_att();  /* assign attributes + errors to all single elements */
3360  mod_errors();  /* flip normal components */
3361
3362  write_all_el();
3363  write_blocks();
3364  write_struct();
3365  write_f16_errors();
3366  write_f34_special();
3367  write_f3aux();
3368  write_f3_matrix();
3369  write_f3_aper();
3370  write_f8_errors();
3371}
3372
3373// public interface
3374
3375void
3376conv_sixtrack(struct in_cmd* mycmd) /* writes sixtrack input files from MAD-X */
3377{
3378  last_row = 0;
3379
3380  puts("  ++++++++++++++++++++++++++++");
3381  puts("  +   c6t version 2.0        +");
3382  puts("  ++++++++++++++++++++++++++++\n");
3383
3384  c6t_init();
3385  get_args(mycmd);
3386  process_c6t();
3387  printf("\nc6t terminated - total number of elements: %d\n", elem_cnt);
3388  printf("                    field errors    MAD-X: %d\n", field_cnt);
3389  printf("                    field errors SixTrack: %d\n", f16_cnt);
3390  printf("                 alignment errors   MAD-X: %d\n", align_cnt);
3391  printf("                alignment errors SixTrack: %d\n", f8_cnt);
3392  printf("                          sequence length: %f [m]\n", sequ_length);
3393  c6t_finish();
3394}
3395
3396
Note: See TracBrowser for help on using the repository browser.