Commit 2bcfc4a8 authored by reiche's avatar reiche

Debugging BSRead

parent f1f26915
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
class DispersionMeasurement:
def __init__(self):
#define order of RF stations
self.rfin=[]
for i in range(0,4):
self.rfin.append('SINSB%2.2d' % (i+1))
for i in range(0,1):
self.rfin.append('SINXB%2.2d' % (i+1))
self.rfl1=[]
for i in range(0,9):
self.rfl1.append('S10CB%2.2d' % (i+1))
self.rfl2=[]
for i in range(0,4):
self.rfl2.append('S20CB%2.2d' % (i+1))
self.rfl3=[]
for i in range(0,13):
self.rfl3.append('S30CB%2.2d' % (i+1))
#------
# internal function
def addenergy(self,energy,rf,scan):
for ele in rf:
rfloc=np.array(scan.data[ele+'-RMSM:SM-GET'])
rfon=np.array(rfloc==9)*1.0
rfphase=np.array(scan.data[ele+'-RSYS:GET-BEAM-PHASE'])
rfvolta=np.array(scan.data[ele+'-RSYS:GET-ACC-VOLT'])
de=rfon*rfvolta*np.sin(rfphase*np.pi/180.)
energy=energy+de
return energy
#------
# common interface
def analyse(self,scan,names): # scan is a pointer to the NBPYSCAN instance
result={} # empty the result
error={}
disp1=[] # 1st order dispersion
disp2=[] # 2nd order dispersion
disp1err=[] # quadratic order fit to display
disp2err=[] # linear order to display
if len(scan.actuator.actuators) < 1:
return None
actor=scan.actuator.actuators[0] # first actuator in the list (this scan does only have one actuator
name=actor['PV']
val=actor['val']
isAramis=False
if 'SAR'in name:
isAramis=True
# calculate energy profile
energy=7.1
energy=self.addenergy(energy,self.rfin,scan)
energy=self.addenergy(energy,self.rfl1,scan)
energy=self.addenergy(energy,self.rfl2,scan)
if isAramis:
energy=self.addenergy(energy,self.rfl3,scan)
E0=np.mean(energy)
x=np.array((energy-E0)/E0)
scan.derivedData.clear()
scan.derivedData['Global']={}
scan.derivedData['Global']['RelEnergy']=x
x=np.reshape(x,(x.size))
for name in names:
if 'DBPM' in name and 'MARK' in name:
elename=name[0:15].replace('.','-')
for key in scan.data.keys():
if ':X' in key or ':Y' in key:
if elename in key:
if elename not in scan.derivedData.keys():
scan.derivedData[elename]={}
y=np.array(scan.data[key])*1e-3 # convert from mm to m
y=np.reshape(y,(y.size))
res,cov=np.polyfit(x,y,2,full=False,cov=True) # 3 element vector - first is quadratic term
if ':X' in key:
scan.derivedData[elename]['DX']=res[1]
scan.derivedData[elename]['DDX']=res[0]
scan.derivedData[elename]['DX_ERR']=np.sqrt(cov[1,1])
scan.derivedData[elename]['DDX_ERR']=np.sqrt(cov[0,0])
else:
scan.derivedData[elename]['DY']=res[1]
scan.derivedData[elename]['DDY']=res[0]
scan.derivedData[elename]['DY_ERR']=np.sqrt(cov[1,1])
scan.derivedData[elename]['DDY_ERR']=np.sqrt(cov[0,0])
......@@ -22,9 +22,34 @@ class DispersionModel:
self.Facility.writeFacility(self.MI)
def CorrectorResponse(self,didx=0):
def updateModel(self,didx):
val=self.MI.readMagnet()
for key in val:
if val[key] is None:
print('Channel for key', key, 'cannot be read')
else:
if didx == 0 and 'SAR' in key:
name=key.replace('-','.')
if name in self.Facility.ElementDB.keys():
ele=self.Facility.ElementDB[name]
if 'MQUA' in key or 'MQSK' in key:
self.Facility.ElementDB[name].k1=val[key]/ele.Length
elif 'MSEX' in key:
self.Facility.ElementDB[name].k2=val[key]/ele.Length*2
else:
print('Element',name,'not found in Online Model Database')
def DispersionPrediction(self,didx=0):
self.updateModel(didx)
opt=self.selectBeamline(didx)
s1,dx1,dy1,ddx1,ddy1,names=self.track(opt)
return s1,dx1,dy1,ddx1,ddy1,names
def CorrectorResponse(self,didx=0):
self.updateModel(didx)
cors=['MQUA150','MQSK160','MQSK300','MQSK420','MQUA430']
sv={}
res={}
......@@ -48,7 +73,7 @@ class DispersionModel:
def EnergyResponse(self,didx=0):
self.updateModel(didx)
opt=self.selectBeamline(didx)
s1,dx1,dy1,names=self.track(opt)
self.scaleMagnets(1)
......@@ -97,8 +122,8 @@ class DispersionModel:
self.Madx.write('use, sequence=swissfel;\n')
self.Madx.write('Select,flag=Error,pattern="SAT.*";\n')
self.Madx.write('EALIGN,DY=0.01;\n')
self.Madx.write('select, flag=twiss, column=NAME,S,BETX,ALFX,BETY,ALFY,DX,DY;\n')
self.Madx.write('twiss, range=#s/#e, sequence=swissfel,betx=betax0,bety=betay0,alfx=alphax0,alfy=alphay0,file="twiss.dat";\n')
self.Madx.write('select, flag=twiss, column=NAME,S,BETX,ALFX,BETY,ALFY,DX,DY,DDX,DDY;\n')
self.Madx.write('twiss, range=#s/#e, /CHROM, sequence=swissfel,betx=betax0,bety=betay0,alfx=alphax0,alfy=alphay0,file="twiss.dat";\n')
self.Madx.write('plot, haxis = s, vaxis = betx, bety, range = #s/#e,colour=100;\n')
self.Madx.write('plot, haxis = s, vaxis = dx, dy, range = #s/#e,colour=100;\n')
self.Madx.write('exit;')
......@@ -136,6 +161,8 @@ class DispersionModel:
s=np.zeros((nlen))
dx=np.zeros((nlen))
dy=np.zeros((nlen))
ddx=np.zeros((nlen))
ddy=np.zeros((nlen))
names=[]
for i in range(1,nlen+1):
......@@ -144,7 +171,9 @@ class DispersionModel:
s[i-1]=float(line[1])
dx[i-1]=float(line[6])
dy[i-1]=float(line[7])
return s,dx,dy,names
ddx[i-1]=float(line[8])
ddy[i-1]=float(line[9])
return s,dx,dy,ddx,ddy,names
def scaleMagnets(self,val):
......
This diff is collapsed.
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1420</width>
<height>1178</height>
<width>1556</width>
<height>1376</height>
</rect>
</property>
<property name="windowTitle">
......@@ -56,7 +56,7 @@
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="Log">
<widget class="QTextEdit" name="Log">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
......@@ -102,6 +102,48 @@
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Step 1 - Preparation</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QPushButton" name="MagCycle">
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 170, 0);</string>
</property>
<property name="text">
<string>Cycle Set Magnets</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="CorZero">
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 170, 0);</string>
</property>
<property name="text">
<string>Set Correctors to Zero</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="Step1">
<property name="enabled">
......@@ -114,7 +156,7 @@
</sizepolicy>
</property>
<property name="title">
<string>Step 1 - Measure Dispersion</string>
<string>Step 2 - Measure Dispersion</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
......@@ -217,25 +259,46 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="COEPICs">
<property name="text">
<string>EPICS Only</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QCheckBox" name="COValid">
<property name="text">
<string>Add VALID Channel</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Actuator</string>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="COAct">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<column>
<property name="text">
<string>PV</string>
</property>
</column>
<column>
<property name="text">
<string>Min</string>
</property>
</column>
<column>
<property name="text">
<string>Max</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="lineWidth">
......@@ -303,9 +366,19 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="pyRELoad">
<widget class="QCheckBox" name="COEPICs">
<property name="text">
<string>Load Measurement</string>
<string>EPICS Only</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="COValid">
<property name="text">
<string>Add VALID Channel</string>
</property>
</widget>
</item>
......@@ -322,6 +395,13 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pyRELoad">
<property name="text">
<string>Load Data and Analyse</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
......@@ -340,7 +420,7 @@
</size>
</property>
<property name="title">
<string>Step 2 - Energy Correction</string>
<string>Step 3 - Energy Correction</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
......@@ -374,7 +454,7 @@
<string notr="true">background-color: rgb(170, 170, 0);</string>
</property>
<property name="text">
<string>Apply Correction</string>
<string>Apply Energy Correction</string>
</property>
</widget>
</item>
......@@ -409,7 +489,7 @@
</size>
</property>
<property name="title">
<string>Step 3 - Local Correction</string>
<string>Step 4 - Residual Correction</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
......@@ -475,12 +555,9 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QPushButton" name="CorZero">
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 170, 0);</string>
</property>
<widget class="QPushButton" name="ModelDispCalc">
<property name="text">
<string>Set Correctors to Zero</string>
<string>Predict Dispersion</string>
</property>
</widget>
</item>
......@@ -514,6 +591,19 @@
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
......
Markdown is supported
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