Files
OpticsTools/matching.py
2026-01-26 12:51:49 +01:00

59 lines
2.0 KiB
Python

converttwiss= {'betax':'betx','betay':'bety','alphax':'alfx','alphay':'alfy',
'etax':'dx','etay':'dy','etapx':'dpx','etapy':'dpy',
'x':'x','y':'y','px':'px','py':'py','energy':'energy'}
class Matching:
def __init__(self,parent=None,model=None,reference = None):
self.parent = parent
self.model = model
self.reference = reference
self.destination=''
def match(self):
config = self.reference.getMatchingPoint()
if config is None:
return
vars = {}
for var in config['Variable']:
var0=var.replace('-','.').lower()
if 'mqua' in var0 or 'mqsk' in var0:
var0 += '.k1'
elif 'msex' in var0:
var0 += '.k2'
vars[var0]={'Min':-1e6,'Max':1e6,'Preset':0,'Step':0.00001}
print(vars)
return
if not config['End'] == self.destination:
self.destination = config['End']
self.model.setBranch(self.destination)
start=config['Reference']['Location']
if not start in self.reference.reference.keys():
return
twiss_in = self.reference.reference[start]['Twiss']
twiss0 = {converttwiss[key]: twiss_in[key] for key in twiss_in.keys()}
twiss0['Energy']= 150.*1e-3
loc = self.reference.reference[start]['Location']
print(loc)
print(twiss0)
print(config['Variable'])
#
def matchold(self,sequence,destination, variables,conditions,periodic=False,plot=True):
self.setBranch(destination.upper())
twiss={'energy0':150.}
self.madx.updateVariables(twiss)
res,twiss,tar=self.madx.match(sequence,variables,conditions,periodic)
energy = self.calcEnergyProfile(twiss)
matchtwiss={'betax':twiss.betx[0],'betay':twiss.bety[0],'alphax':twiss.alfx[0],'alphay':twiss.alfy[0]}
if plot:
self.parent.plot.newData(twiss, energy)
return res, matchtwiss,tar