def main(): for n in N: err = str(n) err7 = str(n) Z[0] = n HS = Helmholtz(n, sqrt(K2 + 2.0 / nu / dt), "GC", False) BS = Biharmonic(n, -nu * dt / 2., 1. + nu * dt * K2, -(K2 + nu * dt / 2. * K4), quad="GC", solver="cython") BS2 = Biharmonic(n, -nu * dt / 2., 1. + nu * dt * K2, -(K2 + nu * dt / 2. * K4), quad="GC", solver="scipy") fb = random.random((Z[0], Z[1], Z[2] // 2 + 1)) + random.random( (Z[0], Z[1], Z[2] // 2 + 1)) * 1j fb[-4:] = 0 ub = zeros((Z[0], Z[1], Z[2] // 2 + 1), dtype=complex) #sleep(0.5) t0 = time() for m in range(M): ub = BS(ub, fb) t1 = (time() - t0) / M / Z[1:].prod() t0 = time() for m in range(M): ub = BS2(ub, fb) t7 = (time() - t0) / M / Z[1:].prod() #cProfile.runctx("for m in range(M): ub = BS(ub, fb)", globals(), locals(), "res1.stats") #ps = pstats.Stats("res1.stats") #for key, val in iteritems(ps.stats): #if "Solve_Biharmonic" in key[2]: #results = val[3]/M/Z[1:].prod() #break #t1 = results err += " & {:2.2e} ({:2.2f}) ".format( t1, 0 if n == N[0] else t1 / t11 / 2.) err += " & {:2.2e} ({:2.2f}) ".format( t7, 0 if n == N[0] else t7 / t71 / 2.) t11 = t1 t71 = t7 fh = random.random((Z[0], Z[1], Z[2] // 2 + 1)) + random.random( (Z[0], Z[1], Z[2] // 2 + 1)) * 1j fh[-2:] = 0 uh = zeros((Z[0], Z[1], Z[2] // 2 + 1), dtype=complex) #sleep(0.5) t0 = time() for m in range(M): uh = HS(uh, fh) t2 = (time() - t0) / M / Z[1:].prod() #cProfile.runctx("for m in range(M): uh = HS(uh, fh)", globals(), locals(), "res.stats") #ps = pstats.Stats("res.stats") #for key, val in iteritems(ps.stats): #if "Solve_Helmholtz" in key[2]: #results = val[3]/M/Z[1:].prod() #break #t2 = results err += "& {:2.2e} ({:2.2f}) \\\ ".format( t2, 0 if n == N[0] else t2 / t22 / 2.) t22 = t2 print(err)
def test_ABBmat(SB): M = 6 * N u = sin(6 * pi * x)**2 f = u.diff(x, 2) points, weights = SB.points_and_weights(M) uj = np.array([u.subs(x, h) for h in points], dtype=np.float) fj = np.array([f.subs(x, h) for h in points], dtype=np.float) A = ABBmat(np.arange(M).astype(np.float)) f_hat = np.zeros(M) f_hat = SB.fastShenScalar(fj, f_hat) u_hat = np.zeros(M) u_hat[:-4] = la.spsolve(A.diags(), f_hat[:-4]) u0 = np.zeros(M) u0 = SB.ifst(u_hat, u0) assert np.allclose(u0, uj) u1 = np.zeros(M) u1 = SB.fst(uj, u1) c = A.matvec(u1) assert np.allclose(c, f_hat, 1e-6, 1e-6) # Multidimensional f_hat = (f_hat.repeat(16).reshape( (M, 4, 4)) + 1j * f_hat.repeat(16).reshape((M, 4, 4))) u1 = (u1.repeat(16).reshape((M, 4, 4)) + 1j * u1.repeat(16).reshape( (M, 4, 4))) c = A.matvec(u1) assert np.allclose(c, f_hat, 1e-6, 1e-6) B = BBBmat(np.arange(M).astype(np.float), SB.quad) u0 = np.random.randn(M) u0_hat = np.zeros(M) u0_hat = SB.fst(u0, u0_hat) u0 = SB.ifst(u0_hat, u0) b = np.zeros(M) k = 2. b = A.matvec(u0_hat) - k**2 * B.matvec(u0_hat) AA = A.diags().toarray() - k**2 * B.diags().toarray() z0_hat = np.zeros(M) z0_hat[:-4] = solve(AA, b[:-4]) z0 = np.zeros(M) z0 = SB.ifst(z0_hat, z0) assert np.allclose(z0, u0) k = np.ones(M) * 2 k = k.repeat(16).reshape((M, 4, 4)) k2 = k**2 u0_hat = u0_hat.repeat(16).reshape( (M, 4, 4)) + 1j * u0_hat.repeat(16).reshape((M, 4, 4)) u0 = u0.repeat(16).reshape((M, 4, 4)) b = A.matvec(u0_hat) - k**2 * B.matvec(u0_hat) alfa = np.ones((M, 4, 4)) BH = Biharmonic(M, 0, alfa[0], -k2[0], SB.quad, "cython") z0_hat = np.zeros((M, 4, 4), dtype=np.complex) z0_hat = BH(z0_hat, b) z0 = np.zeros((M, 4, 4)) z0 = SB.ifst(z0_hat.real, z0) #from IPython import embed; embed() assert np.allclose(z0, u0)
N = array([64, 128, 256]) Z = array([0, 200, 1800, 5400]) M = 100 print("\hline") print("z & " + " & ".join([str(n) for n in N]) + " \\\ ") print("\hline") for z in Z: err = str(z) for n in N: errb = 0 errs = 0 vb = zeros(n) sb = zeros(n) ss = zeros(n) BH = Biharmonic(n, -nu * dt / 2., 1. + nu * dt * z**2, -(z**2 + nu * dt / 2. * z**4), "GC", "cython") SH = Biharmonic(n, -nu * dt / 2., 1. + nu * dt * z**2, -(z**2 + nu * dt / 2. * z**4), "GC", "scipy") for m in range(M): u = random.random(n) u[-4:] = 0 vb = BH.matvec(u, vb) sb = BH(sb, vb) errb += max(abs(sb - u)) / max(abs(u)) ### ss = SH(ss, vb) errs += max(abs(ss - u)) / max(abs(u)) ### #err += " & {:2.2e} ".format(errb/M) err += " & {:2.2e} & {:2.2e} ".format(errb / M, errs / M) err += " \\\ "
SD = ShenBiharmonicBasis("GC", True) points, weights = SD.points_and_weights(N) uj = np.array([u.subs(x, j) for j in points], dtype=float) fj = np.array([f.subs(x, j) for j in points], dtype=float) # Get f on quad points #uj_hat = np.zeros(N) #uj_hat = SD.fst(uj, uj_hat) #uj = SD.ifst(uj_hat, uj) #fj_hat = np.zeros(N) #fj_hat = SD.fst(fj, fj_hat) #fj = SD.ifst(fj_hat, fj) solver = Biharmonic(N, a, b, c, quad=SD.quad, solver="cython") solver2 = Biharmonic(N, a, b, c, quad=SD.quad) f_hat = np.zeros(N) f_hat = SD.fastShenScalar(fj, f_hat) u_hat = np.zeros(N) u_hat2 = np.zeros(N) from time import time t0 = time() u_hat = solver(u_hat, f_hat) t1 = time() u_hat2 = solver2(u_hat2, f_hat) t2 = time() print "cython / scipy ", t1-t0, t2-t1