try: Ahat = np.vstack([Ahat, snap['A'][:]]) F = np.vstack([F, snap['F'][:]]) except: Ahat = snap['A'][:] F = snap['F'][:] t = np.array(t) it0 = np.where(t / 86400 > 260)[0][0] grad = (np.gradient(F[it0:], axis=1)) fmask = ((grad - np.mean(grad)) / np.std(grad) > 5) * 1.0 ict, dmask = dg.count_blocks(fmask, 10, 10) ts = t[it0:][np.where(dmask > 0.5)[0]] / 86400 xs = x[np.where(dmask > 0.5)[1]] / 1e3 nsig = np.sum(fmask) result = { "forcing phase": xc * n / Lx, "onset": (xs, ts), "delay": (xs, ts - initc.tc), "nblocks": ict, "integrated blocking": nsig } ensemble.append(result)
def blocking_iteration(cxpeak=1.0, fpeak=1.8, beta=60, Y=10, noisy_initc=None): if not noisy_initc: noisy_initc = noisyconditions(cfunc=noisybackground, cxpeak=cxpeak, peak=fpeak, background=True, forcing=True, beta=beta, Y=Y) else: noisy_initc.beta = beta noisy_initc.Y = Y noisy_initc.cxpeak = cxpeak noisy_initc.peak = fpeak cleanup = True if cleanup: os.system("rm -rf output/") cond = noisy_initc wavenum = 2 model = AtmosphericBlocking.Model(nx=1024, Lx=28000e3, dt=.005 * 86400, alpha=cond.alpha, tmax=3.5 * 86400, D=3.26e5, tau=cond.tau, sfunc=cond.forcing, cfunc=cond.getcx, forcingpeak=cond.peak, injection=cond.inject, save_to_disk=True, overwrite=True, tsave_snapshots=50, path='output/') model.tmax = 450 * 86400 model.run() setup = h5py.File("output/setup.h5") x = setup['grid/x'][:] fnis = np.array(sorted(glob.glob("output/snapshots/*.h5"))) Ahat, F, S, C = 0, 0, 0, 0 t = [] for fni in fnis[0::2]: snap = h5py.File(fni) t.append(snap['t'][()]) try: Ahat = np.vstack([Ahat, snap['A'][:]]) F = np.vstack([F, snap['F'][:]]) S = np.vstack([S, snap['S'][:]]) C = np.vstack([C, snap['C'][:]]) except: Ahat = snap['A'][:] F = snap['F'][:] S = snap['S'][:] C = snap['C'][:] t = np.array(t) it0 = np.where(t / 86400 > 260)[0][0] ua = 40 - cond.alpha * (Ahat + model.A0[np.newaxis, :]) gamma = 4 * cond.alpha * F / C**2 masked = gamma.copy() masked[np.where(gamma < 0.99999)] = 0 masked[np.where(gamma >= 0.99999)] = 1 threshu = 5 ugrad = -(np.gradient(ua[it0:], axis=1)) umask = ((ugrad - np.mean(ugrad)) / np.std(ugrad) > threshu) * 1.0 uct, ucmask = dg.count_blocks(umask, 80 - 2 * wavenum, 10) uts = t[it0:][np.where(ucmask > 0.5)[0]] / 86400.0 uxs = x[np.where(ucmask > 0.5)[1]] / 1.0e3 shock_size = np.sum(umask) smask = (S[it0:] > 0.6e-4) * 1.0 sct, scmask = dg.count_blocks(smask, 5, 5) sts = t[it0:][np.where(scmask > 0.5)[0]] / 86400 sxs = x[np.where(scmask > 0.5)[1]] / 1e3 return cond, uct, shock_size, uxs, uts, sct, sts, sxs