Ejemplo n.º 1
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
Ejemplo n.º 2
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.
        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
 def test_linear_map_cleans_slices(self):
     '''Tests whether the slice_sets are deleted when the track() method
     of the LinearMap is called
     '''
     circumference = 1.
     Q_s = 0.012
     linear_map = LinearMap(self.alpha_array, circumference, Q_s,
                            printer=AccumulatorPrinter())
     beam = self.create_all1_bunch()
     sliceset_mock = {'mock': 42}
     beam._slice_sets = sliceset_mock
     linear_map.track(beam)
     self.assertFalse(beam._slice_sets, 'slice_sets not deleted after ' +
                      'calling LinearMap.track() [changing the ' +
                      'z-coordinates of the beam]')
Ejemplo n.º 4
0
 def create_longitudinal_map(self, one_turn_map_insert_idx=0):
     if self.longitudinal_focusing == 'linear':
         self.longitudinal_map = LinearMap(
             [self.alpha],
             self.circumference,
             self.Q_s,
             D_x=self.D_x[one_turn_map_insert_idx],
             D_y=self.D_y[one_turn_map_insert_idx])
     elif self.longitudinal_focusing == 'non-linear':
         self.longitudinal_map = RFSystems(
             self.circumference,
             [self.h1, self.h2],
             [self.V1, self.V2],
             [self.dphi1, self.dphi2],
             [self.alpha],
             self.gamma,
             self.p_increment,
             D_x=self.D_x[one_turn_map_insert_idx],
             D_y=self.D_y[one_turn_map_insert_idx],
             charge=self.charge,
             mass=self.mass,
         )
     else:
         raise NotImplementedError(
             'Something wrong with self.longitudinal_focusing')
Ejemplo n.º 5
0
 def gimme(*detuners):
     trans_map = TransverseMap(s, alpha_x, beta_x, D_x, alpha_y, beta_y,
                               D_y, Q_x, Q_y, *detuners)
     long_map = LinearMap(alpha_0, C, Q_s)
     bunch = generate_bunch(n_macroparticles, alpha_x_inj, alpha_y_inj,
                            beta_x_inj, beta_y_inj, long_map)
     return bunch, trans_map, long_map
    def gimme(D_x=None, D_y=None, *args, **kwargs):
        # Parameters for transverse map.
        s = np.arange(0, n_segments + 1) * C / n_segments

        alpha_x_inj = 0.
        alpha_y_inj = 0.
        beta_x_inj = 66.0064
        beta_y_inj = 71.5376

        alpha_x = alpha_x_inj * np.ones(n_segments)
        beta_x = beta_x_inj * np.ones(n_segments)
        if D_x is None:
            D_x = np.zeros(n_segments)

        alpha_y = alpha_y_inj * np.ones(n_segments)
        beta_y = beta_y_inj * np.ones(n_segments)
        if D_y is None:
            D_y = np.zeros(n_segments)

        if 'detuners' in kwargs:
            trans_map = TransverseMap(
                s, alpha_x, beta_x, D_x, alpha_y, beta_y, D_y, Q_x, Q_y, kwargs['detuners'])
        else:
            trans_map = TransverseMap(
                s, alpha_x, beta_x, D_x, alpha_y, beta_y, D_y, Q_x, Q_y,
                printer=SilentPrinter())

        long_map = LinearMap(alpha_0, C, Q_s)
        bunch = generate_bunch(
            n_macroparticles, alpha_x_inj, alpha_y_inj, beta_x_inj, beta_y_inj,
            long_map)
        return bunch, trans_map, long_map
 def test_linear_map_track(self):
     '''Tests whether only the dp and z coordinates are modified when
     applying the track() method of LinearMap
     '''
     circumference = 1.
     Q_s = 0.012
     linear_map = LinearMap(self.alpha_array, circumference, Q_s)
     beam = self.create_all1_bunch()
     beam2 = self.create_all1_bunch()
     linear_map.track(beam)
     self.assertTrue(np.allclose(beam.x, beam2.x),
                     'x coord of beam has changed in LinearMap.track()')
     self.assertTrue(np.allclose(beam.y, beam2.y),
                     'y coord of beam has changed in LinearMap.track()')
     self.assertTrue(np.allclose(beam.xp, beam2.xp),
                     'xp coord of beam has changed in LinearMap.track()')
     self.assertTrue(np.allclose(beam.yp, beam2.yp),
                     'yp coord of beam has changed in LinearMap.track()')
 def test_linear_map_higher_order_warning(self):
     ''' Tests whether a warning is generated when higher order terms
     are specified for a linear map (alpha_array)
     '''
     alpha_array3 = [1, 2, 3]
     circumference = 1.
     Q_s = 0.011
     warnings = AccumulatorPrinter()
     linear_map = LinearMap(alpha_array3, circumference, Q_s,
                            warningprinter=warnings)
     self.assertTrue(warnings.log, 'No warning generated when specifying' +
                     'higher order terms in LinearMap')
Ejemplo n.º 9
0
    def _construct_longitudinal_map(self, longitudinal_mode=None,
                                    h_RF=None, V_RF=None, dphi_RF=None,
                                    alpha_mom_compaction=None, Q_s=None,
                                    p_increment=None, RF_at=None):

        if longitudinal_mode is None:
            return

        # Provide an RF bucket if it is at hand
        if (
                h_RF is not None and V_RF is not None and
                dphi_RF is not None and alpha_mom_compaction is not None and
                p_increment is not None):

            self.rfbucket = RFBucket(
                charge=self.charge, mass=self.mass,
                circumference=self.circumference, gamma=self.gamma,
                alpha_array=np.atleast_1d(alpha_mom_compaction),
                p_increment=p_increment,
                voltage_list=V_RF, harmonic_list=h_RF, phi_offset_list=dphi_RF)

        if RF_at == 'middle':
            # compute the index of the element before which to insert
            # the longitudinal map
            if longitudinal_mode is not None:
                for insert_before, si in enumerate(self.transverse_map.s):
                    if si > 0.5 * self.circumference:
                        break
            insert_before -= 1
        elif RF_at == 'end_of_transverse':
            insert_before = -1
        else:
            raise ValueError('RF_at=%s not recognized!')

        if longitudinal_mode == 'linear':
            if Q_s is None:
                try:
                    Q_s = self.rfbucket.Q_s
                except AttributeError:
                    raise AttributeError('Q_s not available!')

            self.longitudinal_map = LinearMap(
                np.atleast_1d(alpha_mom_compaction),
                self.circumference, Q_s,
                D_x=self.transverse_map.D_x[insert_before],
                D_y=self.transverse_map.D_y[insert_before])

        elif longitudinal_mode == 'non-linear':
                self.longitudinal_map = RFSystems(
                    charge=self.charge, mass=self.mass,
                    circumference=self.circumference,
                    gamma_reference=self.gamma,
                    voltage_list=np.atleast_1d(V_RF),
                    harmonic_list=np.atleast_1d(h_RF),
                    phi_offset_list=np.atleast_1d(dphi_RF),
                    alpha_array=np.atleast_1d(alpha_mom_compaction),
                    p_increment=p_increment,
                    D_x=self.transverse_map.D_x[insert_before],
                    D_y=self.transverse_map.D_y[insert_before]
                )
        else:
                raise NotImplementedError(
                        'Something wrong with longitudinal_mode')

        if insert_before == -1:
            self.one_turn_map.append(self.longitudinal_map)
        else:
            self.one_turn_map.insert(insert_before, self.longitudinal_map)
Ejemplo n.º 10
0
def run():
    def track_n_save(bunch, map_):
        mean_x = np.empty(n_turns)
        mean_y = np.empty(n_turns)
        sigma_z = np.empty(n_turns)

        for i in xrange(n_turns):
            mean_x[i] = bunch.mean_x()
            mean_y[i] = bunch.mean_y()
            sigma_z[i] = bunch.sigma_z()

            for m_ in map_:
                m_.track(bunch)

        return mean_x, mean_y, sigma_z

    def my_fft(data):
        t = np.arange(len(data))
        fft = np.fft.rfft(data)
        fft_freq = np.fft.rfftfreq(t.shape[-1])

        return fft_freq, np.abs(fft.real)

    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

    def track_n_show(bunch, slicer, map_woWakes, wake_field):
        fig, ((ax1, ax2)) = plt.subplots(2, 1, figsize=(16, 16))

        xp_diff = np.zeros(n_macroparticles)

        for i in xrange(n_turns):
            for m_ in map_woWakes:
                m_.track(bunch)

            # Dipole X kick.
            if i == (n_turns - 1):
                xp_old = bunch.xp.copy()
            wake_field.track(bunch)

            if i == (n_turns - 1):
                xp_diff[:] = bunch.xp[:] - xp_old[:]

        # Plot bunch.z vs. slice index of particle. Mark particles within
        # z cuts in green.
        nsigmaz_lbl = ' (nsigmaz =' + str(n_sigma_z) + ')'

        slice_set = bunch.get_slices(slicer)
        pidx = slice_set.particles_within_cuts
        slidx = slice_set.slice_index_of_particle

        z_cut_tail, z_cut_head = slice_set.z_cut_tail, slice_set.z_cut_head

    # In[4]:
    # Basic parameters.
    n_turns = 10
    n_segments = 1
    n_macroparticles = 50

    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]

    #waketable filename
    fn = os.path.join(os.path.dirname(__file__), 'wake_table.dat')
    # In[5]:

    # Parameters for transverse map.
    s = np.arange(0, n_segments + 1) * C / n_segments

    alpha_x = alpha_x_inj * np.ones(n_segments)
    beta_x = beta_x_inj * np.ones(n_segments)
    D_x = np.zeros(n_segments)

    alpha_y = alpha_y_inj * np.ones(n_segments)
    beta_y = beta_y_inj * np.ones(n_segments)
    D_y = np.zeros(n_segments)

    # In[6]:

    # In[7]:

    # In[8]:

    # CASE TEST SETUP
    trans_map = TransverseMap(s, alpha_x, beta_x, D_x, alpha_y, beta_y, D_y,
                              Q_x, Q_y)
    long_map = LinearMap(alpha_0, C, Q_s)

    bunch = generate_bunch(n_macroparticles, alpha_x_inj, alpha_y_inj,
                           beta_x_inj, beta_y_inj, long_map)

    # In[9]:

    # CASE I
    # Transverse and long. tracking (linear), and wakes from WakeTable source.
    # DIPOLE X, UniformBinSlicer

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    wake_file_columns = [
        'time', 'dipole_x', 'no_dipole_y', 'no_quadrupole_x',
        'no_quadrupole_y', 'no_dipole_xy', 'no_dipole_yx'
    ]
    table = WakeTable(fn,
                      wake_file_columns,
                      printer=SilentPrinter(),
                      warningprinter=SilentPrinter())
    wake_field = WakeField(uniform_bin_slicer, table)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[10]:

    # CASE II
    # Transverse and long. tracking (linear), and wakes from WakeTable source.
    # DIPOLE X, UniformChargeSlicer

    n_sigma_z = 2
    n_slices = 15
    uniform_charge_slicer = UniformChargeSlicer(n_slices=n_slices,
                                                n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    wake_file_columns = [
        'time', 'dipole_x', 'no_dipole_y', 'no_quadrupole_x',
        'no_quadrupole_y', 'no_dipole_xy', 'no_dipole_yx'
    ]
    table = WakeTable(fn,
                      wake_file_columns,
                      printer=SilentPrinter(),
                      warningprinter=SilentPrinter())
    wake_field = WakeField(uniform_charge_slicer, table)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[11]:

    # CASE III
    # Transverse and long. tracking (linear), and wakes from WakeTable source.
    # Quadrupole X, UniformChargeSlicer

    n_sigma_z = 2
    n_slices = 15
    uniform_charge_slicer = UniformChargeSlicer(n_slices=n_slices,
                                                n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    wake_file_columns = [
        'time', 'no_dipole_x', 'no_dipole_y', 'quadrupole_x',
        'no_quadrupole_y', 'no_dipole_xy', 'no_dipole_yx'
    ]
    table = WakeTable(fn,
                      wake_file_columns,
                      printer=SilentPrinter(),
                      warningprinter=SilentPrinter())
    wake_field = WakeField(uniform_charge_slicer, table)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[12]:

    # CASE IV
    # Transverse and long. tracking (linear), and wakes from WakeTable source.
    # Quadrupole X, UniformBinSlicer

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    wake_file_columns = [
        'time', 'no_dipole_x', 'no_dipole_y', 'quadrupole_x',
        'no_quadrupole_y', 'no_dipole_xy', 'no_dipole_yx'
    ]
    table = WakeTable(fn,
                      wake_file_columns,
                      printer=SilentPrinter(),
                      warningprinter=SilentPrinter())
    wake_field = WakeField(uniform_bin_slicer, table)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[15]:

    # CASE V
    # Transverse and long. tracking (linear),
    # Resonator circular

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    reson_circ = CircularResonator(R_shunt=1e6, frequency=1e8, Q=1)
    wake_field = WakeField(uniform_bin_slicer, reson_circ)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[16]:

    # CASE V b.
    # Transverse and long. tracking (linear),
    # Several Resonators circular

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    reson_circ = CircularResonator(R_shunt=1e6, frequency=1e8, Q=1)
    reson_circ2 = CircularResonator(R_shunt=1e6, frequency=1e9, Q=0.8)
    reson_circ3 = CircularResonator(R_shunt=5e6, frequency=1e6, Q=0.2)

    wake_field = WakeField(uniform_bin_slicer, reson_circ, reson_circ2,
                           reson_circ3)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[17]:

    # CASE V c.
    # Transverse and long. tracking (linear),
    # Resonator parallel_plates

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    reson_para = ParallelPlatesResonator(R_shunt=1e6, frequency=1e8, Q=1)
    wake_field = WakeField(uniform_bin_slicer, reson_para)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[18]:

    # CASE V d.
    # Transverse and long. tracking (linear),
    # Resonator w. longitudinal wake

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    reson = Resonator(R_shunt=1e6,
                      frequency=1e8,
                      Q=1,
                      Yokoya_X1=1,
                      Yokoya_X2=1,
                      Yokoya_Y1=1,
                      Yokoya_Y2=1,
                      switch_Z=True)
    wake_field = WakeField(uniform_bin_slicer, reson)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[19]:

    # CASE VI
    # Transverse and long. tracking (linear),
    # ResistiveWall circular

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    resis_circ = CircularResistiveWall(pipe_radius=5e-2,
                                       resistive_wall_length=C,
                                       conductivity=1e6,
                                       dt_min=1e-3)
    wake_field = WakeField(uniform_bin_slicer, resis_circ)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[20]:

    # CASE VI b.
    # Transverse and long. tracking (linear),
    # ResistiveWall parallel_plates

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    resis_para = ParallelPlatesResistiveWall(pipe_radius=5e-2,
                                             resistive_wall_length=C,
                                             conductivity=1e6,
                                             dt_min=1e-3)
    wake_field = WakeField(uniform_bin_slicer, resis_para)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)

    # In[21]:

    # CASE VII.
    # Transverse and long. tracking (linear),
    # Pass mixture of WakeSources to define WakeField.

    n_sigma_z = 2
    n_slices = 15
    uniform_bin_slicer = UniformBinSlicer(n_slices=n_slices,
                                          n_sigma_z=n_sigma_z)

    # Definition of WakeField as a composition of different sources.
    resis_circ = CircularResistiveWall(pipe_radius=5e-2,
                                       resistive_wall_length=C,
                                       conductivity=1e6,
                                       dt_min=1e-3)
    reson_para = ParallelPlatesResonator(R_shunt=1e6, frequency=1e8, Q=1)
    wake_file_columns = [
        'time', 'dipole_x', 'dipole_y', 'quadrupole_x', 'quadrupole_y',
        'dipole_xy', 'dipole_yx'
    ]
    table = WakeTable(fn,
                      wake_file_columns,
                      printer=SilentPrinter(),
                      warningprinter=SilentPrinter())

    wake_field = WakeField(uniform_bin_slicer, resis_circ, reson_para, table)

    trans_map = [m for m in trans_map]
    map_woWakes = trans_map + [long_map]

    track_n_save(bunch, map_woWakes)