def create_base_files_100pC(scaling, sol=0.34, lsol=0.08, subdir=True): if subdir: dir = 'basefiles_' + str(scaling) + '_100pC/' + str(sol) + '/' + str( lsol) + '/' else: dir = 'basefiles_' + str(scaling) + '_100pC/' framework = Framework(dir, overwrite=True, clean=True) if subdir: framework.loadSettings('Lattices/clara400_v12_v3_elegant.def') else: framework.loadSettings('Lattices/clara400_v12_v3.def') if not os.name == 'nt': framework.defineASTRACommand(scaling=scaling) framework.defineCSRTrackCommand(scaling=scaling) framework.generator.number_of_particles = 2**(3 * scaling) framework.generator.charge = 100e-12 framework['CLA-HRG1-GUN-SOL'].field_amplitude = sol framework['CLA-L01-CAV-SOL-01'].field_amplitude = lsol framework['CLA-L01-CAV-SOL-02'].field_amplitude = -lsol framework['CLA-L02-CAV'].phase = -15 framework['CLA-L03-CAV'].phase = -12 framework['bunch_compressor'].angle = 0.135 # framework['L02'].sample_interval = 2**(3*2) framework.track(endfile="S07") #, startfile='VBC') twiss.read_astra_emit_files(dir + '/injector400.Xemit.001') return twiss['enx'][-1]
def create_base_file(settings='./clara400_v12_v3.def'): cavity_absolute_phases, cavity_absolute_momenta = rcp.get_Cavity_Phases(dir='../basefiles_4', settings='./clara400_v12_v3.def') lattice = Framework('test', clean=False, verbose=False) lattice.loadSettings(settings) if not os.name == 'nt': scaling = 4 lattice.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) # lattice.defineASTRACommand(['/opt/ASTRA/astra.sh']) lattice.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) lattice.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) lattice.generator.number_of_particles = 2**(3*scaling) else: lattice.generator.number_of_particles = 2**(3*3) lattice.defineElegantCommand(['elegant']) for lat in lattice.latticeObjects: print 'lattice = ', lat if hasattr(lattice[lat], 'headers') and not lat == 'generator': lattice.setSubDirectory('basefiles') lattice[lat].headers['newrun']['auto_phase'] = True lattice[lat].headers['newrun']['Track_All'] = False lattice[lat].file_block['input']['prefix'] = '../test/' lattice.track(files=[lat], postprocess=False) lattice.setSubDirectory('test') cavities = lattice[lat].getElementType('cavity') if len(cavities) > 0: absolute_phases, absolute_momenta = rcp.get_Cavity_Phase('basefiles/'+lat+'.log') print zip(cavities, absolute_phases) for cav, absphase in zip(cavities, absolute_phases): cav['phase'] = absphase + cav['phase'] lattice[lat].headers['newrun']['auto_phase'] = False lattice[lat].headers['newrun']['Track_All'] = True lattice.track(files=[lat], postprocess=True)
def test_ASTRA_Elegant_C2V(quads, phase=4, charge=70): global dir, lattice, scaling, bestdelta dir = './SETUP/TOMP_SETUP' lattice = Framework(dir, clean=True, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP_ASTRA.def') lattice.change_Lattice_Code('C2V', 'elegant') scaling = 6 if not os.name == 'nt': lattice.defineASTRACommand(scaling=(scaling)) lattice.defineCSRTrackCommand(scaling=(scaling)) lattice.define_gpt_command(scaling=(scaling)) lattice['S02'].file_block['input'][ 'prefix'] = '../SETUP/TOMP_SETUP_' + str(phase) + '_' + str( charge) + '/' lattice['S02'].sample_interval = 1 #2**(3*2) dir = './test_EleC2V_' + str(phase) + '_' + str(charge) lattice.setSubDirectory(dir) lattices = lattice.latticeObjects.values() # for l in lattices: # if hasattr(l, 'headers'): # if isinstance(l.headers, (dict)): # if 'charge' in l.headers.keys(): # l.headers['charge'].space_charge_mode = False optFuncVELA(quads, track=False) lattice.track(startfile='S02', track=True)
def optimise_Lattice(phase=None, q=70, do_optimisation=False): global dir, lattice, scaling, bestdelta bestdelta = 1e10 dir = './SETUP/TOMP_SETUP' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP_Separated.def') scaling = 6 if not os.name == 'nt': lattice.defineASTRACommand(scaling=(scaling)) lattice.defineCSRTrackCommand(scaling=(scaling)) lattice.define_gpt_command(scaling=(scaling)) lattice['L01'].file_block['input']['prefix'] = '../basefiles_' + str( scaling) + '_' + str(q) + '/' quads = 0.107 * np.array([ 21.11058462, -11.36377551, 24.69336696, -22.63264054, 56.07039682, -51.58739658 ]) # quads = setChicane(quads) # optFuncChicane(quads) # print('Chicane = ', quads) quads = [ lattice.getElement('CLA-S02-MAG-QUAD-01', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-02', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-03', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-04', 'k1l'), lattice.getElement('CLA-C2V-MAG-QUAD-01', 'k1l'), lattice.getElement('CLA-C2V-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-07', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-08', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-09', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-10', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-11', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-15', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-01', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-03', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-04', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-05', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-06', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-07', 'k1l'), ] quads = np.array([ 1.7815538119218322, -1.6517067100557492, 2.3043853160232697, -1.3861969157005136, 5.757177173351052, -4.733558263809512, 1.7234634394591193, -1.5865723560117138, 0.3758596578541717, -0.3895175193164829, 0.17091495276247198, 0.12327634704511606, -0.2637956911506127, -0.9593369561940668, 1.2250265489541068, 1.150196537233205, 0.0385787389105507, -1.3341278723969179, 1.3605277843974064 ]) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-DIA-FCUP-01' lattice['S02'].sample_interval = 2**(3 * 3) if do_optimisation: if phase is not None: lattice['S02'].file_block['input'][ 'prefix'] = '../TOMP_SETUP_' + str(phase) + '_' + str(q) + '/' quads = setVELA(quads) optFuncVELA(quads) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-DIA-FCUP-01' lattice['S02'].sample_interval = 1
def optimise_Lattice(phase=None, q=70, do_optimisation=False): global dir, lattice, scaling, bestdelta bestdelta = 1e10 dir = './SETUP/TOMP_SETUP' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP.def') scaling = 6 if not os.name == 'nt': lattice.defineASTRACommand(scaling=(scaling)) lattice.defineCSRTrackCommand(scaling=(scaling)) lattice.define_gpt_command(scaling=(scaling)) lattice['L01'].file_block['input']['prefix'] = '../basefiles_' + str( scaling) + '_' + str(q) + '/' quads = 0.107 * np.array([ 21.11058462, -11.36377551, 24.69336696, -22.63264054, 56.07039682, -51.58739658 ]) # quads = setChicane(quads) # optFuncChicane(quads) # print('Chicane = ', quads) quads = [ lattice.getElement('CLA-S02-MAG-QUAD-01', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-02', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-03', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-04', 'k1l'), lattice.getElement('CLA-C2V-MAG-QUAD-01', 'k1l'), lattice.getElement('CLA-C2V-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-07', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-08', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-09', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-10', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-11', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-15', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-01', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-03', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-04', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-05', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-06', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-07', 'k1l'), ] quadsNEW = np.array([ 1.7901911899775773, -1.6407693149976745, 2.2849042966309447, -1.3685069221066561, 5.7572751421298305, -4.696861959661223, 1.653747009525063, -1.597658628810405, 0.3920095459041216, -0.3922879907823872, 0.16851104048712628, 0.12208077087083297, -0.26073294653351364, -0.9516625759490311, 1.2270249450337767, 1.1374710985669274, 0.038188155183286554, -1.3227126094830322, 1.360338937783752 ]) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-DIA-FCUP-01' lattice['S02'].sample_interval = 2**(3 * 3) if do_optimisation: if phase is not None: lattice['S02'].file_block['input'][ 'prefix'] = '../TOMP_SETUP_' + str(phase) + '_' + str(q) + '/' quads = setVELA(quads) optFuncVELA(quads) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-DIA-FCUP-01' lattice['S02'].sample_interval = 1
def optimise_Lattice(phase=None, q=70, do_optimisation=False): global dir, lattice, scaling, bestdelta bestdelta = 1e10 dir = './SETUP/TOMP_SETUP' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-FE.def') lattice.modifyElement('CLA-L01-CAV-SOL-01', 'field_amplitude', 0.07) lattice.modifyElement('CLA-L01-CAV-SOL-02', 'field_amplitude', -0.05) scaling = 6 if not os.name == 'nt': lattice.defineASTRACommand(scaling=(scaling)) lattice.defineCSRTrackCommand(scaling=(scaling)) lattice.define_gpt_command(scaling=(scaling)) # lattice['L01'].file_block['input']['prefix'] = '../../CLARA_BA1_Gaussian/basefiles_'+str(scaling)+'_'+str(q)+'/' quads = [ lattice.getElement('CLA-S02-MAG-QUAD-01', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-02', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-03', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-04', 'k1l'), lattice.getElement('CLA-S02-MAG-QUAD-05', 'k1l'), ] quads = np.array([ 2.018036284043403, -1.6775106326141502, 2.0713066380968943, -1.4092617257511626, 0.07875236608471231 ]) lattice['S02'].sample_interval = 8 if do_optimisation: if phase is not None: lattice['S02'].file_block['input'][ 'prefix'] = '../TOMP_SETUP_' + str(phase) + '_' + str(q) + '/' quads = setVELA(quads) optFuncVELA(quads) print('VELA = ', quads) lattice['S02'].sample_interval = 1 #2**(3*2)
def create_base_files(scaling): framework = Framework('basefiles_FEBE_'+str(scaling), overwrite=False) framework.loadSettings('Lattices/claraX400_v12_80MVm.def') if not os.name == 'nt': framework.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) framework.generator.number_of_particles = 2**(3*scaling) framework.track(run=True)
def create_base_files(scaling, changes=None): framework = Framework('basefiles_' + str(scaling), overwrite=True) framework.loadSettings('FEBE_Single_L01.def') if not os.name == 'nt': framework.defineASTRACommand(scaling=scaling) framework.generator.number_of_particles = 2**(3 * scaling) if not changes is None: framework.load_changes_file(changes) framework.track()
def create_base_files(scaling): framework = Framework('basefiles_' + str(scaling), overwrite=True) framework.loadSettings('Lattices/clara400_v12_v3.def') if not os.name == 'nt': framework.defineASTRACommand(scaling=scaling) framework.defineCSRTrackCommand(scaling=scaling) framework.generator.number_of_particles = 2**(3 * scaling) # framework.change_Lattice_Code('VBC', 'ASTRA') framework.track(endfile="S07") #startfile='VBC')
def create_base_files(scaling): framework = Framework('basefiles_FEBE_'+str(scaling), overwrite=False) framework.loadSettings('Lattices/clara400_v12_FEBE.def') if not os.name == 'nt': framework.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) framework.defineElegantCommand(['elegant']) framework['S07'].file_block['input']['prefix'] = '../../basefiles_'+str(scaling)+'/' framework.track(track=True, startfile='S07')
def create_base_files(scaling, charge=70): framework = Framework('basefiles_' + str(scaling) + '_' + str(charge), overwrite=True, clean=True) framework.loadSettings('CLA10-BA1_TOMP_ASTRA.def') if not os.name == 'nt': framework.defineASTRACommand(scaling=(scaling)) framework.defineCSRTrackCommand(scaling=(scaling)) framework.generator.number_of_particles = 2**(3 * scaling) framework.modifyElement('CLA-LRG1-GUN-CAV', 'phase', -5) framework['generator'].charge = charge * 1e-12 # before_tracking() framework.track(files=['generator', 'injector10'])
def load_lattice(): global lattice lattice = Framework('example', clean=False, verbose=False) lattice.loadSettings('split.def') if not os.name == 'nt': scaling = 4 lattice.defineASTRACommand(['mpiexec','-np',str(4*scaling),'/opt/ASTRA/astra_MPICH2.sh']) # lattice.defineASTRACommand(['/opt/ASTRA/astra.sh']) lattice.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) lattice.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) lattice.generator.number_of_particles = 2**(3*scaling) else: lattice.generator.number_of_particles = 2**(3*3) lattice.defineElegantCommand(['elegant'])
def optimise_Lattice(): global dir, lattice dir = './TOMP_SETUP' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP.def') if not os.name == 'nt': scaling = 5 lattice.defineASTRACommand( ['mpiexec', '-np', str(3 * scaling), '/opt/ASTRA/astra_MPICH2.sh']) # lattice.defineASTRACommand(['/opt/ASTRA/astra.sh']) lattice.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) lattice.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(3 * scaling), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) lattice.defineElegantCommand(['elegant']) scaling = 5 lattice['L01'].file_block['input']['prefix'] = '../basefiles_' + str( scaling) + '/' # lattice.modifyElement('CLA-L01-CAV', 'phase', 0) # setMomentum() # lattice.change_Lattice_Code('L01','ASTRA') # lattice.track(startfile='L01', endfile='L01', track=True) quads = [ 21.11058462, -11.36377551, 24.69336696, -22.63264054, 56.07039682, -51.58739658 ] # quads = setChicane(quads) optFuncChicane(quads) print 'Chicane = ', quads quads = [ lattice.getElement('EBT-INJ-MAG-QUAD-07', 'k1'), lattice.getElement('EBT-INJ-MAG-QUAD-08', 'k1'), lattice.getElement('EBT-INJ-MAG-QUAD-09', 'k1'), lattice.getElement('EBT-INJ-MAG-QUAD-10', 'k1'), lattice.getElement('EBT-INJ-MAG-QUAD-11', 'k1'), lattice.getElement('EBT-INJ-MAG-QUAD-15', 'k1'), ] quads = [ 19.27190175, -17.69572111, 7.0315249, -5.1809911, 2.34047285, 3.39357893 ] # quads = setVELA(quads) optFuncVELA(quads) print 'VELA = ', quads lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-COFFIN-FOC'
def create_base_files(scaling): framework = Framework('basefiles_' + str(scaling), overwrite=True, clean=True) framework.loadSettings('CLA10-BA1_TOMP.def') if not os.name == 'nt': framework.defineASTRACommand( ['mpiexec', '-np', str(3 * scaling), '/opt/ASTRA/astra_MPICH2.sh']) framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) framework.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(3 * scaling), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) framework.generator.number_of_particles = 2**(3 * scaling) framework.modifyElement('CLA-LRG1-GUN-CAV', 'phase', -5) framework.track(files=['generator', 'injector10'])
def create_base_files_20pC(scaling): framework = Framework('basefiles_' + str(scaling) + '_20pC', overwrite=True) framework.loadSettings('Lattices/clara400_v12_v3.def') if not os.name == 'nt': framework.defineASTRACommand(scaling=scaling) framework.defineCSRTrackCommand(scaling=scaling) framework.generator.number_of_particles = 2**(3 * scaling) framework.generator.charge = 20e-12 framework['CLA-HRG1-GUN-SOL'].field_amplitude = 0.322 framework['CLA-L01-CAV-SOL-01'].field_amplitude = 0.08 framework['CLA-L01-CAV-SOL-02'].field_amplitude = -0.08 framework['CLA-L02-CAV'].phase = 20 framework['CLA-L03-CAV'].phase = 30 # framework['bunch_compressor'].angle = 0.1 # framework['L02'].sample_interval = 2**(3*2) framework.track(endfile="S07") #, startfile='VBC')
def optimise_Lattice(): global dir, lattice dir = './TOMP_SETUP_DBURT' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP.def') scaling = 5 if not os.name == 'nt': lattice.defineASTRACommand( ['mpiexec', '-np', str(3 * scaling), '/opt/ASTRA/astra_MPICH2.sh']) # lattice.defineASTRACommand(['/opt/ASTRA/astra.sh']) lattice.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) lattice.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(3 * scaling), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) lattice.defineElegantCommand(['elegant']) # lattice.defineElegantCommand(['mpiexec','-np','6','Pelegant']) scaling = 5 lattice['L01'].file_block['input']['prefix'] = '../basefiles_' + str( scaling) + '/' client = unitsC.zmqClient() namesK = client.getNamesK(DBURT='CLARA_2_BA1_BA2_2018-11-20-1951.dburt') for name, param, k in namesK: if param is not None: if str(name) in lattice.elementObjects.keys(): print name, param, k lattice.modifyElement(str(name), str(param), float(k)) # exit() # quads = [ 21.11058462, -11.36377551, 24.69336696, -22.63264054, 56.07039682, -51.58739658] quads = [-13.84327193, -2.92364597, 18.55449508, -18.53314705] quads = setChicane(quads) optFuncChicane(quads) print 'Chicane = ', quads # lattice.modifyElement('EBT-BA1-MAG-QUAD-01', 'k1', -1.22) # lattice.modifyElement('EBT-BA1-MAG-QUAD-02', 'k1', 2.18) # lattice.modifyElement('EBT-BA1-MAG-QUAD-03', 'k1', -1.22) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-COFFIN-FOC'
# framework.defineGeneratorCommand(['/opt/ASTRA/generator']) # framework.defineASTRACommand(['mpiexec','-np',str(ncpu),'/opt/ASTRA/astra_MPICH2.sh']) # framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(ncpu),'/opt/CSRTrack/csrtrack_openmpi.sh']) # framework.defineElegantCommand(['elegant']) # # framework.loadSettings('Lattices/CLA10-BA1.def') # framework.change_Lattice_Code('All','elegant') # framework['S02'].prefix = '../../basefiles_4/' # framework.track(startfile='S02', endfile='BA1_dipole') basedir = 'dipole_scan' framework = Framework(basedir, clean=True, verbose=False) if not os.name == 'nt': framework.defineGeneratorCommand(['/opt/ASTRA/generator']) framework.defineASTRACommand( ['mpiexec', '-np', str(ncpu), '/opt/ASTRA/astra_MPICH2.sh']) framework.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(ncpu), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) framework.defineElegantCommand(['elegant']) framework.loadSettings('Lattices/CLA10-BA1.def') framework.change_Lattice_Code('All', 'elegant') framework['S02'].prefix = '../../basefiles_4/' etax = [] etax2 = [] kValues = np.arange(0, 3, 0.1) for k in kValues: #np.arange(0,1,0.1): print('setting k = ', k)
class fitnessFunc(): def __init__(self, args, tempdir, scaling=5, overwrite=True, verbose=False, summary=False, post_injector=True): self.cons = constraintsClass() self.beam = rbf.beam() self.scaling = scaling self.tmpdir = tempdir self.verbose = verbose self.summary = summary self.overwrite = overwrite self.post_injector = post_injector ''' if only post-injector optimisation''' if self.post_injector: linac2field, linac2phase, linac3field, linac3phase, fhcfield, fhcphase, linac4field, linac4phase, bcangle = args self.parameters = dict( zip([ 'linac2field', 'linac2phase', 'linac3field', 'linac3phase', 'fhcfield', 'fhcphase', 'linac4field', 'linac4phase', 'bcangle' ], args)) else: ''' including injector parameters ''' gunphase, gunsol, linac1field, linac1phase, linac1sol1, linac1sol2, linac2field, linac2phase, linac3field, linac3phase, fhcfield, fhcphase, linac4field, linac4phase, bcangle = args self.parameters = dict( zip([ 'gunphase', 'gunsol', 'linac1field', 'linac1phase', 'linac1sol1', 'linac1sol2', 'linac2field', 'linac2phase', 'linac3field', 'linac3phase', 'fhcfield', 'fhcphase', 'linac4field', 'linac4phase', 'bcangle' ], args)) self.npart = 2**(3 * scaling) ncpu = scaling * 3 if self.post_injector: self.sbandlinacfields = np.array( [linac2field, linac3field, linac4field]) else: self.sbandlinacfields = np.array( [linac1field, linac2field, linac3field, linac4field]) self.dirname = os.path.basename(self.tmpdir) self.framework = Framework(self.dirname, overwrite=overwrite, verbose=verbose) if not os.name == 'nt': self.framework.defineGeneratorCommand(['/opt/ASTRA/generator']) self.framework.defineASTRACommand( ['mpiexec', '-np', str(ncpu), '/opt/ASTRA/astra_MPICH2.sh']) self.framework.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(ncpu), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) self.framework.defineElegantCommand(['elegant']) self.framework.loadSettings('Lattices/clara400_v12_v3_elegant.def') if not self.post_injector: self.framework.generator.particles = self.npart self.framework.modifyElement('CLA-HRG1-GUN-CAV', 'phase', gunphase) self.framework.modifyElement('CLA-HRG1-GUN-SOL', 'field_amplitude', gunsol) self.framework.modifyElement('CLA-L01-CAV', 'field_amplitude', abs(linac1field)) self.framework.modifyElement('CLA-L01-CAV', 'phase', linac1phase) self.framework.modifyElement('CLA-L01-CAV-SOL-01', 'field_amplitude', linac1sol1) self.framework.modifyElement('CLA-L01-CAV-SOL-02', 'field_amplitude', linac1sol2) self.framework.modifyElement('CLA-L02-CAV', 'field_amplitude', abs(linac2field)) self.framework.modifyElement('CLA-L02-CAV', 'phase', linac2phase) self.framework.modifyElement('CLA-L03-CAV', 'field_amplitude', abs(linac3field)) self.framework.modifyElement('CLA-L03-CAV', 'phase', linac3phase) self.framework.modifyElement('CLA-L4H-CAV', 'field_amplitude', abs(fhcfield)) self.framework.modifyElement('CLA-L4H-CAV', 'phase', fhcphase) self.framework.modifyElement('CLA-L04-CAV', 'field_amplitude', abs(linac4field)) self.framework.modifyElement('CLA-L04-CAV', 'phase', linac4phase) self.framework['bunch_compressor'].set_angle(abs(bcangle)) def calculateBeamParameters(self): try: if self.post_injector: self.framework['POSTINJ'].file_block['input'][ 'prefix'] = '../basefiles_' + str(self.scaling) + '/' self.framework.track(startfile='POSTINJ') else: self.framework.track() #startfile='FMS') self.beam.read_HDF5_beam_file(self.dirname + '/CLA-FMS-APER-01.hdf5') ## CONVERT THIS TO A DIST FILE!!! except Exception as e: print(e) return 1e6
class fitnessFunc(): def __init__(self, args, tempdir, scaling=4, overwrite=True, verbose=False, summary=False, post_injector=True): self.cons = constraintsClass() self.beam = rbf.beam() self.twiss = rtf.twiss() self.scaling = scaling self.tmpdir = tempdir self.verbose = verbose self.summary = summary self.overwrite = overwrite self.post_injector = post_injector # print 'self.post_injector = ', self.post_injector ''' if only post-injector optimisation''' if self.post_injector: linac2field, linac2phase, linac3field, linac3phase, fhcfield, fhcphase, linac4field, linac4phase, bcangle = args self.parameters = dict( zip([ 'linac2field', 'linac2phase', 'linac3field', 'linac3phase', 'fhcfield', 'fhcphase', 'linac4field', 'linac4phase', 'bcangle' ], args)) else: ''' including injector parameters ''' gunphase, gunsol, linac1field, linac1phase, linac1sol1, linac1sol2, linac2field, linac2phase, linac3field, linac3phase, fhcfield, fhcphase, linac4field, linac4phase, bcangle = args self.parameters = dict( zip([ 'gunphase', 'gunsol', 'linac1field', 'linac1phase', 'linac1sol1', 'linac1sol2', 'linac2field', 'linac2phase', 'linac3field', 'linac3phase', 'fhcfield', 'fhcphase', 'linac4field', 'linac4phase', 'bcangle' ], args)) self.npart = 2**(3 * scaling) ncpu = scaling * 3 if self.post_injector: self.sbandlinacfields = np.array( [linac2field, linac3field, linac4field]) else: self.sbandlinacfields = np.array( [linac1field, linac2field, linac3field, linac4field]) self.dirname = os.path.basename(self.tmpdir) self.framework = Framework(self.dirname, overwrite=overwrite, verbose=verbose) if not os.name == 'nt': self.framework.defineGeneratorCommand(['/opt/ASTRA/generator']) self.framework.defineASTRACommand( ['mpiexec', '-np', str(ncpu), '/opt/ASTRA/astra_MPICH2.sh']) self.framework.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(ncpu), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) self.framework.defineElegantCommand(['elegant']) self.framework.loadSettings('Lattices/clara400_v12_v3.def') if not self.post_injector: self.framework.generator.particles = self.npart self.framework.modifyElement('CLA-HRG1-GUN-CAV', 'phase', gunphase) self.framework.modifyElement('CLA-HRG1-GUN-SOL', 'field_amplitude', gunsol) self.framework.modifyElement('CLA-L01-CAV', 'field_amplitude', abs(linac1field)) self.framework.modifyElement('CLA-L01-CAV', 'phase', linac1phase) self.framework.modifyElement('CLA-L01-CAV-SOL-01', 'field_amplitude', linac1sol1) self.framework.modifyElement('CLA-L01-CAV-SOL-02', 'field_amplitude', linac1sol2) self.framework.modifyElement('CLA-L02-CAV', 'field_amplitude', abs(linac2field)) self.framework.modifyElement('CLA-L02-CAV', 'phase', linac2phase) self.framework.modifyElement('CLA-L03-CAV', 'field_amplitude', abs(linac3field)) self.framework.modifyElement('CLA-L03-CAV', 'phase', linac3phase) self.framework.modifyElement('CLA-L4H-CAV', 'field_amplitude', abs(fhcfield)) self.framework.modifyElement('CLA-L4H-CAV', 'phase', fhcphase) self.framework.modifyElement('CLA-L04-CAV', 'field_amplitude', abs(linac4field)) self.framework.modifyElement('CLA-L04-CAV', 'phase', linac4phase) self.framework['bunch_compressor'].set_angle(abs(bcangle)) def between(self, value, minvalue, maxvalue, absolute=True): if absolute: result = max([minvalue, min([maxvalue, abs(value)])]) else: result = np.sign(value) * max( [minvalue, min([maxvalue, abs(value)])]) return result def calculateBeamParameters(self): bcangle = self.framework['bunch_compressor'].angle # print 'bcangle = ', bcangle try: # if abs(bcangle) < 0.01 or abs(bcangle) > 0.175: # raise ValueError if self.overwrite: startS = self.framework['S02'].startObject['position_start'][2] if self.post_injector: self.framework['S02'].file_block['input'][ 'prefix'] = '../basefiles_' + str(self.scaling) + '/' self.framework.track(startfile='S02', endfile='S07') #startfile='FMS') else: self.framework.track(endfile='S07') # self.beam.read_astra_beam_file(self.dirname+'/S07.4928.001') self.beam.read_HDF5_beam_file(self.dirname + '/CLA-S07-MARK-03.hdf5') self.beam.slices = 10 self.beam.bin_time() sigmat = 1e12 * np.std(self.beam.t) sigmap = np.std(self.beam.p) meanp = np.mean(self.beam.p) emitx = 1e6 * self.beam.normalized_mve_horizontal_emittance emity = 1e6 * self.beam.normalized_mve_horizontal_emittance density = self.beam.density fitp = 100 * sigmap / meanp fhcfield = self.parameters['fhcfield'] peakI, peakIMomentumSpread, peakIEmittanceX, peakIEmittanceY, peakIMomentum, peakIDensity = self.beam.mvesliceAnalysis( ) chirp = self.beam.chirp constraintsList = { 'peakI_min': { 'type': 'greaterthan', 'value': abs(peakI), 'limit': 600, 'weight': 60 }, 'peakI_max': { 'type': 'lessthan', 'value': abs(peakI), 'limit': 750, 'weight': 10 }, # 'peakIMomentumSpread': {'type': 'lessthan', 'value': peakIMomentumSpread, 'limit': 0.1, 'weight': 2}, # 'peakIEmittanceX': {'type': 'lessthan', 'value': 1e6*peakIEmittanceX, 'limit': 0.75, 'weight': 5}, # 'peakIEmittanceY': {'type': 'lessthan', 'value': 1e6*peakIEmittanceY, 'limit': 0.75, 'weight': 5}, 'peakIMomentum': { 'type': 'equalto', 'value': 1e-6 * peakIMomentum, 'limit': 220, 'weight': 20 }, 'sband_linac fields': { 'type': 'lessthan', 'value': 1e-6 * self.sbandlinacfields, 'limit': 32, 'weight': 200 }, # 'xband_linac fields': {'type': 'lessthan', 'value': 1e-6*self.xbandlinacfields, 'limit': 100, 'weight': 100}, '4hc field': { 'type': 'lessthan', 'value': 1e-6 * fhcfield, 'limit': 35, 'weight': 100 }, # 'horizontal emittance': {'type': 'lessthan', 'value': emitx, 'limit': 2, 'weight': 0}, # 'vertical emittance': {'type': 'lessthan', 'value': emity, 'limit': 2, 'weight': 0}, # 'momentum_spread': {'type': 'lessthan', 'value': fitp, 'limit': 0.1, 'weight': 2}, 'chirp': { 'type': 'equalto', 'value': abs(chirp), 'limit': 0.75, 'weight': 5 }, 'correct_chirp': { 'type': 'lessthan', 'value': chirp, 'limit': 0, 'weight': 100 }, 'peakI_volume': { 'type': 'greaterthan', 'value': peakIDensity, 'limit': 1e32, 'weight': 5 }, 'volume': { 'type': 'greaterthan', 'value': density, 'limit': 1e30, 'weight': 5 }, } # self.twiss.read_astra_emit_files(self.dirname+'/S07.Zemit.001') # constraintsList5 = { # 'last_exn_5': {'type': 'lessthan', 'value': 1e6*self.twiss['enx'], 'limit': 0.75, 'weight': 1}, # 'last_eyn_5': {'type': 'lessthan', 'value': 1e6*self.twiss['eny'], 'limit': 0.75, 'weight': 1}, # } # constraintsList = merge_two_dicts(constraintsList, constraintsList5) fitness = self.cons.constraints(constraintsList) if self.verbose: print self.cons.constraintsList(constraintsList) if self.summary: np.save('summary_constraints.txt', self.cons.constraintsList(constraintsList)) # self.astra.createHDF5Summary(reference='Longitudinal_GA') print fitness, 1e-6 * peakIMomentum, abs( peakI ), 1e6 * peakIEmittanceX, 1e6 * peakIEmittanceY, chirp, peakIDensity, density return fitness except Exception as e: print(e) return 1e6
class fitnessFunc(): def __init__(self, args, tempdir, scaling=4, overwrite=True, verbose=False, summary=False): self.cons = constraintsClass() self.beam = rbf.beam() self.twiss = rtf.twiss() self.tmpdir = tempdir self.verbose = verbose self.summary = summary self.parameters = list(args) self.dirname = os.path.basename(self.tmpdir) self.framework = Framework(self.dirname, clean=False) self.framework.loadSettings('Lattices/clara400_v12_elegant.def') if not os.name == 'nt': self.framework.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) self.framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) self.framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) self.framework.generator.number_of_particles = 2**(3*scaling) else: self.framework.generator.number_of_particles = 2**(3*3) self.framework.setElementType('quadrupole','k1', self.parameters) def between(self, value, minvalue, maxvalue, absolute=True): if absolute: result = max([minvalue,min([maxvalue,abs(value)])]) else: result = np.sign(value)*max([minvalue,min([maxvalue,abs(value)])]) return result def calculateBeamParameters(self): twiss = self.twiss try: self.framework.track(run=True) constraintsList = {} constraintsListQuads = { 'max_k': {'type': 'lessthan', 'value': [abs(p) for p in self.parameters], 'limit': 2.5, 'weight': 10}, } constraintsList = merge_two_dicts(constraintsList, constraintsListQuads) twiss.read_astra_emit_files( [ self.dirname+'/'+n+'.Zemit.001' for n in self.framework.fileSettings.keys() if self.framework.fileSettings[n]['code'].upper() == 'ASTRA'] ) constraintsListSigmas = { 'max_xrms': {'type': 'lessthan', 'value': 1e3*twiss['sigma_x'], 'limit': 1, 'weight': 10}, 'max_yrms': {'type': 'lessthan', 'value': 1e3*twiss['sigma_y'], 'limit': 1, 'weight': 10}, 'min_xrms': {'type': 'greaterthan', 'value': 1e3*twiss['sigma_x'], 'limit': 0.1, 'weight': 10}, 'min_yrms': {'type': 'greaterthan', 'value': 1e3*twiss['sigma_y'], 'limit': 0.1, 'weight': 10}, 'last_exn': {'type': 'lessthan', 'value': 1e6*twiss['enx'][-1], 'limit': 0.6, 'weight': 1}, 'last_eyn': {'type': 'lessthan', 'value': 1e6*twiss['eny'][-1], 'limit': 0.6, 'weight': 1}, } constraintsList = merge_two_dicts(constraintsList, constraintsListSigmas) twiss.read_astra_emit_files(self.dirname+'/S07.Zemit.001') tdc_position = self.framework['CLA-S07-TDC-01-R']['position_start'][2] tdc_screen_position = self.framework['CLA-S07-DIA-SCR-03-W']['position_start'][2] dechirper_position = self.framework['CLA-S07-DCP-01']['position_start'][2] constraintsListS07 = { 'tdc_phase_advance': {'type': 'equalto', 'value': twiss.interpolate(tdc_screen_position,'muy') - twiss.interpolate(tdc_position,'muy'), 'limit': 0.25, 'weight': 1}, 'tdc_screen_beta_y': {'type': 'greaterthan', 'value': twiss.extract_values('beta_y', tdc_position, tdc_screen_position), 'limit': 5, 'weight': 1}, 'dechirper_sigma_x': {'type': 'equalto', 'value': 1e3*twiss.interpolate(dechirper_position, 'sigma_x'), 'limit': 0.1, 'weight': 10}, 'dechirper_sigma_y': {'type': 'equalto', 'value': 1e3*twiss.interpolate(dechirper_position, 'sigma_y'), 'limit': 0.1, 'weight': 10}, 'dechirper_sigma_xy': {'type': 'equalto', 'value': 1e3*twiss.interpolate(dechirper_position, 'sigma_y') - 1e3*twiss.interpolate(dechirper_position, 'sigma_x'), 'limit': 0.0, 'weight': 20}, } constraintsList = merge_two_dicts(constraintsList, constraintsListS07) fitness = self.cons.constraints(constraintsList) if self.verbose: print self.cons.constraintsList(constraintsList) if self.summary: self.astra.createHDF5Summary(reference='Transverse_GA') return fitness except: return 1e6
# if not os.name == 'nt': # framework.defineGeneratorCommand(['/opt/ASTRA/generator']) # framework.defineASTRACommand(['mpiexec','-np',str(ncpu),'/opt/ASTRA/astra_MPICH2.sh']) # framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(ncpu),'/opt/CSRTrack/csrtrack_openmpi.sh']) # framework.defineElegantCommand(['elegant']) # # framework.loadSettings('Lattices/clara400_v12_v3.def') # framework['VBC'].file_block['input']['prefix'] = '../basefiles_5/' # framework.track(startfile='VBC', endfile='S07') ################################ ELEGANT ###################################### framework = Framework('VBC_Elegant') if not os.name == 'nt': # framework.defineGeneratorCommand(['/opt/ASTRA/generator']) framework.defineASTRACommand(ncpu=ncpu) framework.defineCSRTrackCommand(ncpu=ncpu) framework.loadSettings('Lattices/clara400_v12_v3.def') framework.change_Lattice_Code('VBC', 'elegant') framework.change_Lattice_Code('S06', 'elegant') framework.change_Lattice_Code('L04', 'elegant') framework.change_Lattice_Code('S07', 'elegant') framework['VBC'].file_block['input']['prefix'] = '../basefiles_5/' framework.track(startfile='VBC', endfile='S07') ################################ ASTRA ###################################### framework = Framework('Phase_Comparison_ASTRA') if not os.name == 'nt': # framework.defineGeneratorCommand(ncpu=ncpu)
import sys, os sys.path.append('../../../') from SimulationFramework.Framework import * lattice = Framework('./ASTRA', clean=False, verbose=True) lattice.loadSettings('Lattices/CLA10-BA1.def') if not os.name == 'nt': scaling = 5 lattice.defineASTRACommand( ['mpiexec', '-np', str(3 * scaling), '/opt/ASTRA/astra_MPICH2.sh']) # lattice.defineASTRACommand(['/opt/ASTRA/astra.sh']) lattice.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) lattice.defineCSRTrackCommand([ '/opt/OpenMPI-1.4.3/bin/mpiexec', '-n', str(3 * scaling), '/opt/CSRTrack/csrtrack_openmpi.sh' ]) lattice.defineElegantCommand(['elegant']) lattice['S02'].file_block['input']['prefix'] = '../../basefiles_5/' lattice.generator.particles = 2**(3 * 3) lattice.track(startfile='C2V')
def optimise_Lattice(phase=4, q=70, do_optimisation=False): global dir, lattice, scaling, bestdelta bestdelta = 1e10 dir = './SETUP/TOMP_SETUP' lattice = Framework(dir, clean=False, verbose=False) lattice.loadSettings('CLA10-BA1_TOMP_ASTRA.def') lattice.change_Lattice_Code('S02', 'elegant') lattice.change_Lattice_Code('C2V', 'elegant') scaling = 6 if not os.name == 'nt': lattice.defineASTRACommand(scaling=(scaling)) lattice.defineCSRTrackCommand(scaling=(scaling)) lattice.define_gpt_command(scaling=(scaling)) lattice['L01'].file_block['input']['prefix'] = '../basefiles_' + str( scaling) + '_' + str(q) + '/' quads = 0.107 * np.array([ 21.11058462, -11.36377551, 24.69336696, -22.63264054, 56.07039682, -51.58739658 ]) quads = [ # lattice.getElement('CLA-S02-MAG-QUAD-01', 'k1l'), # lattice.getElement('CLA-S02-MAG-QUAD-02', 'k1l'), # lattice.getElement('CLA-S02-MAG-QUAD-03', 'k1l'), # lattice.getElement('CLA-S02-MAG-QUAD-04', 'k1l'), # lattice.getElement('CLA-C2V-MAG-QUAD-01', 'k1l'), # lattice.getElement('CLA-C2V-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-07', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-08', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-09', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-10', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-11', 'k1l'), lattice.getElement('EBT-INJ-MAG-QUAD-15', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-01', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-02', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-03', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-04', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-05', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-06', 'k1l'), lattice.getElement('EBT-BA1-MAG-QUAD-07', 'k1l'), ] quads = np.array([ 1.813883341270411, -1.657984997152041, 2.436078305927638, -1.3805803719324001, 5.757521138743945, -4.774512264530579, 1.5759640821849459, -1.5658635139945611, 0.4665090704522008, -0.3701448642096947, 0.14139459095219498, 0.1294045271940972, -0.26170990073523187, -0.972243028906357, 1.2999713197810956, 1.3880410074706666, 0.043801044316032774, -1.3992446074305926, 1.3604179976621753 ]) quadsCHICANE = quads[:6] # quadsCHICANE = np.array([ 1.81966806, -1.60694003, 2.52387281, -1.34318408, 5.75696896, # -4.86231975,]) lattice['S02'].sample_interval = 2**(3 * 3) if do_optimisation: if phase is not None: lattice['S02'].file_block['input'][ 'prefix'] = '../TOMP_SETUP_' + str(phase) + '_' + str(q) + '/' lattice['S02'].file_block['output']['end_element'] = 'CLA-C2V-MARK-02' val = optFuncChicane(quadsCHICANE) quadsCHICANE = setChicane(quadsCHICANE) val = optFuncChicane(quadsCHICANE) print val print('Chicane = ', quadsCHICANE) quads[:6] = quadsCHICANE # exit() lattice['S02'].file_block['output'][ 'end_element'] = 'EBT-BA1-DIA-FCUP-01' val = optFuncVELA(quads) while val > 10: print('################ CURRENT VALUE OF OPTIMISATION IS ', val, ' ##################') quads = setVELA(quads) val = optFuncVELA(quads) else: val = optFuncVELA(quads) lattice['S02'].file_block['output']['end_element'] = 'EBT-BA1-DIA-FCUP-01' lattice['S02'].sample_interval = 1 return quads
class fitnessFunc(): def __init__(self, args, tempdir, scaling=4, overwrite=True, verbose=False, summary=False, clean=False): self.cons = constraintsClass() self.beam = rbf.beam() self.twiss = rtf.twiss() self.tmpdir = tempdir self.scaling = scaling self.verbose = verbose self.summary = summary self.parameters = list(args) self.dirname = os.path.basename(self.tmpdir) self.framework = Framework(self.dirname, clean=clean, verbose=False) self.framework.loadSettings('Lattices/claraX400_v12_80MVm.def') if not os.name == 'nt': self.framework.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) self.framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) self.framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) # self.framework.defineElegantCommand(['mpiexec','-np',str(3*scaling),'Pelegant']) # else: self.framework.defineElegantCommand(['elegant']) self.framework.setElementType('quadrupole','k1', self.parameters) def between(self, value, minvalue, maxvalue, absolute=True): if absolute: result = max([minvalue,min([maxvalue,abs(value)])]) else: result = np.sign(value)*max([minvalue,min([maxvalue,abs(value)])]) return result def calculateBeamParameters(self): twiss = self.twiss # try: startS = self.framework['L02'].startObject['position_start'][2] self.framework['L02'].file_block['input']['prefix'] = '../../basefiles_'+str(self.scaling)+'/' self.framework.track() constraintsList = {} constraintsListQuads = { 'max_k': {'type': 'lessthan', 'value': [abs(p) for p in self.parameters], 'limit': 2.5, 'weight': 10}, } # constraintsList = merge_two_dicts(constraintsList, constraintsListQuads) twiss.reset_dicts() twiss.read_astra_emit_files( self.dirname+'/'+'L04.Xemit.001' ) constraintsListSigmas = { 'max_xrms': {'type': 'lessthan', 'value': 1e3*twiss['sigma_x'], 'limit': 1, 'weight': 10}, 'max_yrms': {'type': 'lessthan', 'value': 1e3*twiss['sigma_y'], 'limit': 1, 'weight': 10}, 'min_xrms': {'type': 'greaterthan', 'value': 1e3*twiss['sigma_x'], 'limit': 0.1, 'weight': 0}, 'min_yrms': {'type': 'greaterthan', 'value': 1e3*twiss['sigma_y'], 'limit': 0.1, 'weight': 0}, 'last_exn': {'type': 'lessthan', 'value': 1e6*twiss['enx'][-1], 'limit': 0.6, 'weight': 1}, 'last_eyn': {'type': 'lessthan', 'value': 1e6*twiss['eny'][-1], 'limit': 0.6, 'weight': 1}, } # constraintsList = merge_two_dicts(constraintsList, constraintsListSigmas) # tdc_position = self.framework['CLA-S07-TDC-01-R']['position_start'][2] # tdc_screen_position = self.framework['CLA-S07-DIA-SCR-03-W']['position_start'][2] # dechirper_position = self.framework['CLA-S07-DCP-01']['position_start'][2] # constraintsListS07 = { # 'tdc_phase_advance': {'type': 'equalto', 'value': (twiss.interpolate(tdc_screen_position,'muy', index='s') - twiss.interpolate(tdc_position,'muy', index='s')) % 0.25, 'limit': 0, 'weight': 1}, # 'tdc_screen_beta_y': {'type': 'greaterthan', 'value': twiss.extract_values('betay', tdc_position, tdc_screen_position), 'limit': 5, 'weight': 1}, # 'dechirper_sigma_x': {'type': 'lessthan', 'value': 1e3*twiss.interpolate(dechirper_position, 'Sx', index='s'), 'limit': 0.1, 'weight': 10}, # 'dechirper_sigma_y': {'type': 'lessthan', 'value': 1e3*twiss.interpolate(dechirper_position, 'Sy', index='s'), 'limit': 0.1, 'weight': 10}, # 'dechirper_sigma_xy': {'type': 'equalto', 'value': 1e3*twiss.interpolate(dechirper_position, 'Sy', index='s'), 'limit': 1e3*twiss.interpolate(dechirper_position, 'Sx', index='s'), 'weight': 35}, # } # constraintsList = merge_two_dicts(constraintsList, constraintsListS07) fitness = self.cons.constraints(constraintsList) if self.verbose: print self.cons.constraintsList(constraintsList) if self.summary: self.framework.createHDF5Summary(reference='Transverse_GA') return fitness
class fitnessFunc(): def __init__(self, args, tempdir, scaling=4, overwrite=True, verbose=False, summary=False, clean=False): global preparameters, variables self.cons = constraintsClass() self.beam = rbf.beam() self.twiss = rtf.twiss() self.tmpdir = tempdir self.scaling = scaling self.verbose = verbose self.summary = summary self.parameters = preparameters + list(args) self.dirname = os.path.basename(self.tmpdir) self.framework = Framework(self.dirname, clean=clean, verbose=False) self.framework.loadSettings('Lattices/clara400_v12_FEBE.def') if not os.name == 'nt': self.framework.defineASTRACommand(['mpiexec','-np',str(3*scaling),'/opt/ASTRA/astra_MPICH2.sh']) self.framework.defineGeneratorCommand(['/opt/ASTRA/generator.sh']) self.framework.defineCSRTrackCommand(['/opt/OpenMPI-1.4.3/bin/mpiexec','-n',str(3*scaling),'/opt/CSRTrack/csrtrack_openmpi.sh']) # self.framework.defineElegantCommand(['mpiexec','-np',str(3*scaling),'Pelegant']) # else: self.framework.defineElegantCommand(['elegant']) # self.framework.setElementType('quadrupole','k1', self.parameters) [setattr(a, 'k1', 2*a['k1']) for a in variables] def between(self, value, minvalue, maxvalue, absolute=True): if absolute: result = max([minvalue,min([maxvalue,abs(value)])]) else: result = np.sign(value)*max([minvalue,min([maxvalue,abs(value)])]) return result def calculateBeamParameters(self): twiss = self.twiss # try: startS = self.framework['S07'].startObject['position_start'][2] self.framework['S07'].file_block['input']['prefix'] = '../../basefiles_'+str(self.scaling)+'/' self.framework.track(track=True, startfile='S07') constraintsList = {} constraintsListQuads = { 'max_k': {'type': 'lessthan', 'value': [abs(p) for p in self.parameters], 'limit': 2.5, 'weight': 10}, } # constraintsList = merge_two_dicts(constraintsList, constraintsListQuads) twiss.reset_dicts() twiss.read_sdds_file( self.dirname+'/'+'FEBE.twi' ) twiss.read_sdds_file( self.dirname+'/'+'FEBE.sig' ) ip_position = self.framework['FEBE'].findS('CLA-FEB-W-FOCUS-01')[0][1] constraintsListS07 = { 'dechirper_sigma_x': {'type': 'lessthan', 'value': 1e3*twiss.interpolate(ip_position, 'betax', index='s'), 'limit': 0.1, 'weight': 10}, 'dechirper_sigma_y': {'type': 'lessthan', 'value': 1e3*twiss.interpolate(ip_position, 'betay', index='s'), 'limit': 0.1, 'weight': 10}, } constraintsList = merge_two_dicts(constraintsList, constraintsListS07) fitness = self.cons.constraints(constraintsList) if self.verbose: print(self.cons.constraintsList(constraintsList)) if self.summary: self.framework.createHDF5Summary(reference='Transverse_GA') return fitness