def _setupElement(self): """ Setup the finite element for reference cell. """ from FIAT.lagrange import Lagrange from FIAT.reference_element import default_simplex return Lagrange(default_simplex(1), self.degree)
def test_TFE_2Dx1D_vector_triangle_hcurl(): S = UFCTriangle() T = UFCInterval() Ned1 = Nedelec(S, 1) P1 = Lagrange(T, 1) elt = Hcurl(TensorProductElement(Ned1, P1)) assert elt.value_shape() == (3, ) tab = elt.tabulate(1, [(0.1, 0.2, 0.3)]) tabA = Ned1.tabulate(1, [(0.1, 0.2)]) tabB = P1.tabulate(1, [(0.3, )]) for da, db in [[(0, 0), (0, )], [(1, 0), (0, )], [(0, 1), (0, )], [(0, 0), (1, )]]: dc = da + db assert np.isclose(tab[dc][0][0][0], tabA[da][0][0][0] * tabB[db][0][0]) assert np.isclose(tab[dc][1][0][0], tabA[da][0][0][0] * tabB[db][1][0]) assert np.isclose(tab[dc][2][0][0], tabA[da][1][0][0] * tabB[db][0][0]) assert np.isclose(tab[dc][3][0][0], tabA[da][1][0][0] * tabB[db][1][0]) assert np.isclose(tab[dc][4][0][0], tabA[da][2][0][0] * tabB[db][0][0]) assert np.isclose(tab[dc][5][0][0], tabA[da][2][0][0] * tabB[db][1][0]) assert np.isclose(tab[dc][0][1][0], tabA[da][0][1][0] * tabB[db][0][0]) assert np.isclose(tab[dc][1][1][0], tabA[da][0][1][0] * tabB[db][1][0]) assert np.isclose(tab[dc][2][1][0], tabA[da][1][1][0] * tabB[db][0][0]) assert np.isclose(tab[dc][3][1][0], tabA[da][1][1][0] * tabB[db][1][0]) assert np.isclose(tab[dc][4][1][0], tabA[da][2][1][0] * tabB[db][0][0]) assert np.isclose(tab[dc][5][1][0], tabA[da][2][1][0] * tabB[db][1][0]) assert tab[dc][0][2][0] == 0.0 assert tab[dc][1][2][0] == 0.0 assert tab[dc][2][2][0] == 0.0 assert tab[dc][3][2][0] == 0.0 assert tab[dc][4][2][0] == 0.0 assert tab[dc][5][2][0] == 0.0
def test_TFE_2Dx1D_scalar_quad(): T = UFCInterval() P1 = Lagrange(T, 1) P1_DG = DiscontinuousLagrange(T, 1) elt = TensorProductElement(TensorProductElement(P1, P1_DG), P1) assert elt.value_shape() == () tab = elt.tabulate(1, [(0.1, 0.2, 0.3)]) tA = P1.tabulate(1, [(0.1, )]) tB = P1_DG.tabulate(1, [(0.2, )]) tC = P1.tabulate(1, [(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(tab[dd][0][0], tA[da][0][0] * tB[db][0][0] * tC[dc][0][0]) assert np.isclose(tab[dd][1][0], tA[da][0][0] * tB[db][0][0] * tC[dc][1][0]) assert np.isclose(tab[dd][2][0], tA[da][0][0] * tB[db][1][0] * tC[dc][0][0]) assert np.isclose(tab[dd][3][0], tA[da][0][0] * tB[db][1][0] * tC[dc][1][0]) assert np.isclose(tab[dd][4][0], tA[da][1][0] * tB[db][0][0] * tC[dc][0][0]) assert np.isclose(tab[dd][5][0], tA[da][1][0] * tB[db][0][0] * tC[dc][1][0]) assert np.isclose(tab[dd][6][0], tA[da][1][0] * tB[db][1][0] * tC[dc][0][0]) assert np.isclose(tab[dd][7][0], tA[da][1][0] * tB[db][1][0] * tC[dc][1][0])
def __new__(cls, ref_el, degree): dim = ref_el.get_spatial_dimension() if dim == 1: return Lagrange(ref_el, degree) elif dim == 0: raise IndexError("reference element cannot be dimension 0") else: self = super().__new__(cls) return self
def __init__(self, ref_el, m): from FIAT.lagrange import Lagrange vertices = Lagrange(ref_el, m).dual.get_nodes() pts = [v.get_point_dict().keys()[0] for v in vertices] npts = len(pts) wts = (ref_el.volume() / npts, ) * npts QuadratureRule.__init__(self, ref_el, pts, wts) return
def test_TFE_1Dx1D_vector(): T = UFCInterval() P1_DG = DiscontinuousLagrange(T, 1) P2 = Lagrange(T, 2) elt = TensorProductElement(P1_DG, P2) hdiv_elt = Hdiv(elt) hcurl_elt = Hcurl(elt) assert hdiv_elt.value_shape() == (2, ) assert hcurl_elt.value_shape() == (2, ) tabA = P1_DG.tabulate(1, [(0.1, )]) tabB = P2.tabulate(1, [(0.2, )]) hdiv_tab = hdiv_elt.tabulate(1, [(0.1, 0.2)]) for da, db in [[(0, ), (0, )], [(1, ), (0, )], [(0, ), (1, )]]: dc = da + db assert hdiv_tab[dc][0][0][0] == 0.0 assert hdiv_tab[dc][1][0][0] == 0.0 assert hdiv_tab[dc][2][0][0] == 0.0 assert hdiv_tab[dc][3][0][0] == 0.0 assert hdiv_tab[dc][4][0][0] == 0.0 assert hdiv_tab[dc][5][0][0] == 0.0 assert np.isclose(hdiv_tab[dc][0][1][0], tabA[da][0][0] * tabB[db][0][0]) assert np.isclose(hdiv_tab[dc][1][1][0], tabA[da][0][0] * tabB[db][1][0]) assert np.isclose(hdiv_tab[dc][2][1][0], tabA[da][0][0] * tabB[db][2][0]) assert np.isclose(hdiv_tab[dc][3][1][0], tabA[da][1][0] * tabB[db][0][0]) assert np.isclose(hdiv_tab[dc][4][1][0], tabA[da][1][0] * tabB[db][1][0]) assert np.isclose(hdiv_tab[dc][5][1][0], tabA[da][1][0] * tabB[db][2][0]) hcurl_tab = hcurl_elt.tabulate(1, [(0.1, 0.2)]) for da, db in [[(0, ), (0, )], [(1, ), (0, )], [(0, ), (1, )]]: dc = da + db assert np.isclose(hcurl_tab[dc][0][0][0], tabA[da][0][0] * tabB[db][0][0]) assert np.isclose(hcurl_tab[dc][1][0][0], tabA[da][0][0] * tabB[db][1][0]) assert np.isclose(hcurl_tab[dc][2][0][0], tabA[da][0][0] * tabB[db][2][0]) assert np.isclose(hcurl_tab[dc][3][0][0], tabA[da][1][0] * tabB[db][0][0]) assert np.isclose(hcurl_tab[dc][4][0][0], tabA[da][1][0] * tabB[db][1][0]) assert np.isclose(hcurl_tab[dc][5][0][0], tabA[da][1][0] * tabB[db][2][0]) assert hcurl_tab[dc][0][1][0] == 0.0 assert hcurl_tab[dc][1][1][0] == 0.0 assert hcurl_tab[dc][2][1][0] == 0.0 assert hcurl_tab[dc][3][1][0] == 0.0 assert hcurl_tab[dc][4][1][0] == 0.0 assert hcurl_tab[dc][5][1][0] == 0.0
def __init__(self, ref_el, degree): element = Lagrange(ref_el, degree) cell_dim = ref_el.get_dimension() assert cell_dim == max(element.entity_dofs().keys()) cell_entity_dofs = element.entity_dofs()[cell_dim][0] if len(cell_entity_dofs) == 0: raise RuntimeError('Bubble element of degree %d has no dofs' % degree) super(Bubble, self).__init__(element, indices=cell_entity_dofs)
def test_TFE_2Dx1D_vector_quad_hdiv(): T = UFCInterval() P1 = Lagrange(T, 1) P0 = DiscontinuousLagrange(T, 0) P1_DG = DiscontinuousLagrange(T, 1) P1P0 = Hdiv(TensorProductElement(P1, P0)) P0P1 = Hdiv(TensorProductElement(P0, P1)) horiz_elt = EnrichedElement(P1P0, P0P1) elt = Hdiv(TensorProductElement(horiz_elt, P1_DG)) assert elt.value_shape() == (3, ) tab = elt.tabulate(1, [(0.1, 0.2, 0.3)]) tA = P1.tabulate(1, [(0.1, )]) tB = P0.tabulate(1, [(0.2, )]) tC = P0.tabulate(1, [(0.1, )]) tD = P1.tabulate(1, [(0.2, )]) tE = P1_DG.tabulate(1, [(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(tab[dd][0][0][0], -tA[da][0][0] * tB[db][0][0] * tE[dc][0][0]) assert np.isclose(tab[dd][1][0][0], -tA[da][0][0] * tB[db][0][0] * tE[dc][1][0]) assert np.isclose(tab[dd][2][0][0], -tA[da][1][0] * tB[db][0][0] * tE[dc][0][0]) assert np.isclose(tab[dd][3][0][0], -tA[da][1][0] * tB[db][0][0] * tE[dc][1][0]) assert tab[dd][4][0][0] == 0.0 assert tab[dd][5][0][0] == 0.0 assert tab[dd][6][0][0] == 0.0 assert tab[dd][7][0][0] == 0.0 assert tab[dd][0][1][0] == 0.0 assert tab[dd][1][1][0] == 0.0 assert tab[dd][2][1][0] == 0.0 assert tab[dd][3][1][0] == 0.0 assert np.isclose(tab[dd][4][1][0], tC[da][0][0] * tD[db][0][0] * tE[dc][0][0]) assert np.isclose(tab[dd][5][1][0], tC[da][0][0] * tD[db][0][0] * tE[dc][1][0]) assert np.isclose(tab[dd][6][1][0], tC[da][0][0] * tD[db][1][0] * tE[dc][0][0]) assert np.isclose(tab[dd][7][1][0], tC[da][0][0] * tD[db][1][0] * tE[dc][1][0]) assert tab[dd][0][2][0] == 0.0 assert tab[dd][1][2][0] == 0.0 assert tab[dd][2][2][0] == 0.0 assert tab[dd][3][2][0] == 0.0 assert tab[dd][4][2][0] == 0.0 assert tab[dd][5][2][0] == 0.0 assert tab[dd][6][2][0] == 0.0 assert tab[dd][7][2][0] == 0.0
def __init__(self, ref_el, degree, codim): element = Lagrange(ref_el, degree) cell_dim = ref_el.get_dimension() assert cell_dim == max(element.entity_dofs().keys()) dofs = list( sorted(chain(*element.entity_dofs()[cell_dim - codim].values()))) if len(dofs) == 0: raise RuntimeError( 'Bubble element of degree %d and codimension %d has no dofs' % (degree, codim)) super(CodimBubble, self).__init__(element, indices=dofs)
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])
def test_TFE_1Dx1D_scalar(): T = UFCInterval() P1_DG = DiscontinuousLagrange(T, 1) P2 = Lagrange(T, 2) elt = TensorProductElement(P1_DG, P2) assert elt.value_shape() == () tab = elt.tabulate(1, [(0.1, 0.2)]) tabA = P1_DG.tabulate(1, [(0.1, )]) tabB = P2.tabulate(1, [(0.2, )]) for da, db in [[(0, ), (0, )], [(1, ), (0, )], [(0, ), (1, )]]: dc = da + db assert np.isclose(tab[dc][0][0], tabA[da][0][0] * tabB[db][0][0]) assert np.isclose(tab[dc][1][0], tabA[da][0][0] * tabB[db][1][0]) assert np.isclose(tab[dc][2][0], tabA[da][0][0] * tabB[db][2][0]) assert np.isclose(tab[dc][3][0], tabA[da][1][0] * tabB[db][0][0]) assert np.isclose(tab[dc][4][0], tabA[da][1][0] * tabB[db][1][0]) assert np.isclose(tab[dc][5][0], tabA[da][1][0] * tabB[db][2][0])
def test_TFE_2Dx1D_scalar_triangle_hdiv(): S = UFCTriangle() T = UFCInterval() P1_DG = DiscontinuousLagrange(S, 1) P2 = Lagrange(T, 2) elt = Hdiv(TensorProductElement(P1_DG, P2)) assert elt.value_shape() == (3, ) tab = elt.tabulate(1, [(0.1, 0.2, 0.3)]) tabA = P1_DG.tabulate(1, [(0.1, 0.2)]) tabB = P2.tabulate(1, [(0.3, )]) for da, db in [[(0, 0), (0, )], [(1, 0), (0, )], [(0, 1), (0, )], [(0, 0), (1, )]]: dc = da + db assert tab[dc][0][0][0] == 0.0 assert tab[dc][1][0][0] == 0.0 assert tab[dc][2][0][0] == 0.0 assert tab[dc][3][0][0] == 0.0 assert tab[dc][4][0][0] == 0.0 assert tab[dc][5][0][0] == 0.0 assert tab[dc][6][0][0] == 0.0 assert tab[dc][7][0][0] == 0.0 assert tab[dc][8][0][0] == 0.0 assert tab[dc][0][1][0] == 0.0 assert tab[dc][1][1][0] == 0.0 assert tab[dc][2][1][0] == 0.0 assert tab[dc][3][1][0] == 0.0 assert tab[dc][4][1][0] == 0.0 assert tab[dc][5][1][0] == 0.0 assert tab[dc][6][1][0] == 0.0 assert tab[dc][7][1][0] == 0.0 assert tab[dc][8][1][0] == 0.0 assert np.isclose(tab[dc][0][2][0], tabA[da][0][0] * tabB[db][0][0]) assert np.isclose(tab[dc][1][2][0], tabA[da][0][0] * tabB[db][1][0]) assert np.isclose(tab[dc][2][2][0], tabA[da][0][0] * tabB[db][2][0]) assert np.isclose(tab[dc][3][2][0], tabA[da][1][0] * tabB[db][0][0]) assert np.isclose(tab[dc][4][2][0], tabA[da][1][0] * tabB[db][1][0]) assert np.isclose(tab[dc][5][2][0], tabA[da][1][0] * tabB[db][2][0]) assert np.isclose(tab[dc][6][2][0], tabA[da][2][0] * tabB[db][0][0]) assert np.isclose(tab[dc][7][2][0], tabA[da][2][0] * tabB[db][1][0]) assert np.isclose(tab[dc][8][2][0], tabA[da][2][0] * tabB[db][2][0])
def test_basis_derivatives_scaling(): "Regression test for issue #9" class Interval(ReferenceElement): def __init__(self, a, b): verts = ((a, ), (b, )) edges = {0: (0, 1)} topology = {0: {0: (0, ), 1: (1, )}, 1: edges} super(Interval, self).__init__(LINE, verts, topology) random.seed(42) for i in range(26): a = 1000.0 * (random.random() - 0.5) b = 1000.0 * (random.random() - 0.5) a, b = min(a, b), max(a, b) interval = Interval(a, b) element = Lagrange(interval, 1) points = [(a, ), (0.5 * (a + b), ), (b, )] tab = element.get_nodal_basis().tabulate(points, 2) # first basis function assert np.isclose(tab[(0, )][0][0], 1.0) assert np.isclose(tab[(0, )][0][1], 0.5) assert np.isclose(tab[(0, )][0][2], 0.0) # second basis function assert np.isclose(tab[(0, )][1][0], 0.0) assert np.isclose(tab[(0, )][1][1], 0.5) assert np.isclose(tab[(0, )][1][2], 1.0) # first and second derivatives D = 1.0 / (b - a) for p in range(len(points)): assert np.isclose(tab[(1, )][0][p], -D) assert np.isclose(tab[(1, )][1][p], +D) assert np.isclose(tab[(2, )][0][p], 0.0) assert np.isclose(tab[(2, )][1][p], 0.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])
import os, sys # Find PETSc/BuildSystem if 'PETSC_DIR' in os.environ: configDir = os.path.join(os.environ['PETSC_DIR'], 'config') bsDir = os.path.join(configDir, 'BuildSystem') sys.path.insert(0, bsDir) sys.path.insert(0, configDir) import PETSc.FEM from FIAT.reference_element import default_simplex from FIAT.lagrange import Lagrange generator = PETSc.FEM.QuadratureGenerator() generator.setup() elements = [] if not (len(sys.argv)-2) % 5 == 0: sys.exit('Incomplete set of arguments') for n in range((len(sys.argv)-2) / 5): dim = int(sys.argv[n*5+1]) order = int(sys.argv[n*5+2]) components = int(sys.argv[n*5+3]) numBlocks = int(sys.argv[n*5+4]) operator = sys.argv[n*5+5] element = Lagrange(default_simplex(dim), order) element.numComponents = components elements.append(element) filename = sys.argv[-1] generator.quadDegree = max([e.order for e in elements]) generator.run(elements, numBlocks, operator, filename)
# Get coeffs of primal and dual bases w.r.t. expansion set coeffs_poly = poly_set.get_coeffs() coeffs_dual = dual_set.to_riesz(poly_set) assert coeffs_poly.shape == coeffs_dual.shape # Check nodality for i in range(coeffs_dual.shape[0]): for j in range(coeffs_poly.shape[0]): assert np.isclose( coeffs_dual[i].flatten().dot(coeffs_poly[j].flatten()), 1.0 if i == j else 0.0) @pytest.mark.parametrize('elements', [ (Lagrange(I, 2), Bubble(I, 2)), (Lagrange(T, 3), Bubble(T, 3)), (Lagrange(S, 4), Bubble(S, 4)), (Lagrange(I, 1), Lagrange(I, 1)), (Lagrange(I, 1), Bubble(I, 2), Bubble(I, 2)), ]) def test_illposed_nodal_enriched(elements): """Check that nodal enriched element fails on ill-posed (non-unisolvent) case """ with pytest.raises(np.linalg.LinAlgError): NodalEnrichedElement(*elements) def test_empty_bubble(): "Check that bubble of too low degree fails"