Esempio n. 1
0
def test_pickling():
    # some simple checks for pickle
    mesh = MeshQuad()
    elem = ElementQuad1()
    mapping = MappingIsoparametric(mesh, elem)
    basis = CellBasis(mesh, elem, mapping)

    pickled_mesh = pickle.dumps(mesh)
    pickled_elem = pickle.dumps(elem)
    pickled_mapping = pickle.dumps(mapping)
    pickled_basis = pickle.dumps(basis)

    mesh1 = pickle.loads(pickled_mesh)
    elem1 = pickle.loads(pickled_elem)
    mapping1 = pickle.loads(pickled_mapping)
    basis1 = pickle.loads(pickled_basis)

    assert_almost_equal(
        laplace.assemble(basis).toarray(),
        laplace.assemble(basis1).toarray(),
    )
    assert_almost_equal(
        mesh.doflocs,
        mesh1.doflocs,
    )
    assert_almost_equal(
        mapping.J(0, 0, np.array([[.3], [.3]])),
        mapping1.J(0, 0, np.array([[.3], [.3]])),
    )
    assert_almost_equal(
        elem.doflocs,
        elem1.doflocs,
    )
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_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)
    A = laplace.assemble(basis)
    M = mass.assemble(basis)

    @LinearForm
    def linf(v, w):
        return np.sin(2. * np.pi * w.x[0]) * v

    f = linf.assemble(basis)
    x = solve(A + 1e-6 * M, f)

    def uexact(x):
        return (1. / (2. * np.pi)**2) * np.sin(2. * np.pi * x[0])

    @Functional
    def func(w):
        uexact = (1. / (2. * np.pi)**2) * np.sin(2. * np.pi * w.x[0])
        return (uexact - w['u'])**2

    assert_almost_equal(func.assemble(basis, u=x), 0, decimal=5)
Esempio n. 4
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. 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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
0
def test_periodic_mesh_assembly(m, mdgtype, etype, check1, check2):
    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(check1)),
                          _sort(m.nodes_satisfying(check2)))
    basis = Basis(mp, etype())
    A = laplace.assemble(basis)

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

    if m.dim() == 2:
        assert_almost_equal(x.max(), 0.125)
    else:
        assert_almost_equal(x.max(), 0.07389930610869411, decimal=3)
    assert not np.isnan(x).any()
Esempio n. 9
0
def laplace(m, **params):
    """Solve the Laplace equation using the FEM.

    Parameters
    ----------
    m
        A Mesh object.

    """
    e = ElementTriP1()
    basis = CellBasis(m, e)
    A = laplacian.assemble(basis)
    b = unit_load.assemble(basis)
    u = solve(*condense(A, b, I=m.interior_nodes()))

    # evaluate the error estimators
    fbasis = [InteriorFacetBasis(m, e, side=i) for i in [0, 1]]
    w = {"u" + str(i + 1): fbasis[i].interpolate(u) for i in [0, 1]}

    @Functional
    def interior_residual(w):
        h = w.h
        return h**2

    eta_K = interior_residual.elemental(basis)

    @Functional
    def edge_jump(w):
        h = w.h
        n = w.n
        dw1 = grad(w["u1"])
        dw2 = grad(w["u2"])
        return h * ((dw1[0] - dw2[0]) * n[0] + (dw1[1] - dw2[1]) * n[1])**2

    eta_E = edge_jump.elemental(fbasis[0], **w)

    tmp = np.zeros(m.facets.shape[1])
    np.add.at(tmp, fbasis[0].find, eta_E)
    eta_E = np.sum(0.5 * tmp[m.t2f], axis=0)

    return eta_E + eta_K
Esempio n. 10
0
def assembler(m):
    basis = Basis(m, ElementTetP1())
    return (
        laplace.assemble(basis),
        unit_load.assemble(basis),
    )