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

Add check of element hash #3186

Merged
merged 15 commits into from
May 2, 2024
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install-python-components: &install-python-components
cmake --install build-dir
pip3 install ./basix/python
pip3 install git+https://github.com/FEniCS/ufl.git
pip3 install git+https://github.com/FEniCS/ffcx.git
pip3 install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash

configure-cpp: &configure-cpp
name: Configure (C++)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
run: |
pip install git+https://github.com/FEniCS/ufl.git
pip install git+https://github.com/FEniCS/basix.git
pip install git+https://github.com/FEniCS/ffcx.git
pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash
- name: Install FEniCS Python components
if: github.event_name == 'workflow_dispatch'
run: |
Expand Down Expand Up @@ -162,7 +162,7 @@ jobs:
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git
python3 -m pip install git+https://github.com/FEniCS/basix.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash
- name: Install FEniCS Python components
if: github.event_name == 'workflow_dispatch'
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
run: |
python -m pip install git+https://github.com/FEniCS/basix.git
python -m pip install git+https://github.com/FEniCS/ufl.git
python -m pip install git+https://github.com/FEniCS/ffcx.git
python -m pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash

- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/oneapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
with:
path: ./ffcx
repository: FEniCS/ffcx
ref: main
ref: mscroggs/basix-hash
- name: Install FFCx C interface
run: |
. /opt/intel/oneapi/setvars.sh
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pyvista.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git
python3 -m pip install git+https://github.com/FEniCS/basix.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash
apt-get update
apt-get install -y --no-install-recommends libgl1-mesa-dev xvfb # pyvista
apt-get install -y --no-install-recommends python3-pyqt5 libgl1-mesa-glx # pyvistaqt
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git
python3 -m pip install git+https://github.com/FEniCS/basix.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash

- name: Configure C++
run: cmake -G Ninja -DCMAKE_BUILD_TYPE=Developer -B build -S cpp/
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
run: |
python3 -m pip install git+https://github.com/FEniCS/ufl.git
python3 -m pip install git+https://github.com/FEniCS/basix.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@mscroggs/basix-hash
- name: Run build-wrapper
run: |
mkdir build
Expand Down
9 changes: 7 additions & 2 deletions cpp/dolfinx/fem/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,17 @@ Form<T, U> create_form_factory(
"Mismatch between number of expected and provided Form constants.");
}

// Check argument function spaces
#ifndef NDEBUG
// Check argument function spaces
for (std::size_t i = 0; i < spaces.size(); ++i)
{
assert(spaces[i]->element());
// TODO: Hash check of element
auto ufcx_element = ufcx_form.finite_elements[i];
if (ufcx_element != 0 && ufcx_element != spaces[i]->element()->basix_element().hash())
{
throw std::runtime_error(
"Cannot create form. Elements are different to those used to compile the form.");
}
}
#endif

Expand Down
89 changes: 89 additions & 0 deletions python/test/unit/fem/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

import pytest

import basix
import basix.ufl
import dolfinx
from dolfinx.fem import extract_function_spaces, form, functionspace
from dolfinx.fem.forms import form_cpp_class
from dolfinx.mesh import create_unit_square
from ufl import TestFunction, TrialFunction, dx, inner

Expand Down Expand Up @@ -48,3 +52,88 @@ def test_extract_forms():
assert Vr[1] is V1._cpp_object
with pytest.raises(AssertionError):
extract_function_spaces(a, 1)


def test_incorrect_element():
"""Test that an error is raised if an incorrect element is used."""
if not dolfinx.common.has_debug:
pytest.skip("Error will only be thrown for incorrect spaecs in debug mode.")

dtype = dolfinx.default_scalar_type

mesh = create_unit_square(MPI.COMM_WORLD, 32, 31)
element = basix.ufl.element(
"Lagrange",
"triangle",
4,
lagrange_variant=basix.LagrangeVariant.gll_warped,
dtype=dtype,
)
incorrect_element = basix.ufl.element(
"Lagrange",
"triangle",
4,
lagrange_variant=basix.LagrangeVariant.equispaced,
dtype=dtype,
)

space = functionspace(mesh, element)
incorrect_space = functionspace(mesh, incorrect_element)

u = TrialFunction(space)
v = TestFunction(space)

a = inner(u, v) * dx

ftype = form_cpp_class(dtype)

ufcx_form, module, code = dolfinx.jit.ffcx_jit(
mesh.comm, a, form_compiler_options={"scalar_type": dtype}
)

f = ftype(
module.ffi.cast("uintptr_t", module.ffi.addressof(ufcx_form)),
[space._cpp_object, space._cpp_object],
[],
[],
{dolfinx.cpp.fem.IntegralType.cell: []},
{},
mesh._cpp_object,
)
dolfinx.fem.Form(f, ufcx_form, code)

with pytest.raises(RuntimeError):
f = ftype(
module.ffi.cast("uintptr_t", module.ffi.addressof(ufcx_form)),
[incorrect_space._cpp_object, space._cpp_object],
[],
[],
{dolfinx.cpp.fem.IntegralType.cell: []},
{},
mesh._cpp_object,
)
dolfinx.fem.Form(f, ufcx_form, code)

with pytest.raises(RuntimeError):
f = ftype(
module.ffi.cast("uintptr_t", module.ffi.addressof(ufcx_form)),
[space._cpp_object, incorrect_space._cpp_object],
[],
[],
{dolfinx.cpp.fem.IntegralType.cell: []},
{},
mesh._cpp_object,
)
dolfinx.fem.Form(f, ufcx_form, code)

with pytest.raises(RuntimeError):
f = ftype(
module.ffi.cast("uintptr_t", module.ffi.addressof(ufcx_form)),
[incorrect_space._cpp_object, incorrect_space._cpp_object],
[],
[],
{dolfinx.cpp.fem.IntegralType.cell: []},
{},
mesh._cpp_object,
)
dolfinx.fem.Form(f, ufcx_form, code)
mscroggs marked this conversation as resolved.
Show resolved Hide resolved
Loading