예제 #1
0
def test_Mult_Div():

    SD = ShenDirichletBasis("GC")
    SN = ShenDirichletBasis("GC")

    Cm = CNDmat(np.arange(N).astype(np.float))
    Bm = BNDmat(np.arange(N).astype(np.float), "GC")

    uk = np.random.randn((N)) + np.random.randn((N)) * 1j
    vk = np.random.randn((N)) + np.random.randn((N)) * 1j
    wk = np.random.randn((N)) + np.random.randn((N)) * 1j

    b = np.zeros(N, dtype=np.complex)
    uk0 = np.zeros(N, dtype=np.complex)
    vk0 = np.zeros(N, dtype=np.complex)
    wk0 = np.zeros(N, dtype=np.complex)

    uk0 = SD.fst(uk, uk0)
    uk = SD.ifst(uk0, uk)
    uk0 = SD.fst(uk, uk0)
    vk0 = SD.fst(vk, vk0)
    vk = SD.ifst(vk0, vk)
    vk0 = SD.fst(vk, vk0)
    wk0 = SD.fst(wk, wk0)
    wk = SD.ifst(wk0, wk)
    wk0 = SD.fst(wk, wk0)

    SFTc.Mult_Div_1D(N, 7, 7, uk0[: N - 2], vk0[: N - 2], wk0[: N - 2], b[1 : N - 2])

    uu = Cm.matvec(uk0)
    uu += 1j * 7 * Bm.matvec(vk0) + 1j * 7 * Bm.matvec(wk0)

    # from IPython import embed; embed()
    assert np.allclose(uu, b)

    uk0 = uk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * uk0.repeat(4 * 4).reshape((N, 4, 4))
    vk0 = vk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * vk0.repeat(4 * 4).reshape((N, 4, 4))
    wk0 = wk0.repeat(4 * 4).reshape((N, 4, 4)) + 1j * wk0.repeat(4 * 4).reshape((N, 4, 4))
    b = np.zeros((N, 4, 4), dtype=np.complex)
    m = np.zeros((4, 4)) + 7
    n = np.zeros((4, 4)) + 7
    SFTc.Mult_Div_3D(N, m, n, uk0[: N - 2], vk0[: N - 2], wk0[: N - 2], b[1 : N - 2])

    uu = Cm.matvec(uk0)
    uu += 1j * 7 * Bm.matvec(vk0) + 1j * 7 * Bm.matvec(wk0)

    assert np.allclose(uu, b)
예제 #2
0
    
    Au + kx^2*Bu = f

"""

# Use sympy to compute a rhs, given an analytical solution
x = Symbol("x")
u = (1-x**2)**2*cos(np.pi*x)*(x-0.25)**2
kx = np.sqrt(5)
f = -u.diff(x, 2) + kx**2*u

# Choices
solver = "lu"
N = 16

ST = ShenDirichletBasis(quad="GL")
points, weights = ST.points_and_weights(N) 

# Gauss-Chebyshev quadrature to compute rhs
fj = np.array([f.subs(x, j) for j in points], dtype=float)     # Get f on quad points

#@profile
def solve(fk):
    
    k = ST.wavenumbers(N)
        
    if solver == "sparse":
        A = Amat(np.arange(N).astype(np.float)).diags()
        B = BDmat(np.arange(N).astype(np.float), "GL").diags()        
        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])