Exemplo n.º 1
0
def create_predefined(out_dir=None):
    from sirepo.template import srw_common
    import sirepo.sim_data
    import srwl_uti_src
    import pykern.pkjson

    sim_data = sirepo.sim_data.get_class('srw')
    beams = []
    for beam in srwl_uti_src.srwl_uti_src_e_beam_predef():
        info = beam[1]
        # _Iavg, _e, _sig_e, _emit_x, _beta_x, _alpha_x, _eta_x, _eta_x_pr, _emit_y, _beta_y, _alpha_y
        beams.append(
            srw_common.process_beam_parameters(
                PKDict(
                    name=beam[0],
                    current=info[0],
                    energy=info[1],
                    rmsSpread=info[2],
                    horizontalEmittance=sim_data.srw_format_float(info[3] *
                                                                  1e9),
                    horizontalBeta=info[4],
                    horizontalAlpha=info[5],
                    horizontalDispersion=info[6],
                    horizontalDispersionDerivative=info[7],
                    verticalEmittance=sim_data.srw_format_float(info[8] * 1e9),
                    verticalBeta=info[9],
                    verticalAlpha=info[10],
                    verticalDispersion=0,
                    verticalDispersionDerivative=0,
                    energyDeviation=0,
                    horizontalPosition=0,
                    verticalPosition=0,
                    drift=0.0,
                    isReadOnly=True,
                )), )

    def f(file_type):
        return sim_data.srw_lib_file_paths_for_type(
            file_type,
            lambda f: PKDict(fileName=f.basename),
            want_user_lib_dir=False,
        )

    p = srw_common.PREDEFINED_JSON
    p = pkio.py_path(out_dir).join(p) if out_dir else sim_data.resource_path(p)
    pykern.pkjson.dump_pretty(
        PKDict(
            beams=beams,
            magnetic_measurements=f('undulatorTable'),
            mirrors=f('mirror'),
            sample_images=f('sample'),
        ),
        filename=p,
    )
    return 'Created {}'.format(p)
Exemplo n.º 2
0
def process_beam_parameters(ebeam):
    import srwlib
    import sirepo.sim_data

    sim_data = sirepo.sim_data.get_class('srw')

    def _convert_ebeam_units(field_name, value, to_si=True):
        """Convert values from the schema to SI units (m, rad) and back.

        Args:
            field_name: name of the field in _SCHEMA['model']['electronBeam'].
            value: value of the field.
            to_si: if set to True, convert to SI units, otherwise convert back to the units in the schema.

        Returns:
            value: converted value.
        """
        def _invert_value(value):
            return value**-1 if to_si else value

        s = sim_data.schema()
        if field_name in s['model']['electronBeam'].keys():
            label, field_type = s['model']['electronBeam'][field_name]
            if field_type == 'Float':
                if re.search(r'\[m(m|rad)\]', label):
                    value *= _invert_value(1e3)
                elif re.search(r'\[\xb5(m|rad)\]', label):  # mu
                    value *= _invert_value(1e6)
                elif re.search(r'\[n(m|rad)\]', label):
                    value *= _invert_value(1e9)
        return value

    # if the beamDefinition is "twiss", compute the moments fields and set on ebeam
    moments_fields = [
        'rmsSizeX', 'xxprX', 'rmsDivergX', 'rmsSizeY', 'xxprY', 'rmsDivergY'
    ]
    for k in moments_fields:
        if k not in ebeam:
            ebeam[k] = 0
    if 'beamDefinition' not in ebeam:
        ebeam['beamDefinition'] = 't'

    if ebeam['beamDefinition'] == 't':  # Twiss
        model = copy.deepcopy(ebeam)
        # Convert to SI units to perform SRW calculation:
        for k in model:
            model[k] = _convert_ebeam_units(k, ebeam[k])
        beam = srwlib.SRWLPartBeam()
        beam.from_Twiss(
            _e=model['energy'],
            _sig_e=model['rmsSpread'],
            _emit_x=model['horizontalEmittance'],
            _beta_x=model['horizontalBeta'],
            _alpha_x=model['horizontalAlpha'],
            _eta_x=model['horizontalDispersion'],
            _eta_x_pr=model['horizontalDispersionDerivative'],
            _emit_y=model['verticalEmittance'],
            _beta_y=model['verticalBeta'],
            _alpha_y=model['verticalAlpha'],
            _eta_y=model['verticalDispersion'],
            _eta_y_pr=model['verticalDispersionDerivative'],
        )
        # copy moments values into the ebeam
        for i, k in enumerate(moments_fields):
            v = beam.arStatMom2[i] if k in ['xxprX', 'xxprY'
                                            ] else beam.arStatMom2[i]**0.5
            ebeam[k] = sim_data.srw_format_float(
                _convert_ebeam_units(k, v, to_si=False))
    return ebeam