Skip to content

Commit b9695f8

Browse files
Merge pull request #28 from lnls-fac/fix-eta-calc
Fix calculation of dispersion function.
2 parents 9d3fee3 + 0552aee commit b9695f8

File tree

5 files changed

+69
-64
lines changed

5 files changed

+69
-64
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434

3535
Debug/
3636
build/
37+
.vscode/

include/trackcpp/optics.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ Status::type calc_twiss(const Accelerator& accelerator,
4848
const Pos<double>& fixed_point,
4949
Matrix& m66,
5050
std::vector<Twiss>& twiss,
51-
Twiss twiss0 = Twiss(),
52-
bool closed_flag = false);
51+
Twiss twiss0 = Twiss());
5352

5453

5554
#endif

include/trackcpp/tracking.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
#include <limits>
2727
#include <cmath>
2828

29-
Status::type track_findm66 (const Accelerator& accelerator, std::vector<Pos<double> >& closed_orbit, std::vector<Matrix>& tm, Matrix& m66, Pos<double>& v0);
30-
Status::type track_findorbit4 (const Accelerator& accelerator, std::vector<Pos<double> >& closed_orbit, const Pos<double>& fixed_point_guess = Pos<double>(0));
31-
Status::type track_findorbit6 (const Accelerator& accelerator, std::vector<Pos<double> >& closed_orbit, const Pos<double>& fixed_point_guess = Pos<double>(0));
32-
Pos<double> linalg_solve4_posvec (const std::vector<Pos<double> >& M, const Pos<double>& b);
33-
Pos<double> linalg_solve6_posvec (const std::vector<Pos<double> >& M, const Pos<double>& b);
29+
Status::type track_findm66(const Accelerator& accelerator, const Pos<double>& fixed_point, std::vector<Matrix>& tm, Matrix& m66, Pos<double>& v0);
30+
Status::type track_findorbit4(const Accelerator& accelerator, std::vector<Pos<double> >& closed_orbit, const Pos<double>& fixed_point_guess = Pos<double>(0));
31+
Status::type track_findorbit6(const Accelerator& accelerator, std::vector<Pos<double> >& closed_orbit, const Pos<double>& fixed_point_guess = Pos<double>(0));
32+
Pos<double> linalg_solve4_posvec(const std::vector<Pos<double> >& M, const Pos<double>& b);
33+
Pos<double> linalg_solve6_posvec(const std::vector<Pos<double> >& M, const Pos<double>& b);
3434

3535
template <typename T>
3636
Status::type track_elementpass (

src/optics.cpp

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ Status::type calc_twiss(const Accelerator& accelerator,
6464
const Pos<double>& fixed_point,
6565
Matrix& m66,
6666
std::vector<Twiss>& twiss,
67-
Twiss twiss0,
68-
bool closed_flag) {
67+
Twiss twiss0) {
6968

7069
#ifdef TIMEIT
7170
auto start = std::chrono::steady_clock::now();
@@ -84,14 +83,7 @@ Status::type calc_twiss(const Accelerator& accelerator,
8483
unsigned int element_offset = 0;
8584
Status::type status = track_linepass(accelerator, fp, closed_orbit, element_offset, lost_plane, true);
8685
if (status != Status::success) return status;
87-
if (not closed_flag) closed_orbit.pop_back();
8886

89-
// std::vector<Pos<double>> closed_orbit0;
90-
// if (not accelerator.cavity_on) {
91-
// Pos<double> fp = fixed_point; fp.de = 0;
92-
// Status::type status = track_linepass(accelerator, fp, closed_orbit0, element_offset, lost_plane, true);
93-
// if (status != Status::success) return status;
94-
// }
9587

9688
#ifdef TIMEIT
9789
end = std::chrono::steady_clock::now(); diff = end - start;
@@ -102,22 +94,10 @@ Status::type calc_twiss(const Accelerator& accelerator,
10294
start = std::chrono::steady_clock::now();
10395
#endif
10496

105-
// finds accumulated transfer matrices
106-
107-
// std::vector<Matrix> atm0;
108-
// if (not accelerator.cavity_on) {
109-
// status = track_findm66 (accelerator, closed_orbit0, atm0, m66);
110-
// if (status != Status::success) return status;
111-
// if (closed_flag) atm.push_back(atm.back());
112-
// }
113-
11497
std::vector<Matrix> atm;
11598
Pos<double> v0;
116-
status = track_findm66 (accelerator, closed_orbit, atm, m66, v0);
99+
status = track_findm66(accelerator, closed_orbit[0], atm, m66, v0);
117100
if (status != Status::success) return status;
118-
if (closed_flag) atm.push_back(atm.back());
119-
120-
121101

122102
#ifdef TIMEIT
123103
end = std::chrono::steady_clock::now(); diff = end - start;
@@ -128,6 +108,11 @@ Status::type calc_twiss(const Accelerator& accelerator,
128108
start = std::chrono::steady_clock::now();
129109
#endif
130110

111+
const double dpp = 1e-8;
112+
Pos<double> fpp = fixed_point;
113+
fpp.de += dpp;
114+
std::vector<Pos<double>> codp;
115+
131116
// initial twiss parameters
132117
twiss.clear(); twiss.reserve(atm.size());
133118
if (twiss0.isundef()) { // case of periodic solution
@@ -141,15 +126,32 @@ Status::type calc_twiss(const Accelerator& accelerator,
141126
twiss0.betay = m66[2][3]/sin_muy;
142127
// --- closed orbit
143128
twiss0.co = closed_orbit[0];
144-
// --- dispersion function based on eta = (1 - M)^(-1) D
145-
Vector Dx({m66[0][4], m66[1][4]});
146-
Vector Dy({m66[2][4], m66[3][4]});
147-
Matrix eye2({{1,0},{0,1}});
148-
Matrix mx; m66.getM(mx, 2, 2, 0, 0); mx.linear_combination(1.0,eye2,-1.0,mx); mx.inverse();
149-
Matrix my; m66.getM(my, 2, 2, 2, 2); my.linear_combination(1.0,eye2,-1.0,my); my.inverse();
150-
twiss0.etax.multiplication(mx, Dx);
151-
twiss0.etay.multiplication(my, Dy);
129+
130+
// // --- dispersion function based on eta = (1 - M)^(-1) D
131+
// Vector Dx({m66[0][4], m66[1][4]});
132+
// Vector Dy({m66[2][4], m66[3][4]});
133+
// Matrix eye2({{1,0},{0,1}});
134+
// Matrix mx; m66.getM(mx, 2, 2, 0, 0); mx.linear_combination(1.0,eye2,-1.0,mx); mx.inverse();
135+
// Matrix my; m66.getM(my, 2, 2, 2, 2); my.linear_combination(1.0,eye2,-1.0,my); my.inverse();
136+
// twiss0.etax.multiplication(mx, Dx);
137+
// twiss0.etay.multiplication(my, Dy);
138+
139+
// Dispersion Function based on tracking:
140+
Status::type status = track_findorbit4(accelerator, codp, fpp);
141+
if (status != Status::success) return status;
142+
twiss0.etax[0] = (codp[0].rx - closed_orbit[0].rx) / dpp;
143+
twiss0.etax[1] = (codp[0].px - closed_orbit[0].px) / dpp;
144+
twiss0.etay[0] = (codp[0].ry - closed_orbit[0].ry) / dpp;
145+
twiss0.etay[1] = (codp[0].py - closed_orbit[0].py) / dpp;
146+
} else {
147+
fpp.rx += twiss0.etax[0] * dpp;
148+
fpp.px += twiss0.etax[1] * dpp;
149+
fpp.ry += twiss0.etay[0] * dpp;
150+
fpp.py += twiss0.etay[1] * dpp;
151+
Status::type status = track_linepass(accelerator, fpp, codp, element_offset, lost_plane, true);
152+
if (status != Status::success) return status;
152153
}
154+
153155
twiss.push_back(twiss0);
154156

155157
#ifdef TIMEIT
@@ -178,20 +180,25 @@ Status::type calc_twiss(const Accelerator& accelerator,
178180
// --- closed orbit
179181
tw.co = closed_orbit[i];
180182

181-
// --- dispersion function
182-
Matrix t1(atm[i-1]); t1.inverse();
183-
Matrix T; T.multiplication(atm[i],t1);
184-
Vector Dx({T[0][4], T[1][4]});
185-
Vector Dy({T[2][4], T[3][4]});
186-
Matrix mx; T.getMx(mx);
187-
Matrix my; T.getMy(my);
188-
tw.etax.multiplication(mx, twiss[i-1].etax);
189-
tw.etay.multiplication(my, twiss[i-1].etay);
190-
tw.etax = Dx + tw.etax.multiplication(mx, twiss[i-1].etax);
191-
tw.etay = Dy + tw.etay.multiplication(my, twiss[i-1].etay);
183+
// // --- dispersion function based on propagation
184+
// Matrix t1(atm[i-1]); t1.inverse();
185+
// Matrix T; T.multiplication(atm[i],t1);
186+
// Vector Dx({T[0][4], T[1][4]});
187+
// Vector Dy({T[2][4], T[3][4]});
188+
// Matrix mx; T.getMx(mx);
189+
// Matrix my; T.getMy(my);
190+
// tw.etax.multiplication(mx, twiss[i-1].etax);
191+
// tw.etay.multiplication(my, twiss[i-1].etay);
192+
// tw.etax = Dx + tw.etax.multiplication(mx, twiss[i-1].etax);
193+
// tw.etay = Dy + tw.etay.multiplication(my, twiss[i-1].etay);
194+
195+
// Dispersion Function based on tracking
196+
tw.etax[0] = (codp[i].rx - closed_orbit[i].rx) / dpp;
197+
tw.etax[1] = (codp[i].px - closed_orbit[i].px) / dpp;
198+
tw.etay[0] = (codp[i].ry - closed_orbit[i].ry) / dpp;
199+
tw.etay[1] = (codp[i].py - closed_orbit[i].py) / dpp;
192200

193201
twiss.push_back(tw);
194-
195202
}
196203

197204
// unwraps betatron phases

src/tracking.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,39 @@
2424
// returns a vector with 6-d transfer matrices, one for each element
2525
//
2626
// inputs:
27-
// accelerator: structure representing the accelerator
28-
// closed_orbit: Pos vector representing calculated closed orbit.
27+
// accelerator: Structure representing the accelerator
28+
// fixed_point: Pos representing calculated fixed_point.
2929
//
3030
// outputs:
3131
// tm: vector of Matrix6 elements. Each component represents the accumulated
32-
// transfer matrix from the start of the lattice to the entrance of that element.
32+
// transfer matrix from the start of the lattice to the entrance of that element. The last element is the m66 of the line.
3333
// m66: one-turn transfer matrix
3434
//
3535
// v0: const term of final map
3636
//
3737
// RETURN: status do tracking (see 'auxiliary.h')
3838

3939
Status::type track_findm66 (const Accelerator& accelerator,
40-
std::vector<Pos<double> >& closed_orbit,
40+
const Pos<double>& fixed_point,
4141
std::vector<Matrix>& tm,
4242
Matrix& m66,
4343
Pos<double>& v0) {
4444

4545
Status::type status = Status::success;
4646
const std::vector<Element>& lattice = accelerator.lattice;
4747

48-
std::vector<double> row0 = {0,0,0,0,0,0};
48+
Pos<double> fp = fixed_point;
4949

5050
// case no closed_orbit has been defined
51-
if (closed_orbit.size() != lattice.size()) {
52-
closed_orbit.clear();
53-
for(unsigned int i=0; i<lattice.size(); ++i) {
54-
closed_orbit.push_back(Pos<double>(0,0,0,0,0,0));
55-
}
56-
}
51+
if (std::isnan(fp.rx)) fp = Pos<double>(0,0,0,0,0,0);
52+
5753

5854
Pos<Tpsa<6,1> > map;
59-
map.rx = Tpsa<6,1>(closed_orbit[0].rx, 0); map.px = Tpsa<6,1>(closed_orbit[0].px, 1);
60-
map.ry = Tpsa<6,1>(closed_orbit[0].ry, 2); map.py = Tpsa<6,1>(closed_orbit[0].py, 3);
61-
map.de = Tpsa<6,1>(closed_orbit[0].de, 4); map.dl = Tpsa<6,1>(closed_orbit[0].dl, 5);
55+
map.rx = Tpsa<6,1>(fp.rx, 0); map.px = Tpsa<6,1>(fp.px, 1);
56+
map.ry = Tpsa<6,1>(fp.ry, 2); map.py = Tpsa<6,1>(fp.py, 3);
57+
map.de = Tpsa<6,1>(fp.de, 4); map.dl = Tpsa<6,1>(fp.dl, 5);
6258

63-
tm.clear(); tm.resize(lattice.size(), Matrix(6));
59+
tm.clear(); tm.resize(lattice.size()+1, Matrix(6));
6460
for(unsigned int i=0; i<lattice.size(); ++i) {
6561
Matrix& m = tm[i];
6662
m[0][0] = map.rx.c[1]; m[0][1] = map.rx.c[2]; m[0][2] = map.rx.c[3]; m[0][3] = map.rx.c[4]; m[0][4] = map.rx.c[5]; m[0][5] = map.rx.c[6];
@@ -82,6 +78,8 @@ Status::type track_findm66 (const Accelerator& accelerator,
8278
m[4][0] = map.de.c[1]; m[4][1] = map.de.c[2]; m[4][2] = map.de.c[3]; m[4][3] = map.de.c[4]; m[4][4] = map.de.c[5]; m[4][5] = map.de.c[6];
8379
m[5][0] = map.dl.c[1]; m[5][1] = map.dl.c[2]; m[5][2] = map.dl.c[3]; m[5][3] = map.dl.c[4]; m[5][4] = map.dl.c[5]; m[5][5] = map.dl.c[6];
8480

81+
tm[lattice.size()] = m66;
82+
8583
// constant term of the final map
8684
v0.rx = map.rx.c[0]; v0.px = map.px.c[0]; v0.ry = map.ry.c[0]; v0.py = map.py.c[0]; v0.de = map.de.c[0]; v0.dl = map.dl.c[0];
8785

0 commit comments

Comments
 (0)