Exemplo n.º 1
0
def run(cfg_dir):
    """Run code in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run code in
    """
    template = sirepo.template.import_module(pkinspect.module_basename(run))
    with pkio.save_chdir(cfg_dir):
        _run_code()
        data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
        data_file = template.open_data_file(py.path.local())
        model = data['models'][data['report']]

        if data['report'] == 'laserPreviewReport':
            field = model['field']
            coordinate = model['coordinate']
            mode = model['mode']
            if mode != 'all':
                mode = int(mode)
            res = template.extract_field_report(field, coordinate, mode, data_file)
        elif data['report'] == 'beamPreviewReport':
            res = template.extract_particle_report(
                model,
                'beam',
                cfg_dir,
                data_file,
            )

        simulation_db.write_result(res)
Exemplo n.º 2
0
def prepare_simulation(data, run_dir):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``)
    Updates the parameters in ``data`` and save.
    Generate the pkcli command.

    Args:
        data (dict): report and model parameters
        run_dir (py.path.local): dir simulation will be run in
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    import sirepo.sim_data
    sim_type = data.simulationType
    template = sirepo.template.import_module(data)
    s = sirepo.sim_data.get_class(sim_type)
    s.lib_files_to_run_dir(data, run_dir)
    update_rsmanifest(data)
    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = s.is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
Exemplo n.º 3
0
def test_module_basename():
    p1 = pkunit.import_module_from_data_dir('p1')
    assert pkinspect.module_basename(p1) == 'p1'
    m1 = pkunit.import_module_from_data_dir('p1.m1')
    assert pkinspect.module_basename(m1) == 'm1'
    assert pkinspect.module_basename(m1.C) == 'm1'
    assert pkinspect.module_basename(m1.C()) == 'm1'
    assert pkinspect.module_basename(m1) == 'm1'
    assert pkinspect.module_basename(m1.C) == 'm1'
    assert pkinspect.module_basename(m1.C()) == 'm1'
    p2 = pkunit.import_module_from_data_dir('p1.p2')
    assert pkinspect.module_basename(p2) == 'p2'
    m2 = pkunit.import_module_from_data_dir('p1.p2.m2')
    assert pkinspect.module_basename(m2) == 'm2'
Exemplo n.º 4
0
def test_module_basename():
    p1 = pkunit.import_module_from_data_dir('p1')
    assert pkinspect.module_basename(p1) == 'p1'
    m1 = pkunit.import_module_from_data_dir('p1.m1')
    assert pkinspect.module_basename(m1) == 'm1'
    assert pkinspect.module_basename(m1.C) == 'm1'
    assert pkinspect.module_basename(m1.C()) == 'm1'
    assert pkinspect.module_basename(m1) == 'm1'
    assert pkinspect.module_basename(m1.C) == 'm1'
    assert pkinspect.module_basename(m1.C()) == 'm1'
    p2 = pkunit.import_module_from_data_dir('p1.p2')
    assert pkinspect.module_basename(p2) == 'p2'
    m2 = pkunit.import_module_from_data_dir('p1.p2.m2')
    assert pkinspect.module_basename(m2) == 'm2'
Exemplo n.º 5
0
def template_globals(sim_type=None):
    """Initializer for templates

    Usage::
        _SIM_DATA, SIM_TYPE, _SCHEMA = sirepo.sim_data.template_globals()

    Args:
        sim_type (str): simulation type [calling module's basename]
    Returns:
        (class, str, object): SimData class, simulation type, and schema
    """
    c = get_class(sim_type or pkinspect.module_basename(pkinspect.caller_module()))
    return c, c.sim_type(), c.schema()
Exemplo n.º 6
0
def _display_name(e):
    """Lookup display_name for enum

    Args:
        e (Enum): what to look up

    Returns:
        str: display name for `e`
    """
    f = pkinspect.module_basename(e)
    if f not in _display_name_cache:
        # Name collisions avoided because this is a radtrack namespace
        _display_name_cache[f] = pkyaml.load_resource(f)
    return _display_name_cache[f][e.__class__.__name__][e.name]
Exemplo n.º 7
0
def prepare_simulation(data, tmp_dir=None):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``), or (if
    specified) a ``tmp_dir``.
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
        tmp_dir (py.path.local):
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    if tmp_dir is None:
        # This is the legacy (pre-runner-daemon) code path
        run_dir = simulation_run_dir(data, remove_dir=True)
        #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
        #   probably can only do with celery.
        pkio.mkdir_parent(run_dir)
        out_dir = run_dir
        # Only done on the legacy path, because the runner daemon owns the
        # status file.
        write_status('pending', out_dir)
    else:
        # This is the runner-daemon code path -- tmp_dir is always given, as a
        # new temporary directory we have to create.
        run_dir = simulation_run_dir(data)
        pkio.mkdir_parent(tmp_dir)
        out_dir = tmp_dir
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, out_dir)

    write_json(out_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=out_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
Exemplo n.º 8
0
def prepare_simulation(data, run_dir=None):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``)
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
        run_dir (py.path.local): defaults to `simulation_run_dir`
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    import sirepo.sim_data
    if run_dir is None:
        # This is the legacy (pre-runner-daemon) code path
        run_dir = simulation_run_dir(data, remove_dir=True)
        #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
        #   probably can only do with celery.
        pkio.mkdir_parent(run_dir)
        write_status('pending', run_dir)
    sim_type = data.simulationType
    template = sirepo.template.import_module(data)
    s = sirepo.sim_data.get_class(sim_type)
    s.lib_files_to_run_dir(data, run_dir)
    update_rsmanifest(data)
    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = s.is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
Exemplo n.º 9
0
def run(cfg_dir):
    """Run code in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run code in
    """
    template = sirepo.template.import_module(pkinspect.module_basename(run))
    with pkio.save_chdir(cfg_dir):
        _run_code()
        a = PKDict(
            # see template.warppba.open_data_file (opens last frame)
            frameIndex=None,
            run_dir=pkio.py_path(cfg_dir),
            sim_in=simulation_db.read_json(template_common.INPUT_BASE_NAME),
        )
        a.frameReport = a.sim_in.report
        a.update(a.sim_in.models[a.frameReport])
        if a.frameReport == 'laserPreviewReport':
            res = template.sim_frame_fieldAnimation(a)
        elif a.frameReport == 'beamPreviewReport':
            res = template.sim_frame_beamAnimation(a)
        simulation_db.write_result(res)
Exemplo n.º 10
0
def prepare_simulation(data):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``).
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    run_dir = simulation_run_dir(data, remove_dir=True)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    pkio.mkdir_parent(run_dir)
    write_status('pending', run_dir)
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, run_dir)

    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
Exemplo n.º 11
0
def prepare_simulation(data):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``).
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    run_dir = simulation_run_dir(data, remove_dir=True)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    pkio.mkdir_parent(run_dir)
    write_status('pending', run_dir)
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, run_dir)

    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
Exemplo n.º 12
0
 def sim_type(cls):
     return cls._memoize(pkinspect.module_basename(cls))