Пример #1
0
    def runTest(self):
        m = MeshQuad()
        m.refine(2)
        e = ElementQuad1()
        basis = InteriorBasis(m, e)

        M, X = basis.refinterp(m.p[0], 3)

        self.assertEqual(M.p.shape[1], len(X))
Пример #2
0
class ConvergenceQ2(ConvergenceQ1):
    rateL2 = 3.0
    rateH1 = 2.0

    def create_basis(self, m):
        e = ElementQuad2()
        return InteriorBasis(m, e)

    def setUp(self):
        self.mesh = MeshQuad()
        self.mesh.refine(2)
Пример #3
0
def test_periodic_failure():

    # these meshes cannot be made periodic due to insufficient number of
    # elements
    with pytest.raises(ValueError):
        mp = MeshLine1DG.periodic(MeshLine(), [0], [1])

    with pytest.raises(ValueError):
        mp = MeshQuad1DG.periodic(MeshQuad(), [0], [1])

    with pytest.raises(ValueError):
        mp = MeshQuad1DG.periodic(MeshQuad().refined(2), [0], [1, 2])
Пример #4
0
    def runTest(self):
        m = MeshQuad()
        m.refine(3)
        e = ElementQuad1()
        basis = InteriorBasis(m, e)

        @functional
        def x_squared(w):
            return w.x[0]**2

        y = asm(x_squared, basis)

        self.assertAlmostEqual(y, 1. / 3.)
        self.assertEqual(len(x_squared.elemental(basis)), m.t.shape[1])
Пример #5
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,
    )
Пример #6
0
    def runRefineTest(self):
        mesh = MeshQuad()
        mesh.define_boundary('left', lambda x: x[0] == 0)
        mesh.refine()
        mesh_tri = mesh.to_meshtri()

        for b in mesh.boundaries:
            np.testing.assert_array_equal(*[m.facets.T[m.boundaries[b]]
                                            for m in [mesh, mesh_tri]])
Пример #7
0
def test_multimesh():

    m1 = MeshTri().refined()
    m2 = MeshQuad().refined().translated((1., 0.))
    m3 = MeshTri().refined().translated((2., 0.))
    M = m1 @ m2 @ m3

    assert len(M) == 3

    E = [ElementTriP1(), ElementQuad1(), ElementTriP1()]
    basis = list(map(Basis, M, E))

    Mm = asm(mass, basis)

    assert Mm.shape[0] == 3 * 7
Пример #8
0
        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)),
        square.assemble(basisdg,
Пример #9
0

class NormalVectorTestTet(NormalVectorTestTri):

    case = (MeshTet(), ElementTetP1())


class NormalVectorTestTetP2(NormalVectorTestTri):

    case = (MeshTet(), ElementTetP2())
    test_integrate_volume = False


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):
Пример #10
0
 def createBasis(self):
     m = MeshQuad().refined(6)
     self.fbasis = FacetBasis(m, self.elem)
     self.boundary_area = 4.0000
Пример #11
0
 def initialize_meshes(self):
     m0 = MeshQuad()
     m = MeshQuad([[0, 1, 1, 0], [0, .9, 1, 1]], m0.t)
     return m
Пример #12
0
 (
     MeshTri().refined(2),
     MeshTri1DG,
     ElementTriP1,
     lambda x: x[0] == 1,
     lambda x: x[0] == 0,
 ),
 (
     MeshTri().refined(3),
     MeshTri1DG,
     ElementTriP1,
     lambda x: x[0] == 1,
     lambda x: x[0] == 0,
 ),
 (
     MeshQuad().refined(2),
     MeshQuad1DG,
     ElementQuad1,
     lambda x: x[0] == 1,
     lambda x: x[0] == 0,
 ),
 (
     MeshQuad().refined(2),
     MeshQuad1DG,
     ElementQuad2,
     lambda x: x[0] == 1,
     lambda x: x[0] == 0,
 ),
 (
     MeshTri().refined(2),
     MeshTri1DG,
Пример #13
0
class ConvergenceQ1(unittest.TestCase):
    rateL2 = 2.0
    rateH1 = 1.0
    eps = 0.1

    def runTest(self):
        @LinearForm
        def load(v, w):
            x = w.x
            if x.shape[0] == 1:
                return (np.sin(np.pi * x[0]) * (np.pi**2) * v)
            elif x.shape[0] == 2:
                return (np.sin(np.pi * x[0]) * np.sin(np.pi * x[1]) *
                        (2.0 * np.pi**2) * v)
            elif x.shape[0] == 3:
                return (np.sin(np.pi * x[0]) * np.sin(np.pi * x[1]) *
                        np.sin(np.pi * x[2]) * (3.0 * np.pi**2) * v)
            else:
                raise Exception("The dimension not supported")

        m = self.mesh
        Nitrs = 3
        L2s = np.zeros(Nitrs)
        H1s = np.zeros(Nitrs)
        hs = np.zeros(Nitrs)

        for itr in range(Nitrs):
            if itr > 0:
                m.refine()
            ib = self.create_basis(m)

            A = asm(laplace, ib)
            b = asm(load, ib)

            D = ib.get_dofs().all()
            x = solve(*condense(A, b, D=D))

            # calculate error
            L2s[itr], H1s[itr] = self.compute_error(m, ib, x)
            hs[itr] = m.param()

        rateL2 = np.polyfit(np.log(hs), np.log(L2s), 1)[0]
        rateH1 = np.polyfit(np.log(hs), np.log(H1s), 1)[0]

        self.assertLess(np.abs(rateL2 - self.rateL2),
                        self.eps,
                        msg='observed L2 rate: {}'.format(rateL2))
        self.assertLess(np.abs(rateH1 - self.rateH1),
                        self.eps,
                        msg='observed H1 rate: {}'.format(rateH1))
        self.assertLess(H1s[-1], 0.3)
        self.assertLess(L2s[-1], 0.008)

    def compute_error(self, m, basis, U):
        uh, duh, *_ = basis.interpolate(U)
        dx = basis.dx
        x = basis.global_coordinates()

        def u(y):
            if y.shape[0] == 1:
                return np.sin(np.pi * y[0])
            elif y.shape[0] == 2:
                return (np.sin(np.pi * y[0]) * np.sin(np.pi * y[1]))
            elif y.shape[0] == 3:
                return (np.sin(np.pi * y[0]) * np.sin(np.pi * y[1]) *
                        np.sin(np.pi * y[2]))
            else:
                raise Exception("The dimension not supported")

        L2 = np.sqrt(np.sum(np.sum((uh - u(x.f))**2 * dx, axis=1)))

        def ux(y):
            if y.shape[0] == 1:
                return np.pi * np.cos(np.pi * y[0])
            elif y.shape[0] == 2:
                return np.pi * (np.cos(np.pi * y[0]) * np.sin(np.pi * y[1]))
            elif y.shape[0] == 3:
                return np.pi * (np.cos(np.pi * y[0]) * np.sin(np.pi * y[1]) *
                                np.sin(np.pi * y[2]))
            else:
                raise Exception("The dimension not supported")

        if m.dim() >= 2:

            def uy(y):
                if y.shape[0] == 2:
                    return np.pi * (np.sin(np.pi * y[0]) *
                                    np.cos(np.pi * y[1]))
                elif y.shape[0] == 3:
                    return np.pi * (np.sin(np.pi * y[0]) * np.cos(np.pi * y[1])
                                    * np.sin(np.pi * y[2]))
                else:
                    raise Exception("The dimension not supported")

        if m.dim() == 3:

            def uz(y):
                return np.pi * (np.sin(np.pi * y[0]) * np.sin(np.pi * y[1]) *
                                np.cos(np.pi * y[2]))

        if m.dim() == 3:
            H1 = np.sqrt(
                np.sum(
                    np.sum(((duh[0] - ux(x.f))**2 + (duh[1] - uy(x.f))**2 +
                            (duh[2] - uz(x.f))**2) * dx,
                           axis=1)))
        elif m.dim() == 2:
            H1 = np.sqrt(
                np.sum(
                    np.sum(
                        ((duh[0] - ux(x.f))**2 + (duh[1] - uy(x.f))**2) * dx,
                        axis=1)))
        else:
            H1 = np.sqrt(np.sum(np.sum(((duh[0] - ux(x.f))**2) * dx, axis=1)))

        return L2, H1

    def create_basis(self, m):
        e = ElementQuad1()
        return InteriorBasis(m, e)

    def setUp(self):
        self.mesh = MeshQuad()
        self.mesh.refine(2)
Пример #14
0
 def setUp(self):
     self.mesh = MeshQuad().refined(2)
Пример #15
0
                         .dot(m2.p[0] * 0. + 1.)), lenright)

    assert_almost_equal((mass.assemble(mb[1], mb[0])
                         .dot(m2.p[0] * 0. + 1.)
                         .dot(m1.p[0] * 0. + 1.)), lenright)

    assert_allclose(mass.assemble(mb[0], mb[1]).toarray(),
                    mass.assemble(mb[1], mb[0]).T.toarray())


@pytest.mark.parametrize(
    "m, e, fun",
    [
        (MeshTri(), ElementTriP1(), lambda x: x[0]),
        (MeshTri(), ElementTriRT0(), lambda x: x),
        (MeshQuad(), ElementQuadRT0(), lambda x: x),
    ]
)
def test_basis_project(m, e, fun):

    basis = CellBasis(m, e)
    y = basis.project(fun)

    @Functional
    def int_y_1(w):
        return w.y ** 2

    @Functional
    def int_y_2(w):
        return fun(w.x) ** 2
Пример #16
0

class NormalVectorTestTet(NormalVectorTestTri):

    case = (MeshTet(), ElementTetP1())


class NormalVectorTestTetP2(NormalVectorTestTri):

    case = (MeshTet(), ElementTetP2())
    test_integrate_volume = False


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):
Пример #17
0
 def setUp(self):
     self.mesh = MeshQuad()
     self.mesh.refine(2)
Пример #18
0
            self.assertEqual(finder(np.array([0.001]))[0], 0)
            self.assertEqual(finder(np.array([0.999]))[0], 2**itr - 1)


class TestFinder1DLinspaced(TestCase):
    def runTest(self):

        for itr in range(5):
            finder = (MeshLine(np.linspace(0, 1, 2**itr + 1)).element_finder())
            self.assertEqual(finder(np.array([0.999]))[0], 2**itr - 1)
            self.assertEqual(finder(np.array([0.001]))[0], 0)


@pytest.mark.parametrize("m", [
    MeshTri.init_circle(),
    MeshQuad.init_tensor([0, 1, 3], [0, 1, 3]),
    MeshTet().refined(3),
    MeshHex.init_tensor([0, 1, 3], [0, 1, 3], [0, 1, 3]),
])
def test_smoothed(m):
    M = m.smoothed()
    assert M.is_valid()
    # points have moved?
    assert np.linalg.norm((M.p - m.p)**2) > 0


@pytest.mark.parametrize("m", [
    MeshTri(),
    MeshTet(),
])
def test_oriented(m):
Пример #19
0
    # integral is over the domain of the first argument
    assert_almost_equal((mass.assemble(
        mb[0], mb[1]).dot(m1.p[0] * 0. + 1.).dot(m2.p[0] * 0. + 1.)), lenright)

    assert_almost_equal((mass.assemble(
        mb[1], mb[0]).dot(m2.p[0] * 0. + 1.).dot(m1.p[0] * 0. + 1.)), lenright)

    assert_allclose(
        mass.assemble(mb[0], mb[1]).toarray(),
        mass.assemble(mb[1], mb[0]).T.toarray())


@pytest.mark.parametrize("m, e, fun", [
    (MeshTri(), ElementTriP1(), lambda x: x[0]),
    (MeshTri(), ElementTriRT0(), lambda x: x),
    (MeshQuad(), ElementQuadRT0(), lambda x: x),
])
def test_basis_project(m, e, fun):

    basis = CellBasis(m, e)
    y = basis.project(fun)

    @Functional
    def int_y_1(w):
        return w.y**2

    @Functional
    def int_y_2(w):
        return fun(w.x)**2

    assert_almost_equal(int_y_1.assemble(basis, y=y), int_y_2.assemble(basis))