Example #1
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())
Example #2
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)
Example #3
0
    def runTest(self):
        m = self.init_mesh()
        basis = InteriorBasis(m, self.element_type())

        A = laplace.assemble(basis)
        b = unit_load.assemble(basis)
        x = solve(*condense(A, b, D=basis.get_dofs()))

        self.assertAlmostEqual(np.max(x), self.maxval, places=3)
Example #4
0
    def runTest(self):
        path = Path(__file__).parents[1] / 'docs' / 'examples' / 'meshes'
        m = self.mesh_type.load(path / self.filename)
        basis = InteriorBasis(m, self.element_type())

        A = laplace.assemble(basis)
        b = unit_load.assemble(basis)
        x = solve(*condense(A, b, D=basis.get_dofs()))

        self.assertAlmostEqual(np.max(x), 0.06261690318912218, places=3)
Example #5
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())
Example #6
0
    def runTest(self):

        m = self.mesh().refined(3)

        ib = InteriorBasis(m, self.elem())

        A = asm(laplace, ib)

        D = ib.get_dofs().all()
        I = ib.complement_dofs(D)

        for X in self.funs:
            x = self.set_bc(X, ib)
            Xh = x.copy()
            x = solve(*condense(A, x=x, I=I))
            self.assertLessEqual(np.sum(x - Xh), 1e-10)
Example #7
0
    def runTest(self):

        m = MeshTri()
        e = ElementTriArgyris()
        basis = InteriorBasis(m, e)
        all_dofs = basis.get_dofs()

        self.assertEqual(len(all_dofs.keep('u').nodal), 1)
        self.assertTrue('u' in all_dofs.keep('u').nodal)

        self.assertEqual(len(all_dofs.keep('u_n').facet), 1)
        self.assertEqual(len(all_dofs.drop('u').facet), 1)

        all_dofs = basis.dofs.get_facet_dofs(m.facets_satisfying(lambda x: 1))

        self.assertEqual(len(all_dofs.keep('u_n').facet), 1)
        self.assertEqual(len(all_dofs.drop('u').facet), 1)
Example #8
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)
Example #9
0
    def runTest(self):

        m = MeshTri()
        e = ElementTriArgyris()
        basis = InteriorBasis(m, e)
        all_dofs = basis.get_dofs()

        assert_allclose(
            all_dofs.keep(['u', 'u_n']).keep('u'), all_dofs.keep('u'))

        assert_allclose(
            all_dofs.drop(['u_x', 'u_y', 'u_xx', 'u_xy', 'u_yy', 'u_n']),
            all_dofs.keep('u'))

        assert_allclose(all_dofs, all_dofs.drop('does_not_exist'))

        assert_allclose(np.empty((0, ), dtype=np.int64),
                        all_dofs.keep('does_not_exist'))
Example #10
0
    def runTest(self):
        @bilinear_form
        def dudv(u, du, v, dv, w):
            return sum(du * dv)

        m = self.mesh()
        m.refine(4)

        ib = InteriorBasis(m, self.elem())

        A = asm(dudv, ib)

        D = ib.get_dofs().all()
        I = ib.complement_dofs(D)

        for X in self.funs:
            x = self.set_bc(X, ib)
            Xh = x.copy()
            x = solve(*condense(A, 0 * x, x=x, I=I))
            self.assertLessEqual(np.sum(x - Xh), 1e-10)
Example #11
0
    mesh, element, elements=mesh.subdomains['inner_conductor'])
outer_conductor_basis = InteriorBasis(
    mesh, element, elements=mesh.subdomains['outer_conductor'])
inner_insulator_basis = InteriorBasis(
    mesh, element, elements=mesh.subdomains['inner_insulator'])
outer_insulator_basis = InteriorBasis(
    mesh, element, elements=mesh.subdomains['outer_insulator'])

inner_conductor_outer_surface_basis = FacetBasis(
    mesh, element, facets=mesh.boundaries['inner_conductor_outer_surface'])
outer_conductor_inner_surface_basis = FacetBasis(
    mesh, element, facets=mesh.boundaries['outer_conductor_inner_surface'])

dofs = {
    'boundary':
    global_basis.get_dofs(mesh.boundaries['boundary']),
    'inner_conductor_outer_surface':
    global_basis.get_dofs(mesh.boundaries['inner_conductor_outer_surface']),
    'outer_conductor_inner_surface':
    global_basis.get_dofs(mesh.boundaries['outer_conductor_inner_surface'])
}

# functional to compute the integral of a load vector over the domain
load_integral = solve(asm(mass, global_basis), asm(unit_load, global_basis))

# all materials have a relative permeability of effectively 1
K_mag = asm(laplace, global_basis) * (1 / mu0)

# pass 1A through the conductors
current = 1
Example #12
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)