Example #1
0
def test_funspace_1():
    '''Test of FunctionSpace.int_phi made up of linear elements'''
    ox, dx, nx = 0., 10., 5
    mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1')
    V = FunctionSpace(mesh, {'B1': Element(type='link2')})

    # basic properties
    assert V.num_dof == (nx + 1)
    assert V.size == nx
    X = np.linspace(ox, dx, nx+1)
    assert allclose(V.X, X)

    # Integrate[1, {x, ox, lx}]
    f = lambda x: 1
    ans = np.sum(V.int_phi(f))
    exact = V.X[-1]
    assert areclose(ans, exact)

    # Integrate[x, {x, ox, lx}]
    f = lambda x: x
    ans = np.sum(V.int_phi(f))
    exact = V.X[-1] ** 2 / 2.
    assert areclose(ans, exact)

    # Integrate[x**2, {x, ox, lx}]
    f = lambda x: x * x
    ans = np.sum(V.int_phi(f))
    exact = V.X[-1] ** 3 / 3.
    assert areclose(ans, exact)

    # Integrate[x**3, {x, ox, lx}]
    f = lambda x: x * x * x
    ans = np.sum(V.int_phi(f))
    exact = V.X[-1] ** 4 / 4.
    assert areclose(ans, exact)

    # Integrate[x**4, {x, ox, lx}]
    f = lambda x: x * x * x * x
    ans = np.sum(V.int_phi(f))
    exact = V.X[-1] ** 5 / 5.
    assert areclose(ans, exact, tol=1.5)
Example #2
0
def test_funspace_8():
    '''Testing of FunctionSpace.int_phi_phi, MMS'''
    ox, dx, nx = 0., 10., 10
    mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1')
    V = FunctionSpace(mesh, {'B1': Element(type='link2')})

    rhs = np.random.rand(V.num_dof)
    A = V.int_phi_phi()
    f = lambda x: np.interp(x, V.X, rhs)
    b = V.int_phi(f)
    assert areclose(np.dot(A, rhs), b)
    return
Example #3
0
def test_funspace_6():
    '''Test of FunctionSpace.int_phi_phi made up of linear elements, Laplace
    norm check.  Solution = x^2

    '''
    ox, dx, nx = 0., 1., 10
    mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1')
    V = FunctionSpace(mesh, {'B1': Element(type='link2')})
    C = V.int_phi_phi(derivative=(True, True))

    sol = V.X ** 2
    b = np.dot(C, sol)
    rhs = V.int_phi(lambda x: 2.)
    # natural b.c. not satisfied on right, don't check it
    rhs[-1] = -b[-1]
    assert norm(rhs + b) < 1.e-12
Example #4
0
def test_funspace_9():
    '''Testing of FunctionSpace.int_phi_phi, mixed integration MMS'''
    ox, dx, nx = 0., 10., 10
    mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1')
    V = FunctionSpace(mesh, {'B1': Element(type='link2')})

    D = V.int_phi_phi(derivative=(False,True))
    sol = np.ones(V.num_dof)
    b = np.dot(D, sol)
    # Norm check (rhs d/dx + Neumann, const soln)
    assert norm(b) < 1.e-12

    D[0, 0] = 1.0
    D[0, 1:] = 0.0
    D[-1, -1] = 1.0
    D[-1, 0:-1] = 0.0
    sol = V.X
    b = np.dot(D, sol)
    rhs = V.int_phi(lambda x: 1)
    rhs[0] = sol[0]
    rhs[-1] = sol[-1]
    # norm check (d/dx+Dirichlet sol=x)
    assert norm(rhs - b) < 1.e-12