def findm66(accelerator, closed_orbit = None): """Calculate accumulatep_in = _trackcpp.DoublePos() p_in.rx,p_in.px,p_in.ry,p_in.py,p_in.dl,p_in.de = pos[:,i] matrices -- array of matrices along accelerator elements Raises TrackingException """ if closed_orbit is None: closed_orbit = _trackcpp.CppDoublePosVector() r = _trackcpp.track_findorbit6(accelerator._accelerator, closed_orbit) if r > 0: raise TrackingException(_trackcpp.string_error_messages[r]) else: if closed_orbit.shape[1] != len(accelerator): closed_orbit = _trackcpp.CppDoublePosVector() m66 = _trackcpp.CppDoubleMatrixVector() r = _trackcpp.track_findm66(accelerator._accelerator, closed_orbit, m66) if r > 0: raise TrackingException(_trackcpp.string_error_messages[r]) m66_out = [] for i in range(len(m66)): m = _numpy.zeros((6,6)) for r in range(6): for c in range(6): m[r,c] = m66[i][r][c] m66_out.append(m) return m66_out
def findorbit6(accelerator, indices=None, fixed_point_guess=None): """Calculate 6D closed orbit of accelerator and return it. Accepts an optional list of indices of ring elements where closed orbit coordinates are to be returned. If this argument is not passed, closed orbit positions are returned at the start of the first element. In addition a guess fixed point at the entrance of the ring may be provided. Keyword arguments: accelerator : Accelerator object indices : may be a (list,tuple, numpy.ndarray) of element indices where closed orbit data is to be returned or a string: 'open' : return the closed orbit at the entrance of all elements. 'closed': besides all the points of 'open' also return the orbit at the exit of the last element. If indices is None the closed orbit is returned only at the entrance of the first element. fixed_point_guess -- A 6D position where to start the search of the closed orbit at the entrance of the first element. If not provided the algorithm will start with zero orbit. Returns: orbit : 6D closed orbit at the entrance of the selected elements as a 2D numpy array with the 6 phase space variables in the first dimension and the indices of the elements in the second dimension. Raises TrackingException """ if fixed_point_guess is None: fixed_point_guess = _trackcpp.CppDoublePos() else: fixed_point_guess = _Numpy2CppDoublePos(fixed_point_guess) _closed_orbit = _trackcpp.CppDoublePosVector() r = _trackcpp.track_findorbit6(accelerator._accelerator, _closed_orbit, fixed_point_guess) if r > 0: raise TrackingException(_trackcpp.string_error_messages[r]) if indices is None: closed_orbit = _CppDoublePos2Numpy(_closed_orbit[0])[None,:] elif indices == 'open': closed_orbit = _numpy.zeros((len(accelerator),6)) for i in range(len(accelerator)): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[i]) elif indices =='closed': closed_orbit = _numpy.zeros((len(accelerator)+1,6)) for i in range(len(accelerator)): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[i]) closed_orbit[-1] = closed_orbit[0] elif isinstance(indices,(list,tuple,_numpy.ndarray)): closed_orbit = _numpy.zeros((len(indices),6)) for i,ind in enumerate(indices): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[ind]) else: raise TrackingException("invalid value for 'indices' in findorbit6") return closed_orbit.T
def find_orbit6(accelerator, indices=None, fixed_point_guess=None): """Calculate 6D closed orbit of accelerator and return it. Accepts an optional list of indices of ring elements where closed orbit coordinates are to be returned. If this argument is not passed, closed orbit positions are returned at the start of the first element. In addition a guess fixed point at the entrance of the ring may be provided. Keyword arguments: accelerator : Accelerator object indices : may be a (list,tuple, numpy.ndarray) of element indices where closed orbit data is to be returned or a string: 'open' : return the closed orbit at the entrance of all elements. 'closed': besides all the points of 'open' also return the orbit at the exit of the last element. If indices is None the closed orbit is returned only at the entrance of the first element. fixed_point_guess -- A 6D position where to start the search of the closed orbit at the entrance of the first element. If not provided the algorithm will start with zero orbit. Returns: orbit : 6D closed orbit at the entrance of the selected elements as a 2D numpy array with the 6 phase space variables in the first dimension and the indices of the elements in the second dimension. Raises TrackingException """ if fixed_point_guess is None: fixed_point_guess = _trackcpp.CppDoublePos() else: fixed_point_guess = _Numpy2CppDoublePos(fixed_point_guess) _closed_orbit = _trackcpp.CppDoublePosVector() r = _trackcpp.track_findorbit6(accelerator._accelerator, _closed_orbit, fixed_point_guess) if r > 0: raise TrackingException(_trackcpp.string_error_messages[r]) if indices is None: closed_orbit = _CppDoublePos2Numpy(_closed_orbit[0])[None, :] elif indices == 'open': closed_orbit = _numpy.zeros((len(accelerator), 6)) for i in range(len(accelerator)): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[i]) elif indices == 'closed': closed_orbit = _numpy.zeros((len(accelerator) + 1, 6)) for i in range(len(accelerator)): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[i]) closed_orbit[-1] = closed_orbit[0] elif isinstance(indices, (list, tuple, _numpy.ndarray)): closed_orbit = _numpy.zeros((len(indices), 6)) for i, ind in enumerate(indices): closed_orbit[i] = _CppDoublePos2Numpy(_closed_orbit[ind]) else: raise TrackingException("invalid value for 'indices' in findorbit6") return closed_orbit.T
def findorbit6(accelerator, indices=None): """Calculate 6D orbit closed-orbit. Accepts an optional list of indices of ring elements where closed-orbit coordinates are to be returned. If this argument is not passed closed-orbit positions are returned at the start of every element. Keyword arguments: accelerator -- Accelerator object Returns: orbit -- 6D position at elements Raises TrackingException """ closed_orbit = _trackcpp.CppDoublePosVector() r = _trackcpp.track_findorbit6(accelerator._accelerator, closed_orbit) if r > 0: raise TrackingException(_trackcpp.string_error_messages[r]) closed_orbit = _CppDoublePosVector2Numpy(closed_orbit, indices) return closed_orbit
def calc_twiss(accelerator=None, init_twiss=None, fixed_point=None, indices = 'open', energy_offset=None): """Return Twiss parameters of uncoupled dynamics. Keyword arguments: accelerator -- Accelerator object init_twiss -- Twiss parameters at the start of first element fixed_point -- 6D position at the start of first element indices -- Open or closed energy_offset -- float denoting the energy deviation (used only for periodic solutions). Returns: tw -- list of Twiss objects (closed orbit data is in the objects vector) m66 -- one-turn transfer matrix """ if indices == 'open': closed_flag = False elif indices == 'closed': closed_flag = True else: raise OpticsException("invalid value for 'indices' in calc_twiss") _m66 = _trackcpp.Matrix() _twiss = _trackcpp.CppTwissVector() if init_twiss is not None: ''' as a transport line: uses init_twiss ''' _init_twiss = init_twiss._t if fixed_point is None: _fixed_point = _init_twiss.co else: raise OpticsException('arguments init_twiss and fixed_point are mutually exclusive') r = _trackcpp.calc_twiss(accelerator._accelerator, _fixed_point, _m66, _twiss, _init_twiss, closed_flag) else: ''' as a periodic system: try to find periodic solution ''' if accelerator.harmonic_number == 0: raise OpticsException('Either harmonic number was not set or calc_twiss was' 'invoked for transport line without initial twiss') if fixed_point is None: _closed_orbit = _trackcpp.CppDoublePosVector() _fixed_point_guess = _trackcpp.CppDoublePos() if energy_offset is not None: _fixed_point_guess.de = energy_offset if not accelerator.cavity_on and not accelerator.radiation_on: r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit, _fixed_point_guess) elif not accelerator.cavity_on and accelerator.radiation_on: raise OpticsException('The radiation is on but the cavity is off') else: r = _trackcpp.track_findorbit6(accelerator._accelerator, _closed_orbit, _fixed_point_guess) if r > 0: raise _tracking.TrackingException(_trackcpp.string_error_messages[r]) _fixed_point = _closed_orbit[0] else: _fixed_point = _tracking._Numpy2CppDoublePos(fixed_point) if energy_offset is not None: _fixed_point.de = energy_offset r = _trackcpp.calc_twiss(accelerator._accelerator, _fixed_point, _m66, _twiss) if r > 0: raise OpticsException(_trackcpp.string_error_messages[r]) twiss = TwissList(_twiss) m66 = _tracking._CppMatrix2Numpy(_m66) return twiss, m66
def calc_twiss(accelerator=None, init_twiss=None, fixed_point=None, indices='open', energy_offset=None): """Return Twiss parameters of uncoupled dynamics. Keyword arguments: accelerator -- Accelerator object init_twiss -- Twiss parameters at the start of first element fixed_point -- 6D position at the start of first element indices -- Open or closed energy_offset -- float denoting the energy deviation (used only for periodic solutions). Returns: tw -- list of Twiss objects (closed orbit data is in the objects vector) m66 -- one-turn transfer matrix """ if indices == 'open': closed_flag = False elif indices == 'closed': closed_flag = True else: raise OpticsException("invalid value for 'indices' in calc_twiss") _m66 = _trackcpp.Matrix() _twiss = _trackcpp.CppTwissVector() if init_twiss is not None: ''' as a transport line: uses init_twiss ''' _init_twiss = init_twiss._t if fixed_point is None: _fixed_point = _init_twiss.co else: raise OpticsException( 'arguments init_twiss and fixed_point are mutually exclusive') r = _trackcpp.calc_twiss(accelerator._accelerator, _fixed_point, _m66, _twiss, _init_twiss, closed_flag) else: ''' as a periodic system: try to find periodic solution ''' if accelerator.harmonic_number == 0: raise OpticsException( 'Either harmonic number was not set or calc_twiss was' 'invoked for transport line without initial twiss') if fixed_point is None: _closed_orbit = _trackcpp.CppDoublePosVector() _fixed_point_guess = _trackcpp.CppDoublePos() if energy_offset is not None: _fixed_point_guess.de = energy_offset if not accelerator.cavity_on and not accelerator.radiation_on: r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit, _fixed_point_guess) elif not accelerator.cavity_on and accelerator.radiation_on: raise OpticsException( 'The radiation is on but the cavity is off') else: r = _trackcpp.track_findorbit6(accelerator._accelerator, _closed_orbit, _fixed_point_guess) if r > 0: raise _tracking.TrackingException( _trackcpp.string_error_messages[r]) _fixed_point = _closed_orbit[0] else: _fixed_point = _tracking._Numpy2CppDoublePos(fixed_point) if energy_offset is not None: _fixed_point.de = energy_offset r = _trackcpp.calc_twiss(accelerator._accelerator, _fixed_point, _m66, _twiss) if r > 0: raise OpticsException(_trackcpp.string_error_messages[r]) twiss = TwissList(_twiss) m66 = _tracking._CppMatrix2Numpy(_m66) return twiss, m66