def run_just_twiss(self): for matcher in self._matchers_list: self._extract_sequences(matcher) self._set_initial_values(matcher) self._define_aux_vars(matcher) self._run_corrected_twiss(matcher) madx_script = self._main_template.format( LIB=self._accel_cls.MACROS_NAME, MAIN_SEQ=self._accel_cls.load_main_seq_madx(), EXTRACT_SEQUENCES="\n".join(self._extract_sequences_list), SET_INITIAL_VALUES="\n".join(self._set_initial_values_list), DEFINE_CONSTRAINTS_AUX_VALS="\n".join( self._aux_var_definition_list), START_MATCH="", DEFINE_VARIABLES="\n".join(self._define_variables_list), SET_MATCHING_MACROS="\n".join(self._set_matching_macros_list), END_MATCH="", GEN_CHANGEPARAMETERS="\n".join(self._gen_changeparameters_list), SAVE_CHANGEPARAMETERS=CALL_CHANGEPARAMETERS.format( match_path=self._match_path), RUN_CORRECTED_TWISS="\n".join(self._run_corrected_twiss_list), ) madx_wrapper.resolve_and_run_string(madx_script, output_file=self._output_file, log_file=self._log_file)
def _create_corrected_model(twiss_out, change_params, accel_inst, debug): """ Use the calculated deltas in changeparameters.madx to create a corrected model """ # create script from template madx_script = accel_inst.get_update_correction_job(twiss_out, change_params) # run madx if debug: madx_wrapper.resolve_and_run_string(madx_script) else: madx_wrapper.resolve_and_run_string( madx_script, log_file=os.devnull, )
def _run_tracking_model(directory, options): tbt_path = _get_tbt_path(directory) if options.usetracking: if not os.path.exists(tbt_path): raise IOError("Tracking data '" + options.usetracking + "' not found!") LOGGER.info("Using previously created model and tracking: " + options.usetracking) return print("Creating model and tracking...") madx_script = _get_madx_script(BEAM, directory, options) madx_wrapper.resolve_and_run_string( madx_script, madx_path=MADX_PATH, output_file=os.path.join(directory, 'job.test.madx'), log_file=os.path.join(directory, 'madx_log.txt')) track_path = _get_track_path(directory, one=True) with silence(): if options.addnoise: ADDbpmerror.convert_files(infile=track_path, outfile=tbt_path, x_error=1e-4, y_error=1e-4) else: ADDbpmerror.convert_files(infile=track_path, outfile=tbt_path) headers = [ "\n".join([ "#SDDSASCIIFORMAT v1", "#Beam: LHCB" + str(BEAM), "#Created: " + time.strftime("%Y-%m-%d at %H:%M:%S") + " By: ADDbpmerror", "#Number of turns: " + str(NUM_TURNS), "#Number of horizontal monitors: 522", "#Number of vertical monitors: 522", "#Acquisition date: " + time.strftime("%Y-%m-%d at %H:%M:%S"), "#dpp: 0.0", ]) ] lines = [] with open(tbt_path, "r") as tbt_data: for line in tbt_data: if line.startswith("#"): headers.append(line) else: lines.append(line) with open(tbt_path, "w") as tbt_data: for header in headers: tbt_data.write(header) for line in reversed(lines): tbt_data.write(line)
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.resolve_and_run_string(content, log_file=log_file, cwd=tmpdir) assert isfile(log_file) assert call_file in str(e)
def _get_elements_from_madx(accel_inst, output_dir, locations, correction): """ Create and call the madx jobs to apply the corrections """ corr_name = os.path.splitext(os.path.basename(correction))[0] twiss_path = os.path.join(output_dir, "{:s}_{:s}.dat".format(TWISS_CORRECT, corr_name)) job_content = _get_madx_job(accel_inst, twiss_path, locations, correction) madx_wrapper.resolve_and_run_string( job_content, output_file=os.path.join(output_dir, "job.corrections.madx"), log_file=os.path.join(output_dir, "job.corrections.log"), ) return tfs_pandas.read_tfs(twiss_path, index="NAME")
def _run_tracking_model(directory, options): tbt_path = _get_tbt_path(directory) if options.usetracking: if not os.path.exists(tbt_path): raise IOError("Tracking data '" + options.usetracking + "' not found!") LOGGER.info("Using previously created model and tracking: " + options.usetracking) return print("Creating model and tracking...") madx_script = _get_madx_script(BEAM, directory, options) print(tbt_path) print(MADX_PATH) print(directory) madx_wrapper.resolve_and_run_string( madx_script, madx_path=MADX_PATH, output_file=os.path.join(directory, 'job.test.madx'), log_file=os.path.join(directory, 'madx_log.txt') ) track_path = _get_track_path(directory, one=True) with silence(): if options.addnoise: ADDbpmerror.convert_files(infile=track_path, outfile=tbt_path, x_error=1e-4, y_error=1e-4) else: ADDbpmerror.convert_files(infile=track_path, outfile=tbt_path) headers = [ "\n".join(["#SDDSASCIIFORMAT v1", "#Beam: LHCB" + str(BEAM), "#Created: " + time.strftime("%Y-%m-%d at %H:%M:%S") + " By: ADDbpmerror", "#Number of turns: " + str(NUM_TURNS), "#Number of horizontal monitors: 522", "#Number of vertical monitors: 522", "#Acquisition date: " + time.strftime("%Y-%m-%d at %H:%M:%S"), "#dpp: 0.0", ]) ] lines = [] with open(tbt_path, "r") as tbt_data: for line in tbt_data: if line.startswith("#"): headers.append(line) else: lines.append(line) with open(tbt_path, "w") as tbt_data: for header in headers: tbt_data.write(header) for line in reversed(lines): tbt_data.write(line)
def _create_corrected_model(twiss_out, change_params, accel_inst, debug): """ Use the calculated deltas in changeparameters.madx to create a corrected model """ # create script from template madx_script = accel_inst.get_update_correction_job(twiss_out, change_params) # run madx if debug: with logging_tools.TempFile("correct_iter_madxout.tmp", LOG.debug) as log_file: madx_wrapper.resolve_and_run_string( madx_script, log_file=log_file, ) else: madx_wrapper.resolve_and_run_string( madx_script, log_file=os.devnull, )
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 = os.path.join(tmpdir, "tmp_log.log") with pytest.raises(madx_wrapper.MadxError) as e: madx_wrapper.resolve_and_run_string(content, log_file=log_file, cwd=tmpdir) assert os.path.isfile(log_file) assert call_file in str(e)
def _call_madx(accel_inst, corrections): """ Create and call the madx jobs to apply the corrections """ original_content = _get_madx_job(accel_inst) for dir_correct in sorted(corrections): dir_out = os.path.join(dir_correct, RESULTS_DIR) iotools.create_dirs(dir_out) job_content = original_content job_content += "twiss, file='{:s}';\n".format(os.path.join(dir_out, getdiff.TWISS_NOT_CORRECTED)) for file in sorted(corrections[dir_correct]): job_content += "call, file='{:s}';\n".format(file) job_content += "twiss, file='{:s}';\n".format(os.path.join(dir_out, getdiff.TWISS_CORRECTED)) madx_wrapper.resolve_and_run_string( job_content, output_file=os.path.join(dir_out, MADX_FILE), log_file=os.path.join(dir_out, MADXLOG_FILE), )
def _call_madx(accel_inst, corrections): """ Create and call the madx jobs to apply the corrections """ original_content = _get_madx_job(accel_inst) for dir_correct in sorted(corrections): dir_out = os.path.join(dir_correct, RESULTS_DIR) iotools.create_dirs(dir_out) job_content = original_content job_content += "twiss, file='{:s}';\n".format(os.path.join(dir_out, getdiff.TWISS_NOT_CORRECTED)) for file in sorted(corrections[dir_correct]): job_content += "call, file='{:s}';\n".format(file) job_content += "twiss, file='{:s}';\n".format(os.path.join(dir_out, getdiff.TWISS_CORRECTED)) madx_wrapper.resolve_and_run_string( job_content, output_file=os.path.join(dir_out, MADX_FILE), log_file=os.path.join(dir_out, MADXLOG_FILE), )
def test_with_macro(): """ Checks: - Output_file is created. - Macros resolve correctly. """ content = ( "!@require lhc_runII\n" "!@require lhc_runII_ats.macros.madx\n" ) resolved_lines = [ 'call,file="{}";'.format(join(madx_wrapper.LIB, "lhc_runII.macros.madx")), 'call,file="{}";'.format(join(madx_wrapper.LIB, "lhc_runII_ats.macros.madx")) ] with temporary_dir() as tmpdir: outfile = join(tmpdir, "job.with_macro.madx") with silence(): madx_wrapper.resolve_and_run_string(content, output_file=outfile, cwd=tmpdir) assert exists(outfile) with open(outfile, "r") as of: out_lines = of.read().split("\n") out_lines = [ol.replace(" ", "") for ol in out_lines] assert all([r in out_lines for r in resolved_lines])
def test_with_macro(): """ Checks: - Output_file is created. - Macros resolve correctly. """ content = ("!@require lhc_runII\n" "!@require lhc_runII_ats.macros.madx\n") resolved_lines = [ 'call,file="{}";'.format( os.path.join(madx_wrapper.LIB, "lhc_runII.macros.madx")), 'call,file="{}";'.format( os.path.join(madx_wrapper.LIB, "lhc_runII_ats.macros.madx")) ] with temporary_dir() as tmpdir: outfile = os.path.join(tmpdir, "job.with_macro.madx") with silence(): madx_wrapper.resolve_and_run_string(content, output_file=outfile, cwd=tmpdir) assert os.path.exists(outfile) with open(outfile, "r") as of: out_lines = of.read().split("\n") out_lines = [ol.replace(" ", "") for ol in out_lines] assert all([r in out_lines for r in resolved_lines])
def run_madx(madx_script, logfile=None, writeto=None): madx_wrapper.resolve_and_run_string( madx_script, output_file=writeto, log_file=logfile )
def run_madx(madx_script, logfile=None, writeto=None): madx_wrapper.resolve_and_run_string(madx_script, output_file=writeto, log_file=logfile)
def run_madx_command(command, macros_list, output_file, log_file): madx_script = create_madx_script(command, macros_list) madx_wrapper.resolve_and_run_string(madx_script, output_file, log_file)