Пример #1
0
def twiss_to_madx(elegant_twiss_file, madx_twiss_file):
    outfile = 'sdds_output.txt'
    twiss_file = 'twiss-with-mu.sdds'
    # convert elegant psix to mad-x MU, rad --> rad / 2pi
    pksubprocess.check_call_with_signals([
        'sddsprocess',
        elegant_twiss_file,
        '-define=column,mux,psix 2 pi * /',
        '-define=column,muy,psiy 2 pi * /',
        twiss_file,
    ],
                                         output=outfile,
                                         env=elegant_common.subprocess_env())
    pksubprocess.check_call_with_signals([
        'sdds2stream',
        twiss_file,
        '-columns={}'.format(','.join(
            map(lambda x: x[0], _ELEGANT_TO_MADX_COLUMNS))),
    ],
                                         output=outfile,
                                         env=elegant_common.subprocess_env())
    lines = pkio.read_text(outfile).split('\n')
    header = '* {}\n$ \n'.format(' '.join(
        map(lambda x: x[1], _ELEGANT_TO_MADX_COLUMNS)))
    pkio.write_text(madx_twiss_file, header + '\n'.join(lines) + '\n')
Пример #2
0
def twiss_to_madx(elegant_twiss_file, madx_twiss_file):
    outfile = 'sdds_output.txt'
    twiss_file = 'twiss-with-mu.sdds'
    # convert elegant psix to mad-x MU, rad --> rad / 2pi
    pksubprocess.check_call_with_signals([
        'sddsprocess',
        elegant_twiss_file,
        '-define=column,mux,psix 2 pi * /',
        '-define=column,muy,psiy 2 pi * /',
        twiss_file,
    ], output=outfile, env=elegant_common.subprocess_env())
    pksubprocess.check_call_with_signals([
        'sdds2stream',
        twiss_file,
        '-columns={}'.format(','.join(map(lambda x: x[0], _ELEGANT_TO_MADX_COLUMNS))),
    ], output=outfile, env=elegant_common.subprocess_env())
    lines = pkio.read_text(outfile).split('\n')
    header = '* {}\n$ \n'.format(' '.join(map(lambda x: x[1], _ELEGANT_TO_MADX_COLUMNS)))
    pkio.write_text(madx_twiss_file, header + '\n'.join(lines) + '\n')
Пример #3
0
def _run_elegant(bunch_report=False, with_mpi=False):
    exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE), locals(),
         locals())
    if bunch_report and re.search('\&sdds_beam\s', elegant_file):
        return
    pkio.write_text('elegant.lte', lattice_file)
    ele = 'elegant.ele'
    pkio.write_text(ele, elegant_file)
    kwargs = {
        'output': ELEGANT_LOG_FILE,
        'env': elegant_common.subprocess_env(),
    }
    #TODO(robnagler) Need to handle this specially, b/c different binary
    if execution_mode == 'parallel' and with_mpi and mpi.cfg.cores > 1:
        return mpi.run_program(['Pelegant', ele], **kwargs)
    pksubprocess.check_call_with_signals(['elegant', ele], msg=pkdp, **kwargs)
Пример #4
0
def run_elegant(with_mpi=False):
    # also used by pkcli.rcscon
    r = template_common.exec_parameters()
    pkio.write_text('elegant.lte', r.lattice_file)
    ele = 'elegant.ele'
    pkio.write_text(ele, r.elegant_file)
    kwargs = {
        'output': ELEGANT_LOG_FILE,
        'env': elegant_common.subprocess_env(),
    }
    #TODO(robnagler) Need to handle this specially, b/c different binary
    if r.execution_mode == 'parallel' and with_mpi and mpi.cfg.cores > 1:
        mpi.run_program(['Pelegant', ele], **kwargs)
    else:
        pksubprocess.check_call_with_signals(['elegant', ele],
                                             msg=pkdlog,
                                             **kwargs)
Пример #5
0
def _run_elegant(bunch_report=False, with_mpi=False):
    exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE), locals(), locals())
    pkio.write_text('elegant.lte', lattice_file)
    ele = 'elegant.ele'
    pkio.write_text(ele, elegant_file)
    kwargs = {
        'output': ELEGANT_LOG_FILE,
        'env': elegant_common.subprocess_env(),
    }
    try:
        #TODO(robnagler) Need to handle this specially, b/c different binary
        if execution_mode == 'parallel' and with_mpi and mpi.cfg.cores > 1:
            mpi.run_program(['Pelegant', ele], **kwargs)
        else:
            pksubprocess.check_call_with_signals(['elegant', ele], msg=pkdlog, **kwargs)
    except Exception as e:
        # ignore elegant failures - errors will be parsed from the log
        pass
Пример #6
0
def parse_rpn_value(value, variable_list):
    variables = {x['name']: x['value'] for x in variable_list}
    depends = build_variable_dependency(value, variables, [])
    var_list = ' '.join(
        map(lambda x: '{} sto {}'.format(variables[x], x), depends))
    #TODO(pjm): security - need to scrub field value
    out = ''
    try:
        with open(os.devnull, 'w') as devnull:
            pkdc('rpnl "{}" "{}"', var_list, value)
            out = subprocess.check_output(
                ['rpnl', '{} {}'.format(var_list, value)],
                env=elegant_common.subprocess_env(),
                stderr=devnull,
            )
    except subprocess.CalledProcessError as e:
        return None, 'invalid'
    if len(out):
        return float(out.strip()), None
    return None, 'empty'
Пример #7
0
def _run_elegant(bunch_report=False, with_mpi=False):
    exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE), locals(),
         locals())
    pkio.write_text('elegant.lte', lattice_file)
    ele = 'elegant.ele'
    pkio.write_text(ele, elegant_file)
    kwargs = {
        'output': ELEGANT_LOG_FILE,
        'env': elegant_common.subprocess_env(),
    }
    try:
        #TODO(robnagler) Need to handle this specially, b/c different binary
        if execution_mode == 'parallel' and with_mpi and mpi.cfg.cores > 1:
            mpi.run_program(['Pelegant', ele], **kwargs)
        else:
            pksubprocess.check_call_with_signals(['elegant', ele],
                                                 msg=pkdlog,
                                                 **kwargs)
    except Exception as e:
        # ignore elegant failures - errors will be parsed from the log
        pass