Skip to content

Commit 9d3fee3

Browse files
Merge pull request #27 from lnls-fac/add-matrix-pm
Add matrix pass_method
2 parents e2b8847 + 26edaf4 commit 9d3fee3

File tree

11 files changed

+135
-10
lines changed

11 files changed

+135
-10
lines changed

include/trackcpp/auxiliary.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class PassMethodsClass {
3434
static const int pm_thinquad_pass = 6;
3535
static const int pm_thinsext_pass = 7;
3636
static const int pm_kicktable_pass = 8;
37-
static const int pm_nr_pms = 9;
37+
static const int pm_matrix_pass = 9;
38+
static const int pm_nr_pms = 10;
3839
PassMethodsClass() {
3940
passmethods.push_back("identity_pass");
4041
passmethods.push_back("drift_pass");
@@ -45,6 +46,7 @@ class PassMethodsClass {
4546
passmethods.push_back("thinquad_pass");
4647
passmethods.push_back("thinsext_pass");
4748
passmethods.push_back("kicktable_pass");
49+
passmethods.push_back("matrix_pass");
4850
}
4951
int size() const { return passmethods.size(); }
5052
std::string operator[](const int i) const { return passmethods[i]; }
@@ -64,7 +66,8 @@ struct PassMethod {
6466
pm_thinquad_pass = 6,
6567
pm_thinsext_pass = 7,
6668
pm_kicktable_pass = 8,
67-
pm_nr_pms = 9
69+
pm_matrix_pass = 9,
70+
pm_nr_pms = 10,
6871
};
6972
};
7073

@@ -79,7 +82,8 @@ const std::vector<std::string> pm_dict = {
7982
"cavity_pass",
8083
"thinquad_pass",
8184
"thinsext_pass",
82-
"kicktable_pass"
85+
"kicktable_pass",
86+
"matrix_pass"
8387
};
8488

8589
struct Status {

include/trackcpp/elements.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "kicktable.h"
2121
#include "auxiliary.h"
22+
#include "linalg.h"
2223
#include <vector>
2324
#include <string>
2425
#include <fstream>
@@ -61,8 +62,9 @@ class Element {
6162

6263
std::vector<double> polynom_a = default_polynom;
6364
std::vector<double> polynom_b = default_polynom;
65+
Matrix matrix66 = Matrix(6);
6466

65-
const Kicktable* kicktable = nullptr;
67+
const Kicktable* kicktable = nullptr;
6668

6769
double t_in[6], t_out[6];
6870
double r_in[36], r_out[36];
@@ -84,6 +86,7 @@ class Element {
8486
static Element vcorrector (const std::string& fam_name_, const double& length_, const double& vkick_);
8587
static Element corrector (const std::string& fam_name_, const double& length_, const double& hkick_, const double& vkick_);
8688
static Element drift (const std::string& fam_name_, const double& length_);
89+
static Element matrix (const std::string& fam_name_, const double& length_);
8790
static Element rbend (const std::string& fam_name_, const double& length_,
8891
const double& angle_, const double& angle_in_ = 0, const double& angle_out_ = 0,
8992
const double& gap_ = 0, const double& fint_in_ = 0, const double& fint_out_ = 0,
@@ -103,6 +106,7 @@ class Element {
103106
void initialize_marker(Element& element);
104107
void initialize_corrector(Element& element, const double& hkick, const double& vkick);
105108
void initialize_drift(Element& element);
109+
void initialize_matrix(Element &element);
106110
void initialize_rbend(Element& element, const double& angle, const double& angle_in, const double& angle_out,
107111
const double& gap, const double& fint_in, const double& fint_out,
108112
const std::vector<double>& polynom_a, const std::vector<double>& polynom_b,

include/trackcpp/linalg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ void matrix6_set_identity_posvec(std::vector<Pos<T> >& m, const T& a = 1) {
6868
m[0].rx = m[1].px = m[2].ry = m[3].py = m[4].de = m[5].dl = a;
6969
}
7070

71+
void multiply_transf_matrix66(Matrix &m, const double k1);
72+
7173
#endif

include/trackcpp/passmethods.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ template <typename T> Status::type pm_cavity_pass (Pos<T> &pos, c
3232
template <typename T> Status::type pm_thinquad_pass (Pos<T> &pos, const Element &elem, const Accelerator& accelerator);
3333
template <typename T> Status::type pm_thinsext_pass (Pos<T> &pos, const Element &elem, const Accelerator& accelerator);
3434
template <typename T> Status::type pm_kicktable_pass (Pos<T> &pos, const Element &elem, const Accelerator& accelerator);
35+
template <typename T> Status::type pm_matrix_pass (Pos<T> &pos, const Element &elem, const Accelerator& accelerator);
3536

3637
#include "passmethods.hpp"
3738

include/trackcpp/passmethods.hpp

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "pos.h"
3030
#include "auxiliary.h"
3131
#include "tpsa.h"
32+
#include "linalg.h"
3233
#include <cmath>
3334

3435
// constants for 4th-order symplectic integrator
@@ -121,6 +122,21 @@ Status::type kicktablethinkick(Pos<T>& pos, const Kicktable* kicktable,
121122
return status;
122123
}
123124

125+
template <typename T>
126+
void matthinkick(Pos<T> &pos, const Matrix &m) {
127+
128+
T rx = pos.rx, px = pos.px;
129+
T ry = pos.ry, py = pos.py;
130+
T de = pos.de, dl = pos.dl;
131+
132+
pos.rx = m[0][0]*rx+m[0][1]*px+m[0][2]*ry+m[0][3]*py+m[0][4]*de+m[0][5]*dl;
133+
pos.px = m[1][0]*rx+m[1][1]*px+m[1][2]*ry+m[1][3]*py+m[1][4]*de+m[1][5]*dl;
134+
pos.ry = m[2][0]*rx+m[2][1]*px+m[2][2]*ry+m[2][3]*py+m[2][4]*de+m[2][5]*dl;
135+
pos.py = m[3][0]*rx+m[3][1]*px+m[3][2]*ry+m[3][3]*py+m[3][4]*de+m[3][5]*dl;
136+
pos.de = m[4][0]*rx+m[4][1]*px+m[4][2]*ry+m[4][3]*py+m[4][4]*de+m[4][5]*dl;
137+
pos.dl = m[5][0]*rx+m[5][1]*px+m[5][2]*ry+m[5][3]*py+m[5][4]*de+m[5][5]*dl;
138+
}
139+
124140
template <typename T>
125141
void strthinkick(Pos<T>& pos, const double& length,
126142
const std::vector<double>& polynom_a,
@@ -230,7 +246,6 @@ void local_2_global(Pos<T> &pos, const Element &elem) {
230246
translate_pos(pos, elem.t_out);
231247
}
232248

233-
234249
template <typename T>
235250
Status::type pm_identity_pass(Pos<T> &pos, const Element &elem,
236251
const Accelerator& accelerator) {
@@ -259,9 +274,9 @@ Status::type pm_str_mpole_symplectic4_pass(Pos<T> &pos, const Element &elem,
259274
const std::vector<double> &polynom_a = elem.polynom_a;
260275
const std::vector<double> &polynom_b = elem.polynom_b;
261276
for(unsigned int i=0; i<elem.nr_steps; ++i) {
262-
drift(pos, l1);
277+
drift<T>(pos, l1);
263278
strthinkick<T>(pos, k1, polynom_a, polynom_b, accelerator);
264-
drift(pos, l2);
279+
drift<T>(pos, l2);
265280
strthinkick<T>(pos, k2, polynom_a, polynom_b, accelerator);
266281
drift<T>(pos, l2);
267282
strthinkick<T>(pos, k1, polynom_a, polynom_b, accelerator);
@@ -301,7 +316,6 @@ Status::type pm_bnd_mpole_symplectic4_pass(Pos<T> &pos, const Element &elem,
301316
return Status::success;
302317
}
303318

304-
305319
template <typename T>
306320
Status::type pm_corrector_pass(Pos<T> &pos, const Element &elem,
307321
const Accelerator& accelerator) {
@@ -333,7 +347,6 @@ Status::type pm_corrector_pass(Pos<T> &pos, const Element &elem,
333347
return Status::success;
334348
}
335349

336-
337350
template <typename T>
338351
Status::type pm_cavity_pass(Pos<T> &pos, const Element &elem,
339352
const Accelerator& accelerator) {
@@ -410,6 +423,37 @@ Status::type pm_kicktable_pass(Pos<T> &pos, const Element &elem,
410423
return status;
411424
}
412425

426+
template <typename T>
427+
Status::type pm_matrix_pass(Pos<T> &pos, const Element &elem,
428+
const Accelerator& accelerator) {
429+
430+
global_2_local(pos, elem);
431+
if (elem.length > 0) {
432+
double sl = elem.length / float(elem.nr_steps);
433+
double l1 = sl * DRIFT1;
434+
double l2 = sl * DRIFT2;
435+
double k1 = KICK1 / float(elem.nr_steps);
436+
double k2 = KICK2 / float(elem.nr_steps);
437+
Matrix mat1 = elem.matrix66;
438+
Matrix mat2 = elem.matrix66;
439+
multiply_transf_matrix66(mat1, k1);
440+
multiply_transf_matrix66(mat2, k2);
441+
for(unsigned int i=0; i<elem.nr_steps; ++i) {
442+
drift<T>(pos, l1);
443+
matthinkick<T>(pos, mat1);
444+
drift<T>(pos, l2);
445+
matthinkick<T>(pos, mat2);
446+
drift<T>(pos, l2);
447+
matthinkick<T>(pos, mat1);
448+
drift<T>(pos, l1);
449+
}
450+
} else {
451+
matthinkick<T>(pos, elem.matrix66);
452+
}
453+
local_2_global(pos, elem);
454+
return Status::success;
455+
}
456+
413457
#undef DRIFT1
414458
#undef DRIFT2
415459
#undef KICK1

include/trackcpp/tracking.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Status::type track_elementpass (
6868
case PassMethod::pm_kicktable_pass:
6969
if ((status = pm_kicktable_pass<T>(orig_pos, el, accelerator)) != Status::success) return status;
7070
break;
71+
case PassMethod::pm_matrix_pass:
72+
if ((status = pm_matrix_pass<T>(orig_pos, el, accelerator)) != Status::success) return status;
73+
break;
7174
default:
7275
return Status::passmethod_not_defined;
7376
}

python_package/interface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Element drift_wrapper(const std::string &fam_name_, const double &length_) {
8181
return Element::drift(fam_name_, length_);
8282
}
8383

84+
Element matrix_wrapper(const std::string &fam_name_, const double &length_) {
85+
return Element::matrix(fam_name_, length_);
86+
}
87+
8488
Element rbend_wrapper(const std::string& fam_name_, const double& length_,
8589
const double& angle_, const double& angle_in_, const double& angle_out_,
8690
const double& gap_, const double& fint_in_, const double& fint_out_,

python_package/interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Element hcorrector_wrapper(const std::string& fam_name_, const double& length_,
6868
Element vcorrector_wrapper(const std::string& fam_name_, const double& length_, const double& vkick_);
6969
Element corrector_wrapper(const std::string& fam_name_, const double& length_, const double& hkick_, const double& vkick_);
7070
Element drift_wrapper(const std::string& fam_name_, const double& length_);
71+
Element matrix_wrapper(const std::string& fam_name_, const double& length_);
7172
Element quadrupole_wrapper(const std::string& fam_name_, const double& length_, const double& K_, const int nr_steps_ = 1);
7273
Element sextupole_wrapper(const std::string& fam_name_, const double& length_, const double& S_, const int nr_steps_ = 1);
7374
Element rfcavity_wrapper(const std::string& fam_name_, const double& length_, const double& frequency_, const double& voltage_, const double& phase_lag);

src/elements.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Element::Element(const std::string& fam_name_, const double& length_) :
3434
}
3535
}
3636
}
37-
37+
matrix66.eye();
3838
}
3939

4040
const std::string& Element::get_pass_method() {
@@ -69,6 +69,12 @@ Element Element::drift (const std::string& fam_name_, const double& length_) {
6969
return e;
7070
}
7171

72+
Element Element::matrix(const std::string& fam_name_, const double& length_) {
73+
Element e = Element(fam_name_, length_);
74+
initialize_matrix(e);
75+
return e;
76+
}
77+
7278
Element Element::hcorrector(const std::string& fam_name_, const double& length_, const double& hkick_) {
7379
Element e = Element(fam_name_, length_);
7480
initialize_corrector(e, hkick_, 0.0);
@@ -160,6 +166,12 @@ bool Element::operator==(const Element& o) const {
160166
if (this->phase_lag != o.phase_lag) return false;
161167
if (this->polynom_a != o.polynom_a) return false;
162168
if (this->polynom_b != o.polynom_b) return false;
169+
const Matrix& m = this->matrix66;
170+
const Matrix& mo = o.matrix66;
171+
for(unsigned int i=0; i<m.size(); ++i){
172+
for(unsigned int j=0; j<m[i].size(); ++j)
173+
if (m[i][j] != mo[i][j]) return false;
174+
}
163175
for(unsigned int i=0; i<6; ++i) {
164176
if (this->t_in[i] != o.t_in[i]) return false;
165177
if (this->t_out[i] != o.t_out[i]) return false;
@@ -214,6 +226,10 @@ void initialize_drift(Element &element) {
214226
element.pass_method = PassMethod::pm_drift_pass;
215227
}
216228

229+
void initialize_matrix(Element &element) {
230+
element.pass_method = PassMethod::pm_matrix_pass;
231+
}
232+
217233
void initialize_rbend(Element& element, const double& angle, const double& angle_in, const double& angle_out, const double& gap, const double& fint_in, const double& fint_out, const std::vector<double>& polynom_a, const std::vector<double>& polynom_b, const double& K, const double& S, const int nr_steps) {
218234
element.pass_method = PassMethod::pm_bnd_mpole_symplectic4_pass;
219235
element.angle = angle;

src/flat_file.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <trackcpp/flat_file.h>
1818
#include <trackcpp/elements.h>
1919
#include <trackcpp/auxiliary.h>
20+
#include <trackcpp/linalg.h>
2021
#include <fstream>
2122
#include <string>
2223
#include <sstream>
@@ -28,8 +29,10 @@ static bool read_boolean_string(std::istringstream& ss);
2829
static std::string get_boolean_string(bool value);
2930
static bool has_t_vector(const double* t);
3031
static bool has_r_matrix(const double* r);
32+
static bool has_matrix66(const Matrix& r);
3133
static bool has_polynom(const std::vector<double>& p);
3234
static void write_6d_vector(std::ostream& fp, const std::string& label, const double* t);
35+
static void write_6d_vector(std::ostream& fp, const std::string& label, const std::vector<double>& t);
3336
static void write_polynom(std::ostream& fp, const std::string& label, const std::vector<double>& p);
3437
static void synchronize_polynomials(Element& e);
3538
static void read_polynomials(std::ifstream& fp, Element& e);
@@ -135,6 +138,14 @@ void write_flat_file_trackcpp(std::ostream& fp, const Accelerator& accelerator)
135138
write_6d_vector(fp, "de|r_out", &e.r_out[6*4]);
136139
write_6d_vector(fp, "dl|r_out", &e.r_out[6*5]);
137140
}
141+
if (has_matrix66(e.matrix66)) {
142+
write_6d_vector(fp, "rx|matrix66", e.matrix66[0]);
143+
write_6d_vector(fp, "px|matrix66", e.matrix66[1]);
144+
write_6d_vector(fp, "ry|matrix66", e.matrix66[2]);
145+
write_6d_vector(fp, "py|matrix66", e.matrix66[3]);
146+
write_6d_vector(fp, "de|matrix66", e.matrix66[4]);
147+
write_6d_vector(fp, "dl|matrix66", e.matrix66[5]);
148+
}
138149

139150
fp << '\n';
140151
}
@@ -218,6 +229,12 @@ Status::type read_flat_file_trackcpp(std::istream& fp, Accelerator& accelerator)
218229
if (cmd.compare("py|r_out") == 0) { for(auto i=0; i<6; ++i) ss >> e.r_out[3*6+i]; continue; }
219230
if (cmd.compare("de|r_out") == 0) { for(auto i=0; i<6; ++i) ss >> e.r_out[4*6+i]; continue; }
220231
if (cmd.compare("dl|r_out") == 0) { for(auto i=0; i<6; ++i) ss >> e.r_out[5*6+i]; continue; }
232+
if (cmd.compare("rx|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[0][i]; continue; }
233+
if (cmd.compare("px|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[1][i]; continue; }
234+
if (cmd.compare("ry|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[2][i]; continue; }
235+
if (cmd.compare("py|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[3][i]; continue; }
236+
if (cmd.compare("de|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[4][i]; continue; }
237+
if (cmd.compare("dl|matrix66") == 0) { for(auto i=0; i<6; ++i) ss >> e.matrix66[5][i]; continue; }
221238
if (cmd.compare("pass_method") == 0) {
222239
std::string pass_method; ss >> pass_method;
223240
bool found_pm = false;
@@ -454,6 +471,16 @@ static bool has_r_matrix(const double* r) {
454471
return false;
455472
}
456473

474+
static bool has_matrix66(const Matrix& m) {
475+
for (int i=0; i<6; ++i)
476+
for (int j=0; j<6; ++j)
477+
if ((i != j) & (m[i][j] != 0.0))
478+
return true;
479+
else if ((i == j) & (m[i][j] != 1.0))
480+
return true;
481+
return false;
482+
}
483+
457484
static bool has_polynom(const std::vector<double>& p) {
458485
for (int i=0; i<p.size(); ++i)
459486
if (p[i] != 0)
@@ -469,6 +496,13 @@ static void write_6d_vector(std::ostream& fp, const std::string& label, const do
469496
fp << '\n';
470497
}
471498

499+
static void write_6d_vector(std::ostream& fp, const std::string& label, const std::vector<double>& t) {
500+
fp << std::setw(pw) << label;
501+
for (int i=0; i<6; ++i)
502+
fp << t[i] << " ";
503+
fp << '\n';
504+
}
505+
472506
static void write_polynom(std::ostream& fp, const std::string& label, const std::vector<double>& p) {
473507
fp << std::setw(pw) << label;
474508
for (int i=0; i<p.size(); ++i)

0 commit comments

Comments
 (0)