-
Notifications
You must be signed in to change notification settings - Fork 39
/
mad_mkthin.cpp
2854 lines (2603 loc) · 149 KB
/
mad_mkthin.cpp
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
/* mad_mkthin.cpp
Thick to thin lens converter makethin. Helmut Burkhardt
Major steps
2001, 2002 early versions by Mark Hayes
2005 : Standard selection SELECT,FLAG=makethin,RANGE=range,CLASS=class,PATTERN=pattern[,FULL][,CLEAR];
Implementation of slicing for solenoids
2012 : Extension of TEAPOT slicing to n>4
2013 : Keep thick elements if slice number <1, Code now C++,
Thick slicing for quadrupoles
Automatic generation of dipedge elements for dipoles
2014 : Thick bend slicing, with or without dipedge
2018 : Thick solenoid slicing, write bend angle to multipole if different from k0*l
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#ifdef __cplusplus
extern "C" {
#endif
#include "madx.h"
#ifdef __cplusplus
}
#endif
// #define el_par_value(a,b)
// (fprintf(stderr,"el_par_value: %s:%d %s, %g, %s, %s\n", __FILE__, __LINE__, (b)->name, (b)->length, (b)->def->name, (a)), el_par_value(a,b))
// --- macros helper
#define ARRSIZE(a) \
((const void*)(a) == (const void*)&(a) ? sizeof (a)/sizeof *(a) : \
(assert(!"invalid use of ARRSIZE in " __FILE__ " line " mkstring(__LINE__)), 0))
#define mkstring(s) mkstring_(s)
#define mkstring_(s) #s
// LD: variables local to module that control makethin behavior (was pushed in option before)
static int iMakeDipedge, iMakeEndMarkers, iMinimizeParents, iMoreExpressions;
//------------------------------- forward declarations --------------
class SliceDistPos // defines the distances and positions, depending on number of slices and slicing style
{
public:
SliceDistPos(const int n,const bool teapot_fl); // constructor
~SliceDistPos() {}; // (empty) destructor
void Print() const;
double delta;
double Delta;
//
std::string delta_str,delta_half_str; // string expression
std::string Delta_str,Delta_half_str; // string expression
private:
int n; // number of slices
bool teapot_fl;
};
class OneElementWithSlices // One Element with Slices used to work on slices, derived from thick_elem which is not modified - declared as constant
{
public:
OneElementWithSlices(const element* thick_elem,element* sliced_elem); // constructor
~OneElementWithSlices() {}; // (empty) destructor
const element* thick_elem; // pointer to the thick element
std::vector<element*> theSlices; // pointer(s) to the one or several slices
};
class ElementListWithSlices
{
public:
std::vector<OneElementWithSlices*> VecElemWithSlices; // vector of thick elements+slices
ElementListWithSlices(unsigned int verbose); // constructor
~ElementListWithSlices(); // destructor
void put_slice(const element* thick_elem, element* sliced_elem); // add sliced_elem to VecElemWithSlices
element* find_slice(const element* thick_elem, const int slice); // find address of thin slice by slice number for thick_elem
element* find_slice(const element* thick_elem, const std::string& name); // find address of thin slice by slice name for thick_elem
void Print() const;
void PrintCounter() const;
private:
int find_thick(const element* thick_elem); // find thick_element in VecElemWithSlices, <0 means not found, used inside find_slice
unsigned int verbose;
unsigned int get_thin_calls,get_thin_iteractions; // to monitor the search (in-) efficiency find_slice
int ilast1,ilast2; // keep last two found find_slice, useful in recursive searches which switch between slices and parents
};
class SeqElList // sequence with elements considered for slicing
{
public:
SeqElList(const std::string& seqname,const std::string& slice_style,sequence* thick_sequ,sequence* sliced_seq,node* thick_node); // constructor
~SeqElList(); // destructor
void Print() const;
void slice_node();
node* thick_node; // current node
private:
double simple_at_shift(const int slices, const int slice_no) const;
double teapot_at_shift(const int slices, const int slice_no) const;
double collim_at_shift(const int slices, const int slice_no) const;
double hybrid_at_shift(const int slices, const int slice_no) const;
double at_shift(const int slices, const int slice_no,const std::string& local_slice_style) const; // return at relative shifts from centre of unsliced magnet
void kn_ks_from_thick_elem(const element* thick_elem,command_parameter* kn_pars[4],command_parameter* ks_pars[4]) const; // read k0-k3, k0s-k3s in thick_elem and put them in kn_pars, ks_pars
command_parameter* make_k_list(const char* parnam,command_parameter* k_pars[4],command_parameter* k_param) const; // from k values 0-3 to expr lists
element* sbend_from_rbend(const element* rbend_el);
element* create_thick_slice(element* thick_elem,const int slice_type);
element* create_sliced_magnet(const element* thick_elem, int slice_no,bool ThickSLice);
element* create_thin_solenoid(const element* thick_elem, int slice_no);
element* create_thin_elseparator(const element* thick_elem, int slice_no);
element* create_thin_obj(const element* thick_elem, int slice_no);
void slice_this_node(); // called in loop over nodes which can be sliced, makes slices, adds them to the sliced sequence
node* copy_thin(node* thick_node);
ElementListWithSlices *theSliceList, *RbendList, *theBendEdgeList; // Elements, of various types; consider to make separate lists for quadrupoles, sext .. to speed up search
std::string seqname; // name of the sequence
std::string slice_style;
sequence *thick_sequ, *sliced_seq;
unsigned int verbose;
const double eps;
bool MakeDipedge; // translate dipoles to dipedge, dipole without edge effects, dipedge
};
class SequenceList
{
public:
void put_sequ(sequence* thick_sequ); // add a sequence to the sequence list
sequence* get_sequ(sequence* thick_sequ); // check if thick_sequ is already there, if yes return the pointer to it, used to check if the sequence was already sliced
void Print() const;
void Reset();
private:
std::vector<sequence*> my_sequ_list_vec; // list of sequences
};
//------------------------------- source code --------------
static const int k_logical=0;
static const int k_int=1;
static const int k_double=2;
static const int k_cstring=3;
static const int k_int_array=11;
static const int k_double_array=12;
static const int k_cstring_array=13;
static const bool dipedge_h1_h2_fl=false; // normally false to avoid potentially non-simplectic partial higher order in dipedge. Optionally true as requested by Andrea Latina in 10/2014
static const bool kill_fringe_fl=true; // requested by Laurent et al., somewhat redundant, should be sufficient to check existance of non-default h1,e1; h2,e2 parameters
// check general options
inline bool debug_fl() { return get_option("debug"); }
inline bool verbose_fl() { return get_option("verbose"); }
inline bool thin_foc_fl() { return get_option("thin_foc"); }
inline bool rbarc_fl() { return get_option("rbarc"); } // by default on, then use (reduced) length of rbends
static void warning_to_c(std::ostringstream& WarnStr) { warning((WarnStr.str()).c_str(),""); }
static bool NameIsInList(const char *name, int n, const char *list[])
{
for(int i=0; i < n; ++i)
if(strcmp(name, list[i]) == 0) return true;
return false;
}
// --- Warning: 'my_' versions clone code from MAD-X
double my_get_expression_value(expression* ex) // check for NULL and update the value as done in dump_expression
{
double result=0;
if(ex)
{
result = expression_value(ex, 2); // also make sure the value stored agree with expression
ex->value = result; // also make sure the value stored agree with expression
}
return result;
}
static std::string my_dump_expression(expression* ex) // dump_expression in mad_expr.c only prints the value, here show also the expression and check for NULL
{
std::ostringstream ostr;
ostr << std::setprecision(15) << "expression ";
if(ex==NULL) ostr << " is NULL";
else
{
if(ex->string) ostr << " string=" << std::left << std::setw(20) << ex->string << std::right;
ostr << " value=" << my_get_expression_value(ex);
}
return ostr.str();
}
static double my_get_int_or_double_value(const element* el,const char* parnam,bool &found) // works for integer and double, also useful as my_get_int_or_double_value(el->base_type,char* parnam); to get the default values
{
// just returning el_par_value(parnam,base_el); or el_par_value_recurse(parnam,base_el);
// is not good enough, gets 0 for integer parameters and in some cases too specific - like checks for non-zero length of dipoles which does not allow to compare with the base type for dipoles
// rather do all directly here, descending from el / el->def / el->def->par to parameters[i], loop through them and look at integer and double values
// in case of expression uses the expression_value
double val=0;
found=false;
unsigned int verbose=0;
if(verbose_fl()) verbose=2;
// verbose=3;
if(el && el->def && el->def->par)
{
command_parameter_list* pl=el->def->par;
if(verbose>2) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " el->name=" << std::setw(15) << std::left << el->name;
for (int i = 0; i < pl->curr; ++i)
{
if(pl->parameters[i])
{
command_parameter* cp=pl->parameters[i];
if( !strcmp(cp->name, parnam) )
{
if(cp->expr)
{
val = my_get_expression_value(cp->expr);
if(verbose>2) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " el->name=" << std::setw(15) << std::left << el->name << " use the expression_value=" << val << '\n';
found=true;
}
else switch (cp->type)
{
case k_int: // int value of expression, actually same as double value
if(verbose>2) std::cout << " int ";
found=true;
val = cp->double_value;
break;
case k_double: // double value of expression
if(verbose>2) std::cout << " double ";
found=true;
val = cp->double_value;
break;
}
}
}
}
}
if(verbose>2)
{
std::cout << " parameter " << std::setw(15) << parnam;
if(found) std::cout << " found"; else std::cout << " not found";
std::cout << std::right << " val=" << val << '\n';
}
return val;
}
static std::string my_dump_name_list(const name_list* nl) // name_list defined in mad_name.h
{
std::ostringstream ostr;
ostr << std::setprecision(15) << "my_dump_name_list name i j inform max=" << std::setw(2) << nl->max << " curr=" << std::setw(2) << nl->curr << " name=" << std::setw(30) << nl->name << " list address=" << nl << std::endl; // max typically defined in new_name_list in mad_name.c
if(nl==NULL) ostr << " is NULL";
else
{
for (int i = 0; i < nl->curr; ++i)
{
const int j=nl->index[i];
std::string nl_name="NULL";
if(nl->names[j]) nl_name=std::string(nl->names[j]); else ostr << " *** code debug warning *** name for " << i << " is NULL, this should not happen" << '\n';
if(nl_name.length()>30) ostr << " *** code debug warning *** name for " << i << " is long, length=" << nl_name.length() << " looks like something was overwritten, name within quotes =\"" << nl_name << "\"" << '\n';
ostr
<< std::setw(30) << std::left << nl_name << std::right
<< std::setw(4) << i
<< std::setw(4) << j
<< std::setw(4) << nl->inform[j]
<< '\n';
}
}
return ostr.str();
}
static double cmd_par_val(const command_parameter* par) // return the double value given by expression or directly the value
{
double result=0;
if(par)
{
if(par->type==k_double)
{
if(par->expr) result=expression_value(par->expr,0); else result=par->double_value;
}
}
return result;
}
static std::string my_dump_command_parameter(const command_parameter* cp) // dump_command_parameter in mad_cmdpar.c only prints the value, here show also the expression by calling my_dump_expression and check for NULL
{
std::ostringstream ostr;
ostr << std::setprecision(15) << "my_dump_command_parameter ";
if(cp==NULL) ostr << " is NULL";
else
{
ostr << "parameter:" << std::left << std::setw(15) ;
ostr << cp->name;
ostr << std::right << " cp->type=" << std::setw(2) << cp->type;
ostr << " stamp=" << cp->stamp << " ";
double default_val=0;
const double eps=1.e-15; // used to check if strength is compatible with zero
switch (cp->type)
{
case k_logical:
ostr << "logical: ";
if( (int) cp->double_value) ostr << "true"; else ostr << "false";
ostr << '\n';
break;
case k_int: // int value of expression, actually same as double value
case k_double: // double value of expression
if(cp->expr) ostr << my_dump_expression(cp->expr); else ostr << " expression=NULL ";
if(cp->call_def) default_val=cp->call_def->double_value;
if(cp->expr==NULL && fabs(cp->double_value-default_val)>eps)
ostr << " value=" << std::setw(10) << cp->double_value << std::setw(10) << default_val;
ostr << '\n';
break;
case k_int_array: // int array, expr_list
case k_double_array: // double array, expr_list, used for example for Aperture, http://mad.web.cern.ch/mad/madx.old/Introduction/aperture.html
if (cp->double_array != NULL)
{
if (cp->expr_list != NULL) // calculate the values
{
ostr << "array of " << cp->double_array->curr << " ";
for (int ei = 0; ei < cp->double_array->curr; ++ei)
{
if (ei < cp->expr_list->curr && cp->expr_list->list[ei] != NULL)
{
ostr << std::right << std::setw(3) << ei << " :" << std::left << my_dump_expression(cp->expr_list->list[ei]) << std::right; // show expression and value
}
}
}
}
ostr << '\n';
break;
case k_cstring:
ostr << "cstring:";
if(cp->string) ostr << cp->string; else ostr << " NULL";
ostr << '\n';
break;
case k_cstring_array: // string array
dump_char_p_array(cp->m_string);
/* FALLTHRU */
case '?':
ostr << " cp->type=" << cp->type << " no info dump implemented so far" << '\n';
}
}
return ostr.str();
}
static std::string my_dump_command_parameter_list(command_parameter_list* pl)
{
std::ostringstream ostr;
ostr << std::setprecision(15) << "my_dump_command_parameter_list";
if(pl==NULL) ostr << " is NULL";
else
{
ostr << " name=" << pl->name;
ostr << " curr=" << pl->curr << " max=" << pl->max << '\n';
if(pl->curr > pl->max)
{
ostr << "*** error *** seen in my_dump_command_parameter_list max=" << pl->curr << " > " << " curr" << pl->curr << " set curr back to max" << '\n';
pl->curr = pl->max;
}
for (int i = 0; i < pl->curr; ++i)
{
ostr << std::setw(2) << i << " : ";
if(pl->parameters[i]) ostr << my_dump_command_parameter(pl->parameters[i]); else ostr << " NULL ";
}
}
return ostr.str();
}
static void SetParameterValue(const char* parnam,element* el,const double val,const int type=k_double) // set value and type, by default double
{
const int ei=name_list_pos(parnam,el->def->par_names);
if(ei > -1)
{
command_parameter* cp=el->def->par->parameters[ei];
if(cp)
{
if(verbose_fl())
{
std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " el->name=" << el->name << " parameter " << parnam
<< " was double_value=" << cp->double_value
<< " and type=" << cp->type;
if(cp->expr) std::cout << " has " << my_dump_expression(cp->expr); else std::cout << " no expression";
std::cout << " set to val=" << val
<< " and type=" << type << '\n';
}
if(cp->expr) cp->expr=NULL; // remove any expression
cp->double_value=val; // set the double value
cp->type=type; // set the type value
}
}
else
{
std::ostringstream WarnStr;
WarnStr << "SetParameterValue for parameter " << parnam << " failed for " << el->name << " parameter not in element name_list";
warning_to_c(WarnStr);
}
}
static void ParameterTurnOn(const char* parnam,element* el) // request that this parameter is written to output
{
const int ei=name_list_pos(parnam,el->def->par_names);
if(ei > -1) el->def->par_names->inform[ei]=1; // Turn on by setting inform to 1
else
{
std::ostringstream WarnStr;
WarnStr << "ParameterTurnOn for parameter " << parnam << " failed for " << el->name << " parameter not in element name_list ";
warning_to_c(WarnStr);
}
}
static void ParameterRemove(const char* parnam,element* el)
{
const int ei=name_list_pos(parnam,el->def->par_names);
if(ei > -1)
{
el->def->par_names->inform[ei]=0; // Turn off by setting inform to 0 -- effective in save, but still used in twiss; so really delete expression and turn value off
const int ei_base=name_list_pos(parnam,el->def->par_names);
double default_value=0;
if(ei_base) default_value=el->base_type->def->par->parameters[ei_base]->double_value; // el_par_value(parnam,el->base_type) cannot be used here, base element length may be zero
command_parameter* cp=el->def->par->parameters[ei];
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " in " << el-> name << " parameter" << std::setw(12) << parnam
<< " value=" << std::setw(6) << cp->double_value << " set to default=" << std::setw(6) << default_value
<< " for " << std::setw(12) << parnam << " cp->expr=" << cp->expr << " and set expression to NULL" << '\n';
cp->type = k_double;
cp->double_value = default_value;
cp->expr=NULL; // remove expression, -- without freeing space -- better memory leak than crash
// if(cp->expr != NULL ) delete_expression(cp->expr); // delete expression -- resulted in crash in thick_bends/bend.madx -- madX problem with poorly defined "objects"
}
}
static std::string Check_command_parameter_consistence(const command* cmd)
{
std::string EmptyStr="";
if(cmd==NULL) return EmptyStr;
command_parameter_list* cl=cmd->par;
name_list* nl=cmd->par_names;
if(cl==NULL || nl==NULL) return EmptyStr;
if(cl->curr<1 || nl->curr<1) return EmptyStr;
//-- at this point cmdpar and name_list exist and have at least one name, see if they match
std::vector<std::string> cl_names(cl->curr);
for(int i=0;i < cl->curr; ++i) cl_names[i]=cl->parameters[i]->name;
std::vector<std::string> nl_names(nl->curr);
for (int i = 0; i < nl->curr; ++i) nl_names[i]=nl->names[i];
unsigned int imax = (unsigned int) cl_names.size();
if(nl_names.size()>imax) imax = (unsigned int) nl_names.size();
int ierr=0;
if(cl_names.size() != nl_names.size()) ierr=1;
std::ostringstream ostr,ostr2;
if(verbose_fl() || ierr) ostr2 << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " #cmdpar names=" << cl_names.size() << " #name_list names=" << nl_names.size() << '\n'
<< " i cmdpar name name_list name" << '\n';
for(unsigned int i=0; i<imax; ++i)
{
if(verbose_fl())
{
ostr2 << std::setw(5) << i;
if(i<cl_names.size()) ostr2 << std::setw(20) << cl_names[i]; else ostr2 << " NULL ";
if(i<nl_names.size()) ostr2 << std::setw(20) << nl_names[i]; else ostr2 << " NULL ";
if(i<cl_names.size() && i<nl_names.size() && cl_names[i] !=nl_names[i]) ostr2 << " <----- not equal";
ostr2 << '\n';
}
if(i<cl_names.size() && i<nl_names.size() && cl_names[i] !=nl_names[i]) ierr++;
}
if(ierr)
{
ostr << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " *** ERROR *** " << " command " << cmd->name << " has inconsistent parameter_list and name_list names ierr=" << ierr << '\n' << ostr2.str() << '\n';
}
else if(verbose_fl()) ostr << "command " << cmd->name << " has consistent names" << '\n'; // in this case no need to show ostr2
// if(ierr) exit(ierr); // - exit in case of inconsistency - maybe useful for debugging after changes
return ostr.str();
}
static std::string my_dump_command(const command* cmd)
{
std::ostringstream ostr;
if(cmd==NULL) ostr << " is NULL";
else
{ // command defined in mad_elem.h, c-structures based on pointing to pointers, contains name_list par_names and command_parameter_list par
ostr << "command: " ; ostr << cmd->name;
ostr << " module: " ; ostr << cmd->module;
ostr << " group: " ; ostr << cmd->group;
ostr << " stamp= " << cmd->stamp << " link_type= " << cmd->link_type << " mad8_type= " << cmd->mad8_type;
ostr << " #par_names "; if(cmd->par_names->curr) ostr << cmd->par_names->curr; else ostr << " NULL";
ostr << " #par= " ; if(cmd->par->curr) ostr << cmd->group; else ostr << " NULL";
ostr << '\n';
ostr << "within command par_names:"; if(cmd->par_names) ostr << '\n' << my_dump_name_list(cmd->par_names); else ostr << " NULL" << '\n';
ostr << "within command par:"; if(cmd->par) ostr << '\n' << my_dump_command_parameter_list(cmd->par); else ostr << " NULL" << '\n';
}
ostr << '\n';
ostr << Check_command_parameter_consistence(cmd);
return ostr.str();
}
static bool copy_cmd_par(const char* from_name,const char* to_name,const element* from_el,command* cmd) // copy parameter (expression) from an element to another command used in a new element, possible to change name
{
const struct command_parameter* p = return_param_recurse(from_name, from_el);
if (!p) return false;
command_parameter* to_param = clone_command_parameter(p); // clone to allow for changes in copy, uses my const clone_command_parameter which checks for NULL
bool found=false;
double value =my_get_int_or_double_value(from_el ,from_name,found);
double default_val=my_get_int_or_double_value(from_el->base_type,from_name,found);
const double eps=1.e-20; // used to check if strength is compatible with zero
bool Meaningful_value= (found && fabs(value-default_val)>eps);
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " found=" << found << " Meaningful_value=" << Meaningful_value << " to_param " << my_dump_command_parameter(to_param);
if(Meaningful_value) // expression defined and non-trivial value
{
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " from_name=" << from_name << " to_name=" << to_name << " cloned to_param=" << to_param << " before strcpy " << my_dump_command_parameter(to_param);
strcpy(to_param->name, to_name); // put to_name to the cloned command_parameter*
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " from_name=" << from_name << " to_name=" << to_name << " cloned to_param=" << to_param << " after strcpy " << my_dump_command_parameter(to_param);
add_cmd_parameter_clone(cmd, to_param, to_name, 1);
return true;
}
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " " << from_name << " parameter not defined or on default values, do not copy " << my_dump_command_parameter(to_param) << '\n';
return false;
}
static void ParametersActiveOn(element* el) // turn active parameters on so that these values are written by save, where active means the expression exists or value different from default value,
{
if(el && el->def && el->def->par)
{
command_parameter_list* cmdpar=el->def->par;
for (int i = 0; i < cmdpar->curr; ++i)
{
command_parameter* cmdi=cmdpar->parameters[i];
char* parnam=cmdi->name;
if( cmdi->expr && (strcmp(cmdi->expr->string, none) != 0) ) // turn on when expression defined and not none
{
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " in " << el-> name << " has expression, turn on " << parnam << '\n';
ParameterTurnOn(parnam,el);
}
else // check for non-default value
{
const double eps=1.e-15; // used to check if strength is compatible with zero
bool found=false;
double default_val=my_get_int_or_double_value(el->base_type,parnam,found);
if(found && fabs(cmdi->double_value-default_val)>eps)
{
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " in " << el-> name
<< " value=" << std::setw(6) << cmdi->double_value << " default=" << std::setw(6) << default_val << " has non trivial value, turn on " << parnam << '\n';
ParameterTurnOn(parnam,el); // turn this parameter on
}
}
}
}
}
static std::string my_dump_element(const element* el)
{
std::ostringstream ostr;
ostr << std::setprecision(15) << '\n' << "my_dump_element";
if(el==NULL) ostr << " is NULL";
else
{ // element defined in mad_cmd.h, c-structures based on pointing to pointers
ostr << " name=" << el->name;
if(el->base_type) ostr << " base_type=" << el->base_type->name;
ostr << " stamp=" << el->stamp << " length=" << el->length << " parent name=" << el->parent->name;
ostr << " def_type=" << el->def_type;
if(el->def_type) ostr << " which means defined separately"; else ostr << " which means inside sequence";
ostr << '\n';
ostr << "within element " << my_dump_command(el->def);
}
return ostr.str();
}
static void Remove_All_Fringe_Field_Parameters(element* el)
{
static const char *FringePar[] = {"e1","e2","fint","fintx","h1","h2","hgap"};
const char* eltype=el->base_type->name;
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " el name=" << el->name << " type" << eltype << " before remove : " << my_dump_element(el) << '\n';
for(unsigned int i=0; i < ARRSIZE(FringePar); ++i) ParameterRemove(FringePar[i],el);
if(kill_fringe_fl)
{
SetParameterValue("kill_ent_fringe",el,true,k_logical);
SetParameterValue("kill_exi_fringe",el,true,k_logical);
ParameterTurnOn("kill_ent_fringe",el); // turn writing on
ParameterTurnOn("kill_exi_fringe",el); // turn writing on
}
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " el name=" << el->name << " type" << eltype << " after remove : " << my_dump_element(el) << '\n';
}
static std::string my_dump_node(const node* node)
{
std::ostringstream ostr;
ostr << std::setprecision(15) << '\n' << "my_dump_node";
if(node==NULL) ostr << " is NULL";
else
{
ostr << std::setprecision(15) << " node:";
char pname[NAME_L] = "NULL", nname[NAME_L] = "NULL", from_name[NAME_L] = "NULL";
if (node->previous != NULL) strcpy(pname, node->previous->name);
if (node->next != NULL) strcpy(nname, node->next->name);
if (node->from_name != NULL) strcpy(from_name, node->from_name);
ostr << " name=" << std::left << std::setw(20) << node->name << std::right
<< " occ=" << node->occ_cnt
<< " node->base_name=" << std::left << std::setw(15) << node->base_name << std::right
<< " from_name=" << std::left << std::setw(10) << from_name << std::right
<< " at_value=" << std::setw(10) << node->at_value
<< " position=" << std::setw(10) << node->position
<< " previous=" << std::left << std::setw(15) << pname
<< " next=" << std::setw(15) << nname << std::right
<< " at_expr: ";
if(node->at_expr) ostr << my_dump_expression(node->at_expr); else ostr << "NULL ";
if(node->p_elem) ostr << my_dump_element(node->p_elem);
if(node->cl!=NULL) for(int i=0; i< node->cl->curr; ++i) dump_constraint(node->cl->constraints[i]);
}
ostr << '\n';
return ostr.str();
}
static std::string my_dump_sequence(const sequence* c_sequ,const int level)
{ // level 1 little info, 3 dump also nodes, 4 dump also elements
std::ostringstream ostr;
if(c_sequ==NULL) ostr << "sequence is NULL";
else
{
node* c_node;
double suml = zero;
ostr << "sequence:" << c_sequ->name;
if(c_sequ->refpos) ostr << " refpos=" << c_sequ->refpos; else ostr << " refpos=NULL";
if(c_sequ->next_sequ) ostr << " next_sequ=" << c_sequ->next_sequ; else ostr << " next_sequ=NULL";
ostr << " ref_flag=" << c_sequ->ref_flag; // -1 for exit, 0 for centre, 1 for entry
if(c_sequ->ref_flag==-1) ostr << " (exit) ";
else if(c_sequ->ref_flag==0) ostr << " (centre) ";
else if(c_sequ->ref_flag==1) ostr << " (entry) ";
ostr << " share=" << c_sequ->share << " nested=" << c_sequ->nested << " con_cnt=" << c_sequ->con_cnt << " stamp=" << c_sequ->stamp << " line=" << c_sequ->line << " add_pass=" << c_sequ->add_pass << " length=" << c_sequ->length << '\n';
c_node = c_sequ->start;
ostr << std::setprecision(15);
double lastvalue=0;
while(c_node != NULL)
{
suml += c_node->length;
if (level > 2)
{
ostr << my_dump_node(c_node);
if (level > 3 && c_node->p_elem != NULL) ostr << my_dump_element(c_node->p_elem);
}
else if (level > 0 && strcmp(c_node->base_name, "drift") != 0)
{
ostr << std::left << std::setw(20) << c_node->name << std::right
<< " at_value=" << std::setw(10) << c_node->at_value
<< " position=" << std::setw(6) << c_node->position
<< " length=" << std::setw(17) << c_node->length;
if(c_node->from_name) ostr << " from " << c_node->from_name;
if(c_node->at_expr) ostr << " at_expr " << my_dump_expression(c_node->at_expr);
if(c_node->at_expr) { double currentvalue=my_get_expression_value(c_node->at_expr); ostr << " diff=" << currentvalue-lastvalue; lastvalue=currentvalue; }
ostr << '\n';
}
if (c_node == c_sequ->end) break;
c_node = c_node->next;
}
ostr << "===== sum of node length=" << std::setw(8) << suml << '\n';
ostr << '\n';
}
return ostr.str();
}
static std::string my_get_cmd_expr_str(const command_parameter* cmd) // return the expression as string, if there is only a value, return the value as string
{
std::string result="";
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " " << my_dump_command_parameter(cmd);
if(cmd)
{
if(cmd->expr && cmd->expr->string) result=cmd->expr->string; // the expression is define as string, use this
else // look for a value
{
const double eps=1.e-15; // used to check if strength is compatible with zero
if( fabs(cmd->double_value)>eps ) // value defined and non-zero
{
std::ostringstream ostr;
if(cmd->double_value<0) ostr << "("; // enclose negative value in brackets
ostr << cmd->double_value;
if(cmd->double_value<0) ostr << ")"; // enclose negative value in brackets
result=ostr.str();
}
}
}
if(verbose_fl()) std::cout << " my_get_cmd_expr_str result=" << result << '\n';
return result;
}
static expression* my_get_param_expression(const element* el,const char* parnam) // get a new copy of the expression for a parameter from an element, use the value as new expression if the expression was NULL
{
const int ipar = name_list_pos(parnam,el->def->par_names);
const command_parameter* cmdpar = NULL;
if(ipar > -1) cmdpar = el->def->par->parameters[ipar]; else return NULL; // pointer to the original length parameter
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " for element " << std::setw(20) << el->name << " parameter " << std::setw(20) << parnam << " ipar=" << ipar << " my_dump_expression(cmdpar->expr):" << my_dump_expression(cmdpar->expr) << " cmdpar->double_value=" << cmdpar->double_value << '\n';
command_parameter* cmdpar_copy = clone_command_parameter( cmdpar ); // copy of the original length parameter that can be modified
// if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << "clone_command_parameter done" << '\n';
if(cmdpar_copy->expr==NULL)
{ // use the value as new expression if the expression was NULL
std::ostringstream ostr;
ostr << std::setprecision(15) << cmdpar->double_value; // use the value as string
cmdpar_copy->expr = new_expression(ostr.str().c_str(),deco); // where deco is a global. // cmdpar_copy->expr->value = cmdpar->double_value; // seems to have no effect and this not needed
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " cmdpar_copy->expr was NULL, create new expression from string " << ostr.str() << " now " << my_dump_expression(cmdpar_copy->expr) << '\n';
}
// if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << "done" << '\n';
return cmdpar_copy->expr;
}
static bool thick_fl(const element* el) // true if the element has a thick parameter and if the value is positive, 0 otherwise
{
const int thick_pos = name_list_pos("thick", el->def->par_names);
return (thick_pos > -1 && el->def->par->parameters[thick_pos]->double_value > 0);
}
static void dump_slices() // Loops over all current elements and prints the number of slices. Used for debug and info, uses global element_list
{
unsigned int verbose=0;
if(verbose_fl()) verbose=2;
// verbose=3; // special for code development, shows all elements in element_list
printf("++++++ dump_slices");
if(verbose>1) printf(" verbose on, all elements are listed\n"); else printf(" only elements with non default selection (other than 1 thin) are shown\n");
if(verbose>2) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " verbose=" << verbose << " list all elements" << '\n';
printf(" name #slices derived from #slices\n");
int n_elem_with_slice=0,n_elem_with_slice_gt_1=0;
for(int i=0; i< element_list->curr; ++i) // loop over element_list
{
element* el_i = element_list->elem[i];
int el_i_slice_pos = name_list_pos("slice",el_i->def->par_names);
if(el_i_slice_pos > -1) // element with slice number defined
{
n_elem_with_slice++;
const int slices=el_i->def->par->parameters[el_i_slice_pos]->double_value;
int slices_parent=0;
const char* parent_name="no parent";
if(el_i->parent!=NULL) // look also at parent if existing
{
slices_parent=el_i->parent->def->par->parameters[el_i_slice_pos]->double_value;
parent_name=el_i->parent->name;
}
if(slices>1) n_elem_with_slice_gt_1++;
if(verbose_fl() || slices !=1 || thick_fl(el_i)) // print all with verbose. with debug skip elements with default selection thin 1 slice
{
printf(" %15s %2d",el_i->name,slices);
if(thick_fl(el_i)) printf(" thick"); else printf(" thin ");
if(el_i != el_i->parent) {
printf("%18s %2d",parent_name,slices_parent); // show also parent if not same as child
if(thick_fl(el_i->parent)) printf(" thick"); else printf(" thin ");
}
printf("\n");
}
}
else if(verbose>2) std::cout << std::setw(16) << el_i->name << '\n'; // no slice number defined, just give the name
}
if(verbose>1) std::cout << " general option thin_foc=" << thin_foc_fl() << '\n'; // global option like debug or verbose, not element specific, still print here for info
printf("------ end of dump slices. There were %4d elements, %3d with slice numbers and %2d with slice numbers>1\n\n",element_list->curr,n_elem_with_slice,n_elem_with_slice_gt_1);
}
static void force_consistent_slices() // hbu 10/2005 loop over all elements and check that #slices of child and parent agree, if not, use the maximum for both
{
for(int i=0; i< element_list->curr; ++i) // loop over element_list
{
element* el_i = element_list->elem[i];
int el_i_slice_pos = name_list_pos("slice",el_i->def->par_names);
if(el_i_slice_pos > -1 && el_i->parent!=NULL && el_i != el_i->parent )
{
command_parameter* child=el_i->def->par->parameters[el_i_slice_pos];
command_parameter* parent=el_i->parent->def->par->parameters[el_i_slice_pos];
int slices=child->double_value;
int slices_parent=parent->double_value;
if(slices != slices_parent)
{
if(slices>slices_parent) slices_parent=slices; else slices=slices_parent;
child->double_value=slices;
parent->double_value=slices_parent;
int el_i_thick_pos = name_list_pos("thick",el_i->def->par_names);
if(el_i_thick_pos > -1) el_i->parent->def->par->parameters[el_i_thick_pos]->double_value = el_i->def->par->parameters[el_i_thick_pos]->double_value; // copy thick flag from child to parent
}
}
}
if (debug_fl()) { printf("end of force_consistent_slices\n"); dump_slices(); }
}
static int get_slices_from_elem(const element* elem)
{
int elem_slice_pos=0,slices=1;
if( (elem_slice_pos = name_list_pos("slice",elem->def->par_names)) > -1 ) slices=elem->def->par->parameters[elem_slice_pos]->double_value;
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " elem_slice_pos=" << elem_slice_pos << " slices=" << slices << '\n';
return slices;
}
static char* make_thin_name(const char* e_name,const int slice) // make a node name from element name and slice number
{ // example e_name=mqxa.1r1 slice=1 result=mqxa.1r1..1
char name[2*NAME_L];
assert(strlen(e_name) < NAME_L);
if (sprintf(name, "%s..%d", e_name, slice) >= NAME_L)
warning("slice name is too long, truncated at " mkstring(NAME_L) " characters", name);
name[NAME_L-1] = '\0';
return permbuff(name);
}
static command_parameter*
scale_and_slice(command_parameter* kn_param,const command_parameter* length_param,const int slices,const bool mult_with_length,const int kl_flag) // multiply the k by length and divide by slice
{
int last_non_zero=-1;
if (kn_param == nullptr) return nullptr;
const double eps=1.e-15; // used to check if strength is compatible with zero
for (int i=0; i<kn_param->expr_list->curr; ++i)
{
expression* kn_i_expr = kn_param->expr_list->list[i];
double kn_i_val = kn_param->double_array->a[i];
if ((kn_i_expr != NULL && zero_string(kn_i_expr->string)==0) || fabs(kn_i_val)>eps )
{
last_non_zero=i;
if (kl_flag == 0 && (mult_with_length||i>0)) // apply mult_with_length only to zero order multipole
{
if (length_param->expr || kn_i_expr)
kn_i_expr = compound_expr(kn_i_expr, kn_i_val, "*", length_param->expr, length_param->double_value); // multiply expression with length
else kn_i_val *= length_param->double_value; // multiply value with length
}
if (slices > 1) // give the correct weight by slice (divide by the number of slices)
{
if (kn_i_expr) kn_i_expr = compound_expr(kn_i_expr,kn_i_val,"/",NULL,slices);
else kn_i_val *= 1./slices;
}
if(verbose_fl()) { printf("verbose %s %s line %d kn_i_val=%f kl_flag=%d\n",__FILE__,__FUNCTION__,__LINE__,kn_i_val,kl_flag); if(kn_i_expr) std::cout << my_dump_expression(kn_i_expr) << '\n'; }
}
if(kn_i_expr) kn_param->expr_list->list[i] = kn_i_expr;
kn_param->double_array->a[i] = kn_i_val;
} // for i ..
if (last_non_zero==-1)
{
delete_command_parameter(kn_param);
kn_param=nullptr;
}
return kn_param;
}
static void add_lrad(command* cmd,const command_parameter* length_param,const int slices)
{
command_parameter* l_par;
if(length_param)
{
add_cmd_parameter_new(cmd,0.,"l",1); // new parameter l with value of 0
l_par = cmd->par->parameters[cmd->par->curr] = clone_command_parameter(length_param); // keep what was l
strcpy(l_par->name,"lrad"); // but rename to lrad and slice :
if (slices > 1) // divide numbers or expressions by the number of slices
{
if (l_par->expr) l_par->expr = compound_expr(l_par->expr,0.,"/",NULL,slices);
else l_par->double_value /= slices;
}
add_to_name_list("lrad",1,cmd->par_names);
cmd->par->curr++;
}
}
static element* new_element(const char* el_type, const char* el_name,
size_t size, const char* ParList[], const element* thick_el)
{
int imarker_pos = name_list_pos(el_type, defined_commands->list);
command* p = defined_commands->commands[imarker_pos];
const int mx=p->par->curr; // maximum number of par names and parvalues -- take from definition
command* cmd = new_command(p->name, mx, mx, p->module, p->group, p->link_type,p->mad8_type);
for(unsigned int i=0; i<size; ++i)
add_cmd_parameter_clone(cmd,
return_param_recurse(ParList[i], thick_el), ParList[i], 1); // copy specified attributes from thick_el
return make_element(el_name, el_type, cmd,-1);
}
static element* new_marker_element(const char* el_type,const char* el_name,const element* thick_el)
{
const char *ParList[] = {
"at","kmax","kmin","polarity","calib","type","apertype","aperture","aper_offset","aper_tol","mech_sep","v_pos","from"
};
return new_element(el_type, el_name, ARRSIZE(ParList), ParList, thick_el);
}
static expression* curved_from_straight_length(const element* rbend_el)
{
expression* l_rbend_expr = my_get_param_expression(rbend_el,"l"); // get expression or create new from constant
expression* l_sbend_expr = NULL;
if(rbarc_fl() && l_rbend_expr ) // increase the straight rbend length to sbend length
{ // Mad-X very confusing for RBEND, "l" parameter el_par_value("l","rbend") is the shorter straight length, val = l * angle / (two * sin(angle/two)); with rbarc on as done by default
// this is also shown in twiss node and element length give always the curved length
// in going from rbend to sbend, this correction must be applied if the "l" expression is used, not for the value
std::string anglestr = my_get_cmd_expr_str( return_param_recurse("angle", rbend_el) );
// const string rat = "("+anglestr+")*0.5/sin(("+anglestr+")/2)"; // L_sbend / L_rbend
// LD (16.06.2015): quick and dirty fix to broken inheritance of attributes,
// set angle to 0 (default) when the returned string is empty
if (anglestr == "") anglestr = "0";
const std::string rat = "1.0/sinc("+anglestr+"*0.5)"; // L_sbend / L_rbend
expression* rat_expr = new_expression(rat.c_str(),deco);
// try status=0 or 1 to update
l_sbend_expr = compound_expr(l_rbend_expr,0,"*",rat_expr,0); // this also updates the value
if(verbose_fl())
{
bool found=false;
double straight_length=my_get_int_or_double_value(rbend_el,"l",found);
std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " " << rbend_el->name << " rbarc on, increase rbend straight length expression of value " << straight_length << " to curved sbend length using anglestr=" << anglestr
<< " updated l_sbend_expr " << my_dump_expression(l_sbend_expr) << " value should now be same as the curved rbend_el->length=" << rbend_el->length << '\n';
}
}
else l_sbend_expr=l_rbend_expr;
return l_sbend_expr;
}
static void add_node_at_end_of_sequence(node* node,sequence* sequ) // position in thin sequence defined with at_value and from_name
{
if (sequ->start == NULL) // first node in new sequence
{
sequ->start = node;
node->next = NULL;
node->previous = NULL;
}
else // add to end
{
sequ->end->next = node;
node->previous = sequ->end;
}
sequ->end = node;
// node->at_expr=NULL; // use this to only write at values, no expressions
if(verbose_fl())
{
std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " " << std::left << std::setw(20) << node->name << " " << std::setw(20) << node->base_name << std::right
<< " position=" << std::setw(10) << node->position << " at_value=" << std::setw(10) << node->at_value;
if(node->at_expr) std::cout << " " << my_dump_expression(node->at_expr);
if(node->from_name) std::cout << " from " << std::setw(5) << node->from_name; else std::cout << " ";
std::cout << " length=" << std::setw(10) << node->length << " in " << sequ->name << '\n';
}
add_to_node_list(node, 0, sequ->nodes);
return;
}
static void add_half_angle_to(const element* rbend_el,element* to_el,const char* to_parm) // get half surface angle of rbend, add to e1 or e2 of dipedge or sbend
{
if(rbend_el && to_el)
{
expression* half_angle_expr = scale_expr(my_get_param_expression(rbend_el,"angle"),0.5); // angle*0.5
command_parameter* to_param = return_param_recurse(to_parm,to_el); // get param from element, may not exist, use here the non const version of return_param_recurse, to modify to_el
if(to_param) // modify the existing parameter in to_el
{
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " original to_param " << my_dump_command_parameter(to_param) << '\n';
to_param->expr = compound_expr( my_get_param_expression(to_param) ,0,"+",half_angle_expr,0);
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " now to_param " << my_dump_command_parameter(to_param) << '\n';
}
else // param not yet in to_el, start from parameter definition
{
int ipar = name_list_pos(to_parm,to_el->def->par_names);
if(ipar > -1) // already in name_list
{
to_param = clone_command_parameter( to_el->def->par->parameters[ipar] ); // copy of the original length parameter that can be modified
to_el->def->par->parameters[ipar]->expr=half_angle_expr;
ParameterTurnOn(to_parm,to_el);
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " use existing to_param from ipar= " << ipar
<< " to_param=" << to_param
<< " to_el->def->par->parameters[ipar]->expr=" << to_el->def->par->parameters[ipar]->expr << '\n';
}
else // not in name_list_pos
{
ipar = name_list_pos(to_parm,to_el->base_type->def->par_names); // parameter in the definition, must always exist
if(ipar > -1)
{
to_param = clone_command_parameter( to_el->base_type->def->par->parameters[ipar] ); // copy of the original length parameter that can be modified
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " for element " << std::setw(20) << to_el->name << " parameter " << std::setw(20) << to_parm << " my_dump_expression(to_param->expr):" << my_dump_expression(to_param->expr) << " to_param->double_value=" << to_param->double_value << '\n';
}
else
{
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " *** error *** element " << std::setw(20) << to_el->name << " of type " << to_el->base_type->name << " has no parameter " << to_parm << '\n';
return;
}
to_param->expr=half_angle_expr; // set expression, which is NULL from definition, to half_angle_expr
if(verbose_fl()) std::cout << __FILE__<< " " << __FUNCTION__ << " line " << std::setw(4) << __LINE__ << " now to_param " << my_dump_command_parameter(to_param) << '\n';
add_cmd_parameter_clone(to_el->def,to_param,to_parm,1); // add new parameter to element, increases the name_list
}
}
}
}
static const char *CheckBendParams[] = {
"polarity", "tilt", "hgap", "mech_sep", "v_pos", "magnet", "model", "method", "exact", "nst" };
static element* create_bend_dipedge_element(element* thick_elem,const bool Entry) // using Dipedge http://mad.web.cern.ch/mad/madx.old/Introduction/dipedge.html
{
// see also twiss.f90 SUBROUTINE tmbend, and SUBROUTINE tmfrng for fringe fields, and SUBROUTINE tmdpdg for dipedge
// makes dipedge element for start or end of the dipole
// example
// from original thick
// mb1: sbend,l:=lmb ,angle:=amb ,k1:=kmb ,e1:=ee1 ,e2:=ee2 ;
// to
// mb1_l: dipedge, h:= amb/lmb ; e1:=ee1 ; !--------- new dipedge at entry
// mb1: sbend,l:=lmb ,angle:=amb ,k1:=kmb ; ! modified middle, e1, e2 removed
// mb1_r: dipedge, h:= amb/lmb ; e1:=ee2 ; !--------- new dipedge at exit
//
// request from Laurent Deniau and Andrea Latina in 10/2014 also move any h1, h2 parameters as h parameter to entry, exit dipedge
//
unsigned int verbose=0;
if(debug_fl()) verbose=1;
if(verbose_fl()) verbose=2;
// verbose=3; // extra debug - print element definitions
element* dipedge=NULL;
if (thick_elem)
{
std::string dipedge_name=std::string(thick_elem->name);