예제 #1
0
    def runTest(self):
        m = self.mesh().refined(4)
        basis = InteriorBasis(m, self.elem)
        boundary_basis = FacetBasis(m, self.elem)
        boundary_dofs = boundary_basis.get_dofs().flatten()

        def dirichlet(x):
            """return a harmonic function"""
            return ((x[0] + 1.j * x[1])**2).real

        u = basis.zeros()
        A = laplace.assemble(basis)
        u[boundary_dofs] = projection(dirichlet,
                                      boundary_basis,
                                      I=boundary_dofs)
        u = solve(*enforce(A, x=u, D=boundary_dofs))

        @Functional
        def gradu(w):
            gradu = w['sol'].grad
            return dot(gradu, gradu)

        self.assertAlmostEqual(
            gradu.assemble(basis, sol=basis.interpolate(u)),
            8 / 3,
            delta=1e-10,
        )
예제 #2
0
def test_solving_inhomogeneous_laplace(mesh_elem, impose):
    """Adapted from example 14."""

    mesh, elem = mesh_elem

    m = mesh().refined(4)
    basis = Basis(m, elem)
    boundary_basis = FacetBasis(m, elem)
    boundary_dofs = boundary_basis.get_dofs().flatten()

    def dirichlet(x):
        """return a harmonic function"""
        return ((x[0] + 1.j * x[1])**2).real

    u = basis.zeros()
    A = laplace.assemble(basis)
    u[boundary_dofs] = projection(dirichlet, boundary_basis, I=boundary_dofs)
    u = solve(*impose(A, x=u, D=boundary_dofs))

    @Functional
    def gradu(w):
        gradu = w['sol'].grad
        return dot(gradu, gradu)

    np.testing.assert_almost_equal(gradu.assemble(basis,
                                                  sol=basis.interpolate(u)),
                                   8 / 3,
                                   decimal=9)
예제 #3
0
 def runTest(self):
     m = self.prepare_mesh()
     basis = CellBasis(m, self.element_type())
     x = projection(lambda x: x[0] ** 2, basis)
     fun = basis.interpolator(x)
     X = np.linspace(0, 1, 10)
     dim = m.dim()
     if dim == 3:
         y = fun(np.array([X, [0.31] * 10, [0.62] * 10]))
     elif dim == 2:
         y = fun(np.array([X, [0.31] * 10]))
     else:
         y = fun(np.array([X]))
     assert_allclose(y, X ** 2, atol=1e-10)
예제 #4
0
def test_trace(mtype, e1, e2, flat):

    m = mtype().refined(3)

    # use the boundary where last coordinate is zero
    basis = FacetBasis(m, e1, facets=m.facets_satisfying(lambda x: x[-1] == 0.0))
    xfun = projection(lambda x: x[0], CellBasis(m, e1))
    nbasis, y = basis.trace(xfun, lambda p: p[0] if flat else p[:-1], target_elem=e2)

    @Functional
    def integ(w):
        return w.y

    # integrate f(x) = x_1 over trace mesh
    assert_almost_equal(integ.assemble(nbasis, y=nbasis.interpolate(y)), .5)
예제 #5
0
def test_interpolator_probes(mtype, e, nrefs, npoints):

    m = mtype()
    if nrefs > 0:
        m = m.refined(nrefs)

    np.random.seed(0)
    X = np.random.rand(m.p.shape[0], int(npoints))

    basis = CellBasis(m, e)

    y = projection(lambda x: x[0] + x[1], basis)

    assert_allclose(basis.probes(X) @ y, basis.interpolator(y)(X))
    assert_allclose(basis.probes(X) @ y, X[0] + X[1])