Skip to content
1 change: 1 addition & 0 deletions include/trackcpp/accelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Accelerator {
int radiation_on = RadiationState::off;
bool vchamber_on = false;
int harmonic_number = 0;
double frequency = 0; // [Hz]
std::vector<Element> lattice;
std::string lattice_version = "";

Expand Down
6 changes: 3 additions & 3 deletions include/trackcpp/elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Element {
double fint_out = 0;
double thin_KL = 0; // [1/m]
double thin_SL = 0; // [1/m²]
double frequency = 0; // [Hz]
int harmonic = 0; //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the default value be 1 instead of 0 ?

double voltage = 0; // [V]
double phase_lag = 0; // [rad]
int kicktable_idx = -1; // index of kickmap object in kicktable_list
Expand Down Expand Up @@ -109,7 +109,7 @@ class Element {
const double& K_ = 0, const double& S_ = 0, const int nr_steps_ = 20);
static Element quadrupole (const std::string& fam_name_, const double& length_, const double& K_, const int nr_steps_ = 10);
static Element sextupole (const std::string& fam_name_, const double& length_, const double& S_, const int nr_steps_ = 5);
static Element rfcavity (const std::string& fam_name_, const double& length_, const double& frequency_, const double& voltage_, const double& phase_lag_);
static Element rfcavity (const std::string& fam_name_, const double& length_, const double& harmonic_, const double& voltage_, const double& phase_lag_);
static Element kickmap (const std::string& fam_name_, const std::string& kicktable_fname_, const int nr_steps_ = 20, const double& rescale_length_ = 1.0, const double& rescale_kicks_ = 1.0);

bool operator==(const Element& o) const;
Expand All @@ -130,7 +130,7 @@ void initialize_rbend(Element& element, const double& angle, const double& angle
const double& K, const double& S, const int nr_steps);
void initialize_quadrupole(Element& element, const double& K, const int& nr_steps);
void initialize_sextupole(Element& element, const double& S, const int& nr_steps);
void initialize_rfcavity(Element& element, const double& frequency, const double& voltage, const double& phase_lag);
void initialize_rfcavity(Element& element, const double& harmonic, const double& voltage, const double& phase_lag);
void initialize_kickmap(Element& element, const int& kicktable_idx, const int& nr_steps, const double &rescale_kicks);

#endif
2 changes: 1 addition & 1 deletion include/trackcpp/lattice.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::vector<Element> latt_read_flat_file(const std::string& filename);
Status::type latt_read_flat_file(const std::string& filename, Accelerator& accelerator);
std::vector<int> latt_findcells_fam_name (const std::vector<Element>& lattice, const std::string& value, bool reverse = false);
std::vector<int> latt_findcells_angle (const std::vector<Element>& lattice, const double& value, bool reverse = false);
std::vector<int> latt_findcells_frequency (const std::vector<Element>& lattice, const double& value, bool reverse = false);
std::vector<int> latt_findcells_harmonic (const std::vector<Element>& lattice, const double& value, bool reverse = false);
std::vector<int> latt_findcells_polynom_b (const std::vector<Element>& lattice, unsigned int n, const double& value, bool reverse = false);
std::vector<int> latt_findcells_polynom_a (const std::vector<Element>& lattice, unsigned int n, const double& value, bool reverse = false);
std::vector<int> latt_findcells_pass_method (const std::vector<Element>& lattice, const std::string& value, bool reverse = false);
Expand Down
5 changes: 3 additions & 2 deletions include/trackcpp/passmethods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,10 @@ Status::type pm_cavity_pass(Pos<T> &pos, const Element &elem,

global_2_local(pos, elem);
double nv = elem.voltage / accelerator.energy;
double freq = elem.harmonic * accelerator.frequency;
if (elem.length == 0) {
T &de = pos.de, &dl = pos.dl;
de += -nv * sin(TWOPI*elem.frequency * dl/ light_speed - elem.phase_lag);
de += -nv * sin(TWOPI*freq * dl/ light_speed - elem.phase_lag);
} else {
T &rx = pos.rx, &px = pos.px;
T &ry = pos.ry, &py = pos.py;
Expand All @@ -414,7 +415,7 @@ Status::type pm_cavity_pass(Pos<T> &pos, const Element &elem,
ry += norml * py;
dl += 0.5 * norml * pnorm * (px*px + py*py);
// longitudinal momentum kick
de += -nv * sin(TWOPI*elem.frequency*dl/light_speed - elem.phase_lag);
de += -nv * sin(TWOPI*freq*dl/light_speed - elem.phase_lag);
// drift half length
pnorm = 1.0 / (1.0 + de);
norml = (0.5 * elem.length) * pnorm;
Expand Down
4 changes: 2 additions & 2 deletions python_package/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ Element sextupole_wrapper(const std::string &fam_name_, const double &length_, c
return Element::sextupole(fam_name_, length_, S_, nr_steps_);
}

Element rfcavity_wrapper(const std::string &fam_name_, const double &length_, const double &frequency_, const double &voltage_, const double &phase_lag_) {
return Element::rfcavity(fam_name_, length_, frequency_, voltage_, phase_lag_);
Element rfcavity_wrapper(const std::string &fam_name_, const double &length_, const double &harmonic_, const double &voltage_, const double &phase_lag_) {
return Element::rfcavity(fam_name_, length_, harmonic_, voltage_, phase_lag_);
}

Element kickmap_wrapper(const std::string& fam_name_, const std::string& kicktable_fname_, const int nr_steps_, const double& rescale_length_, const double& rescale_kicks_) {
Expand Down
2 changes: 1 addition & 1 deletion python_package/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Element drift_g2l_wrapper(const std::string& fam_name_, const double& length_);
Element matrix_wrapper(const std::string& fam_name_, const double& length_);
Element quadrupole_wrapper(const std::string& fam_name_, const double& length_, const double& K_, const int nr_steps_ = 10);
Element sextupole_wrapper(const std::string& fam_name_, const double& length_, const double& S_, const int nr_steps_ = 5);
Element rfcavity_wrapper(const std::string& fam_name_, const double& length_, const double& frequency_, const double& voltage_, const double& phase_lag);
Element rfcavity_wrapper(const std::string& fam_name_, const double& length_, const double& harmonic_, const double& voltage_, const double& phase_lag);
Element kickmap_wrapper(const std::string& fam_name_, const std::string& kicktable_fname_, const int nr_steps_ = 20, const double& rescale_length = 1.0, const double& rescale_kicks = 1.0);
Element rbend_wrapper(const std::string& fam_name_, const double& length_,
const double& angle_, const double& angle_in_, const double& angle_out_,
Expand Down
2 changes: 2 additions & 0 deletions src/accelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bool Accelerator::operator==(const Accelerator& o) const {
if (this->radiation_on != o.radiation_on) return false;
if (this->vchamber_on != o.vchamber_on) return false;
if (this->harmonic_number != o.harmonic_number) return false;
if (this->frequency != o.frequency) return false;
if (this->lattice != o.lattice) return false;
if (this->lattice_version != o.lattice_version) return false;

Expand All @@ -48,6 +49,7 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) {
out << std::endl << "radiation_on : " << a.radiation_on;
out << std::endl << "vchamber_on : " << a.vchamber_on;
out << std::endl << "harmonic_number: " << a.harmonic_number;
out << std::endl << "frequency : " << a.frequency;
out << std::endl << "lattice : " << a.lattice.size() << " elements";
out << std::endl << "lattice_version: " << a.lattice_version;
return out;
Expand Down
12 changes: 6 additions & 6 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ Element Element::rbend (const std::string& fam_name_, const double& length_,
return e;
}

Element Element::rfcavity (const std::string& fam_name_, const double& length_, const double& frequency_, const double& voltage_, const double& phase_lag_) {
Element Element::rfcavity (const std::string& fam_name_, const double& length_, const double& harmonic_, const double& voltage_, const double& phase_lag_) {
Element e = Element(fam_name_, length_);
initialize_rfcavity(e, frequency_, voltage_, phase_lag_);
initialize_rfcavity(e, harmonic_, voltage_, phase_lag_);
return e;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ bool Element::operator==(const Element& o) const {
if (this->fint_out != o.fint_out) return false;
if (this->thin_KL != o.thin_KL) return false;
if (this->thin_SL != o.thin_SL) return false;
if (this->frequency != o.frequency) return false;
if (this->harmonic != o.harmonic) return false;
if (this->voltage != o.voltage) return false;
if (this->phase_lag != o.phase_lag) return false;
if (this->polynom_a != o.polynom_a) return false;
Expand Down Expand Up @@ -240,7 +240,7 @@ std::ostream& operator<< (std::ostream &out, const Element& el) {
}
print_polynom( out, "polynom_a : ", el.polynom_a);
print_polynom( out, "polynom_b : ", el.polynom_b);
if (el.frequency != 0)out << std::endl << "frequency : " << el.frequency;
if (el.harmonic != 0) out << std::endl << "harmonic : " << el.harmonic;
if (el.voltage != 0) out << std::endl << "voltage : " << el.voltage;
if (el.phase_lag != 0)out << std::endl << "phase_lag : " << el.phase_lag;
return out;
Expand Down Expand Up @@ -295,9 +295,9 @@ void initialize_sextupole(Element &element, const double &S, const int &nr_steps
element.nr_steps = nr_steps;
}

void initialize_rfcavity(Element &element, const double &frequency, const double &voltage, const double &phase_lag) {
void initialize_rfcavity(Element &element, const double &harmonic, const double &voltage, const double &phase_lag) {
element.pass_method = PassMethod::pm_cavity_pass;
element.frequency = frequency;
element.harmonic = harmonic;
element.voltage = voltage;
element.phase_lag = phase_lag;
}
Expand Down
24 changes: 19 additions & 5 deletions src/flat_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void write_flat_file_trackcpp(std::ostream& fp, const Accelerator& accelerator)

fp << std::setw(hw) << "% energy" << accelerator.energy << " eV\n";
fp << std::setw(hw) << "% harmonic_number" << accelerator.harmonic_number << "\n";
fp << std::setw(hw) << "% frequency" << accelerator.frequency << "\n";
fp << std::setw(hw) << "% cavity_on" << get_boolean_string(accelerator.cavity_on) << "\n";
fp << std::setw(hw) << "% radiation_on" << accelerator.radiation_on << "\n";
fp << std::setw(hw) << "% vchamber_on" << get_boolean_string(accelerator.vchamber_on) << "\n";
Expand Down Expand Up @@ -122,7 +123,7 @@ void write_flat_file_trackcpp(std::ostream& fp, const Accelerator& accelerator)
if (e.fint_in != 0) { fp << std::setw(pw) << "fint_in" << e.fint_in << '\n'; }
if (e.fint_out != 0) { fp << std::setw(pw) << "fint_out" << e.fint_out << '\n'; }
if (e.voltage != 0) { fp << std::setw(pw) << "voltage" << e.voltage << '\n'; }
if (e.frequency != 0) { fp << std::setw(pw) << "frequency" << e.frequency << '\n'; }
if (e.harmonic != 0) { fp << std::setw(pw) << "harmonic" << e.harmonic << '\n'; }
if (e.phase_lag != 0) { fp << std::setw(pw) << "phase_lag" << e.phase_lag << '\n'; }
if (e.angle_in != 0) { fp << std::setw(pw) << "angle_in" << e.angle_in << '\n'; }
if (e.angle_out != 0) { fp << std::setw(pw) << "angle_out" << e.angle_out << '\n'; }
Expand Down Expand Up @@ -170,6 +171,7 @@ Status::type read_flat_file_trackcpp(std::istream& fp, Accelerator& accelerator)
bool found_hmin = false;
bool found_vmin = false;
unsigned int line_count = 0;
std::vector<double> frequencies = {};
while (std::getline(fp, line)) {
//std::cout << line << std::endl;
line_count++;
Expand All @@ -182,6 +184,7 @@ Status::type read_flat_file_trackcpp(std::istream& fp, Accelerator& accelerator)
ss >> cmd;
if (cmd.compare("energy") == 0) { ss >> accelerator.energy; continue; }
if (cmd.compare("harmonic_number") == 0) { ss >> accelerator.harmonic_number; continue; }
if (cmd.compare("frequency") == 0) { ss >> accelerator.frequency; continue; }
if (cmd.compare("cavity_on") == 0) { accelerator.cavity_on = read_boolean_string(ss); continue; }
// radiation_on needs its own processing function due backward compatibility
if (cmd.compare("radiation_on") == 0){ accelerator.radiation_on = process_rad_property(ss); continue; }
Expand Down Expand Up @@ -221,7 +224,13 @@ Status::type read_flat_file_trackcpp(std::istream& fp, Accelerator& accelerator)
if (cmd.compare("fint_in") == 0) { ss >> e.fint_in; continue; }
if (cmd.compare("fint_out") == 0) { ss >> e.fint_out; continue; }
if (cmd.compare("voltage") == 0) { ss >> e.voltage; continue; }
if (cmd.compare("frequency") == 0) { ss >> e.frequency; continue; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about reading old lattice flatten files ?

if (cmd.compare("harmonic") == 0) { ss >> e.harmonic; continue; }
if (cmd.compare("frequency") == 0) {
double freq;
ss >> freq;
frequencies.push_back(freq);
continue;
}
if (cmd.compare("phase_lag") == 0) { ss >> e.phase_lag; continue; }
if (cmd.compare("angle_in") == 0) { ss >> e.angle_in; continue; }
if (cmd.compare("angle_out") == 0) { ss >> e.angle_out; continue; }
Expand Down Expand Up @@ -311,6 +320,10 @@ Status::type read_flat_file_trackcpp(std::istream& fp, Accelerator& accelerator)
accelerator.lattice.push_back(e);
}

if (frequencies.size() > 0) {
accelerator.frequency = frequencies[0];
}

return Status::success;

}
Expand Down Expand Up @@ -364,11 +377,12 @@ static Status::type read_flat_file_tracy(const std::string& filename, Accelerato
case FlatFileType::cavity:
{
e.pass_method = PassMethod::pm_cavity_pass;
int hnumber; double energy;
fp >> e.voltage >> e.frequency >> hnumber >> energy;
e.voltage *= energy; e.frequency *= light_speed / (2*M_PI);
int hnumber; double energy; double freq;
fp >> e.voltage >> freq >> hnumber >> energy;
e.voltage *= energy; freq *= light_speed / (2*M_PI);
accelerator.harmonic_number = hnumber;
accelerator.energy = energy;
accelerator.frequency = freq;
}; break;
case FlatFileType::mpole:
{
Expand Down
6 changes: 3 additions & 3 deletions src/lattice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ double latt_findspos(const std::vector<Element>& lattice, const int idx) {

void latt_setcavity(std::vector<Element>& lattice, const std::string& state) {
for(unsigned int i=0; i<lattice.size(); ++i) {
if (lattice[i].frequency != 0) {
if (lattice[i].harmonic != 0) {
if (state == "on") {
lattice[i].pass_method = PassMethod::pm_cavity_pass;
} else if (state == "off") {
Expand All @@ -100,10 +100,10 @@ std::vector<int> latt_findcells_fam_name(const std::vector<Element>& lattice, co
return r;
}

std::vector<int> latt_findcells_frequency(const std::vector<Element>& lattice, const double& value, bool reverse) {
std::vector<int> latt_findcells_harmonic(const std::vector<Element>& lattice, const double& value, bool reverse) {
std::vector<int> r;
for(unsigned int i=0; i<lattice.size(); ++i) {
if ((reverse and (lattice[i].frequency != value)) or (!reverse and (lattice[i].frequency == value))) {
if ((reverse and (lattice[i].harmonic != value)) or (!reverse and (lattice[i].harmonic == value))) {
r.push_back(i);
}
};
Expand Down
13 changes: 0 additions & 13 deletions src/sirius_v500.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

void sirius_v500(std::vector<Element>& the_ring) {

int harmonic_number = 864;

//double energy = 3e9;

// AC10_5
double qaf_strength = 2.536876;
double qad_strength = -2.730416;
Expand Down Expand Up @@ -257,15 +253,6 @@ void sirius_v500(std::vector<Element>& the_ring) {
}
}

//""" sets cavity frequency according to lattice length """
double C = latt_findspos(the_ring, 1+the_ring.size());

double rev_freq = light_speed / C;
std::vector<int> rf_idx = latt_findcells_fam_name(the_ring, "cav");

for(unsigned int idx = 0; idx<rf_idx.size(); ++idx) {
the_ring[rf_idx[idx]].frequency = rev_freq * harmonic_number;
}
latt_setcavity(the_ring, "on");
//latt_setradiation(the_ring, "on", 3e9);

Expand Down
1 change: 1 addition & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ int test_linepass2() {
//sirius_v500(accelerator.lattice);
accelerator.energy = 3e9; // [ev]
accelerator.harmonic_number = 864;
accelerator.frequency = light_speed * accelerator.harmonic_number / accelerator.get_length();
accelerator.radiation_on = RadiationState::off;
accelerator.cavity_on = false;
accelerator.vchamber_on = false;
Expand Down
4 changes: 1 addition & 3 deletions src/tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ Status::type track_findorbit6(
// calcs longitudinal fixed point
double L0 = latt_findspos(the_ring, 1+the_ring.size());
double T0 = L0 / light_speed;
std::vector<int> cav_idx = latt_findcells_frequency(the_ring, 0, true);
double frf = the_ring[cav_idx[0]].frequency;
double fixedpoint = light_speed*((1.0*accelerator.harmonic_number)/frf - T0);
double fixedpoint = light_speed*((1.0*accelerator.harmonic_number)/accelerator.frequency - T0);

// temporary vectors and matrices
std::vector<Pos<double> > co(7,0);
Expand Down