def plot_traj(x, ex, R, S, a, dt, totpts, vb): """ """ me = "test_traj.plot_traj: " fig = plt.figure() ax = fig.gca() cm = plt.get_cmap("GnBu") ## ------------------------------------------------------------------------ ## LINE COLOUR ## Constant colour for each line segment of CH points ## Colour decays away and hits min value. cm = plt.get_cmap("GnBu") CH = int(a / dt / 20) ## Points in a colouring chunk NCH = totpts / CH ## Total number of chunks NCHC = 50 ## Number of coloured chunks, after which line is cmin cmin = 0.3 ## colourlist is linearly declining until NCHC chunks have been coloured, then constant colour colourlist = [ cm(max(1. * (NCHC - i) / (NCHC - 1), cmin)) for i in range(NCHC) ] + [cm(cmin)] * (NCH - NCHC) # colourlist = colourlist[::-1] ax.set_prop_cycle("color", colourlist) ## ------------------------------------------------------------------------ ## Plot segments x = x[::-1] ex = ex[::-1] for i in range(x.shape[0] / CH): ax.plot(x[i * CH:(i + 1) * CH + 1], ex[i * CH:(i + 1) * CH + 1], zorder=1) ## Plot wall xfine = np.linspace(S - 3, R + 3, 100 * (R - S + 6) + 1) ax.plot(xfine, -force_dlin([xfine, 0], R, S)[0], "k-") ax.set_xlim(S - 2, R + 2) ax.set_ylim(-3, +3) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) ax.set_xlabel(r"$x$") ax.set_ylabel(r"$\eta$") ax.grid() return fig
def plot_traj(x,ex,R,S,a,dt,totpts,vb): """ """ me = "test_traj.plot_traj: " fig = plt.figure(); ax = fig.gca() cm = plt.get_cmap("GnBu") ## ------------------------------------------------------------------------ ## LINE COLOUR ## Constant colour for each line segment of CH points ## Colour decays away and hits min value. cm = plt.get_cmap("GnBu") CH = int(a/dt/20) ## Points in a colouring chunk NCH = totpts/CH ## Total number of chunks NCHC = 50 ## Number of coloured chunks, after which line is cmin cmin = 0.3 ## colourlist is linearly declining until NCHC chunks have been coloured, then constant colour colourlist = [cm(max(1.*(NCHC-i)/(NCHC-1),cmin)) for i in range(NCHC)] + [cm(cmin)]*(NCH-NCHC) # colourlist = colourlist[::-1] ax.set_prop_cycle("color", colourlist) ## ------------------------------------------------------------------------ ## Plot segments x=x[::-1]; ex=ex[::-1] for i in range(x.shape[0]/CH): ax.plot(x[i*CH:(i+1)*CH+1],ex[i*CH:(i+1)*CH+1],zorder=1) ## Plot wall xfine = np.linspace(S-3,R+3,100*(R-S+6)+1) ax.plot(xfine, -force_dlin([xfine,0],R,S)[0], "k-") ax.set_xlim(S-2,R+2) ax.set_ylim(-3,+3) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) ax.set_xlabel(r"$x$") ax.set_ylabel(r"$\eta$") ax.grid() return fig
def calc_mass_ratio(histdir, srchstr, noread, vb): """ Read in directory of files with inner and outer regions. Compute mass in each region, take ratio. Compare with integrated and calculated white noise result. """ me = me0 + ".calc_mass_ratio: " t0 = time.time() ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histdir, me + "Functional only for Cartesian geometry." assert "_DL_" not in histdir, me + "Must have interior region." ## File discovery filelist = np.sort(glob.glob(histdir + "/BHIS_CAR_*" + srchstr + "*.npy")) numfiles = len(filelist) assert numfiles > 1, me + "Check input directory." if vb: print me + "found", numfiles, "files" ##------------------------------------------------------------------------- A, ML, MR = np.zeros([3, numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): ## Assume R, S, T are same for all files A[i] = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T = -S ## Space bins = np.load( os.path.dirname(histfile) + "/BHISBIN" + os.path.basename(histfile)[4:-4] + ".npz") xbins = bins["xbins"] x = 0.5 * (xbins[1:] + xbins[:-1]) ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum() * (x[1] - x[0])) ## Mass on either side of cusp: data. Left, right. if "_CL_" in histfile: cuspind = np.abs(0.5 * (T + S) - x).argmin() ## Half-domain elif "_ML_" in histfile: cuspind = np.abs(0.5 * (T + S) - x).argmin() elif "_NL_" in histfile: cuspind = np.abs(S - x).argmin() ML[i] = np.trapz(Qx[:cuspind], x[:cuspind]) MR[i] = np.trapz(Qx[cuspind:], x[cuspind:]) ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] ML = ML[srtidx] MR = MR[srtidx] ##------------------------------------------------------------------------- ## WN result from density solution if "_DL_" in histfile: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R, S)[0] else: raise IOError, me + "Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) MLwn = np.trapz(Qx_WN[:cuspind], x[:cuspind]) MRwn = np.trapz(Qx_WN[cuspind:], x[cuspind:]) ##------------------------------------------------------------------------- ## Add a=0 point if 0.0 not in A: A = np.hstack([0.0, A]) ML = np.hstack([MLwn, ML]) MR = np.hstack([MRwn, MR]) ##------------------------------------------------------------------------- ### This might not be the cleanest thing to save... ## SAVING if not noread: massfile = histdir + "/MASS_" + srchstr + ".npz" np.savez(massfile, A=A, ML=ML, MR=MR, MLwn=MLwn, MRwn=MRwn, x=x, Qx_WN=Qx_WN, R=R, S=S, T=T, cuspind=cuspind) if vb: print me + "Calculations saved to", massfile print me + "Calculation time %.1f seconds." % (time.time() - t0) return { "A": A, "ML": ML, "MR": MR, "MLwn": MLwn, "MRwn": MRwn, "x": x, "Qx_WN": Qx_WN, "R": R, "S": S, "T": T, "cuspind": cuspind }
def plot_pdf1d(histfile, nosave, vb): """ Calculate Q(r) and q(eta) from file and plot. """ me = me0 + ".plot_pdf1d: " t0 = time.time() ##------------------------------------------------------------------------- ## Filename pars assert "_CAR_" in histfile, me + "Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T = -S doQfit = (R == S and "_DL_" in histfile) plotq = int(False) ##------------------------------------------------------------------------- ## Space bins = np.load( os.path.dirname(histfile) + "/BHISBIN" + os.path.basename(histfile)[4:-4] + ".npz") xbins = bins["xbins"] exbins = bins["exbins"] eybins = bins["eybins"] x = 0.5 * (xbins[1:] + xbins[:-1]) etax = 0.5 * (exbins[1:] + exbins[:-1]) etay = 0.5 * (eybins[1:] + eybins[:-1]) ## Wall indices Rind, Sind = np.abs(x - R).argmin(), np.abs(x - S).argmin() ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) rho = H / (H.sum() * (x[1] - x[0]) * (etax[1] - etax[0]) * (etay[1] - etay[0])) ## Spatial density Qx = rho.sum(axis=2).sum(axis=1) * (etax[1] - etax[0]) * (etay[1] - etay[0]) ## Force density qx = rho.sum(axis=2).sum(axis=0) * (x[1] - x[0]) * (etay[1] - etay[0]) qy = rho.sum(axis=1).sum(axis=0) * (x[1] - x[0]) * (etax[1] - etax[0]) ##------------------------------------------------------------------------- ## Fit gauss = lambda x, m, s2: 1 / np.sqrt(2 * np.pi * s2) * np.exp(-0.5 * ( x - m)**2 / s2) if doQfit: fitQx = sp.optimize.curve_fit(gauss, x, Qx, p0=[R, 1 / np.sqrt(1 + a)])[0] ##------------------------------------------------------------------------- ## PLOTTING fig, axs = plt.subplots(1 + plotq, 1, figsize=fs["figsize"]) fig.canvas.set_window_title("1D PDFs") ## Set number of ticks for ax in np.ravel([axs]): ax.xaxis.set_major_locator(MaxNLocator(5)) ax.yaxis.set_major_locator(MaxNLocator(4)) ##------------------------------------------------------------------------- ## Spatial density plot ax = axs[0] if plotq else axs ## Data ax.plot(x, Qx, label=r"OUP") ax.fill_between(x, 0.0, Qx, color="b", alpha=0.1) ## Gaussian for spatial density if doQfit: ax.plot(x, gauss(x, fitQx[0], 1 / (1 + a)), "c-", label=r"$G\left(\mu, \frac{1}{\alpha+1}\right)$") ## Potential and WN if "_DC_" in histfile: fx = force_dcon([x, 0], R, S)[0] elif "_DL_" in histfile: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R, S)[0] else: raise IOError, me + "Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() ## Plot passive density Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) ax.plot(x, Qx_WN, "r-", label="Passive") ax.fill_between(x, 0.0, Qx_WN, color="r", alpha=0.1) ## Plot potential ax.plot(x, U / U.max() * ax.get_ylim()[1], "k--", label=r"$U(x)$") ## Indicate bulk ax.axvline(S, c="k", lw=1) ax.axvline(R, c="k", lw=1) if T >= 0.0: ax.axvspan(S, R, color="y", alpha=0.1) ax.axvline(T, c="k", lw=1) ax.axvspan(-R, T, color="y", alpha=0.1) ax.axvline(-R, c="k", lw=1) elif T < 0.0: ax.axvline(-R, c="k", lw=1) ax.set_xlim(left=x[0], right=x[-1]) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$n(x)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- if plotq: ## Force density plot ax = axs[1] ## Data ax.plot(etax, qx, label=r"Simulation $x$") ax.plot(etay, qy, label=r"Simulation $y$") ## Gaussian ax.plot(etax, gauss(etax, 0.0, 1 / a), "c-", label=r"$G\left(0, \frac{1}{\alpha}\right)$") ax.set_xlabel(r"$\eta$", fontsize=fs["fsa"]) ax.set_ylabel(r"$q(\eta)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- fig.tight_layout() fig.subplots_adjust(top=0.95) title = r"PDFs in $r$ and $\eta$. $\alpha=%.1f, R=%.1f, S=%.1f$"%(a,R,S) if T<0.0\ else r"PDFs in $r$ and $\eta$. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$"%(a,R,S,T) else: title = r"Spatial PDF. $\alpha=%.1f, R=%.1g, S=%.1g$"%(a,R,S) if T<0.0\ else r"Spatial PDF. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.f$"%(a,R,S,T) # fig.suptitle(title, fontsize=fs["fst"]) if not nosave: plotfile = os.path.dirname(histfile) + "/PDFxy1d" + os.path.basename( histfile)[4:-4] plotfile += "." + fs["saveext"] fig.savefig(plotfile, format=fs["saveext"]) if vb: print me + "Figure saved to", plotfile if vb: print me + "Execution time %.1f seconds." % (time.time() - t0) return
HP = np.load(histfileP) bP = np.load(os.path.dirname(histfileP)+"/BHISBIN"+os.path.basename(histfileP)[4:-4]+".npz") HC = np.load(histfileC) bC = np.load(os.path.dirname(histfileC)+"/BHISBIN"+os.path.basename(histfileC)[4:-4]+".npz") r = 0.5*(bP["rbins"][1:]+bP["rbins"][:-1]) x = 0.5*(bC["xbins"][1:]+bC["xbins"][:-1]) ## Spatial density QP = HP.sum(axis=2).sum(axis=1) / (2*np.pi*r) QP /= np.trapz(2*np.pi*r*QP, r) QC = HC.sum(axis=2).sum(axis=1) QC /= np.trapz(QC, x) ## Potential if "_DL_" in histfileP: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfileP: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfileP: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfileP: fx = force_nlin([x,0],R,S)[0] else: raise IOError, me+"Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() ## ------------------------------------------------------------------------ ## 1D if 1: fig, axs = plt.subplots(1,1, figsize=(10,10)) ax = axs ax.plot(r, QP/QP.max(), label=r"$n_{\rm rad}(r)$") ax.plot(x, QC/QC.max(), label=r"$n_{\rm car}(x)$")
def main(): """ """ me = me0+".main: " a = 1.0 timefac = 1.0 dt = 0.01 R = 2.0 S = 0.0 vb = True fx = lambda x: force_dlin([x,0],R,S)[0] outdir = "./Pressure/TRAJ_CAR_DL_a%.1f_R%.1f_S%.1f_t%.1f"%(a,R,S,timefac) if not os.path.isdir(outdir): os.mkdir(outdir) print me+"Created directory",outdir ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2*timefac ## Simulation limits xmax = R+3.0 xmin = S-3.0 ## Injection x coordinate xini = 0.5*(S+R) ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2/dt)*np.random.normal(0.0, 1.0) else: eIC = dt/np.sqrt(a)*np.random.normal(0.0, 1.0) ## Integration algorithm x_step = lambda x, ex: eul(x, ex, fx, dt) ## ---------------------------------------------------------------- ## SIMULATION ## Precompute exp(-t) expmt = np.exp((np.arange(-10*a,dt,dt))/a) x, ex = boundary_sim(xini, eIC, a, x_step, dt, tmax, expmt) ## Coarsen to speed up plotting crsn = 5 if crsn>1: x = x[::crsn] ex = ex[::crsn] ## ---------------------------------------------------------------- ## PLOT TRAJECTORY ## Number of steps per frame nsteps = int(tmax/dt/crsn) stride = 100/crsn ## Make a frame every stride timesteps numf = nsteps/stride ## Loop over frames for fnum in range(numf): ti = time.time() fig = plot_traj(x[:fnum*stride],ex[:fnum*stride],R,S,a,dt,x.shape[0],True) ## Save plotfile = outdir+"/f%04d.png"%(fnum) fig.savefig(plotfile) plt.close() if not fnum%10: print me+"Frame %04d/%04d saved. Time per file %.1f seconds."%(fnum,numf,time.time()-ti) os.system("ffmpeg -r 10 -f image2 -s 1920x1080 -i f%04d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p TRAJ_CAR_DL_a%.1f_R%.1f_S%.1f_t%.1f.mp4"%(a,R,S,t)) return
def calc_pressure_dir(histdir, srchstr, noread, vb): """ Calculate the pressure for all files in directory matching string. The """ me = me0 + ".calc_pressure_dir: " t0 = time.time() ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histdir, me + "Functional only for Cartesian geometry." Casimir = "_DL_" not in histdir ## File discovery filelist = np.sort(glob.glob(histdir + "/BHIS_CAR_*" + srchstr + "*.npy")) numfiles = len(filelist) assert numfiles > 1, me + "Check input directory." if vb: print me + "found", numfiles, "files" ##------------------------------------------------------------------------- A, R, S, T, PR, PS, PT, PU, PR_WN, PS_WN, PT_WN, PU_WN = np.zeros( [12, numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): ti = time.time() ## Assuming R, S, T are same for all files A[i] = filename_par(histfile, "_a") R[i] = filename_par(histfile, "_R") S[i] = filename_par(histfile, "_S") try: T[i] = filename_par(histfile, "_T") except ValueError: T[i] = -S[i] ## Space bins = np.load( os.path.dirname(histfile) + "/BHISBIN" + os.path.basename(histfile)[4:-4] + ".npz") xbins = bins["xbins"] x = 0.5 * (xbins[1:] + xbins[:-1]) ## Wall indices Rind, Sind, Tind = np.abs(x - R[i]).argmin( ), np.abs(x - S[i]).argmin() + 1, np.abs(x - T[i]).argmin() STind = 0 if T[i] < 0.0 else (Sind + Tind) / 2 ## Adjust indices for pressure calculation if "_DC_" in histfile: STind = 0 elif "_DL_" in histfile: STind = 0 elif "_NL_" in histfile: STind = Sind Sind = Rind Tind = x.size - Rind ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum() * (x[1] - x[0])) ##------------------------------------------------------------------------- ## Choose force if "_DC_" in histfile: fx = force_dcon([x, 0], R[i], S[i])[0] elif "_DL_" in histfile: fx = force_dlin([x, 0], R[i], S[i])[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R[i], S[i], T[i])[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R[i], S[i], T[i])[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R[i], S[i])[0] else: raise IOError, me + "Force not recognised." ## Calculate integral pressure PR[i] = -sp.integrate.trapz(fx[Rind:] * Qx[Rind:], x[Rind:]) PS[i] = +sp.integrate.trapz(fx[STind:Sind] * Qx[STind:Sind], x[STind:Sind]) PT[i] = -sp.integrate.trapz(fx[Tind:STind] * Qx[Tind:STind], x[Tind:STind]) if "_ML_" in histfile: mRind = x.size - Rind ## Index of wall at x=-R PU[i] = +sp.integrate.trapz(fx[:mRind] * Qx[:mRind], x[:mRind]) if vb: print me + "a=%.1f:\tPressure calculation %.2g seconds" % ( A[i], time.time() - ti) ## Potential U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) ## WN pressure PR_WN[i] = -sp.integrate.trapz(fx[Rind:] * Qx_WN[Rind:], x[Rind:]) PS_WN[i] = +sp.integrate.trapz(fx[STind:Sind] * Qx_WN[STind:Sind], x[STind:Sind]) if Casimir: PT_WN[i] = -sp.integrate.trapz(fx[Tind:STind] * Qx_WN[Tind:STind], x[Tind:STind]) if "_ML_" in histfile: PU_WN[i] = +sp.integrate.trapz(fx[:mRind] * Qx_WN[:mRind], x[:mRind]) ##------------------------------------------------------------------------- ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] R, S, T = R[srtidx], S[srtidx], T[srtidx] PR, PS, PT, PU = PR[srtidx], PS[srtidx], PT[srtidx], PU[srtidx] PR_WN, PS_WN, PT_WN, PU_WN = PR_WN[srtidx], PS_WN[srtidx], PT_WN[ srtidx], PU_WN[srtidx] ## Normalise PR /= PR_WN + (PR_WN == 0) PS /= PS_WN + (PS_WN == 0) if Casimir: PT /= PT_WN + (PT_WN == 0) if "_ML_" in histdir: PU /= PU_WN + (PU_WN == 0) ##------------------------------------------------------------------------- ## SAVING if not noread: pressfile = histdir + "/PRESS_" + srchstr + ".npz" np.savez(pressfile, A=A, R=R, S=S, T=T, PR=PR, PS=PS, PT=PT, PU=PU, PR_WN=PR_WN, PS_WN=PS_WN, PT_WN=PT_WN, PU_WN=PU_WN) if vb: print me + "Calculations saved to", pressfile print me + "Calculation time %.1f seconds." % (time.time() - t0) return { "A": A, "R": R, "S": S, "T": T, "PR": PR, "PS": PS, "PT": PT, "PU": PU, "PR_WN": PR_WN, "PS_WN": PS_WN, "PT_WN": PT_WN, "PU_WN": PU_WN }
def calc_pressure_dir(histdir, srchstr, noread, vb): """ Calculate the pressure for all files in directory matching string. The """ me = me0+".calc_pressure_dir: " t0 = time.time() ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histdir, me+"Functional only for Cartesian geometry." Casimir = "_DL_" not in histdir ## File discovery filelist = np.sort(glob.glob(histdir+"/BHIS_CAR_*"+srchstr+"*.npy")) numfiles = len(filelist) assert numfiles>1, me+"Check input directory." if vb: print me+"found",numfiles,"files" ##------------------------------------------------------------------------- A, R, S, T, PR, PS, PT, PU, PR_WN, PS_WN, PT_WN, PU_WN = np.zeros([12,numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): ti = time.time() ## Assuming R, S, T are same for all files A[i] = filename_par(histfile, "_a") R[i] = filename_par(histfile, "_R") S[i] = filename_par(histfile, "_S") try: T[i] = filename_par(histfile, "_T") except ValueError: T[i] = -S[i] ## Space bins = np.load(os.path.dirname(histfile)+"/BHISBIN"+os.path.basename(histfile)[4:-4]+".npz") xbins = bins["xbins"] x = 0.5*(xbins[1:]+xbins[:-1]) ## Wall indices Rind, Sind, Tind = np.abs(x-R[i]).argmin(), np.abs(x-S[i]).argmin()+1, np.abs(x-T[i]).argmin() STind = 0 if T[i]<0.0 else (Sind+Tind)/2 ## Adjust indices for pressure calculation if "_DC_" in histfile: STind = 0 elif "_DL_" in histfile: STind = 0 elif "_NL_" in histfile: STind = Sind Sind = Rind Tind = x.size-Rind ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum()*(x[1]-x[0])) ##------------------------------------------------------------------------- ## Choose force if "_DC_" in histfile: fx = force_dcon([x,0],R[i],S[i])[0] elif "_DL_" in histfile: fx = force_dlin([x,0],R[i],S[i])[0] elif "_CL_" in histfile: fx = force_clin([x,0],R[i],S[i],T[i])[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R[i],S[i],T[i])[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R[i],S[i])[0] else: raise IOError, me+"Force not recognised." ## Calculate integral pressure PR[i] = -sp.integrate.trapz(fx[Rind:]*Qx[Rind:], x[Rind:]) PS[i] = +sp.integrate.trapz(fx[STind:Sind]*Qx[STind:Sind], x[STind:Sind]) PT[i] = -sp.integrate.trapz(fx[Tind:STind]*Qx[Tind:STind], x[Tind:STind]) if "_ML_" in histfile: mRind = x.size-Rind ## Index of wall at x=-R PU[i] = +sp.integrate.trapz(fx[:mRind]*Qx[:mRind], x[:mRind]) if vb: print me+"a=%.1f:\tPressure calculation %.2g seconds"%(A[i],time.time()-ti) ## Potential U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) ## WN pressure PR_WN[i] = -sp.integrate.trapz(fx[Rind:]*Qx_WN[Rind:], x[Rind:]) PS_WN[i] = +sp.integrate.trapz(fx[STind:Sind]*Qx_WN[STind:Sind], x[STind:Sind]) if Casimir: PT_WN[i] = -sp.integrate.trapz(fx[Tind:STind]*Qx_WN[Tind:STind], x[Tind:STind]) if "_ML_" in histfile: PU_WN[i] = +sp.integrate.trapz(fx[:mRind]*Qx_WN[:mRind], x[:mRind]) ##------------------------------------------------------------------------- ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] R, S, T = R[srtidx], S[srtidx], T[srtidx] PR, PS, PT, PU = PR[srtidx], PS[srtidx], PT[srtidx], PU[srtidx] PR_WN, PS_WN, PT_WN, PU_WN = PR_WN[srtidx], PS_WN[srtidx], PT_WN[srtidx], PU_WN[srtidx] ## Normalise PR /= PR_WN + (PR_WN==0) PS /= PS_WN + (PS_WN==0) if Casimir: PT /= PT_WN + (PT_WN==0) if "_ML_" in histdir: PU /= PU_WN + (PU_WN==0) ##------------------------------------------------------------------------- ## SAVING if not noread: pressfile = histdir+"/PRESS_"+srchstr+".npz" np.savez(pressfile, A=A, R=R, S=S, T=T, PR=PR, PS=PS, PT=PT, PU=PU, PR_WN=PR_WN, PS_WN=PS_WN, PT_WN=PT_WN, PU_WN=PU_WN) if vb: print me+"Calculations saved to",pressfile print me+"Calculation time %.1f seconds."%(time.time()-t0) return {"A":A,"R":R,"S":S,"T":T,"PR":PR,"PS":PS,"PT":PT,"PU":PU, "PR_WN":PR_WN,"PS_WN":PS_WN,"PT_WN":PT_WN,"PU_WN":PU_WN}
def plot_pdf1d(histfile, nosave, vb): """ Calculate Q(r) and q(eta) from file and plot. """ me = me0+".plot_pdf1d: " t0 = time.time() ##------------------------------------------------------------------------- ## Filename pars assert "_CAR_" in histfile, me+"Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T= -S doQfit = (R==S and "_DL_" in histfile) plotq = int(False) ##------------------------------------------------------------------------- ## Space bins = np.load(os.path.dirname(histfile)+"/BHISBIN"+os.path.basename(histfile)[4:-4]+".npz") xbins = bins["xbins"] exbins = bins["exbins"] eybins = bins["eybins"] x = 0.5*(xbins[1:]+xbins[:-1]) etax = 0.5*(exbins[1:]+exbins[:-1]) etay = 0.5*(eybins[1:]+eybins[:-1]) ## Wall indices Rind, Sind = np.abs(x-R).argmin(), np.abs(x-S).argmin() ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) rho = H / (H.sum() * (x[1]-x[0])*(etax[1]-etax[0])*(etay[1]-etay[0])) ## Spatial density Qx = rho.sum(axis=2).sum(axis=1) * (etax[1]-etax[0])*(etay[1]-etay[0]) ## Force density qx = rho.sum(axis=2).sum(axis=0) * (x[1]-x[0])*(etay[1]-etay[0]) qy = rho.sum(axis=1).sum(axis=0) * (x[1]-x[0])*(etax[1]-etax[0]) ##------------------------------------------------------------------------- ## Fit gauss = lambda x, m, s2: 1/np.sqrt(2*np.pi*s2)*np.exp(-0.5*(x-m)**2/s2) if doQfit: fitQx = sp.optimize.curve_fit(gauss, x, Qx, p0=[R,1/np.sqrt(1+a)])[0] ##------------------------------------------------------------------------- ## PLOTTING fig, axs = plt.subplots(1+plotq,1, figsize=fs["figsize"]) fig.canvas.set_window_title("1D PDFs") ## Set number of ticks for ax in np.ravel([axs]): ax.xaxis.set_major_locator(MaxNLocator(5)) ax.yaxis.set_major_locator(MaxNLocator(4)) ##------------------------------------------------------------------------- ## Spatial density plot ax = axs[0] if plotq else axs ## Data ax.plot(x, Qx, label=r"OUP") ax.fill_between(x, 0.0, Qx, color="b", alpha=0.1) ## Gaussian for spatial density if doQfit: ax.plot(x, gauss(x,fitQx[0],1/(1+a)), "c-", label=r"$G\left(\mu, \frac{1}{\alpha+1}\right)$") ## Potential and WN if "_DC_" in histfile: fx = force_dcon([x,0],R,S)[0] elif "_DL_" in histfile: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R,S)[0] else: raise IOError, me+"Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() ## Plot passive density Qx_WN = np.exp(-U)/np.trapz(np.exp(-U),x) ax.plot(x, Qx_WN, "r-", label="Passive") ax.fill_between(x, 0.0, Qx_WN, color="r", alpha=0.1) ## Plot potential ax.plot(x, U/U.max()*ax.get_ylim()[1], "k--",label=r"$U(x)$") ## Indicate bulk ax.axvline(S,c="k",lw=1) ax.axvline(R,c="k",lw=1) if T>=0.0: ax.axvspan(S,R,color="y",alpha=0.1) ax.axvline(T,c="k",lw=1) ax.axvspan(-R,T,color="y",alpha=0.1) ax.axvline(-R,c="k",lw=1) elif T<0.0: ax.axvline(-R,c="k",lw=1) ax.set_xlim(left=x[0],right=x[-1]) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$n(x)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- if plotq: ## Force density plot ax = axs[1] ## Data ax.plot(etax, qx, label=r"Simulation $x$") ax.plot(etay, qy, label=r"Simulation $y$") ## Gaussian ax.plot(etax, gauss(etax,0.0,1/a), "c-", label=r"$G\left(0, \frac{1}{\alpha}\right)$") ax.set_xlabel(r"$\eta$", fontsize=fs["fsa"]) ax.set_ylabel(r"$q(\eta)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- fig.tight_layout() fig.subplots_adjust(top=0.95) title = r"PDFs in $r$ and $\eta$. $\alpha=%.1f, R=%.1f, S=%.1f$"%(a,R,S) if T<0.0\ else r"PDFs in $r$ and $\eta$. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$"%(a,R,S,T) else: title = r"Spatial PDF. $\alpha=%.1f, R=%.1g, S=%.1g$"%(a,R,S) if T<0.0\ else r"Spatial PDF. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.f$"%(a,R,S,T) # fig.suptitle(title, fontsize=fs["fst"]) if not nosave: plotfile = os.path.dirname(histfile)+"/PDFxy1d"+os.path.basename(histfile)[4:-4] plotfile += "."+fs["saveext"] fig.savefig(plotfile, format=fs["saveext"]) if vb: print me+"Figure saved to",plotfile if vb: print me+"Execution time %.1f seconds."%(time.time()-t0) return
def plot_file(histfile, nosave, vb): """ """ me = me0 + ".plot_file: " ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histfile, me + "Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile ## Get pars from filename a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") if Casimir else -S ## Calculate quantities x, Q, BC = bulk_const(histfile)[:3] ex2 = BC / (Q + (Q == 0.0)) ##------------------------------------------------------------------------- ## Potential if "_DL_" in histfile: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R, S)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() ##------------------------------------------------------------------------- ## Smooth sp.ndimage.gaussian_filter1d(Q, 1.0, order=0, output=Q) sp.ndimage.gaussian_filter1d(BC, 1.0, order=0, output=BC) sp.ndimage.gaussian_filter1d(ex2, 1.0, order=0, output=ex2) ##------------------------------------------------------------------------- ## PLOT fig, ax = plt.subplots(1, 1, figsize=fs["figsize"]) ## Data ax.plot(x, Q / Q.max(), label=r"$n(x)$", lw=2) ax.plot(x, ex2 / ex2.max(), label=r"$\langle\eta_x^2\rangle(x)$", lw=2) ax.plot(x, BC / BC.max(), label=r"$\langle\eta_x^2\rangle \cdot n$", lw=2) ax.plot(x, U / U.max() * ax.get_ylim()[1], "k--", label=r"$U(x)$") ## Indicate bulk region if "_DL_" in histfile: ax.axvspan(S, R, color="yellow", alpha=0.2) ax.axvline(S, c="k", lw=2) ax.axvline(R, c="k", lw=2) elif "_ML_" in histfile: ax.axvspan(S, R, color="yellow", alpha=0.2) ax.axvspan(-R, T, color="yellow", alpha=0.2) ax.axvline(S, c="k", lw=2) ax.axvline(R, c="k", lw=2) ax.axvline(T, c="k", lw=2) ax.axvline(-R, c="k", lw=2) elif "_CL_" in histfile: ax.axvspan(S, R, color="yellow", alpha=0.2) ax.axvspan(0, T, color="yellow", alpha=0.2) ax.axvline(S, c="k", lw=2) ax.axvline(R, c="k", lw=2) ax.axvline(T, c="k", lw=2) ax.axvline(-R, c="k", lw=2) ##------------------------------------------------------------------------- ## ATTRIBUTES ax.set_xlim(left=x[0], right=x[-1]) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) ax.set_xlabel("$x$", fontsize=fs["fsa"]) ax.set_ylabel("Rescaled variable", fontsize=fs["fsa"]) ax.grid() legloc = [0.35, 0.25] if "_ML_" in histfile else [0.32, 0.67] ax.legend(loc=legloc, fontsize=fs["fsl"]).get_frame().set_alpha(0.8) title = r"Bulk Constant. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$."%(a,R,S,T) if T>=0.0\ else r"Bulk Constant. $\alpha=%.1f, R=%.1f, S=%.1f$."%(a,R,S) # fig.suptitle(title,fontsize=fs["fst"]) ## SAVE # ax.set_ylim(top=BC.max()) plotfile = os.path.dirname(histfile) + "/QEe2" + os.path.basename( histfile)[4:-4] + "." + fs["saveext"] if not nosave: fig.savefig(plotfile) if vb: print me + "Figure saved to", plotfile ##------------------------------------------------------------------------- return plotfile
def plot_dir(histdir, srchstr, logplot, nosave, vb): """ For each file in directory, calculate the pressure in both ways for all walls (where applicable) and plot against alpha. """ me = me0 + ".plot_dir: " filelist = np.sort(glob.glob(histdir + "/BHIS_CAR_*" + srchstr + "*.npy")) numfiles = filelist.size if vb: print me + "Found", numfiles, "files." ## Initialise arrays A, pR, pS, pT, PR, PS, PT = np.zeros([7, numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile ## Get pars from filename A[i] = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") if Casimir else -S ## Calculate BC x, Qx, BC = bulk_const(histfile)[:3] ## Wall indices Rind, Sind, Tind = np.abs(x - R).argmin(), np.abs( x - S).argmin(), np.abs(x - T).argmin() STind = 0 if "_DL_" in histfile else (Tind + Sind) / 2 ##--------------------------------------------------------------------- ## Calculate pressure from BC if "_DL_" in histfile: BCsr = BC[Sind:Rind + 1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * BCsr elif "_CL_" in histfile: BCsr = BC[Sind:Rind + 1].mean() BCts = BC[STind] BC0t = BC[0:Tind + 1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * (BCsr - BCts) pT[i] = A[i] * (BC0t - BCts) elif "_ML_" in histfile: BCsr = BC[Sind:Rind + 1].mean() BCts = BC[STind] BCrt = BC[x.size - Rind:Tind + 1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * (-BCsr + BCts) pT[i] = A[i] * (-BCrt + BCts) elif "_NL_" in histfile: BCr = BC[Rind] BCs = BC[Sind] BCmr = BC[x.size - Rind] pR[i] = A[i] * BCr pS[i] = A[i] * (BCs - BCr) pT[i] = A[i] * (BCs - BCmr) ##--------------------------------------------------------------------- ## Calculate pressure from integral ## Choose force if "_DL_" in histfile: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R, S)[0] ## Calculate integral pressure PR[i] = -sp.integrate.trapz(fx[Rind:] * Qx[Rind:], x[Rind:]) PS[i] = +sp.integrate.trapz(fx[STind:Sind] * Qx[STind:Sind], x[STind:Sind]) PT[i] = -sp.integrate.trapz(fx[Tind:STind] * Qx[Tind:STind], x[Tind:STind]) ##--------------------------------------------------------------------- ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] pR, pS, pT = pR[srtidx], pS[srtidx], pT[srtidx] PR, PS, PT = PR[srtidx], PS[srtidx], PT[srtidx] ##------------------------------------------------------------------------- ## Calculate white noise PDF and pressure -- assuming alpha is only varying parameter U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) PR_WN = -sp.integrate.trapz(fx[Rind:] * Qx_WN[Rind:], x[Rind:]) PS_WN = +sp.integrate.trapz(fx[STind:Sind] * Qx_WN[STind:Sind], x[STind:Sind]) PT_WN = -sp.integrate.trapz(fx[Tind:STind] * Qx_WN[Tind:STind], x[Tind:STind]) ## Normalise pR /= PR_WN pS /= PS_WN pT /= PT_WN PR /= PR_WN PS /= PS_WN PT /= PT_WN ##------------------------------------------------------------------------- ## Add a=0 point if 0.0 not in A: nlin = np.unique(S).size A = np.hstack([[0.0] * nlin, A]) pR = np.hstack([[1.0] * nlin, pR]) pS = np.hstack([[1.0] * nlin, pS]) PR = np.hstack([[1.0] * nlin, PR]) PS = np.hstack([[1.0] * nlin, PS]) ##------------------------------------------------------------------------- ## PLOT DATA fig, ax = plt.subplots(1, 1, figsize=fs["figsize"]) sty = ["-", "--", ":"] A += int(logplot) """ lpR = ax.plot(A, pR, "o"+sty[0], label=r"BC pR") lpS = ax.plot(A, pS, "o"+sty[1], c=ax.lines[-1].get_color(), label=r"BC pS") if Casimir: lpT = ax.plot(A, pT, "o"+sty[2], c=ax.lines[-1].get_color(), label=r"BC pT") ax.plot(A, PR, "v"+sty[0], label=r"Int PR") ax.plot(A, PS, "v"+sty[1], c=ax.lines[-1].get_color(), label=r"Int PS") if Casimir: ax.plot(A, PT, "v"+sty[2], c=ax.lines[-1].get_color(), label=r"Int PT") """ lpR = ax.plot(A, 0.5 * (pR + pS), "o--", label=r"$\alpha\left<\eta^2\right>n(x)|^{\rm bulk}$") ax.plot(A, 0.5 * (PR + PS), "v--", label=r"$-\int f(x)n(x) {\rm d}x$") ##------------------------------------------------------------------------- ## ACCOUTREMENTS if logplot: ax.set_xscale("log") ax.set_yscale("log") xlim = (ax.get_xlim()[0], A[-1]) xlabel = r"$1+\alpha$" else: xlim = (0.0, A[-1]) xlabel = r"$\alpha$" ax.set_xlim(xlim) ax.set_ylim(1e-1, 1e+1) ax.set_xlabel(xlabel, fontsize=fs["fsa"]) ax.set_ylabel(r"$P(\alpha)/P^{\rm passive}$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="best", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) title = "Pressure normalised by WN result. $R=%.1f, S=%.1f, T=%.1f.$"%(R,S,T) if T>=0.0\ else "Pressure normalised by WN result. $R=%.1f, S=%.1f.$"%(R,S) # fig.suptitle(title,fontsize=fs["fst"]) ## SAVING plotfile = histdir+"/QEe2_Pa_R%.1f_S%.1f_T%.1f"%(R,S,T) if T>=0.0\ else histdir+"/QEe2_Pa_R%.1f_S%.1f"%(R,S) plotfile += "_loglog" * logplot + "." + fs["saveext"] if not nosave: fig.savefig(plotfile) if vb: print me + "Figure saved to", plotfile return plotfile
def plot_current_1d(histfile, nosave, vb): """ """ me = me0+".plot_current_1d: " t0 = time.time() ##------------------------------------------------------------------------- ## Filename pars a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") ##------------------------------------------------------------------------- ## Space bins = np.load(os.path.dirname(histfile)+"/BHISBIN"+os.path.basename(histfile)[4:-4]+".npz") xbins = bins["xbins"] exbins = bins["exbins"] x = 0.5*(xbins[1:]+xbins[:-1]) ## Double space x = np.hstack([-x[::-1],x]) etax = 0.5*(exbins[1:]+exbins[:-1]) X, ETAX = np.meshgrid(x,etax, indexing="ij") ## Wall indices Rind, Sind = np.abs(x-R).argmin(), np.abs(x-S).argmin() ##------------------------------------------------------------------------- ## Force if "_DL_" in histfile: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] else: raise IOError, me+"Force not recognised." F = fx.repeat(etax.size).reshape([x.size,etax.size]) ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) rho = H.sum(axis=2) / (H.sum() * (x[1]-x[0])*(etax[1]-etax[0])) ## Double space rho = np.vstack([rho[::-1,::-1],rho]) ## Currents Jx = (F + ETAX)*rho Jy = -1/a*ETAX*rho - 1/(a*a)*np.gradient(rho,etax[1]-etax[0])[1] Vx, Vy = Jx/rho, Jy/rho ##------------------------------------------------------------------------- ## SMOOTHING Vy = sp.ndimage.gaussian_filter(Vy, 2.0, order=0) ##------------------------------------------------------------------------- ## PLOTTING plt.rcParams["image.cmap"] = "Greys" fig, ax = plt.subplots(1,1, figsize=fs["figsize"]) fig.canvas.set_window_title("Current in x-eta") ##------------------------------------------------------------------------- ## Data ax.contourf(x, etax, rho.T) sx, se = 50, 5 # sx, se = 20, 2 ax.quiver(x[::sx], etax[::se], Vx.T[::se,::sx], Vy.T[::se,::sx] , scale=2, units='x', width=0.011*2) ## Indicate bulk if 0: ax.axvline(S,c="k",lw=1) ax.axvline(R,c="k",lw=1) ## Set number of ticks # ax.xaxis.set_major_locator(NullLocator()) #MaxNLocator(5) # ax.yaxis.set_major_locator(NullLocator()) #MaxNLocator(4) ax.set_xticks([-S,-0.5*(S+T),T,+0.5*(S+T),+S]) ax.set_xticklabels([""]*5) ax.set_yticks([-0.5*(S+T),0.0,+0.5*(S+T)]) ax.set_yticklabels([""]*3) # ax.set_xlim(left=x[0],right=x[-1]) ax.set_xlim(left=-S*2,right=S*2) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$\eta$", fontsize=fs["fsa"]) ax.grid() # ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- ## Add force line if 1: ax.plot(x, -fx, "k-", label=r"$-f(x)$") ymax = min(3*fx.max(),etax.max()) ax.set_ylim(-ymax,ymax) ##------------------------------------------------------------------------- ## Add in BC line if 1: from LE_CBulkConst import bulk_const x, Q, BC = bulk_const(histfile) ## Double space x = np.hstack([-x[::-1],x]) Q = np.hstack([Q[::-1],Q]) BC = np.hstack([BC[::-1],BC]) ax.plot(x, (Q/Q.max())*0.5*ax.get_ylim()[1]+ax.get_ylim()[0], "b-", lw=4) ax.plot(x, (BC/BC.max())*0.5*ax.get_ylim()[1]+ax.get_ylim()[0], "r-", lw=4) ax2 = ax.twinx() ax2.yaxis.set_major_locator(NullLocator()) ax2.set_ylabel(r"$n$ \& $\left<\eta^2\right>n$ \hfill") ax2.yaxis.set_label_coords(-0.07,0.15) # ax.yaxis.set_label_coords(-0.07,0.5) ##------------------------------------------------------------------------- if not nosave: plotfile = os.path.dirname(histfile)+"/Jxeta"+os.path.basename(histfile)[4:-4] plotfile += "."+fs["saveext"] fig.savefig(plotfile, format=fs["saveext"]) if vb: print me+"Figure saved to",plotfile if vb: print me+"Execution time %.1f seconds."%(time.time()-t0) return
HC = np.load(histfileC) bC = np.load( os.path.dirname(histfileC) + "/BHISBIN" + os.path.basename(histfileC)[4:-4] + ".npz") r = 0.5 * (bP["rbins"][1:] + bP["rbins"][:-1]) x = 0.5 * (bC["xbins"][1:] + bC["xbins"][:-1]) ## Spatial density QP = HP.sum(axis=2).sum(axis=1) / (2 * np.pi * r) QP /= np.trapz(2 * np.pi * r * QP, r) QC = HC.sum(axis=2).sum(axis=1) QC /= np.trapz(QC, x) ## Potential if "_DL_" in histfileP: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfileP: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfileP: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfileP: fx = force_nlin([x, 0], R, S)[0] else: raise IOError, me + "Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() ## ------------------------------------------------------------------------ ## 1D if 1: fig, axs = plt.subplots(1, 1, figsize=(10, 10)) ax = axs ax.plot(r, QP / QP.max(), label=r"$n_{\rm rad}(r)$")
def main(): """ """ me = me0 + ".main: " a = 1.0 timefac = 1.0 dt = 0.01 R = 2.0 S = 0.0 vb = True fx = lambda x: force_dlin([x, 0], R, S)[0] outdir = "./Pressure/TRAJ_CAR_DL_a%.1f_R%.1f_S%.1f_t%.1f" % (a, R, S, timefac) if not os.path.isdir(outdir): os.mkdir(outdir) print me + "Created directory", outdir ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2 * timefac ## Simulation limits xmax = R + 3.0 xmin = S - 3.0 ## Injection x coordinate xini = 0.5 * (S + R) ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2 / dt) * np.random.normal(0.0, 1.0) else: eIC = dt / np.sqrt(a) * np.random.normal(0.0, 1.0) ## Integration algorithm x_step = lambda x, ex: eul(x, ex, fx, dt) ## ---------------------------------------------------------------- ## SIMULATION ## Precompute exp(-t) expmt = np.exp((np.arange(-10 * a, dt, dt)) / a) x, ex = boundary_sim(xini, eIC, a, x_step, dt, tmax, expmt) ## Coarsen to speed up plotting crsn = 5 if crsn > 1: x = x[::crsn] ex = ex[::crsn] ## ---------------------------------------------------------------- ## PLOT TRAJECTORY ## Number of steps per frame nsteps = int(tmax / dt / crsn) stride = 100 / crsn ## Make a frame every stride timesteps numf = nsteps / stride ## Loop over frames for fnum in range(numf): ti = time.time() fig = plot_traj(x[:fnum * stride], ex[:fnum * stride], R, S, a, dt, x.shape[0], True) ## Save plotfile = outdir + "/f%04d.png" % (fnum) fig.savefig(plotfile) plt.close() if not fnum % 10: print me + "Frame %04d/%04d saved. Time per file %.1f seconds." % ( fnum, numf, time.time() - ti) os.system( "ffmpeg -r 10 -f image2 -s 1920x1080 -i f%04d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p TRAJ_CAR_DL_a%.1f_R%.1f_S%.1f_t%.1f.mp4" % (a, R, S, t)) return
def calc_mass_ratio(histdir, srchstr, noread, vb): """ Read in directory of files with inner and outer regions. Compute mass in each region, take ratio. Compare with integrated and calculated white noise result. """ me = me0+".calc_mass_ratio: " t0 = time.time() ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histdir, me+"Functional only for Cartesian geometry." assert "_DL_" not in histdir, me+"Must have interior region." ## File discovery filelist = np.sort(glob.glob(histdir+"/BHIS_CAR_*"+srchstr+"*.npy")) numfiles = len(filelist) assert numfiles>1, me+"Check input directory." if vb: print me+"found",numfiles,"files" ##------------------------------------------------------------------------- A, ML, MR = np.zeros([3,numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): ## Assume R, S, T are same for all files A[i] = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T = -S ## Space bins = np.load(os.path.dirname(histfile)+"/BHISBIN"+os.path.basename(histfile)[4:-4]+".npz") xbins = bins["xbins"] x = 0.5*(xbins[1:]+xbins[:-1]) ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum()*(x[1]-x[0])) ## Mass on either side of cusp: data. Left, right. if "_CL_" in histfile: cuspind = np.abs(0.5*(T+S)-x).argmin() ## Half-domain elif "_ML_" in histfile: cuspind = np.abs(0.5*(T+S)-x).argmin() elif "_NL_" in histfile: cuspind = np.abs(S-x).argmin() ML[i] = np.trapz(Qx[:cuspind],x[:cuspind]) MR[i] = np.trapz(Qx[cuspind:],x[cuspind:]) ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] ML = ML[srtidx]; MR = MR[srtidx] ##------------------------------------------------------------------------- ## WN result from density solution if "_DL_" in histfile: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R,S)[0] else: raise IOError, me+"Force not recognised." U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) MLwn = np.trapz(Qx_WN[:cuspind],x[:cuspind]) MRwn = np.trapz(Qx_WN[cuspind:],x[cuspind:]) ##------------------------------------------------------------------------- ## Add a=0 point if 0.0 not in A: A = np.hstack([0.0,A]) ML = np.hstack([MLwn,ML]) MR = np.hstack([MRwn,MR]) ##------------------------------------------------------------------------- ### This might not be the cleanest thing to save... ## SAVING if not noread: massfile = histdir+"/MASS_"+srchstr+".npz" np.savez(massfile, A=A, ML=ML, MR=MR, MLwn=MLwn, MRwn=MRwn, x=x, Qx_WN=Qx_WN, R=R, S=S, T=T, cuspind=cuspind) if vb: print me+"Calculations saved to",massfile print me+"Calculation time %.1f seconds."%(time.time()-t0) return {"A":A, "ML":ML, "MR":MR, "MLwn":MLwn, "MRwn":MRwn, "x":x, "Qx_WN":Qx_WN, "R":R, "S":S, "T":T, "cuspind":cuspind}
def plot_dir(histdir, srchstr, logplot, nosave, vb): """ For each file in directory, calculate the pressure in both ways for all walls (where applicable) and plot against alpha. """ me = me0+".plot_dir: " filelist = np.sort(glob.glob(histdir+"/BHIS_CAR_*"+srchstr+"*.npy")) numfiles = filelist.size if vb: print me+"Found",numfiles,"files." ## Initialise arrays A, pR, pS, pT, PR, PS, PT = np.zeros([7,numfiles]) ## Retrieve data for i, histfile in enumerate(filelist): Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile ## Get pars from filename A[i] = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") if Casimir else -S ## Calculate BC x, Qx, BC = bulk_const(histfile)[:3] ## Wall indices Rind, Sind, Tind = np.abs(x-R).argmin(), np.abs(x-S).argmin(), np.abs(x-T).argmin() STind = 0 if "_DL_" in histfile else (Tind+Sind)/2 ##--------------------------------------------------------------------- ## Calculate pressure from BC if "_DL_" in histfile: BCsr = BC[Sind:Rind+1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * BCsr elif "_CL_" in histfile: BCsr = BC[Sind:Rind+1].mean() BCts = BC[STind] BC0t = BC[0:Tind+1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * (BCsr - BCts) pT[i] = A[i] * (BC0t - BCts) elif "_ML_" in histfile: BCsr = BC[Sind:Rind+1].mean() BCts = BC[STind] BCrt = BC[x.size-Rind:Tind+1].mean() pR[i] = A[i] * BCsr pS[i] = A[i] * (-BCsr + BCts) pT[i] = A[i] * (-BCrt + BCts) elif "_NL_" in histfile: BCr = BC[Rind] BCs = BC[Sind] BCmr = BC[x.size-Rind] pR[i] = A[i] * BCr pS[i] = A[i] * (BCs - BCr) pT[i] = A[i] * (BCs - BCmr) ##--------------------------------------------------------------------- ## Calculate pressure from integral ## Choose force if "_DL_" in histfile: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R,S)[0] ## Calculate integral pressure PR[i] = -sp.integrate.trapz(fx[Rind:]*Qx[Rind:], x[Rind:]) PS[i] = +sp.integrate.trapz(fx[STind:Sind]*Qx[STind:Sind], x[STind:Sind]) PT[i] = -sp.integrate.trapz(fx[Tind:STind]*Qx[Tind:STind], x[Tind:STind]) ##--------------------------------------------------------------------- ## SORT BY ALPHA srtidx = A.argsort() A = A[srtidx] pR, pS, pT = pR[srtidx], pS[srtidx], pT[srtidx] PR, PS, PT = PR[srtidx], PS[srtidx], PT[srtidx] ##------------------------------------------------------------------------- ## Calculate white noise PDF and pressure -- assuming alpha is only varying parameter U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U),x) PR_WN = -sp.integrate.trapz(fx[Rind:]*Qx_WN[Rind:], x[Rind:]) PS_WN = +sp.integrate.trapz(fx[STind:Sind]*Qx_WN[STind:Sind], x[STind:Sind]) PT_WN = -sp.integrate.trapz(fx[Tind:STind]*Qx_WN[Tind:STind], x[Tind:STind]) ## Normalise pR /= PR_WN; pS /= PS_WN; pT /= PT_WN PR /= PR_WN; PS /= PS_WN; PT /= PT_WN ##------------------------------------------------------------------------- ## Add a=0 point if 0.0 not in A: nlin = np.unique(S).size A = np.hstack([[0.0]*nlin,A]) pR = np.hstack([[1.0]*nlin,pR]) pS = np.hstack([[1.0]*nlin,pS]) PR = np.hstack([[1.0]*nlin,PR]) PS = np.hstack([[1.0]*nlin,PS]) ##------------------------------------------------------------------------- ## PLOT DATA fig, ax = plt.subplots(1,1, figsize=fs["figsize"]) sty = ["-","--",":"] A += int(logplot) """ lpR = ax.plot(A, pR, "o"+sty[0], label=r"BC pR") lpS = ax.plot(A, pS, "o"+sty[1], c=ax.lines[-1].get_color(), label=r"BC pS") if Casimir: lpT = ax.plot(A, pT, "o"+sty[2], c=ax.lines[-1].get_color(), label=r"BC pT") ax.plot(A, PR, "v"+sty[0], label=r"Int PR") ax.plot(A, PS, "v"+sty[1], c=ax.lines[-1].get_color(), label=r"Int PS") if Casimir: ax.plot(A, PT, "v"+sty[2], c=ax.lines[-1].get_color(), label=r"Int PT") """ lpR = ax.plot(A, 0.5*(pR+pS), "o--", label=r"$\alpha\left<\eta^2\right>n(x)|^{\rm bulk}$") ax.plot(A, 0.5*(PR+PS), "v--", label=r"$-\int f(x)n(x) {\rm d}x$") ##------------------------------------------------------------------------- ## ACCOUTREMENTS if logplot: ax.set_xscale("log"); ax.set_yscale("log") xlim = (ax.get_xlim()[0],A[-1]) xlabel = r"$1+\alpha$" else: xlim = (0.0,A[-1]) xlabel = r"$\alpha$" ax.set_xlim(xlim) ax.set_ylim(1e-1,1e+1) ax.set_xlabel(xlabel,fontsize=fs["fsa"]) ax.set_ylabel(r"$P(\alpha)/P^{\rm passive}$",fontsize=fs["fsa"]) ax.grid() ax.legend(loc="best", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) title = "Pressure normalised by WN result. $R=%.1f, S=%.1f, T=%.1f.$"%(R,S,T) if T>=0.0\ else "Pressure normalised by WN result. $R=%.1f, S=%.1f.$"%(R,S) # fig.suptitle(title,fontsize=fs["fst"]) ## SAVING plotfile = histdir+"/QEe2_Pa_R%.1f_S%.1f_T%.1f"%(R,S,T) if T>=0.0\ else histdir+"/QEe2_Pa_R%.1f_S%.1f"%(R,S) plotfile += "_loglog"*logplot+"."+fs["saveext"] if not nosave: fig.savefig(plotfile) if vb: print me+"Figure saved to",plotfile return plotfile
def plot_current_1d(histfile, nosave, vb): """ """ me = me0 + ".plot_current_1d: " t0 = time.time() ##------------------------------------------------------------------------- ## Filename pars a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") ##------------------------------------------------------------------------- ## Space bins = np.load( os.path.dirname(histfile) + "/BHISBIN" + os.path.basename(histfile)[4:-4] + ".npz") xbins = bins["xbins"] exbins = bins["exbins"] x = 0.5 * (xbins[1:] + xbins[:-1]) ## Double space x = np.hstack([-x[::-1], x]) etax = 0.5 * (exbins[1:] + exbins[:-1]) X, ETAX = np.meshgrid(x, etax, indexing="ij") ## Wall indices Rind, Sind = np.abs(x - R).argmin(), np.abs(x - S).argmin() ##------------------------------------------------------------------------- ## Force if "_DL_" in histfile: fx = force_dlin([x, 0], R, S)[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] else: raise IOError, me + "Force not recognised." F = fx.repeat(etax.size).reshape([x.size, etax.size]) ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) rho = H.sum(axis=2) / (H.sum() * (x[1] - x[0]) * (etax[1] - etax[0])) ## Double space rho = np.vstack([rho[::-1, ::-1], rho]) ## Currents Jx = (F + ETAX) * rho Jy = -1 / a * ETAX * rho - 1 / (a * a) * np.gradient( rho, etax[1] - etax[0])[1] Vx, Vy = Jx / rho, Jy / rho ##------------------------------------------------------------------------- ## SMOOTHING Vy = sp.ndimage.gaussian_filter(Vy, 2.0, order=0) ##------------------------------------------------------------------------- ## PLOTTING plt.rcParams["image.cmap"] = "Greys" fig, ax = plt.subplots(1, 1, figsize=fs["figsize"]) fig.canvas.set_window_title("Current in x-eta") ##------------------------------------------------------------------------- ## Data ax.contourf(x, etax, rho.T) sx, se = 50, 5 # sx, se = 20, 2 ax.quiver(x[::sx], etax[::se], Vx.T[::se, ::sx], Vy.T[::se, ::sx], scale=2, units='x', width=0.011 * 2) ## Indicate bulk if 0: ax.axvline(S, c="k", lw=1) ax.axvline(R, c="k", lw=1) ## Set number of ticks # ax.xaxis.set_major_locator(NullLocator()) #MaxNLocator(5) # ax.yaxis.set_major_locator(NullLocator()) #MaxNLocator(4) ax.set_xticks([-S, -0.5 * (S + T), T, +0.5 * (S + T), +S]) ax.set_xticklabels([""] * 5) ax.set_yticks([-0.5 * (S + T), 0.0, +0.5 * (S + T)]) ax.set_yticklabels([""] * 3) # ax.set_xlim(left=x[0],right=x[-1]) ax.set_xlim(left=-S * 2, right=S * 2) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$\eta$", fontsize=fs["fsa"]) ax.grid() # ax.legend(loc="upper right", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- ## Add force line if 1: ax.plot(x, -fx, "k-", label=r"$-f(x)$") ymax = min(3 * fx.max(), etax.max()) ax.set_ylim(-ymax, ymax) ##------------------------------------------------------------------------- ## Add in BC line if 1: from LE_CBulkConst import bulk_const x, Q, BC = bulk_const(histfile) ## Double space x = np.hstack([-x[::-1], x]) Q = np.hstack([Q[::-1], Q]) BC = np.hstack([BC[::-1], BC]) ax.plot(x, (Q / Q.max()) * 0.5 * ax.get_ylim()[1] + ax.get_ylim()[0], "b-", lw=4) ax.plot(x, (BC / BC.max()) * 0.5 * ax.get_ylim()[1] + ax.get_ylim()[0], "r-", lw=4) ax2 = ax.twinx() ax2.yaxis.set_major_locator(NullLocator()) ax2.set_ylabel(r"$n$ \& $\left<\eta^2\right>n$ \hfill") ax2.yaxis.set_label_coords(-0.07, 0.15) # ax.yaxis.set_label_coords(-0.07,0.5) ##------------------------------------------------------------------------- if not nosave: plotfile = os.path.dirname(histfile) + "/Jxeta" + os.path.basename( histfile)[4:-4] plotfile += "." + fs["saveext"] fig.savefig(plotfile, format=fs["saveext"]) if vb: print me + "Figure saved to", plotfile if vb: print me + "Execution time %.1f seconds." % (time.time() - t0) return
def plot_file(histfile, nosave, vb): """ """ me = me0+".plot_file: " ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histfile, me+"Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile ## Get pars from filename a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") T = filename_par(histfile, "_T") if Casimir else -S ## Calculate quantities x, Q, BC = bulk_const(histfile)[:3] ex2 = BC/(Q+(Q==0.0)) ##------------------------------------------------------------------------- ## Potential if "_DL_" in histfile: fx = force_dlin([x,0],R,S)[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R,S)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() ##------------------------------------------------------------------------- ## Smooth sp.ndimage.gaussian_filter1d(Q,1.0,order=0,output=Q) sp.ndimage.gaussian_filter1d(BC,1.0,order=0,output=BC) sp.ndimage.gaussian_filter1d(ex2,1.0,order=0,output=ex2) ##------------------------------------------------------------------------- ## PLOT fig, ax = plt.subplots(1,1, figsize=fs["figsize"]) ## Data ax.plot(x, Q/Q.max(), label=r"$n(x)$",lw=2) ax.plot(x, ex2/ex2.max(), label=r"$\langle\eta_x^2\rangle(x)$",lw=2) ax.plot(x, BC/BC.max(), label=r"$\langle\eta_x^2\rangle \cdot n$",lw=2) ax.plot(x, U/U.max()*ax.get_ylim()[1], "k--", label=r"$U(x)$") ## Indicate bulk region if "_DL_" in histfile: ax.axvspan(S,R, color="yellow",alpha=0.2) ax.axvline(S, c="k",lw=2); ax.axvline(R, c="k",lw=2) elif "_ML_" in histfile: ax.axvspan(S,R, color="yellow",alpha=0.2) ax.axvspan(-R,T, color="yellow",alpha=0.2) ax.axvline(S, c="k",lw=2); ax.axvline(R, c="k",lw=2) ax.axvline(T, c="k",lw=2); ax.axvline(-R, c="k",lw=2) elif "_CL_" in histfile: ax.axvspan(S,R, color="yellow",alpha=0.2) ax.axvspan(0,T, color="yellow",alpha=0.2) ax.axvline(S, c="k",lw=2); ax.axvline(R, c="k",lw=2) ax.axvline(T, c="k",lw=2); ax.axvline(-R, c="k",lw=2) ##------------------------------------------------------------------------- ## ATTRIBUTES ax.set_xlim(left=x[0],right=x[-1]) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) ax.set_xlabel("$x$",fontsize=fs["fsa"]) ax.set_ylabel("Rescaled variable",fontsize=fs["fsa"]) ax.grid() legloc = [0.35,0.25] if "_ML_" in histfile else [0.32,0.67] ax.legend(loc=legloc,fontsize=fs["fsl"]).get_frame().set_alpha(0.8) title = r"Bulk Constant. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$."%(a,R,S,T) if T>=0.0\ else r"Bulk Constant. $\alpha=%.1f, R=%.1f, S=%.1f$."%(a,R,S) # fig.suptitle(title,fontsize=fs["fst"]) ## SAVE # ax.set_ylim(top=BC.max()) plotfile = os.path.dirname(histfile)+"/QEe2"+os.path.basename(histfile)[4:-4]+"."+fs["saveext"] if not nosave: fig.savefig(plotfile) if vb: print me+"Figure saved to",plotfile ##------------------------------------------------------------------------- return plotfile
def plot_pressure_file(histfile, nosave, vb): """ Plot spatial PDF Q(x) and spatially-varying pressure P(x). """ me = me0+".plot_pressure_file: " ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histfile, me+"Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile ##------------------------------------------------------------------------- ## Filename parameters a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T = -S ## Space bins = np.load(os.path.dirname(histfile)+"/BHISBIN"+os.path.basename(histfile)[4:-4]+".npz") xbins = bins["xbins"] x = 0.5*(xbins[1:]+xbins[:-1]) ## Wall indices Rind, Sind, Tind = np.abs(x-R).argmin(), np.abs(x-S).argmin(), np.abs(x-T).argmin() STind = (Sind+Tind)/2 ## Adjust indices for pressure calculation if "_DC_" in histfile: STind = 0 elif "_DL_" in histfile: STind = 0 elif "_NL_" in histfile: STind = Sind Sind = Rind Tind = x.size-Rind ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum()*(x[1]-x[0])) ##------------------------------------------------------------------------- ## Choose force if "_DC_" in histfile: fx = force_dcon([x,0],R,S)[0] elif "_DL_" in histfile: fx = force_dlin([x,0],R[i],S[i])[0] elif "_CL_" in histfile: fx = force_clin([x,0],R,S,T)[0] elif "_ML_" in histfile: fx = force_mlin([x,0],R,S,T)[0] elif "_NL_" in histfile: fx = force_nlin([x,0],R,S)[0] else: raise IOError, me+"Force not recognised." ## Calculate integral pressure PR = -sp.integrate.cumtrapz(fx[Rind:]*Qx[Rind:], x[Rind:], initial=0.0) PS = -sp.integrate.cumtrapz(fx[STind:Sind+1]*Qx[STind:Sind+1], x[STind:Sind+1], initial=0.0); PS -= PS[-1] if Casimir: PT = -sp.integrate.cumtrapz(fx[Tind:STind+1]*Qx[Tind:STind+1], x[Tind:STind+1], initial=0.0) if x[0]<0: R2ind = x.size-Rind PR2 = -sp.integrate.cumtrapz(fx[:R2ind]*Qx[:R2ind], x[:R2ind], initial=0.0); PR2 -= PR2[-1] ##------------------------------------------------------------------------- ## Potential and WN U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) ## WN pressure PR_WN = -sp.integrate.cumtrapz(fx[Rind:]*Qx_WN[Rind:], x[Rind:], initial=0.0) PS_WN = -sp.integrate.cumtrapz(fx[STind:Sind+1]*Qx_WN[STind:Sind+1], x[STind:Sind+1], initial=0.0); PS_WN -= PS_WN[-1] if Casimir: PT_WN = -sp.integrate.cumtrapz(fx[Tind:STind+1]*Qx_WN[Tind:STind+1], x[Tind:STind+1], initial=0.0) ##------------------------------------------------------------------------- ## PLOTTING fig, axs = plt.subplots(2,1, sharex=True, figsize=fs["figsize"]) if "_DL_" in histfile: legloc = "upper right" elif "_CL_" in histfile: legloc = "upper right" elif "_ML_" in histfile: legloc = "upper left" elif "_NL_" in histfile: legloc = "lower left" else: legloc = "best" ## Plot PDF ax = axs[0] lQ = ax.plot(x, Qx, lw=2, label=r"CN") ax.plot(x, Qx_WN, lQ[0].get_color()+":", lw=2, label="WN") ## Potential ax.plot(x, U/U.max()*ax.get_ylim()[1], "k--", lw=2, label=r"$U(x)$") ax.set_xlim((x[0],x[-1])) ax.set_ylim(bottom=0.0) ax.set_ylabel(r"$Q(x)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc=legloc, fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ## Plot pressure ax = axs[1] lPR = ax.plot(x[Rind:], PR, lw=2, label=r"$P_R$") lPS = ax.plot(x[STind:Sind+1], PS, lw=2, label=r"$P_S$") if Casimir: lPT = ax.plot(x[Tind:STind+1], PT, lw=2, label=r"$P_T$") if x[0]<0: ax.plot(x[:R2ind], PR2, lPR[0].get_color()+"-", lw=2) ## WN result ax.plot(x[Rind:], PR_WN, lPR[0].get_color()+":", lw=2) ax.plot(x[STind:Sind+1], PS_WN, lPS[0].get_color()+":", lw=2) if Casimir: ax.plot(x[Tind:STind+1], PT_WN, lPT[0].get_color()+":", lw=2) if x[0]<0: ax.plot(x[:R2ind], PR_WN[::-1], lPR[0].get_color()+":", lw=2) ## Potential ax.plot(x, U/U.max()*ax.get_ylim()[1], "k--", lw=2)#, label=r"$U(x)$") ax.set_xlim((x[0],x[-1])) ax.set_ylim(bottom=0.0) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$P(x)$", fontsize=fs["fsa"]) ax.grid() if Casimir: ax.legend(loc=legloc, fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- fig.tight_layout() fig.subplots_adjust(top=0.90) title = r"Spatial PDF and Pressure. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$"%(a,R,S,T) if T>=0.0\ else r"Spatial PDF and Pressure. $\alpha=%.1f, R=%.1f, S=%.1f$"%(a,R,S) fig.suptitle(title, fontsize=fs["fst"]) if not nosave: plotfile = os.path.dirname(histfile)+"/PDFP"+os.path.basename(histfile)[4:-4]+".jpg" fig.savefig(plotfile) if vb: print me+"Figure saved to",plotfile return
def plot_pressure_file(histfile, nosave, vb): """ Plot spatial PDF Q(x) and spatially-varying pressure P(x). """ me = me0 + ".plot_pressure_file: " ##------------------------------------------------------------------------- ## Dir pars assert "_CAR_" in histfile, me + "Functional only for Cartesian geometry." Casimir = "_CL_" in histfile or "_ML_" in histfile or "_NL_" in histfile ##------------------------------------------------------------------------- ## Filename parameters a = filename_par(histfile, "_a") R = filename_par(histfile, "_R") S = filename_par(histfile, "_S") try: T = filename_par(histfile, "_T") except ValueError: T = -S ## Space bins = np.load( os.path.dirname(histfile) + "/BHISBIN" + os.path.basename(histfile)[4:-4] + ".npz") xbins = bins["xbins"] x = 0.5 * (xbins[1:] + xbins[:-1]) ## Wall indices Rind, Sind, Tind = np.abs(x - R).argmin(), np.abs(x - S).argmin(), np.abs( x - T).argmin() STind = (Sind + Tind) / 2 ## Adjust indices for pressure calculation if "_DC_" in histfile: STind = 0 elif "_DL_" in histfile: STind = 0 elif "_NL_" in histfile: STind = Sind Sind = Rind Tind = x.size - Rind ##------------------------------------------------------------------------- ## Histogram H = np.load(histfile) ## Spatial density Qx = H.sum(axis=2).sum(axis=1) / (H.sum() * (x[1] - x[0])) ##------------------------------------------------------------------------- ## Choose force if "_DC_" in histfile: fx = force_dcon([x, 0], R, S)[0] elif "_DL_" in histfile: fx = force_dlin([x, 0], R[i], S[i])[0] elif "_CL_" in histfile: fx = force_clin([x, 0], R, S, T)[0] elif "_ML_" in histfile: fx = force_mlin([x, 0], R, S, T)[0] elif "_NL_" in histfile: fx = force_nlin([x, 0], R, S)[0] else: raise IOError, me + "Force not recognised." ## Calculate integral pressure PR = -sp.integrate.cumtrapz(fx[Rind:] * Qx[Rind:], x[Rind:], initial=0.0) PS = -sp.integrate.cumtrapz(fx[STind:Sind + 1] * Qx[STind:Sind + 1], x[STind:Sind + 1], initial=0.0) PS -= PS[-1] if Casimir: PT = -sp.integrate.cumtrapz(fx[Tind:STind + 1] * Qx[Tind:STind + 1], x[Tind:STind + 1], initial=0.0) if x[0] < 0: R2ind = x.size - Rind PR2 = -sp.integrate.cumtrapz( fx[:R2ind] * Qx[:R2ind], x[:R2ind], initial=0.0) PR2 -= PR2[-1] ##------------------------------------------------------------------------- ## Potential and WN U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() Qx_WN = np.exp(-U) / np.trapz(np.exp(-U), x) ## WN pressure PR_WN = -sp.integrate.cumtrapz( fx[Rind:] * Qx_WN[Rind:], x[Rind:], initial=0.0) PS_WN = -sp.integrate.cumtrapz(fx[STind:Sind + 1] * Qx_WN[STind:Sind + 1], x[STind:Sind + 1], initial=0.0) PS_WN -= PS_WN[-1] if Casimir: PT_WN = -sp.integrate.cumtrapz( fx[Tind:STind + 1] * Qx_WN[Tind:STind + 1], x[Tind:STind + 1], initial=0.0) ##------------------------------------------------------------------------- ## PLOTTING fig, axs = plt.subplots(2, 1, sharex=True, figsize=fs["figsize"]) if "_DL_" in histfile: legloc = "upper right" elif "_CL_" in histfile: legloc = "upper right" elif "_ML_" in histfile: legloc = "upper left" elif "_NL_" in histfile: legloc = "lower left" else: legloc = "best" ## Plot PDF ax = axs[0] lQ = ax.plot(x, Qx, lw=2, label=r"CN") ax.plot(x, Qx_WN, lQ[0].get_color() + ":", lw=2, label="WN") ## Potential ax.plot(x, U / U.max() * ax.get_ylim()[1], "k--", lw=2, label=r"$U(x)$") ax.set_xlim((x[0], x[-1])) ax.set_ylim(bottom=0.0) ax.set_ylabel(r"$Q(x)$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc=legloc, fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ## Plot pressure ax = axs[1] lPR = ax.plot(x[Rind:], PR, lw=2, label=r"$P_R$") lPS = ax.plot(x[STind:Sind + 1], PS, lw=2, label=r"$P_S$") if Casimir: lPT = ax.plot(x[Tind:STind + 1], PT, lw=2, label=r"$P_T$") if x[0] < 0: ax.plot(x[:R2ind], PR2, lPR[0].get_color() + "-", lw=2) ## WN result ax.plot(x[Rind:], PR_WN, lPR[0].get_color() + ":", lw=2) ax.plot(x[STind:Sind + 1], PS_WN, lPS[0].get_color() + ":", lw=2) if Casimir: ax.plot(x[Tind:STind + 1], PT_WN, lPT[0].get_color() + ":", lw=2) if x[0] < 0: ax.plot(x[:R2ind], PR_WN[::-1], lPR[0].get_color() + ":", lw=2) ## Potential ax.plot(x, U / U.max() * ax.get_ylim()[1], "k--", lw=2) #, label=r"$U(x)$") ax.set_xlim((x[0], x[-1])) ax.set_ylim(bottom=0.0) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]) ax.set_ylabel(r"$P(x)$", fontsize=fs["fsa"]) ax.grid() if Casimir: ax.legend(loc=legloc, fontsize=fs["fsl"]).get_frame().set_alpha(0.5) ##------------------------------------------------------------------------- fig.tight_layout() fig.subplots_adjust(top=0.90) title = r"Spatial PDF and Pressure. $\alpha=%.1f, R=%.1f, S=%.1f, T=%.1f$"%(a,R,S,T) if T>=0.0\ else r"Spatial PDF and Pressure. $\alpha=%.1f, R=%.1f, S=%.1f$"%(a,R,S) fig.suptitle(title, fontsize=fs["fst"]) if not nosave: plotfile = os.path.dirname(histfile) + "/PDFP" + os.path.basename( histfile)[4:-4] + ".jpg" fig.savefig(plotfile) if vb: print me + "Figure saved to", plotfile return