Exemple #1
0
def _generate_parameters_file(data, run_dir=None, is_parallel=False):
    _validate_data(data, simulation_db.get_schema(SIM_TYPE))
    _convert_meters_to_centimeters(data['models'])
    _convert_meters_to_centimeters(data['models']['beamline'])
    v = template_common.flatten_data(data['models'], {})
    r = data['report']
    report_model = data['models'][r]
    beamline = data['models']['beamline']
    v['shadowOutputFile'] = _SHADOW_OUTPUT_FILE

    if r == 'initialIntensityReport':
        v['distanceFromSource'] = beamline[0]['position'] if len(beamline) else template_common.DEFAULT_INTENSITY_DISTANCE
    elif template_common.is_watchpoint(r):
        v['beamlineOptics'] = _generate_beamline_optics(data['models'], template_common.watchpoint_id(r))
    else:
        v['distanceFromSource'] = report_model['distanceFromSource']

    if v['simulation_sourceType'] == 'bendingMagnet':
        v['bendingMagnetSettings'] = _generate_bending_magnet(data)
    elif v['simulation_sourceType'] == 'geometricSource':
        v['geometricSourceSettings'] = _generate_geometric_source(data)
    elif v['simulation_sourceType'] == 'wiggler':
        v['wigglerSettings'] = _generate_wiggler(data)
        v['wigglerTrajectoryFilename'] = _WIGGLER_TRAJECTOR_FILENAME
        v['wigglerTrajectoryInput'] = ''
        if data['models']['wiggler']['b_from'] in ('1', '2'):
            v['wigglerTrajectoryInput'] = _wiggler_file(data['models']['wiggler']['trajFile'])
    return template_common.render_jinja(SIM_TYPE, v)
Exemple #2
0
def api_blueskyAuth():
    req = http_request.parse_json()
    auth_hash(req, verify=True)
    sid = req.simulationId
    sim_type = req.simulationType
    path = simulation_db.find_global_simulation(
        sim_type,
        sid,
        checked=True,
    )
    cookie.set_user(simulation_db.uid_from_dir_name(path))
    return http_reply.gen_json_ok(dict(
        data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId),
        schema=simulation_db.get_schema(req.simulationType),
    ))
Exemple #3
0
def generate_parameters_file(data, run_dir=None, is_parallel=False):
    _validate_data(data, simulation_db.get_schema(_SIMULATION_TYPE))
    v = template_common.flatten_data(data['models'], {})
    v['outputDir'] = '"{}"'.format(run_dir) if run_dir else None
    v['isAnimationView'] = is_parallel
    v['incSteps'] = 50
    v['diagnosticPeriod'] = 50
    if data['models']['simulation']['sourceType'] == 'electronBeam':
        v['useBeam'] = 1
        v['useLaser'] = 0
    else:
        v['useBeam'] = 0
        v['useLaser'] = 1
    if data['models']['electronBeam']['beamRadiusMethod'] == 'a':
        v['electronBeam_transverseEmittance'] = 0
    return pkjinja.render_resource('warp.py', v)
Exemple #4
0
"""Wrapper to run synergia from the command line.

:copyright: Copyright (c) 2018 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkdebug import pkdp, pkdc
from sirepo import mpi
from sirepo import simulation_db
from sirepo.template import template_common
import py.path
import re
import sirepo.template.synergia as template

_SCHEMA = simulation_db.get_schema(template.SIM_TYPE)


def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    report = data['report']
    if 'bunchReport' in report or report == 'twissReport' or report == 'twissReport2':
        try:
            with pkio.save_chdir(cfg_dir):
                exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE), locals(), locals())
            template.save_report_data(data, py.path.local(cfg_dir))
        except Exception as e:
            res = template.parse_error_log(py.path.local(cfg_dir)) or {
                'error': str(e),
            }
            simulation_db.write_result(res)
Exemple #5
0
    "GammaDeriv": "GammaDeriv [1/m]",
}

_PLOT_TITLE = {"x-xp": "Horizontal", "y-yp": "Vertical", "x-y": "Cross-section", "t-p": "Longitudinal"}

_SDDS_INDEX = 0

_SDDS_DOUBLE_TYPE = 1

_SDDS_STRING_TYPE = 7

_STATIC_FOLDER = py.path.local(pkresource.filename("static"))

_SIMULATION_TYPE = "elegant"

_SCHEMA = simulation_db.get_schema(_SIMULATION_TYPE)

_ELEGANT_ME_EV = _SCHEMA["constant"]["ELEGANT_ME_EV"]


def background_percent_complete(report, run_dir, is_running, schema):
    # TODO(robnagler) remove duplication in run_dir.exists() (outer level?)
    errors, last_element = _parse_elegant_log(run_dir)
    res = {"percentComplete": 100, "frameCount": 0, "errors": errors}
    if is_running:
        data = simulation_db.read_json(run_dir.join(template_common.INPUT_BASE_NAME))
        res["percentComplete"] = _compute_percent_complete(data, last_element)
        return res
    if not run_dir.join(_ELEGANT_SEMAPHORE_FILE).exists():
        return res
    data = simulation_db.read_json(run_dir.join(template_common.INPUT_BASE_NAME))
Exemple #6
0
def app_simulation_schema():
    sim_type = flask.request.form['simulationType']
    return _json_response(simulation_db.get_schema(sim_type))
Exemple #7
0
_PLOT_TITLE = {
    'x-xp': 'Horizontal',
    'y-yp': 'Vertical',
    'x-y': 'Cross-section',
    't-p': 'Longitudinal',
}

_REPORT_STYLE_FIELDS = ['colorMap', 'notes']

_SDDS_INDEX = 0

_SDDS_DOUBLE_TYPE = 1

_SDDS_STRING_TYPE = 7

_SCHEMA = simulation_db.get_schema(elegant_common.SIM_TYPE)

_SIMPLE_UNITS = ['m', 's', 'C', 'rad', 'eV']

_X_FIELD = 's'


def background_percent_complete(report, run_dir, is_running):
    #TODO(robnagler) remove duplication in run_dir.exists() (outer level?)
    errors, last_element = parse_elegant_log(run_dir)
    res = {
        'percentComplete': 100,
        'frameCount': 0,
        'errors': errors,
    }
    if is_running:
Exemple #8
0
def app_simulation_schema():
    sim_type = flask.request.form['simulationType']
    return _json_response(simulation_db.get_schema(sim_type))
Exemple #9
0
"""Wrapper to run myapp from the command line.

:copyright: Copyright (c) 2018 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkdebug import pkdp, pkdc
from sirepo import simulation_db
from sirepo.template import template_common
import h5py
import numpy as np
import py.path
import sirepo.template.synergia as template

_SCHEMA = simulation_db.get_schema(template.SIM_TYPE)


def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    report = data['report']
    if report == 'bunchReport' or report == 'twissReport' or report == 'twissReport2':
        try:
            with pkio.save_chdir(cfg_dir):
                exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE),
                     locals(), locals())
            if report == 'bunchReport':
                res = _run_bunch_report(data)
            else:
                res = _run_twiss_report(data, report)
        except Exception as e:
Exemple #10
0
def api_simulationSchema():
    sim_type = sirepo.template.assert_sim_type(flask.request.form['simulationType'])
    return http_reply.gen_json(simulation_db.get_schema(sim_type))
Exemple #11
0
"""

from __future__ import absolute_import, division, print_function
from pykern import pkcollections
from pykern import pkio
from pykern.pkdebug import pkdc, pkdp
from sirepo import simulation_db
from sirepo.template import template_common
import os.path
import py.path
import xraylib

#: Simulation type
SIM_TYPE = 'shadow'

_SCHEMA = simulation_db.get_schema(SIM_TYPE)

_RESOURCE_DIR = template_common.resource_dir(SIM_TYPE)
_SHADOW_OUTPUT_FILE = 'shadow-output.dat'

_REPORT_STYLE_FIELDS = ['colorMap', 'notes']

_CENTIMETER_FIELDS = {
    'electronBeam':
    ['sigmax', 'sigmaz', 'epsi_x', 'epsi_z', 'epsi_dx', 'epsi_dz'],
    'geometricSource':
    ['wxsou', 'wzsou', 'sigmax', 'sigmaz', 'wysou', 'sigmay'],
    'rayFilter': ['distance', 'x1', 'x2', 'z1', 'z2'],
    'aperture': [
        'position', 'horizontalSize', 'verticalSize', 'horizontalOffset',
        'verticalOffset'
Exemple #12
0
_OUTPUT_INFO_VERSION = '1'

_PLOT_TITLE = {
    'x-xp': 'Horizontal',
    'y-yp': 'Vertical',
    'x-y': 'Cross-section',
    't-p': 'Longitudinal',
}

_SDDS_INDEX = 0

_SDDS_DOUBLE_TYPE = 1

_SDDS_STRING_TYPE = 7

_SCHEMA = simulation_db.get_schema(elegant_common.SIM_TYPE)

_SIMPLE_UNITS = ['m', 's', 'C', 'rad', 'eV']

_X_FIELD = 's'


def background_percent_complete(report, run_dir, is_running):
    #TODO(robnagler) remove duplication in run_dir.exists() (outer level?)
    errors, last_element = parse_elegant_log(run_dir)
    res = {
        'percentComplete': 100,
        'frameCount': 0,
        'errors': errors,
    }
    if is_running:
Exemple #13
0
    def schema(cls):
        from sirepo import simulation_db

        return cls._memoize(simulation_db.get_schema(cls.sim_type()))
Exemple #14
0
    'y-yp': 'Vertical',
    'x-y': 'Cross-section',
    't-p': 'Longitudinal',
}

_SDDS_INDEX = 0

_SDDS_DOUBLE_TYPE = 1

_SDDS_STRING_TYPE = 7

_STATIC_FOLDER = py.path.local(pkresource.filename('static'))

_SIMULATION_TYPE = 'elegant'

_SCHEMA = simulation_db.get_schema(_SIMULATION_TYPE)


def background_percent_complete(report, run_dir, is_running, schema):
    #TODO(robnagler) remove duplication in run_dir.exists() (outer level?)
    errors, last_element = _parse_elegant_log(run_dir)
    res = {
        'percentComplete': 100,
        'frameCount': 0,
        'errors': errors,
    }
    if is_running:
        data = simulation_db.read_json(
            run_dir.join(template_common.INPUT_BASE_NAME))
        res['percentComplete'] = _compute_percent_complete(data, last_element)
        return res
import re
import subprocess

_IGNORE_FIELD = ['rootname', 'search_path', 'semaphore_file']

_ANGLE_FIELDS = ['angle', 'kick', 'hkick']
_BEND_TYPES = [
    'BUMPER', 'CSBEND', 'CSRCSBEND', 'FMULT', 'HKICK', 'KICKER', 'KPOLY',
    'KSBEND', 'KQUSE', 'MBUMPER', 'MULT', 'NIBEND', 'NISEPT', 'RBEN', 'SBEN',
    'TUBEND'
]
_DRIFT_TYPES = ['CSRDRIFT', 'DRIF', 'EDRIFT', 'EMATRIX', 'LSCDRIFT']
_IGNORE_LENGTH_TYPES = ['ILMATRIX', 'STRAY', 'SCRIPT']
_LENGTH_FIELDS = ['l', 'xmax', 'length']

_SCHEMA = simulation_db.get_schema('elegant')


def _init_types():
    res = {}
    for name in _SCHEMA['model']:
        if name == name.upper():
            res[name] = True
    return res


_ELEGANT_TYPES = _init_types()


def build_variable_dependency(value, variables, depends):
    for v in str(value).split(' '):
# -*- coding: utf-8 -*-
u"""elegant lattice parser.

:copyright: Copyright (c) 2015 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

import re

from sirepo import simulation_db
from sirepo.template import elegant_command_parser
from sirepo.template import elegant_lattice_importer

_SCHEMA = simulation_db.get_schema('elegant')

def _init_types():
    res = {}
    for name in _SCHEMA['model']:
        if name.startswith('command_'):
            name = re.sub(r'^command_', '', name)
            res[name] = True
    return res


_ELEGANT_TYPES = _init_types()


def import_file(text):
    commands = elegant_command_parser.parse_file(text)
    if not len(commands):
Exemple #17
0
    'twissReport2': 'twiss.h5',
    'beamEvolutionAnimation': 'diagnostics.h5',
    'turnComparisonAnimation': 'diagnostics.h5',
}

SIM_TYPE = 'synergia'

WANT_BROWSER_FRAME_CACHE = True

_COORD6 = ['x', 'xp', 'y', 'yp', 'z', 'zp']

_FILE_ID_SEP = '-'

_IGNORE_ATTRIBUTES = ['lrad']

_SCHEMA = simulation_db.get_schema(SIM_TYPE)

_UNITS = {
    'x': 'm',
    'y': 'm',
    'z': 'm',
    'xp': 'rad',
    'yp': 'rad',
    'zp': 'rad',
    'cdt': 'm',
    'xstd': 'm',
    'ystd': 'm',
    'zstd': 'm',
    'xmean': 'm',
    'ymean': 'm',
    'zmean': 'm',
Exemple #18
0
def api_simulationSchema():
    return http_reply.gen_json(
        simulation_db.get_schema(
            http_request.parse_params(
                type=flask.request.form['simulationType'], ).type, ), )
Exemple #19
0
def _simulation_run_status(data, quiet=False):
    """Look for simulation status and output

    Args:
        data (dict): request
        quiet (bool): don't write errors to log

    Returns:
        dict: status response
    """
    try:
        #TODO(robnagler): Lock
        rep = simulation_db.report_info(data)
        is_processing = cfg.job_queue.is_processing(rep.job_id)
        is_running = rep.job_status in _RUN_STATES
        res = {'state': rep.job_status}
        pkdc(
            '{}: is_processing={} is_running={} state={} cached_data={}',
            rep.job_id,
            is_processing,
            is_running,
            rep.job_status,
            bool(rep.cached_data),
        )
        if is_processing and not is_running:
            cfg.job_queue.race_condition_reap(rep.job_id)
            pkdc('{}: is_processing and not is_running', rep.job_id)
            is_processing = False
        if is_processing:
            if not rep.cached_data:
                return _simulation_error(
                    'input file not found, but job is running',
                    rep.input_file,
                )
        else:
            is_running = False
            if rep.run_dir.exists():
                res, err = simulation_db.read_result(rep.run_dir)
                if err:
                    return _simulation_error(err, 'error in read_result', rep.run_dir)
        if simulation_db.is_parallel(data):
            template = sirepo.template.import_module(data)
            new = template.background_percent_complete(
                rep.model_name,
                rep.run_dir,
                is_running,
                simulation_db.get_schema(data['simulationType']),
            )
            new.setdefault('percentComplete', 0.0)
            new.setdefault('frameCount', 0)
            res.update(new)
        res['parametersChanged'] = rep.parameters_changed
        if res['parametersChanged']:
            pkdlog(
                '{}: parametersChanged=True req_hash={} cached_hash={}',
                rep.job_id,
                rep.req_hash,
                rep.cached_hash,
            )
        #TODO(robnagler) verify serial number to see what's newer
        res.setdefault('startTime', _mtime_or_now(rep.input_file))
        res.setdefault('lastUpdateTime', _mtime_or_now(rep.run_dir))
        res.setdefault('elapsedTime', res['lastUpdateTime'] - res['startTime'])
        if is_processing:
            res['nextRequestSeconds'] = simulation_db.poll_seconds(rep.cached_data)
            res['nextRequest'] = {
                'report': rep.model_name,
                'reportParametersHash': rep.cached_hash,
                'simulationId': rep.cached_data['simulationId'],
                'simulationType': rep.cached_data['simulationType'],
            }
        pkdc(
            '{}: processing={} state={} cache_hit={} cached_hash={} data_hash={}',
            rep.job_id,
            is_processing,
            res['state'],
            rep.cache_hit,
            rep.cached_hash,
            rep.req_hash,
        )
    except Exception:
        return _simulation_error(pkdexc(), quiet=quiet)
    return res
Exemple #20
0
def _simulation_run_status(data, quiet=False):
    """Look for simulation status and output

    Args:
        data (dict): request
        quiet (bool): don't write errors to log

    Returns:
        dict: status response
    """
    try:
        #TODO(robnagler): Lock
        rep = simulation_db.report_info(data)
        is_processing = cfg.job_queue.is_processing(rep.job_id)
        is_running = rep.job_status in _RUN_STATES
        res = {'state': rep.job_status}
        pkdc(
            '{}: is_processing={} is_running={} state={} cached_data={}',
            rep.job_id,
            is_processing,
            is_running,
            rep.job_status,
            bool(rep.cached_data),
        )
        if is_processing and not is_running:
            cfg.job_queue.race_condition_reap(rep.job_id)
            pkdc('{}: is_processing and not is_running', rep.job_id)
            is_processing = False
        if is_processing:
            if not rep.cached_data:
                return _simulation_error(
                    'input file not found, but job is running',
                    rep.input_file,
                )
        else:
            is_running = False
            if rep.run_dir.exists():
                res2, err = simulation_db.read_result(rep.run_dir)
                if err:
                    if simulation_db.is_parallel(data):
                        # allow parallel jobs to use template to parse errors below
                        res['state'] = 'error'
                    else:
                        return _simulation_error(err, 'error in read_result', rep.run_dir)
                else:
                    res = res2
        if simulation_db.is_parallel(data):
            template = sirepo.template.import_module(data)
            new = template.background_percent_complete(
                rep.model_name,
                rep.run_dir,
                is_running,
                simulation_db.get_schema(data['simulationType']),
            )
            new.setdefault('percentComplete', 0.0)
            new.setdefault('frameCount', 0)
            res.update(new)
        res['parametersChanged'] = rep.parameters_changed
        if res['parametersChanged']:
            pkdlog(
                '{}: parametersChanged=True req_hash={} cached_hash={}',
                rep.job_id,
                rep.req_hash,
                rep.cached_hash,
            )
        #TODO(robnagler) verify serial number to see what's newer
        res.setdefault('startTime', _mtime_or_now(rep.input_file))
        res.setdefault('lastUpdateTime', _mtime_or_now(rep.run_dir))
        res.setdefault('elapsedTime', res['lastUpdateTime'] - res['startTime'])
        if is_processing:
            res['nextRequestSeconds'] = simulation_db.poll_seconds(rep.cached_data)
            res['nextRequest'] = {
                'report': rep.model_name,
                'reportParametersHash': rep.cached_hash,
                'simulationId': rep.cached_data['simulationId'],
                'simulationType': rep.cached_data['simulationType'],
            }
        pkdc(
            '{}: processing={} state={} cache_hit={} cached_hash={} data_hash={}',
            rep.job_id,
            is_processing,
            res['state'],
            rep.cache_hit,
            rep.cached_hash,
            rep.req_hash,
        )
    except Exception:
        return _simulation_error(pkdexc(), quiet=quiet)
    return res