Skip to content

Commit 70683e0

Browse files
authored
Cache split cells (#128)
* Cache split cells
1 parent 7aa40e6 commit 70683e0

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

FIAT/macro.py

+14
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ class AlfeldSplit(PowellSabinSplit):
304304
"""Splits a simplicial complex by connecting cell vertices to their
305305
barycenter.
306306
"""
307+
def __new__(cls, ref_el):
308+
try:
309+
return ref_el._split_cache[cls]
310+
except KeyError:
311+
self = super().__new__(cls)
312+
return ref_el._split_cache.setdefault(cls, self)
313+
307314
def __init__(self, ref_el):
308315
sd = ref_el.get_spatial_dimension()
309316
super().__init__(ref_el, dimension=sd)
@@ -313,6 +320,13 @@ class WorseyFarinSplit(PowellSabinSplit):
313320
"""Splits a simplicial complex by connecting cell and facet vertices to their
314321
barycenter. This reduces to Powell-Sabin on the triangle, and Alfeld on the interval.
315322
"""
323+
def __new__(cls, ref_el):
324+
try:
325+
return ref_el._split_cache[cls]
326+
except KeyError:
327+
self = super().__new__(cls)
328+
return ref_el._split_cache.setdefault(cls, self)
329+
316330
def __init__(self, ref_el):
317331
sd = ref_el.get_spatial_dimension()
318332
super().__init__(ref_el, dimension=sd-1)

FIAT/reference_element.py

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def __init__(self, shape, vertices, topology):
181181
d01_entities = tuple(e for d, e in neighbors if d == dim1)
182182
self.connectivity[(dim0, dim1)].append(d01_entities)
183183

184+
# Dictionary with derived cells
185+
self._split_cache = {}
186+
184187
def _key(self):
185188
"""Hashable object key data (excluding type)."""
186189
# Default: only type matters

test/FIAT/unit/test_macro.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def cell(request):
1616
return ufc_simplex(dim)
1717

1818

19+
def test_split_cache(cell):
20+
A = AlfeldSplit(cell)
21+
B = AlfeldSplit(cell)
22+
assert B is A
23+
fe = Lagrange(cell, 1, variant="alfeld")
24+
C = fe.ref_complex
25+
assert C is A
26+
27+
1928
@pytest.mark.parametrize("split", (AlfeldSplit, IsoSplit))
2029
def test_split_entity_transform(split, cell):
2130
split_cell = split(cell)

0 commit comments

Comments
 (0)