Esempio n. 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)))
Esempio n. 2
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))
Esempio n. 3
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
Esempio n. 4
0
    def runTest(self):

        m = MeshHex()
        # check that these assemble to the same matrix
        ec = ElementHex1() * ElementHex1() * ElementHex1()
        ev = ElementVectorH1(ElementHex1())
        basisc = Basis(m, ec)
        basisv = Basis(m, ev)

        @BilinearForm
        def bilinf_ev(u, v, w):
            from skfem.helpers import dot
            return dot(u, v)

        @BilinearForm
        def bilinf_ec(ux, uy, uz, vx, vy, vz, w):
            return ux * vx + uy * vy + uz * vz

        Kv = asm(bilinf_ev, basisv)
        Kc = asm(bilinf_ec, basisc)

        self.assertAlmostEqual(np.sum(np.sum((Kv - Kc).todense())), 0.)
Esempio n. 5
0
 def createBasis(self):
     m = MeshHex().refined(3)
     self.fbasis = FacetBasis(m, ElementHex1())
     self.boundary_area = 6.000
Esempio n. 6
0

class NormalVectorTestQuad(NormalVectorTestTri):

    case = (MeshQuad(), ElementQuad1())


class NormalVectorTestQuadP(NormalVectorTestTri):

    case = (MeshQuad(), ElementQuadP(3))
    test_integrate_volume = False


class NormalVectorTestHex(NormalVectorTestTri):

    case = (MeshHex(), ElementHex1())
    intorder = 3


class NormalVectorTestHexS2(NormalVectorTestTri):

    case = (MeshHex(), ElementHexS2())
    intorder = 3
    test_integrate_volume = False


class NormalVectorTestHex2(NormalVectorTestTri):

    case = (MeshHex(), ElementHex2())
    intorder = 3
    test_integrate_volume = False
Esempio n. 7
0
        ElementQuad2(),
    ),
    (
        MeshTri().refined(2),
        MeshTri1DG,
        ElementTriP2(),
    ),
    (
        MeshLine().refined(5),
        MeshLine1DG,
        ElementLineP1(),
    ),
    (
        MeshHex().refined(3),
        MeshHex1DG,
        ElementHex1(),
    ),
    (
        MeshLine().refined(),
        MeshLine1DG,
        ElementLinePp(5),
    ),
])
def test_periodic_loading(m, mdgtype, e):
    def _sort(ix):
        # sort index arrays so that ix[0] matches
        return ix[np.argsort(np.sum(m.p, axis=0)[ix])]

    mp = mdgtype.periodic(m, _sort(m.nodes_satisfying(lambda x: x[0] == 0)),
                          _sort(m.nodes_satisfying(lambda x: x[0] == 1)))
    basis = Basis(mp, e)
Esempio n. 8
0
 def create_basis(self, m):
     e = ElementHex1()
     return Basis(m, e)
Esempio n. 9
0
 def mapping(self):
     return MappingIsoparametric(self, ElementHex1(), ElementQuad1())
Esempio n. 10
0
            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:
        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)
Esempio n. 11
0

class NormalVectorTestQuad(NormalVectorTestTri):

    case = (MeshQuad(), ElementQuad1())


class NormalVectorTestQuadP(NormalVectorTestTri):

    case = (MeshQuad(), ElementQuadP(3))
    test_integrate_volume = False


class NormalVectorTestHex(NormalVectorTestTri):

    case = (MeshHex(), ElementHex1())
    intorder = 3


class NormalVectorTestHexS2(NormalVectorTestTri):

    case = (MeshHex(), ElementHexS2())
    intorder = 3
    test_integrate_volume = False


class NormalVectorTestHex2(NormalVectorTestTri):

    case = (MeshHex(), ElementHex2())
    intorder = 3
    test_integrate_volume = False
Esempio n. 12
0
        element = ElementQuadBFS()
        with self.assertRaises(ValueError):
            element.gdof(0, 0, -1)
        with self.assertRaises(ValueError):
            element.gdof(0, 0, 16)


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

    edg = edg(e)

    @Functional
    def square(w):
        return w['random']**2

    basis = InteriorBasis(m, e)
    basisdg = InteriorBasis(m, edg)

    assert_allclose(
        square.assemble(basis, random=basis.interpolate(basis.zeros() + 1)),
Esempio n. 13
0
 def mapping(self):
     from skfem.mapping import MappingIsoparametric
     from skfem.element import ElementHex1, ElementQuad1
     return MappingIsoparametric(self, ElementHex1(), ElementQuad1())
Esempio n. 14
0
            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)

    # use the boundary where last coordinate is zero
    basis = FacetBasis(
        m, e1, facets=m.facets_satisfying(lambda x: x[x.shape[0] - 1] == 0.0))
    xfun = projection(lambda x: x[0], InteriorBasis(m, e1))
    nbasis, y = basis.trace(xfun,
                            lambda p: p[0:(p.shape[0] - 1)],
                            target_elem=e2)
Esempio n. 15
0
            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)

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