def update_frame(frame, format=1): if format == 1: t, vxm, vym, num = frame phi = np.sqrt(vxm**2 + vym**2) elif format == 2: t, phi, num = frame rho = num.astype(float) rho_s = gaussian_filter(rho, sigma=[5, 1]) xh1, rho_h = half_peak.find_interface(rho, sigma=[5, 1]) xh2, rho_h = half_peak.find_interface(rho, sigma=[10, 1]) xh3, rho_h = half_peak.find_interface(rho, sigma=[15, 1]) xh4, rho_h = half_peak.find_interface(rho, sigma=[20, 1]) xh1 = half_rho.untangle(xh1, Lx) xh2 = half_rho.untangle(xh2, Lx) xh3 = half_rho.untangle(xh3, Lx) xh4 = half_rho.untangle(xh4, Lx) w1 = np.var(xh1) w2 = np.var(xh2) w3 = np.var(xh3) w4 = np.var(xh4) dx = np.round(100 - np.mean(xh1)).astype(int) xh1 += dx xh2 += dx xh3 += dx rho_s = np.roll(rho_s, dx, axis=1) im.set_data(rho_s.T) line1.set_data(yh, xh1) line3.set_data(yh, xh3) title.set_text(title_template % (eta, eps, Lx, Ly, N, t, phi, w1, w3)) writer.grab_frame() print("t=", t) f.write("%d\t%f\t%f\t%f\t%f\t%f\n" % (t, phi, w1, w2, w3, w4))
def handle_raw_snap(): import load_snap from half_rho import untangle import snake import half_peak os.chdir(r"D:\tmp") Lx = 180 Ly = 1000 sigma = [5, 1] r = np.round(np.logspace(2, 18, 17, base=np.sqrt(2))).astype(int) G1 = np.zeros(r.size) G2 = np.zeros(r.size) count = 0 snap = load_snap.RawSnap(r"so_%g_%g_%d_%d_%d_%d_%d.bin" % (0.35, 0, Lx, Ly, Lx * Ly, 2000, 1234)) n = snap.get_num_frames() print("n=", n) t_beg = 250 t_end = 300 for i, frame in enumerate(snap.gene_frames(t_beg, t_end)): x, y, theta = frame rho = load_snap.coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly, ncols=Lx, nrows=Ly).astype(float) xh1, rho_h = half_peak.find_interface(rho, sigma=sigma) rho_s = gaussian_filter(rho, sigma=sigma) xh2, yh2 = snake.find_interface(rho_s, 0.5, 0.1, 0.25, 400, rho_h, dx=5) xh1 = untangle(xh1, Lx) xh2 = untangle(xh2, Lx) G1 += cal_G(xh1, r) G2 += cal_G(xh2, r) count += 1 print("i = ", i) G1 /= count G2 /= count plt.plot(r, G1) plt.plot(r, G2) plt.xscale("log") plt.yscale("log") plt.show() plt.close() for i in range(G1.size): print(r[i], G1[i], G2[i]) print(count)
def show_separated_snaps(Lx, Ly, seed, t_beg, t_end, dt=None, eta=0.35, eps=0, rho0=1, transpos=False, sigma=[5, 1]): from half_peak import find_interface from half_rho import untangle if dt is None: dt = t_beg t = t_beg while t <= t_end: file = r's_%g_%g_%g_%d_%d_%d_%08d.bin' % (eta, eps, rho0, Lx, Ly, seed, t) snap = RawSnap(file) for frame in snap.gene_frames(): try: x, y, theta = frame phi = np.sqrt( np.mean(np.cos(theta))**2 + np.mean(np.sin(theta))**2) rho = coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly).astype(float) yh = np.linspace(0.5, Ly - 0.5, Ly) xh, rho_h = find_interface(rho, sigma=sigma) w = np.var(untangle(xh, Lx)) print("t=%d, phi=%f, w=%f" % (t, phi, w)) plt.subplot(121) if transpos: x, y = y, x xh, yh = yh, xh plt.xlim(0, Ly) plt.ylim(0, Lx) else: plt.xlim(0, Lx) plt.ylim(0, Ly) plt.scatter(x, y, s=1, c=theta, cmap="hsv") plt.plot(xh, yh) plt.title(r"$t=%d, \phi=%g, w^2=%g$" % (t, phi, w)) plt.colorbar() plt.subplot(122) plt.plot(rho.mean(axis=0)) plt.show() plt.close() except: print("t=%d, Error" % t) t += dt
def update_frame(): for i in range(nfile): t, vxm, vym, num = next(frames[i]) xh, rho_h = half_peak.find_interface(num.astype(float), sigma=[20, 1]) xh = half_rho.untangle(xh, Lx[i]) w = np.var(xh) dx = 100 - np.mean(xh) xh += dx phi = np.sqrt(vxm**2 + vym**2) line[i].set_data(yh[i], xh) line[i].set_label(r"$L_y=%d, w^2=%.4f, \phi=%.4f$" % (Lx[i], w, phi)) title.set_text(r"$\eta=%g, \epsilon=%g, L_x=%d, t=%d$" % (eta[0], eps[0], Ly[0], t)) plt.legend() writer.grab_frame() print("t=", t)
def cal_spectrum(Lx, Ly, sigma_y=10, show=False, out=False, eps=0, dt=1): file = r"so_0.35_%g_%d_%d_%d_2000_1234.bin" % (eps, Lx, Ly, Lx * Ly) snap = load_snap.RawSnap(file) if not isinstance(sigma_y, list): nrows = 1 sigma_y = [sigma_y] else: nrows = len(sigma_y) ncols = Ly // 2 + 1 q = np.arange(ncols) / Ly spectrum = np.zeros((nrows, ncols)) count = np.zeros(nrows, int) for frame in snap.gene_frames(beg_idx=300, interval=dt): x, y, theta = frame rho = load_snap.coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly).astype(float) for i, sy in enumerate(sigma_y): try: xh, rho_h = find_interface(rho, sigma=[sy, 1]) xh = untangle(xh, Lx) h = xh - np.mean(xh) hq = np.fft.rfft(h) / Ly A2 = np.abs(hq)**2 spectrum[i] += A2 count[i] += 1 except: pass print("t=%d" % (dt * count[-1])) for i in range(nrows): spectrum[i] /= count[i] if show: plt.loglog(q, spectrum) plt.show() plt.close() if out: outfile = "hq_%g_%d_%d.dat" % (eps, Lx, Ly) with open(outfile, "w") as f: for i in range(q.size): line = "%f" % (q[i]**2) for j in range(nrows): line += "\t%.8f" % (spectrum[j, i]) line += "\n" f.write(line)
def time_ave(Lx, Ly, sigma_y=100, show=False, out=False, eps=0, dt=1): file = r"so_0.35_%g_%d_%d_%d_2000_1234.bin" % (eps, Lx, Ly, Lx * Ly) snap = load_snap.RawSnap(file) if not isinstance(sigma_y, list): nrows = 1 sigma_y = [sigma_y] else: nrows = len(sigma_y) ncols = Lx rho_mean = np.zeros((nrows, ncols)) count = np.zeros(nrows, int) x0 = np.arange(Lx) + 0.5 for frame in snap.gene_frames(beg_idx=300, interval=dt): x, y, theta = frame rho = load_snap.coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly).astype(float) rho_real = load_snap.coarse_grain(x, y, Lx=Lx, Ly=Ly) for i, sy in enumerate(sigma_y): try: xh, rho_h = find_interface(rho, sigma=[sy, 1]) rho_mean[i] += ave_one_frame(rho_real, xh) count[i] += 1 except: pass print("t=%d" % (dt * count[-1])) for i in range(nrows): rho_mean[i] /= count[i] if show: for i in range(nrows): plt.plot(x0, rho_mean[i]) plt.show() plt.close() if out: outfile = "avePeak_%g_%d_%d.dat" % (eps, Lx, Ly) with open(outfile, "w") as f: for i in range(ncols): line = "%f" % (x0[i]) for j in range(nrows): line += "\t%.8f" % (rho_mean[j, i]) line += "\n" f.write(line)
def handle_raw_snap(file): import half_peak import spatial_corr from half_rho import untangle path, file = file.split("/") os.chdir(path) str_list = file.split("_") Lx = int(str_list[3]) Ly = int(str_list[4]) snap = RawSnap(file) outfile = file.replace(".bin", ".dat") f = open(outfile, "w") corr2D = spatial_corr.Corr2D(file, 1) for i, frame in enumerate(snap.gene_frames()): x, y, theta = frame vxm = np.mean(np.cos(theta)) vym = np.mean(np.sin(theta)) phi = np.sqrt(vxm**2 + vym**2) rho = coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly, ncols=Lx, nrows=Ly).astype(float) line = "%d\t%f" % (i, phi) for sigma_y in [1, 5, 10, 15, 20]: try: xh, rho_h = half_peak.find_interface(rho, sigma=[sigma_y, 1]) xh = untangle(xh, Lx) w = np.var(xh) line += "\t%f" % (w) except: print("Error when sigma_y = %d and i = %d" % (sigma_y, i)) line += "\t" line += "\n" f.write(line) if i >= 300: rho, vx, vy = coarse_grain(x, y, theta, Lx, Ly) corr2D.accu(vx, vy, rho) f.close() corr2D.outfile()
width1 = [] width2 = [] debug = True t_beg = 0 t_end = None for i, frame in enumerate(snap.gene_frames(t_beg, t_end, 100)): x, y, theta = frame rho = load_snap.coarse_grain2(x, y, theta, Lx=Lx, Ly=Ly, ncols=Lx, nrows=Ly).astype(float) xh, rho_h = half_rho.find_interface(rho, sigma=[1, 1]) xh2, rho_h2 = half_peak.find_interface(rho, sigma=[1, 1]) rho_s = gaussian_filter(rho, sigma=[1, 1]) xh3, yh3 = find_interface(rho_s, 0.5, 0.1, 0.25, 500, rho_h2, dx=5) yh = np.linspace(0.5, Ly - 0.5, Ly) if debug: # plt.scatter(y, x, s=0.5, c=theta, cmap="hsv") plt.plot(y, x, "o", ms="1") rho_s[rho_s > 5] = 5 # plt.imshow(rho_s.T, interpolation="none", origin="lower") xh = untangle(xh, Lx) xh2 = untangle(xh2, Lx) xh3 = untangle(xh3, Lx) plt.plot(yh, xh, "g") plt.plot(yh, xh2, "r--") plt.plot(yh3, xh3, "k:") plt.show()
os.chdir(path) outfile = file.replace(".bin", "_skew.dat") f = open(outfile, "w") snap = load_snap.CoarseGrainSnap(file) tot_frames = snap.get_num_frames() ts = np.zeros(tot_frames, int) h10 = np.zeros((tot_frames, Ly)) h15 = np.zeros((tot_frames, Ly)) frames = snap.gene_frames() for i, frame in enumerate(frames): t, vxm, vym, num = frame ts[i] = t line = "%d" % t rho = num.astype(float) xh1, rho_h = half_peak.find_interface(rho, sigma=[10, 1]) xh1 = half_rho.untangle(xh1, Lx) h10[i] = xh1 gamma1 = skew(xh1) gamma2 = kurt(xh1) line += "\t%f\t%f" % (gamma1, gamma2) xh1, rho_h = half_peak.find_interface(rho, sigma=[15, 1]) xh1 = half_rho.untangle(xh1, Lx) h15[i] = xh1 gamma1 = skew(xh1) gamma2 = kurt(xh1) line += "\t%f\t%f" % (gamma1, gamma2) try: xh2 = isoline(rho, Lx, Ly, Lx, Ly // 20) gamma1 = skew(xh2)
def show(self, i_beg=0, i_end=None, di=1, lx=1, ly=1, transpos=True, sigma=[5, 1], output=True, show=True): import half_rho import half_peak if output: f = open(self.file.replace(".bin", "_%d.dat" % (sigma[0])), "w") for i, frame in enumerate(self.gene_frames(i_beg, i_end, di)): t, vxm, vym, num = frame rho = num.astype(np.float32) yh = np.linspace(0.5, self.nrows - 0.5, self.nrows) # xh1, rho_h1 = half_rho.find_interface(rho, sigma=sigma) xh2, rho_h2 = half_peak.find_interface(rho, sigma=sigma) # xh1 = half_rho.untangle(xh1, self.ncols) xh2 = half_rho.untangle(xh2, self.ncols) # w1 = np.var(xh1) w2 = np.var(xh2) if show: if ly > 1: rho = np.array([ np.mean(num[i * ly:(i + 1) * ly], axis=0) for i in range(self.nrows // ly) ]) if lx > 1: rho = np.array([ np.mean(rho[:, i * lx:(i + 1) * lx], axis=1) for i in range(self.ncols // lx) ]) rho = gaussian_filter(rho, sigma=[5, 1]) if transpos: rho = rho.T box = [0, self.nrows, 0, self.ncols] plt.figure(figsize=(14, 3)) plt.plot(yh, xh2, "r") plt.xlabel(r"$y$") plt.ylabel(r"$x$") else: box = [0, self.ncols, 0, self.nrows] plt.figure(figsize=(4, 12)) plt.plot(xh2, yh, "r") plt.xlabel(r"$x$") plt.ylabel(r"$y$") rho[rho > 4] = 4 plt.imshow(rho, origin="lower", interpolation="none", extent=box, aspect="auto") plt.title(r"$t=%d, \phi=%g, w^2=%g$" % (t, np.sqrt(vxm**2 + vym**2), w2)) plt.tight_layout() plt.show() plt.close() print(t, np.sqrt(vxm**2 + vym**2), w2) if output: f.write("%d\t%f\t%f\n" % (t, np.sqrt(vxm**2 + vym**2), w2)) if output: f.close()