def __init__(self):
     super(Optimise_Genesis_Elegant, self).__init__()
     self.cons = constraintsClass()
     self.changes = None
     self.lattice = None
     self.resultsDict = {}
     self.opt_iteration = 0
     self.bestfit = 1e26
     # ******************************************************************************
     CLARA_dir = os.path.relpath(__file__ + '/../../')
     self.POST_INJECTOR = True
     CREATE_BASE_FILES = False
     scaling = 5
     if self.POST_INJECTOR and CREATE_BASE_FILES:
         self.optfunc = partial(self.OptimisingFunction,
                                scaling=scaling,
                                post_injector=self.POST_INJECTOR,
                                verbose=False,
                                basefiles='../basefiles_' + str(scaling) +
                                '/',
                                CLARA_dir=CLARA_dir)
     else:
         self.optfunc = partial(self.OptimisingFunction,
                                scaling=scaling,
                                post_injector=self.POST_INJECTOR,
                                verbose=False,
                                CLARA_dir=CLARA_dir)
     # ******************************************************************************
     if not self.POST_INJECTOR:
         best = injector_startingvalues + best
     elif CREATE_BASE_FILES:
         for i in [scaling]:
             pass
 def __init__(self):
     super(Optimise_Genesis_Elegant, self).__init__()
     self.cons = constraintsClass()
     self.changes = None
     self.lattice = None
     self.resultsDict = {}
     self.opt_iteration = 0
     self.bestfit = 1e26
     self.beam_file = 'CLA-S07-APER-01.hdf5'
     # ******************************************************************************
     CLARA_dir = os.path.relpath(__file__ + '/../../')
Example #3
0
def optfunc(inputargs, verbose=False, *args, **kwargs):
    global startingvalues, global_best
    process = multiprocessing.current_process()
    runno = process.pid
    # inputargs = [a*b for a,b in zip(startingvalues,inputargsmult)]
    # print 'inputargs = ', inputargs
    with runEle.TemporaryDirectory(dir=os.getcwd()) as tmpdir:
        try:
            fit = runEle.fitnessFunc(inputargs, tmpdir, *args, **kwargs)
            fitvalue = fit.calculateBeamParameters()
            sys.stdout = open(tmpdir + '/' + 'std.out', 'w')
            sys.stderr = open(tmpdir + '/' + 'std.err', 'w')
            e, b = evalBeamWithGenesis(tmpdir, dirno=runno)
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            if e > global_best:
                global_best = e
                print 'New Best: ', global_best, 1e4 * e, 1e2 * b, inputargs
            if verbose:
                print 'bandwidth = ', 1e2 * b
                print 'pulse energy =', 1e4 * e
            constraintsList = {
                'energy': {
                    'type': 'greaterthan',
                    'value': abs(1e4 * e),
                    'limit': 3,
                    'weight': 1
                },
                'bandwidth': {
                    'type': 'lessthan',
                    'value': abs(1e2 * b),
                    'limit': 1,
                    'weight': 1
                },
            }
            cons = constraintsClass()
            fitness = cons.constraints(constraintsList)
            saveState(inputargs, fitness, e, b)
            # print cons.constraintsList(constraintsList)

            return 1e4 * e, 1e2 * b
        except Exception as e:
            print 'Error! ', e
            return 0, 10
Example #4
0
 def __init__(self):
     super(Optimise_Elegant, self).__init__()
     self.cons = constraintsClass()
     self.changes = None
     self.lattice = None
     self.resultsDict = {}
     # ******************************************************************************
     self.CLARA_dir = os.path.relpath(__file__ + '/../../CLARA')
     self.POST_INJECTOR = True
     CREATE_BASE_FILES = False
     self.scaling = 5
     self.verbose = False
     self.basefiles = '../../CLARA/basefiles_' + str(self.scaling) + '/'
     # ******************************************************************************
     if not self.POST_INJECTOR:
         best = injector_startingvalues + best
     elif CREATE_BASE_FILES:
         for i in [self.scaling]:
             pass
def optfunc(inputargs, *args, **kwargs):
    idclient = idn.zmqClient()
    n = idclient.get_id()  #str(uuid.uuid4())
    # kwargs = args[0]
    # print 'optfunc kwargs = ', kwargs

    cons = constraintsClass()
    if 'dir' in kwargs.keys():
        dir = kwargs['dir']
        del kwargs['dir']
        e, b, ee, be, l, g = genesisBeamFile.optfunc(inputargs,
                                                     dir=dir,
                                                     **kwargs)
    else:
        e, b, ee, be, l, g = genesisBeamFile.optfunc(inputargs,
                                                     dir='de/de_iteration_' +
                                                     str(n),
                                                     **kwargs)
    e = 1e2 * e
    ee = 1e2 * ee
    pos12m = list(g.z).index(10.)
    pos20m = list(g.z).index(20.)
    momentum = 1e-6 * np.mean(g.momentum)
    print 'Momentum = ', momentum
    if e < 1:
        l = 500
    constraintsList = {
        'bandwidth': {
            'type': 'lessthan',
            'value': b,
            'limit': 0.25,
            'weight': 3
        },
        'pulse_energy': {
            'type': 'greaterthan',
            'value': e,
            'limit': 120,
            'weight': 4
        },
        'bandwidth_end': {
            'type': 'lessthan',
            'value': 1e2 * abs(g.spectrum_lamdwidth_std[pos20m]),
            'limit': 0.4,
            'weight': 1
        },
        'pulse_energy_end': {
            'type': 'greaterthan',
            'value': 1e6 * abs(g.energy[pos20m]),
            'limit': 250,
            'weight': 2
        },
        'max_brightness_position': {
            'type': 'lessthan',
            'value': abs(l),
            'limit': 10,
            'weight': 2
        },
        'energy': {
            'type': 'greaterthan',
            'value': abs(momentum),
            'limit': 240,
            'weight': 2
        },
    }
    fitvalue = cons.constraints(constraintsList)
    print cons.constraintsList(constraintsList)
    print 'fitvalue[', n, '] = ', fitvalue
    try:
        saveState(inputargs, n, fitvalue)
    except Exception as e:
        print 'Error! ', e
    return fitvalue