def test_orbit(f): orm2 = hla.measorm.Orm([], []) if not os.path.exists(f): return True orm2.load(f) print orm2 print "delay: ", orm2.TSLEEP npoint, nbpm, ntrim = np.shape(orm2._rawmatrix) for i in range(5): #itrim = np.random.randint(ntrim) itrim = 0 while True: ibpm = np.random.randint(nbpm) if orm2.trim[itrim][1] == orm2.bpm[ibpm][1]: break #print hla.getOrbit() bpmrb = orm2.bpm[ibpm][2] trimsp = orm2.trim[itrim][3] x0 = caget(bpmrb) k = caget(trimsp) dk = 1e-4 caput(trimsp, k + dk) time.sleep(orm2.TSLEEP) dx = orm2.m[ibpm, itrim] * dk x1 = caget(bpmrb) print trimsp, "% .2e" % k, bpmrb, \ "% .4e % .4e % .4e % .4e" % (x0, x1, x1-x0, dx) caput(trimsp, k) time.sleep(orm2.TSLEEP) plt.clf() plt.plot(orm2._rawkick[itrim, 1:-1], orm2._rawmatrix[1:-1, ibpm, itrim], '-o') plt.plot([k, k + dk], [x0, x1], '-x') plt.grid(True) plt.title("%s.%s/%s.%s" % (orm2.bpm[ibpm][0], orm2.bpm[ibpm][1], orm2.trim[itrim][0], orm2.trim[itrim][1])) plt.savefig('orm-test-%03d.png' % i) return True plt.clf() plt.plot(ratio, '-o') plt.ylabel("[(x1-x0)-dx]/x0") plt.savefig("orm-orbit-reproduce-1.png") plt.clf() plt.plot(x0, '--', label='orbit 0') plt.plot(x1, '-', label='orbit 1') plt.plot(x0 + dx, 'x', label='ORM predict') plt.ylabel("orbit") plt.savefig("orm-orbit-reproduce-2.png") for i in range(len(bpm)): if x0[i] + dx[i] - x1[i] > 1e-4: print "Not agree well:", i, bpm[i], x0[i] + dx[i], x1[i] print "Done", time.time()
def test_orbit(f): orm2 = hla.measorm.Orm([], []) if not os.path.exists(f): return True orm2.load(f) print orm2 print "delay: ", orm2.TSLEEP npoint, nbpm, ntrim = np.shape(orm2._rawmatrix) for i in range(5): #itrim = np.random.randint(ntrim) itrim = 0 while True: ibpm = np.random.randint(nbpm) if orm2.trim[itrim][1] == orm2.bpm[ibpm][1]: break #print hla.getOrbit() bpmrb = orm2.bpm[ibpm][2] trimsp = orm2.trim[itrim][3] x0 = caget(bpmrb) k = caget(trimsp) dk = 1e-4 caput(trimsp, k + dk) time.sleep(orm2.TSLEEP) dx = orm2.m[ibpm,itrim]*dk x1 = caget(bpmrb) print trimsp, "% .2e" % k, bpmrb, \ "% .4e % .4e % .4e % .4e" % (x0, x1, x1-x0, dx) caput(trimsp, k) time.sleep(orm2.TSLEEP) plt.clf() plt.plot(orm2._rawkick[itrim, 1:-1], orm2._rawmatrix[1:-1,ibpm,itrim], '-o') plt.plot([k, k+dk], [x0, x1], '-x') plt.grid(True) plt.title("%s.%s/%s.%s" % (orm2.bpm[ibpm][0], orm2.bpm[ibpm][1], orm2.trim[itrim][0], orm2.trim[itrim][1])) plt.savefig('orm-test-%03d.png' % i) return True plt.clf() plt.plot(ratio, '-o') plt.ylabel("[(x1-x0)-dx]/x0") plt.savefig("orm-orbit-reproduce-1.png") plt.clf() plt.plot(x0, '--', label='orbit 0') plt.plot(x1, '-', label='orbit 1') plt.plot(x0+dx, 'x', label='ORM predict') plt.ylabel("orbit") plt.savefig("orm-orbit-reproduce-2.png") for i in range(len(bpm)): if x0[i]+dx[i] - x1[i] > 1e-4: print "Not agree well:", i,bpm[i], x0[i]+dx[i], x1[i] print "Done", time.time()
def correct_orbit(f): orm = hla.orm.Orm([], []) if not os.path.exists(f): return True orm.load(f) hla.reset_trims() time.sleep(5) npoint, nbpm, ntrim = np.shape(orm._rawmatrix) bpm = [] for c in range(10): cc = "C%02d" % c b = hla.getGroupMembers(['*', 'BPMX', cc], op='intersection') print cc, b bpm.extend(b) print len(bpm), bpm ntrim_used = ntrim m = np.zeros((len(bpm), ntrim_used), 'd') sel = [0] * nbpm bpmpv = [] bpmfullpv = [b[2] for i, b in enumerate(orm.bpm)] for i, b in enumerate(orm.bpm): if not b[0] in bpm: continue sel[i] = 1 bpmpv.append(b[2]) v0 = np.array(hla.caget(bpmfullpv)) v = np.array(hla.caget(bpmpv)) plt.clf() plt.plot(v0, 'r-') plt.plot(v, 'ro') plt.savefig("orbit-00.png") m = np.compress(sel, orm.m[:, :ntrim_used], axis=0) print np.shape(m) #u,s,vh = np.linalg.svd(m) #print u, s, vh dk, resids, rank, s = np.linalg.lstsq(m, -1.0 * v) print dk trimpv = [t[3] for i, t in enumerate(orm.trim)] print[t[0] for i, t in enumerate(orm.trim)] hla.caput(trimpv[:ntrim_used], dk) for i in range(1, 10): time.sleep(2) plt.clf() v1 = np.array(hla.caget(bpmfullpv)) plt.plot(v1, '-o') plt.savefig("orbit-%02d.png" % i)
def correct_orbit(f): orm = hla.orm.Orm([], []) if not os.path.exists(f): return True orm.load(f) hla.reset_trims() time.sleep(5) npoint, nbpm, ntrim = np.shape(orm._rawmatrix) bpm = [] for c in range(10): cc = "C%02d" % c b = hla.getGroupMembers(['*', 'BPMX', cc], op='intersection') print cc,b bpm.extend(b) print len(bpm), bpm ntrim_used = ntrim m = np.zeros((len(bpm), ntrim_used), 'd') sel = [0] * nbpm bpmpv = [] bpmfullpv = [b[2] for i,b in enumerate(orm.bpm)] for i,b in enumerate(orm.bpm): if not b[0] in bpm: continue sel[i] = 1 bpmpv.append(b[2]) v0 = np.array(hla.caget(bpmfullpv)) v = np.array(hla.caget(bpmpv)) plt.clf() plt.plot(v0, 'r-') plt.plot(v, 'ro') plt.savefig("orbit-00.png") m = np.compress(sel, orm.m[:,:ntrim_used], axis=0) print np.shape(m) #u,s,vh = np.linalg.svd(m) #print u, s, vh dk, resids, rank, s = np.linalg.lstsq(m, -1.0*v) print dk trimpv = [t[3] for i,t in enumerate(orm.trim)] print [t[0] for i,t in enumerate(orm.trim)]
plt.plot(v, 'ro') plt.savefig("orbit-00.png") m = np.compress(sel, orm.m[:,:ntrim_used], axis=0) print np.shape(m) #u,s,vh = np.linalg.svd(m) #print u, s, vh dk, resids, rank, s = np.linalg.lstsq(m, -1.0*v) print dk trimpv = [t[3] for i,t in enumerate(orm.trim)] print [t[0] for i,t in enumerate(orm.trim)] hla.caput(trimpv[:ntrim_used], dk) for i in range(1,10): time.sleep(2) plt.clf() v1 = np.array(hla.caget(bpmfullpv)) plt.plot(v1, '-o') plt.savefig("orbit-%02d.png" % i) if __name__ == "__main__": #filter_orm('../test/dat/orm-full-0179.pkl') #filter_orm('../test/dat/orm-full-0181.pkl') #filter_orm('../test/dat/orm-full.pkl') #filter_orm('../test/dat/orm-sub1.pkl') #mask_orm('../test/dat/orm-full-0179.pkl', [52, 90, 141, 226, 317, 413]) #merge_orm('../test/dat/orm-full-0179.pkl', # '../test/dat/orm-full-0181.pkl') #merge_orm('../test/dat/orm-full-0181.pkl', 'orm.pkl') #test_orbit('../test/dat/orm-full-0181.pkl')