Skip to content

Commit c42535f

Browse files
committed
Add test
1 parent 974e701 commit c42535f

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

tests/inputs/madng/fodo.seq

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
circum = 60;
2+
lcell = 20;
3+
f := lcell/sin(pi/4)/4;
4+
k := 1/f;
5+
qf: multipole, knl := {0, k};
6+
qd: multipole, knl := {0, -k};
7+
bpm: monitor;
8+
9+
seq: sequence, refer=centre, l=circum;
10+
bpm1: bpm, at = 0;
11+
12+
qf: qf, at = 0;
13+
qd: qd, at = 0.5 * lcell;
14+
qf: qf, at = 1.0 * lcell;
15+
16+
bpm2: bpm, at = 1.5 * lcell;
17+
18+
qd: qd, at = 1.5 * lcell;
19+
qf: qf, at = 2.0 * lcell;
20+
qd: qd, at = 2.5 * lcell;
21+
22+
bpm3: bpm, at = 3 * lcell;
23+
endsequence;

tests/inputs/madng/fodo_track.mad

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
MADX:load("fodo.seq", "fodo.mad") -- convert on need
2+
local seq in MADX
3+
4+
local beam, track in MAD
5+
seq.beam = beam
6+
7+
local observed in MAD.element.flags
8+
seq:deselect(observed)
9+
seq:select(observed, {pattern="BPM"})
10+
local part1 = {x = 1e-3, px = 0, y =-1e-3, py = 0, t = 0, pt = 0}
11+
local part2 = {x =-1e-3, px = 0, y = 1e-3, py = 0, t = 0, pt = 0}
12+
13+
local mtbl = track {
14+
sequence = seq,
15+
X0 = {part1, part2},
16+
nturn = 3,
17+
}
18+
mtbl:write("fodo_track.tfs", {"name", "x", "y", "eidx", "turn", "id"})

tests/inputs/madng/fodo_track.tfs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ name %03s "SEQ"
2+
@ type %05s "track"
3+
@ title %03s "SEQ"
4+
@ origin %18s "MAD 0.9.9 Linux 64"
5+
@ date %08s "28/11/24"
6+
@ time %08s "18:31:03"
7+
@ refcol %04s "name"
8+
@ direction %le 1
9+
@ observe %le 1
10+
@ implicit %b false
11+
@ misalign %b false
12+
@ radiate %b false
13+
@ energy %le 1
14+
@ deltap %le 0
15+
@ lost %le 0
16+
* name x y eidx turn id
17+
$ %s %le %le %le %le %le
18+
"BPM1" 0.001 -0.001 2 1 1
19+
"BPM1" -0.001 0.001 2 1 2
20+
"BPM2" -0.0009999999503 0.00100000029 6 1 1
21+
"BPM2" 0.0009999999503 -0.00100000029 6 1 2
22+
"BPM3" 0.002414213831 0.0004142133507 10 1 1
23+
"BPM3" -0.002414213831 -0.0004142133507 10 1 2
24+
"BPM1" 0.002414213831 0.0004142133507 2 2 1
25+
"BPM1" -0.002414213831 -0.0004142133507 2 2 2
26+
"BPM2" -0.0004142138307 -0.002414213351 6 2 1
27+
"BPM2" 0.0004142138307 0.002414213351 6 2 2
28+
"BPM3" -0.0009999991309 0.001000000149 10 2 1
29+
"BPM3" 0.0009999991309 -0.001000000149 10 2 2
30+
"BPM1" -0.0009999991309 0.001000000149 2 3 1
31+
"BPM1" 0.0009999991309 -0.001000000149 2 3 2
32+
"BPM2" 0.0009999998012 -0.001000001159 6 3 1
33+
"BPM2" -0.0009999998012 0.001000001159 6 3 2
34+
"BPM3" -0.002414214191 -0.0004142129907 10 3 1
35+
"BPM3" 0.002414214191 0.0004142129907 10 3 2

tests/test_madng.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
import numpy as np
3+
import pandas as pd
4+
import pytest
5+
6+
from tests.test_lhc_and_general import INPUTS_DIR, compare_tbt
7+
from turn_by_turn.structures import TbtData, TransverseData
8+
from turn_by_turn.madng import read_tbt
9+
from pymadng import MAD
10+
11+
12+
def test_read_ng(_ng_file):
13+
with MAD() as mad:
14+
file_as_string = mad.quote_strings(str(_ng_file))
15+
df = mad.mtable.read(file_as_string).eval().to_df()
16+
new = read_tbt(df)
17+
origin = _original_simulation_data()
18+
compare_tbt(origin, new, True)
19+
20+
# ---- Helpers ---- #
21+
def _original_simulation_data() -> TbtData:
22+
# Create a TbTData object with the original data
23+
names = np.array(["BPM1", "BPM2", "BPM3"])
24+
bpm1_p1_x = np.array([ 1e-3, 0.002414213831,-0.0009999991309])
25+
bpm1_p1_y = np.array([-1e-3, 0.0004142133507, 0.001000000149])
26+
bpm1_p2_x = np.array([-1e-3,-0.002414213831, 0.0009999991309])
27+
bpm1_p2_y = np.array([ 1e-3,-0.0004142133507,-0.001000000149])
28+
29+
bpm2_p1_x = np.array([-0.0009999999503,-0.0004142138307, 0.0009999998012])
30+
bpm2_p1_y = np.array([ 0.00100000029,-0.002414213351,-0.001000001159])
31+
bpm2_p2_x = np.array([ 0.0009999999503, 0.0004142138307,-0.0009999998012])
32+
bpm2_p2_y = np.array([-0.00100000029, 0.002414213351, 0.001000001159])
33+
34+
bpm3_p1_x = np.array([ 0.002414213831,-0.0009999991309,-0.002414214191])
35+
bpm3_p1_y = np.array([ 0.0004142133507, 0.001000000149,-0.0004142129907])
36+
bpm3_p2_x = np.array([-0.002414213831, 0.0009999991309, 0.002414214191])
37+
bpm3_p2_y = np.array([-0.0004142133507,-0.001000000149, 0.0004142129907])
38+
39+
print(pd.DataFrame(index=names, data=[bpm1_p1_x, bpm2_p1_x, bpm3_p1_x]))
40+
matrix = [
41+
TransverseData( # first particle
42+
X=pd.DataFrame(index=names, data=[bpm1_p1_x, bpm2_p1_x, bpm3_p1_x]),
43+
Y=pd.DataFrame(index=names, data=[bpm1_p1_y, bpm2_p1_y, bpm3_p1_y]),
44+
),
45+
TransverseData( # second particle
46+
X=pd.DataFrame(index=names, data=[bpm1_p2_x, bpm2_p2_x, bpm3_p2_x]),
47+
Y=pd.DataFrame(index=names, data=[bpm1_p2_y, bpm2_p2_y, bpm3_p2_y]),
48+
),
49+
]
50+
return TbtData(matrices=matrix, bunch_ids=[0, 1], nturns=3)
51+
52+
53+
# ---- Fixtures ---- #
54+
@pytest.fixture
55+
def _ng_file(tmp_path):
56+
return INPUTS_DIR / "madng" / "fodo_track.tfs"

0 commit comments

Comments
 (0)