def setUp(self): from metaclass2 import twiss2 for root, subFolders, files in os.walk('assets/twiss/'): if 'twiss' in files and 'fort.18' in files: print "\nTesting: ", root twissfile = os.path.join(root, "twiss") fortfile = os.path.join(root, "fort.18") self.t = twiss2(twissfile) self.m = Map2(self.t) self.mm = Map2(filename=fortfile)
def setUp(self): from mapclass25 import Map o = 10 f = 'assets/fort.18' ff = 'assets/5fort.18' self.compare = True self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)]) self.m = Map2(order=o, filename=f) self.mm = Map(order=o, filename=f) self.m2 = Map2(order=o, filename=ff) self.mm2 = Map(order=o, filename=ff)
def sigma(deltafamilies): ######################### global betx, bety, gamma, ex, ey, SF1, SD0, order #not necessary since no longer calling function from metaclass2 import twiss2 from mapclass import Map2 from math import sqrt SF1 = 0 SD0 = 0 order = 3 gaussian = False betx=64.99988501 bety=17.99971417 gamma=3e6 ex=68e-8 ey=2e-8 t0 = twiss2('assets/ffs.twiss') t0 = t0.stripLine() t0 = t0.mergeElems() t0.elems[74].K2L = SF1 t0.elems[82].K2L = SD0 m0 = Map2(t0, terr=None, order=order, nbProc=2) print 'Deltafamilies = ', deltafamilies t0.elems[74].K2L = deltafamilies[0] t0.elems[82].K2L = deltafamilies[1] print 'SF1 =',t0.elems[74].K2L, 'and SD0 = ', t0.elems[82].K2L sigmaFFS = [sqrt(ex*betx/gamma), sqrt(ex/betx/gamma), sqrt(ey*bety/gamma), sqrt(ey/bety/gamma), 0.01, 0.002] return(sqrt(m0.sigma('x', sigmaFFS, gaussian).real))
def siglength(deltafamilies): ######################### global betx, bety, gamma, ex, ey, SF1, SD0 #not necessary since no longer calling function from metaclass2 import twiss2 from mapclass import Map2 from math import sqrt order = 9 gaussian = False betx = 64.99988501 bety = 17.99971417 gamma = 3e6 ex = 68e-8 ey = 2e-8 t0 = twiss2('assets/ffs.twiss') t0 = t0.stripLine() t0 = t0.mergeElems() sext = [24, 38, 43, 74, 82] pos = [0] * 5 for i in range(0, len(sext)): while deltafamilies[i] < -1 * t0.elems[ sext[i] - 1].L or deltafamilies[i] > t0.elems[sext[i] + 1].L: deltafamilies[i] = deltafamilies[i] / 2 print "Adjusting guess for sextupole", sext[i] sys.stdout.flush() t = t0.alterElem(sext[i], dPos=deltafamilies[i]) if t is None: break print "Position for ", i, " is outside drifts." else: t0 = t #print 'Deltafamilies = ', deltafamilies #print "positions are", pos #for i in [24, 38, 43, 74, 82]: # print 'Position of element', i, ' = ', t0.elems[i].S #for i in [24, 30, 31, 32, 43, 49, 74, 82]: # print 'Strength of element', i, ' = ', t0.elems[i].K2L m0 = Map2(t0, terr=None, order=order, nbProc=2) sigmaFFS = [ sqrt(ex * betx / gamma), sqrt(ex / betx / gamma), sqrt(ey * bety / gamma), sqrt(ey / bety / gamma), 0.01, 0.002 ] h = sqrt(m0.sigma('x', sigmaFFS, gaussian).real) j = sqrt(m0.sigma('y', sigmaFFS, gaussian).real) h0 = 40e-9 #sigmaFFS[0] j0 = 1e-9 #sigmaFFS[2] chi = (h - h0)**2 / 40**2 + (j - j0)**2 print 'chi2 = ', chi return ((h - h0)**2 / 40**2 + (j - j0)**2)
def sigma(deltafamilies): ######################### #global betx, bety, gamma, ex, ey, SF1, SD0 #not necessary since no longer calling function from metaclass2 import twiss2 from mapclass import Map2 from math import sqrt order = 9 gaussian = False betx = 64.99988501 bety = 17.99971417 gamma = 3e6 ex = 68e-8 ey = 2e-8 t0 = twiss2('assets/ffs.twiss') t0 = t0.stripLine() t0 = t0.mergeElems() sext = [24, 38, 43, 74, 82] positions = [ -0.19078705557404368, -0.071001213587260539, 0.066302461879649, -0.011871072667854801, -0.0097637289616161835 ] # for i in range(0, len(positions)): # t0.elems[sext[i]].S = t0.elems[sext[i]].S + positions[i] print 'Deltafamilies = ', deltafamilies t0.elems[24].K2L = deltafamilies[0] t0.elems[30].K2L = deltafamilies[1] t0.elems[31].K2L = deltafamilies[2] t0.elems[32].K2L = deltafamilies[3] t0.elems[38].K2L = deltafamilies[4] t0.elems[43].K2L = deltafamilies[5] t0.elems[49].K2L = deltafamilies[6] t0.elems[74].K2L = deltafamilies[7] t0.elems[82].K2L = deltafamilies[8] #for i in [24, 38, 43, 74, 82]: # print 'Position of element', i, ' = ', t0.elems[i].S #for i in [24, 30, 31, 32, 43, 49, 74, 82]: # print 'Strength of element', i, ' = ', t0.elems[i].K2L m0 = Map2(t0, terr=None, order=order, nbProc=2) #print 'SF1 =',t0.elems[74].K2L, 'and SD0 = ', t0.elems[82].K2L sigmaFFS = [ sqrt(ex * betx / gamma), sqrt(ex / betx / gamma), sqrt(ey * bety / gamma), sqrt(ey / bety / gamma), 0.01, 0.002 ] h = sqrt(m0.sigma('x', sigmaFFS, gaussian).real) j = sqrt(m0.sigma('y', sigmaFFS, gaussian).real) h0 = 40e-9 #sigmaFFS[0] j0 = 1e-9 #sigmaFFS[2] chi = (h - h0)**2 / 40**2 + (j - j0)**2 print 'chi2 = ', chi, 'sig x = ', h, 'sig y = ', j return ((h - h0)**2 / 40**2 + (j - j0)**2)
def setUp(self): from mapclass25_6var import Map self.sigmaFFS = [0.000227, 9.306e-5, 5.02e-4, 4.21e-5, 0.00666, 0.002] o = 10 f = 'assets/6Dfort.18' self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',self.sigmaFFS[5])]) self.m = Map2(order=o, filename=f) self.mm = Map(order=o, filename=f)
def setUp(self): from mapclassGaussianDelta25 import Map o = 10 f = 'assets/fort.18' self.gaussian = True self.correlation = True self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)]) self.m = Map2(order=o, filename=f) self.mm = Map(order=o, filename=f)
def sigma(deltafamilies): ######################### global betx, bety, gamma, ex, ey, SF1, SD0 #not necessary since no longer calling function from metaclass2 import twiss2 from mapclass import Map2 from math import sqrt order = 3 gaussian = False betx = 64.99988501 bety = 17.99971417 gamma = 3e6 ex = 68e-8 ey = 2e-8 t0 = twiss2('assets/ffs.twiss') t0 = t0.stripLine() t0 = t0.mergeElems() #print 'Deltafamilies = ', deltafamilies t0.elems[24].K2L = deltafamilies[0] t0.elems[30].K2L = deltafamilies[1] t0.elems[31].K2L = deltafamilies[2] t0.elems[32].K2L = deltafamilies[3] t0.elems[38].K2L = deltafamilies[4] t0.elems[43].K2L = deltafamilies[5] t0.elems[49].K2L = deltafamilies[6] t0.elems[74].K2L = deltafamilies[7] t0.elems[82].K2L = deltafamilies[8] m0 = Map2(t0, terr=None, order=order, nbProc=2) #print 'SF1 =',t0.elems[74].K2L, 'and SD0 = ', t0.elems[82].K2L sigmaFFS = [ sqrt(ex * betx / gamma), sqrt(ex / betx / gamma), sqrt(ey * bety / gamma), sqrt(ey / bety / gamma), 0.01, 0.002 ] h = sqrt(m0.sigma('x', sigmaFFS, gaussian).real) j = sqrt(m0.sigma('y', sigmaFFS, gaussian).real) h0 = 40e-9 #sigmaFFS[0] j0 = 1e-9 #sigmaFFS[2] chi = (h - h0)**2 / 40**2 + (j - j0)**2 #print 'chi2 = ',chi, 'sig x = ', h, 'sig y = ', j return ((h - h0)**2 / 40**2 + (j - j0)**2)
#! /usr/bin/env python import sys if len(sys.argv) < 2: sys.stderr.write('Usage: sys.argv[0] dir') sys.exit(1) sys.path.append(sys.argv[1]) from metaclass2 import twiss2 from mapclass import Map2 t = twiss2() m = Map2(t) mm = Map2(order=6) v = ['x', 'px', 'y', 'py'] print mm.compc(m, v).real
### Setup startTime = time.time() t = twiss2('assets/ffs.twiss') t = t.stripLine()# already done in c++ by default t = t.mergeElems() # The following is a call to construct the Map using the C++ Map construction from a twiss file #m = Map2('assets/ffs.twiss',filenameerr=None, order=args.order, nbProc=args.nbProc, strpl=args.strpl) # The following is call to the old mapclass which uses Python map construction #m = Map2(t, "old", order=args.order) # The following is a call to construct the Map from a Twiss Python object m = Map2(t, terr=None, order=args.order, nbProc=args.nbProc, fmultipole=args.fmultipole, strpl=args.strpl) mm = Map2(filename='assets/fort.18',order=args.order) #from fort call v = ['x','px','y','py'] ### Obtain numbers print '' print "!!!!! Maps of order %i" % args.order, if args.gaussian: print "and with Gaussian Delta" print '' print "########## Comparison [It's expected of both to give the same value for high chi2]"
for i in range(0, len(positions)): t0 = t.alterElem(sext1[i], dPos=positions[i]) if t is None: print "IT NO WORK" break else: t = t0 # print t.elems[sext1[i]].S for i in range(0, len(strengths)): t.elems[sext2[i]].K2L = strengths[i] #print t.elems[sext2[i]].K2L m = Map2(t, terr=None, order=args.order, nbProc=args.nbProc) ### betx = 64.99988501 bety = 17.99971417 gamma = 3e6 ex = 68e-8 ey = 2e-8 sigmaFFS = [ sqrt(ex * betx / gamma), sqrt(ex / betx / gamma), sqrt(ey * bety / gamma), sqrt(ey / bety / gamma), 0.01, 0.002 ] ### h = sqrt(m.sigma('x', sigmaFFS, args.gaussian).real) j = sqrt(m.sigma('y', sigmaFFS, args.gaussian).real)
if len(sys.argv) < 3: sys.stderr.write('Usage: sys.argv[0] dir [-t|-f]') sys.exit(1) sys.path.append(sys.argv[1]) from metaclass2 import twiss2 from mapclass import Map2 o = int(sys.argv[3]) startTime = time.time() if sys.argv[2] == "-t": t = twiss2() m = Map2(t, order=o) if sys.argv[2] == "-m": t = twiss2() t = t.stripLine() t = t.mergeElems() m = Map2(t, order=o) else: m = Map2(order=o) v = ['x', 'px', 'y', 'py'] ### betx = 64.99988501 bety = 17.99971417 gamma = 3e6 ex = 68e-8
filename = "sigma.dat" fsigma = open(tmpDir + filename, 'w') t = twiss2(path + 'doc/FFSexample/assets/ffs.twiss') print "SLOW: Takes a while to run. Lower the upperbound of the range to 7 for faster results" for o in range(1, 8): print "* Order " + str(o) # Suppress printing old_stdout = sys.stdout sys.stdout = DummyFile() try: m = Map2(t, order=o) finally: sys.stdout = old_stdout mm = Map2(filename=path + 'doc/FFSexample/assets/fort.18', order=o) fsigma.write(str(o)) for i in v: s0 = sqrt(mm.sigma(i, sigmaFFS).real) sigma = abs(sqrt(m.sigma(i, sigmaFFS).real) - s0) / s0 fsigma.write("\t" + str(sigma)) fsigma.write("\n")
chilist = [0] * len(posrange) print "* Calculating chi-squared for modified position of element", sxtnum, "." for i in range(len(posrange)): sys.stdout.write(".") sys.stdout.flush() t = t0.alterElem(sxtnum, dPos=posrange[i]) if t is None: break sigmaFFS = [ sqrt(ex * betx / gamma), sqrt(ex / betx / gamma), sqrt(ey * bety / gamma), sqrt(ey / bety / gamma), 0.01, 0.002 ] m0 = Map2(t, terr=None, order=order, nbProc=2) h0 = 40e-9 #sigmaFFS[0] j0 = 1e-9 #sigmaFFS[2] h = sqrt(m0.sigma('x', sigmaFFS, gaussian).real) j = sqrt(m0.sigma('y', sigmaFFS, gaussian).real) chi = (h - h0)**2 / 40**2 + (j - j0)**2 #chi = (h/40)**2 + (j)**2 chilist[i] = chi chimin = min(chilist) plt.plot(posrange, chilist) plt.ylabel('chi-squared') plt.xlabel('position') plt.title('Position optimisation for element %d' % sxtnum) for i in range(len(posrange)):