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): 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