def sigmaBends2(self, E, s=None, s0=0, n=20): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems) - 1 endPhase = self.markers[1].MUX ss = self.elems[-1].L else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap(nE): def f(ss): para = self.getBeta(nE, ss) disp = self.getDisp(nE, ss) if disp.DX == 0: return 0 alpha = math.atan(-para.ALFX - para.BETX * disp.DPX / disp.DX) Phi = (endPhase - self.getPhase(nE, ss).MUX) * 2 * math.pi + alpha cosPhi = (math.cos(Phi)**2).real H = self.getH(nE, ss) P = abs(e.L / e.ANGLE) return H.HX * cosPhi / P**3 return f c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 * E**5 * self.markers[1].BETX total = 0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND': if i == nEFirst: total += coeff * simpson(wrap(i), ss0, e.L, n) elif i == nELast: total += coeff * simpson(wrap(i), 0, ss, n) else: total += coeff * simpson(wrap(i), 0, e.L, n) if i == nELast: return total
def sigmaBends2(self, E, s=None, s0=0, n=20): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems)-1 endPhase = self.markers[1].MUX ss = self.elems[-1].L else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap(nE): def f(ss): para = self.getBeta(nE,ss) disp = self.getDisp(nE,ss) if disp.DX == 0: return 0 alpha = math.atan(-para.ALFX - para.BETX * disp.DPX / disp.DX) Phi = (endPhase - self.getPhase(nE,ss).MUX) * 2 * math.pi + alpha cosPhi = (math.cos(Phi) ** 2).real H = self.getH(nE, ss) P = abs(e.L / e.ANGLE) return H.HX * cosPhi / P**3 return f c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 * E**5 * self.markers[1].BETX total = 0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND': if i == nEFirst: total += coeff * simpson(wrap(i), ss0, e.L, n) elif i == nELast: total += coeff * simpson(wrap(i), 0, ss, n) else: total += coeff * simpson(wrap(i), 0, e.L, n) if i == nELast: return total
def getChrom(self, s=None, s0=0, n=100): """ Calculates chromaticity using -1/4pi * integral (beta*K) ds :param float s: end location along beamline :param float s0: start location along beamline (optional) :param int n: number of intervals for integration (optional) :returns: chromaticity between s0 and s """ if s is None: s = self.markers[1].S ## CHECK: positive/negative signs on K for focus vs. defocus... ## Is this natural chromaticity also because it only considers quadrupoles? ## What about multipole quadrupoles? def fX(s): nE = self.findElem(s) e = self.elems[nE] ss = s - (e.S - e.L) bet = self.getBeta(nE, ss) if e.K1L != 0: return bet.BETX * -e.K1L / e.L # Correct to make negative? else: return 0 def fY(s): nE = self.findElem(s) e = self.elems[nE] ss = s - (e.S - e.L) bet = self.getBeta(nE, ss) if e.K1L != 0: return bet.BETY * e.K1L / e.L else: return 0 return dct([('ChromX', -simpson(fX, s0, s, n) / (4 * math.pi)), ('ChromY', -simpson(fY, s0, s, n) / (4 * math.pi))])
def sigmaBends(self, E, s=None, s0=0, n=100): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S endPhase = self.markers[1].MUX else: nE = self.findElem(s) e = self.elems[nE] ss = s - (e.S - e.L) endPhase = self.getPhase(nE, ss) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def f(s): nE = self.findElem(s) e = self.elems[nE] # Calculate function only if element is dipole (i.e. ANGLE not 0) if e.ANGLE != 0: # ss from beginning of element nE == s from beginning of beamline ss = s - (e.S - e.L) para = self.getBeta(nE, ss) disp = self.getDisp(nE, ss) alpha = math.atan(-para.ALFX - para.BETX * disp.DPX / disp.DX) Phi = (endPhase - self.getPhase(nE, ss).MUX) * 2 * math.pi + alpha cosPhi = (math.cos(Phi)**2).real H = self.getH(nE, ss) P = abs(e.L / e.ANGLE) return H.HX * cosPhi / P**3 else: return 0 c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 * E**5 * self.markers[1].BETX return coeff * simpson(f, s0, s, n)
def sigmaBends(self, E, s=None, s0=0, n=100): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S endPhase = self.markers[1].MUX else: nE = self.findElem(s) e = self.elems[nE] ss = s - (e.S - e.L) endPhase = self.getPhase(nE, ss) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def f(s): nE = self.findElem(s) e = self.elems[nE] # Calculate function only if element is dipole (i.e. ANGLE not 0) if e.ANGLE != 0: # ss from beginning of element nE == s from beginning of beamline ss = s - (e.S - e.L) para = self.getBeta(nE,ss) disp = self.getDisp(nE,ss) alpha = math.atan(-para.ALFX - para.BETX * disp.DPX / disp.DX) Phi = (endPhase - self.getPhase(nE,ss).MUX) * 2 * math.pi + alpha cosPhi = (math.cos(Phi) ** 2).real H = self.getH(nE, ss) P = abs(e.L / e.ANGLE) return H.HX * cosPhi / P**3 else: return 0 c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 * E**5 * self.markers[1].BETX return coeff * simpson(f, s0, s, n)
def oide(self, emi=2e-8, gamma=2.9354207436399e-6, betas=None, n=100): """ Returns delta(sigma^2) due to Oide Effect :param float emi: emittance :param float gamma: Lorentz factor = E/Eo = 1500/0.000511 for CLIC :param int n: number of intervals for integration (optional) """ re = 2.817940325e-15 lame = 3.861592678e-13 if betas is None: betas = self.markers[1].BETY # Reads 6.77249e-5 from FFS # (betas = 17.92472388e-6 from mathematica nb) coeff = 110 * re * lame * gamma**5 / (3 * math.sqrt(6 * math.pi)) # Read twiss object in reverse to find first DRIFT and QUADRUPOLE # to get ls, Lq and Kq for e in reversed(self.elems): if e.KEYWORD == 'DRIFT': ls = e.L break for e in reversed(self.elems): if e.KEYWORD == 'QUADRUPOLE': # Multiplied by 2 because final quadrupoles split in file Lq = 2 * e.L Kq = abs(e.K1L / e.L) break c = math.sqrt(Kq) * ls b = math.sqrt(Kq) * Lq # Define functions for integration def f(x): return math.sin(x) + c * math.cos(x) def g(y): return (abs(math.sin(y) + c * math.cos(y))**3) * simpson(f, 0, y, n)**2 integral = simpson(g, 0, b, n) return coeff * integral * (emi / (betas * gamma))**2.5
def sigmaBends2b(self, E, s=None, s0=0, n=10): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems) - 1 endPhase = self.markers[1].MUX ss = self.elems[-1].L else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap5(nE, betaxL, dx0, dpx0, dxL, dxpL, E): def f(ss): dE = 14.6e-6 * (e.ANGLE / e.L)**2 * ss * E**4 Energie = E - dE matnE = self.matrixnE(nE, ss) para = self.getBeta2(nE, ss, matnE) disp = self.getDisp2(nE, ss, matnE) etax = disp.DX etapx = disp.DPX dphi = (endPhase - self.getPhase(nE, ss).MUX) * 2 * math.pi dphiT = abs( self.getPhase(nE, e.L).MUX - self.getPhase(nE, 0).MUX) * 2 * math.pi # print dphiT # fact1 = math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (math.cos(e.ANGLE)*dx0 + e.L/e.ANGLE*math.sin(e.ANGLE)*dpx0) fact1 = ( math.sqrt(betaxL) / math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx) * math.sin(dphi)) - (dxL)) * (1 - 1 / dphiT) fact2 = fact1 * fact1 P = abs(e.L / e.ANGLE) return Energie**5 * fact2 / (P * P * P) return f coeff = 4.13e-11 # m^2(GeV)^-5 coeff2 = 2.0 / 3 * 2.8179403267e-15 / ((0.510998928e-3)**3) # coeff3 = 5*math.sqrt(3)*2.8179403267e-15/(6*197.326963e-18*299792458) total = 0 total2 = 0 total3 = 0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND': if i == nEFirst: total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), ss0, e.L, n) elif i == nELast: total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), self.mar0, ss, n) else: total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), 0, e.L, n) if i == nELast: return total
def sigmaBends2a(self, E, s=None, s0=0, n=10): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems) - 1 endPhase = self.markers[1].MUX ss = self.elems[-1].L # print "aqui" else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap5(nE, betaxL, dx0, dpx0, dxL, dxpL, E): def f(ss): dE = 14.6e-6 * (e.ANGLE / e.L)**2 * ss * E**4 Energie = E - dE matnE = self.matrixnE(nE, ss) # print matnE para = self.getBeta2(nE, ss, matnE) disp = self.getDisp2(nE, ss, matnE) etax = disp.DX # print ss,disp.DX, disp.DPX, dxL, dxpL, para.BETX, betaxL etapx = disp.DPX dphi = (endPhase - self.getPhase(nE, ss).MUX) * 2 * math.pi # fact1 = math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (math.cos(e.ANGLE)*dx0 + e.L/e.ANGLE*math.sin(e.ANGLE)*dpx0) fact1 = math.sqrt(betaxL) / math.sqrt( para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx) * math.sin(dphi)) - (dxL) fact2 = fact1 * fact1 P = abs(e.L / e.ANGLE) return Energie**5 * fact2 / (P * P * P) return f c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 total = 0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND' and e.ANGLE != 0: if i == nEFirst: # print self.markers[0].DX,self.markers[0].DPX total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), ss0, e.L, n) # print "este" elif i == nELast: total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), self.mar0, ss, n) # print "oeste" else: total += coeff * simpson( wrap5(i, self.markers[1].BETX, self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), 0, e.L, n) # print "norte" if i == nELast: return total
def g(y): return (abs(math.sin(y) + c * math.cos(y))**3) * simpson(f, 0, y, n)**2
value = 12 * f(t) * t - 8 * f(t) * t**3 return value ## error of simpson integration as a function of N, step number def error_simpson(a, b, N): h = (a - b) / N threeD_a = threeD_f(a) threeD_b = threeD_f(b) return 1 / 90 * h**4 * (f(a) - f(b)) # use simpson integration method to find the integral of e^-x^2 from 0 to x for each x value step = np.linspace(1, 6000, 501) In = list(map(lambda n: itg.simpson(f, 0, 3, n), step)) #find the result of the integral ny taking n steps error = list(map(lambda n, I: np.abs(error_simpson(0, 3, n)) / I, step, In)) #compute fractional error #plot fractional error plt.plot(step, error, label="Fractional error") plt.yscale('log') ## plot in log scale plt.xlabel("Number of bins $N$") plt.ylabel("log(Fractional error of $J_n(x))$") plt.title("Fractional error of $E(x)=\int_0^x e^{-t^2} dx$ ") plt.legend() plt.savefig('erfError.pdf') plt.show()
def sigmaBends2b(self, E, s=None, s0=0, n=10): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems)-1 endPhase = self.markers[1].MUX ss = self.elems[-1].L else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap5(nE,betaxL,dx0,dpx0,dxL,dxpL,E): def f(ss): dE = 14.6e-6 * (e.ANGLE/e.L)**2 * ss*E**4 Energie = E - dE matnE = self.matrixnE(nE,ss) para = self.getBeta2(nE,ss,matnE) disp = self.getDisp2(nE,ss, matnE) etax = disp.DX etapx = disp.DPX dphi = (endPhase - self.getPhase(nE,ss).MUX) * 2 * math.pi dphiT = abs(self.getPhase(nE,e.L).MUX - self.getPhase(nE,0).MUX) * 2 * math.pi # print dphiT # fact1 = math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (math.cos(e.ANGLE)*dx0 + e.L/e.ANGLE*math.sin(e.ANGLE)*dpx0) fact1 = (math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (dxL)) * (1-1/dphiT) fact2 = fact1 * fact1 P = abs(e.L / e.ANGLE) return Energie**5*fact2/(P*P*P) return f coeff = 4.13e-11 # m^2(GeV)^-5 coeff2 = 2.0/3*2.8179403267e-15/((0.510998928e-3)**3) # coeff3 = 5*math.sqrt(3)*2.8179403267e-15/(6*197.326963e-18*299792458) total = 0 total2 =0 total3=0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND': if i == nEFirst: total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E),ss0, e.L, n) elif i == nELast: total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), self.mar0, ss, n) else: total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), 0, e.L, n) if i == nELast: return total
def sigmaBends2a(self, E, s=None, s0=0, n=10): """ Returns delta(sigma^2) due to bends (dipoles) :param float E: energy :param float s: location of interest along beamline (optional) :param float s0: start location along beamline (optional) :param int n: number of intervals for integrations (optional) """ if s is None: s = self.markers[1].S nELast = len(self.elems)-1 endPhase = self.markers[1].MUX ss = self.elems[-1].L # print "aqui" else: nELast = self.findElem(s) last = self.elems[nELast] ss = s - (last.S - last.L) endPhase = self.getPhase(nELast, ss).MUX if s0 != 0: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) rangeElems = xrange(nEFirst, len(self.elems)) else: nEFirst = self.findElem(s0) first = self.elems[nEFirst] ss0 = s0 - (first.S - first.L) nEFirst = 0 rangeElems = xrange(len(self.elems)) # Calculates H*G^3 cos(phi)^2 at location s along the beamline def wrap5(nE,betaxL,dx0,dpx0,dxL,dxpL,E): def f(ss): dE = 14.6e-6 * (e.ANGLE/e.L)**2 * ss*E**4 Energie = E - dE matnE = self.matrixnE(nE,ss) # print matnE para = self.getBeta2(nE,ss,matnE) disp = self.getDisp2(nE,ss, matnE) etax = disp.DX # print ss,disp.DX, disp.DPX, dxL, dxpL, para.BETX, betaxL etapx = disp.DPX dphi = (endPhase - self.getPhase(nE,ss).MUX) * 2 * math.pi # fact1 = math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (math.cos(e.ANGLE)*dx0 + e.L/e.ANGLE*math.sin(e.ANGLE)*dpx0) fact1 = math.sqrt(betaxL)/math.sqrt(para.BETX) * (etax * math.cos(dphi) + (para.ALFX * etax + para.BETX * etapx)*math.sin(dphi)) - (dxL) fact2 = fact1 * fact1 P = abs(e.L / e.ANGLE) return Energie**5*fact2/(P*P*P) return f c2 = 4.13e-11 # m^2(GeV)^-5 coeff = c2 total = 0 for i in rangeElems: e = self.elems[i] if e.KEYWORD == 'SBEND' and e.ANGLE!=0: if i == nEFirst: # print self.markers[0].DX,self.markers[0].DPX total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E),ss0, e.L, n) # print "este" elif i == nELast: total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), self.mar0, ss, n) # print "oeste" else: total += coeff * simpson(wrap5(i,self.markers[1].BETX,self.markers[0].DX, self.markers[0].DPX, self.markers[1].DX, self.markers[1].DPX, E), 0, e.L, n) # print "norte" if i == nELast: return total
def Jm(m, x): ## what is the integrand with m and x f = f_mx(m, x) ## integrate from 0 to pi, using Simpson's rule with N = 1000 I = itg.simpson(f, 0, np.pi, 1000) return (1 / np.pi) * I