Exemplo n.º 1
0
def test_flattened_against_tpe_quad():
    T = UFCInterval()
    P1 = Lagrange(T, 1)
    tpe_quad = TensorProductElement(P1, P1)
    flattened_quad = FlattenedDimensions(tpe_quad)
    assert tpe_quad.value_shape() == ()
    tpe_tab = tpe_quad.tabulate(1, [(0.1, 0.2)])
    flattened_tab = flattened_quad.tabulate(1, [(0.1, 0.2)])

    for da, db in [[(0,), (0,)], [(1,), (0,)], [(0,), (1,)]]:
        dc = da + db
        assert np.isclose(tpe_tab[dc][0][0], flattened_tab[dc][0][0])
        assert np.isclose(tpe_tab[dc][1][0], flattened_tab[dc][1][0])
        assert np.isclose(tpe_tab[dc][2][0], flattened_tab[dc][2][0])
        assert np.isclose(tpe_tab[dc][3][0], flattened_tab[dc][3][0])
Exemplo n.º 2
0
def test_flattened_against_tpe_quad():
    T = UFCInterval()
    P1 = Lagrange(T, 1)
    tpe_quad = TensorProductElement(P1, P1)
    flattened_quad = FlattenedDimensions(tpe_quad)
    assert tpe_quad.value_shape() == ()
    tpe_tab = tpe_quad.tabulate(1, [(0.1, 0.2)])
    flattened_tab = flattened_quad.tabulate(1, [(0.1, 0.2)])

    for da, db in [[(0, ), (0, )], [(1, ), (0, )], [(0, ), (1, )]]:
        dc = da + db
        assert np.isclose(tpe_tab[dc][0][0], flattened_tab[dc][0][0])
        assert np.isclose(tpe_tab[dc][1][0], flattened_tab[dc][1][0])
        assert np.isclose(tpe_tab[dc][2][0], flattened_tab[dc][2][0])
        assert np.isclose(tpe_tab[dc][3][0], flattened_tab[dc][3][0])
Exemplo n.º 3
0
def test_flattened_against_tpe_hex():
    T = UFCInterval()
    P1 = Lagrange(T, 1)
    tpe_quad = TensorProductElement(P1, P1)
    tpe_hex = TensorProductElement(tpe_quad, P1)
    flattened_quad = FlattenedDimensions(tpe_quad)
    flattened_hex = FlattenedDimensions(TensorProductElement(flattened_quad, P1))
    assert tpe_quad.value_shape() == ()
    tpe_tab = tpe_hex.tabulate(1, [(0.1, 0.2, 0.3)])
    flattened_tab = flattened_hex.tabulate(1, [(0.1, 0.2, 0.3)])

    for da, db, dc in [[(0,), (0,), (0,)], [(1,), (0,), (0,)], [(0,), (1,), (0,)], [(0,), (0,), (1,)]]:
        dd = da + db + dc
        assert np.isclose(tpe_tab[dd][0][0], flattened_tab[dd][0][0])
        assert np.isclose(tpe_tab[dd][1][0], flattened_tab[dd][1][0])
        assert np.isclose(tpe_tab[dd][2][0], flattened_tab[dd][2][0])
        assert np.isclose(tpe_tab[dd][3][0], flattened_tab[dd][3][0])
        assert np.isclose(tpe_tab[dd][4][0], flattened_tab[dd][4][0])
        assert np.isclose(tpe_tab[dd][5][0], flattened_tab[dd][5][0])
        assert np.isclose(tpe_tab[dd][6][0], flattened_tab[dd][6][0])
        assert np.isclose(tpe_tab[dd][7][0], flattened_tab[dd][7][0])
Exemplo n.º 4
0
def convert_finiteelement(element, vector_is_mixed):
    if element.family() == "Real":
        # Real element is just DG0
        cell = element.cell()
        return create_element(ufl.FiniteElement("DG", cell, 0),
                              vector_is_mixed)
    cell = as_fiat_cell(element.cell())
    if element.family() == "Quadrature":
        degree = element.degree()
        scheme = element.quadrature_scheme()
        if degree is None or scheme is None:
            raise ValueError("Quadrature scheme and degree must be specified!")

        quad_rule = FIAT.create_quadrature(cell, degree, scheme)
        return FIAT.QuadratureElement(cell, quad_rule.get_points())
    lmbda = supported_elements[element.family()]
    if lmbda is None:
        if element.cell().cellname() == "quadrilateral":
            # Handle quadrilateral short names like RTCF and RTCE.
            element = element.reconstruct(cell=quadrilateral_tpc)
        elif element.cell().cellname() == "hexahedron":
            # Handle hexahedron short names like NCF and NCE.
            element = element.reconstruct(cell=hexahedron_tpc)
        else:
            raise ValueError("%s is supported, but handled incorrectly" %
                             element.family())
        return FlattenedDimensions(create_element(element, vector_is_mixed))

    kind = element.variant()
    if kind is None:
        kind = 'spectral' if element.cell().cellname(
        ) == 'interval' else 'equispaced'  # default variant

    if element.family() == "Lagrange":
        if kind == 'equispaced':
            lmbda = FIAT.Lagrange
        elif kind == 'spectral' and element.cell().cellname() == 'interval':
            lmbda = FIAT.GaussLobattoLegendre
        else:
            raise ValueError("Variant %r not supported on %s" %
                             (kind, element.cell()))
    elif element.family() in [
            "Discontinuous Lagrange", "Discontinuous Lagrange L2"
    ]:
        if kind == 'equispaced':
            lmbda = FIAT.DiscontinuousLagrange
        elif kind == 'spectral' and element.cell().cellname() == 'interval':
            lmbda = FIAT.GaussLegendre
        else:
            raise ValueError("Variant %r not supported on %s" %
                             (kind, element.cell()))
    return lmbda(cell, element.degree())
Exemplo n.º 5
0
def test_flattened_against_tpe_hex():
    T = UFCInterval()
    P1 = Lagrange(T, 1)
    tpe_quad = TensorProductElement(P1, P1)
    tpe_hex = TensorProductElement(tpe_quad, P1)
    flattened_quad = FlattenedDimensions(tpe_quad)
    flattened_hex = FlattenedDimensions(
        TensorProductElement(flattened_quad, P1))
    assert tpe_quad.value_shape() == ()
    tpe_tab = tpe_hex.tabulate(1, [(0.1, 0.2, 0.3)])
    flattened_tab = flattened_hex.tabulate(1, [(0.1, 0.2, 0.3)])

    for da, db, dc in [[(0, ), (0, ), (0, )], [(1, ), (0, ), (0, )],
                       [(0, ), (1, ), (0, )], [(0, ), (0, ), (1, )]]:
        dd = da + db + dc
        assert np.isclose(tpe_tab[dd][0][0], flattened_tab[dd][0][0])
        assert np.isclose(tpe_tab[dd][1][0], flattened_tab[dd][1][0])
        assert np.isclose(tpe_tab[dd][2][0], flattened_tab[dd][2][0])
        assert np.isclose(tpe_tab[dd][3][0], flattened_tab[dd][3][0])
        assert np.isclose(tpe_tab[dd][4][0], flattened_tab[dd][4][0])
        assert np.isclose(tpe_tab[dd][5][0], flattened_tab[dd][5][0])
        assert np.isclose(tpe_tab[dd][6][0], flattened_tab[dd][6][0])
        assert np.isclose(tpe_tab[dd][7][0], flattened_tab[dd][7][0])
Exemplo n.º 6
0
def _create_fiat_element(ufl_element):
    """Create FIAT element corresponding to given finite element."""

    # Get element data
    family = ufl_element.family()
    cell = ufl_element.cell()
    cellname = cell.cellname()
    degree = ufl_element.degree()

    # Check that FFC supports this element
    if family not in supported_families:
        raise RuntimeError("This element family (%s) is not supported by FFC." % family)

    # Create FIAT cell
    fiat_cell = reference_cell(cellname)

    # Handle the space of the constant
    if family == "Real":
        element = _create_fiat_element(ufl.FiniteElement("DG", cell, 0))
        element.__class__ = type('SpaceOfReals', (type(element), SpaceOfReals), {})
        return element

    if cellname == "quadrilateral":
        # Handle quadrilateral case by reconstructing the element with
        # cell TensorProductCell (interval x interval)
        quadrilateral_tpc = ufl.TensorProductCell(ufl.Cell("interval"), ufl.Cell("interval"))
        return FlattenedDimensions(
            _create_fiat_element(ufl_element.reconstruct(cell=quadrilateral_tpc)))
    elif cellname == "hexahedron":
        # Handle hexahedron case by reconstructing the element with cell
        # TensorProductCell (quadrilateral x interval). This creates
        # TensorProductElement(TensorProductElement(interval, interval),
        # interval) Therefore dof entities consists of nested tuples,
        # example: ((0, 1), 1)
        hexahedron_tpc = ufl.TensorProductCell(ufl.Cell("quadrilateral"), ufl.Cell("interval"))
        return FlattenedDimensions(
            _create_fiat_element(ufl_element.reconstruct(cell=hexahedron_tpc)))

    # FIXME: AL: Should this really be here?
    # Handle QuadratureElement
    if family == "Quadrature":
        # Compute number of points per axis from the degree of the element
        scheme = ufl_element.quadrature_scheme()
        assert degree is not None
        assert scheme is not None

        # Create quadrature (only interested in points)
        # TODO: KBO: What should we do about quadrature functions that live on ds, dS?
        # Get cell and facet names.
        points, weights = create_quadrature(cellname, degree, scheme)

        # Make element
        element = QuadratureElement(fiat_cell, points)
    else:
        # Check if finite element family is supported by FIAT
        if family not in FIAT.supported_elements:
            raise RuntimeError("Sorry, finite element of type \"%s\" are not supported by FIAT.",
                               family)

        ElementClass = FIAT.supported_elements[family]

        # Create tensor product FIAT finite element
        if isinstance(ufl_element, ufl.TensorProductElement):
            A = create_element(ufl_element.sub_elements()[0])
            B = create_element(ufl_element.sub_elements()[1])
            element = ElementClass(A, B)

        # Create normal FIAT finite element
        else:
            if degree is None:
                element = ElementClass(fiat_cell)
            else:
                element = ElementClass(fiat_cell, degree)

    if element.value_shape() != ufl_element.reference_value_shape():
        # Consistency check between UFL and FIAT elements.
        raise RuntimeError("Something went wrong in the construction of FIAT element from UFL element."
                           + "Shapes are {} and {}.".format(element.value_shape(),
                                                            ufl_element.reference_value_shape()))

    return element