def qp_hi_compare_pzz(h_fig, h_axs, out_num=1): file1 = outfile.OutFile( code_name='hipace', path= '/beegfs/desy/group/fla/plasma/OSIRIS-runs/2D-runs/MZ/hi_qp_compare/hi4', field_name='raw', spec_name='driver', use_num_list=True, out_num=out_num) file1.open() file1.read_raw_p1() file1.read_raw_x1() ax_range = [[np.min(file1._raw_p1), np.max(file1._raw_p1)], [np.min(file1._raw_x1), np.max(file1._raw_x1)]] h_fig, h_ax = file1.plot_raw_hist2D(h_fig, h_axs[1], dims='p1x1', range=ax_range, if_log_colorbar=True, if_colorbar=True) hi_data = file1._data file1.close() file1 = outfile.OutFile( code_name='quickpic', path= '/beegfs/desy/group/fla/plasma/OSIRIS-runs/2D-runs/MZ/qp_hi_compare/qp4', field_name='raw', spec_name='Beam0001', use_num_list=True, out_num=out_num) file1.open(cell_size_qp_raw=np.array([0.03125, 0.03125, 0.01953125])) file1.read_raw_q() file1.read_raw_x1() file1.read_raw_p1() file1._raw_x1 = 10. - file1._raw_x1 h_fig, h_ax = file1.plot_raw_hist2D(h_fig, h_axs[0], dims='p1x1', range=ax_range, if_reread=False, if_log_colorbar=True, if_colorbar=True) file1.close() file1._data = hi_data - file1._data file1.plot_data(h_fig, h_axs[2]) file1.close() h_axs[2].set_title('Difference')
def __init__(self, code_name = 'osiris', simulation_path = None, frame_folder = None, plot_type = 'laser_driven', plot_spec_name=None, average='', use_num_list = False, start_num = 0, stride_num=1, count_num=1, driver_spec_name='driver', driver_vmin=None, driver_vmax=None, background_spec_name='e', background_vmin=None, background_vmax=None, trail_spec_name=None, trail_vmin=None, trail_vmax=None, if_e1=False, e1_multiple=1., if_psi=False, psi_multiple=1., if_driver_cm=False, if_trail_cm=False, dir=2, save_type='png', max_missing_file=0): self.code_name = code_name self.simulation_path = simulation_path if frame_folder is None: if code_name == 'osiris': frame_pref = 'os_Frames' elif code_name == 'hipace': frame_pref = 'hi_Frames' elif code_name == 'quickpic': frame_pref = 'qp_Frames' else: raise NotImplementedError('code_name {} not implemented.'.format(code_name)) if dir==0: frame_surf = 'slice_x-y' elif dir==1: frame_surf = 'slice_y-z' elif dir==2: frame_surf = 'slice_x-z' else: raise ValueError('dir should be 1, 2 or 3.') frame_folder = frame_pref+'/'+frame_surf self.frame_path = simulation_path+'/'+frame_folder # when use_num_list is True, scan the folder and generate a list of all available fils. self.use_num_list = use_num_list self.start_num = start_num self.stride_num = stride_num self.count_num = count_num self.plot_type = plot_type # For phasespace plots, i.e. plot_type = 'p1x1' or so on, one has to give plot_spec_name self.plot_spec_name = plot_spec_name self.driver_spec_name = driver_spec_name self.driver_vmin = driver_vmin self.driver_vmax = driver_vmax self.background_spec_name = background_spec_name self.background_vmin = background_vmin self.background_vmax = background_vmax self.trail_spec_name = trail_spec_name self.trail_vmin = trail_vmin self.trail_vmax = trail_vmax self.if_e1 = if_e1 self.e1_multiple = e1_multiple self.if_psi = if_psi self.psi_multiple = psi_multiple # if_driver_cm = True means plot the lineout of driver beam center of mass in the beam driven case self.if_driver_cm = if_driver_cm # if_trail_cm = True means plot the lineout of trail beam center of mass self.if_trail_cm = if_trail_cm self.dir = dir self.save_type = save_type #allowed number of missing files when doing plot loop. In HiPACE sometimes there are missing output files. self.max_missing_file = max_missing_file # Initialize outfile object, especially initialize avail_num_list if use_num_list. if ('beam_driven' == self.plot_type) or ('laser_driven' == self.plot_type): self.outfile = outfile.OutFile(code_name = code_name, path=simulation_path, field_name='charge', average=average, spec_name=background_spec_name, use_num_list = use_num_list, out_num=start_num) else: self.outfile = outfile.OutFile(code_name = code_name, path=simulation_path, field_name='raw', spec_name=plot_spec_name, use_num_list = use_num_list, out_num=start_num)
def get_W_vs_t(path, field_name='e3', species_name='', average='', cyl_m='False', start=0, count=0, stride=1): '''Plot the W evolution''' if not isinstance(path, str): raise TypeError('The path should be a string!') if not isinstance(field_name, str): raise TypeError('The field name should be a string!') if not isinstance(species_name, str): raise TypeError('The species name should be a string!') if not isinstance(start, int): raise TypeError('The start should be a integer!') if not isinstance(count, int): raise TypeError('The count should be a integer!') if not isinstance(stride, int): raise TypeError('The stride should be a integer!') if cyl_m: field_name=field_name+'_cyl_m' os_file = outfile.OutFile(path=path, field_name=field_name, average=average, cyl_m_num=1, cyl_m_re_im='re') if start not in range(10**os_file.digit_num): raise ValueError('start = {0} not in range!'.format(start)) if count not in range(10**os_file.digit_num): raise ValueError('count = {0} not in range!'.format(count)) if stride not in range(10**os_file.digit_num): raise ValueError('stride = {0} not in range!'.format(stride)) W_array = list() t_array = list() a_array = list() try: popt = None for i in range(start, start+count*stride, stride): os_file.out_num = i try: os_file.open() except IOError: print('Warning: unable to open file \'{0}\'. Iteration breaks.'.format(os_file.path_filename)) break os_file.read_data_project(if_abs = True) popt, h_fig, h_ax = os_file.fit_for_W(guess_values=popt) W_array.append(os_file._W) t_array.append(os_file.time) a_array.append(os_file._a) plt.show(block=False) time.sleep(1) plt.close(h_fig) #h_ax=None os_file.close() except KeyboardInterrupt: print('Keyboard interruption occurs at file \'{0}\'. Iteration breaks.'.format(os_file.path_filename)) #W_max = max(W_array) #W_max_i = W_array.index(W_max) #print('W_max = {0} at index {1}, t = {2}'.format(W_max, W_max_i, t_array[W_max_i])) #fig1 = plt.figure() #ax1 = fig1.add_subplot(111) #ax1.plot(t_array, W_array) #plt.xlabel('t [$\\omega_p$]') #plt.ylabel('w [$c / \\omega_p$]') #return ([[i*stride+start, t_array[i], W_array[i], a_array[i]] for i in range(len(t_array))], fig1) return ([[i*stride+start, t_array[i], W_array[i], a_array[i]] for i in range(len(t_array))])
def qp_hi_compare_Ez(h_fig, h_axs, out_num=1): file1 = outfile.OutFile( code_name='quickpic', path= '/beegfs/desy/group/fla/plasma/OSIRIS-runs/2D-runs/MZ/qp_hi_compare/qp5', field_name='e1', use_num_list=True, out_num=out_num, fld_slice=1) file1.open() file1.read_data() file1.plot_data(h_fig, h_axs[0]) data_qp = file1._data file1.read_data_lineout() lineout_qp = file1._data file1.close() file1 = outfile.OutFile( code_name='hipace', path= '/beegfs/desy/group/fla/plasma/OSIRIS-runs/2D-runs/MZ/hi_qp_compare/hi5', field_name='e1', use_num_list=True, out_num=out_num) file1.open() file1.read_data_slice() file1.plot_data(h_fig, h_axs[1]) file1._data = file1._data - np.fliplr(data_qp) file1.plot_data(h_fig, h_axs[2]) file1.read_data_lineout() file1.plot_data(h_fig, h_axs[2], c='k', ls='--', label='HiPACE') file1._data = np.flip(lineout_qp) file1.plot_data(h_fig, h_axs[2], c='r', ls=':', label='QuickPIC') file1.close() h_axs[2].legend() h_axs[2].set_title('Difference')
def __init__(self, code_name = 'osiris', simulation_path = None, frame_folder = 'Frames', plot_type = 'laser_driven', plot_spec_name=None, average='', use_num_list = False, start_num = 0, stride_num=1, count_num=1, driver_spec_name='driver', driver_vmin=None, driver_vmax=None, background_spec_name='e', background_vmin=None, background_vmax=None, trail_spec_name=None, trail_vmin=None, trail_vmax=None, if_e1=False, if_psi=False, if_driver_cm=False, if_trail_cm=False, dir=2, save_type='png', max_missing_file=0): self.code_name = code_name self.simulation_path = simulation_path self.frame_path = simulation_path+'/'+frame_folder # when use_num_list is True, scan the folder and generate a list of all available fils. self.use_num_list = use_num_list self.start_num = start_num self.stride_num = stride_num self.count_num = count_num self.plot_type = plot_type # For phasespace plots, i.e. plot_type = 'p1x1' or so on, one has to give plot_spec_name self.plot_spec_name = plot_spec_name self.driver_spec_name = driver_spec_name self.driver_vmin = driver_vmin self.driver_vmax = driver_vmax self.background_spec_name = background_spec_name self.background_vmin = background_vmin self.background_vmax = background_vmax self.trail_spec_name = trail_spec_name self.trail_vmin = trail_vmin self.trail_vmax = trail_vmax self.if_e1 = if_e1 self.if_psi = if_psi # if_driver_cm = True means plot the lineout of driver beam center of mass in the beam driven case self.if_driver_cm = if_driver_cm # if_trail_cm = True means plot the lineout of trail beam center of mass self.if_trail_cm = if_trail_cm self.dir = dir self.save_type = save_type #allowed number of missing files when doing plot loop. In HiPACE sometimes there are missing output files. self.max_missing_file = max_missing_file # Initialize outfile object, especially initialize avail_num_list if use_num_list. if ('beam_driven' == self.plot_type) or ('laser_driven' == self.plot_type): self.outfile = outfile.OutFile(code_name = code_name, path=simulation_path, field_name='charge', average=average, spec_name=background_spec_name, use_num_list = use_num_list, out_num=start_num) else: self.outfile = outfile.OutFile(code_name = code_name, path=simulation_path, field_name='raw', spec_name=plot_spec_name, use_num_list = use_num_list, out_num=start_num)
def plot_beam_parameters_vs_t(path, species_name, n0_per_cc, code_name='osiris', gamma_threshold=1., use_num_list=False, start=0, count=0, stride=1, t_offset=0., max_missing_file=0, gamma_spread_method="lfit", h_f=None, charge_abs=False, linestyle='-', label=None, cell_size_qp_raw=None): ''' max_missing_file is the maximum continue files missing allowed. HiPACE sometimes fails in dumping output files. ''' t_array = list() q_pC_array = list() emittance_x_array = list() emittance_y_array = list() center_gamma_array = list() spread_gamma_array = list() Courant_Snyder_parameter_alpha_x = list() Courant_Snyder_parameter_alpha_y = list() Courant_Snyder_parameter_beta_x = list() Courant_Snyder_parameter_beta_y = list() Courant_Snyder_parameter_gamma_x = list() Courant_Snyder_parameter_gamma_y = list() missing_file = 0 outfile_object = outfile.OutFile(code_name=code_name, path=path, field_name='raw', spec_name=species_name, use_num_list=use_num_list) for i in range(start, start + count * stride, stride): try: outfile_object.out_num = i except KeyError: print('Reaching the end of number list. Finishing...') break print('File number {}'.format(outfile_object.actual_num)) try: outfile_object.open(cell_size_qp_raw=cell_size_qp_raw) #set missing_file = 0 if success missing_file = 0 except IOError as err: missing_file += 1 if missing_file > max_missing_file: print( 'Warning: Cannot open file number {}. Iteration breaks. Exception message:\n{}' .format(i, err)) break else: print('Warning: File missing. Exception message:\n{}'.format( err)) continue t, q_pC, emittances, center_gamma, spread_gamma, Courant_Snyder_parameters = get_beam_parameters( outfile_object, n0_per_cc, gamma_threshold, gamma_spread_method=gamma_spread_method) outfile_object.close() t_array.append(t + t_offset) q_pC_array.append(q_pC) emittance_x_array.append(emittances[0]) emittance_y_array.append(emittances[1]) center_gamma_array.append(center_gamma) spread_gamma_array.append(spread_gamma) Courant_Snyder_parameter_alpha_x.append( Courant_Snyder_parameters[0][0]) Courant_Snyder_parameter_alpha_y.append( Courant_Snyder_parameters[0][1]) Courant_Snyder_parameter_beta_x.append(Courant_Snyder_parameters[1][0]) Courant_Snyder_parameter_beta_y.append(Courant_Snyder_parameters[1][1]) Courant_Snyder_parameter_gamma_x.append( Courant_Snyder_parameters[2][0]) Courant_Snyder_parameter_gamma_y.append( Courant_Snyder_parameters[2][1]) if h_f is None: h_f = plt.figure() h_ax = h_f.add_subplot(331) if charge_abs: q_pC_array = np.abs(q_pC_array) h_ax.plot(t_array, q_pC_array, linestyle=linestyle, label=label) plt.minorticks_on() plt.ylim() plt.xlabel('t [$\\omega_p$]') plt.ylabel('q [pC]') if label is not None: plt.legend() h_ax = h_f.add_subplot(332) h_ax.plot(t_array, emittance_x_array, color='k', linestyle=linestyle, label='$\\epsilon_x$') h_ax.plot(t_array, emittance_y_array, color='r', linestyle=linestyle, label='$\\epsilon_y$') plt.xlabel('t [$\\omega_p$]') plt.ylabel('$\epsilon$ [$\mu$m]') plt.legend() plt.minorticks_on() h_ax = h_f.add_subplot(333) h_ax.plot(t_array, center_gamma_array, linestyle=linestyle) plt.xlabel('t [$\\omega_p$]') plt.ylabel('$\\gamma$') plt.minorticks_on() h_ax = h_f.add_subplot(334) h_ax.plot(t_array, spread_gamma_array, linestyle=linestyle) plt.xlabel('t [$\\omega_p$]') plt.ylabel('$\\Delta \\gamma$') plt.minorticks_on() h_ax = h_f.add_subplot(335) h_ax.plot(t_array, np.divide(spread_gamma_array, center_gamma_array), linestyle=linestyle) plt.xlabel('t [$\\omega_p$]') plt.ylabel('$\\Delta \\gamma / \\gamma$') try: h_ax = h_f.add_subplot(336) outfile_object.plot_raw_hist_gamma(h_f, h_ax, if_select=True, linestyle=linestyle) plt.minorticks_on() if t_offset != 0.: h_ax.set_title('$t={0:.2f}$'.format(outfile_object.time + t_offset)) except Exception as err: print("Exception message: {}".format(err)) # plot Courant_Snyder_parameters h_ax = h_f.add_subplot(337) h_ax.plot(t_array, Courant_Snyder_parameter_alpha_x, color='k', linestyle=linestyle, label='$\\alpha_x$') h_ax.plot(t_array, Courant_Snyder_parameter_alpha_y, color='r', linestyle=linestyle, label='$\\alpha_y$') plt.xlabel('t [$\\omega_p$]') plt.ylabel('Courant Snyder $\\alpha$') plt.minorticks_on() plt.legend() h_ax = h_f.add_subplot(338) h_ax.plot(t_array, Courant_Snyder_parameter_beta_x, color='k', linestyle=linestyle, label='$\\beta_x$') h_ax.plot(t_array, Courant_Snyder_parameter_beta_y, color='r', linestyle=linestyle, label='$\\beta_y$') plt.xlabel('t [$\\omega_p$]') plt.ylabel('Courant Snyder $\\beta$ [m]') plt.minorticks_on() plt.legend() h_ax = h_f.add_subplot(339) h_ax.plot(t_array, Courant_Snyder_parameter_gamma_x, color='k', linestyle=linestyle, label='$\\gamma_x$') h_ax.plot(t_array, Courant_Snyder_parameter_gamma_y, color='r', linestyle=linestyle, label='$\\gamma_y$') plt.xlabel('t [$\\omega_p$]') plt.ylabel('Courant Snyder $\\gamma$ [$m^{-1}$]') plt.minorticks_on() plt.legend()