예제 #1
0
def test_CXXmat(SXSX):
    S1, S2 = SXSX

    if S1.__class__.__name__ == "ShenDirichletBasis" and S2.__class__.__name__ == "ShenDirichletBasis":
        Cm = CDDmat(np.arange(N).astype(np.float))
    elif S1.__class__.__name__ == "ShenDirichletBasis":
        Cm = CDNmat(np.arange(N).astype(np.float))
    elif S1.__class__.__name__ == "ShenNeumannBasis":
        Cm = CNDmat(np.arange(N).astype(np.float))

    fj = np.random.randn(N)
    # project to S2
    f_hat = np.zeros(N)
    f_hat = S2.fst(fj, f_hat)
    fj = S2.ifst(f_hat, fj)

    # Check S1.fss(f) equals Cm*S2.fst(f)
    f_hat = S2.fst(fj, f_hat)
    cs = Cm.matvec(f_hat)
    df = np.zeros(N)
    df = S2.fastChebDerivative(fj, df)
    cs2 = np.zeros(N)
    cs2 = S1.fastShenScalar(df, cs2)

    # from IPython import embed; embed()
    assert np.allclose(cs, cs2)

    # Multidimensional version
    f_hat = f_hat.repeat(4 * 4).reshape((N, 4, 4)) + 1j * f_hat.repeat(4 * 4).reshape((N, 4, 4))
    df = df.repeat(4 * 4).reshape((N, 4, 4)) + 1j * df.repeat(4 * 4).reshape((N, 4, 4))
    cs = Cm.matvec(f_hat)
    cs2 = np.zeros((N, 4, 4), dtype=np.complex)
    cs2 = S1.fastShenScalar(df, cs2)

    assert np.allclose(cs, cs2)
예제 #2
0
def test_CDDmat(SD):
    M = 256
    u = (1 - x ** 2) * sin(np.pi * 6 * x)
    dudx = u.diff(x, 1)
    points, weights = SD.points_and_weights(M)
    dudx_j = np.array([dudx.subs(x, h) for h in points], dtype=np.float)
    uj = np.array([u.subs(x, h) for h in points], dtype=np.float)

    dudx_j = np.zeros(M)
    u_hat = np.zeros(M)
    u_hat = SD.fst(uj, u_hat)
    uj = SD.ifst(u_hat, uj)
    u_hat = SD.fst(uj, u_hat)

    uc_hat = np.zeros(M)
    uc_hat = SD.fct(uj, uc_hat)
    du_hat = np.zeros(M)
    dudx_j = SD.fastChebDerivative(uj, dudx_j)

    Cm = CDDmat(np.arange(M).astype(np.float))
    TDMASolver = TDMA(SD.quad, False)

    cs = Cm.matvec(u_hat)

    # Should equal (but not exact so use extra resolution)
    cs2 = np.zeros(M)
    cs2 = SD.fastShenScalar(dudx_j, cs2)

    assert np.allclose(cs, cs2)

    cs = TDMASolver(cs)
    du = np.zeros(M)
    du = SD.ifst(cs, du)

    assert np.linalg.norm(du - dudx_j) / M < 1e-10

    # Multidimensional version
    u3_hat = u_hat.repeat(4 * 4).reshape((M, 4, 4)) + 1j * u_hat.repeat(4 * 4).reshape((M, 4, 4))
    cs = Cm.matvec(u3_hat)
    cs2 = np.zeros((M, 4, 4), dtype=np.complex)
    du3 = dudx_j.repeat(4 * 4).reshape((M, 4, 4)) + 1j * dudx_j.repeat(4 * 4).reshape((M, 4, 4))
    cs2 = SD.fastShenScalar(du3, cs2)

    assert np.allclose(cs, cs2, 1e-10)

    cs = TDMASolver(cs)
    d3 = np.zeros((M, 4, 4), dtype=np.complex)
    d3 = SD.ifst(cs, d3)

    # from IPython import embed; embed()
    assert np.linalg.norm(du3 - d3) / (M * 16) < 1e-10