def _callMadx(pathToInputFile, attemptsLeft=5): result = madx_wrapper.resolve_and_run_file(pathToInputFile, log_file=DEV_NULL) if result is not 0: # then madx failed for whatever reasons, lets try it again (tbach) print "madx failed. result:", result, "pathToInputFile:", pathToInputFile, "attempts left:", attemptsLeft if attemptsLeft is 0: raise Exception("madx finally failed, can not continue") print "lets wait 0.5s and try again..." time.sleep(0.5) return _callMadx(pathToInputFile, attemptsLeft - 1) return result
def _madcreator(dpps, files_dict): ''' :param dpps: list of dp/p to create model for :param dict files_dict: dpp_value --> corresponding_filenames ''' # creating the DPP dppstring = '' dppstring_ac = '' for dpp in dpps: if _InputData.accel == "ESRF": _InputData.accel = "STORAGE04_SS23_7m" path_to_twiss_dpp = _join_with_output_path("twiss_"+str(dpp)+".dat") if not os.path.exists(path_to_twiss_dpp): dppstring = dppstring+'twiss, chrom,sequence='+_InputData.accel+', deltap='+str(dpp)+', file="'+path_to_twiss_dpp+'";\n' # if the model has twiss_ac.dat: if os.path.exists(_InputData.twissfile.replace(".dat","_ac.dat")): # this is only correct as long as the filenames are <filename>_ac.dat and <filename>.dat! path_to_twiss_ac_dpp = _join_with_output_path("twiss_"+str(dpp)+"_ac.dat") dppstring_ac = dppstring_ac+'twiss, chrom,sequence='+_InputData.accel+', deltap='+str(dpp)+', file="'+path_to_twiss_ac_dpp+'";\n' else: # do not create ac file if we don't have ac in our original model.. dppstring_ac = '' if not dppstring: print "No need to run madx" return 0 dict_for_replacing = {} dict_for_replacing["DPP"] = dppstring (qx, qy, qdx, qdy, qmx, qmy) = _get_tunes(files_dict) if _InputData.accel.upper().startswith("LHCB"): dict_for_replacing["DP_AC_P"] = dppstring_ac if _InputData.lhc_run == "RUNI": dict_for_replacing["RUN"] = "I" elif _InputData.lhc_run == "RUNII": dict_for_replacing["RUN"] = "II" elif _InputData.lhc_run == "RUNII_2016": dict_for_replacing["RUN"] = "II_2016" if _InputData.accel == 'LHCB1': dict_for_replacing["NUM_BEAM"] = "1" elif _InputData.accel == 'LHCB2': dict_for_replacing["NUM_BEAM"] = "2" else: print "WARNING: Could not decide what BEAM should be" dict_for_replacing["QMX"] = int(qx * 1000000) dict_for_replacing["QMY"] = int(qy * 1000000) dict_for_replacing["QX"] = qx dict_for_replacing["QY"] = qy dict_for_replacing["QDX"] = qdx dict_for_replacing["QDY"] = qdy dict_for_replacing["STOP"] = "!" path_to_job_chrom_template = os.path.join(_InputData.path_to_beta_beat, "MODEL", "LHCB", "model", "job.twiss_chrom.madx.macro") elif _InputData.accel == "STORAGE04_SS23_7m": dict_for_replacing["QMX"] = qmx dict_for_replacing["QMY"] = qmy dict_for_replacing["ESRF_MODEL"] = os.path.join(_InputData.path_to_beta_beat, "MODEL", "ESRF") path_to_job_chrom_template = os.path.join(_InputData.path_to_beta_beat, "MODEL", "ESRF", "job.ESRF.twiss_chrom.madx") for testpath in [_InputData.output_path, os.path.dirname(_InputData.twissfile)]: _tmpmod = os.path.join(testpath, 'modifiers.madx') if os.path.isfile(_tmpmod): print "INFO: Using", _tmpmod dict_for_replacing["MODIFIERS"] = _tmpmod break print "Creating madx" path_to_job_chrom_madx = _join_with_output_path("job.chrom.madx") path_to_job_chrom_madx_log = _join_with_output_path("log.job.chrom.madx") Utilities.iotools.replace_keywords_in_textfile( path_to_job_chrom_template, dict_for_replacing, path_to_job_chrom_madx ) print "Running madx" errcode = madx_wrapper.resolve_and_run_file(path_to_job_chrom_madx, log_file=path_to_job_chrom_madx_log) if 0 != errcode: print >> sys.stderr, "Mad-X failed. Check log file: " + str(path_to_job_chrom_madx_log) raise ValueError("Mad-X failed")
def _madcreator(dpps, files_dict): ''' :param dpps: list of dp/p to create model for :param dict files_dict: dpp_value --> corresponding_filenames ''' # creating the DPP dppstring = '' dppstring_ac = '' for dpp in dpps: if _InputData.accel == "ESRF": _InputData.accel = "STORAGE04_SS23_7m" path_to_twiss_dpp = _join_with_output_path("twiss_" + str(dpp) + ".dat") if not os.path.exists(path_to_twiss_dpp): dppstring = dppstring + 'twiss, chrom,sequence=' + _InputData.accel + ', deltap=' + str( dpp) + ', file="' + path_to_twiss_dpp + '";\n' # if the model has twiss_ac.dat: if os.path.exists( _InputData.twissfile.replace(".dat", "_ac.dat") ): # this is only correct as long as the filenames are <filename>_ac.dat and <filename>.dat! path_to_twiss_ac_dpp = _join_with_output_path("twiss_" + str(dpp) + "_ac.dat") dppstring_ac = dppstring_ac + 'twiss, chrom,sequence=' + _InputData.accel + ', deltap=' + str( dpp) + ', file="' + path_to_twiss_ac_dpp + '";\n' else: # do not create ac file if we don't have ac in our original model.. dppstring_ac = '' if not dppstring: print "No need to run madx" return 0 dict_for_replacing = {} dict_for_replacing["DPP"] = dppstring (qx, qy, qdx, qdy, qmx, qmy) = _get_tunes(files_dict) if _InputData.accel.upper().startswith("LHCB"): dict_for_replacing["DP_AC_P"] = dppstring_ac if _InputData.lhc_run == "RUNI": dict_for_replacing["RUN"] = "I" elif _InputData.lhc_run == "RUNII": dict_for_replacing["RUN"] = "II" elif _InputData.lhc_run == "RUNII_2016": dict_for_replacing["RUN"] = "II_2016" if _InputData.accel == 'LHCB1': dict_for_replacing["NUM_BEAM"] = "1" elif _InputData.accel == 'LHCB2': dict_for_replacing["NUM_BEAM"] = "2" else: print "WARNING: Could not decide what BEAM should be" dict_for_replacing["QMX"] = int(qx * 1000000) dict_for_replacing["QMY"] = int(qy * 1000000) dict_for_replacing["QX"] = qx dict_for_replacing["QY"] = qy dict_for_replacing["QDX"] = qdx dict_for_replacing["QDY"] = qdy dict_for_replacing["STOP"] = "!" path_to_job_chrom_template = os.path.join( _InputData.path_to_beta_beat, "MODEL", "LHCB", "model", "job.twiss_chrom.madx.macro") elif _InputData.accel == "STORAGE04_SS23_7m": dict_for_replacing["QMX"] = qmx dict_for_replacing["QMY"] = qmy dict_for_replacing["ESRF_MODEL"] = os.path.join( _InputData.path_to_beta_beat, "MODEL", "ESRF") path_to_job_chrom_template = os.path.join(_InputData.path_to_beta_beat, "MODEL", "ESRF", "job.ESRF.twiss_chrom.madx") for testpath in [ _InputData.output_path, os.path.dirname(_InputData.twissfile) ]: _tmpmod = os.path.join(testpath, 'modifiers.madx') if os.path.isfile(_tmpmod): print "INFO: Using", _tmpmod dict_for_replacing["MODIFIERS"] = _tmpmod break print "Creating madx" path_to_job_chrom_madx = _join_with_output_path("job.chrom.madx") path_to_job_chrom_madx_log = _join_with_output_path("log.job.chrom.madx") Utilities.iotools.replace_keywords_in_textfile(path_to_job_chrom_template, dict_for_replacing, path_to_job_chrom_madx) print "Running madx" errcode = madx_wrapper.resolve_and_run_file( path_to_job_chrom_madx, log_file=path_to_job_chrom_madx_log) if 0 != errcode: print >> sys.stderr, "Mad-X failed. Check log file: " + str( path_to_job_chrom_madx_log) raise ValueError("Mad-X failed")
def main(accel_cls, options): ''' :Parameters: 'options': Values Values instance with all options from OptionParser :Return: int 0 if execution was successful otherwise !=0 ''' print("+++ Starting Segment by Segment +++") print("Using accelerator class: " + accel_cls.__name__) measurement_path = options.path w_path = options.wpath if w_path == "0": w_path = measurement_path input_data = _InputData(measurement_path, w_path) save_path = options.save + os.path.sep Utilities.iotools.create_dirs(save_path) elements_data = options.segf.split(',') error_cut = float(options.cuts) twiss_file = options.twiss print("Input model twiss file", twiss_file) input_model = _try_to_load_twiss(twiss_file) if input_model is None: raise IOError("Cannot read input model, aborting.") twiss_directory = os.path.dirname(twiss_file) elements_names, start_bpms, end_bpms = structure_elements_info( elements_data) summaries = _Summaries(save_path) for element_name in elements_names: print("Started processing", element_name) start_bpm_name, end_bpm_name, is_element = get_good_bpms( input_data, error_cut, input_model, start_bpms, end_bpms, element_name) (start_bpm_horizontal_data, start_bpm_vertical_data, end_bpm_horizontal_data, end_bpm_vertical_data) = gather_data(input_data, start_bpm_name, end_bpm_name) element_has_dispersion, start_bpm_dispersion, end_bpm_dispersion = _get_dispersion_parameters( input_data, start_bpm_name, end_bpm_name) element_has_coupling, f_ini, f_end = _get_coupling_parameters( input_data, start_bpm_name, end_bpm_name) element_has_chrom, chrom_ini, chrom_end = _get_chrom_parameters( input_data, start_bpm_name, end_bpm_name) accel_instance = accel_cls.get_segment( element_name, start_bpm_name, end_bpm_name, os.path.join(save_path, "modifiers.madx")) if not options.madpass: _run4mad(save_path, accel_instance, start_bpm_horizontal_data, start_bpm_vertical_data, end_bpm_horizontal_data, end_bpm_vertical_data, start_bpm_dispersion, end_bpm_dispersion, f_ini, f_end, chrom_ini, chrom_end, options.path, twiss_directory, input_data.couple_method, options.bb, options.mad) else: print("Just rerunning mad") mad_file_path, log_file_path = _get_files_for_mad( save_path, element_name) madx_wrapper.resolve_and_run_file(mad_file_path, log_file=log_file_path) propagated_models = _PropagatedModels(save_path, element_name) kmod_data_file_x, kmod_data_file_y = _get_kmod_files() getAndWriteData(element_name, input_data, input_model, propagated_models, save_path, is_element, element_has_dispersion, element_has_coupling, element_has_chrom, accel_instance, summaries, kmod_data_file_x, kmod_data_file_y) print("Everything done for", element_name, "\n") summaries.write_summaries_to_files() print("+++ Ended Segment by Segment +++")
def _callMadx(pathToInputFile): return madx_wrapper.resolve_and_run_file(pathToInputFile, log_file=DEV_NULL)