def extract(infile, col_1, col_2, col_3=0): get = GetData(infile) data = get.data_column() turn = data[col_1] value = data[col_2] if col_3 == 0: return turn, value else: value_2 = data[col_3] return turn, value, value_2
def get_turns(infile): get = GetData(infile) data = get.data_column() x = np.asarray(data[0], dtype='float64') y = np.asarray(data[2], dtype='float64') if 'b1' in infile: name = txt.replace('b1.txt', '').upper() elif 'b2' in infile: name = txt.replace('b2.txt', '').upper() elif infile == 'data_turn.txt': name = 'Collimation system' return x, y, name
def get_data(infile): get = GetData(infile) data = get.data_column() x = np.asarray(data[0]) xp = data[1] y = data[2] yp = data[3] sigma_x = np.std(x) sigma_y = np.std(y) mu, std = norm.fit(x) return sigma_x, sigma_y, mu, std, x, y
def get_ap(infile): if os.stat(infile).st_size == 0: print '>> ' + infile + ' file is empty' return 0 else: get = GetData(infile) data = get.data_column(dtype='string') name = data[0] s = np.asarray(data[1], dtype='float64') l = np.asarray(data[2], dtype='float64') aper1 = np.asarray(data[3], dtype='float64') aper2 = np.asarray(data[4], dtype='float64') aper3 = np.asarray(data[5], dtype='float64') aper4 = np.asarray(data[6], dtype='float64') apertype = np.asarray(data[7]) return name, s, l, aper1, aper2, aper3, aper4, apertype
def get_twiss(infile): if os.stat(infile).st_size == 0: print '>> ' + infile + ' file is empty' return 0 else: get = GetData(infile) data = get.data_column(dtype='string') name = data[1] s = np.asarray(data[2]) l = np.asarray(data[3]) x = np.asarray(data[4]) y = np.asarray(data[5]) betx = np.asarray(data[6]) bety = np.asarray(data[7]) alfx = np.asarray(data[8]) alfy = np.asarray(data[9]) dx = np.asarray(data[12]) dy = np.asarray(data[13]) px = np.asarray(data[16]) py = np.asarray(data[17]) return name, s, l, x, y, betx, bety, alfx, alfy, dx, dy, px, py
def get_dist(data_file, dir_core, dir_tail, core_weight, tail_weight): if data_file == 'loss_maps.txt': col_1 = 1 col_2 = 3 elif data_file == 'aperture.txt': col_1 = 0 col_2 = 2 x = [] y = [] if os.path.isfile(dir_core + '/' + data_file) and os.path.isfile(dir_tail + '/' + data_file): print ' ' print '>> File ' + data_file + ' present for core and tail' get_core = GetData(dir_core + '/' + data_file) data_core = get_core.data_column(dtype='string') xc_coll = data_core[col_1] yc_coll = data_core[col_2] get_tail = GetData(dir_tail + '/' + data_file) data_tail = get_tail.data_column(dtype='string') xt_coll = data_tail[col_1] yt_coll = data_tail[col_2] for i, k in zip(xc_coll, yc_coll): for j, l in zip(xt_coll, yt_coll): if i == j: x.append(float(i)) y.append(core_weight * float(k) + tail_weight * float(l)) xc_coll.remove(i) yc_coll.remove(k) xt_coll.remove(j) yt_coll.remove(l) for i, k in zip(xc_coll, yc_coll): x.append(float(i)) y.append(core_weight * float(k)) for j, l in zip(xt_coll, yt_coll): x.append(float(j)) y.append(tail_weight * float(l)) elif os.path.isfile(dir_core + '/' + data_file) == False: print ' ' print '>> File ' + data_file + ' present only for tail' get_tail = GetData(dir_tail + '/' + data_file) data_tail = get_tail.data_column(dtype='string') xt_coll = data_tail[col_1] yt_coll = data_tail[col_2] for j, l in zip(xt_coll, yt_coll): x.append(float(j)) y.append(tail_weight * float(l)) elif os.path.isfile(dir_tail + '/' + data_file) == False: print ' ' print '>> File ' + data_file + ' present only for core' get_core = GetData(dir_core + '/' + data_file) data_core = get_core.data_column(dtype='string') xc_coll = data_core[col_1] yc_coll = data_core[col_2] for j, l in zip(xc_coll, yc_coll): x.append(float(j)) y.append(core_weight * float(l)) return x, y
# -------------------------------------------------------------------- m_p = 938.272046e6 # Proton mass [eV/c^2] c = 299792458 # Speed of light [m/s] gamma_rel = energy/m_p # Relativistic gamma beta_rel = np.sqrt(1-(1/gamma_rel**2)) # Relativistic beta emittance_geom = emittance_norm/(beta_rel*gamma_rel) sigma_x = np.sqrt(emittance_geom*beta_x) sigma_y = np.sqrt(emittance_geom*beta_y) with open(outfile,'w') as f: for item in os.listdir(rootdir): if os.path.isdir(item): infile = os.path.join(os.path.join(rootdir, item), 'dump.txt') phase = int(item.strip('job_')) get = GetData(infile) data_dict = get.data_column(column=0, regex=r'1\b') for turn, x, xp, y, yp in zip(data_dict[1], data_dict[3], data_dict[4], data_dict[5], data_dict[6]): x = float(data_dict[3])*1e-3 xp = float(data_dict[4])*1e-3 y = float(data_dict[5])*1e-3 yp = float(data_dict[6])*1e-3 # Sigma matrix for X term_1_x = mean(np.multiply(x,x)) - np.multiply(mean(x), mean(x)) term_2_x = mean(np.multiply(x,xp)) - np.multiply(mean(x),mean(xp)) term_3_x = mean(np.multiply(xp,x)) - np.multiply(mean(xp), mean(x)) term_4_x = mean(np.multiply(xp,xp)) - np.multiply(mean(xp),mean(xp)) # Sigma matrix for Y term_1_y = mean(np.multiply(y,y)) - np.multiply(mean(y), mean(y)) term_2_y = mean(np.multiply(y,yp)) - np.multiply(mean(y),mean(yp)) term_3_y = mean(np.multiply(yp,y)) - np.multiply(mean(yp), mean(y)) term_4_y = mean(np.multiply(yp,yp)) - np.multiply(mean(yp),mean(yp))
#!/usr/bin/env python import subprocess import numpy as np from util import GetData infile = 'seed_old.txt' get = GetData(infile) data = get.data_column(dtype='string') seed = data[3] jobs = range(1, 500 + 1) for i, j in zip(seed, jobs): subprocess.call(['generate_distribution.py','19968','7e12','HL_coll','False', '1','0.9','2.5e-6','2.5e-6','0.003485','-0.000764','0.150739','0.150235','-7.5e-4','0','0','295e-6','0.003652','0.000517','0.0755','1.13e-4',i]) subprocess.Popen(['rename.sh %s' % j], shell=True)
term_4_y = np.zeros(total_turns) det_x = np.zeros(total_turns) em_x = np.zeros(total_turns) det_y = np.zeros(total_turns) em_y = np.zeros(total_turns) mean_x = np.zeros(total_turns) mean_y = np.zeros(total_turns) # ------------------------------------------------------------------------------ # Extraction and treatment of data # ------------------------------------------------------------------------------ get = GetData(infile) data = get.data_column() turns = data[1] x_tot = data[3] xp_tot = data[4] y_tot = data[5] yp_tot = data[6] z_tot = data[7] e_tot = data[8] tset=list(set(turns)) def get_turns(coord, turns): d = {} for t in set(turns): d[t] = []
else: print '>> Please input B1 or B2 as first argument' plt.subplots_adjust(left=0.16, bottom=0.19, right=0.94, top=0.88) plt.savefig('loss_map.png', dpi=1000) plt.savefig('loss_map.eps', format='eps', dpi=1000) plt.clf() elif len(sys.argv) == 2: files = glob.glob('*.txt') for txt in files: if txt == 'loss_maps.txt': print ' ' print '>> File ' + txt + ' present' get = GetData(txt) data = get.data_column(dtype='string') x_coll = np.asarray(data[1], dtype='float64') y_coll = np.asarray(data[3], dtype='float64') elif txt == 'aperture.txt': print ' ' print '>> File ' + txt + ' present' get = GetData(txt) data = get.data_column(dtype='string') x_ap = np.asarray(data[0], dtype='float64') y_ap = np.asarray(data[2], dtype='float64') # ------------------------------------------------------------------------------ # Plotting lossmaps # ------------------------------------------------------------------------------ if os.path.exists('loss_maps.txt') == False and os.path.exists( 'aperture.txt') == False: sys.exit('>> No losses')
import numpy as np import matplotlib from matplotlib import pyplot as plt from matplotlib import rc from matplotlib import rcParams from util import GetData # ------------------------------------------------------------------------------ # Constant voltage data loading # ------------------------------------------------------------------------------ infile = sys.argv[1] # title = sys.argv[2] get = GetData(infile) data = get.data_column() turns = np.asarray(data[0], dtype='float64') dx = np.asarray(data[1], dtype='float64') dy = np.asarray(data[2], dtype='float64') cx = np.asarray(data[3], dtype='float64') cy = np.asarray(data[4], dtype='float64') # ------------------------------------------------------------------------------ # PLOTTING # ------------------------------------------------------------------------------ DPI = 500 textwidth = 3.25 font_spec = { "font.family": "serif", # use as default font # "font.serif": ["New Century Schoolbook"], # custom serif font
#!/usr/bin/env python import sys import numpy as np from matplotlib import pyplot as plt from matplotlib import rc from matplotlib import rcParams from util import GetData infile_before = 'before.txt' get_before = GetData(infile_before) my_data_before = get_before.data_column(column=1, regex=r'2\b') yp_before = my_data_before[6] z_before = my_data_before[7] delta_before = my_data_before[8] infile_after = 'after.txt' get_after = GetData(infile_after) my_data_after = get_after.data_column(column=1, regex=r'2\b') yp_after = my_data_after[6] z_after = my_data_after[7] delta_after = my_data_after[8] y = [] x = [] for a, b, c, d, e, f in zip(yp_before, delta_before, z_before, yp_after, delta_after, z_after): term_1 = d / (1 + e) term_2 = a / (1 + b) y.append((term_1 - term_2) * 7 * 1e12)
# -------------------------------------------------------------------- m_p = 938.272046e6 # Proton mass [eV/c^2] c = 299792458 # Speed of light [m/s] gamma_rel = energy / m_p # Relativistic gamma beta_rel = np.sqrt(1 - (1 / gamma_rel ** 2)) # Relativistic beta emittance_geom = emittance_norm / (beta_rel * gamma_rel) sigma_x = np.sqrt(emittance_geom * beta_x) sigma_y = np.sqrt(emittance_geom * beta_y) with open(outfile, "w") as f: for item in os.listdir(rootdir): if os.path.isdir(item): infile = os.path.join(os.path.join(rootdir, item), "dump.txt") phase = int(item.strip("job_")) get = GetData(infile) data_dict = get.data_column(column=0, regex=r"1\b") for turn, x, xp, y, yp in zip(data_dict[1], data_dict[3], data_dict[4], data_dict[5], data_dict[6]): x = float(data_dict[3]) * 1e-3 xp = float(data_dict[4]) * 1e-3 y = float(data_dict[5]) * 1e-3 yp = float(data_dict[6]) * 1e-3 # Sigma matrix for X term_1_x = mean(np.multiply(x, x)) - np.multiply(mean(x), mean(x)) term_2_x = mean(np.multiply(x, xp)) - np.multiply(mean(x), mean(xp)) term_3_x = mean(np.multiply(xp, x)) - np.multiply(mean(xp), mean(x)) term_4_x = mean(np.multiply(xp, xp)) - np.multiply(mean(xp), mean(xp)) # Sigma matrix for Y term_1_y = mean(np.multiply(y, y)) - np.multiply(mean(y), mean(y)) term_2_y = mean(np.multiply(y, yp)) - np.multiply(mean(y), mean(yp)) term_3_y = mean(np.multiply(yp, y)) - np.multiply(mean(yp), mean(y)) term_4_y = mean(np.multiply(yp, yp)) - np.multiply(mean(yp), mean(yp))
turns = int(sys.argv[2]) simulated_particles = int(sys.argv[1]) * sixtrack_particle_limit beam = sys.argv[3] print ' ' print 'Number of simulated particles: ', simulated_particles, '(100 %)' if not os.path.exists('tot/' + str(simulated_particles)): os.makedirs('tot/' + str(simulated_particles)) # ------------------------------------------------------------------------------ # Associating name with collimator ID # ------------------------------------------------------------------------------ infile_2 = 'coll_summary.dat' get_2 = GetData(infile_2) get_2 = GetData(infile_2) data_2 = get_2.data_column(dtype='string') names_2 = data_2[1] numbers = data_2[0] numbers_dict = {} for i in range(len(names_2)): numbers_dict[float(numbers[i])] = names_2[i] # ------------------------------------------------------------------------------ # Associating name with position # ------------------------------------------------------------------------------ if beam == 'B1': infile_3 = 'CollPositionsHL.b1.dat' elif beam == 'B2': infile_3 = 'CollPositions.b2.dat'