forked from FEniCS/fiat
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Johnson Mercier as PhysicallyMappedElement (#126)
Co-authored-by: Rob Kirby <robert.c.kirby@gmail.com>
- Loading branch information
Showing
8 changed files
with
237 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import FIAT | ||
import numpy | ||
from gem import ListTensor, Literal | ||
|
||
from finat.aw import _facet_transform | ||
from finat.fiat_elements import FiatElement | ||
from finat.physically_mapped import Citations, PhysicallyMappedElement | ||
|
||
|
||
class JohnsonMercier(PhysicallyMappedElement, FiatElement): # symmetric matrix valued | ||
def __init__(self, cell, degree, variant=None): | ||
if degree != 1: | ||
raise ValueError("Degree must be 1 for Johnson-Mercier element") | ||
if Citations is not None: | ||
Citations().register("Gopalakrishnan2024") | ||
self._indices = slice(None, None) | ||
super(JohnsonMercier, self).__init__(FIAT.JohnsonMercier(cell, degree, variant=variant)) | ||
|
||
def basis_transformation(self, coordinate_mapping): | ||
numbf = self._element.space_dimension() | ||
ndof = self.space_dimension() | ||
V = numpy.eye(numbf, ndof, dtype=object) | ||
for multiindex in numpy.ndindex(V.shape): | ||
V[multiindex] = Literal(V[multiindex]) | ||
|
||
Vsub = _facet_transform(self.cell, 1, coordinate_mapping) | ||
Vsub = Vsub[:, self._indices] | ||
m, n = Vsub.shape | ||
V[:m, :n] = Vsub | ||
|
||
# Note: that the edge DOFs are scaled by edge lengths in FIAT implies | ||
# that they are already have the necessary rescaling to improve | ||
# conditioning. | ||
return ListTensor(V.T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
import FIAT | ||
import finat | ||
import numpy as np | ||
import pytest | ||
from gem.interpreter import evaluate | ||
|
||
from fiat_mapping import MyMapping | ||
|
||
|
||
def test_hct(): | ||
degree = 3 | ||
ref_cell = FIAT.ufc_simplex(2) | ||
ref_pts = finat.point_set.PointSet(FIAT.reference_element.make_lattice(ref_cell.vertices, degree)) | ||
ref_element = finat.HsiehCloughTocher(ref_cell, degree, avg=True) | ||
@pytest.fixture | ||
def ref_cell(request): | ||
K = FIAT.ufc_simplex(2) | ||
return K | ||
|
||
phys_cell = FIAT.ufc_simplex(2) | ||
phys_cell.vertices = ((0.0, 0.1), (1.17, -0.09), (0.15, 1.84)) | ||
|
||
mapping = MyMapping(ref_cell, phys_cell) | ||
z = (0, 0) | ||
finat_vals_gem = ref_element.basis_evaluation(0, ref_pts, coordinate_mapping=mapping)[z] | ||
finat_vals = evaluate([finat_vals_gem])[0].arr | ||
@pytest.fixture | ||
def phys_cell(request): | ||
K = FIAT.ufc_simplex(2) | ||
K.vertices = ((0.0, 0.1), (1.17, -0.09), (0.15, 1.84)) | ||
return K | ||
|
||
phys_cell_FIAT = FIAT.HsiehCloughTocher(phys_cell, degree) | ||
phys_points = FIAT.reference_element.make_lattice(phys_cell.vertices, degree) | ||
phys_vals = phys_cell_FIAT.tabulate(0, phys_points)[z] | ||
|
||
assert np.allclose(finat_vals.T, phys_vals) | ||
def make_unisolvent_points(element): | ||
degree = element.degree() | ||
ref_complex = element.get_reference_complex() | ||
sd = ref_complex.get_spatial_dimension() | ||
top = ref_complex.get_topology() | ||
pts = [] | ||
for cell in top[sd]: | ||
pts.extend(ref_complex.make_points(sd, cell, degree+sd+1)) | ||
return pts | ||
|
||
|
||
def test_reduced_hct(): | ||
@pytest.mark.parametrize("reduced", (False, True), ids=("standard", "reduced")) | ||
def test_hct(ref_cell, phys_cell, reduced): | ||
degree = 3 | ||
ref_cell = FIAT.ufc_simplex(2) | ||
ref_pts = finat.point_set.PointSet(FIAT.reference_element.make_lattice(ref_cell.vertices, degree)) | ||
ref_element = finat.ReducedHsiehCloughTocher(ref_cell, degree) | ||
|
||
phys_cell = FIAT.ufc_simplex(2) | ||
phys_cell.vertices = ((0.0, 0.1), (1.17, -0.09), (0.15, 1.84)) | ||
if reduced: | ||
ref_element = finat.ReducedHsiehCloughTocher(ref_cell, degree) | ||
else: | ||
ref_element = finat.HsiehCloughTocher(ref_cell, degree, avg=True) | ||
ref_pts = finat.point_set.PointSet(make_unisolvent_points(ref_element._element)) | ||
|
||
mapping = MyMapping(ref_cell, phys_cell) | ||
z = (0, 0) | ||
z = (0,) * ref_element.cell.get_spatial_dimension() | ||
finat_vals_gem = ref_element.basis_evaluation(0, ref_pts, coordinate_mapping=mapping)[z] | ||
finat_vals = evaluate([finat_vals_gem])[0].arr | ||
|
||
phys_cell_FIAT = FIAT.HsiehCloughTocher(phys_cell, degree, reduced=True) | ||
phys_points = FIAT.reference_element.make_lattice(phys_cell.vertices, degree) | ||
phys_vals = phys_cell_FIAT.tabulate(0, phys_points)[z] | ||
phys_element = FIAT.HsiehCloughTocher(phys_cell, degree, reduced=reduced) | ||
phys_points = make_unisolvent_points(phys_element) | ||
phys_vals = phys_element.tabulate(0, phys_points)[z] | ||
|
||
numbf = ref_element.space_dimension() | ||
assert np.allclose(finat_vals.T, phys_vals[:numbf]) |
Oops, something went wrong.