eKin = bunch.getSyncParticle().kinEnergy()*1.0e+3 s = " %5d %35s %4.5f %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f %7.5f %10.6f %8d "%(paramsDict["count"],node.getName(),pos,x_rms,y_rms,z_rms,z_rms_deg,xp_rms,yp_rms,dE_rms,eKin,bunch.getSize()) file_out.write(s +"\n") print s #actionContainer.addAction(action_entrance, AccActionsContainer.ENTRANCE) actionContainer.addAction(action_exit, AccActionsContainer.EXIT) time_start = time.clock() accLattice.trackBunch(bunch_in, paramsDict = paramsDict, actionContainer = actionContainer) time_exec = time.clock() - time_start print "time[sec]=",time_exec file_out.close() eKin = bunch_in.getSyncParticle().kinEnergy()*1.0e+3 twiss_analysis.analyzeBunch(bunch_in) (alphaX,betaX,emittX) = (twiss_analysis.getTwiss(0)[0],twiss_analysis.getTwiss(0)[1],twiss_analysis.getTwiss(0)[3]) (alphaY,betaY,emittY) = (twiss_analysis.getTwiss(1)[0],twiss_analysis.getTwiss(1)[1],twiss_analysis.getTwiss(1)[3]) (alphaZ,betaZ,emittZ) = (twiss_analysis.getTwiss(2)[0],twiss_analysis.getTwiss(2)[1],twiss_analysis.getTwiss(2)[3]) print " ========= CCL4 exit PyORBIT Twiss =========== eKin=",eKin print " aplha beta emitt[mm*mrad] X= ( %6.4f , %6.4f , %6.4f ) "%(alphaX,betaX,emittX*1.0e+6) print " aplha beta emitt[mm*mrad] Y= ( %6.4f , %6.4f , %6.4f ) "%(alphaY,betaY,emittY*1.0e+6) print " aplha beta emitt[mm*MeV] Z= ( %6.4f , %6.4f , %6.4f ) "%(alphaZ,betaZ,emittZ*1.0e+6)
(x, xp, y, yp, z, dE) = distGen.getCoordinates() b.addParticle(x, xp, y, yp, z, dE) b.compress() syncPart = b.getSyncParticle() syncPart.kinEnergy(TK) # copy the initial bunch to another to track through TEAPOT Quad b1 = Bunch() b.copyBunchTo(b1) twiss_analysis = BunchTwissAnalysis() twiss_analysis.analyzeBunch(b) print "============before==================" print "X Twiss =", twiss_analysis.getTwiss(0) print "Y Twiss =", twiss_analysis.getTwiss(1) print "Z Twiss =", twiss_analysis.getTwiss(2) #b.dumpBunch() G = 30.0 # [T/m] length = 0.1 # [m] fieldSource = FieldSource(G) tracker = RungeKuttaTracker(length) print "Tracker Entrance plane (a,b,c,d)=", tracker.entrancePlane() print "Tracker Exit plane (a,b,c,d)=", tracker.exitPlane() # the spatial eps is useless because we have quad field = 0 on axis for the syncPart #tracker.spatialEps(0.00000000001) # we have to specify the number of time steps tracker.stepsNumber(40) tracker.trackBunch(b, fieldSource)
class SNS_Linac_BunchGenerator: """ Generates the pyORBIT SNS Linac Bunches using the Gauss distribution. Twiss parameters have the following units: x in [m], xp in [rad] and the X and Y emittances are un-normalized. The longitudinal emittance is in [GeV*m]. """ def __init__(self,frequency = 402.5e+6): self.bunch_frequency = frequency #set H- mass #self.bunch.mass(0.9382723 + 2*0.000511) self.bunch = Bunch() self.bunch.getSyncParticle().kinEnergy(0.0025) self.init_coords = (0.,0.,0.,0.,0.,0.) self.bunch.mass(0.939294) self.bunch.charge(-1.0) self.c = 2.99792458e+8 # speed of light in m/sec self.beam_current = 38.0 # beam current in mA , design = 38 mA self.rf_wave_lenght = self.c/self.bunch_frequency self.si_e_charge = 1.6021773e-19 #---------------------------------------- self.twiss_analysis = BunchTwissAnalysis() def setInitialCorrdsCenter(self,x0,xp0,y0,yp0,z0,dE0): self.init_coords = (x0,xp0,y0,yp0,z0,dE0) def getInitialCorrdsCenter(self): return self.init_coords def setParticleCharge(self,charge): """ Sets the particle charge H- => -1.0 and proton => +1.0 """ self.bunch.charge(charge) def getKinEnergy(self): """ Returns the kinetic energy in GeV """ return self.bunch.getSyncParticle().kinEnergy() def setKinEnergy(self, e_kin = 0.0025): """ Sets the kinetic energy in GeV """ self.bunch.getSyncParticle().kinEnergy(e_kin) def getZtoPhaseCoeff(self,bunch): """ Returns the coefficient to calculate phase in degrees from the z-coordinate. """ bunch_lambda = bunch.getSyncParticle().beta()*self.rf_wave_lenght phase_coeff = 360./bunch_lambda return phase_coeff def getBeamCurrent(self): """ Returns the beam currect in mA """ return self.beam_current def setBeamCurrent(self, current): """ Sets the beam currect in mA """ self.beam_current = current def getBunch(self, nParticles, twissX, twissY, twissZ, cut_off = -1.): """ Returns the pyORBIT bunch with particular number of particles. """ (x0,xp0,y0,yp0,z0,dE0) = self.init_coords comm = orbit_mpi.mpi_comm.MPI_COMM_WORLD rank = orbit_mpi.MPI_Comm_rank(comm) size = orbit_mpi.MPI_Comm_size(comm) data_type = mpi_datatype.MPI_DOUBLE main_rank = 0 bunch = Bunch() self.bunch.copyEmptyBunchTo(bunch) macrosize = (self.beam_current*1.0e-3/self.bunch_frequency) macrosize /= (math.fabs(bunch.charge())*self.si_e_charge) distributor = GaussDist3D(twissX,twissY,twissZ, cut_off) bunch.getSyncParticle().time(0.) for i in range(nParticles): (x,xp,y,yp,z,dE) = distributor.getCoordinates() (x,xp,y,yp,z,dE) = orbit_mpi.MPI_Bcast((x,xp,y,yp,z,dE),data_type,main_rank,comm) if(i%size == rank): bunch.addParticle(x+x0,xp+xp0,y+y0,yp+yp0,z+z0,dE+dE0) nParticlesGlobal = bunch.getSizeGlobal() bunch.macroSize(macrosize/nParticlesGlobal) return bunch def bunchAnalysis(self,bunch): #---- returns (x_rms,y_rms,z_rms_deg) self.twiss_analysis.analyzeBunch(bunch) gamma = bunch.getSyncParticle().gamma() beta = bunch.getSyncParticle().beta() x_rms = math.sqrt(self.twiss_analysis.getTwiss(0)[1]*self.twiss_analysis.getTwiss(0)[3])*1000. y_rms = math.sqrt(self.twiss_analysis.getTwiss(1)[1]*self.twiss_analysis.getTwiss(1)[3])*1000. z_rms = math.sqrt(self.twiss_analysis.getTwiss(2)[1]*self.twiss_analysis.getTwiss(2)[3])*1000. z_to_phase_coeff = bunch_gen.getZtoPhaseCoeff(bunch) z_rms_deg = z_to_phase_coeff*z_rms/1000.0 (alphaX,betaX,emittX) = (self.twiss_analysis.getTwiss(0)[0],self.twiss_analysis.getTwiss(0)[1],self.twiss_analysis.getTwiss(0)[3]*1.0e+6) (alphaY,betaY,emittY) = (self.twiss_analysis.getTwiss(1)[0],self.twiss_analysis.getTwiss(1)[1],self.twiss_analysis.getTwiss(1)[3]*1.0e+6) (alphaZ,betaZ,emittZ) = (self.twiss_analysis.getTwiss(2)[0],self.twiss_analysis.getTwiss(2)[1],self.twiss_analysis.getTwiss(2)[3]*1.0e+6) norm_emittX = emittX*gamma*beta norm_emittY = emittY*gamma*beta #gammaZ = (1.0+alphaZ**2)/betaZ #dE_sigma = math.sqrt(gammaZ*emittZ) #print "debug dE_sigma [MeV]=",dE_sigma #---- phi_de_emittZ will be in [pi*deg*MeV] phi_de_emittZ = z_to_phase_coeff*emittZ return (x_rms,y_rms,z_rms_deg) def shiftPhase(self,bunch,delta_phi_deg): dz = - delta_phi_deg/self.getZtoPhaseCoeff(bunch) nParts = bunch.getSize() for ind in range(nParts): z = bunch.z(ind) bunch.z(ind,z+dz)
time_start = time.clock() accLattice.trackBunch(bunch_in, paramsDict=paramsDict, actionContainer=actionContainer) time_exec = time.clock() - time_start print "time[sec]=", time_exec fl_loss_pos_coord_out.close() file_out.close() eKin = bunch_in.getSyncParticle().kinEnergy() * 1.0e+3 twiss_analysis.analyzeBunch(bunch_in) (alphaX, betaX, emittX) = (twiss_analysis.getTwiss(0)[0], twiss_analysis.getTwiss(0)[1], twiss_analysis.getTwiss(0)[3]) (alphaY, betaY, emittY) = (twiss_analysis.getTwiss(1)[0], twiss_analysis.getTwiss(1)[1], twiss_analysis.getTwiss(1)[3]) (alphaZ, betaZ, emittZ) = (twiss_analysis.getTwiss(2)[0], twiss_analysis.getTwiss(2)[1], twiss_analysis.getTwiss(2)[3]) print " ========= CCL4 exit PyORBIT Twiss =========== eKin=", eKin print " aplha beta emitt[mm*mrad] X= ( %6.4f , %6.4f , %6.4f ) " % ( alphaX, betaX, emittX * 1.0e+6) print " aplha beta emitt[mm*mrad] Y= ( %6.4f , %6.4f , %6.4f ) " % ( alphaY, betaY, emittY * 1.0e+6) print " aplha beta emitt[mm*MeV] Z= ( %6.4f , %6.4f , %6.4f ) " % (
#set up design - arrival times at each fist gaps of all RF cavities accLattice.trackDesignBunch(bunch_in) print "Design tracking completed." #real tracking of the bunch. Now we have the bunch at the BNCH02 entrance accLattice.trackBunch(bunch_in, None, None, 0, bnch02_ind - 1) #---- memorize initial coordinates of particles in PartAttributes copyCoordsToInitCoordsAttr(bunch_in) twiss_analysis = BunchTwissAnalysis() bunch = bunch_in twiss_analysis.analyzeBunch(bunch) x_rms = math.sqrt( twiss_analysis.getTwiss(0)[1] * twiss_analysis.getTwiss(0)[3]) * 1000. y_rms = math.sqrt( twiss_analysis.getTwiss(1)[1] * twiss_analysis.getTwiss(1)[3]) * 1000. z_rms = math.sqrt( twiss_analysis.getTwiss(2)[1] * twiss_analysis.getTwiss(2)[3]) * 1000. (alphaX, betaX, emittX) = (twiss_analysis.getTwiss(0)[0], twiss_analysis.getTwiss(0)[1], twiss_analysis.getTwiss(0)[3]) (alphaY, betaY, emittY) = (twiss_analysis.getTwiss(1)[0], twiss_analysis.getTwiss(1)[1], twiss_analysis.getTwiss(1)[3]) (alphaZ, betaZ, emittZ) = (twiss_analysis.getTwiss(2)[0], twiss_analysis.getTwiss(2)[1], twiss_analysis.getTwiss(2)[3]) gammaZ = (1.0 + alphaZ**2) / betaZ dE_rms = math.sqrt(gammaZ * emittZ * 1.0e+6) * 1000.
#set up design - arrival times at each fist gaps of all RF cavities accLattice.trackDesignBunch(bunch_in) print "Design tracking completed." #real tracking of the bunch. Now we have the bunch at the BNCH02 entrance accLattice.trackBunch(bunch_in, None, None, 0, bnch02_ind - 1) #---- memorize initial coordinates of particles in PartAttributes copyCoordsToInitCoordsAttr(bunch_in) twiss_analysis = BunchTwissAnalysis() bunch = bunch_in twiss_analysis.analyzeBunch(bunch) x_rms = math.sqrt( twiss_analysis.getTwiss(0)[1] * twiss_analysis.getTwiss(0)[3]) * 1000. y_rms = math.sqrt( twiss_analysis.getTwiss(1)[1] * twiss_analysis.getTwiss(1)[3]) * 1000. z_rms = math.sqrt( twiss_analysis.getTwiss(2)[1] * twiss_analysis.getTwiss(2)[3]) * 1000. (alphaZ, betaZ, emittZ) = (twiss_analysis.getTwiss(2)[0], twiss_analysis.getTwiss(2)[1], twiss_analysis.getTwiss(2)[3]) gammaZ = (1.0 + alphaZ**2) / betaZ dE_rms = math.sqrt(gammaZ * emittZ * 1.0e+6) * 1000. z_to_phase_coeff = bunch_gen.getZtoPhaseCoeff(bunch) z_rms_deg = z_to_phase_coeff * z_rms / 1000.0 print "========== Bunch RMS sizes at BNCH02 entrance:" print "(Sx[mm],Sy[mm],Sz[deg]) = %5.3f %5.3f %5.3f " % (x_rms, y_rms, z_rms_deg) print "(alphaZ, betaZ[deg/MeV], emittZ[deg*keV] = (%5.4f %5.4f %12.5g )" % (
b.compress() syncPart = b.getSyncParticle() syncPart.kinEnergy(TK) # copy the initial bunch to another to track through TEAPOT Quad b1 = Bunch() b.copyBunchTo(b1) twiss_analysis = BunchTwissAnalysis() twiss_analysis.analyzeBunch(b) print "============before==================" print "X Twiss =",twiss_analysis.getTwiss(0) print "Y Twiss =",twiss_analysis.getTwiss(1) print "Z Twiss =",twiss_analysis.getTwiss(2) #b.dumpBunch() G = 30.0 # [T/m] length = 0.1 # [m] fieldSource = FieldSource(G) tracker = RungeKuttaTracker(length) print "Tracker Entrance plane (a,b,c,d)=",tracker.entrancePlane() print "Tracker Exit plane (a,b,c,d)=",tracker.exitPlane() # the spatial eps is useless because we have quad field = 0 on axis for the syncPart #tracker.spatialEps(0.00000000001) # we have to specify the number of time steps tracker.stepsNumber(40) tracker.trackBunch(b,fieldSource)
twissY = TwissContainer(alphaY,betaY,emittY) twissZ = TwissContainer(alphaZ,betaZ,emittZ) distGen = GaussDist3D(twissX,twissY,twissZ) distGen = WaterBagDist3D(twissX,twissY,twissZ) distGen = KVDist3D(twissX,twissY,twissZ) n_parts = 1000 for i in range(n_parts): (x,xp,y,yp,z,dE) = distGen.getCoordinates() bunch_ini.addParticle(x,xp,y,yp,z,dE) twiss_analysis = BunchTwissAnalysis() twiss_analysis.analyzeBunch(bunch_ini) print "============Initial Bunch Twiss parameters ==================" print "X Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )"%twiss_analysis.getTwiss(0) print "Y Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )"%twiss_analysis.getTwiss(1) print "Z Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )"%twiss_analysis.getTwiss(2) x_rms = math.sqrt(twiss_analysis.getTwiss(0)[1]*twiss_analysis.getTwiss(0)[3])*1000. y_rms = math.sqrt(twiss_analysis.getTwiss(1)[1]*twiss_analysis.getTwiss(1)[3])*1000. z_rms = math.sqrt(twiss_analysis.getTwiss(2)[1]*twiss_analysis.getTwiss(2)[3])*1000. print "Initial Bunch (x_rms,y_rms,z_rms) = ( %6.3f, %6.3f, %6.3f )"%(x_rms,y_rms,z_rms) print "============================================================" bunch_tmp = Bunch() bunch_ini.copyBunchTo(bunch_tmp) start_ind = accLattice.getNodeIndex(quads[0]) stop_ind = accLattice.getNodeIndex(quads[len(quads)-1]) accLattice.trackDesignBunch(bunch_tmp,None,None, index_start = start_ind, index_stop = stop_ind)
twissY = TwissContainer(alphaY, betaY, emittY) twissZ = TwissContainer(alphaZ, betaZ, emittZ) distGen = GaussDist3D(twissX, twissY, twissZ) #distGen = WaterBagDist3D(twissX,twissY,twissZ) #distGen = KVDist3D(twissX,twissY,twissZ) n_parts = 1000 for i in range(n_parts): (x, xp, y, yp, z, dE) = distGen.getCoordinates() bunch_ini.addParticle(x, xp, y, yp, z, dE) twiss_analysis = BunchTwissAnalysis() twiss_analysis.analyzeBunch(bunch_ini) print "============Initial Bunch Twiss parameters ==================" print "X Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )" % twiss_analysis.getTwiss(0) print "Y Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )" % twiss_analysis.getTwiss(1) print "Z Twiss = ( %8.2f, %8.2f, %8.2f, %10.3e )" % twiss_analysis.getTwiss(2) x_rms = math.sqrt( twiss_analysis.getTwiss(0)[1] * twiss_analysis.getTwiss(0)[3]) * 1000. y_rms = math.sqrt( twiss_analysis.getTwiss(1)[1] * twiss_analysis.getTwiss(1)[3]) * 1000. z_rms = math.sqrt( twiss_analysis.getTwiss(2)[1] * twiss_analysis.getTwiss(2)[3]) * 1000. print "Initial Bunch (x_rms,y_rms,z_rms) = ( %6.3f, %6.3f, %6.3f )" % ( x_rms, y_rms, z_rms) print "============================================================" bunch_tmp = Bunch() bunch_ini.copyBunchTo(bunch_tmp)