#/usr/bin/env python
#Filename:inject.py

import elements as el
import math
import bump
import inj_dist

C = 2.99792458e8  #speed of light [m/sec]                                
E0 = 0.938272310  #proton mass [GeV]                                     
einj = 0.08       #[GeV] injection kinetic Energy                        
ek = 0.08                                                                 
PC = (ek * (ek + 2 * E0)) ** 0.5      #  Value PC                                 
brho = 1.e9 * PC / C             #  Value BRHO                               

ang = 2 * math.pi / 24.0
ee = ang / 2.0
lbend = 2.1
lqf01 = 0.41
lqd02 = 0.90
lqf03 = 0.41
lqf04 = 0.45
lqd05 = 0.90
lqf06 = 0.62

l1001 = 5.5
l1011 = 0.8
l1021 = 1.15
l1031 = 3.8
l1032 = 1.2
l1033 = 1.3
l1041 = 1.3
l1051 = 0.8
l1061 = 0.9
l1062 = 1.75

lkicker = 0.3
lk_qd = 0.4
lk_qf = 0.45

#kqf06 = 0.80297056 / brho     
#kqd05 = -0.76457619 / brho      
#kqf04 = 0.752954981 / brho     
#kqf03 = 0.87652783 / brho     
#kqd02 = -0.76457619 / brho      
#kqf01 = 0.977412095 / brho
kqf06 = 0.8053181 / brho     
kqd05 = -0.7703965 / brho     
kqf04 = 0.7540277 / brho      
kqf03 = 0.8773794 / brho     
kqd02 = -0.7703965 / brho     
kqf01 = 0.9881082 / brho


bunch = list()
bunch_lost = list()
for i in range(0,500):
	betax_inj = 1.609
	betay_inj = 1.784
	alphax_inj = 0.005
	alphay_inj = -0.014
	emitx_inj = 1.0
	emity_inj = 1.0
#deta_inj = 0.0
#deta_inj = (0.5 - float(i) / 200) * 0.01
#deta_inj = 0.001 
#deta_inj = 0.025 
	number_per = 10
	lx1 = 0.3
	lx2 = 2.01
	lx3 = 0.3
	lx4 = 1.960
	lx5 = 1.960
	lx6 = 0.3
	lx7 = 2.01
	lx8 = 0.3
	ly1 = 0.3
	ly2 = 2.01
	ly3 = 0.3
	ly4 = 2.66
	ly5 = 2.66
	ly6 = 0.3
	ly7 = 2.01
	ly8 = 0.3
	hx,thetax,hy,thetay = bump.bump(i,500,5000,30.0,6.278,5.556,0.0,0.0)
	bunch_inj = inj_dist.bunch(betax_inj,betay_inj,alphax_inj,alphay_inj,emitx_inj,emity_inj,number_per)
#bunch_inj = [[0.0,7.0,0.0,7.0,0.0,0.0,0.0]]
	
	thetax1,thetax2,thetax3,thetax4 = bump.theta(lx1,lx2,lx3,lx4,lx5,lx6,lx7,lx8,hx,thetax)
	thetay1,thetay2,thetay3,thetay4 = bump.theta(ly1,ly2,ly3,ly4,ly5,ly6,ly7,ly8,hy,thetay)
	for k in range(0,number_per):
		#bunch_inj[k][1] += (30.0 - hx)
		#bunch_inj[k][2] -= thetax
		#bunch_inj[k][3] += (30.0 - hy)
		#bunch_inj[k][4] -= thetay
		bunch_inj[k][1] += 30.0
		bunch_inj[k][3] += 30.0
	bunch,bunch_lost = el.vkick(bunch,ly1,thetay1,1300,1300)
	bunch,bunch_lost = el.drift(bunch,0.4,1300,1300)
	bunch,bunch_lost = el.hkick(bunch,lx1,thetax1,1300,1300)
	bunch,bunch_lost = el.drift(bunch,1.31,1300,1300)
	bunch,bunch_lost = el.vkick(bunch,ly3,thetay2,1300,1300)
	bunch,bunch_lost = el.drift(bunch,0.4,1300,1300)
	bunch,bunch_lost = el.hkick(bunch,lx3,thetax2,1300,1300)
	bunch,bunch_lost = el.drift(bunch,lx4,1300,1300)
	bunch += bunch_inj	#this is the injection
#	if i < 1: bunch += bunch_inj	#this is the injection
	bunch,bunch_lost = el.drift(bunch,lx5,1300,1300)
	bunch,bunch_lost = el.hkick(bunch,lx6,thetax3,1300,1300)
	bunch,bunch_lost = el.drift(bunch,0.4,1300,1300)
	bunch,bunch_lost = el.vkick(bunch,ly6,thetay3,1300,1300)
	bunch,bunch_lost = el.drift(bunch,1.31,1300,1300)
	bunch,bunch_lost = el.hkick(bunch,lx8,thetax4,1300,1300)
	bunch,bunch_lost = el.drift(bunch,0.4,1300,1300)
	bunch,bunch_lost = el.vkick(bunch,ly8,thetay4,1300,1300)
	bunch,bunch_lost = el.drift(bunch,l1001 - 5.27,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001,120,120)

	bunch,bunch_lost = el.drift(bunch,l1001,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.drift(bunch,l1062,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1061,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf06,kqf06,120,120)
	bunch,bunch_lost = el.drift(bunch,l1051,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd05,kqd05,120,120)
	bunch,bunch_lost = el.drift(bunch,l1041,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf04,kqf04,120,120)
	bunch,bunch_lost = el.drift(bunch,l1033,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1032,120,120)
	bunch,bunch_lost = el.sbend(bunch,lbend,ang,ee,ee,120,120)
	bunch,bunch_lost = el.drift(bunch,l1031,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf03,kqf03,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qf,120,120)
	bunch,bunch_lost = el.drift(bunch,lkicker,120,120)
	bunch,bunch_lost = el.drift(bunch,lk_qd,120,120)
	bunch,bunch_lost = el.quad(bunch,lqd02,kqd02,120,120)
	bunch,bunch_lost = el.drift(bunch,l1011,120,120)
	bunch,bunch_lost = el.quad(bunch,lqf01,kqf01,120,120)
	bunch,bunch_lost = el.drift(bunch,l1001 - 5.27,120,120)
#acc 
	bunch = el.acc(bunch,31.0/1000, 0.0/1000,0.0,80.0,80.0,2,4)
#print len(bunch)
bunch,bunch_lost = el.drift(bunch,5.27,120,120)
el.dumppar(bunch,'dis1.txt')
el.dumppar(bunch_lost,'lost.txt')
