Beispiel #1
0
    def generate_bunch(n_macroparticles, alpha_x, alpha_y, beta_x, beta_y,
                       alpha_0, Q_s, R):
        intensity = 1.05e11
        sigma_z = 0.059958
        gamma = 3730.26
        eta = alpha_0 - 1. / gamma**2
        gamma_t = 1. / np.sqrt(alpha_0)
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = eta * R / Q_s

        epsn_x = 3.75e-6  # [m rad]
        epsn_y = 3.75e-6  # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)

        bunch = generators.generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles,
            intensity=intensity,
            charge=e,
            gamma=gamma,
            mass=m_p,
            circumference=C,
            alpha_x=alpha_x,
            beta_x=beta_x,
            epsn_x=epsn_x,
            alpha_y=alpha_y,
            beta_y=beta_y,
            epsn_y=epsn_y,
            beta_z=beta_z,
            epsn_z=epsn_z)
        #print bunch.sigma_z()

        return bunch
Beispiel #2
0
    def generate_bunch(n_macroparticles, alpha_x, alpha_y, beta_x, beta_y,
                       linear_map):

        intensity = 1.05e11
        sigma_z = 0.059958
        gamma = 3730.26
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = (linear_map.eta(dp=0, gamma=gamma) *
                  linear_map.circumference / (2 * np.pi * linear_map.Q_s))

        epsn_x = 3.75e-6  # [m rad]
        epsn_y = 3.75e-6  # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)

        bunch = generators.generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles,
            intensity=intensity,
            charge=e,
            gamma=gamma,
            mass=m_p,
            circumference=C,
            alpha_x=alpha_x,
            beta_x=beta_x,
            epsn_x=epsn_x,
            alpha_y=alpha_y,
            beta_y=beta_y,
            epsn_y=epsn_y,
            beta_z=beta_z,
            epsn_z=epsn_z)
        # print ('bunch sigma_z=' + bunch.sigma_z())

        return bunch
Beispiel #3
0
    def generate_gaussian6dBunch(self,n_macroparticles, alpha_x, alpha_y, beta_x,
                                  beta_y, dispx, dispy,
                                  gamma = 3730.27):
        Q_s = 0.0020443
        C = 26658.883
        alpha_0 = [0.0003225]
        linear_map = LinearMap(alpha_0, C, Q_s)

        intensity = 1.05e11
        sigma_z = 0.0059958
        gamma_t = 1. / np.sqrt(alpha_0)
        p0 = np.sqrt(gamma**2 - 1) * m_p * c
        beta_z = (linear_map.eta(dp=0, gamma=gamma) * linear_map.circumference /
              (2 * np.pi * linear_map.Qs))
        epsn_x = 3.75e-6 # [m rad]
        epsn_y = 3.75e-6 # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)
        bunch = generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles, intensity=intensity, charge=e,
            gamma=gamma, mass=m_p, circumference=C,
            alpha_x=0., beta_x=1., epsn_x=epsn_x,
            alpha_y=0., beta_y=1., epsn_y=epsn_y,
            beta_z=beta_z, epsn_z=epsn_z)
        # Scale to correct beta and alpha
        bunch.x *= np.sqrt(beta_x)
        bunch.xp = -alpha_x/np.sqrt(beta_x) * bunch.x + 1./np.sqrt(beta_x) * bunch.xp
        bunch.y = np.sqrt(beta_y)*bunch.y
        bunch.yp = -alpha_y/np.sqrt(beta_y) * bunch.y + 1./np.sqrt(beta_y) * bunch.yp
        bunch.x += dispx * bunch.dp
        bunch.y += dispy * bunch.dp
        return bunch
Beispiel #4
0
    def generate_gaussian6dBunch(self,n_macroparticles, alpha_x, alpha_y, beta_x,
                                  beta_y, dispx, dispy,
                                  gamma = 3730.27):
        Q_s = 0.0020443
        C = 26658.883
        alpha_0 = [0.0003225]
        linear_map = LinearMap(alpha_0, C, Q_s)

        intensity = 1.05e11
        sigma_z = 0.0059958
        gamma_t = 1. / np.sqrt(alpha_0)
        p0 = np.sqrt(gamma**2 - 1) * m_p * c
        beta_z = (linear_map.eta(dp=0, gamma=gamma) * linear_map.circumference /
              (2 * np.pi * linear_map.Q_s))
        epsn_x = 3.75e-6 # [m rad]
        epsn_y = 3.75e-6 # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)
        bunch = generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles, intensity=intensity, charge=e,
            gamma=gamma, mass=m_p, circumference=C,
            alpha_x=0., beta_x=1., epsn_x=epsn_x,
            alpha_y=0., beta_y=1., epsn_y=epsn_y,
            beta_z=beta_z, epsn_z=epsn_z)
        # Scale to correct beta and alpha
        bunch.x *= np.sqrt(beta_x)
        bunch.xp = -alpha_x/np.sqrt(beta_x) * bunch.x + 1./np.sqrt(beta_x) * bunch.xp
        bunch.y = np.sqrt(beta_y)*bunch.y
        bunch.yp = -alpha_y/np.sqrt(beta_y) * bunch.y + 1./np.sqrt(beta_y) * bunch.yp
        bunch.x += dispx * bunch.dp
        bunch.y += dispy * bunch.dp
        return bunch
 def generate_bunch(n_particles):
     bunch = generate_Gaussian6DTwiss(
         n_particles, intensity, e, m_p, circumference, gamma,
         inj_alpha_x, inj_alpha_y, inj_beta_x, inj_beta_y, beta_z,
         epsn_x, epsn_y, epsn_z
         )
     return bunch
    def create_bunch_with_params(self, alpha_x, beta_x, disp_x, gamma):
        np.random.seed(0)
        beta_y = beta_x
        alpha_y = alpha_x
        disp_y = disp_x
        alpha0 = [0.00308]
        C = 6911.
        Q_s = 0.017
        epsn_x = 3.75e-6
        epsn_y = 3.75e-6
        linear_map = LinearMap(alpha0, Q_s, C)
        # then transform...
        intensity = 1.05e11
        sigma_z = 0.23
        gamma_t = 1. / np.sqrt(linear_map.alpha_array[0])
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = np.abs(
            (linear_map.eta(dp=0, gamma=gamma) * linear_map.circumference /
             (2 * np.pi * linear_map.Q_s)))

        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)
        #print ('epsn_z: ' + str(epsn_z))
        bunch = generate_Gaussian6DTwiss(
            macroparticlenumber=10000,
            intensity=intensity,
            charge=e,
            gamma=gamma,
            mass=m_p,
            circumference=linear_map.circumference,
            alpha_x=0.,
            beta_x=1.,
            epsn_x=epsn_x,
            alpha_y=0.,
            beta_y=1.,
            epsn_y=epsn_y,
            beta_z=beta_z,
            epsn_z=epsn_z)
        # Scale to correct beta and alpha
        xx = bunch.x.copy()
        yy = bunch.y.copy()
        bunch.x *= np.sqrt(beta_x)
        bunch.xp = -alpha_x / np.sqrt(beta_x) * xx + 1. / np.sqrt(
            beta_x) * bunch.xp
        bunch.y *= np.sqrt(beta_y)
        bunch.yp = -alpha_y / np.sqrt(beta_y) * yy + 1. / np.sqrt(
            beta_y) * bunch.yp
        bunch.x += disp_x * bunch.dp
        bunch.y += disp_y * bunch.dp
        return bunch
Beispiel #7
0
    def generate_bunch(n_macroparticles, alpha_x, alpha_y, beta_x, beta_y, alpha_0, Q_s, R):

        intensity = 1.05e11
        sigma_z = 0.059958
        gamma = 3730.26
        eta = alpha_0 - 1. / gamma**2
        gamma_t = 1. / np.sqrt(alpha_0)
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = eta * R / Q_s

        epsn_x = 3.75e-6 # [m rad]
        epsn_y = 3.75e-6 # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e) # WITH OR WITHOUT 4 PIjQuery202047649151738733053_1414145430832?

        bunch = generators.generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles, intensity=intensity, charge=e,
            gamma=gamma, mass=m_p, circumference=C,
            alpha_x=alpha_x, beta_x=beta_x, epsn_x=epsn_x,
            alpha_y=alpha_y, beta_y=beta_y, epsn_y=epsn_y,
            beta_z=beta_z, epsn_z=epsn_z)
        return bunch
Beispiel #8
0
    def create_bunch_with_params(self,alpha_x, beta_x, disp_x, gamma):
        np.random.seed(0)
        beta_y = beta_x
        alpha_y = alpha_x
        disp_y = disp_x
        alpha0= [0.00308]
        C = 6911.
        Qs = 0.017
        epsn_x = 3.75e-6
        epsn_y = 3.75e-6
        linear_map = LinearMap(alpha0, Qs, C)
       # then transform...
        intensity = 1.05e11
        sigma_z = 0.23
        gamma_t = 1. / np.sqrt(linear_map.alpha_array[0])
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = np.abs((linear_map.eta(dp=0, gamma=gamma) * linear_map.circumference /
                  (2 * np.pi * linear_map.Qs)))

        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)
        #print ('epsn_z: ' + str(epsn_z))
        bunch = generate_Gaussian6DTwiss(
            macroparticlenumber=10000, intensity=intensity, charge=e,
            gamma=gamma, mass=m_p, circumference=linear_map.circumference,
            alpha_x=0., beta_x=1., epsn_x=epsn_x,
            alpha_y=0., beta_y=1., epsn_y=epsn_y,
            beta_z=beta_z, epsn_z=epsn_z)
        # Scale to correct beta and alpha
        xx = bunch.x.copy()
        yy = bunch.y.copy()
        bunch.x *= np.sqrt(beta_x)
        bunch.xp = -alpha_x/np.sqrt(beta_x) * xx + 1./np.sqrt(beta_x) * bunch.xp
        bunch.y *= np.sqrt(beta_y)
        bunch.yp = -alpha_y/np.sqrt(beta_y) * yy + 1./np.sqrt(beta_y) * bunch.yp
        bunch.x += disp_x * bunch.dp
        bunch.y += disp_y * bunch.dp
        return bunch
    def generate_bunch(n_macroparticles, alpha_x, alpha_y, beta_x, beta_y, linear_map):

        intensity = 1.05e11
        sigma_z = 0.059958
        gamma = 3730.26
        gamma_t = 1. / np.sqrt(alpha_0)
        p0 = np.sqrt(gamma**2 - 1) * m_p * c

        beta_z = (linear_map.eta(dp=0, gamma=gamma) * linear_map.circumference /
                  (2 * np.pi * linear_map.Qs))

        epsn_x = 3.75e-6 # [m rad]
        epsn_y = 3.75e-6 # [m rad]
        epsn_z = 4 * np.pi * sigma_z**2 * p0 / (beta_z * e)

        bunch = generators.generate_Gaussian6DTwiss(
            macroparticlenumber=n_macroparticles, intensity=intensity, charge=e,
            gamma=gamma, mass=m_p, circumference=C,
            alpha_x=alpha_x, beta_x=beta_x, epsn_x=epsn_x,
            alpha_y=alpha_y, beta_y=beta_y, epsn_y=epsn_y,
            beta_z=beta_z, epsn_z=epsn_z)

        return bunch
eta       = alpha-1/gamma**2
beta_z    = np.abs(eta)*R/Q_s
epsn_x    = 2e-6
epsn_y    = 2e-6
epsn_z    = 2.5
sigma_z   = 0.155 #2*0.0755

sigma_x   = np.sqrt(epsn_x/(beta*gamma) * beta_x[0])
sigma_xp  = sigma_x/beta_x[0]
sigma_y   = np.sqrt(epsn_y/(beta*gamma) * beta_y[0])
sigma_yp  = sigma_y/beta_y[0]
sigma_dp  = sigma_z/beta_z
epsn_z    = 4*np.pi * p0/e * sigma_z*sigma_dp

bunch     = generate_Gaussian6DTwiss(
    macroparticlenumber, intensity, charge, mass, circumference, gamma,
    alpha_x, alpha_y, beta_x, beta_y, beta_z, epsn_x, epsn_y, epsn_z)
xoffset = 0e-4
yoffset = 0e-4
bunch.x += xoffset
bunch.y += yoffset


afile = open('bunch', 'wb')
pickle.dump(bunch, afile)
afile.close()

# ===================================
# CREATE TRANVERSE AND LONGITUDINAL MAPS
# ==================================
scale_factor = 2*bunch.p0 # for detuning coefficients
    def prepareDis(self, twiss, closed_orbit):

        if closed_orbit is not None:
            x_co = twiss[0]['x']
            y_co = twiss[0]['y']
        else:
            x_co = 0
            y_co = 0

        np.random.seed(0)
        D_x_0 = twiss[0]['dx'] * self.beta
        D_y_0 = twiss[0]['dy'] * self.beta

        Dp_x_0 = twiss[0]['dpx'] * self.beta
        Dp_y_0 = twiss[0]['dpy'] * self.beta

        bx_0 = twiss[0]['betx']
        by_0 = twiss[0]['bety']

        s0 = twiss[-1]['s']
        circumference = s0

        alfx_0 = twiss[0]['alfx']
        alfy_0 = twiss[0]['alfy']

        pyht_beam = generators.generate_Gaussian6DTwiss(
            self.npart,
            1,
            self.charge,
            self.mass,
            s0,
            self.gamma,
            alfx_0,
            alfy_0,
            bx_0,
            by_0,
            1,
            self.epsn_x,
            self.epsn_y,
            1,
            dispersion_x=None,
            dispersion_y=None,
            limit_n_rms_x=self.limit_n_rms_x**2,
            limit_n_rms_y=self.limit_n_rms_y**2,
            limit_n_rms_z=self.limit_n_rms_z**2,
        )

        distribution_z_uncut = generators.gaussian2D(self.sig_z**2)
        is_accepted = generators.make_is_accepted_within_n_sigma(
            epsn_rms=self.sig_z,
            limit_n_rms=2.5,
        )
        distribution_z_cut = generators.cut_distribution(
            distribution_z_uncut, is_accepted)

        z, dp = distribution_z_cut(self.npart)
        pyht_beam.z, pyht_beam.dp = z, dp / self.beta_z

        # recentre on 0 to avoid dipolar motion:
        pyht_beam.x -= pyht_beam.mean_x()
        pyht_beam.xp -= pyht_beam.mean_xp()
        pyht_beam.y -= pyht_beam.mean_y()
        pyht_beam.yp -= pyht_beam.mean_yp()
        pyht_beam.z -= pyht_beam.mean_z()
        pyht_beam.dp -= pyht_beam.mean_dp()

        # PyHT generates around 0, need to offset with closed orbit:
        pyht_beam.x += x_co
        pyht_beam.y += y_co
        # add dispersive contribution to coordinates:
        pyht_beam.x += D_x_0 * pyht_beam.dp
        pyht_beam.y += D_y_0 * pyht_beam.dp
        # also need to add D'_{x,y} to momenta:
        pyht_beam.xp += Dp_x_0 * pyht_beam.dp
        pyht_beam.yp += Dp_y_0 * pyht_beam.dp

        return pyht_beam
Beispiel #12
0
def run():
    #HELPERS
    def test_particle_indices_of_slice(bunch, slice_set):
        '''Get particle_indices_of_slice for specific slice index. Apply
        'inverse function' slice_index_of_particle to get back slice_index
        if everything works correctly.
        '''
        all_pass = True
        for i in xrange(slice_set.n_slices):
            pix_slice = slice_set.particle_indices_of_slice(i)
            six_pix = slice_set.slice_index_of_particle[pix_slice]
            if (six_pix != i).any():
                all_pass = False
        if not all_pass:
            print(
                '  Particle_indices_of_slice and slice_index_of_particle FAILED'
            )

    def slice_check_statistics(slice_set):
        '''Test if statistics functions are executable. No value
        checking
        '''
        slice_set.mean_x
        slice_set.sigma_x
        slice_set.epsn_x
        slice_set.mean_y
        slice_set.sigma_y
        slice_set.epsn_y
        slice_set.mean_z
        slice_set.sigma_z
        slice_set.epsn_z
        slice_set.mean_xp
        slice_set.mean_yp
        slice_set.mean_dp
        slice_set.sigma_dp

    def call_slice_set_attributes(bunch, slice_set, line_density_testing=True):
        # Call all the properties / attributes / methods.
        slice_set.z_cut_head
        slice_set.z_cut_tail
        slice_set.z_centers
        slice_set.n_slices
        slice_set.slice_widths
        slice_set.slice_positions
        slice_set.n_macroparticles_per_slice
        slice_set.particles_within_cuts
        slice_set.particle_indices_by_slice

        # if line_density_testing:
        #     slice_set.line_density_derivative_gauss()
        #     slice_set.line_density_derivative()

    def call_slicer_attributes():
        pass

    def clean_bunch(bunch):
        bunch.clean_slices()


# In[4]:
# Basic parameters.

    n_macroparticles = 500

    Q_x = 64.28
    Q_y = 59.31
    Q_s = 0.0020443

    C = 26658.883
    R = C / (2. * np.pi)

    alpha_x_inj = 0.
    alpha_y_inj = 0.
    beta_x_inj = 66.0064
    beta_y_inj = 71.5376
    alpha_0 = [0.0003225]

    # In[5]:

    # general simulation parameters
    n_particles = 10000

    # machine parameters
    circumference = 157.
    inj_alpha_x = 0
    inj_alpha_y = 0
    inj_beta_x = 5.9  # in [m]
    inj_beta_y = 5.7  # in [m]
    Qx = 5.1
    Qy = 6.1
    gamma_tr = 4.05
    alpha_c_array = [gamma_tr**-2]
    V_rf = 8e3  # in [V]
    harmonic = 1
    phi_offset = 0  # measured from aligned focussing phase (0 or pi)
    pipe_radius = 5e-2

    # beam parameters
    Ekin = 1.4e9  # in [eV]
    intensity = 1.684e12
    epsn_x = 2.5e-6  # in [m*rad]
    epsn_y = 2.5e-6  # in [m*rad]
    epsn_z = 1.2  # 4pi*sig_z*sig_dp (*p0/e) in [eVs]

    # calculations
    gamma = 1 + ee * Ekin / (m_p * c**2)
    beta = np.sqrt(1 - gamma**-2)
    eta = alpha_c_array[0] - gamma**-2
    if eta < 0:
        phi_offset = np.pi - phi_offset
    Etot = gamma * m_p * c**2 / ee
    p0 = np.sqrt(gamma**2 - 1) * m_p * c
    Q_s = np.sqrt(np.abs(eta) * V_rf / (2 * np.pi * beta**2 * Etot))
    beta_z = np.abs(eta) * circumference / (2 * np.pi * Q_s)
    turn_period = circumference / (beta * c)

    bunch = generators.generate_Gaussian6DTwiss(  # implicitly tests Gaussian and Gaussian2DTwiss as well
        n_particles,
        intensity,
        ee,
        m_p,
        circumference,
        gamma=gamma,
        alpha_x=inj_alpha_x,
        beta_x=inj_beta_x,
        epsn_x=epsn_x,
        alpha_y=inj_alpha_y,
        beta_y=inj_beta_y,
        epsn_y=epsn_y,
        beta_z=beta_z,
        epsn_z=epsn_z)

    # In[6]:

    # Uniform bin slicer
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    # Request slice_set from bunch with the uniform_bin_slicer config.
    bunch._slice_sets
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer,
                                             statistics=True)
    bunch._slice_sets

    uniform_bin_slicer.config
    call_slice_set_attributes(bunch, uniform_bin_slice_set)
    #call_slicer_attributes(uniform_bin_slice_set)

    # Let bunch remove the slice_set.
    bunch.clean_slices()
    bunch._slice_sets

    # In[7]:

    # Uniform charge slicer
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)
    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer,
                                                statistics=True)
    uniform_charge_slice_set.mode
    uniform_charge_slicer.config
    call_slice_set_attributes(bunch,
                              uniform_charge_slice_set,
                              line_density_testing=False)

    try:
        call_slice_set_attributes(bunch,
                                  uniform_charge_slice_set,
                                  line_density_testing=True)
    except ModeIsNotUniformBin as e:
        pass

    # In[8]:

    # Other cases. When are slicers equal?
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)

    # In[9]:

    # Other cases. When are slicers equal?
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)
    uniform_bin_slicer_2 = UniformBinSlicer(n_slices, n_sigma_z)

    # In[10]:

    # Does bunch slice_set management work?
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer)
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer)

    bunch.clean_slices()

    # In[11]:

    # Old method update_slices should give RuntimeError.
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)

    # In[12]:

    # beam parameters attached to SliceSet?
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    slicer = UniformBinSlicer(n_slices, n_sigma_z)
    slices = bunch.get_slices(slicer)

    beam_parameters = slicer.extract_beam_parameters(bunch)

    for p_name, p_value in beam_parameters.iteritems():
        pass

    # In[14]:

    # CASE I
    # UniformBinSlicer, no longitudinal cut.
    n_slices = 10
    n_sigma_z = None
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets

    # In[15]:

    # CASE II
    # UniformBinSlicer, n_sigma_z = 1
    n_slices = 10
    n_sigma_z = 1
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets

    # In[16]:

    # CASE II b.
    # UniformBinSlicer, set z_cuts
    n_slices = 10
    z_cuts = (-0.05, 0.15)
    uniform_bin_slicer = UniformBinSlicer(n_slices, z_cuts=z_cuts)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets

    # In[18]:

    # CASE III
    # UniformChargeSlicer, no longitudinal cut.
    n_slices = 10
    n_sigma_z = None
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_charge_slicer config.
    bunch.get_slices(uniform_charge_slicer)
    bunch._slice_sets
Beispiel #13
0
from scipy.constants import e
import numpy as np
import pickle
from PyHEADTAIL.particles.generators import generate_Gaussian6DTwiss

sigma_x = np.sqrt(pp.epsn_x / (pp.beta * pp.gamma) * pp.beta_x[0])
sigma_xp = sigma_x / pp.beta_x[0]
sigma_y = np.sqrt(pp.epsn_y / (pp.beta * pp.gamma) * pp.beta_y[0])
sigma_yp = sigma_y / pp.beta_y[0]
sigma_dp = pp.sigma_z / pp.beta_z
epsn_z = 4 * np.pi * pp.p0 / e * pp.sigma_z * sigma_dp
epsn_z = 2.5

bunch = generate_Gaussian6DTwiss(pp.macroparticlenumber, pp.intensity,
                                 pp.charge, pp.mass, pp.circumference,
                                 pp.gamma, pp.alpha_x[0], pp.alpha_y[0],
                                 pp.beta_x[0], pp.beta_y[0], pp.beta_z,
                                 pp.epsn_x, pp.epsn_y, epsn_z)

bunch.x += pp.xoffset
bunch.y += pp.yoffset

print('rms x = {} m'.format(np.std(bunch.x)))
print('rms y = {} m'.format(np.std(bunch.y)))
print('rms z = {} m'.format(np.std(bunch.z)))
print('rms delta = {} m'.format(np.std(bunch.dp)))

afile = open('input/bunch', 'wb')
pickle.dump(bunch, afile)
afile.close()
Beispiel #14
0
def run():
    #HELPERS
    def test_particle_indices_of_slice(bunch, slice_set):
        '''Get particle_indices_of_slice for specific slice index. Apply
        'inverse function' slice_index_of_particle to get back slice_index
        if everything works correctly.
        '''
        all_pass = True
        for i in xrange(slice_set.n_slices):
            pix_slice = slice_set.particle_indices_of_slice(i)
            six_pix = slice_set.slice_index_of_particle[pix_slice]
            if (six_pix != i).any():
                all_pass = False
        if not all_pass:
            print ('  Particle_indices_of_slice and slice_index_of_particle FAILED')

    def slice_check_statistics(slice_set):
        '''Test if statistics functions are executable. No value
        checking
        '''
        slice_set.mean_x
        slice_set.sigma_x
        slice_set.epsn_x
        slice_set.mean_y
        slice_set.sigma_y
        slice_set.epsn_y
        slice_set.mean_z
        slice_set.sigma_z
        slice_set.epsn_z
        slice_set.mean_xp
        slice_set.mean_yp
        slice_set.mean_dp
        slice_set.sigma_dp


    def call_slice_set_attributes(bunch, slice_set, line_density_testing=True):
        # Call all the properties / attributes / methods.
        slice_set.z_cut_head
        slice_set.z_cut_tail
        slice_set.z_centers
        slice_set.n_slices
        slice_set.slice_widths
        slice_set.slice_positions
        slice_set.n_macroparticles_per_slice
        slice_set.particles_within_cuts
        slice_set.particle_indices_by_slice


        # if line_density_testing:
        #     slice_set.line_density_derivative_gauss()
        #     slice_set.line_density_derivative()


    def call_slicer_attributes():
        pass

    def clean_bunch(bunch):
        bunch.clean_slices()


# In[4]:
    # Basic parameters.
    n_macroparticles = 500

    Q_x = 64.28
    Q_y = 59.31
    Q_s = 0.0020443

    C = 26658.883
    R = C / (2.*np.pi)

    alpha_x_inj = 0.
    alpha_y_inj = 0.
    beta_x_inj = 66.0064
    beta_y_inj = 71.5376
    alpha_0 = [0.0003225]


    # In[5]:

    # general simulation parameters
    n_particles = 10000

    # machine parameters
    circumference = 157.
    inj_alpha_x = 0
    inj_alpha_y = 0
    inj_beta_x = 5.9 # in [m]
    inj_beta_y = 5.7 # in [m]
    Qx = 5.1
    Qy = 6.1
    gamma_tr = 4.05
    alpha_c_array = [gamma_tr**-2]
    V_rf = 8e3 # in [V]
    harmonic = 1
    phi_offset = 0 # measured from aligned focussing phase (0 or pi)
    pipe_radius = 5e-2

    # beam parameters
    Ekin = 1.4e9 # in [eV]
    intensity = 1.684e12
    epsn_x = 2.5e-6 # in [m*rad]
    epsn_y = 2.5e-6 # in [m*rad]
    epsn_z = 1.2 # 4pi*sig_z*sig_dp (*p0/e) in [eVs]

    # calculations
    gamma = 1 + ee * Ekin / (m_p * c**2)
    beta = np.sqrt(1 - gamma**-2)
    eta = alpha_c_array[0] - gamma**-2
    if eta < 0:
        phi_offset = np.pi - phi_offset
    Etot = gamma * m_p * c**2 / ee
    p0 = np.sqrt(gamma**2 - 1) * m_p * c
    Qs = np.sqrt(np.abs(eta) * V_rf / (2 * np.pi * beta**2 * Etot))
    beta_z = np.abs(eta) * circumference / (2 * np.pi * Qs)
    turn_period = circumference / (beta * c)

    bunch = generators.generate_Gaussian6DTwiss( # implicitly tests Gaussian and Gaussian2DTwiss as well
        n_particles, intensity, ee, m_p, circumference, gamma=gamma,
        alpha_x=inj_alpha_x, beta_x=inj_beta_x, epsn_x=epsn_x,
        alpha_y=inj_alpha_y, beta_y=inj_beta_y, epsn_y=epsn_y,
        beta_z=beta_z, epsn_z=epsn_z
        )



    # In[6]:

    # Uniform bin slicer
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    # Request slice_set from bunch with the uniform_bin_slicer config.
    bunch._slice_sets
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer, statistics=True)
    bunch._slice_sets

    uniform_bin_slicer.config
    call_slice_set_attributes(bunch, uniform_bin_slice_set)
    #call_slicer_attributes(uniform_bin_slice_set)

    # Let bunch remove the slice_set.
    bunch.clean_slices()
    bunch._slice_sets


    # In[7]:

    # Uniform charge slicer
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)
    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer, statistics=True)
    uniform_charge_slice_set.mode
    uniform_charge_slicer.config
    call_slice_set_attributes(bunch, uniform_charge_slice_set, line_density_testing=False)

    try:
        call_slice_set_attributes(bunch, uniform_charge_slice_set, line_density_testing=True)
    except ModeIsNotUniformBin as e:
        pass



    # In[8]:

    # Other cases. When are slicers equal?
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)


    # In[9]:

    # Other cases. When are slicers equal?
    n_slices = 10
    n_sigma_z = 2
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)
    uniform_bin_slicer_2 = UniformBinSlicer(n_slices, n_sigma_z)


    # In[10]:

    # Does bunch slice_set management work?
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer)
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    uniform_charge_slice_set = bunch.get_slices(uniform_charge_slicer)

    bunch.clean_slices()


    # In[11]:

    # Old method update_slices should give RuntimeError.
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)


    # In[12]:

    # beam parameters attached to SliceSet?
    n_slices = 10
    n_sigma_z = 2

    clean_bunch(bunch)

    slicer = UniformBinSlicer(n_slices, n_sigma_z)
    slices = bunch.get_slices(slicer)

    beam_parameters = slicer.extract_beam_parameters(bunch)

    for p_name, p_value in beam_parameters.iteritems():
        pass

    # In[14]:

    # CASE I
    # UniformBinSlicer, no longitudinal cut.
    n_slices = 10
    n_sigma_z = None
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets



    # In[15]:

    # CASE II
    # UniformBinSlicer, n_sigma_z = 1
    n_slices = 10
    n_sigma_z = 1
    uniform_bin_slicer = UniformBinSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets



    # In[16]:

    # CASE II b.
    # UniformBinSlicer, set z_cuts
    n_slices = 10
    z_cuts = (-0.05, 0.15)
    uniform_bin_slicer = UniformBinSlicer(n_slices, z_cuts=z_cuts)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_bin_slicer config.
    uniform_bin_slice_set = bunch.get_slices(uniform_bin_slicer)
    bunch._slice_sets




    # In[18]:

    # CASE III
    # UniformChargeSlicer, no longitudinal cut.
    n_slices = 10
    n_sigma_z = None
    uniform_charge_slicer = UniformChargeSlicer(n_slices, n_sigma_z)

    clean_bunch(bunch)

    bunch._slice_sets

    # Request slice_set from bunch with the uniform_charge_slicer config.
    bunch.get_slices(uniform_charge_slicer)
    bunch._slice_sets
 def generate_bunch(n_particles):
     bunch = generate_Gaussian6DTwiss(n_particles, intensity, e, m_p,
                                      circumference, gamma, inj_alpha_x,
                                      inj_alpha_y, inj_beta_x, inj_beta_y,
                                      beta_z, epsn_x, epsn_y, epsn_z)
     return bunch