forked from MethodicalAcceleratorDesign/MAD-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mad_seq.c
2259 lines (2039 loc) · 68.5 KB
/
mad_seq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "madx.h"
static void
make_sequ_node(struct sequence* sequ, int occ_cnt)
/* makes + links a node pointing to a sub-sequence */
{
prev_node = current_node;
current_node = new_sequ_node(sequ, occ_cnt);
current_node->occ_cnt = occ_cnt;
add_to_node_list(current_node, 0, current_sequ->nodes);
prev_node->next = current_node;
current_node->previous = prev_node;
current_node->next = NULL;
}
static struct sequence_list*
delete_sequence_list(struct sequence_list* sql)
{
const char *rout_name = "delete_sequence_list";
if (sql == NULL) return NULL;
if (stamp_flag && sql->stamp != 123456)
fprintf(stamp_file, "d_s_l double delete --> %s\n", sql->name);
if (watch_flag) fprintf(debug_file, "deleting --> %s\n", sql->name);
if (sql->list != NULL) delete_name_list(sql->list);
if (sql->sequs != NULL) myfree(rout_name, sql->sequs);
myfree(rout_name, sql);
return NULL;
}
static void
dump_exp_sequ(struct sequence* sequ, int level)
/* executes the command dumpsequ, ... */
{
struct node* c_node;
int j;
double suml = zero;
puts("+++++++++ dump expanded sequence +++++++++");
c_node = sequ->ex_start;
while(c_node != NULL)
{
suml += c_node->length;
if (level > 2)
{
dump_node(c_node);
if (c_node->p_al_err != NULL)
{
puts("alignment errors:");
for (j = 0; j < c_node->p_al_err->curr; j++)
printf(v_format("%F "), c_node->p_al_err->a[j]);
printf("\n");
}
if (c_node->p_fd_err != NULL)
{
puts("field errors:");
for (j = 0; j < c_node->p_fd_err->curr; j++)
printf(v_format("%e "), c_node->p_fd_err->a[j]);
printf("\n");
}
if (level > 3 && c_node->p_elem != NULL) dump_element(c_node->p_elem);
}
else if (level > 0 && strcmp(c_node->base_name, "drift") != 0)
fprintf(prt_file, v_format("%S: at = %F flag = %I\n"), c_node->name,
c_node->position, c_node->enable);
if (c_node == sequ->ex_end) break;
c_node = c_node->next;
}
fprintf(prt_file, v_format("=== sum of node length: %F\n"), suml);
}
#if 0 // not used...
static void
dump_sequ(struct sequence* c_sequ, int level)
{
struct node* c_node;
double suml = zero;
fprintf(prt_file, v_format("+++ dump sequence: %S\n"), c_sequ->name);
c_node = c_sequ->start;
while(c_node != NULL)
{
suml += c_node->length;
if (level > 2)
{
dump_node(c_node);
if (level > 3 && c_node->p_elem != NULL) dump_element(c_node->p_elem);
}
else if (level > 0 && strcmp(c_node->base_name, "drift") != 0)
fprintf(prt_file, v_format("%S: at = %F\n"),
c_node->name, c_node->position);
if (c_node == c_sequ->end) break;
c_node = c_node->next;
}
fprintf(prt_file, v_format("=== sum of node length: %F\n"), suml);
}
#endif
static void
grow_sequence_list(struct sequence_list* l)
{
const char *rout_name = "grow_sequence_list";
struct sequence** sloc = l->sequs;
int new = 2*l->max;
l->max = new;
l->sequs = mycalloc(rout_name, new, sizeof *l->sequs);
for (int j = 0; j < l->curr; j++) l->sequs[j] = sloc[j];
myfree(rout_name, sloc);
}
static void
make_occ_list(struct sequence* sequ)
/* makes the node occurrence list */
{
struct node* c_node = sequ->start;
int i;
while (c_node != NULL) {
if (c_node->p_elem != NULL) {
if ((i = name_list_pos(c_node->p_elem->name, occ_list)) < 0)
i = add_to_name_list(c_node->p_elem->name, 1, occ_list);
else ++occ_list->inform[i];
}
if (c_node == sequ->end) break;
c_node = c_node->next;
}
}
static void
all_node_pos(struct sequence* sequ)
/* calculates all node positions in an expanded sequence */
{
struct node* node = sequ->start;
while (node != NULL) {
if (node->p_elem != NULL)
node->length = node->p_elem->length
= element_value(node, "l");
else if (node->p_sequ != NULL)
node->length = sequence_length(node->p_sequ);
else fatal_error("node is neither element nor sequence:",
node->name);
// 2014-Mar-19 16:27:15 ghislain:
// The following assumes that the sequence is circular!!!!
// this also conflicts with the insertion of sequences where a refpos is given.
//if ((node->position = get_node_pos(node, sequ)) < zero)
// node->position += sequence_length(sequ);
node->position = get_node_pos(node, sequ);
if (node == sequ->end) break;
node = node->next;
}
}
static struct sequence*
extract_sequence(char* name, struct sequence* sequ, struct node* from, struct node* to, char* refpos)
{
struct element* el;
int pos, marker_pos, from_cond;
struct command* clone;
struct sequence* keep_curr_sequ = current_sequ;
struct sequence* new_sequ;
struct node *q = from, *from_node;
struct node_list* new_nodes;
double start_value, end_value;
char tmp_name[2*NAME_L];
if (get_option("info")) printf("+++ extracting sequence %s from %s to %s\n",
sequ->name, from->name, to->name);
current_sequ = new_sequence(name, sequ->ref_flag);
current_sequ->share = 1; /* make shared for recombination */
current_sequ->refpos = refpos;
current_sequ->cavities = new_el_list(100);
new_nodes = new_node_list(1000);
/* fill new_node list for 'from' references */
while (q)
{
add_to_node_list(q, 0, new_nodes);
if (q == to) break;
q = q->next;
}
/* sequence construction */
q = from;
start_value = get_node_pos(from, sequ);
end_value = get_node_pos(to, sequ);
current_sequ->l_expr = NULL;
current_sequ->length = end_value - start_value;
// 2014-Mar-21 20:42:19 ghislain:
// this might be dangerous for non-circular sequences!!!
// if (current_sequ->length < zero) current_sequ->length += sequ->length;
marker_pos = name_list_pos("marker", defined_commands->list);
clone = clone_command(defined_commands->commands[marker_pos]);
sprintf(c_dum->c, "%s$start", name);
el = make_element(c_dum->c, "marker", clone, 0);
current_node = NULL;
make_elem_node(el, 1);
current_sequ->start = current_node;
make_elem_node(from->p_elem, from->occ_cnt);
current_node->at_value = 0;
current_node->at_expr = NULL;
/* loop over sequ nodes from 'from' to 'to' */
while (current_node != NULL)
{
while (strchr(q->next->name, '$')) q = q->next; /* suppress internal markers */
if (q->next->p_elem)
make_elem_node(q->next->p_elem, q->next->occ_cnt);
else if(q->next->p_sequ)
make_sequ_node(q->next->p_sequ, q->next->occ_cnt);
else
fatal_error("node has neither element nor sequence reference:", q->next->name);
q = q->next;
if (q->p_elem && strcmp(q->p_elem->base_type->name, "rfcavity") == 0 &&
find_element(q->p_elem->name, current_sequ->cavities) == NULL)
add_to_el_list(&q->p_elem, 0, current_sequ->cavities, 0);
from_cond = 0;
if (q->from_name)
{
strcpy(tmp_name, q->from_name);
strcat(tmp_name, ":1");
if ((pos = name_list_pos(tmp_name, sequ->nodes->list)) > -1)
{
from_node = sequ->nodes->nodes[pos];
from_cond = 1;
if (name_list_pos(tmp_name, new_nodes->list) > -1)
from_cond = 2;
}
}
if (from_cond == 2)
{
current_node->from_name = q->from_name;
current_node->at_value = q->at_value;
current_node->at_expr = q->at_expr;
}
else
{
if (from_cond == 1)
current_node->at_value = expr_combine(q->at_expr, q->at_value, " + ",
from_node->at_expr, from_node->at_value,
¤t_node->at_expr);
else
{
current_node->at_expr = q->at_expr;
current_node->at_value = q->at_value;
}
current_node->at_value = expr_combine(current_node->at_expr,
current_node->at_value, " - ",
NULL, start_value,
¤t_node->at_expr);
if (current_node->at_value < zero)
current_node->at_value = expr_combine(current_node->at_expr,
current_node->at_value, " + ",
sequ->l_expr, sequ->length,
¤t_node->at_expr);
}
if (q == to) break;
}
clone = clone_command(defined_commands->commands[marker_pos]);
sprintf(c_dum->c, "%s$end", name);
el = make_element(c_dum->c, "marker", clone, 0);
make_elem_node(el, 1);
current_node->at_expr = current_sequ->l_expr;
current_node->at_value = current_sequ->length;
current_sequ->end = current_node;
current_node->next = current_sequ->start;
current_sequ->start->previous = current_node;
new_sequ = current_sequ;
current_sequ = keep_curr_sequ;
if (get_option("info"))
printf("+++ new sequence: %s with current length = %.12g\n\n",
new_sequ->name, new_sequ->length);
return new_sequ;
}
static void
fill_sequ_var_list(struct sequence_list* sql, struct el_list* ell, struct var_list* varl)
/* puts all variables a sequence depends on, in a list */
{
int i;
struct sequence* sequ;
struct node* c_node;
for (i = 0; i < sql->curr; i++)
{
sequ = sql->sequs[i];
if (sequ->l_expr != NULL) fill_expr_var_list(ell, sequ->l_expr, varl);
c_node = sequ->start;
while(c_node != NULL)
{
if (c_node->at_expr != NULL)
fill_expr_var_list(ell, c_node->at_expr, varl);
if (c_node == sequ->end) break;
c_node = c_node->next;
}
}
}
static void
seq_edit_ex(struct sequence* seq)
{
edit_sequ = seq;
edit_is_on = 1;
seqedit_install = seqedit_move = seqedit_remove = seqedit_replace = 0;
if (edit_sequ->ex_start != NULL) {
edit_sequ->ex_nodes = delete_node_list(edit_sequ->ex_nodes);
edit_sequ->ex_start = delete_node_ring(edit_sequ->ex_start);
}
if (occ_list == NULL)
occ_list = new_name_list("occ_list", 10000); /* for occurrence count */
else occ_list->curr = 0;
resequence_nodes(edit_sequ);
all_node_pos(edit_sequ);
}
static void
seq_end_ex(void)
{
occ_list->curr = 0;
resequence_nodes(edit_sequ);
selected_ranges->curr = 0;
selected_ranges->list->curr = 0;
edit_is_on = 0;
}
static void
expand_line(struct char_p_array* l_buff)
/* expands a beam line, applies rep. count and inversion */
{
int add=0, i=0, j=0, k=0, n=0, number=0, dummy=0, rep=-1, pos=0;
int level = 0, l_max = 0, b_cnt = 0;
char* p = blank;
struct int_array* lbpos = new_int_array(l_buff->curr);
struct int_array* rbpos = new_int_array(l_buff->curr);
struct int_array* b_level = new_int_array(l_buff->curr);
/* first get all bracket pairs with their level; keep max. level */
for (i = 0; i < l_buff->curr; i++) {
if (*l_buff->p[i] == '(') {
lbpos->i[b_cnt] = i;
b_level->i[b_cnt++] = level++;
if (level > l_max) l_max = level;
}
else if (*l_buff->p[i] == ')') level--;
}
l_max--;
for (i = 0; i < b_cnt; i++)
get_bracket_t_range(l_buff->p, '(', ')', lbpos->i[i],
l_buff->curr-1, &dummy, &rbpos->i[i]);
lbpos->curr = rbpos->curr = b_level->curr = b_cnt;
/* now loop over level from highest down to zero, expand '*' in each pair */
for (level = l_max; level >=0; level--) {
for (i = 0; i < b_cnt; i++) {
if (b_level->i[i] == level && (pos = lbpos->i[i]) > 1) {
if (*l_buff->p[pos-1] == '*') {
sscanf(l_buff->p[pos-2], "%d", &rep);
add = rep - 1;
number = rbpos->i[i] - pos - 1; /* inside bracket */
n = number * add; /* extra tokens */
while (l_buff->curr + n >= l_buff->max) grow_char_p_array(l_buff);
for (j = l_buff->curr; j > pos + number; j--) /* shift upwards */
l_buff->p[j+n] = l_buff->p[j];
l_buff->curr += n;
for (k = 1; k <= add; k++) {
for (j = pos+1; j <= pos+number; j++)
l_buff->p[j+k*number] = tmpbuff(l_buff->p[j]);
}
for (j = 0; j < b_cnt; j++) { /* reset bracket pointers */
if (lbpos->i[j] > pos + number) lbpos->i[j] += n;
if (rbpos->i[j] > pos + number) rbpos->i[j] += n;
}
l_buff->p[pos-1] = l_buff->p[pos-2] = blank;
}
}
}
}
/* loop over buffer, expand simple element repetition defined with '*' f.g. 10*myquad */
for (pos = 2; pos < l_buff->curr; pos++) {
if (*l_buff->p[pos] == '*') {
rep = -1;
sscanf(l_buff->p[pos-1], "%d", &rep);
if (rep < 0) fatal_error("expand_line","Problem with reading number of copies");
n = add = rep - 1;
while (l_buff->curr + n >= l_buff->max) grow_char_p_array(l_buff);
for (j = l_buff->curr; j > pos + 1; j--) /* shift upwards */
l_buff->p[j+n] = l_buff->p[j];
l_buff->curr += n;
j = pos+1;
for (k = 1; k <= add; k++) l_buff->p[j+k] = tmpbuff(l_buff->p[j]);
for (j = 0; j < b_cnt; j++) { /* reset bracket pointers */
if (lbpos->i[j] > pos + 1) lbpos->i[j] += n;
if (rbpos->i[j] > pos + 1) rbpos->i[j] += n;
}
// 2014-Aug-18 19:56:34 ghislain: in case of single element with rep_count,
// l_buff->p[pos-1] points to the rep_count
// and l_buff->p[pos-2] the previous element which is then lost!!!
// l_buff->p[pos-1] = l_buff->p[pos-2] = blank;
l_buff->p[pos-1] = blank;
}
}
/* get bracket pointers including new ones */
while (b_level->max < l_buff->curr) grow_int_array(b_level);
while (lbpos->max < l_buff->curr) grow_int_array(lbpos);
while (rbpos->max < l_buff->curr) grow_int_array(rbpos);
level = b_cnt = 0;
for (i = 0; i < l_buff->curr; i++) {
if (*l_buff->p[i] == '(') {
lbpos->i[b_cnt] = i;
b_level->i[b_cnt++] = level++;
}
else if (*l_buff->p[i] == ')') level--;
}
for (i = 0; i < b_cnt; i++)
get_bracket_t_range(l_buff->p, '(', ')', lbpos->i[i],
l_buff->curr-1, &dummy, &rbpos->i[i]);
lbpos->curr = rbpos->curr = b_level->curr = b_cnt;
/* now loop over level from highest down to zero, invert if '-' */
for (level = l_max; level >= 0; level--) {
for (i = 0; i < b_cnt; i++) {
pos = lbpos->i[i];
if (b_level->i[i] == level) {
p = blank;
for (j = pos - 1; j > 0; j--) {
p = l_buff->p[j];
if (*p != ' ') break;
}
if (*p == '-') {
number = rbpos->i[i] - pos - 1; // length of sequence
n = number / 2;
for (j = 0; j < n; j++) {
p = l_buff->p[pos+j+1];
l_buff->p[pos+j+1] = l_buff->p[pos+number-j];
l_buff->p[pos+number-j] = p;
}
}
}
}
}
/* finally remove all non-alpha tokens */
n = 0;
for (i = 0; i < l_buff->curr; i++) {
if (isalpha(*l_buff->p[i])) l_buff->p[n++] = l_buff->p[i];
}
l_buff->curr = n;
lbpos = delete_int_array(lbpos);
rbpos = delete_int_array(rbpos);
b_level = delete_int_array(b_level);
}
static void
expand_sequence(struct sequence* sequ, int flag)
/* expands a sequence into nodes, expands sequence nodes */
{
/* Transfers errors from original nodes if flag != 0;
this is needed for SXF input */
struct node *p, *q = sequ->start;
int debug=get_option("debug");
// 2015-Jun-09 14:14:24 ghislain: guard added for negative seq length
if (sequ->length < 0)
fatal_error("trying to expand sequence with negative length:", sequ->name);
if (debug)
printf("\n\nTOP Expand_sequence name %s with length %e, ref_flag %d\n",
sequ->name, sequ->length, sequ->ref_flag);
p = sequ->ex_start = clone_node(sequ->start, 0);
add_to_node_list(p, 0, sequ->ex_nodes);
while (p != NULL) {
if (q == sequ->end) break;
p->next = clone_node(q->next, flag);
p->next->previous = p;
p = p->next;
q = q->next;
if (p->p_sequ == NULL) { // simple element, not a subsequence
// 2015-Jun-09 14:14:24 ghislain: guard added for negative element length
if (p->length < 0)
fatal_error("trying to add node with negative length to current sequence:", p->name);
add_to_node_list(p, 0, sequ->ex_nodes);
}
else { // subsequence
if (p->p_sequ->refpos != NULL) { // REFPOS given for subsequence, ignore REFER of current sequence
if (debug)
printf("\n\n Expand sub-sequence %s with initial position %e, final position %e, length %e, ref_flag %d, refpos '%s'\n",
p->name, p->position, p->position - sequ->ref_flag*p->p_sequ->length/2.,
p->length, sequ->ref_flag, p->p_sequ->refpos);
p = expand_node(p, sequ, sequ, p->position - sequ->ref_flag*p->p_sequ->length/2. );
if (debug) printf("\n\n");
}
else { // no REFPOS given
if (debug)
printf("\n\n Expand sub-sequence %s with position %e, length %e, ref_flag %d\n",
p->name, p->position, p->length, sequ->ref_flag);
p = expand_node(p, sequ, sequ, p->position);
if (debug) printf("\n\n");
}
}
}
sequ->ex_end = p;
sequ->ex_end->next = sequ->ex_start;
sequ->ex_start->previous = sequ->ex_end;
p = sequ->ex_start;
while (p != sequ->ex_end) {
if (strstr(p->base_name, "kicker") || strstr(p->base_name, "monitor"))
p->enable = 1; /* flag for orbit correction module */
p = p->next;
}
/*
Attempt to discard attached twiss table not anymore valid...
note: it cannot be done properly as table_register keep references
on-pointer-to-table (address of address) not to-table itself...
hence
if (sequ->tw_table) sequ->tw_table = NULL;
breaks the list of table_register used everywhere,
leading to bus error in many places (pointers are almost never checked)
*/
}
static void
seq_flatten(struct sequence* sequ)
/* executes flatten command */
{
struct node* c_node;
struct node_list* nl;
if (occ_list == NULL)
occ_list = new_name_list("occ_list", 10000); /* for occurrence count */
else occ_list->curr = 0;
make_occ_list(sequ);
all_node_pos(sequ);
sequ->ex_nodes = new_node_list(2*sequ->nodes->curr);
expand_sequence(sequ, 0);
sequ->nested = 0;
nl = sequ->nodes;
sequ->nodes = sequ->ex_nodes;
sequ->ex_nodes = delete_node_list(nl);
sequ->start = sequ->ex_start; sequ->ex_start = NULL;
sequ->end = sequ->ex_end; sequ->ex_end = NULL;
c_node = sequ->start;
while (c_node != NULL) {
c_node->at_value = c_node->position;
c_node->at_expr = NULL;
c_node->from_name = NULL;
if (c_node == sequ->end) break;
c_node = c_node->next;
}
sequ->ref_flag = 0;
}
static void
insert_elem(struct sequence* sequ, struct node* node)
/* inserts an element in a sequence as function of its position */
{
struct node* c_node = sequ->start->next;
while (c_node != NULL) {
if (node->position <= c_node->position || c_node == sequ->end) break;
c_node = c_node->next;
}
link_in_front(node, c_node);
}
static struct node*
install_one(struct element* el, char* from_name, double at_value, struct expression* at_expr, double position)
/* adds an element to a sequence */
{
struct node* node;
int i, occ = 1;
if (strcmp(el->base_type->name, "rfcavity") == 0 &&
find_element(el->name, edit_sequ->cavities) == NULL)
add_to_el_list(&el, 0, edit_sequ->cavities, 0);
if ((i = name_list_pos(el->name, occ_list)) < 0)
i = add_to_name_list(el->name, occ, occ_list);
else occ = ++occ_list->inform[i];
node = new_elem_node(el, occ);
add_to_node_list(node, 0, edit_sequ->nodes);
node->position = position;
node->at_value = at_value;
node->at_expr = at_expr;
node->from_name = from_name;
set_command_par_value("at", el->def, position);
insert_elem(edit_sequ, node);
return node;
}
static void
make_sequ_from_line(char* name)
/* converts a line into a sequence from actual line definition */
{
char** tmp = NULL;
int pos = name_list_pos(name, line_list->list);
struct macro* line;
int mpos = name_list_pos("marker", defined_commands->list);
struct command* clone = clone_command(defined_commands->commands[mpos]);
struct element* el;
if (pos < 0) fatal_error("unknown line: ", name);
line = line_list->macros[pos];
line->dead = 1; /* prevent line from further conversion to sequence */
line_buffer = new_char_p_array(1000);
replace_lines(line, 0, tmp); /* replaces all referenced lines */
expand_line(line_buffer); /* act on '-' and rep. count */
current_sequ = new_sequence(name, 0); /* node positions = centre */
delete_sequence(find_sequence(name, sequences));
add_to_sequ_list(current_sequ, sequences);
if (current_sequ->cavities != NULL) current_sequ->cavities->curr = 0;
else current_sequ->cavities = new_el_list(100);
if (occ_list == NULL)
occ_list = new_name_list("occ_list", 10000); /* for occurrence count */
else occ_list->curr = 0;
sprintf(c_dum->c, "%s$start", current_sequ->name);
el = make_element(c_dum->c, "marker", clone, 0);
current_node = NULL;
make_elem_node(el, 1);
current_sequ->start = current_node;
current_sequ->length = line_nodes(line_buffer);
sprintf(c_dum->c, "%s$end", current_sequ->name);
el = make_element(c_dum->c, "marker", clone, 0);
make_elem_node(el, 1);
current_node->at_value = current_node->position = current_sequ->length;
current_sequ->end = current_node;
current_sequ->start->previous = current_sequ->end;
current_sequ->end->next = current_sequ->start;
current_sequ->line = 1; /* remember origin of sequence */
if(line_buffer) delete_char_p_array(line_buffer,1);
}
static void
export_sequence(struct sequence* sequ, FILE* file, int noexpr)
/* exports sequence in mad-X format */
{
char num[2*NAME_L];
struct element* el;
struct sequence* sq;
struct node* c_node = sequ->start;
int exp_par_flag;
int seqref = 0;
char rpos[3][6] = {"exit", "centre", "entry"};
seqref = sequ->ref_flag; /* uncomment line to get entry or exit */
*c_dum->c = '\0';
if (sequ->share) strcat(c_dum->c, "shared ");
strcat(c_dum->c, sequ->export_name);
strcat(c_dum->c, ": sequence");
if (seqref)
{
strcat(c_dum->c, ", refer = ");
strcat(c_dum->c, rpos[seqref+1]);
}
if (sequ->refpos != NULL)
{
strcat(c_dum->c, ", refpos = ");
strcat(c_dum->c, sequ->refpos);
}
strcat(c_dum->c, ", l = ");
if (sequ->l_expr != NULL) strcat(c_dum->c, sequ->l_expr->string);
else
{
sprintf(num, v_format("%F"), sequence_length(sequ));
strcat(c_dum->c, supp_tb(num));
}
write_nice(c_dum->c, file);
while(c_node != NULL)
{
exp_par_flag = 0;
*c_dum->c = '\0';
if (strchr(c_node->name, '$') == NULL
&& strstr(c_node->name, "_p_") == NULL
&& strcmp(c_node->base_name, "drift") != 0)
{
if ((el = c_node->p_elem) != NULL)
{
if (c_node->p_elem->def_type)
{
strcat(c_dum->c, el->name);
strcat(c_dum->c, ": ");
strcat(c_dum->c, el->parent->name);
exp_par_flag = 1;
}
else strcat(c_dum->c, el->name);
}
else if ((sq = c_node->p_sequ) != NULL) strcat(c_dum->c, sq->name);
else fatal_error("save error: node without link:", c_node->name);
strcat(c_dum->c, ", at = ");
if (c_node->at_expr != NULL) strcat(c_dum->c, c_node->at_expr->string);
else
{
sprintf(num, v_format("%F"), c_node->at_value);
strcat(c_dum->c, supp_tb(num));
}
if (c_node->from_name != NULL)
{
strcat(c_dum->c, ", from = ");
strcat(c_dum->c, c_node->from_name);
}
if (exp_par_flag) export_el_def(c_node->p_elem, c_dum->c, noexpr);
write_nice(c_dum->c, file);
}
if (c_node == sequ->end) break;
c_node = c_node->next;
}
strcpy(c_dum->c, "endsequence");
write_nice(c_dum->c, file);
}
static void
export_sequ_8(struct sequence* sequ, struct command_list* cl, FILE* file)
/* exports sequence in mad-8 format */
/* set refer = centre always (local var. seqref) HG 9.1.09 */
{
char num[2*NAME_L];
int exp_par_flag;
int seqref = 0;
struct element* el;
struct sequence* sq;
struct node* c_node = sequ->start;
seqref = sequ->ref_flag; /* uncomment line to get entry or exit */
if (pass_select_list_str(sequ->name, "sequence", cl) == 0) return;
*c_dum->c = '\0';
strcat(c_dum->c, sequ->export_name);
strcat(c_dum->c, ": sequence");
if (seqref == 1) strcat(c_dum->c, ", refer=entry");
write_nice_8(c_dum->c, file);
while(c_node != NULL) {
exp_par_flag = 0;
*c_dum->c = '\0';
if (strchr(c_node->name, '$') == NULL
&& strcmp(c_node->base_name, "drift") != 0) {
if ((el = c_node->p_elem) != NULL) {
if (c_node->p_elem->def_type) {
strcat(c_dum->c, el->name);
strcat(c_dum->c, ": ");
strcat(c_dum->c, el->parent->name);
exp_par_flag = 1;
}
else strcat(c_dum->c, el->name);
}
else if ((sq = c_node->p_sequ) != NULL) strcat(c_dum->c, sq->name);
else fatal_error("save error: node without link:", c_node->name);
strcat(c_dum->c, ", at = ");
if (c_node->at_expr != NULL) strcat(c_dum->c, c_node->at_expr->string);
else {
sprintf(num, v_format("%F"), c_node->at_value);
strcat(c_dum->c, supp_tb(num));
}
if (c_node->from_name != NULL) {
strcat(c_dum->c, ", from = ");
strcat(c_dum->c, c_node->from_name);
}
if (exp_par_flag) export_el_def_8(c_node->p_elem, c_dum->c);
write_nice_8(c_dum->c, file);
}
if (c_node == sequ->end) break;
c_node = c_node->next;
}
strcpy(c_dum->c, sequ->name);
strcat(c_dum->c, "_end: marker, at = ");
sprintf(num, v_format("%F"), sequence_length(sequ));
strcat(c_dum->c,num);
write_nice_8(c_dum->c, file);
strcpy(c_dum->c, "endsequence");
write_nice_8(c_dum->c, file);
}
static void
write_sequs(struct sequence_list* sql,struct command_list* cl, FILE* file, int noexpr)
{
/* exports sequences in order of their nest level, flat first etc. */
int i, j, max_nest = 0;
for (i = 0; i < sql->curr; i++)
if(sql->sequs[i]->nested > max_nest) max_nest = sql->sequs[i]->nested;
for (j = 0; j <= max_nest; j++)
{
for (i = 0; i < sql->curr; i++)
if(sql->sequs[i]->nested == j)
{
if (pass_select_list_str(sql->sequs[i]->name, "sequence", cl))
export_sequence(sql->sequs[i], file, noexpr);
}
}
}
static void
seq_cycle(struct in_cmd* cmd)
/* cycles a sequence */
{
struct node *node, *clone;
int pos;
char* name = command_par_string_user("start", cmd->clone);
if (name)
{
sprintf(c_dum->c, "%s:1", name);
if ((pos = name_list_pos(c_dum->c, edit_sequ->nodes->list)) > -1)
{
node = edit_sequ->nodes->nodes[pos];
sprintf(c_dum->c, "%s%s_p_", edit_sequ->name,strip(node->name));
if (strstr(node->previous->name, "_p_") == NULL)
{
clone = clone_node(node, 0);
clone->p_elem = clone_element(node->p_elem);
strcpy(clone->p_elem->name, c_dum->c);
/* IA 29.11.07 : fixes a bug with aperture module */
/* Removed HG 11.10.2009 */
/* sprintf(c_dum->c, " ");
set_command_par_string("apertype", clone->p_elem->def,c_dum->c); */
add_to_el_list(&clone->p_elem, node->p_elem->def->mad8_type,element_list,1);
link_in_front(clone, node);
}
edit_sequ->start = node;
edit_sequ->end = node->previous;
set_new_position(edit_sequ);
all_node_pos(edit_sequ);
}
else warning("cycle: unknown element ignored:", name);
}
else warning("cycle: no start given,","ignored");
}
static void
seq_edit(struct in_cmd* cmd)
/* executes seqedit command */
{
char* name = NULL;
struct sequence* seq;
name = command_par_string_user("sequence", cmd->clone);
if (name) {
if ((seq = find_sequence(name, sequences))) {
if (seq->line)
warning("sequence originates from line,","edit ignored");
else seq_edit_ex(seq);
}
else warning("unknown sequence:", "ignored");
}
else warning("seqedit without sequence:", "ignored");
}
static void
seq_end(struct in_cmd* cmd)
/* executes endedit command */
{
char tmp[8];
(void)cmd;
sprintf(tmp, "%d", seqedit_install);
put_info("seqedit - number of elements installed: ", tmp);
sprintf(tmp, "%d", seqedit_move);
put_info("seqedit - number of elements moved: ", tmp);
sprintf(tmp, "%d", seqedit_remove);
put_info("seqedit - number of elements removed: ", tmp);
sprintf(tmp, "%d", seqedit_replace);
put_info("seqedit - number of elements replaced: ", tmp);
seq_end_ex();
}
static void
seq_install(struct in_cmd* cmd)
/* executes install command */
{
struct element *cl, *el;
struct node* c_node;
struct expression* expr = NULL;
double at, from = zero;
char name[NAME_L], *pname, *name_e = NULL, *name_c = NULL, *from_name = NULL;
int k, any = 0;
name_e = command_par_string_user("element", cmd->clone);
name_c = command_par_string_user("class", cmd->clone);
if (name_e) {
if (name_c) {
if ((cl = find_element(name_c, element_list)) == NULL) {
warning("ignored, unknown class:", name_c);
return;
} else {
el = clone_element(cl);
strcpy(el->name, name_e);
add_to_el_list(&el, cl->def->mad8_type, element_list, 2);
}
} else if ((el = find_element(name_e, element_list)) == NULL) {
warning("ignored, unknown command or element:", name_c);
return;
}
} else {
warning("no element specified,","ignored");
return;
}
if (!par_present("at", cmd->clone)) {
warning("no 'at':", "ignored");
return;
}
at = command_par_value("at", cmd->clone);
expr = clone_expression(command_par_expr("at", cmd->clone));
from_name = command_par_string_user("from", cmd->clone);
if (from_name) {
if (strcmp(from_name, "selected") == 0) {
if (seqedit_select->curr == 0) {
warning("no active select commands:", "ignored");
return;
}
if (get_select_ranges(edit_sequ, seqedit_select, selected_ranges) == 0) any = 1;
c_node = edit_sequ->start;
while (c_node != NULL) {
if (any || name_list_pos(c_node->name, selected_ranges->list) > -1) {
for (k = 0; k < seqedit_select->curr; k++) {
myrepl(":", "[", c_node->name, name);
strcat(name, "]");
if (strchr(name, '$') == NULL && pass_select_el(c_node->p_elem, seqedit_select->commands[k])) break;
}
if (k < seqedit_select->curr) {
from = get_node_pos(c_node, edit_sequ);
pname = permbuff(name);
install_one(el, pname, at, expr, at+from);
seqedit_install++;
}
}
if (c_node == edit_sequ->end) break;
c_node = c_node->next;
}
} else {
from_name = permbuff(from_name);
if ((from = hidden_node_pos(from_name, edit_sequ)) == INVALID) {
warning("ignoring 'from' reference to unknown element:", from_name);
return;
}
install_one(el, from_name, at, expr, at+from);
seqedit_install++;
}
} else {
install_one(el, from_name, at, expr, at);
seqedit_install++;
}
}
static void
seq_move(struct in_cmd* cmd)
/* executes move command */
{
char *name, *from_name=NULL;
double at, by, to, from = zero;
int any = 0, k;
struct node *node, *next, *node2, *next2;
struct element* el;
int pos, is_ref_from_moved,max_c=100;
struct expression* tmp = NULL;
struct expression* expr = NULL;
struct expression* newexp = NULL;
struct name_list* movelist;
name = command_par_string_user("element", cmd->clone);
if (name)
{