diff --git a/cpp/dolfinx/fem/utils.h b/cpp/dolfinx/fem/utils.h index 619a9e02d7c..7f79dfb00ef 100644 --- a/cpp/dolfinx/fem/utils.h +++ b/cpp/dolfinx/fem/utils.h @@ -775,6 +775,10 @@ FunctionSpace create_functionspace( "Cannot specify value shape for non-scalar base element."); } + if (mesh::cell_type_from_basix_type(e.cell_type()) + != mesh->topology()->cell_type()) + throw std::runtime_error("Cell type of element and mesh must match."); + std::size_t bs = value_shape.empty() ? 1 : std::accumulate(value_shape.begin(), value_shape.end(), diff --git a/cpp/test/CMakeLists.txt b/cpp/test/CMakeLists.txt index 53ece3f893f..e652537d80c 100644 --- a/cpp/test/CMakeLists.txt +++ b/cpp/test/CMakeLists.txt @@ -40,6 +40,7 @@ add_executable( common/sub_systems_manager.cpp common/index_map.cpp common/sort.cpp + fem/functionspace.cpp mesh/distributed_mesh.cpp common/CIFailure.cpp refinement/interval.cpp diff --git a/cpp/test/fem/functionspace.cpp b/cpp/test/fem/functionspace.cpp new file mode 100644 index 00000000000..faf3b7233dd --- /dev/null +++ b/cpp/test/fem/functionspace.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2024 Paul Kühner +// +// This file is part of DOLFINX (https://www.fenicsproject.org) +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include + +#include + +#include +#include +#include +#include + +using namespace dolfinx; + +TEST_CASE("Create Function Space (mismatch of elements)", "[functionspace]") +{ + auto mesh = std::make_shared>( + dolfinx::mesh::create_rectangle( + MPI_COMM_SELF, {{{0, 0}, {1, 1}}}, {1, 1}, mesh::CellType::triangle)); + + auto element = basix::create_element( + basix::element::family::P, basix::cell::type::interval, 1, + basix::element::lagrange_variant::unset, + basix::element::dpc_variant::unset, false); + + CHECK_THROWS(fem::create_functionspace(mesh, element, {})); +}