Skip to content

Commit

Permalink
Fixes for obscure functionality (which is probably broken in main)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrichardson committed Aug 29, 2023
1 parent cdf41a5 commit 9afe210
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion demo/CellGeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
14 changes: 10 additions & 4 deletions ffcx/codegeneration/C/c_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "}"
Expand All @@ -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}")

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion ffcx/codegeneration/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion ffcx/codegeneration/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9afe210

Please sign in to comment.