def test_parseval(self, rtol=1.0e-15): """ Make sure 2D fft satisfies Parseval's relation within machine double precision """ # set random stream function p1 = np.random.randn(self.m.nz, self.m.ny, self.m.nx) p1 -= p1.mean(axis=(-2, -1))[:, np.newaxis, np.newaxis] ph1 = self.m.fft(p1) # var(P) from Fourier coefficients P_var_spec = spec_var(self.m, ph1) # print "Variance from spectrum: %5.16f" % P_var_spec # var(P) in physical space P_var_phys = p1.var(axis=(-2, -1)) # print "Variance in physical space: %5.16f" %P_var_phys # relative error error = np.abs((P_var_phys - P_var_spec) / P_var_phys).sum() print "error = %5.16f" % error self.assertTrue(error < rtol, "fft does not satisfy Parseval's relation")
def test_parseval(self, rtol=1.e-15): """ Make sure 2D fft satisfies Parseval's relation within machine double precision """ # set random stream function p1 = np.random.randn(self.m.nz, self.m.ny, self.m.nx) p1 -= p1.mean(axis=(-2, -1))[:, np.newaxis, np.newaxis] ph1 = self.m.fft(p1) # var(P) from Fourier coefficients P_var_spec = spec_var(self.m, ph1) #print "Variance from spectrum: %5.16f" % P_var_spec # var(P) in physical space P_var_phys = p1.var(axis=(-2, -1)) #print "Variance in physical space: %5.16f" %P_var_phys # relative error error = np.abs((P_var_phys - P_var_spec) / P_var_phys).sum() print "error = %5.16f" % error self.assertTrue(error < rtol, "fft does not satisfy Parseval's relation")
ntd=1) # McWilliams 84 IC condition fk = m.wv != 0 ckappa = np.zeros_like(m.wv2) ckappa[fk] = np.sqrt(m.wv2[fk] * (1. + (m.wv2[fk] / 36.)**2))**-1 nhx, nhy = m.wv2.shape Pi_hat = np.random.randn(nhx, nhy) * ckappa + 1j * np.random.randn( nhx, nhy) * ckappa Pi = m.ifft(Pi_hat[np.newaxis]) Pi = Pi - Pi.mean() Pi_hat = m.fft(Pi) KEaux = spec_var(m, m.filtr * m.wv * Pi_hat) pih = (Pi_hat * np.sqrt(0.5 / KEaux)) qih = -m.wv2 * pih qi = m.ifft(qih) m.set_q(qi) t = time.time() # run the model m.run() elapsed = (time.time() - t) / nsteps * 1000 print('Time per time-step: %.3f ms' % elapsed) # import matplotlib.pyplot as plt # plt.figure(figsize=(10, 4))
qh = snap['qh'][:] ph = qh2ph(m.a,qh) pph, pth = psi_tau_h(ph) # calculates the thickness flux prod = calc_tfh(pph,pth,m.U[0],m.k) ki, prod = diagnostic_tools.calc_ispec(m,prod) try: prod_k = np.vstack([prod_k,prod]) except: prod_k = prod Ebt = np.hstack([Ebt, diagnostic_tools.spec_var(m,m.wv*pph)/2.]) Ebc = np.hstack([Ebc, diagnostic_tools.spec_var(m,m.wv*pth)/2.]) Epe = np.hstack([Epe, diagnostic_tools.spec_var(m,(1./m.rd)*pth)/2.]) nlh = pth.conj()*jacob_h(m,pph,-m.wv2*pth) E_bt_bc = np.hstack([E_bt_bc, nlh.real.sum()]) E_ek_bt = np.hstack([E_ek_bt, diagnostic_tools.spec_var(m,m.wv*pth)]) ki, ti = np.meshgrid(ki,kefile['t']) prod_k = np.ma.masked_array(prod_k, prod_k<=.0) # the fractional energy production at deformation radius scales plt.rc('xtick', labelsize=20) plt.rc('ytick', labelsize=20)
beta = 0., H = 1., rek = 0., rd = None, dt = 0.005, taveint=year, ntd=2) # McWilliams 84 IC condition fk = m.wv != 0 ckappa = np.zeros_like(m.wv2) ckappa[fk] = np.sqrt( m.wv2[fk]*(1. + (m.wv2[fk]/36.)**2) )**-1 nhx,nhy = m.wv2.shape Pi_hat = np.random.randn(nhx,nhy)*ckappa +1j*np.random.randn(nhx,nhy)*ckappa Pi = m.ifft( Pi_hat[np.newaxis] ) Pi = Pi - Pi.mean() Pi_hat = m.fft( Pi ) KEaux = spec_var( m, m.filtr*m.wv*Pi_hat ) pih = ( Pi_hat/np.sqrt(KEaux) ) qih = -m.wv2*pih qi = m.ifft(qih) m.set_q(qi) # run the model and plot some figs plt.rcParams['image.cmap'] = 'RdBu_r' plt.ion() for snapshot in m.run_with_snapshots(tsnapstart=0, tsnapint=15*m.dt): plt.clf() p1 = plt.imshow(m.q[0] + m.beta * m.y)