From 6998638efa95d9081ce807e88282106f96584635 Mon Sep 17 00:00:00 2001 From: vitor Date: Thu, 6 Jun 2024 09:59:26 -0300 Subject: [PATCH 01/10] tracking-adpt --- pyaccel/tracking.py | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index 33eedae..ba79064 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -173,9 +173,8 @@ def set_6d_tracking(accelerator, rad_full=False): accelerator.cavity_on = True accelerator.radiation_on = 'full' if rad_full else 'damping' - @_interactive -def element_pass(element, particles, energy, **kwargs): +def element_pass(element, particles, energy, nturn=0, **kwargs): """Track particle(s) through an element. Accepts one or multiple particles initial positions. In the latter case, @@ -208,6 +207,8 @@ def element_pass(element, particles, energy, **kwargs): """ # checks if all necessary arguments have been passed kwargs['energy'] = energy + if not isinstance(nturn, (int, _np.integer)): + raise ValueError("nturn arg must be a integer") # creates accelerator for tracking accelerator = _accelerator.Accelerator(**kwargs) @@ -217,7 +218,7 @@ def element_pass(element, particles, energy, **kwargs): # tracks through the list of pos ret = _trackcpp.track_elementpass_wrapper( - element.trackcpp_e, p_in, accelerator.trackcpp_acc) + element.trackcpp_e, p_in, accelerator.trackcpp_acc, float(nturn)) if ret > 0: raise TrackingException @@ -227,7 +228,7 @@ def element_pass(element, particles, energy, **kwargs): @_interactive def line_pass( accelerator, particles, indices=None, element_offset=0, - parallel=False): + parallel=False, nturn=0): """Track particle(s) along a line. Accepts one or multiple particles initial positions. In the latter case, @@ -318,14 +319,14 @@ def line_pass( if not parallel: p_out, lost_flag, lost_element, lost_plane = _line_pass( - accelerator, p_in, indices, element_offset) + accelerator, p_in, indices, element_offset, nturn) else: slcs = _get_slices_multiprocessing(parallel, p_in.shape[1]) with _multiproc.Pool(processes=len(slcs)) as pool: res = [] for slc in slcs: res.append(pool.apply_async(_line_pass, ( - accelerator, p_in[:, slc], indices, element_offset, True))) + accelerator, p_in[:, slc], indices, element_offset, nturn, True))) p_out, lost_element, lost_plane = [], [], [] lost_flag = False @@ -345,12 +346,13 @@ def line_pass( return p_out, lost_flag, lost_element, lost_plane -def _line_pass(accelerator, p_in, indices, element_offset, set_seed=False): +def _line_pass(accelerator, p_in, indices, element_offset, nturn=0, set_seed=False): # store only final position? args = _trackcpp.LinePassArgs() for idx in indices: args.indices.push_back(int(idx)) args.element_offset = int(element_offset) + args.nturn = int(nturn) n_part = p_in.shape[1] p_out = _np.zeros((6, n_part * len(indices)), dtype=float) @@ -641,6 +643,33 @@ def find_orbit6(accelerator, indices=None, fixed_point_guess=None): closed_orbit = _CppDoublePosVector2Numpy(_closed_orbit) return closed_orbit[:, indices] +@_interactive +def find_orbit6_dct(accelerator, indices=None, fixed_point_guess=None, dct=0): + """.""" + indices = _process_indices(accelerator, indices) + + # The orbit can't be found when quantum excitation is on. + rad_stt = accelerator.radiation_on + accelerator.radiation_on = 'damping' + + if fixed_point_guess is None: + fixed_point_guess = _trackcpp.CppDoublePos() + else: + fixed_point_guess = _Numpy2CppDoublePos(fixed_point_guess) + + _closed_orbit = _trackcpp.CppDoublePosVector() + + ret = _trackcpp.track_findorbit6_dct( + accelerator.trackcpp_acc, _closed_orbit, fixed_point_guess, float(dct)) + + accelerator.radiation_on = rad_stt + + if ret > 0: + raise TrackingException(_trackcpp.string_error_messages[ret]) + + closed_orbit = _CppDoublePosVector2Numpy(_closed_orbit) + return closed_orbit[:, indices] + @_interactive def find_orbit( From 9e47b6c6cb0f82545e7df2a228aa7f25e937c1e2 Mon Sep 17 00:00:00 2001 From: vitor Date: Tue, 11 Jun 2024 11:45:18 -0300 Subject: [PATCH 02/10] adapt trackcpp last works --- pyaccel/tracking.py | 76 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index ba79064..18466ef 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -352,7 +352,12 @@ def _line_pass(accelerator, p_in, indices, element_offset, nturn=0, set_seed=Fal for idx in indices: args.indices.push_back(int(idx)) args.element_offset = int(element_offset) - args.nturn = int(nturn) + + args.line_length = accelerator.trackcpp_acc.get_time_aware_elements_info( + args.time_aware_element_indices, + args.time_aware_element_positions, + int(element_offset) + ) n_part = p_in.shape[1] p_out = _np.zeros((6, n_part * len(indices)), dtype=float) @@ -621,10 +626,6 @@ def find_orbit6(accelerator, indices=None, fixed_point_guess=None): """ indices = _process_indices(accelerator, indices) - # The orbit can't be found when quantum excitation is on. - rad_stt = accelerator.radiation_on - accelerator.radiation_on = 'damping' - if fixed_point_guess is None: fixed_point_guess = _trackcpp.CppDoublePos() else: @@ -635,35 +636,6 @@ def find_orbit6(accelerator, indices=None, fixed_point_guess=None): ret = _trackcpp.track_findorbit6( accelerator.trackcpp_acc, _closed_orbit, fixed_point_guess) - accelerator.radiation_on = rad_stt - - if ret > 0: - raise TrackingException(_trackcpp.string_error_messages[ret]) - - closed_orbit = _CppDoublePosVector2Numpy(_closed_orbit) - return closed_orbit[:, indices] - -@_interactive -def find_orbit6_dct(accelerator, indices=None, fixed_point_guess=None, dct=0): - """.""" - indices = _process_indices(accelerator, indices) - - # The orbit can't be found when quantum excitation is on. - rad_stt = accelerator.radiation_on - accelerator.radiation_on = 'damping' - - if fixed_point_guess is None: - fixed_point_guess = _trackcpp.CppDoublePos() - else: - fixed_point_guess = _Numpy2CppDoublePos(fixed_point_guess) - - _closed_orbit = _trackcpp.CppDoublePosVector() - - ret = _trackcpp.track_findorbit6_dct( - accelerator.trackcpp_acc, _closed_orbit, fixed_point_guess, float(dct)) - - accelerator.radiation_on = rad_stt - if ret > 0: raise TrackingException(_trackcpp.string_error_messages[ret]) @@ -755,6 +727,13 @@ def find_m66(accelerator, indices='m66', fixed_point=None): else: trackcpp_idx.push_back(len(accelerator)) + timeaware_element_indices = _trackcpp.CppUnsigIntVector() + timeaware_element_positions = _trackcpp.CppDoubleVector() + linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( + timeaware_element_indices, + timeaware_element_positions + ) + if fixed_point is None: # Closed orbit is calculated by trackcpp fixed_point_guess = _trackcpp.CppDoublePos() @@ -772,8 +751,15 @@ def find_m66(accelerator, indices='m66', fixed_point=None): m66 = _np.zeros((6, 6), dtype=float) _v0 = _trackcpp.CppDoublePos() ret = _trackcpp.track_findm66_wrapper( - accelerator.trackcpp_acc, _closed_orbit[0], cumul_trans_matrices, - m66, _v0, trackcpp_idx) + accelerator.trackcpp_acc, + _closed_orbit[0], + cumul_trans_matrices, + m66, + _v0, + trackcpp_idx, + linelength, + timeaware_element_indices, + timeaware_element_positions) if ret > 0: raise TrackingException(_trackcpp.string_error_messages[ret]) @@ -831,12 +817,26 @@ def find_m44(accelerator, indices='m44', energy_offset=0.0, fixed_point=None): _closed_orbit = _trackcpp.CppDoublePosVector() _closed_orbit.push_back(_fixed_point) + timeaware_element_indices = _trackcpp.CppUnsigIntVector() + timeaware_element_positions = _trackcpp.CppDoubleVector() + linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( + timeaware_element_indices, + timeaware_element_positions + ) + cumul_trans_matrices = _np.zeros((trackcpp_idx.size(), 4, 4), dtype=float) m44 = _np.zeros((4, 4), dtype=float) _v0 = _trackcpp.CppDoublePos() ret = _trackcpp.track_findm66_wrapper( - accelerator.trackcpp_acc, _closed_orbit[0], cumul_trans_matrices, - m44, _v0, trackcpp_idx) + accelerator.trackcpp_acc, + _closed_orbit[0], + cumul_trans_matrices, + m44, + _v0, + trackcpp_idx, + linelength, + timeaware_element_indices, + timeaware_element_positions) if ret > 0: raise TrackingException(_trackcpp.string_error_messages[ret]) From 8de911cb2aee935137d89963eaa7fa7bcb9148ba Mon Sep 17 00:00:00 2001 From: vitor Date: Tue, 11 Jun 2024 11:55:50 -0300 Subject: [PATCH 03/10] avoid local-test notebook from being uploaded --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 18e814d..4e3b9d9 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,5 @@ examples/.* test/flatfile2.txt *.swp + +t.ipynb From b45e4392212e2cbdacdc95ead66a2a081669f735 Mon Sep 17 00:00:00 2001 From: vitor Date: Tue, 11 Jun 2024 14:34:25 -0300 Subject: [PATCH 04/10] saving corrections --- pyaccel/tracking.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index 18466ef..eb71601 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -355,8 +355,7 @@ def _line_pass(accelerator, p_in, indices, element_offset, nturn=0, set_seed=Fal args.line_length = accelerator.trackcpp_acc.get_time_aware_elements_info( args.time_aware_element_indices, - args.time_aware_element_positions, - int(element_offset) + args.time_aware_element_positions ) n_part = p_in.shape[1] From c0bed3bea170753635f25889d1abd3a10340c0bb Mon Sep 17 00:00:00 2001 From: vitor Date: Sat, 31 Aug 2024 19:13:16 -0300 Subject: [PATCH 05/10] FIX: duplicated lines --- pyaccel/tracking.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index aa89059..e4e0372 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -502,11 +502,6 @@ def _line_pass(accelerator, p_in, indices, element_offset, nturn=0, set_seed=Fal args.time_aware_element_positions ) - args.line_length = accelerator.trackcpp_acc.get_time_aware_elements_info( - args.time_aware_element_indices, - args.time_aware_element_positions - ) - p_in = p_in.copy() n_part = p_in.shape[1] p_out = _np.zeros((6, n_part * len(indices)), dtype=float) From 1769cee61037ff6d8e94e1cfe6d3e927a0a62568 Mon Sep 17 00:00:00 2001 From: vitor Date: Sat, 31 Aug 2024 19:15:13 -0300 Subject: [PATCH 06/10] FIX: required version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index dfc3d1e..7d3bdc8 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from distutils.version import StrictVersion -trackcpp_version = '4.10.6' +trackcpp_version = '4.10.5' try: import trackcpp From e8239a9d7427ee5c80fb0935ab8ff46bbfb95ef6 Mon Sep 17 00:00:00 2001 From: vitor Date: Mon, 9 Sep 2024 10:00:58 -0300 Subject: [PATCH 07/10] missing line: rad_sts change --- pyaccel/tracking.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index b4f9fac..b429982 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -821,6 +821,8 @@ def find_orbit6( ret = _trackcpp.track_findorbit6( accelerator.trackcpp_acc, _closed_orbit, fixed_point_guess) + accelerator.radiation_on = rad_stt + if ret > 0: raise TrackingError(_trackcpp.string_error_messages[ret]) From ffaa97cb4f97b96c5127e240a9579ebd456036c3 Mon Sep 17 00:00:00 2001 From: vitor Date: Mon, 9 Sep 2024 10:23:40 -0300 Subject: [PATCH 08/10] removing "nturn" arg (old/test) --- .gitignore | 2 -- pyaccel/tracking.py | 17 +++++++---------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 4e3b9d9..18e814d 100644 --- a/.gitignore +++ b/.gitignore @@ -72,5 +72,3 @@ examples/.* test/flatfile2.txt *.swp - -t.ipynb diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index b429982..ac3944d 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -294,7 +294,7 @@ def set_6d_tracking(accelerator: _Accelerator, rad_full=False): def element_pass( element, particles, - energy, nturn=0, + energy, harmonic_number=1, cavity_on=False, radiation_on='Off', @@ -324,7 +324,7 @@ def element_pass( TrackingError: When there is some problem in tracking; Returns: - part_out (numpy.ndarray): a numpy array with tracked 6D position(s) of + part_out (numpy.ndarray): a numpy array with tracked 6D position(s) of the particle(s). If elementpass is invoked for a single particle then 'part_out' is a simple vector with one index that refers to the particle coordinates. If 'particles' represents many @@ -333,9 +333,6 @@ def element_pass( Raises TrackingException """ - # checks if all necessary arguments have been passed - if not isinstance(nturn, (int, _np.integer)): - raise ValueError("nturn arg must be a integer") # creates accelerator for tracking accelerator = _Accelerator( @@ -352,7 +349,7 @@ def element_pass( # tracks through the list of pos ret = _trackcpp.track_elementpass_wrapper( accelerator.trackcpp_acc, element.trackcpp_e, p_in - , float(nturn)) + ) if ret > 0: raise TrackingError("Problem found during tracking.") @@ -366,7 +363,7 @@ def line_pass( indices=None, element_offset: float = 0, parallel=False -, nturn=0): +): """Track particle(s) along an accelerator. Accepts one or multiple particles initial positions. In the latter case, @@ -462,14 +459,14 @@ def line_pass( if not parallel: p_out, lost_flag, lost_element, lost_plane, lost_pos = _line_pass( - accelerator, p_in, indices, element_offset, nturn) + accelerator, p_in, indices, element_offset) else: slcs = _get_slices_multiprocessing(parallel, p_in.shape[1]) with _multiproc.Pool(processes=len(slcs)) as pool: res = [] for slc in slcs: res.append(pool.apply_async(_line_pass, ( - accelerator, p_in[:, slc], indices, element_offset, nturn, True))) + accelerator, p_in[:, slc], indices, element_offset, True))) lost_pos, p_out = [], [] lost_flag, lost_element, lost_plane = [], [], [] @@ -490,7 +487,7 @@ def line_pass( return p_out, loss_info -def _line_pass(accelerator, p_in, indices, element_offset, nturn=0, set_seed=False): +def _line_pass(accelerator, p_in, indices, element_offset, set_seed=False): # store only final position? args = _trackcpp.LinePassArgs() for idx in indices: From bf5f485bd3834a87b4190d1f0d375d1e144f5356 Mon Sep 17 00:00:00 2001 From: vitor Date: Mon, 9 Sep 2024 10:29:40 -0300 Subject: [PATCH 09/10] remove duplicated info in docstring --- pyaccel/tracking.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index ac3944d..fba2a5b 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -290,6 +290,7 @@ def set_6d_tracking(accelerator: _Accelerator, rad_full=False): accelerator.cavity_on = True accelerator.radiation_on = 'Full' if rad_full else 'Damping' + @_interactive def element_pass( element, @@ -330,10 +331,7 @@ def element_pass( the particle coordinates. If 'particles' represents many particles, the first index of 'part_out' selects the coordinate and the second index selects the particle. - - Raises TrackingException """ - # creates accelerator for tracking accelerator = _Accelerator( energy=energy, From 1c65b1d40a27340ef574da04dd7440fb7105e630 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 9 May 2025 14:38:19 -0300 Subject: [PATCH 10/10] followup-trackcpp --- pyaccel/tracking.py | 49 ++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/pyaccel/tracking.py b/pyaccel/tracking.py index fba2a5b..b1da720 100644 --- a/pyaccel/tracking.py +++ b/pyaccel/tracking.py @@ -359,7 +359,7 @@ def line_pass( accelerator: _Accelerator, particles, indices=None, - element_offset: float = 0, + element_offset: int = 0, parallel=False ): """Track particle(s) along an accelerator. @@ -492,11 +492,6 @@ def _line_pass(accelerator, p_in, indices, element_offset, set_seed=False): args.indices.push_back(int(idx)) args.element_offset = int(element_offset) - args.line_length = accelerator.trackcpp_acc.get_time_aware_elements_info( - args.time_aware_element_indices, - args.time_aware_element_positions - ) - p_in = p_in.copy() n_part = p_in.shape[1] p_out = _np.zeros((6, n_part * len(indices)), dtype=float) @@ -527,7 +522,7 @@ def ring_pass( particles, nr_turns: int = 1, turn_by_turn: bool = False, - element_offset: float = 0, + element_offset: int = 0, parallel=False ): """Track particle(s) along a ring. @@ -933,12 +928,12 @@ def find_m66( else: trackcpp_idx.push_back(len(accelerator)) - timeaware_element_indices = _trackcpp.CppUnsigIntVector() - timeaware_element_positions = _trackcpp.CppDoubleVector() - linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( - timeaware_element_indices, - timeaware_element_positions - ) + # timeaware_element_indices = _trackcpp.CppUnsigIntVector() + # timeaware_element_positions = _trackcpp.CppDoubleVector() + # linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( + # timeaware_element_indices, + # timeaware_element_positions + # ) if fixed_point is None: # Closed orbit is calculated by trackcpp @@ -962,10 +957,10 @@ def find_m66( cumul_trans_matrices, m66, _v0, - trackcpp_idx, - linelength, - timeaware_element_indices, - timeaware_element_positions) + trackcpp_idx) + # linelength, + # timeaware_element_indices, + # timeaware_element_positions) if ret > 0: raise TrackingError(_trackcpp.string_error_messages[ret]) @@ -1035,12 +1030,12 @@ def find_m44( _closed_orbit = _trackcpp.CppDoublePosVector() _closed_orbit.push_back(_fixed_point) - timeaware_element_indices = _trackcpp.CppUnsigIntVector() - timeaware_element_positions = _trackcpp.CppDoubleVector() - linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( - timeaware_element_indices, - timeaware_element_positions - ) + # timeaware_element_indices = _trackcpp.CppUnsigIntVector() + # timeaware_element_positions = _trackcpp.CppDoubleVector() + # linelength = accelerator.trackcpp_acc.get_time_aware_elements_info( + # timeaware_element_indices, + # timeaware_element_positions + # ) cumul_trans_matrices = _np.zeros((trackcpp_idx.size(), 4, 4), dtype=float) m44 = _np.zeros((4, 4), dtype=float) @@ -1051,10 +1046,10 @@ def find_m44( cumul_trans_matrices, m44, _v0, - trackcpp_idx, - linelength, - timeaware_element_indices, - timeaware_element_positions) + trackcpp_idx) + # linelength, + # timeaware_element_indices, + # timeaware_element_positions) if ret > 0: raise TrackingError(_trackcpp.string_error_messages[ret])