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

        m = MeshTri()

        fbasis1 = FacetBasis(m, ElementTriP1() * ElementTriP1(),
                             facets=m.facets_satisfying(lambda x: x[0] == 0))
        fbasis2 = FacetBasis(m, ElementTriP1(),
                             facets=lambda x: x[0] == 0)
        fbasis3 = FacetBasis(m, ElementTriP1(), facets='left')

        @BilinearForm
        def uv1(u, p, v, q, w):
            return u * v + p * q

        @BilinearForm
        def uv2(u, v, w):
            return u * v

        A = asm(uv1, fbasis1)
        B = asm(uv2, fbasis2)
        C = asm(uv2, fbasis2)

        assert_allclose(A[0].todense()[0, ::2],
                        B[0].todense()[0])

        assert_allclose(A[0].todense()[0, ::2],
                        C[0].todense()[0])
Esempio n. 5
0
 def runTest(self):
     m = self.mesh().refined()
     e = self.elem()
     fb = FacetBasis(m, e)
     x = fb.global_coordinates().value
     eps = 1e-6
     for itr in range(m.p.shape[0]):
         case = (x[itr] < eps) * (x[itr] > -eps)
         for jtr in range(m.p.shape[0]):
             normals = fb.normals.value[jtr][case]
             if itr == jtr:
                 self.assertTrue((normals == -1).all())
             else:
                 self.assertTrue((np.abs(normals) < 1e-14).all())
Esempio n. 6
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)
Esempio n. 7
0
def test_oriented_interface_integral2(m, e, facets, normal):

    fb = FacetBasis(m, e, facets=m.facets_satisfying(facets, normal=normal))
    assert_almost_equal(
        Functional(lambda w: dot(w.fun.grad, w.n)).assemble(fb, fun=m.p[0]),
        1.0,
    )
Esempio n. 8
0
def test_oriented_interface_integral(m, e, facets, fun):

    fb = FacetBasis(m, e, facets=facets)
    assert_almost_equal(
        Functional(lambda w: dot(w.fun.grad, w.n)).assemble(fb, fun=fun(m)),
        1.0,
    )
Esempio n. 9
0
def test_subdomain_facet_assembly_2():

    m = MeshTri().refined(4).with_subdomains({'all': lambda x: x[0] * 0 + 1})
    e = ElementTriP1()

    @Functional
    def boundary_integral(w):
        return dot(w.n, grad(w.u))

    sfbasis = FacetBasis(m, e, facets=m.facets_around('all'))
    fbasis = FacetBasis(m, e)

    assert_almost_equal(
        boundary_integral.assemble(sfbasis, u=m.p[0] * m.p[1]),
        boundary_integral.assemble(fbasis, u=m.p[0] * m.p[1]),
    )
Esempio n. 10
0
    def runTest(self):
        @Functional
        def hydrostatic_pressure(w):
            return w.n * w.x[1]

        np.testing.assert_allclose(
            hydrostatic_pressure.assemble(FacetBasis(MeshTri(),
                                                     ElementTriP1())), [0, 1])
Esempio n. 11
0
    def runTest(self):
        m = self.initialize_meshes()
        e = self.element()
        fb = FacetBasis(m, e)

        x = fb.mapping.G(fb.X, find=fb.find)
        Y0 = fb.mapping.invF(x, tind=fb.mesh.f2t[0, fb.find])

        assert self.within_refelem(Y0)
Esempio n. 12
0
def test_oriented_gauss_integral(m, e):

    facets = m.facets_around('mid')
    fb = FacetBasis(m, e, facets=facets)
    cb = CellBasis(m, e, elements='mid')
    assert_almost_equal(
        Functional(lambda w: w.x[0] * w.n[0]).assemble(fb),
        Functional(lambda w: 1. + 0. * w.x[0]).assemble(cb),
        decimal=5,
    )
Esempio n. 13
0
    def runTest(self):
        mtype, etype = self.case
        m = mtype().refined(3)
        bnd = m.facets_satisfying(lambda x: x[0] == 1.0)
        fb = FacetBasis(m, etype(), facets=bnd)

        @BilinearForm
        def uv(u, v, w):
            x, y, z = w.x
            return x**2 * y**2 * z**2 * u * v

        B = asm(uv, fb)
        ones = np.ones(B.shape[0])

        self.assertAlmostEqual(ones @ (B @ ones), 0.11111111, places=5)
Esempio n. 14
0
    def runTest(self):
        m = MeshLine(np.linspace(0., 1.)).refined(2)
        ib = InteriorBasis(m, self.e)
        fb = FacetBasis(m, self.e)

        @LinearForm
        def boundary_flux(v, w):
            return v * (w.x[0] == 1.)

        L = asm(laplace, ib)
        b = asm(boundary_flux, fb)
        D = m.nodes_satisfying(lambda x: x == 0.0)
        I = ib.complement_dofs(D)  # noqa E741
        u = solve(*condense(L, b, I=I))  # noqa E741

        np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], m.p[0], -10)
Esempio n. 15
0
    def runTest(self):
        m = MeshLine(np.linspace(0., 1.)).refined(2)
        ib = InteriorBasis(m, self.e)
        fb = FacetBasis(m, self.e)

        @LinearForm
        def boundary_flux(v, w):
            return v * (w.x[0] == 1) - v * (w.x[0] == 0)

        L = asm(laplace, ib)
        M = asm(mass, ib)
        b = asm(boundary_flux, fb)
        u = solve(L + 1e-6 * M, b)

        np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], m.p[0] - .5,
                                             -4)
Esempio n. 16
0
    def runTest(self):
        m = MeshLine(np.linspace(0., 1.))
        m.refine(2)
        e = ElementLineP1()
        ib = InteriorBasis(m, e)
        fb = FacetBasis(m, e)

        @linear_form
        def boundary_flux(v, dv, w):
            return v * (w.x[0] == 1) - v * (w.x[0] == 0)

        L = asm(laplace, ib)
        M = asm(mass, ib)
        b = asm(boundary_flux, fb)
        u = solve(L + 1e-6 * M, b)

        self.assertTrue(np.sum(np.abs(u - m.p[0, :] + 0.5)) < 1e-4)
Esempio n. 17
0
    def runTest(self):
        cases = [(MeshHex, ElementHex1), (MeshTet, ElementTetP1),
                 (MeshTet, ElementTetP0)]

        for (mtype, etype) in cases:
            m = mtype().refined(3)
            fb = FacetBasis(m, etype())

            @BilinearForm
            def uv(u, v, w):
                x, y, z = w.x
                return x**2 * y**2 * z**2 * u * v

            B = asm(uv, fb)

            ones = np.ones(B.shape[0])

            self.assertAlmostEqual(ones @ (B @ ones), 0.3333333333, places=5)
Esempio n. 18
0
    def runTest(self):
        m = MeshLine(np.linspace(0., 1.)).refined(2)
        ib = InteriorBasis(m, self.e)
        m.define_boundary('left', lambda x: x[0] == 0.0)
        m.define_boundary('right', lambda x: x[0] == 1.0)
        fb = FacetBasis(m, self.e, facets=m.boundaries['right'])

        @LinearForm
        def boundary_flux(v, w):
            return -w.x[0] * v

        L = asm(laplace, ib)
        b = asm(boundary_flux, fb)
        D = ib.find_dofs()['left'].all()
        I = ib.complement_dofs(D)  # noqa E741
        u = solve(*condense(L, b, I=I))  # noqa E741

        np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], -m.p[0], -10)
Esempio n. 19
0
    def runTest(self):
        m = MeshLine(np.linspace(0., 1.))
        m.refine(2)
        e = ElementLineP1()
        ib = InteriorBasis(m, e)
        fb = FacetBasis(m, e)

        @linear_form
        def boundary_flux(v, dv, w):
            return -v * (w.x[0] == 1.)

        L = asm(laplace, ib)
        b = asm(boundary_flux, fb)
        D = m.nodes_satisfying(lambda x: x == 0.0)
        I = ib.complement_dofs(D)  # noqa E741
        u = solve(*condense(L, b, I=I))  # noqa E741

        self.assertTrue(np.sum(np.abs(u + m.p[0, :])) < 1e-10)
Esempio n. 20
0
global_basis = Basis(mesh, element)
inner_conductor_basis = Basis(mesh,
                              element,
                              elements=mesh.subdomains['inner_conductor'])
outer_conductor_basis = Basis(mesh,
                              element,
                              elements=mesh.subdomains['outer_conductor'])
inner_insulator_basis = Basis(mesh,
                              element,
                              elements=mesh.subdomains['inner_insulator'])
outer_insulator_basis = Basis(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))
Esempio n. 21
0
# relative permittivity of polytetrafluoroethylene
eps_ptfe = 2.1
# relative permittivity of fluorinated ethylene propylene
eps_fep = 2.1

global_basis = InteriorBasis(mesh, element)
inner_conductor_basis = InteriorBasis(
    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))
Esempio n. 22
0
 def createBasis(self):
     m = MeshHex().refined(3)
     self.fbasis = FacetBasis(m, ElementHex2())
     self.boundary_area = 6.000
Esempio n. 23
0
 def createBasis(self):
     m = MeshQuad().refined(6)
     self.fbasis = FacetBasis(m, self.elem)
     self.boundary_area = 4.0000