From 8a7a06758e58ed54cd4ba633d386485e28e96708 Mon Sep 17 00:00:00 2001 From: sterbini Date: Wed, 23 Jun 2021 09:58:30 +0200 Subject: [PATCH 01/31] Changing the link address on the config --- python_examples/ions_python/config.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index daac18e..82fbb4c 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -3,8 +3,7 @@ # Links to be made for tools and scripts 'links' : { 'tracking_tools': '/afs/cern.ch/eng/tracking-tools', - #'modules': 'tracking_tools/modules', - 'modules': '../../', + 'modules': 'tracking_tools/modules', 'tools': 'tracking_tools/tools', 'beambeam_macros': 'tracking_tools/beambeam_macros', 'errors': 'tracking_tools/errors', From 9f27f25718f5b6ba19921fd27e62c79f060a4c2f Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Thu, 24 Jun 2021 12:40:37 +0200 Subject: [PATCH 02/31] Work in progress, untested --- pymask/beambeam.py | 70 ++++++++++++++--------- python_examples/ions_python/000_pymask.py | 34 +++++------ python_examples/ions_python/config.py | 27 +++++---- 3 files changed, 76 insertions(+), 55 deletions(-) diff --git a/pymask/beambeam.py b/pymask/beambeam.py index 67fc7b6..1641c34 100644 --- a/pymask/beambeam.py +++ b/pymask/beambeam.py @@ -181,7 +181,8 @@ def generate_set_of_bb_encounters_1beam( harmonic_number = 35640, bunch_spacing_buckets = 10, numberOfHOSlices = 11, - bunch_charge_ppb = 0., + bunch_num_particles = 0., + bunch_particle_charge = 0., sigt=0.0755, relativistic_beta=1., ip_names = ['ip1', 'ip2', 'ip5', 'ip8'], @@ -201,7 +202,8 @@ def generate_set_of_bb_encounters_1beam( if len(myBBLRlist)>0: myBBLR=pd.DataFrame(myBBLRlist)[['beam','other_beam','ip_name','label','identifier']] - myBBLR['self_charge_ppb'] = bunch_charge_ppb + myBBLR['self_num_particles'] = bunch_num_particles + myBBLR['self_particle_charge'] = bunch_particle_charge myBBLR['self_relativistic_beta'] = relativistic_beta myBBLR['elementName']=myBBLR.apply(lambda x: elementName(x.label, x.ip_name.replace('ip', ''), x.beam, x.identifier), axis=1) myBBLR['other_elementName']=myBBLR.apply( @@ -229,7 +231,7 @@ def generate_set_of_bb_encounters_1beam( myBBHO=pd.DataFrame(myBBHOlist)[['beam','other_beam', 'ip_name','label','identifier']] - myBBHO['self_charge_ppb'] = bunch_charge_ppb/numberOfHOSlices + myBBHO['self_num_particles'] = bunch_num_particles/numberOfHOSlices myBBHO['self_relativistic_beta'] = relativistic_beta for ip_nn in ip_names: myBBHO.loc[myBBHO['ip_name']==ip_nn, 'atPosition']=list(z_centroids) @@ -249,7 +251,7 @@ def generate_set_of_bb_encounters_1beam( return myBB -def generate_mad_bb_info(bb_df, mode='dummy', madx_reference_bunch_charge=1): +def generate_mad_bb_info(bb_df, mode='dummy', madx_reference_bunch_num_particles=1): if mode == 'dummy': bb_df['elementClass']='beambeam' @@ -259,7 +261,7 @@ def generate_mad_bb_info(bb_df, mode='dummy', madx_reference_bunch_charge=1): 'yma = 1, ' + \ f'charge = 0*{charge}, ' +\ 'slot_id = %d'%({'bb_lr': 4, 'bb_ho': 6}[label]) # need to add 60 for central - bb_df['elementDefinition']=bb_df.apply(lambda x: elementDefinition(x.elementName, x.elementClass, eattributes(x['self_charge_ppb'], x['label'])), axis=1) + bb_df['elementDefinition']=bb_df.apply(lambda x: elementDefinition(x.elementName, x.elementClass, eattributes(x['self_num_particles'], x['label'])), axis=1) bb_df['elementInstallation']=bb_df.apply(lambda x: elementInstallation(x.elementName, x.elementClass, x.atPosition, x.ip_name), axis=1) elif mode=='from_dataframe': bb_df['elementClass']='beambeam' @@ -272,7 +274,7 @@ def generate_mad_bb_info(bb_df, mode='dummy', madx_reference_bunch_charge=1): bb_df['elementDefinition']=bb_df.apply(lambda x: elementDefinition(x.elementName, x.elementClass, eattributes(np.sqrt(x['other_Sigma_11']),np.sqrt(x['other_Sigma_33']), x['xma'], x['yma'], - x['other_charge_ppb']/madx_reference_bunch_charge, x['label'])), + x['other_particle_charge']*x['other_num_particles']/madx_reference_bunch_num_particles, x['label'])), # patch due to the fact that mad-x takes n_part from the weak beam axis=1) bb_df['elementInstallation']=bb_df.apply(lambda x: elementInstallation(x.elementName, x.elementClass, x.atPosition, x.ip_name), axis=1) else: @@ -290,8 +292,10 @@ def get_counter_rotating(bb_df): c_bb_df['identifier'] = bb_df['identifier'] c_bb_df['elementClass'] = bb_df['elementClass'] c_bb_df['elementName'] = bb_df['elementName'] - c_bb_df['self_charge_ppb'] = bb_df['self_charge_ppb'] - c_bb_df['other_charge_ppb'] = bb_df['other_charge_ppb'] + c_bb_df['self_num_particles'] = bb_df['self_num_particles'] + c_bb_df['other_num_particles'] = bb_df['other_num_particles'] + c_bb_df['self_particle_charge'] = bb_df['self_particle_charge'] + c_bb_df['other_particle_charge'] = bb_df['other_particle_charge'] c_bb_df['other_elementName'] = bb_df['other_elementName'] c_bb_df['atPosition'] = bb_df['atPosition'] * (-1.) @@ -349,9 +353,9 @@ def install_lenses_in_sequence(mad, bb_df, sequence_name, regenerate_mad_bb_info_in_df=True): if regenerate_mad_bb_info_in_df: - madx_reference_bunch_charge = mad.sequence[sequence_name].beam.npart + madx_reference_bunch_num_particles = mad.sequence[sequence_name].beam.npart generate_mad_bb_info(bb_df, mode='from_dataframe', - madx_reference_bunch_charge=madx_reference_bunch_charge) + madx_reference_bunch_num_particles=madx_reference_bunch_num_particles) mad.input(bb_df['elementDefinition'].str.cat(sep='\n')) @@ -429,7 +433,8 @@ def get_partner_corrected_position_and_optics(bb_df_b1, bb_df_b2, ip_position_df for ss in _sigma_names: self_df.loc[ee, f'other_Sigma_{ss}'] = other_df.loc[other_ee, f'self_Sigma_{ss}'] # Get charge of other beam - self_df.loc[ee, 'other_charge_ppb'] = other_df.loc[other_ee, 'self_charge_ppb'] + self_df.loc[ee, 'other_num_particles'] = other_df.loc[other_ee, 'self_num_particles'] + self_df.loc[ee, 'other_particle_charge'] = other_df.loc[other_ee, 'self_particle_charge'] self_df.loc[ee, 'other_relativistic_beta'] = other_df.loc[other_ee, 'self_relativistic_beta'] def compute_separations(bb_df): @@ -626,7 +631,8 @@ def setup_beam_beam_in_line( for ee, eename in zip(line.elements, line.element_names): if isinstance(ee, pysixtrack.elements.BeamBeam4D): - ee.charge = bb_df.loc[eename, 'other_charge_ppb'] + ee.charge = (bb_df.loc[eename, 'other_num_particles'] + * bb_df.loc[eename, 'other_particle_charge']) # TODO update pysixtrack interface to separate charge and b. population ee.sigma_x = np.sqrt(bb_df.loc[eename, 'other_Sigma_11']) ee.sigma_y = np.sqrt(bb_df.loc[eename, 'other_Sigma_33']) ee.beta_r = bb_df.loc[eename, 'other_relativistic_beta'] @@ -639,8 +645,8 @@ def setup_beam_beam_in_line( ee.alpha = bb_df.loc[eename, 'alpha'] ee.x_bb_co = bb_df.loc[eename, 'separation_x'] ee.y_bb_co = bb_df.loc[eename, 'separation_y'] - - ee.charge_slices = [bb_df.loc[eename, 'other_charge_ppb']] + ee.charge_slices = [(bb_df.loc[eename, 'other_num_particles'] + * bb_df.loc[eename, 'other_particle_charge'])] # TODO update pysixtrack interface to separate charge and b. population ee.zeta_slices = [0.0] ee.sigma_11 = bb_df.loc[eename, 'other_Sigma_11'] ee.sigma_12 = bb_df.loc[eename, 'other_Sigma_12'] @@ -735,7 +741,8 @@ def generate_bb_dataframes(mad, harmonic_number=35640, bunch_spacing_buckets=10, numberOfHOSlices=11, - bunch_population_ppb=None, + bunch_num_particles=None, + bunch_particle_charge=None, sigmaz_m=None, z_crab_twiss=0., remove_dummy_lenses=True): @@ -744,13 +751,17 @@ def generate_bb_dataframes(mad, assert mad.sequence.lhcb1.beam[pp] == mad.sequence.lhcb2.beam[pp] circumference = mad.sequence.lhcb1.beam.circ - madx_reference_bunch_charge = mad.sequence.lhcb1.beam.npart + madx_reference_bunch_num_particles = mad.sequence.lhcb1.beam.npart + + if bunch_num_particles is None: + bunch_num_particles = madx_reference_bunch_num_particles + if bunch_particle_charge is None: + bunch_particle_charge = mad.sequence.lhcb1.beam.charge + + + relativistic_gamma = mad.sequence.lhcb1.beam.gamma relativistic_beta = np.sqrt(1 - 1.0 / relativistic_gamma ** 2) - if bunch_population_ppb is not None: - bunch_charge_ppb = bunch_population_ppb - else: - bunch_charge_ppb = madx_reference_bunch_charge if sigmaz_m is not None: sigt = sigmaz_m @@ -760,15 +771,18 @@ def generate_bb_dataframes(mad, bb_df_b1 = generate_set_of_bb_encounters_1beam( circumference, harmonic_number, bunch_spacing_buckets, - numberOfHOSlices, bunch_charge_ppb, sigt, - relativistic_beta, ip_names, numberOfLRPerIRSide, + numberOfHOSlices, + bunch_num_particles, bunch_particle_charge, + sigt, relativistic_beta, ip_names, numberOfLRPerIRSide, beam_name = 'b1', other_beam_name = 'b2') bb_df_b2 = generate_set_of_bb_encounters_1beam( circumference, harmonic_number, bunch_spacing_buckets, - numberOfHOSlices, bunch_charge_ppb, sigt, + numberOfHOSlices, + bunch_num_particles, bunch_particle_charge, + sigt, relativistic_beta, ip_names, numberOfLRPerIRSide, beam_name = 'b2', other_beam_name = 'b1') @@ -808,13 +822,13 @@ def generate_bb_dataframes(mad, # Generate mad info generate_mad_bb_info(bb_df_b1, mode='from_dataframe', - madx_reference_bunch_charge=madx_reference_bunch_charge) + madx_reference_bunch_num_particles=madx_reference_bunch_num_particles) generate_mad_bb_info(bb_df_b2, mode='from_dataframe', - madx_reference_bunch_charge=madx_reference_bunch_charge) + madx_reference_bunch_num_particles=madx_reference_bunch_num_particles) generate_mad_bb_info(bb_df_b3, mode='from_dataframe', - madx_reference_bunch_charge=madx_reference_bunch_charge) + madx_reference_bunch_num_particles=madx_reference_bunch_num_particles) generate_mad_bb_info(bb_df_b4, mode='from_dataframe', - madx_reference_bunch_charge=madx_reference_bunch_charge) + madx_reference_bunch_num_particles=madx_reference_bunch_num_particles) bb_dfs = { 'b1': bb_df_b1, @@ -873,4 +887,4 @@ def compute_xma_yma(bb_df): ) bb_df['xma'] = xma - bb_df['yma'] = yma \ No newline at end of file + bb_df['yma'] = yma diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 0f37d14..0a68112 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -89,19 +89,19 @@ mad.globals.nrj = configuration['beam_energy_tot'] particle_type = 'proton' -if 'beam_mass' in configuration.keys(): - beam_mass = configuration['beam_mass'] +if 'particle_mass' in configuration.keys(): + particle_mass = configuration['particle_mass'] particle_type = 'ion' else: - beam_mass = mad.globals.pmass # proton mass + particle_mass = mad.globals.pmass # proton mass -if 'beam_charge' in configuration.keys(): - beam_charge = configuration['beam_charge'] +if 'particle_charge' in configuration.keys(): + particle_charge = configuration['particle_charge'] particle_type = 'ion' else: - beam_charge = 1. + particle_charge = 1. -gamma_rel = (beam_charge*configuration['beam_energy_tot'])/beam_mass +gamma_rel = (particle_charge*configuration['beam_energy_tot'])/particle_mass for ss in mad.sequence.keys(): # bv and bv_aux flags if ss == 'lhcb1': @@ -115,15 +115,15 @@ mad.globals['bv_aux'] = ss_bv_aux mad.input(f''' beam, particle={particle_type},sequence={ss}, - energy={configuration['beam_energy_tot']*beam_charge}, + energy={configuration['beam_energy_tot']*particle_charge}, sigt={configuration['beam_sigt']}, bv={ss_beam_bv}, npart={configuration['beam_npart']}, sige={configuration['beam_sige']}, ex={configuration['beam_norm_emit_x'] * 1e-6 / gamma_rel}, ey={configuration['beam_norm_emit_y'] * 1e-6 / gamma_rel}, - mass={beam_mass}, - charge={beam_charge}, + mass={particle_mass}, + charge={particle_charge}, ''') @@ -247,15 +247,17 @@ # Prepare bb dataframes if enable_bb_python: + bbconfig = configuration['beambeam_config'] bb_dfs = pm.generate_bb_dataframes(mad, ip_names=['ip1', 'ip2', 'ip5', 'ip8'], harmonic_number=35640, - numberOfLRPerIRSide=configuration['numberOfLRPerIRSide'], - bunch_spacing_buckets=configuration['bunch_spacing_buckets'], - numberOfHOSlices=configuration['numberOfHOSlices'], - bunch_population_ppb=configuration['bunch_population_ppb'], - sigmaz_m=configuration['sigmaz_m'], - z_crab_twiss=configuration['z_crab_twiss']*float(enable_crabs), + numberOfLRPerIRSide=bbconfig['numberOfLRPerIRSide'], + bunch_spacing_buckets=bbconfig['bunch_spacing_buckets'], + numberOfHOSlices=bbconfig['numberOfHOSlices'], + bunch_num_particles = bbconfig['bunch_num_particles'], + bunch_particle_charge = bbconfig['bunch_particle_charge'], + sigmaz_m=bbconfig['sigmaz_m'], + z_crab_twiss=bbconfig['z_crab_twiss']*float(enable_crabs), remove_dummy_lenses=True) # Here the datafremes can be edited, e.g. to set bbb intensity diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index daac18e..fe7a60f 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -47,8 +47,8 @@ 'beam_energy_tot' : 7000. , # [GeV] # Ion parameters - 'beam_mass' :193.68715, - 'beam_charge' :82, + 'particle_mass' :193.68715, + 'particle_charge' :82, # Tunes and chromaticities 'qx0' : 62.31, @@ -79,15 +79,20 @@ 'nco_IP8' : 398, # Beam-beam parameters (used by python tools - NOT by legacy macros) - 'numberOfLRPerIRSide' : [25, 20, 25, 20], - 'bunch_spacing_buckets' : 10, - 'numberOfHOSlices' : 11, - 'bunch_population_ppb' : None, - 'sigmaz_m' : None, - 'z_crab_twiss' : 0., - - 'bunch_to_track' : 488, - 'filling_scheme_json' : 'filling.json', + 'beambeam_config' : + { + 'numberOfLRPerIRSide' : [25, 20, 25, 20], + 'bunch_spacing_buckets' : 10, + 'numberOfHOSlices' : 11, + 'bunch_num_particles' : None, + 'bunch_particle_charge' : None, + 'sigmaz_m' : None, + 'z_crab_twiss' : 0., + + # Select from filling scheme + 'filling_scheme_json' : 'filling.json', + 'bunch_to_track' : 488, + }, # Match tunes and chromaticities including beam-beam effects 'match_q_dq_with_bb' : False, # should be off at collision From 02ae31cb07feb16477337aabfdcca1a79bc9783f Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Thu, 24 Jun 2021 13:11:20 +0200 Subject: [PATCH 03/31] Still in progress, to be sorted out --- pymask/pymasktools.py | 18 +++++++++++++----- python_examples/ions_python/000_pymask.py | 5 ++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pymask/pymasktools.py b/pymask/pymasktools.py index 2111fa6..07db3e1 100644 --- a/pymask/pymasktools.py +++ b/pymask/pymasktools.py @@ -182,7 +182,8 @@ def check_separations_against_madvars(checks, twiss_df_b1, twiss_df_b2, variable cc['plane'], target, cc['tol']) def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, - reference_bunch_charge_sixtrack_ppb, + reference_num_particles_sixtrack, + reference_particle_charge_sixtrack, emitnx_sixtrack_um, emitny_sixtrack_um, sigz_sixtrack_m, @@ -233,8 +234,11 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, if len(sxt_df_4d) > 0: sxt_df_4d['h-sep [mm]'] = -sxt_df_4d['separation_x']*1e3 sxt_df_4d['v-sep [mm]'] = -sxt_df_4d['separation_y']*1e3 - sxt_df_4d['strength-ratio'] = (sxt_df_4d['other_charge_ppb'] - / reference_bunch_charge_sixtrack_ppb) + sxt_df_4d['strength-ratio'] = ( + sxt_df_4d['other_num_particles'] + * sxt_df_4d['other_particle_charge'] + / reference_num_particles_sixtrack + / reference_particle_charge_sixtrack) sxt_df_4d['4dSxx [mm*mm]'] = sxt_df_4d['other_Sigma_11']*1e6 sxt_df_4d['4dSyy [mm*mm]'] = sxt_df_4d['other_Sigma_33']*1e6 sxt_df_4d['4dSxy [mm*mm]'] = sxt_df_4d['other_Sigma_13']*1e6 @@ -256,7 +260,11 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, sxt_df_6d['v-sep [mm]'] = -sxt_df_6d['separation_y']*1e3 sxt_df_6d['phi [rad]'] = sxt_df_6d['phi'] sxt_df_6d['alpha [rad]'] = sxt_df_6d['alpha'] - sxt_df_6d['strength-ratio'] = sxt_df_6d['other_charge_ppb']/reference_bunch_charge_sixtrack_ppb + sxt_df_6d['strength-ratio'] = ( + sxt_df_6d['other_num_particles'] + * sxt_df_6d['other_particle_charge'] + / reference_num_particles_sixtrack + / reference_particle_charge_sixtrack) sxt_df_6d['Sxx [mm*mm]'] = sxt_df_6d['other_Sigma_11'] *1e6 sxt_df_6d['Sxxp [mm*mrad]'] = sxt_df_6d['other_Sigma_12'] *1e6 sxt_df_6d['Sxpxp [mrad*mrad]'] = sxt_df_6d['other_Sigma_22'] *1e6 @@ -290,7 +298,7 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, ]), axis=1) f3_common_settings = ' '.join([ - f"{reference_bunch_charge_sixtrack_ppb}", + f"{reference_num_particles_sixtrack}", f"{emitnx_sixtrack_um}", f"{emitny_sixtrack_um}", f"{sigz_sixtrack_m}", diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 0a68112..55c4cb6 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -494,8 +494,11 @@ seq_name=sequence_to_track, bb_df=bb_df_track, output_folder='./', - reference_bunch_charge_sixtrack_ppb=( + reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), + reference_particle_charge_sixtrack=1., # TODO + # This is to patch a known bug of sixtrack + # https://github.com/SixTrack/SixTrack/issues/1082 emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( From e911fde62dcfbb2d71ff1f1ba1457cac8b3f613d Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Thu, 24 Jun 2021 13:15:12 +0200 Subject: [PATCH 04/31] Moved filling scheme part --- python_examples/ions_python/000_pymask.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 55c4cb6..5b2e1f2 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -262,6 +262,14 @@ # Here the datafremes can be edited, e.g. to set bbb intensity + # For B1, to be generalized for B4 + if 'filling_scheme_json' in configuration.keys(): + filling_scheme_json = configuration['filling_scheme_json'] + bunch_to_track = configuration['bunch_to_track'] + bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( + filling_scheme_json,bunch_to_track, beam='1') + bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) + ################### # Generate beam 4 # @@ -287,13 +295,6 @@ save_twiss_files=save_intermediate_twiss, check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) -# For B1, to be generalized for B4 -if 'filling_scheme_json' in configuration.keys(): - filling_scheme_json = configuration['filling_scheme_json'] - bunch_to_track = configuration['bunch_to_track'] - bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( - filling_scheme_json,bunch_to_track, beam='1') - bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) ################################################## # Select mad instance for tracking configuration # From 97594e5e9fd83c576f7758060e5d35e6377d5495 Mon Sep 17 00:00:00 2001 From: Sofia Kostoglou Date: Thu, 24 Jun 2021 18:17:15 +0200 Subject: [PATCH 05/31] fixed bug, not yet working --- pymask/beambeam.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pymask/beambeam.py b/pymask/beambeam.py index 1641c34..7a49749 100644 --- a/pymask/beambeam.py +++ b/pymask/beambeam.py @@ -232,6 +232,7 @@ def generate_set_of_bb_encounters_1beam( myBBHO['self_num_particles'] = bunch_num_particles/numberOfHOSlices + myBBHO['self_particle_charge'] = bunch_particle_charge myBBHO['self_relativistic_beta'] = relativistic_beta for ip_nn in ip_names: myBBHO.loc[myBBHO['ip_name']==ip_nn, 'atPosition']=list(z_centroids) From fd0602741ea73a57a3aa92a5b3d26712a1d4eac1 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Thu, 24 Jun 2021 20:12:14 +0200 Subject: [PATCH 06/31] update mad check --- .../hl_lhc_collisions_python/000_pymask.py | 21 +++++++++++++++++++ .../hl_lhc_collisions_python/config.py | 14 ++++++------- .../optics_specific_tools.py | 10 ++++----- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/000_pymask.py b/python_examples/hl_lhc_collisions_python/000_pymask.py index 0f37d14..1b57ce1 100644 --- a/python_examples/hl_lhc_collisions_python/000_pymask.py +++ b/python_examples/hl_lhc_collisions_python/000_pymask.py @@ -534,6 +534,21 @@ pickle_lines_in_folder=pysix_fol_name, skip_mad_use=True) +##################################### +# Save sequence and errors # +##################################### + +mad_track.select(flag="error",clear=True) +mad_track.select(flag="error",class_="multipole") +mad_track.select(flag="error",class_="hkicker") +mad_track.select(flag="error",class_="vkicker") +mad_track.select(flag="error",class_="kicker") +mad_track.esave(file="errors_lhc.tfs") +mad_track.select(flag="error",clear=True) +mad_track.select(flag="error",full=True) +mad_track.esave(file="errors_all_lhc.tfs") +mad_track.save(sequence=ss,beam=True,file=f"saved_seq.madx") + ############################# # Save final twiss # ############################# @@ -544,3 +559,9 @@ sdf = mad_track.get_summ_df('summ') tdf.to_parquet('final_twiss_BBOFF.parquet') sdf.to_parquet('final_summ_BBOFF.parquet') + + + + + + diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 3a85ea3..87560da 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -2,11 +2,11 @@ # Links to be made for tools and scripts 'links' : { - 'tracking_tools': '/afs/cern.ch/eng/tracking-tools', - 'modules': 'tracking_tools/modules', - 'tools': 'tracking_tools/tools', - 'beambeam_macros': 'tracking_tools/beambeam_macros', - 'errors': 'tracking_tools/errors', + 'base': '/home/rdemaria/local/', + 'modules': 'base/lhcmask', + 'tools': 'base/lhctoolkit', + #'beambeam_macros': 'base/beambeam_macros', + 'errors': 'base/lhcerrors', }, # Mode - choose between: @@ -20,13 +20,13 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_with_bb_legacy_macros', + 'mode' : 'b1_without_bb', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': # 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, # Optics file - 'optics_file' : '/afs/cern.ch/eng/lhc/optics/HLLHCV1.4/round/opt_round_150_1500_thin.madx', #15 cm + 'optics_file' : 'base/hllhc14/round/opt_round_150_1500_thin.madx', #15 cm # Enable checks 'check_betas_at_ips' : True, diff --git a/python_examples/hl_lhc_collisions_python/optics_specific_tools.py b/python_examples/hl_lhc_collisions_python/optics_specific_tools.py index f4f3cea..18d9100 100644 --- a/python_examples/hl_lhc_collisions_python/optics_specific_tools.py +++ b/python_examples/hl_lhc_collisions_python/optics_specific_tools.py @@ -11,7 +11,7 @@ def build_sequence(mad, beam): mad.input(f'mylhcbeam = {beam}') # Make link to optics toolkit - pm.make_links({'optics_toolkit': '/afs/cern.ch/eng/lhc/optics/HLLHCV1.4/toolkit'}, + pm.make_links({'optics_toolkit': 'base/hllhc14/toolkit'}, force=True) mad.input(''' @@ -21,20 +21,20 @@ def build_sequence(mad, beam): ver_hllhc_optics = 1.4; ! Get the toolkit - call, file="/afs/cern.ch/eng/lhc/optics/HLLHCV1.4/toolkit/macro.madx"; + call, file="base/hllhc14/toolkit/macro.madx"; ! Build sequence option, -echo,-warn,-info; if (mylhcbeam==4){ - call,file="/afs/cern.ch/eng/lhc/optics/runIII/lhcb4.seq"; + call,file="base/lhcrunIII/lhcb4.seq"; } else { - call,file="/afs/cern.ch/eng/lhc/optics/runIII/lhc.seq"; + call,file="base/lhcrunIII/lhc.seq"; }; option, -echo, warn,-info; !Install HL-LHC - call, file="/afs/cern.ch/eng/lhc/optics/HLLHCV1.4/hllhc_sequence.madx"; + call, file="base/hllhc14/hllhc_sequence.madx"; ! Slice nominal sequence From 330b3856bf706d56fb1ccf4983d4c845eeb35f72 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 25 Jun 2021 09:11:04 +0200 Subject: [PATCH 07/31] adding reload and check --- .../hl_lhc_collisions_python/001_reload.madx | 8 ++ .../hl_lhc_collisions_python/001_reload.py | 103 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 python_examples/hl_lhc_collisions_python/001_reload.madx create mode 100644 python_examples/hl_lhc_collisions_python/001_reload.py diff --git a/python_examples/hl_lhc_collisions_python/001_reload.madx b/python_examples/hl_lhc_collisions_python/001_reload.madx new file mode 100644 index 0000000..d8ca1b6 --- /dev/null +++ b/python_examples/hl_lhc_collisions_python/001_reload.madx @@ -0,0 +1,8 @@ +set,format=".15g"; +call,file="saved_lhcb1.madx"; +use,sequence="lhcb1"; +twiss; +readtable,file="error_all.tfs", table="errtab"; +seterr,table="errtab"; +twiss; +sixtrack,cavall,radius=0.017; diff --git a/python_examples/hl_lhc_collisions_python/001_reload.py b/python_examples/hl_lhc_collisions_python/001_reload.py new file mode 100644 index 0000000..010cc5b --- /dev/null +++ b/python_examples/hl_lhc_collisions_python/001_reload.py @@ -0,0 +1,103 @@ +import os, shutil + +import numpy as np + +from cpymad.madx import Madx + +def norm(x): + return np.sqrt(np.sum(np.array(x) ** 2)) + +def trim_last_zeros(a): + zz=np.where(a!=0)[0] + if len(zz)>0: + return a[:zz[-1]+1] + else: + return a + + + +def fc_to_fort(): + for iff in [2,8,16,34]: + os.system(f"rm fort.{iff}") + try: + shutil.copy(f"fc.{iff}", f"fort.{iff}") + except Exception as ex: + print(ex) + print(f"fc.{iff} not found!") + + with open("fort.3", "w") as fout: + with open("checks_and_doc/fort_parts/fort_beginning.3", "r") as fid_fort3b: + fout.write(fid_fort3b.read()) + with open("fc.3", "r") as fid_fc3: + fout.write(fid_fc3.read()) + with open("checks_and_doc/fort_parts/fort_end.3", "r") as fid_fort3e: + fout.write(fid_fort3e.read()) + + + +mad=Madx() + +mad.call("saved_seq.madx") +mad.use(sequence="lhcb1") +mad.twiss() +mad.readtable(file="errors_lhc.tfs", table="errtab") +mad.seterr(table="errtab") +mad.set(format=".15g") +mad.twiss() +mad.sixtrack(cavall=True,radius=0.017,max_mult_ord=18,mult_auto_off=True) +fc_to_fort() + +import sixtracktools +import pysixtrack + +sixin=sixtracktools.sixinput.SixInput(".") +linesix=pysixtrack.Line.from_sixinput(sixin) +linemad=pysixtrack.Line.from_madx_sequence(mad.sequence.lhcb1,apply_madx_errors=True) + +#linemad=pysixtrack.Line.from_madx_sequence(mad.sequence.lhcb1) + +for ll in (linesix, linemad): + ll.remove_inactive_multipoles(inplace=True) + ll.remove_zero_length_drifts(inplace=True) + ll.merge_consecutive_drifts(inplace=True) + ll.merge_consecutive_multipoles(inplace=True) + +for lb,li in [("fmad",linemad),("fsix",linesix)]: + with open(lb,"w") as fh: + for el,en in zip(li.elements,li.element_names): + fh.write(str(en)+"\n") + fh.write(str(el)+"\n") + +len(linesix.elements) +len(linemad.elements) + + +for ii, (ee_test, ee_six, nn_test, nn_six) in enumerate( + zip(linemad.elements, linesix.elements, linemad.element_names, linesix.element_names) +): + assert type(ee_test) == type(ee_six) + dtest = ee_test.to_dict(keepextra=True) + dsix = ee_six.to_dict(keepextra=True) + for kk in dtest.keys(): + if dtest[kk] == dsix[kk]: + continue + try: + if hasattr(dtest[kk],"__iter__"): + atest=trim_last_zeros(np.array(dtest[kk])) + asix=trim_last_zeros(np.array(dsix[kk])) + else: + atest=(np.array(dtest[kk])) + asix=(np.array(dsix[kk])) + diff_rel = norm(atest - asix) / norm(dtest[kk]) + except ZeroDivisionError: + diff_rel = 100.0 + if diff_rel < 1e-9: + continue + diff_abs = norm(atest - asix) + if diff_abs > 0 and kk!="length": + print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dsix[kk]}") + if diff_abs < 1e-12: + continue + + + From b6b3db8f600d0f8a210fa8ce4f27ede2623838aa Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Fri, 25 Jun 2021 13:08:05 +0200 Subject: [PATCH 08/31] Fix mass --- python_examples/ions_python/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index fe7a60f..22aec7d 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -47,7 +47,7 @@ 'beam_energy_tot' : 7000. , # [GeV] # Ion parameters - 'particle_mass' :193.68715, + 'particle_mass' :193.6872729, 'particle_charge' :82, # Tunes and chromaticities From b2944a7d11e4356263662aab3717f31e2edc3338 Mon Sep 17 00:00:00 2001 From: Sofia Kostoglou Date: Fri, 25 Jun 2021 14:49:06 +0200 Subject: [PATCH 09/31] small bugs --- python_examples/ions_python/000_pymask.py | 10 ++++++---- python_examples/ions_python/config.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 55c4cb6..af6c879 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -288,11 +288,13 @@ check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) # For B1, to be generalized for B4 -if 'filling_scheme_json' in configuration.keys(): - filling_scheme_json = configuration['filling_scheme_json'] - bunch_to_track = configuration['bunch_to_track'] +if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] + bunch_to_track = configuration['beambeam_config']['bunch_to_track'] + #bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( + # filling_scheme_json,bunch_to_track, beam='1') bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( - filling_scheme_json,bunch_to_track, beam='1') + filling_scheme_json,bunch_to_track, beam=1) bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) ################################################## diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index fe7a60f..22aec7d 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -47,7 +47,7 @@ 'beam_energy_tot' : 7000. , # [GeV] # Ion parameters - 'particle_mass' :193.68715, + 'particle_mass' :193.6872729, 'particle_charge' :82, # Tunes and chromaticities From 3e2e406c9f58025ca2026f352ed05f5b4770b530 Mon Sep 17 00:00:00 2001 From: Sofia Kostoglou Date: Mon, 28 Jun 2021 12:13:04 +0200 Subject: [PATCH 10/31] working for ions --- pymask/pymasktools.py | 12 ++++++------ python_examples/ions_python/000_pymask.py | 4 +--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pymask/pymasktools.py b/pymask/pymasktools.py index 07db3e1..990adb1 100644 --- a/pymask/pymasktools.py +++ b/pymask/pymasktools.py @@ -194,7 +194,7 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, ibbc_sixtrack, radius_sixtrack_multip_conversion_mad, skip_mad_use=False): - + six_fol_name = output_folder os.makedirs(six_fol_name, exist_ok=True) @@ -237,8 +237,8 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, sxt_df_4d['strength-ratio'] = ( sxt_df_4d['other_num_particles'] * sxt_df_4d['other_particle_charge'] - / reference_num_particles_sixtrack - / reference_particle_charge_sixtrack) + / reference_num_particles_sixtrack) + #/ reference_particle_charge_sixtrack)# patch for sixtrack inconsistency sxt_df_4d['4dSxx [mm*mm]'] = sxt_df_4d['other_Sigma_11']*1e6 sxt_df_4d['4dSyy [mm*mm]'] = sxt_df_4d['other_Sigma_33']*1e6 sxt_df_4d['4dSxy [mm*mm]'] = sxt_df_4d['other_Sigma_13']*1e6 @@ -263,8 +263,8 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, sxt_df_6d['strength-ratio'] = ( sxt_df_6d['other_num_particles'] * sxt_df_6d['other_particle_charge'] - / reference_num_particles_sixtrack - / reference_particle_charge_sixtrack) + / reference_num_particles_sixtrack) + #/ reference_particle_charge_sixtrack) # patch for sixtrack sxt_df_6d['Sxx [mm*mm]'] = sxt_df_6d['other_Sigma_11'] *1e6 sxt_df_6d['Sxxp [mm*mrad]'] = sxt_df_6d['other_Sigma_12'] *1e6 sxt_df_6d['Sxpxp [mrad*mrad]'] = sxt_df_6d['other_Sigma_22'] *1e6 @@ -298,7 +298,7 @@ def generate_sixtrack_input(mad, seq_name, bb_df, output_folder, ]), axis=1) f3_common_settings = ' '.join([ - f"{reference_num_particles_sixtrack}", + f"{reference_num_particles_sixtrack*reference_particle_charge_sixtrack}", f"{emitnx_sixtrack_um}", f"{emitny_sixtrack_um}", f"{sigz_sixtrack_m}", diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index af6c879..5144256 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -498,9 +498,7 @@ output_folder='./', reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), - reference_particle_charge_sixtrack=1., # TODO - # This is to patch a known bug of sixtrack - # https://github.com/SixTrack/SixTrack/issues/1082 + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( From 8f202567262eeeb478183e02a5e10f6aaccfcae4 Mon Sep 17 00:00:00 2001 From: "Frederik F. Van der Veken" Date: Wed, 30 Jun 2021 15:59:54 +0200 Subject: [PATCH 11/31] Hotfix: MBH not present in Run 3, so it should not be aligned. --- submodule_04b_alignsep.madx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/submodule_04b_alignsep.madx b/submodule_04b_alignsep.madx index 7a30859..8a23a39 100644 --- a/submodule_04b_alignsep.madx +++ b/submodule_04b_alignsep.madx @@ -24,9 +24,9 @@ exec, align_mbx28; ! V6.503 D1 in IR28 exec, align_mbrc28;! V6.503 D2 in IR28 exec, align_mbrs; ! V6.503 D3 in IR4 exec, align_mbrb; ! V6.503 D4 in IR4 -if (ver_lhc_run < 3 && ver_hllhc_optics < 1.3) { +if (ver_hllhc_optics > 0 && ver_hllhc_optics < 1.3) { call, file="tools/align_mbh_v1.madx"; ! align 11T dipoles }; -if (ver_lhc_run >= 3 || ver_hllhc_optics >= 1.3) { +if (ver_hllhc_optics >= 1.3) { call, file="tools/align_mbh_v2.madx"; ! align 11T dipoles }; From 8677f1e0aea7570c15961e4df90ef462bd611120 Mon Sep 17 00:00:00 2001 From: skostogl Date: Wed, 30 Jun 2021 18:13:06 +0200 Subject: [PATCH 12/31] update --- python_examples/ions_python/000_pymask.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 370aee5..1d8ea8d 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -262,15 +262,6 @@ # Here the datafremes can be edited, e.g. to set bbb intensity - # For B1, to be generalized for B4 - if 'filling_scheme_json' in configuration.keys(): - filling_scheme_json = configuration['filling_scheme_json'] - bunch_to_track = configuration['bunch_to_track'] - bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( - filling_scheme_json,bunch_to_track, beam='1') - bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) - - ################### # Generate beam 4 # ################### From 0701da37d0bf15039c7a1c50ee620155258c9f0d Mon Sep 17 00:00:00 2001 From: sterbini Date: Fri, 2 Jul 2021 12:16:39 +0200 Subject: [PATCH 13/31] working on the merge --- pymask/pymasktools.py | 65 +++++++------- .../hl_lhc_collisions_python/000_pymask.py | 89 +++++++++---------- .../checks_and_doc/helpers.py | 38 ++++---- ...xtrack_lines.py => t004_compare_xlines.py} | 26 +++--- .../checks_and_doc/t005_check_crabs.py | 24 ++--- .../t006_calc_and_plot_footprint.py | 16 ++-- .../checks_and_doc/tests.md | 6 +- .../hl_lhc_collisions_python/config.py | 32 ++++--- .../customization.bash.example | 5 ++ .../optics_specific_tools.py | 10 +-- python_examples/ions_python/000_pymask.py | 31 +++++-- python_examples/ions_python/config.py | 4 +- .../ions_python/customization.bash.example | 5 ++ .../run3_collisions_python/000_pymask.py | 77 ++++++++++------ 14 files changed, 241 insertions(+), 187 deletions(-) rename python_examples/hl_lhc_collisions_python/checks_and_doc/{t004_compare_pysixtrack_lines.py => t004_compare_xlines.py} (87%) create mode 100644 python_examples/hl_lhc_collisions_python/customization.bash.example create mode 100644 python_examples/ions_python/customization.bash.example diff --git a/pymask/pymasktools.py b/pymask/pymasktools.py index 990adb1..0e69764 100644 --- a/pymask/pymasktools.py +++ b/pymask/pymasktools.py @@ -378,24 +378,24 @@ def get_optics_and_orbit_at_start_ring(mad, seq_name, with_bb_forces=False, } return optics_at_start_ring -def generate_pysixtrack_line_with_bb(mad, seq_name, bb_df, +def generate_xline_with_bb(mad, seq_name, bb_df, closed_orbit_method='from_mad', pickle_lines_in_folder=None, skip_mad_use=False): opt_and_CO = get_optics_and_orbit_at_start_ring(mad, seq_name, with_bb_forces=False, skip_mad_use=True) - # Build pysixtrack model - import pysixtrack - pysxt_line = pysixtrack.Line.from_madx_sequence( + # Build xline model + import xline + line = xline.Line.from_madx_sequence( mad.sequence[seq_name]) if bb_df is not None: - bb.setup_beam_beam_in_line(pysxt_line, bb_df, bb_coupling=False) + bb.setup_beam_beam_in_line(line, bb_df, bb_coupling=False) # Temporary fix due to bug in mad loader - cavities, cav_names = pysxt_line.get_elements_of_type( - pysixtrack.elements.Cavity) + cavities, cav_names = line.get_elements_of_type( + xline.elements.Cavity) for cc, nn in zip(cavities, cav_names): if cc.frequency ==0.: ii_mad = mad.sequence[seq_name].element_names().index(nn) @@ -405,37 +405,42 @@ def generate_pysixtrack_line_with_bb(mad, seq_name, bb_df, mad_CO = np.array([opt_and_CO[kk] for kk in ['x', 'px', 'y', 'py', 'sigma', 'delta']]) - pysxt_line.disable_beambeam() - part_on_CO = pysxt_line.find_closed_orbit( + line.disable_beambeam() + part_on_CO = line.find_closed_orbit( guess=mad_CO, p0c=opt_and_CO['p0c_eV'], method={'from_mad': 'get_guess', 'from_tracking': 'Nelder-Mead'}[closed_orbit_method]) - pysxt_line.enable_beambeam() + line.enable_beambeam() - pysxt_line_bb_dipole_cancelled = pysxt_line.copy() + line_bb_dipole_cancelled = line.copy() - pysxt_line_bb_dipole_cancelled.beambeam_store_closed_orbit_and_dipolar_kicks( + line_bb_dipole_cancelled.beambeam_store_closed_orbit_and_dipolar_kicks( part_on_CO, separation_given_wrt_closed_orbit_4D=True, separation_given_wrt_closed_orbit_6D=True) - pysxt_dict = { - 'line_bb_dipole_not_cancelled': pysxt_line, - 'line_bb_dipole_cancelled': pysxt_line_bb_dipole_cancelled, + xline_dict = { + 'line_bb_dipole_not_cancelled': line, + 'line_bb_dipole_cancelled': line_bb_dipole_cancelled, 'particle_on_closed_orbit': part_on_CO} if pickle_lines_in_folder is not None: - pysix_fol_name = pickle_lines_in_folder - os.makedirs(pysix_fol_name, exist_ok=True) - - with open(pysix_fol_name + "/line_bb_dipole_not_cancelled.pkl", "wb") as fid: - pickle.dump(pysxt_line.to_dict(keepextra=True), fid) - - with open(pysix_fol_name + "/line_bb_dipole_cancelled.pkl", "wb") as fid: - pickle.dump(pysxt_line_bb_dipole_cancelled.to_dict(keepextra=True), fid) - - with open(pysix_fol_name + "/particle_on_closed_orbit.pkl", "wb") as fid: - pickle.dump(part_on_CO.to_dict(), fid) - - return pysxt_dict - - + xline_fol_name = pickle_lines_in_folder + os.makedirs(xline_fol_name, exist_ok=True) + + line.to_json(xline_fol_name + "/line_bb_dipole_not_cancelled.json", keepextra=True) + line_bb_dipole_cancelled.to_json(xline_fol_name + "/line_bb_dipole_cancelled.json", keepextra=True) + part_on_CO.to_json(xline_fol_name + "/particle_on_closed_orbit.pkl") + + return xline_dict + +def save_mad_sequence_and_error(mad, seq_name, filename='lhc'): + mad.select(flag="error",clear=True) + mad.select(flag="error",class_="multipole") + mad.select(flag="error",class_="hkicker") + mad.select(flag="error",class_="vkicker") + mad.select(flag="error",class_="kicker") + mad.esave(file=filename + "_errors.tfs") + mad.select(flag="error",clear=True) + mad.select(flag="error",full=True) + mad.esave(file=filename + "_errors_all.tfs") + mad.save(sequence=seq_name,beam=True,file=filename + "_seq.madx") diff --git a/python_examples/hl_lhc_collisions_python/000_pymask.py b/python_examples/hl_lhc_collisions_python/000_pymask.py index 1b57ce1..8a348d6 100644 --- a/python_examples/hl_lhc_collisions_python/000_pymask.py +++ b/python_examples/hl_lhc_collisions_python/000_pymask.py @@ -89,19 +89,19 @@ mad.globals.nrj = configuration['beam_energy_tot'] particle_type = 'proton' -if 'beam_mass' in configuration.keys(): - beam_mass = configuration['beam_mass'] +if 'particle_mass' in configuration.keys(): + particle_mass = configuration['particle_mass'] particle_type = 'ion' else: - beam_mass = mad.globals.pmass # proton mass + particle_mass = mad.globals.pmass # proton mass -if 'beam_charge' in configuration.keys(): - beam_charge = configuration['beam_charge'] +if 'particle_charge' in configuration.keys(): + particle_charge = configuration['particle_charge'] particle_type = 'ion' else: - beam_charge = 1. + particle_charge = 1. -gamma_rel = (beam_charge*configuration['beam_energy_tot'])/beam_mass +gamma_rel = (particle_charge*configuration['beam_energy_tot'])/particle_mass for ss in mad.sequence.keys(): # bv and bv_aux flags if ss == 'lhcb1': @@ -115,15 +115,15 @@ mad.globals['bv_aux'] = ss_bv_aux mad.input(f''' beam, particle={particle_type},sequence={ss}, - energy={configuration['beam_energy_tot']*beam_charge}, + energy={configuration['beam_energy_tot']*particle_charge}, sigt={configuration['beam_sigt']}, bv={ss_beam_bv}, npart={configuration['beam_npart']}, sige={configuration['beam_sige']}, ex={configuration['beam_norm_emit_x'] * 1e-6 / gamma_rel}, ey={configuration['beam_norm_emit_y'] * 1e-6 / gamma_rel}, - mass={beam_mass}, - charge={beam_charge}, + mass={particle_mass}, + charge={particle_charge}, ''') @@ -247,20 +247,21 @@ # Prepare bb dataframes if enable_bb_python: + bbconfig = configuration['beambeam_config'] bb_dfs = pm.generate_bb_dataframes(mad, ip_names=['ip1', 'ip2', 'ip5', 'ip8'], harmonic_number=35640, - numberOfLRPerIRSide=configuration['numberOfLRPerIRSide'], - bunch_spacing_buckets=configuration['bunch_spacing_buckets'], - numberOfHOSlices=configuration['numberOfHOSlices'], - bunch_population_ppb=configuration['bunch_population_ppb'], - sigmaz_m=configuration['sigmaz_m'], - z_crab_twiss=configuration['z_crab_twiss']*float(enable_crabs), + numberOfLRPerIRSide=bbconfig['numberOfLRPerIRSide'], + bunch_spacing_buckets=bbconfig['bunch_spacing_buckets'], + numberOfHOSlices=bbconfig['numberOfHOSlices'], + bunch_num_particles = bbconfig['bunch_num_particles'], + bunch_particle_charge = bbconfig['bunch_particle_charge'], + sigmaz_m=bbconfig['sigmaz_m'], + z_crab_twiss=bbconfig['z_crab_twiss']*float(enable_crabs), remove_dummy_lenses=True) # Here the datafremes can be edited, e.g. to set bbb intensity - ################### # Generate beam 4 # ################### @@ -286,11 +287,11 @@ check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) # For B1, to be generalized for B4 -if 'filling_scheme_json' in configuration.keys(): - filling_scheme_json = configuration['filling_scheme_json'] - bunch_to_track = configuration['bunch_to_track'] +if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] + bunch_to_track = configuration['beambeam_config']['bunch_to_track'] bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( - filling_scheme_json,bunch_to_track, beam='1') + filling_scheme_json,bunch_to_track, beam=1) bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) ################################################## @@ -337,6 +338,7 @@ # Legacy bb macros if enable_bb_legacy: + bbconfig = configuration['beambeam_config'] assert(beam_to_configure == 1) assert(not(track_from_b4_mad_instance)) assert(not(enable_bb_python)) @@ -344,7 +346,7 @@ mad_track.set_variables_from_dict( params=configuration['pars_for_legacy_bb_macros']) mad_track.set_variables_from_dict( - params={f'par_nho_ir{ir}':configuration['numberOfHOSlices'] + params={f'par_nho_ir{ir}':bbconfig['numberOfHOSlices'] for ir in [1,2,5,8]}) mad_track.input("call, file='modules/module_03_beambeam.madx';") @@ -492,8 +494,9 @@ seq_name=sequence_to_track, bb_df=bb_df_track, output_folder='./', - reference_bunch_charge_sixtrack_ppb=( + reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( @@ -525,33 +528,20 @@ ############################# if enable_bb_legacy: - print('Pysixtrack line is not generated with bb legacy macros') + print('xline is not generated with bb legacy macros') else: - pysix_fol_name = "./pysixtrack" - dct_pysxt = pm.generate_pysixtrack_line_with_bb(mad_track, + xline_fol_name = "./xline" + dct_xline = pm.generate_xline_with_bb(mad_track, sequence_to_track, bb_df_track, closed_orbit_method='from_mad', - pickle_lines_in_folder=pysix_fol_name, + pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) -##################################### -# Save sequence and errors # -##################################### - -mad_track.select(flag="error",clear=True) -mad_track.select(flag="error",class_="multipole") -mad_track.select(flag="error",class_="hkicker") -mad_track.select(flag="error",class_="vkicker") -mad_track.select(flag="error",class_="kicker") -mad_track.esave(file="errors_lhc.tfs") -mad_track.select(flag="error",clear=True) -mad_track.select(flag="error",full=True) -mad_track.esave(file="errors_all_lhc.tfs") -mad_track.save(sequence=ss,beam=True,file=f"saved_seq.madx") -############################# -# Save final twiss # -############################# + +################################### +# Save final twiss # +################################### mad_track.globals.on_bb_charge = 0 mad_track.twiss() @@ -561,7 +551,16 @@ sdf.to_parquet('final_summ_BBOFF.parquet') +mad_track.globals.on_bb_charge = 1 +mad_track.twiss() +tdf = mad_track.get_twiss_df('twiss') +sdf = mad_track.get_summ_df('summ') +tdf.to_parquet('final_twiss_BBON.parquet') +sdf.to_parquet('final_summ_BBON.parquet') +############################# +# Save sequence and errors # +############################# - +pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/helpers.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/helpers.py index 2d67edb..b88ce4a 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/helpers.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/helpers.py @@ -1,7 +1,7 @@ import numpy as np import os import sixtracktools -import pysixtrack +import xline import time @@ -79,10 +79,10 @@ def track_particle_sixtrack( lines_f13 = [] for i_part in range(n_part): - if type(partCO) is pysixtrack.Particles: + if type(partCO) is xline.Particles: temp_part = partCO.copy() else: - temp_part = pysixtrack.Particles(**partCO) + temp_part = xline.Particles(**partCO) temp_part.x += Dx_wrt_CO_m[i_part] temp_part.px += Dpx_wrt_CO_rad[i_part] temp_part.y += Dy_wrt_CO_m[i_part] @@ -159,7 +159,7 @@ def track_particle_sixtrack( return x_tbt, px_tbt, y_tbt, py_tbt, sigma_tbt, delta_tbt -def track_particle_pysixtrack(line, part, Dx_wrt_CO_m, Dpx_wrt_CO_rad, +def track_particle_xline(line, part, Dx_wrt_CO_m, Dpx_wrt_CO_rad, Dy_wrt_CO_m, Dpy_wrt_CO_rad, Dsigma_wrt_CO_m, Ddelta_wrt_CO, n_turns, verbose=False): @@ -220,10 +220,10 @@ def track_particle_sixtracklib( Dy_wrt_CO_m, Dpy_wrt_CO_rad, Dsigma_wrt_CO_m, Ddelta_wrt_CO) - #if type(partCO) is pysixtrack.Particles: + #if type(partCO) is xline.Particles: # temp_part = partCO.copy() #else: - # temp_part = pysixtrack.Particles(**partCO) + # temp_part = xline.Particles(**partCO) import sixtracklib elements=sixtracklib.Elements() @@ -239,10 +239,10 @@ def track_particle_sixtracklib( for i_part in range(n_part): - if type(partCO) is pysixtrack.Particles: + if type(partCO) is xline.Particles: part = partCO.copy() else: - part = pysixtrack.Particles(**partCO) + part = xline.Particles(**partCO) part.x += Dx_wrt_CO_m[i_part] part.px += Dpx_wrt_CO_rad[i_part] part.y += Dy_wrt_CO_m[i_part] @@ -255,7 +255,7 @@ def track_particle_sixtracklib( part.elemid = 0 part.turn = 0 - p.from_pysixtrack(part, i_part) + p.from_xline(part, i_part) if device is None: job = sixtracklib.TrackJob(elements, ps) @@ -308,10 +308,10 @@ def track_particle_sixtracklib_long( Dzeta_wrt_CO_m, Ddelta_wrt_CO) - #if type(partCO) is pysixtrack.Particles: + #if type(partCO) is xline.Particles: # part = partCO.copy() #else: - # part = pysixtrack.Particles(**partCO) + # part = xline.Particles(**partCO) n_turns_tbt=1000 n_turns_to_store=1000 skip_turns=int(n_turns)//n_turns_to_store @@ -347,10 +347,10 @@ def track_particle_sixtracklib_long( for i_part in range(n_part): - if type(partCO) is pysixtrack.Particles: + if type(partCO) is xline.Particles: part = partCO.copy() else: - part = pysixtrack.Particles(**partCO) + part = xline.Particles(**partCO) part.x += Dx_wrt_CO_m[i_part] part.px += Dpx_wrt_CO_rad[i_part] part.y += Dy_wrt_CO_m[i_part] @@ -363,7 +363,7 @@ def track_particle_sixtracklib_long( part.elemid = 0 part.turn = 0 - p.from_pysixtrack(part, i_part) + p.from_xline(part, i_part) if device is None: job = sixtracklib.TrackJob(elements, ps) @@ -453,10 +453,10 @@ def track_particle_sixtracklib_firstlast( Dsigma_wrt_CO_m, Ddelta_wrt_CO) - #if type(partCO) is pysixtrack.Particles: + #if type(partCO) is xline.Particles: # part = partCO.copy() #else: - # part = pysixtrack.Particles(**partCO) + # part = xline.Particles(**partCO) n_turns_to_store=1000 n_turns_tbt=1000 @@ -488,10 +488,10 @@ def track_particle_sixtracklib_firstlast( for i_part in range(n_part): - if type(partCO) is pysixtrack.Particles: + if type(partCO) is xline.Particles: part = partCO.copy() else: - part = pysixtrack.Particles(**partCO) + part = xline.Particles(**partCO) part.x += Dx_wrt_CO_m[i_part] part.px += Dpx_wrt_CO_rad[i_part] part.y += Dy_wrt_CO_m[i_part] @@ -504,7 +504,7 @@ def track_particle_sixtracklib_firstlast( part.elemid = 0 part.turn = 0 - p.from_pysixtrack(part, i_part) + p.from_xline(part, i_part) if device is None: job = sixtracklib.TrackJob(elements, ps) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_pysixtrack_lines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py similarity index 87% rename from python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_pysixtrack_lines.py rename to python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 61c0466..66da7b2 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_pysixtrack_lines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -2,7 +2,7 @@ import pickle import numpy as np -import pysixtrack +import xline import sixtracktools # Test b1 @@ -17,22 +17,22 @@ # path_ref = '../../../examples/hl_lhc_collision_nobb_b4' # type_ref = 'sixtrack' -# # Test b4 nobb pysixtrack (not working for now) -# path_test = './pysixtrack/line_bb_dipole_not_cancelled.pkl' -# type_test = 'pysixtrack' +# # Test b4 nobb xline (not working for now) +# path_test = './xline/line_bb_dipole_not_cancelled.pkl' +# type_test = 'xline' # path_ref = '../hl_lhc_collision_nobb_b4' # type_ref = 'sixtrack' def prepare_line(path, input_type): - if input_type == 'pysixtrack': - # Load pysixtrack machine + if input_type == 'xline': + # Load xline machine with open(path, "rb") as fid: - ltest = pysixtrack.Line.from_dict(pickle.load(fid)) + ltest = xline.Line.from_dict(pickle.load(fid)) elif input_type == 'sixtrack': - print('Build pysixtrack from sixtrack input:') + print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) - ltest = pysixtrack.Line.from_sixinput(sixinput_test) + ltest = xline.Line.from_sixinput(sixinput_test) print('Done') else: raise ValueError('What?!') @@ -112,20 +112,20 @@ def norm(x): # Exception: drift length (100 um tolerance) if isinstance( - ee_test, (pysixtrack.elements.Drift, pysixtrack.elements.DriftExact) + ee_test, (xline.elements.Drift, xline.elements.DriftExact) ): if kk == "length": if diff_abs < 1e-4: continue # Exception: multipole lrad is not passed to sixtraxk - if isinstance(ee_test, pysixtrack.elements.Multipole): + if isinstance(ee_test, xline.elements.Multipole): if kk == "length": if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: continue # Exceptions BB4D (separations are recalculated) - if isinstance(ee_test, pysixtrack.elements.BeamBeam4D): + if isinstance(ee_test, xline.elements.BeamBeam4D): if kk == "x_bb": if diff_abs / dtest["sigma_x"] < 0.01: # This is neede to accommodate different leveling routines (1% difference) continue @@ -140,7 +140,7 @@ def norm(x): continue # Exceptions BB4D (angles and separations are recalculated) - if isinstance(ee_test, pysixtrack.elements.BeamBeam6D): + if isinstance(ee_test, xline.elements.BeamBeam6D): if kk == "alpha": if diff_abs < 10e-6: continue diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py index b4eab61..29c75b8 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py @@ -2,7 +2,7 @@ import pickle import numpy as np -import pysixtrack +import xline import sixtracktools L_lhc = 27e3 @@ -48,14 +48,14 @@ def prepare_line(path, input_type): - if input_type == 'pysixtrack': - # Load pysixtrack machine + if input_type == 'xline': + # Load xline machine with open(path, "rb") as fid: - ltest = pysixtrack.Line.from_dict(pickle.load(fid)) + ltest = xline.Line.from_dict(pickle.load(fid)) elif input_type == 'sixtrack': - print('Build pysixtrack from sixtrack input:') + print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) - ltest = pysixtrack.Line.from_sixinput(sixinput_test) + ltest = xline.Line.from_sixinput(sixinput_test) print('Done') else: raise ValueError('What?!') @@ -123,12 +123,12 @@ def prepare_line(path, input_type): # Switch off all beam-beam lenses -bb_all, _ = ltest.get_elements_of_type([pysixtrack.elements.BeamBeam4D, - pysixtrack.elements.BeamBeam6D]) +bb_all, _ = ltest.get_elements_of_type([xline.elements.BeamBeam4D, + xline.elements.BeamBeam6D]) for bb in bb_all: bb.enabled = False # # Switch off all beam-beam lenses -crabs, crab_names = ltest.get_elements_of_type([pysixtrack.elements.RFMultipole]) +crabs, crab_names = ltest.get_elements_of_type([xline.elements.RFMultipole]) #for cc in crabs: # cc.pn = [-90] # cc.ps = [-90] @@ -136,7 +136,7 @@ def prepare_line(path, input_type): # for cc in crabs: # cc.knl[0] *= -1 -partco = pysixtrack.Particles.from_dict(ddd) +partco = xline.Particles.from_dict(ddd) z_slices = s_rel * 2.0 partco.zeta += z_slices partco.x += 0*z_slices @@ -184,7 +184,7 @@ def prepare_line(path, input_type): for ibb, bb in enumerate(bb_elems): r_lenses.append(getattr(list_co[bb_index[ibb]], plane)[ibb]) -axcrab.plot(s_rel, r_lenses, 'o', color='b', alpha=.5, label= 'weak pysixtrack') +axcrab.plot(s_rel, r_lenses, 'o', color='b', alpha=.5, label= 'weak xline') Rw_crab = phi_c_weak * L_lhc / (2*np.pi*h_cc) *np.sin(2*np.pi*h_cc/L_lhc*2*s_rel) Rw_no_crab = phi_weak * s_rel axcrab.plot(s_rel, Rw_crab + Rw_no_crab, '*', color='darkblue', @@ -222,7 +222,7 @@ def prepare_line(path, input_type): axcbx.plot(s_twiss, x_twiss) axcby.plot(s_twiss, y_twiss) -part = pysixtrack.Particles.from_dict(ddd) +part = xline.Particles.from_dict(ddd) z_test = np.array([0, z_crab_track]) part.zeta += z_test part.x += 0*z_test + np.array([0, x_twiss[0]]) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t006_calc_and_plot_footprint.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t006_calc_and_plot_footprint.py index 6f184b6..aa621da 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t006_calc_and_plot_footprint.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t006_calc_and_plot_footprint.py @@ -5,7 +5,7 @@ import footprint import matplotlib.pyplot as plt -import pysixtrack +import xline import sixtracktools track_with = 'PySixtrack' @@ -25,15 +25,15 @@ def prepare_line(path, input_type): - if input_type == 'pysixtrack': - # Load pysixtrack machine + if input_type == 'xline': + # Load xline machine with open(path, "rb") as fid: - ltest = pysixtrack.Line.from_dict( + ltest = xline.Line.from_dict( pickle.load(fid)) elif input_type == 'sixtrack': - print('Build pysixtrack from sixtrack input:') + print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) - ltest = pysixtrack.Line.from_sixinput(sixinput_test) + ltest = xline.Line.from_sixinput(sixinput_test) print('Done') else: raise ValueError('What?!') @@ -47,7 +47,7 @@ def prepare_line(path, input_type): ddd = pickle.load(fid) ddd['p0c'] = ddd['p0c_eV'] -partCO = pysixtrack.Particles.from_dict(ddd) +partCO = xline.Particles.from_dict(ddd) # line.disable_beambeam() part = partCO.copy() @@ -82,7 +82,7 @@ def prepare_line(path, input_type): part = partCO.copy() - x_tbt, px_tbt, y_tbt, py_tbt, sigma_tbt, delta_tbt = hp.track_particle_pysixtrack( + x_tbt, px_tbt, y_tbt, py_tbt, sigma_tbt, delta_tbt = hp.track_particle_xline( line, part=part, Dx_wrt_CO_m=0., Dpx_wrt_CO_rad=DpxDpy_wrt_CO[:, :, 0].flatten(), Dy_wrt_CO_m=0, Dpy_wrt_CO_rad=DpxDpy_wrt_CO[:, :, 1].flatten(), Dsigma_wrt_CO_m=0., Ddelta_wrt_CO=0., n_turns=n_turns, verbose=True) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/tests.md b/python_examples/hl_lhc_collisions_python/checks_and_doc/tests.md index ee593a1..ca47fa5 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/tests.md +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/tests.md @@ -57,7 +57,7 @@ We execute the reference (can be reused from the previous test): python ../../unmask.py main.mask parameters_for_unmask.txt --run ``` In the folder ```checks_and_doc```: -Configure ```t004_compare_pysixtrack_lines.py``` for the test: +Configure ```t004_compare_xline_lines.py``` for the test: ```python # Test b1 path_test = '../' @@ -66,10 +66,10 @@ path_ref = '../../examples/hl_lhc_collision' type_ref = 'sixtrack ``` -Check using pysixtrack lines: +Check using xline lines: ```bash python t003_fc_to_fort.py -python t004_compare_pysixtrack_lines.py +python t004_compare_xline_lines.py ``` **Check crab cavities:** diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 87560da..98f1bab 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -2,11 +2,12 @@ # Links to be made for tools and scripts 'links' : { - 'base': '/home/rdemaria/local/', - 'modules': 'base/lhcmask', - 'tools': 'base/lhctoolkit', - #'beambeam_macros': 'base/beambeam_macros', - 'errors': 'base/lhcerrors', + 'tracking_tools': '/afs/cern.ch/eng/tracking-tools', + 'modules': 'tracking_tools/modules', + 'tools': 'tracking_tools/tools', + 'beambeam_macros': 'tracking_tools/beambeam_macros', + 'errors': 'tracking_tools/errors', + 'optics_repository' : '/afs/cern.ch/eng/lhc/optics', }, # Mode - choose between: @@ -20,13 +21,13 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_without_bb', + 'mode' : 'b1_with_bb_legacy_macros', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': # 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, # Optics file - 'optics_file' : 'base/hllhc14/round/opt_round_150_1500_thin.madx', #15 cm + 'optics_file' : 'optics_repository/HLLHCV1.4/round/opt_round_150_1500_thin.madx', #15 cm # Enable checks 'check_betas_at_ips' : True, @@ -72,13 +73,16 @@ 'nco_IP8' : 2572, # number of Head-On collisions in IP1 # Beam-beam parameters (used by python tools - NOT by legacy macros) - 'numberOfLRPerIRSide' : [25, 20, 25, 20], - 'bunch_spacing_buckets' : 10, - 'numberOfHOSlices' : 11, - 'bunch_population_ppb' : None, - 'sigmaz_m' : None, - 'z_crab_twiss' : 0.075, - + 'beambeam_config' : + { + 'numberOfLRPerIRSide' : [25, 20, 25, 20], + 'bunch_spacing_buckets' : 10, + 'numberOfHOSlices' : 11, + 'bunch_num_particles' : None, + 'bunch_particle_charge' : None, + 'sigmaz_m' : None, + 'z_crab_twiss' : 0.075, + }, # Match tunes and chromaticities including beam-beam effects 'match_q_dq_with_bb' : False, # should be off at collision diff --git a/python_examples/hl_lhc_collisions_python/customization.bash.example b/python_examples/hl_lhc_collisions_python/customization.bash.example new file mode 100644 index 0000000..3ef2c56 --- /dev/null +++ b/python_examples/hl_lhc_collisions_python/customization.bash.example @@ -0,0 +1,5 @@ +#rm modules +#ln -fns /home/sterbini/test_new_mask/v0.5/lhcmachines machines +#ln -fns /home/sterbini/test_new_mask/v0.5/lhctoolkit tools +ln -fns /home/sterbini/lhcmask modules +#ln -fns /home/sterbini/test_new_mask/v0.5/lhcerrors errors diff --git a/python_examples/hl_lhc_collisions_python/optics_specific_tools.py b/python_examples/hl_lhc_collisions_python/optics_specific_tools.py index 18d9100..ca99362 100644 --- a/python_examples/hl_lhc_collisions_python/optics_specific_tools.py +++ b/python_examples/hl_lhc_collisions_python/optics_specific_tools.py @@ -11,7 +11,7 @@ def build_sequence(mad, beam): mad.input(f'mylhcbeam = {beam}') # Make link to optics toolkit - pm.make_links({'optics_toolkit': 'base/hllhc14/toolkit'}, + pm.make_links({'optics_toolkit': 'optics_repository/HLLHCV1.4/toolkit'}, force=True) mad.input(''' @@ -21,20 +21,20 @@ def build_sequence(mad, beam): ver_hllhc_optics = 1.4; ! Get the toolkit - call, file="base/hllhc14/toolkit/macro.madx"; + call, file="optics_repository/HLLHCV1.4/toolkit/macro.madx"; ! Build sequence option, -echo,-warn,-info; if (mylhcbeam==4){ - call,file="base/lhcrunIII/lhcb4.seq"; + call,file="optics_repository/runIII/lhcb4.seq"; } else { - call,file="base/lhcrunIII/lhc.seq"; + call,file="optics_repository/runIII/lhc.seq"; }; option, -echo, warn,-info; !Install HL-LHC - call, file="base/hllhc14/hllhc_sequence.madx"; + call, file="optics_repository/HLLHCV1.4/hllhc_sequence.madx"; ! Slice nominal sequence diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 1d8ea8d..0425057 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -527,18 +527,20 @@ ############################# if enable_bb_legacy: - print('Pysixtrack line is not generated with bb legacy macros') + print('xline is not generated with bb legacy macros') else: - pysix_fol_name = "./pysixtrack" - dct_pysxt = pm.generate_pysixtrack_line_with_bb(mad_track, + xline_fol_name = "./xline" + dct_xline = pm.generate_xline_with_bb(mad_track, sequence_to_track, bb_df_track, closed_orbit_method='from_mad', - pickle_lines_in_folder=pysix_fol_name, + pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) -############################# -# Save final twiss # -############################# + + +################################### +# Save final twiss # +################################### mad_track.globals.on_bb_charge = 0 mad_track.twiss() @@ -546,3 +548,18 @@ sdf = mad_track.get_summ_df('summ') tdf.to_parquet('final_twiss_BBOFF.parquet') sdf.to_parquet('final_summ_BBOFF.parquet') + + +mad_track.globals.on_bb_charge = 1 +mad_track.twiss() +tdf = mad_track.get_twiss_df('twiss') +sdf = mad_track.get_summ_df('summ') +tdf.to_parquet('final_twiss_BBON.parquet') +sdf.to_parquet('final_summ_BBON.parquet') + +############################# +# Save sequence and errors # +############################# + +pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') + diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index 2c83788..4aa81f8 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -84,7 +84,7 @@ 'bunch_spacing_buckets' : 10, 'numberOfHOSlices' : 11, 'bunch_num_particles' : None, - 'bunch_particle_charge' : None, + 'bunch_particle_charge' : None, 'sigmaz_m' : None, 'z_crab_twiss' : 0., @@ -98,7 +98,7 @@ # Enable crab cavities 'enable_crabs' : False, - # N. iterations coupling correction + # N. iterations for coupling correction 'N_iter_coupling' : 2, # Value to be added to linear coupling knobs (on sequence_to_track) diff --git a/python_examples/ions_python/customization.bash.example b/python_examples/ions_python/customization.bash.example new file mode 100644 index 0000000..3ef2c56 --- /dev/null +++ b/python_examples/ions_python/customization.bash.example @@ -0,0 +1,5 @@ +#rm modules +#ln -fns /home/sterbini/test_new_mask/v0.5/lhcmachines machines +#ln -fns /home/sterbini/test_new_mask/v0.5/lhctoolkit tools +ln -fns /home/sterbini/lhcmask modules +#ln -fns /home/sterbini/test_new_mask/v0.5/lhcerrors errors diff --git a/python_examples/run3_collisions_python/000_pymask.py b/python_examples/run3_collisions_python/000_pymask.py index 0f37d14..0425057 100644 --- a/python_examples/run3_collisions_python/000_pymask.py +++ b/python_examples/run3_collisions_python/000_pymask.py @@ -89,19 +89,19 @@ mad.globals.nrj = configuration['beam_energy_tot'] particle_type = 'proton' -if 'beam_mass' in configuration.keys(): - beam_mass = configuration['beam_mass'] +if 'particle_mass' in configuration.keys(): + particle_mass = configuration['particle_mass'] particle_type = 'ion' else: - beam_mass = mad.globals.pmass # proton mass + particle_mass = mad.globals.pmass # proton mass -if 'beam_charge' in configuration.keys(): - beam_charge = configuration['beam_charge'] +if 'particle_charge' in configuration.keys(): + particle_charge = configuration['particle_charge'] particle_type = 'ion' else: - beam_charge = 1. + particle_charge = 1. -gamma_rel = (beam_charge*configuration['beam_energy_tot'])/beam_mass +gamma_rel = (particle_charge*configuration['beam_energy_tot'])/particle_mass for ss in mad.sequence.keys(): # bv and bv_aux flags if ss == 'lhcb1': @@ -115,15 +115,15 @@ mad.globals['bv_aux'] = ss_bv_aux mad.input(f''' beam, particle={particle_type},sequence={ss}, - energy={configuration['beam_energy_tot']*beam_charge}, + energy={configuration['beam_energy_tot']*particle_charge}, sigt={configuration['beam_sigt']}, bv={ss_beam_bv}, npart={configuration['beam_npart']}, sige={configuration['beam_sige']}, ex={configuration['beam_norm_emit_x'] * 1e-6 / gamma_rel}, ey={configuration['beam_norm_emit_y'] * 1e-6 / gamma_rel}, - mass={beam_mass}, - charge={beam_charge}, + mass={particle_mass}, + charge={particle_charge}, ''') @@ -247,20 +247,21 @@ # Prepare bb dataframes if enable_bb_python: + bbconfig = configuration['beambeam_config'] bb_dfs = pm.generate_bb_dataframes(mad, ip_names=['ip1', 'ip2', 'ip5', 'ip8'], harmonic_number=35640, - numberOfLRPerIRSide=configuration['numberOfLRPerIRSide'], - bunch_spacing_buckets=configuration['bunch_spacing_buckets'], - numberOfHOSlices=configuration['numberOfHOSlices'], - bunch_population_ppb=configuration['bunch_population_ppb'], - sigmaz_m=configuration['sigmaz_m'], - z_crab_twiss=configuration['z_crab_twiss']*float(enable_crabs), + numberOfLRPerIRSide=bbconfig['numberOfLRPerIRSide'], + bunch_spacing_buckets=bbconfig['bunch_spacing_buckets'], + numberOfHOSlices=bbconfig['numberOfHOSlices'], + bunch_num_particles = bbconfig['bunch_num_particles'], + bunch_particle_charge = bbconfig['bunch_particle_charge'], + sigmaz_m=bbconfig['sigmaz_m'], + z_crab_twiss=bbconfig['z_crab_twiss']*float(enable_crabs), remove_dummy_lenses=True) # Here the datafremes can be edited, e.g. to set bbb intensity - ################### # Generate beam 4 # ################### @@ -286,11 +287,11 @@ check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) # For B1, to be generalized for B4 -if 'filling_scheme_json' in configuration.keys(): - filling_scheme_json = configuration['filling_scheme_json'] - bunch_to_track = configuration['bunch_to_track'] +if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] + bunch_to_track = configuration['beambeam_config']['bunch_to_track'] bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( - filling_scheme_json,bunch_to_track, beam='1') + filling_scheme_json,bunch_to_track, beam=1) bb_dfs['b1']=ost.filter_bb_df(bb_dfs['b1'],bb_schedule_to_track_b1) ################################################## @@ -492,8 +493,9 @@ seq_name=sequence_to_track, bb_df=bb_df_track, output_folder='./', - reference_bunch_charge_sixtrack_ppb=( + reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( @@ -525,18 +527,20 @@ ############################# if enable_bb_legacy: - print('Pysixtrack line is not generated with bb legacy macros') + print('xline is not generated with bb legacy macros') else: - pysix_fol_name = "./pysixtrack" - dct_pysxt = pm.generate_pysixtrack_line_with_bb(mad_track, + xline_fol_name = "./xline" + dct_xline = pm.generate_xline_with_bb(mad_track, sequence_to_track, bb_df_track, closed_orbit_method='from_mad', - pickle_lines_in_folder=pysix_fol_name, + pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) -############################# -# Save final twiss # -############################# + + +################################### +# Save final twiss # +################################### mad_track.globals.on_bb_charge = 0 mad_track.twiss() @@ -544,3 +548,18 @@ sdf = mad_track.get_summ_df('summ') tdf.to_parquet('final_twiss_BBOFF.parquet') sdf.to_parquet('final_summ_BBOFF.parquet') + + +mad_track.globals.on_bb_charge = 1 +mad_track.twiss() +tdf = mad_track.get_twiss_df('twiss') +sdf = mad_track.get_summ_df('summ') +tdf.to_parquet('final_twiss_BBON.parquet') +sdf.to_parquet('final_summ_BBON.parquet') + +############################# +# Save sequence and errors # +############################# + +pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') + From fdc677620d51a6c7baac1766e2edf835ee095bdc Mon Sep 17 00:00:00 2001 From: sterbini Date: Fri, 2 Jul 2021 17:46:42 +0200 Subject: [PATCH 14/31] update --- pymask/pymasktools.py | 4 ++-- .../hl_lhc_collisions_python/001_reload.py | 12 ++++++------ python_examples/hl_lhc_collisions_python/config.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pymask/pymasktools.py b/pymask/pymasktools.py index 0e69764..55a4341 100644 --- a/pymask/pymasktools.py +++ b/pymask/pymasktools.py @@ -388,7 +388,7 @@ def generate_xline_with_bb(mad, seq_name, bb_df, # Build xline model import xline line = xline.Line.from_madx_sequence( - mad.sequence[seq_name]) + mad.sequence[seq_name], apply_madx_errors=True) if bb_df is not None: bb.setup_beam_beam_in_line(line, bb_df, bb_coupling=False) @@ -429,7 +429,7 @@ def generate_xline_with_bb(mad, seq_name, bb_df, line.to_json(xline_fol_name + "/line_bb_dipole_not_cancelled.json", keepextra=True) line_bb_dipole_cancelled.to_json(xline_fol_name + "/line_bb_dipole_cancelled.json", keepextra=True) - part_on_CO.to_json(xline_fol_name + "/particle_on_closed_orbit.pkl") + part_on_CO.to_json(xline_fol_name + "/particle_on_closed_orbit.json") return xline_dict diff --git a/python_examples/hl_lhc_collisions_python/001_reload.py b/python_examples/hl_lhc_collisions_python/001_reload.py index 010cc5b..440d2ad 100644 --- a/python_examples/hl_lhc_collisions_python/001_reload.py +++ b/python_examples/hl_lhc_collisions_python/001_reload.py @@ -37,10 +37,10 @@ def fc_to_fort(): mad=Madx() -mad.call("saved_seq.madx") +mad.call("final_seq.madx") mad.use(sequence="lhcb1") mad.twiss() -mad.readtable(file="errors_lhc.tfs", table="errtab") +mad.readtable(file="final_errors.tfs", table="errtab") mad.seterr(table="errtab") mad.set(format=".15g") mad.twiss() @@ -48,13 +48,13 @@ def fc_to_fort(): fc_to_fort() import sixtracktools -import pysixtrack +import xline sixin=sixtracktools.sixinput.SixInput(".") -linesix=pysixtrack.Line.from_sixinput(sixin) -linemad=pysixtrack.Line.from_madx_sequence(mad.sequence.lhcb1,apply_madx_errors=True) +linesix=xline.Line.from_sixinput(sixin) +linemad=xline.Line.from_madx_sequence(mad.sequence.lhcb1,apply_madx_errors=True) -#linemad=pysixtrack.Line.from_madx_sequence(mad.sequence.lhcb1) +#linemad=xline.Line.from_madx_sequence(mad.sequence.lhcb1) for ll in (linesix, linemad): ll.remove_inactive_multipoles(inplace=True) diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 98f1bab..9744fff 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -21,7 +21,7 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_with_bb_legacy_macros', + 'mode' : 'b1_without_bb', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': # 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, From 90f4476333eb54ef77f8aea687b75437b2844b70 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 3 Jul 2021 07:31:08 +0200 Subject: [PATCH 15/31] Same length but by chance... Problem with crabs --- .../checks_and_doc/t004_compare_xlines.py | 28 +++++++++++++------ .../hl_lhc_collisions_python/config.py | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 66da7b2..d1c94be 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -11,6 +11,12 @@ path_ref = '../../../examples/hl_lhc_collision' type_ref = 'sixtrack' +# Test b1 +path_test = '../xline/line_bb_dipole_not_cancelled.json' +type_test = 'xline' +path_ref = '../../../examples/hl_lhc_collision' +type_ref = 'sixtrack' + # # Test b4 nobb sixtrack # path_test = '../../' # type_test = 'sixtrack' @@ -27,8 +33,7 @@ def prepare_line(path, input_type): if input_type == 'xline': # Load xline machine - with open(path, "rb") as fid: - ltest = xline.Line.from_dict(pickle.load(fid)) + ltest = xline.Line.from_json(path_test) elif input_type == 'sixtrack': print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) @@ -51,6 +56,7 @@ def prepare_line(path, input_type): ll.remove_inactive_multipoles(inplace=True) ll.remove_zero_length_drifts(inplace=True) ll.merge_consecutive_drifts(inplace=True) + ll.merge_consecutive_multipoles(inplace=True) # Check that the two machines are identical assert len(ltest) == len(lref) @@ -70,28 +76,32 @@ def norm(x): dtest = ee_test.to_dict(keepextra=True) - dsix = ee_six.to_dict(keepextra=True) + dref = ee_six.to_dict(keepextra=True) for kk in dtest.keys(): # Check if they are identical - if dtest[kk] == dsix[kk]: + if np.isscalar(dref[kk]) and dtest[kk] == dref[kk]: continue # Check if the relative error is small try: - diff_rel = norm(np.array(dtest[kk]) - np.array(dsix[kk])) / norm( - dtest[kk] - ) + val_test = dtest[kk] + val_ref = dref[kk] + if not np.isscalar(val_ref): + lmin = min(len(val_ref), len(val_test)) + val_test = val_test[:lmin] + val_ref = val_ref[:lmin] + diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) except ZeroDivisionError: diff_rel = 100.0 if diff_rel < 3e-5: continue # Check if absolute error is small - diff_abs = norm(np.array(dtest[kk]) - np.array(dsix[kk])) + diff_abs = norm(np.array(dtest[kk]) - np.array(dref[kk])) if diff_abs > 0: - print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dsix[kk]}") + print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dref[kk]}") if diff_abs < 1e-12: continue diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 9744fff..570eb3a 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -21,7 +21,7 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_without_bb', + 'mode' : 'b1_with_bb', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': # 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, From e055ea1e0f8e2619c9dd4429efd44578cfeed300 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 3 Jul 2021 13:24:33 +0200 Subject: [PATCH 16/31] Fixed issue with crabs, now issue with bb --- .../checks_and_doc/t004_compare_xlines.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index d1c94be..c3b9774 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -58,6 +58,29 @@ def prepare_line(path, input_type): ll.merge_consecutive_drifts(inplace=True) ll.merge_consecutive_multipoles(inplace=True) + # Remove inactive RFMultipoles and normalize phase + for ii, ee in enumerate(ll.elements): + if ee.__class__.__name__ == 'RFMultipole': + if ( np.max(np.abs(ee.knl)) < 1e-20 and + np.max(np.abs(ee.ksl)) < 1e-20 and + abs(ee.voltage) < 1e-20): + ll.element_names[ii] = None + ll.elements[ii] = None + for kkll, pp in [[ee.knl, ee.pn], + [ee.ksl, ee.ps]]: + for ii, vv in enumerate(kkll): + if vv < 0: + kkll[ii] = -vv + pp[ii] += 180 + if pp[ii]>180: + pp[ii] -= 360 + + while None in ll.element_names: + ll.element_names.remove(None) + while None in ll.elements: + ll.elements.remove(None) + + # Check that the two machines are identical assert len(ltest) == len(lref) From 0154731737c5a765683ccf52968bf2650e2a9995 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sat, 3 Jul 2021 14:03:47 +0200 Subject: [PATCH 17/31] Removed all references to pysixtrack --- pymask/beambeam.py | 10 +- python_examples/clean_all | 2 +- .../hl_lhc_collisions_python/000_pymask.py | 15 +- .../run3_collisions_python/old/000_pymask.py | 271 ------------------ .../old/knob_parameters.py | 29 -- .../old/mask_parameters.py | 71 ----- .../old/optics_specific_tools.py | 186 ------------ .../old/t000_plots_from_twiss.py | 38 --- .../old/t001_check_b4_against_b2.py | 44 --- .../old/t003_fc_to_fort.py | 25 -- 10 files changed, 13 insertions(+), 678 deletions(-) delete mode 100644 python_examples/run3_collisions_python/old/000_pymask.py delete mode 100644 python_examples/run3_collisions_python/old/knob_parameters.py delete mode 100644 python_examples/run3_collisions_python/old/mask_parameters.py delete mode 100644 python_examples/run3_collisions_python/old/optics_specific_tools.py delete mode 100644 python_examples/run3_collisions_python/old/t000_plots_from_twiss.py delete mode 100644 python_examples/run3_collisions_python/old/t001_check_b4_against_b2.py delete mode 100644 python_examples/run3_collisions_python/old/t003_fc_to_fort.py diff --git a/pymask/beambeam.py b/pymask/beambeam.py index 7a49749..1bbf9d8 100644 --- a/pymask/beambeam.py +++ b/pymask/beambeam.py @@ -627,27 +627,27 @@ def setup_beam_beam_in_line( bb_df, bb_coupling=False, ): - import pysixtrack + import xline assert bb_coupling is False # Not implemented for ee, eename in zip(line.elements, line.element_names): - if isinstance(ee, pysixtrack.elements.BeamBeam4D): + if isinstance(ee, xline.elements.BeamBeam4D): ee.charge = (bb_df.loc[eename, 'other_num_particles'] - * bb_df.loc[eename, 'other_particle_charge']) # TODO update pysixtrack interface to separate charge and b. population + * bb_df.loc[eename, 'other_particle_charge']) # TODO update xline interface to separate charge and b. population ee.sigma_x = np.sqrt(bb_df.loc[eename, 'other_Sigma_11']) ee.sigma_y = np.sqrt(bb_df.loc[eename, 'other_Sigma_33']) ee.beta_r = bb_df.loc[eename, 'other_relativistic_beta'] ee.x_bb = bb_df.loc[eename, 'separation_x'] ee.y_bb = bb_df.loc[eename, 'separation_y'] - if isinstance(ee, pysixtrack.elements.BeamBeam6D): + if isinstance(ee, xline.elements.BeamBeam6D): ee.phi = bb_df.loc[eename, 'phi'] ee.alpha = bb_df.loc[eename, 'alpha'] ee.x_bb_co = bb_df.loc[eename, 'separation_x'] ee.y_bb_co = bb_df.loc[eename, 'separation_y'] ee.charge_slices = [(bb_df.loc[eename, 'other_num_particles'] - * bb_df.loc[eename, 'other_particle_charge'])] # TODO update pysixtrack interface to separate charge and b. population + * bb_df.loc[eename, 'other_particle_charge'])] # TODO update xline interface to separate charge and b. population ee.zeta_slices = [0.0] ee.sigma_11 = bb_df.loc[eename, 'other_Sigma_11'] ee.sigma_12 = bb_df.loc[eename, 'other_Sigma_12'] diff --git a/python_examples/clean_all b/python_examples/clean_all index 996393d..0d467d2 100644 --- a/python_examples/clean_all +++ b/python_examples/clean_all @@ -9,7 +9,7 @@ rm twiss_bb rm last_twiss.*.gz rm fort.* rm *.tfs -rm -r pysixtrack +rm -r xline rm -r temp rm -r __pycache__ diff --git a/python_examples/hl_lhc_collisions_python/000_pymask.py b/python_examples/hl_lhc_collisions_python/000_pymask.py index 8a348d6..08fdd3c 100644 --- a/python_examples/hl_lhc_collisions_python/000_pymask.py +++ b/python_examples/hl_lhc_collisions_python/000_pymask.py @@ -496,7 +496,7 @@ output_folder='./', reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), - reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( @@ -523,22 +523,21 @@ pickle.dump(optics_orbit_start_ring, fid) -############################# -# Generate pysixtrack lines # -############################# +################### +# Generate xlines # +################### if enable_bb_legacy: print('xline is not generated with bb legacy macros') else: xline_fol_name = "./xline" + import pdb; pdb.set_trace() dct_xline = pm.generate_xline_with_bb(mad_track, sequence_to_track, bb_df_track, closed_orbit_method='from_mad', pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) - - ################################### # Save final twiss # ################################### @@ -561,6 +560,6 @@ ############################# # Save sequence and errors # ############################# - -pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') +# N.B. this erases the errors +# pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') diff --git a/python_examples/run3_collisions_python/old/000_pymask.py b/python_examples/run3_collisions_python/old/000_pymask.py deleted file mode 100644 index 411daab..0000000 --- a/python_examples/run3_collisions_python/old/000_pymask.py +++ /dev/null @@ -1,271 +0,0 @@ -import sys, os -import pickle - -import numpy as np - -# Import pymask -sys.path.append('../../') -import pymask as pm - -import optics_specific_tools as ost -from mask_parameters import mask_parameters - -Madx = pm.Madxp - -# Select mode -#mode = 'b1_without_bb' -mode = 'b1_with_bb' -#mode = 'b1_with_bb_legacy_macros' -#mode = 'b4_without_bb' -#mode = 'b4_from_b2_without_bb' -mode = 'b4_from_b2_with_bb' - -# Tolarances for checks [ip1, ip2, ip5, ip8] -tol_beta = [1e-3, 10e-2, 1e-3, 1e-2] -tol_sep = [1e-6, 1e-6, 1e-6, 1e-6] - -pm.make_links(force=True, links_dict={ - 'tracking_tools': '/afs/cern.ch/eng/tracking-tools', - 'modules': 'tracking_tools/modules', - 'tools': 'tracking_tools/tools', - 'beambeam_macros': 'tracking_tools/beambeam_macros', - 'errors': 'tracking_tools/errors'}) - -optics_file = 'opticsfile.29' - -check_betas_at_ips = True -check_separations_at_ips = True -save_intermediate_twiss = True - -# Check and load parameters -pm.checks_on_parameter_dict(mask_parameters) - -# Define configuration -(beam_to_configure, sequences_to_check, sequence_to_track, generate_b4_from_b2, - track_from_b4_mad_instance, enable_bb_python, enable_bb_legacy, - force_disable_check_separations_at_ips, - ) = pm.get_pymask_configuration(mode) -if force_disable_check_separations_at_ips: - check_separations_at_ips = False - -# Start mad -mad = Madx() - -# Build sequence -ost.build_sequence(mad, beam=beam_to_configure) - -# Apply optics -ost.apply_optics(mad, optics_file=optics_file) - -# Force disable beam-beam when needed -if not(enable_bb_legacy) and not(enable_bb_python): - mask_parameters['par_on_bb_switch'] = 0. - -# Pass parameters to mad -mad.set_variables_from_dict(params=mask_parameters) - -# Prepare sequences and attach beam -mad.call("modules/submodule_01a_preparation.madx") -mad.call("modules/submodule_01b_beam.madx") - -# Test machine before any change -twiss_dfs, other_data = ost.twiss_and_check(mad, sequences_to_check, - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_from_optics', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, - check_separations_at_ips=check_separations_at_ips) - -# Set phase, apply and save crossing -mad.call("modules/submodule_01c_phase.madx") - -# Set optics-specific knobs -ost.set_optics_specific_knobs(mad, mode) - -# Crossing-save and some reference measurements -mad.input('exec, crossing_save') -mad.call("modules/submodule_01e_final.madx") - -# Test flat machine -mad.input('exec, crossing_disable') -twiss_dfs, other_data = ost.twiss_and_check(mad, sequences_to_check, - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_no_crossing', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=check_separations_at_ips) -# Check flatness -flat_tol = 1e-6 -for ss in twiss_dfs.keys(): - tt = twiss_dfs[ss] - assert np.max(np.abs(tt.x)) < flat_tol - assert np.max(np.abs(tt.y)) < flat_tol - -# Check machine after crossing restore -mad.input('exec, crossing_restore') -twiss_dfs, other_data = ost.twiss_and_check(mad, sequences_to_check, - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_with_crossing', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=check_separations_at_ips) - -mad.use(f'lhcb{beam_to_configure}') - -# # Call leveling module -# if mode=='b4_without_bb': -# print('Leveling not working in this mode!') -# else: -# mad.call("modules/module_02_lumilevel.madx") - - -mad.input('on_disp = 0') - -# Prepare bb dataframes -if enable_bb_python: - bb_dfs = pm.generate_bb_dataframes(mad, - ip_names=['ip1', 'ip2', 'ip5', 'ip8'], - harmonic_number=35640, - numberOfLRPerIRSide=[25, 20, 25, 20], - bunch_spacing_buckets=10, - numberOfHOSlices=11, - bunch_population_ppb=None, - sigmaz_m=None, - z_crab_twiss = 0, - remove_dummy_lenses=True) - - # Here the datafremes can be edited, e.g. to set bbb intensity - -# Generate mad instance for b4 -if generate_b4_from_b2: - mad_b4 = Madx() - ost.build_sequence(mad_b4, beam=4) - ost.apply_optics(mad_b4, optics_file=optics_file) - - pm.configure_b4_from_b2(mad_b4, mad) - - twiss_dfs_b2, other_data_b2 = ost.twiss_and_check(mad, - sequences_to_check=['lhcb2'], - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_b2_for_b4check', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) - - twiss_dfs_b4, other_data_b4 = ost.twiss_and_check(mad_b4, - sequences_to_check=['lhcb2'], - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_b4_for_b4check', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) - - - -# We working exclusively on the sequence to track -# Select mad object -if track_from_b4_mad_instance: - mad_track = mad_b4 -else: - mad_track = mad - -mad_collider = mad -del(mad) - -# Twiss machine to track -twiss_dfs, other_data = ost.twiss_and_check(mad_track, sequences_to_check, - tol_beta=tol_beta, tol_sep=tol_sep, - twiss_fname='twiss_track_intermediate', - save_twiss_files=save_intermediate_twiss, - check_betas_at_ips=check_betas_at_ips, check_separations_at_ips=False) - - -# Install bb lenses -if enable_bb_python: - if track_from_b4_mad_instance: - bb_df_track = bb_dfs['b4'] - assert(sequence_to_track=='lhcb2') - else: - bb_df_track = bb_dfs['b1'] - assert(sequence_to_track=='lhcb1') - - pm.install_lenses_in_sequence(mad_track, bb_df_track, sequence_to_track) - - # Disable bb - mad_track.globals.on_bb_charge = 0 -else: - bb_df_track = None - - -# Legacy bb macros -if enable_bb_legacy: - assert(beam_to_configure == 1) - assert(not(track_from_b4_mad_instance)) - assert(not(enable_bb_python)) - mad_track.call("modules/module_03_beambeam.madx") - -# # Install crab cavities -# mad_track.call("tools/enable_crabcavities.madx") - -# # Install crab cavities -#mad_track.call("modules/submodule_04_1a_install_crabs.madx") - -# Save references (orbit at IPs) -mad_track.call('modules/submodule_04_1b_save_references.madx') - -# Switch off dipersion correction knob -mad_track.globals.on_disp = 0. - -# Final use -mad_track.use(sequence_to_track) -# Disable use -mad_track._use = mad_track.use -mad_track.use = None - -# # Install and correct errors -# mad_track.call("modules/module_04_errors.madx") - -# Machine tuning (enables bb) -mad_track.call("modules/module_05_tuning.madx") - -# # Switch on crab cavities -# mad_track.globals.on_crab1 = mad_track.globals.par_crab1 -# mad_track.globals.on_crab5 = mad_track.globals.par_crab5 - -# Generate sixtrack -if enable_bb_legacy: - mad_track.call("modules/module_06_generate.madx") -else: - pm.generate_sixtrack_input(mad_track, - seq_name=sequence_to_track, - bb_df=bb_df_track, - output_folder='./', - reference_bunch_charge_sixtrack_ppb=( - mad_track.sequence[sequence_to_track].beam.npart), - emitnx_sixtrack_um=( - mad_track.sequence[sequence_to_track].beam.exn), - emitny_sixtrack_um=( - mad_track.sequence[sequence_to_track].beam.eyn), - sigz_sixtrack_m=( - mad_track.sequence[sequence_to_track].beam.sigt), - sige_sixtrack=( - mad_track.sequence[sequence_to_track].beam.sige), - ibeco_sixtrack=1, - ibtyp_sixtrack=0, - lhc_sixtrack=2, - ibbc_sixtrack=0, - radius_sixtrack_multip_conversion_mad=0.017, - skip_mad_use=True) - -# Get optics and orbit at start ring -optics_orbit_start_ring = pm.get_optics_and_orbit_at_start_ring( - mad_track, sequence_to_track, skip_mad_use=True) -with open('./optics_orbit_at_start_ring.pkl', 'wb') as fid: - pickle.dump(optics_orbit_start_ring, fid) - -# Generate pysixtrack lines -if enable_bb_legacy: - print('Pysixtrack line is not generated with bb legacy macros') -else: - pysix_fol_name = "./pysixtrack" - dct_pysxt = pm.generate_pysixtrack_line_with_bb(mad_track, - sequence_to_track, bb_df_track, - closed_orbit_method='from_mad', - pickle_lines_in_folder=pysix_fol_name, - skip_mad_use=True) diff --git a/python_examples/run3_collisions_python/old/knob_parameters.py b/python_examples/run3_collisions_python/old/knob_parameters.py deleted file mode 100644 index f2d6c02..0000000 --- a/python_examples/run3_collisions_python/old/knob_parameters.py +++ /dev/null @@ -1,29 +0,0 @@ - -knob_parameters = { - - #IP specific orbit settings - 'par_x1' : 150., - 'par_x5' : 150., - 'par_sep1' : 0, - 'par_sep5' : 0, - 'par_sep2h' : 1, - 'par_sep2v' : 0, - 'par_x2h' : 0, - 'par_x2v' : 200, - 'par_sep8h' : 0, - 'par_sep8v' : 1, - 'par_x8h' : 0, - 'par_x8v' : 135, - - # Dispersion correction knob - 'par_on_disp' : 1, # Could be optics-dependent - - # Magnets of the experiments - 'par_on_alice' : 1, - 'par_on_lhcb' : 1, - - 'par_on_sol_atlas' : 0, - 'par_on_sol_cms' : 0, - 'par_on_sol_alice' : 0, - -} diff --git a/python_examples/run3_collisions_python/old/mask_parameters.py b/python_examples/run3_collisions_python/old/mask_parameters.py deleted file mode 100644 index 017ce09..0000000 --- a/python_examples/run3_collisions_python/old/mask_parameters.py +++ /dev/null @@ -1,71 +0,0 @@ - -mask_parameters = { - 'par_verbose' : 1, - - # Beam parameters - 'par_beam_norm_emit' : 2.5, # [um] - 'par_beam_sigt' : 0.075, # [m] - 'par_beam_sige' : 1.1e-4, # [-] - 'par_beam_npart' : 1.16e11, # [-] - 'par_beam_energy_tot' : 7000, # [GeV] - - # Settings - 'par_oct_current' : 350., # [A] - 'par_chromaticity' : 15., # [-] (Q':5 for colliding bunches, Q':15 for non-colliding bunches) - 'par_vrf_total' : 16., # [MV] - - # Tunes - 'par_qx0' : 62.31, - 'par_qy0' : 60.32, - - - #*************************# - # Beam-beam configuration # - #*************************# - - 'par_on_bb_switch' : 1, - 'par_match_with_bb' : 0, # should be off at collision - 'par_b_t_dist' : 25., # bunch separation [ns] - 'par_n_inside_D1' : 5, # default value for the number of additionnal parasitic encounters inside D1 - - 'par_nho_IR1' : 11, # number of slices for head-on in IR1 (between 0 and 201) - 'par_nho_IR2' : 11, # number of slices for head-on in IR2 (between 0 and 201) - 'par_nho_IR5' : 11, # number of slices for head-on in IR5 (between 0 and 201) - 'par_nho_IR8' : 11, # number of slices for head-on in IR8 (between 0 and 201) - - #*************************# - # Leveling in IP8 # - #*************************# - - # This variables set the leveled luminosity in IP8 (considered if par_on_collision:1) - 'par_lumi_ip8' : 2e33, #[Hz/cm2] - - # These variables define the number of Head-On collisions in the 4 IPs - 'par_nco_IP1' : 2592, - 'par_nco_IP2' : 2288, - 'par_nco_IP5' : 2592, - 'par_nco_IP8' : 2396, - - #*************************# - # Errors and corrections # - #*************************# - - # Select seed for errors - 'par_myseed' : 0, - - # Set this flag to correct the errors of D2 in the NLC (warning: for now only correcting b3 of D2, still in development) - 'par_correct_for_D2' : 0, - # Set this flag to correct the errors of MCBXF in the NLC (warning: this might be less reproducable in reality, use with care) - 'par_correct_for_MCBX' : 0, - - 'par_on_errors_LHC' : 0, - 'par_on_errors_MBH' : 0, - 'par_on_errors_Q5' : 0, - 'par_on_errors_Q4' : 0, - 'par_on_errors_D2' : 0, - 'par_on_errors_D1' : 0, - 'par_on_errors_IT' : 0, - 'par_on_errors_MCBRD' : 0, - 'par_on_errors_MCBXF' : 0, - -} diff --git a/python_examples/run3_collisions_python/old/optics_specific_tools.py b/python_examples/run3_collisions_python/old/optics_specific_tools.py deleted file mode 100644 index b990ad2..0000000 --- a/python_examples/run3_collisions_python/old/optics_specific_tools.py +++ /dev/null @@ -1,186 +0,0 @@ -import pymask as pm - -# The parts marked by (*) in following need to be -# adapted according to knob definitions - -def build_sequence(mad, beam): - - slicefactor = 2 - - pm.make_links(force=True, links_dict={ - 'optics_indep_macros.madx': 'tools/optics_indep_macros.madx', - 'macro.madx': ('/afs/cern.ch/user/s/sterbini/public/' - 'tracking_tools/tools/macro.madx'), - 'optics_runII': '/afs/cern.ch/eng/lhc/optics/runII', - 'optics_runIII': '/afs/cern.ch/eng/lhc/optics/runIII',}) - - - mylhcbeam = int(beam) - - mad.input('ver_lhc_run = 3') - - mad.input(f'mylhcbeam = {beam}') - mad.input('option, -echo,warn, -info;') - - # optics dependent macros - mad.call('macro.madx') - # optics independent macros - mad.call('optics_indep_macros.madx') - - assert mylhcbeam in [1, 2, 4], "Invalid mylhcbeam (it should be in [1, 2, 4])" - - if mylhcbeam in [1, 2]: - mad.call('optics_runII/2018/lhc_as-built.seq') - else: - mad.call('optics_runII/2018/lhcb4_as-built.seq') - - # New IR7 MQW layout and cabling - mad.call('optics_runIII/RunIII_dev/IR7-Run3seqedit.madx') - - # Makethin part - if slicefactor > 0: - # the variable in the macro is slicefactor - mad.input(f'slicefactor={slicefactor};') - mad.call('optics_runII/2018/toolkit/myslice.madx') - mad.beam() - for my_sequence in ['lhcb1','lhcb2']: - if my_sequence in list(mad.sequence): - mad.input(f'use, sequence={my_sequence}; makethin, sequence={my_sequence}, style=teapot, makedipedge=false;') - else: - warnings.warn('The sequences are not thin!') - - # Cycling w.r.t. to IP3 (mandatory to find closed orbit in collision in the presence of errors) - for my_sequence in ['lhcb1','lhcb2']: - if my_sequence in list(mad.sequence): - mad.input(f'seqedit, sequence={my_sequence}; flatten; cycle, start=IP3; flatten; endedit;') - -def apply_optics(mad, optics_file): - pm.make_links(force=True, links_dict={ - 'optics.madx' : 'optics_runIII/RunIII_dev/2022_V1/PROTON/' + optics_file}) - mad.call('optics.madx') - -def check_beta_at_ips_against_madvars(beam, twiss_df, variable_dicts, tol): - twiss_value_checks=[] - for iip, ip in enumerate([1,2,5,8]): - for plane in ['x', 'y']: - # (*) Adapet based on knob definitions - twiss_value_checks.append({ - 'element_name': f'ip{ip}:1', - 'keyword': f'bet{plane}', - 'varname': f'bet{plane}ip{ip}b{beam}', - 'tol': tol[iip]}) - - pm.check_twiss_against_madvars(twiss_value_checks, twiss_df, variable_dicts) - - -def set_optics_specific_knobs(mad, mode=None): - - from knob_parameters import knob_parameters as kp - - mad.set_variables_from_dict(params=kp) - - # Set IP knobs - mad.globals['on_x1'] = kp['par_x1'] - mad.globals['on_sep1'] = kp['par_sep1'] - - mad.globals['on_x2h'] = kp['par_x2h'] - mad.globals['on_x2v'] = kp['par_x2v'] - mad.globals['on_sep2h'] = kp['par_sep2h'] - mad.globals['on_sep2v'] = kp['par_sep2v'] - - mad.globals['on_x5'] = kp['par_x5'] - mad.globals['on_sep5'] = kp['par_sep5'] - - mad.globals['on_x8h'] = kp['par_x8h'] - mad.globals['on_x8v'] = kp['par_x8v'] - mad.globals['on_sep8h'] = kp['par_sep8h'] - mad.globals['on_sep8v'] = kp['par_sep8v'] - - mad.globals['on_disp'] = kp['par_on_disp'] - - # A check - if mad.globals.nrj < 500: - assert kp['par_on_disp'] == 0 - - # Spectrometers at experiments - if kp['par_on_alice'] == 1: - mad.globals.on_alice = 7000./mad.globals.nrj - if kp['par_on_lhcb'] == 1: - mad.globals.on_lhcb = 7000./mad.globals.nrj - - # Solenoids at experiments - mad.globals.on_sol_atlas = kp['par_on_sol_atlas'] - mad.globals.on_sol_cms = kp['par_on_sol_cms'] - mad.globals.on_sol_alice = kp['par_on_sol_alice'] - -def check_separations_at_ips_against_madvars(twiss_df_b1, twiss_df_b2, - variables_dict, tol): - - separations_to_check = [] - for iip, ip in enumerate([2,8]): - for plane in ['x', 'y']: - # (*) Adapet based on knob definitions - separations_to_check.append({ - 'element_name': f'ip{ip}:1', - 'scale_factor': -2*1e-3, - 'plane': plane, - # knobs like on_sep1h, onsep8v etc - 'varname': f'on_sep{ip}'+{'x':'h', 'y':'v'}[plane], - 'tol': tol[iip]}) - separations_to_check.append({ # IP1 - 'element_name': f'ip1:1', - 'scale_factor': -2*1e-3, - 'plane': 'x', - 'varname': 'on_sep1', - 'tol': tol[0]}) - separations_to_check.append({ # IP5 - 'element_name': f'ip5:1', - 'scale_factor': -2*1e-3, - 'plane': 'y', - 'varname': 'on_sep5', - 'tol': tol[2]}) - pm.check_separations_against_madvars(separations_to_check, - twiss_df_b1, twiss_df_b2, variables_dict) - -def twiss_and_check(mad, sequences_to_check, twiss_fname, - tol_beta=1e-3, tol_sep=1e-6, save_twiss_files=True, - check_betas_at_ips=True, check_separations_at_ips=True): - - var_dict = mad.get_variables_dicts() - twiss_dfs = {} - summ_dfs = {} - for ss in sequences_to_check: - mad.use(ss) - mad.twiss() - tdf = mad.get_twiss_df('twiss') - twiss_dfs[ss] = tdf - sdf = mad.get_summ_df('summ') - summ_dfs[ss] = sdf - - if save_twiss_files: - for ss in sequences_to_check: - tt = twiss_dfs[ss] - if twiss_fname is not None: - tt.to_parquet(twiss_fname + f'_seq_{ss}.parquet') - - if check_betas_at_ips: - for ss in sequences_to_check: - tt = twiss_dfs[ss] - check_beta_at_ips_against_madvars(beam=ss[-1], - twiss_df=tt, - variable_dicts=var_dict, - tol=tol_beta) - print('IP beta test against knobs passed!') - - if check_separations_at_ips: - twiss_df_b1 = twiss_dfs['lhcb1'] - twiss_df_b2 = twiss_dfs['lhcb2'] - check_separations_at_ips_against_madvars(twiss_df_b1, twiss_df_b2, - var_dict, tol=tol_sep) - print('IP separation test against knobs passed!') - - other_data = {} - other_data.update(var_dict) - other_data['summ_dfs'] = summ_dfs - - return twiss_dfs, other_data diff --git a/python_examples/run3_collisions_python/old/t000_plots_from_twiss.py b/python_examples/run3_collisions_python/old/t000_plots_from_twiss.py deleted file mode 100644 index 50c9863..0000000 --- a/python_examples/run3_collisions_python/old/t000_plots_from_twiss.py +++ /dev/null @@ -1,38 +0,0 @@ -import numpy as np -import pandas as pd -import matplotlib.pyplot as plt - -twiss_df = pd.read_parquet('./twiss_with_crossing_seq_lhcb1.parquet') - - -plt.close('all') - -# %% -fig = plt.figure(1, figsize=(6.4*1.6, 4.8*1.5)) -axbetx = fig.add_subplot(2,2,1) -axbetx.plot(twiss_df['s'], twiss_df['betx']) -axbetx.set_xlabel('s') -axbetx.set_ylabel(r'$\beta_x$ [m]') -axbetx.ticklabel_format(style='sci', scilimits=(0, 0), axis='y') - -# %% -axbety = fig.add_subplot(2,2,2, sharex=axbetx, sharey=axbetx) -axbety.plot(twiss_df['s'], twiss_df['bety']) -axbety.set_xlabel('s') -axbety.set_ylabel(r'$\beta_y$ [m]') -axbety.ticklabel_format(style='sci', scilimits=(0, 0), axis='y') -axbety.legend() -# %% - -axcox = fig.add_subplot(2,2,3, sharex=axbetx) -axcox.plot(twiss_df['s'], twiss_df['x']) -axcox.set_xlabel('s') -axcox.set_ylabel('x [m]') -#plt.xlim(5500,7500) -# %% -axcoy = fig.add_subplot(2,2,4, sharex=axbetx, sharey=axcox) -axcoy.plot(twiss_df['s'], twiss_df['y']) -axcoy.set_xlabel('s') -axcoy.set_ylabel('y [m]') - -plt.show() diff --git a/python_examples/run3_collisions_python/old/t001_check_b4_against_b2.py b/python_examples/run3_collisions_python/old/t001_check_b4_against_b2.py deleted file mode 100644 index d8aa29d..0000000 --- a/python_examples/run3_collisions_python/old/t001_check_b4_against_b2.py +++ /dev/null @@ -1,44 +0,0 @@ -import numpy as np -import pandas as pd -import matplotlib.pyplot as plt - -twiss_df_b2 = pd.read_parquet('./twiss_b2_for_b4check_seq_lhcb2.parquet') -twiss_df_b4 = pd.read_parquet('./twiss_b4_for_b4check_seq_lhcb2.parquet') - - -plt.close('all') - -# %% -fig = plt.figure(1, figsize=(6.4*1.6, 4.8*1.5)) -axbetx = fig.add_subplot(2,2,1) -axbetx.plot(twiss_df_b4['s'][-1]-twiss_df_b4['s'],twiss_df_b4['betx'],'b') -axbetx.plot(twiss_df_b2['s'], twiss_df_b2['betx'],'--r') -axbetx.set_xlabel('s') -axbetx.set_ylabel(r'$\beta_x$ [m]') -axbetx.ticklabel_format(style='sci', scilimits=(0, 0), axis='y') - -# %% -axbety = fig.add_subplot(2,2,2, sharex=axbetx, sharey=axbetx) -axbety.plot(twiss_df_b4['s'][-1]-twiss_df_b4['s'],twiss_df_b4['bety'],'b', - label='b4') -axbety.plot(twiss_df_b2['s'], twiss_df_b2['bety'],'--r', label='b2') -axbety.set_xlabel('s') -axbety.set_ylabel(r'$\beta_y$ [m]') -axbety.ticklabel_format(style='sci', scilimits=(0, 0), axis='y') -axbety.legend() -# %% - -axcox = fig.add_subplot(2,2,3, sharex=axbetx) -axcox.plot(twiss_df_b4['s'][-1]-twiss_df_b4['s'],-twiss_df_b4['x'],'b') -axcox.plot(twiss_df_b2['s'], twiss_df_b2['x'], '--r') -axcox.set_xlabel('s') -axcox.set_ylabel('x [m]') -#plt.xlim(5500,7500) -# %% -axcoy = fig.add_subplot(2,2,4, sharex=axbetx, sharey=axcox) -axcoy.plot(twiss_df_b4['s'][-1]-twiss_df_b4['s'],twiss_df_b4['y'],'b') -axcoy.plot(twiss_df_b2['s'], twiss_df_b2['y'], '--r') -axcoy.set_xlabel('s') -axcoy.set_ylabel('y [m]') - -plt.show() diff --git a/python_examples/run3_collisions_python/old/t003_fc_to_fort.py b/python_examples/run3_collisions_python/old/t003_fc_to_fort.py deleted file mode 100644 index 5c77e19..0000000 --- a/python_examples/run3_collisions_python/old/t003_fc_to_fort.py +++ /dev/null @@ -1,25 +0,0 @@ -import os -import shutil - -folder_list = [ - './', - '../hl_lhc_collision/', - '../hl_lhc_collision/reference', - '../hl_lhc_collision_nobb_b4'] - -for sixtrack_input_folder in folder_list: - for iff in [2,8,16,34]: - os.system(f"rm {sixtrack_input_folder}/fort.{iff}") - try: - shutil.copy(sixtrack_input_folder + f"/fc.{iff}", - sixtrack_input_folder + f"/fort.{iff}") - except Exception: - print(f"/fc.{iff} not found!") - - with open(sixtrack_input_folder + "/fort.3", "w") as fout: - with open("fort_parts/fort_beginning.3", "r") as fid_fort3b: - fout.write(fid_fort3b.read()) - with open(sixtrack_input_folder + "/fc.3", "r") as fid_fc3: - fout.write(fid_fc3.read()) - with open("fort_parts/fort_end.3", "r") as fid_fort3e: - fout.write(fid_fort3e.read()) From 0cf81ae7d51153bdea4204c110f0da61b7c8c940 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 00:09:50 +0200 Subject: [PATCH 18/31] Working on 004 --- .../hl_lhc_collisions_python/000_pymask.py | 1 - .../checks_and_doc/t004_compare_xlines.py | 36 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/000_pymask.py b/python_examples/hl_lhc_collisions_python/000_pymask.py index 08fdd3c..d9146fe 100644 --- a/python_examples/hl_lhc_collisions_python/000_pymask.py +++ b/python_examples/hl_lhc_collisions_python/000_pymask.py @@ -531,7 +531,6 @@ print('xline is not generated with bb legacy macros') else: xline_fol_name = "./xline" - import pdb; pdb.set_trace() dct_xline = pm.generate_xline_with_bb(mad_track, sequence_to_track, bb_df_track, closed_orbit_method='from_mad', diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index c3b9774..1b0cc1f 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -14,7 +14,7 @@ # Test b1 path_test = '../xline/line_bb_dipole_not_cancelled.json' type_test = 'xline' -path_ref = '../../../examples/hl_lhc_collision' +path_ref = '../' type_ref = 'sixtrack' # # Test b4 nobb sixtrack @@ -108,21 +108,27 @@ def norm(x): continue # Check if the relative error is small + val_test = dtest[kk] + val_ref = dref[kk] try: - val_test = dtest[kk] - val_ref = dref[kk] - if not np.isscalar(val_ref): - lmin = min(len(val_ref), len(val_test)) - val_test = val_test[:lmin] - val_ref = val_ref[:lmin] - diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) + if not np.isscalar(val_ref) and len(val_ref) != len(val_test): + diff_rel = 100 + #lmin = min(len(val_ref), len(val_test)) + #val_test = val_test[:lmin] + #val_ref = val_ref[:lmin] + else: + diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) except ZeroDivisionError: diff_rel = 100.0 if diff_rel < 3e-5: continue # Check if absolute error is small - diff_abs = norm(np.array(dtest[kk]) - np.array(dref[kk])) + + if not np.isscalar(val_ref) and len(val_ref) != len(val_test): + diff_abs = 1000 + else: + diff_abs = norm(np.array(val_test) - np.array(val_ref)) if diff_abs > 0: print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dref[kk]}") if diff_abs < 1e-12: @@ -156,6 +162,18 @@ def norm(x): if kk == "length": if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: continue + if kk == 'knl' or kk == 'ksl': + ARRIVATO QUA + if len(val_ref) != len(val_test): + lmin = min(len(val_ref), len(val_test)) + if lmin < 13: + raise ValueError('Missing significane multipole strength') + else: + diff_rel = norm(np.array(val_test[:lmin]) + - np.array(val_ref[:lmin]))/norm(val_ref) + if diff_rel < 1e-5: + continue + # Exceptions BB4D (separations are recalculated) if isinstance(ee_test, xline.elements.BeamBeam4D): From 7f839074745052d6742e98d9d7d8af33cc4e40b9 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 00:14:02 +0200 Subject: [PATCH 19/31] Making test more robust --- .../checks_and_doc/t004_compare_xlines.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 1b0cc1f..03837a0 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -163,10 +163,11 @@ def norm(x): if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: continue if kk == 'knl' or kk == 'ksl': - ARRIVATO QUA + val_ref = np.trim_zeros(val_ref) + val_test= np.trim_zeros(val_test) if len(val_ref) != len(val_test): lmin = min(len(val_ref), len(val_test)) - if lmin < 13: + if lmin < 10: raise ValueError('Missing significane multipole strength') else: diff_rel = norm(np.array(val_test[:lmin]) From 0a8ac5f227a59e3580c230eba370f3b6a85afb04 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 10:05:20 +0200 Subject: [PATCH 20/31] Improved check --- .../checks_and_doc/t004_compare_xlines.py | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 03837a0..6aedd49 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -66,14 +66,15 @@ def prepare_line(path, input_type): abs(ee.voltage) < 1e-20): ll.element_names[ii] = None ll.elements[ii] = None - for kkll, pp in [[ee.knl, ee.pn], - [ee.ksl, ee.ps]]: - for ii, vv in enumerate(kkll): - if vv < 0: - kkll[ii] = -vv - pp[ii] += 180 - if pp[ii]>180: - pp[ii] -= 360 + # # Normalize phase + # for kkll, pp in [[ee.knl, ee.pn], + # [ee.ksl, ee.ps]]: + # for ii, vv in enumerate(kkll): + # if vv < 0: + # kkll[ii] = -vv + # pp[ii] += 180 + # if pp[ii]>180: + # pp[ii] -= 360 while None in ll.element_names: ll.element_names.remove(None) @@ -170,10 +171,15 @@ def norm(x): if lmin < 10: raise ValueError('Missing significane multipole strength') else: - diff_rel = norm(np.array(val_test[:lmin]) - - np.array(val_ref[:lmin]))/norm(val_ref) - if diff_rel < 1e-5: - continue + val_ref = val_ref[:lmin] + val_test = val_test[:lmin] + if len(val_ref) == 0 and len(val_test) == 0: + continue + else: + diff_rel = (norm(np.array(val_test) - np.array(val_ref)) + /norm(val_test)) + if diff_rel < 1e-5: + continue # Exceptions BB4D (separations are recalculated) From 4d1c99027ec1880b90641d5b29ebcbfe8e34f239 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 10:16:41 +0200 Subject: [PATCH 21/31] Testing sixtrack output again, investigating a new discrepancy --- .../checks_and_doc/t004_compare_xlines.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 6aedd49..32221e0 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -11,11 +11,11 @@ path_ref = '../../../examples/hl_lhc_collision' type_ref = 'sixtrack' -# Test b1 -path_test = '../xline/line_bb_dipole_not_cancelled.json' -type_test = 'xline' -path_ref = '../' -type_ref = 'sixtrack' +# # Test b1 +# path_test = '../xline/line_bb_dipole_not_cancelled.json' +# type_test = 'xline' +# path_ref = '../' +# type_ref = 'sixtrack' # # Test b4 nobb sixtrack # path_test = '../../' @@ -135,21 +135,6 @@ def norm(x): if diff_abs < 1e-12: continue - # Exception: correctors involved in lumi leveling - if nn_test in [ - 'mcbcv.5l8.b1', 'mcbyv.a4l8.b1', 'mcbxv.3l8', - 'mcbyv.4r8.b1', 'mcbyv.b5r8.b1', - 'mcbyh.b5l2.b1', 'mcbyh.4l2.b1', 'mcbxh.3l2', 'mcbyh.a4r2.b1', - 'mcbch.5r2.b1', - 'mcbcv.5l8.b2', 'mcbyv.a4l8.b2', 'mcbxv.3l8', - 'mcbyv.4r8.b2', 'mcbyv.b5r8.b2', - 'mcbyh.b5l2.b2', 'mcbyh.4l2.b2', 'mcbxh.3l2', 'mcbyh.a4r2.b2', - 'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2', - 'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2', - 'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']: - if diff_rel < 1e-2: - continue - # Exception: drift length (100 um tolerance) if isinstance( ee_test, (xline.elements.Drift, xline.elements.DriftExact) @@ -181,6 +166,21 @@ def norm(x): if diff_rel < 1e-5: continue + # Exception: correctors involved in lumi leveling + if nn_test in [ + 'mcbcv.5l8.b1', 'mcbyv.a4l8.b1', 'mcbxv.3l8', + 'mcbyv.4r8.b1', 'mcbyv.b5r8.b1', + 'mcbyh.b5l2.b1', 'mcbyh.4l2.b1', 'mcbxh.3l2', 'mcbyh.a4r2.b1', + 'mcbch.5r2.b1', + 'mcbcv.5l8.b2', 'mcbyv.a4l8.b2', 'mcbxv.3l8', + 'mcbyv.4r8.b2', 'mcbyv.b5r8.b2', + 'mcbyh.b5l2.b2', 'mcbyh.4l2.b2', 'mcbxh.3l2', 'mcbyh.a4r2.b2', + 'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2', + 'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2', + 'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']: + if diff_rel < 1e-2: + continue + # Exceptions BB4D (separations are recalculated) if isinstance(ee_test, xline.elements.BeamBeam4D): From b9cc5ed093fe459e703e998f84463d2b6081ccec Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 10:54:09 +0200 Subject: [PATCH 22/31] Now passing also when comparing sixtrack lines --- .../checks_and_doc/t004_compare_xlines.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 32221e0..71fc87c 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -154,7 +154,7 @@ def norm(x): if len(val_ref) != len(val_test): lmin = min(len(val_ref), len(val_test)) if lmin < 10: - raise ValueError('Missing significane multipole strength') + raise ValueError('Missing significant multipole strength') else: val_ref = val_ref[:lmin] val_test = val_test[:lmin] @@ -167,7 +167,8 @@ def norm(x): continue # Exception: correctors involved in lumi leveling - if nn_test in [ + passed_corr = False + for nn_corr in [ 'mcbcv.5l8.b1', 'mcbyv.a4l8.b1', 'mcbxv.3l8', 'mcbyv.4r8.b1', 'mcbyv.b5r8.b1', 'mcbyh.b5l2.b1', 'mcbyh.4l2.b1', 'mcbxh.3l2', 'mcbyh.a4r2.b1', @@ -178,8 +179,11 @@ def norm(x): 'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2', 'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2', 'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']: - if diff_rel < 1e-2: - continue + if nn_corr in nn_test and diff_rel < 1e-2: + passed_corr = True + break + if passed_corr: + continue # Exceptions BB4D (separations are recalculated) From 50c290118a0a6c8986ada5677e7fe2871230ea9b Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 11:13:37 +0200 Subject: [PATCH 23/31] Update compare script --- .../checks_and_doc/t004_compare_xlines.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 71fc87c..992d327 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -10,12 +10,18 @@ type_test = 'sixtrack' path_ref = '../../../examples/hl_lhc_collision' type_ref = 'sixtrack' +rtol = 3e-5 +atol = 1e-12 +strict = False # # Test b1 # path_test = '../xline/line_bb_dipole_not_cancelled.json' # type_test = 'xline' # path_ref = '../' # type_ref = 'sixtrack' +# rtol = 3e-7 +# atol = 1e-100 +# strict=True # # Test b4 nobb sixtrack # path_test = '../../' @@ -121,7 +127,7 @@ def norm(x): diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) except ZeroDivisionError: diff_rel = 100.0 - if diff_rel < 3e-5: + if diff_rel < rtol: continue # Check if absolute error is small @@ -132,11 +138,11 @@ def norm(x): diff_abs = norm(np.array(val_test) - np.array(val_ref)) if diff_abs > 0: print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dref[kk]}") - if diff_abs < 1e-12: + if diff_abs < atol: continue # Exception: drift length (100 um tolerance) - if isinstance( + if not(strict) and isinstance( ee_test, (xline.elements.Drift, xline.elements.DriftExact) ): if kk == "length": @@ -163,7 +169,7 @@ def norm(x): else: diff_rel = (norm(np.array(val_test) - np.array(val_ref)) /norm(val_test)) - if diff_rel < 1e-5: + if diff_rel < rtol: continue # Exception: correctors involved in lumi leveling @@ -182,12 +188,12 @@ def norm(x): if nn_corr in nn_test and diff_rel < 1e-2: passed_corr = True break - if passed_corr: + if not(strict) and passed_corr: continue # Exceptions BB4D (separations are recalculated) - if isinstance(ee_test, xline.elements.BeamBeam4D): + if not(strict) and isinstance(ee_test, xline.elements.BeamBeam4D): if kk == "x_bb": if diff_abs / dtest["sigma_x"] < 0.01: # This is neede to accommodate different leveling routines (1% difference) continue @@ -202,7 +208,7 @@ def norm(x): continue # Exceptions BB4D (angles and separations are recalculated) - if isinstance(ee_test, xline.elements.BeamBeam6D): + if not(strict) and isinstance(ee_test, xline.elements.BeamBeam6D): if kk == "alpha": if diff_abs < 10e-6: continue From 0846258b67b8548c85fbc2bce3140a8336b998a2 Mon Sep 17 00:00:00 2001 From: Giovanni Iadarola Date: Sun, 4 Jul 2021 12:14:26 +0200 Subject: [PATCH 24/31] problem with b4 --- .../checks_and_doc/t004_compare_xlines.py | 28 +++++++++++-------- .../hl_lhc_collisions_python/config.py | 4 +-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 992d327..bc9bdcd 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -14,7 +14,7 @@ atol = 1e-12 strict = False -# # Test b1 +# # Test b1 (xline) # path_test = '../xline/line_bb_dipole_not_cancelled.json' # type_test = 'xline' # path_ref = '../' @@ -23,17 +23,23 @@ # atol = 1e-100 # strict=True -# # Test b4 nobb sixtrack -# path_test = '../../' -# type_test = 'sixtrack' -# path_ref = '../../../examples/hl_lhc_collision_nobb_b4' -# type_ref = 'sixtrack' +# Test b4 nobb sixtrack +path_test = '../' +type_test = 'sixtrack' +path_ref = '../../../examples/hl_lhc_collision_nobb_b4' +type_ref = 'sixtrack' +rtol = 3e-5 +atol = 1e-12 +strict = True -# # Test b4 nobb xline (not working for now) -# path_test = './xline/line_bb_dipole_not_cancelled.pkl' -# type_test = 'xline' -# path_ref = '../hl_lhc_collision_nobb_b4' -# type_ref = 'sixtrack' +# Test b4 (xline) +path_test = '../xline/line_bb_dipole_not_cancelled.json' +type_test = 'xline' +path_ref = '../' +type_ref = 'sixtrack' +rtol = 3e-7 +atol = 1e-100 +strict=True def prepare_line(path, input_type): diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 570eb3a..e1f060e 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -21,10 +21,10 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_with_bb', + 'mode' : 'b4_from_b2_without_bb', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': - # 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, + 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, # Optics file 'optics_file' : 'optics_repository/HLLHCV1.4/round/opt_round_150_1500_thin.madx', #15 cm From 1c11e3be63a9d9a210b2e71db1d92db3d062745e Mon Sep 17 00:00:00 2001 From: sterbini Date: Mon, 5 Jul 2021 15:54:08 +0200 Subject: [PATCH 25/31] Fixing test --- .../checks_and_doc/t004_compare_xlines.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index bc9bdcd..7b794db 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -37,7 +37,7 @@ type_test = 'xline' path_ref = '../' type_ref = 'sixtrack' -rtol = 3e-7 +rtol = 4e-7 atol = 1e-100 strict=True @@ -161,22 +161,26 @@ def norm(x): if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: continue if kk == 'knl' or kk == 'ksl': - val_ref = np.trim_zeros(val_ref) - val_test= np.trim_zeros(val_test) if len(val_ref) != len(val_test): lmin = min(len(val_ref), len(val_test)) - if lmin < 10: - raise ValueError('Missing significant multipole strength') - else: - val_ref = val_ref[:lmin] - val_test = val_test[:lmin] + for vv in [val_ref,val_test]: + if len(vv)> lmin: + for oo in range(lmin, len(vv)): # we do not care about errors above 10 + if vv[oo] != 0 and oo < 10: + raise ValueError('Missing significant multipole strength') + + val_ref = val_ref[:lmin] + val_test = val_test[:lmin] + if len(val_ref) == 0 and len(val_test) == 0: continue else: - diff_rel = (norm(np.array(val_test) - np.array(val_ref)) - /norm(val_test)) + diff_abs = norm(np.array(val_test) - np.array(val_ref)) + diff_rel = diff_abs/norm(val_test) if diff_rel < rtol: continue + if diff_abs < atol: + continue # Exception: correctors involved in lumi leveling passed_corr = False From cda19db73909252cbe953b176904fe585cce8c69 Mon Sep 17 00:00:00 2001 From: sterbini Date: Mon, 5 Jul 2021 16:15:39 +0200 Subject: [PATCH 26/31] Improved test script --- .../checks_and_doc/t004_compare_xlines.py | 407 +++++++++--------- 1 file changed, 215 insertions(+), 192 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 7b794db..5ebbe75 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -1,3 +1,4 @@ +import time import shutil import pickle import numpy as np @@ -24,22 +25,32 @@ # strict=True # Test b4 nobb sixtrack -path_test = '../' -type_test = 'sixtrack' -path_ref = '../../../examples/hl_lhc_collision_nobb_b4' -type_ref = 'sixtrack' -rtol = 3e-5 -atol = 1e-12 -strict = True +tests = [ + { + 'test_name': 'B4 - pymask sixtrack input vs madx mask', + 'path_test': '../', + 'type_test': 'sixtrack', + 'path_ref': '../../../examples/hl_lhc_collision_nobb_b4', + 'type_ref': 'sixtrack', + 'rtol': 3e-5, + 'atol': 1e-12, + 'strict': True, + }, + { + 'test_name': 'B4 - pymask xline vs ymask sixtrack input', + 'path_test': '../xline/line_bb_dipole_not_cancelled.json', + 'type_test': 'xline', + 'path_ref': '../', + 'type_ref': 'sixtrack', + 'rtol': 4e-7, + 'atol': 1e-100, + 'strict': True, + } + + ] -# Test b4 (xline) -path_test = '../xline/line_bb_dipole_not_cancelled.json' -type_test = 'xline' -path_ref = '../' -type_ref = 'sixtrack' -rtol = 4e-7 -atol = 1e-100 -strict=True +def norm(x): + return np.sqrt(np.sum(np.array(x) ** 2)) def prepare_line(path, input_type): @@ -56,185 +67,197 @@ def prepare_line(path, input_type): return ltest -# Load -ltest = prepare_line(path_test, type_test) -lref = prepare_line(path_ref, type_ref) - -original_length = ltest.get_length() -assert (lref.get_length() - original_length) < 1e-6 - -# Simplify the two machines -for ll in (ltest, lref): - ll.remove_inactive_multipoles(inplace=True) - ll.remove_zero_length_drifts(inplace=True) - ll.merge_consecutive_drifts(inplace=True) - ll.merge_consecutive_multipoles(inplace=True) - - # Remove inactive RFMultipoles and normalize phase - for ii, ee in enumerate(ll.elements): - if ee.__class__.__name__ == 'RFMultipole': - if ( np.max(np.abs(ee.knl)) < 1e-20 and - np.max(np.abs(ee.ksl)) < 1e-20 and - abs(ee.voltage) < 1e-20): - ll.element_names[ii] = None - ll.elements[ii] = None - # # Normalize phase - # for kkll, pp in [[ee.knl, ee.pn], - # [ee.ksl, ee.ps]]: - # for ii, vv in enumerate(kkll): - # if vv < 0: - # kkll[ii] = -vv - # pp[ii] += 180 - # if pp[ii]>180: - # pp[ii] -= 360 - - while None in ll.element_names: - ll.element_names.remove(None) - while None in ll.elements: - ll.elements.remove(None) - - -# Check that the two machines are identical -assert len(ltest) == len(lref) - -assert (ltest.get_length() - original_length) < 1e-6 -assert (lref.get_length() - original_length) < 1e-6 - - -def norm(x): - return np.sqrt(np.sum(np.array(x) ** 2)) - - -for ii, (ee_test, ee_six, nn_test, nn_six) in enumerate( - zip(ltest.elements, lref.elements, ltest.element_names, lref.element_names) -): - assert type(ee_test) == type(ee_six) - - - dtest = ee_test.to_dict(keepextra=True) - dref = ee_six.to_dict(keepextra=True) - - for kk in dtest.keys(): - - # Check if they are identical - if np.isscalar(dref[kk]) and dtest[kk] == dref[kk]: - continue - - # Check if the relative error is small - val_test = dtest[kk] - val_ref = dref[kk] - try: +for tt in tests: + test_name = tt['test_name'] + path_test = tt['path_test'] + type_test = tt['type_test'] + path_ref = tt['path_ref'] + type_ref = tt['type_ref'] + rtol = tt['rtol'] + atol = tt['atol'] + strict = tt['strict'] + + # Load + ltest = prepare_line(path_test, type_test) + lref = prepare_line(path_ref, type_ref) + + original_length = ltest.get_length() + assert (lref.get_length() - original_length) < 1e-6 + + # Simplify the two machines + for ll in (ltest, lref): + ll.remove_inactive_multipoles(inplace=True) + ll.remove_zero_length_drifts(inplace=True) + ll.merge_consecutive_drifts(inplace=True) + ll.merge_consecutive_multipoles(inplace=True) + + # Remove inactive RFMultipoles and normalize phase + for ii, ee in enumerate(ll.elements): + if ee.__class__.__name__ == 'RFMultipole': + if ( np.max(np.abs(ee.knl)) < 1e-20 and + np.max(np.abs(ee.ksl)) < 1e-20 and + abs(ee.voltage) < 1e-20): + ll.element_names[ii] = None + ll.elements[ii] = None + # # Normalize phase + # for kkll, pp in [[ee.knl, ee.pn], + # [ee.ksl, ee.ps]]: + # for ii, vv in enumerate(kkll): + # if vv < 0: + # kkll[ii] = -vv + # pp[ii] += 180 + # if pp[ii]>180: + # pp[ii] -= 360 + + while None in ll.element_names: + ll.element_names.remove(None) + while None in ll.elements: + ll.elements.remove(None) + + + # Check that the two machines are identical + assert len(ltest) == len(lref) + + assert (ltest.get_length() - original_length) < 1e-6 + assert (lref.get_length() - original_length) < 1e-6 + + + + + for ii, (ee_test, ee_six, nn_test, nn_six) in enumerate( + zip(ltest.elements, lref.elements, ltest.element_names, lref.element_names) + ): + assert type(ee_test) == type(ee_six) + + + dtest = ee_test.to_dict(keepextra=True) + dref = ee_six.to_dict(keepextra=True) + + for kk in dtest.keys(): + + # Check if they are identical + if np.isscalar(dref[kk]) and dtest[kk] == dref[kk]: + continue + + # Check if the relative error is small + val_test = dtest[kk] + val_ref = dref[kk] + try: + if not np.isscalar(val_ref) and len(val_ref) != len(val_test): + diff_rel = 100 + #lmin = min(len(val_ref), len(val_test)) + #val_test = val_test[:lmin] + #val_ref = val_ref[:lmin] + else: + diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) + except ZeroDivisionError: + diff_rel = 100.0 + if diff_rel < rtol: + continue + + # Check if absolute error is small + if not np.isscalar(val_ref) and len(val_ref) != len(val_test): - diff_rel = 100 - #lmin = min(len(val_ref), len(val_test)) - #val_test = val_test[:lmin] - #val_ref = val_ref[:lmin] + diff_abs = 1000 else: - diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test) - except ZeroDivisionError: - diff_rel = 100.0 - if diff_rel < rtol: - continue - - # Check if absolute error is small - - if not np.isscalar(val_ref) and len(val_ref) != len(val_test): - diff_abs = 1000 - else: - diff_abs = norm(np.array(val_test) - np.array(val_ref)) - if diff_abs > 0: - print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dref[kk]}") - if diff_abs < atol: - continue - - # Exception: drift length (100 um tolerance) - if not(strict) and isinstance( - ee_test, (xline.elements.Drift, xline.elements.DriftExact) - ): - if kk == "length": - if diff_abs < 1e-4: - continue - - # Exception: multipole lrad is not passed to sixtraxk - if isinstance(ee_test, xline.elements.Multipole): - if kk == "length": - if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: - continue - if kk == 'knl' or kk == 'ksl': - if len(val_ref) != len(val_test): - lmin = min(len(val_ref), len(val_test)) - for vv in [val_ref,val_test]: - if len(vv)> lmin: - for oo in range(lmin, len(vv)): # we do not care about errors above 10 - if vv[oo] != 0 and oo < 10: - raise ValueError('Missing significant multipole strength') - - val_ref = val_ref[:lmin] - val_test = val_test[:lmin] - - if len(val_ref) == 0 and len(val_test) == 0: - continue - else: - diff_abs = norm(np.array(val_test) - np.array(val_ref)) - diff_rel = diff_abs/norm(val_test) - if diff_rel < rtol: + diff_abs = norm(np.array(val_test) - np.array(val_ref)) + if diff_abs > 0: + print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dref[kk]}") + if diff_abs < atol: + continue + + # Exception: drift length (100 um tolerance) + if not(strict) and isinstance( + ee_test, (xline.elements.Drift, xline.elements.DriftExact) + ): + if kk == "length": + if diff_abs < 1e-4: continue - if diff_abs < atol: + + # Exception: multipole lrad is not passed to sixtraxk + if isinstance(ee_test, xline.elements.Multipole): + if kk == "length": + if np.abs(ee_test.hxl) + np.abs(ee_test.hyl) == 0.0: continue - - # Exception: correctors involved in lumi leveling - passed_corr = False - for nn_corr in [ - 'mcbcv.5l8.b1', 'mcbyv.a4l8.b1', 'mcbxv.3l8', - 'mcbyv.4r8.b1', 'mcbyv.b5r8.b1', - 'mcbyh.b5l2.b1', 'mcbyh.4l2.b1', 'mcbxh.3l2', 'mcbyh.a4r2.b1', - 'mcbch.5r2.b1', - 'mcbcv.5l8.b2', 'mcbyv.a4l8.b2', 'mcbxv.3l8', - 'mcbyv.4r8.b2', 'mcbyv.b5r8.b2', - 'mcbyh.b5l2.b2', 'mcbyh.4l2.b2', 'mcbxh.3l2', 'mcbyh.a4r2.b2', - 'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2', - 'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2', - 'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']: - if nn_corr in nn_test and diff_rel < 1e-2: - passed_corr = True - break - if not(strict) and passed_corr: - continue - - - # Exceptions BB4D (separations are recalculated) - if not(strict) and isinstance(ee_test, xline.elements.BeamBeam4D): - if kk == "x_bb": - if diff_abs / dtest["sigma_x"] < 0.01: # This is neede to accommodate different leveling routines (1% difference) - continue - if kk == "y_bb": - if diff_abs / dtest["sigma_y"] < 0.01: - continue - if kk == "sigma_x": - if diff_rel < 1e-5: - continue - if kk == "sigma_y": - if diff_rel < 1e-5: - continue - - # Exceptions BB4D (angles and separations are recalculated) - if not(strict) and isinstance(ee_test, xline.elements.BeamBeam6D): - if kk == "alpha": - if diff_abs < 10e-6: - continue - if kk == "x_bb_co": - if diff_abs / np.sqrt(dtest["sigma_11"]) < 0.015: - continue - if kk == "y_bb_co": - if diff_abs / np.sqrt(dtest["sigma_33"]) < 0.015: - continue - - # If it got here it means that no condition above is met - raise ValueError("Too large discrepancy!") -print( + if kk == 'knl' or kk == 'ksl': + if len(val_ref) != len(val_test): + lmin = min(len(val_ref), len(val_test)) + for vv in [val_ref,val_test]: + if len(vv)> lmin: + for oo in range(lmin, len(vv)): # we do not care about errors above 10 + if vv[oo] != 0 and oo < 10: + raise ValueError('Missing significant multipole strength') + + val_ref = val_ref[:lmin] + val_test = val_test[:lmin] + + if len(val_ref) == 0 and len(val_test) == 0: + continue + else: + diff_abs = norm(np.array(val_test) - np.array(val_ref)) + diff_rel = diff_abs/norm(val_test) + if diff_rel < rtol: + continue + if diff_abs < atol: + continue + + # Exception: correctors involved in lumi leveling + passed_corr = False + for nn_corr in [ + 'mcbcv.5l8.b1', 'mcbyv.a4l8.b1', 'mcbxv.3l8', + 'mcbyv.4r8.b1', 'mcbyv.b5r8.b1', + 'mcbyh.b5l2.b1', 'mcbyh.4l2.b1', 'mcbxh.3l2', 'mcbyh.a4r2.b1', + 'mcbch.5r2.b1', + 'mcbcv.5l8.b2', 'mcbyv.a4l8.b2', 'mcbxv.3l8', + 'mcbyv.4r8.b2', 'mcbyv.b5r8.b2', + 'mcbyh.b5l2.b2', 'mcbyh.4l2.b2', 'mcbxh.3l2', 'mcbyh.a4r2.b2', + 'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2', + 'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2', + 'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']: + if nn_corr in nn_test and diff_rel < 1e-2: + passed_corr = True + break + if not(strict) and passed_corr: + continue + + + # Exceptions BB4D (separations are recalculated) + if not(strict) and isinstance(ee_test, xline.elements.BeamBeam4D): + if kk == "x_bb": + if diff_abs / dtest["sigma_x"] < 0.01: # This is neede to accommodate different leveling routines (1% difference) + continue + if kk == "y_bb": + if diff_abs / dtest["sigma_y"] < 0.01: + continue + if kk == "sigma_x": + if diff_rel < 1e-5: + continue + if kk == "sigma_y": + if diff_rel < 1e-5: + continue + + # Exceptions BB4D (angles and separations are recalculated) + if not(strict) and isinstance(ee_test, xline.elements.BeamBeam6D): + if kk == "alpha": + if diff_abs < 10e-6: + continue + if kk == "x_bb_co": + if diff_abs / np.sqrt(dtest["sigma_11"]) < 0.015: + continue + if kk == "y_bb_co": + if diff_abs / np.sqrt(dtest["sigma_33"]) < 0.015: + continue + + # If it got here it means that no condition above is met + raise ValueError("Too large discrepancy!") + print( + f""" + ******************************************************************* + test: {test_name} + + The line from test seq. and the line from reference are identical! + ******************************************************************* """ -******************************************************************* - The line from test seq. and the line from reference are identical! -******************************************************************* -""" -) + ) + + time.sleep(1) From e69214c930856a8d5df180b40071ca65841be53b Mon Sep 17 00:00:00 2001 From: sterbini Date: Mon, 5 Jul 2021 17:47:04 +0200 Subject: [PATCH 27/31] improved test --- .../checks_and_doc/t004_compare_xlines.py | 50 +++++++++++-------- .../hl_lhc_collisions_python/config.py | 4 +- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 5ebbe75..7e46587 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -6,38 +6,21 @@ import xline import sixtracktools -# Test b1 -path_test = '../' -type_test = 'sixtrack' -path_ref = '../../../examples/hl_lhc_collision' -type_ref = 'sixtrack' -rtol = 3e-5 -atol = 1e-12 -strict = False -# # Test b1 (xline) -# path_test = '../xline/line_bb_dipole_not_cancelled.json' -# type_test = 'xline' -# path_ref = '../' -# type_ref = 'sixtrack' -# rtol = 3e-7 -# atol = 1e-100 -# strict=True - -# Test b4 nobb sixtrack +# Tests b1 with bb tests = [ { - 'test_name': 'B4 - pymask sixtrack input vs madx mask', + 'test_name': 'B1 - pymask sixtrack input vs madx mask', 'path_test': '../', 'type_test': 'sixtrack', - 'path_ref': '../../../examples/hl_lhc_collision_nobb_b4', + 'path_ref': '../../../examples/hl_lhc_collision', 'type_ref': 'sixtrack', 'rtol': 3e-5, 'atol': 1e-12, 'strict': True, }, { - 'test_name': 'B4 - pymask xline vs ymask sixtrack input', + 'test_name': 'B1 - pymask xline vs pymask sixtrack input', 'path_test': '../xline/line_bb_dipole_not_cancelled.json', 'type_test': 'xline', 'path_ref': '../', @@ -46,8 +29,31 @@ 'atol': 1e-100, 'strict': True, } +] - ] +# # Tests b4 no bb +# tests = [ +# { +# 'test_name': 'B4 - pymask sixtrack input vs madx mask', +# 'path_test': '../', +# 'type_test': 'sixtrack', +# 'path_ref': '../../../examples/hl_lhc_collision_nobb_b4', +# 'type_ref': 'sixtrack', +# 'rtol': 3e-5, +# 'atol': 1e-12, +# 'strict': True, +# }, +# { +# 'test_name': 'B4 - pymask xline vs pymask sixtrack input', +# 'path_test': '../xline/line_bb_dipole_not_cancelled.json', +# 'type_test': 'xline', +# 'path_ref': '../', +# 'type_ref': 'sixtrack', +# 'rtol': 4e-7, +# 'atol': 1e-100, +# 'strict': True, +# } +# ] def norm(x): return np.sqrt(np.sum(np.array(x) ** 2)) diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index e1f060e..397e007 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -21,10 +21,10 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b4_from_b2_without_bb', + 'mode' : 'b1_with_bb_legacy_macros', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': - 'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, + #'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, # Optics file 'optics_file' : 'optics_repository/HLLHCV1.4/round/opt_round_150_1500_thin.madx', #15 cm From 952671a3c24dd1f4955fe83299bd4a7a29232dd3 Mon Sep 17 00:00:00 2001 From: giadarol Date: Mon, 5 Jul 2021 22:21:02 +0200 Subject: [PATCH 28/31] Now it t004 works --- .../checks_and_doc/t004_compare_xlines.py | 4 ++-- python_examples/hl_lhc_collisions_python/config.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index 7e46587..e42bcdd 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -17,7 +17,7 @@ 'type_ref': 'sixtrack', 'rtol': 3e-5, 'atol': 1e-12, - 'strict': True, + 'strict': False, }, { 'test_name': 'B1 - pymask xline vs pymask sixtrack input', @@ -62,7 +62,7 @@ def prepare_line(path, input_type): if input_type == 'xline': # Load xline machine - ltest = xline.Line.from_json(path_test) + ltest = xline.Line.from_json(path) elif input_type == 'sixtrack': print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) diff --git a/python_examples/hl_lhc_collisions_python/config.py b/python_examples/hl_lhc_collisions_python/config.py index 397e007..ba45642 100644 --- a/python_examples/hl_lhc_collisions_python/config.py +++ b/python_examples/hl_lhc_collisions_python/config.py @@ -21,7 +21,7 @@ # 'b1_with_bb_legacy_macros' # 'b4_without_bb' - 'mode' : 'b1_with_bb_legacy_macros', + 'mode' : 'b1_with_bb', # For test against madx mask for modes 'b4_from_b2_without_bb' and 'b4_without_bb': #'force_leveling' : {'on_sep8': -0.03425547139366354, 'on_sep2': 0.14471680504084292}, From 605a21ade7cf16cd978cf274d9a7e09fc139631b Mon Sep 17 00:00:00 2001 From: giadarol Date: Mon, 5 Jul 2021 22:30:31 +0200 Subject: [PATCH 29/31] Settings for b2 checks --- .../checks_and_doc/t004_compare_xlines.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py index e42bcdd..c997e41 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t004_compare_xlines.py @@ -39,9 +39,9 @@ # 'type_test': 'sixtrack', # 'path_ref': '../../../examples/hl_lhc_collision_nobb_b4', # 'type_ref': 'sixtrack', -# 'rtol': 3e-5, +# 'rtol': 8e-5, # 'atol': 1e-12, -# 'strict': True, +# 'strict': False, # }, # { # 'test_name': 'B4 - pymask xline vs pymask sixtrack input', From d93eada0558533538c196f567dd3d329bc829f43 Mon Sep 17 00:00:00 2001 From: giadarol Date: Mon, 5 Jul 2021 23:58:08 +0200 Subject: [PATCH 30/31] Added xline test in t005 --- .../checks_and_doc/t005_check_crabs.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py b/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py index 29c75b8..af9fe25 100644 --- a/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py +++ b/python_examples/hl_lhc_collisions_python/checks_and_doc/t005_check_crabs.py @@ -46,12 +46,14 @@ path_test = '../' type_test = 'sixtrack' +path_test = '../xline/line_bb_dipole_not_cancelled.json' +type_test = 'xline' + def prepare_line(path, input_type): if input_type == 'xline': # Load xline machine - with open(path, "rb") as fid: - ltest = xline.Line.from_dict(pickle.load(fid)) + ltest = xline.Line.from_json(path) elif input_type == 'sixtrack': print('Build xline from sixtrack input:') sixinput_test = sixtracktools.sixinput.SixInput(path) From 4db4aa444d598337ef45bb4512b50cbf40dddc21 Mon Sep 17 00:00:00 2001 From: sterbini Date: Tue, 6 Jul 2021 12:37:33 +0200 Subject: [PATCH 31/31] Adding assert in 000 files --- .../hl_lhc_collisions_python/000_pymask.py | 3 +- .../hl_lhc_collisions_python/001_reload.madx | 8 -- .../hl_lhc_collisions_python/001_reload.py | 103 ------------------ python_examples/ions_python/000_pymask.py | 18 +-- python_examples/ions_python/config.py | 3 +- .../run3_collisions_python/000_pymask.py | 18 +-- .../run3_collisions_python/config.py | 21 ++-- 7 files changed, 35 insertions(+), 139 deletions(-) delete mode 100644 python_examples/hl_lhc_collisions_python/001_reload.madx delete mode 100644 python_examples/hl_lhc_collisions_python/001_reload.py diff --git a/python_examples/hl_lhc_collisions_python/000_pymask.py b/python_examples/hl_lhc_collisions_python/000_pymask.py index d9146fe..92fd58f 100644 --- a/python_examples/hl_lhc_collisions_python/000_pymask.py +++ b/python_examples/hl_lhc_collisions_python/000_pymask.py @@ -288,6 +288,7 @@ # For B1, to be generalized for B4 if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + assert 'b4' not in mode filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] bunch_to_track = configuration['beambeam_config']['bunch_to_track'] bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( @@ -346,7 +347,7 @@ mad_track.set_variables_from_dict( params=configuration['pars_for_legacy_bb_macros']) mad_track.set_variables_from_dict( - params={f'par_nho_ir{ir}':bbconfig['numberOfHOSlices'] + params={f'par_nho_ir{ir}': bbconfig['numberOfHOSlices'] for ir in [1,2,5,8]}) mad_track.input("call, file='modules/module_03_beambeam.madx';") diff --git a/python_examples/hl_lhc_collisions_python/001_reload.madx b/python_examples/hl_lhc_collisions_python/001_reload.madx deleted file mode 100644 index d8ca1b6..0000000 --- a/python_examples/hl_lhc_collisions_python/001_reload.madx +++ /dev/null @@ -1,8 +0,0 @@ -set,format=".15g"; -call,file="saved_lhcb1.madx"; -use,sequence="lhcb1"; -twiss; -readtable,file="error_all.tfs", table="errtab"; -seterr,table="errtab"; -twiss; -sixtrack,cavall,radius=0.017; diff --git a/python_examples/hl_lhc_collisions_python/001_reload.py b/python_examples/hl_lhc_collisions_python/001_reload.py deleted file mode 100644 index 440d2ad..0000000 --- a/python_examples/hl_lhc_collisions_python/001_reload.py +++ /dev/null @@ -1,103 +0,0 @@ -import os, shutil - -import numpy as np - -from cpymad.madx import Madx - -def norm(x): - return np.sqrt(np.sum(np.array(x) ** 2)) - -def trim_last_zeros(a): - zz=np.where(a!=0)[0] - if len(zz)>0: - return a[:zz[-1]+1] - else: - return a - - - -def fc_to_fort(): - for iff in [2,8,16,34]: - os.system(f"rm fort.{iff}") - try: - shutil.copy(f"fc.{iff}", f"fort.{iff}") - except Exception as ex: - print(ex) - print(f"fc.{iff} not found!") - - with open("fort.3", "w") as fout: - with open("checks_and_doc/fort_parts/fort_beginning.3", "r") as fid_fort3b: - fout.write(fid_fort3b.read()) - with open("fc.3", "r") as fid_fc3: - fout.write(fid_fc3.read()) - with open("checks_and_doc/fort_parts/fort_end.3", "r") as fid_fort3e: - fout.write(fid_fort3e.read()) - - - -mad=Madx() - -mad.call("final_seq.madx") -mad.use(sequence="lhcb1") -mad.twiss() -mad.readtable(file="final_errors.tfs", table="errtab") -mad.seterr(table="errtab") -mad.set(format=".15g") -mad.twiss() -mad.sixtrack(cavall=True,radius=0.017,max_mult_ord=18,mult_auto_off=True) -fc_to_fort() - -import sixtracktools -import xline - -sixin=sixtracktools.sixinput.SixInput(".") -linesix=xline.Line.from_sixinput(sixin) -linemad=xline.Line.from_madx_sequence(mad.sequence.lhcb1,apply_madx_errors=True) - -#linemad=xline.Line.from_madx_sequence(mad.sequence.lhcb1) - -for ll in (linesix, linemad): - ll.remove_inactive_multipoles(inplace=True) - ll.remove_zero_length_drifts(inplace=True) - ll.merge_consecutive_drifts(inplace=True) - ll.merge_consecutive_multipoles(inplace=True) - -for lb,li in [("fmad",linemad),("fsix",linesix)]: - with open(lb,"w") as fh: - for el,en in zip(li.elements,li.element_names): - fh.write(str(en)+"\n") - fh.write(str(el)+"\n") - -len(linesix.elements) -len(linemad.elements) - - -for ii, (ee_test, ee_six, nn_test, nn_six) in enumerate( - zip(linemad.elements, linesix.elements, linemad.element_names, linesix.element_names) -): - assert type(ee_test) == type(ee_six) - dtest = ee_test.to_dict(keepextra=True) - dsix = ee_six.to_dict(keepextra=True) - for kk in dtest.keys(): - if dtest[kk] == dsix[kk]: - continue - try: - if hasattr(dtest[kk],"__iter__"): - atest=trim_last_zeros(np.array(dtest[kk])) - asix=trim_last_zeros(np.array(dsix[kk])) - else: - atest=(np.array(dtest[kk])) - asix=(np.array(dsix[kk])) - diff_rel = norm(atest - asix) / norm(dtest[kk]) - except ZeroDivisionError: - diff_rel = 100.0 - if diff_rel < 1e-9: - continue - diff_abs = norm(atest - asix) - if diff_abs > 0 and kk!="length": - print(f"{nn_test}[{kk}] - test:{dtest[kk]} six:{dsix[kk]}") - if diff_abs < 1e-12: - continue - - - diff --git a/python_examples/ions_python/000_pymask.py b/python_examples/ions_python/000_pymask.py index 0425057..92fd58f 100644 --- a/python_examples/ions_python/000_pymask.py +++ b/python_examples/ions_python/000_pymask.py @@ -288,6 +288,7 @@ # For B1, to be generalized for B4 if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + assert 'b4' not in mode filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] bunch_to_track = configuration['beambeam_config']['bunch_to_track'] bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( @@ -338,6 +339,7 @@ # Legacy bb macros if enable_bb_legacy: + bbconfig = configuration['beambeam_config'] assert(beam_to_configure == 1) assert(not(track_from_b4_mad_instance)) assert(not(enable_bb_python)) @@ -345,7 +347,7 @@ mad_track.set_variables_from_dict( params=configuration['pars_for_legacy_bb_macros']) mad_track.set_variables_from_dict( - params={f'par_nho_ir{ir}':configuration['numberOfHOSlices'] + params={f'par_nho_ir{ir}': bbconfig['numberOfHOSlices'] for ir in [1,2,5,8]}) mad_track.input("call, file='modules/module_03_beambeam.madx';") @@ -495,7 +497,7 @@ output_folder='./', reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), - reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( @@ -522,9 +524,9 @@ pickle.dump(optics_orbit_start_ring, fid) -############################# -# Generate pysixtrack lines # -############################# +################### +# Generate xlines # +################### if enable_bb_legacy: print('xline is not generated with bb legacy macros') @@ -536,8 +538,6 @@ pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) - - ################################### # Save final twiss # ################################### @@ -560,6 +560,6 @@ ############################# # Save sequence and errors # ############################# - -pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') +# N.B. this erases the errors +# pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') diff --git a/python_examples/ions_python/config.py b/python_examples/ions_python/config.py index 4aa81f8..d97b213 100644 --- a/python_examples/ions_python/config.py +++ b/python_examples/ions_python/config.py @@ -7,6 +7,7 @@ 'tools': 'tracking_tools/tools', 'beambeam_macros': 'tracking_tools/beambeam_macros', 'errors': 'tracking_tools/errors', + 'optics_repository' : '/afs/cern.ch/eng/lhc/optics', }, # Mode - choose between: @@ -23,7 +24,7 @@ 'mode' : 'b1_with_bb', # Optics file - 'optics_file' : '/afs/cern.ch/eng/lhc/optics/runII/2018/ION/opticsfile.21', + 'optics_file' : 'optics_repository/runII/2018/ION/opticsfile.21', # Enable checks 'check_betas_at_ips' : True, diff --git a/python_examples/run3_collisions_python/000_pymask.py b/python_examples/run3_collisions_python/000_pymask.py index 0425057..92fd58f 100644 --- a/python_examples/run3_collisions_python/000_pymask.py +++ b/python_examples/run3_collisions_python/000_pymask.py @@ -288,6 +288,7 @@ # For B1, to be generalized for B4 if 'filling_scheme_json' in configuration['beambeam_config'].keys(): + assert 'b4' not in mode filling_scheme_json = configuration['beambeam_config']['filling_scheme_json'] bunch_to_track = configuration['beambeam_config']['bunch_to_track'] bb_schedule_to_track_b1 = ost.create_bb_shedule_to_track( @@ -338,6 +339,7 @@ # Legacy bb macros if enable_bb_legacy: + bbconfig = configuration['beambeam_config'] assert(beam_to_configure == 1) assert(not(track_from_b4_mad_instance)) assert(not(enable_bb_python)) @@ -345,7 +347,7 @@ mad_track.set_variables_from_dict( params=configuration['pars_for_legacy_bb_macros']) mad_track.set_variables_from_dict( - params={f'par_nho_ir{ir}':configuration['numberOfHOSlices'] + params={f'par_nho_ir{ir}': bbconfig['numberOfHOSlices'] for ir in [1,2,5,8]}) mad_track.input("call, file='modules/module_03_beambeam.madx';") @@ -495,7 +497,7 @@ output_folder='./', reference_num_particles_sixtrack=( mad_track.sequence[sequence_to_track].beam.npart), - reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, + reference_particle_charge_sixtrack=mad_track.sequence[sequence_to_track].beam.charge, emitnx_sixtrack_um=( mad_track.sequence[sequence_to_track].beam.exn), emitny_sixtrack_um=( @@ -522,9 +524,9 @@ pickle.dump(optics_orbit_start_ring, fid) -############################# -# Generate pysixtrack lines # -############################# +################### +# Generate xlines # +################### if enable_bb_legacy: print('xline is not generated with bb legacy macros') @@ -536,8 +538,6 @@ pickle_lines_in_folder=xline_fol_name, skip_mad_use=True) - - ################################### # Save final twiss # ################################### @@ -560,6 +560,6 @@ ############################# # Save sequence and errors # ############################# - -pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') +# N.B. this erases the errors +# pm.save_mad_sequence_and_error(mad_track, sequence_to_track, filename='final') diff --git a/python_examples/run3_collisions_python/config.py b/python_examples/run3_collisions_python/config.py index b954ae5..65c8233 100644 --- a/python_examples/run3_collisions_python/config.py +++ b/python_examples/run3_collisions_python/config.py @@ -7,6 +7,7 @@ 'tools': 'tracking_tools/tools', 'beambeam_macros': 'tracking_tools/beambeam_macros', 'errors': 'tracking_tools/errors', + 'optics_repository' : '/afs/cern.ch/eng/lhc/optics', }, # Mode - choose between: @@ -23,7 +24,7 @@ 'mode' : 'b1_with_bb', # Optics file - 'optics_file' : '/afs/cern.ch/eng/lhc/optics/runIII/RunIII_dev/2022_V1/PROTON/opticsfile.29', + 'optics_file' : 'optics_repository/runIII/RunIII_dev/2022_V1/PROTON/opticsfile.29', # Enable checks 'check_betas_at_ips' : True, @@ -69,12 +70,16 @@ 'nco_IP8' : 2376, # Beam-beam parameters (used by python tools - NOT by legacy macros) - 'numberOfLRPerIRSide' : [25, 20, 25, 20], - 'bunch_spacing_buckets' : 10, - 'numberOfHOSlices' : 11, - 'bunch_population_ppb' : None, - 'sigmaz_m' : None, - 'z_crab_twiss' : 0., + 'beambeam_config' : + { + 'numberOfLRPerIRSide' : [25, 20, 25, 20], + 'bunch_spacing_buckets' : 10, + 'numberOfHOSlices' : 11, + 'bunch_num_particles' : None, + 'bunch_particle_charge' : None, + 'sigmaz_m' : None, + 'z_crab_twiss' : 0., + }, # Match tunes and chromaticities including beam-beam effects 'match_q_dq_with_bb' : False, # should be off at collision @@ -82,7 +87,7 @@ # Enable crab cavities 'enable_crabs' : False, - # N. iterations coupling correction + # N. iterations for coupling correction 'N_iter_coupling' : 2, # Value to be added to linear coupling knobs (on sequence_to_track)