Example #1
0
File: a.py Project: mpyliyj/sqmat
def cirDist(k2s):
	'''
	circle distortion
	'''
	npass = 64

	norder = 7
	tol = 1e-12

	sexts = ring.matchElements('sh1.*')
	for s in sexts:
		ring[s,'K2'] = k2s[0]
	sexts = ring.matchElements('sh3.*')
	for s in sexts:
		ring[s,'K2'] = k2s[1]
	sexts = ring.matchElements('sh4.*')
	for s in sexts:
		ring[s,'K2'] = k2s[2]
	sexts = ring.matchElements('sl3.*')
	for s in sexts:
		ring[s,'K2'] = k2s[3]
	sexts = ring.matchElements('sl2.*')
	for s in sexts:
		ring[s,'K2'] = k2s[4]
	sexts = ring.matchElements('sl1.*')
	for s in sexts:
		ring[s,'K2'] = k2s[5]
		
        # six phase space varialbe, 4 independent, expand to n'rd order.
	m = tesla.TPSMap(6,4,norder)
	m.c = [0, 0, 0, 0, 0, 0]
	m.m = [[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1], [0,0,0,0],[0,0,0,0]]
	ms = ring.trackTPSMaps(m, 0, ring.elements()) #ms has the maps of all elements around the ring, m is one turn map
	tw, ml = tesla.calcLinearTwiss(ring)

        #4. Construct map matrix mm
	mf,powerindex = sqdf.mfunction(m,norder) # m is one turn map
        #This program only applies when x[0]=0,xp[0]=0,y[0]=0, yp[0]=0. If not, then the map matrix mm is no longer semi-triangular
	mf[0][0] = 0
	mf[1][0] = 0
	mf[2][0] = 0
	mf[3][0] = 0
	mf = np.array(mf)

	mlen=len(powerindex)

        # sequencenumber[i1,i2,i3,i4] gives the sequence number in power index for power of x^i1*xp^i2*y^i3*yp^i4
	sequencenumber = np.zeros((norder+1,norder+1,norder+1,norder+1),'i')
	powerindex = sqdf.powerindex4(norder)
	powerindex = np.array(powerindex,'i')
	mlen = len(powerindex)

	for i in range(mlen):
		ip = powerindex[i]
		sequencenumber[ip[0]][ip[1]][ip[2]][ip[3]] = i

	betax0,phix0,alphax0,betay0,phiy0,alphay0 = sqdf.extracttwiss(mf)
	gammax0 = (1+alphax0**2)/betax0
	mlix = [np.cos(phix0)+alphax0*np.sin(phix0), betax0*np.sin(phix0)],\
	    [-gammax0*np.sin(phix0), np.cos(phix0)-alphax0*np.sin(phix0)]
	gammay0 = (1+alphay0**2)/betay0
	mliy = [np.cos(phiy0)+alphay0*np.sin(phiy0), betay0*np.sin(phiy0)],\
	    [-gammay0*np.sin(phiy0), np.cos(phiy0)-alphay0*np.sin(phiy0)]

	elemtx = np.zeros((4,4))
	elemtx[0:2,0:2] = mlix #Replace linear part of elegant, which is accurate only to 8 digits, 
        #by the twiss matrix obtained from the twiss parameters extract from the tpsa linear part of matrix
	elemtx[2:4,2:4] = mliy #Notice that mlix and mliy is accurate simplex to machine precision, so their determinantes are closer to zero.

	mf[:,1:5] = elemtx #Now replace the linear part of map matrix by the more accurate twiss matrix

	sqrtbetax = np.sqrt(betax0)
	sqrtbetay = np.sqrt(betay0)

        #5. Construct the BK square matrix using the first 5 rows.
	bK,bKi = sqdf.BKmatrix(betax0,phix0,alphax0,\
			       betay0,phiy0,alphay0,
			       0,0,norder,powerindex,sequencenumber,tol)

        #6. Derive normalized map M=(BK)^(-1).mm.BK, see my notes 'Relation to Normal Form' of Wednesday, March 30, 2011 10:34 PM
        #   here #mfbk is the first 4 rows of M, it is 
	mfbk = jfdf.d3(bKi[1:5,1:5],mf,bK) 
    
	mfbk,scalemf,As,Asm = scalingmf(mfbk,powerindex)

	Ms = sqdf.squarematrix(mfbk,norder,powerindex,sequencenumber,tol) 

	Bi = np.array([[1/sqrtbetax,0,0,0],[alphax0/sqrtbetax,sqrtbetax,0,0],
		       [0,0,1/sqrtbetay,0],[0,0,alphay0/sqrtbetay,sqrtbetay]])
	Ki = np.array([[1,-1j,0,0],[1,1j,0,0],[0,0,1,-1j],[0,0,1,1j]]) 
	bKi4b4 = np.dot(Ki,Bi)

	try:
	    ux1,uxbar,Jx,scalex,Msx,As2x,Asm2x = \
		jfdf.UMsUbarexpJ(Ms,phix0,1,powerindex,scalemf,sequencenumber[1,0,0,0],ypowerorder=norder)
	    uy1,uybar,Jy,scaley,Msy,As2y,Asm2y = \
		jfdf.UMsUbarexpJ(Ms,phiy0,1,powerindex,scalemf,sequencenumber[0,0,1,0],ypowerorder=norder)
	except:
            return [1e8]*8

	# --- particle 1
	x0 = 2.5e-2
	y0 = 5e-3
	xxpyyp = np.zeros((4,npass+1))
	xxpyyp[:,0] = np.array([x0,0,y0,0])
	for i in xrange(1,npass+1):
		xxpyyp[:,i] = np.dot(elemtx,xxpyyp[:,i-1])
	xybar = np.dot(Bi,xxpyyp)
	xy = zip(*xybar)
	section = []
	for row in xy:
		section.append(normalcoordinate(*row))
	st = zip(*section)
        #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
	section1 = [] #section1 is without joined blocks
	xy = zip(*xxpyyp)
	for row in xy:
		row1 = row+(ux1,uy1)
		section1.append(tuneshift(row1[0],row1[1],row1[2],row1[3],row1[4],row1[5],
					  bKi4b4,scalex,scaley,powerindex,norder))
	st1 = zip(*section1)
	#print 'K2s = %.2f:'%k2s
	zmax = max(st1[0])
	zmin = min(st1[0])
	zav = np.mean(st1[0])
	zx1 = (zmax-zmin)/zav
	#print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
	zmax = max(st1[1])
	zmin = min(st1[1])
	zav = np.mean(st1[1])
	zy1 = (zmax-zmin)/zav
	#print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
	#print ''
	
	# --- particle 2
	x0 = 1.0e-2
	y0 = 2e-3
	xxpyyp = np.zeros((4,npass+1))
	xxpyyp[:,0] = np.array([x0,0,y0,0])
	for i in xrange(1,npass+1):
		xxpyyp[:,i] = np.dot(elemtx,xxpyyp[:,i-1])
	xybar = np.dot(Bi,xxpyyp)
	xy = zip(*xybar)
	section = []
	for row in xy:
		section.append(normalcoordinate(*row))
	st = zip(*section)
        #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
	section1 = [] #section1 is without joined blocks
	xy = zip(*xxpyyp)
	for row in xy:
		row1 = row+(ux1,uy1)
		section1.append(tuneshift(row1[0],row1[1],row1[2],row1[3],row1[4],row1[5],
					  bKi4b4,scalex,scaley,powerindex,norder))
	st1 = zip(*section1)
	#print 'K2s = %.2f:'%k2s
	zmax = max(st1[0])
	zmin = min(st1[0])
	zav = np.mean(st1[0])
	zx2 = (zmax-zmin)/zav
	#print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
	zmax = max(st1[1])
	zmin = min(st1[1])
	zav = np.mean(st1[1])
	zy2 = (zmax-zmin)/zav
	#print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
	#print ''

	# --- particle 3
	x0 = 3.5e-2
	y0 = 3.0e-3
	xxpyyp = np.zeros((4,npass+1))
	xxpyyp[:,0] = np.array([x0,0,y0,0])
	for i in xrange(1,npass+1):
		xxpyyp[:,i] = np.dot(elemtx,xxpyyp[:,i-1])
	xybar = np.dot(Bi,xxpyyp)
	xy = zip(*xybar)
	section = []
	for row in xy:
		section.append(normalcoordinate(*row))
	st = zip(*section)
        #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
	section1 = [] #section1 is without joined blocks
	xy = zip(*xxpyyp)
	for row in xy:
		row1 = row+(ux1,uy1)
		section1.append(tuneshift(row1[0],row1[1],row1[2],row1[3],row1[4],row1[5],
					  bKi4b4,scalex,scaley,powerindex,norder))
	st1 = zip(*section1)
	#print 'K2s = %.2f:'%k2s
	zmax = max(st1[0])
	zmin = min(st1[0])
	zav = np.mean(st1[0])
	zx3 = (zmax-zmin)/zav
	#print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
	zmax = max(st1[1])
	zmin = min(st1[1])
	zav = np.mean(st1[1])
	zy3 = (zmax-zmin)/zav
	#print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
	#print ''

	xlist = np.linspace(-3e-2,3e-2,30) #a list of theta0
	ylist = np.arange(1e-6,1.0e-2,10) #a list of theta0
	xyplane = [[i,j] for i in xlist for j in ylist]

	nu,da = [],[]
	for x,y in xyplane:
		t1,t2,t3,t4,t5,t6,t7,t8 = tuneshift(x,0,y,0,ux1,uy1,
						    bKi4b4,scalex,scaley,powerindex,norder)
		nu.append([t5,t6])
		da.append([t7,t8])

	da = np.array(da)
	nux = [i[0].real/2/np.pi for i in nu]
	nuy = [i[1].real/2/np.pi for i in nu]
	
	dnuxda = np.max(np.abs(nux))
	dnuyda = np.max(np.abs(nuy))
	dax = np.max(np.abs(da[:,0]))
	day = np.max(np.abs(da[:,1]))
	return [zx1,zy1,zx2,zy2,zx3,zy3,dnuxda,dnuyda,dax,day]
Example #2
0
alphax0,alphay0 = nsls2.ring.alfax[0],nsls2.ring.alfay[0]
phix0,phiy0 = nsls2.ring.nux*np.pi*2,nsls2.ring.nuy*np.pi*2
sqrtbetax = np.sqrt(betax0)
sqrtbetay = np.sqrt(betay0)

mlen = len(powerindex)
# sequencenumber[i1,i2,i3,i4] gives the sequence number in power index for power of x^i1*xp^i2*y^i3*yp^i4
sequencenumber = np.zeros((norder+1,norder+1,norder+1,norder+1),'i')

for i in xrange(mlen):
    ipi = powerindex[i]
    sequencenumber[ipi[0]][ipi[1]][ipi[2]][ipi[3]] = i

#5. Construct the BK square matrix using the first 5 rows.
bK,bKi = sqdf.BKmatrix(betax0,phix0,alphax0,\
                       betay0,phiy0,alphay0,
                       0,0,norder,powerindex,sequencenumber,tol)

Bi = np.array([[1/sqrtbetax,0,0,0],[alphax0/sqrtbetax,sqrtbetax,0,0],
               [0,0,1/sqrtbetay,0],[0,0,alphay0/sqrtbetay,sqrtbetay]])
B = np.array([[sqrtbetax,0,0,0],[-alphax0/sqrtbetax,1/sqrtbetax,0,0],
              [0,0,sqrtbetay,0],[0,0,-alphay0/sqrtbetay,1/sqrtbetay]])
Ki = np.array([[1,-1j,0,0],[1,1j,0,0],[0,0,1,-1j],[0,0,1,1j]]) 
bKi4b4 = np.dot(Ki,Bi)


def Jxy(x,y,Bi,B,n=64):
    '''
    here Jx = sqrt(2*Jx (as SY Lee book)) 
    '''
    Jx = np.sqrt(Bi[0,0]*Bi[0,0]+Bi[1,0]*Bi[1,0])*x
Example #3
0
File: d.py Project: mpyliyj/sqmat
def cirDist(k2s):
    '''
	circle distortion
	'''
    npass = 64

    norder = 7

    # slow but general ---
    '''
	sext = nsls2.ring.getElements('sext','sh1')[0]
        sext.put('K2',k2s[0])
	sext = nsls2.ring.getElements('sext','sh3')[0]
        sext.put('K2',k2s[1])
	sext = nsls2.ring.getElements('sext','sh4')[0]
        sext.put('K2',k2s[2])
	sext = nsls2.ring.getElements('sext','sl3')[0]
        sext.put('K2',k2s[3])
	sext = nsls2.ring.getElements('sext','sl2')[0]
        sext.put('K2',k2s[4])
	sext = nsls2.ring.getElements('sext','sl1')[0]
        sext.put('K2',k2s[5])

        K2 = [nsls2.ring.bl[k].K2 for k in nsls2.ring.klist]
        '''
    # ---fast but not general, only for nsls2-II chrom+7/+7
    sm12 = [-26.21867845, 30.69679902, -28.01658206]
    K2 = np.hstack((k2s[:3], sm12, k2s[3:], k2s[-1:-4:-1], sm12, k2s[-4::-1]))

    # --- on momentum 0%
    temp = copy.deepcopy(unit)
    for i, k in enumerate(nsls2.ring.klist):
        temp = sm.lmPass(nsls2.ring.mlist[i][:nv, :nv], temp)
        temp = sm.thickSextPass(L[i], K2[i], 1, temp, truncate=norder)
    temp = sm.lmPass(nsls2.ring.mlist[-1][:nv, :nv], temp)
    mf = np.array([sm.aline(temp[i], powerindex) for i in xrange(nv)])

    #print mf[0],mf.shape
    elemtx = mf[:, 1:5]

    #6. Derive normalized map M=(BK)^(-1).mm.BK, see my notes 'Relation to Normal Form' of Wednesday, March 30, 2011 10:34 PM
    #   here #mfbk is the first 4 rows of M, it is
    mfbk = jfdf.d3(bKi[1:5, 1:5], mf, bK)

    mfbk, scalemf, As, Asm = scalingmf(mfbk, powerindex)
    #print mfbk[0]

    Ms = sqdf.squarematrix(mfbk, norder, powerindex, sequencenumber, tol)

    try:
        #print Ms.shape,Ms[-1],phix0,1,powerindex,scalemf,sequencenumber[1,0,0,0],norder
        ux1,uxbar,Jx,scalex,Msx,As2x,Asm2x = \
                    jfdf.UMsUbarexpJ(Ms,phix0,1,powerindex,scalemf,sequencenumber[1,0,0,0],ypowerorder=norder)
        uy1,uybar,Jy,scaley,Msy,As2y,Asm2y = \
                    jfdf.UMsUbarexpJ(Ms,phiy0,1,powerindex,scalemf,sequencenumber[0,0,1,0],ypowerorder=norder)
    except:
        #print '0 error'
        return [1e8] * 22

    # --- particle 1
    x0 = 2.5e-2
    y0 = 5e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bi, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 2
    x0 = 1.0e-2
    y0 = 2e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bi, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx2 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy2 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 3
    x0 = 3.5e-2
    y0 = 3.0e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bi, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx3 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy3 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # -1%
    delta = -0.0
    K2n1 = K2 / (1 + delta)
    temp = copy.deepcopy(unit)
    for i, k in enumerate(nsls2n1.ring.klist):
        temp = sm.lmPass(nsls2n1.ring.mlist[i][:nv, :nv], temp)
        temp = sm.addDispersive(temp, delta, displist[i])
        temp = sm.thickSextPass(L[i], K2n1[i], 1, temp, truncate=norder)
    temp = sm.lmPass(nsls2n1.ring.mlist[-1][:nv, :nv], temp)
    mf = np.array([sm.aline(temp[i], powerindex) for i in xrange(nv)])
    # ---  remove nonzero fixed point
    for i1 in xrange(len(mf)):
        mf[i1, 0] = 0.
    elemtx = mf[:, 1:5]

    nux, betax0n1, alphax0n1, gx = latt.matrix2twiss(elemtx, plane='x')
    nuy, betay0n1, alphay0n1, gy = latt.matrix2twiss(elemtx, plane='x')
    phix0n1, phiy0n1 = nux * np.pi * 2, nuy * np.pi * 2
    sqrtbetaxn1 = np.sqrt(betax0n1)
    sqrtbetayn1 = np.sqrt(betay0n1)
    bKn1,bKin1 = sqdf.BKmatrix(betax0n1,phix0n1,alphax0n1,\
                               betay0n1,phiy0n1,alphay0n1,
                               0,0,norder,powerindex,sequencenumber,tol)
    Bin1 = np.array([[1 / sqrtbetaxn1, 0, 0, 0],
                     [alphax0n1 / sqrtbetaxn1, sqrtbetaxn1, 0, 0],
                     [0, 0, 1 / sqrtbetayn1, 0],
                     [0, 0, alphay0n1 / sqrtbetayn1, sqrtbetayn1]])
    bKi4b4n1 = np.dot(Ki, Bin1)

    #6. Derive normalized map M=(BK)^(-1).mm.BK, see my notes 'Relation to Normal Form' of Wednesday, March 30, 2011 10:34 PM
    #   here #mfbk is the first 4 rows of M, it is
    mfbk = jfdf.d3(bKin1[1:5, 1:5], mf, bKn1)
    mfbk, scalemf, As, Asm = scalingmf(mfbk, powerindex)
    Ms = sqdf.squarematrix(mfbk, norder, powerindex, sequencenumber, tol)
    try:
        ux1,uxbar,Jx,scalex,Msx,As2x,Asm2x = \
     jfdf.UMsUbarexpJ(Ms,phix0n1,1,powerindex,scalemf,sequencenumber[1,0,0,0],ypowerorder=norder)
        uy1,uybar,Jy,scaley,Msy,As2y,Asm2y = \
     jfdf.UMsUbarexpJ(Ms,phiy0n1,1,powerindex,scalemf,sequencenumber[0,0,1,0],ypowerorder=norder)
    except:
        #print '-1 error'
        return [1e8] * 22
    # --- particle 1
    x0 = 2.5e-2
    y0 = 5e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bin1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4n1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx1n1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy1n1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 2
    x0 = 1.0e-2
    y0 = 2e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bin1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4n1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx2n1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy2n1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 3
    x0 = 3.5e-2
    y0 = 3.0e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bin1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4n1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx3n1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy3n1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''
    # --- end of -1%

    # +1%
    delta = 0.01
    K2p1 = K2 / (1 + delta)
    temp = copy.deepcopy(unit)
    for i, k in enumerate(nsls2p1.ring.klist):
        temp = sm.lmPass(nsls2p1.ring.mlist[i][:nv, :nv], temp)
        temp = sm.addDispersive(temp, delta, displist[i])
        temp = sm.thickSextPass(L[i], K2p1[i], 1, temp, truncate=norder)
    temp = sm.lmPass(nsls2p1.ring.mlist[-1][:nv, :nv], temp)
    mf = np.array([sm.aline(temp[i], powerindex) for i in xrange(nv)])
    for i1 in xrange(len(mf)):
        mf[i1, 0] = 0.
    elemtx = mf[:, 1:5]
    nux, betax0p1, alphax0p1, gx = latt.matrix2twiss(elemtx, plane='x')
    nuy, betay0p1, alphay0p1, gy = latt.matrix2twiss(elemtx, plane='x')
    phix0p1, phiy0p1 = nux * np.pi * 2, nuy * np.pi * 2
    sqrtbetaxp1 = np.sqrt(betax0p1)
    sqrtbetayp1 = np.sqrt(betay0p1)
    bKp1,bKip1 = sqdf.BKmatrix(betax0p1,phix0p1,alphax0p1,\
                               betay0p1,phiy0p1,alphay0p1,
                               0,0,norder,powerindex,sequencenumber,tol)
    Bip1 = np.array([[1 / sqrtbetaxp1, 0, 0, 0],
                     [alphax0p1 / sqrtbetaxp1, sqrtbetaxp1, 0, 0],
                     [0, 0, 1 / sqrtbetayp1, 0],
                     [0, 0, alphay0p1 / sqrtbetayp1, sqrtbetayp1]])
    bKi4b4p1 = np.dot(Ki, Bip1)

    #6. Derive normalized map M=(BK)^(-1).mm.BK, see my notes 'Relation to Normal Form' of Wednesday, March 30, 2011 10:34 PM
    #   here #mfbk is the first 4 rows of M, it is
    mfbk = jfdf.d3(bKip1[1:5, 1:5], mf, bKp1)
    mfbk, scalemf, As, Asm = scalingmf(mfbk, powerindex)
    Ms = sqdf.squarematrix(mfbk, norder, powerindex, sequencenumber, tol)
    try:
        ux1,uxbar,Jx,scalex,Msx,As2x,Asm2x = \
     jfdf.UMsUbarexpJ(Ms,phix0p1,1,powerindex,scalemf,sequencenumber[1,0,0,0],ypowerorder=norder)
        uy1,uybar,Jy,scaley,Msy,As2y,Asm2y = \
     jfdf.UMsUbarexpJ(Ms,phiy0p1,1,powerindex,scalemf,sequencenumber[0,0,1,0],ypowerorder=norder)
    except:
        #print '+1 error'
        return [1e8] * 22
    # --- particle 1
    x0 = 2.5e-2
    y0 = 5e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bip1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    sectiop1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4p1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx1p1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy1p1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 2
    x0 = 1.0e-2
    y0 = 2e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bip1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4p1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx2p1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy2p1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''

    # --- particle 3
    x0 = 3.5e-2
    y0 = 3.0e-3
    xxpyyp = np.zeros((4, npass + 1))
    xxpyyp[:, 0] = np.array([x0, 0, y0, 0])
    for i in xrange(1, npass + 1):
        xxpyyp[:, i] = np.dot(elemtx, xxpyyp[:, i - 1])
    xybar = np.dot(Bip1, xxpyyp)
    xy = zip(*xybar)
    section = []
    for row in xy:
        section.append(normalcoordinate(*row))
    st = zip(*section)
    #11. Prepare data for |wx|,thetax,|wy|,thetay to plot Poincare section of thetax,|wy|,thetay
    section1 = []  #section1 is without joined blocks
    xy = zip(*xxpyyp)
    for row in xy:
        row1 = row + (ux1, uy1)
        section1.append(
            tuneshift(row1[0], row1[1], row1[2], row1[3], row1[4], row1[5],
                      bKi4b4p1, scalex, scaley, powerindex, norder))
    st1 = zip(*section1)
    #print 'K2s = %.2f:'%k2s
    zmax = max(st1[0])
    zmin = min(st1[0])
    zav = np.mean(st1[0])
    zx3p1 = (zmax - zmin) / zav
    #print "for wx1 without resonance block,  (zmax-zmin)/zmean = ", zx1
    zmax = max(st1[1])
    zmin = min(st1[1])
    zav = np.mean(st1[1])
    zy3p1 = (zmax - zmin) / zav
    #print "for wy1 without resonance block,  (zmax-zmin)/zmean = ", zy1
    #print ''
    # --- end of +1%

    xlist = np.linspace(-3e-2, 3e-2, 30)  #a list of theta0
    ylist = np.arange(1e-6, 1.0e-2, 10)  #a list of theta0
    xyplane = [[i, j] for i in xlist for j in ylist]

    nu, da = [], []
    for x, y in xyplane:
        t1, t2, t3, t4, t5, t6, t7, t8 = tuneshift(x, 0, y, 0, ux1, uy1,
                                                   bKi4b4, scalex, scaley,
                                                   powerindex, norder)
        nu.append([t5, t6])
        da.append([t7, t8])

    da = np.array(da)
    nux = [i[0].real / 2 / np.pi for i in nu]
    nuy = [i[1].real / 2 / np.pi for i in nu]

    dnuxda = np.max(np.abs(nux))
    dnuyda = np.max(np.abs(nuy))
    dax = np.max(np.abs(da[:, 0]))
    day = np.max(np.abs(da[:, 1]))
    return [
        zx1, zy1, zx2, zy2, zx3, zy3, zx1n1, zy1n1, zx2n1, zy2n1, zx3n1, zy3n1,
        zx1p1, zy1p1, zx2p1, zy2p1, zx3p1, zy3p1, dnuxda, dnuyda, dax, day
    ]