EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH

Selection Statements

The elements, or a range of elements, in a sequence can be selected for various purposes. Such selections remain valid until cleared (in difference to MAD-8); it is therefore recommended to always start with a
select,flag=...,clear;
before setting a new selection.
SELECT,FLAG=name,RANGE=range,CLASS=class,PATTERN=pattern[,FULL][,CLEAR];
where the name for FLAG can be one of ERROR, MAKETHIN, SEQEDIT or the name of a twiss table which is established for all sequence positions in general.

Selected elements have to fulfill the RANGE, CLASS, and PATTERN criteria.

Any number of SELECT commands can be issued for the same flag and are accumulated (logically ORed). In this context note the following:

SELECT,FLAG=name,FULL;
selects all positions in the sequence for this flag. This is the default for all tables and makethin, whereas for ERROR and SEQEDIT the default is "nothing selected".

SAVE: A SELECT,FLAG=SAVE statement causes the selected sequences, elements, and variables to be written into the save file. A class (only used for element selection), and a pattern can be specified. Example:

select,flag=save,class=variable,pattern="abc.*";
save,file=mysave;
will save all variables (and sequences) containing "abc" in their name, but not elements with names containing "abc" since the class "variable" does not exist (astucieux, non ?).

SECTORMAP: A SELECT,FLAG=SECTORMAP statement causes sectormaps to be written into the file "sectormap" like in MAD-8. For the file to be written, a flag SECTORMAP must be issued on the TWISS command in addition.

TWISS: A SELECT,FLAG=TWISS statement causes the selected rows and columns to be written into the Twiss TFS file (former OPTICS command in MAD-8). The column selection is done on the same select. See as well example 2. Example 1:

TITLE,'Test input for MAD-X';

option,rbarc=false; // use arc length of rbends
beam; ! sets the default beam for the following sequence
option,-echo;
call file=fv9.opt;  ! contains optics parameters
call file="fv9.seq"; ! contains a small sequence "fivecell"
OPTION,ECHO;
SELECT,FLAG=SECTORMAP,clear;
SELECT,FLAG=SECTORMAP,PATTERN="^m.*";
SELECT,FLAG=TWISS,clear;
SELECT,FLAG=TWISS,PATTERN="^m.*",column=name,s,betx,bety;
USE,PERIOD=FIVECELL;
twiss,file=optics,sectormap;
stop;
This produces a file sectormap, and a twiss output file (name = optics):
@ TYPE             %05s "TWISS"
@ PARTICLE         %08s "POSITRON"
@ MASS             %le          0.000510998902
@ CHARGE           %le                       1
@ E0               %le                       1
@ PC               %le           0.99999986944
@ GAMMA            %le           1956.95136738
@ KBUNCH           %le                       1
@ NPART            %le                       0
@ EX               %le                       1
@ EY               %le                       1
@ ET               %le                       0
@ LENGTH           %le                   534.6
@ ALFA             %le        0.00044339992938
@ ORBIT5           %le                      -0
@ GAMMATR          %le           47.4900022541
@ Q1               %le           1.25413071556
@ Q2               %le           1.25485338377
@ DQ1              %le           1.05329608302
@ DQ2              %le           1.04837000224
@ DXMAX            %le           2.17763211131
@ DYMAX            %le                       0
@ XCOMAX           %le                       0
@ YCOMAX           %le                       0
@ BETXMAX          %le            177.70993499
@ BETYMAX          %le           177.671582415
@ XCORMS           %le                       0
@ YCORMS           %le                       0
@ DXRMS            %le           1.66004270906
@ DYRMS            %le                       0
@ DELTAP           %le                       0
@ TITLE            %20s "Test input for MAD-X"
@ ORIGIN           %16s "MAD-X 0.20 Linux"
@ DATE             %08s "07/06/02"
@ TIME             %08s "14.25.51"
* NAME               S                  BETX               BETY               
$ %s                 %le                %le                %le                
 "MSCBH"             4.365              171.6688159        33.31817319       
 "MB"                19.72              108.1309095        58.58680717       
 "MB"                35.38              61.96499987        102.9962313       
 "MB"                51.04              34.61640793        166.2227523       
 "MSCBV.1"           57.825             33.34442808        171.6309057       
 "MB"                73.18              58.61984637        108.0956006       
 "MB"                88.84              103.0313887        61.93159422       
 "MB"                104.5              166.2602486        34.58939635       
 "MSCBH"             111.285            171.6688159        33.31817319       
 "MB"                126.64             108.1309095        58.58680717       
 "MB"                142.3              61.96499987        102.9962313       
 "MB"                157.96             34.61640793        166.2227523       
 "MSCBV"             164.745            33.34442808        171.6309057       
 "MB"                180.1              58.61984637        108.0956006       
 "MB"                195.76             103.0313887        61.93159422       
 "MB"                211.42             166.2602486        34.58939635       
 "MSCBH"             218.205            171.6688159        33.31817319       
 "MB"                233.56             108.1309095        58.58680717       
 "MB"                249.22             61.96499987        102.9962313       
 "MB"                264.88             34.61640793        166.2227523       
 "MSCBV"             271.665            33.34442808        171.6309057       
 "MB"                287.02             58.61984637        108.0956006       
 "MB"                302.68             103.0313887        61.93159422       
 "MB"                318.34             166.2602486        34.58939635       
 "MSCBH"             325.125            171.6688159        33.31817319       
 "MB"                340.48             108.1309095        58.58680717       
 "MB"                356.14             61.96499987        102.9962313       
 "MB"                371.8              34.61640793        166.2227523       
 "MSCBV"             378.585            33.34442808        171.6309057       
 "MB"                393.94             58.61984637        108.0956006       
 "MB"                409.6              103.0313887        61.93159422       
 "MB"                425.26             166.2602486        34.58939635       
 "MSCBH"             432.045            171.6688159        33.31817319       
 "MB"                447.4              108.1309095        58.58680717       
 "MB"                463.06             61.96499987        102.9962313       
 "MB"                478.72             34.61640793        166.2227523       
 "MSCBV"             485.505            33.34442808        171.6309057       
 "MB"                500.86             58.61984637        108.0956006       
 "MB"                516.52             103.0313887        61.93159422       
 "MB"                532.18             166.2602486        34.58939635       

Example 2:

Addition of variables to (any internal) table:

   select,flag=table,column=name,s,betx,...,var1,var2,...; or
   select,flag=table,full,column=var1,var2,...; ! default col.s + new
will write the current value of var1 etc. into the table each time a new line is added; values from the same (current) line can be accessed by these variables, e.g.
   var1:= sqrt(beam->ex*table(twiss,betx));
in the case of table above being "twiss". The plot command accepts the new variables.

Remark: this replaces the "string" variables of MAD-8.

This example demonstrates as well the usage of a user defined table.

beam,ex=1.e-6,ey=1.e-3;
// element definitions
mb:rbend, l=14.2, angle:=0,k0:=bang/14.2;
mq:quadrupole, l:=3.1,apertype=ellipse,aperture={1,2};
qft:mq, l:=0.31, k1:=kqf,tilt=-pi/4;
qf.1:mq, l:=3.1, k1:=kqf;
qf.2:mq, l:=3.1, k1:=kqf;
qf.3:mq, l:=3.1, k1:=kqf;
qf.4:mq, l:=3.1, k1:=kqf;
qf.5:mq, l:=3.1, k1:=kqf;
qd.1:mq, l:=3.1, k1:=kqd;
qd.2:mq, l:=3.1, k1:=kqd;
qd.3:mq, l:=3.1, k1:=kqd;
qd.4:mq, l:=3.1, k1:=kqd;
qd.5:mq, l:=3.1, k1:=kqd;
bph:hmonitor, l:=l.bpm;
bpv:vmonitor, l:=l.bpm;
cbh:hkicker;
cbv:vkicker;
cbh.1:cbh, kick:=acbh1;
cbh.2:cbh, kick:=acbh2;
cbh.3:cbh, kick:=acbh3;
cbh.4:cbh, kick:=acbh4;
cbh.5:cbh, kick:=acbh5;
cbv.1:cbv, kick:=acbv1;
cbv.2:cbv, kick:=acbv2;
cbv.3:cbv, kick:=acbv3;
cbv.4:cbv, kick:=acbv4;
cbv.5:cbv, kick:=acbv5;
!mscbh:sextupole, l:=1.1, k2:=ksf;
mscbh:multipole, knl:={0,0,0,ksf},tilt=-pi/8;
mscbv:sextupole, l:=1.1, k2:=ksd;
!mscbv:octupole, l:=1.1, k3:=ksd,tilt=-pi/8;

// sequence declaration

fivecell:sequence, refer=centre, l=534.6;
   qf.1:qf.1, at=1.550000e+00;
   qft:qft, at=3.815000e+00;
!   mscbh:mscbh, at=3.815000e+00;
   cbh.1:cbh.1, at=4.365000e+00;
   mb:mb, at=1.262000e+01;
   mb:mb, at=2.828000e+01;
   mb:mb, at=4.394000e+01;
   bpv:bpv, at=5.246000e+01;
   qd.1:qd.1, at=5.501000e+01;
   mscbv:mscbv, at=5.727500e+01;
   cbv.1:cbv.1, at=5.782500e+01;
   mb:mb, at=6.608000e+01;
   mb:mb, at=8.174000e+01;
   mb:mb, at=9.740000e+01;
   bph:bph, at=1.059200e+02;
   qf.2:qf.2, at=1.084700e+02;
   mscbh:mscbh, at=1.107350e+02;
   cbh.2:cbh.2, at=1.112850e+02;
   mb:mb, at=1.195400e+02;
   mb:mb, at=1.352000e+02;
   mb:mb, at=1.508600e+02;
   bpv:bpv, at=1.593800e+02;
   qd.2:qd.2, at=1.619300e+02;
   mscbv:mscbv, at=1.641950e+02;
   cbv.2:cbv.2, at=1.647450e+02;
   mb:mb, at=1.730000e+02;
   mb:mb, at=1.886600e+02;
   mb:mb, at=2.043200e+02;
   bph:bph, at=2.128400e+02;
   qf.3:qf.3, at=2.153900e+02;
   mscbh:mscbh, at=2.176550e+02;
   cbh.3:cbh.3, at=2.182050e+02;
   mb:mb, at=2.264600e+02;
   mb:mb, at=2.421200e+02;
   mb:mb, at=2.577800e+02;
   bpv:bpv, at=2.663000e+02;
   qd.3:qd.3, at=2.688500e+02;
   mscbv:mscbv, at=2.711150e+02;
   cbv.3:cbv.3, at=2.716650e+02;
   mb:mb, at=2.799200e+02;
   mb:mb, at=2.955800e+02;
   mb:mb, at=3.112400e+02;
   bph:bph, at=3.197600e+02;
   qf.4:qf.4, at=3.223100e+02;
   mscbh:mscbh, at=3.245750e+02;
   cbh.4:cbh.4, at=3.251250e+02;
   mb:mb, at=3.333800e+02;
   mb:mb, at=3.490400e+02;
   mb:mb, at=3.647000e+02;
   bpv:bpv, at=3.732200e+02;
   qd.4:qd.4, at=3.757700e+02;
   mscbv:mscbv, at=3.780350e+02;
   cbv.4:cbv.4, at=3.785850e+02;
   mb:mb, at=3.868400e+02;
   mb:mb, at=4.025000e+02;
   mb:mb, at=4.181600e+02;
   bph:bph, at=4.266800e+02;
   qf.5:qf.5, at=4.292300e+02;
   mscbh:mscbh, at=4.314950e+02;
   cbh.5:cbh.5, at=4.320450e+02;
   mb:mb, at=4.403000e+02;
   mb:mb, at=4.559600e+02;
   mb:mb, at=4.716200e+02;
   bpv:bpv, at=4.801400e+02;
   qd.5:qd.5, at=4.826900e+02;
   mscbv:mscbv, at=4.849550e+02;
   cbv.5:cbv.5, at=4.855050e+02;
   mb:mb, at=4.937600e+02;
   mb:mb, at=5.094200e+02;
   mb:mb, at=5.250800e+02;
   bph:bph, at=5.336000e+02;
end:marker, at=5.346000e+02;
endsequence;

// forces and other constants

l.bpm:=.3;
bang:=.509998807401e-2;
kqf:=.872651312e-2;
kqd:=-.872777242e-2;
ksf:=.0198492943;
ksd:=-.039621283;
acbv1:=1.e-4;
acbh1:=1.e-4;
!save,sequence=fivecell,file,mad8;

s := table(twiss,bpv[5],betx);
myvar := sqrt(beam->ex*table(twiss,betx));
use, period=fivecell;
select,flag=twiss,column=name,s,myvar,apertype;
twiss,file;
n = 0;
create,table=mytab,column=dp,mq1,mq2;
mq1:=table(summ,q1);
mq2:=table(summ,q2);
while ( n < 11)
{
  n = n + 1;
  dp = 1.e-4*(n-6);
  twiss,deltap=dp;
  fill,table=mytab;
}
write,table=mytab;
plot,haxis=s,vaxis=aper_1,aper_2,colour=100,range=#s/cbv.1,notitle;
stop;
prints the following user table on output:
@ NAME             %05s "MYTAB"
@ TYPE             %04s "USER"
@ TITLE            %08s "no-title"
@ ORIGIN           %16s "MAD-X 1.09 Linux"
@ DATE             %08s "10/12/02"
@ TIME             %08s "10.45.25"
* DP                 MQ1                MQ2                
$ %le                %le                %le                
 -0.0005            1.242535951        1.270211135       
 -0.0004            1.242495534        1.270197018       
 -0.0003            1.242452432        1.270185673       
 -0.0002            1.242406653        1.270177093       
 -0.0001            1.242358206        1.270171269       
 0                  1.242307102        1.27016819        
 0.0001             1.242253353        1.270167843       
 0.0002             1.242196974        1.270170214       
 0.0003             1.24213798         1.270175288       
 0.0004             1.242076387        1.270183048       
 0.0005             1.242012214        1.270193477       
and produces a twiss file with the additional column myvar, as well as a plot file with the aperture values plotted.

Example of joing 2 tables with different length into a third table making use of the length of either table as given by table("your_table_name", tablelength) and adding names by the "_name" attribute.


title,   "summing of offset and alignment tables";
 set,    format="13.6f";

 readtable, table=align,  file="align.ip2.b1.tfs";   // mesured alignment
 readtable, table=offset, file="offset.ip2.b1.tfs";  // nominal offsets

 n_elem  =  table(offset, tablelength);

 create,  table=align_offset, column=_name,s_ip,x_off,dx_off,ddx_off,y_off,dy_off,ddy_off;

 calcul(elem_name) : macro = {
     x_off = table(align,  elem_name, x_ali) + x_off;
     y_off = table(align,  elem_name, y_ali) + y_off;
 }


 one_elem(j_elem) : macro = {
     setvars, table=offset, row=j_elem;
     exec,  calcul(tabstring(offset, name, j_elem));
     fill,  table=align_offset;
 }


 i_elem = 0;
 while (i_elem < n_elem) { i_elem = i_elem + 1; exec,  one_elem($i_elem); }

 write, table=align_offset, file="align_offset.tfs";


stop;
hansg, May 8, 2001