def foreach_decompose(file_num): e_filename = e1[file_num] e1file = read_hdf(e_filename) e2file = read_hdf(e2[file_num]) e1data = analysis.analysis(e1file.data, ['fft']) e2data = analysis.analysis(e2file.data, ['fft']) h5_output.run_attributes['TIME'][0] = e1file.run_attributes['TIME'][0] h5_output.run_attributes['ITER'][0] = e1file.run_attributes['ITER'][0] if blockx or blocky: e1data = arraymask.mask(e1data, axes=[ky, kx], region=brg) e2data = arraymask.mask(e2data, axes=[ky, kx], region=brg) dive = np.multiply(kxvec, e1data) + np.multiply(kyvec, e2data) h5_output.data = np.multiply(kxvec, dive) # transverse part, longitudinal part if ift: tr1, e1data = (analysis.analysis(e1data - h5_output.data, ['ifft ' + ift]), analysis.analysis(h5_output.data, ['ifft ' + ift])) else: tr1, e1data = e1data - h5_output.data, h5_output.data if 't1' in ocomp: h5_output.data = np.abs(tr1) h5_output.run_attributes['NAME'][0], h5_output.data_attributes[ 'LONG_NAME'] = 'Et1', 'E_{t1}' newname = outdir + 'Et1' + os.path.basename(e_filename)[2:] write_hdf(h5_output, newname) h5_output.data = np.multiply(kyvec, dive) if ift: tr2, e2data = (analysis.analysis(e2data - h5_output.data, ['ifft ' + ift]), analysis.analysis(h5_output.data, ['ifft ' + ift])) else: tr2, e2data = e2data - h5_output.data, h5_output.data if 't2' in ocomp: h5_output.data = np.abs(tr2) h5_output.run_attributes['NAME'][0], h5_output.data_attributes[ 'LONG_NAME'] = 'Et2', 'E_{t2}' newname = outdir + 'Et2' + os.path.basename(e_filename)[2:] write_hdf(h5_output, newname) if 't' in ocomp: h5_output.data = np.sqrt(np.abs(np.square(tr1) + np.square(tr2))) h5_output.run_attributes['NAME'][0], h5_output.data_attributes[ 'LONG_NAME'] = 'ET', 'E_T' newname = outdir + 'ET' + os.path.basename(e_filename)[2:] write_hdf(h5_output, newname) if 'l' in ocomp: h5_output.data = np.sqrt(np.abs(np.square(e1data) + np.square(e2data))) h5_output.run_attributes['NAME'][0], h5_output.data_attributes[ 'LONG_NAME'] = 'EL', 'E_L' newname = outdir + 'EL' + os.path.basename(e_filename)[2:] write_hdf(h5_output, newname) return e_filename
def plot_t_vs_k(output_directory, v0=3.0, mass_ratio=1.0 / 100.0): workdir = os.getcwd() dirname = output_directory filename = workdir + '/' + dirname + '/Ex.h5' test4 = h5_utilities.read_hdf(filename) k_data = np.fft.fft(test4.data, axis=1) test4.data = np.abs(k_data) test4.axes[0].axis_max = 2.0 * 3.1415926 plt.figure(figsize=(8, 6)) analysis.plotme(test4) maxgr, kofmax, kedge = get_theory(v0, mass_ratio) #k_bound=0.446664 #k_max=0.34713 k_bound = kedge k_max = kofmax plt.plot([k_bound, k_bound], [0, 200], 'b--', label='Instability Boundary') plt.plot([k_max, k_max], [0, 200], 'r--', label='Peak Location') plt.xlim(0, 1) plt.ylim(0, 190) plt.xlabel('Wavenumber [$1/\Delta x$]') plt.ylabel('Time [$1/\omega_p$]') # plt.ylim(0,50) # plt.ylim(tlim[0],tlim[1]) plt.legend(loc='lower right', prop={'size': 12}) plt.show()
def foreach_decompose(file_num): f_filename = flst[file_num] ffile = read_hdf(f_filename) if adjust_ops == 'subrange': ffile.data = adjust.subrange(ffile.data, **adjust_ops.keywords) if rebin: ffile.data = analysis.rebin(ffile.data, fac=rebin) # central difference for interior, forward and backward at the boundaries grad = np.gradient(ffile.data, axis=pdim) if dfdp: h5_output.data = grad else: pf = - paxis * ffile.data # there are zeros because: 1. the origin is included in the axis points; 2. the tail of distribution is zero pf[pf == 0.] = 999999.0 h5_output.data = np.divide(grad, pf) if mininp: h5_output.data = adjust.subrange_phys(h5_output.data, bound=mininp, axis=pdim, axesdata=tmp_axis, update_axis=False) tmp = h5_output.data.copy() h5_output.data = analysis.analysis(h5_output.data, [getmin]) h5_output.run_attributes['TIME'][0] = ffile.run_attributes['TIME'][0] h5_output.run_attributes['ITER'][0] = ffile.run_attributes['ITER'][0] newname = outdir + os.path.basename(f_filename) write_hdf(h5_output, newname) if mininp: h5_output.data = pax[analysis.analysis(tmp, [min_index])] write_hdf(h5_output, outdir + inddir + os.path.basename(f_filename)) return f_filename
def plot_t_vs_k(output_directory, v0=9.0, tlim=[0,80]): klim=5 #tlim=80 PATH = os.getcwd() + '/' + output_directory +'/'+ 'Ex.h5' hdf5_data = h5_utilities.read_hdf(PATH) k_data=np.fft.fft(hdf5_data.data,axis=1) hdf5_data.data=np.abs(k_data) hdf5_data.axes[0].axis_max=2.0*3.1415926*v0 N=100 dt = float(tlim[1])/N tvals=np.arange(0,tlim[1],dt) kvals=np.zeros(N) kpeak_vals=np.zeros(N) for i in range(0,N): kvals[i]=np.sqrt(2) kpeak_vals[i]=0.85 plt.figure(figsize=(8,5)) analysis.plotme(hdf5_data) plt.plot(kvals,tvals,'b--',label='Instability Boundary') plt.plot(kpeak_vals,tvals,'r--',label='Peak Location') plt.title('Ex t-k space' ) plt.xlabel(' α ',**axis_font) plt.ylabel(' Time [$1/ \omega_{pe}$]',**axis_font) plt.xlim(0,klim) plt.ylim(tlim) plt.legend() plt.show()
def plot_t_vs_k(output_directory): workdir = os.getcwd() dirname = output_directory filename = workdir + '/' + dirname + '/Ex.h5' # print(filename) test4 = h5_utilities.read_hdf(filename) # here we fourier analyze the data in space # # k_data=np.fft.fft(test.data,axis=1) k_data = np.fft.fft(test4.data, axis=1) # k_data_2=np.fft.fft(k_data,axis=0) test4.data = np.abs(k_data) test4.axes[0].axis_max = 2.0 * 3.1415926 # test4.data=np.log10(np.real(test4.data)+1e-10) plt.figure(figsize=(8, 5)) analysis.plotme(test4) # k_bound=0.13 k_max = 0.1 # plt.plot([k_bound,k_bound],[0,200],'b--',label='Instability Boundary') plt.plot([k_max, k_max], [0, 200], 'r--', label='Peak Location') plt.xlim(0, 1) plt.ylim(0, 190) plt.xlabel('Wavenumber [$1/\Delta x$]') plt.ylabel('Time [$1/\omega_p$]') # plt.ylim(0,50) # plt.ylim(tlim[0],tlim[1]) plt.legend(loc='lower right', prop={'size': 12}) plt.show()
def compare_sim_with_theory(output_directory, modemin=1, modemax=5, v0=1, init_amplitude=1e-5, tlim=[0,80]): rundir = output_directory field = 'Ex' #tlim = 80 PATH = os.getcwd() + '/' + rundir +'/'+ field + '.h5' hdf5_data = h5_utilities.read_hdf(PATH) k_data=np.fft.fft(hdf5_data.data,axis=1) hdf5_data.data=np.abs(k_data) nx=hdf5_data.data.shape[1] nt=hdf5_data.data.shape[0] taxis=np.linspace(0,hdf5_data.axes[1].axis_max,nt) deltak=2.0*3.1415926/nx hdf5_data.axes[0].axis_max=2.0*3.1415926*v0 # nplots=modemax-modemin+1 nplots=modemax-modemin+2 N=100 plt.figure(figsize=(8,3*nplots+1)) plt.subplot(nplots,1,1) for imode in range(modemin,modemax+1): plt.semilogy(taxis,hdf5_data.data[:,imode],label='mode '+repr(imode)) plt.ylabel('Mode Amplitudes') plt.xlabel('Time [$1/ \omega_{p}$]') plt.legend() plt.xlim(tlim) # Shrink current axis by 20% ax = plt.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop={'size': 12}) for imode in range(modemin,modemax+1): plt.subplot(nplots,1,imode+1) stream_theory=np.zeros(nt) growth_rate=tstream_root_minus_i(deltak*imode,v0,1.0) for it in range(0,nt): stream_theory[it]=init_amplitude*np.exp(growth_rate*taxis[it]) plt.semilogy(taxis,hdf5_data.data[:,imode],label='PIC simulation, mode ='+repr(imode)) plt.semilogy(taxis,stream_theory,'r',label='theory, growth rate = {:.3f}'.format(growth_rate)) plt.ylabel('mode'+repr(imode)) plt.xlabel('Time [$1/ \omega_{p}$]') plt.legend() plt.xlim(tlim) plt.show()
def compare_sim_with_theory(output_directory, v0, mode, mass_ratio): alpha = np.arange(0, 2.0, 0.01) rmass = mass_ratio growth_rate = osiris.buneman_growth_rate(alpha, rmass) growth_rate_func = interp1d(alpha, np.abs(growth_rate), kind='cubic') workdir = os.getcwd() dirname = output_directory filename = workdir + '/' + dirname + '/Ex.h5' test4 = h5_utilities.read_hdf(filename) k_data = np.fft.fft(test4.data, axis=1) test4.data = np.abs(k_data) test4.axes[0].axis_max = 2.0 * 3.1415926 nx = test4.data.shape[1] nt = test4.data.shape[0] dk = 2 * 3.1415926 / nx display_mode = mode bracket = False #v0=3.0 alpha = v0 * dk * (display_mode) # growth_rate = 0.0 # if (alpha<np.sqrt(2)): growth_rate = growth_rate_func(alpha)[()] taxis = np.linspace(0, test4.axes[1].axis_max, nt) stream_theory = np.zeros(nt) stream_theory_plus = np.zeros(nt) stream_theory_minus = np.zeros(nt) init_amplitude = 1e-7 for it in range(0, nt): stream_theory[it] = init_amplitude * np.exp(growth_rate * taxis[it]) stream_theory_plus[it] = init_amplitude * np.exp( 1.15 * growth_rate * taxis[it]) stream_theory_minus[it] = init_amplitude * np.exp( 0.85 * growth_rate * taxis[it]) plt.figure(figsize=(8, 6)) plt.semilogy(taxis, test4.data[:, display_mode], label='PIC simulation, mode =' + repr(display_mode)) plt.semilogy(taxis, stream_theory, 'r', label='theory, growth rate =' + '%.3f' % growth_rate) if (bracket): plt.semilogy(taxis, stream_theory_plus, 'g.') plt.semilogy(taxis, stream_theory_minus, 'g.') plt.ylim((1e-7, 1000)) plt.legend() plt.xlabel('Time $[1/\omega_{pe}]$', **axis_font) plt.ylabel('Time History [a.u.]', **axis_font) plt.show()
else: flst = None flst = comm.bcast(flst, root=0) # # divide the task total_time = len(flst) my_share = total_time // size i_begin = rank * my_share if rank < (size - 1): i_end = (rank + 1) * my_share else: i_end = total_time # # READ ONE FILE, FIGURE OUT THE AXES INFORMATION AND SET COMMON PARAMETERS h5_filename = flst[i_begin] h5_output = read_hdf(h5_filename) nx = h5_output.shape ndim = h5_output.data.ndim # make some adjustments before processing data, useful for rebinning etc. if adjust_ops == 'subrange': h5_output = adjust.subrange(h5_output, axesdata=h5_output.axes, **adjust_ops.keywords) # determine which dimension to differentiate if not pdim: for pdim, ax in enumerate(h5_output.axes): if b'p' in ax.attributes['NAME'][0].lower(): break else: sys.exit('Cannot find velocity axis and no dim parameter specified. Exiting...') h5_output.NAME[0] += b' slope' paxis = h5_output.axes[pdim].get_axis_points()
def compare_sim_with_theory(output_directory, v0, mode, density_ratio): alpha = np.linspace(0, 20, num=200) rmass = density_ratio growth_rate = osiris.buneman_growth_rate(alpha, rmass) growth_rate_func = interp1d(alpha, np.abs(growth_rate), kind='cubic') workdir = os.getcwd() dirname = output_directory filename = workdir + '/' + dirname + '/Ex.h5' # print(filename) test4 = h5_utilities.read_hdf(filename) # here we fourier analyze the data in space # # k_data=np.fft.fft(test.data,axis=1) k_data = np.fft.fft(test4.data, axis=1) # k_data_2=np.fft.fft(k_data,axis=0) test4.data = np.abs(k_data) test4.axes[0].axis_max = 2.0 * 3.1415926 nx = test4.data.shape[1] nt = test4.data.shape[0] # print(repr(nt)) dk = 2 * 3.1415926 / nx # print('Delta k = '+repr(dk)) # To compare with theory, just specify the mode you want to look at here # display_mode = mode bracket = False # # #v0=10.0 alpha = v0 * dk * (display_mode) # growth_rate = 0.0 # if (alpha<np.sqrt(2)): growth_rate = growth_rate_func(alpha)[()] taxis = np.linspace(0, test4.axes[1].axis_max, nt) stream_theory = np.zeros(nt) stream_theory_plus = np.zeros(nt) stream_theory_minus = np.zeros(nt) init_amplitude = 1e-7 for it in range(0, nt): stream_theory[it] = init_amplitude * np.exp(growth_rate * taxis[it]) stream_theory_plus[it] = init_amplitude * np.exp( 1.15 * growth_rate * taxis[it]) stream_theory_minus[it] = init_amplitude * np.exp( 0.85 * growth_rate * taxis[it]) plt.figure(figsize=(8, 5)) plt.semilogy(taxis, test4.data[:, display_mode], label='PIC simulation, mode =' + repr(display_mode)) plt.semilogy(taxis, stream_theory, 'r', label='theory, growth rate =' + '%.3f' % growth_rate) if (bracket): plt.semilogy(taxis, stream_theory_plus, 'g.') plt.semilogy(taxis, stream_theory_minus, 'g.') plt.ylim((1e-7, 1000)) plt.legend() plt.xlabel('Time $[1/\omega_{pe}]$', **axis_font) plt.ylabel('Mode amplitude [a.u.]', **axis_font) plt.show()