Skip to content

Commit 4490d30

Browse files
Merge branch 'release/0.97.0'
2 parents 6aa229e + 1e33ae3 commit 4490d30

File tree

89 files changed

+3723
-1543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3723
-1543
lines changed

examples/element_internal_record/000_internal_record.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ class TestElementRecord(xo.HybridClass):
3636
# element number.
3737

3838
track_method_source = r'''
39-
/*gpufun*/
40-
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0){
39+
#include "xtrack/headers/track.h"
4140
41+
GPUFUN
42+
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0)
43+
{
4244
// Extract the record and record_index
4345
TestElementRecordData record = TestElementData_getp_internal_record(el, part0);
4446
RecordIndex record_index = NULL;
45-
if (record){
47+
if (record) {
4648
record_index = TestElementRecordData_getp__index(record);
4749
}
4850
4951
int64_t n_kicks = TestElementData_get_n_kicks(el);
5052
printf("n_kicks %d\n", (int)n_kicks);
5153
52-
//start_per_particle_block (part0->part)
54+
START_PER_PARTICLE_BLOCK(part0, part);
5355
5456
for (int64_t i = 0; i < n_kicks; i++) {
5557
double rr = 1e-6 * RandomUniform_generate(part);
@@ -72,8 +74,7 @@ class TestElementRecord(xo.HybridClass):
7274
}
7375
}
7476
75-
76-
//end_per_particle_block
77+
END_PER_PARTICLE_BLOCK;
7778
}
7879
'''
7980

examples/element_internal_record/001_multirecord.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class TestElementRecord(xo.HybridClass):
4141
# code of the beam element.
4242

4343
TestElement_track_method_source = r'''
44-
/*gpufun*/
44+
#include "xtrack/headers/track.h"
45+
46+
GPUFUN
4547
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0){
4648
4749
// Extract the record and record_index
@@ -60,7 +62,7 @@ class TestElementRecord(xo.HybridClass):
6062
int64_t n_kicks = TestElementData_get_n_kicks(el);
6163
printf("n_kicks %d\n", (int)n_kicks);
6264
63-
//start_per_particle_block (part0->part)
65+
START_PER_PARTICLE_BLOCK(part0, part);
6466
6567
// Record in table1 info about the ingoing particle
6668
if (record){
@@ -106,7 +108,7 @@ class TestElementRecord(xo.HybridClass):
106108
}
107109
}
108110
109-
//end_per_particle_block
111+
END_PER_PARTICLE_BLOCK;
110112
}
111113
'''
112114

examples/element_internal_record/002_record_in_individual_element.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class TestElementRecord(xo.HybridClass):
4040
# code of the beam element.
4141

4242
TestElement_track_method_source = r'''
43-
/*gpufun*/
43+
#include "xtrack/headers/track.h"
44+
45+
GPUFUN
4446
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0){
4547
4648
// Extract the record and record_index
@@ -59,7 +61,7 @@ class TestElementRecord(xo.HybridClass):
5961
int64_t n_kicks = TestElementData_get_n_kicks(el);
6062
// printf("n_kicks %d\n", (int)n_kicks);
6163
62-
//start_per_particle_block (part0->part)
64+
START_PER_PARTICLE_BLOCK(part0, part);
6365
6466
// Record in table1 info about the ingoing particle
6567
if (record){
@@ -105,7 +107,7 @@ class TestElementRecord(xo.HybridClass):
105107
}
106108
}
107109
108-
//end_per_particle_block
110+
END_PER_PARTICLE_BLOCK;
109111
}
110112
'''
111113

examples/random_number_generator/001_usage_in_beam_element.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ class TestElement(xt.BeamElement):
2727

2828
_extra_c_sources = [
2929
'''
30-
/*gpufun*/
31-
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0){
32-
//start_per_particle_block (part0->part)
30+
#include "xtrack/headers/track.h"
31+
32+
GPUFUN
33+
void TestElement_track_local_particle(TestElementData el, LocalParticle* part0)
34+
{
35+
START_PER_PARTICLE_BLOCK(part0, part);
3336
double rr = !!GENERATOR!!_generate(part);
3437
LocalParticle_set_x(part, rr);
35-
//end_per_particle_block
38+
END_PER_PARTICLE_BLOCK;
3639
}
3740
'''.replace('!!GENERATOR!!', generator_to_test)
3841
]

examples/spin_lep/chirp_kicker.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ class VerticalChirpKicker(xt.BeamElement):
1919

2020
_extra_c_sources =['''
2121
22-
#include <headers/track.h>
23-
#include <beam_elements/elements_src/track_magnet.h>
22+
#include "xtrack/headers/track.h"
23+
#include "xtrack/beam_elements/elements_src/track_magnet.h"
2424
25-
/*gpufun*/
25+
GPUFUN
2626
void VerticalChirpKicker_track_local_particle(
27-
VerticalChirpKickerData el, LocalParticle* part0){
28-
27+
VerticalChirpKickerData el, LocalParticle* part0)
28+
{
2929
double const k0sl = VerticalChirpKickerData_get_k0sl(el);
3030
double const q_start = VerticalChirpKickerData_get_q_start(el);
3131
double const q_end = q_start + VerticalChirpKickerData_get_q_span(el);
3232
double const num_turns = VerticalChirpKickerData_get_num_turns(el);
3333
double const length = VerticalChirpKickerData_get_length(el);
3434
35-
//start_per_particle_block (part0->part)
35+
START_PER_PARTICLE_BLOCK(part0, part);
3636
double const at_turn = LocalParticle_get_at_turn(part);
37-
if (at_turn < num_turns){
37+
if (at_turn < num_turns) {
3838
// integrating to get the instantaneous phase
3939
double const phi = 2 * PI * q_start * at_turn
4040
+ PI * (q_end - q_start) / ((double) num_turns) * ((double) at_turn * at_turn);
@@ -60,9 +60,9 @@ class VerticalChirpKicker(xt.BeamElement):
6060
0, // frame curvature
6161
length,
6262
length // lpath - same for a thin element
63-
);
64-
#endif
63+
);
64+
#endif
6565
}
66-
//end_per_particle_block
66+
END_PER_PARTICLE_BLOCK;
6767
}
6868
''']

examples/true_rbend/000a_check_edge_full.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
l_ref = xt.Line([b_ref])
1313
l_ref.append('end', xt.Marker())
1414
l_ref.particle_ref = xt.Particles(p0c=10e9)
15-
tw_ref0 = l_ref.twiss(betx=1, bety=1)
15+
tw_ref0 = l_ref.twiss(betx=1, bety=1, strengths=True)
1616
tw_ref = l_ref.twiss(betx=1, bety=1, x=2e-3, px=1e-3, y=2e-3, py=2e-3, delta=1e-3)
1717

1818
b_test = xt.RBend(
1919
angle=0.1, k0_from_h=True, length_straight=3)
2020
b_test.rbend_model = 'straight-body'
2121
b_test.model = 'bend-kick-bend'
2222
b_test.num_multipole_kicks = 100
23+
b_test.rbend_compensate_sagitta = True
2324
b_test.edge_entry_model = edge_model
2425
b_test.edge_exit_model = edge_model
2526
l_test = xt.Line([b_test])
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import xtrack as xt
2+
import numpy as np
3+
import xobjects as xo
4+
5+
edge_model = 'full'
6+
7+
b_ref = xt.RBend(angle=0.1, k0_from_h=True, length_straight=3., rbend_angle_diff=0.1)
8+
b_ref.edge_entry_model = edge_model
9+
b_ref.edge_exit_model = edge_model
10+
b_ref.model = 'rot-kick-rot'
11+
b_ref.num_multipole_kicks = 100
12+
l_ref = xt.Line([b_ref])
13+
l_ref.append('end', xt.Marker())
14+
l_ref.particle_ref = xt.Particles(p0c=10e9)
15+
tw_ref0 = l_ref.twiss(betx=1, bety=1, strengths=True)
16+
tw_ref = l_ref.twiss(betx=1, bety=1, x=2e-3, px=1e-3, y=2e-3, py=2e-3, delta=1e-3)
17+
18+
b_test = xt.RBend(
19+
angle=0.1, k0_from_h=True, length_straight=3, rbend_angle_diff=0.1,
20+
rbend_shift=0, rbend_compensate_sagitta=False)
21+
b_test.rbend_model = 'straight-body'
22+
b_test.model = 'bend-kick-bend'
23+
b_test.num_multipole_kicks = 100
24+
b_test.edge_entry_model = edge_model
25+
b_test.edge_exit_model = edge_model
26+
27+
b_test.rbend_shift += b_test._x0_in
28+
29+
l_test = xt.Line([b_test])
30+
l_test.append('end', xt.Marker())
31+
l_test.particle_ref = xt.Particles(p0c=10e9)
32+
tw_test0 = l_test.twiss(betx=1, bety=1)
33+
tw_test = l_test.twiss(betx=1, bety=1, x=2e-3, px=1e-3, y=2e-3, py=2e-3, delta=1e-3)
34+
35+
xo.assert_allclose(tw_ref0.betx, tw_test0.betx, rtol=1e-9, atol=0.0)
36+
xo.assert_allclose(tw_ref0.bety, tw_test0.bety, rtol=1e-9, atol=0.0)
37+
xo.assert_allclose(tw_ref0.x, tw_test0.x, rtol=0, atol=1e-12)
38+
xo.assert_allclose(tw_ref0.y, tw_test0.y, rtol=0, atol=1e-12)
39+
xo.assert_allclose(tw_ref0.s, tw_test0.s, rtol=0, atol=1e-12)
40+
xo.assert_allclose(tw_ref0.zeta, tw_test0.zeta, rtol=0, atol=1e-11)
41+
xo.assert_allclose(tw_ref0.px, tw_test0.px, rtol=0, atol=1e-12)
42+
xo.assert_allclose(tw_ref0.py, tw_test0.py, rtol=0, atol=1e-12)
43+
44+
xo.assert_allclose(tw_ref.betx, tw_test.betx, rtol=5e-9, atol=0.0)
45+
xo.assert_allclose(tw_ref.bety, tw_test.bety, rtol=5e-9, atol=0.0)
46+
xo.assert_allclose(tw_ref.x, tw_test.x, rtol=0, atol=1e-12)
47+
xo.assert_allclose(tw_ref.y, tw_test.y, rtol=0, atol=1e-12)
48+
xo.assert_allclose(tw_ref.s, tw_test.s, rtol=0, atol=1e-12)
49+
xo.assert_allclose(tw_ref.zeta, tw_test.zeta, rtol=0, atol=1e-1)
50+
xo.assert_allclose(tw_ref.px, tw_test.px, rtol=0, atol=1e-12)
51+
xo.assert_allclose(tw_ref.py, tw_test.py, rtol=0, atol=1e-12)
52+
53+
l_sliced = l_test.copy(shallow=True)
54+
l_sliced.cut_at_s(np.linspace(0, l_test.get_length(), 100))
55+
tw_test_sliced0 = l_sliced.twiss(betx=1, bety=1)
56+
57+
xo.assert_allclose(tw_test_sliced0.betx[-1], tw_test0.betx[-1], rtol=1e-9, atol=0.0)
58+
xo.assert_allclose(tw_test_sliced0.bety[-1], tw_test0.bety[-1], rtol=1e-9, atol=0.0)
59+
xo.assert_allclose(tw_test_sliced0.x[-1], tw_test0.x[-1], rtol=0, atol=1e-12)
60+
xo.assert_allclose(tw_test_sliced0.y[-1], tw_test0.y[-1], rtol=0, atol=1e-12)
61+
xo.assert_allclose(tw_test_sliced0.s[-1], tw_test0.s[-1], rtol=0, atol=1e-12)
62+
xo.assert_allclose(tw_test_sliced0.zeta[-1], tw_test0.zeta[-1], rtol=0, atol=1e-11)
63+
xo.assert_allclose(tw_test_sliced0.px[-1], tw_test0.px[-1], rtol=0, atol=1e-12)
64+
xo.assert_allclose(tw_test_sliced0.py[-1], tw_test0.py[-1], rtol=0, atol=1e-12)
65+
66+
import matplotlib.pyplot as plt
67+
plt.close('all')
68+
69+
tw_test_sliced0.plot('x')
70+
plt.xlim(-0.1, 3.1)
71+
72+
plt.show()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import xtrack as xt
2+
import numpy as np
3+
import xobjects as xo
4+
5+
edge_model = 'linear'
6+
7+
b_ref = xt.RBend(angle=0.1, k0_from_h=True, length_straight=3., rbend_angle_diff=0.1)
8+
b_ref.edge_entry_model = edge_model
9+
b_ref.edge_exit_model = edge_model
10+
b_ref.model = 'rot-kick-rot'
11+
b_ref.num_multipole_kicks = 100
12+
l_ref = xt.Line([b_ref])
13+
l_ref.append('end', xt.Marker())
14+
l_ref.particle_ref = xt.Particles(p0c=10e9)
15+
tw_ref0 = l_ref.twiss(betx=1, bety=1)
16+
tw_ref = l_ref.twiss(betx=1, bety=1, x=2e-3, px=1e-3, y=2e-3, py=2e-3, delta=1e-3)
17+
18+
b_test = xt.RBend(
19+
angle=0.1, k0_from_h=True, length_straight=3, rbend_angle_diff=0.1,
20+
rbend_shift=0, rbend_compensate_sagitta=False)
21+
b_test.rbend_model = 'straight-body'
22+
b_test.model = 'bend-kick-bend'
23+
b_test.num_multipole_kicks = 100
24+
b_test.edge_entry_model = edge_model
25+
b_test.edge_exit_model = edge_model
26+
27+
b_test.rbend_shift += b_test._x0_in
28+
29+
l_test = xt.Line([b_test])
30+
l_test.append('end', xt.Marker())
31+
l_test.particle_ref = xt.Particles(p0c=10e9)
32+
tw_test0 = l_test.twiss(betx=1, bety=1)
33+
tw_test = l_test.twiss(betx=1, bety=1, x=2e-3, px=1e-3, y=2e-3, py=2e-3, delta=1e-3)
34+
35+
xo.assert_allclose(tw_ref0.betx, tw_test0.betx, rtol=1e-9, atol=0.0)
36+
xo.assert_allclose(tw_ref0.bety, tw_test0.bety, rtol=1e-9, atol=0.0)
37+
xo.assert_allclose(tw_ref0.x, tw_test0.x, rtol=0, atol=1e-12)
38+
xo.assert_allclose(tw_ref0.y, tw_test0.y, rtol=0, atol=1e-12)
39+
xo.assert_allclose(tw_ref0.s, tw_test0.s, rtol=0, atol=1e-12)
40+
xo.assert_allclose(tw_ref0.zeta, tw_test0.zeta, rtol=0, atol=1e-11)
41+
xo.assert_allclose(tw_ref0.px, tw_test0.px, rtol=0, atol=1e-12)
42+
xo.assert_allclose(tw_ref0.py, tw_test0.py, rtol=0, atol=1e-12)
43+
44+
xo.assert_allclose(tw_ref.betx, tw_test.betx, rtol=5e-6, atol=0.0)
45+
xo.assert_allclose(tw_ref.bety, tw_test.bety, rtol=5e-6, atol=0.0)
46+
xo.assert_allclose(tw_ref.x, tw_test.x, rtol=0, atol=2e-8)
47+
xo.assert_allclose(tw_ref.y, tw_test.y, rtol=0, atol=2e-8)
48+
xo.assert_allclose(tw_ref.s, tw_test.s, rtol=0, atol=2e-12)
49+
xo.assert_allclose(tw_ref.zeta, tw_test.zeta, rtol=0, atol=1e-1)
50+
xo.assert_allclose(tw_ref.px, tw_test.px, rtol=0, atol=5e-9)
51+
xo.assert_allclose(tw_ref.py, tw_test.py, rtol=0, atol=5e-9)
52+
53+
tw_back = l_test.twiss(init=tw_test, init_at='end')
54+
55+
assert tw_back.orientation == 'backward'
56+
xo.assert_allclose(tw_back.betx, tw_test.betx, rtol=5e-6, atol=0.0)
57+
xo.assert_allclose(tw_back.bety, tw_test.bety, rtol=5e-6, atol=0.0)
58+
xo.assert_allclose(tw_back.x, tw_test.x, rtol=0, atol=1e-8)
59+
xo.assert_allclose(tw_back.y, tw_test.y, rtol=0, atol=1e-8)
60+
xo.assert_allclose(tw_back.s, tw_test.s, rtol=0, atol=1e-12)
61+
xo.assert_allclose(tw_back.zeta, tw_test.zeta, rtol=0, atol=1e-1)
62+
xo.assert_allclose(tw_back.px, tw_test.px, rtol=0, atol=1e-9)
63+
xo.assert_allclose(tw_back.py, tw_test.py, rtol=0, atol=1e-9)
64+
65+
l_sliced = l_test.copy(shallow=True)
66+
l_sliced.cut_at_s(np.linspace(0, l_test.get_length(), 100))
67+
tw_test_sliced0 = l_sliced.twiss(betx=1, bety=1)
68+
69+
xo.assert_allclose(tw_test_sliced0.betx[-1], tw_test0.betx[-1], rtol=1e-9, atol=0.0)
70+
xo.assert_allclose(tw_test_sliced0.bety[-1], tw_test0.bety[-1], rtol=1e-9, atol=0.0)
71+
xo.assert_allclose(tw_test_sliced0.x[-1], tw_test0.x[-1], rtol=0, atol=1e-12)
72+
xo.assert_allclose(tw_test_sliced0.y[-1], tw_test0.y[-1], rtol=0, atol=1e-12)
73+
xo.assert_allclose(tw_test_sliced0.s[-1], tw_test0.s[-1], rtol=0, atol=1e-12)
74+
xo.assert_allclose(tw_test_sliced0.zeta[-1], tw_test0.zeta[-1], rtol=0, atol=1e-11)
75+
xo.assert_allclose(tw_test_sliced0.px[-1], tw_test0.px[-1], rtol=0, atol=1e-12)
76+
xo.assert_allclose(tw_test_sliced0.py[-1], tw_test0.py[-1], rtol=0, atol=1e-12)
77+
78+
tw_test_sliced_back = l_sliced.twiss(init=tw_test_sliced0, init_at='end')
79+
80+
assert tw_test_sliced_back.orientation == 'backward'
81+
xo.assert_allclose(tw_test_sliced_back.betx, tw_test_sliced0.betx, rtol=5e-9, atol=0.0)
82+
xo.assert_allclose(tw_test_sliced_back.bety, tw_test_sliced0.bety, rtol=5e-9, atol=0.0)
83+
xo.assert_allclose(tw_test_sliced_back.x, tw_test_sliced0.x, rtol=0, atol=1e-12)
84+
xo.assert_allclose(tw_test_sliced_back.y, tw_test_sliced0.y, rtol=0, atol=1e-12)
85+
xo.assert_allclose(tw_test_sliced_back.s, tw_test_sliced0.s, rtol=0, atol=1e-12)
86+
xo.assert_allclose(tw_test_sliced_back.zeta, tw_test_sliced0.zeta, rtol=0, atol=1e-11)
87+
xo.assert_allclose(tw_test_sliced_back.px, tw_test_sliced0.px, rtol=0, atol=1e-12)
88+
xo.assert_allclose(tw_test_sliced_back.py, tw_test_sliced0.py, rtol=0, atol=1e-12)
89+
90+
import matplotlib.pyplot as plt
91+
plt.close('all')
92+
93+
tw_test_sliced0.plot('x')
94+
plt.xlim(-0.1, 3.1)
95+
96+
plt.show()

0 commit comments

Comments
 (0)