diff --git a/.github/branches b/.github/branches index 89e627e..f504af3 100644 --- a/.github/branches +++ b/.github/branches @@ -1,3 +1,3 @@ -export FIAT_BRANCH=indiamai/fuse +export FIAT_BRANCH=indiamai/integrate_fuse export FIREDRAKE_BRANCH=indiamai/fuse export BASE_FIREDRAKE_BRANCH=connorjward/pyop3 diff --git a/.github/workflows/setup_repos.sh b/.github/workflows/setup_repos.sh index 6f07963..5d3c932 100644 --- a/.github/workflows/setup_repos.sh +++ b/.github/workflows/setup_repos.sh @@ -21,13 +21,3 @@ git fetch git checkout "$FIAT_BRANCH" git status python3 -m pip install --break-system-packages -e . - -#/usr/bin/git config --global --add safe.directory ~ -#cd ~ -#git clone https://github.com/firedrakeproject/ufl.git -#/usr/bin/git config --global --add safe.directory ~/ufl -#cd ufl -#git fetch -#git checkout indiamai/integrate-fuse -#git status -#python3 -m pip install --break-system-packages -e . diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ff2fe2..811e9a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,6 +34,8 @@ jobs: PYOP2_SPMD_STRICT: 1 EXTRA_PYTEST_ARGS: --splitting-algorithm least_duration --timeout=600 --timeout-method=thread -o faulthandler_timeout=660 --durations-path=./fuse-repo/test/test_durations.json --durations=50 PYTEST_MPI_MAX_NPROCS: 8 + # TODO: reduce this + FIREDRAKE_RUN_SPLIT_TESTS_TIMEOUT: 3600 steps: - name: Fix HOME # For unknown reasons GitHub actions overwrite HOME to /github/home @@ -191,7 +193,7 @@ jobs: run: | . venv/bin/activate python3 -m pip install --break-system-packages -e './fiat-repo' - + - name: Run tests run: | . venv/bin/activate diff --git a/Makefile b/Makefile index 2b4b4a7..10058a4 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,12 @@ tests: @echo " Running all tests" @python3 -m coverage run -p -m pytest -rx test +mini_tests: + @FIREDRAKE_USE_FUSE=1 python3 -m pytest test/test_2d_examples_docs.py + @FIREDRAKE_USE_FUSE=1 python3 -m pytest test/test_convert_to_fiat.py::test_1d + @FIREDRAKE_USE_FUSE=1 python3 -m pytest test/test_orientations.py::test_surface_vec_rt + @FIREDRAKE_USE_FUSE=1 python3 -m pytest test/test_convert_to_fiat.py::test_projection_convergence_3d\[construct_tet_ned-N1curl-1-0.8\] + coverage: @python3 -m coverage combine @python3 -m coverage report -m diff --git a/fuse/__init__.py b/fuse/__init__.py index 823d0eb..db5de93 100644 --- a/fuse/__init__.py +++ b/fuse/__init__.py @@ -1,9 +1,10 @@ -from fuse.cells import Point, Edge, polygon, make_tetrahedron, constructCellComplex +from fuse.cells import Point, Edge, polygon, line, make_tetrahedron, constructCellComplex, TensorProductPoint from fuse.groups import S1, S2, S3, D4, Z3, Z4, tet_C2, C3, C4, S4, A4, tet_edges, tet_faces, sq_edges, GroupRepresentation, PermutationSetRepresentation, Permutation, get_cyc_group, get_sym_group + from fuse.dof import DeltaPairing, DOF, L2Pairing, FuseFunction, PointKernel, VectorKernel, BarycentricPolynomialKernel, PolynomialKernel, ComponentKernel from fuse.triples import ElementTriple, DOFGenerator, immerse from fuse.traces import TrH1, TrGrad, TrHess, TrHCurl, TrHDiv -from fuse.tensor_products import tensor_product +from fuse.tensor_products import tensor_product, symmetric_tensor_product from fuse.vectorisation import VectorTriple from fuse.spaces.pullbacks import Pullback, IdentityPullback, CovariantPiola, ContravariantPiola, Fid, Fcurl, Fdiv diff --git a/fuse/cells.py b/fuse/cells.py index 5f6113a..2079b5a 100644 --- a/fuse/cells.py +++ b/fuse/cells.py @@ -10,11 +10,13 @@ from matplotlib.patches import FancyArrowPatch from mpl_toolkits.mplot3d import proj3d from sympy.combinatorics.named_groups import SymmetricGroup -from fuse.utils import sympy_to_numpy, fold_reduce, numpy_to_str_tuple, orientation_value +from fuse.utils import sympy_to_numpy, fold_reduce, numpy_to_str_tuple, orientation_value, _SYMBOLS, as_tuple from FIAT.reference_element import Simplex, TensorProductCell as FiatTensorProductCell, Hypercube from FIAT.quadrature_schemes import create_quadrature from ufl.cell import Cell, TensorProductCell from functools import cache +from itertools import product +from collections import defaultdict class Arrow3D(FancyArrowPatch): @@ -151,6 +153,13 @@ def compute_scaled_verts(d, n): raise ValueError("Dimension {} not supported".format(d)) +def line(): + """ + Constructs the default 1D interval + """ + return Point(1, [Point(0), Point(0)], vertex_num=2) + + def polygon(n): """ Constructs the 2D default cell with n sides/vertices @@ -265,6 +274,15 @@ def ufc_tetrahedron(): # return Point(3, vertex_num=4, edges=[face1, face4, face3, face4], edge_orientations={3: [2, 1, 0]}) +def is_hypercube(cell): + """True for interval-product entities (quad, hex, ...), i.e. cells with + ``2**dim`` vertices and ``dim >= 2``.""" + if cell.dimension < 2: + return False + nverts = len(cell.vertices()) + return nverts == 2 ** cell.dimension + + class Point(): """ Cell complex representation of a finite element cell @@ -373,6 +391,7 @@ def compute_cell_group(self): """ verts = self.ordered_vertices() v_coords = [self.get_node(v, return_coords=True) for v in verts] + n = len(verts) max_group = SymmetricGroup(n) edges = [edge.ordered_vertices() for edge in self.edges()] @@ -394,6 +413,9 @@ def get_spatial_dimension(self): def dim(self): return self.dimension + def dimensions(self): + return [i for i in range(self.dimension + 1)] + def get_shape(self): num_verts = len(self.vertices()) if num_verts == 1: @@ -507,6 +529,15 @@ def get_starter_ids(self): min_ids = [min(dimension) for dimension in structure] return min_ids + def local_id(self, node): + structure = [sorted(generation) for generation in nx.topological_generations(self.G)] + structure.reverse() + min_id = self.get_starter_ids() + for d in range(len(structure)): + if node.id in structure[d]: + return node.id - min_id[d] + raise ValueError("Node not found in cell") + def graph_dim(self): if self.oriented: dim = self.dimension + 1 @@ -555,6 +586,7 @@ def ordered_vertex_coords(self): def d_entities_ids(self, d): return self.d_entities(d, get_class=False) + @cache def d_entities(self, d, get_class=True): """Get all the d dimensional entities of the cell complex. @@ -653,7 +685,6 @@ def basis_vectors(self, return_coords=True, entity=None, order=False, norm=True) self_levels = [generation for generation in nx.topological_generations(self.G)] vertices = entity.ordered_vertices() if self.dimension == 0: - # return [[] raise ValueError("Dimension 0 entities cannot have Basis Vectors") if self.oriented: # ordered_vertices() handles the orientation so we want to drop the orientation node @@ -833,7 +864,7 @@ def attachment(self, source, dst): chain = cache[(source, dst)] return lambda *x: fold_reduce(chain, *x) - def attachment_J(self, source, dst): + def attachment_J_det(self, source, dst): attachment = self.attachment(source, dst) symbol_names = ["x", "y", "z"] symbols = [] @@ -842,7 +873,7 @@ def attachment_J(self, source, dst): for i in range(self.dim_of_node(dst)): symbols += [sp.Symbol(symbol_names[i])] J = sp.Matrix(attachment(*symbols)).jacobian(sp.Matrix(symbols)) - return J + return np.sqrt(abs(float(sp.det(J.T * J)))) def quadrature(self, degree): fiat_el = self.to_fiat() @@ -850,6 +881,34 @@ def quadrature(self, degree): pts, wts = Q.get_points(), Q.get_weights() return pts, wts + def volume(self): + vertices = np.asarray(self.ordered_vertex_coords()) + if self.get_spatial_dimension() == 0: + return 1 + elif self.get_spatial_dimension() == 1: + return abs(vertices[1] - vertices[0])[0] + elif self.get_spatial_dimension() == 2: + x = vertices[:, 0] + y = vertices[:, 1] + return 0.5 * abs(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1))) + elif self.get_spatial_dimension() == 3: + vertices = np.asarray(vertices) + V = 0.0 + for face in self.d_entities(2): + pts = np.array([self.get_node(v, return_coords=True) for v in face.ordered_vertices()]) + c = pts.mean(axis=0) + + n = np.zeros(3) + for i in range(len(pts)): + v0 = pts[i] + v1 = pts[(i + 1) % len(pts)] + n += np.cross(v0, v1) + V += np.dot(c, n) + V = abs(V) / 3.0 + return V + else: + raise NotImplementedError("Dimension not accounted for") + def cartesian_to_barycentric(self, pts): verts = np.array(self.ordered_vertex_coords()) v_0 = self.ordered_vertex_coords()[0] @@ -916,9 +975,12 @@ def dict_id(self): def _from_dict(o_dict): return Point(o_dict["dim"], o_dict["edges"], oriented=o_dict["oriented"], cell_id=o_dict["id"]) - -def _attachment_symbols(nvals): - return tuple(sp.Symbol(s) for s in ["x", "y", "z"][:nvals]) + def equivalent(self, other): + if self.dimension != other.dimension: + return False + if set(self.ordered_vertex_coords()) != set(other.ordered_vertex_coords()): + return False + return self.get_topology() == other.get_topology() @cache @@ -926,18 +988,16 @@ def _component_evaluator(expr, nvals): """ Compile one scalar attachment component for numeric evaluation. - Returns None if the component cannot be evaluated numerically, in which - case the caller must fall back to the symbolic path. Cells are rebuilt - frequently from the same attachment polynomials, so this is cached on the + Only components fully determined by the given values are compiled; + everything else defers to sympy_to_numpy, which decides how partially + substituted expressions are represented. Cells are rebuilt frequently + from the same attachment polynomials, so this is cached on the expression rather than on the edge holding it. """ - free = len(expr.atoms(sp.Symbol)) - if free == nvals: - fn = sp.lambdify(_attachment_symbols(nvals), expr, "math") + if len(expr.atoms(sp.Symbol)) == nvals: + fn = sp.lambdify(_SYMBOLS[:nvals], expr, "math") return lambda *x: float(fn(*x)) - if free == 0: - return lambda *x: expr - return None + return lambda *x: sympy_to_numpy(expr, _SYMBOLS, x) @cache @@ -947,7 +1007,7 @@ def _matrix_evaluator(expr, nvals): """ if len(expr.atoms(sp.Symbol)) != nvals: return None - fn = sp.lambdify(_attachment_symbols(nvals), expr, "numpy") + fn = sp.lambdify(_SYMBOLS[:nvals], expr, "numpy") def evaluate(*x): res = np.array(fn(*x)).astype(np.float64) @@ -984,8 +1044,9 @@ def _evaluator(self, nvals): if nvals not in cache: if hasattr(self.attachment, '__iter__'): parts = [_component_evaluator(c, nvals) for c in self.attachment] - built = None if any(p is None for p in parts) else \ - (lambda *x: tuple(p(*x) for p in parts)) + + def built(*x): + return tuple(p(*x) for p in parts) else: built = _matrix_evaluator(sp.ImmutableMatrix(self.attachment), nvals) cache[nvals] = built @@ -1009,7 +1070,12 @@ def __call__(self, *x): if hasattr(self.attachment, '__iter__'): res = [] for attach_comp in self.attachment: - res.append(sympy_to_numpy(attach_comp, syms, x)) + if len(attach_comp.atoms(sp.Symbol)) <= len(x): + res.append(sympy_to_numpy(attach_comp, syms, x)) + else: + res_val = attach_comp.subs({syms[i]: x[i] for i in range(len(x))}) + res.append(res_val) + return tuple(res) return sympy_to_numpy(self.attachment, syms, x) return x @@ -1040,50 +1106,274 @@ def _from_dict(o_dict): class TensorProductPoint(): + id_iter = itertools.count() - def __init__(self, A, B, flat=False): - self.A = A - self.B = B + def __init__(self, *factors): + self.id = next(self.id_iter) + self.A = factors[0] + self.B = factors[1] + self.factors = factors self.dimension = self.A.dimension + self.B.dimension - self.flat = flat + self.flat = False + self.fiat_elem = None + self.group = self.compute_cell_group() + self.entities = {} + + for d in self.dimensions()[:-1]: + self.entities[d] = [TensorProductPoint(*entities) for entities in product(*(f.d_entities(degree, True) for f, degree in zip(factors, d)))] + self.entities[self.dim()] = [self] def ordered_vertices(self): - return self.A.ordered_vertices() + self.B.ordered_vertices() + return self.entities[0] + + def ordered_vertex_coords(self): + return [sum(verts, ()) for verts in product(*(f.vertices(return_coords=True) for f in self.factors))] + + def component_orientations(self): + from fuse.utils import canonical_tensor_orientation_key + from fuse.groups import signed_axis_permutation + self.component_os_to_os = {} + for dim in self.to_fiat().get_topology(): + self.component_os_to_os[dim] = {} + ents = [f.d_entities(d)[0] for f, d in zip(self.factors, dim)] + active = [i for i, d in enumerate(dim) if d > 0] + ed = sum(dim) + group = list(product(*(e.group.members() for e in ents))) + for gs in group: + # Each active factor may itself be a multi-dimensional entity + # (e.g. a flattened quad face used as a tensor factor), so its + # member is decomposed into its own (axis_perm, flips) block + # rather than assumed to contribute a single reflection bit. + axis_perm = [0] * ed + flips = [0] * ed + offset = 0 + for i in active: + d_local = dim[i] + local_perm, local_flips = signed_axis_permutation(gs[i], d_local) + for j in range(d_local): + axis_perm[offset + j] = offset + local_perm[j] + flips[offset + j] = local_flips[j] + offset += d_local + o_val = canonical_tensor_orientation_key(tuple(axis_perm), tuple(flips), ed) + self.component_os_to_os[dim][tuple(g.numeric_rep() for g in gs)] = o_val + return self.component_os_to_os + + def compute_cell_group(self): + """ + Systematically work out the symmetry group of the tensor product cell. + """ + verts = self.vertices() + group = list(product(*(f.group.members() for f in self.factors))) + # group = [(g_a, g_b) for g_a in self.A.group.members() for g_b in self.B.group.members()] + perms = [] + for gs in group: + new_verts = list(product(*(g.permute(f.vertices()) for g, f in zip(gs, self.factors)))) + # new_verts = [(v_a, v_b) for v_a in g_a.permute(self.A.vertices()) for v_b in g_b.permute(self.B.vertices())] + perm = [verts.index(v) for v in new_verts] + perms += [fuse_groups.Permutation(perm)] + + grp = fuse_groups.PermutationSetRepresentation(perms).add_cell(self) + return grp + + def get_starter_ids(self): + # this doesn't actually make sense - remove when confirmed all changes to eliminate min ids from triple is done + raise NotImplementedError + a_starts = self.A.get_starter_ids() + b_starts = self.B.get_starter_ids() + ids = [] + for a, b in zip(a_starts, b_starts): + ids += [max(a, b)] + return ids def get_spatial_dimension(self): return self.dimension def get_sub_entities(self): - self.A.get_sub_entities() - self.B.get_sub_entities() + return self.to_fiat().sub_entities + + def dim(self): + return self.dimensions()[-1] - def dimension(self): - return tuple(self.A.dimension, self.B.dimension) + def dimensions(self): + return list(product(*(f.dimensions() for f in self.factors))) def d_entities(self, d, get_class=True): - return self.A.d_entities(d, get_class) + self.B.d_entities(d, get_class) + if isinstance(d, tuple): + if get_class: + return self.entities[d] + return [e.id for e in self.entities[d]] + raise NotImplementedError("Tensor Product point must be indexed by a tuple of dimensions") def vertices(self, get_class=True, return_coords=False): # TODO maybe refactor with get_node - verts = self.d_entities(0, get_class) if return_coords: - a_verts = self.A.vertices(return_coords=return_coords) - b_verts = self.B.vertices(return_coords=return_coords) - return [a + b for a in a_verts for b in b_verts] - return verts + # a_verts = self.A.vertices(return_coords=return_coords) + # b_verts = self.B.vertices(return_coords=return_coords) + # return [a + b for a in a_verts for b in b_verts] + return [sum(verts, ()) for verts in product(*(f.vertices(return_coords=True) for f in self.factors))] + # return [(a, b) for a in self.A.vertices() for b in self.B.vertices()] + return list(product(*(f.vertices() for f in self.factors))) + + def __repr__(self): + return "*".join([str(f) for f in self.factors]) + + def to_ufl(self, name=None): + return TensorProductCell(*[f.to_ufl() for f in self.factors]) + + def to_fiat(self, name=None): + if self.fiat_elem is None: + self.fiat_elem = CellComplexToFiatTensorProduct(self, name) + return self.fiat_elem + + def flatten(self): + # Each factor must itself be hypercube-shaped: either a genuine + # interval (dimension == 1)or ann already-flattened cell + assert all(f.dimension == 1 or getattr(f, "flat", False) for f in self.factors) + return FlattenedPoint(*self.factors) + + +class FlattenedPoint(Point, TensorProductPoint): + d_entities_by_total_d = Point.d_entities + + def __init__(self, *factors): + self.A = factors[0] + self.B = factors[1] + self.factors = factors + self.dimension = sum(f.dimension for f in factors) + self.flat = True + fuse_edges = self.construct_fuse_rep() + super().__init__(self.dimension, fuse_edges) def to_ufl(self, name=None): - if self.flat: - return CellComplexToUFL(self, "quadrilateral") - return TensorProductCell(self.A.to_ufl(), self.B.to_ufl()) + return CellComplexToUFL(self, name=name) def to_fiat(self, name=None): - if self.flat: - return CellComplexToFiatHypercube(self, CellComplexToFiatTensorProduct(self, name)) - return CellComplexToFiatTensorProduct(self, name) + # TODO this should check if it actually is a hypercube + fiat = CellComplexToFiatHypercube(self, CellComplexToFiatTensorProduct(self, name)) + return fiat + + def d_entities(self, d, get_class=True): + if isinstance(d, tuple): + if not get_class: + return [p.id for p in self.all_subpoints[d]] + return self.all_subpoints[d] + return self.d_entities_by_total_d(d, get_class) + + def tensor_attachment_expr(self, axis, factor_edge, parent_mask, child_mask): + """ + Build the tensor-product attachment as a tuple of SymPy expressions. + + Parameters + ---------- + axis: + The factor in which the parent cell is being restricted to a facet. + + factor_edge: + The Fuse Edge from the factor parent entity to the factor child entity. + Its `.attachment` is expected to be a SymPy expression or tuple of + SymPy expressions. + + parent_mask: + Dimension tuple of the parent product entity. + + child_mask: + Dimension tuple of the child product entity. + + Example + ------- + For parent mask (1, 1), child mask (0, 1), axis 0: + + parent coords: (x, y) + attachment might be: (0, y) or (1, y) + + For parent mask (1, 1, 1), child mask (1, 0, 1), axis 1: + + parent coords: (x, y, z) + attachment might be: (x, 0, z) or (x, 1, z) + """ + child_dim = sum(child_mask) + child_syms = _SYMBOLS[:child_dim] + result = tuple() + child_offset = 0 + + for i, (pdim, cdim) in enumerate(zip(parent_mask, child_mask)): + if i == axis: + local_expr = as_tuple(factor_edge.attachment) + # Substitute the child coordinates belonging to this factor. + local_child_syms = child_syms[child_offset:child_offset + cdim] + local_child_symbols = _SYMBOLS[:cdim] + subs = {old: new for old, new in zip(local_child_symbols, local_child_syms)} + mapped = tuple(sp.sympify(expr).subs(subs) for expr in local_expr) + for comp in mapped: + result += comp + child_offset += cdim + else: + # Identity map on unchanged tensor factors. + result += tuple(child_syms[child_offset:child_offset + cdim]) + child_offset += cdim + return result + + def construct_fuse_rep(self): + """ + Construct a Fuse Point for the tensor product of two or three Fuse Point objects. + """ + if len(self.factors) not in (2, 3): + raise NotImplementedError("Only 2- and 3-factor tensor products are supported.") + top_dim = sum(f.dimension for f in self.factors) + # Cache all subentities of each factor by dimension. + factor_entities = [{d: tuple(f.d_entities(d, get_class=True)) for d in range(f.dimension + 1)} + for f in self.factors] + masks_by_total_dim = defaultdict(list) + for mask in product(*(range(f.dimension + 1) for f in self.factors)): + masks_by_total_dim[sum(mask)].append(mask) + + product_points = {} + all_subpoints = {mask: [] + for mask in product(*(range(f.dimension + 1) for f in self.factors))} + + def codim_one_facets(product_entity, mask): + """ + Yield (child_product_entity, axis, factor_edge) for each codim-1 facet. + product_entity is a tuple of factor subentities. + mask is the corresponding tuple of factor dimensions. + """ + for axis, dim in enumerate(mask): + if dim == 0: + continue + factor_parent = product_entity[axis] + for factor_edge in factor_parent.connections: + child_factor_entity = factor_edge.point + + child_entity = list(product_entity) + child_entity[axis] = child_factor_entity + child_entity = tuple(child_entity) + + yield child_entity, axis, factor_edge + + top_level_edges = [] + for total_dim in range(top_dim + 1): + for mask in masks_by_total_dim[total_dim]: + for prod_ent in product(*(factor_entities[i][d] for i, d in enumerate(mask))): + if total_dim == 0: + product_point = Point(0) + else: + boundary = [] + for child_ent, axis, factor_edge in codim_one_facets(prod_ent, mask): + child_point = product_points[child_ent] + child_mask = tuple(e.dimension for e in child_ent) + attach = self.tensor_attachment_expr(axis, factor_edge, mask, child_mask) + boundary.append(Edge(child_point, attach, factor_edge.o)) + product_point = Point(total_dim, boundary) + + if prod_ent == tuple(self.factors): + top_level_edges = boundary + product_points[prod_ent] = product_point + all_subpoints[mask].append(product_point) + self.all_subpoints = all_subpoints + return top_level_edges def flatten(self): - return TensorProductPoint(self.A, self.B, True) + return self class CellComplexToFiatSimplex(Simplex): @@ -1111,7 +1401,7 @@ def __init__(self, cell, name=None, renumber=False): # breakpoint() def cellname(self): - return self.name + return "FUSE_" + self.name def construct_subelement(self, dimension, e_id=0, o=None): """Constructs the reference element of a cell @@ -1144,15 +1434,14 @@ def __new__(cls, cell, name=None, *args, **kwargs): def __init__(self, cell, name=None): self.fe_cell = cell - self.sub_cells = [cell.A.to_fiat(), cell.B.to_fiat()] + fiat_factors = [f.to_fiat() for f in cell.factors] if name is None: - name = " * ".join([s.name for s in self.sub_cells]) + name = " * ".join([s.name for s in fiat_factors]) self.name = name -# , sub_entities=self.fe_cell.get_sub_entities() - super(CellComplexToFiatTensorProduct, self).__init__(cell.A.to_fiat(), cell.B.to_fiat()) + super(CellComplexToFiatTensorProduct, self).__init__(*fiat_factors) def cellname(self): - return self.name + return "FUSE_" + self.name def construct_subelement(self, dimension): """Constructs the reference element of a cell @@ -1180,10 +1469,11 @@ class CellComplexToFiatHypercube(Hypercube): def __init__(self, cell, product): self.fe_cell = cell + self.name = product.name super(CellComplexToFiatHypercube, self).__init__(product.get_spatial_dimension(), product) def cellname(self): - return self.name + return "FUSE_" + self.name def construct_subelement(self, dimension): """Constructs the reference element of a cell @@ -1272,18 +1562,18 @@ def constructCellComplex(name): return polygon(3).to_ufl(name) # return ufc_triangle().to_ufl(name) elif name == "quadrilateral": - interval = Point(1, [Point(0), Point(0)], vertex_num=2) - return TensorProductPoint(interval, interval).flatten().to_ufl(name) - # return ufc_quad().to_ufl(name) + return TensorProductPoint(line(), line()).flatten().to_ufl(name) + # return firedrake_quad().to_ufl(name) # return polygon(4).to_ufl(name) elif name == "tetrahedron": # return ufc_tetrahedron().to_ufl(name) return make_tetrahedron().to_ufl(name) elif name == "hexahedron": - import warnings - warnings.warn("Hexahedron unimplemented in Fuse") - import ufl - return ufl.Cell(name) + # import warnings + # warnings.warn("Hexahedron unimplemented in Fuse") + # import ufl + # return ufl.Cell(name) + return TensorProductPoint(line(), line(), line()).flatten().to_ufl(name) elif "*" in name: components = [constructCellComplex(c.strip()).cell_complex for c in name.split("*")] return TensorProductPoint(*components).to_ufl(name) diff --git a/fuse/dof.py b/fuse/dof.py index 866ef9f..e0dc878 100644 --- a/fuse/dof.py +++ b/fuse/dof.py @@ -3,6 +3,7 @@ from fuse.traces import TrH1 import numpy as np import sympy as sp +import numbers class Pairing(): @@ -35,7 +36,7 @@ def __call__(self, kernel, v, cell): return v(*kernel.pt) def tabulate(self): - return 1 + return np.eye(self.entity.dim()) def add_entity(self, entity): res = DeltaPairing() @@ -80,8 +81,8 @@ def tabulate(self): if self.orientation: new_bvs = np.array(self.entity.orient(self.orientation).basis_vectors()) basis_change = np.matmul(np.linalg.inv(new_bvs), bvs) - return basis_change - return np.eye(bvs.shape[0]) + return (1/self.entity.volume())*basis_change + return (1/self.entity.volume())*np.eye(bvs.shape[0]) def add_entity(self, entity): res = L2Pairing() @@ -190,11 +191,15 @@ def __call__(self, *args): return self.pt def evaluate(self, Qpts, Qwts, basis_change, immersed, dim, value_shape): - if isinstance(self.pt, int): - return Qpts, np.array([wt*self.pt for wt in Qwts]).astype(np.float64), [[(i,) for i in range(dim)] for pt in Qpts] + if len(value_shape) == 0: + comps = [[tuple()] for pt in Qpts] + else: + comps = [[(i,) for v in value_shape for i in range(v)] for pt in Qpts] + if isinstance(self.pt, tuple) or isinstance(self.pt, numbers.Number): + return Qpts, np.array([wt*self.pt for wt in Qwts]).astype(np.float64), comps if not immersed: - return Qpts, np.array([wt*np.matmul(self.pt, basis_change)for wt in Qwts]).astype(np.float64), [[(i,) for i in range(dim)] for pt in Qpts] - return Qpts, np.array([wt*immersed(np.matmul(self.pt, basis_change))for wt in Qwts]).astype(np.float64), [[(i,) for i in range(dim)] for pt in Qpts] + return Qpts, np.array([wt*np.matmul(self.pt, basis_change) for wt in Qwts]).astype(np.float64), comps + return Qpts, np.array([wt*immersed(np.matmul(self.pt, basis_change)) for wt in Qwts]).astype(np.float64), comps def _to_dict(self): o_dict = {"pt": self.pt} @@ -274,10 +279,14 @@ class PolynomialKernel(BaseKernel): def __init__(self, fn, g=None, symbols=[]): if hasattr(fn, "__iter__"): - if len(symbols) != 0 and any(not sp.sympify(fn[i]).as_poly() for i in range(len(fn))): - raise ValueError("Function components must be able to be interpreted as a sympy polynomial") - self.fn = [sp.sympify(fn[i]).as_poly() for i in range(len(fn))] - self.shape = len(fn) + shape = len(fn) + else: + shape = 0 + if len(symbols) != 0 and (shape != 0 and any(not sp.sympify(fn[i]).as_poly() for i in range(shape))) and not sp.sympify(fn).as_poly(): + raise ValueError("Function argument or its components must be able to be interpreted as a sympy polynomial") + if shape != 0: + self.fn = [sp.sympify(fn[i]).as_poly() for i in range(shape)] + self.shape = shape else: self.fn = sp.sympify(fn) self.shape = 0 @@ -304,9 +313,7 @@ def __call__(self, *args): if self.shape == 0: res = sympy_to_numpy(self.fn, self.syms, args[:len(self.syms)]) else: - res = [] - for i in range(self.shape): - res += [sympy_to_numpy(self.fn[i], self.syms, args[:len(self.syms)])] + res = [sympy_to_numpy(self.fn[i], self.syms, args[:len(self.syms)]) for i in range(self.shape)] return res def evaluate(self, Qpts, Qwts, basis_change, immersed, dim, value_shape): @@ -467,13 +474,7 @@ def convert_to_fiat(self, ref_el, interpolant_degree, value_shape=tuple()): def to_quadrature(self, arg_degree, value_shape): Qpts, Qwts = self.cell_defined_on.quadrature(self.kernel.degree(arg_degree)) Qwts = Qwts.reshape(Qwts.shape + (1,)) - dim = self.cell_defined_on.get_spatial_dimension() - if dim > 0: - bvs = np.array(self.cell_defined_on.basis_vectors()) - new_bvs = np.array(self.cell_defined_on.orient(self.pairing.orientation).basis_vectors()) - basis_change = np.matmul(np.linalg.inv(new_bvs), bvs) - else: - basis_change = np.eye(dim) + basis_change = self.pairing.tabulate() if self.immersed and (isinstance(self.kernel, VectorKernel) or isinstance(self.kernel, BarycentricPolynomialKernel) or isinstance(self.kernel, PolynomialKernel)): def immersed(pt): @@ -502,20 +503,22 @@ def immersed(pt): pts, wts, comps = self.kernel.evaluate(Qpts, Qwts, basis_change, immersed, self.cell.dimension, value_shape) if self.immersed: - # need to compute jacobian from attachment. pts = np.array([self.cell.attachment(self.cell.id, self.cell_defined_on.id)(*pt) for pt in pts]) - # J_det = self.cell.attachment_J_det(self.cell.id, self.cell_defined_on.id) - J_det = 1 + J_det = self.cell.attachment_J_det(self.cell.id, self.cell_defined_on.id) if not np.allclose(J_det, 1): raise ValueError("Jacobian Determinant is not 1 did you do something wrong") + J_det = 1 + # if self.pairing.orientation: + # immersion = self.target_space.tabulate(wts, self.pairing.entity.orient(self.pairing.orientation))[0] + # else: immersion = self.target_space.tabulate(pts, self.cell_defined_on) + # Special case - force evaluation on different orientation of entity for construction of matrix transforms + # if self.entity_o: + # immersion = self.target_space.tabulate(wts, self.pairing.entity.orient(self.entity_o)) if isinstance(self.target_space, TrH1): - new_wts = wts + new_wts = wts * J_det else: new_wts = np.outer(wts * J_det, immersion) - # shape is wrong for 2d face on tet - # if isinstance(self.kernel, BarycentricPolynomialKernel) and self.kernel.shape > 1: - # new_wts = np.array([self.cell.attachment(self.cell.id, self.cell_defined_on.id)(*pt) for pt in new_wts]) else: new_wts = wts # pt dict is { pt: [(weight, component)]} diff --git a/fuse/element_construction.py b/fuse/element_construction.py index 3b35a5a..63fc5ad 100644 --- a/fuse/element_construction.py +++ b/fuse/element_construction.py @@ -6,6 +6,9 @@ import itertools from functools import reduce from operator import mul +# Aliased to avoid clashing with the HDiv/HCurl interpolation-space tags +# already brought in by `from fuse import *`. +from fuse.tensor_products import HDiv as HDivTP, HCurl as HCurlTP def convert_to_generation(coords, verts, return_idx=False): @@ -444,6 +447,41 @@ def construct_tet_cgN(deg): return cg +def construct_interval_cgN(deg, cell=None): + if cell is None: + cell = line() + vert = cell.vertices()[0] + + xs = [DOF(DeltaPairing(), PointKernel(()))] + dg0 = ElementTriple(vert, (P0, CellL2, C0), DOFGenerator(xs, S1, S1)) + v_xs = [immerse(cell, dg0, TrH1)] + v_dofs = [DOFGenerator(v_xs, get_cyc_group(len(cell.vertices())), S1)] + + points = recursive_nodes(1, deg, domain="equilateral")[1:-1].flatten() + + Pk = PolynomialSpace(deg) + sym_points = [DOF(DeltaPairing(), PointKernel((pt,))) for pt in points[:len(points)//2]] + sym_dofs = [DOFGenerator([pt], S2, S1) for pt in sym_points] + if 0 in points: + centre_dof = [DOFGenerator([DOF(DeltaPairing(), PointKernel((0,)))], S1, S1)] + else: + centre_dof = [] + + cg = ElementTriple(cell, (Pk, CellH1, C0), v_dofs + sym_dofs + centre_dof) + assert len(cg.generate()) == deg + 1 + return cg + + +def construct_interval_dgN_integral(deg, cell=None): + if cell is None: + cell = line() + Pk = PolynomialSpace(deg) + dofs = lagrange_facet_fns(cell, deg, interior=True, vector=False) + dg = ElementTriple(cell, (Pk, CellL2, C0), dofs) + assert len(dg.generate()) == deg + 1 + return dg + + def construct_tri_ndN(deg): cell = polygon(3) edge = cell.edges()[0] @@ -645,6 +683,7 @@ def construct_dim_dgN(deg): def construct_dgNminus(dim): + if dim == 2: cell = polygon(3) inc = 3 @@ -663,9 +702,106 @@ def construct_dim_dgNminus(deg): return construct_dim_dgNminus +# The factors below are deliberately shared rather than rebuilt per axis: +# an axis permutation is resolved by matching DOFs across axes, and DOFs +# compare by identity, so axes built from separate calls never match. + + +def construct_quad_cgN(deg): + A = construct_interval_cgN(deg) + elem = tensor_product(A, A).flatten() + assert len(elem.generate()) == (deg + 1)**2 + return elem + + +def construct_hex_cgN(deg): + A = construct_interval_cgN(deg) + elem = symmetric_tensor_product(A, A, A).flatten() + assert len(elem.generate()) == (deg + 1)**3 + return elem + + +def construct_quad_dgN(deg): + A = construct_interval_dgN_integral(deg) + elem = tensor_product(A, A).flatten() + assert len(elem.generate()) == (deg + 1)**2 + return elem + + +def construct_hex_dgN(deg): + A = construct_interval_dgN_integral(deg) + elem = symmetric_tensor_product(A, A, A).flatten() + assert len(elem.generate()) == (deg + 1)**3 + return elem + + +def construct_quad_rtN(deg): + cgN = construct_interval_cgN(deg) + dgNm1 = construct_interval_dgN_integral(deg - 1) + # Enriched first, then flattened: the sum is the smallest object whose + # DOF set is closed under the axis swap, so flattening a component on + # its own would bury a non-symmetric flat element inside the sum. + elem = (HDivTP(tensor_product(cgN, dgNm1)) + HDivTP(tensor_product(dgNm1, cgN))).flatten() + assert len(elem.generate()) == 2 * deg * (deg + 1) + return elem + + +def construct_quad_ndN(deg): + cgN = construct_interval_cgN(deg) + dgNm1 = construct_interval_dgN_integral(deg - 1) + # See construct_quad_rtN: enriched first, then flattened. + elem = (HCurlTP(tensor_product(cgN, dgNm1)) + HCurlTP(tensor_product(dgNm1, cgN))).flatten() + assert len(elem.generate()) == 2 * deg * (deg + 1) + return elem + + +def construct_hex_rtN(deg): + # In-plane RT_deg-on-quad pieces, extruded by a discontinuous + # interval, following the same structure as rt1_hex (deg=1 case). + cgN = construct_interval_cgN(deg) + dgNm1 = construct_interval_dgN_integral(deg - 1) + h1 = HDivTP(tensor_product(cgN, dgNm1).flatten()) + h2 = HDivTP(tensor_product(dgNm1, cgN).flatten()) + x_component = HDivTP(tensor_product(h1, dgNm1)) + y_component = HDivTP(tensor_product(h2, dgNm1)) + dg_quad = tensor_product(dgNm1, dgNm1).flatten() + z_component = HDivTP(tensor_product(dg_quad, cgN)) + elem = x_component + y_component + z_component + assert len(elem.generate()) == 3 * deg**2 * (deg + 1) + # Unlike the quad case, the outer tensor_product here combines an + # already-flat 2D piece with a genuine 1D interval, so the result + # isn't itself flat yet (matches rt1_hex's own need for an explicit + # .flatten() at the call site) -- flatten here so callers get a + # directly-usable element, consistent with construct_hex_cgN/dgN. + return elem.flatten() + + +def construct_hex_ndN(deg): + # In-plane Nedelec-1st-kind-deg-on-quad pieces, extruded by a + # continuous interval, following the same structure as ned1_hex + # (deg=1 case). + cgN = construct_interval_cgN(deg) + dgNm1 = construct_interval_dgN_integral(deg - 1) + ex = HCurlTP(tensor_product(dgNm1, cgN).flatten()) + ey = HCurlTP(tensor_product(cgN, dgNm1).flatten()) + x_component = HCurlTP(tensor_product(ex, cgN)) + y_component = HCurlTP(tensor_product(ey, cgN)) + cg_quad = tensor_product(cgN, cgN).flatten() + z_component = HCurlTP(tensor_product(cg_quad, dgNm1)) + elem = x_component + y_component + z_component + assert len(elem.generate()) == 3 * (deg + 1)**2 * deg + # See construct_hex_rtN: flatten here so callers get a directly-usable + # element, consistent with construct_hex_cgN/dgN. + return elem.flatten() + + # column: dimension: form number constructors = { 0: { + 1: { + 0: construct_interval_cgN, + 1: construct_interval_dgN_integral, + }, 2: { 0: construct_tri_cgN, 1: construct_tri_ndN, @@ -680,6 +816,10 @@ def construct_dim_dgNminus(deg): }, }, 1: { + 1: { + 0: construct_interval_cgN, + 1: construct_interval_dgN_integral, + }, 2: { 0: construct_tri_cgN, 1: construct_tri_ndN_2, @@ -693,6 +833,24 @@ def construct_dim_dgNminus(deg): 3: construct_dgN(3), }, }, + 2: { + 1: { + 0: construct_interval_cgN, + 1: construct_interval_dgN_integral, + }, + 2: { + 0: construct_quad_cgN, + 1: construct_quad_ndN, + 2: construct_quad_rtN, + 3: construct_quad_dgN, + }, + 3: { + 0: construct_hex_cgN, + 1: construct_hex_ndN, + 2: construct_hex_rtN, + 3: construct_hex_dgN, + }, + }, } diff --git a/fuse/enriched.py b/fuse/enriched.py new file mode 100644 index 0000000..36e9866 --- /dev/null +++ b/fuse/enriched.py @@ -0,0 +1,130 @@ +import numpy as np +from fuse.tensor_products import TensorProductTriple +import finat.ufl + + +class EnrichedElement(TensorProductTriple): + """ + Non-nodal representation of an enriched element. + + In general, FUSE element triples should be represented nodally, + however this may not be possible for all constructions. + + In particular, we need to preserve tensor product structure. + """ + + def __init__(self, A, B, flat=False, symmetric=None, matrices=True): + valid_types = (TensorProductTriple, EnrichedElement) + if not isinstance(A, valid_types) or not isinstance(B, valid_types): + raise ValueError("EnrichedElement should only be used for Tensor product elements. Use + between triples for enrichment.") + self.A = A + self.B = B + self.spaces = (A.spaces[0] + B.spaces[0], A.spaces[1], max([A.spaces[2], B.spaces[2]])) + + self.DOFGenerator = [A.DOFGenerator, B.DOFGenerator] + if A.cell.flat != B.cell.flat: + raise ValueError("Tensor products must both be flat or both not flat for enrichment.") + self.cell = A.cell + # Derived, not trusted: an enriched element whose cell is flat but + # which reports flat=False silently skips the axis-swap fill and the + # DOF regrouping. + self.flat = flat or self.cell.flat + if hasattr(A, "unflat_cell"): + self.unflat_cell = A.unflat_cell + # See TensorProductTriple.__init__ for the meaning of ``symmetric``. + self.requested_symmetric = symmetric + self.symmetric = True if symmetric is None else symmetric + self.apply_matrices = matrices + if self.apply_matrices: + self.setup_matrices() + + self.pure_perm = not matrices + + @property + def sub_elements(self): + return [self.A, self.B] + + def get_value_shape(self): + if str(self.spaces[1]) in ("HDiv", "HCurl"): + return (self.cell.get_spatial_dimension(),) + return super().get_value_shape() + + def __repr__(self): + return "Enriched(%s, %s)" % (repr(self.A), repr(self.B)) + + def __add__(self, other): + assert self.spaces[0].shape == other.spaces[0].shape + assert str(self.spaces[1]) == str(other.spaces[1]) + return EnrichedElement(self, other, flat=self.flat and other.flat, + matrices=self.apply_matrices or other.apply_matrices) + + def setup_matrices(self): + if self.flat and not self.symmetric: + raise NotImplementedError("Matrices for flattened cells that are not symmetric not supported") + self.A.to_ufl() + self.B.to_ufl() + dofs = self.generate() + dof_keys, key_to_index = self._axis_key_maps(dofs) + oriented_mats_by_entity, flat_by_entity = self._initialise_entity_dicts(dofs, tensor=(not self.flat)) + if self.flat: + cell = self.A.unflat_cell + else: + cell = self.cell + top = cell.to_fiat().get_topology() + seen_total_dims = set() + for dim in top.keys(): + total_dim = sum(dim) if self.flat else dim + if total_dim in seen_total_dims: + continue + seen_total_dims.add(total_dim) + ents = self.entity_dofs[total_dim].keys() + # comp_os = cell.component_orientations() + for e_idx, e in enumerate(ents): + ent_dofs = self.entity_dofs[total_dim][e] + if len(ent_dofs) >= 1: + sub_mat = oriented_mats_by_entity[total_dim][e_idx] + a_mat = self.A.generation_order_matrices()[total_dim][e_idx] + a_ent_ids = self.A.entity_dofs[total_dim][e] + b_mat = self.B.generation_order_matrices()[total_dim][e_idx] + b_ent_ids = self.B.entity_dofs[total_dim][e] + + for o in a_mat.keys(): + a_sub_mat = a_mat[o][np.ix_(a_ent_ids, a_ent_ids)] + b_sub_mat = b_mat[o][np.ix_(b_ent_ids, b_ent_ids)] + combined_sub_mat = np.block([[a_sub_mat, np.zeros((a_sub_mat.shape[0], b_sub_mat.shape[1]))], + [np.zeros((b_sub_mat.shape[0], a_sub_mat.shape[1])), b_sub_mat]]) + sub_mat[o][np.ix_(ent_dofs, ent_dofs)] = np.matmul(sub_mat[o][np.ix_(ent_dofs, ent_dofs)], combined_sub_mat) + if self.flat: + self._fill_face_axis_swaps(dim, ent_dofs, sub_mat, dof_keys, key_to_index) + + self.matrices = oriented_mats_by_entity + self.reversed_matrices = self.reverse_dof_perms(self.matrices) + if self.flat: + self._snapshot_generation_order() + self._regroup_matrices() + + self._resolve_symmetry() + + def generate(self): + a_dofs = self.A.generate() + b_dofs = self.B.generate() + numAdofs = len(a_dofs) + self.entity_dofs = {} + for dim in self.A.entity_dofs.keys(): + self.entity_dofs[dim] = {} + for ent in self.A.entity_dofs[dim]: + self.entity_dofs[dim][ent] = self.A.entity_dofs[dim][ent] + [b_dof + numAdofs for b_dof in self.B.entity_dofs[dim][ent]] + self.dofs = a_dofs + b_dofs + return self.dofs + + def to_ufl(self): + ufl_sub_elements = [e.to_ufl() for e in self.sub_elements] + return finat.ufl.EnrichedElement(*ufl_sub_elements, triple=self) + + def flatten(self, symmetric=None): + if symmetric is None: + symmetric = self.requested_symmetric + return EnrichedElement(self.A.flatten(), self.B.flatten(), flat=True, symmetric=symmetric, matrices=self.apply_matrices) + + def unflatten(self): + return EnrichedElement(self.A.unflatten(), self.B.unflatten(), flat=False, symmetric=self.requested_symmetric, matrices=self.apply_matrices) diff --git a/fuse/groups.py b/fuse/groups.py index 5666acd..8d5f69d 100644 --- a/fuse/groups.py +++ b/fuse/groups.py @@ -1,5 +1,5 @@ import fuse.cells as cells -from fuse.utils import orientation_value +from fuse.utils import orientation_value, canonical_tensor_orientation_key from sympy.combinatorics import PermutationGroup, Permutation from sympy.combinatorics.named_groups import SymmetricGroup, DihedralGroup, CyclicGroup, AlternatingGroup from sympy.matrices.expressions import PermutationMatrix @@ -29,6 +29,50 @@ def perm_list_to_matrix(identity, perm): return res +def is_hypercube_cell(cell): + """True for interval-product entities (quad, hex, ...), i.e. cells with + ``2**dim`` vertices and ``dim >= 2``. Simplices never satisfy this, so + their numbering is untouched.""" + if cell is None: + return False + dim = getattr(cell, "dimension", None) + if dim is None or dim < 2: + return False + try: + nverts = len(cell.vertices()) + except (AttributeError, TypeError): + return False + return nverts == 2 ** dim + + +def signed_axis_permutation(member, d): + """Decompose a hypercube symmetry into ``(axis_perm, flips)``. + + Reads the linear part of the member's affine transform (``new = v @ L``): + input axis ``i`` maps to output axis ``axis_perm[i]``, and ``flips[i]`` + marks a reflection of axis ``i``. + """ + L = np.array(member.transform_matrix)[:d, :d] + axis_perm = [0] * d + flips = [0] * d + for j in range(d): + i = int(np.argmax(np.abs(L[:, j]))) + axis_perm[i] = j + flips[i] = 1 if L[i, j] < 0 else 0 + return tuple(axis_perm), tuple(flips) + + +def canonical_hypercube_numbering(members, cell): + """Map each member's raw orientation value to its canonical FIAT/dmcommon + key for an interval-product ``cell``.""" + d = cell.dimension + numbering = {} + for m in members: + axis_perm, flips = signed_axis_permutation(m, d) + numbering[m.numeric_rep()] = canonical_tensor_orientation_key(axis_perm, flips, d) + return numbering + + class GroupMemberRep(object): def __init__(self, perm, M, group): @@ -65,9 +109,15 @@ def compute_perm(self, base_val=None): return val, val_list def numeric_rep(self): + """ Uses a standard formula to number permutations in the group. + For the case where this doesn't automatically number from 0..n (ie the group is not the full symmetry group), + a mapping is constructed on group creation""" identity = self.group.identity.vertex_order_form m_array = self.vertex_order_form - return orientation_value(identity, m_array) + val = orientation_value(identity, m_array) + if self.group.group_rep_numbering is not None: + return self.group.group_rep_numbering[val] + return val def _to_dict(self): return {"perm": self.perm.array_form, "M": self.transform_matrix.tolist(), "group": self.group} @@ -206,6 +256,14 @@ def __init__(self, perm_list, cell=None, name=None): counter += 1 # self._members = sorted(self._members, key=lambda g: g.numeric_rep()) + self.group_rep_numbering = None + if is_hypercube_cell(self.cell): + self.group_rep_numbering = canonical_hypercube_numbering(self.members(), self.cell) + else: + numeric_reps = [m.numeric_rep() for m in self.members()] + if sorted(numeric_reps) != list(range(len(numeric_reps))): + self.group_rep_numbering = {a: b for a, b in zip(sorted(numeric_reps), list(range(len(numeric_reps))))} + def add_cell(self, cell): return PermutationSetRepresentation(self.perm_list, cell=cell, name=self.name) @@ -355,6 +413,14 @@ def __init__(self, base_group, cell=None, name=None): self.identity = p_rep counter += 1 + self.group_rep_numbering = None + if is_hypercube_cell(self.cell): + self.group_rep_numbering = canonical_hypercube_numbering(self.members(), self.cell) + else: + numeric_reps = [m.numeric_rep() for m in self.members()] + if sorted(numeric_reps) != list(range(len(numeric_reps))): + self.group_rep_numbering = {a: b for a, b in zip(sorted(numeric_reps), list(range(len(numeric_reps))))} + # this order produces simpler generator lists # self.generators.reverse() diff --git a/fuse/spaces/polynomial_spaces.py b/fuse/spaces/polynomial_spaces.py index feb3382..4c3b49c 100644 --- a/fuse/spaces/polynomial_spaces.py +++ b/fuse/spaces/polynomial_spaces.py @@ -1,13 +1,16 @@ from FIAT.polynomial_set import ONPolynomialSet +from FIAT.expansions import morton_index2, morton_index3 from FIAT.quadrature_schemes import create_quadrature from FIAT.reference_element import cell_to_simplex from FIAT import expansions, polynomial_set, reference_element from itertools import chain -from fuse.utils import tabulate_sympy, max_deg_sp_mat +from fuse.utils import tabulate_sympy, max_deg_sp_expr import sympy as sp import numpy as np from functools import total_ordering +morton_index = {2: morton_index2, 3: morton_index3} + def normalise_shape(shape): """Canonicalise a declared value shape to a tuple of positive ints. @@ -84,24 +87,29 @@ def degree(self): return self.maxdegree def to_ON_polynomial_set(self, ref_el, k=None): - # how does super/sub degrees work here if not isinstance(ref_el, reference_element.Cell): ref_el = ref_el.to_fiat() ref_el = cell_to_simplex(ref_el) - shape = self.shape + base_ON = ONPolynomialSet(ref_el, self.maxdegree, self.shape, scale="orthonormal") + indices = None if self.mindegree > 0: - base_ON = ONPolynomialSet(ref_el, self.maxdegree, shape, scale="orthonormal") dimPmin = expansions.polynomial_dimension(ref_el, self.mindegree) dimPmax = expansions.polynomial_dimension(ref_el, self.maxdegree) - if shape: - num_components = int(np.prod(shape)) + if self.shape: + num_components = int(np.prod(self.shape)) indices = list(chain(*(range(i * dimPmin, i * dimPmax) for i in range(num_components)))) else: indices = list(range(dimPmin, dimPmax)) - restricted_ON = base_ON.take(indices) - return restricted_ON - return ONPolynomialSet(ref_el, self.maxdegree, shape, scale="orthonormal") + + if self.contains != self.maxdegree and self.contains != -1: + indices = [morton_index[ref_el.get_spatial_dimension()](p, q) for p in range(self.contains + 1) for q in range(self.contains + 1)] + + if indices is None: + return base_ON + + restricted_ON = base_ON.take(indices) + return restricted_ON def __repr__(self): res = "" @@ -121,9 +129,7 @@ def __mul__(self, x): the sympy object on the right. This is due to Sympy's implementation of __mul__ not passing to this handler as it should. """ - if isinstance(x, sp.Symbol): - return ConstructedPolynomialSpace([x], [self]) - elif isinstance(x, sp.Matrix): + if isinstance(x, sp.Symbol) or isinstance(x, sp.Expr) or isinstance(x, sp.Matrix): return ConstructedPolynomialSpace([x], [self]) else: raise TypeError(f'Cannot multiply a PolySpace with {type(x)}') @@ -170,8 +176,7 @@ def dict_id(self): return "PolynomialSpace" def _from_dict(obj_dict): - shape = obj_dict["shape"] if "shape" in obj_dict else obj_dict["set_shape"] - return PolynomialSpace(obj_dict["max"], obj_dict["contains"], obj_dict["min"], shape) + return PolynomialSpace(obj_dict["max"], obj_dict["contains"], obj_dict["min"], obj_dict["shape"]) class ConstructedPolynomialSpace(PolynomialSpace): @@ -186,7 +191,7 @@ def __init__(self, weights, spaces): self.weights = weights self.spaces = spaces - weight_degrees = [0 if not (isinstance(w, sp.Expr) or isinstance(w, sp.Matrix)) else max_deg_sp_mat(w) for w in self.weights] + weight_degrees = [0 if not (isinstance(w, sp.Expr) or isinstance(w, sp.Matrix)) else max_deg_sp_expr(w) for w in self.weights] maxdegree = max([space.maxdegree + w_deg for space, w_deg in zip(spaces, weight_degrees)]) mindegree = min([space.mindegree + w_deg for space, w_deg in zip(spaces, weight_degrees)]) @@ -209,39 +214,47 @@ def to_ON_polynomial_set(self, ref_el): if not isinstance(ref_el, reference_element.Cell): ref_el = ref_el.to_fiat() k = max([s.maxdegree for s in self.spaces]) - space_poly_sets = [s.to_ON_polynomial_set(ref_el) for s in self.spaces] sd = ref_el.get_spatial_dimension() ref_el = cell_to_simplex(ref_el) - if all([w == 1 for w in self.weights]): - weighted_sets = space_poly_sets - # otherwise have to work on this through tabulation - Q = create_quadrature(ref_el, 2 * (k + 1)) - Qpts, Qwts = Q.get_points(), Q.get_weights() weighted_sets = [] - for (space, w) in zip(space_poly_sets, self.weights): + for (s, w) in zip(self.spaces, self.weights): + space = s.to_ON_polynomial_set(ref_el) if not (isinstance(w, sp.Expr) or isinstance(w, sp.Matrix)): weighted_sets.append(space) else: - w_deg = max_deg_sp_mat(w) - Pkpw = ONPolynomialSet(ref_el, space.degree + w_deg, scale="orthonormal") - vec_Pkpw = ONPolynomialSet(ref_el, space.degree + w_deg, self.shape, scale="orthonormal") + if isinstance(w, sp.Expr): + w = sp.Matrix([[w]]) + vec = False + else: + vec = True + w_deg = max_deg_sp_expr(w) + Q = create_quadrature(ref_el, 2 * (k + w_deg + 1)) + Qpts, Qwts = Q.get_points(), Q.get_weights() + Pkpw = ONPolynomialSet(ref_el, space.degree + w_deg, s.shape, scale="orthonormal") space_at_Qpts = space.tabulate(Qpts)[(0,) * sd] Pkpw_at_Qpts = Pkpw.tabulate(Qpts)[(0,) * sd] tabulated_expr = tabulate_sympy(w, Qpts).T + if tabulated_expr.shape[0] != int(np.prod(self.shape)): raise ValueError(f"Weight {w} has {tabulated_expr.shape[0]} components but the space has value shape {self.shape}.") + scaled_at_Qpts = space_at_Qpts[:, None, :] * tabulated_expr[None, :, :] + if not vec and len(s.shape) == 0: + # remove extra dimensions if we don't have a vector valued space + scaled_at_Qpts = scaled_at_Qpts.squeeze() PkHw_coeffs = np.dot(np.multiply(scaled_at_Qpts, Qwts), Pkpw_at_Qpts.T) + if len(PkHw_coeffs.shape) == 1: + PkHw_coeffs = PkHw_coeffs.reshape(1, -1) weighted_sets.append(polynomial_set.PolynomialSet(ref_el, space.degree + w_deg, space.degree + w_deg, - vec_Pkpw.get_expansion_set(), + Pkpw.get_expansion_set(), PkHw_coeffs)) combined_sets = weighted_sets[0] for i in range(1, len(weighted_sets)): @@ -260,6 +273,9 @@ def __add__(self, x): s.extend([x]) return ConstructedPolynomialSpace(w, s) + def to_vector(self): + return ConstructedPolynomialSpace(self.weights, [space.to_vector() for space in self.spaces]) + def _to_dict(self): super_dict = super(ConstructedPolynomialSpace, self)._to_dict() super_dict["spaces"] = self.spaces diff --git a/fuse/tensor_products.py b/fuse/tensor_products.py index 75e9939..89f1176 100644 --- a/fuse/tensor_products.py +++ b/fuse/tensor_products.py @@ -1,19 +1,76 @@ from fuse.triples import ElementTriple +from fuse.traces import TrHCurl, TrHDiv +from fuse.spaces.element_sobolev_spaces import CellHDiv, CellHCurl from fuse.cells import TensorProductPoint -from finat.ufl import TensorProductElement, FuseElement +import numpy as np +from finat.ufl import TensorProductElement, FuseElement, HDivElement, HCurlElement +from itertools import product, permutations +from functools import reduce +from collections import defaultdict -def tensor_product(A, B): - if not (isinstance(A, ElementTriple) and isinstance(B, ElementTriple)): - raise ValueError("Both components of Tensor Product need to be a Fuse Triple.") - return TensorProductTriple(A, B) +def tensor_product(*factors, matrices=True): + if not all(isinstance(f, ElementTriple) for f in factors): + raise ValueError("All components of Tensor Product need to be a Fuse Triple.") + return TensorProductTriple(*factors, matrices=matrices) + + +def symmetric_tensor_product(*factors, matrices=True): + if not all(isinstance(f, ElementTriple) for f in factors): + raise ValueError("All components of Tensor Product need to be a Fuse Triple.") + return TensorProductTriple(*factors, matrices=matrices, symmetric=True) + + +def flatten_dictionary(tensor_dict): + counters = {} + flat_dict = {} + for dim in tensor_dict.keys(): + total_dim = sum(dim) + if total_dim not in counters.keys(): + counters[total_dim] = 0 + flat_dict[total_dim] = {} + for i in range(len(tensor_dict[dim].keys())): + flat_dict[total_dim][i + counters[total_dim]] = tensor_dict[dim][i] + counters[total_dim] += len(tensor_dict[dim].keys()) + return flat_dict + + +def leaf_dof_keys(elem, out=None): + """Map each of ``elem``'s generated DOFs to a tuple of per-axis leaf DOFs. + + A tensor product DOF is a tuple with one component per factor, but a + factor may itself be a product, so a component can be a nested tuple. + Descending to the one-dimensional leaves gives every DOF a flat key with + one entry per spatial axis, which is what an axis permutation acts on. + """ + if out is None: + out = {} + from fuse.enriched import EnrichedElement + if isinstance(elem, EnrichedElement): + # Checked before TensorProductTriple, which it subclasses. + leaf_dof_keys(elem.A, out) + leaf_dof_keys(elem.B, out) + elif isinstance(elem, TensorProductTriple): + sub_keys = [leaf_dof_keys(f) for f in elem.factors] + for dof in elem.generate(): + out[dof] = sum((sub_keys[i][comp] for i, comp in enumerate(dof)), ()) + else: + for dof in elem.generate(): + out[dof] = (dof,) + return out class TensorProductTriple(ElementTriple): - def __init__(self, A, B, flat=False): - self.A = A - self.B = B + # Axis permutations the DOF set turned out not to be closed under. + # Populated by ``_fill_face_axis_swaps``; stays empty when matrices are + # not built at all. + _closure_failures = frozenset() + + def __init__(self, *factors, flat=False, symmetric=None, matrices=True): + if len(factors) < 2: + raise ValueError("Cannot create a tensor product with fewer than 2 factors") + self.factors = factors (poly_a, wi_a, pullback_a) = A.spaces (poly_b, wi_b, pullback_b) = B.spaces if pullback_a != pullback_b: @@ -22,27 +79,470 @@ def __init__(self, A, B, flat=False): wi_a if wi_a >= wi_b else wi_b, pullback_a] - self.DOFGenerator = [A.DOFGenerator, B.DOFGenerator] - self.cell = TensorProductPoint(A.cell, B.cell) + self.DOFGenerator = [f.DOFGenerator for f in self.factors] + self.cell = TensorProductPoint(*[f.cell for f in factors]) + # ``symmetric=None`` means derive it from whether the DOF set is + # actually closed under axis permutation; True additionally asserts + # that it is, False opts out of building the axis-swap orientations. + self.requested_symmetric = symmetric + self.symmetric = True if symmetric is None else symmetric self.flat = flat - self.apply_matrices = False + if self.flat: + self.unflat_cell = self.cell + self.cell = self.cell.flatten() + self.dofs = self.generate() + # Subclasses (HDiv, HCurl) set self.mat_transformer before calling + # this constructor; only default it here if they haven't. + self.mat_transformer = getattr(self, "mat_transformer", None) + self.apply_matrices = matrices + if self.apply_matrices: + self.setup_matrices() + + self.pure_perm = not matrices + + @property def sub_elements(self): - return [self.A, self.B] + return self.factors + + @property + def form_degree(self): + # Using lowest dimension dof to define form degree, tensor product dims are additive + return min(sum(comp.cell_defined_on.dim() for comp in dof) for dof in self.generate()) def __repr__(self): - return "TensorProd(%s, %s)" % (repr(self.A), repr(self.B)) + return f"TensorProd({','.join(['{}' for f in self.factors])})".format(*(repr(f) for f in self.factors)) + + def _entity_associations(self, dofs, overall=True): + return self.entity_assocs, None, None + + def setup_matrices(self): + if self.flat and not self.symmetric: + raise NotImplementedError("Matrices for flattened cells that are not symmetric not supported") + for f in self.factors: + f.to_ufl() + dofs = self.generate() + dof_keys, key_to_index = self._axis_key_maps(dofs) + oriented_mats_by_entity, flat_by_entity = self._initialise_entity_dicts(dofs, tensor=True) + if self.flat: + cell = self.unflat_cell + else: + cell = self.cell + top = cell.to_fiat().get_topology() + for dim in top.keys(): + total_dim = sum(dim) if self.flat else dim + f_ents = [f.cell.get_topology()[d].keys() for f, d in zip(self.factors, dim)] + ents = list(product(*(f_ents))) + comp_os = cell.component_orientations() + for e, sub_ents in enumerate(ents): + ent_dofs = self.entity_dofs[total_dim][self.ent_mapping[dim][sub_ents]] + if len(ent_dofs) >= 1: + sub_mat = oriented_mats_by_entity[dim][e] + mats = [f.generation_order_matrices()[d][ent] for f, d, ent in zip(self.factors, dim, sub_ents)] + ent_ids = [f.entity_dofs[d][ent] for f, d, ent in zip(self.factors, dim, sub_ents)] + os = list(product(*([mat.keys() for mat in mats]))) + for o in os: + sub_mats = [mat[o_f][np.ix_(ent_id, ent_id)] for mat, o_f, ent_id in zip(mats, o, ent_ids)] + if self.mat_transformer is not None: + o_classes = [f.cell.group.get_member_by_val(o_f) for f, o_f in zip(self.factors, o)] + combined_sub_mat = self.mat_transformer(*sub_mats, o_classes) + else: + combined_sub_mat = reduce(lambda acc, x: np.kron(acc, x), sub_mats) + new_o = comp_os[dim][o] + if new_o in sub_mat.keys(): + sub_mat[new_o][np.ix_(ent_dofs, ent_dofs)] = np.matmul(sub_mat[new_o][np.ix_(ent_dofs, ent_dofs)], combined_sub_mat) + # sub_mat[new_o][np.ix_(ent_dofs, ent_dofs)] = np.eye(np.matmul(sub_mat[new_o][np.ix_(ent_dofs, ent_dofs)], combined_sub_mat).shape[0]) + if self.flat: + self._fill_face_axis_swaps(dim, ent_dofs, sub_mat, dof_keys, key_to_index) + + if self.flat: + oriented_mats_by_entity = flatten_dictionary(oriented_mats_by_entity) + + self.matrices = oriented_mats_by_entity + self.reversed_matrices = self.reverse_dof_perms(self.matrices) + + if self.flat: + self._snapshot_generation_order() + self._regroup_matrices() + + self._resolve_symmetry() + + def _resolve_symmetry(self): + """Settle ``self.symmetric`` against the closure the fill observed. + + A flat element is symmetric exactly when every entity's DOFs are + closed under permutation of that entity's axes, which is what + ``_fill_face_axis_swaps`` needs in order to produce the axis-swap + orientations at all. + """ + closed = not self._closure_failures + if self.requested_symmetric is None: + self.symmetric = closed + elif self.requested_symmetric and not closed: + raise NotImplementedError( + "%r was declared symmetric but its DOFs are not closed under " + "axis permutation %r" % (self, sorted(self._closure_failures))) + + def _axis_permutation_sign(self, tau): + """Value change an H(div) DOF picks up from permuting axes. + + Reordering an entity's axes by an odd permutation reverses its + orientation, so the normal an H(div) DOF integrates against flips. + Nothing downstream supplies this: Firedrake's assembly selects one of + these matrices and multiplies by it once (``FuseMatrixApplyBuilder`` + in ``firedrake/pack.py``), so the sign has to be carried here. + + The reflection part of an orientation already carries its own sign + through the matrix being composed with, leaving only the + permutation's parity. Tangential (H(curl)) and scalar DOFs are + unaffected by the reversal itself. + """ + if str(self.spaces[1]) != "HDiv": + return 1 + inversions = sum(1 for i in range(len(tau)) + for j in range(i + 1, len(tau)) if tau[i] > tau[j]) + return -1 if inversions % 2 else 1 + + def _axis_key_maps(self, dofs): + """Per-axis leaf keys for ``dofs``, indexed both ways. + + Returns ``(dof_keys, key_to_index)`` where ``dof_keys`` maps a global + DOF index to its leaf key and ``key_to_index`` inverts that. Resets + the record of axis permutations the DOF set is not closed under. + """ + self._closure_failures = set() + leaves = leaf_dof_keys(self) + dof_keys = {} + key_to_index = {} + for i, dof in enumerate(dofs): + key = leaves.get(dof) + if key is None: + continue + dof_keys[i] = key + key_to_index[key] = i + return dof_keys, key_to_index + + def _snapshot_generation_order(self): + """Keep a copy of the matrices indexed in generation DOF order. + + ``_regroup_matrices`` rewrites ``self.matrices`` into the + dimension-grouped order Firedrake consumes, but ``self.entity_dofs`` + stays in generation order. Parent elements pair the two when they + read a factor's blocks, so they need the un-regrouped copy. + """ + self._gen_order_matrices = {dim: {e: {o: mat.copy() for o, mat in os.items()} + for e, os in ents.items()} + for dim, ents in self.matrices.items()} + + def _regroup_matrices(self): + """Re-express the orientation matrices in closure DOF order. + + FUSE generates tensor-product and enriched DOFs in an interleaved + order. Firedrake packs each cell's closure DOFs by entity dimension + and, within a dimension, by entity number, and applies these matrices + in that order. + + Ordering by dimension alone is not enough. An entity's matrix is the + identity apart from a block sitting at that entity's own DOFs, so if + the entities within a dimension come out in the wrong order the block + lands on a different entity's DOFs -- the right transformation applied + to the wrong DOF. That stays invisible while every orientation is the + identity and only bites once an entity is actually reversed. + """ + grouped = [dof + for total_dim in sorted(self.entity_dofs) + for ent in sorted(self.entity_dofs[total_dim]) + for dof in self.entity_dofs[total_dim][ent]] + n = len(grouped) + if grouped == list(range(n)): + return + ix = np.ix_(grouped, grouped) + for mats in (self.matrices, self.reversed_matrices): + for ents in mats.values(): + for os in ents.values(): + for k in list(os.keys()): + os[k] = os[k][ix].copy() + + def _fill_face_axis_swaps(self, dim, ent_dofs, sub_mat, dof_keys, key_to_index): + """Populate the axis-permuting orientations of an entity. + + The per-entity loop in ``setup_matrices`` fills only the reflection + subgroup (extrinsic orientation ``eo == 0``, canonical keys + ``0..2**d - 1``) because it enumerates products of the factors' own + orientations, which cannot permute axes. Enriched elements are worse + still: they combine their summands block-diagonally, so they cannot + even express a permutation that maps one summand's DOFs onto + another's. + + The remaining members compose those reflections with a pure DOF + permutation. An axis permutation ``tau`` sends the DOF whose per-axis + leaf key is ``k`` to the DOF with key ``tau(k)``, so looking that key + up gives the permutation directly, for any number of axes and across + summand blocks. The canonical key ``2**d * eo + io`` (see + ``fuse.utils.canonical_tensor_orientation_key``) is then + ``M[io] @ P_tau``. + + Skipped when the entity's DOFs are not closed under ``tau``; the + caller records that as a failure of symmetry. + """ + ed = sum(dim) if isinstance(dim, tuple) else dim + if ed < 2 or len(ent_dofs) == 0: + # A point or an interval has no axes to permute. + return + keys = [dof_keys.get(d) for d in ent_dofs] + if any(k is None for k in keys): + self._closure_failures.add((ed, None)) + return + # Which leaf axes this entity actually extends along. Taking these + # from the DOFs rather than from `dim` is what lets one code path + # serve hex cells, hex faces, and factors that are themselves + # flattened quads. + active = {tuple(j for j, c in enumerate(k) if c.cell_defined_on.dim() == 1) for k in keys} + if len(active) != 1 or len(next(iter(active))) != ed: + # The entity's DOFs disagree about which axes it extends along, + # so there is no well-defined action to build. + self._closure_failures.add((ed, None)) + return + act = active.pop() + local = {d: i for i, d in enumerate(ent_dofs)} + grid = np.ix_(ent_dofs, ent_dofs) + for eo, tau in enumerate(sorted(permutations(range(ed)))): + if eo == 0: + continue + perm = [] + for k in keys: + new_key = list(k) + for i in range(ed): + new_key[act[i]] = k[act[tau.index(i)]] + target = key_to_index.get(tuple(new_key)) + if target is None or target not in local: + perm = None + break + perm.append(local[target]) + if perm is None: + self._closure_failures.add((ed, eo)) + continue + P = self._axis_permutation_sign(tau) * np.eye(len(ent_dofs))[perm] + for io in range(2 ** ed): + swap_key = 2 ** ed * eo + io + if io in sub_mat and swap_key in sub_mat: + sub_mat[swap_key][grid] = np.matmul(sub_mat[io][grid], P) + + def generate(self): + dofs = [f.generate() for f in self.factors] + ent_assocs = [f._entity_associations(dofs_f, overall=False)[0] for f, dofs_f in zip(self.factors, dofs)] + if self.flat: + top = self.unflat_cell.to_fiat().get_topology() + else: + top = self.cell.to_fiat().get_topology() + self.entity_dofs = defaultdict(dict) + self.ent_mapping = {} + self.entity_assocs = defaultdict(dict) + self.dof_ids = {} + dofs = [] + ent_counter = defaultdict(lambda: 0) + dof_counter = 0 + for dim in top.keys(): + total_dim = sum(dim) if self.flat else dim + ents = [ent_assoc[d].keys() for ent_assoc, d in zip(ent_assocs, dim)] + # if total_dim not in self.entity_dofs.keys(): + # self.entity_dofs[total_dim] = {} + # self.entity_assocs[total_dim] = {} + self.ent_mapping[dim] = {} + ent_list = [] + for i, ent in enumerate(list(product(*ents))): + self.ent_mapping[dim][ent] = i + ent_counter[total_dim] if self.flat else ent + self.entity_dofs[total_dim][self.ent_mapping[dim][ent]] = [] + ent_list += [ent] + for es in ent_list: + e_dofs = [[d for dofs in ent_assoc[d][e].values() for d in dofs] for ent_assoc, d, e in zip(ent_assocs, dim, es)] + new_dofs = list(product(*e_dofs)) + dofs += new_dofs + dof_gens = "(" + "*".join([",".join(list(ent_assoc[d][e].keys())) for ent_assoc, d, e in zip(ent_assocs, dim, es)]) + ")" + self.entity_assocs[total_dim][self.ent_mapping[dim][es]] = {dof_gens: new_dofs} + self.entity_dofs[total_dim][self.ent_mapping[dim][es]] += [i + dof_counter for i in range(len(new_dofs))] + for d in new_dofs: + self.dof_ids[d] = dof_counter + dof_counter += 1 + ent_counter[total_dim] += 1 + + return dofs def to_ufl(self): + ufl_sub_elements = [e.to_ufl() for e in self.sub_elements] if self.flat: - return FuseElement(self, self.cell.flatten().to_ufl()) - ufl_sub_elements = [e.to_ufl() for e in self.sub_elements()] - # self.setup_matrices() - # breakpoint() - return TensorProductElement(*ufl_sub_elements, cell=self.cell.to_ufl()) + return FuseElement(self, self.cell.to_ufl()) + return TensorProductElement(*ufl_sub_elements, cell=self.cell.to_ufl(), triple=self) + + def __add__(self, other): + # assert self.cell == other.cell + assert self.spaces[0].shape == other.spaces[0].shape + assert str(self.spaces[1]) == str(other.spaces[1]) + from fuse.enriched import EnrichedElement + return EnrichedElement(self, other, flat=self.flat and other.flat, + matrices=self.apply_matrices or other.apply_matrices) + + def flatten(self): + return TensorProductTriple(*self.factors, flat=True, symmetric=self.requested_symmetric, matrices=self.apply_matrices) + + def unflatten(self): + return TensorProductTriple(*self.factors, flat=False, symmetric=self.requested_symmetric, matrices=self.apply_matrices) + + +def compute_matrix_transform(trace, cell, o): + dim = cell.get_spatial_dimension() + bvs = np.array(cell.basis_vectors()) + new_bvs = np.array(cell.orient(~o).basis_vectors()) + if bvs.shape[0] != dim: + # basis_vectors() gives one vector per non-reference vertex, which + # only forms a square (invertible) basis for simplices (vertex + # count == dim + 1). For non-simplex cells (e.g. a quadrilateral + # face of a hex), the vectors from the reference vertex to its two + # adjacent vertices (the first `dim` entries) already form a valid + # basis; later entries are redundant (e.g. diagonals). + bvs = bvs[:dim] + new_bvs = new_bvs[:dim] + basis_change = np.matmul(new_bvs, np.linalg.inv(bvs)) + # if len(ent_dofs_ids) == basis_change.shape[0]: + # sub_mat = basis_change + # elif len(dof_gen_class[dim].g2.members()) == 2 and len(ent_dofs_ids) == 1: + # # equivalently g1 trivial + # sub_mat = trace.manipulate_basis(basis_change) + # else: + # case where value change is a restriction of the full transformation of the basis + value_change = trace(cell).manipulate_basis(basis_change) + # sub_mat = np.kron((~o).matrix_form(), value_change) + return value_change + + +class HDiv(TensorProductTriple): + + def __init__(self, tensor_element): + self.base_element = tensor_element + self.gem_transformer, self.mat_transformer = self.select_fuse_hdiv_transformer(tensor_element) + self.trace = TrHDiv + super(HDiv, self).__init__(*tensor_element.factors, flat=tensor_element.flat, symmetric=tensor_element.requested_symmetric, matrices=tensor_element.apply_matrices) + self.spaces = (self.spaces[0], CellHDiv(self.cell), self.spaces[2]) + + def to_ufl(self): + return HDivElement(super(HDiv, self).to_ufl(), transform=self.gem_transformer) + + def repr(self): + return "HDiv(" + super(HDiv, self).repr() + ")" + + def select_fuse_hdiv_transformer(self, element): + # Assume: something x interval + import gem + assert len(element.sub_elements) == 2 + assert element.sub_elements[1].cell.get_shape() == 1 + ks = tuple(fe.form_degree for fe in element.sub_elements) + dims = tuple(fe.cell.get_spatial_dimension() for fe in element.sub_elements) + transform = lambda cell, o: compute_matrix_transform(self.trace, cell, o) + if ks == (0, 1) and dims == (1, 1): + # Both factors are 1D intervals (2D quad case). Make the + # scalar value the right hand rule normal on the y-aligned + # edges. + cell = element.sub_elements[1].cell + bv = cell.basis_vectors()[0][0] + mats = lambda m_a, m_b, o: np.kron(transform(cell, o[1]) * m_a, m_b) + return lambda v: [gem.Product(gem.Literal(bv), v), gem.Zero()], mats + elif ks == (1, 0) and dims == (1, 1): + # Both factors are 1D intervals (2D quad case). Make the + # scalar value the upward-pointing normal on the x-aligned + # edges. + cell = element.sub_elements[0].cell + bv = cell.basis_vectors()[0][0] + return lambda v: [gem.Zero(), gem.Product(gem.Literal(bv), v)], lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[0]) * m_b) + elif ks == (2, 0) and dims == (2, 1): + # First factor is a plain (unwrapped) scalar DG element on a + # 2D base cell, second is a CG interval + cell = element.sub_elements[0].cell + mats = lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[0]) * m_b) + return lambda v: [gem.Zero(), gem.Zero(), v], mats + elif ks == (1, 1) and dims == (2, 1) and str(element.sub_elements[0].spaces[1]) == "HDiv": + # First factor is an already H(div)-wrapped 2D element (the + # in-plane RT part), second is a DG interval: the horizontal + # (x, y) components of a 3D H(div) field. + cell = element.sub_elements[1].cell + mats = lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[1]) * m_b) + return lambda v: [gem.Indexed(v, (0,)), gem.Indexed(v, (1,)), gem.Zero()], mats + elif ks == (1, 1) and dims == (2, 1) and str(element.sub_elements[0].spaces[1]) == "HCurl": + # First factor is an already H(curl)-wrapped 2D element, + # second is a DG interval: rotate the tangential 2-vector 90 + # degrees anticlockwise into a 3-vector and pad. + cell = element.sub_elements[1].cell + mats = lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[1]) * m_b) + return lambda v: [gem.Indexed(v, (1,)), gem.Product(gem.Literal(-1), gem.Indexed(v, (0,))), gem.Zero()], mats + else: + raise NotImplementedError("Unexpected original mapping!") + assert False, "Unexpected form degree combination!" + + def flatten(self): + return HDiv(self.base_element.flatten()) + + def unflatten(self): + return HDiv(self.base_element.unflatten()) + + +class HCurl(TensorProductTriple): + + def __init__(self, tensor_element): + self.base_element = tensor_element + self.gem_transformer, self.mat_transformer = self.select_fuse_hcurl_transformer(tensor_element) + self.trace = TrHCurl + super(HCurl, self).__init__(*tensor_element.factors, flat=tensor_element.flat, symmetric=tensor_element.requested_symmetric, matrices=tensor_element.apply_matrices) + self.spaces = (self.spaces[0], CellHCurl(self.cell), self.spaces[2]) + + def to_ufl(self): + return HCurlElement(super(HCurl, self).to_ufl(), self.gem_transformer) + + def repr(self): + return "HCurl(" + super(HCurl, self).repr() + ")" + + def select_fuse_hcurl_transformer(self, element): + import gem + # Assume: something x interval + assert len(element.sub_elements) == 2 + assert element.sub_elements[1].cell.get_shape() == 1 + + dim = element.cell.get_spatial_dimension() + ks = tuple(fe.form_degree for fe in element.sub_elements) + dims = tuple(fe.cell.get_spatial_dimension() for fe in element.sub_elements) + transform = lambda cell, o: compute_matrix_transform(self.trace, cell, o) + if all(str(fe.spaces[1]) == "H1" or str(fe.spaces[1]) == "L2" for fe in element.sub_elements) and dims == (1, 1): # affine mapping, both factors 1D intervals (2D quad case) + if ks == (1, 0): + # Can only be 2D. Make the scalar value the + # tangential following the cell edge direction on the x-aligned edges. + cell = element.sub_elements[0].cell + bv = element.sub_elements[0].cell.basis_vectors()[0][0] + mats = lambda m_a, m_b, o: np.kron(transform(cell, o[0]) * m_a, m_b) + return lambda v: [gem.Product(gem.Literal(bv), v), gem.Zero()], mats + elif ks == (0, 1): + # Can be any spatial dimension. Make the scalar value the + # tangential following the cell edge direction . + cell = element.sub_elements[1].cell + bv = element.sub_elements[1].cell.basis_vectors()[0][0] + mats = lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[1]) * m_b) + return lambda v: [gem.Zero()] * (dim - 1) + [gem.Product(gem.Literal(bv), v)], mats + else: + assert False + elif ks == (1, 0) and dims == (2, 1) and str(element.sub_elements[0].spaces[1]) == "HCurl": + # First factor is an already H(curl)-wrapped 2D element (an + # in-plane tangential edge component), second is a CG interval + mats = lambda m_a, m_b, o: np.kron(m_a, m_b) + return lambda v: [gem.Indexed(v, (0,)), gem.Indexed(v, (1,)), gem.Zero()], mats + elif ks == (0, 1) and dims == (2, 1) and str(element.sub_elements[0].spaces[1]) == "H1": + # First factor is a plain (unwrapped) bilinear (Q1) scalar + # element on a 2D base cell, second is a DG interval + cell = element.sub_elements[1].cell + mats = lambda m_a, m_b, o: np.kron(m_a, transform(cell, o[1]) * m_b) + return lambda v: [gem.Zero(), gem.Zero(), v], mats + else: + raise NotImplementedError("Unexpected original mapping!") + assert False, "Unexpected original mapping!" def flatten(self): - return TensorProductTriple(self.A, self.B, flat=True) + return HCurl(self.base_element.flatten()) def unflatten(self): - return TensorProductTriple(self.A, self.B, flat=False) + return HCurl(self.base_element.unflatten()) diff --git a/fuse/triples.py b/fuse/triples.py index 89cf247..06839ee 100644 --- a/fuse/triples.py +++ b/fuse/triples.py @@ -6,6 +6,8 @@ from FIAT.dual_set import DualSet from FIAT.finite_element import CiarletElement from FIAT.reference_element import ufc_cell +from functools import cache +from itertools import product import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt @@ -13,7 +15,6 @@ import warnings import numpy as np import scipy -from functools import cache class ElementTriple(): @@ -37,6 +38,7 @@ def __init__(self, cell, spaces, dof_gen, perm=True): self.spaces = tuple(spaces) self.DOFGenerator = dof_gen self.flat = False + self.symmetric = True self.ref_el = None @@ -46,15 +48,15 @@ def __init__(self, cell, spaces, dof_gen, perm=True): def setup_ids_and_nodes(self): dofs = self.generate() - degree = self.spaces[0].degree() + 1 + degree = self.degree value_shape = self.get_value_shape() top = self.ref_el.get_topology() min_ids = self.cell.get_starter_ids() - entity_ids = {} + entity_dofs = {} nodes = [] for dim in sorted(top): - entity_ids[dim] = {i: [] for i in top[dim]} + entity_dofs[dim] = {i: [] for i in top[dim]} self.dof_id_to_fiat_id = {} entities = [(dim, entity) for dim in sorted(top) for entity in sorted(top[dim])] @@ -64,17 +66,17 @@ def setup_ids_and_nodes(self): for i in range(len(dofs)): if entity[1] == dofs[i].cell_defined_on.id - min_ids[dim]: self.dof_id_to_fiat_id[dofs[i].id] = counter - entity_ids[dim][dofs[i].cell_defined_on.id - min_ids[dim]].append(counter) + entity_dofs[dim][dofs[i].cell_defined_on.id - min_ids[dim]].append(counter) nodes.append(dofs[i].convert_to_fiat(self.ref_el, degree, value_shape)) counter += 1 self.nodes = nodes # for i in range(4): - # entity_ids[2][i] = [entity_ids[2][i][-1]] + entity_ids[2][i][:-1] - return entity_ids, nodes + # entity_dofs[2][i] = [entity_dofs[2][i][-1]] + entity_dofs[2][i][:-1] + return entity_dofs, nodes def setup_matrices(self): - # self.matrices_by_entity = self.make_entity_dense_matrices(self.ref_el, self.entity_ids, self.nodes, self.poly_set) - matrices, entity_perms, pure_perm = self.make_dof_perms(self.ref_el, self.entity_ids, self.nodes, self.poly_set) + # self.matrices_by_entity = self.make_entity_dense_matrices(self.ref_el, self.entity_dofs, self.nodes, self.poly_set) + matrices, entity_perms, pure_perm = self.make_dof_perms(self.ref_el, self.entity_dofs, self.nodes, self.poly_set) reversed_matrices = self.reverse_dof_perms(matrices) if self.perm: self.pure_perm = pure_perm @@ -87,7 +89,6 @@ def setup_matrices(self): self.apply_matrices = True self.entity_perms = entity_perms - self.entity_perms = None return matrices, reversed_matrices def __repr__(self): @@ -150,21 +151,28 @@ def to_ufl(self): # set up for eventual conversion to FIAT if not already done self.ref_el = self.cell.to_fiat() self.poly_set = self.spaces[0].to_ON_polynomial_set(self.ref_el) - self.entity_ids, self.nodes = self.setup_ids_and_nodes() + self.entity_dofs, self.nodes = self.setup_ids_and_nodes() self.matrices, self.reversed_matrices = self.setup_matrices() return FuseElement(self) def to_fiat(self): # call this to ensure set up is complete self.to_ufl() + if self.flat and not self.symmetric: + # A flattened cell's group contains axis permutations, so every + # orientation must be available. Only an element whose DOFs are + # closed under those permutations can supply them. + raise NotImplementedError( + "%r is not symmetric, so it cannot supply the axis-permuting " + "orientations a flattened cell requires" % (self,)) form_degree = self.form_degree degree = self.spaces[0].degree() # sanity check that the dofs span the space - original_V, original_basis = self.compute_dense_matrix(self.ref_el, self.entity_ids, self.nodes, self.poly_set) + original_V, original_basis = self.compute_dense_matrix(self.ref_el, self.entity_dofs, self.nodes, self.poly_set) if self.pure_perm: - dual = DualSet(self.nodes, self.ref_el, self.entity_ids, self.entity_perms) + dual = DualSet(self.nodes, self.ref_el, self.entity_dofs, self.entity_perms) else: - dual = DualSet(self.nodes, self.ref_el, self.entity_ids) + dual = DualSet(self.nodes, self.ref_el, self.entity_dofs) return CiarletElement(self.poly_set, dual, degree, form_degree) def to_tikz(self, show=True, scale=3): @@ -256,8 +264,8 @@ def plot(self, filename="temp.png"): else: raise ValueError("Plotting not supported in this dimension") - def compute_dense_matrix(self, ref_el, entity_ids, nodes, poly_set): - dual = DualSet(nodes, ref_el, entity_ids) + def compute_dense_matrix(self, ref_el, entity_dofs, nodes, poly_set): + dual = DualSet(nodes, ref_el, entity_dofs) old_coeffs = poly_set.get_coeffs() dualmat = dual.to_riesz(poly_set) @@ -277,7 +285,7 @@ def compute_dense_matrix(self, ref_el, entity_ids, nodes, poly_set): % (V.shape, np.linalg.matrix_rank(V))) return A, new_coeffs_flat - def make_entity_dense_matrices(self, ref_el, entity_ids, nodes, poly_set): + def make_entity_dense_matrices(self, ref_el, entity_dofs, nodes, poly_set): raise NotImplementedError("This should be deprecated") degree = self.spaces[0].degree() min_ids = self.cell.get_starter_ids() @@ -294,7 +302,7 @@ def make_entity_dense_matrices(self, ref_el, entity_ids, nodes, poly_set): # dof_ids = [self.dof_id_to_fiat_id[d.id] for d in self.generate() if d.cell_defined_on == e] dof_ids = [d.id for d in self.generate() if d.cell_defined_on == e] # res_dict[dim][e_id][0] = np.eye(len(dof_ids)) - original_V, original_basis = self.compute_dense_matrix(ref_el, entity_ids, nodes, poly_set) + original_V, original_basis = self.compute_dense_matrix(ref_el, entity_dofs, nodes, poly_set) for g in self.cell.group.members(): permuted_e, permuted_g = self.cell.permute_entities(g, dim)[e_id] @@ -309,7 +317,7 @@ def make_mat(perm_g): # , entity_o=perm_g new_nodes = [d(g, entity_o=perm_g).convert_to_fiat(ref_el, degree, self.get_value_shape()) if d.cell_defined_on == e else d.convert_to_fiat(ref_el, degree, self.get_value_shape()) for d in self.generate()] # new_nodes = [d(g).convert_to_fiat(ref_el, degree, self.get_value_shape()) if d.cell_defined_on == e else d.convert_to_fiat(ref_el, degree, self.get_value_shape()) for d in self.generate()] - transformed_V, transformed_basis = self.compute_dense_matrix(ref_el, entity_ids, new_nodes, poly_set) + transformed_V, transformed_basis = self.compute_dense_matrix(ref_el, entity_dofs, new_nodes, poly_set) return np.matmul(transformed_basis, original_V.T) temp = make_mat(permuted_g) # if dim == 1 and e_id == 0: @@ -326,7 +334,7 @@ def make_mat(perm_g): res_dict[dim][e_id][val] = temp[np.ix_(dof_ids, dof_ids)] return res_dict - def make_overall_dense_matrices(self, ref_el, entity_ids, nodes, poly_set): + def make_overall_dense_matrices(self, ref_el, entity_dofs, nodes, poly_set): raise NotImplementedError("this function should be unnecessary") min_ids = self.cell.get_starter_ids() dim = self.cell.dim() @@ -334,20 +342,19 @@ def make_overall_dense_matrices(self, ref_el, entity_ids, nodes, poly_set): e_id = e.id - min_ids[dim] res_dict = {dim: {e_id: {}}} degree = self.spaces[0].degree() - original_V, original_basis = self.compute_dense_matrix(ref_el, entity_ids, nodes, poly_set) + original_V, original_basis = self.compute_dense_matrix(ref_el, entity_dofs, nodes, poly_set) for g in self.cell.group.members(): val = g.numeric_rep() if g.perm.is_Identity: res_dict[dim][e_id][val] = np.eye(len(nodes)) else: new_nodes = [d(g).convert_to_fiat(ref_el, degree, self.get_value_shape()) for d in self.generate()] - transformed_V, transformed_basis = self.compute_dense_matrix(ref_el, entity_ids, new_nodes, poly_set) + transformed_V, transformed_basis = self.compute_dense_matrix(ref_el, entity_dofs, new_nodes, poly_set) res_dict[dim][e_id][val] = np.matmul(transformed_basis, original_V.T) return res_dict - def _entity_associations(self, dofs): - min_ids = self.cell.get_starter_ids() - entity_associations = {dim: {e.id - min_ids[dim]: {} for e in self.cell.d_entities(dim)} + def _entity_associations(self, dofs, overall=True): + entity_associations = {dim: {i: {} for i, e in enumerate(self.cell.d_entities(dim))} for dim in range(self.cell.dim() + 1)} cell_dim = self.cell.dim() cell_dict = entity_associations[cell_dim][0] @@ -358,8 +365,13 @@ def _entity_associations(self, dofs): # construct mapping of entities to the dof generators and the dofs they generate for d in dofs: sub_dim = d.cell_defined_on.dim() - sub_dict = entity_associations[sub_dim][d.cell_defined_on.id - min_ids[sub_dim]] - for dim in set([sub_dim, cell_dim]): + cell_defined_on_id = self.cell.d_entities_ids(sub_dim).index(d.cell_defined_on.id) + sub_dict = entity_associations[sub_dim][cell_defined_on_id] + if overall: + dims = set([sub_dim, cell_dim]) + else: + dims = [sub_dim] + for dim in dims: dof_gen = str(d.generation[dim]) num_dofs[dof_gen] = (dim, d.generation[dim].g1.size()) @@ -373,7 +385,6 @@ def _entity_associations(self, dofs): sub_dict[dof_gen] += [d] elif dim < cell_dim or not d.immersed: sub_dict[dof_gen] = [d] - if dof_gen in cell_dict.keys() and dim == cell_dim and d.immersed: cell_dict[dof_gen] += [d] elif dim == cell_dim and d.immersed: @@ -381,17 +392,23 @@ def _entity_associations(self, dofs): return entity_associations, pure_perm, sub_pure_perm - def _initialise_entity_dicts(self, dofs): - min_ids = self.cell.get_starter_ids() + def _initialise_entity_dicts(self, dofs, tensor=False): + # min_ids = self.cell.get_starter_ids() dof_id_mat = np.eye(len(dofs)) oriented_mats_by_entity = {} flat_by_entity = {} - for dim in range(self.cell.dim() + 1): + cell = self.cell + if tensor: + dims = list(product(*(f.dimensions() for f in cell.factors))) + else: + dims = [i for i in range(cell.dimension + 1)] + for dim in dims: oriented_mats_by_entity[dim] = {} flat_by_entity[dim] = {} - ents = self.cell.d_entities(dim) - for e in ents: - e_id = e.id - min_ids[dim] + + ents = cell.d_entities(dim) + for e_id, e in enumerate(ents): + # old_e_id = e.id - min_ids[dim] members = e.group.members() oriented_mats_by_entity[dim][e_id] = {} flat_by_entity[dim][e_id] = {} @@ -402,22 +419,20 @@ def _initialise_entity_dicts(self, dofs): flat_by_entity[dim][e_id][val] = [] return oriented_mats_by_entity, flat_by_entity - def make_dof_perms(self, ref_el, entity_ids, nodes, poly_set): + def make_dof_perms(self, ref_el, entity_dofs, nodes, poly_set): dofs = self.generate() - min_ids = self.cell.get_starter_ids() entity_associations, pure_perm, sub_pure_perm = self._entity_associations(dofs) # if pure_perm is False: # #TODO think about where this call goes # return self.matrices_by_entity, None, pure_perm - # return self.make_overall_dense_matrices(ref_el, entity_ids, nodes, poly_set), None, pure_perm + # return self.make_overall_dense_matrices(ref_el, entity_dofs, nodes, poly_set), None, pure_perm oriented_mats_by_entity, flat_by_entity = self._initialise_entity_dicts(dofs) # for each entity, look up generation on that entity and permute the # dof mapping according to the generation for dim in range(self.cell.dim() + 1): ents = self.cell.d_entities(dim) - for e in ents: - e_id = e.id - min_ids[dim] + for e_id, e in enumerate(ents): members = e.group.members() for g in members: val = g.numeric_rep() @@ -497,13 +512,26 @@ def make_dof_perms(self, ref_el, entity_ids, nodes, poly_set): # Interior matrices for tetrahedrons are tricky - and they don't matter unless you're in 4d warnings.warn("Interior Matrices in 3d not implemented, but are not needed.") oriented_mats_by_entity[dim][e_id][val][np.ix_(ent_dofs_ids, ent_dofs_ids)] = np.eye(len(ent_dofs_ids)) - else: - # TODO what if an orientation is not in G1 - warnings.warn("FUSE: orientation case not covered") - # sub_mat = g.matrix_form() - # oriented_mats_by_entity[dim][e_id][val][np.ix_(ent_dofs_ids, ent_dofs_ids)] = sub_mat.copy() - # raise NotImplementedError(f"Orientation {g} is not in group {dof_gen_class[dim].g1.members()}") + elif len(dof_gen_class.keys()) == 2 and dim == self.cell.dim(): + # Immersed DOFs revisited at the cell's own top-level entity: the + # matrix for this (dim, e_id, val) block is unconditionally + # recomputed by the immersion-handling block below, so there is + # nothing to do here. pass + elif len(dof_gen_class.keys()) == 1 and dim == self.cell.dim(): + # Non-immersed DOFs defined directly on the cell interior, where the + # dof count doesn't match the vertex count (e.g. interior dofs of + # higher-degree 3d elements). These dofs are never shared with a + # neighbouring cell, so no cross-cell orientation matching is + # required and the identity set by _initialise_entity_dicts is + # correct as-is. + warnings.warn("Interior Matrices in 3d not implemented, but are not needed.") + else: + raise NotImplementedError( + f"Orientation {g} on entity dim {dim} is not covered: " + f"dof_gen_class keys={list(dof_gen_class.keys())}, " + f"ndofs={len(ent_dofs_ids)}, nverts={len(self.cell.vertices())}" + ) if len(dof_gen_class.keys()) == 2 and dim == self.cell.dim(): # Handle immersion - can only happen once so number of keys is max 2 dimensions = list(dof_gen_class.keys()) @@ -515,7 +543,7 @@ def make_dof_perms(self, ref_el, entity_ids, nodes, poly_set): g_sub_mat = perm_list_to_matrix(identity, [sub_e for sub_e, _ in permuted_ents]) for sub_e, sub_g in permuted_ents: sub_e = self.cell.get_node(sub_e) - sub_e_id = sub_e.id - min_ids[sub_e.dim()] + sub_e_id = self.cell.d_entities(sub_e.dim(), get_class=False).index(sub_e.id) sub_ent_ids = [] for (k, v) in entity_associations[immersed_dim][sub_e_id].items(): sub_ent_ids += [self.dof_id_to_fiat_id[e.id] for e in v] @@ -530,7 +558,7 @@ def make_dof_perms(self, ref_el, entity_ids, nodes, poly_set): oriented_mats_overall = oriented_mats_by_entity[dim][0] if pure_perm and sub_pure_perm: for val, mat in oriented_mats_overall.items(): - cell_dofs = entity_ids[dim][0] + cell_dofs = entity_dofs[dim][0] flat_by_entity[dim][e_id][val] = perm_matrix_to_perm_array(mat[np.ix_(cell_dofs, cell_dofs)]) return oriented_mats_by_entity, flat_by_entity, True @@ -560,13 +588,14 @@ def orient_mat_perms(self): num_ents += len(ents) def reverse_dof_perms(self, matrices): - min_ids = self.cell.get_starter_ids() reversed_mats = {} + cell = self.cell + # if isinstance(cell, TensorProductPoint)and cell.flat: + # cell = self.unflat_cell for dim in matrices.keys(): reversed_mats[dim] = {} - ents = self.cell.d_entities(dim) - for e in ents: - e_id = e.id - min_ids[dim] + ents = cell.d_entities(dim) + for e_id, e in enumerate(ents): perms_copy = matrices[dim][e_id].copy() members = e.group.members() for m in members: @@ -584,6 +613,34 @@ def reverse_dof_perms(self, matrices): reversed_mats[dim][e_id] = perms_copy return reversed_mats + def generation_order_matrices(self): + """Orientation matrices indexed to match ``self.entity_dofs``. + + Equal to ``self.matrices`` unless a subclass has reindexed those into + Firedrake's dimension-grouped closure order, in which case the + generation-order copy taken before reindexing is returned. + """ + return getattr(self, "_gen_order_matrices", self.matrices) + + def __add__(self, other): + """ Construct a new element triple by combining the degrees of freedom + This implementation does not make assertions about the properties + of the resulting element. + + Elements being adding must be defined over the same cell and have the same + value shape and mapping""" + assert self.cell == other.cell + assert self.spaces[0].shape == other.spaces[0].shape + assert str(self.spaces[1]) == str(other.spaces[1]) + + spaces = (self.spaces[0] + other.spaces[0], self.spaces[1], max([self.spaces[2], other.spaces[2]])) + + from fuse.tensor_products import TensorProductTriple + if isinstance(other, TensorProductTriple): + return other + self + + return ElementTriple(self.cell, spaces, self.DOFGenerator + other.DOFGenerator) + def _to_dict(self): o_dict = {"cell": self.cell, "spaces": self.spaces, "dofs": self.DOFGenerator} return o_dict @@ -621,38 +678,37 @@ def num_dofs(self): return self.dof_numbers def generate(self, cell, space, id_counter): - if self.ls is None: - self.ls = [] - for l_g in self.x: - i = 0 - for g in self.g1.members(): - generated = l_g(g) - if not isinstance(generated, list): - generated = [generated] - for dof in generated: - dof.add_context(self, cell, space, g, id_counter, i) - id_counter += 1 - i += 1 - self.ls.extend(generated) - self.dof_numbers = len(self.ls) - self.dof_ids = [dof.id for dof in self.ls] + self.ls = [] + for l_g in self.x: + i = 0 + for g in self.g1.members(): + generated = l_g(g) + if not isinstance(generated, list): + generated = [generated] + for dof in generated: + dof.add_context(self, cell, space, g, id_counter, i) + id_counter += 1 + i += 1 + self.ls.extend(generated) + self.dof_numbers = len(self.ls) + self.dof_ids = [dof.id for dof in self.ls] return self.ls - def make_entity_ids(self): + def make_entity_dofs(self): dofs = self.ls - entity_ids = {} + entity_dofs = {} min_ids = dofs[0].cell.get_starter_ids() top = dofs[0].cell.get_topology() for dim in sorted(top): - entity_ids[dim] = {i: [] for i in top[dim]} + entity_dofs[dim] = {i: [] for i in top[dim]} for i in range(len(dofs)): entity = dofs[i].cell_defined_on dim = entity.dim() - entity_ids[dim][entity.id - min_ids[dim]].append(i) - return entity_ids + entity_dofs[dim][entity.id - min_ids[dim]].append(i) + return entity_dofs def __repr__(self): repr_str = "DOFGen(" diff --git a/fuse/utils.py b/fuse/utils.py index d57f690..645386e 100644 --- a/fuse/utils.py +++ b/fuse/utils.py @@ -2,6 +2,8 @@ import sympy as sp import math +_SYMBOLS = tuple(sp.Symbol(s) for s in ("x", "y", "z")) + def fold_reduce(func_list, *prev): """ @@ -29,7 +31,7 @@ def sympy_to_numpy(array, symbols, values): """ substituted = array.subs({symbols[i]: values[i] for i in range(len(values))}) - if len(array.atoms(sp.Symbol)) == len(values) and all(not isinstance(v, sp.Expr) for v in values): + if len(array.atoms(sp.Symbol)) <= len(values) and all(not isinstance(v, sp.Expr) for v in values): nparray = np.array(substituted).astype(np.float64) if len(nparray.shape) > 1: @@ -47,9 +49,9 @@ def tabulate_sympy(expr, pts): # expr: sp matrix expression in x,y,z for components of R^d # pts: n values in R^d # returns: evaluation of expr at pts - res = np.array(pts) + res = np.zeros((pts.shape[0],) + (expr.shape[-1],)) i = 0 - syms = ["x", "y", "z"] + syms = _SYMBOLS for pt in pts: if not hasattr(pt, "__iter__"): pt = (pt,) @@ -57,16 +59,20 @@ def tabulate_sympy(expr, pts): subbed = np.array(subbed).astype(np.float64) res[i] = subbed[0] i += 1 - final = res.squeeze() - return final + # final = res.squeeze() + return res -def max_deg_sp_mat(sp_mat): +def max_deg_sp_expr(sp_expr): degs = [] - for comp in sp_mat: - # only compute degree if component is a polynomial - if sp.sympify(comp).as_poly(): - degs += [sp.sympify(comp).as_poly().degree()] + if isinstance(sp_expr, sp.Matrix): + for comp in sp_expr: + # only compute degree if component is a polynomial + if sp.sympify(comp).as_poly(): + degs += [sp.sympify(comp).as_poly().total_degree()] + else: + if sp.sympify(sp_expr).as_poly(): + degs += [sp.sympify(sp_expr).as_poly().total_degree()] return max(degs) @@ -101,3 +107,32 @@ def orientation_value(identity_arg, perm_arg): identity.remove(perm[i]) val += loc * math.factorial(len(perm) - i - 1) return val + + +def lehmer_rank(perm): + """Rank of ``perm`` within ``sorted(permutations(range(len(perm))))``.""" + return orientation_value(list(range(len(perm))), list(perm)) + + +def canonical_tensor_orientation_key(axis_perm, flips, d): + """Canonical FIAT/dmcommon orientation key for an interval-product entity. + + ``o = (2**d) * lehmer_rank(axis_perm) + sum_i flips[i] * 2**(d - 1 - i)`` + + ``axis_perm`` is a permutation of ``range(d)`` sending input axis ``i`` to + output axis ``axis_perm[i]``; ``flips[i]`` in ``{0, 1}`` marks a reflection + of axis ``i``. This matches FIAT's + ``make_entity_permutations_tensorproduct``, whose tuple keys + ``(eo, o_1, ..., o_d)`` flatten to this same integer, and the numbering + consumed by Firedrake's ``dmcommon`` tensor-product orientation switch. + """ + io = sum(int(flips[i]) * 2 ** (d - 1 - i) for i in range(d)) + return (2 ** d) * lehmer_rank(axis_perm) + io + + +def as_tuple(expr): + if isinstance(expr, tuple): + return expr + if isinstance(expr, list): + return tuple(expr) + return (expr,) diff --git a/pyproject.toml b/pyproject.toml index 72e43e7..50d09f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,9 +60,10 @@ testpaths = [ ] markers = [ "smoke: fast tests that do not require Firedrake (see conftest.SMOKE_MODULES)", + "slow: builds that take minutes (deselect with '-m \"not slow\"')", ] [tool.coverage.run] include=[ "fuse/*", -] \ No newline at end of file +] diff --git a/test/test_2d_examples_docs.py b/test/test_2d_examples_docs.py index 4fb514a..f490452 100644 --- a/test/test_2d_examples_docs.py +++ b/test/test_2d_examples_docs.py @@ -1,6 +1,7 @@ from fuse import * import sympy as sp import numpy as np +import pytest np.set_printoptions(legacy="1.25") @@ -30,6 +31,33 @@ def construct_dg1(): return dg1 +def construct_dg0_integral(edge=None): + if not edge: + edge = Point(1, [Point(0), Point(0)], vertex_num=2) + xs = [DOF(L2Pairing(), VectorKernel(0.5))] + dg0 = ElementTriple(edge, (P0, CellL2, C0), DOFGenerator(xs, S1, S1)) + return dg0 + + +def construct_dg1_integral(cell=None): + edge = Point(1, [Point(0), Point(0)], vertex_num=2) + x = sp.Symbol("x") + xs = [DOF(L2Pairing(), PolynomialKernel((1/2)*(x + 1), symbols=(x,)))] + dg1 = ElementTriple(edge, (P1, CellL2, C0), DOFGenerator(xs, S2, S1)) + return dg1 + + +def construct_dg2_integral(cell=None): + edge = Point(1, [Point(0), Point(0)], vertex_num=2) + x = sp.Symbol("x") + xs = [DOF(L2Pairing(), PolynomialKernel((x/2)*(x + 1), symbols=(x,)))] + centre = [DOF(L2Pairing(), PolynomialKernel((1 - x**2), symbols=(x,)))] + + dofs = [DOFGenerator(xs, S2, S1), DOFGenerator(centre, S1, S1)] + dg2 = ElementTriple(edge, (PolynomialSpace(2), CellL2, C0), dofs) + return dg2 + + def plot_dg1(): dg1 = construct_dg1() dg1.plot() @@ -78,9 +106,11 @@ def test_dg_examples(): assert any(np.isclose(val, dof.eval(test_func)) for val in dof_vals) -def construct_cg1(): +def construct_cg1(edge=None): + # [test_cg1 0] - edge = Point(1, [Point(0), Point(0)], vertex_num=2) + if not edge: + edge = Point(1, [Point(0), Point(0)], vertex_num=2) vert = edge.vertices()[0] xs = [DOF(DeltaPairing(), PointKernel(()))] @@ -345,6 +375,7 @@ def test_nd_example(): for dof in ned.generate(): assert [np.allclose(1, dof.eval(basis_func).flatten()) for basis_func in basis_funcs].count(True) == 1 assert [np.allclose(0, dof.eval(basis_func).flatten()) for basis_func in basis_funcs].count(True) == 2 + ned.to_fiat() def construct_rt(tri=None): @@ -393,6 +424,14 @@ def test_rt_example(): rt.to_fiat() +@pytest.mark.parametrize(["triple", "expected"], [(construct_dg1_integral(), 1), + (construct_cg1(), 0), + (construct_rt(), 1), + (construct_dg1_tri(), 2)]) +def test_form_degree(triple, expected): + assert triple.form_degree == expected + + def construct_hermite(): tri = polygon(3) vert = tri.vertices()[0] diff --git a/test/test_3d_examples_docs.py b/test/test_3d_examples_docs.py index 43d1983..fbb3bfa 100644 --- a/test/test_3d_examples_docs.py +++ b/test/test_3d_examples_docs.py @@ -680,11 +680,11 @@ def test_tet_nd(): def construct_V(elem): nodes = elem.nodes ref_el = elem.ref_el - entity_ids = elem.entity_ids + entity_dofs = elem.entity_dofs poly_set = elem.poly_set from FIAT.dual_set import DualSet - dual = DualSet(nodes, ref_el, entity_ids) + dual = DualSet(nodes, ref_el, entity_dofs) old_coeffs = poly_set.get_coeffs() dualmat = dual.to_riesz(poly_set) diff --git a/test/test_algebra.py b/test/test_algebra.py new file mode 100644 index 0000000..5f0c9a7 --- /dev/null +++ b/test/test_algebra.py @@ -0,0 +1,36 @@ +from fuse import * +from firedrake import * +import numpy as np +import sympy as sp +from test_convert_to_fiat import create_cg2_tri, construct_cg3 + + +def construct_bubble(cell=None): + if cell is None: + cell = polygon(3) + x = sp.Symbol("x") + y = sp.Symbol("y") + f = (3*np.sqrt(3)/4)*(y + np.sqrt(3)/3)*(np.sqrt(3)*x + y - 2*np.sqrt(3)/3)*(-np.sqrt(3)*x + y - 2*np.sqrt(3)/3) + space = PolynomialSpace(3).restrict(0, 0)*f + xs = [DOF(DeltaPairing(), PointKernel((0, 0)))] + bubble = ElementTriple(cell, (space, CellL2, L2), DOFGenerator(xs, S1, S1)) + return bubble + + +def test_bubble(): + mesh = UnitTriangleMesh(use_fuse=True) + x = SpatialCoordinate(mesh) + + tri = polygon(3) + bub = construct_bubble(tri) + cg2 = create_cg2_tri(tri) + p2b3 = bub + cg2 + V = FunctionSpace(mesh, p2b3.to_ufl()) + W = FunctionSpace(mesh, construct_cg3().to_ufl()) + + bubble_func = 27*x[0]*x[1]*(1-x[0]-x[1]) + u = project(bubble_func, V) + exact = Function(W) + exact.interpolate(bubble_func, W) + # make sure that these are the same + assert sqrt(assemble((u-exact)*(u-exact)*dx)) < 1e-14 diff --git a/test/test_cells.py b/test/test_cells.py index f367fa7..2f5bc76 100644 --- a/test/test_cells.py +++ b/test/test_cells.py @@ -4,11 +4,12 @@ from fuse.cells import ufc_triangle, ufc_tetrahedron import pytest import numpy as np +import sympy as sp from FIAT.reference_element import default_simplex, ufc_simplex, Simplex from test_convert_to_fiat import helmholtz_solve -@pytest.fixture(scope='module', params=[0, 1, 2]) +@pytest.fixture(scope='module', params=[0, 1, 2, 3]) def C(request): dim = request.param if dim == 0: @@ -17,6 +18,8 @@ def C(request): return Point(1, [Point(0), Point(0)], vertex_num=2) elif dim == 2: return polygon(3) + elif dim == 3: + return make_tetrahedron() def test_vertices(C): @@ -197,6 +200,16 @@ def test_comparison(): # print(tensor_product1 >= tensor_product1) +def test_self_equality(C): + assert C == C + + +@pytest.mark.parametrize(["A", "B", "res"], [(ufc_triangle(), polygon(3), False), + (line(), line(), True),]) +def test_equivalence(A, B, res): + assert A.equivalent(B) == res + + @pytest.mark.parametrize(["cell"], [(ufc_triangle(),), (polygon(3),)]) def test_connectivity(cell): cell = cell.to_fiat() @@ -350,3 +363,19 @@ def test_tet_groups(): print() print([s.array_form for s in sub_group]) print([s.array_form for s in flip_group]) + + +@pytest.mark.parametrize("attachment", [(sp.Integer(-1), sp.Symbol("x")), + (sp.Symbol("x"), sp.Integer(-1)), + (sp.Integer(-1), sp.Integer(1))]) +def test_edge_components_evaluate_numerically(attachment): + """Components without symbols must evaluate like the ones that have them. + + Attachments mixing constant and symbolic components previously returned a + mix of floats and sympy objects, which numpy cannot compare. + """ + edge = Edge(Point(0), attachment=attachment) + res = edge(0.5) + expected = tuple(float(c.subs({sp.Symbol("x"): 0.5})) for c in attachment) + assert res == expected + assert all(not isinstance(v, sp.Expr) for v in res) diff --git a/test/test_construction.py b/test/test_construction.py index 3dbd864..abe2d8c 100644 --- a/test/test_construction.py +++ b/test/test_construction.py @@ -19,6 +19,150 @@ def test_construction3d(col, k, deg): elem.to_fiat() +quad_params = [(2, k, deg) for deg in list(range(1, 4)) for k in [0, 1, 2, 3]] + + +@pytest.mark.parametrize("col,k,deg", quad_params) +def test_construction_quad(col, k, deg): + elem = periodic_table(col, 2, k, deg) + mesh = UnitSquareMesh(2, 2, quadrilateral=True, use_fuse=True) + FunctionSpace(mesh, elem.to_ufl()) + + +hex_params = [(2, k, deg) for deg in list(range(1, 3)) for k in [0, 1, 2, 3]] + + +@pytest.mark.parametrize("col,k,deg", hex_params) +def test_construction_hex(col, k, deg): + elem = periodic_table(col, 3, k, deg) + mesh = UnitCubeMesh(2, 2, 2, hexahedral=True, use_fuse=True) + FunctionSpace(mesh, elem.to_ufl()) + + +def project_only(V, mesh, expr): + f = assemble(project(expr, V)) + out = Function(V) + u = TrialFunction(V) + v = TestFunction(V) + a = inner(u, v)*dx + L = inner(f, v)*dx + solve(a == L, out) + return sqrt(assemble(dot(out - expr, out - expr) * dx)) + + +cg_quad_params = [(2, 0, deg, deg + 0.75) for deg in list(range(1, 4))] +dg_quad_params = [(2, 3, deg, deg + 0.75) for deg in list(range(0, 3))] + + +@pytest.mark.parametrize("col,k,deg,conv_rate", cg_quad_params + dg_quad_params) +def test_convergence_quad(col, k, deg, conv_rate): + elem = periodic_table(col, 2, k, deg) + scale_range = range(3, 6) + diff_inte = [0 for i in scale_range] + for n in scale_range: + mesh = UnitSquareMesh(2**n, 2**n, quadrilateral=True, use_fuse=True) + + V = FunctionSpace(mesh, elem.to_ufl()) + x, y = SpatialCoordinate(mesh) + expr = cos(x*pi*2)*sin(y*pi*2) + _, exact = get_expression(V) + _, diff_inte[n-min(scale_range)] = interpolate_vs_project(V, expr, exact) + + print("interpolation l2 error norms:", diff_inte) + diff_inte = np.array(diff_inte) + conv = np.log2(diff_inte[:-1] / diff_inte[1:]) + print("convergence order:", conv) + assert all([c > conv_rate for c in conv]) + + +nd_quad_params = [(2, 1, deg, deg - 0.2) for deg in list(range(1, 4))] +rt_quad_params = [(2, 2, deg, deg - 0.2) for deg in list(range(1, 4))] + + +@pytest.mark.parametrize("col,k,deg,conv_rate", nd_quad_params + rt_quad_params) +def test_convergence_quad_vec(col, k, deg, conv_rate): + elem = periodic_table(col, 2, k, deg) + scale_range = range(3, 6) + diff_proj = [0 for i in scale_range] + for n in scale_range: + mesh = UnitSquareMesh(2**n, 2**n, quadrilateral=True, use_fuse=True) + + V = FunctionSpace(mesh, elem.to_ufl()) + x, y = SpatialCoordinate(mesh) + expr = as_vector([cos(x*pi*2)*sin(y*pi*2), cos(x*pi*2)*sin(y*pi*2)]) + diff_proj[n-min(scale_range)] = project_only(V, mesh, expr) + + print("projection l2 error norms:", diff_proj) + diff_proj = np.array(diff_proj) + conv = np.log2(diff_proj[:-1] / diff_proj[1:]) + print("convergence order:", conv) + assert all([c > conv_rate for c in conv]) + + +cg_hex_params = [(2, 0, deg, deg + 0.75) for deg in list(range(1, 3))] +dg_hex_params = [(2, 3, deg, deg + 0.75) for deg in list(range(0, 3))] + + +@pytest.mark.parametrize("col,k,deg,conv_rate", cg_hex_params + dg_hex_params) +def test_convergence_hex(col, k, deg, conv_rate): + elem = periodic_table(col, 3, k, deg) + + scale_range = range(2, 4) + diff_proj = [0 for i in scale_range] + for n in scale_range: + mesh = UnitCubeMesh(2**n, 2**n, 2**n, hexahedral=True, use_fuse=True) + + V = FunctionSpace(mesh, elem.to_ufl()) + x, y, z = SpatialCoordinate(mesh) + expr = cos(x*pi*2)*sin(y*pi*2) + diff_proj[n-min(scale_range)] = project_test(V, mesh, expr) + + print("projection l2 error norms:", diff_proj) + diff_proj = np.array(diff_proj) + conv1 = np.log2(diff_proj[:-1] / diff_proj[1:]) + print("convergence order:", conv1) + assert all([c > conv_rate for c in conv1]) + + +nd_hex_params = [(2, 1, deg, deg - 0.2) for deg in list(range(1, 3))] +rt_hex_params = [(2, 2, deg, deg - 0.2) for deg in list(range(1, 3))] + + +@pytest.mark.parametrize("col,k,deg,conv_rate", nd_hex_params + rt_hex_params) +def test_convergence_hex_vec(col, k, deg, conv_rate): + elem = periodic_table(col, 3, k, deg) + + scale_range = range(2, 4) + diff_proj = [0 for i in scale_range] + for n in scale_range: + mesh = UnitCubeMesh(2**n, 2**n, 2**n, hexahedral=True, use_fuse=True) + + V = FunctionSpace(mesh, elem.to_ufl()) + x, y, z = SpatialCoordinate(mesh) + expr = as_vector([cos(x*pi*2)*sin(y*pi*2)]*3) + diff_proj[n-min(scale_range)] = project_only(V, mesh, expr) + + print("projection l2 error norms:", diff_proj) + diff_proj = np.array(diff_proj) + conv1 = np.log2(diff_proj[:-1] / diff_proj[1:]) + print("convergence order:", conv1) + assert all([c > conv_rate for c in conv1]) + + +@pytest.mark.parametrize("k", [1, 2]) +def test_hex_orientation_consistency(k): + f_vec = as_vector((2, 3, 5)) + mesh = UnitCubeMesh(3, 3, 3, hexahedral=True, use_fuse=True) + elem = periodic_table(2, 3, k, 2) + V = FunctionSpace(mesh, elem.to_ufl()) + u = TrialFunction(V) + v = TestFunction(V) + sol = Function(V) + solve(inner(u, v) * dx == inner(f_vec, v) * dx, sol) + error = sqrt(assemble(dot(sol - f_vec, sol - f_vec) * dx)) + assert error < 1e-10 + + cg_params = [(0, 0, deg, deg + 0.75) for deg in list(range(1, 7))] + [(1, 0, deg, deg + 0.75) for deg in list(range(1, 3))] nd_params = [(0, 1, deg, deg - 0.2) for deg in list(range(1, 7))] rt_params = [(0, 2, deg, deg - 0.2) for deg in list(range(1, 7))] @@ -124,28 +268,32 @@ def test_polynomial_poisson_solve(deg): assert np.allclose(res, 0) -# def test_plane(): -# from fuse import make_tetrahedron -# cell = make_tetrahedron() -# verts = cell.ordered_vertex_coords() -# res = check_below_plane(verts[1], verts[2], verts[3], (verts[1] + verts[2] + verts[3])/3) -# print(res) - - -# def test_check_line(): -# from fuse import polygon -# cell = polygon(3) -# verts = np.array(sorted(cell.ordered_vertex_coords())) -# midpoint = (verts[1] + verts[2])/2 -# midpoint1 = (verts[0] + verts[2])/2 -# assert check_below_line(verts[0], midpoint, (0, 0)) == 0 -# assert check_on_line(verts[0], midpoint, (0, 0)) -# assert check_on_line(verts[1], verts[2], midpoint) -# assert not check_on_line(verts[1], verts[2], midpoint1) - -# assert check_below_line(verts[0], midpoint, (-0.5, 0)) == -1 -# assert check_below_line(verts[0], midpoint, (0, -0.5)) == 1 +def test_ned3(): + nd3_pt = periodic_table(1, 3, 1, 3) + pt_gen = nd3_pt.DOFGenerator[1].x[0].triple.DOFGenerator[0].g1.members() + nd3_pt.to_fiat() + from test_3d_examples_docs import construct_tet_ned_2nd_kind_3 + nd3_mn = construct_tet_ned_2nd_kind_3() + mn_gen = nd3_mn.DOFGenerator[1].x[0].triple.DOFGenerator[0].g1.members() + nd3_mn.to_fiat() -# assert check_below_line(verts[1], midpoint1, verts[0]) == 1 + print([nd3_pt.dofs[i].id for i in range(24, 30)]) + print([nd3_mn.dofs[i].id for i in range(24, 30)]) + # for i in range(24, 30): + # print(i) + # print(pt_gen[i - 24], nd3_pt.dofs[i]) + # print(mn_gen[i - 24], nd3_mn.dofs[i]) -# test_construction3d(1,3, 2) + def permute_face(elem, o): + dof_ids = [d.id for d in elem.dofs] + transform_mat = elem.matrices[2][0][o.numeric_rep()] + transformed = np.matmul(transform_mat, dof_ids) + return transformed[24:30] + for o in mn_gen: + print(o) + print(o.numeric_rep()) + for o in pt_gen: + print(o) + print(o.numeric_rep()) + # print(permute_face(nd3_pt, o)) + # print(permute_face(nd3_mn, o)) diff --git a/test/test_convert_to_fiat.py b/test/test_convert_to_fiat.py index dd203f3..c844e19 100644 --- a/test/test_convert_to_fiat.py +++ b/test/test_convert_to_fiat.py @@ -7,7 +7,7 @@ from finat.ufl import CellBackend from sympy.combinatorics import Permutation from FIAT.quadrature_schemes import create_quadrature -from test_2d_examples_docs import construct_cg1, construct_nd, construct_rt, construct_cg3 +from test_2d_examples_docs import construct_cg1, construct_nd, construct_rt, construct_cg3, construct_dg0_integral, construct_dg1_integral, construct_dg2_integral from test_3d_examples_docs import (construct_tet_rt, construct_tet_rt2, construct_tet_rt3, construct_tet_ned, construct_tet_ned_2nd_kind, construct_tet_ned_2nd_kind_2, construct_tet_ned_2nd_kind_2_non_bary, @@ -116,15 +116,13 @@ def create_cg1(cell): def create_cg1_quad(): deg = 1 - cell = polygon(4) - # cell = constructCellComplex("quadrilateral").cell_complex - - vert_dg = create_dg0(cell.vertices()[0]) + cell = TensorProductPoint(line(), line()).flatten() + print(cell, type(cell)) + vert_dg = create_dg1(cell.vertices()[0]) xs = [immerse(cell, vert_dg, TrH1)] Pk = PolynomialSpace(deg, deg + 1) cg = ElementTriple(cell, (Pk, C0, Fid), DOFGenerator(xs, get_cyc_group(len(cell.vertices())), S1)) - return cg @@ -149,6 +147,8 @@ def create_cg1_flipped(cell): def create_cg2(cell=None): + if cell is None: + cell = line() deg = 2 if cell is None: cell = Point(1, [Point(0), Point(0)], vertex_num=2) @@ -372,7 +372,10 @@ def test_entity_perms(elem_gen, cell): @pytest.mark.parametrize("elem_gen,elem_code,deg", [(create_cg1, "CG", 1), (create_dg1, "DG", 1), - pytest.param(create_dg2, "DG", 2, marks=pytest.mark.xfail(reason='Need to update TSFC in CI')), + (construct_dg0_integral, "DG", 0), + (construct_dg1_integral, "DG", 1), + (construct_dg2_integral, "DG", 2), + (create_dg2, "DG", 2), (create_cg2, "CG", 2) ]) def test_1d(elem_gen, elem_code, deg): @@ -536,6 +539,29 @@ def poisson_solve(r, elem, parameters={}, quadrilateral=False): return sqrt(assemble(inner(u - f, u - f) * dx)) +def run_test_original(r, elem_code, deg, parameters={}, quadrilateral=False): + # Create mesh and define function space + m = UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=quadrilateral) + + x = SpatialCoordinate(m) + V = FunctionSpace(m, elem_code, deg) + # Define variational problem + u = Function(V) + v = TestFunction(V) + a = inner(grad(u), grad(v)) * dx + + bcs = [DirichletBC(V, Constant(0), 3), + DirichletBC(V, Constant(42), 4)] + + # Compute solution + solve(a == 0, u, solver_parameters=parameters, bcs=bcs) + + f = Function(V) + f.interpolate(42*x[1]) + + return sqrt(assemble(inner(u - f, u - f) * dx)) + + @pytest.mark.parametrize(['params', 'elem_gen'], [(p, d) for p in [{}, {'snes_type': 'ksponly', 'ksp_type': 'preonly', 'pc_type': 'lu'}] @@ -547,8 +573,7 @@ def test_poisson_analytic(params, elem_gen): @pytest.mark.parametrize(['elem_gen'], - [pytest.param(create_cg1_quad_tensor, marks=pytest.mark.xfail(reason="Needs tensor prod fiat branch")), - pytest.param(create_cg1_quad, marks=pytest.mark.xfail(reason='Need to allow generation on tensor product quads'))]) + [(create_cg1_quad_tensor,), (create_cg1_quad,)]) def test_quad(elem_gen): elem = elem_gen() r = 0 @@ -556,8 +581,13 @@ def test_quad(elem_gen): assert (poisson_solve(r, ufl_elem, parameters={}, quadrilateral=True) < 1.e-9) +@pytest.mark.xfail(reason="Issue with quad cell") def test_non_tensor_quad(): - create_cg1_quad() + elem = create_cg1_quad() + # ufl_elem = elem.to_ufl() + print(elem.to_fiat().entity_permutations()) + # elem.cell.hasse_diagram(filename="cg1quad.png") + assert (run_test_original(1, "CG", 1, parameters={}, quadrilateral=True) < 1.e-9) def project(U, mesh, func): @@ -876,10 +906,10 @@ def test_basis_funcs_gen(form_num): for v in basis_funcs[:1]: print(v) vec = as_tensor(sp.lambdify(symbols, v)(x_m[0], x_m[1], x_m[2])[:, 0]) - min_id1 = min([v for e in elem.entity_ids[2].values() for v in e]) - max_id1 = max([v for e in elem.entity_ids[2].values() for v in e]) + 1 - min_id2 = min([v for e in elem2.entity_ids[2].values() for v in e]) - max_id2 = max([v for e in elem2.entity_ids[2].values() for v in e]) + 1 + min_id1 = min([v for e in elem.entity_dofs[2].values() for v in e]) + max_id1 = max([v for e in elem.entity_dofs[2].values() for v in e]) + 1 + min_id2 = min([v for e in elem2.entity_dofs[2].values() for v in e]) + max_id2 = max([v for e in elem2.entity_dofs[2].values() for v in e]) + 1 res = assemble(interpolate(vec, V)).dat.data res2 = assemble(interpolate(vec, V2)).dat.data @@ -1088,7 +1118,7 @@ def vec(mesh): (construct_tet_ned_2nd_kind_3, "N2curl", 3, 1e-12), (construct_tet_ned2, "N1curl", 2, 1e-13), (periodic_table(1, 3, 1, 3), "N2curl", 3, 1e-12), - (periodic_table(1, 3, 1, 4), "N2curl", 4, 1e-12), + (periodic_table(1, 3, 1, 4), "N2curl", 4, 1e-11), (construct_tet_ned3_old, "N1curl", 2, 1e-13)]) def test_two_tet_projection(elem_gen, elem_code, deg, max_err): if hasattr(elem_gen, "__call__"): @@ -1123,6 +1153,159 @@ def expr(mesh): assert all([res < max_err for res in errors]) +def _two_hex_d4_perms(): + # The 8 cube symmetries of cell A that fix its top/bottom face pair, + from sympy.combinatorics.named_groups import DihedralGroup + T = [0, 3, 2, 1] + Tinv = [T.index(i) for i in range(4)] + perms = [] + for m in DihedralGroup(4).generate(): + sigma = list(m.array_form) + bottom = [Tinv[sigma[T[i]]] for i in range(4)] + perms.append(Permutation(bottom + [4 + i for i in sigma], size=8)) + return perms + + +@pytest.mark.parametrize("deg", [1, 2]) +def test_two_hex_projection_fiat_cg(deg): + is_vector = False + + from firedrake.utility_meshes import TwoHexMesh + from firedrake import project as firedrake_project # this module's own `project` (line 592) shadows the builtin + group = _two_hex_d4_perms() + + errors = [] + for g in group: + mesh = TwoHexMesh(perm=g) + V = FunctionSpace(mesh, "CG", deg) + x = SpatialCoordinate(mesh) + # k=0 (CG) spaces at any degree >= 1 exactly represent a linear + # scalar field; k=1/k=2 (Nedelec/RT) spaces at any degree >= 1 + # exactly represent a constant vector field (matching + # test_hdiv_3d_orientation_consistency's rationale). + expr = as_vector((2, 3, 5)) if is_vector else x[0] + 2*x[1] + 3*x[2] + u = TrialFunction(V) + v = TestFunction(V) + f = assemble(firedrake_project(expr, V)) + out = Function(V) + a = inner(u, v)*dx + L = inner(f, v)*dx + solve(a == L, out) + res = sqrt(assemble(dot(out - expr, out - expr) * dx)) + print(g.array_form, res) + errors += [res] + assert all([res < 1e-10 for res in errors]) + + +@pytest.mark.parametrize("col,k,deg", [(2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 0, 4), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]) +def test_two_hex_projection(col, k, deg): + # Analogous to test_two_tet_projection, but for hexahedra: sweeps the + # shared quadrilateral face's full 8-element dihedral symmetry group + elem = periodic_table(col, 3, k, deg) + ufl_elem = elem.to_ufl() + is_vector = len(elem.get_value_shape()) > 0 + + from firedrake.utility_meshes import TwoHexMesh + from firedrake import project as firedrake_project # this module's own `project` (line 592) shadows the builtin + group = _two_hex_d4_perms() + + errors = [] + for g in group: + mesh = TwoHexMesh(perm=g, use_fuse=True) + V = FunctionSpace(mesh, ufl_elem) + x = SpatialCoordinate(mesh) + # k=0 (CG) spaces at any degree >= 1 exactly represent a linear + # scalar field; k=1/k=2 (Nedelec/RT) spaces at any degree >= 1 + # exactly represent a constant vector field (matching + # test_hdiv_3d_orientation_consistency's rationale). k=3 (DG) is + # excluded: it has no shared DOFs across cells, so there is no + # cross-cell orientation to get wrong. + expr = as_vector((2, 3, 5)) if is_vector else x[0] + 2*x[1] + 3*x[2] + u = TrialFunction(V) + v = TestFunction(V) + f = assemble(firedrake_project(expr, V)) + out = Function(V) + a = inner(u, v)*dx + L = inner(f, v)*dx + solve(a == L, out) + res = sqrt(assemble(dot(out - expr, out - expr) * dx)) + print(g.array_form, res) + errors += [res] + assert all([res < 1e-10 for res in errors]) + + +def _one_form_norm_spread(ufl_elem, is_vector, mesh_factory, perms): + # For a transformation that is a signed permutation, the norm of the assembled + # vector should be fixed. + norms = [] + for g in perms: + mesh = mesh_factory(g) + V = FunctionSpace(mesh, ufl_elem) + v = TestFunction(V) + x = SpatialCoordinate(mesh) + f = as_vector((2, 3, 5)) if is_vector else x[0] + 2*x[1] + 3*x[2] + b = assemble(inner(f, v)*dx) + norms.append(float(np.linalg.norm(np.asarray(b.dat.data_ro).reshape(-1)))) + norms = np.array(norms) + return norms, norms.max() - norms.min() + + +_HEX_VEC_XFAIL = pytest.mark.xfail( + reason="hex H(div)/H(curl) facet orientation sign is wrong on reflections; " + "the 1-form norm is not orientation-invariant", + strict=True) + + +@pytest.mark.parametrize("k,deg", [ + pytest.param(0, 2, id="CG-2"), + pytest.param(0, 3, id="CG-3"), + # RT deg 1 (single-dof faces) is correct; its 1x1 sign reconciliation is + # invisible to ||b||, so it is an expected pass, guarded by the projection test. + pytest.param(2, 1, id="RT-1"), + pytest.param(1, 1, marks=_HEX_VEC_XFAIL, id="N1curl-1"), + pytest.param(1, 2, marks=_HEX_VEC_XFAIL, id="N1curl-2"), + pytest.param(2, 2, marks=_HEX_VEC_XFAIL, id="RT-2"), +]) +def test_two_hex_one_form_orientation_invariance(k, deg): + # Scalar (CG) cases are orientation-invariant; the hex vector cases + # (k=1 H(curl), k=2 H(div)) are the known facet-sign bug and are xfail. + from firedrake.utility_meshes import TwoHexMesh + elem = periodic_table(2, 3, k, deg) + ufl_elem = elem.to_ufl() + is_vector = len(elem.get_value_shape()) > 0 + perms = _two_hex_d4_perms() + _, spread = _one_form_norm_spread( + ufl_elem, is_vector, lambda g: TwoHexMesh(perm=g, use_fuse=True), perms) + assert spread < 1e-10 + + +_TET_ONE_FORM_PERMS = [ + Permutation([0, 1, 2, 3]), + Permutation([0, 2, 3, 1]), + Permutation([0, 3, 1, 2]), + Permutation([0, 1, 3, 2]), + Permutation([0, 3, 2, 1]), + Permutation([0, 2, 1, 3]), +] + + +@pytest.mark.parametrize("elem_gen", [ + pytest.param(construct_tet_cg4, id="CG-4"), + pytest.param(construct_tet_rt2, id="RT-2"), + pytest.param(construct_tet_ned_2nd_kind_2, id="N2curl-2"), +]) +def test_two_tet_one_form_orientation_invariance(elem_gen): + # construct_tet_ned2 (1st-kind Nedelec deg 2) is deliberately excluded: its + # face orientation matrices are not signed permutations + from firedrake.utility_meshes import TwoTetMesh + elem = elem_gen() + ufl_elem = elem.to_ufl() + is_vector = len(elem.get_value_shape()) > 0 + _, spread = _one_form_norm_spread( + ufl_elem, is_vector, lambda g: TwoTetMesh(perm=g, use_fuse=True), _TET_ONE_FORM_PERMS) + assert spread < 1e-10 + + @pytest.mark.parametrize("elem_gen,elem_code,deg", [(construct_tet_cg4, "CG", 4), (construct_tet_rt2, "RT", 2), (construct_tet_ned2, "N1curl", 2), (construct_tet_bdm2, "BDM", 2), ]) diff --git a/test/test_dofs.py b/test/test_dofs.py index d156312..1f8b2f9 100644 --- a/test/test_dofs.py +++ b/test/test_dofs.py @@ -3,7 +3,6 @@ from test_orientations import construct_nd2 import sympy as sp -import numpy as np def test_permute_dg1(): @@ -79,7 +78,7 @@ def test_permute_nd(): for g in nd.cell.group.members(): print("g:", g, g.numeric_rep()) for dof in nd.generate(): - print(dof(g).convert_to_fiat(cell.to_fiat(), 0).pt_dict) + print(dof(g).convert_to_fiat(cell.to_fiat(), 0, (2,)).pt_dict) print(dof, "->", dof(g), "eval, ", dof(g).eval(func)) @@ -103,55 +102,6 @@ def test_permute_nd2(): print(dof, "->", dof(g), "eval, ", dof(g).eval(func)) -def test_permute_nd_old(): - cell = polygon(3) - - nd = construct_nd(cell) - x = sp.Symbol("x") - y = sp.Symbol("y") - # func = FuseFunction(sp.Matrix([x, -1/3 + 2*y]), symbols=(x, y)) - - # phi_0 = FuseFunction(sp.Matrix([-0.333333333333333*y - 0.192450089729875, 0.333333333333333*x + 0.333333333333333]), symbols=(x, y)) - # phi_1 = FuseFunction(sp.Matrix([0.333333333333333*y + 0.192450089729875, 0.333333333333333 - 0.333333333333333*x]), symbols=(x, y)) - - # # original dofs - phi_2 = FuseFunction(sp.Matrix([1/3 - (np.sqrt(3)/6)*y, (np.sqrt(3)/6)*x]), symbols=(x, y)) - phi_0 = FuseFunction(sp.Matrix([-1/6 - (np.sqrt(3)/6)*y, (-np.sqrt(3)/6) + (np.sqrt(3)/6)*x]), symbols=(x, y)) - phi_1 = FuseFunction(sp.Matrix([-1/6 - (np.sqrt(3)/6)*y, - (np.sqrt(3)/6) + (np.sqrt(3)/6)*x]), symbols=(x, y)) - - for g in nd.cell.group.members(): - if g.numeric_rep() == 0 or g.numeric_rep() == 1: - print(g) - for dof in nd.generate(): - print(dof, "->", dof(g), dof(g).convert_to_fiat(cell.to_fiat(), 1).pt_dict) - print(dof, "->", dof(g), "eval p2 ", dof(g).eval(phi_2), "eval p0 ", dof(g).eval(phi_0), "eval p1 ", dof(g).eval(phi_1)) - - # reflected dofs - phi_2 = FuseFunction(sp.Matrix([0.288675134594813*y - 0.333333333333333, -0.288675134594813*x]), symbols=(x, y)) - phi_0 = FuseFunction(sp.Matrix([0.288675134594813*y + 0.166666666666667, -0.288675134594813*x - 0.288675134594813]), symbols=(x, y)) - phi_1 = FuseFunction(sp.Matrix([0.288675134594813*y + 0.166666666666667, 0.288675134594813 - 0.288675134594813*x]), symbols=(x, y)) - reflect = nd.cell.group.get_member([0, 1, 2]) - print(nd.cell.permute_entities(reflect, 1)) - reflect = nd.cell.group.get_member([2, 0, 1]) - print(nd.cell.permute_entities(reflect, 1)) - # print(reflect) - print(nd.cell.get_topology()) - # nd.cell.plot(filename="test_perms.png") - for g in nd.cell.group.members(): - if g.numeric_rep() == 0 or g.numeric_rep() == 1: - print(g) - for dof in nd.generate(): - print(dof, "->", dof(g), dof(g).convert_to_fiat(cell.to_fiat(), 1).pt_dict) - print(dof, "->", dof(g), "eval p2 ", dof(g).eval(phi_2), "eval p0 ", dof(g).eval(phi_0), "eval p1 ", dof(g).eval(phi_1)) - # # print(dof.convert_to_fiat(cell.to_fiat(), 1)(lambda x: np.array([1/3 - (np.sqrt(3)/6)*x[1], (np.sqrt(3)/6)*x[0]]))) - - # for g in nd.cell.group.members(): - # print(g) - # print(nd.cell.permute_entities(g, 0)) - # print(nd.cell.permute_entities(g, 1)) - - def test_permute_nodes(): cell = polygon(3) cg1 = create_cg1(cell) @@ -220,6 +170,6 @@ def test_generate_quadrature(): print("fiat", d.pt_dict) print() for d in elem.generate(): - print("fuse", d.to_quadrature(degree, (2,))) + print("fuse", d.to_quadrature(degree, value_shape=(2,))) elem.to_fiat() diff --git a/test/test_hypercube_orientation_keys.py b/test/test_hypercube_orientation_keys.py new file mode 100644 index 0000000..b275038 --- /dev/null +++ b/test/test_hypercube_orientation_keys.py @@ -0,0 +1,122 @@ +"""Pin FUSE's interval-product (quad/hex) orientation keys to FIAT/dmcommon. + +These are lightweight unit tests (no Firedrake): they check the canonical key +helper in ``fuse.utils`` and the group numbering of flattened quad/hex cells +against FIAT's ``make_entity_permutations_tensorproduct`` and the dmcommon +tensor-product orientation convention ``o = (2**d) * eo + io``. +""" +import itertools +import pytest +from fuse.cells import line, TensorProductPoint +from fuse.utils import canonical_tensor_orientation_key +from FIAT.reference_element import UFCInterval +from FIAT.orientation_utils import make_entity_permutations_tensorproduct + + +def _fiat_vertex_perm_to_key(d): + """FIAT vertex-image permutation -> dmcommon integer key for the + ``d``-fold interval product.""" + o_p_maps = [{0: [0, 1], 1: [1, 0]}] * d + tuple_perm_map = make_entity_permutations_tensorproduct( + [UFCInterval()] * d, [1] * d, o_p_maps) + out = {} + for tup, vperm in tuple_perm_map.items(): + eo = tup[0] + io = sum(b * 2 ** (d - 1 - i) for i, b in enumerate(tup[1:])) + out[tuple(vperm)] = (2 ** d) * eo + io + return out + + +# @pytest.mark.parametrize("d", [1, 2, 3]) +# def test_canonical_key_round_trip(d): +# axis_perms = sorted(itertools.permutations(range(d))) +# seen = set() +# for eo, axis_perm in enumerate(axis_perms): +# for io in range(2 ** d): +# flips = tuple((io >> (d - 1 - i)) & 1 for i in range(d)) +# key = canonical_tensor_orientation_key(axis_perm, flips, d) +# assert key == (2 ** d) * eo + io +# assert inverse_canonical_tensor_orientation_key(key, d) == (axis_perm, flips) +# seen.add(key) +# assert seen == set(range(2 ** d * len(axis_perms))) + + +@pytest.mark.parametrize("d", [2, 3]) +def test_canonical_key_matches_fiat(d): + """Every FIAT tuple key (eo, o_1, ..., o_d) flattens to the same integer + the helper produces from (axis_perm, flips).""" + axis_perms = sorted(itertools.permutations(range(d))) + o_p_maps = [{0: [0, 1], 1: [1, 0]}] * d + tuple_perm_map = make_entity_permutations_tensorproduct( + [UFCInterval()] * d, [1] * d, o_p_maps) + for tup in tuple_perm_map: + eo, flips = tup[0], tup[1:] + expected = (2 ** d) * eo + sum(b * 2 ** (d - 1 - i) for i, b in enumerate(flips)) + assert canonical_tensor_orientation_key(axis_perms[eo], flips, d) == expected + + +def test_flattened_quad_keys_match_dmcommon(): + """The flattened quad's 8 group members carry exactly the dmcommon keys + 0..7, matching FIAT identity-to-identity (by vertex-image permutation).""" + interval = line() + quad = TensorProductPoint(interval, interval).flatten() + fiat = _fiat_vertex_perm_to_key(2) + keys = {} + for m in quad.group.members(): + keys[tuple(m.array_form)] = m.numeric_rep() + # Every member's key equals the dmcommon key for its vertex image perm. + for vperm, key in keys.items(): + assert key == fiat[vperm] + assert sorted(keys.values()) == list(range(8)) + # Pin the reflection (eo == 0) block against the dmcommon docstring table: + # identity -> 0, flip y -> 1, flip x -> 2, flip both -> 3. + assert keys[(0, 1, 2, 3)] == 0 + assert keys[(1, 0, 3, 2)] == 1 + assert keys[(2, 3, 0, 1)] == 2 + assert keys[(3, 2, 1, 0)] == 3 + + +def test_flattened_hex_keys_match_dmcommon(): + """The flattened hex cell group carries exactly dmcommon keys 0..47.""" + interval = line() + hexf = TensorProductPoint(interval, interval, interval).flatten() + fiat = _fiat_vertex_perm_to_key(3) + keys = {} + for m in hexf.group.members(): + keys[tuple(m.array_form)] = m.numeric_rep() + for vperm, key in keys.items(): + assert key == fiat[vperm] + assert sorted(keys.values()) == list(range(48)) + + +def test_flattened_hex_face_keys_match_dmcommon(): + """Each quad face of the hex numbers its own D4 group with dmcommon keys + 0..7, agreeing with FIAT identity-to-identity.""" + interval = line() + hexf = TensorProductPoint(interval, interval, interval).flatten() + fiat = _fiat_vertex_perm_to_key(2) + face_dims = [dt for dt in hexf.all_subpoints if sum(dt) == 2] + assert face_dims, "expected 2D face sub-entities" + for dt in face_dims: + for face in hexf.all_subpoints[dt]: + keys = {tuple(m.array_form): m.numeric_rep() for m in face.group.members()} + for vperm, key in keys.items(): + assert key == fiat[vperm] + assert sorted(keys.values()) == list(range(8)) + + +def test_component_orientations_hit_subentity_numbering(): + """The structural keys emitted by ``component_orientations`` are always + valid keys of the corresponding flattened sub-entity's group numbering + (this is what dissolves the historical KeyError entanglement).""" + interval = line() + hex_tp = TensorProductPoint(interval, interval, interval) + hexf = hex_tp.flatten() + comp = hex_tp.component_orientations() + for dimtuple, table in comp.items(): + if sum(dimtuple) == 0: + continue + subgroup_keys = set() + for sub in hexf.all_subpoints[dimtuple]: + subgroup_keys |= {m.numeric_rep() for m in sub.group.members()} + assert set(table.values()) <= subgroup_keys diff --git a/test/test_orientation_representation.py b/test/test_orientation_representation.py new file mode 100644 index 0000000..6929960 --- /dev/null +++ b/test/test_orientation_representation.py @@ -0,0 +1,222 @@ +"""Check that hypercube orientation matrices are genuine group representations. + +An entity's orientation matrices describe how its DOFs transform under the +entity's symmetry group, so they must satisfy ``M[g] @ M[h] == M[g*h]``. That +is a self-contained criterion -- it needs no reference implementation -- and it +catches both a missing axis-permutation orientation (left as the identity) and +one built from the wrong DOF permutation. + +These are lightweight unit tests: no Firedrake, only ``fuse`` and FIAT. +""" +import itertools +import numpy as np +import pytest +from FIAT.reference_element import UFCInterval +from FIAT.orientation_utils import (make_entity_permutations_simplex, + make_entity_permutations_tensorproduct) + +from fuse.element_construction import (periodic_table, construct_interval_cgN, + construct_interval_dgN_integral) +from fuse.tensor_products import tensor_product, symmetric_tensor_product + + +# Quad builds are cheap; every (k, deg) is worth covering. +QUAD_PARAMS = [(k, deg) for k in range(4) for deg in (1, 2, 3)] + +# Hex builds take well over a minute each, so only the cases that carry +# signal are listed. Entities with at most one DOF give identity matrices, +# which satisfy the criterion vacuously -- that rules out hex CG1/CG2 and +# hex RT1/ND1. The first degree with a multi-DOF entity is 3 for the scalar +# families and 2 for the vector ones. +HEX_PARAMS = [(0, 3), (3, 2), (1, 2), (2, 2)] + + +def homomorphism_failures(elem, dim, ent_id): + """Count ``(g, h)`` pairs where ``M[g] @ M[h] != M[g*h]``.""" + entity = elem.cell.d_entities(dim)[ent_id] + mats = elem.matrices[dim][ent_id] + members = entity.group.members() + bad, total = 0, 0 + for g in members: + for h in members: + keys = (g.numeric_rep(), h.numeric_rep(), (g * h).numeric_rep()) + if any(k not in mats for k in keys): + continue + total += 1 + if not np.allclose(mats[keys[0]] @ mats[keys[1]], mats[keys[2]]): + bad += 1 + return bad, total + + +def assert_is_representation(elem): + dim = elem.cell.get_spatial_dimension() + checked = 0 + for d in range(dim + 1): + for ent_id, dofs in elem.entity_dofs[d].items(): + if len(dofs) == 0: + continue + bad, total = homomorphism_failures(elem, d, ent_id) + assert bad == 0, "dim %d entity %d: %d/%d products wrong" % (d, ent_id, bad, total) + checked += total + assert checked > 0 + + +def regrouped_positions(elem): + """Where each generated DOF ends up after ``_regroup_matrices``.""" + dim_of = {d: total_dim + for total_dim, ents in elem.entity_dofs.items() + for dofs in ents.values() + for d in dofs} + grouped = sorted(range(len(dim_of)), key=lambda i: (dim_of[i], i)) + return {gen: pos for pos, gen in enumerate(grouped)} + + +@pytest.mark.parametrize("k,deg", QUAD_PARAMS) +def test_quad_orientation_matrices_are_representations(k, deg): + assert_is_representation(periodic_table(2, 2, k, deg)) + + +@pytest.mark.slow +@pytest.mark.parametrize("k,deg", HEX_PARAMS) +def test_hex_orientation_matrices_are_representations(k, deg): + assert_is_representation(periodic_table(2, 3, k, deg)) + + +def assert_entity_blocks(elem): + """An entity's matrices may only mix that entity's own DOFs. + + Anything else means a block was written at the wrong offset -- which is + what happens when matrices reindexed into Firedrake's closure order are + paired with ``entity_dofs``, still in generation order. + """ + positions = regrouped_positions(elem) + for dim, ents in elem.entity_dofs.items(): + if dim == 0: + continue + for ent_id, dofs in ents.items(): + if len(dofs) == 0: + continue + own = [positions[d] for d in dofs] + outside = [i for i in range(len(positions)) if i not in own] + for key, mat in elem.matrices[dim][ent_id].items(): + block = mat[np.ix_(outside, outside)] + assert np.allclose(block, np.eye(len(outside))), \ + "dim %d entity %d orientation %d touches other entities' DOFs" % (dim, ent_id, key) + + +@pytest.mark.parametrize("k,deg", QUAD_PARAMS) +def test_quad_matrices_respect_entity_blocks(k, deg): + assert_entity_blocks(periodic_table(2, 2, k, deg)) + + +@pytest.mark.slow +@pytest.mark.parametrize("k,deg", HEX_PARAMS) +def test_hex_matrices_respect_entity_blocks(k, deg): + assert_entity_blocks(periodic_table(2, 3, k, deg)) + + +@pytest.mark.parametrize("d,deg", [ + (2, 2), (2, 3), (3, 3), + pytest.param(2, 4, marks=pytest.mark.xfail( + strict=True, + reason="pre-existing: the interval element reflects 3+ interior nodes " + "by pairing them ([1,0,2]) rather than reversing ([2,1,0]), so " + "every product built from it inherits the wrong reflection")), +]) +def test_cell_interior_matches_fiat_tensorproduct(d, deg): + """Pin the interior block against FIAT's own tensor-product permutations. + + The homomorphism criterion only checks self-consistency, so it cannot + detect a convention that is uniformly wrong. FIAT's + ``make_entity_permutations_tensorproduct`` is an independent source for + exactly the same object: how the interior nodes of an interval product + are permuted by each orientation. + """ + elem = periodic_table(2, d, 0, deg) + positions = regrouped_positions(elem) + interior = [positions[i] for i in elem.entity_dofs[d][0]] + assert len(interior) == (deg - 1) ** d + + o_p_maps = [make_entity_permutations_simplex(1, deg - 1)] * d + tuple_perm_map = make_entity_permutations_tensorproduct( + [UFCInterval()] * d, [deg - 1] * d, o_p_maps) + + mats = elem.matrices[d][0] + for tup, perm in tuple_perm_map.items(): + eo, flips = tup[0], tup[1:] + key = (2 ** d) * eo + sum(b * 2 ** (d - 1 - i) for i, b in enumerate(flips)) + assert key in mats + expected = np.eye(len(perm))[list(perm)] + block = mats[key][np.ix_(interior, interior)] + assert np.allclose(block, expected), \ + "orientation %d (eo=%d, flips=%s) disagrees with FIAT" % (key, eo, flips) + + +def test_symmetry_is_derived_not_assumed(): + """A product of unequal factors is not closed under the axis swap.""" + cg = construct_interval_cgN(2) + dg = construct_interval_dgN_integral(1) + + asymmetric = tensor_product(cg, dg).flatten() + assert asymmetric.symmetric is False + + assert tensor_product(cg, cg).flatten().symmetric is True + + +def test_declared_symmetry_is_checked(): + cg = construct_interval_cgN(2) + dg = construct_interval_dgN_integral(1) + with pytest.raises(NotImplementedError): + symmetric_tensor_product(cg, dg).flatten() + + +def test_asymmetric_element_rejected_by_fiat(): + """Flattening stays permissive; handing the result to FIAT does not. + + The hex H(div)/H(curl) constructions legitimately build non-symmetric + flat pieces to use as factors, so the rejection belongs at the boundary + where every orientation must actually be supplied. + """ + cg = construct_interval_cgN(2) + dg = construct_interval_dgN_integral(1) + elem = tensor_product(cg, dg).flatten() + with pytest.raises(NotImplementedError): + elem.to_fiat() + + +@pytest.mark.parametrize("k,deg", QUAD_PARAMS) +def test_quad_constructors_are_symmetric(k, deg): + assert periodic_table(2, 2, k, deg).symmetric is True + + +def test_quad_axis_swap_crosses_enriched_components(): + """RT's interior axis swap must map one component onto the other. + + Each summand of RT_k on a quad carries interior DOFs along a single + axis, so the swap sends every DOF of one summand to a DOF of the other. + A within-summand permutation (such as treating the block as a square + grid) cannot do that. + """ + elem = periodic_table(2, 2, 2, 2) + positions = regrouped_positions(elem) + interior = [positions[i] for i in elem.entity_dofs[2][0]] + assert len(interior) == 4 + + # Key 4 is the pure axis swap: eo == 1, no reflections. Swapping two axes + # is an odd permutation, so it reverses orientation and an H(div) DOF + # changes sign on top of being moved. + swap = elem.matrices[2][0][4][np.ix_(interior, interior)] + half = len(interior) // 2 + expected = -np.block([[np.zeros((half, half)), np.eye(half)], + [np.eye(half), np.zeros((half, half))]]) + assert np.allclose(swap, expected) + + +def test_leaf_key_transport_covers_every_axis_permutation(): + """Every axis permutation of a hex cell block resolves to a real DOF.""" + elem = periodic_table(2, 2, 0, 3) + assert not elem._closure_failures + # All 2**d * d! orientations are present on the cell entity. + d = elem.cell.get_spatial_dimension() + expected = 2 ** d * len(list(itertools.permutations(range(d)))) + assert len(elem.matrices[d][0]) == expected diff --git a/test/test_polynomial_space.py b/test/test_polynomial_space.py index 7d60d80..be5c4c4 100644 --- a/test/test_polynomial_space.py +++ b/test/test_polynomial_space.py @@ -54,6 +54,7 @@ def test_restriction(): res_on_set = restricted.to_ON_polynomial_set(cell) P3_on_set = P3.to_ON_polynomial_set(cell) + assert res_on_set.get_num_members() < P3_on_set.get_num_members() not_restricted = P3.restrict(0, 3) @@ -61,6 +62,16 @@ def test_restriction(): assert not_restricted.mindegree == 0 +def test_square_space(): + cell = polygon(3) + q2 = PolynomialSpace(3, 1) + + q2_on_set = q2.to_ON_polynomial_set(cell) + P3_on_set = P3.to_ON_polynomial_set(cell) + + assert q2_on_set.get_num_members() < P3_on_set.get_num_members() + + @pytest.mark.parametrize("deg", [1, 2, 3, 4]) def test_complete_space(deg): cell = polygon(3) diff --git a/test/test_tensor_prod.py b/test/test_tensor_prod.py index 0025066..2de7d3e 100644 --- a/test/test_tensor_prod.py +++ b/test/test_tensor_prod.py @@ -3,8 +3,91 @@ from fuse import * from firedrake import * from finat.ufl import CellBackend -from test_2d_examples_docs import construct_cg1, construct_dg1 -# from test_convert_to_fiat import create_cg1 +from test_2d_examples_docs import construct_cg1, construct_dg1, construct_dg0_integral, construct_dg1_integral +from test_convert_to_fiat import create_cg2, create_dg0, helmholtz_solve as helmholtz_solve2 +from fuse.tensor_products import HDiv as HDiv_fuse, HCurl as HCurl_fuse + + +def create_cg3_interval(cell=None): + if cell is None: + cell = line() + deg = 3 + if cell.dim() > 1: + raise NotImplementedError("This method is for cg3 on edges, please use construct_cg3 for triangles") + vert_dg = create_dg0(cell.vertices()[0]) + xs = [immerse(cell, vert_dg, TrH1)] + interior = [DOF(DeltaPairing(), PointKernel((-1/np.sqrt(5), )))] + + Pk = PolynomialSpace(deg) + cg = ElementTriple(cell, (Pk, CellL2, C0), [DOFGenerator(xs, get_cyc_group(len(cell.vertices())), S1), + DOFGenerator(interior, S2, S1)]) + return cg + + +def ned1_quad(): + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + return HCurl_fuse(tensor_product(cg1, dg0).flatten()) + HCurl_fuse(tensor_product(dg0, cg1).flatten()) + + +def rt1_quad(): + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + return HDiv_fuse(tensor_product(cg1, dg0).flatten()) + HDiv_fuse(tensor_product(dg0, cg1).flatten()) + + +def rt1_hex(): + # In-plane (x, y) RT1-on-quad, extruded by a discontinuous interval in z. + h1 = HDiv_fuse(tensor_product(construct_cg1(), construct_dg0_integral()).flatten()) + h2 = HDiv_fuse(tensor_product(construct_dg0_integral(), construct_cg1()).flatten()) + x_component = HDiv_fuse(tensor_product(h1, construct_dg0_integral())) + y_component = HDiv_fuse(tensor_product(h2, construct_dg0_integral())) + # z-normal component: DG0-on-quad extruded by a continuous interval in z. + dg0_quad = tensor_product(construct_dg0_integral(), construct_dg0_integral()).flatten() + z_component = HDiv_fuse(tensor_product(dg0_quad, construct_cg1())) + return x_component + y_component + z_component + + +def ned1_hex(): + # In-plane (x, y) tangential edge components (Nedelec-1st-kind-on-quad + # pieces), extruded by a continuous interval in z + ex = HCurl_fuse(tensor_product(construct_dg0_integral(), construct_cg1()).flatten()) + ey = HCurl_fuse(tensor_product(construct_cg1(), construct_dg0_integral()).flatten()) + x_component = HCurl_fuse(tensor_product(ex, construct_cg1())) + y_component = HCurl_fuse(tensor_product(ey, construct_cg1())) + # z-tangential component: bilinear (Q1) scalar quad extruded by a + # discontinuous interval in z. + cg1_quad = tensor_product(construct_cg1(), construct_cg1()).flatten() + z_component = HCurl_fuse(tensor_product(cg1_quad, construct_dg0_integral())) + return x_component + y_component + z_component + + +def ned1_tensor(): + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + + CG_1 = FiniteElement("CG", "interval", 1) + DG_0 = FiniteElement("DG", "interval", 0) + P1P0 = TensorProductElement(CG_1, DG_0) + horiz = HCurlElement(P1P0) + P0P1 = TensorProductElement(DG_0, CG_1) + vert = HCurlElement(P0P1) + firedrake_ned1 = horiz + vert + return HCurl_fuse(tensor_product(cg1, dg0)) + HCurl_fuse(tensor_product(dg0, cg1)), firedrake_ned1 + + +def rt1_tensor(): + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + + CG_1 = FiniteElement("CG", "interval", 1) + DG_0 = FiniteElement("DG", "interval", 0) + P1P0 = TensorProductElement(CG_1, DG_0) + RT_horiz = HDivElement(P1P0) + P0P1 = TensorProductElement(DG_0, CG_1) + RT_vert = HDivElement(P0P1) + firedrake_rt1 = RT_horiz + RT_vert + return HDiv_fuse(tensor_product(cg1, dg0)) + HDiv_fuse(tensor_product(dg0, cg1)), firedrake_rt1 def helmholtz_solve(mesh, V): @@ -18,6 +101,8 @@ def helmholtz_solve(mesh, V): u = Function(V) solve(a == L, u) f.interpolate(cos(x*pi*2)*cos(y*pi*2)) + print("res", u.dat.data) + print("true", f.dat.data) return sqrt(assemble(dot(u - f, u - f) * dx)) @@ -33,40 +118,48 @@ def mass_solve(U): assemble(L) solve(a == L, out) assert np.allclose(out.dat.data, f.dat.data, rtol=1e-5) + return out.dat.data -@pytest.mark.xfail(reason="tensor prod issues") -@pytest.mark.parametrize("generator, code, deg", [(construct_cg1, "CG", 1), (construct_dg1, "DG", 1)]) -def test_tensor_product_ext_mesh(generator, code, deg): +@pytest.mark.parametrize("generator1, generator2, code1, code2, deg1, deg2", + [(construct_cg1, construct_cg1, "CG", "CG", 1, 1), + (construct_dg1, construct_dg1, "DG", "DG", 1, 1), + (construct_dg1, construct_cg1, "DG", "CG", 1, 1), + (construct_dg1_integral, construct_cg1, "DG", "CG", 1, 1)]) +def test_ext_mesh(generator1, generator2, code1, code2, deg1, deg2): m = UnitIntervalMesh(2, cell_backend=CellBackend.FUSE) mesh = ExtrudedMesh(m, 2) # manual method of creating tensor product elements - horiz_elt = FiniteElement(code, as_cell("interval"), deg) - vert_elt = FiniteElement(code, as_cell("interval"), deg) + horiz_elt = FiniteElement(code1, as_cell("interval"), deg1) + vert_elt = FiniteElement(code2, as_cell("interval"), deg2) elt = TensorProductElement(horiz_elt, vert_elt) U = FunctionSpace(mesh, elt) - mass_solve(U) + res1 = mass_solve(U) # fuseonic way of creating tensor product elements - A = generator() - B = generator() + A = generator1() + B = generator2() elem = tensor_product(A, B) U = FunctionSpace(mesh, elem.to_ufl()) - mass_solve(U) + res2 = mass_solve(U) + + assert np.allclose(res1, res2) -@pytest.mark.xfail(reason="tensor prod issues") -def test_helmholtz(): +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(construct_cg1, "CG", 1, 1.8), + (create_cg2, "CG", 2, 3.8), + (create_cg3_interval, "CG", 3, 4.8)]) +def test_helmholtz(elem_gen, elem_code, deg, conv_rate): vals = range(3, 6) res = [] for r in vals: m = UnitIntervalMesh(2**r, cell_backend=CellBackend.FUSE) mesh = ExtrudedMesh(m, 2**r) - A = construct_cg1() - B = construct_cg1() + A = elem_gen() + B = elem_gen() elem = tensor_product(A, B) U = FunctionSpace(mesh, elem.to_ufl()) @@ -75,7 +168,135 @@ def test_helmholtz(): res = np.array(res) conv = np.log2(res[:-1] / res[1:]) print("convergence order:", conv) - assert (np.array(conv) > 1.8).all() + assert (np.array(conv) > conv_rate).all() + + +def project_expr(mesh, U, expr): + x = SpatialCoordinate(mesh) + f = assemble(project(expr(x), U)) + out = Function(U) + u = TrialFunction(U) + v = TestFunction(U) + a = inner(u, v)*dx + L = inner(f, v)*dx + solve(a == L, out) + res = sqrt(assemble(dot(out - expr(x), out - expr(x)) * dx)) + return res + + +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(rt1_quad, "RTCF", 1, 1.8), (ned1_quad, "RTCE", 1, 0.8)]) +def test_project_vec_quad(elem_gen, elem_code, deg, conv_rate): + vals = range(3, 6) + function = lambda x, i: cos((3/4)*pi*x[i]) + expr = lambda x: as_vector([function(x, 0), function(x, 1)]) + res_fuse = [] + res_fire = [] + for r in vals: + mesh_fuse = UnitSquareMesh(2**r, 2**r, quadrilateral=True, use_fuse=True) + U = FunctionSpace(mesh_fuse, elem_gen().to_ufl()) + res_fuse += [project_expr(mesh_fuse, U, expr)] + + mesh_fire = UnitSquareMesh(2**r, 2**r, quadrilateral=True) + U = FunctionSpace(mesh_fire, elem_code, deg) + res_fire += [project_expr(mesh_fire, U, expr)] + + print("fuse l2 error norms:", res_fuse) + res_fuse = np.array(res_fuse) + conv_fuse = np.log2(res_fuse[:-1] / res_fuse[1:]) + print("fuse convergence order:", conv_fuse) + + print("fire l2 error norms:", res_fire) + res_fire = np.array(res_fire) + conv_fire = np.log2(res_fire[:-1] / res_fire[1:]) + print("fire convergence order:", conv_fire) + + assert (conv_fuse > conv_rate).all() + assert (conv_fire > conv_rate).all() + + +@pytest.mark.parametrize(["elem_gen", "conv_rate"], [(rt1_tensor, 0.8), (ned1_tensor, 0.8)]) +def test_project_vec_ext(elem_gen, conv_rate): + vals = range(3, 6) + function = lambda x, i: cos((3/4)*pi*x[i]) + expr = lambda x: as_vector([function(x, 0), function(x, 1)]) + res_fuse = [] + res_fire = [] + for r in vals: + fuse_elem, firedrake_elem = elem_gen() + mesh_fuse = ExtrudedMesh(UnitIntervalMesh(2**r, use_fuse=True), 2**r) + U = FunctionSpace(mesh_fuse, fuse_elem.to_ufl()) + res_fuse += [project_expr(mesh_fuse, U, expr)] + + mesh_fire = ExtrudedMesh(UnitIntervalMesh(2**r), 2**r) + U = FunctionSpace(mesh_fire, firedrake_elem) + res_fire += [project_expr(mesh_fire, U, expr)] + + print("fuse l2 error norms:", res_fuse) + res_fuse = np.array(res_fuse) + conv_fuse = np.log2(res_fuse[:-1] / res_fuse[1:]) + print("fuse convergence order:", conv_fuse) + + print("fire l2 error norms:", res_fire) + res_fire = np.array(res_fire) + conv_fire = np.log2(res_fire[:-1] / res_fire[1:]) + print("fire convergence order:", conv_fire) + + assert (conv_fuse > conv_rate).all() + assert (conv_fire > conv_rate).all() + + +@pytest.mark.parametrize(["elem_gen", "conv_rate"], [(rt1_hex, 1.8), (ned1_hex, 0.8)]) +def test_project_vec_hex(elem_gen, conv_rate): + vals = [2, 3] + function = lambda x, i: cos((3/4)*pi*x[i]) + expr = lambda x: as_vector([function(x, 0), function(x, 1), function(x, 2)]) + res_fuse = [] + for r in vals: + mesh_fuse = UnitCubeMesh(2**r, 2**r, 2**r, hexahedral=True, use_fuse=True) + U = FunctionSpace(mesh_fuse, elem_gen().flatten().to_ufl()) + res_fuse += [project_expr(mesh_fuse, U, expr)] + + print("fuse l2 error norms:", res_fuse) + res_fuse = np.array(res_fuse) + conv_fuse = np.log2(res_fuse[:-1] / res_fuse[1:]) + print("fuse convergence order:", conv_fuse) + + assert (conv_fuse > conv_rate).all() + + +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(construct_cg1, "CG", 1, 1.8), + (create_cg2, "CG", 2, 3.8), + (create_cg3_interval, "CG", 3, 4.8)]) +def test_helmholtz_3d(elem_gen, elem_code, deg, conv_rate): + vals = range(2, 4) + res_ufc = [] + res_fuse = [] + for r in vals: + m = UnitSquareMesh(2**r, 2**r, quadrilateral=True, use_fuse=True) + mesh_fuse = ExtrudedMesh(m, 2**r) + + A = elem_gen() + B = elem_gen() + C = elem_gen() + elem = tensor_product(tensor_product(A, B).flatten(), C) + + U1 = FunctionSpace(mesh_fuse, elem.to_ufl()) + res_fuse += [helmholtz_solve2(U1, mesh_fuse)] + + m = UnitSquareMesh(2**r, 2**r, quadrilateral=True) + mesh_ufc = ExtrudedMesh(m, 2**r) + U2 = FunctionSpace(mesh_ufc, elem_code, deg) + res_ufc += [helmholtz_solve2(U2, mesh_ufc)] + print("l2 error norms:", res_ufc) + res_ufc = np.array(res_ufc) + conv_ufc = np.log2(res_ufc[:-1] / res_ufc[1:]) + print("convergence order:", conv_ufc) + print("l2 error norms:", res_fuse) + res_fuse = np.array(res_fuse) + conv_fuse = np.log2(res_fuse[:-1] / res_fuse[1:]) + print("convergence order:", conv_fuse) + # assert (np.array(conv_fuse) > conv_rate).all() + # assert (np.array(conv_ufc) > conv_rate).all() @pytest.mark.xfail(reason="Needs updated FIAT tensor branch") @@ -94,32 +315,383 @@ def test_on_quad_mesh(): mass_solve(U) -@pytest.mark.xfail(reason="Needs updated FIAT tensor branch") -def test_quad_mesh_helmholtz(): +def test_cg3(): + r = 1 + mesh = UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=True, use_fuse=True) + res_fuse = [] + A = create_cg3_interval() + B = create_cg3_interval() + # elem = symmetric_tensor_product(A, B, matrices=False).flatten() + # U = FunctionSpace(mesh, elem.to_ufl()) + # res_fuse += [helmholtz_solve(mesh, U)] + elem = symmetric_tensor_product(A, B).flatten() + U = FunctionSpace(mesh, elem.to_ufl()) + res_fuse += [helmholtz_solve(mesh, U)] + assert all(np.array(res_fuse) < 0.003) + + +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(construct_cg1, "CG", 1, 1.8), + (create_cg2, "CG", 2, 3.8), + (create_cg3_interval, "CG", 3, 4.8)]) +def test_quad_mesh_helmholtz(elem_gen, elem_code, deg, conv_rate): quadrilateral = True vals = range(3, 6) res_fuse = [] - res_fire = [] + res_fiat = [] for r in vals: mesh_fuse = UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=quadrilateral, cell_backend=CellBackend.FUSE) - - A = construct_cg1() - B = construct_cg1() - elem = tensor_product(A, B).flatten() + A = elem_gen() + B = elem_gen() + elem = symmetric_tensor_product(A, B).flatten() U = FunctionSpace(mesh_fuse, elem.to_ufl()) res_fuse += [helmholtz_solve(mesh_fuse, U)] mesh_ufc = UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=quadrilateral) - U = FunctionSpace(mesh_ufc, "CG", 1) - res_fire += [helmholtz_solve(mesh_ufc, U)] - print("l2 error norms:", res_fuse) + U = FunctionSpace(mesh_ufc, elem_code, deg) + res_fiat += [helmholtz_solve(mesh_ufc, U)] + print("Fuse l2 error norms:", res_fuse) + res = np.array(res_fuse) + conv = np.log2(res[:-1] / res[1:]) + print("Fuse convergence order:", conv) + assert (np.array(conv) > conv_rate).all() + + print("FIAT l2 error norms:", res_fiat) + res = np.array(res_fiat) + conv = np.log2(res[:-1] / res[1:]) + print("Fiat convergence order:", conv) + assert (np.array(conv) > conv_rate).all() + + +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(construct_cg1, "CG", 1, 1.7), + (create_cg2, "CG", 2, 3.8), + (create_cg3_interval, "CG", 3, 4.8)]) +def test_ext_mesh_helmholtz_3d(elem_gen, elem_code, deg, conv_rate): + vals = range(2, 4) + res_fuse = [] + res_fiat = [] + for r in vals: + mesh_fuse = ExtrudedMesh(UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=True, use_fuse=True), 2**r) + A = elem_gen() + B = elem_gen() + C = elem_gen() + elem = tensor_product(tensor_product(A, B).flatten(), C) + U = FunctionSpace(mesh_fuse, elem.to_ufl()) + res_fuse += [helmholtz_solve2(U, mesh_fuse)] + + mesh_ufc = ExtrudedMesh(UnitSquareMesh(2 ** r, 2 ** r), 2**r) + U = FunctionSpace(mesh_ufc, elem_code, deg) + res_fiat += [helmholtz_solve2(U, mesh_ufc)] + print("Fuse l2 error norms:", res_fuse) res = np.array(res_fuse) conv_fuse = np.log2(res[:-1] / res[1:]) - print("convergence order:", conv_fuse) + print("Fuse convergence order:", conv_fuse) - print("l2 error norms:", res_fire) - res = np.array(res_fire) - conv_ufc = np.log2(res[:-1] / res[1:]) - print("convergence order:", conv_ufc) - assert (np.array(conv_fuse) > 1.8).all() - assert (np.array(conv_ufc) > 1.8).all() + print("FIAT l2 error norms:", res_fiat) + res = np.array(res_fiat) + conv = np.log2(res[:-1] / res[1:]) + print("Fiat convergence order:", conv) + assert (np.array(conv_fuse) > conv_rate).all() + # assert (np.array(conv) > conv_rate).all() + + +@pytest.mark.parametrize(["elem_gen", "elem_code", "deg", "conv_rate"], [(construct_cg1, "CG", 1, 1.7), + (create_cg2, "CG", 2, 3.8), + (create_cg3_interval, "CG", 3, 4.8)]) +def test_quad_mesh_helmholtz_3d(elem_gen, elem_code, deg, conv_rate): + vals = range(2, 4) + res_fuse = [] + res_fiat = [] + for r in vals: + mesh_fuse = UnitCubeMesh(2 ** r, 2 ** r, 2 ** r, hexahedral=True, use_fuse=True) + A = elem_gen() + B = elem_gen() + C = elem_gen() + elem = symmetric_tensor_product(A, B, C).flatten() + U = FunctionSpace(mesh_fuse, elem.to_ufl()) + res_fuse += [helmholtz_solve2(U, mesh_fuse)] + + mesh_ufc = UnitCubeMesh(2 ** r, 2 ** r, 2 ** r, hexahedral=True) + U = FunctionSpace(mesh_ufc, elem_code, deg) + res_fiat += [helmholtz_solve2(U, mesh_ufc)] + print("Fuse l2 error norms:", res_fuse) + res = np.array(res_fuse) + conv_fuse = np.log2(res[:-1] / res[1:]) + print("Fuse convergence order:", conv_fuse) + + print("FIAT l2 error norms:", res_fiat) + res = np.array(res_fiat) + conv_fiat = np.log2(res[:-1] / res[1:]) + print("Fiat convergence order:", conv_fiat) + assert (np.array(conv_fuse) > conv_rate).all() + assert (np.array(conv_fiat) > conv_rate).all() + + +@pytest.mark.parametrize(["A", "B", "res"], [(Point(0), line(), False), + (line(), line(), True), + (polygon(3), line(), False),]) +def test_flattening(A, B, res): + tensor_cell = TensorProductPoint(A, B) + if not res: + with pytest.raises(AssertionError): + tensor_cell.flatten() + else: + cell = tensor_cell.flatten() + cell.construct_fuse_rep() + + +@pytest.mark.parametrize(["A", "B", "C"], [(line(), line(), line()),]) +def test_creation(A, B, C): + tensor_cell_2d = TensorProductPoint(A, B) + tensor_cell_2d.to_ufl() + tensor_cell_2d.to_fiat() + flat_tensor_cell_2d = tensor_cell_2d.flatten() + print(flat_tensor_cell_2d) + tensor_cell_3d = TensorProductPoint(A, B, C) + tensor_cell_3d.to_ufl() + tensor_cell_3d.to_fiat() + flat_tensor_cell_3d = tensor_cell_3d.flatten() + print(flat_tensor_cell_3d) + + +@pytest.mark.xfail(reason="FUSE has no facet-restricted 'HDiv Trace' analogue yet: " + "tensor_product(...).flatten() produces basis functions with " + "full cell/edge support (see entity_support_dofs), not functions " + "that vanish off their associated facet like FIAT's HDivTrace, so " + "the facet mass form (ds/dS) is not well posed for this space.") +def test_trace_galerkin_projection(): + mesh = UnitSquareMesh(10, 10, quadrilateral=True, use_fuse=True) + + x, y = SpatialCoordinate(mesh) + A = construct_cg1() + B = construct_dg1_integral() + elem = tensor_product(A, B).flatten() + elem2 = tensor_product(B, A).flatten() + + # Define the Trace Space + T = FunctionSpace(mesh, (elem + elem2).to_ufl()) + + # Define trial and test functions + lambdar = TrialFunction(T) + gammar = TestFunction(T) + + # Define right hand side function + + V = FunctionSpace(mesh, tensor_product(A, construct_cg1()).flatten().to_ufl()) + f = Function(V) + f.interpolate(cos(x*pi*2)*cos(y*pi*2)) + + # Construct bilinear form + a = inner(lambdar, gammar) * ds + inner(lambdar('+'), gammar('+')) * dS + + # Construct linear form + l = inner(f, gammar) * ds + inner(f('+'), gammar('+')) * dS + + # Compute the solution + t = Function(T) + solve(a == l, t, solver_parameters={'ksp_rtol': 1e-14}) + + # Compute error in trace norm + trace_error = sqrt(assemble(FacetArea(mesh)*inner((t - f)('+'), (t - f)('+')) * dS)) + + assert trace_error < 1e-13 + + +def test_hdiv(): + np.set_printoptions(linewidth=90, precision=4, suppress=True) + + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + fuse_rt1 = HDiv_fuse(tensor_product(cg1, dg0)) + HDiv_fuse(tensor_product(dg0, cg1)) + + CG_1 = FiniteElement("CG", "interval", 1) + DG_0 = FiniteElement("DG", "interval", 0) + P1P0 = TensorProductElement(CG_1, DG_0) + RT_horiz = HDivElement(P1P0) + P0P1 = TensorProductElement(DG_0, CG_1) + RT_vert = HDivElement(P0P1) + firedrake_rt1 = RT_horiz + RT_vert + + m = UnitIntervalMesh(2) + mesh = ExtrudedMesh(m, 2) + m = UnitIntervalMesh(2, use_fuse=True) + mesh2 = ExtrudedMesh(m, 2) + V = FunctionSpace(mesh, firedrake_rt1) + V2 = FunctionSpace(mesh2, fuse_rt1.to_ufl()) + for V, mesh in zip([V, V2], (mesh, mesh2)): + u = TrialFunction(V) + v = TestFunction(V) + f = Function(V) + x, y = SpatialCoordinate(mesh) + # f_vec = as_vector(((1+8*pi*pi)*cos(x*pi*2)*cos(y*pi*2), (1+8*pi*pi)*cos(x*pi*2)*cos(y*pi*2))) + f_vec = as_vector((2, 3)) + f = project(f_vec, V) + a = (inner(grad(u), grad(v)) + inner(u, v)) * dx + L = inner(f, v) * dx + u = Function(V) + solve(a == L, u) + # f_vec is constant, so grad(f_vec) = 0 and the exact solution of + # (grad(u):grad(v) + u.v)dx = f.v dx is u = f_vec everywhere. + error = sqrt(assemble(dot(u - f_vec, u - f_vec) * dx)) + assert error < 1e-10 + + +def test_hcurl(): + np.set_printoptions(linewidth=90, precision=4, suppress=True) + + cg1 = construct_cg1() + dg0 = construct_dg0_integral() + fuse_ncurl1 = HCurl_fuse(tensor_product(dg0, cg1)) + HCurl_fuse(tensor_product(cg1, dg0)) + + CG_1 = FiniteElement("CG", "interval", 1) + DG_0 = FiniteElement("DG", "interval", 0) + DG0CG1 = TensorProductElement(DG_0, CG_1) + Ned_x = HCurlElement(DG0CG1) + CG1DG0 = TensorProductElement(CG_1, DG_0) + Ned_y = HCurlElement(CG1DG0) + firedrake_ncurl1 = Ned_x + Ned_y + + m = UnitIntervalMesh(2) + mesh = ExtrudedMesh(m, 2) + m = UnitIntervalMesh(2, use_fuse=True) + mesh2 = ExtrudedMesh(m, 2) + V = FunctionSpace(mesh, firedrake_ncurl1) + V2 = FunctionSpace(mesh2, fuse_ncurl1.to_ufl()) + assert V.dim() == V2.dim() + for V, mesh in zip([V, V2], (mesh, mesh2)): + u = TrialFunction(V) + v = TestFunction(V) + x, y = SpatialCoordinate(mesh) + f_vec = as_vector((2, 3)) + f = project(f_vec, V) + a = (inner(grad(u), grad(v)) + inner(u, v)) * dx + L = inner(f, v) * dx + u = Function(V) + solve(a == L, u) + # f_vec is constant, so grad(f_vec) = 0 and the exact solution of + # (grad(u):grad(v) + u.v)dx = f.v dx is u = f_vec everywhere. + error = sqrt(assemble(dot(u - f_vec, u - f_vec) * dx)) + assert error < 1e-10 + + +def test_hdiv_3d_orientation_consistency(): + # If neighbouring cells disagreed on the sign of a shared facet DOF, the + # global RT space could no longer represent a true constant vector field + f_vec = as_vector((2, 3, 5)) + mesh = UnitCubeMesh(3, 3, 3, hexahedral=True, use_fuse=True) + V = FunctionSpace(mesh, rt1_hex().flatten().to_ufl()) + + u = TrialFunction(V) + v = TestFunction(V) + sol = Function(V) + solve(inner(u, v) * dx == inner(f_vec, v) * dx, sol) + + error = sqrt(assemble(dot(sol - f_vec, sol - f_vec) * dx)) + assert error < 1e-10 + + +def test_hcurl_3d_orientation_consistency(): + # exact reproduction of a constant vector field is a genuine cross-cell + # sign-consistency check for the tangential edge DOFs, not just a + # well-posedness check. + f_vec = as_vector((2, 3, 5)) + mesh = UnitCubeMesh(3, 3, 3, hexahedral=True, use_fuse=True) + elem = ned1_hex().flatten() + V = FunctionSpace(mesh, elem.to_ufl()) + u = TrialFunction(V) + v = TestFunction(V) + sol = Function(V) + solve(inner(u, v) * dx == inner(f_vec, v) * dx, sol) + + error = sqrt(assemble(dot(sol - f_vec, sol - f_vec) * dx)) + assert error < 1e-10 + + +def test_transforms(): + edge = Point(1, [Point(0), Point(0)], vertex_num=2) + rev_edge = edge.orient(edge.group.members()[1]) + from fuse.tensor_products import HDiv, HCurl + cg1 = construct_cg1() + rev_cg1 = construct_cg1(rev_edge) + dg0 = construct_dg0_integral() + rev_dg0 = construct_dg0_integral(rev_edge) + import gem + v = gem.Literal(5) + print("HCurl") + print(HCurl(tensor_product(dg0, cg1)).gem_transformer(v)) + print(HCurl(tensor_product(rev_dg0, cg1)).gem_transformer(v)) + print(HCurl(tensor_product(cg1, dg0)).gem_transformer(v)) + print(HCurl(tensor_product(cg1, rev_dg0)).gem_transformer(v)) + print(HCurl(tensor_product(dg0, rev_cg1)).gem_transformer(v)) + print(HCurl(tensor_product(rev_cg1, dg0)).gem_transformer(v)) + print("HDiv") + print(HDiv(tensor_product(dg0, cg1)).gem_transformer(v)) + print(HDiv(tensor_product(rev_dg0, cg1)).gem_transformer(v)) + print(HDiv(tensor_product(cg1, dg0)).gem_transformer(v)) + print(HDiv(tensor_product(cg1, rev_dg0)).gem_transformer(v)) + print(HDiv(tensor_product(dg0, rev_cg1)).gem_transformer(v)) + print(HDiv(tensor_product(rev_cg1, dg0)).gem_transformer(v)) + + +def test_sum_fac(): + # In 2d we have O(N_q^2N_i^4) -> O(p^6) + # Sum factorisation gains 1 factor so we expect O(p^5) + # For CG3 p = 3 so it should be 3x faster + mesh1 = ExtrudedMesh(UnitIntervalMesh(10, use_fuse=True), 10) + mesh2 = ExtrudedMesh(UnitIntervalMesh(10), 10) + A = create_cg3_interval() + B = create_cg3_interval() + elem = tensor_product(A, B) + mesh3 = UnitSquareMesh(10, 10, quadrilateral=True, use_fuse=True) + mesh4 = UnitSquareMesh(10, 10, quadrilateral=True) + C = create_cg3_interval() + D = create_cg3_interval() + elem2 = symmetric_tensor_product(C, D).flatten() + V = FunctionSpace(mesh1, elem.to_ufl()) + V1 = FunctionSpace(mesh2, "CG", 3) + V2 = FunctionSpace(mesh3, elem2.to_ufl()) + V3 = FunctionSpace(mesh4, "CG", 3) + Vs = [V, V1, V2, V3] + for V in Vs: + print(V) + u = TrialFunction(V) + v = TestFunction(V) + a = dot(grad(u), grad(v))*dx # Laplace operator + from tsfc import compile_form + kernel_vanilla, = compile_form(a, parameters={"mode": "vanilla"}) + print("Local assembly FLOPs with vanilla mode is {0:.3g}".format(kernel_vanilla.flop_count)) + kernel_spectral, = compile_form(a) + print("Local assembly FLOPs with spectral mode is {0:.3g}".format(kernel_spectral.flop_count)) + assert (kernel_vanilla.flop_count / kernel_spectral.flop_count) > 3 + + +# @pytest.mark.xfail(reason="3D tensor products not implemented") +def test_sum_fac_3d(): + # In 2d we have O(N_q^3N_i^6) -> O(p^9) + # Sum factorisation gains 2 factors so we expect O(p^7) + # For CG3 p = 3 so it should be 9x faster - seems that it is faster than this in regular firedrake + mesh = ExtrudedMesh(UnitSquareMesh(10, 10, use_fuse=True), 10) + mesh2 = ExtrudedMesh(UnitSquareMesh(10, 10), 10) + A = create_cg3_interval() + B = create_cg3_interval() + C = create_cg3_interval() + elem = tensor_product(tensor_product(A, B).flatten(), C) + mesh3 = UnitCubeMesh(10, 10, 10, hexahedral=True, use_fuse=True) + mesh4 = UnitCubeMesh(10, 10, 10, hexahedral=True) + elem2 = symmetric_tensor_product(A, B, C).flatten() + V = FunctionSpace(mesh, elem.to_ufl()) + V1 = FunctionSpace(mesh2, "CG", 3) + V2 = FunctionSpace(mesh3, elem2.to_ufl()) + V3 = FunctionSpace(mesh4, "CG", 3) + Vs = [V, V1, V2, V3] + names = ["Extruded FUSE", "Extruded FIAT", "Hex FUSE", "Hex FIAT"] + for V, name in zip(Vs, names): + print(name) + u = TrialFunction(V) + v = TestFunction(V) + a = dot(grad(u), grad(v))*dx # Laplace operator + from tsfc import compile_form + kernel_vanilla, = compile_form(a, parameters={"mode": "vanilla"}) + print("Local assembly FLOPs with vanilla mode is {0:.3g}".format(kernel_vanilla.flop_count)) + kernel_spectral, = compile_form(a) + print("Local assembly FLOPs with spectral mode is {0:.3g}".format(kernel_spectral.flop_count)) + print(kernel_vanilla.flop_count / kernel_spectral.flop_count) diff --git a/test/test_vectorisation.py b/test/test_vectorisation.py index 6560bd1..92fbb40 100644 --- a/test/test_vectorisation.py +++ b/test/test_vectorisation.py @@ -207,9 +207,9 @@ def test_vector_triple_entity_ids_scale(builder, deg): N = vec.N assert len(vec.nodes) == vec.poly_set.get_num_members() - for dim in vec.entity_ids: - for entity in vec.entity_ids[dim]: - assert len(vec.entity_ids[dim][entity]) == N * len(base.entity_ids[dim][entity]) + for dim in vec.entity_dofs: + for entity in vec.entity_dofs[dim]: + assert len(vec.entity_dofs[dim][entity]) == N * len(base.entity_dofs[dim][entity]) for vec_id, (base_id, comp) in vec.comp_map.items(): assert vec.dof_id_to_fiat_id[vec_id] == N * base.dof_id_to_fiat_id[base_id] + comp