def plot_rb(self): fig = plt.figure() plt.plot(self.zeta_array, self.rb) plt.plot(self.zeta_array, self.rb_maxrho) ax = plt.gca() ax.set_ylabel(r'$k_p r_b$', fontsize=14) ax.set_xlabel(r'$k_p \zeta$', fontsize=14) ax.legend([r'$r_b$ from zero crossing', r'$r_b$ from global maximum']) filesuffix = '_t_%06.f' % (np.floor(self.g3d.get_time())) fileprefix = 'plasma_charge_rb' saveformat = 'eps' savepath = self.args.savepath mkdirs_if_nexist(savepath) savename = fileprefix + filesuffix + '.' + saveformat fig.savefig(savepath + '/' + savename, format=saveformat) if self.args.h5plot: h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax) h5lp.write(savepath + '/' + fileprefix + filesuffix + '.h5') if self.args.verbose: print('Saved "' + savename + '" at: ' + self.args.savepath) plt.close(fig)
def plot_r_lines(self): if not hasattr(self, 'r_z_slice_model'): self.gen_model_sheath() z_inds = np.int16(np.linspace(0, self.Nzeta - 1, 10)) fig = plt.figure() for z_ind in z_inds: p = plt.plot(self.r_array, self.r_z_slice[:, z_ind]) plt.plot(self.r_array, self.r_z_slice_model[:, z_ind], color=p[0].get_color(), linestyle='--') plt.plot(self.r_array, self.r_z_slice_model_flocmax[:, z_ind], color=p[0].get_color(), linestyle='-.') ax = plt.gca() ax.set_ylabel(r'$\rho$', fontsize=14) ax.set_xlabel(r'$k_p r$', fontsize=14) filesuffix = '_t_%06.f' % (np.floor(self.g3d.get_time())) fileprefix = 'plasma_charge_r' saveformat = 'eps' savepath = self.args.savepath mkdirs_if_nexist(savepath) savename = fileprefix + filesuffix + '.' + saveformat fig.savefig(savepath + '/' + savename, format=saveformat) plt.close(fig) if self.args.h5plot: h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax) h5lp.write(savepath + '/' + fileprefix + filesuffix + '.h5') if self.args.verbose: print('Saved "' + savename + '" at: ' + self.args.savepath)
def saveas_eps_pdf(fig, savepath, savename, h5plot=True, verbose=True, fformat='pdf', transparent=True): fformat_list = ['eps', 'pdf'] if fformat not in fformat_list: print("Error: fformat must be one of: " + fformat_list) if savepath != None: if savepath == '': savepath = '.' else: mkdirs_if_nexist(savepath) fig.savefig(savepath + '/' + savename + '.' + fformat, format=fformat, transparent=transparent) if verbose: sys.stdout.write('Saved "%s.%s" at: %s/\n' % (savename, fformat, savepath)) sys.stdout.flush() if h5plot: h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(plt.gca()) h5lp.write(savepath + '/' + savename + '.h5') if verbose: sys.stdout.write('Saved "%s.h5" at: %s/\n' % (savename, savepath)) sys.stdout.flush()
def calc_transversal_probability_density(r_max, nx, nb, ni, rbunch, lbunch): ELECTRON_CHARGE_IN_COUL = 1.60217657e-19 ELECTRON_MASS_IN_KG = 9.10938291e-31 VAC_PERMIT_FARAD_PER_M = 8.854187817e-12 SPEED_OF_LIGHT_IN_M_PER_S = 299792458 omega_alpha = 4.13e16 # [s^-1] E_alpha = 5.1e11 # [V/m] elec_density = 1e+23 # [1/m^3] omega_p = np.sqrt(4 * np.pi * elec_density * (ELECTRON_CHARGE_IN_COUL**2) / (VAC_PERMIT_FARAD_PER_M * ELECTRON_MASS_IN_KG)) E_0 = omega_p * ELECTRON_MASS_IN_KG * SPEED_OF_LIGHT_IN_M_PER_S / ELECTRON_CHARGE_IN_COUL #Use zeta array in order to calculate delta t savepath = './plots/g3d-line/' mkdirs_if_nexist(savepath) bunch_array = np.arange(0, lbunch + lbunch / 10, lbunch / 10) bunch_array_sum = np.cumsum(bunch_array) deltat = np.abs(bunch_array_sum) / omega_p vcalc_ion_rate = np.vectorize(calc_ion_rate) r_array = np.arange(0, r_max + r_max / nx, r_max / nx) c1prime = -1 / 2 * nb c3prime = -1 / 2 * rbunch**2 * (nb) savename = 'Ex_analytic' E = np.zeros(shape=np.shape(r_array)) for i in range(len(r_array)): if (r_array[i] <= rbunch): E[i] = np.real(c1prime) * r_array[i] else: E[i] = np.real(c3prime) / r_array[i] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.append(-r_array[::-1], r_array), np.append(-(E[::-1]), E[:])) x, Ex = plot_hipace_Ex(-0.2) # input = zeta pos ax.plot(x, Ex) h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax) h5lp.write(savepath + '/' + savename + '.h5') plt.show() #use zeta array for shaping the elctric field to get the same spacing etc. #print(Earray) ionization_rate = np.zeros(shape=np.shape(r_array)) ionization_rate[:] = vcalc_ion_rate(E * E_0, 1, 13.659843449, 13.659843449, 0, 0) # complete formulae for hydrogen #print(ionization_rate[1]) fig = plt.figure() ax = fig.add_subplot(111) #print('Deltat is %0.3e' %deltat) #computing the ionization probability savename = 'ion_probability_test' ion_probability = np.zeros(shape=(len(bunch_array) - 1, len(ionization_rate))) for i in range(1, 2): #len(bunch_array)): ion_probability[i - 1, :] = 1.0 - np.exp(-ionization_rate[:] * deltat[i]) ax.plot(np.append(-r_array[::-1], r_array), np.append((ion_probability[i - 1, ::-1]), ion_probability[i - 1, :]), label=('prob at lb: ' + str(np.around(bunch_array[i], decimals=1)))) #ax.legend() h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax) h5lp.write(savepath + '/' + savename + '.h5') plt.show()
def plot_deltarho_rhomax_sheathcharge(self): if not hasattr(self, 'rb'): self.calc_rb() if not hasattr(self, 'rho_max'): self.calc_deltarho_rhomax_sheathcharge() fig_charge = plt.figure() plt.plot(self.zeta_array, self.charge) ax_charge = plt.gca() ax_charge.set_ylabel(r'$\lambda=\int_{r_b}^\infty r \rho(r) dr$', fontsize=14) ax_charge.set_xlabel(r'$k_p \zeta$', fontsize=14) charge_fileprefix = 'charge' fig_rhomax = plt.figure() plt.plot(self.zeta_array, self.rho_max) plt.plot(self.zeta_array, self.rho_flocmax) ax_rhomax = plt.gca() ax_rhomax.legend( [r'using global maximum', r'using first local maximum']) ax_rhomax.set_ylabel(r'$\rho_\mathrm{max}$', fontsize=14) ax_rhomax.set_xlabel(r'$k_p \zeta$', fontsize=14) rhomax_fileprefix = 'rho_max' fig_deltarho = plt.figure() plt.plot(self.zeta_array, self.delta_rho) plt.plot(self.zeta_array, self.delta_rho_flocmax) ax_deltarho = plt.gca() ax_deltarho.legend( [r'using global maximum', r'using first local maximum']) ax_deltarho.set_ylabel(r'$\Delta_\rho$', fontsize=14) ax_deltarho.set_xlabel(r'$k_p \zeta$', fontsize=14) deltarho_fileprefix = 'delta_rho' filesuffix = '_t_%06.f' % (np.floor(self.g3d.get_time())) saveformat = 'eps' savepath = self.args.savepath mkdirs_if_nexist(savepath) charge_savename = charge_fileprefix + filesuffix + '.' + saveformat rhomax_savename = rhomax_fileprefix + filesuffix + '.' + saveformat deltarho_savename = deltarho_fileprefix + filesuffix + '.' + saveformat fig_charge.savefig(savepath + '/' + charge_savename, format=saveformat) if self.args.verbose: print('Saved "' + charge_savename + '" at: ' + self.args.savepath) fig_rhomax.savefig(savepath + '/' + rhomax_savename, format=saveformat) if self.args.verbose: print('Saved "' + rhomax_savename + '" at: ' + self.args.savepath) fig_deltarho.savefig(savepath + '/' + deltarho_savename, format=saveformat) if self.args.verbose: print('Saved "' + deltarho_savename + '" at: ' + self.args.savepath) if self.args.h5plot: h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax_charge) h5lp.write(savepath + '/' + charge_fileprefix + filesuffix + '.h5') h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax_rhomax) h5lp.write(savepath + '/' + rhomax_fileprefix + filesuffix + '.h5') h5lp = H5Plot() h5lp.inherit_matplotlib_line_plots(ax_deltarho) h5lp.write(savepath + '/' + deltarho_fileprefix + filesuffix + '.h5') plt.show()
def main(): parser = h5plot_parser() args = parser.parse_args() if len(sys.argv) == 1: parser.print_help() sys.exit(1) fig = plt.figure(figsize=(6, 5)) if (args.line_no != None) and (len(args.line_no) != len(args.paths)): print( 'ERROR: number of selected lines must be equal to the number of provided hdf5 files!' ) sys.exit(1) if (args.labels != None) and (len(args.labels) != len(args.paths)): print( 'ERROR: number of prepend labels must be equal to the number of provided hdf5 files!' ) sys.exit(1) h5lp = [] linestyles = ['-', '--', '-.', ':'] save_append_str = '' type_str = 'comp' if not args.latexoff: plt.rc('text', usetex=True) plt.rc('font', family='serif') j = 0 if args.diff: h5firstlp = H5Plot() h5firstlp.read(args.paths[0]) type_str = 'diff' for (x, y, label, linestyle, color) in h5firstlp.get_line_plots(): if j == 0: x0 = x y0 = y j += 1 h5firstlp = H5Plot() h5firstlp.read(args.paths[0]) for (x, y, label, linestyle, color) in h5firstlp.get_line_plots(): if not args.extrue: if (any(y < 0) and any(y > 0)): for i in range(1, len(x)): if y[i] > 0 and y[i - 1] < 0: y_strich = -(y[i + 5] - y[i - 6]) / abs(x[i + 5] - x[i - 6]) print('slope around root: from %f to %f is : %f' % ((x[i + 5]), x[i - 6], y_strich)) else: print('there are not roots') else: for i in range(1, len(x)): if x[i - 1] * x[i] < 0: y_strich = (y[i] - y[i - 1]) / abs(x[i] - x[i - 1]) print('slope at x = %f is : %f' % ((x[i] + x[i - 1]) / 2, y_strich)) i = 0 for path in args.paths: if not os.path.isfile(path): print('ERROR: File %s does not exist!' % path) sys.exit(1) h5lp.append(H5Plot()) h5lp[i].read(path) if args.cno == None: argcolor = plt.cm.Dark2(i) else: argcolor = plt.cm.Dark2(args.cno[i]) j = 0 for (x, y, label, linestyle, color) in h5lp[i].get_line_plots(): if args.labels != None: save_append_str += '_' + args.labels[i] label = args.labels[i] if label == '_line0': label = path if not args.latexoff: if label[0] != '$' or label[-1] != '$': label = r'$\textrm{' + label + '}$' if args.diff: if (j == 0 and i == 0): continue else: y -= np.interp(x, x0, y0) if args.lstyle == None: linestyle_code = linestyles[i % len(linestyles)] else: linestyle_code = linestyles[args.lstyle[i]] if (args.line_no != None): if args.line_no[i] == j: if args.absylog: plt.semilogy(x, y, label=label, linestyle=linestyle_code, color=argcolor) else: plt.plot(x, y, label=label, linestyle=linestyle_code, color=argcolor) else: if args.absylog: plt.semilogy(x, y, label=label, linestyle=linestyle_code, color=argcolor) else: #plt.plot(x, y, label=label, linestyle=linestyle, color=color) plt.plot(x, y, label=label, linestyle=linestyle_code, color=argcolor) j += 1 i += 1 ax = plt.gca() ax.set_xlabel(h5lp[0].get_xlab(), fontsize=14) ax.set_ylabel(h5lp[0].get_ylab(), fontsize=14) handles, labels = ax.get_legend_handles_labels() #plt.legend(flip(handles, 2), flip(labels, 2), ncol=2) plt.legend(frameon=False) plt.gcf().subplots_adjust(left=0.15, bottom=0.15) if not (-3.0 < math.log(np.max(abs(y)), 10) < 3.0): ax.yaxis.set_major_formatter(FormatStrFormatter('%.1e')) plt.gcf().subplots_adjust(left=0.18) plt.show() fname, fext = os.path.splitext(args.paths[0]) save_path_name = fname + save_append_str + '_' + type_str + '.' + args.file_format fig.savefig(save_path_name, format=args.file_format) plt.close(fig) if args.verbose: sys.stdout.write('Saved: %s\n' % save_path_name) sys.stdout.flush()
def main(): parser = h5plot_parser() args = parser.parse_args() if len(sys.argv) == 1: parser.print_help() sys.exit(1) fig = plt.figure(figsize=(6, 5)) if (args.line_no != None) and (len(args.line_no) != len(args.paths)): print( 'ERROR: number of selected lines must be equal to the number of provided hdf5 files!' ) sys.exit(1) if (args.labels != None) and (len(args.labels) != len(args.paths)): print( 'ERROR: number of prepend labels must be equal to the number of provided hdf5 files!' ) sys.exit(1) h5lp = [] linestyles = ['-', '--', '-.', ':'] save_append_str = '' type_str = 'comp' if not args.latexoff: plt.rc('text', usetex=True) plt.rc('font', family='serif') j = 0 if args.diff: h5firstlp = H5Plot() h5firstlp.read(args.paths[0]) type_str = 'diff' for (x, y, label, linestyle, color) in h5firstlp.get_line_plots(): if j == 0: x0 = x y0 = y j += 1 h5firstlp = H5Plot() h5firstlp.read(args.paths[0]) for (x, y, label, linestyle, color) in h5firstlp.get_line_plots(): x_p = x[int(np.floor(len(x) / 2)):] y_p = y[int(np.floor(len(y) / 2)):] n = len(x_p) #the number of data mean = sum(x_p * y_p) / n #note this correction sigma = sum(y_p * (x_p - mean)**2) / n def gaus(x, a, x0, sigma): return a * exp(-(x - x0)**2 / (2 * sigma**2)) popt, pcov = curve_fit(gaus, x_p, y_p, p0=[max(y_p), mean, sigma]) i = 0 for path in args.paths: if not os.path.isfile(path): print('ERROR: File %s does not exist!' % path) sys.exit(1) h5lp.append(H5Plot()) h5lp[i].read(path) if args.cno == None: argcolor = plt.cm.Dark2(i) else: argcolor = plt.cm.Dark2(args.cno[i]) j = 0 for (x, y, label, linestyle, color) in h5lp[i].get_line_plots(): if args.labels != None: save_append_str += '_' + args.labels[i] label = args.labels[i] if label == '_line0': label = path if not args.latexoff: if label[0] != '$' or label[-1] != '$': label = r'$\textrm{' + label + '}$' if args.diff: if (j == 0 and i == 0): continue else: y -= np.interp(x, x0, y0) if args.lstyle == None: linestyle_code = linestyles[i % len(linestyles)] else: linestyle_code = linestyles[args.lstyle[i]] if (args.line_no != None): if args.line_no[i] == j: if args.absylog: plt.semilogy(x, y, label=label, linestyle=linestyle_code, color=argcolor) else: plt.plot(x, y, label=label, linestyle=linestyle_code, color=argcolor) plt.plot(x_p, gaus(x_p, *popt), 'r--', label='fit') else: if args.absylog: plt.semilogy(x, y, label=label, linestyle=linestyle_code, color=argcolor) else: #plt.plot(x, y, label=label, linestyle=linestyle, color=color) plt.plot(x, y, label=label, linestyle=linestyle_code, color=argcolor) plt.plot(x_p, gaus(x_p, *popt), 'r--', label='fit') j += 1 i += 1 ax = plt.gca() ax.set_xlabel(h5lp[0].get_xlab(), fontsize=14) ax.set_ylabel(h5lp[0].get_ylab(), fontsize=14) handles, labels = ax.get_legend_handles_labels() #plt.legend(flip(handles, 2), flip(labels, 2), ncol=2) plt.legend(frameon=False) plt.gcf().subplots_adjust(left=0.15, bottom=0.15) if not (-3.0 < math.log(np.max(abs(y)), 10) < 3.0): ax.yaxis.set_major_formatter(FormatStrFormatter('%.1e')) plt.gcf().subplots_adjust(left=0.18) plt.show() fname, fext = os.path.splitext(args.paths[0]) save_path_name = fname + save_append_str + '_' + type_str + '.' + args.file_format fig.savefig(save_path_name, format=args.file_format) plt.close(fig) if args.verbose: sys.stdout.write('Saved: %s\n' % save_path_name) sys.stdout.flush()
def main(): parser = h5plot_parser() args = parser.parse_args() if args.maketitle: ''' Calculating plasma frequency for length ''' ELECTRON_CHARGE_IN_COUL = 1.60217657e-19 ELECTRON_MASS_IN_KG = 9.10938291e-31 VAC_PERMIT_FARAD_PER_M = 8.854187817e-12 SPEED_OF_LIGHT_IN_M_PER_S = 299792458.0 omega_p = np.sqrt( args.n0 * (ELECTRON_CHARGE_IN_COUL**2)/ (VAC_PERMIT_FARAD_PER_M * ELECTRON_MASS_IN_KG)); skin_depth = SPEED_OF_LIGHT_IN_M_PER_S/omega_p if len(sys.argv)==1: parser.print_help() sys.exit(1) saveformat = args.file_format h5lp = [] linestyles = ['-', '--', '-.', ':'] save_append_str = '' type_str = 'comp' if args.latexon: plt.rc('text', usetex=True) plt.rc('font', family='serif') j = 0 i = 0 for path in args.paths: fig = plt.figure(figsize=(7,5)) if not os.path.isfile(path): print('ERROR: File %s does not exist!' % path) sys.exit(1) h5lp.append(H5Plot()) h5lp[i].read(path) timestamp = path.split("_")[-1].split(".h5")[0] if args.data2: for files in args.data2: if timestamp in files or args.manual: print('Reading second data set...') h5secondlp = H5Plot() h5secondlp.read(files) if args.cno == None: argcolor = plt.cm.tab20(6) else: argcolor = plt.cm.tab20(args.cno[0]) j = 0 for (x, y, label, linestyle, color) in h5lp[i].get_line_plots(): if args.labels != None: save_append_str += '_' + args.labels[i] label = args.labels[i] if label == '_line0': label = path if args.latexon: if label[0] != '$' or label[-1] != '$': label = r'$\textrm{' + label + '}$' if args.abs: y = np.abs(y) if args.lstyle == None: linestyle_code = linestyles[0] else: linestyle_code = linestyles[args.lstyle[0]] if args.absylog: plt.semilogy( x, y, label=label, linestyle=linestyle_code, color=argcolor, zorder=20) else: #plt.plot(x, y, label=label, linestyle=linestyle, color=color) plt.plot(x, y, label=label, linestyle=linestyle_code, color=argcolor, zorder=20) ax = plt.gca() ax.tick_params('y', colors=argcolor) plt.gcf().subplots_adjust(left=0.15, bottom=0.15, right=1-0.15) if not (-3.0 < math.log(np.max(abs(y)),10) < 3.0): ax.yaxis.set_major_formatter(FormatStrFormatter('%.1e')) plt.gcf().subplots_adjust(left=0.18) ax2 = ax.twinx() ''' PLOTTING THE SECOND PLOT ''' if args.cno == None: argcolor = plt.cm.tab20(1) else: argcolor = plt.cm.tab20(args.cno[1]) ax2.tick_params('y', colors=argcolor) j = 0 for (x2, y2, label, linestyle, color) in h5secondlp.get_line_plots(): if args.labels != None: save_append_str += '_' + args.labels[i] label = args.labels[i] if label == '_line0': label = path if args.latexon: if label[0] != '$' or label[-1] != '$': label = r'$\textrm{' + label + '}$' if args.abs: y2 = np.abs(y2) if args.lstyle == None: linestyle_code = linestyles[0] else: linestyle_code = linestyles[args.lstyle[1]] if args.absy2log: plt.semilogy( x2, y2, label=label, linestyle=linestyle_code, color=argcolor, zorder=0) else: #plt.plot(x, y, label=label, linestyle=linestyle, color=color) plt.plot(x2, y2, label=label, linestyle=linestyle_code, color=argcolor, zorder=0) if args.xlim != None: ax.set_xlim(args.xlim[0],args.xlim[1]) if args.ylim != None: ax.set_ylim(args.ylim[0],args.ylim[1]) if args.y2lim != None: ax2.set_ylim(args.y2lim[0],args.y2lim[1]) if args.xlab != None: xlab = args.xlab else: xlab = h5lp[0].get_xlab() if args.ylab != None: ylab = args.ylab else: ylab = [] ylab.append(h5lp[0].get_ylab()) ylab.append(h5secondlp.get_ylab()) if args.latexon: if xlab[0] != '$' or xlab[-1] != '$': xlab = r'$' + xlab + '$' if args.latexon: if ylab[0,0] != '$' or ylab[0,-1] != '$': ylab[0] = r'$' + ylab[0] + '$' ylab[1] = r'$' + ylab[1] + '$' ax.set_xlabel(xlab, fontsize=14) ax.set_ylabel(ylab[0], fontsize=14) ax2.set_ylabel(ylab[1], fontsize=14) ax.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2 ax.patch.set_visible(False) # hide the 'canvas' # handles, labels = ax.get_legend_handles_labels() # #plt.legend(flip(handles, 2), flip(labels, 2), ncol=2) # plt.legend(frameon=False) #plt.gcf().subplots_adjust(left=0.15, bottom=0.15, right=0.15) if not (-3.0 < math.log(np.max(abs(y2)),10) < 3.0): ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1e')) plt.gcf().subplots_adjust(right=1-0.18) #plt.show() ''' make the title ''' if args.maketitle: time = np.around(float(timestamp)) distance = time*skin_depth*1e2 title = 'Time: ' + str(time) + r'$\,\omega_p^{-1}$ ' + ' Distance: ' + str(np.around(distance,2)) + r'$\,$cm' plt.title(title) if args.show: plt.show() spath, fname = os.path.split(args.paths[0]) if args.savepath != None: spath = args.savepath if not args.manual: save_name = type_str + '_' + fname + save_append_str + '_' + timestamp else: save_name = type_str + '_' + fname + save_append_str if saveformat==args.file_format: saveas_png(fig, spath, save_name, args.dpi) else: saveas_eps_pdf(fig, savepath=spath, savename=save_name) plt.close(fig)