def test_with_macro(tmp_path): """ Checks: - Output_file is created. """ content = (f"call,file='{LIB / 'lhc.macros.madx'}';\n" f"call,file='{LIB / 'general.macros.madx'}';\n") outfile = tmp_path / "job.with_macro.madx" with silence(): madx_wrapper.run_string(content, output_file=outfile, cwd=tmp_path) assert outfile.is_file() out_lines = outfile.read_text() assert out_lines == content
def test_with_nonexistent_file(tmp_path): """ Checks: - Madx crashes when tries to call a non-existent file - Logfile is created - Error message is read from log """ call_file = "does_not_exist.madx" content = f"call, file ='{call_file:s}';" log_file = tmp_path / "tmp_log.log" with pytest.raises(madx_wrapper.MadxError) as e: madx_wrapper.run_string(content, log_file=log_file, cwd=tmp_path) assert log_file.is_file() assert call_file in str(e.value)
def test_with_nonexistent_file(): """ Checks: - Madx crashes when tries to call a non-existent file - Logfile is created - Error message is read from log """ call_file = "does_not_exist.madx" content = "call, file ='{:s}';".format(call_file) with temporary_dir() as tmpdir: log_file = join(tmpdir, "tmp_log.log") with pytest.raises(madx_wrapper.MadxError) as e: madx_wrapper.run_string(content, log_file=log_file, cwd=tmpdir) assert isfile(log_file) assert call_file in str(e.value)
def test_with_macro(): """ Checks: - Output_file is created. """ content = "call,file='{}';\ncall,file='{}';\n".format( join(LIB, "lhc.macros.madx"), join(LIB, "general.macros.madx")) with temporary_dir() as tmpdir: outfile = join(tmpdir, "job.with_macro.madx") with silence(): madx_wrapper.run_string(content, output_file=outfile, cwd=tmpdir) assert exists(outfile) with open(outfile, "r") as of: out_lines = of.read() assert out_lines == content
def _create_corrected_model(twiss_out: str, change_params, accel_inst: Accelerator) -> tfs.TfsDataFrame: """ Use the calculated deltas in changeparameters.madx to create a corrected model """ madx_script: str = accel_inst.get_update_correction_script( twiss_out, change_params) twiss_out_path = Path(twiss_out) madx_script = f"! Based on model '{accel_inst.model_dir}'\n" + madx_script madx_wrapper.run_string( madx_script, output_file=twiss_out_path.parent / f"job.create_{twiss_out_path.stem}.madx", log_file=twiss_out_path.parent / f"job.create_{twiss_out_path.stem}.log", cwd=accel_inst.model_dir, # models are always run from there ) return tfs.read(twiss_out, index=NAME)
def create_instance_and_model(opt, accel_opt): if sys.flags.debug: numeric_level = getattr(logging, "DEBUG", None) ch = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( ' %(asctime)s %(levelname)s | %(name)s : %(message)s') ch.setFormatter(formatter) logging.getLogger().addHandler(ch) logging.getLogger().setLevel(numeric_level) else: numeric_level = getattr(logging, "WARNING", None) logging.basicConfig(level=numeric_level) # warning level to stderr create_dirs(opt.outputdir) accel_inst = manager.get_accelerator(accel_opt) LOGGER.info( f"Accelerator Instance {accel_inst.NAME}, model type {opt.type}") accel_inst.verify_object() creator = CREATORS[accel_inst.NAME][opt.type] creator.prepare_run(accel_inst, opt.outputdir) madx_script = creator.get_madx_script(accel_inst, opt.outputdir) run_string(madx_script, output_file=opt.writeto, log_file=opt.logfile)
def create_instance_and_model(opt, accel_opt) -> Accelerator: """ Manager Keyword Args: *--Required--* - **accel**: Choose the accelerator to use.Can be the class already. choices: ``['lhc', 'ps', 'esrf', 'psbooster', 'skekb', 'JPARC', 'petra', 'iota']`` Creator Keyword Args: *--Required--* - **outputdir** *(str)*: Output path for model, twiss files will be writen here. *--Optional--* - **logfile** *(str)*: Path to the file where to write the MAD-X script output.If not provided it will be written to sys.stdout. - **type**: Type of model to create. choices: ``('nominal', 'best_knowledge', 'coupling_correction')`` Accelerator Keyword Args: lhc: :mod:`omc3.model.accelerators.lhc` ps: :mod:`omc3.model.accelerators.ps` esrf: :mod:`omc3.model.accelerators.esrf` psbooster: :mod:`omc3.model.accelerators.psbooster` skekb: :mod:`omc3.model.accelerators.skekb` iota: :mod:`omc3.model.accelerators.iota` petra: :mod:`omc3.model.accelerators.petra` (not implemented) JPARC: Not implemented """ # Prepare paths create_dirs(opt.outputdir) accel_inst = manager.get_accelerator(accel_opt) LOG.info(f"Accelerator Instance {accel_inst.NAME}, model type {opt.type}") creator = CREATORS[accel_inst.NAME][opt.type] # Prepare model-dir output directory accel_inst.model_dir = opt.outputdir creator.prepare_run(accel_inst) # get madx-script with relative output-paths # as `cwd` changes run to correct directory. # The resulting model-dir is then more self-contained. (jdilly) accel_inst.model_dir = Path() madx_script = creator.get_madx_script(accel_inst) # Run madx to create model run_string(madx_script, output_file=opt.outputdir / JOB_MODEL_MADX, log_file=opt.logfile, cwd=opt.outputdir) # Check output and return accelerator instance accel_inst.model_dir = opt.outputdir creator.check_run_output(accel_inst) return accel_inst
def create_instance_and_model(opt, accel_opt): """ Manager Keyword Args: *--Required--* - **accel**: Choose the accelerator to use.Can be the class already. choices: ``['lhc', 'ps', 'esrf', 'psbooster', 'skekb', 'JPARC', 'petra', 'iota']`` Creator Keyword Args: *--Required--* - **outputdir** *(str)*: Output path for model, twiss files will be writen here. *--Optional--* - **logfile** *(str)*: Path to the file where to write the MAD-X script output.If not provided it will be written to sys.stdout. - **type**: Type of model to create, either nominal or best_knowledge choices: ``('nominal', 'best_knowledge', 'coupling_correction')`` - **writeto** *(str)*: Path to the file where to write the resulting MAD-X script. Accelerator Keyword Args: lhc: :mod:`omc3.model.accelerators.lhc` ps: :mod:`omc3.model.accelerators.ps` esrf: :mod:`omc3.model.accelerators.esrf` psbooster: :mod:`omc3.model.accelerators.psbooster` skekb: :mod:`omc3.model.accelerators.skekb` iota: :mod:`omc3.model.accelerators.iota` petra: :mod:`omc3.model.accelerators.petra` (not implemented) JPARC: Not implemented """ if sys.flags.debug: numeric_level = getattr(logging, "DEBUG", None) ch = logging.StreamHandler(sys.stdout) formatter = logging.Formatter(' %(asctime)s %(levelname)s | %(name)s : %(message)s') ch.setFormatter(formatter) logging.getLogger().addHandler(ch) logging.getLogger().setLevel(numeric_level) else: numeric_level = getattr(logging, "WARNING", None) logging.basicConfig(level=numeric_level) # warning level to stderr create_dirs(opt.outputdir) accel_inst = manager.get_accelerator(accel_opt) LOGGER.info(f"Accelerator Instance {accel_inst.NAME}, model type {opt.type}") accel_inst.verify_object() creator = CREATORS[accel_inst.NAME][opt.type] creator.prepare_run(accel_inst, opt.outputdir) madx_script = creator.get_madx_script(accel_inst, opt.outputdir) run_string(madx_script, output_file=opt.writeto, log_file=opt.logfile)