From 9afe210967fa8cd94f97aaae4aed83f103afdf17 Mon Sep 17 00:00:00 2001 From: Chris Richardson Date: Tue, 29 Aug 2023 14:37:36 +0100 Subject: [PATCH] Fixes for obscure functionality (which is probably broken in main) --- demo/CellGeometry.py | 8 +++++++- ffcx/codegeneration/C/c_implementation.py | 14 ++++++++++---- ffcx/codegeneration/access.py | 2 +- ffcx/codegeneration/geometry.py | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/demo/CellGeometry.py b/demo/CellGeometry.py index 57cd9e88f..8eea59f0f 100644 --- a/demo/CellGeometry.py +++ b/demo/CellGeometry.py @@ -3,7 +3,8 @@ # A functional M involving a bunch of cell geometry quantities. import basix.ufl from ufl import (CellVolume, Circumradius, Coefficient, FacetArea, FacetNormal, - SpatialCoordinate, ds, dx, tetrahedron) + SpatialCoordinate, ds, dx, tetrahedron, TrialFunction) +from ufl.geometry import FacetEdgeVectors cell = tetrahedron V = basix.ufl.element("P", cell.cellname(), 1) @@ -17,3 +18,8 @@ area = FacetArea(cell) M = u * (x[0] * vol * rad) * dx + u * (x[0] * vol * rad * area) * ds # + u*area*avg(n[0]*x[0]*vol*rad)*dS + +# Test some obscure functionality +fev = FacetEdgeVectors(cell) +v = TrialFunction(V) +L = fev[0, 0] * v * ds diff --git a/ffcx/codegeneration/C/c_implementation.py b/ffcx/codegeneration/C/c_implementation.py index 36935e19b..cd0e86244 100644 --- a/ffcx/codegeneration/C/c_implementation.py +++ b/ffcx/codegeneration/C/c_implementation.py @@ -145,18 +145,22 @@ def __init__(self, scalar, precision=None) -> None: if precision is None: np_type = cdtype_to_numpy(self.real_type) self.precision = np.finfo(np_type).precision + 1 + else: + assert isinstance(precision, int) + self.precision = precision - def _format_float(self, x): + def _format_number(self, x): prec = self.precision if isinstance(x, complex): return "({:.{prec}}+I*{:.{prec}})".format(x.real, x.imag, prec=prec) - else: + elif isinstance(x, float): return "{:.{prec}}".format(x, prec=prec) + return str(x) def _build_initializer_lists(self, values): arr = "{" if len(values.shape) == 1: - arr += ", ".join(self._format_float(v) for v in values) + arr += ", ".join(self._format_number(v) for v in values) elif len(values.shape) > 1: arr += ",\n ".join(self._build_initializer_lists(v) for v in values) arr += "}" @@ -176,6 +180,8 @@ def format_array_decl(self, arr) -> str: typename = self.scalar_type elif dtype == L.DataType.REAL: typename = self.real_type + elif dtype == L.DataType.INT: + typename = "int" else: raise ValueError(f"Invalid dtype: {dtype}") @@ -239,7 +245,7 @@ def format_not(self, val) -> str: return f"{val.op}({arg})" def format_literal_float(self, val) -> str: - value = self._format_float(val.value) + value = self._format_number(val.value) return f"{value}" def format_literal_int(self, val) -> str: diff --git a/ffcx/codegeneration/access.py b/ffcx/codegeneration/access.py index 2b1658d58..b047251ee 100644 --- a/ffcx/codegeneration/access.py +++ b/ffcx/codegeneration/access.py @@ -333,7 +333,7 @@ def facet_edge_vectors(self, e, mt, tabledata, num_points): # Get edge vertices facet = self.symbols.entity("facet", mt.restriction) facet_edge = mt.component[0] - facet_edge_vertices = L.Symbol(f"{cellname}_facet_edge_vertices") + facet_edge_vertices = L.Symbol(f"{cellname}_facet_edge_vertices", dtype=L.DataType.INT) vertex0 = facet_edge_vertices[facet][facet_edge][0] vertex1 = facet_edge_vertices[facet][facet_edge][1] diff --git a/ffcx/codegeneration/geometry.py b/ffcx/codegeneration/geometry.py index bc68375bb..b52f0f473 100644 --- a/ffcx/codegeneration/geometry.py +++ b/ffcx/codegeneration/geometry.py @@ -81,7 +81,7 @@ def reference_edge_vectors(tablename, cellname): topology = basix.topology(celltype) geometry = basix.geometry(celltype) edge_vectors = [geometry[j] - geometry[i] for i, j in topology[1]] - out = np.array(edge_vectors[cellname]) + out = np.array(edge_vectors) symbol = L.Symbol(f"{cellname}_{tablename}", dtype=L.DataType.REAL) return L.ArrayDecl(symbol, values=out, const=True)