Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dof ordering when generating tensor factors #695

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 54 additions & 41 deletions cpp/basix/e-lagrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,20 @@ create_d_iso(cell::type celltype, int degree, element::lagrange_variant variant,
template <std::floating_point T>
std::vector<std::tuple<std::vector<FiniteElement<T>>, std::vector<int>>>
create_tensor_product_factors(cell::type celltype, int degree,
element::lagrange_variant variant)
element::lagrange_variant variant,
std::vector<int> dof_ordering)
{

if (dof_ordering.size() == 0)
{
std::size_t ndofs = celltype == cell::type::quadrilateral
? (degree + 1) * (degree + 1)
: (degree + 1) * (degree + 1) * (degree + 1);
std::vector<int> d(ndofs);
std::iota(d.begin(), d.end(), 0);
return create_tensor_product_factors<T>(celltype, degree, variant, d);
}

switch (celltype)
{
case cell::type::quadrilateral:
Expand All @@ -871,25 +883,25 @@ create_tensor_product_factors(cell::type celltype, int degree,
cell::type::interval, degree, variant, true);
std::vector<int> perm((degree + 1) * (degree + 1));
if (degree == 0)
perm[0] = 0;
perm[dof_ordering[0]] = 0;
else
{
int p = 0;
int n = degree - 1;
perm[p++] = 0;
perm[p++] = 2;
perm[dof_ordering[p++]] = 0;
perm[dof_ordering[p++]] = 2;
for (int i = 0; i < n; ++i)
perm[p++] = 4 + n + i;
perm[p++] = 1;
perm[p++] = 3;
perm[dof_ordering[p++]] = 4 + n + i;
perm[dof_ordering[p++]] = 1;
perm[dof_ordering[p++]] = 3;
for (int i = 0; i < n; ++i)
perm[p++] = 4 + 2 * n + i;
perm[dof_ordering[p++]] = 4 + 2 * n + i;
for (int i = 0; i < n; ++i)
{
perm[p++] = 4 + i;
perm[p++] = 4 + 3 * n + i;
perm[dof_ordering[p++]] = 4 + i;
perm[dof_ordering[p++]] = 4 + 3 * n + i;
for (int j = 0; j < n; ++j)
perm[p++] = 4 + i + (4 + j) * n;
perm[dof_ordering[p++]] = 4 + i + (4 + j) * n;
}
}
return {{{sub_element, sub_element}, std::move(perm)}};
Expand All @@ -900,57 +912,58 @@ create_tensor_product_factors(cell::type celltype, int degree,
cell::type::interval, degree, variant, true);
std::vector<int> perm((degree + 1) * (degree + 1) * (degree + 1));
if (degree == 0)
perm[0] = 0;
perm[dof_ordering[0]] = 0;
else
{
int p = 0;
int n = degree - 1;
perm[p++] = 0;
perm[p++] = 4;
perm[dof_ordering[p++]] = 0;
perm[dof_ordering[p++]] = 4;
for (int i = 0; i < n; ++i)
perm[p++] = 8 + 2 * n + i;
perm[p++] = 2;
perm[p++] = 6;
perm[dof_ordering[p++]] = 8 + 2 * n + i;
perm[dof_ordering[p++]] = 2;
perm[dof_ordering[p++]] = 6;
for (int i = 0; i < n; ++i)
perm[p++] = 8 + 6 * n + i;
perm[dof_ordering[p++]] = 8 + 6 * n + i;
for (int i = 0; i < n; ++i)
{
perm[p++] = 8 + n + i;
perm[p++] = 8 + 9 * n + i;
perm[dof_ordering[p++]] = 8 + n + i;
perm[dof_ordering[p++]] = 8 + 9 * n + i;
for (int j = 0; j < n; ++j)
perm[p++] = 8 + 12 * n + 2 * n * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 12 * n + 2 * n * n + i + n * j;
}
perm[p++] = 1;
perm[p++] = 5;
perm[dof_ordering[p++]] = 1;
perm[dof_ordering[p++]] = 5;
for (int i = 0; i < n; ++i)
perm[p++] = 8 + 4 * n + i;
perm[p++] = 3;
perm[p++] = 7;
perm[dof_ordering[p++]] = 8 + 4 * n + i;
perm[dof_ordering[p++]] = 3;
perm[dof_ordering[p++]] = 7;
for (int i = 0; i < n; ++i)
perm[p++] = 8 + 7 * n + i;
perm[dof_ordering[p++]] = 8 + 7 * n + i;
for (int i = 0; i < n; ++i)
{
perm[p++] = 8 + 3 * n + i;
perm[p++] = 8 + 10 * n + i;
perm[dof_ordering[p++]] = 8 + 3 * n + i;
perm[dof_ordering[p++]] = 8 + 10 * n + i;
for (int j = 0; j < n; ++j)
perm[p++] = 8 + 12 * n + 3 * n * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 12 * n + 3 * n * n + i + n * j;
}
for (int i = 0; i < n; ++i)
{
perm[p++] = 8 + i;
perm[p++] = 8 + 8 * n + i;
perm[dof_ordering[p++]] = 8 + i;
perm[dof_ordering[p++]] = 8 + 8 * n + i;
for (int j = 0; j < n; ++j)
perm[p++] = 8 + 12 * n + n * n + i + n * j;
perm[p++] = 8 + 5 * n + i;
perm[p++] = 8 + 11 * n + i;
perm[dof_ordering[p++]] = 8 + 12 * n + n * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 5 * n + i;
perm[dof_ordering[p++]] = 8 + 11 * n + i;
for (int j = 0; j < n; ++j)
perm[p++] = 8 + 12 * n + 4 * n * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 12 * n + 4 * n * n + i + n * j;
for (int j = 0; j < n; ++j)
{
perm[p++] = 8 + 12 * n + i + n * j;
perm[p++] = 8 + 12 * n + 5 * n * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 12 * n + i + n * j;
perm[dof_ordering[p++]] = 8 + 12 * n + 5 * n * n + i + n * j;
for (int k = 0; k < n; ++k)
perm[p++] = 8 + 12 * n + 6 * n * n + i + n * j + n * n * k;
perm[dof_ordering[p++]]
= 8 + 12 * n + 6 * n * n + i + n * j + n * n * k;
}
}
}
Expand Down Expand Up @@ -1420,8 +1433,8 @@ basix::element::create_lagrange(cell::type celltype, int degree,

sobolev::space space
= discontinuous ? sobolev::space::L2 : sobolev::space::H1;
auto tensor_factors
= create_tensor_product_factors<T>(celltype, degree, variant);
auto tensor_factors = create_tensor_product_factors<T>(celltype, degree,
variant, dof_ordering);
return FiniteElement<T>(
family::P, celltype, polyset::type::standard, degree, {},
impl::mdspan_t<T, 2>(math::eye<T>(ndofs).data(), ndofs, ndofs), xview,
Expand Down
17 changes: 17 additions & 0 deletions test/test_tensor_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,20 @@ def test_tensor_product_factorisation_hexahedron(degree):

dphi_tensor = dphi_tensor.reshape([Nq*Nq*Nq, Nd*Nd*Nd])
assert numpy.allclose(dphi_z, dphi_tensor)


@pytest.mark.parametrize("cell_type", [
basix.CellType.quadrilateral,
basix.CellType.hexahedron,
])
@pytest.mark.parametrize("family, args", [
(basix.ElementFamily.P, (basix.LagrangeVariant.equispaced, )),
(basix.ElementFamily.P, (basix.LagrangeVariant.gll_warped, )),
])
@pytest.mark.parametrize("degree", range(1, 5))
def test_dof_ordering(cell_type, family, args, degree):
e = basix.create_element(family, cell_type, degree, *args)
perm = e.get_tensor_product_representation()[0][1]
e2 = basix.create_element(family, cell_type, degree, *args, dof_ordering=perm)
for i, j in enumerate(e2.get_tensor_product_representation()[0][1]):
assert i == j
Loading