def make_figure(): from scipy.optimize import curve_fit from pyphare.pharesee.hierarchy import finest_field rwT = Run("./withTagging") rNoRef = Run("./noRefinement") plot_time = 11 v = 2 BwT = rwT.GetB(plot_time) BNoRef = rNoRef.GetB(plot_time) JwT = rwT.GetJ(plot_time) JNoRef = rNoRef.GetJ(plot_time) bywT, xbywT = finest_field(BwT, "By") byNoRef, xbyNoRef = finest_field(BNoRef, "By") jzwT, xjzwT = finest_field(JwT, "Jz") jzNoRef, xjzNoRef = finest_field(JNoRef, "Jz") fig, axarr = plt.subplots(nrows=3, figsize=(8, 8)) def S(x, x0, l): return 0.5 * (1 + np.tanh((x - x0) / l)) def by(x): L = 200 v1 = -1 v2 = 1 return v1 + (v2 - v1) * (S(x, L * 0.25, 1) - S(x, L * 0.75, 1)) wT0 = 150. ax0, ax1, ax2 = axarr ax0.plot(xbywT, bywT) ax0.plot(xbyNoRef, byNoRef, color="k", alpha=0.6) ax0.plot(xbyNoRef, by(xbyNoRef), ls='--') ax1.plot(xbywT, bywT) ax1.plot(xbyNoRef, byNoRef, color='k') ax1.set_xlim((wT0, 195)) ax1.set_ylim((-1.5, 2)) ax2.plot(xjzwT, jzwT) ax2.plot(xjzNoRef, jzNoRef, color='k') ax2.set_xlim((wT0, 195)) ax2.set_ylim((-1.5, 0.5)) # draw level patches for ilvl, level in BwT.levels().items(): for patch in level.patches: dx = patch.layout.dl[0] x0 = patch.origin[0] x1 = (patch.box.upper[0] + 1) * patch.layout.dl[0] for ax in (ax1, ax2, ax0): ax.axvspan(x0, x1, color='b', ec='k', alpha=0.2, ymin=ilvl / 4, ymax=(ilvl + 1) / 4) from pyphare.pharesee.plotting import zoom_effect zoom_effect(ax0, ax1, wT0, 195) for ax in (ax0, ax1, ax2): ax.axvline(wT0 + plot_time * v, color="r") # select data around the rightward TD idx = np.where((xbywT > 150) & (xbywT < 190)) xx = xbywT[idx] bby = bywT[idx] # now we will fit by_fit to the data # and we expect to find x0=172 and L=1 # or close enough def by_fit(x, x0, L): v1 = 1 v2 = -1 return v1 + (v2 - v1) * S(x, x0, L) popt, pcov = curve_fit(by_fit, xx, bby, p0=(150, 1)) x0, L = popt if np.abs(L - 1) > 0.5: raise RuntimeError(f"L (={L}) too far from 1.O") if np.abs(x0 - (150 + plot_time * v)) > 0.5: raise RuntimeError(f"x0 (={x0}) too far from 172")
def finest_field_plot(run_path, qty, **kwargs): """ plot the given quantity (qty) at 'run_path' with only the finest data * run_path : the path of the run * qty : ['Bx', 'By', 'Bz', 'Ex', 'Ey', 'Ez', 'Fx', 'Fy', 'Fz', 'Vx', 'Vy', 'Vz', 'rho'] kwargs: * ax : the handle for the fig axes * time : time (that should be in the time_stamps of the run) * interp : the type of interpolation for the interpolator * draw_style : steps-mid ou default... only for 1d * title : (str) title of the plot * xlabel, ylabel * xlim, ylim * filename : (str) if exists, save plot to figure under that name return value : fig,ax """ import os from pyphare.pharesee.hierarchy import get_times_from_h5 from pyphare.pharesee.run import Run from mpl_toolkits.axes_grid1 import make_axes_locatable import pyphare.core.gridlayout as gridlayout r = Run(run_path) time = kwargs.get("time", None) dim = r.GetDl('finest', time).shape[0] interp = kwargs.get("interp", "nearest") domain = r.GetDomainSize() if qty in ['Bx', 'By', 'Bz']: file = os.path.join(run_path, "EM_B.h5") if time is None: times = get_times_from_h5(file) time = times[0] interpolator, finest_coords = r.GetB(time, merged=True,\ interp=interp)[qty] elif qty in ['Ex', 'Ey', 'Ez']: file = os.path.join(run_path, "EM_E.h5") if time is None: times = get_times_from_h5(file) time = times[0] interpolator, finest_coords = r.GetE(time, merged=True,\ interp=interp)[qty] elif qty in ['Vx', 'Vy', 'Vz']: file = os.path.join(run_path, "ions_bulkVelocity.h5") if time is None: times = get_times_from_h5(file) time = times[0] interpolator, finest_coords = r.GetVi(time, merged=True,\ interp=interp)[qty] elif qty == 'rho': file = os.path.join(run_path, "ions_density.h5") if time is None: times = get_times_from_h5(file) time = times[0] interpolator, finest_coords = r.GetNi(time, merged=True,\ interp=interp)[qty] elif qty in ("Jx", "Jy", "Jz"): file = os.path.join(run_path, "EM_B.h5") if time is None: times = get_times_from_h5(file) time = times[0] interpolator, finest_coords = r.GetJ(time, merged=True,\ interp=interp)[qty] else: # ___ TODO : should also include the files for a given population raise ValueError( "qty should be in ['Bx', 'By', 'Bz', 'Ex', 'Ey', 'Ez', 'Fx', 'Fy', 'Fz', 'Vx', 'Vy', 'Vz', 'rho']" ) if "ax" not in kwargs: fig, ax = plt.subplots() else: ax = kwargs["ax"] fig = ax.figure if dim == 1: drawstyle = kwargs.get("drawstyle", "steps-mid") ax.plot(finest_coords[0], interpolator(finest_coords[0]),\ drawstyle=drawstyle) elif dim == 2: x = finest_coords[0] y = finest_coords[1] dx = x[1] - x[0] dy = y[1] - y[0] # pcolormesh considers DATA_ij to be the center of the pixel # and X,Y are the corners so XY need to be made 1 value larger # and shifted around DATA_ij x -= dx / 2 x = np.append(x, x[-1] + dx) y -= dy / 2 y = np.append(y, y[-1] + dy) X, Y = np.meshgrid(x, y) DATA = interpolator(X, Y) vmin = kwargs.get("vmin", np.nanmin(DATA)) vmax = kwargs.get("vmax", np.nanmax(DATA)) cmap = kwargs.get("cmap", 'Spectral_r') im = ax.pcolormesh(x, y, DATA, cmap=cmap, vmin=vmin, vmax=vmax) ax.set_aspect("equal") divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.08) cb = plt.colorbar(im, ax=ax, cax=cax) else: raise ValueError("finest_field_plot not yet ready for 3d") ax.set_title(kwargs.get("title", "")) ax.set_xlabel(kwargs.get("xlabel", "$x c / \omega_p$")) ax.set_ylabel(kwargs.get("ylabel", "$y c / \omega_p$")) if "xlim" in kwargs: ax.set_xlim(kwargs["xlim"]) if "ylim" in kwargs: ax.set_ylim(kwargs["ylim"]) if "filename" in kwargs: fig.savefig(kwargs["filename"]) return fig, ax