Ejemplo n.º 1
0
    def setUp(self):
        linac = Linac()

        linac.add_beamline(
            'astra',
            name='gun',
            swd=osp.join(test_path, 'astra'),
            fin='injector.in',
            template=osp.join(test_path, 'astra/injector.in.000'),
            pout='injector.0100.001')

        linac.add_beamline(
            'impactt',
            name='matching',
            swd=osp.join(test_path, 'impactt'),
            fin='ImpactT.in',
            template=osp.join(test_path, 'impactt/ImpactT.in.000'),
            pout='fort.106',
            charge=10e-12)

        self.opt = LinacOptimization(linac)

        self.opt.add_obj('f', expr='matching.out.emitx', scale=1e6)

        self.opt.add_icon('g1', func=lambda a: a.gun.out.Sx * 1e3, ub=0.2)
        self.opt.add_icon('g2', func=lambda a: a.matching.out.betax, ub=0.2)
        self.opt.add_icon('g3', func=lambda a: a.matching.out.betay, ub=0.2)
        self.opt.add_icon(
            'g4',
            func=lambda a: abs(a.matching.out.betax - a.matching.out.betay),
            ub=0.01)

        self.opt.add_var('gun/laser_spot', value=0.1, lb=0.04, ub=0.30)
        self.opt.add_var('matching/MQZM1_G', value=0.0, lb=-6.0, ub=0.0)
        self.opt.add_var('matching/MQZM2_G', value=0.0, lb=0.0, ub=6.0)
Ejemplo n.º 2
0
    def setUp(self):
        linac = Linac()
        linac.add_beamline('astra',
                           name='gun',
                           swd=test_path,
                           fin='injector.in',
                           template=osp.join(test_path, 'injector.in.000'),
                           pout='injector.0150.001')

        self.opt = LinacOptimization(linac)
        # self.opt.printout = 1

        self.opt.add_obj('emitx_um', expr='gun.out.emitx', scale=1.e6)
        self.opt.add_var('laser_spot', value=0.1, lb=0.04, ub=0.3)
        self.opt.add_var('main_sole_b', value=0.1, lb=0.0, ub=0.4)
Ejemplo n.º 3
0
    def setUp(self):
        linac = Linac()
        linac.add_beamline('astra',
                           name='gun',
                           swd=test_path,
                           fin='injector.in',
                           template=osp.join(test_path, 'injector.in.000'),
                           pout='injector.0150.001')

        self.opt = LinacOptimization(linac)

        self.opt.add_obj('f', expr='gun.out.Sx', scale=1.e6)
        self.opt.add_icon('g1',
                          func=lambda a: a['gun'].max.emitx * 1e6,
                          ub=0.041)
        self.opt.add_econ('g2', func=lambda a: a['gun'].out.gamma, eq=10.0)

        self.opt.add_var('laser_spot', value=0.1, lb=0.04, ub=0.3)
        self.opt.add_var('main_sole_b', value=0.1, lb=0.0, ub=0.3)
        self.opt.add_var('gun_gradient', value=130, lb=90.0, ub=130.0)
Ejemplo n.º 4
0
class TestLocalOptimizer(unittest.TestCase):
    def setUp(self):
        linac = Linac()
        linac.add_beamline('astra',
                           name='gun',
                           swd=test_path,
                           fin='injector.in',
                           template=osp.join(test_path, 'injector.in.000'),
                           pout='injector.0150.001')

        self.opt = LinacOptimization(linac)
        # self.opt.printout = 1

        self.opt.add_obj('emitx_um', expr='gun.out.emitx', scale=1.e6)
        self.opt.add_var('laser_spot', value=0.1, lb=0.04, ub=0.3)
        self.opt.add_var('main_sole_b', value=0.1, lb=0.0, ub=0.4)

    def tearDown(self):
        for file in glob.glob(osp.join(test_path, "injector.*.001")):
            os.remove(file)
        try:
            os.remove(osp.join(test_path, "injector.in"))
        except FileNotFoundError:
            pass

    def test_optimization_nelderMead(self):
        optimizer = NelderMead()

        opt_f, opt_x = self.opt.solve(optimizer)
        self.assertAlmostEqual(opt_f, 0.03964, delta=0.0001)
        self.assertAlmostEqual(opt_x[0], 0.04000, delta=0.0001)
        self.assertAlmostEqual(opt_x[1], 0.23000, delta=0.01)

    @unittest.skipIf(SKIP_SDPEN_TEST is True, "Failed to import library")
    def test_optimization_sdpen(self):
        optimizer = SDPEN()

        opt_f, opt_x = self.opt.solve(optimizer)
        self.assertAlmostEqual(opt_f, 0.03964, delta=0.0001)
        self.assertAlmostEqual(opt_x[0], 0.04000, delta=0.0001)
        self.assertAlmostEqual(opt_x[1], 0.23000, delta=0.01)
Ejemplo n.º 5
0
# fin: simulation input file name
# template: simulation input template file path.
# pout: output file name. It must be in the same folder as 'fin'.
linac.add_beamline('astra',
                   name='gun',
                   swd='../astra_files',
                   fin='injector.in',
                   template='injector.in.000',
                   pout='injector.0450.001')

mapping = {
    'laser_spot': 0.1,
    'main_sole_b': 0.2,
}

opt = LinacOptimization(linac)  # instantiate Optimization (problem)

# There are two options to access a parameter of a linac:
# 1. Use a string: the string must have the form
#        beamline_name.WatchParameters_name.param_name
#    or
#        beamline_name.LineParameters_name.param_name.
# 2. Use a function object: the function has only one argument which is
#    the linac instance.

# add the objective (the horizontal emittance at the end of the 'gun' beamline)
opt.add_obj('emitx_um', expr='gun.out.emitx', scale=1e6)

# add variables with lower boundary (lb) and upper boundary (ub)
opt.add_var('laser_spot', value=0.10, lb=0.04, ub=0.3)
opt.add_var('main_sole_b', value=0.20, lb=0.00, ub=0.4)
Ejemplo n.º 6
0
class TestGlobalOptimizer(unittest.TestCase):
    def setUp(self):
        linac = Linac()
        linac.add_beamline('astra',
                           name='gun',
                           swd=test_path,
                           fin='injector.in',
                           template=osp.join(test_path, 'injector.in.000'),
                           pout='injector.0150.001')

        self.opt = LinacOptimization(linac)

        self.opt.add_obj('f', expr='gun.out.Sx', scale=1.e6)
        self.opt.add_icon('g1',
                          func=lambda a: a['gun'].max.emitx * 1e6,
                          ub=0.041)
        self.opt.add_econ('g2', func=lambda a: a['gun'].out.gamma, eq=10.0)

        self.opt.add_var('laser_spot', value=0.1, lb=0.04, ub=0.3)
        self.opt.add_var('main_sole_b', value=0.1, lb=0.0, ub=0.3)
        self.opt.add_var('gun_gradient', value=130, lb=90.0, ub=130.0)

    def tearDown(self):
        for file in glob.glob(osp.join(test_path, "injector.*.001")):
            os.remove(file)
        os.remove(osp.join(test_path, "injector.in"))

    def test_optimization(self):
        optimizer = ALPSO()

        opt_f, opt_x = self.opt.solve(optimizer)

        self.assertAlmostEqual(opt_f, 65.62, delta=0.10)
        self.assertAlmostEqual(opt_x[0], 0.040000, delta=0.00010)
        self.assertAlmostEqual(opt_x[1], 0.2521, delta=0.0010)
        self.assertAlmostEqual(opt_x[2], 100.4, delta=1.0)
Ejemplo n.º 7
0
                   template='injector.in.000',
                   pout='injector.0450.001')

# Add the second beamline
#
# Note: 'charge' is required for an ImpactT simulation but this argument
# will be ignored if the code is Astra.
linac.add_beamline('impactt',
                   name='chicane',
                   swd='../impactt_files',
                   fin='ImpactT.in',
                   template='ImpactT.in.000',
                   pout='fort.106',
                   charge=1e-15)

opt = LinacOptimization(linac)

opt.add_obj('St_betaxy',
            func=lambda a: max(a['chicane'].out.emitx * 1e6, a['chicane'].out.
                               betax, a['chicane'].out.betay))

opt.add_var('gun/laser_spot', value=0.1, lb=0.04, ub=0.30)
opt.add_var('gun/main_sole_b', value=0.1, lb=0.00, ub=0.40)
opt.add_var('chicane/MQZM1_G', value=0.0, lb=-10, ub=10)
opt.add_var('chicane/MQZM3_G', value=0.0, lb=-10, ub=10)

opt.add_covar('chicane/MQZM2_G', 'chicane/MQZM1_G', scale=-1)
opt.add_covar('chicane/MQZM4_G', 'chicane/MQZM3_G', scale=-1)

optimizer = NelderMead()
opt.solve(optimizer)
Ejemplo n.º 8
0
class TestMultiCodeOptimization(unittest.TestCase):
    def setUp(self):
        linac = Linac()

        linac.add_beamline(
            'astra',
            name='gun',
            swd=osp.join(test_path, 'astra'),
            fin='injector.in',
            template=osp.join(test_path, 'astra/injector.in.000'),
            pout='injector.0100.001')

        linac.add_beamline(
            'impactt',
            name='matching',
            swd=osp.join(test_path, 'impactt'),
            fin='ImpactT.in',
            template=osp.join(test_path, 'impactt/ImpactT.in.000'),
            pout='fort.106',
            charge=10e-12)

        self.opt = LinacOptimization(linac)

        self.opt.add_obj('f', expr='matching.out.emitx', scale=1e6)

        self.opt.add_icon('g1', func=lambda a: a.gun.out.Sx * 1e3, ub=0.2)
        self.opt.add_icon('g2', func=lambda a: a.matching.out.betax, ub=0.2)
        self.opt.add_icon('g3', func=lambda a: a.matching.out.betay, ub=0.2)
        self.opt.add_icon(
            'g4',
            func=lambda a: abs(a.matching.out.betax - a.matching.out.betay),
            ub=0.01)

        self.opt.add_var('gun/laser_spot', value=0.1, lb=0.04, ub=0.30)
        self.opt.add_var('matching/MQZM1_G', value=0.0, lb=-6.0, ub=0.0)
        self.opt.add_var('matching/MQZM2_G', value=0.0, lb=0.0, ub=6.0)

    def tearDown(self):
        for file in glob.glob(os.path.join(test_path, "astra/injector.*.001")):
            os.remove(file)
        os.remove(os.path.join(test_path, "astra/injector.in"))
        for file in glob.glob(os.path.join(test_path, "impactt/fort.*")):
            os.remove(file)
        os.remove(os.path.join(test_path, "impactt/ImpactT.in"))

    def test_optimization(self):
        optimizer = ALPSO()

        opt_f, opt_x = self.opt.solve(optimizer)

        self.assertAlmostEqual(opt_f, 0.04025, delta=0.00040)
        self.assertAlmostEqual(opt_x[0], 0.04000, delta=0.00010)
        self.assertAlmostEqual(opt_x[1], -0.78, delta=0.030)
        self.assertAlmostEqual(opt_x[2], 0.96, delta=0.040)
Ejemplo n.º 9
0
Author: Jun Zhu
"""
from liso import Linac, LinacOptimization, ALPSO
from liso.logging import logger
logger.setLevel('DEBUG')

linac = Linac()

linac.add_beamline('astra',
                   name='gun',
                   swd='../astra_files',
                   fin='injector.in',
                   template='injector.in.000',
                   pout='injector.0450.001')

opt = LinacOptimization(linac)

opt.add_obj('St', expr='gun.out.St', scale=1e15)

# equality constraint (the No. of particles at the end of the 'gun' beamline)
opt.add_econ('n_pars', expr='gun.out.n', eq=2000)
# inequality constraint (the beta [x] function at the end of the 'gun' beamline) with the upper boundary.
opt.add_icon('emitx', expr='gun.out.emitx', scale=1e6, ub=0.3)
# inequality constraint (the Lorentz factor at the end of the 'gun' beamline) with the lower boundary.
opt.add_icon('gamma', func=lambda a: a['gun'].out.gamma, lb=20.0)
# inequality constraint (the maximum beam size [x] throughout the 'gun' beamline) with upper boundary.
opt.add_icon('max_Sx', func=lambda a: a['gun'].max.Sx * 1e3, ub=3.0)

opt.add_var('laser_spot', value=0.1, lb=0.04, ub=0.5)
opt.add_var('main_sole_b', value=0.2, lb=0.00, ub=0.4)
opt.add_var('gun_phase', value=0.0, lb=-10, ub=10)