def test_Helmholtz2(SD):
    M = 2*N
    kx = 11
    points, weights = SD.points_and_weights(M)
    uj = np.random.randn(M)
    u_hat = np.zeros(M)
    u_hat = SD.fst(uj, u_hat)
    uj = SD.ifst(u_hat, uj)
    
    A = ADDmat(np.arange(M).astype(np.float))
    B = BDDmat(np.arange(M).astype(np.float), SD.quad)
    s = slice(0, M-2)

    u1 = np.zeros(M)
    u1 = SD.fst(uj, u1)
    c = A.matvec(u1)+kx**2*B.matvec(u1)

    b = np.zeros(M)
    SFTc.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
    SFTc.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
def test_ADDmat(ST2):
    M = 2*N
    u = (1-x**2)*sin(np.pi*x)
    f = -u.diff(x, 2)
    
    points, weights = ST2.points_and_weights(M)
    uj = np.array([u.subs(x, h) for h in points], dtype=np.float)
    fj = np.array([f.subs(x, h) for h in points], dtype=np.float)
    
    if ST2.__class__.__name__ == "ShenDirichletBasis":
        A = ADDmat(np.arange(M).astype(np.float))
        s = slice(0, M-2)
    elif ST2.__class__.__name__ == "ShenNeumannBasis":
        A = ANNmat(np.arange(M).astype(np.float))
        s = slice(1, M-2)
        fj -= np.dot(fj, weights)/weights.sum()
        uj -= np.dot(uj, weights)/weights.sum()
        
    f_hat = np.zeros(M)
    f_hat = ST2.fastShenScalar(fj, f_hat)
    u_hat = np.zeros(M)
    u_hat[s] = la.spsolve(A.diags(), f_hat[s])
    
    u0 = np.zeros(M)
    u0 = ST2.ifst(u_hat, u0)
        
    #from IPython import embed; embed()
    assert np.allclose(u0, uj)
    
    u1 = np.zeros(M)
    u1 = ST2.fst(uj, u1)
    c = A.matvec(u1)
    
    assert np.allclose(c, f_hat)
Beispiel #3
0
def test_ADDmat(ST2):
    M = 2 * N
    u = (1 - x**2) * sin(np.pi * x)
    f = -u.diff(x, 2)

    points, weights = ST2.points_and_weights(M)
    uj = np.array([u.subs(x, h) for h in points], dtype=np.float)
    fj = np.array([f.subs(x, h) for h in points], dtype=np.float)

    if ST2.__class__.__name__ == "ShenDirichletBasis":
        A = ADDmat(np.arange(M).astype(np.float))
        s = slice(0, M - 2)
    elif ST2.__class__.__name__ == "ShenNeumannBasis":
        A = ANNmat(np.arange(M).astype(np.float))
        s = slice(1, M - 2)
        fj -= np.dot(fj, weights) / weights.sum()
        uj -= np.dot(uj, weights) / weights.sum()

    f_hat = np.zeros(M)
    f_hat = ST2.fastShenScalar(fj, f_hat)
    u_hat = np.zeros(M)
    u_hat[s] = la.spsolve(A.diags(), f_hat[s])

    u0 = np.zeros(M)
    u0 = ST2.ifst(u_hat, u0)

    #from IPython import embed; embed()
    assert np.allclose(u0, uj)

    u1 = np.zeros(M)
    u1 = ST2.fst(uj, u1)
    c = A.matvec(u1)

    assert np.allclose(c, f_hat)
Beispiel #4
0
def test_Helmholtz_matvec(SD):
    M = 2 * N
    kx = 11
    points, weights = SD.points_and_weights(M)
    uj = np.random.randn(M)
    u_hat = np.zeros(M)
    u_hat = SD.fst(uj, u_hat)
    uj = SD.ifst(u_hat, uj)

    A = ADDmat(np.arange(M).astype(np.float))
    B = BDDmat(np.arange(M).astype(np.float), SD.quad)
    AB = HelmholtzCoeff(np.arange(M).astype(np.float), 1, kx**2, SD.quad)
    s = slice(0, M - 2)

    u1 = np.zeros(M)
    u1 = SD.fst(uj, u1)
    c = A.matvec(u1) + kx**2 * B.matvec(u1)

    b = np.zeros(M)
    #SFTc.Mult_Helmholtz_1D(M, SD.quad=="GL", 1, kx**2, u1, b)
    b = AB.matvec(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
    #SFTc.Mult_Helmholtz_3D_complex(M, SD.quad=="GL", 1.0, kx**2, u1, b)
    AB = HelmholtzCoeff(np.arange(M).astype(np.float), 1, kx**2, SD.quad)
    b = AB.matvec(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
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
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 #7
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 #8
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
def solve(fk):

    N = len(fk)+2
    k = ST.wavenumbers(N)
    if solver == "banded":
        A = np.zeros((N-2, N-2))
        A[-1, :] = -2*np.pi*(k+1)*(k+2)
        for i in range(2, N-2, 2):
            A[-i-1, i:] = -4*np.pi*(k[:-i]+1)
        uk_hat = solve_banded((0, N-3), A, fk)

    elif solver == "sparse":
        aij = [-2*np.pi*(k+1)*(k+2)]
        for i in range(2, N-2, 2):
            aij.append(np.array(-4*np.pi*(k[:-i]+1)))
        A = diags(aij, range(0, N-2, 2))
        uk_hat = la.spsolve(A, fk)

    elif solver == "bs":
        uk_hat = np.zeros_like(fk)
        A = ADDmat(np.arange(N).astype(float), scale=1.0)
        uk_hat = A.apply_inverse(fk, uk_hat)

    return uk_hat
def solve(fk):

    N = len(fk) + 2
    k = ST.wavenumbers(N)
    if solver == "banded":
        A = np.zeros((N - 2, N - 2))
        A[-1, :] = -2 * np.pi * (k + 1) * (k + 2)
        for i in range(2, N - 2, 2):
            A[-i - 1, i:] = -4 * np.pi * (k[:-i] + 1)
        uk_hat = solve_banded((0, N - 3), A, fk)

    elif solver == "sparse":
        aij = [-2 * np.pi * (k + 1) * (k + 2)]
        for i in range(2, N - 2, 2):
            aij.append(np.array(-4 * np.pi * (k[:-i] + 1)))
        A = diags(aij, range(0, N - 2, 2))
        uk_hat = la.spsolve(A, fk)

    elif solver == "bs":
        uk_hat = np.zeros_like(fk)
        A = ADDmat(np.arange(N).astype(float), scale=1.0)
        uk_hat = A.apply_inverse(fk, uk_hat)

    return uk_hat