コード例 #1
0
ファイル: tracking.py プロジェクト: lnls-fac/pyaccel
def find_m44(accelerator, indices=None, energy_offset = 0.0, closed_orbit=None):
    """Calculate 4D transfer matrices of elements in an accelerator.

    Keyword arguments:
    accelerator
    indices
    energy_offset
    closed_orbit

    Return values:
    m44
    cumul_trans_matrices -- values at the start of each lattice element
    """
    if indices is None:
        indices = list(range(len(accelerator)))

    if closed_orbit is None:
        # calcs closed orbit if it was not passed.
        fixed_point_guess = _trackcpp.CppDoublePos()
        fixed_point_guess.de = energy_offset
        _closed_orbit = _trackcpp.CppDoublePosVector()
        r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit, fixed_point_guess)
        if r > 0:
            raise TrackingException(_trackcpp.string_error_messages[r])
    else:
        _closed_orbit = _4Numpy2CppDoublePosVector(closed_orbit,de=energy_offset)

    _cumul_trans_matrices = _trackcpp.CppMatrixVector()
    _m44 = _trackcpp.Matrix()
    _v0 = _trackcpp.CppDoublePos()
    r = _trackcpp.track_findm66(
        accelerator._accelerator,
        _closed_orbit,
        _cumul_trans_matrices,
        _m44,
        _v0
    )
    if r > 0:
        raise TrackingException(_trackcpp.string_error_messages[r])

    m44 = _CppMatrix24Numpy(_m44)
    if indices == 'm44':
        return m44

    cumul_trans_matrices = []
    for i in range(len(_cumul_trans_matrices)):
        cumul_trans_matrices.append(_CppMatrix24Numpy(_cumul_trans_matrices[i]))

    return m44, cumul_trans_matrices
コード例 #2
0
def find_m44(accelerator, indices=None, energy_offset=0.0, closed_orbit=None):
    """Calculate 4D transfer matrices of elements in an accelerator.

    Keyword arguments:
    accelerator
    indices
    energy_offset
    closed_orbit

    Return values:
    m44
    cumul_trans_matrices -- values at the start of each lattice element
    """
    if indices is None:
        indices = list(range(len(accelerator)))

    if closed_orbit is None:
        # calcs closed orbit if it was not passed.
        fixed_point_guess = _trackcpp.CppDoublePos()
        fixed_point_guess.de = energy_offset
        _closed_orbit = _trackcpp.CppDoublePosVector()
        r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit,
                                       fixed_point_guess)
        if r > 0:
            raise TrackingException(_trackcpp.string_error_messages[r])
    else:
        _closed_orbit = _4Numpy2CppDoublePosVector(closed_orbit,
                                                   de=energy_offset)

    _cumul_trans_matrices = _trackcpp.CppMatrixVector()
    _m44 = _trackcpp.Matrix()
    _v0 = _trackcpp.CppDoublePos()
    r = _trackcpp.track_findm66(accelerator._accelerator, _closed_orbit,
                                _cumul_trans_matrices, _m44, _v0)
    if r > 0:
        raise TrackingException(_trackcpp.string_error_messages[r])

    m44 = _CppMatrix24Numpy(_m44)
    if indices == 'm44':
        return m44

    cumul_trans_matrices = []
    for i in range(len(_cumul_trans_matrices)):
        cumul_trans_matrices.append(_CppMatrix24Numpy(
            _cumul_trans_matrices[i]))

    return m44, cumul_trans_matrices
コード例 #3
0
ファイル: tracking.py プロジェクト: guduan/pyaccel
def findorbit4(accelerator, energy_offset = 0, indices=None, fixed_point_guess=None):
    """Calculate 4D 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
    energy_offset : relative energy deviation from nominal energy
    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 : 4D closed orbit at the entrance of the selected elements as a 2D
        numpy array with the 4 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 = _4Numpy2CppDoublePos(fixed_point_guess)
    fixed_point_guess.de = energy_offset


    _closed_orbit = _trackcpp.CppDoublePosVector()
    r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit, fixed_point_guess)
    if r > 0:
        raise TrackingException(_trackcpp.string_error_messages[r])

    if indices is None:
        closed_orbit = _CppDoublePos24Numpy(_closed_orbit[0])
    elif indices == 'open':
        closed_orbit = _numpy.zeros((len(accelerator),4))
        for i in range(len(accelerator)):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[i])
    elif indices =='closed':
        closed_orbit = _numpy.zeros((len(accelerator)+1,4))
        for i in range(len(accelerator)):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[i])
        closed_orbit[-1] = closed_orbit[0]
    elif isinstance(indices,(list,tuple,_numpy.ndarray)):
        closed_orbit = _numpy.zeros((len(indices),4))
        for i,ind in enumerate(indices):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[ind])
    else:
        raise TrackingException("invalid value for 'indices' in findorbit4")

    return closed_orbit.T
コード例 #4
0
ファイル: optics.py プロジェクト: lnls-fac/pyaccel
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
コード例 #5
0
ファイル: optics.py プロジェクト: vdmv/pyaccel
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
コード例 #6
0
def find_orbit4(accelerator,
                energy_offset=0,
                indices=None,
                fixed_point_guess=None):
    """Calculate 4D 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
    energy_offset : relative energy deviation from nominal energy
    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 : 4D closed orbit at the entrance of the selected elements as a 2D
        numpy array with the 4 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 = _4Numpy2CppDoublePos(fixed_point_guess)
    fixed_point_guess.de = energy_offset

    _closed_orbit = _trackcpp.CppDoublePosVector()
    r = _trackcpp.track_findorbit4(accelerator._accelerator, _closed_orbit,
                                   fixed_point_guess)
    if r > 0:
        raise TrackingException(_trackcpp.string_error_messages[r])

    if indices is None:
        closed_orbit = _CppDoublePos24Numpy(_closed_orbit[0])
    elif indices == 'open':
        closed_orbit = _numpy.zeros((len(accelerator), 4))
        for i in range(len(accelerator)):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[i])
    elif indices == 'closed':
        closed_orbit = _numpy.zeros((len(accelerator) + 1, 4))
        for i in range(len(accelerator)):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[i])
        closed_orbit[-1] = closed_orbit[0]
    elif isinstance(indices, (list, tuple, _numpy.ndarray)):
        closed_orbit = _numpy.zeros((len(indices), 4))
        for i, ind in enumerate(indices):
            closed_orbit[i] = _CppDoublePos24Numpy(_closed_orbit[ind])
    else:
        raise TrackingException("invalid value for 'indices' in findorbit4")

    return closed_orbit.T