Beispiel #1
0
class TestNodality(TestCase):
    """Test for Element.doflocs."""

    elems = [
        ElementLineP0(),
        ElementLineP1(),
        ElementLineP2(),
        ElementLinePp(1),
        ElementLinePp(3),
        ElementLineMini(),
        ElementTriP0(),
        ElementTriP1(),
        ElementTriP2(),
        ElementTriP3(),
        ElementTriP4(),
        ElementTriMini(),
        ElementQuad0(),
        ElementQuad1(),
        ElementQuad2(),
        ElementQuadS2(),
        ElementQuadP(1),
        ElementQuadP(3),
        ElementTetP0(),
        ElementTetP1(),
        ElementTetP2(),
        ElementTetMini(),
        ElementHex1(),
        ElementHexS2(),
        ElementHex2(),
        ElementTetCR(),
        ElementTetCCR(),
        ElementTriCR(),
        ElementTriCCR(),
        ElementWedge1(),
    ]

    def runTest(self):
        for e in self.elems:
            N = e.doflocs.shape[0]
            Ih = np.zeros((N, N))
            for itr in range(N):
                Ih[itr] = e.lbasis(e.doflocs.T, itr)[0]

            # Remove nan-rows: test nodality only on non-nan doflocs.
            #
            # Some elements, such as ElementTriMini might have a combination
            # of nodal dofs and non-nodal dofs.
            #
            # Nodal dof is defined so that there exists a point where the
            # corresponding basis function is one, and other basis functions
            # are zero. Non-nodal dof does not satisfy this property.
            ix = np.isnan(np.sum(Ih, axis=1))
            Nnan = np.sum(ix)
            ixs = np.nonzero(~ix)[0]
            Ih = Ih[ixs].T[ixs].T

            assert_allclose(Ih,
                            np.eye(N - Nnan),
                            atol=1e-13,
                            err_msg="{}".format(type(e)))
Beispiel #2
0
class SolveInhomogeneousLaplace(TestCase):
    """Adapted from example 14."""

    mesh = MeshTri
    elem = ElementTriP2()

    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,
        )
Beispiel #3
0
class TestPartitionofUnity(TestCase):
    """Test that elements form a partition of unity."""

    elems = [
        ElementLineP1(),
        ElementLineP2(),
        ElementTriP1(),
        ElementTriP2(),
        ElementQuad1(),
        ElementQuad2(),
        ElementQuadS2(),
        ElementTetP1(),
        ElementTetP2(),
        ElementHex1(),
        ElementHexS2(),
        ElementHex2(),
    ]

    def runTest(self):
        for elem in self.elems:
            if elem.dim == 1:
                y = np.array([[.15]])
            elif elem.dim == 2:
                y = np.array([[.15], [.15]])
            elif elem.dim == 3:
                y = np.array([[.15], [.15], [.15]])
            out = 0.
            for i in range(elem.doflocs.shape[0]):
                out += elem.lbasis(y, i)[0][0]
            self.assertAlmostEqual(out, 1, msg='failed for {}'.format(elem))
Beispiel #4
0
    def __init__(self, doflocs, t, **kwargs):

        warnings.warn("MeshTri2 is an experimental feature and "
                      "not governed by the semantic versioning. "
                      "Several features of MeshTri are still "
                      "missing.")

        if t.shape[0] == 6:
            dofs, ix = np.unique(t[:3], return_inverse=True)
            super(MeshTri2, self).__init__(
                doflocs[:, dofs],
                np.arange(len(dofs), dtype=np.int)[ix].reshape(t[:3].shape),
                sort_t=False,
                **kwargs)
        else:
            # fallback for refinterp
            super(MeshTri2, self).__init__(doflocs, t, **kwargs)
        from skfem.element import ElementTriP2
        from skfem.assembly import InteriorBasis
        from skfem.mapping import MappingAffine
        self._elem = ElementTriP2()
        self._basis = InteriorBasis(self, self._elem, MappingAffine(self))
        self._mesh = MeshTri.from_basis(self._basis)
        if t.shape[0] == 6:
            self._mesh.p = doflocs
            self._mesh.t = t
Beispiel #5
0
def test_subdomain_facet_assembly():
    def subdomain(x):
        return np.logical_and(
            np.logical_and(x[0] > .25, x[0] < .75),
            np.logical_and(x[1] > .25, x[1] < .75),
        )

    m, e = MeshTri().refined(4), ElementTriP2()
    cbasis = CellBasis(m, e)
    cbasis_p0 = cbasis.with_element(ElementTriP0())

    sfbasis = FacetBasis(m, e, facets=m.facets_around(subdomain, flip=True))
    sfbasis_p0 = sfbasis.with_element(ElementTriP0())
    sigma = cbasis_p0.zeros() + 1

    @BilinearForm
    def laplace(u, v, w):
        return dot(w.sigma * grad(u), grad(v))

    A = laplace.assemble(cbasis, sigma=cbasis_p0.interpolate(sigma))
    u0 = cbasis.zeros()
    u0[cbasis.get_dofs(elements=subdomain)] = 1
    u0_dofs = cbasis.get_dofs() + cbasis.get_dofs(elements=subdomain)
    A, b = enforce(A, D=u0_dofs, x=u0)
    u = solve(A, b)

    @Functional
    def measure_current(w):
        return dot(w.n, w.sigma * grad(w.u))

    meas = measure_current.assemble(sfbasis,
                                    sigma=sfbasis_p0.interpolate(sigma),
                                    u=sfbasis.interpolate(u))

    assert_almost_equal(meas, 9.751915526759191)
Beispiel #6
0
class TestDerivatives(TestCase):
    """Test values of derivatives."""

    elems = [
        ElementLineP1(),
        ElementLineP2(),
        ElementTriP1(),
        ElementTriP2(),
        ElementTriMini(),
        ElementQuad1(),
        ElementQuad2(),
        ElementQuadS2(),
        ElementTetP1(),
        ElementTetP2(),
        ElementTetMini(),
        ElementHex1(),
        ElementHexS2(),
    ]

    def runTest(self):
        for elem in self.elems:
            eps = 1e-6
            for base in [0., .3, .6, .9]:
                if elem.dim == 1:
                    y = np.array([[base, base + eps]])
                elif elem.dim == 2:
                    y = np.array([[base, base + eps, base, base],
                                  [base, base, base, base + eps]])
                elif elem.dim == 3:
                    y = np.array([[base, base + eps, base, base, base, base],
                                  [base, base, base, base + eps, base, base],
                                  [base, base, base, base, base, base + eps]])
                i = 0
                while True:
                    try:
                        out = elem.lbasis(y, i)
                    except ValueError:
                        break
                    diff = (out[0][1] - out[0][0]) / eps
                    errmsg = 'x-derivative for {}th bfun failed for {}'
                    self.assertAlmostEqual(diff,
                                           out[1][0][0],
                                           delta=1e-3,
                                           msg=errmsg.format(i, elem))
                    if elem.dim > 1:
                        diff = (out[0][3] - out[0][2]) / eps
                        errmsg = 'y-derivative for {}th bfun failed for {}'
                        self.assertAlmostEqual(diff,
                                               out[1][1][3],
                                               delta=1e-3,
                                               msg=errmsg.format(i, elem))
                    if elem.dim == 3:
                        diff = (out[0][5] - out[0][4]) / eps
                        errmsg = 'z-derivative for {}th bfun failed for {}'
                        self.assertAlmostEqual(diff,
                                               out[1][2][4],
                                               delta=1e-3,
                                               msg=errmsg.format(i, elem))
                    i += 1
Beispiel #7
0
    def runTest(self):

        m = MeshTri()
        basis = InteriorBasis(m, ElementTriP2())
        dofs = basis.get_dofs()

        self.assertEqual(len(dofs.nodal['u']), 4)
        self.assertEqual(len(dofs.facet['u']), 4)
Beispiel #8
0
    def runTest(self):

        m = MeshTri()
        basis = InteriorBasis(m, ElementTriP2())
        D1 = basis.get_dofs(lambda x: x[0] == 0)
        D2 = basis.get_dofs(lambda x: x[0] == 1)
        D3 = basis.get_dofs(lambda x: x[1] == 1)
        D4 = basis.get_dofs(lambda x: x[1] == 0)
        assert_allclose(D1 | D2 | D3 | D4, basis.get_dofs())
        assert_allclose(D1 + D2 + D3 + D4, basis.get_dofs())
Beispiel #9
0
    def runTest(self):
        """Solve Stokes problem, try splitting and other small things."""

        m = MeshTri().refined()
        m = m.refined(3).with_boundaries({
            'up': lambda x: x[1] == 1.,
            'rest': lambda x: x[1] != 1.,
        })

        e = ElementVectorH1(ElementTriP2()) * ElementTriP1()

        basis = CellBasis(m, e)

        @BilinearForm
        def bilinf(u, p, v, q, w):
            from skfem.helpers import grad, ddot, div
            return (ddot(grad(u), grad(v)) - div(u) * q - div(v) * p
                    - 1e-2 * p * q)

        S = asm(bilinf, basis)

        D = basis.find_dofs(skip=['u^2'])
        x = basis.zeros()
        x[D['up'].all('u^1^1')] = .1

        x = solve(*condense(S, x=x, D=D))

        (u, u_basis), (p, p_basis) = basis.split(x)

        self.assertEqual(len(u), m.p.shape[1] * 2 + m.facets.shape[1] * 2)
        self.assertEqual(len(p), m.p.shape[1])

        self.assertTrue(np.sum(p - x[basis.nodal_dofs[2]]) < 1e-8)

        U, P = basis.interpolate(x)
        self.assertTrue(isinstance(U.value, np.ndarray))
        self.assertTrue(isinstance(P.value, np.ndarray))
        self.assertTrue(P.shape[0] == m.nelements)

        self.assertTrue((basis.doflocs[:, D['up'].all()][1] == 1.).all())

        # test blocks splitting of forms while at it
        C1 = asm(bilinf.block(1, 1), CellBasis(m, ElementTriP1()))
        C2 = S[basis.nodal_dofs[-1]].T[basis.nodal_dofs[-1]].T
        self.assertTrue(abs((C1 - C2).min()) < 1e-10)
        self.assertTrue(abs((C1 - C2).max()) < 1e-10)

        # test splitting ElementVector
        (ux, uxbasis), (uy, uybasis) = u_basis.split(u)
        assert_allclose(ux[uxbasis.nodal_dofs[0]], u[u_basis.nodal_dofs[0]])
        assert_allclose(ux[uxbasis.facet_dofs[0]], u[u_basis.facet_dofs[0]])
        assert_allclose(uy[uybasis.nodal_dofs[0]], u[u_basis.nodal_dofs[1]])
        assert_allclose(uy[uybasis.facet_dofs[0]], u[u_basis.facet_dofs[1]])
Beispiel #10
0
    def runTest(self):
        """Solve Stokes problem, try splitting and other small things."""

        m = MeshTri()
        m.refine()
        m.define_boundary('centreline',
                          lambda x: x[0] == .5,
                          boundaries_only=False)
        m.refine(3)

        e = ElementVectorH1(ElementTriP2()) * ElementTriP1()

        m.define_boundary('up', lambda x: x[1] == 1.)
        m.define_boundary('rest', lambda x: x[1] != 1.)

        basis = InteriorBasis(m, e)
        self.assertEqual(
            basis.get_dofs(m.boundaries['centreline']).all().size,
            (2 + 1) * (2**(1 + 3) + 1) + 2 * 2**(1 + 3))
        self.assertEqual(basis.find_dofs()['centreline'].all().size,
                         (2 + 1) * (2**(1 + 3) + 1) + 2 * 2**(1 + 3))

        @BilinearForm
        def bilinf(u, p, v, q, w):
            from skfem.helpers import grad, ddot, div
            return (ddot(grad(u), grad(v)) - div(u) * q - div(v) * p -
                    1e-2 * p * q)

        S = asm(bilinf, basis)

        D = basis.find_dofs(skip=['u^2'])
        x = basis.zeros()
        x[D['up'].all('u^1^1')] = .1

        x = solve(*condense(S, basis.zeros(), x=x, D=D))

        (u, u_basis), (p, p_basis) = basis.split(x)

        self.assertEqual(len(u), m.p.shape[1] * 2 + m.facets.shape[1] * 2)
        self.assertEqual(len(p), m.p.shape[1])

        self.assertTrue(np.sum(p - x[basis.nodal_dofs[2]]) < 1e-8)

        U, P = basis.interpolate(x)
        self.assertTrue(isinstance(U.value, np.ndarray))
        self.assertTrue(isinstance(P.value, np.ndarray))

        self.assertTrue((basis.doflocs[:, D['up'].all()][1] == 1.).all())
    def runTest(self):
        """Solve Stokes problem, try splitting and other small things."""

        m = MeshTri().refined()
        m = m.refined(3).with_boundaries({
            'up': lambda x: x[1] == 1.,
            'rest': lambda x: x[1] != 1.,
        })

        e = ElementVectorH1(ElementTriP2()) * ElementTriP1()

        basis = CellBasis(m, e)

        @BilinearForm
        def bilinf(u, p, v, q, w):
            from skfem.helpers import grad, ddot, div
            return (ddot(grad(u), grad(v)) - div(u) * q - div(v) * p
                    - 1e-2 * p * q)

        S = asm(bilinf, basis)

        D = basis.find_dofs(skip=['u^2'])
        x = basis.zeros()
        x[D['up'].all('u^1^1')] = .1

        x = solve(*condense(S, x=x, D=D))

        (u, u_basis), (p, p_basis) = basis.split(x)

        self.assertEqual(len(u), m.p.shape[1] * 2 + m.facets.shape[1] * 2)
        self.assertEqual(len(p), m.p.shape[1])

        self.assertTrue(np.sum(p - x[basis.nodal_dofs[2]]) < 1e-8)

        U, P = basis.interpolate(x)
        self.assertTrue(isinstance(U.value, np.ndarray))
        self.assertTrue(isinstance(P.value, np.ndarray))

        self.assertTrue((basis.doflocs[:, D['up'].all()][1] == 1.).all())
Beispiel #12
0
        def nonsym(u, v, w):
            return u.grad[0] * v

        @BilinearForm(nthreads=2)
        def threaded_nonsym(u, v, w):
            return u.grad[0] * v

        assert_almost_equal(
            nonsym.assemble(basis).toarray(),
            threaded_nonsym.assemble(basis).toarray(),
        )


@pytest.mark.parametrize("m,e,edg", [
    (MeshTri().refined(), ElementTriP1(), ElementTriDG),
    (MeshTri().refined(), ElementTriP2(), ElementTriDG),
    (MeshTet().refined(), ElementTetP1(), ElementTetDG),
    (MeshTet().refined(), ElementTetP2(), ElementTetDG),
    (MeshTri().refined(), ElementTriMorley(), ElementTriDG),
    (MeshTri().refined(), ElementTriHermite(), ElementTriDG),
    (MeshQuad().refined(), ElementQuad1(), ElementQuadDG),
    (MeshQuad().refined(), ElementQuad2(), ElementQuadDG),
    (MeshQuad().refined(), ElementQuadP(4), ElementQuadDG),
    (MeshHex().refined(), ElementHex2(), ElementHexDG),
])
def test_coodata_inverse(m, e, edg):

    E = edg(e)
    basis = Basis(m, E)
    basisdg = Basis(m, E)
    M1 = mass.assemble(basis)
Beispiel #13
0
    element_type = ElementTetP1
    maxval = 0.0405901240018571

    def init_mesh(self):
        return self.mesh_type.init_ball().scaled(0.5)


class SolveCirclePoissonTet2(SolveCirclePoisson):

    mesh_type = MeshTet2
    element_type = ElementTetP2
    filename = "quadratic_sphere_tet.msh"
    maxval = 0.0405901240018571


@pytest.mark.parametrize("mesh_elem", [(MeshTri, ElementTriP2()),
                                       (MeshQuad, ElementQuad2())])
@pytest.mark.parametrize("impose", [enforce, penalize])
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
Beispiel #14
0
 def create_basis(self, m):
     e = ElementTriP2()
     return Basis(m, e)
Beispiel #15
0
class TestIncompatibleMeshElement(TestCase):

    def runTest(self):

        with self.assertRaises(ValueError):
            m = MeshTri()
            e = ElementTetP2()
            basis = CellBasis(m, e)


@pytest.mark.parametrize(
    "mtype,e,nrefs,npoints",
    [
        (MeshTri, ElementTriP1(), 0, 10),
        (MeshTri, ElementTriP2(), 1, 10),
        (MeshTri, ElementTriP1(), 5, 10),
        (MeshTri, ElementTriP1(), 1, 3e5),
        (MeshTet, ElementTetP2(), 1, 10),
        (MeshTet, ElementTetP1(), 4, 10),
        (MeshTet, ElementTetP1(), 1, 3e4),
        (MeshQuad, ElementQuad1(), 1, 10),
        (MeshQuad, ElementQuad1(), 1, 3e5),
        (MeshHex, ElementHex1(), 1, 1e5),
        (MeshWedge1, ElementWedge1(), 0, 10),
    ]
)
def test_interpolator_probes(mtype, e, nrefs, npoints):

    m = mtype()
    if nrefs > 0:
Beispiel #16
0
    nrefs = 5


class TestIncompatibleMeshElement(TestCase):
    def runTest(self):

        with self.assertRaises(ValueError):
            m = MeshTri()
            e = ElementTetP2()
            basis = InteriorBasis(m, e)


@pytest.mark.parametrize("mtype,e1,e2", [
    (MeshTri, ElementTriP1(), ElementTriP0()),
    (MeshTri, ElementTriP1(), ElementTriP1()),
    (MeshTri, ElementTriP2(), ElementTriP1()),
    (MeshTri, ElementTriP2(), ElementTriP2()),
    (MeshTri, ElementTriP2(), None),
    (MeshQuad, ElementQuad1(), ElementQuad0()),
    (MeshQuad, ElementQuad1(), ElementQuad1()),
    (MeshQuad, ElementQuad2(), ElementQuad2()),
    (MeshTet, ElementTetP1(), ElementTetP0()),
    (MeshTet, ElementTetP2(), ElementTetP2()),
    (MeshHex, ElementHex1(), ElementHex0()),
    (MeshHex, ElementHex1(), ElementHex1()),
    (MeshHex, ElementHex2(), ElementHex2()),
])
def test_trace(mtype, e1, e2):

    m = mtype().refined(3)
Beispiel #17
0
    nrefs = 5


class TestIncompatibleMeshElement(TestCase):
    def runTest(self):

        with self.assertRaises(ValueError):
            m = MeshTri()
            e = ElementTetP2()
            basis = InteriorBasis(m, e)


@pytest.mark.parametrize("mtype,e1,e2", [
    (MeshTri, ElementTriP1(), ElementLineP0()),
    (MeshTri, ElementTriP1(), ElementLineP1()),
    (MeshTri, ElementTriP2(), ElementLineP1()),
    (MeshTri, ElementTriP2(), ElementLineP2()),
    (MeshTri, ElementTriP2(), None),
    (MeshQuad, ElementQuad1(), ElementLineP0()),
    (MeshQuad, ElementQuad1(), ElementLineP1()),
    (MeshQuad, ElementQuad2(), ElementLineP2()),
    (MeshTet, ElementTetP1(), ElementTriP0()),
    (MeshTet, ElementTetP2(), ElementTriP2()),
    (MeshHex, ElementHex1(), ElementQuad0()),
    (MeshHex, ElementHex1(), ElementQuad1()),
    (MeshHex, ElementHex2(), ElementQuad2()),
])
def test_trace(mtype, e1, e2):

    m = mtype().refined(3)