Exemplo n.º 1
0
def test_CDDmat(quad):
    M = 128
    SD = cbases.ShenDirichletBasis(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])

    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
Exemplo n.º 2
0
 def __init__(self, N, alfa, beta, quad="GL"):
     """alfa*ADD + beta*BDD
     """
     self.quad = quad
     self.shape = (N - 2, N - 2)
     SD = bases.ShenDirichletBasis(N, quad)
     self.B = inner_product((SD, 0), (SD, 0))
     self.A = inner_product((SD, 0), (SD, 2))
     self.alfa = alfa
     self.beta = beta
Exemplo n.º 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.ShenDirichletBasis(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
Exemplo n.º 4
0
 def __init__(self, K, alfa, beta, quad="GL"):
     """alfa*ADD + beta*BDD
     """
     self.quad = quad
     N = self.N = K.shape[0] - 2
     self.shape = (N, N)
     SD = bases.ShenDirichletBasis(N + 2, quad)
     self.B = inner_product((SD, 0), (SD, 0))
     self.A = inner_product((SD, 0), (SD, 2))
     self.alfa = alfa
     self.beta = beta