def _set_up_paths(self): self._data_path = os.path.join(CURRENT_PATH, "data") self._tracking_jobs_path = os.path.join(self._data_path, "tracking_jobs") self._output_paths = [] self._madx_template_paths = [] self._madx_file_paths = [] self._sbs_match_output_paths = [] self._errors = {} for file_name in iotools.get_all_filenames_in_dir(self._tracking_jobs_path): if not file_name.startswith("~"): output_path = os.path.join(self._data_path, "output_" + file_name) iotools.create_dirs(output_path) self._output_paths.append(output_path) self._madx_template_paths.append(os.path.join(self._tracking_jobs_path, file_name)) self._madx_file_paths.append(os.path.join(output_path, file_name)) if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "linux3": self._path_to_drive = os.path.join(CURRENT_PATH, "..", "..", "drive", "Drive_God_lin") elif sys.platform == "win32": self._path_to_drive = os.path.join(CURRENT_PATH, "..", "..", "drive", "Drive_God_lin_win.exe") else: raise OSError("No drive version for given os: " + sys.platform) self._path_to_getllm = os.path.join(CURRENT_PATH, "..", "..", "GetLLM", "GetLLM.py") self._path_to_sbs = os.path.join(CURRENT_PATH, "..", "..", "SegmentBySegment", "SegmentBySegment.py") self._path_to_sbs_match = os.path.join( CURRENT_PATH, "..", "..", "SegmentBySegmentMatch", "SegmentBySegmentMatch.py" )
def match(lhc_run, ip, sbs_data_b1_path, sbs_data_b2_path, match_temporary_path, use_errors, mode): print "+++ Starting Segment by Segment Match +++" beam1_temporary_path = os.path.join(match_temporary_path, "Beam1") beam2_temporary_path = os.path.join(match_temporary_path, "Beam2") iotools.create_dirs(os.path.join(beam1_temporary_path, "sbs")) iotools.create_dirs(os.path.join(beam2_temporary_path, "sbs")) if mode == "phase": run_genphases(ip, match_temporary_path, sbs_data_b1_path, sbs_data_b2_path) elif mode == "coupling": run_gen_f_terms(ip, match_temporary_path, sbs_data_b1_path, sbs_data_b2_path) print "Copying files into temporary folder..." iotools.copy_item(os.path.join(CURRENT_PATH, "dumpB1.gplot"), match_temporary_path) iotools.copy_item(os.path.join(CURRENT_PATH, "dumpB2.gplot"), match_temporary_path) _copy_beam1_temp_files(ip, sbs_data_b1_path, beam1_temporary_path) _copy_beam2_temp_files(ip, sbs_data_b2_path, beam2_temporary_path) print "Getting matching range..." ((range_beam1_start_s, range_beam1_start_name), (range_beam1_end_s, range_beam1_end_name)) = _get_match_bpm_range( os.path.join(beam1_temporary_path, "sbs", "sbsphasext_IP" + ip + ".out")) ((range_beam2_start_s, range_beam2_start_name), (range_beam2_end_s, range_beam2_end_name)) = _get_match_bpm_range( os.path.join(beam2_temporary_path, "sbs", "sbsphasext_IP" + ip + ".out")) print "Matching range for Beam 1:", range_beam1_start_name, range_beam1_end_name print "Matching range for Beam 2:", range_beam2_start_name, range_beam2_end_name print "Running MADX..." label = "IP" + str(ip) _prepare_script_and_run_madx(lhc_run, label, mode, beam1_temporary_path, beam2_temporary_path, match_temporary_path, range_beam1_start_name, range_beam1_end_name, range_beam2_start_name, range_beam2_end_name) print "Writting sbs files from MADX results..." _write_sbs_data(ip, beam1_temporary_path, beam2_temporary_path, range_beam1_start_name, range_beam2_start_name) print "Building changeparameters_match.madx..." _build_changeparameters_file(match_temporary_path) print "Running GNUPlot..." _prepare_and_run_gnuplot(ip, match_temporary_path, range_beam1_start_s, range_beam1_end_s, range_beam2_start_s, range_beam2_end_s) print "+++ Ended Segment by Segment Match +++" return 0
def generate_variables_coupling(ip, variables_path=os.path.join( CURRENT_PATH, "match"), exclude_string=""): if not os.path.exists(variables_path): iotools.create_dirs(variables_path) exclude_list = _parse_exclude_string(exclude_string) variables_coupling = json.load(file(ALL_LISTS_COUPLE_BEAM1_PATH, 'r'))['getListsByIR'][ip] redefine_strengths_path = os.path.join(variables_path, "redefine_strengths.madx") define_variables_path = os.path.join(variables_path, "define_variables.madx") genchangpars_path = os.path.join(variables_path, "genchangpars.seqx") with open(redefine_strengths_path, "w") as redefine_strengths_file: with open(define_variables_path, "w") as define_variables_file: with open(genchangpars_path, "w") as genchangpars_file: genchangpars_file.write("select,flag=save, clear;\n") for variable_name in variables_coupling: if variable_name not in exclude_list: redefine_strengths_file.write(variable_name + "_0 = " + variable_name + ";\n") redefine_strengths_file.write(variable_name + " := " + variable_name + "_0 + " + "d" + variable_name + ";\n") define_variables_file.write("vary, name=d" + variable_name + ", step:=1e-4;\n") genchangpars_file.write( "select,flag=save,pattern=\"d" + variable_name + "\";\n")
def _run_Segment_by_Segment_Match(self, output_path): print "Running Segment by Segment Match..." beam1path = "" beam2path = "" if "beam1" in output_path: beam1path = output_path beam2path = beam1path.replace("beam1", "beam2") if "beam2" in output_path: beam2path = output_path beam1path = beam2path.replace("beam2", "beam1") if os.path.isdir(os.path.join(beam1path, "sbs")) and os.path.isdir( os.path.join(beam2path, "sbs")): temp_dir = beam1path.replace("beam1", "match") iotools.create_dirs(temp_dir) self._sbs_match_output_paths.append(temp_dir) for ip in IP_SEGMENTS_TO_RUN: print "For IP" + str(ip) + "..." call_command = sys.executable + " " + os.path.abspath(self._path_to_sbs_match) + \ " --ip " + str(ip) + \ " --beam1 " + beam1path + \ " --beam2 " + beam2path + \ " --temp " + temp_dir self._run_outer_process(call_command, "Segment by Segment Match") iotools.copy_item( os.path.join(temp_dir, "match", "changeparameters.madx"), os.path.join(temp_dir, "changeparametersIP" + str(ip) + ".madx")) iotools.delete_item(os.path.join(temp_dir, "match")) else: print "No data for both beams yet."
def get_systematic_errors(model_twiss, num_simulations, num_processes, output_dir, errors_path, accelerator, energy, tunex, tuney): print "Started systematic error script, using " + str(num_processes) + " processes for " + str(num_simulations) + " simulations" model_dir_path = os.path.dirname(model_twiss) run_data_path = os.path.join(output_dir, "RUN_DATA") if accelerator.upper().startswith("LHCB"): if errors_path is None: beam = accelerator.upper().replace("LHC", "").replace("RUNII", "").replace("_2016", "") errors_path = os.path.join(CURRENT_PATH, "..", "..", "MODEL", "LHC" + beam, "dipole_b2_errors") elif accelerator.upper() in ["ALBA", "ESRF"]: if not errors_path is None: print >> sys.stdout, str(accelerator) + " doesn't need error tables, ignoring input" else: print >> sys.stderr, "No error table templates available for", accelerator, "specify an error tables path (--error-tables option)" sys.exit(-1) iotools.create_dirs(run_data_path) pool = multiprocessing.Pool(processes=num_processes) print "Running simulations..." start_time = time.time() times = _run_parallel_simulations(run_data_path, model_dir_path, num_simulations, errors_path, accelerator, energy, tunex, tuney, pool) _show_time_statistics(times) end_time = time.time() print "Done (" + str(end_time - start_time) + " seconds)\n" print "Calculating systematic error bars..." start_time = time.time() _parallel_get_systematic_errors_binary_file(model_twiss, run_data_path, output_dir, pool, num_simulations, num_processes) end_time = time.time() print "Done (" + str(end_time - start_time) + " seconds)\n" print "Cleaning output directory..." _create_summary(model_twiss, num_simulations, num_processes, output_dir, errors_path, accelerator, energy, tunex, tuney) iotools.delete_item(run_data_path) print "All done."
def _copy_measurement_files(label, measurement_path, match_math): iotools.create_dirs(match_math) iotools.create_dirs(os.path.join(match_math, "sbs")) # GetLLM output files: _copy_files_with_extension(measurement_path, match_math, ".out") # SbS output files for the given label: _copy_files_which_contains(os.path.join(measurement_path, "sbs"), os.path.join(match_math, "sbs"), label) # SbS MAD-X files (not necessary but useful): _copy_files_with_extension(os.path.join(measurement_path, "sbs"), os.path.join(match_math, "sbs"), ".madx")
def match(lhc_run, ip, sbs_data_b1_path, sbs_data_b2_path, match_temporary_path, use_errors, mode): print "+++ Starting Segment by Segment Match +++" beam1_temporary_path = os.path.join(match_temporary_path, "Beam1") beam2_temporary_path = os.path.join(match_temporary_path, "Beam2") iotools.create_dirs(os.path.join(beam1_temporary_path, "sbs")) iotools.create_dirs(os.path.join(beam2_temporary_path, "sbs")) if mode == "phase": run_genphases(ip, match_temporary_path, sbs_data_b1_path, sbs_data_b2_path) elif mode == "coupling": run_gen_f_terms(ip, match_temporary_path, sbs_data_b1_path, sbs_data_b2_path) print "Copying files into temporary folder..." iotools.copy_item(os.path.join(CURRENT_PATH, "dumpB1.gplot"), match_temporary_path) iotools.copy_item(os.path.join(CURRENT_PATH, "dumpB2.gplot"), match_temporary_path) _copy_beam1_temp_files(ip, sbs_data_b1_path, beam1_temporary_path) _copy_beam2_temp_files(ip, sbs_data_b2_path, beam2_temporary_path) print "Getting matching range..." ((range_beam1_start_s, range_beam1_start_name), (range_beam1_end_s, range_beam1_end_name)) = _get_match_bpm_range(os.path.join(beam1_temporary_path, "sbs", "sbsphasext_IP" + ip + ".out")) ((range_beam2_start_s, range_beam2_start_name), (range_beam2_end_s, range_beam2_end_name)) = _get_match_bpm_range(os.path.join(beam2_temporary_path, "sbs", "sbsphasext_IP" + ip + ".out")) print "Matching range for Beam 1:", range_beam1_start_name, range_beam1_end_name print "Matching range for Beam 2:", range_beam2_start_name, range_beam2_end_name print "Running MADX..." label = "IP" + str(ip) _prepare_script_and_run_madx(lhc_run, label, mode, beam1_temporary_path, beam2_temporary_path, match_temporary_path, range_beam1_start_name, range_beam1_end_name, range_beam2_start_name, range_beam2_end_name) print "Writting sbs files from MADX results..." _write_sbs_data(ip, beam1_temporary_path, beam2_temporary_path, range_beam1_start_name, range_beam2_start_name) print "Building changeparameters_match.madx..." _build_changeparameters_file(match_temporary_path) print "Running GNUPlot..." _prepare_and_run_gnuplot(ip, match_temporary_path, range_beam1_start_s, range_beam1_end_s, range_beam2_start_s, range_beam2_end_s) print "+++ Ended Segment by Segment Match +++" return 0
def _run_sbsmatch_for_input(self, ip, input_dir_path, directory_to_check): beam1_directory_path = os.path.join(input_dir_path, "beam1") beam2_directory_path = os.path.join(input_dir_path, "beam2") iotools.create_dirs(self.path_to_check) log_stream = open(os.path.join(self.path_to_check, "test_log.log"), "w") self._run_sbsmatch_variables_for_input(ip, directory_to_check, log_stream) self._run_sbsmatch_constraints_for_input( ip, directory_to_check, beam1_directory_path, beam2_directory_path, log_stream ) self._run_sbsmatch_match_for_input( ip, directory_to_check, beam1_directory_path, beam2_directory_path, log_stream )
def generate_variables_phase(ip, variables_path=os.path.join(CURRENT_PATH, "match"), exclude_string="", lists=None): if not os.path.exists(variables_path): iotools.create_dirs(variables_path) exclude_list = _parse_exclude_string(exclude_string) if lists == None: variables_beam1 = json.load(file(ALL_LISTS_BEAM1_PATH, 'r'))['getListsByIR'][1] variables_common, variables_beam2 = json.load(file(ALL_LISTS_BEAM2_PATH, 'r'))['getListsByIR'] else: variables_beam1 = json.load(file(os.path.join(lists, "LHCB1", "AllLists.json"), 'r'))['getListsByIR'][1] variables_common, variables_beam2 = json.load(file(os.path.join(lists, "LHCB2", "AllLists.json"), 'r'))['getListsByIR'] ip_string = str(ip) apply_correction_file = open(os.path.join(variables_path, "applycorrection.seqx"), 'w') variables_common_file = open(os.path.join(variables_path, "variablesc.seqx"), 'w') variables_beam1_file = open(os.path.join(variables_path, "variablesb1.seqx"), 'w') variables_beam2_file = open(os.path.join(variables_path, "variablesb2.seqx"), 'w') variables_s_file = open(os.path.join(variables_path, "svariables.seqx"), 'w') variables_d_file = open(os.path.join(variables_path, "dvariables.seqx"), 'w') param_change_generator_file = open(os.path.join(variables_path, "genchangpars.seqx"), 'w') param_change_generator_file.write('select,flag=save, clear;') variables = variables_beam1[ip_string] param_change_generator_file.write('!B1\n') _phase_vars_to_files(apply_correction_file, variables_beam1_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables = variables_beam2[ip_string] param_change_generator_file.write('\n!B2\n') _phase_vars_to_files(apply_correction_file, variables_beam2_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables = variables_common[ip_string] param_change_generator_file.write('\n!B1 and B2\n') _phase_vars_to_files(apply_correction_file, variables_common_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables_common_file.close() variables_beam1_file.close() variables_beam2_file.close() variables_s_file.close() variables_d_file.close() param_change_generator_file.close()
def _run_sbsmatch_for_input(self, ip, input_dir_path, directory_to_check): beam1_directory_path = os.path.join(input_dir_path, "beam1") beam2_directory_path = os.path.join(input_dir_path, "beam2") iotools.create_dirs(self.path_to_check) log_stream = open(os.path.join(self.path_to_check, "test_log.log"), "w") self._run_sbsmatch_variables_for_input(ip, directory_to_check, log_stream) self._run_sbsmatch_constraints_for_input(ip, directory_to_check, beam1_directory_path, beam2_directory_path, log_stream) self._run_sbsmatch_match_for_input(ip, directory_to_check, beam1_directory_path, beam2_directory_path, log_stream)
def generate_variables_coupling(ip, variables_path=os.path.join(CURRENT_PATH, "match"), exclude_string=""): if not os.path.exists(variables_path): iotools.create_dirs(variables_path) exclude_list = _parse_exclude_string(exclude_string) variables_coupling = json.load(file(ALL_LISTS_COUPLE_BEAM1_PATH, 'r'))['getListsByIR'][ip] redefine_strengths_path = os.path.join(variables_path, "redefine_strengths.madx") define_variables_path = os.path.join(variables_path, "define_variables.madx") genchangpars_path = os.path.join(variables_path, "genchangpars.seqx") with open(redefine_strengths_path, "w") as redefine_strengths_file: with open(define_variables_path, "w") as define_variables_file: with open(genchangpars_path, "w") as genchangpars_file: genchangpars_file.write("select,flag=save, clear;\n") for variable_name in variables_coupling: if variable_name not in exclude_list: redefine_strengths_file.write(variable_name + "_0 = " + variable_name + ";\n") redefine_strengths_file.write(variable_name + " := " + variable_name + "_0 + " + "d" + variable_name + ";\n") define_variables_file.write("vary, name=d" + variable_name + ", step:=1e-4;\n") genchangpars_file.write("select,flag=save,pattern=\"d" + variable_name + "\";\n")
def prepare_error_tables(output_dir, num_processes, beam, energy): error_tables_dir = os.path.abspath(os.path.join(output_dir, "error_tables_" + energy)) iotools.create_dirs(error_tables_dir) try: os.symlink("/afs/cern.ch/eng/lhc/optics/V6.503", "db5") except(OSError): pass pool = multiprocessing.Pool(processes=num_processes) print "Preparing error files..." start_time = time.time() _parallel_prepare_error_files(error_tables_dir, beam, energy, pool) end_time = time.time() try: os.unlink("db5") os.unlink("ats") except(OSError): pass print "Done (" + str(end_time - start_time) + " seconds)\n"
def __init__(self, name, ip, measurement_data_path, match_path, beam): self._beam_match_path = os.path.join(match_path, "Beam" + str(beam) + "_" + name) self._beam_match_sbs_path = os.path.join(self._beam_match_path, "sbs") print "Copying measurement files for beam", str(beam),\ "into match folder..." iotools.create_dirs(self._beam_match_sbs_path) self._copy_measurement_files(ip, measurement_data_path) print "Getting matching range for beam" + str(beam) + "..." ((self._range_start_s, self._range_start_name), (self._range_end_s, self._range_end_name)) = MatchData._get_match_bpm_range( os.path.join(self._beam_match_sbs_path, "sbsphasext_IP" + str(ip) + ".out") ) print "Matching range for Beam " + str(beam),\ self._range_start_name, self._range_end_name self._modifiers = os.path.join(self._beam_match_sbs_path, "modifiers.madx") assert os.path.isfile(self._modifiers)
def _set_up_paths(self): self._data_path = os.path.join(CURRENT_PATH, "data") self._tracking_jobs_path = os.path.join(self._data_path, "tracking_jobs") self._output_paths = [] self._madx_template_paths = [] self._madx_file_paths = [] self._sbs_match_output_paths = [] self._errors = {} for file_name in iotools.get_all_filenames_in_dir( self._tracking_jobs_path): if not file_name.startswith("~"): output_path = os.path.join(self._data_path, "output_" + file_name) iotools.create_dirs(output_path) self._output_paths.append(output_path) self._madx_template_paths.append( os.path.join(self._tracking_jobs_path, file_name)) self._madx_file_paths.append( os.path.join(output_path, file_name)) if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "linux3": self._path_to_drive = os.path.join(CURRENT_PATH, "..", "..", "drive", "Drive_God_lin") elif sys.platform == "win32": self._path_to_drive = os.path.join(CURRENT_PATH, "..", "..", "drive", "Drive_God_lin_win.exe") else: raise OSError("No drive version for given os: " + sys.platform) self._path_to_getllm = os.path.join(CURRENT_PATH, "..", "..", "GetLLM", "GetLLM.py") self._path_to_sbs = os.path.join(CURRENT_PATH, "..", "..", "SegmentBySegment", "SegmentBySegment.py") self._path_to_sbs_match = os.path.join(CURRENT_PATH, "..", "..", "SegmentBySegmentMatch", "SegmentBySegmentMatch.py")
def _run_Segment_by_Segment_Match(self, output_path): print "Running Segment by Segment Match..." beam1path = "" beam2path = "" if "beam1" in output_path: beam1path = output_path beam2path = beam1path.replace("beam1", "beam2") if "beam2" in output_path: beam2path = output_path beam1path = beam2path.replace("beam2", "beam1") if os.path.isdir(os.path.join(beam1path, "sbs")) and os.path.isdir(os.path.join(beam2path, "sbs")): temp_dir = beam1path.replace("beam1", "match") iotools.create_dirs(temp_dir) self._sbs_match_output_paths.append(temp_dir) for ip in IP_SEGMENTS_TO_RUN: print "For IP" + str(ip) + "..." call_command = ( sys.executable + " " + os.path.abspath(self._path_to_sbs_match) + " --ip " + str(ip) + " --beam1 " + beam1path + " --beam2 " + beam2path + " --temp " + temp_dir ) self._run_outer_process(call_command, "Segment by Segment Match") iotools.copy_item( os.path.join(temp_dir, "match", "changeparameters.madx"), os.path.join(temp_dir, "changeparametersIP" + str(ip) + ".madx"), ) iotools.delete_item(os.path.join(temp_dir, "match")) else: print "No data for both beams yet."
def prepare_error_tables(output_dir, num_processes, beam, energy): error_tables_dir = os.path.abspath( os.path.join(output_dir, "error_tables_" + energy)) iotools.create_dirs(error_tables_dir) try: os.symlink("/afs/cern.ch/eng/lhc/optics/V6.503", "db5") except (OSError): pass pool = multiprocessing.Pool(processes=num_processes) print "Preparing error files..." start_time = time.time() _parallel_prepare_error_files(error_tables_dir, beam, energy, pool) end_time = time.time() try: os.unlink("db5") os.unlink("ats") except (OSError): pass print "Done (" + str(end_time - start_time) + " seconds)\n"
def generate_variables_phase(ip, variables_path=os.path.join( CURRENT_PATH, "match"), exclude_string="", lists=None): if not os.path.exists(variables_path): iotools.create_dirs(variables_path) exclude_list = _parse_exclude_string(exclude_string) if lists == None: variables_beam1 = json.load(file(ALL_LISTS_BEAM1_PATH, 'r'))['getListsByIR'][1] variables_common, variables_beam2 = json.load( file(ALL_LISTS_BEAM2_PATH, 'r'))['getListsByIR'] else: variables_beam1 = json.load( file(os.path.join(lists, "LHCB1", "AllLists.json"), 'r'))['getListsByIR'][1] variables_common, variables_beam2 = json.load( file(os.path.join(lists, "LHCB2", "AllLists.json"), 'r'))['getListsByIR'] ip_string = str(ip) apply_correction_file = open( os.path.join(variables_path, "applycorrection.seqx"), 'w') variables_common_file = open( os.path.join(variables_path, "variablesc.seqx"), 'w') variables_beam1_file = open( os.path.join(variables_path, "variablesb1.seqx"), 'w') variables_beam2_file = open( os.path.join(variables_path, "variablesb2.seqx"), 'w') variables_s_file = open(os.path.join(variables_path, "svariables.seqx"), 'w') variables_d_file = open(os.path.join(variables_path, "dvariables.seqx"), 'w') param_change_generator_file = open( os.path.join(variables_path, "genchangpars.seqx"), 'w') param_change_generator_file.write('select,flag=save, clear;') variables = variables_beam1[ip_string] param_change_generator_file.write('!B1\n') _phase_vars_to_files(apply_correction_file, variables_beam1_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables = variables_beam2[ip_string] param_change_generator_file.write('\n!B2\n') _phase_vars_to_files(apply_correction_file, variables_beam2_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables = variables_common[ip_string] param_change_generator_file.write('\n!B1 and B2\n') _phase_vars_to_files(apply_correction_file, variables_common_file, variables_s_file, variables_d_file, param_change_generator_file, variables, exclude_list) variables_common_file.close() variables_beam1_file.close() variables_beam2_file.close() variables_s_file.close() variables_d_file.close() param_change_generator_file.close()