Ejemplo n.º 1
0
def api_copyNonSessionSimulation():
    req = http_request.parse_json()
    sim_type = req['simulationType']
    src = py.path.local(
        simulation_db.find_global_simulation(
            sim_type,
            req['simulationId'],
            checked=True,
        ))
    data = simulation_db.open_json_file(
        sim_type,
        src.join(simulation_db.SIMULATION_DATA_FILE),
    )
    if 'report' in data:
        del data['report']
    data['models']['simulation']['isExample'] = False
    data['models']['simulation']['outOfSessionSimulationId'] = req[
        'simulationId']
    res = _save_new_and_reply(data)
    target = simulation_db.simulation_dir(sim_type,
                                          simulation_db.parse_sid(data))
    template_common.copy_lib_files(
        data,
        simulation_db.lib_dir_from_sim_dir(src),
        simulation_db.lib_dir_from_sim_dir(target),
    )
    template = sirepo.template.import_module(data)
    if hasattr(template, 'copy_related_files'):
        template.copy_related_files(data, str(src), str(target))
    return res
Ejemplo n.º 2
0
def api_copyNonSessionSimulation():
    req = http_request.parse_post(id=True, template=True)
    simulation_db.verify_app_directory(req.type)
    src = pkio.py_path(
        simulation_db.find_global_simulation(
            req.type,
            req.id,
            checked=True,
        ), )
    data = simulation_db.open_json_file(
        req.type,
        src.join(simulation_db.SIMULATION_DATA_FILE),
    )
    data.pkdel('report')
    data.models.simulation.isExample = False
    data.models.simulation.outOfSessionSimulationId = req.id
    res = _save_new_and_reply(data)
    sirepo.sim_data.get_class(req.type).lib_files_from_other_user(
        data,
        simulation_db.lib_dir_from_sim_dir(src),
    )
    target = simulation_db.simulation_dir(req.type,
                                          data.models.simulation.simulationId)
    #TODO(robnagler) does not work, supervisor needs to be notified to
    # copy the simulation state.
    # if hasattr(req.template, 'copy_related_files'):
    #     req.template.copy_related_files(data, str(src), str(target))
    return res
Ejemplo n.º 3
0
def _generate_parameters_file(data):
    res, v = template_common.generate_parameters_file(data)

    v.report = re.sub(r'\d+$', '', data.get('report', ''))
    if v.report in _INITIAL_REPORTS:
        # these reports do not require running madx first
        v.initialTwissParameters = _get_initial_twiss_params(data)
        v.numParticles = data.models.simulation.numberOfParticles
        v.particleFile = simulation_db.simulation_dir(SIM_TYPE, data.simulationId) \
            .join(data.report).join('ptc_particles.txt')
        res = template_common.render_jinja(SIM_TYPE, v, 'bunch.py')
        return res

    util = LatticeUtil(data, _SCHEMA)
    filename_map = _build_filename_map_from_util(util)
    report = data.get('report', '')
    code_var = _code_var(data.models.rpnVariables)
    v.twissOutputFilename = _TWISS_OUTPUT_FILE
    v.lattice = _generate_lattice(filename_map, util)
    v.variables = _generate_variables(code_var, data)

    v.useBeamline = util.select_beamline().name
    if report == 'twissReport':
        v.twissOutputFilename = _TWISS_OUTPUT_FILE
        return template_common.render_jinja(SIM_TYPE, v, 'twiss.madx')
    _add_commands(data, util)
    v.commands = _generate_commands(filename_map, util)
    v.hasTwiss = bool(util.find_first_command(data, 'twiss'))
    if not v.hasTwiss:
        v.twissOutputFilename = _TWISS_OUTPUT_FILE
    return template_common.render_jinja(SIM_TYPE, v, 'parameters.madx')
Ejemplo n.º 4
0
def api_copyNonSessionSimulation():
    req = _json_input()
    sim_type = req['simulationType']
    global_path = simulation_db.find_global_simulation(sim_type,
                                                       req['simulationId'])
    if global_path:
        data = simulation_db.open_json_file(
            sim_type,
            os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE),
        )
        data['models']['simulation']['isExample'] = False
        data['models']['simulation']['outOfSessionSimulationId'] = req[
            'simulationId']
        res = _save_new_and_reply(data)
        target = simulation_db.simulation_dir(sim_type,
                                              simulation_db.parse_sid(data))
        template_common.copy_lib_files(
            data,
            py.path.local(os.path.dirname(global_path)).join('lib'),
            target.join('../lib'),
        )
        template = sirepo.template.import_module(data)
        if hasattr(template, 'copy_related_files'):
            template.copy_related_files(data, global_path, str(target))
        return res
    werkzeug.exceptions.abort(404)
Ejemplo n.º 5
0
def get_animation_name(data):
    if data['modelName'].startswith('dicomAnimation'):
        return 'dicomAnimation'
    if data['modelName'] == 'dicomDose':
        # if the doseCalculation has been run, use that directory for work
        # otherwise, it is an imported dose file
        if simulation_db.simulation_dir(SIM_TYPE, simulation_db.parse_sid(data)).join('doseCalculation').exists():
            return 'doseCalculation'
        return 'dicomAnimation'
    return data['modelName']
Ejemplo n.º 6
0
def _update_database(user_data, oauth_type):
    with _db_serial_lock:
        user = User.query.filter_by(oauth_id=user_data['id'], oauth_type=oauth_type).first()
        session_uid = server.session_user(checked=False)
        if user:
            if session_uid and session_uid != user.uid:
                simulation_db.move_user_simulations(user.uid)
            user.user_name = user_data['login']
            user.display_name = user_data['name']
            server.session_user(user.uid)
        else:
            if not session_uid:
                # ensures the user session (uid) is ready if new user logs in from logged-out session
                pkdlog('creating new session for user: {}', user_data['id'])
                simulation_db.simulation_dir('')
            user = User(server.session_user(), user_data['login'], user_data['name'], oauth_type, user_data['id'])
        _db.session.add(user)
        _db.session.commit()
        return user
Ejemplo n.º 7
0
def _update_database(user_data, oauth_type):
    with _db_serial_lock:
        user = User.query.filter_by(oauth_id=user_data['id'], oauth_type=oauth_type).first()
        session_uid = cookie.get_user(checked=False)
        if user:
            if session_uid and session_uid != user.uid:
                simulation_db.move_user_simulations(user.uid)
            user.user_name = user_data['login']
            user.display_name = user_data['name']
            cookie.set_user(user.uid)
        else:
            if not session_uid:
                # ensures the user session (uid) is ready if new user logs in from logged-out session
                pkdlog('creating new session for user: {}', user_data['id'])
                simulation_db.simulation_dir('')
            user = User(cookie.get_user(), user_data['login'], user_data['name'], oauth_type, user_data['id'])
        _db.session.add(user)
        _db.session.commit()
        return user
Ejemplo n.º 8
0
def get_animation_name(data):
    if data['modelName'].startswith('dicomAnimation'):
        return 'dicomAnimation'
    if data['modelName'] == 'dicomDose':
        # if the doseCalculation has been run, use that directory for work
        # otherwise, it is an imported dose file
        if simulation_db.simulation_dir(SIM_TYPE, simulation_db.parse_sid(
                data)).join('doseCalculation').exists():
            return 'doseCalculation'
        return 'dicomAnimation'
    return data['modelName']
Ejemplo n.º 9
0
 def _compute_model(cls, analysis_model, data):
     if analysis_model.startswith('dicomAnimation'):
         return 'dicomAnimation'
     if analysis_model == 'dicomDose' or analysis_model == 'doseCalculation':
         # if the doseCalculation has been run, use that directory for work
         # otherwise, it is an imported dose file
         if simulation_db.simulation_dir(
                 cls.sim_type(),
                 data.simulationId).join('doseCalculation').exists():
             return 'doseCalculation'
         return 'dicomAnimation'
     return analysis_model
Ejemplo n.º 10
0
def get_application_data(data):
    if data['method'] == 'compute_simulation_steps':
        run_dir = simulation_db.simulation_dir(SIM_TYPE, data['simulationId']).join('fieldReport')
        if run_dir.exists():
            res = simulation_db.read_result(run_dir)[0]
            if res and 'tof_expected' in res:
                return {
                    'timeOfFlight': res['tof_expected'],
                    'steps': res['steps_expected'],
                    'electronFraction': res['e_cross'] if 'e_cross' in res else 0,
                }
        return {}
    raise RuntimeError('unknown application data method: {}'.format(data['method']))
Ejemplo n.º 11
0
def get_application_data(data):
    if data['method'] == 'compute_simulation_steps':
        run_dir = simulation_db.simulation_dir(SIM_TYPE, data['simulationId']).join('fieldReport')
        if run_dir.exists():
            res = simulation_db.read_result(run_dir)[0]
            if res and 'tof_expected' in res:
                return {
                    'timeOfFlight': res['tof_expected'],
                    'steps': res['steps_expected'],
                    'electronFraction': res['e_cross'] if 'e_cross' in res else 0,
                }
        return {}
    raise RuntimeError('unknown application data method: {}'.format(data['method']))
Ejemplo n.º 12
0
def get_application_data(data, **kwargs):
    if 'method' in data and data['method'] == 'compute_simulation_steps':
        field_file = simulation_db.simulation_dir(SIM_TYPE, data['simulationId']) \
            .join('fieldCalculationAnimation').join(_FIELD_ESTIMATE_FILE)
        if field_file.exists():
            res = simulation_db.read_json(field_file)
            if res and 'tof_expected' in res:
                return {
                    'timeOfFlight': res['tof_expected'],
                    'steps': res['steps_expected'],
                    'electronFraction': res['e_cross'] if 'e_cross' in res else 0,
                }
        return {}
    if 'polys' in data:
        _save_stl_polys(data)
        return {}
    raise RuntimeError('unknown application data method: {}'.format(data['method']))
Ejemplo n.º 13
0
def _start_simulation(data, run_async=False):
    """Setup and start the simulation.

    Args:
        data (dict): app data
        run_async (bool): run-background or run

    Returns:
        object: _Command or daemon instance
    """
    run_dir = simulation_db.simulation_run_dir(data, remove_dir=True)
    pkio.mkdir_parent(run_dir)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    simulation_type = data['simulationType']
    sid = simulation_db.parse_sid(data)
    data = simulation_db.fixup_old_data(simulation_type, data)
    assert simulation_type in simulation_db.APP_NAMES, \
        '{}: invalid simulation type'.format(simulation_type)
    template = sirepo.template.import_module(simulation_type)
    for d in simulation_db.simulation_dir(
            simulation_type,
            sid), simulation_db.simulation_lib_dir(simulation_type):
        for f in glob.glob(str(d.join('*.*'))):
            if os.path.isfile(f):
                py.path.local(f).copy(run_dir)
    template.prepare_aux_files(run_dir, data)
    simulation_db.save_simulation_json(simulation_type, data)
    with open(str(run_dir.join('in{}'.format(simulation_db.JSON_SUFFIX))),
              'w') as outfile:
        json.dump(data, outfile)
    pkio.write_text(
        run_dir.join(simulation_type + '_parameters.py'),
        template.generate_parameters_file(
            data,
            _schema_cache(simulation_type),
            run_dir=run_dir,
            run_async=run_async,
        ))

    cmd = [_ROOT_CMD, simulation_type] \
        + ['run-background' if run_async else 'run'] + [str(run_dir)]
    if run_async:
        return cfg.job_queue(sid, run_dir, cmd)
    return _Command(cmd, cfg.foreground_time_limit)
Ejemplo n.º 14
0
def app_copy_nonsession_simulation():
    req = _json_input()
    simulation_type = req['simulationType']
    global_path = simulation_db.find_global_simulation(simulation_type,
                                                       req['simulationId'])
    if global_path:
        data = simulation_db.open_json_file(
            simulation_type,
            os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE))
        data['models']['simulation']['isExample'] = ''
        data['models']['simulation']['outOfSessionSimulationId'] = req[
            'simulationId']
        res = _save_new_and_reply(simulation_type, data)
        sirepo.template.import_module(simulation_type).copy_animation_file(
            global_path,
            simulation_db.simulation_dir(simulation_type,
                                         simulation_db.parse_sid(data)))
        return res
    werkzeug.exceptions.abort(404)
Ejemplo n.º 15
0
def _start_simulation(data, run_async=False):
    """Setup and start the simulation.

    Args:
        data (dict): app data
        run_async (bool): run-background or run

    Returns:
        object: _Command or daemon instance
    """
    run_dir = simulation_db.simulation_run_dir(data, remove_dir=True)
    pkio.mkdir_parent(run_dir)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    simulation_type = data['simulationType']
    sid = simulation_db.parse_sid(data)
    data = simulation_db.fixup_old_data(simulation_type, data)
    assert simulation_type in simulation_db.APP_NAMES, \
        '{}: invalid simulation type'.format(simulation_type)
    template = sirepo.template.import_module(simulation_type)
    simulation_db.save_simulation_json(simulation_type, data)
    for d in simulation_db.simulation_dir(simulation_type, sid), simulation_db.simulation_lib_dir(simulation_type):
        for f in glob.glob(str(d.join('*.*'))):
            if os.path.isfile(f):
                py.path.local(f).copy(run_dir)
    with open(str(run_dir.join('in{}'.format(simulation_db.JSON_SUFFIX))), 'w') as outfile:
        json.dump(data, outfile)
    pkio.write_text(
        run_dir.join(simulation_type + '_parameters.py'),
        template.generate_parameters_file(
            data,
            _schema_cache(simulation_type),
            run_dir=run_dir,
            run_async=run_async,
        )
    )

    cmd = [_ROOT_CMD, simulation_type] \
        + ['run-background' if run_async else 'run'] + [str(run_dir)]
    if run_async:
        return cfg.job_queue(sid, run_dir, cmd)
    return _Command(cmd, cfg.foreground_time_limit)
Ejemplo n.º 16
0
def _sim_file(sim_id, filename):
    return str(simulation_db.simulation_dir(SIM_TYPE, sim_id).join(filename))
Ejemplo n.º 17
0
def _sim_file(sim_id, filename):
    return str(simulation_db.simulation_dir(SIM_TYPE, sim_id).join(filename))
Ejemplo n.º 18
0
def _elegant_dir():
    return simulation_db.simulation_dir(SIM_TYPE).join('../elegant')
Ejemplo n.º 19
0
def _epics_dir(sim_id):
    return simulation_db.simulation_dir(SIM_TYPE,
                                        sim_id).join('epicsServerAnimation')
Ejemplo n.º 20
0
 def jspec_elegant_dir(cls):
     return simulation_db.simulation_dir('elegant')
Ejemplo n.º 21
0
def app_delete_simulation():
    data = _json_input()
    pkio.unchecked_remove(
        simulation_db.simulation_dir(data['simulationType'],
                                     data['simulationId']))
    return '{}'
Ejemplo n.º 22
0
def _get_res_file(sim_id, filename):
    return simulation_db.simulation_dir(SIM_TYPE, sim_id) \
        .join(_GEOM_DIR).join(filename)
Ejemplo n.º 23
0
def app_copy_nonsession_simulation():
    req = _json_input()
    sim_type = req['simulationType']
    global_path = simulation_db.find_global_simulation(sim_type, req['simulationId'])
    if global_path:
        data = simulation_db.open_json_file(sim_type, os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE))
        data['models']['simulation']['isExample'] = False
        data['models']['simulation']['outOfSessionSimulationId'] = req['simulationId']
        res = _save_new_and_reply(sim_type, data)
        sirepo.template.import_module(data).copy_related_files(data, global_path, str(simulation_db.simulation_dir(sim_type, simulation_db.parse_sid(data))))
        return res
    werkzeug.exceptions.abort(404)
Ejemplo n.º 24
0
def app_delete_simulation():
    data = _json_input()
    pkio.unchecked_remove(simulation_db.simulation_dir(data['simulationType'], data['simulationId']))
    return '{}'