Ejemplo n.º 1
0
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)),
        square.assemble(basisdg,
                        random=basisdg.interpolate(basisdg.zeros() + 1)),
    )
Ejemplo n.º 2
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,
        )
Ejemplo n.º 3
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())
Ejemplo n.º 4
0
    def runTest(self):

        m = MeshTri()
        e = ElementTriP1()
        basis = InteriorBasis(m, e)

        @Functional
        def feqx(w):
            from skfem.helpers import grad
            f = w['func']  # f(x) = x
            return grad(f)[0]  # f'(x) = 1

        func = basis.interpolate(m.p[0])

        # integrate f'(x) = 1 over [0, 1]^2
        self.assertAlmostEqual(feqx.assemble(basis, func=func), 1.)
Ejemplo n.º 5
0
    def runTest(self):

        m = MeshTri()
        e = ElementTriP1()
        basis = InteriorBasis(m, e)

        @Functional
        def feqx(w):
            from skfem.helpers import grad
            f = w['func']  # f(x, y) = x
            g = w['gunc']  # g(x, y) = y
            return grad(f)[0] + grad(g)[1]

        func = basis.interpolate(m.p[0])
        gunc = basis.interpolate(m.p[1])

        self.assertAlmostEqual(feqx.assemble(basis, func=func, gunc=gunc), 2.)
Ejemplo n.º 6
0
    def runTest(self):
        m = self.case[0]()
        m.refine(self.prerefs)

        hs = []
        L2s = []

        for itr in range(3):
            e = self.case[1]()
            ib = InteriorBasis(m, e)

            @BilinearForm
            def bilinf(u, v, w):
                return ddot(dd(u), dd(v))

            @LinearForm
            def linf(v, w):
                return 1. * v

            K = asm(bilinf, ib)
            f = asm(linf, ib)

            x = solve(*condense(K, f, D=ib.get_dofs().all()))

            X = ib.interpolate(x)

            def exact(x):
                return (x ** 2 - 2. * x ** 3 + x ** 4) / 24.

            @Functional
            def error(w):
                return (w.w - exact(w.x)) ** 2

            L2 = np.sqrt(error.assemble(ib, w=X))

            L2s.append(L2)
            hs.append(m.param())
            m.refine()

        hs = np.array(hs)
        L2s = np.array(L2s)
        pfit = np.polyfit(np.log10(hs), np.log10(L2s), 1)
        self.assertGreater(pfit[0], self.limits[0])
        self.assertLess(pfit[0], self.limits[1])
        self.assertLess(L2s[-1], self.abs_limit)
Ejemplo n.º 7
0
    def runTest(self):
        m = self.case[0]().refined(self.prerefs)

        hs = []
        L2s = []

        for itr in range(3):
            e = self.case[1]()
            ib = InteriorBasis(m, e)

            t = 1.
            E = 1.
            nu = 0.3
            D = E * t ** 3 / (12. * (1. - nu ** 2))

            @BilinearForm
            def bilinf(u, v, w):

                def C(T):
                    trT = T[0, 0] + T[1, 1]
                    return E / (1. + nu) * \
                        np.array([[T[0, 0] + nu / (1. - nu) * trT, T[0, 1]],
                                  [T[1, 0], T[1, 1] + nu / (1. - nu) * trT]])

                return t ** 3 / 12.0 * ddot(C(dd(u)), dd(v))

            def load(x):
                return np.sin(np.pi * x[0]) * np.sin(np.pi * x[1])

            @LinearForm
            def linf(v, w):
                return load(w.x) * v

            K = asm(bilinf, ib)
            f = asm(linf, ib)

            # TODO fix boundary conditions
            # u_x should be zero on top/bottom
            # u_y should be zero on left/right
            x = solve(*condense(K, f, D=ib.get_dofs().all('u')))

            X = ib.interpolate(x)

            def exact(x):
                return 1. / (4. * D * np.pi ** 4) * load(x)

            @Functional
            def error(w):
                return (w.w - exact(w.x)) ** 2

            L2 = np.sqrt(error.assemble(ib, w=X))

            L2s.append(L2)
            hs.append(m.param())
            m = m.refined()

        hs = np.array(hs)
        L2s = np.array(L2s)
        pfit = np.polyfit(np.log10(hs), np.log10(L2s), 1)
        self.assertGreater(pfit[0], self.limits[0])
        self.assertLess(pfit[0], self.limits[1])
        self.assertLess(L2s[-1], self.abs_limit)