Commit 746e58ad authored by Andreas Freise's avatar Andreas Freise
Browse files

fixing various bugs in asc_test example

parent 0c56ecc3
......@@ -6,7 +6,6 @@ from __future__ import unicode_literals
from pykat import finesse
from pykat.commands import *
import copy
#import shelve
import pickle
import sys
import scipy.optimize
......@@ -41,11 +40,8 @@ def main():
kat.maxtem=3
Lambda=1064.0e-9
result = {}
# defining variables as global for debugging
#global kat
#global out
#global result
#global kat, out, result
print("--------------------------------------------------------")
print(" 1. tunes ETM position to find resonance")
......@@ -67,13 +63,13 @@ def main():
scale 2 PDrefl_q
"""
kat.parseKatCode(code_det)
kat.PDrefl_p.phi1=result['p_phase']
kat.PDrefl_q.phi1=result['q_phase']
kat.PDrefl_p.phase1=result['p_phase']
kat.PDrefl_q.phase1=result['q_phase']
print("--------------------------------------------------------")
print(" 4. adding a 0.1nm offset to ETM and compute PDH signal")
result['phi_tuned']=float(kat.ETM.phi)
result['phi_detuned'] = result['phi_tuned'] + 0.1/1064.0*360
result['phi_detuned'] = result['phi_tuned'] + 0.1*360.0/1064.0
kat.ETM.phi=result['phi_detuned']
print(" new ETM phi tuning = %g " % kat.ETM.phi)
......@@ -92,17 +88,12 @@ def main():
kat.saveScript(tmpkatfile)
with open(tmpresultfile, 'wb') as handle:
pickle.dump(result, handle)
# now the result variables (former version usuing shelve):
#tmpfile = shelve.open(tmpresultfile, flag="c")
#tmpfile[str('result')]=result
#tmpfile.close()
#---------------------------------------------------------------------------
def pd_signal(tmpkat):
kat = copy.deepcopy(tmpkat)
code1="""
pd cav nITM2
yaxis abs
......@@ -111,13 +102,12 @@ def pd_signal(tmpkat):
kat.noxaxis = True
global out
out = kat.run()
print(" Cavity power: {0:.6f}W".format(out.y[0,2]))
return (out.y[0,0], out.y[0,1])
print(" Cavity power: {0:.6f}W".format(float(out['cav'])))
return (float(out['PDrefl_p']), float(out['PDrefl_q']))
def pd_phase(tmpkat):
kat = copy.deepcopy(tmpkat)
code_det = """
pd1 PDrefl_q 9M 90 nWFS1
"""
......@@ -129,15 +119,13 @@ def pd_phase(tmpkat):
def PD_q_test(x):
kat.PDrefl_q.phase1=x
out = kat.run()
print('\r root finding: function value %g ' % out.y, end=' ')
print('\r root finding: function value {0:<16g}'.format(float(out.y)), end='')
sys.stdout.flush()
return out.y
return float(out.y)
# do root finding
xtol=1e-8
#print("Starting values for bisect are: %e and %e \n" % (PD_q_test(60.0),PD_q_test(100.0)))
(result, info)=scipy.optimize.bisect(PD_q_test,80.0,100.0, xtol=xtol, maxiter=500, full_output=True)
print("")
......@@ -175,9 +163,7 @@ def powers(tmpkat):
global out
out = kat.run()
code1 = code1.split("\n")
for i in range(len(out.y)):
for i in range(len(out.y[0])):
print(" %8s: %.4e" % (out.ylabels[i], out.y[0,i]))
......@@ -195,10 +181,9 @@ def resonance(tmpkat):
# function for root finding
def carrier_resonance(x):
kat.ETM.phi=x
global out
out = kat.run()
phase = (out.y[0,0]-out.y[0,1]-90)%360-180
print('\r root finding: function value %g ' % phase, end=' ')
phase = (out.y[0,0]-out.y[0,1]-90)%360-180.0
print('\r root finding: function value {0:<16g}'.format(float(phase)), end='')
sys.stdout.flush()
return phase
......
......@@ -10,7 +10,6 @@ import scipy
from scipy.optimize import fmin
import numpy as np
import pickle
#import shelve
import copy
import sys
......@@ -41,8 +40,7 @@ def main():
# %reset -f
# making these global during testing and debugging
#global kat
#global out
#global kat, out
kat = finesse.kat(tempdir=".",tempname="test")
kat.verbose = False
......@@ -52,9 +50,6 @@ def main():
# loading data saved by master.py
kat.loadKatFile('asc_base2.kat')
try:
#tmpfile = shelve.open(tmpresultfile, flag="c")
#result=tmpfile[str('result')]
#tmpfile.close()
with open(tmpresultfile, 'rb') as handle:
result = pickle.load(handle)
except: raise Exception("Could not open temprary results file {0}".format(tmpresultfile))
......@@ -73,7 +68,7 @@ def main():
print("--------------------------------------------------------")
print(" 5. checking wavefronts for ITM/ETM tilt of 0.1nrad")
tilt(kat)
print("--------------------------------------------------------")
print(" 6. compute beam tilt from beam propogation")
gravity_tilt(kat)
......@@ -117,7 +112,7 @@ def main():
print("--------------------------------------------------------")
print(" Saving results in temp. files to be read by master3.py")
# re-enable PDH photo diode for savinf
# re-enable PDH photo diode for saving of kat file for next script
kat.PDrefl_p.enabled = True
kat.PDrefl_q.enabled = True
......@@ -125,12 +120,7 @@ def main():
tmpresultfile = "myshelf2.dat"
print(" kat object saved in: {0}".format(tmpkatfile))
print(" current results saved in: {0}".format(tmpresultfile))
# first the current kat file
kat.saveScript(tmpkatfile)
# now the result variables:
#tmpfile = shelve.open(tmpresultfile)
#tmpfile[str('result')]=result
#tmpfile.close()
with open(tmpresultfile, 'wb') as handle:
pickle.dump(result, handle)
......@@ -222,11 +212,12 @@ def gravity_tilt(tmpkat):
kat = copy.deepcopy(tmpkat)
def compute_gravity_tilt(tmpkat):
#global out, y1, y2
kat = copy.deepcopy(tmpkat)
out = kat.run()
y1 = out["b1"]
y2 = out["b1_1k"]
y1 = np.abs(out["b1"])
y2 = np.abs(out["b1_1k"])
# shift of beam center on detector 1 (as m/w0y)
x1 = np.sum(out.x*y1)/np.sum(y1)
# shift of beam center on detector 2 (as m/w0y)
......@@ -236,7 +227,7 @@ def gravity_tilt(tmpkat):
w0=out["w0y"][0]
detector_distance = 1000.0
tilt=w0*(x2-x1)/detector_distance
print(" Wavefront tilt : %g nrad" % tilt)
print(" Wavefront tilt : %g rad" % tilt)
code_WFS1 = """
beam b1 nWFS1
......@@ -258,20 +249,20 @@ def gravity_tilt(tmpkat):
yaxis abs
"""
print(" WFS1:")
print(" ITM ybeta 0.1nm")
print(" ITM ybeta 0.1nrad")
kat.parseKatCode(code_WFS1)
kat.parseKatCode(code_xaxis)
kat.spo1.L=1000.0
kat.ITM.ybeta=1e-10
kat.ETM.ybeta=0.0
compute_gravity_tilt(kat)
print(" ETM ybeta -0.1nm")
print(" ETM ybeta -0.1nrad")
kat.ITM.ybeta=0.0
kat.ETM.ybeta=-1e-10
compute_gravity_tilt(kat)
print(" WFS2:")
print(" ITM ybeta 0.1nm")
print(" ITM ybeta 0.1nrad")
kat = copy.deepcopy(tmpkat)
kat.parseKatCode(code_WFS2)
kat.parseKatCode(code_xaxis)
......@@ -279,7 +270,7 @@ def gravity_tilt(tmpkat):
kat.ITM.ybeta=1e-10
kat.ETM.ybeta=0.0
compute_gravity_tilt(kat)
print(" ETM ybeta -0.1nm")
print(" ETM ybeta -0.1nrad")
kat.ITM.ybeta=0.0
kat.ETM.ybeta=-1e-10
compute_gravity_tilt(kat)
......@@ -289,11 +280,9 @@ def tilt(tmpkat):
kat = copy.deepcopy(tmpkat)
def compute_tilt(tmpkat):
global kat
#global kat, out
kat = copy.deepcopy(tmpkat)
global out
out = kat.run()
# compute data x range in meters
beamsize = out["w0y"][0].real
xrange = beamsize*(out.x.max()-out.x.min())
......@@ -301,8 +290,9 @@ def tilt(tmpkat):
print(" Beamsize %e m" % beamsize)
print(" Measurement range: %e m, stepszie: %e m" % (xrange, stepsize))
# compute difference in angle between wavefront of carrier and sidebands
diff_l = (out["PDrefl_low"].real-out["PDrefl_car"].real)/stepsize
diff_u = (out["PDrefl_up"].real-out["PDrefl_car"].real)/stepsize
#global diff_l, diff_u, tilt_l, tilt_u
diff_l = (np.angle(out["PDrefl_low"], deg=True) - np.angle(out["PDrefl_car"], deg=True))/stepsize
diff_u = (np.angle(out["PDrefl_up"], deg=True) - np.angle(out["PDrefl_car"], deg=True))/stepsize
tilt_l = diff_l[1:-1]-diff_l[0:-2]
tilt_u = diff_u[1:-1]-diff_u[0:-2]
print(" Tilt (upper - car), mean: %e m/deg, stddev %e m/deg" % (np.mean(tilt_u), np.std(tilt_u)))
......@@ -330,20 +320,20 @@ def tilt(tmpkat):
"""
print(" WFS1:")
print(" ITM ybeta 0.1nm")
print(" ITM ybeta 0.1nrad")
kat.parseKatCode(code_comm)
kat.parseKatCode(code_WFS1)
kat.ITM.ybeta=1e-10
kat.ETM.ybeta=0.0
(a1, a2) = compute_tilt(kat)
print(" ETM ybeta -0.1nm")
print(" ETM ybeta -0.1nrad")
kat.ITM.ybeta=0.0
kat.ETM.ybeta=-1e-10
(a3, a4) = compute_tilt(kat)
print(" WFS2:")
print(" ITM ybeta 0.1nm")
print(" ITM ybeta 0.1nrad")
kat = copy.deepcopy(tmpkat)
kat.parseKatCode(code_comm)
kat.parseKatCode(code_WFS2)
......@@ -351,7 +341,7 @@ def tilt(tmpkat):
kat.ETM.ybeta=0.0
(a5, a6) = compute_tilt(kat)
print(" ETM ybeta -0.1nm")
print(" ETM ybeta -0.1nrad")
kat.ITM.ybeta=0.0
kat.ETM.ybeta=-1e-10
(a6, a7) = compute_tilt(kat)
......
......@@ -37,8 +37,7 @@ def main():
# %reset -f
# making these global during testing and debugging
#global kat
#global out
#global kat, out
kat = finesse.kat(tempdir=".",tempname="test")
kat.verbose = False
......@@ -50,14 +49,11 @@ def main():
try:
with open(tmpresultfile, 'rb') as handle:
result = pickle.load(handle)
#tmpfile = shelve.open(tmpresultfile)
#result=tmpfile[str('result')]
#tmpfile.close()
except: raise Exception("Could not open temprary results file {0}".format(tmpresultfile))
kat.PDrefl_q.enabled = False
kat.WFS1_Q.enables = False
kat.WFS2_Q.enables = False
kat.WFS1_Q.enabled = False
kat.WFS2_Q.enabled = False
print("--------------------------------------------------------")
print(" 9. ASC signals for large misalignments (ITM)")
......@@ -89,7 +85,7 @@ def asc_large(tmpkat, mir_name):
#kat.verbose=1
xscale = 1e6
yscale = 1e6
global out
#global out
tmpfilename = "datashelf_{0}.dat".format(mir_name)
backupname = "datashelf_{0}.dat.bck".format(mir_name)
out={}
......@@ -103,16 +99,9 @@ def asc_large(tmpkat, mir_name):
import os.path
if os.path.isfile(tmpfilename):
shutil.copyfile(tmpfilename, backupname)
print(" current results saved in: {0}".format(tmpfilename))
with open(tmpfilename, 'wb') as handle:
pickle.dump({ "out": out, "maxtems": done_maxtems}, handle)
#tmpfile = shelve.open(tmpfilename)
#tmpfile[str('out')]=out
#tmpfile[str('maxtems')]=done_maxtems
#tmpfile.close()
pickle.dump({ "out": out, "maxtems": done_maxtems}, handle)
if __name__ == '__main__':
main()
......@@ -39,9 +39,7 @@ def main():
# close('all')
# making these global during testing and debugging
#global kat
#global out
#global result
#global kat, out, result
print("--------------------------------------------------------")
print(" Plotting ASC signals for large misalignments")
......@@ -61,10 +59,6 @@ def asc_large(mir_name):
backupname = "datashelf_{0}.dat.bck".format(mir_name)
try:
#tmpfile = shelve.open(tmpfilename)
#out=tmpfile[str('out')]
#maxtems=tmpfile[str('maxtems')]
#tmpfile.close()
readpickle = pickle.load(open(tmpfilename, "rb"))
out=readpickle['out']
maxtems=readpickle['maxtems']
......
......@@ -48,8 +48,7 @@ def main():
# %reset -f
# making these global during testing and debugging
#global kat
#global out
#global kat, out
kat = finesse.kat(tempdir=".",tempname="test")
kat.verbose = False
......@@ -61,9 +60,6 @@ def main():
try:
with open(tmpresultfile, 'rb') as handle:
result = pickle.load(handle)
#tmpfile = shelve.open(tmpresultfile)
#result=tmpfile[str('result')]
#tmpfile.close()
except: raise Exception("Could not open temprary results file {0}".format(tmpresultfile))
# this does not work yet due to the scale command
......@@ -108,6 +104,9 @@ def main():
t_node=kat.s2.npo2
kat = set_thermal_lens(kat,50e3)
#import sys
#sys.exit()
print("--------------------------------------------------------")
print(" 12. computing beam tilt with thermal lens (f={0})".format(kat.ITM_TL.f))
......@@ -123,6 +122,7 @@ def main():
print(" Setting compromise beam parameter {0}: w0={1}, z={2}".format(node_text, beam5.w0, beam5.z))
t_node.node.setGauss(t_comp, beam5)
#maxtems = [1, 3, 5, 9, 11, 13, 15, 19, 23, 25, 27, 29]
#maxtems = [1, 3, 5, 9, 11, 13, 15]
maxtems = [1, 3, 5, 7]
converge_tilt(kat, maxtems)
......@@ -135,6 +135,7 @@ def main():
print(" 12. computing alignment signal with thermal lens (f={0})".format(kat.ITM_TL.f))
t_node.node.setGauss(t_comp, beam50)
#maxtems = [1, 3, 5, 9, 11, 13, 15, 17, 19]
#maxtems = [1, 3, 5, 9, 11, 13, 15]
maxtems = [1, 3, 5, 7]
converge_asc(kat, maxtems, 'asc_signals_50.txt')
......@@ -143,6 +144,7 @@ def main():
print(" 13. computing alignment signal with thermal lens (f={0})".format(kat.ITM_TL.f))
t_node.node.setGauss(t_comp, beam5)
#maxtems = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
#maxtems = [1, 3, 5, 9, 11, 13, 15]
maxtems = [1, 3, 5, 7]
converge_asc(kat, maxtems, 'asc_signals_5.txt')
......@@ -174,6 +176,7 @@ def converge_tilt(tmpkat, maxtems):
np.savetxt(filename, savedata[0:idx+1,:], fmt=b'%.18e', delimiter=' ')
def get_qs(tmpkat,f):
global kat, out
kat = copy.deepcopy(tmpkat)
# measure beam parameter for the 'cold beam' i.e. the laser beam
......@@ -185,7 +188,7 @@ def get_qs(tmpkat,f):
kat.parseKatCode('yaxis re:im')
kat.noxaxis = True
kat.maxtem=0
def beam_size(tmpkat, f, beam0):
kat = copy.deepcopy(tmpkat)
kat.psl.npsl.node.setGauss(kat.psl, beam0)
......@@ -193,14 +196,13 @@ def get_qs(tmpkat,f):
# add thermal lens and propagate input beam to ITM
kat = set_thermal_lens(kat, f)
out = kat.run()
# computing beam size at ITM
# and then we reflect of ITM, an set it as new startnode
q_in = out['w1']
from pykat.optics.ABCD import apply, mirror_refl
abcd = mirror_refl(1,-2500)
q_out = apply(abcd,q_in,1,1)
beam1 = beam_param(q=q_out)
#import pykat.optics.ABCD as ABCD
#abcd = ABCD.mirror_refl(1,2500)
#q_out = ABCD.apply(abcd,q_in,1,1)
beam1 = beam_param(q=q_in)
kat.removeLine("startnode")
kat.psl.npsl.node.removeGauss()
if "ITM_TL_r" in kat._kat__components:
......@@ -210,7 +212,6 @@ def get_qs(tmpkat,f):
kat.ITM.nITM1.node.setGauss(kat.ITM, beam1)
kat.parseKatCode("startnode nITM1")
out = kat.run()
# computing beam size at WFS1 and WFS2
q2 = out['w2']
beam2 = beam_param(q=q2)
......@@ -226,16 +227,15 @@ def get_qs(tmpkat,f):
print(" - WFS2 w={0:.6}cm (w0={1}, z={2})".format(100.0*beam3.w,beam3.w0, beam3.z))
print(" - npo2 w={0:.6}cm (w0={1}, z={2})".format(100.0*beam4.w,beam4.w0, beam4.z))
#raw_input("Press enter to continue")
return [beam1, beam2, beam3, beam4]
global out
#global out
# run finesse with input laser mode matched to cavity (no thermal lens)
out = kat.run()
# beam at laser when matched to cold cavity
# (note the sign flip of the real part to change direction of gauss param)
q0 = -1.0*out['w0'].conjugate()
beam0 = beam_param(q=q0)
beam0 = beam_param(q=q0)
# compute beam sizes when tracing this beam back through the system
(beam1,beam2,beam3, beam4)=beam_size(kat,f,beam0)
......@@ -285,8 +285,9 @@ def gravity_tilt(tmpkat):
kat = copy.deepcopy(tmpkat)
out = kat.run()
y1 = out["b1"]
y2 = out["b1_1k"]
y1 = np.abs(out["b1"])
y2 = np.abs(out["b1_1k"])
# shift of beam center on detector 1 (as m/w0y)
x1 = np.sum(out.x*y1)/np.sum(y1)
# shift of beam center on detector 2 (as m/w0y)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment