def slice_a_bunch(this_bunch, z_cut, n_slices): # Slice bunch if populated if this_bunch.slice_info['slice_4_EC']: bunch_center = this_bunch.slice_info['z_bin_center'] this_slicer = UniformBinSlicer(z_cuts=(bunch_center - z_cut, bunch_center + z_cut), n_slices=n_slices) this_slices = this_bunch.extract_slices(this_slicer, include_non_sliced='always') sliced = this_slices[:-1] unsliced = this_slices[-1] if unsliced.slice_info != 'unsliced': raise ValueError("Something went wrong") for ss in sliced: ss.slice_info['interact_with_EC'] = True # Build head and tail slices mask_head = unsliced.z >= sliced[-1].slice_info['z_bin_right'] mask_tail = unsliced.z <= sliced[0].slice_info['z_bin_left'] slice_tail = Particles(macroparticlenumber=np.sum(mask_tail), particlenumber_per_mp=unsliced.particlenumber_per_mp, charge=unsliced.charge, mass=unsliced.mass, circumference=unsliced.circumference, gamma=unsliced.gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(unsliced.x[mask_tail]), 'xp':np.atleast_1d(unsliced.xp[mask_tail]), 'y':np.atleast_1d(unsliced.y[mask_tail]), 'yp':np.atleast_1d(unsliced.yp[mask_tail]), 'z':np.atleast_1d(unsliced.z[mask_tail]), 'dp':np.atleast_1d(unsliced.dp[mask_tail])}) slice_tail.slice_info = { 'z_bin_center': 0.5 * (this_bunch.slice_info['z_bin_left'] + sliced[0].slice_info['z_bin_left']), 'z_bin_left': this_bunch.slice_info['z_bin_left'], 'z_bin_right': sliced[0].slice_info['z_bin_left'], 'interact_with_EC': False } slice_head = Particles(macroparticlenumber=np.sum(mask_head), particlenumber_per_mp=unsliced.particlenumber_per_mp, charge=unsliced.charge, mass=unsliced.mass, circumference=unsliced.circumference, gamma=unsliced.gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(unsliced.x[mask_head]), 'xp':np.atleast_1d(unsliced.xp[mask_head]), 'y':np.atleast_1d(unsliced.y[mask_head]), 'yp':np.atleast_1d(unsliced.yp[mask_head]), 'z':np.atleast_1d(unsliced.z[mask_head]), 'dp':np.atleast_1d(unsliced.dp[mask_head])}) slice_head.slice_info = { 'z_bin_center': 0.5 * (this_bunch.slice_info['z_bin_right'] + sliced[-1].slice_info['z_bin_right']), 'z_bin_left': sliced[-1].slice_info['z_bin_right'], 'z_bin_right': this_bunch.slice_info['z_bin_right'], 'interact_with_EC': False } list_slices_this_bunch = [slice_tail] + sliced + [slice_head] else: # Build a copy of the bunch copy_this_bunch = Particles( macroparticlenumber=this_bunch.macroparticlenumber, particlenumber_per_mp=this_bunch.particlenumber_per_mp, charge=this_bunch.charge, mass=this_bunch.mass, circumference=this_bunch.circumference, gamma=this_bunch.gamma, coords_n_momenta_dict=this_bunch.get_coords_n_momenta_dict()) copy_this_bunch.slice_info = { kk: this_bunch.slice_info[kk] for kk in list(this_bunch.slice_info.keys()) } list_slices_this_bunch = [copy_this_bunch] for i_sl, ss in enumerate( list_slices_this_bunch[::-1]): # I want slice 0 to be at the head ss.slice_info['info_parent_bunch'] = { kk: this_bunch.slice_info[kk] for kk in list(this_bunch.slice_info.keys()) } ss.slice_info['i_slice'] = i_sl ss.slice_info['N_slices_tot_bunch'] = len(list_slices_this_bunch) return list_slices_this_bunch
def slice_a_bunch(this_bunch, z_cut, n_slices): # Slice bunch if populated if this_bunch.slice_info['slice_4_EC']: bunch_center = this_bunch.slice_info['z_bin_center'] this_slicer = UniformBinSlicer(z_cuts=(bunch_center-z_cut, bunch_center+z_cut), n_slices=n_slices) this_slices = this_bunch.extract_slices(this_slicer, include_non_sliced='always') sliced = this_slices[:-1] unsliced = this_slices[-1] if unsliced.slice_info!='unsliced': raise ValueError("Something went wrong") for ss in sliced: ss.slice_info['interact_with_EC'] = True # Build head and tail slices mask_head = unsliced.z>=sliced[-1].slice_info['z_bin_right'] mask_tail = unsliced.z<=sliced[0].slice_info['z_bin_left'] slice_tail = Particles(macroparticlenumber=np.sum(mask_tail), particlenumber_per_mp=unsliced.particlenumber_per_mp, charge=unsliced.charge, mass=unsliced.mass, circumference=unsliced.circumference, gamma=unsliced.gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(unsliced.x[mask_tail]), 'xp':np.atleast_1d(unsliced.xp[mask_tail]), 'y':np.atleast_1d(unsliced.y[mask_tail]), 'yp':np.atleast_1d(unsliced.yp[mask_tail]), 'z':np.atleast_1d(unsliced.z[mask_tail]), 'dp':np.atleast_1d(unsliced.dp[mask_tail])}) slice_tail.slice_info={ 'z_bin_center': 0.5*(this_bunch.slice_info['z_bin_left']+sliced[0].slice_info['z_bin_left']), 'z_bin_left': this_bunch.slice_info['z_bin_left'], 'z_bin_right': sliced[0].slice_info['z_bin_left'], 'interact_with_EC': False} slice_head = Particles(macroparticlenumber=np.sum(mask_head), particlenumber_per_mp=unsliced.particlenumber_per_mp, charge=unsliced.charge, mass=unsliced.mass, circumference=unsliced.circumference, gamma=unsliced.gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(unsliced.x[mask_head]), 'xp':np.atleast_1d(unsliced.xp[mask_head]), 'y':np.atleast_1d(unsliced.y[mask_head]), 'yp':np.atleast_1d(unsliced.yp[mask_head]), 'z':np.atleast_1d(unsliced.z[mask_head]), 'dp':np.atleast_1d(unsliced.dp[mask_head])}) slice_head.slice_info={ 'z_bin_center': 0.5*(this_bunch.slice_info['z_bin_right']+sliced[-1].slice_info['z_bin_right']), 'z_bin_left': sliced[-1].slice_info['z_bin_right'], 'z_bin_right': this_bunch.slice_info['z_bin_right'], 'interact_with_EC': False} list_slices_this_bunch = [slice_tail] + sliced + [slice_head] else: # Build a copy of the bunch copy_this_bunch = Particles(macroparticlenumber=this_bunch.macroparticlenumber, particlenumber_per_mp=this_bunch.particlenumber_per_mp, charge=this_bunch.charge, mass=this_bunch.mass, circumference=this_bunch.circumference, gamma=this_bunch.gamma, coords_n_momenta_dict=this_bunch.get_coords_n_momenta_dict()) copy_this_bunch.slice_info = {kk:this_bunch.slice_info[kk] for kk in this_bunch.slice_info.keys()} list_slices_this_bunch = [copy_this_bunch] for i_sl, ss in enumerate(list_slices_this_bunch[::-1]): # I want slice 0 to be at the head ss.slice_info['info_parent_bunch'] = {kk: this_bunch.slice_info[kk] for kk in this_bunch.slice_info.keys()} ss.slice_info['i_slice'] = i_sl ss.slice_info['N_slices_tot_bunch'] = len(list_slices_this_bunch) return list_slices_this_bunch
def buffer_2_beam(buf, mode='pickle'): if buf[0]<0: beam=None else: i_buf = 0 macroparticlenumber = int(buf[i_buf]) i_buf += 1 particlenumber_per_mp = buf[i_buf] i_buf += 1 charge = buf[i_buf] i_buf += 1 mass = buf[i_buf] i_buf += 1 circumference = buf[i_buf] i_buf += 1 gamma = buf[i_buf] i_buf += 1 id_ = buf[i_buf:i_buf+macroparticlenumber].astype(np.int32) i_buf += macroparticlenumber x = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber xp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber y = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber yp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber z = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber dp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber slice_info_buf_size = int(buf[i_buf]) i_buf += 1 slice_info_buf = buf[i_buf:i_buf+slice_info_buf_size] i_buf += slice_info_buf_size beam = Particles(macroparticlenumber=macroparticlenumber, particlenumber_per_mp=particlenumber_per_mp, charge=charge, mass=mass, circumference=circumference, gamma=gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(x), 'xp':np.atleast_1d(xp), 'y':np.atleast_1d(y), 'yp':np.atleast_1d(yp), 'z':np.atleast_1d(z), 'dp':np.atleast_1d(dp)}) beam.id = np.atleast_1d(id_) if mode=='json': si_int = slice_info_buf.astype(np.int) si_str = ''.join(map(unichr, list(si_int))) beam.slice_info = json.loads(si_str) elif mode=='pickle': # Get length in bytes llrec = int(slice_info_buf[0]) s1back_padded = np.frombuffer(slice_info_buf[1:].tobytes(), dtype='S1') s1back = s1back_padded[:llrec] pss_rec = s1back.tobytes() beam.slice_info = pickle.loads(pss_rec) else: raise ValueError('Unknown mode!') # if slice_info_buf[0] < 0.: # beam.slice_info = None # elif slice_info_buf[0] == 0.: # beam.slice_info = 'unsliced' # else: # beam.slice_info = {\ # 'z_bin_center':slice_info_buf[1] , # 'z_bin_right':slice_info_buf[2], # 'z_bin_left':slice_info_buf[3]} return beam
def buffer_2_beam(buf, mode='pickle'): if buf[0]<0: beam=None else: i_buf = 0 macroparticlenumber = int(buf[i_buf]) i_buf += 1 particlenumber_per_mp = buf[i_buf] i_buf += 1 charge = buf[i_buf] i_buf += 1 mass = buf[i_buf] i_buf += 1 circumference = buf[i_buf] i_buf += 1 gamma = buf[i_buf] i_buf += 1 id_ = buf[i_buf:i_buf+macroparticlenumber].astype(np.int32) i_buf += macroparticlenumber x = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber xp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber y = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber yp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber z = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber dp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber slice_info_buf_size = int(buf[i_buf]) i_buf += 1 slice_info_buf = buf[i_buf:i_buf+slice_info_buf_size] i_buf += slice_info_buf_size beam = Particles(macroparticlenumber=macroparticlenumber, particlenumber_per_mp=particlenumber_per_mp, charge=charge, mass=mass, circumference=circumference, gamma=gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(x), 'xp':np.atleast_1d(xp), 'y':np.atleast_1d(y), 'yp':np.atleast_1d(yp), 'z':np.atleast_1d(z), 'dp':np.atleast_1d(dp)}) beam.id = np.atleast_1d(id_) if mode=='json': si_int = slice_info_buf.astype(np.int) si_str = ''.join(map(chr, list(si_int))) beam.slice_info = json.loads(si_str) elif mode=='pickle': # Get length in bytes llrec = int(slice_info_buf[0]) s1back_padded = np.frombuffer(slice_info_buf[1:].tobytes(), dtype='S1') s1back = s1back_padded[:llrec] pss_rec = s1back.tobytes() beam.slice_info = pickle.loads(pss_rec) else: raise ValueError('Unknown mode!') # if slice_info_buf[0] < 0.: # beam.slice_info = None # elif slice_info_buf[0] == 0.: # beam.slice_info = 'unsliced' # else: # beam.slice_info = {\ # 'z_bin_center':slice_info_buf[1] , # 'z_bin_right':slice_info_buf[2], # 'z_bin_left':slice_info_buf[3]} return beam
def buffer_2_beam(buf): if buf[0] < 0: beam = None else: i_buf = 0 macroparticlenumber = int(buf[i_buf]) i_buf += 1 particlenumber_per_mp = buf[i_buf] i_buf += 1 charge = buf[i_buf] i_buf += 1 mass = buf[i_buf] i_buf += 1 circumference = buf[i_buf] i_buf += 1 gamma = buf[i_buf] i_buf += 1 id_ = np.int_(buf[i_buf:i_buf + macroparticlenumber]) i_buf += macroparticlenumber x = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber xp = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber y = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber yp = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber z = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber dp = buf[i_buf:i_buf + macroparticlenumber] i_buf += macroparticlenumber slice_info_buf = buf[i_buf:i_buf + 4] i_buf += 4 beam = Particles(macroparticlenumber=macroparticlenumber, particlenumber_per_mp=particlenumber_per_mp, charge=charge, mass=mass, circumference=circumference, gamma=gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(x), 'xp':np.atleast_1d(xp), 'y':np.atleast_1d(y), 'yp':np.atleast_1d(yp), 'z':np.atleast_1d(z), 'dp':np.atleast_1d(dp)}) beam.id = np.atleast_1d(id_) if slice_info_buf[0] < 0.: beam.slice_info = None elif slice_info_buf[0] == 0.: beam.slice_info = 'unsliced' else: beam.slice_info = {\ 'z_bin_center':slice_info_buf[1] , 'z_bin_right':slice_info_buf[2], 'z_bin_left':slice_info_buf[3]} return beam
def buffer_2_beam(buf): if buf[0]<0: beam=None else: i_buf = 0 macroparticlenumber = int(buf[i_buf]) i_buf += 1 particlenumber_per_mp = buf[i_buf] i_buf += 1 charge = buf[i_buf] i_buf += 1 mass = buf[i_buf] i_buf += 1 circumference = buf[i_buf] i_buf += 1 gamma = buf[i_buf] i_buf += 1 id_ = np.int_(buf[i_buf:i_buf+macroparticlenumber]) i_buf += macroparticlenumber x = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber xp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber y = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber yp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber z = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber dp = buf[i_buf:i_buf+macroparticlenumber] i_buf += macroparticlenumber slice_info_buf = buf[i_buf:i_buf+4] i_buf += 4 beam = Particles(macroparticlenumber=macroparticlenumber, particlenumber_per_mp=particlenumber_per_mp, charge=charge, mass=mass, circumference=circumference, gamma=gamma, coords_n_momenta_dict={\ 'x': np.atleast_1d(x), 'xp':np.atleast_1d(xp), 'y':np.atleast_1d(y), 'yp':np.atleast_1d(yp), 'z':np.atleast_1d(z), 'dp':np.atleast_1d(dp)}) beam.id = np.atleast_1d(id_) if slice_info_buf[0] < 0.: beam.slice_info = None elif slice_info_buf[0] == 0.: beam.slice_info = 'unsliced' else: beam.slice_info = {\ 'z_bin_center':slice_info_buf[1] , 'z_bin_right':slice_info_buf[2], 'z_bin_left':slice_info_buf[3]} return beam