Indication of magnet strengths and matching results in GUI

This commit is contained in:
2026-01-20 15:12:12 +01:00
parent 906890da98
commit 60b9643568
9 changed files with 192 additions and 38 deletions

View File

@@ -2,10 +2,10 @@ import os
import re
from onlinemodel.madx import CMadX
import io
import contextlib
from io import StringIO
import sys
import copy
class MatchMaker:
def __init__(self,signal = None):
@@ -14,6 +14,7 @@ class MatchMaker:
self.variables={}
self.scriptdir = None
self.signal=signal
self.matchresult=[]
def initScripts(self,target):
self.scriptdir = self.matchlist[target]
@@ -56,6 +57,9 @@ class MatchMaker:
def match(self, om, variables = None, Injector=True, Athos = True, Aramis = False, Porthos = False):
self.matchresult.clear()
cwd = os.getcwd()
os.chdir(self.scriptdir)
if Athos:
target = 'SATBD02'
else:
@@ -67,58 +71,68 @@ class MatchMaker:
om.setBranch(target, 'SINLH01')
madx.updateLattice(om, target) # write lattice
madx.commonHeader('SwissFEL', '#s/#e', None) # sets header
madx.madx.call(self.scriptdir + '/initTwiss.madx')
madx.madx.call('initTwiss.madx')
if not variables is None:
madx.definePresets(variables)
if Injector:
print('Matching Injector ...')
madx.madx.call(self.scriptdir+'/matchInjector.madx', chdir=True)
madx.madx.call('matchInjector.madx')
self.updateOnlineModel(om,madx.madx, 's[i1].*k[12]|s20cb.*k1|s20sy01.*k1')
self.parseMatchOutput(f.getvalue().split('\n'),'Injector')
if Athos:
f.truncate(0)
print('Matching Athos ...')
if self.signal:
self.signal.emit('Matching Athos ...')
madx.madx.call(self.scriptdir+'/matchAthos.madx', chdir=True)
madx.madx.call('matchAthos.madx')
self.updateOnlineModel(om, madx.madx, 'sat.*mqua.*k1|sat.*msex.*k2|s20sy02.*m[kq]')
self.parseMatchOutput(f.getvalue().split('\n'), 'Athos')
if Aramis:
f.truncate(0)
print('Matching Aramis ...')
if target=='SATBD01':
if target=='SATBD02':
target = 'SARUN20'
om.setBranch(target, 'SINLH01')
madx.updateLattice(om, target) # write lattice
madx.commonHeader('SwissFEL', '#s/#e', None) # sets header
madx.madx.call(self.scriptdir + '/initTwiss.madx')
madx.madx.call(self.scriptdir + '/matchAramis.madx', chdir=True)
madx.madx.call('initTwiss.madx')
madx.madx.call('matchAramis.madx')
self.updateOnlineModel(om, madx.madx, 's[3a][0r].*k[12]|s20sy03.*k1')
self.parseMatchOutput(f.getvalue().split('\n'), 'Aramis')
if self.signal:
self.signal.emit('Matching done')
os.chdir(cwd)
return madx.madx.table.twiss
def parseMatchOutput(self, result,prefix,full=False):
loc = None
locidx = 1
isMatch = False
penalty = 1
report=[]
for line in result:
if full:
print(line)
if 'MATCH POINT' in line:
isMatch = False
print("--------------------------------------")
print(line)
loc = line.split(':')[1].strip()
continue
if 'MATCH SUMMARY' in line:
if 'MATCH SUMMARY' in line.upper():
isMatch = True
continue
if 'VARIABLE "TAR"' in line:
isMatch = False
print("--------------------------------------")
print('MATCHING RESULTS: %s (error: %e)' % (prefix,penalty))
if loc is None:
loc = 'Matchpoint %d' % locidx
locidx +=1
self.matchresult.append({'Location':loc,'Error':penalty})
if penalty > 1e-5:
for rep in report:
print(rep)
penalty = 1
loc = None
report.clear()
continue
if isMatch and len(line) > 4:
@@ -126,7 +140,7 @@ class MatchMaker:
penalty = float(line.split('=')[1].strip())
else:
report.append(line)
return
def updateOnlineModel(self,om,madx,filter):
reg = re.compile(filter)