Beispiel #1
0
def test_Helmholtz(ST2):
    M = 4 * N
    kx = 12

    points, weights = ST2.points_and_weights(M)

    fj = np.random.randn(M)
    f_hat = np.zeros(M)
    if not ST2.__class__.__name__ == "ChebyshevTransform":
        f_hat = ST2.fst(fj, f_hat)
        fj = ST2.ifst(f_hat, fj)

    if ST2.__class__.__name__ == "ShenDirichletBasis":
        A = ADDmat(np.arange(M).astype(np.float))
        B = BDDmat(np.arange(M).astype(np.float), ST2.quad)
        s = slice(0, M - 2)
    elif ST2.__class__.__name__ == "ShenNeumannBasis":
        A = ANNmat(np.arange(M).astype(np.float))
        B = BNNmat(np.arange(M).astype(np.float), ST2.quad)
        s = slice(1, M - 2)

    f_hat = np.zeros(M)
    f_hat = ST2.fastShenScalar(fj, f_hat)
    u_hat = np.zeros(M)
    u_hat[s] = la.spsolve(A.diags() + kx**2 * B.diags(), f_hat[s])
    u1 = np.zeros(M)
    u1 = ST2.ifst(u_hat, u1)
    c = A.matvec(u_hat) + kx**2 * B.matvec(u_hat)
    c2 = np.dot(A.diags().toarray(),
                u_hat[s]) + kx**2 * np.dot(B.diags().toarray(), u_hat[s])

    assert np.allclose(c, f_hat)
    assert np.allclose(c[s], c2)

    H = Helmholtz(M, kx, ST2.quad,
                  ST2.__class__.__name__ == "ShenNeumannBasis")
    u0_hat = np.zeros(M)
    u0_hat = H(u0_hat, f_hat)
    u0 = np.zeros(M)
    u0 = ST2.ifst(u0_hat, u0)

    assert np.linalg.norm(u0 - u1) < 1e-12

    # Multidimensional
    f_hat = (f_hat.repeat(16).reshape(
        (M, 4, 4)) + 1j * f_hat.repeat(16).reshape((M, 4, 4)))
    kx = np.zeros((4, 4)) + 12
    H = Helmholtz(M, kx, ST2.quad,
                  ST2.__class__.__name__ == "ShenNeumannBasis")
    u0_hat = np.zeros((M, 4, 4), dtype=np.complex)
    u0_hat = H(u0_hat, f_hat)
    u0 = np.zeros((M, 4, 4), dtype=np.complex)
    u0 = ST2.ifst(u0_hat, u0)
    #from IPython import embed; embed()

    assert np.linalg.norm(u0[:, 2, 2].real - u1) / (M * 16) < 1e-12
    assert np.linalg.norm(u0[:, 2, 2].imag - u1) / (M * 16) < 1e-12
Beispiel #2
0
def test_Helmholtz2(quad):
    M = 2*N
    SD = ShenDirichletBasis(M, quad=quad)
    kx = 12
    uj = np.random.randn(M)
    u_hat = np.zeros(M)
    u_hat = SD.forward(uj, u_hat)
    uj = SD.backward(u_hat, uj)

    #from IPython import embed; embed()
    A = inner_product((SD, 0), (SD, 2))
    B = inner_product((SD, 0), (SD, 0))

    u1 = np.zeros(M)
    u1 = SD.forward(uj, u1)
    c0 = np.zeros_like(u1)
    c1 = np.zeros_like(u1)
    c = A.matvec(u1, c0)+kx**2*B.matvec(u1, c1)

    b = np.zeros(M)
    H = Helmholtz(M, kx, SD)
    b = H.matvec(u1, b)
    #LUsolve.Mult_Helmholtz_1D(M, SD.quad=="GL", 1, kx**2, u1, b)
    assert np.allclose(c, b)

    b = np.zeros((M, 4, 4), dtype=np.complex)
    u1 = u1.repeat(16).reshape((M, 4, 4)) +1j*u1.repeat(16).reshape((M, 4, 4))
    kx = np.zeros((4, 4))+kx
    H = Helmholtz(M, kx, SD)
    b = H.matvec(u1, b)
    #LUsolve.Mult_Helmholtz_3D_complex(M, SD.quad=="GL", 1.0, kx**2, u1, b)
    assert np.linalg.norm(b[:, 2, 2].real - c)/(M*16) < 1e-12
    assert np.linalg.norm(b[:, 2, 2].imag - c)/(M*16) < 1e-12
Beispiel #3
0
def solve(fk):

    k = ST.wavenumbers(N)

    if solver == "sparse":
        A = ADDmat(np.arange(N).astype(np.float)).diags()
        B = BDDmat(np.arange(N).astype(np.float), quad).diags()
        fk[0] -= kx**2 * pi / 2. * (a + b)
        fk[1] -= kx**2 * pi / 4. * (a - b)
        uk_hat = la.spsolve(A + kx**2 * B, fk[:-2])
        assert np.allclose(np.dot(A.toarray() + kx**2 * B.toarray(), uk_hat),
                           fk[:-2])

    elif solver == "lu":

        uk_hat = np.zeros(N - 2)
        sol = Helmholtz(N, kx, quad=quad)
        fk[0] -= kx**2 * pi / 2. * (a + b)
        fk[1] -= kx**2 * pi / 4. * (a - b)
        uk_hat = sol(uk_hat, fk[:-2])

    return uk_hat
Beispiel #4
0
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)
Beispiel #5
0
def test_Helmholtz(ST, quad):
    M = 4*N
    ST = ST(M, quad=quad)
    kx = 12

    fj = np.random.randn(M)
    f_hat = np.zeros(M)
    f_hat = ST.forward(fj, f_hat)
    fj = ST.backward(f_hat, fj)
    s = ST.slice()

    A = inner_product((ST, 0), (ST, 2))
    B = inner_product((ST, 0), (ST, 0))

    f_hat = np.zeros(M)
    f_hat = ST.scalar_product(fj, f_hat)
    u_hat = np.zeros(M)
    H = A + kx**2*B
    u_hat[s] = solve(H.diags().toarray()[s, s], f_hat[s])
    if ST.__class__.__name__ == "ShenNeumannBasis":
        u_hat[0] = 0
    u1 = np.zeros(M)
    u1 = ST.backward(u_hat, u1)
    c0 = np.zeros_like(u_hat)
    c1 = np.zeros_like(u_hat)
    c = A.matvec(u_hat, c0)+kx**2*B.matvec(u_hat, c1)
    c2 = np.dot(A.diags().toarray()[s, s], u_hat[s]) + kx**2*np.dot(B.diags().toarray()[s, s], u_hat[s])
    if ST.__class__.__name__ == "ShenNeumannBasis":
        c2[0] = 0

    assert np.allclose(c[s], f_hat[s])
    assert np.allclose(c[s], c2)

    H = Helmholtz(M, kx, ST)
    u0_hat = np.zeros(M)
    u0_hat = H(u0_hat, f_hat)
    u0 = np.zeros(M)
    u0 = ST.backward(u0_hat, u0)

    from shenfun.chebyshev.la import Helmholtz as HH
    A.scale = np.ones(1)
    B.scale = np.array([kx**2])
    Hs = HH(**{'ADDmat':A, 'BDDmat':B})
    u2_hat = np.zeros_like(u1)
    u2_hat = Hs(u2_hat, f_hat)
    u2 = np.zeros_like(u2_hat)
    u2 = ST.backward(u2_hat, u2)
    assert np.linalg.norm(u2 - u1) < 1e-12

    # Multidimensional
    f_hat = (f_hat.repeat(16).reshape((M, 4, 4))+1j*f_hat.repeat(16).reshape((M, 4, 4)))
    ST.plan((M, 4, 4), 0, np.complex, {})
    kx = np.zeros((4, 4))+12

    H = Helmholtz(M, kx, ST)
    u0_hat = np.zeros((M, 4, 4), dtype=np.complex)
    u0_hat = H(u0_hat, f_hat)
    u0 = np.zeros((M, 4, 4), dtype=np.complex)
    u0 = ST.backward(u0_hat, u0)

    A.scale = np.ones((1, 1, 1))
    B.scale = 12**2*np.ones((1, 4, 4))
    A.axis = 0
    B.axis = 0
    #from IPython import embed; embed()

    Hs = HH(**{'ADDmat':A, 'BDDmat':B})
    u2_hat = np.zeros_like(u0_hat)
    u2_hat = Hs(u2_hat, f_hat)
    u2 = np.zeros_like(u2_hat)
    u2 = ST.backward(u2_hat, u2)

    assert np.linalg.norm(u2[:, 2, 2].real - u1)/(M*16) < 1e-12
    assert np.linalg.norm(u2[:, 2, 2].imag - u1)/(M*16) < 1e-12
Beispiel #6
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 += " \\\ "
    print err

print "\hline"
for z in Z:
    err = str(z)
    for n in N:
        errh = 0
        vh = zeros(n)
        sh = zeros(n)
        alfa = sqrt(z**2 + 2.0 / nu / dt)
        HS = Helmholtz(n, alfa, "GC")
        for m in range(M):
            u = random.randn(n)
            u[-2:] = 0
            vh = HS.matvec(u, vh)
            sh = HS(sh, vh)
            errh += max(abs(sh - u)) / max(abs(u))
        err += " & {:2.2e} ".format(errh / M)
    err += " \\\ "
    print err