def UP_CL(ax,R,S,T): """ Plot interior walls of CL potential with Pin Pout annotation. """ if ax==None: fig, ax = plt.subplots(1,1, figsize=fs["figsize"]) if R==None: R = 2.0 S = 1.0 T = 0.0 x = np.linspace(-S-2,+S+2,1000) fx = force_clin([x,0],R,S,T)[0] U = -sp.integrate.cumtrapz(fx,x,initial=0.0); U-=U.min() ax.plot(x, U, "k-", lw=2) textsize = fs["fsa"]-4 Poutpos = 1.2 ## Pout right ax.text(Poutpos, 0.70*U.max(), r"$\mathbf{\Leftarrow}$", fontsize=textsize, horizontalalignment="left", color="g") ax.text(Poutpos, 0.75*U.max(), r"$\mathbf{\Leftarrow P_{\rm out}}$", fontsize=textsize, horizontalalignment="left", color="g") ax.text(Poutpos, 0.80*U.max(), r"$\mathbf{\Leftarrow}$", fontsize=textsize, horizontalalignment="left", color="g") ## Pin right left ax.text(0, 0.70*U.max(), r"$\mathbf{\Leftarrow \qquad \Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ax.text(0, 0.75*U.max(), r"$\mathbf{\Leftarrow P_{\rm in}\Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ax.text(0, 0.80*U.max(), r"$\mathbf{\Leftarrow \qquad \Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ## Pout left ax.text(-Poutpos, 0.70*U.max(), r"$\mathbf{\Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.text(-Poutpos, 0.75*U.max(), r"$\mathbf{P_{\rm out} \Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.text(-Poutpos, 0.80*U.max(), r"$\mathbf{\Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.set_xlim(-R,R) ax.set_ylim(0,1.2*ax.get_ylim()[1]) ax.set_xlabel(r"$x$", fontsize=fs["fsa"]-4) ax.set_ylabel(r"$U$", fontsize=fs["fsa"]-4) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) return
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
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 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 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_pressure_dir(histdir, srchstr, logplot, nosave, noread, vb): """ Plot the pressure for all files in directory matching string. """ me = me0+".plot_pressure_dir: " t0 = time.time() ##------------------------------------------------------------------------- ## Read in existing data or calculate afresh try: assert noread == False pressdata = np.load(histdir+"/PRESS_"+srchstr+".npz") print me+"Pressure data file found:",histdir+"/PRESS_"+srchstr+".npz" except (IOError, AssertionError): print me+"No pressure data found. Calculating from histfiles." pressdata = calc_pressure_dir(histdir, srchstr, noread, vb) A = pressdata["A"] R = pressdata["R"] S = pressdata["S"] T = pressdata["T"] PR = pressdata["PR"] PS = pressdata["PS"] PT = pressdata["PT"] PU = pressdata["PU"] PR_WN = pressdata["PR_WN"] PS_WN = pressdata["PS_WN"] PT_WN = pressdata["PT_WN"] PU_WN = pressdata["PU_WN"] del pressdata Casimir = "_DL_" not in histdir and "_DC_" not in histdir ##------------------------------------------------------------------------- ## FIT if DL if 0 and not Casimir: ## Fit log fitfunc = lambda x, m, c: m*x + c Au = np.unique(A) + int(logplot) for Si in np.unique(S): fitPR = sp.optimize.curve_fit(fitfunc, np.log(1+Au), np.log(PR[S==Si]), p0=[-0.5,+1.0])[0] fitPS = sp.optimize.curve_fit(fitfunc, np.log(1+Au), np.log(PS[S==Si]), p0=[-0.5,+1.0])[0] if vb: print me+"Fit S=%.1f: PR=%.1f*(1+a)^(%.2f), PS=%.1f*(1+a)^(%.2f)"%\ (Si,np.exp(fitPR[1]),fitPR[0],np.exp(fitPS[1]),fitPS[0]) ##------------------------------------------------------------------------- ## PLOTTING t0 = time.time() fig, ax = plt.subplots(1,1, figsize=fs["figsize"]) sty = ["-","--",":"] ## Add a=0 point if 0.0 not in A: nlin = np.unique(S).size A = np.hstack([[0.0]*nlin,A]) R = np.hstack([R[:nlin],R]) S = np.hstack([S[:nlin],S]) T = np.hstack([T[:nlin],T]) PR = np.hstack([[1.0]*nlin,PR]) PS = np.hstack([[1.0]*nlin,PS]) PT = np.hstack([[1.0]*nlin,PT]) PU = np.hstack([[1.0]*nlin,PU]) Au = np.unique(A) + int(logplot) ##------------------------------------------------------------------------- ## Hold R & T fixed and vary S if np.unique(R).size==1: plotfile = histdir+"/PAS_R%.1f_S%.1f_T%.1f."%(R[0],S[0],T[0])+fs["saveext"] if T[0]>=0.0\ else histdir+"/PAS_R%.1f_S%.1f."%(R[0],S[0])+fs["saveext"] title = r"Pressure as a function of $\alpha$ for $R=%.1f,T=%.1f$"%(R[0],T[0]) if T[0]>=0.0\ else r"Pressure as a function of $\alpha$ for $R=%.2f$"%(R[0]) ## To plot a single S if np.unique(S).size==1: ax.plot(Au, PR, "gv--", label=r"$P_R$", zorder=2) ax.plot(Au, PS, "go-", label=r"$P_S$", zorder=2) ax.plot(Au, PT, "bo-", label=r"$P_T$", zorder=2) if "_ML_" in histdir: ax.plot(Au, PU, "bv--", label=r"$P_U$", zorder=2) ax.plot(Au, -(PR-PS+PT-PU), "ks:", label=r"Net", zorder=2) ##--------------------------------- ## Casimir insets if "_CL_" in histdir: ## Pressure key left, bottom, width, height = [0.25, 0.15, 0.63, 0.4] axin = fig.add_axes([left, bottom, width, height]) UP_CL(axin,3.0,2.0,0.0) axin.patch.set_alpha(0.3) ## Potential sketch left, bottom, width, height = [0.18, 0.75, 0.25, 0.13] axin = fig.add_axes([left, bottom, width, height]) x = np.linspace(-R[0]-2,+R[0]+2,1000) fx = force_clin([x,0],R[0],S[0],T[0])[0] U = -sp.integrate.cumtrapz(fx,x,initial=0.0); U-=U.min() axin.plot(x, U, "k-", lw=2, zorder=1) axin.set_xlim(x[0],x[-1]) # axin.set_ylim(0,1.2*ax.get_ylim()[1]) axin.set_xlabel(r"$x$", fontsize=fs["fsa"]-4) axin.set_ylabel(r"$U$", fontsize=fs["fsa"]-4) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.patch.set_alpha(0.3) ##--------------------------------- ## Single wall insets elif "_ML_" in histdir: ## Plot potential as inset Uleft, Ubottom, Uwidth, Uheight = [0.21, 0.18, 0.30, 0.21] Rschem, Sschem, Tschem = 4.0, 2.0, 0.0 x = np.linspace(-Rschem-2.0,+Rschem+2.0,501) fx = force_mlin([x,0],Rschem,Sschem,Tschem)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() cuspind = np.abs(x-0.5*(Sschem+Tschem)).argmin() axin = fig.add_axes([Uleft, Ubottom, Uwidth, Uheight]) axin.plot(x, U, "k-") axin.axvspan(x[0],x[cuspind], color="b",alpha=0.2) axin.axvspan(x[cuspind],x[-1], color="g",alpha=0.2) axin.set_xlim(x[0], x[-1]) axin.set_ylim(top=3*U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.set_yticks([1.0]) axin.set_yticklabels(["1"]) axin.grid() axin.set_xlabel(r"$x$", fontsize = fs["fsa"]-5) axin.set_ylabel(r"$U/T$", fontsize = fs["fsa"]-5) ## Pressure key # left, bottom, width, height = [0.25, 0.15, 0.63, 0.4] # axin = fig.add_axes([left, bottom, width, height]) # UP_ML(axin,R[0],S[0],T[0]) # axin.patch.set_alpha(0.1) ##--------------------------------- ## If S varies else: for Si in np.unique(S)[::-1]: if Casimir: ax.plot(Au, PR[S==Si], "o"+sty[0], label=r"$S=%.1f$"%(Si)) ax.plot(Au, PS[S==Si], "o"+sty[1], c=ax.lines[-1].get_color()) ax.plot(Au, PT[S==Si], "o"+sty[2], c=ax.lines[-1].get_color()) else: ## If DL, label should be bulk width ### One line, average. P = 0.5*(PR+PS) ax.plot(Au, PR[S==Si], "o"+sty[0], label=r"$L=%.1f$"%(R[0]-Si)) # ax.plot(Au, PR[S==Si], "o"+sty[0], label=r"$S=%.1f$"%(Si)) # ax.plot(Au, PS[S==Si], "o"+sty[1], c=ax.lines[-1].get_color()) ## Inset of potential left, bottom, width, height = [0.19, 0.58, 0.35, 0.30] axin = fig.add_axes([left, bottom, width, height]) if "_DL_" in histdir: plot_U1D_Cartesian(axin, "dlin", 2.0, 0.0, 0.0) elif "_DC_" in histdir: plot_U1D_Cartesian(axin, "dcon", 1.0, -1.0, 0.0) ## axin.patch.set_alpha(0.5) ## Prediction for zero and infinite bulk if "_DL_" in histdir: ax.plot(Au, (Au)**(-0.5), "--", c=ax.lines[0].get_color()) ax.plot(Au, np.ones(Au.size), "y--", label=r"$L\to\infty$") if "_DC_" in histdir: ax.plot(Au, np.ones(Au.size), "y--", label=r"$L\to\infty$") ##------------------------------------------------------------------------- ## Plot appearance if logplot: ax.set_xscale("log"); ax.set_yscale("log") ax.set_xlim((ax.get_xlim()[0],A[-1]+1)) xlabel = r"$1+\frac{k\tau}{\zeta}$" plotfile = plotfile[:-4]+"_loglog."+fs["saveext"] else: ax.set_xlim((0.0,A[-1])) ax.set_ylim(bottom=0.0,top=max(1.2*ax.get_ylim()[1],1.0)) xlabel = r"$\alpha$" # ax.set_ylim(1e-1,1e1) ax.set_xlabel(xlabel, fontsize=fs["fsa"]) ax.set_ylabel(r"$P/P^{\rm passive}$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="best", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) # fig.suptitle(title, fontsize=fs["fst"]) if not nosave: fig.savefig(plotfile) if vb: print me+"Figure saved to",plotfile if vb: print me+"Plotting time %.1f seconds."%(time.time()-t0) 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 UP_CL(ax, R, S, T): """ Plot interior walls of CL potential with Pin Pout annotation. """ if ax == None: fig, ax = plt.subplots(1, 1, figsize=fs["figsize"]) if R == None: R = 2.0 S = 1.0 T = 0.0 x = np.linspace(-S - 2, +S + 2, 1000) fx = force_clin([x, 0], R, S, T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() ax.plot(x, U, "k-", lw=2) textsize = fs["fsa"] - 4 Poutpos = 1.2 ## Pout right ax.text(Poutpos, 0.70 * U.max(), r"$\mathbf{\Leftarrow}$", fontsize=textsize, horizontalalignment="left", color="g") ax.text(Poutpos, 0.75 * U.max(), r"$\mathbf{\Leftarrow P_{\rm out}}$", fontsize=textsize, horizontalalignment="left", color="g") ax.text(Poutpos, 0.80 * U.max(), r"$\mathbf{\Leftarrow}$", fontsize=textsize, horizontalalignment="left", color="g") ## Pin right left ax.text(0, 0.70 * U.max(), r"$\mathbf{\Leftarrow \qquad \Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ax.text(0, 0.75 * U.max(), r"$\mathbf{\Leftarrow P_{\rm in}\Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ax.text(0, 0.80 * U.max(), r"$\mathbf{\Leftarrow \qquad \Rightarrow}$", fontsize=textsize, horizontalalignment="center", color="b") ## Pout left ax.text(-Poutpos, 0.70 * U.max(), r"$\mathbf{\Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.text(-Poutpos, 0.75 * U.max(), r"$\mathbf{P_{\rm out} \Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.text(-Poutpos, 0.80 * U.max(), r"$\mathbf{\Rightarrow}$", fontsize=textsize, horizontalalignment="right", color="g") ax.set_xlim(-R, R) ax.set_ylim(0, 1.2 * ax.get_ylim()[1]) ax.set_xlabel(r"$x$", fontsize=fs["fsa"] - 4) ax.set_ylabel(r"$U$", fontsize=fs["fsa"] - 4) ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) return
##============================================================================= ## 3D force CARTESIAN if 0: R = 4.0 S = 2.0 T = 0.0 x = np.linspace(-R - 3, R + 3, 500) ## For nlin, mlin, clin # x = np.linspace(S-4,R+4,500) ## For dlin y = np.linspace(0, 1, 50) X, Y = np.meshgrid(x, y, indexing="ij") fx = force_clin([x, y], R, S, T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() U = np.meshgrid(U, y, indexing="ij")[0] ## Plotting fig = plt.figure() ax = fig.gca(projection="3d") ## 3D contour plot ax.plot_surface(X, Y, U, alpha=0.2, rstride=3, cstride=3,
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
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)$")
##============================================================================= ## 3D force CARTESIAN if 0: R = 4.0 S = 2.0 T = 0.0 x = np.linspace(-R-3,R+3,500) ## For nlin, mlin, clin # x = np.linspace(S-4,R+4,500) ## For dlin y = np.linspace(0,1,50) X, Y = np.meshgrid(x, y, indexing="ij") fx = force_clin([x,y],R,S,T)[0] U = -sp.integrate.cumtrapz(fx,x,initial=0.0); U-=U.min() U = np.meshgrid(U, y, indexing="ij")[0] ## Plotting fig = plt.figure() ax = fig.gca(projection="3d") ## 3D contour plot ax.plot_surface(X, Y, U, alpha=0.2, rstride=3, cstride=3, antialiased=True) ## Angle ax.elev = 15 ax.azim = 75 ax.set_xlabel(r"$x$")
def plot_pressure_dir(histdir, srchstr, logplot, nosave, noread, vb): """ Plot the pressure for all files in directory matching string. """ me = me0 + ".plot_pressure_dir: " t0 = time.time() ##------------------------------------------------------------------------- ## Read in existing data or calculate afresh try: assert noread == False pressdata = np.load(histdir + "/PRESS_" + srchstr + ".npz") print me + "Pressure data file found:", histdir + "/PRESS_" + srchstr + ".npz" except (IOError, AssertionError): print me + "No pressure data found. Calculating from histfiles." pressdata = calc_pressure_dir(histdir, srchstr, noread, vb) A = pressdata["A"] R = pressdata["R"] S = pressdata["S"] T = pressdata["T"] PR = pressdata["PR"] PS = pressdata["PS"] PT = pressdata["PT"] PU = pressdata["PU"] PR_WN = pressdata["PR_WN"] PS_WN = pressdata["PS_WN"] PT_WN = pressdata["PT_WN"] PU_WN = pressdata["PU_WN"] del pressdata Casimir = "_DL_" not in histdir and "_DC_" not in histdir ##------------------------------------------------------------------------- ## FIT if DL if 0 and not Casimir: ## Fit log fitfunc = lambda x, m, c: m * x + c Au = np.unique(A) + int(logplot) for Si in np.unique(S): fitPR = sp.optimize.curve_fit(fitfunc, np.log(1 + Au), np.log(PR[S == Si]), p0=[-0.5, +1.0])[0] fitPS = sp.optimize.curve_fit(fitfunc, np.log(1 + Au), np.log(PS[S == Si]), p0=[-0.5, +1.0])[0] if vb: print me+"Fit S=%.1f: PR=%.1f*(1+a)^(%.2f), PS=%.1f*(1+a)^(%.2f)"%\ (Si,np.exp(fitPR[1]),fitPR[0],np.exp(fitPS[1]),fitPS[0]) ##------------------------------------------------------------------------- ## PLOTTING t0 = time.time() fig, ax = plt.subplots(1, 1, figsize=fs["figsize"]) sty = ["-", "--", ":"] ## Add a=0 point if 0.0 not in A: nlin = np.unique(S).size A = np.hstack([[0.0] * nlin, A]) R = np.hstack([R[:nlin], R]) S = np.hstack([S[:nlin], S]) T = np.hstack([T[:nlin], T]) PR = np.hstack([[1.0] * nlin, PR]) PS = np.hstack([[1.0] * nlin, PS]) PT = np.hstack([[1.0] * nlin, PT]) PU = np.hstack([[1.0] * nlin, PU]) Au = np.unique(A) + int(logplot) ##------------------------------------------------------------------------- ## Hold R & T fixed and vary S if np.unique(R).size == 1: plotfile = histdir+"/PAS_R%.1f_S%.1f_T%.1f."%(R[0],S[0],T[0])+fs["saveext"] if T[0]>=0.0\ else histdir+"/PAS_R%.1f_S%.1f."%(R[0],S[0])+fs["saveext"] title = r"Pressure as a function of $\alpha$ for $R=%.1f,T=%.1f$"%(R[0],T[0]) if T[0]>=0.0\ else r"Pressure as a function of $\alpha$ for $R=%.2f$"%(R[0]) ## To plot a single S if np.unique(S).size == 1: ax.plot(Au, PR, "gv--", label=r"$P_R$", zorder=2) ax.plot(Au, PS, "go-", label=r"$P_S$", zorder=2) ax.plot(Au, PT, "bo-", label=r"$P_T$", zorder=2) if "_ML_" in histdir: ax.plot(Au, PU, "bv--", label=r"$P_U$", zorder=2) ax.plot(Au, -(PR - PS + PT - PU), "ks:", label=r"Net", zorder=2) ##--------------------------------- ## Casimir insets if "_CL_" in histdir: ## Pressure key left, bottom, width, height = [0.25, 0.15, 0.63, 0.4] axin = fig.add_axes([left, bottom, width, height]) UP_CL(axin, 3.0, 2.0, 0.0) axin.patch.set_alpha(0.3) ## Potential sketch left, bottom, width, height = [0.18, 0.75, 0.25, 0.13] axin = fig.add_axes([left, bottom, width, height]) x = np.linspace(-R[0] - 2, +R[0] + 2, 1000) fx = force_clin([x, 0], R[0], S[0], T[0])[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() axin.plot(x, U, "k-", lw=2, zorder=1) axin.set_xlim(x[0], x[-1]) # axin.set_ylim(0,1.2*ax.get_ylim()[1]) axin.set_xlabel(r"$x$", fontsize=fs["fsa"] - 4) axin.set_ylabel(r"$U$", fontsize=fs["fsa"] - 4) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.patch.set_alpha(0.3) ##--------------------------------- ## Single wall insets elif "_ML_" in histdir: ## Plot potential as inset Uleft, Ubottom, Uwidth, Uheight = [0.21, 0.18, 0.30, 0.21] Rschem, Sschem, Tschem = 4.0, 2.0, 0.0 x = np.linspace(-Rschem - 2.0, +Rschem + 2.0, 501) fx = force_mlin([x, 0], Rschem, Sschem, Tschem)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() cuspind = np.abs(x - 0.5 * (Sschem + Tschem)).argmin() axin = fig.add_axes([Uleft, Ubottom, Uwidth, Uheight]) axin.plot(x, U, "k-") axin.axvspan(x[0], x[cuspind], color="b", alpha=0.2) axin.axvspan(x[cuspind], x[-1], color="g", alpha=0.2) axin.set_xlim(x[0], x[-1]) axin.set_ylim(top=3 * U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.set_yticks([1.0]) axin.set_yticklabels(["1"]) axin.grid() axin.set_xlabel(r"$x$", fontsize=fs["fsa"] - 5) axin.set_ylabel(r"$U/T$", fontsize=fs["fsa"] - 5) ## Pressure key # left, bottom, width, height = [0.25, 0.15, 0.63, 0.4] # axin = fig.add_axes([left, bottom, width, height]) # UP_ML(axin,R[0],S[0],T[0]) # axin.patch.set_alpha(0.1) ##--------------------------------- ## If S varies else: for Si in np.unique(S)[::-1]: if Casimir: ax.plot(Au, PR[S == Si], "o" + sty[0], label=r"$S=%.1f$" % (Si)) ax.plot(Au, PS[S == Si], "o" + sty[1], c=ax.lines[-1].get_color()) ax.plot(Au, PT[S == Si], "o" + sty[2], c=ax.lines[-1].get_color()) else: ## If DL, label should be bulk width ### One line, average. P = 0.5 * (PR + PS) ax.plot(Au, PR[S == Si], "o" + sty[0], label=r"$L=%.1f$" % (R[0] - Si)) # ax.plot(Au, PR[S==Si], "o"+sty[0], label=r"$S=%.1f$"%(Si)) # ax.plot(Au, PS[S==Si], "o"+sty[1], c=ax.lines[-1].get_color()) ## Inset of potential left, bottom, width, height = [0.19, 0.58, 0.35, 0.30] axin = fig.add_axes([left, bottom, width, height]) if "_DL_" in histdir: plot_U1D_Cartesian(axin, "dlin", 2.0, 0.0, 0.0) elif "_DC_" in histdir: plot_U1D_Cartesian(axin, "dcon", 1.0, -1.0, 0.0) ## axin.patch.set_alpha(0.5) ## Prediction for zero and infinite bulk if "_DL_" in histdir: ax.plot(Au, (Au)**(-0.5), "--", c=ax.lines[0].get_color()) ax.plot(Au, np.ones(Au.size), "y--", label=r"$L\to\infty$") if "_DC_" in histdir: ax.plot(Au, np.ones(Au.size), "y--", label=r"$L\to\infty$") ##------------------------------------------------------------------------- ## Plot appearance if logplot: ax.set_xscale("log") ax.set_yscale("log") ax.set_xlim((ax.get_xlim()[0], A[-1] + 1)) xlabel = r"$1+\frac{k\tau}{\zeta}$" plotfile = plotfile[:-4] + "_loglog." + fs["saveext"] else: ax.set_xlim((0.0, A[-1])) ax.set_ylim(bottom=0.0, top=max(1.2 * ax.get_ylim()[1], 1.0)) xlabel = r"$\alpha$" # ax.set_ylim(1e-1,1e1) ax.set_xlabel(xlabel, fontsize=fs["fsa"]) ax.set_ylabel(r"$P/P^{\rm passive}$", fontsize=fs["fsa"]) ax.grid() ax.legend(loc="best", fontsize=fs["fsl"]).get_frame().set_alpha(0.5) # fig.suptitle(title, fontsize=fs["fst"]) if not nosave: fig.savefig(plotfile) if vb: print me + "Figure saved to", plotfile if vb: print me + "Plotting time %.1f seconds." % (time.time() - t0) return
def plot_mass_ratio(histdir, srchstr, nosave, noread, vb): """ Plot the mass for all files in directory matching string. """ me = me0 + ".plot_mass_ratio: " t0 = time.time() ##------------------------------------------------------------------------- ## Read in existing data or calculate afresh try: assert noread == False massdata = np.load(histdir + "/MASS_" + srchstr + ".npz") print me + "Mass data file found:", histdir + "/MASS_" + srchstr + ".npz" except (IOError, AssertionError): print me + "No mass data found. Calculating from histfiles." massdata = calc_mass_ratio(histdir, srchstr, noread, vb) A = massdata["A"] ML = massdata["ML"] MR = massdata["MR"] MLwn = massdata["MLwn"] MRwn = massdata["MRwn"] x = massdata["x"] Qx_WN = massdata["Qx_WN"] R, S, T = massdata["R"], massdata["S"], massdata["T"] cuspind = massdata["cuspind"] del massdata ##------------------------------------------------------------------------- ## PLOTTING fig, ax = plt.subplots(1, 1) ## Mass normalised by WN result lL = ax.plot(A, ML / MLwn, "o-", label=r"Left") lR = ax.plot(A, MR / MRwn, "o-", label=r"Right") ax.set_xlabel(r"$\alpha$", fontsize=fs["fsa"]) ax.set_ylabel(r"$M/M^{\rm passive}$", fontsize=fs["fsa"]) ax.grid() ##---------------------------------------------------------------------------- ## Casimir insets if "_CL_" in histdir: ## Plot potential as inset left, bottom, width, height = [0.2, 0.6, 0.3, 0.25] axin = fig.add_axes([left, bottom, width, height]) x = np.linspace(-x[-1], x[-1], 2 * x.size) fx = force_clin([x, 0], R, S, T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() axin.plot(x, U, "k-") cuspind += x.size / 2 ## Because x.size has doubles axin.axvspan(x[0], -x[cuspind], color=lR[0].get_color(), alpha=0.2) axin.axvspan(-x[cuspind], x[cuspind], color=lL[0].get_color(), alpha=0.2) axin.axvspan(x[cuspind], x[-1], color=lR[0].get_color(), alpha=0.2) axin.set_xlim(-R - 2, R + 2) axin.set_ylim(top=2 * U[cuspind]) axin.xaxis.set_ticklabels([]) axin.yaxis.set_ticklabels([]) axin.set_xlabel(r"$x$", fontsize=fs["fsa"] - 5) axin.set_ylabel(r"$U$", fontsize=fs["fsa"] - 5) ## Plot q(eta) as inset left, bottom, width, height = [0.55, 0.27, 0.33, 0.28] axin = fig.add_axes([left, bottom, width, height]) ## Grab a file. Hacky. Assumes only one match. histfile = glob.glob(histdir + "/BHIS_CAR_CL_a5.0_*" + srchstr + "*.npy")[0] plot_peta_CL(histfile, fig, axin, True) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) ##---------------------------------------------------------------------------- ## Single wall insets elif "_ML_" in histdir: ## Plot potential as inset fx = force_mlin([x, 0], R, S, T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() left, bottom, width, height = [0.18, 0.68, 0.33, 0.20] axin = fig.add_axes([left, bottom, width, height]) axin.plot(x, U, "k-") axin.axvspan(x[0], x[cuspind], color=lL[0].get_color(), alpha=0.2) axin.axvspan(x[cuspind], x[-1], color=lR[0].get_color(), alpha=0.2) axin.set_xlim(-R - 1.5, R + 1.5) axin.set_ylim(top=2 * U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.set_xlabel(r"$x$", fontsize=fs["fsa"] - 5) axin.set_ylabel(r"$U$", fontsize=fs["fsa"] - 5) ## Plot q(eta) as inset left, bottom, width, height = [0.55, 0.35, 0.33, 0.23] axin = fig.add_axes([left, bottom, width, height]) ## Grab a file. Hacky. Assumes only one match. histfile = glob.glob(histdir + "/BHIS_CAR_ML_a10.0_*" + srchstr + "*.npy")[0] plot_peta_CL(histfile, fig, axin, True) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) ##---------------------------------------------------------------------------- ## Double well insets elif "_NL_" in histdir: ## Plot potential as inset fx = force_nlin([x, 0], R, S)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0) U -= U.min() left, bottom, width, height = [0.61, 0.30, 0.28, 0.20] axin = fig.add_axes([left, bottom, width, height]) axin.plot(x, U, "k-") axin.axvspan(x[0], x[cuspind], color=lL[0].get_color(), alpha=0.2) axin.axvspan(x[cuspind], x[-1], color=lR[0].get_color(), alpha=0.2) axin.set_xlim(x[0], x[-1]) axin.set_ylim(top=2 * U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.set_xlabel(r"$x$", fontsize=fs["fsa"] - 5) axin.set_ylabel(r"$U$", fontsize=fs["fsa"] - 5) ##---------------------------------------------------------------------------- ##---------------------------------------------------------------------------- if not nosave: if "_ML_" in histfile: geo = "ML" elif "_NL_" in histfile: geo = "NL" elif "_CL_" in histfile: geo = "CL" plotfile = histdir+"/MLR_CAR_"+geo+"_R%.1f_S%.1f_T%.1f."%(R,S,T)+fs["saveext"] if T>=0.0\ else histdir+"/MLR_CAR_"+geo+"_R%.1f_S%.1f."%(R,S)+fs["saveext"] fig.savefig(plotfile) if vb: print me + "Figure saved to", plotfile if vb: print me + "Execution time %.1f seconds." % (time.time() - t0) return
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_mass_ratio(histdir, srchstr, nosave, noread, vb): """ Plot the mass for all files in directory matching string. """ me = me0+".plot_mass_ratio: " t0 = time.time() ##------------------------------------------------------------------------- ## Read in existing data or calculate afresh try: assert noread == False massdata = np.load(histdir+"/MASS_"+srchstr+".npz") print me+"Mass data file found:",histdir+"/MASS_"+srchstr+".npz" except (IOError, AssertionError): print me+"No mass data found. Calculating from histfiles." massdata = calc_mass_ratio(histdir, srchstr, noread, vb) A = massdata["A"] ML = massdata["ML"] MR = massdata["MR"] MLwn = massdata["MLwn"] MRwn = massdata["MRwn"] x = massdata["x"] Qx_WN = massdata["Qx_WN"] R, S, T = massdata["R"], massdata["S"], massdata["T"] cuspind = massdata["cuspind"] del massdata ##------------------------------------------------------------------------- ## PLOTTING fig, ax = plt.subplots(1,1) ## Mass normalised by WN result lL = ax.plot(A, ML/MLwn, "o-", label=r"Left") lR = ax.plot(A, MR/MRwn, "o-", label=r"Right") ax.set_xlabel(r"$\alpha$", fontsize=fs["fsa"]) ax.set_ylabel(r"$M/M^{\rm passive}$", fontsize=fs["fsa"]) ax.grid() ##---------------------------------------------------------------------------- ## Casimir insets if "_CL_" in histdir: ## Plot potential as inset left, bottom, width, height = [0.2, 0.6, 0.3, 0.25] axin = fig.add_axes([left, bottom, width, height]) x = np.linspace(-x[-1],x[-1],2*x.size) fx = force_clin([x,0],R,S,T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() axin.plot(x, U, "k-") cuspind += x.size/2 ## Because x.size has doubles axin.axvspan(x[0],-x[cuspind], color=lR[0].get_color(),alpha=0.2) axin.axvspan(-x[cuspind],x[cuspind], color=lL[0].get_color(),alpha=0.2) axin.axvspan(x[cuspind],x[-1], color=lR[0].get_color(),alpha=0.2) axin.set_xlim(-R-2, R+2) axin.set_ylim(top=2*U[cuspind]) axin.xaxis.set_ticklabels([]) axin.yaxis.set_ticklabels([]) axin.set_xlabel(r"$x$", fontsize = fs["fsa"]-5) axin.set_ylabel(r"$U$", fontsize = fs["fsa"]-5) ## Plot q(eta) as inset left, bottom, width, height = [0.55, 0.27, 0.33, 0.28] axin = fig.add_axes([left, bottom, width, height]) ## Grab a file. Hacky. Assumes only one match. histfile = glob.glob(histdir+"/BHIS_CAR_CL_a5.0_*"+srchstr+"*.npy")[0] plot_peta_CL(histfile, fig, axin, True) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) ##---------------------------------------------------------------------------- ## Single wall insets elif "_ML_" in histdir: ## Plot potential as inset fx = force_mlin([x,0],R,S,T)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() left, bottom, width, height = [0.18, 0.68, 0.33, 0.20] axin = fig.add_axes([left, bottom, width, height]) axin.plot(x, U, "k-") axin.axvspan(x[0],x[cuspind], color=lL[0].get_color(),alpha=0.2) axin.axvspan(x[cuspind],x[-1], color=lR[0].get_color(),alpha=0.2) axin.set_xlim(-R-1.5, R+1.5) axin.set_ylim(top=2*U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.set_xlabel(r"$x$", fontsize = fs["fsa"]-5) axin.set_ylabel(r"$U$", fontsize = fs["fsa"]-5) ## Plot q(eta) as inset left, bottom, width, height = [0.55, 0.35, 0.33, 0.23] axin = fig.add_axes([left, bottom, width, height]) ## Grab a file. Hacky. Assumes only one match. histfile = glob.glob(histdir+"/BHIS_CAR_ML_a10.0_*"+srchstr+"*.npy")[0] plot_peta_CL(histfile, fig, axin, True) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) ##---------------------------------------------------------------------------- ## Double well insets elif "_NL_" in histdir: ## Plot potential as inset fx = force_nlin([x,0],R,S)[0] U = -sp.integrate.cumtrapz(fx, x, initial=0.0); U -= U.min() left, bottom, width, height = [0.61, 0.30, 0.28, 0.20] axin = fig.add_axes([left, bottom, width, height]) axin.plot(x, U, "k-") axin.axvspan(x[0],x[cuspind], color=lL[0].get_color(),alpha=0.2) axin.axvspan(x[cuspind],x[-1], color=lR[0].get_color(),alpha=0.2) axin.set_xlim(x[0],x[-1]) axin.set_ylim(top=2*U[cuspind]) axin.xaxis.set_major_locator(NullLocator()) axin.yaxis.set_major_locator(NullLocator()) axin.set_xlabel(r"$x$", fontsize = fs["fsa"]-5) axin.set_ylabel(r"$U$", fontsize = fs["fsa"]-5) ##---------------------------------------------------------------------------- ##---------------------------------------------------------------------------- if not nosave: if "_ML_" in histfile: geo = "ML" elif "_NL_" in histfile: geo = "NL" elif "_CL_" in histfile: geo = "CL" plotfile = histdir+"/MLR_CAR_"+geo+"_R%.1f_S%.1f_T%.1f."%(R,S,T)+fs["saveext"] if T>=0.0\ else histdir+"/MLR_CAR_"+geo+"_R%.1f_S%.1f."%(R,S)+fs["saveext"] fig.savefig(plotfile) 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 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_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