Example #1
0
non_linear_long_matching = False


# ~ #Here head is left and tail is right
b_spac_s = 25e-9
filling_pattern = [1., 1., 0., 1., 1.]

# ~ b_spac_s = 5e-9
# ~ filling_pattern = 5*([1.]+4*[0.])

macroparticlenumber = 500000

# Build machine
machine = SPS(n_segments = n_segments, 
            machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20., 
            RF_at='end_of_transverse', longitudinal_mode = 'non-linear')
            
bucket_length_m = machine.circumference/(machine.longitudinal_map.harmonics[0])
b_spac_m =  b_spac_s*machine.beta*clight
b_spac_buckets = np.round(b_spac_m/bucket_length_m)
            
#generate bunch objects
list_bunches = gmb.gen_matched_multibunch_beam(machine, macroparticlenumber, filling_pattern, b_spac_s, bunch_intensity, epsn_x, epsn_y, sigma_z, non_linear_long_matching, min_inten_slice4EC)

beam = sum(list_bunches)

import PyPARIS.slicing_tool as st
import PyPARIS.communication_helpers as ch

# Turn slices into buffer
Example #2
0
import pylab as pl
import mystyle as ms
import time

B_multip = [0.5]
N_kicks = 3
N_turns = 2

pl.close('all')
ms.mystyle(fontsz=14)

# define machine for PyHEADTAIL
from PyHEADTAIL.particles.slicing import UniformBinSlicer
from machines_for_testing import SPS
machine = SPS(n_segments=N_kicks,
              machine_configuration='Q20-injection',
              accQ_x=20.,
              accQ_y=20.)
machine.one_turn_map.remove(machine.longitudinal_map)  # We apply it separately

# compute sigma x and y
epsn_x = 2.5e-6
epsn_y = 2.5e-6

inj_optics = machine.transverse_map.get_injection_optics()
sigma_x = np.sqrt(inj_optics['beta_x'] * epsn_x / machine.betagamma)
sigma_y = np.sqrt(inj_optics['beta_y'] * epsn_y / machine.betagamma)

# define apertures and Dh_sc to simulate headtail conditions
x_aper = 20 * sigma_x
y_aper = 20 * sigma_y
Dh_sc = 2 * x_aper / 128 / 2
from PyHEADTAIL.particles.slicing import UniformBinSlicer

n_segments = 10
N_turns = 1

epsn_x = 2.5e-6
epsn_y = 2.5e-6

init_unif_edens_flag = 1
init_unif_edens = 1e11
N_MP_ele_init = 100000
N_mp_max = N_MP_ele_init * 4.

# define the machine
from machines_for_testing import SPS
machine = SPS(n_segments=n_segments, machine_configuration='Q26-injection')

# compute sigma x and y
inj_optics = machine.transverse_map.get_injection_optics()
sigma_x = np.sqrt(inj_optics['beta_x'] * epsn_x / machine.betagamma)
sigma_y = np.sqrt(inj_optics['beta_y'] * epsn_y / machine.betagamma)

# define apertures and Dh_sc to simulate headtail conditions
x_aper = 20 * sigma_x
y_aper = 20 * sigma_y
Dh_sc = 2 * x_aper / 128

# define MP size
nel_mp_ref_0 = init_unif_edens * 4 * x_aper * y_aper / N_MP_ele_init

# define an electron cloud
x = np.reshape(appo[:,1], (-1, n_part_per_turn))[::N_kicks,:]
xp = np.reshape(appo[:,2], (-1, n_part_per_turn))[::N_kicks,:]
y = np.reshape(appo[:,3], (-1, n_part_per_turn))[::N_kicks,:]
yp =np.reshape(appo[:,4], (-1, n_part_per_turn))[::N_kicks,:]
z = np.reshape(appo[:,5], (-1, n_part_per_turn))[::N_kicks,:]
zp = np.reshape(appo[:,6], (-1, n_part_per_turn))[::N_kicks,:]
N_turns = len(x[:,0])

pl.close('all')
ms.mystyle(fontsz=14)


# define machine for PyHEADTAIL
from PyHEADTAIL.particles.slicing import UniformBinSlicer
from machines_for_testing import SPS
machine = SPS(n_segments = N_kicks, machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20.)
#machine.one_turn_map.remove(machine.longitudinal_map)


# compute sigma x and y
epsn_x = 2.5e-6
epsn_y = 2.5e-6

inj_optics = machine.transverse_map.get_injection_optics()
sigma_x = np.sqrt(inj_optics['beta_x']*epsn_x/machine.betagamma)
sigma_y = np.sqrt(inj_optics['beta_y']*epsn_y/machine.betagamma)

# define apertures and Dh_sc to simulate headtail conditions
x_aper  = 20*sigma_x
y_aper  = 20*sigma_y
Dh_sc = 2*x_aper/128/2
x = np.reshape(appo[:,1], (-1, n_part_per_turn))[::N_kicks,:]
xp = np.reshape(appo[:,2], (-1, n_part_per_turn))[::N_kicks,:]
y = np.reshape(appo[:,3], (-1, n_part_per_turn))[::N_kicks,:]
yp =np.reshape(appo[:,4], (-1, n_part_per_turn))[::N_kicks,:]
z = np.reshape(appo[:,5], (-1, n_part_per_turn))[::N_kicks,:]
zp = np.reshape(appo[:,6], (-1, n_part_per_turn))[::N_kicks,:]
N_turns = len(x[:,0])

pl.close('all')
ms.mystyle(fontsz=14)


# define machine for PyHEADTAIL
from PyHEADTAIL.particles.slicing import UniformBinSlicer
from machines_for_testing import SPS
machine = SPS(n_segments = N_kicks, machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20.)
machine.one_turn_map.remove(machine.longitudinal_map) # We apply it separately


# compute sigma x and y
epsn_x = 2.5e-6
epsn_y = 2.5e-6

inj_optics = machine.transverse_map.get_injection_optics()
sigma_x = np.sqrt(inj_optics['beta_x']*epsn_x/machine.betagamma)
sigma_y = np.sqrt(inj_optics['beta_y']*epsn_y/machine.betagamma)

# define apertures and Dh_sc to simulate headtail conditions
x_aper  = 20*sigma_x
y_aper  = 20*sigma_y
Dh_sc = 2*x_aper/128/2
	def init_all(self):

		
		self.n_slices = n_slices
		self.n_segments = n_segments

		from machines_for_testing import SPS
		self.machine = SPS(n_segments = n_segments, 
			machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20., 
					RF_at='end_of_transverse')

		
		# We suppose that all the object that cannot be slice parallelized are at the end of the ring
		i_end_parallel = len(self.machine.one_turn_map)-1 #only RF is not parallelizable

		# split the machine
		sharing = shs.ShareSegments(i_end_parallel, self.ring_of_CPUs.N_nodes)
		myid = self.ring_of_CPUs.myid
		i_start_part, i_end_part = sharing.my_part(myid)
		self.mypart = self.machine.one_turn_map[i_start_part:i_end_part]
		if self.ring_of_CPUs.I_am_a_worker:
			print 'I am id=%d (worker) and my part is %d long'%(myid, len(self.mypart))
		elif self.ring_of_CPUs.I_am_the_master:
			self.non_parallel_part = self.machine.one_turn_map[i_end_parallel:]
			print 'I am id=%d (master) and my part is %d long'%(myid, len(self.mypart))

	
		# config e-cloud
		init_unif_edens_flag=1
		init_unif_edens=2e11
		N_MP_ele_init = 100000
		N_mp_max = N_MP_ele_init*4.
		
		# define apertures and Dh_sc to simulate headtail 
		inj_optics = self.machine.transverse_map.get_injection_optics()
		sigma_x = np.sqrt(inj_optics['beta_x']*epsn_x/self.machine.betagamma)
		sigma_y = np.sqrt(inj_optics['beta_y']*epsn_y/self.machine.betagamma)
		x_aper  = 20*sigma_x
		y_aper  = 20*sigma_y
		Dh_sc = 2*x_aper/128/2
		
		# initial MP size
		nel_mp_ref_0 = init_unif_edens*4*x_aper*y_aper/N_MP_ele_init

		import PyECLOUD.PyEC4PyHT as PyEC4PyHT
		ecloud = PyEC4PyHT.Ecloud(slice_by_slice_mode=True,
						L_ecloud=self.machine.circumference/n_segments, 
						slicer=None, 
						Dt_ref=25e-12, 
						pyecl_input_folder='../../PyECLOUD/testing/tests_PyEC4PyHT/drift_sim/',
						x_aper=x_aper, y_aper=y_aper, Dh_sc=Dh_sc,
						init_unif_edens_flag=init_unif_edens_flag,
						init_unif_edens=init_unif_edens, 
						N_MP_ele_init=N_MP_ele_init,
						N_mp_max=N_mp_max,
						nel_mp_ref_0=nel_mp_ref_0,
						B_multip=B_multip)
		
		
		my_new_part = []
		self.my_list_eclouds = []
		for ele in self.mypart:
			my_new_part.append(ele)
			if ele in self.machine.transverse_map:
				ecloud_new = ecloud.generate_twin_ecloud_with_shared_space_charge()
				my_new_part.append(ecloud_new)
				self.my_list_eclouds.append(ecloud_new)
		self.mypart = my_new_part
class Simulation(object):
	def __init__(self):
		self.N_turns = N_turns_to_simulate

	def init_all(self):

		
		self.n_slices = n_slices
		self.n_segments = n_segments

		from machines_for_testing import SPS
		self.machine = SPS(n_segments = n_segments, 
			machine_configuration = 'Q20-injection', accQ_x=20., accQ_y=20., 
					RF_at='end_of_transverse')

		
		# We suppose that all the object that cannot be slice parallelized are at the end of the ring
		i_end_parallel = len(self.machine.one_turn_map)-1 #only RF is not parallelizable

		# split the machine
		sharing = shs.ShareSegments(i_end_parallel, self.ring_of_CPUs.N_nodes)
		myid = self.ring_of_CPUs.myid
		i_start_part, i_end_part = sharing.my_part(myid)
		self.mypart = self.machine.one_turn_map[i_start_part:i_end_part]
		if self.ring_of_CPUs.I_am_a_worker:
			print 'I am id=%d (worker) and my part is %d long'%(myid, len(self.mypart))
		elif self.ring_of_CPUs.I_am_the_master:
			self.non_parallel_part = self.machine.one_turn_map[i_end_parallel:]
			print 'I am id=%d (master) and my part is %d long'%(myid, len(self.mypart))

	
		# config e-cloud
		init_unif_edens_flag=1
		init_unif_edens=2e11
		N_MP_ele_init = 100000
		N_mp_max = N_MP_ele_init*4.
		
		# define apertures and Dh_sc to simulate headtail 
		inj_optics = self.machine.transverse_map.get_injection_optics()
		sigma_x = np.sqrt(inj_optics['beta_x']*epsn_x/self.machine.betagamma)
		sigma_y = np.sqrt(inj_optics['beta_y']*epsn_y/self.machine.betagamma)
		x_aper  = 20*sigma_x
		y_aper  = 20*sigma_y
		Dh_sc = 2*x_aper/128/2
		
		# initial MP size
		nel_mp_ref_0 = init_unif_edens*4*x_aper*y_aper/N_MP_ele_init

		import PyECLOUD.PyEC4PyHT as PyEC4PyHT
		ecloud = PyEC4PyHT.Ecloud(slice_by_slice_mode=True,
						L_ecloud=self.machine.circumference/n_segments, 
						slicer=None, 
						Dt_ref=25e-12, 
						pyecl_input_folder='../../PyECLOUD/testing/tests_PyEC4PyHT/drift_sim/',
						x_aper=x_aper, y_aper=y_aper, Dh_sc=Dh_sc,
						init_unif_edens_flag=init_unif_edens_flag,
						init_unif_edens=init_unif_edens, 
						N_MP_ele_init=N_MP_ele_init,
						N_mp_max=N_mp_max,
						nel_mp_ref_0=nel_mp_ref_0,
						B_multip=B_multip)
		
		
		my_new_part = []
		self.my_list_eclouds = []
		for ele in self.mypart:
			my_new_part.append(ele)
			if ele in self.machine.transverse_map:
				ecloud_new = ecloud.generate_twin_ecloud_with_shared_space_charge()
				my_new_part.append(ecloud_new)
				self.my_list_eclouds.append(ecloud_new)
		self.mypart = my_new_part

	def init_master(self):
		
		# beam parameters
		sigma_z = 0.2
		intensity = 1.15e11
		macroparticlenumber_track = 300000

		# initialization bunch
		bunch = self.machine.generate_6D_Gaussian_bunch(
			macroparticlenumber_track, intensity, epsn_x, epsn_y, sigma_z=sigma_z)
		print 'Bunch initialized.'

		#replace particles with HDTL ones
		self.n_part_per_turn = 5000
		appo = np.loadtxt(filename)
		
		parid = np.reshape(appo[:,0], (-1, self.n_part_per_turn))[::self.n_segments,:]
		x = np.reshape(appo[:,1], (-1, self.n_part_per_turn))[::self.n_segments,:]
		xp = np.reshape(appo[:,2], (-1, self.n_part_per_turn))[::self.n_segments,:]
		y = np.reshape(appo[:,3], (-1, self.n_part_per_turn))[::self.n_segments,:]
		yp =np.reshape(appo[:,4], (-1, self.n_part_per_turn))[::self.n_segments,:]
		z = np.reshape(appo[:,5], (-1, self.n_part_per_turn))[::self.n_segments,:]
		zp = np.reshape(appo[:,6], (-1, self.n_part_per_turn))[::self.n_segments,:]

		# replace first particles with HEADTAIL ones
		bunch.x[:self.n_part_per_turn] = x[0,:]
		bunch.xp[:self.n_part_per_turn] = xp[0,:]
		bunch.y[:self.n_part_per_turn] = y[0,:]
		bunch.yp[:self.n_part_per_turn] = yp[0,:]
		bunch.z[:self.n_part_per_turn] = z[0,:]
		bunch.dp[:self.n_part_per_turn] =zp[0,:]

		# save id and momenta before track
		self.id_before = bunch.id[bunch.id<=self.n_part_per_turn]
		self.xp_before = bunch.xp[bunch.id<=self.n_part_per_turn]
		self.yp_before = bunch.yp[bunch.id<=self.n_part_per_turn]

		# initial slicing
		from PyHEADTAIL.particles.slicing import UniformBinSlicer
		self.slicer = UniformBinSlicer(n_slices = self.n_slices, n_sigma_z = 3.)

		self.rms_err_x_list = []
		self.rms_err_y_list = []
		
		#slice for the first turn
		slice_obj_list = bunch.extract_slices(self.slicer)

		pieces_to_be_treated = slice_obj_list
		
		print 'N_turns', self.N_turns

		return pieces_to_be_treated

	def init_worker(self):
		pass

	def treat_piece(self, piece):
		for ele in self.mypart: 
				ele.track(piece)

	def finalize_turn_on_master(self, pieces_treated):
		
		# re-merge bunch
		bunch = sum(pieces_treated)

		#finalize present turn (with non parallel part, e.g. synchrotron motion)
		for ele in self.non_parallel_part:
			ele.track(bunch)

		# id and momenta after track
		id_after = bunch.id[bunch.id<=self.n_part_per_turn]
		xp_after = bunch.xp[bunch.id<=self.n_part_per_turn]
		z_after = bunch.z[bunch.id<=self.n_part_per_turn]
		yp_after = bunch.yp[bunch.id<=self.n_part_per_turn]

		# sort id and momenta after track
		indsort = np.argsort(id_after)
		id_after = np.take(id_after, indsort)
		xp_after = np.take(xp_after, indsort)
		yp_after = np.take(yp_after, indsort)
		z_after = np.take(z_after, indsort)

		# save results
		import myfilemanager as mfm
		mfm.save_dict_to_h5('particles_at_turn_%d.h5'%self.ring_of_CPUs.i_turn,{\
		'id_after': id_after,
		'xp_after': xp_after,
		'yp_after': yp_after,
		'z_after': z_after,
		'id_before':self.id_before,
		'xp_before':self.xp_before,
		'yp_before':self.yp_before})


		# prepare next turn (re-slice)
		new_pieces_to_be_treated = bunch.extract_slices(self.slicer)
		orders_to_pass = ['reset_clouds']

		return orders_to_pass, new_pieces_to_be_treated


	def execute_orders_from_master(self, orders_from_master):
		if 'reset_clouds' in orders_from_master:
			for ec in self.my_list_eclouds: ec.finalize_and_reinitialize()


		
	def finalize_simulation(self):
		pass
	def piece_to_buffer(self, piece):
		buf = ch.beam_2_buffer(piece)
		return buf

	def buffer_to_piece(self, buf):
		piece = ch.buffer_2_beam(buf)
		return piece