def calc_grid(xydata, b, X, strfile, BW): """ Caluclate currents from time-series and interpolate onto grid """ me = "LE_Plot.calc_grid: " ## Output options fixscale = False ## If True, user determines axis scale savedata = True if fixscale: outfile = outfile + "_fix" ## Set eta (yy) and xHO/xBW (x1) x1, yy = xydata del xydata ## Set up grid of points in x-y gridsize = 30 if fixscale: xmax, ymax = 2 * X, blim(b, X)[1] else: xmax, ymax = x1.max(), yy.max() x = np.linspace(-xmax, xmax, gridsize) y = np.linspace(-ymax, ymax, gridsize) xi, yi = np.meshgrid(x, y) yi = yi[::-1, :] ## Need to flip yi ## Calculate speeds (1D arrays) vx1 = np.gradient(x1) vyy = np.gradient(yy) ## -------------------------------------------------------------------- ## Interpolate data onto grid t0 = time.time() ## Scipy griddata (slow) gvx11 = griddata(zip(x1, yy), vx1, (xi, yi), method='linear', fill_value=0.0) gvyy1 = griddata(zip(x1, yy), vyy, (xi, yi), method='linear', fill_value=0.0) # gv1 = np.sqrt(gvx11*gvx11+gvyy1*gvyy1) print me + "Gridding data ", round(time.time() - t0, 1), "seconds" """## Split up triangulation step and interpolation step ## gridpoints = np.array([[i,j] for i in y for j in x]) ## Reminder: (x,y)->(row,col), so indices must be reversed""" # vertices,weights = interp_weights(np.array(zip(x1,yy)), np.array([[i,j] for i in y for j in x])) # print me+"Triangulation",round(time.time()-t0,1),"seconds"; t1=time.time() # gvx11 = interpolate(vx1, vertices, weights).reshape([gridsize,gridsize]) # gvyy1 = interpolate(vyy, vertices, weights).reshape([gridsize,gridsize]) # gv1 = interpolate(v1, vertices, weights).reshape([gridsize,gridsize]) # print me+"Interpolation",round(time.time()-t1,1),"seconds"; t1=time.time() ## Write data file and header file if savedata: LE_Simulate.save_data(strfile, np.vstack([x, y, gvx11, gvyy1])) np.savetxt(strfile + ".hdr", np.array([b, X, xmax, ymax, BW])) return x, y, gvx11, gvyy1, (b, X, xmax, ymax, BW)
def calc_grid(xydata, b,X, strfile, BW): """ Caluclate currents from time-series and interpolate onto grid """ me = "LE_Plot.calc_grid: " ## Output options fixscale = False ## If True, user determines axis scale savedata = True if fixscale: outfile = outfile+"_fix" ## Set eta (yy) and xHO/xBW (x1) x1, yy = xydata del xydata ## Set up grid of points in x-y gridsize = 30 if fixscale: xmax, ymax = 2*X, blim(b,X)[1] else: xmax, ymax = x1.max(), yy.max() x = np.linspace(-xmax,xmax, gridsize);y = np.linspace(-ymax,ymax,gridsize) xi,yi = np.meshgrid(x,y); yi = yi[::-1,:] ## Need to flip yi ## Calculate speeds (1D arrays) vx1 = np.gradient(x1) vyy = np.gradient(yy) ## -------------------------------------------------------------------- ## Interpolate data onto grid t0 = time.time() ## Scipy griddata (slow) gvx11 = griddata(zip(x1,yy), vx1, (xi,yi), method='linear',fill_value=0.0) gvyy1 = griddata(zip(x1,yy), vyy, (xi,yi), method='linear',fill_value=0.0) # gv1 = np.sqrt(gvx11*gvx11+gvyy1*gvyy1) print me+"Gridding data ",round(time.time()-t0,1),"seconds" """## Split up triangulation step and interpolation step ## gridpoints = np.array([[i,j] for i in y for j in x]) ## Reminder: (x,y)->(row,col), so indices must be reversed""" # vertices,weights = interp_weights(np.array(zip(x1,yy)), np.array([[i,j] for i in y for j in x])) # print me+"Triangulation",round(time.time()-t0,1),"seconds"; t1=time.time() # gvx11 = interpolate(vx1, vertices, weights).reshape([gridsize,gridsize]) # gvyy1 = interpolate(vyy, vertices, weights).reshape([gridsize,gridsize]) # gv1 = interpolate(v1, vertices, weights).reshape([gridsize,gridsize]) # print me+"Interpolation",round(time.time()-t1,1),"seconds"; t1=time.time() ## Write data file and header file if savedata: LE_Simulate.save_data(strfile, np.vstack([x,y,gvx11,gvyy1]) ) np.savetxt(strfile+".hdr",np.array([b,X,xmax,ymax,BW]) ) return x,y,gvx11,gvyy1,(b,X,xmax,ymax,BW)
def LE_SimStr(b, X): """ A module to produce data from simulation and analyse it to streamplot. """ datafile = LE_Simulate.LEsim(b, X) LEstr(datafile) return
def LE_SimPlt(b, X, timefac, BW, smooth): """ Wrapper for simulation + streamplot. """ me = "LE_Plot.LE_SimPlt: " print "\n== " + me + "b =", b, " X =", X, " BW =", BW, "==\n" t0 = time.time() ## Filenames trafile, rndfile, pdffile, strfile, n = get_filenames(b, X, timefac, BW) ## GET_TRADATA ## Time-series / trajectory data: load or simulate try: txyxidata = np.load(trafile + ".npy")[:n, :] print me + "Trajectory file found", trafile + ".npy" except IOError: print me + "No trajectory file found. Simulating..." txyxidata = LE_Simulate.LEsim(b, X, timefac, BW) print me, collect() ## GET_STRDATA ## Interpolated current data: load or calculate try: A = np.load(strfile + ".npy") grd = A.shape[1] x, y, gvx, gvy = A[0], A[1], A[2:grd + 2], A[grd + 2:] oargs = np.loadtxt(strfile + ".hdr") print me + "Steamgrid file found", strfile + ".npy" except IOError: print me + "No streamgrid file found. Calculating..." x, y, gvx, gvy, oargs = calc_grid(txyxidata[1:3], b, X, strfile, BW) print me, collect() ## Plots # plot_rand(txyxidata, b,X, rndfile) # print me,collect() plot_pdf(txyxidata[1:3], b, X, pdffile) print me, collect() plot_stream(x, y, gvx, gvy, np.append(oargs, smooth), strfile) print me, collect() print me + "Total time", round(time.time() - t0, 1), "seconds" return
def plot_stream_fromPDF(b, X, timefac, BW, smooth): """ Create PDF from trajectory, calculate current field, and plot. """ me = "LE_PlotII.plot_stream_fromPDF: " ## Filenames trafile, rndfile, pdffile, strfile, n = get_filenames(b, X, timefac, BW) ## Read in trajectory data (or simulate anew) try: xydata = np.load(trafile + ".npy")[1:3, :n] print me + "Trajectory file found", trafile + ".npy" except IOError: print me + "No trajectory file found. Simulating..." xydata = LE_Simulate.LEsim(b, X, timefac, BW)[1:3, :n] ## Create histogram and save PDF plot P, x, y = plot_pdf(xydata, b, X, pdffile) xmax, ymax = np.abs(x).max(), np.abs(y).max() x = (x[1:] + x[:-1]) * 0.5 y = (y[1:] + y[:-1]) * 0.5 gx, gy = np.meshgrid(x, y) gy = -gy ## Force field f = LE_Simulate.FBW(gx, b, X) if BW else LE_Simulate.FHO(gx, b, X) ## Compute currents (on-grid) Jx, Jy = J_BW(P, b, f, gy) Jt = np.sqrt(Jx * Jx + Jy * Jy) ## -------------------------------------------------------------------- ## Smooth data if smooth is not 0.0: Jx = gaussian_filter(Jx, smooth) Jy = gaussian_filter(Jy, smooth) Jt = gaussian_filter(Jt, smooth) strfile += "_sm" + str(smooth) ## -------------------------------------------------------------------- ## Plotting t0 = time.time() fs = 25 fig = plt.figure(facecolor="white") fig.suptitle(strfile) ############################## P /= np.trapz(P.sum(axis=1), y) if False: v = 0.051**2 #0.115**2 Pex = 1 / np.sqrt(2 * np.pi * v) * np.exp(-y * y * 0.5 / v) ## Plot P(y) against y plt.plot(y, P.sum(axis=1), "r-") plt.plot(y, Pex, "b--") plt.xlabel("$y$") plt.ylabel("$P(y)$") plt.savefig("Py_b" + str(b) + ".png") plt.show() ## Plot Jy against y plt.plot(y, Jy.sum(axis=1), "r-") plt.plot(y, -Pex * y * (b / v - 1) / (x[1] - x[0]), "b--") plt.xlabel("$y$") plt.ylabel("$J_y(y)$") plt.savefig("Jy_b" + str(b) + ".png") plt.show() ## Plot P(x) against x plt.plot(x, P.sum(axis=0), "r-") plt.xlabel("$x$") plt.ylabel("$P(x)$") plt.savefig("Px_b" + str(b) + ".png") plt.show() ## Plot Jx against y plt.plot(y, Jx.sum(axis=1), "r-") plt.plot(y, -Pex * y / (x[1] - x[0]), "b--") plt.xlabel("$y$") plt.ylabel("$J_x(y)$") plt.savefig("Jx_b" + str(b) + ".png") plt.show() exit() ############################## ## Add subplot with exact solution if not BW: ax1 = fig.add_subplot(121, aspect="auto") ax2 = fig.add_subplot(122, aspect="auto", sharey=ax1) plot_exact((ax2, xmax, ymax, b, False)) # fig.tight_layout();fig.subplots_adjust(top=0.93) print me + "Plotting exact", round(time.time() - t0, 1), "seconds" plot_separatrix(ax1, b, xmax, ymax, 2) else: ax1 = fig.add_subplot(111) plot_walls(ax1, X, xmax, ymax, 2) ## Accoutrements ax1.set_xlim([-xmax, xmax]) ax1.set_ylim([-ymax, ymax]) ax1.set_xlabel("$x$", fontsize=fs) ax1.set_ylabel("$\eta$", fontsize=fs) ## Streamplot t0 = time.time() lw = 2 #3.0*Jt/Jt.max() q = 1 ax1.quiver(gx[::q, ::q], gy[::q, ::q], Jx[::q, ::q], Jy[::q, ::q]) # ax1.streamplot(gx,gy, Jx,Jy, arrowsize=1.8, arrowstyle="->", linewidth=lw, minlength=xmax/20) print me + "Plotting streamlines ", round(time.time() - t0, 1), "seconds" t0 = time.time() ## Output fig.savefig(strfile + "FP.png", edgecolor="none") print me + "Plot saved as\n ", strfile + "FP.png" plt.show() plt.close() return