|
| 1 | +import communication_helpers as ch |
| 2 | +import numpy as np |
| 3 | +from scipy.constants import c, e |
| 4 | +import share_segments as shs |
| 5 | + |
| 6 | +# compute sigma x and y |
| 7 | +epsn_x = 2.5e-6 |
| 8 | +epsn_y = 2.5e-6 |
| 9 | + |
| 10 | + |
| 11 | +class Simulation(object): |
| 12 | + def __init__(self): |
| 13 | + self.N_turns = None |
| 14 | + |
| 15 | + def init_all(self): |
| 16 | + |
| 17 | + n_slices = 64 |
| 18 | + |
| 19 | + self.n_slices = n_slices |
| 20 | + |
| 21 | + n_segments=3 |
| 22 | + |
| 23 | + from machines_for_testing import SPS |
| 24 | + machine = SPS(n_segments = n_segments, |
| 25 | + machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20., |
| 26 | + RF_at=RF_at='end_of_transverse') |
| 27 | + |
| 28 | + |
| 29 | + # We suppose that all the object that cannot be slice parallelized are at the end of the ring |
| 30 | + i_end_parallel = len(self.machine.one_turn_map)-1 #only RF is not parallelizable |
| 31 | + |
| 32 | + # split the machine |
| 33 | + sharing = shs.ShareSegments(i_end_parallel, self.ring_of_CPUs.N_nodes) |
| 34 | + myid = self.ring_of_CPUs.myid |
| 35 | + i_start_part, i_end_part = sharing.my_part(myid) |
| 36 | + self.mypart = self.machine.one_turn_map[i_start_part:i_end_part] |
| 37 | + if self.ring_of_CPUs.I_am_a_worker: |
| 38 | + print 'I am id=%d (worker) and my part is %d long'%(myid, len(self.mypart)) |
| 39 | + elif self.ring_of_CPUs.I_am_the_master: |
| 40 | + self.non_parallel_part = self.machine.one_turn_map[i_end_parallel:] |
| 41 | + print 'I am id=%d (master) and my part is %d long'%(myid, len(self.mypart)) |
| 42 | + |
| 43 | + |
| 44 | + # config e-cloud |
| 45 | + init_unif_edens_flag=1 |
| 46 | + init_unif_edens=2e11 |
| 47 | + N_MP_ele_init = 100000 |
| 48 | + N_mp_max = N_MP_ele_init*4. |
| 49 | + |
| 50 | + # define apertures and Dh_sc to simulate headtail |
| 51 | + inj_optics = machine.transverse_map.get_injection_optics() |
| 52 | + sigma_x = np.sqrt(inj_optics['beta_x']*epsn_x/machine.betagamma) |
| 53 | + sigma_y = np.sqrt(inj_optics['beta_y']*epsn_y/machine.betagamma) |
| 54 | + x_aper = 20*sigma_x |
| 55 | + y_aper = 20*sigma_y |
| 56 | + Dh_sc = 2*x_aper/128/2 |
| 57 | + |
| 58 | + import PyECLOUD.PyEC4PyHT as PyEC4PyHT |
| 59 | + my_new_part = [] |
| 60 | + self.my_list_eclouds = [] |
| 61 | + for ele in self.mypart: |
| 62 | + my_new_part.append(ele) |
| 63 | + if ele in self.machine.transverse_map: |
| 64 | + ecloud_new = PyEC4PyHT.Ecloud(L_ecloud=machine.circumference/N_kicks, |
| 65 | + slicer=None, |
| 66 | + Dt_ref=25e-12, pyecl_input_folder='./drift_sim', |
| 67 | + x_aper=x_aper, y_aper=y_aper, Dh_sc=Dh_sc, |
| 68 | + init_unif_edens_flag=init_unif_edens_flag, |
| 69 | + init_unif_edens=init_unif_edens, |
| 70 | + N_MP_ele_init=N_MP_ele_init, |
| 71 | + N_mp_max=N_mp_max, |
| 72 | + nel_mp_ref_0=nel_mp_ref_0, |
| 73 | + B_multip=B_multip) |
| 74 | + slice_by_slice_mode=True) |
| 75 | + my_new_part.append(ecloud_new) |
| 76 | + self.my_list_eclouds.append(ecloud_new) |
| 77 | + self.mypart = my_new_part |
| 78 | + |
| 79 | + def init_master(self): |
| 80 | + |
| 81 | + # beam parameters |
| 82 | + sigma_z = 0.2 |
| 83 | + intensity = 1.15e11 |
| 84 | + macroparticlenumber_track = 300000 |
| 85 | + |
| 86 | + # initialization bunch |
| 87 | + bunch = self.machine.generate_6D_Gaussian_bunch( |
| 88 | + macroparticlenumber_track, intensity, epsn_x, epsn_y, sigma_z=sigma_z) |
| 89 | + print 'Bunch initialized.' |
| 90 | + |
| 91 | + #replace particles with HDTL ones |
| 92 | + filename = 'headtail_for_test/test_protons/SPS_Q20_proton_check_dipole_3kicks_20150212_prb.dat' |
| 93 | + n_part_per_turn = 5000 |
| 94 | + appo = np.loadtxt(filename) |
| 95 | + |
| 96 | + parid = np.reshape(appo[:,0], (-1, n_part_per_turn))[::N_kicks,:] |
| 97 | + x = np.reshape(appo[:,1], (-1, n_part_per_turn))[::N_kicks,:] |
| 98 | + xp = np.reshape(appo[:,2], (-1, n_part_per_turn))[::N_kicks,:] |
| 99 | + y = np.reshape(appo[:,3], (-1, n_part_per_turn))[::N_kicks,:] |
| 100 | + yp =np.reshape(appo[:,4], (-1, n_part_per_turn))[::N_kicks,:] |
| 101 | + z = np.reshape(appo[:,5], (-1, n_part_per_turn))[::N_kicks,:] |
| 102 | + zp = np.reshape(appo[:,6], (-1, n_part_per_turn))[::N_kicks,:] |
| 103 | + self.N_turns = len(x[:,0]) |
| 104 | + |
| 105 | + # replace first particles with HEADTAIL ones |
| 106 | + bunch.x[:n_part_per_turn] = x[0,:] |
| 107 | + bunch.xp[:n_part_per_turn] = xp[0,:] |
| 108 | + bunch.y[:n_part_per_turn] = y[0,:] |
| 109 | + bunch.yp[:n_part_per_turn] = yp[0,:] |
| 110 | + bunch.z[:n_part_per_turn] = z[0,:] |
| 111 | + bunch.dp[:n_part_per_turn] =zp[0,:] |
| 112 | + |
| 113 | + # save id and momenta before track |
| 114 | + self.id_before = bunch.id[bunch.id<=n_part_per_turn] |
| 115 | + self.xp_before = bunch.xp[bunch.id<=n_part_per_turn] |
| 116 | + self.yp_before = bunch.yp[bunch.id<=n_part_per_turn] |
| 117 | + |
| 118 | + # initial slicing |
| 119 | + from PyHEADTAIL.particles.slicing import UniformBinSlicer |
| 120 | + self.slicer = UniformBinSlicer(n_slices = self.n_slices, n_sigma_z = 3.) |
| 121 | + |
| 122 | + self.rms_err_x_list = [] |
| 123 | + self.rms_err_y_list = [] |
| 124 | + |
| 125 | + #slice for the first turn |
| 126 | + slice_obj_list = bunch.extract_slices(self.slicer) |
| 127 | + |
| 128 | + pieces_to_be_treated = slice_obj_list |
| 129 | + |
| 130 | + return pieces_to_be_treated |
| 131 | + |
| 132 | + def init_worker(self): |
| 133 | + pass |
| 134 | + |
| 135 | + def treat_piece(self, piece): |
| 136 | + for ele in self.mypart: |
| 137 | + ele.track(piece) |
| 138 | + |
| 139 | + def finalize_turn_on_master(self, pieces_treated): |
| 140 | + |
| 141 | + # re-merge bunch |
| 142 | + bunch = sum(pieces_treated) |
| 143 | + |
| 144 | + #finalize present turn (with non parallel part, e.g. synchrotron motion) |
| 145 | + for ele in self.non_parallel_part: |
| 146 | + ele.track(bunch) |
| 147 | + |
| 148 | + # prepare next turn (re-slice) |
| 149 | + new_pieces_to_be_treated = bunch.extract_slices(self.slicer) |
| 150 | + orders_to_pass = ['reset_clouds'] |
| 151 | + |
| 152 | + return orders_to_pass, new_pieces_to_be_treated |
| 153 | + |
| 154 | + |
| 155 | + def execute_orders_from_master(self, orders_from_master): |
| 156 | + if 'reset_clouds' in orders_from_master: |
| 157 | + for ec in self.my_list_eclouds: ec.finalize_and_reinitialize() |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + def finalize_simulation(self): |
| 162 | + pass |
| 163 | + def piece_to_buffer(self, piece): |
| 164 | + buf = ch.beam_2_buffer(piece) |
| 165 | + return buf |
| 166 | + |
| 167 | + def buffer_to_piece(self, buf): |
| 168 | + piece = ch.buffer_2_beam(buf) |
| 169 | + return piece |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
0 commit comments