Пример #1
0
def initialization(myfile):

    # Open files
    print('Importing the files ...')
    data = mfm.h5_to_dict(myfile, group='output')
    in_data = mfm.h5_to_dict(myfile, group='input')
    optics = mfm.h5_to_dict(myfile, group='beam-optics')
    particle_on_CO = mfm.h5_to_dict(myfile, group='closed-orbit')

    # Extract constants
    print('Extracting constants and variables...')
    epsg_x = optics['epsn_x'] / (optics['beta0'] * optics['gamma0'])
    epsg_y = optics['epsn_y'] / (optics['beta0'] * optics['gamma0'])
    invW = optics['invW']

    # Get X
    X = np.array([
        data['x_tbt_first'].T, data['px_tbt_first'].T, data['y_tbt_first'].T,
        data['py_tbt_first'].T, data['zeta_tbt_first'].T,
        data['delta_tbt_first'].T
    ]).T
    part_on_CO = np.array([
        particle_on_CO['x'], particle_on_CO['px'], particle_on_CO['y'],
        particle_on_CO['py'], particle_on_CO['zeta'], particle_on_CO['delta']
    ]).T
    X_init = np.array([
        in_data['init_x'], in_data['init_px'], in_data['init_y'],
        in_data['init_py'], in_data['init_zeta'], in_data['init_delta']
    ]).T
    # Normalize X
    print('Normalizing...')
    X_norm = normalize(X, part_on_CO, invW)
    X_init_norm = normalize(X_init, np.zeros_like(part_on_CO), invW)

    # Get invariants of motion J
    print('Getting invariants of motion J...')
    J1_init = J(X_init_norm[:, 0], X_init_norm[:, 1])
    J2_init = J(X_init_norm[:, 2], X_init_norm[:, 3])

    # Get angle phi
    phi1 = phi(X_init_norm[:, 0], X_init_norm[:, 1])
    phi2 = phi(X_init_norm[:, 2], X_init_norm[:, 3])

    # Define t0
    T = 1  # time 1 turn takes (convert n_turns to t0)
    t0 = T * data['at_turn_last']

    return t0.flatten(), J1_init, J2_init, phi1, phi2, epsg_x, epsg_y
Пример #2
0
def get_dynap(h5_filename, sigmax, sigmay, nturns, i):
    ob = mfm.h5_to_dict(h5_filename)

    #Ax = ob['horizontal_amp_mm']/sigmax
    #Ay = ob['vertical_amp_mm']/sigmay
    last_turn = ob['survived_turns']

    da_index = np.argmax(last_turn < nturns)

    r_max_sigma = 10.
    r_min_sigma = 0.1
    r_step = 0.1
    N_r = int(round((r_max_sigma - r_min_sigma) / r_step)) + 1
    theta_step = (3. / 180.) * np.pi
    theta_min_rad = (i) * theta_step
    theta_max_rad = (i + 1) * theta_step
    N_theta = int(round((theta_max_rad - theta_min_rad) / theta_step))

    xy_norm = footprint.initial_xy_polar(r_min=r_min_sigma,
                                         r_max=r_max_sigma,
                                         r_N=N_r,
                                         theta_min=theta_min_rad,
                                         theta_max=theta_max_rad,
                                         theta_N=N_theta)

    #return Ax[da_index], Ay[da_index]
    r = da_index * r_step + r_min_sigma
    theta = theta_min_rad
    return r * np.cos(theta), r * np.sin(theta)
import sys

sys.path.append('../..')

import myfilemanager_sixtracklib as mfm



n_turns  = 2e7
n_points = 20

t_i      = np.linspace(0, n_turns, n_points + 1) 

input_file = 'losses_sixtracklib_20000_27.h5' 

myinput    = mfm.h5_to_dict(input_file, group = 'input')
optics     = mfm.h5_to_dict(input_file, group = 'optics')
analysis   = mfm.h5_to_dict(input_file, group = 'analysis')

e1              = optics['epsn_x'] / (optics['beta0'] * optics['gamma0'])
e2              = optics['epsn_y'] / (optics['beta0'] * optics['gamma0'])
sigma1, sigma2  = analysis['sigma1'], analysis['sigma2']
t0, J1, J2      = myinput['t0'] + 1, myinput['J1']/e1, myinput['J2']/e2


def K(t0, t1, t2):
    return 1 * np.logical_and(t0 > t1, t0 < t2)
    
def losses(t0, J1, J2, sigma1, sigma2):
    M     = t0.shape[0]    # number of samples          ~ 50,000
    N     = 1              # total number of particles  ~ 10^11
Пример #4
0
def dynamic_aperture_contour(h5_filename):

    ###############
    # input files #
    ###############

    #mydict = mfm.h5_to_dict('dynap_sixtracklib_turns_1000000_delta_0.00009.h5')
    mydict = mfm.h5_to_dict(h5_filename)
    optics = pickle.load(open('optics_mad.pkl', 'rb'))
    co = pickle.load(open('particle_on_CO_mad_line.pkl', 'rb'))

    nturns = 1e6
    delta = mydict['init_delta_wrt_CO']

    ########################
    # non-linear amplitude #
    ########################

    #initialization
    epsn_x = 3.5e-6
    epsn_y = 3.5e-6

    betx = optics['betx']
    bety = optics['bety']
    alfx = optics['alfx']
    alfy = optics['alfy']

    beta0 = optics['beta0']
    gamma0 = optics['gamma0']

    gammax = (1 + alfx**(2.)) / betx
    gammay = (1 + alfy**(2.)) / bety

    # coordinates w.r.t closed orbit co
    x_co = co['x']
    y_co = co['y']
    px_co = co['px']
    py_co = co['py']

    x = mydict['x_tbt_first'] - x_co
    y = mydict['y_tbt_first'] - y_co

    px = mydict['px_tbt_first'] - px_co
    py = mydict['py_tbt_first'] - py_co

    # calculation of amplitudes Ax [sigmax] & Ay [sigmay]

    Jx = 0.5 * (gammax * np.multiply(x, x) + 2 * alfx * np.multiply(x, px) +
                betx * np.multiply(px, px))
    Jy = 0.5 * (gammay * np.multiply(y, y) + 2 * alfy * np.multiply(y, py) +
                bety * np.multiply(py, py))

    Jx_avg = np.mean(Jx[:100], axis=0)
    Jy_avg = np.mean(Jy[:100], axis=0)

    sigmax = np.sqrt((betx * epsn_x) / (beta0 * gamma0))
    sigmay = np.sqrt((bety * epsn_y) / (beta0 * gamma0))

    #Ax = np.sqrt(2*betx*Jx_avg) / sigmax
    #Ay = np.sqrt(2*bety*Jy_avg) / sigmay

    Ax = np.sqrt(2 * betx * Jx[0, :, :]) / sigmax
    Ay = np.sqrt(2 * bety * Jy[0, :, :]) / sigmay

    #mydict['xy_norm'][:,:,0] = Ax[:,:]
    #mydict['xy_norm'][:,:,1] = Ay[:,:]

    ############
    # tracking #
    ############

    # if the particle's last turn < nturns
    # then it has been lost

    #i_max = mydict['xy_norm'].shape[0]
    #j_max = mydict['xy_norm'].shape[1]
    k_max = mydict['at_turn_tbt_last'].shape[0]
    i_max = mydict['at_turn_tbt_last'].shape[1]
    j_max = mydict['at_turn_tbt_last'].shape[2]

    ############# find the boundary of the dynap #############

    index_of_last_turn = np.argmax(mydict['at_turn_tbt_last'], axis=0)
    last_turn = np.max(mydict['at_turn_tbt_last'], axis=0)
    did_not_survive = last_turn < nturns - 1
    boundary = np.argmax(did_not_survive, axis=1)

    x_boundary = Ax[range(0, i_max), boundary]
    y_boundary = Ay[range(0, i_max), boundary]

    return x_boundary, y_boundary
del pic_out
del pic_in
del slices
if do_kicks:
    del ex_slices
    del ey_slices
    del ez_slices

phi_e = np.zeros([n_slices - 4, nx, ny, 8])
if do_kicks:
    ex_e = np.zeros([n_slices - 4, nx, ny, 8])
    ey_e = np.zeros([n_slices - 4, nx, ny, 8])
    ez_e = np.zeros([n_slices - 4, nx, ny, 8])
for i in range(1, n_slices - 3):
    print('Reading Slice: %d' % i)
    ob = mfm_stl.h5_to_dict(temp_folder + '/slice%d.h5' % i)
    phi_e[i - 1, :, :, :] = ob['phi_slice_exact'][:, :, :]
    #ob = mfm.myloadmat_to_obj('temp/slice%d.mat'%i)
    #phi_e[i-1,:,:,:] = ob.phi_slice_exact[:,:,:]
    del ob
    if do_kicks:
        ob_ex = mfm_stl.h5_to_dict(temp_folder + '/ex_slice%d.h5' % i)
        ex_e[i - 1, :, :, :] = ob_ex['ex_slice_exact'][:, :, :]
        del ob_ex
        ob_ey = mfm_stl.h5_to_dict(temp_folder + '/ey_slice%d.h5' % i)
        ey_e[i - 1, :, :, :] = ob_ey['ey_slice_exact'][:, :, :]
        del ob_ey
        ob_ez = mfm_stl.h5_to_dict(temp_folder + '/ez_slice%d.h5' % i)
        ez_e[i - 1, :, :, :] = ob_ez['ez_slice_exact'][:, :, :]
        del ob_ez