def test_importer(import_req): from pykern.pkcollections import PKDict from sirepo.template import elegant import sirepo.lib for fn in pkio.sorted_glob(pkunit.data_dir().join('*')): if not pkio.has_file_extension(fn, ('ele', 'lte')) \ or fn.basename.endswith('.ele.lte'): continue k = PKDict() pkdlog('file={}', fn) if fn.basename.startswith('deviance-'): try: data = elegant.import_file(import_req(fn)) except Exception as e: k.actual = f'{e}\n' else: k.actual = 'did not raise exception' elif fn.ext == '.lte': data = elegant.import_file(import_req(fn)) data['models']['commands'] = [] g = elegant._Generate(data) g.sim() j = g.jinja_env k.actual = j.rpn_variables + j.lattice else: f = sirepo.lib.Importer('elegant').parse_file(fn).write_files(pkunit.work_dir()) k.actual_path = f.commands pkunit.file_eq(fn.basename + '.txt', **k)
def _code(files=None): from pykern import pkunit, pkio, pkjson from pykern.pkdebug import pkdp import inspect import sirepo.lib for i, s in enumerate( pkio.sorted_glob(pkunit.data_dir().join( f'{inspect.stack()[1].function.split("_")[1]}_*', ))): t = s.basename.split('_')[0] d = sirepo.lib.Importer(t).parse_file( pkio.sorted_glob(s.join('first*'))[0]) d2 = d.copy() d2.pkdel('version') for k in [k for k in d2.keys() if '_SimData__' in k]: d2.pkdel(k) pkunit.file_eq(s.join('out.json'), d2) w = pkunit.work_dir().join(s.basename) r = d.write_files(w) for o in pkio.sorted_glob(pkunit.data_dir().join(s.basename, '*.out')): pkunit.file_eq(o, actual_path=w.join(o.basename).new(ext='')) if files: pkunit.pkok( set(files[i]).issubset(set(r.output_files)), 'expecting files={} to be subset of output_files={}', files, r.output_files, )
def test_import_opal_export_madx(import_req): from pykern.pkunit import pkeq, file_eq from sirepo.template import opal from sirepo.template.opal import OpalMadxConverter data = opal.import_file(import_req(pkunit.data_dir().join('test2.in'))) actual = OpalMadxConverter().to_madx_text(data) file_eq( 'test2.madx', actual=actual, )
def test_import_elegant_export_madx(import_req): from pykern.pkunit import pkeq, file_eq from sirepo.template import elegant from sirepo.template.elegant import ElegantMadxConverter data = elegant.import_file(import_req(pkunit.data_dir().join('test1.ele'))) data = elegant.import_file(import_req(pkunit.data_dir().join('test1.lte')), test_data=data) # this is updated from javascript unfortunately data.models.bunch.longitudinalMethod = '3' actual = ElegantMadxConverter().to_madx_text(data) file_eq( 'test1.madx', actual=actual, )
def test_generate_python(): from pykern import pkio from pykern import pkunit from sirepo.template import synergia import re with pkunit.save_chdir_work() as d: for f in pkio.sorted_glob(pkunit.data_dir().join('*.txt')): e = pkio.read_text(f) m = re.search(r'^#\s*(.*\S)\s*$', e, flags=re.MULTILINE) assert m pkunit.file_eq( f, synergia._generate_parameters_file(_example_data( m.group(1)), ), )
def _generate_source(fc, sim, name): from pykern import pkio, pkunit, pkdebug, pkcompat import re d = pkcompat.from_bytes(fc.sr_get( 'pythonSource', PKDict( simulation_id=sim.models.simulation.simulationId, simulation_type=sim.simulationType, ), ).data) pkunit.file_eq( re.sub( r'[^\w\-\.]', '', re.sub(r'\s', '-', '{}.py'.format(name.lower())), ), d, )
def test_from_elegant_to_madx_and_back(): from pykern.pkunit import pkeq, file_eq from sirepo.template import elegant from sirepo.template.elegant import ElegantMadxConverter with pkunit.save_chdir_work() as d: for name in ('SPEAR3', 'Compact Storage Ring', 'Los Alamos Proton Storage Ring'): data = _example_data(name) actual = ElegantMadxConverter().to_madx_text(data) file_eq( name.lower().replace(' ', '-') + '.madx', actual=actual, ) file_eq( name.lower().replace(' ', '-') + '.lte', actual=elegant.python_source_for_model( ElegantMadxConverter().from_madx_text(actual), None, ), )
def test_elegant(): from pykern.pkdebug import pkdp from pykern import pkunit, pkio, pkjson import sirepo.lib import shutil for s in pkio.sorted_glob(pkunit.data_dir().join('*')): t = s.basename.split('_')[0] d = sirepo.lib.Importer(t).parse_file( pkio.sorted_glob(s.join('first*'))[0]) d2 = d.copy() d2.pkdel('version') for k in [k for k in d2.keys() if '_SimData__' in k]: d2.pkdel(k) pkunit.file_eq(s.join('out.json'), d2) w = pkunit.work_dir().join(s.basename) r = d.write_files(w) #TODO(robnagler) may not exist in all cases pkunit.pkeq('run_setup.acceptance.sdds', r.output_files[0]) for o in pkio.sorted_glob(pkunit.data_dir().join(s.basename, '*.out')): pkunit.file_eq(o, actual_path=w.join(o.basename).new(ext=''))