示例#1
0
def test_CDDmat(quad):
    M = 128
    SD = cbases.ShenDirichlet(M, quad=quad)
    u = (1 - x**2) * sin(np.pi * 6 * x)
    dudx = u.diff(x, 1)
    points, weights = SD.points_and_weights(M)

    ul = lambdify(x, u, 'numpy')
    dudx_l = lambdify(x, dudx, 'numpy')
    dudx_j = dudx_l(points)
    uj = ul(points)

    u_hat = shenfun.Function(SD)
    u_hat = SD.forward(uj, u_hat)
    uj = SD.backward(u_hat, uj)
    u_hat = SD.forward(uj, u_hat)

    uc_hat = shenfun.Function(SD)
    uc_hat = SD.CT.forward(uj, uc_hat)
    dudx_j = SD.CT.fast_derivative(uj)

    Cm = inner_product((SD, 0), (SD, 1))
    B = inner_product((SD, 0), (SD, 0))
    TDMASolver = TDMA(B)

    cs = np.zeros_like(u_hat)
    cs = Cm.matvec(u_hat, cs)

    # Should equal (but not exact so use extra resolution)
    cs2 = np.zeros(M)
    cs2 = SD.scalar_product(dudx_j, cs2)
    s = SD.slice()
    assert np.allclose(cs[s], cs2[s], rtol=1e-5, atol=1e-6)

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

    assert np.linalg.norm(du[s] - dudx_j[s]) / 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 = np.zeros_like(u3_hat)
    cs = Cm.matvec(u3_hat, cs)
    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))
    SD.plan((M, 4, 4), 0, np.complex, {})
    cs2 = SD.scalar_product(du3, cs2)

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

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

    assert np.linalg.norm(du3[s] - d3[s]) / (M * 16) < 1e-10
示例#2
0
def test_CDDmat(quad):
    M = 48
    SD = cbases.ShenDirichlet(M, quad=quad)
    u = (1-x**2)*sin(np.pi*6*x)
    dudx = u.diff(x, 1)
    dudx_hat = shenfun.Function(SD, buffer=dudx)
    u_hat = shenfun.Function(SD, buffer=u)
    ducdx_hat = shenfun.project(shenfun.Dx(u_hat, 0, 1), SD)
    assert np.linalg.norm(ducdx_hat-dudx_hat)/M < 1e-10, np.linalg.norm(ducdx_hat-dudx_hat)/M

    # Multidimensional version
    SD0 = cbases.ShenDirichlet(8, quad=quad)
    SD1 = cbases.ShenDirichlet(M, quad=quad)
    T = shenfun.TensorProductSpace(shenfun.comm, (SD0, SD1))
    u = (1-y**2)*sin(np.pi*6*y)
    dudy = u.diff(y, 1)
    dudy_hat = shenfun.Function(T, buffer=dudy)
    u_hat = shenfun.Function(T, buffer=u)
    ducdy_hat = shenfun.project(shenfun.Dx(u_hat, 1, 1), T)
    assert np.linalg.norm(ducdy_hat-dudy_hat)/M < 1e-10, np.linalg.norm(ducdy_hat-dudy_hat)/M
示例#3
0
 def __init__(self, N, alfa, beta, axis, quad="GL"):
     """alfa*ADD + beta*BDD
     """
     self.quad = quad
     self.shape = (N - 2, N - 2)
     SD = bases.ShenDirichlet(N, quad)
     self.B = inner_product((SD, 0), (SD, 0))
     self.A = inner_product((SD, 0), (SD, 2))
     self.axis = axis
     self.alfa = np.broadcast_to(alfa, beta.shape).copy()
     self.beta = beta