From 48a8bd7bb46dad92da2d27f4d04121a4abc24140 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 00:44:17 -0300 Subject: [PATCH 01/28] ADD: Define "time aware" pass methods --- include/trackcpp/auxiliary.h | 9 ++++++++- src/auxiliary.cpp | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/trackcpp/auxiliary.h b/include/trackcpp/auxiliary.h index 3cdfdc5..9686997 100644 --- a/include/trackcpp/auxiliary.h +++ b/include/trackcpp/auxiliary.h @@ -37,7 +37,8 @@ class PassMethodsClass { static const int pm_kickmap_pass = 8; static const int pm_matrix_pass = 9; static const int pm_drift_g2l_pass = 10; - static const int pm_nr_pms = 11; // counter for number of passmethods + static const int pm_nr_pms = 11; // counter for number of passmethods] + static const std::vector time_aware_passmethods; PassMethodsClass() { passmethods.push_back("identity_pass"); passmethods.push_back("drift_pass"); @@ -53,6 +54,12 @@ class PassMethodsClass { } int size() const { return passmethods.size(); } std::string operator[](const int i) const { return passmethods[i]; } + bool is_time_aware_pm(const int i) const { + for (int pm: time_aware_passmethods) { + if (i == pm) {return true;} + } + return false; + }; private: std::vector passmethods; }; diff --git a/src/auxiliary.cpp b/src/auxiliary.cpp index 78ddeff..ea3ec74 100644 --- a/src/auxiliary.cpp +++ b/src/auxiliary.cpp @@ -22,6 +22,10 @@ static std::normal_distribution distr_gauss(0., 1.); static std::uniform_real_distribution distr_uniform(-sqrt(3.0), sqrt(3.0)); int choosen_distribution = Distributions::normal; +const std::vector PassMethodsClass::time_aware_passmethods = { + PassMethodsClass::pm_cavity_pass, +}; + void set_random_distribution(unsigned value){ if (value == Distributions::normal){ choosen_distribution = value; From 30fd1c8f7c652b1236eba9686ba098df69cee664 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 00:58:16 -0300 Subject: [PATCH 02/28] ADD: Get time aware elements info from accelerator --- include/trackcpp/accelerator.h | 5 +++++ src/accelerator.cpp | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/trackcpp/accelerator.h b/include/trackcpp/accelerator.h index 0653507..c0b21ce 100644 --- a/include/trackcpp/accelerator.h +++ b/include/trackcpp/accelerator.h @@ -40,6 +40,11 @@ class Accelerator { bool isequal(const Accelerator& a) const { return *this == a; } // necessary for python_package double get_length() const; friend std::ostream& operator<< (std::ostream &out, const Accelerator& a); + double get_time_aware_elements_info( + std::vector& time_aware_indices, + std::vector& time_aware_displacements, + unsigned int element_offset = 0 + ) const; }; #endif diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 0e83afe..e5e5948 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -52,3 +52,43 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) { out << std::endl << "lattice_version: " << a.lattice_version; return out; } + +double Accelerator::get_time_aware_elements_info( + std::vector& time_aware_indices, + std::vector& time_aware_displacements, + unsigned int element_offset +) const { + + // for longitudinal kick before RF cavities + time_aware_indices.clear(); + time_aware_displacements.clear(); + size_t nr_elements = this->lattice.size(); + PassMethodsClass PMClass = PassMethodsClass(); + + double s_pos = 0.0; + double acclen = 0.0; + for (size_t i = 0; i < nr_elements; i++) { + const Element& element = this->lattice[element_offset]; + acclen += element.length; + if (PMClass.is_time_aware_pm(element.pass_method)) { + time_aware_indices.push_back(i); + time_aware_displacements.push_back(s_pos + element.length/2); + s_pos = 0.0 + element.length/2; + } + else { + s_pos += element.length; + } + element_offset = (element_offset + 1) % nr_elements; + } + + // In case no element is "time aware" + if (time_aware_indices.size() < 1) { + time_aware_indices.push_back(-1); + time_aware_displacements.push_back(0.0); + } else { + time_aware_displacements[0] += s_pos; + } + + return acclen; + +} From 0d2dd7cdc59477a21f5a887e0a58c1fa5bd2d2a8 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 01:17:40 -0300 Subject: [PATCH 03/28] ADD: defining path length kick function --- include/trackcpp/passmethods.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index 9b315ea..002b05a 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -499,4 +499,22 @@ Status::type pm_matrix_pass(Pos &pos, const Element &elem, return Status::success; } +template +inline void adjust_path_length( + const Accelerator& accelerator, + const Element& element, + const unsigned int& element_offset, + Pos& pos, + const double line_length, + const std::vector& time_aware_element_indices, + const std::vector& time_aware_element_positions, + unsigned int& time_aware_pivot +) { + if (element_offset == time_aware_element_indices[time_aware_pivot]) { + ddl = light_speed*accelerator.harmonic_number/element.frequency - line_length; + pos.dl -= ddl * time_aware_element_positions[time_aware_pivot] / line_length; + if (time_aware_pivot < time_aware_element_indices.size() - 1) {time_aware_pivot++;} + } +} + #endif From 79365b99900dc1d3c1f7593f2e3ece17c8e5e37b Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 01:37:03 -0300 Subject: [PATCH 04/28] MNT: rename positions -> displacements --- include/trackcpp/passmethods.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index 002b05a..44142cd 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -503,11 +503,11 @@ template inline void adjust_path_length( const Accelerator& accelerator, const Element& element, - const unsigned int& element_offset, + unsigned int& element_offset, Pos& pos, const double line_length, - const std::vector& time_aware_element_indices, - const std::vector& time_aware_element_positions, + const std::vector& time_aware_indices, + const std::vector& time_aware_displacements, unsigned int& time_aware_pivot ) { if (element_offset == time_aware_element_indices[time_aware_pivot]) { From 6aa49520d48f9496ff8e1062059bad530902a340 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 01:41:15 -0300 Subject: [PATCH 05/28] MNT: insert path length adjustment in line_pass & ring_pass --- include/trackcpp/tracking.h | 57 +++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index a05c7a4..a5536cd 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -143,13 +143,19 @@ Status::type track_linepass ( const std::vector& indices, unsigned int& element_offset, std::vector >& pos, - Plane::type& lost_plane + Plane::type& lost_plane, + const double line_length, + const std::vector& time_aware_indices, + const std::vector& time_aware_displacements ) { Status::type status = Status::success; const std::vector& line = accelerator.lattice; int nr_elements = line.size(); + // For longitudinal RF kick + unsigned int time_aware_pivot = 0; + //pos.clear(); other functions assume pos is not clearedin linepass! pos.reserve(pos.size() + indices.size()); @@ -167,6 +173,16 @@ Status::type track_linepass ( // stores trajectory at entrance of each element if (indcs[i]) pos.push_back(orig_pos); + adjust_path_length( + accelerator, + element, + element_offset, + orig_pos, + line_length, + time_aware_indices, + time_aware_displacements, + time_aware_pivot + ); status = track_elementpass(accelerator, element, orig_pos); lost_plane = check_particle_loss(accelerator, element, orig_pos); if (lost_plane != Plane::no_plane) status = Status::particle_lost; @@ -217,7 +233,10 @@ Status::type track_linepass ( const bool trajectory, unsigned int& element_offset, std::vector >& pos, - Plane::type& lost_plane + Plane::type& lost_plane, + const double line_length, + const std::vector& time_aware_indices, + const std::vector& time_aware_displacements ) { std::vector indices; unsigned int nr_elements = accelerator.lattice.size(); @@ -234,7 +253,10 @@ Status::type track_linepass ( indices, element_offset, pos, - lost_plane + lost_plane, + line_length, + time_aware_indices, + time_aware_displacements ); } @@ -271,7 +293,10 @@ Status::type track_linepass ( std::vector> &pos, std::vector& lost_plane, std::vector& lost_flag, - std::vector& lost_element + std::vector& lost_element, + const double line_length, + const std::vector& time_aware_indices, + const std::vector& time_aware_displacements ) { int nr_elements = accelerator.lattice.size(); @@ -289,7 +314,15 @@ Status::type track_linepass ( unsigned int le = element_offset; status2 = track_linepass( - accelerator, orig_pos[i], indices, le, final_pos, lp + accelerator, + orig_pos[i], + indices, + le, + final_pos, + lp, + line_length, + time_aware_indices, + time_aware_displacements ); if (status2 != Status::success){ @@ -369,6 +402,15 @@ Status::type track_ringpass ( Status::type status = Status::success; std::vector > final_pos; + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + if (turn_by_turn) pos.reserve(nr_turns+1); for(lost_turn=0; lost_turn Date: Fri, 12 Sep 2025 02:06:57 -0300 Subject: [PATCH 06/28] FIX: match unsigned int --- src/accelerator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/accelerator.cpp b/src/accelerator.cpp index e5e5948..f900235 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -55,8 +55,8 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) { double Accelerator::get_time_aware_elements_info( std::vector& time_aware_indices, - std::vector& time_aware_displacements, - unsigned int element_offset + std::vector& time_aware_displacements, + unsigned int element_offset ) const { // for longitudinal kick before RF cavities @@ -83,7 +83,7 @@ double Accelerator::get_time_aware_elements_info( // In case no element is "time aware" if (time_aware_indices.size() < 1) { - time_aware_indices.push_back(-1); + time_aware_indices.push_back(0); time_aware_displacements.push_back(0.0); } else { time_aware_displacements[0] += s_pos; From bd1dfe15289921dc3fb41c64ee0115c57b43871c Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:08:52 -0300 Subject: [PATCH 07/28] MNT: insert path length adjustment in track_findm66 --- src/tracking.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/tracking.cpp b/src/tracking.cpp index f751390..d305928 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -66,8 +66,21 @@ Status::type track_findm66 (Accelerator& accelerator, map.ry = Tpsa<6,1>(fp.ry, 2); map.py = Tpsa<6,1>(fp.py, 3); map.de = Tpsa<6,1>(fp.de, 4); map.dl = Tpsa<6,1>(fp.dl, 5); + // for longitudinal kick before RF cavities + unsigned int element_offset = 0; + unsigned int time_aware_pivot = 0; + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + + tm.clear(); tm.reserve(indices.size()); for(unsigned int i=0; i Date: Fri, 12 Sep 2025 02:19:12 -0300 Subject: [PATCH 08/28] MNT: insert path length adjustment in track_findorbit6 & track_findorbit4 --- src/tracking.cpp | 75 +++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/src/tracking.cpp b/src/tracking.cpp index d305928..3e283b2 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -160,7 +160,7 @@ Status::type track_findorbit6( const Pos& fixed_point_guess) { const std::vector& the_ring = accelerator.lattice; - + unsigned int element_offset = 0; double delta = 1e-9; // [m],[rad],[dE/E] double tolerance = 2.22044604925e-14; int max_nr_iters = 50; @@ -169,12 +169,6 @@ Status::type track_findorbit6( if (radsts == RadiationState::full){ accelerator.radiation_on = RadiationState::damping; } - // calcs longitudinal fixed point - double L0 = latt_findspos(the_ring, 1+the_ring.size()); - double T0 = L0 / light_speed; - std::vector 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); // temporary vectors and matrices std::vector > co(7,0); @@ -183,10 +177,17 @@ Status::type track_findorbit6( std::vector > D(7,0); std::vector > M(6,0); Pos dco(1.0,1.0,1.0,1.0,1.0,1.0); - Pos theta(0.0,0.0,0.0,0.0,0.0,0.0); - theta.dl = fixedpoint; matrix6_set_identity_posvec(D, delta); + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + int nr_iter = 0; while ((get_max(dco) > tolerance) and (nr_iter <= max_nr_iters)) { co = co + D; @@ -197,25 +198,32 @@ Status::type track_findorbit6( Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[0], false, element_offset, co2, lost_plane + accelerator, co[0], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[1], false, element_offset, co2, lost_plane + accelerator, co[1], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[2], false, element_offset, co2, lost_plane + accelerator, co[2], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[3], false, element_offset, co2, lost_plane + accelerator, co[3], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[4], false, element_offset, co2, lost_plane + accelerator, co[4], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[5], false, element_offset, co2, lost_plane + accelerator, co[5], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[6], false, element_offset, co2, lost_plane + accelerator, co[6], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); if (status != Status::success) { @@ -231,7 +239,7 @@ Status::type track_findorbit6( M[4] = (co2[4] - Rf) / delta; M[5] = (co2[5] - Rf) / delta; - Pos b = Rf - Ri - theta; + Pos b = Rf - Ri; std::vector > M_1(6,0); matrix6_set_identity_posvec(M_1); M_1 = M_1 - M; @@ -249,10 +257,10 @@ Status::type track_findorbit6( // propagates fixed point throught the_ring closed_orbit.clear(); - unsigned int element_offset = 0; Plane::type lost_plane; track_linepass( - accelerator, co[6], true, element_offset, closed_orbit, lost_plane + accelerator, co[6], true, element_offset, closed_orbit, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); accelerator.radiation_on = radsts; return Status::success; @@ -265,7 +273,7 @@ Status::type track_findorbit4( const Pos& fixed_point_guess) { const std::vector& the_ring = accelerator.lattice; - + unsigned int element_offset = 0; double delta = 1e-9; // [m],[rad],[dE/E] double tolerance = 2.22044604925e-14; int max_nr_iters = 50; @@ -284,6 +292,15 @@ Status::type track_findorbit4( Pos theta(0.0,0.0,0.0,0.0,0.0,0.0); matrix6_set_identity_posvec(D, delta); + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + int nr_iter = 0; while ((get_max(dco) > tolerance) and (nr_iter <= max_nr_iters)) { co = co + D; @@ -293,19 +310,24 @@ Status::type track_findorbit4( Plane::type lost_plane; Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[0], false, element_offset, co2, lost_plane + accelerator, co[0], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[1], false, element_offset, co2, lost_plane + accelerator, co[1], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[2], false, element_offset, co2, lost_plane + accelerator, co[2], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[3], false, element_offset, co2, lost_plane + accelerator, co[3], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[6], false, element_offset, co2, lost_plane + accelerator, co[6], false, element_offset, co2, lost_plane, + line_length, time_aware_indices, time_aware_displacements )); if (status != Status::success) { return Status::findorbit_one_turn_matrix_problem; @@ -336,7 +358,8 @@ Status::type track_findorbit4( unsigned int element_offset = 0; Plane::type lost_plane; track_linepass( - accelerator, co[6], true, element_offset, closed_orbit, lost_plane + accelerator, co[6], true, element_offset, closed_orbit, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); accelerator.radiation_on = radsts; return Status::success; From 6e05e6fc240faeb46056ec96fcc7c6918dcbfd30 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:34:05 -0300 Subject: [PATCH 09/28] MNT: adjust line_pass args in "optics.cpp" --- src/optics.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/optics.cpp b/src/optics.cpp index 416f850..7e5bdd3 100644 --- a/src/optics.cpp +++ b/src/optics.cpp @@ -81,8 +81,19 @@ Status::type calc_twiss(Accelerator& accelerator, std::vector> closed_orbit; Plane::type lost_plane; unsigned int element_offset = 0; + + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + Status::type status = track_linepass( - accelerator, fp, true, element_offset, closed_orbit, lost_plane + accelerator, fp, true, element_offset, closed_orbit, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); if (status != Status::success) return status; @@ -151,7 +162,8 @@ Status::type calc_twiss(Accelerator& accelerator, fpp.ry += twiss0.etay[0] * dpp; fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( - accelerator, fpp, true, element_offset, codp, lost_plane + accelerator, fpp, true, element_offset, codp, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); if (status != Status::success) return status; } From 59cb994e9269dfb05accf19f424f390234218422 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:34:19 -0300 Subject: [PATCH 10/28] MNT: adjust line_pass args in "commands.cpp" --- src/commands.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/commands.cpp b/src/commands.cpp index d09f431..480c291 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1084,7 +1084,17 @@ int cmd_track_linepass(const std::vector& args) { std::vector> pos_list; Plane::type lost_plane; unsigned int offset_element = start_element; - track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane); + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + offset_element + ); + track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane, + line_length, time_aware_indices, time_aware_displacements + ); std::cout << get_timestamp() << " saving track_linepass data to file" << std::endl; status = print_tracking_linepass(accelerator, pos_list, start_element, "track_linepass_out.txt"); From 7b9dbad2e5dbf00c7d1bd46a2582d49d6a3a9886 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:34:32 -0300 Subject: [PATCH 11/28] MNT: adjust line_pass args in "tests.cpp" --- src/tests.cpp | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/tests.cpp b/src/tests.cpp index 8ed7ead..d7ede03 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -36,8 +36,19 @@ int test_linepass(const Accelerator& accelerator) { std::vector > new_pos; unsigned int element_offset = 0; Plane::type lost_plane; + + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + Status::type status = track_linepass( - accelerator, pos, true, element_offset, new_pos, lost_plane + accelerator, pos, true, element_offset, new_pos, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); std::cout << "status: " << string_error_messages[status] << std::endl; @@ -67,8 +78,19 @@ int test_linepass_tpsa(const Accelerator& accelerator, const std::vector > > new_tpsa; unsigned int element_offset = 0; Plane::type lost_plane; + + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + track_linepass( - accelerator, tpsa, false, element_offset, new_tpsa, lost_plane + accelerator, tpsa, false, element_offset, new_tpsa, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); for(unsigned int i=0; i >& c = new_particles[i]; @@ -409,6 +431,15 @@ int test_linepass2() { Plane::type lost_plane; bool trajectory = true; + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + orig_pos.rx = 0.0001; orig_pos.px = 0.0001; @@ -418,7 +449,10 @@ int test_linepass2() { trajectory, element_offset, pos, - lost_plane + lost_plane, + line_length, + time_aware_indices, + time_aware_displacements ); for(unsigned int i=0; i<10; ++i) { From 86244055aabd3b5ce029578be8c9f516b9f457b5 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:34:45 -0300 Subject: [PATCH 12/28] MNT: adjust line_pass args in "test-kickmap.cpp" --- tests/test-kickmap.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test-kickmap.cpp b/tests/test-kickmap.cpp index efbb3ff..a8ac249 100644 --- a/tests/test-kickmap.cpp +++ b/tests/test-kickmap.cpp @@ -65,8 +65,18 @@ int main() { Plane::type lost_plane; unsigned int element_offset = 0; + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + element_offset + ); + status = track_linepass( - accelerator, fp, true, element_offset, closed_orbit, lost_plane + accelerator, fp, true, element_offset, closed_orbit, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); if (status != Status::success) return status; @@ -119,7 +129,8 @@ int main() { fpp.ry += twiss0.etay[0] * dpp; fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( - accelerator, fpp, true, element_offset, codp, lost_plane + accelerator, fpp, true, element_offset, codp, lost_plane, + line_length, time_aware_indices, time_aware_displacements ); std::cout << "h2" << std::endl; if (status != Status::success) return status; From ad50bdbe198cf5373055419caa468ad7a2ec25e6 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:39:25 -0300 Subject: [PATCH 13/28] MNT: adjust line_pass args in "interface.cpp" --- python_package/interface.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python_package/interface.cpp b/python_package/interface.cpp index 383c7fe..7d417b2 100644 --- a/python_package/interface.cpp +++ b/python_package/interface.cpp @@ -61,6 +61,16 @@ Status::type track_linepass_wrapper( orig_pos[4*ni2 + i], orig_pos[5*ni2 + i] ); } + + // for longitudinal kick before RF cavities + std::vector time_aware_indices; + std::vector time_aware_displacements; + double line_length = accelerator.get_time_aware_elements_info( + time_aware_indices, + time_aware_displacements, + args.element_offset + ); + Status::type status = track_linepass( accelerator, orig_post, @@ -69,7 +79,10 @@ Status::type track_linepass_wrapper( post, args.lost_plane, args.lost_flag, - args.lost_element + args.lost_element, + line_length, + time_aware_indices, + time_aware_displacements ); for (unsigned int i=0; i Date: Fri, 12 Sep 2025 02:47:36 -0300 Subject: [PATCH 14/28] FIX: mistyped variable and missing declaration --- include/trackcpp/passmethods.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index 44142cd..f602035 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -510,10 +510,10 @@ inline void adjust_path_length( const std::vector& time_aware_displacements, unsigned int& time_aware_pivot ) { - if (element_offset == time_aware_element_indices[time_aware_pivot]) { - ddl = light_speed*accelerator.harmonic_number/element.frequency - line_length; - pos.dl -= ddl * time_aware_element_positions[time_aware_pivot] / line_length; - if (time_aware_pivot < time_aware_element_indices.size() - 1) {time_aware_pivot++;} + if (element_offset == time_aware_indices[time_aware_pivot]) { + double ddl = light_speed*accelerator.harmonic_number/element.frequency - line_length; + pos.dl -= ddl * time_aware_displacements[time_aware_pivot] / line_length; + if (time_aware_pivot < time_aware_indices.size() - 1) {time_aware_pivot++;} } } From e99345d8529d5e6195c1b0b76caa640d661217e8 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 02:49:05 -0300 Subject: [PATCH 15/28] FIX: multiple declaration of element_offset --- src/tracking.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/tracking.cpp b/src/tracking.cpp index 3e283b2..8c36689 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -193,7 +193,6 @@ Status::type track_findorbit6( co = co + D; Pos Ri = co[6]; std::vector > co2; - unsigned int element_offset = 0; Plane::type lost_plane; Status::type status = Status::success; @@ -306,7 +305,6 @@ Status::type track_findorbit4( co = co + D; Pos Ri = co[6]; std::vector > co2; - unsigned int element_offset = 0; Plane::type lost_plane; Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( @@ -355,7 +353,6 @@ Status::type track_findorbit4( // propagates fixed point throught the_ring closed_orbit.clear(); - unsigned int element_offset = 0; Plane::type lost_plane; track_linepass( accelerator, co[6], true, element_offset, closed_orbit, lost_plane, From 623e92615805b65831b7fdee891b5d0b83fd3960 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 14:36:49 -0300 Subject: [PATCH 16/28] FIX: remove mistyped "]" --- include/trackcpp/auxiliary.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/trackcpp/auxiliary.h b/include/trackcpp/auxiliary.h index 9686997..cfe9e8e 100644 --- a/include/trackcpp/auxiliary.h +++ b/include/trackcpp/auxiliary.h @@ -37,7 +37,7 @@ class PassMethodsClass { static const int pm_kickmap_pass = 8; static const int pm_matrix_pass = 9; static const int pm_drift_g2l_pass = 10; - static const int pm_nr_pms = 11; // counter for number of passmethods] + static const int pm_nr_pms = 11; // counter for number of passmethods static const std::vector time_aware_passmethods; PassMethodsClass() { passmethods.push_back("identity_pass"); From 0e574f441c8f8a5f73f9bc3dcea201d85375d944 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 12 Sep 2025 14:45:39 -0300 Subject: [PATCH 17/28] MNT: beautify "is_time_aware_pm" function --- include/trackcpp/auxiliary.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/trackcpp/auxiliary.h b/include/trackcpp/auxiliary.h index cfe9e8e..35e2956 100644 --- a/include/trackcpp/auxiliary.h +++ b/include/trackcpp/auxiliary.h @@ -23,6 +23,7 @@ #include #include #include +#include class PassMethodsClass { public: @@ -55,10 +56,7 @@ class PassMethodsClass { int size() const { return passmethods.size(); } std::string operator[](const int i) const { return passmethods[i]; } bool is_time_aware_pm(const int i) const { - for (int pm: time_aware_passmethods) { - if (i == pm) {return true;} - } - return false; + return std::find(time_aware_passmethods.begin(), time_aware_passmethods.end(), i) != time_aware_passmethods.end(); }; private: std::vector passmethods; From 171e1e1ab02c9d04a85654b43786d09bdfda7297 Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 15 Sep 2025 09:42:54 -0300 Subject: [PATCH 18/28] FIX: 0 -> UINT_MAX in "empty" time_aware_indices --- src/accelerator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/accelerator.cpp b/src/accelerator.cpp index f900235..7a48951 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -83,7 +83,7 @@ double Accelerator::get_time_aware_elements_info( // In case no element is "time aware" if (time_aware_indices.size() < 1) { - time_aware_indices.push_back(0); + time_aware_indices.push_back(UINT_MAX); time_aware_displacements.push_back(0.0); } else { time_aware_displacements[0] += s_pos; From d6ca46fdb7abe8b664eddace6a36132b73f6c7bb Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 15 Sep 2025 11:00:45 -0300 Subject: [PATCH 19/28] IMP: avoid precision loss when obtaining the "time aware" elements' info. --- src/accelerator.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 7a48951..0f084d0 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -72,8 +72,9 @@ double Accelerator::get_time_aware_elements_info( acclen += element.length; if (PMClass.is_time_aware_pm(element.pass_method)) { time_aware_indices.push_back(i); - time_aware_displacements.push_back(s_pos + element.length/2); - s_pos = 0.0 + element.length/2; + s_pos += 0.5 * element.length; + time_aware_displacements.push_back(s_pos); + s_pos = 0.5 * element.length; } else { s_pos += element.length; @@ -87,6 +88,9 @@ double Accelerator::get_time_aware_elements_info( time_aware_displacements.push_back(0.0); } else { time_aware_displacements[0] += s_pos; + //? NOTE : The diference between "acclen" and the sum of "time_aware_displacements" is on the order of 1e-15 ~ 1e-16, + //? and the propagation of this tiny error affects the tracking. The following line avoids losing precision. + time_aware_displacements.back() += acclen - std::accumulate(time_aware_displacements.begin(), time_aware_displacements.end(), 0.0); } return acclen; From 978bd6cc85dd3dbb9169dc3eae164c267d4aec3d Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 15 Sep 2025 11:04:18 -0300 Subject: [PATCH 20/28] MNT: Leave empty "time aware" indices/displacements for accelerators without "time aware" elements. --- include/trackcpp/passmethods.hpp | 2 +- src/accelerator.cpp | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index f602035..d4209c8 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -510,7 +510,7 @@ inline void adjust_path_length( const std::vector& time_aware_displacements, unsigned int& time_aware_pivot ) { - if (element_offset == time_aware_indices[time_aware_pivot]) { + if (!time_aware_indices.empty() && element_offset == time_aware_indices[time_aware_pivot]) { double ddl = light_speed*accelerator.harmonic_number/element.frequency - line_length; pos.dl -= ddl * time_aware_displacements[time_aware_pivot] / line_length; if (time_aware_pivot < time_aware_indices.size() - 1) {time_aware_pivot++;} diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 0f084d0..f5646aa 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -82,11 +82,7 @@ double Accelerator::get_time_aware_elements_info( element_offset = (element_offset + 1) % nr_elements; } - // In case no element is "time aware" - if (time_aware_indices.size() < 1) { - time_aware_indices.push_back(UINT_MAX); - time_aware_displacements.push_back(0.0); - } else { + if (time_aware_indices.size() > 0) { time_aware_displacements[0] += s_pos; //? NOTE : The diference between "acclen" and the sum of "time_aware_displacements" is on the order of 1e-15 ~ 1e-16, //? and the propagation of this tiny error affects the tracking. The following line avoids losing precision. From a7c723dd0ce212b233558bda88e4008eaa16ce17 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 17 Sep 2025 12:18:45 -0300 Subject: [PATCH 21/28] ENH: improving functions: "get_time_aware_elements_info" and "adjust_path_length" --- include/trackcpp/accelerator.h | 4 ++-- include/trackcpp/passmethods.hpp | 6 ++---- src/accelerator.cpp | 27 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/trackcpp/accelerator.h b/include/trackcpp/accelerator.h index c0b21ce..32bc008 100644 --- a/include/trackcpp/accelerator.h +++ b/include/trackcpp/accelerator.h @@ -40,9 +40,9 @@ class Accelerator { bool isequal(const Accelerator& a) const { return *this == a; } // necessary for python_package double get_length() const; friend std::ostream& operator<< (std::ostream &out, const Accelerator& a); - double get_time_aware_elements_info( + void get_time_aware_elements_info( std::vector& time_aware_indices, - std::vector& time_aware_displacements, + std::vector& time_aware_dl_kicks, unsigned int element_offset = 0 ) const; }; diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index d4209c8..5970a89 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -505,14 +505,12 @@ inline void adjust_path_length( const Element& element, unsigned int& element_offset, Pos& pos, - const double line_length, const std::vector& time_aware_indices, - const std::vector& time_aware_displacements, + const std::vector& time_aware_dl_kicks, unsigned int& time_aware_pivot ) { if (!time_aware_indices.empty() && element_offset == time_aware_indices[time_aware_pivot]) { - double ddl = light_speed*accelerator.harmonic_number/element.frequency - line_length; - pos.dl -= ddl * time_aware_displacements[time_aware_pivot] / line_length; + pos.dl -= time_aware_dl_kicks[time_aware_pivot]; if (time_aware_pivot < time_aware_indices.size() - 1) {time_aware_pivot++;} } } diff --git a/src/accelerator.cpp b/src/accelerator.cpp index f5646aa..37e0a18 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -53,23 +53,25 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) { return out; } -double Accelerator::get_time_aware_elements_info( +void Accelerator::get_time_aware_elements_info( std::vector& time_aware_indices, - std::vector& time_aware_displacements, + std::vector& time_aware_dl_kicks, unsigned int element_offset ) const { // for longitudinal kick before RF cavities time_aware_indices.clear(); - time_aware_displacements.clear(); + time_aware_dl_kicks.clear(); + std::vector time_aware_displacements = {}; + size_t nr_elements = this->lattice.size(); PassMethodsClass PMClass = PassMethodsClass(); double s_pos = 0.0; - double acclen = 0.0; + for (size_t i = 0; i < nr_elements; i++) { const Element& element = this->lattice[element_offset]; - acclen += element.length; + if (PMClass.is_time_aware_pm(element.pass_method)) { time_aware_indices.push_back(i); s_pos += 0.5 * element.length; @@ -84,11 +86,18 @@ double Accelerator::get_time_aware_elements_info( if (time_aware_indices.size() > 0) { time_aware_displacements[0] += s_pos; - //? NOTE : The diference between "acclen" and the sum of "time_aware_displacements" is on the order of 1e-15 ~ 1e-16, - //? and the propagation of this tiny error affects the tracking. The following line avoids losing precision. - time_aware_displacements.back() += acclen - std::accumulate(time_aware_displacements.begin(), time_aware_displacements.end(), 0.0); } - return acclen; + //? NOTE : The diference between "line_length" and the sum of "time_aware_displacements" (cum_length) is on the order of + //? 1e-15 ~ 1e-16. The propagation of this tiny error affects the tracking. The following line avoids losing precision. + double cum_length = std::accumulate(time_aware_displacements.begin(), time_aware_displacements.end(), 0.0); + double line_length = this->get_length(); + time_aware_displacements.back() += line_length - cum_length; + + double ddl = 0.0; + for (size_t i=0; iharmonic_number/this->lattice[time_aware_indices[i]].frequency - line_length; + time_aware_dl_kicks.push_back(ddl * time_aware_displacements[i] / line_length); + } } From b45f323690675239c499f98bcda02fbdd13d26f0 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 17 Sep 2025 12:21:02 -0300 Subject: [PATCH 22/28] MNT: match args for "get_time_aware_elements_info" and "adjust_path_length" across trackcpp --- include/trackcpp/tracking.h | 27 ++++++++------------ python_package/interface.cpp | 9 +++---- src/commands.cpp | 8 +++--- src/optics.cpp | 10 ++++---- src/tests.cpp | 25 +++++++++--------- src/tracking.cpp | 49 ++++++++++++++++++------------------ tests/test-kickmap.cpp | 10 ++++---- 7 files changed, 64 insertions(+), 74 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index a5536cd..b2a9b71 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -144,9 +144,8 @@ Status::type track_linepass ( unsigned int& element_offset, std::vector >& pos, Plane::type& lost_plane, - const double line_length, const std::vector& time_aware_indices, - const std::vector& time_aware_displacements + const std::vector& time_aware_dl_kicks ) { Status::type status = Status::success; @@ -178,9 +177,8 @@ Status::type track_linepass ( element, element_offset, orig_pos, - line_length, time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, time_aware_pivot ); status = track_elementpass(accelerator, element, orig_pos); @@ -234,9 +232,8 @@ Status::type track_linepass ( unsigned int& element_offset, std::vector >& pos, Plane::type& lost_plane, - const double line_length, const std::vector& time_aware_indices, - const std::vector& time_aware_displacements + const std::vector& time_aware_dl_kicks ) { std::vector indices; unsigned int nr_elements = accelerator.lattice.size(); @@ -254,9 +251,8 @@ Status::type track_linepass ( element_offset, pos, lost_plane, - line_length, time_aware_indices, - time_aware_displacements + time_aware_dl_kicks ); } @@ -294,9 +290,8 @@ Status::type track_linepass ( std::vector& lost_plane, std::vector& lost_flag, std::vector& lost_element, - const double line_length, const std::vector& time_aware_indices, - const std::vector& time_aware_displacements + const std::vector& time_aware_dl_kicks ) { int nr_elements = accelerator.lattice.size(); @@ -320,9 +315,8 @@ Status::type track_linepass ( le, final_pos, lp, - line_length, time_aware_indices, - time_aware_displacements + time_aware_dl_kicks ); if (status2 != Status::success){ @@ -404,10 +398,10 @@ Status::type track_ringpass ( // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); @@ -425,9 +419,8 @@ Status::type track_ringpass ( element_offset, final_pos, lost_plane, - line_length, time_aware_indices, - time_aware_displacements + time_aware_dl_kicks )) != Status::success) { // fill last of vector with nans diff --git a/python_package/interface.cpp b/python_package/interface.cpp index 7d417b2..cbe0775 100644 --- a/python_package/interface.cpp +++ b/python_package/interface.cpp @@ -64,10 +64,10 @@ Status::type track_linepass_wrapper( // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, args.element_offset ); @@ -80,9 +80,8 @@ Status::type track_linepass_wrapper( args.lost_plane, args.lost_flag, args.lost_element, - line_length, time_aware_indices, - time_aware_displacements + time_aware_dl_kicks ); for (unsigned int i=0; i& args) { unsigned int offset_element = start_element; // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, offset_element ); track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); std::cout << get_timestamp() << " saving track_linepass data to file" << std::endl; diff --git a/src/optics.cpp b/src/optics.cpp index 7e5bdd3..a223c37 100644 --- a/src/optics.cpp +++ b/src/optics.cpp @@ -84,16 +84,16 @@ Status::type calc_twiss(Accelerator& accelerator, // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); Status::type status = track_linepass( accelerator, fp, true, element_offset, closed_orbit, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); if (status != Status::success) return status; @@ -163,7 +163,7 @@ Status::type calc_twiss(Accelerator& accelerator, fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( accelerator, fpp, true, element_offset, codp, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); if (status != Status::success) return status; } diff --git a/src/tests.cpp b/src/tests.cpp index d7ede03..a4956e9 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -39,16 +39,16 @@ int test_linepass(const Accelerator& accelerator) { // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); Status::type status = track_linepass( accelerator, pos, true, element_offset, new_pos, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); std::cout << "status: " << string_error_messages[status] << std::endl; @@ -81,16 +81,16 @@ int test_linepass_tpsa(const Accelerator& accelerator, const std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); track_linepass( accelerator, tpsa, false, element_offset, new_tpsa, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); for(unsigned int i=0; i >& c = new_particles[i]; @@ -433,10 +433,10 @@ int test_linepass2() { // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); @@ -450,9 +450,8 @@ int test_linepass2() { element_offset, pos, lost_plane, - line_length, time_aware_indices, - time_aware_displacements + time_aware_dl_kicks ); for(unsigned int i=0; i<10; ++i) { diff --git a/src/tracking.cpp b/src/tracking.cpp index 8c36689..f2dac98 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -70,10 +70,10 @@ Status::type track_findm66 (Accelerator& accelerator, unsigned int element_offset = 0; unsigned int time_aware_pivot = 0; std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); @@ -102,9 +102,8 @@ Status::type track_findm66 (Accelerator& accelerator, lattice[i], element_offset, map, - line_length, time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, time_aware_pivot ); // track through element @@ -181,10 +180,10 @@ Status::type track_findorbit6( // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); @@ -198,31 +197,31 @@ Status::type track_findorbit6( status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[0], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[1], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[2], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[3], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[4], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[5], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[6], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); if (status != Status::success) { @@ -259,7 +258,7 @@ Status::type track_findorbit6( Plane::type lost_plane; track_linepass( accelerator, co[6], true, element_offset, closed_orbit, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); accelerator.radiation_on = radsts; return Status::success; @@ -293,10 +292,10 @@ Status::type track_findorbit4( // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); @@ -309,23 +308,23 @@ Status::type track_findorbit4( Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[0], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[1], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[2], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[3], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); status = (Status::type) ((int) status | (int) track_linepass( accelerator, co[6], false, element_offset, co2, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks )); if (status != Status::success) { return Status::findorbit_one_turn_matrix_problem; @@ -356,7 +355,7 @@ Status::type track_findorbit4( Plane::type lost_plane; track_linepass( accelerator, co[6], true, element_offset, closed_orbit, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); accelerator.radiation_on = radsts; return Status::success; diff --git a/tests/test-kickmap.cpp b/tests/test-kickmap.cpp index a8ac249..41d5cbe 100644 --- a/tests/test-kickmap.cpp +++ b/tests/test-kickmap.cpp @@ -67,16 +67,16 @@ int main() { // for longitudinal kick before RF cavities std::vector time_aware_indices; - std::vector time_aware_displacements; - double line_length = accelerator.get_time_aware_elements_info( + std::vector time_aware_dl_kicks; + accelerator.get_time_aware_elements_info( time_aware_indices, - time_aware_displacements, + time_aware_dl_kicks, element_offset ); status = track_linepass( accelerator, fp, true, element_offset, closed_orbit, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); if (status != Status::success) return status; @@ -130,7 +130,7 @@ int main() { fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( accelerator, fpp, true, element_offset, codp, lost_plane, - line_length, time_aware_indices, time_aware_displacements + time_aware_indices, time_aware_dl_kicks ); std::cout << "h2" << std::endl; if (status != Status::success) return status; From 6b82a9ac6b595d60fdeea8b53aab91e06d89d07f Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 17 Sep 2025 12:23:43 -0300 Subject: [PATCH 23/28] DOC: rewrite comments --- include/trackcpp/tracking.h | 4 ++-- python_package/interface.cpp | 2 +- src/accelerator.cpp | 2 +- src/commands.cpp | 2 +- src/optics.cpp | 2 +- src/tests.cpp | 6 +++--- src/tracking.cpp | 6 +++--- tests/test-kickmap.cpp | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index b2a9b71..20a7ab1 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -152,7 +152,7 @@ Status::type track_linepass ( const std::vector& line = accelerator.lattice; int nr_elements = line.size(); - // For longitudinal RF kick + // for adjusting dl to keep the arrival-time in sync with the wall clock unsigned int time_aware_pivot = 0; //pos.clear(); other functions assume pos is not clearedin linepass! @@ -396,7 +396,7 @@ Status::type track_ringpass ( Status::type status = Status::success; std::vector > final_pos; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/python_package/interface.cpp b/python_package/interface.cpp index cbe0775..4c309b3 100644 --- a/python_package/interface.cpp +++ b/python_package/interface.cpp @@ -62,7 +62,7 @@ Status::type track_linepass_wrapper( ); } - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 37e0a18..7243a54 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -59,7 +59,7 @@ void Accelerator::get_time_aware_elements_info( unsigned int element_offset ) const { - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock time_aware_indices.clear(); time_aware_dl_kicks.clear(); std::vector time_aware_displacements = {}; diff --git a/src/commands.cpp b/src/commands.cpp index ef36d3a..7b09738 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1084,7 +1084,7 @@ int cmd_track_linepass(const std::vector& args) { std::vector> pos_list; Plane::type lost_plane; unsigned int offset_element = start_element; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/src/optics.cpp b/src/optics.cpp index a223c37..d5af28e 100644 --- a/src/optics.cpp +++ b/src/optics.cpp @@ -82,7 +82,7 @@ Status::type calc_twiss(Accelerator& accelerator, Plane::type lost_plane; unsigned int element_offset = 0; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/src/tests.cpp b/src/tests.cpp index a4956e9..da21950 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -37,7 +37,7 @@ int test_linepass(const Accelerator& accelerator) { unsigned int element_offset = 0; Plane::type lost_plane; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( @@ -79,7 +79,7 @@ int test_linepass_tpsa(const Accelerator& accelerator, const std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( @@ -431,7 +431,7 @@ int test_linepass2() { Plane::type lost_plane; bool trajectory = true; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/src/tracking.cpp b/src/tracking.cpp index f2dac98..69c4d9c 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -66,7 +66,7 @@ Status::type track_findm66 (Accelerator& accelerator, map.ry = Tpsa<6,1>(fp.ry, 2); map.py = Tpsa<6,1>(fp.py, 3); map.de = Tpsa<6,1>(fp.de, 4); map.dl = Tpsa<6,1>(fp.dl, 5); - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock unsigned int element_offset = 0; unsigned int time_aware_pivot = 0; std::vector time_aware_indices; @@ -178,7 +178,7 @@ Status::type track_findorbit6( Pos dco(1.0,1.0,1.0,1.0,1.0,1.0); matrix6_set_identity_posvec(D, delta); - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( @@ -290,7 +290,7 @@ Status::type track_findorbit4( Pos theta(0.0,0.0,0.0,0.0,0.0,0.0); matrix6_set_identity_posvec(D, delta); - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( diff --git a/tests/test-kickmap.cpp b/tests/test-kickmap.cpp index 41d5cbe..f618dc3 100644 --- a/tests/test-kickmap.cpp +++ b/tests/test-kickmap.cpp @@ -65,7 +65,7 @@ int main() { Plane::type lost_plane; unsigned int element_offset = 0; - // for longitudinal kick before RF cavities + // for adjusting dl to keep the arrival-time in sync with the wall clock std::vector time_aware_indices; std::vector time_aware_dl_kicks; accelerator.get_time_aware_elements_info( From 5a981424b83e460c382f0002bb506552c7960167 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 24 Sep 2025 15:32:58 -0300 Subject: [PATCH 24/28] ENH: optimize and beautify the usage of time aware info --- include/trackcpp/accelerator.h | 8 +++----- include/trackcpp/passmethods.hpp | 16 +++++++--------- src/accelerator.cpp | 25 ++++++++++--------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/include/trackcpp/accelerator.h b/include/trackcpp/accelerator.h index 32bc008..9116a61 100644 --- a/include/trackcpp/accelerator.h +++ b/include/trackcpp/accelerator.h @@ -34,17 +34,15 @@ class Accelerator { int harmonic_number = 0; std::vector lattice; std::string lattice_version = ""; + mutable std::vector time_aware_indices; + mutable std::vector time_aware_dl_kicks; bool operator==(const Accelerator& o) const; bool operator!=(const Accelerator& o) const { return !(*this == o); }; bool isequal(const Accelerator& a) const { return *this == a; } // necessary for python_package double get_length() const; friend std::ostream& operator<< (std::ostream &out, const Accelerator& a); - void get_time_aware_elements_info( - std::vector& time_aware_indices, - std::vector& time_aware_dl_kicks, - unsigned int element_offset = 0 - ) const; + void update_time_aware_info(void) const; }; #endif diff --git a/include/trackcpp/passmethods.hpp b/include/trackcpp/passmethods.hpp index 5970a89..0931064 100644 --- a/include/trackcpp/passmethods.hpp +++ b/include/trackcpp/passmethods.hpp @@ -32,6 +32,7 @@ #include "tpsa.h" #include "linalg.h" #include +#include template inline T SQR(const T& X) { return X*X; } template inline T POW3(const T& X) { return X*X*X; } @@ -502,16 +503,13 @@ Status::type pm_matrix_pass(Pos &pos, const Element &elem, template inline void adjust_path_length( const Accelerator& accelerator, - const Element& element, - unsigned int& element_offset, - Pos& pos, - const std::vector& time_aware_indices, - const std::vector& time_aware_dl_kicks, - unsigned int& time_aware_pivot + unsigned int& element_index, + Pos& pos ) { - if (!time_aware_indices.empty() && element_offset == time_aware_indices[time_aware_pivot]) { - pos.dl -= time_aware_dl_kicks[time_aware_pivot]; - if (time_aware_pivot < time_aware_indices.size() - 1) {time_aware_pivot++;} + auto it = std::find(accelerator.time_aware_indices.begin(), accelerator.time_aware_indices.end(), element_index); + if (it != accelerator.time_aware_indices.end()) { + auto idx = std::distance(accelerator.time_aware_indices.begin(), it); + pos.dl -= accelerator.time_aware_dl_kicks[idx]; } } diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 7243a54..7703070 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -53,15 +53,11 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) { return out; } -void Accelerator::get_time_aware_elements_info( - std::vector& time_aware_indices, - std::vector& time_aware_dl_kicks, - unsigned int element_offset -) const { +void Accelerator::update_time_aware_info(void) const { // for adjusting dl to keep the arrival-time in sync with the wall clock - time_aware_indices.clear(); - time_aware_dl_kicks.clear(); + this->time_aware_indices.clear(); + this->time_aware_dl_kicks.clear(); std::vector time_aware_displacements = {}; size_t nr_elements = this->lattice.size(); @@ -70,10 +66,10 @@ void Accelerator::get_time_aware_elements_info( double s_pos = 0.0; for (size_t i = 0; i < nr_elements; i++) { - const Element& element = this->lattice[element_offset]; + const Element& element = this->lattice[i]; if (PMClass.is_time_aware_pm(element.pass_method)) { - time_aware_indices.push_back(i); + this->time_aware_indices.push_back(i); s_pos += 0.5 * element.length; time_aware_displacements.push_back(s_pos); s_pos = 0.5 * element.length; @@ -81,23 +77,22 @@ void Accelerator::get_time_aware_elements_info( else { s_pos += element.length; } - element_offset = (element_offset + 1) % nr_elements; } - if (time_aware_indices.size() > 0) { + if (this->time_aware_indices.size() > 0) { time_aware_displacements[0] += s_pos; } //? NOTE : The diference between "line_length" and the sum of "time_aware_displacements" (cum_length) is on the order of - //? 1e-15 ~ 1e-16. The propagation of this tiny error affects the tracking. The following line avoids losing precision. + //? 1e-15 ~ 1e-16. The propagation of this tiny error affects the tracking. The following lines avoid losing precision. double cum_length = std::accumulate(time_aware_displacements.begin(), time_aware_displacements.end(), 0.0); double line_length = this->get_length(); time_aware_displacements.back() += line_length - cum_length; double ddl = 0.0; - for (size_t i=0; iharmonic_number/this->lattice[time_aware_indices[i]].frequency - line_length; - time_aware_dl_kicks.push_back(ddl * time_aware_displacements[i] / line_length); + for (size_t i=0; itime_aware_indices.size(); i++) { + ddl = light_speed*this->harmonic_number/this->lattice[this->time_aware_indices[i]].frequency - line_length; + this->time_aware_dl_kicks.push_back(ddl * time_aware_displacements[i] / line_length); } } From 0535045f5d576468ea13a226bde7a62c0b41f218 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 24 Sep 2025 15:33:39 -0300 Subject: [PATCH 25/28] MNT: match and propagate last modification --- include/trackcpp/tracking.h | 47 +++++--------------- python_package/interface.cpp | 12 +---- src/commands.cpp | 11 +---- src/optics.cpp | 14 ++---- src/tests.cpp | 34 +++------------ src/tracking.cpp | 85 ++++++++++-------------------------- tests/test-kickmap.cpp | 14 ++---- 7 files changed, 48 insertions(+), 169 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index 20a7ab1..f7aa759 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -143,18 +143,13 @@ Status::type track_linepass ( const std::vector& indices, unsigned int& element_offset, std::vector >& pos, - Plane::type& lost_plane, - const std::vector& time_aware_indices, - const std::vector& time_aware_dl_kicks + Plane::type& lost_plane ) { Status::type status = Status::success; const std::vector& line = accelerator.lattice; int nr_elements = line.size(); - // for adjusting dl to keep the arrival-time in sync with the wall clock - unsigned int time_aware_pivot = 0; - //pos.clear(); other functions assume pos is not clearedin linepass! pos.reserve(pos.size() + indices.size()); @@ -172,15 +167,9 @@ Status::type track_linepass ( // stores trajectory at entrance of each element if (indcs[i]) pos.push_back(orig_pos); - adjust_path_length( - accelerator, - element, - element_offset, - orig_pos, - time_aware_indices, - time_aware_dl_kicks, - time_aware_pivot - ); + // for adjusting dl to keep the arrival-time in sync with the wall clock + adjust_path_length(accelerator, element_offset, orig_pos); + status = track_elementpass(accelerator, element, orig_pos); lost_plane = check_particle_loss(accelerator, element, orig_pos); if (lost_plane != Plane::no_plane) status = Status::particle_lost; @@ -231,9 +220,7 @@ Status::type track_linepass ( const bool trajectory, unsigned int& element_offset, std::vector >& pos, - Plane::type& lost_plane, - const std::vector& time_aware_indices, - const std::vector& time_aware_dl_kicks + Plane::type& lost_plane ) { std::vector indices; unsigned int nr_elements = accelerator.lattice.size(); @@ -250,9 +237,7 @@ Status::type track_linepass ( indices, element_offset, pos, - lost_plane, - time_aware_indices, - time_aware_dl_kicks + lost_plane ); } @@ -289,9 +274,7 @@ Status::type track_linepass ( std::vector> &pos, std::vector& lost_plane, std::vector& lost_flag, - std::vector& lost_element, - const std::vector& time_aware_indices, - const std::vector& time_aware_dl_kicks + std::vector& lost_element ) { int nr_elements = accelerator.lattice.size(); @@ -314,9 +297,7 @@ Status::type track_linepass ( indices, le, final_pos, - lp, - time_aware_indices, - time_aware_dl_kicks + lp ); if (status2 != Status::success){ @@ -397,13 +378,7 @@ Status::type track_ringpass ( std::vector > final_pos; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); if (turn_by_turn) pos.reserve(nr_turns+1); @@ -418,9 +393,7 @@ Status::type track_ringpass ( false, element_offset, final_pos, - lost_plane, - time_aware_indices, - time_aware_dl_kicks + lost_plane )) != Status::success) { // fill last of vector with nans diff --git a/python_package/interface.cpp b/python_package/interface.cpp index 4c309b3..c72095d 100644 --- a/python_package/interface.cpp +++ b/python_package/interface.cpp @@ -63,13 +63,7 @@ Status::type track_linepass_wrapper( } // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - args.element_offset - ); + accelerator.update_time_aware_info(); Status::type status = track_linepass( accelerator, @@ -79,9 +73,7 @@ Status::type track_linepass_wrapper( post, args.lost_plane, args.lost_flag, - args.lost_element, - time_aware_indices, - time_aware_dl_kicks + args.lost_element ); for (unsigned int i=0; i& args) { Plane::type lost_plane; unsigned int offset_element = start_element; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - offset_element - ); - track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator.update_time_aware_info(); + track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane ); std::cout << get_timestamp() << " saving track_linepass data to file" << std::endl; diff --git a/src/optics.cpp b/src/optics.cpp index d5af28e..b2a99f6 100644 --- a/src/optics.cpp +++ b/src/optics.cpp @@ -83,17 +83,10 @@ Status::type calc_twiss(Accelerator& accelerator, unsigned int element_offset = 0; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); Status::type status = track_linepass( - accelerator, fp, true, element_offset, closed_orbit, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, fp, true, element_offset, closed_orbit, lost_plane ); if (status != Status::success) return status; @@ -162,8 +155,7 @@ Status::type calc_twiss(Accelerator& accelerator, fpp.ry += twiss0.etay[0] * dpp; fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( - accelerator, fpp, true, element_offset, codp, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, fpp, true, element_offset, codp, lost_plane ); if (status != Status::success) return status; } diff --git a/src/tests.cpp b/src/tests.cpp index da21950..81e1754 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -38,17 +38,10 @@ int test_linepass(const Accelerator& accelerator) { Plane::type lost_plane; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); Status::type status = track_linepass( - accelerator, pos, true, element_offset, new_pos, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, pos, true, element_offset, new_pos, lost_plane ); std::cout << "status: " << string_error_messages[status] << std::endl; @@ -80,17 +73,10 @@ int test_linepass_tpsa(const Accelerator& accelerator, const std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); track_linepass( - accelerator, tpsa, false, element_offset, new_tpsa, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, tpsa, false, element_offset, new_tpsa, lost_plane ); for(unsigned int i=0; i >& c = new_particles[i]; @@ -432,13 +418,7 @@ int test_linepass2() { bool trajectory = true; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); orig_pos.rx = 0.0001; orig_pos.px = 0.0001; @@ -449,9 +429,7 @@ int test_linepass2() { trajectory, element_offset, pos, - lost_plane, - time_aware_indices, - time_aware_dl_kicks + lost_plane ); for(unsigned int i=0; i<10; ++i) { diff --git a/src/tracking.cpp b/src/tracking.cpp index 69c4d9c..86ccaab 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -67,16 +67,7 @@ Status::type track_findm66 (Accelerator& accelerator, map.de = Tpsa<6,1>(fp.de, 4); map.dl = Tpsa<6,1>(fp.dl, 5); // for adjusting dl to keep the arrival-time in sync with the wall clock - unsigned int element_offset = 0; - unsigned int time_aware_pivot = 0; - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); - + accelerator.update_time_aware_info(); tm.clear(); tm.reserve(indices.size()); for(unsigned int i=0; i& fixed_point_guess) { const std::vector& the_ring = accelerator.lattice; - unsigned int element_offset = 0; double delta = 1e-9; // [m],[rad],[dE/E] double tolerance = 2.22044604925e-14; int max_nr_iters = 50; @@ -179,49 +161,37 @@ Status::type track_findorbit6( matrix6_set_identity_posvec(D, delta); // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); int nr_iter = 0; while ((get_max(dco) > tolerance) and (nr_iter <= max_nr_iters)) { co = co + D; Pos Ri = co[6]; std::vector > co2; + unsigned int element_offset = 0; Plane::type lost_plane; Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[0], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[0], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[1], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[1], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[2], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[2], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[3], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[3], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[4], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[4], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[5], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[5], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[6], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[6], false, element_offset, co2, lost_plane )); if (status != Status::success) { @@ -255,10 +225,10 @@ Status::type track_findorbit6( // propagates fixed point throught the_ring closed_orbit.clear(); + unsigned int element_offset = 0; Plane::type lost_plane; track_linepass( - accelerator, co[6], true, element_offset, closed_orbit, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[6], true, element_offset, closed_orbit, lost_plane ); accelerator.radiation_on = radsts; return Status::success; @@ -271,7 +241,6 @@ Status::type track_findorbit4( const Pos& fixed_point_guess) { const std::vector& the_ring = accelerator.lattice; - unsigned int element_offset = 0; double delta = 1e-9; // [m],[rad],[dE/E] double tolerance = 2.22044604925e-14; int max_nr_iters = 50; @@ -291,40 +260,30 @@ Status::type track_findorbit4( matrix6_set_identity_posvec(D, delta); // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); int nr_iter = 0; while ((get_max(dco) > tolerance) and (nr_iter <= max_nr_iters)) { co = co + D; Pos Ri = co[6]; std::vector > co2; + unsigned int element_offset = 0; Plane::type lost_plane; Status::type status = Status::success; status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[0], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[0], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[1], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[1], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[2], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[2], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[3], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[3], false, element_offset, co2, lost_plane )); status = (Status::type) ((int) status | (int) track_linepass( - accelerator, co[6], false, element_offset, co2, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[6], false, element_offset, co2, lost_plane )); if (status != Status::success) { return Status::findorbit_one_turn_matrix_problem; @@ -352,10 +311,10 @@ Status::type track_findorbit4( // propagates fixed point throught the_ring closed_orbit.clear(); + unsigned int element_offset = 0; Plane::type lost_plane; track_linepass( - accelerator, co[6], true, element_offset, closed_orbit, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, co[6], true, element_offset, closed_orbit, lost_plane ); accelerator.radiation_on = radsts; return Status::success; diff --git a/tests/test-kickmap.cpp b/tests/test-kickmap.cpp index f618dc3..ef41f23 100644 --- a/tests/test-kickmap.cpp +++ b/tests/test-kickmap.cpp @@ -66,17 +66,10 @@ int main() { unsigned int element_offset = 0; // for adjusting dl to keep the arrival-time in sync with the wall clock - std::vector time_aware_indices; - std::vector time_aware_dl_kicks; - accelerator.get_time_aware_elements_info( - time_aware_indices, - time_aware_dl_kicks, - element_offset - ); + accelerator.update_time_aware_info(); status = track_linepass( - accelerator, fp, true, element_offset, closed_orbit, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, fp, true, element_offset, closed_orbit, lost_plane ); if (status != Status::success) return status; @@ -129,8 +122,7 @@ int main() { fpp.ry += twiss0.etay[0] * dpp; fpp.py += twiss0.etay[1] * dpp; Status::type status = track_linepass( - accelerator, fpp, true, element_offset, codp, lost_plane, - time_aware_indices, time_aware_dl_kicks + accelerator, fpp, true, element_offset, codp, lost_plane ); std::cout << "h2" << std::endl; if (status != Status::success) return status; From 352ead76f61fe970c60bd3813ffc215ea27a4d19 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 10 Oct 2025 15:17:44 -0300 Subject: [PATCH 26/28] MNT/DOC: edit some comments --- include/trackcpp/tracking.h | 4 ++-- python_package/interface.cpp | 2 +- src/accelerator.cpp | 2 +- src/commands.cpp | 2 +- src/optics.cpp | 2 +- src/tests.cpp | 6 +++--- src/tracking.cpp | 6 +++--- tests/test-kickmap.cpp | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index f7aa759..e75df10 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -167,7 +167,7 @@ Status::type track_linepass ( // stores trajectory at entrance of each element if (indcs[i]) pos.push_back(orig_pos); - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock adjust_path_length(accelerator, element_offset, orig_pos); status = track_elementpass(accelerator, element, orig_pos); @@ -377,7 +377,7 @@ Status::type track_ringpass ( Status::type status = Status::success; std::vector > final_pos; - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); if (turn_by_turn) pos.reserve(nr_turns+1); diff --git a/python_package/interface.cpp b/python_package/interface.cpp index c72095d..7e99942 100644 --- a/python_package/interface.cpp +++ b/python_package/interface.cpp @@ -62,7 +62,7 @@ Status::type track_linepass_wrapper( ); } - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); Status::type status = track_linepass( diff --git a/src/accelerator.cpp b/src/accelerator.cpp index 7703070..04a7f4d 100644 --- a/src/accelerator.cpp +++ b/src/accelerator.cpp @@ -55,7 +55,7 @@ std::ostream& operator<< (std::ostream &out, const Accelerator& a) { void Accelerator::update_time_aware_info(void) const { - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock this->time_aware_indices.clear(); this->time_aware_dl_kicks.clear(); std::vector time_aware_displacements = {}; diff --git a/src/commands.cpp b/src/commands.cpp index e5b9a62..6df24f2 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1084,7 +1084,7 @@ int cmd_track_linepass(const std::vector& args) { std::vector> pos_list; Plane::type lost_plane; unsigned int offset_element = start_element; - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); track_linepass(accelerator, pos, true, offset_element, pos_list, lost_plane ); diff --git a/src/optics.cpp b/src/optics.cpp index b2a99f6..f38c83f 100644 --- a/src/optics.cpp +++ b/src/optics.cpp @@ -82,7 +82,7 @@ Status::type calc_twiss(Accelerator& accelerator, Plane::type lost_plane; unsigned int element_offset = 0; - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); Status::type status = track_linepass( diff --git a/src/tests.cpp b/src/tests.cpp index 81e1754..a084365 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -37,7 +37,7 @@ int test_linepass(const Accelerator& accelerator) { unsigned int element_offset = 0; Plane::type lost_plane; - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); Status::type status = track_linepass( @@ -72,7 +72,7 @@ int test_linepass_tpsa(const Accelerator& accelerator, const std::vector(fp.ry, 2); map.py = Tpsa<6,1>(fp.py, 3); map.de = Tpsa<6,1>(fp.de, 4); map.dl = Tpsa<6,1>(fp.dl, 5); - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); tm.clear(); tm.reserve(indices.size()); @@ -160,7 +160,7 @@ Status::type track_findorbit6( Pos dco(1.0,1.0,1.0,1.0,1.0,1.0); matrix6_set_identity_posvec(D, delta); - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); int nr_iter = 0; @@ -259,7 +259,7 @@ Status::type track_findorbit4( Pos theta(0.0,0.0,0.0,0.0,0.0,0.0); matrix6_set_identity_posvec(D, delta); - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); int nr_iter = 0; diff --git a/tests/test-kickmap.cpp b/tests/test-kickmap.cpp index ef41f23..8f8eb1e 100644 --- a/tests/test-kickmap.cpp +++ b/tests/test-kickmap.cpp @@ -65,7 +65,7 @@ int main() { Plane::type lost_plane; unsigned int element_offset = 0; - // for adjusting dl to keep the arrival-time in sync with the wall clock + // adjust dl to keep the arrival-time in sync with wall clock accelerator.update_time_aware_info(); status = track_linepass( From 3386063bf078d85ebe57a8bba0e9155e0fcd89be Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 2 Feb 2026 09:48:59 -0300 Subject: [PATCH 27/28] ENH: reorder pos.push_back and adjust_path_length; add comments; --- include/trackcpp/tracking.h | 8 +++++--- src/tracking.cpp | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/trackcpp/tracking.h b/include/trackcpp/tracking.h index e75df10..0542992 100644 --- a/include/trackcpp/tracking.h +++ b/include/trackcpp/tracking.h @@ -164,11 +164,13 @@ Status::type track_linepass ( // syntactic-sugar for read-only access to element object parameters const Element& element = line[element_offset]; - // stores trajectory at entrance of each element - if (indcs[i]) pos.push_back(orig_pos); - // adjust dl to keep the arrival-time in sync with wall clock + // note: for performance reasons, the adjustment of the path length + // is done only at the beginning of "time aware" elements (e.g. RF cavities) adjust_path_length(accelerator, element_offset, orig_pos); + + // stores trajectory at entrance of each element + if (indcs[i]) pos.push_back(orig_pos); status = track_elementpass(accelerator, element, orig_pos); lost_plane = check_particle_loss(accelerator, element, orig_pos); diff --git a/src/tracking.cpp b/src/tracking.cpp index c262477..957ee12 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -71,7 +71,9 @@ Status::type track_findm66 (Accelerator& accelerator, tm.clear(); tm.reserve(indices.size()); for(unsigned int i=0; i Date: Fri, 6 Mar 2026 10:50:42 -0300 Subject: [PATCH 28/28] Update version --- python_package/trackcpp/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_package/trackcpp/VERSION b/python_package/trackcpp/VERSION index 195e81e..0062ac9 100644 --- a/python_package/trackcpp/VERSION +++ b/python_package/trackcpp/VERSION @@ -1 +1 @@ -4.10.8 +5.0.0