Esempio n. 1
0
def prepare_el_optics(beam, lat_pkg, E_photon=None, beta_av=30, s=None):
    from ocelot.rad.undulator_params import Ephoton2K
    # if s is None:
    # jj = beam.I / (beam.beta_x * beam.beta_y * beam.emit_x * beam.emit_y)
    # jj = beam.I
    # s = beam.s[jj.argmax()]

    if s is None:
        beam_match = get_beam_peak(beam)
    else:
        beam_match = beam.get_s(s)

    # if beamline == 'SASE1':
    # = create_exfel_sase1_lattice()
    # elif beamline == 'SASE2':
    # lat_pkg = create_exfel_sase2_lattice()
    # elif beamline == 'SASE3':
    # lat_pkg = create_exfel_sase3_lattice()
    # else:
    # raise ValueError('unknown beamline')

    rematch_beam_lat(beam_match, lat_pkg, beta_av)
    transform_beam_twiss(beam, Twiss(beam_match), s=s)
    lat = lat_pkg[0]
    indx_und = np.where([i.__class__ == Undulator for i in lat.sequence])[0]
    und = lat.sequence[indx_und[0]]
    if E_photon is not None:
        und.Kx = Ephoton2K(E_photon, und.lperiod, beam_match.E)
    def testPrepareRun(self):
        """ Tests the method that sets up input files and directories for a genesis run. """

        # Ensure proper cleanup.
        self.__dirs_to_remove.append('source')

        # Get SASE1 template undulator object.
        undulator = sase1.und
        photon_energy = 200.0 # eV
        electron_energy = 16.0e-3 # GeV
        undulator.Kx = Ephoton2K(photon_energy, undulator.lperiod, electron_energy)

        # Calculate undulator-radiator parameters.
        undulator_parameters = UndulatorParameters(undulator, electron_energy)


        # Setup parameters.
        parameters_dict = {
                'time_averaging_window': 1e-8,
                'is_time_dependent': False,
                'undulator_parameters': undulator_parameters,
                }

        # Construct the object.
        xfel_source = GenesisPhotonSource(parameters=parameters_dict, input_path=TestUtilities.generateTestFilePath('simData_8000.h5'), output_path='source')

        # Read the input distribution.
        xfel_source._readH5()

        # Prepare the run.
        xfel_source._prepareGenesisRun()

        # Check generated data.
        self.assertIsInstance( xfel_source._GenesisPhotonSource__genesis_input, genesis.GenesisInput )
        self.assertIsInstance( xfel_source._GenesisPhotonSource__genesis_beam, genesis.GenesisBeam )
Esempio n. 3
0
def prepare_el_optics(beam, lat_pkg, E_photon=None, beta_av=None, s=None):
    from ocelot.rad.undulator_params import Ephoton2K
    # if s is None:
    # jj = beam.I / (beam.beta_x * beam.beta_y * beam.emit_x * beam.emit_y)
    # jj = beam.I
    # s = beam.s[jj.argmax()]

    if s is None:
        beam_match = get_beam_peak(beam)
    else:
        beam_match = beam.get_s(s)

    lat, extra_fodo, cell = lat_pkg
    indx_und = np.where([i.__class__ == Undulator for i in lat.sequence])[0]
    und = lat.sequence[indx_und[0]]
    l_fodo = MagneticLattice(cell).totalLen / 2

    if E_photon is not None:
        und.Kx = Ephoton2K(E_photon, und.lperiod, beam_match.E)
        if np.isnan(und.Kx):
            gamma = beam_match.E / m_e_GeV
            Emax = 2 * gamma**2 / und.lperiod * h_eV_s * speed_of_light
            _logger.error(
                'requested photon energy {}eV is beyond reach for given lperiod {}m and beam energy {}GeV\nmax energy for given parameters is {}'
                .format(E_photon, und.lperiod, beam_match.E, Emax))
            raise ValueError(
                'requested photon energy is beyond reach for given parameters')

    if beta_av is None:
        l_period = und.lperiod
        K_peak = np.max([und.Kx, und.Ky])
        if und.Kx != und.Ky:
            iwityp = 0  # planar undulator
        elif und.Kx == und.Ky:
            iwityp = 1  # helical undulator
        else:
            raise ValueError(
                'unknown undulator: neither planar nor helical, estimation method not applicable'
            )
        fel = beam2fel(beam_match, l_period, K_peak, iwityp=iwityp)
        beta_av = fel.beta_opt(apply=0, method=fel.method)
        if beta_av <= l_fodo * 2:
            beta_av = l_fodo * 2 * 1.001
        raise_min_beta = True
        _logger.info(
            'optimal minimal average beta function was estimated and set to {:.3f}m'
            .format(beta_av))
    else:
        raise_min_beta = False

    rematch_beam_lat(beam_match,
                     lat_pkg,
                     beta_av,
                     raise_min_beta=raise_min_beta)
    transform_beam_twiss(beam, Twiss(beam_match), s=s)
    def testBackengine(self):
        """ Testing the read function and conversion of openpmd input to native beam file."""

        # Ensure proper cleanup.
        #self.__dirs_to_remove.append('source')

        # Get SASE1 template undulator object.
        undulator = sase1.und
        photon_energy = 200.0  # eV
        electron_energy = 16.0e-3  # GeV
        undulator.Kx = Ephoton2K(photon_energy, undulator.lperiod,
                                 electron_energy)

        # Calculate undulator-radiator parameters.
        undulator_parameters = UndulatorParameters(undulator, electron_energy)

        # Setup parameters.
        parameters_dict = {
            'time_averaging_window': 1e-8,
            'is_time_dependent': False,
            'undulator_parameters': undulator_parameters,
        }

        # Construct the object.
        xfel_source = GenesisPhotonSource(
            parameters=parameters_dict,
            input_path=TestUtilities.generateTestFilePath('simData_8000.h5'),
            output_path='source')

        # Read the input distribution.
        xfel_source._readH5()

        # Prepare the run.
        xfel_source._prepareGenesisRun()

        # This should not throw.
        try:
            xfel_source.backengine()
            throws = False
        except:
            throws = True

        self.assertFalse(throws)
Esempio n. 5
0
def prepare_el_optics(beam, lat_pkg, E_photon=None, beta_av=30):
    from ocelot.rad.undulator_params import Ephoton2K
    beam_pk = beam.pk()

    # if beamline == 'SASE1':
    # = create_exfel_sase1_lattice()
    # elif beamline == 'SASE2':
    # lat_pkg = create_exfel_sase2_lattice()
    # elif beamline == 'SASE3':
    # lat_pkg = create_exfel_sase3_lattice()
    # else:
    # raise ValueError('unknown beamline')

    rematch_beam_lat(beam_pk, lat_pkg, beta_av)
    transform_beam_twiss(beam, Twiss(beam_pk))
    lat = lat_pkg[0]
    indx_und = np.where([i.__class__ == Undulator for i in lat.sequence])[0]
    und = lat.sequence[indx_und[0]]
    if E_photon is not None:
        und.Kx = Ephoton2K(E_photon, und.lperiod, beam_pk.E)
def undulator_design(A_contents):
    from ocelot.cpbd.elements import Drift, Quadrupole, Undulator
    from ocelot.cpbd.magnetic_lattice import MagneticLattice
    from ocelot.common import globals
    from ocelot.common.globals import m_e_GeV, speed_of_light, h_eV_s
    from ocelot.rad.undulator_params import Ephoton2K
    from ocelot.rad.undulator_params import UndulatorParameters
    import numpy as np

    ## Taking it from the Notebook
    xlamd = A_contents['xlamd']
    nwig = A_contents['nwig']
    E_beam = A_contents['gamma0'] * m_e_GeV
    E_photon = h_eV_s * speed_of_light / float(A_contents['xlamds'])
    p_beam = np.sqrt(E_beam**2 - m_e_GeV**2)
    fl = A_contents['fl']
    dl = A_contents['dl']
    drl = A_contents['drl']
    quadf = A_contents['quadf']
    quadd = -A_contents['quadd']
    nsec = A_contents['nsec']
    drl = int((fl + drl - nwig) / 2) - 1
    kw0 = float(A_contents['aw0']) / sqrt(0.5)

    # Instance of the Undulator object
    und = Undulator(lperiod=xlamd, nperiods=nwig, Kx=kw0)

    # Calculation of the Undulator parameter from the Photon and Beam Energies)
    if (A_contents['i_scan'] == 1
            and A_contents['parameter'] == 'aw0') or (A_contents['in_gen']
                                                      == 1):
        und.Kx = kw0
    else:
        if A_contents['iwityp'] == 0:
            fact_typ = 1
        elif A_contents['iwityp'] == 1:
            fact_typ = 1 / sqrt(0.5)
        und.Kx = Ephoton2K(E_photon, und.lperiod, E_beam) * fact_typ

# Drift sections (if they are the same)
    d_rift = Drift(l=drl * und.lperiod)
    d_rift2 = Drift(l=drl * und.lperiod)

    # Definition of Quads

    qf = Quadrupole(l=fl * und.lperiod, k1=0.3 * quadf / p_beam)
    qd = Quadrupole(l=dl * und.lperiod, k1=0.3 * quadd / p_beam)
    qdh = deepcopy(qf)
    qdh.l /= 2

    # Creating the cell

    extra_fodo = (und, d_rift, qdh)
    cell_ps = (und, d_rift, qd, d_rift2, und, d_rift, qf, d_rift2)
    l_fodo = MagneticLattice(cell_ps).totalLen / 2  ##Length of fodo cell
    sase3 = MagneticLattice((
        qdh,
        d_rift,
    ) + (np.ceil(nsec / 2) * cell_ps))  # Lattice with nsec modules
    up = UndulatorParameters(
        und, E_beam)  # Instance of the Class UndulatorParameters
    print('++++ Undulator Parameters +++')
    up.printParameters()
    return {'Undulator Parameters': up, 'Magnetic Lattice': sase3}