MAD-X Primer (4): マクロによる記法と誤差の導入

目的

  • MAD-X Primer Appendix 1.4 (Example 4)

  • マクロ サブルーチンを定義し、記載を再利用.

  • 誤差(磁場・アライン) を導入.


マクロの効用

  • すべてのエレメントに 一意の名前 をつけられる.

    • e.g.) 軌道補正など、すべてのエレメントを 名前ありの個別のオブジェクト として扱う場合

    • while文では別名前をつける記述は 不可

    • マクロであれば、 名前もマクロに与える番号からつけ直せる


誤差を与える命令文

  • 遅延定義 " := " で与えるのが 超重要

command

description

example

eoption

誤差解析のオプション ( add: 誤差重畳か、上書きか, seed: 乱数シード、の設定 )

eoption, add=false, seed=20240608

ealign

ミスアラインメントを設定する ( DX,DY,DS: 位置, DPHI: 回転、の誤差を与える.)

EALIGN, DX:=GAUSS()*0.001, DY:=GAUSS()*0.0005;

eprint

設定した誤差を表示

EPRINT, RANGE=MQF.1/MQD.10;

efcomp

誤差磁場.Error Field Componentsの略.

EFCOMP, RADIUS:=0.017, ORDER:=0, DKNR:={0,0,GAUSS()*7e-4, GAUSS()*1e-4,0}, DKSR:={0,0,GAUSS()*3e-4, GAUSS()*6e-4,0};


コード : macro-and-imperfections.seq : マクロによる記載


// サブルーチン inst : ”ラベル”がついたエレメントを挿入できる.
// nx と書かれた部分はすべて、nx の値で書き換えられる(関数でなく、”マクロ”)
inst(nx,n,lcell,lquad2): macro = {
    qf     : qf      , at=(n-1)*lcell+lquad2;
    lsf    : lsf     , at=(n-1)*lcell+lquad2+2.5;
    chnx   : hkicker , l=0.0,kick:=kchnx, at=(n-1)*lcell+lquad2+3.1;
    bpmhnx : monitor , l=0.0, at=(n-1)*lcell+lquad2+3.2;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+3.50;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+9.90;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+22.10;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+28.50;
    qd     : qd      , at=(n-1)*lcell+lquad2+32.00;
    lsd    : lsd     , at=(n-1)*lcell+lquad2+34.50;
    cvnx   : vkicker ,l=0.0,kick:=kcvnx, at=(n-1)*lcell+lquad2+35.10;
    bpmvnx : monitor ,l=0.0,at=(n-1)*lcell+lquad2+35.20;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+35.50;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+41.90;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+54.10;
    mbsps  : mbsps   , at=(n-1)*lcell+lquad2+60.50;
    n = n + 1;
}

// define the total length
circum=6912.0;
// define number of cells and therefore cell length
ncell = 108;
lcell = circum/ncell;
// define lengths of elements and half lengths
lquad = 3.085;
lquad2 = lquad/2.;
lquad3 = 0.0;
lmb = 6.260;
lmb2 = lmb/2.;
lsex = 1.0;
// forces and other constants;
// element definitions;

// define bending magnet as multipole
mbsps: multipole, lrad=dummy, l=lmb, knl={2.0*pi/(8*ncell)};

// define quadrupole and their strengths
qsps: quadrupole, l=lquad;
qf: qsps, k1:=kqf;
qd: qsps, k1:=kqd;
kqf = 1.4631475E-02;
kqd = -1.4643443E-02;

// define sextupoles for chromaticity correction
lsf: sextupole, l=lsex,k2:=ksf;
lsd: sextupole, l=lsex,k2:=ksd;
ksf = 2.0284442E-02;
ksd = -3.8394267E-02;

// define orbit correctors and beam position monitors
bpm: monitor, l=0.1;
ch: hkicker, l=0.1;
cv: vkicker, l=0.1;

// sequence declaration;
cassps: sequence, refer=centre, l=circum;
  start_machine: marker, at = 0;
  // This defines ONE cell, repeat NCELL times to get the full machine
  // SPS has 8 bending magnets per cell
  n = 1;
  while (n < ncell+1) {
        // here we call the macro, cell number n is argument and used for numbering the elements
        exec inst($n,n,lcell,lquad2);
    }
  end_machine: marker at=circum;
endsequence;

コード : macro-and-imperfections.madx : 誤差の導入


TITLE, s="MAD-X test";
option, -echo;

// --  ビームラインは、マクロ(サブルーチン) inst を使用して構成される. -- //
call, file="macro-and-imperfections.seq";

// -- ビーム -- //
Beam, particle=proton, sequence=cassps, energy=450.0;

// -- casspsを利用 -- // 
use, sequence=cassps;

// -- 誤差磁場を与える -- // 
eoption, add=false, seed=62971100;
select, flag=error, pattern="q.*";
ealign, dx:=tgauss(3.0)*1.0e-4, dy:=tgauss(3.0)*2.0e-4;
eprint;

// -- twiss で書き出す項目を設定 -- //
select, flag=twiss, class=monitor, column=name,s,x,betx;
select, flag=twiss, class=vkicker, column=name,s,x,betx;
select, flag=twiss, class=hkicker, column=name,s,x,betx;

// -- 幾何情報(座標系)の出力 -- //
survey, file="out/survey.tfs";

// -- TWISSパラメータの計算 -- //
twiss, save, centre, file="out/twiss.tfs";

ポイント

$n と n の違い

  • $nはマクロとして即時展開される : ソースコード中の nxの部分が書き換えられる.ラベルの番号付けなど.

  • nは文字列としてわたる.:

遅延評価 ":="

  • 遅延評価しないと、すべての誤差が 最初一回評価された値 で、 同じにずれる