diff --git a/.github/actions/install/action.yml b/.github/actions/install/action.yml index 923db50284..56f1bdc3b3 100644 --- a/.github/actions/install/action.yml +++ b/.github/actions/install/action.yml @@ -99,6 +99,9 @@ runs: exit 1 fi + : # DROP BEFORE MERGE: apply PETSc MR 9901 + curl -fsSL https://gitlab.com/petsc/petsc/-/merge_requests/9901.diff | git -C petsc apply + if [ ${{ inputs.os }} = 'linux' ]; then NPROCS=8 elif [ ${{ inputs.os }} = 'macos' ]; then diff --git a/firedrake/mesh.py b/firedrake/mesh.py index 03cb63183c..9515a78b5a 100644 --- a/firedrake/mesh.py +++ b/firedrake/mesh.py @@ -405,16 +405,14 @@ def _from_triangle(filename, dim, comm): with temp_internal_comm(comm) as icomm: if icomm.rank == 0: - try: - facetfile = open(basename+".face") - tdim = 3 - except FileNotFoundError: - try: - facetfile = open(basename+".edge") - tdim = 2 - except FileNotFoundError: - facetfile = None - tdim = 1 + with open(basename+".ele") as elefile: + header = np.fromfile(elefile, dtype=np.int32, count=2, sep=' ') + elecount = header[0] + eledim = header[1] + eles = np.loadtxt(elefile, usecols=list(range(1, eledim+1)), dtype=np.int32, skiprows=1, ndmin=2) + assert elecount == eles.shape[0] + # Each cell is a simplex with tdim + 1 vertices. + tdim = eledim - 1 if dim is None: dim = tdim icomm.bcast(tdim, root=0) @@ -427,13 +425,6 @@ def _from_triangle(filename, dim, comm): coordinates = np.loadtxt(nodefile, usecols=list(range(1, dim+1)), skiprows=1, dtype=np.double) assert nodecount == coordinates.shape[0] - with open(basename+".ele") as elefile: - header = np.fromfile(elefile, dtype=np.int32, count=2, sep=' ') - elecount = header[0] - eledim = header[1] - eles = np.loadtxt(elefile, usecols=list(range(1, eledim+1)), dtype=np.int32, skiprows=1) - assert elecount == eles.shape[0] - cells = list(map(lambda c: c-1, eles)) else: tdim = icomm.bcast(None, root=0) @@ -443,21 +434,22 @@ def _from_triangle(filename, dim, comm): # Apply boundary IDs if icomm.rank == 0: + # Triangle (2D) and TetGen (3D) write the optional facet file. + facetfilename = basename + {2: ".edge", 3: ".face"}[tdim] facets = None - try: - header = np.fromfile(facetfile, dtype=np.int32, count=2, sep=' ') - edgecount = header[0] - facets = np.loadtxt(facetfile, usecols=list(range(1, tdim+2)), dtype=np.int32, skiprows=0) - assert edgecount == facets.shape[0] - finally: - facetfile.close() + if os.path.isfile(facetfilename): + with open(facetfilename) as facetfile: + header = np.fromfile(facetfile, dtype=np.int32, count=2, sep=' ') + edgecount = header[0] + facets = np.loadtxt(facetfile, usecols=list(range(1, tdim+2)), dtype=np.int32, skiprows=0, ndmin=2) + assert edgecount == facets.shape[0] if facets is not None: vStart, vEnd = plex.getDepthStratum(0) # vertices for facet in facets: bid = facet[-1] vertices = list(map(lambda v: v + vStart - 1, facet[:-1])) - join = plex.getJoin(vertices) + join = plex.getFullJoin(vertices) plex.setLabelValue(dmcommon.FACE_SETS_LABEL, join[0], bid) return plex diff --git a/scripts/firedrake-configure b/scripts/firedrake-configure index 318dcd8616..3f401f2076 100755 --- a/scripts/firedrake-configure +++ b/scripts/firedrake-configure @@ -440,6 +440,11 @@ class CommunityArch(Arch): ArchKey = tuple[OS, ScalarType, IntType, GPUPlatform] # Configurations with official support that are tested in CI +# +# On Ubuntu, we tell PETSc to download Exodus II, because the Exodus II library +# from apt does not have ex_open_par, which PETSc needs. We also tell PETSc the +# paths to the HDF5 and NetCDF libraries from apt, because the CMake builds of +# CGNS and Exodus II cannot find them. OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { # Ubuntu 26.04 x86 @@ -467,7 +472,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libhypre-dev", "libmumps-ptscotch-dev", "libmetis-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -486,8 +491,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--with-hypre", "--with-metis", "--with-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/x86_64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/x86_64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/netCDF_pnetcdf'", "--with-ptscotch", "--with-ptscotch-lib='-lptesmumps -lptscotch -lptscotcherr -lesmumps -lscotch -lscotcherr'", "--with-ptscotch-include=/usr/include/scotch", @@ -528,7 +540,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libhdf5-mpi-dev", "libmumps-ptscotch-dev", "libmetis-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -547,8 +559,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--with-hwloc", "--with-metis", "--with-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/x86_64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/x86_64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/netCDF_pnetcdf'", "--with-ptscotch", "--with-ptscotch-lib='-lptesmumps -lptscotch -lptscotcherr -lesmumps -lscotch -lscotcherr'", "--with-ptscotch-include=/usr/include/scotch", @@ -585,7 +604,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libfftw3-mpi-dev", "libhdf5-mpi-dev", "libhwloc-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -602,8 +621,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--download-hypre", "--download-metis", "--download-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/x86_64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/x86_64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/netCDF_pnetcdf'", "--download-ptscotch", "--with-scalapack-lib=-lscalapack-openmpi", "--with-suitesparse", @@ -647,7 +673,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libhypre-dev", "libmumps-ptscotch-dev", "libmetis-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -666,8 +692,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--with-hypre", "--with-metis", "--with-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/aarch64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/aarch64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/aarch64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/aarch64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/aarch64-linux-gnu/cmake/netCDF_pnetcdf'", "--with-ptscotch", "--with-ptscotch-lib='-lptesmumps -lptscotch -lptscotcherr -lesmumps -lscotch -lscotcherr'", "--with-ptscotch-include=/usr/include/scotch", @@ -708,7 +741,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libhdf5-mpi-dev", "libmumps-ptscotch-dev", "libmetis-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -727,8 +760,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--with-hwloc", "--with-metis", "--with-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/aarch64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/aarch64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/aarch64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/aarch64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/aarch64-linux-gnu/cmake/netCDF_pnetcdf'", "--with-ptscotch", "--with-ptscotch-lib='-lptesmumps -lptscotch -lptscotcherr -lesmumps -lscotch -lscotcherr'", "--with-ptscotch-include=/usr/include/scotch", @@ -770,7 +810,7 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "libhwloc-dev", "libmumps-ptscotch-dev", "libmetis-dev", - "libnetcdf-dev", + "libnetcdf-pnetcdf-dev", "libopenblas-dev", "libopenmpi-dev", "libpnetcdf-dev", @@ -797,8 +837,15 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--with-hwloc", "--with-metis", "--with-mumps", - "--with-netcdf", + "--with-netcdf-dir=/usr/lib/x86_64-linux-gnu/netcdf/pnetcdf", "--with-pnetcdf", + "--download-cgns", + "--download-cgns-cmake-arguments=-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi", + "--download-exodusii", + "--download-exodusii-cmake-arguments='" + "-DHDF5_ROOT:PATH=/usr/lib/x86_64-linux-gnu/hdf5/openmpi " + "-DPnetcdf_LIBRARY_DIRS:PATH=/usr/lib/x86_64-linux-gnu " + "-DnetCDF_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/netCDF_pnetcdf'", "--with-ptscotch", "--with-ptscotch-lib='-lptesmumps -lptscotch -lptscotcherr -lesmumps -lscotch -lscotcherr'", "--with-ptscotch-include=/usr/include/scotch", @@ -864,6 +911,8 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--download-mumps", "--download-netcdf", "--download-pnetcdf", + "--download-cgns", + "--download-exodusii", "--download-ptscotch", "--download-superlu_dist", @@ -920,6 +969,8 @@ OFFICIAL_ARCHS: Mapping[ArchKey, Arch] = { "--download-mumps", "--download-netcdf", "--download-pnetcdf", + "--download-cgns", + "--download-exodusii", "--download-ptscotch", "--download-superlu_dist", diff --git a/tests/firedrake/conftest.py b/tests/firedrake/conftest.py index 1f0f1e21c7..5da285646a 100644 --- a/tests/firedrake/conftest.py +++ b/tests/firedrake/conftest.py @@ -103,16 +103,18 @@ def _skip_test_dependency(dependency): except ImportError: return skip - elif dependency in ("mumps", "hypre"): + elif dependency in ("mumps", "hypre", "exodusii", "cgns"): return dependency not in get_external_packages() else: - raise ValueError("Unrecognised dependency to check: {dependency = }") + raise ValueError(f"Unrecognised dependency to check: {dependency}") dependency_skip_markers_and_reasons = ( ("mumps", "skipmumps", "MUMPS not installed with PETSc"), ("hypre", "skiphypre", "hypre not installed with PETSc"), + ("exodusii", "skipexodusii", "ExodusII not installed with PETSc"), + ("cgns", "skipcgns", "CGNS not installed with PETSc"), ("slepc", "skipslepc", "SLEPc is not installed"), ("pytorch", "skiptorch", "PyTorch is not installed"), ("jax", "skipjax", "JAX is not installed"), @@ -139,6 +141,14 @@ def pytest_configure(config): "markers", "skiphypre: mark as skipped unless hypre is installed" ) + config.addinivalue_line( + "markers", + "skipexodusii: mark as skipped unless ExodusII is installed" + ) + config.addinivalue_line( + "markers", + "skipcgns: mark as skipped unless CGNS is installed" + ) config.addinivalue_line( "markers", "skipcomplex: mark as skipped in complex mode" diff --git a/tests/firedrake/regression/test_exodus_mesh.py b/tests/firedrake/regression/test_exodus_mesh.py index bb2cac177b..0af5693305 100644 --- a/tests/firedrake/regression/test_exodus_mesh.py +++ b/tests/firedrake/regression/test_exodus_mesh.py @@ -1,30 +1,22 @@ from os.path import abspath, dirname, join import pytest from firedrake import * -from firedrake.petsc import PETSc + +pytestmark = pytest.mark.skipexodusii cwd = abspath(dirname(__file__)) @pytest.fixture def exodus_mesh(): - try: - return Mesh(join(cwd, "..", "meshes", "brick.e")) - except PETSc.Error as e: - assert e.ierr == 56 + return Mesh(join(cwd, "..", "meshes", "brick.e")) @pytest.mark.parallel(nprocs=2) def test_create_from_file(exodus_mesh): - if exodus_mesh is None: - pytest.skip("PETSc not configured with exodusII") - else: - assert exodus_mesh + assert exodus_mesh @pytest.mark.parallel(nprocs=2) def test_sidesets(exodus_mesh): - if exodus_mesh is None: - pytest.skip("PETSc not configured with exodusII") - else: - assert (exodus_mesh.exterior_facets.unique_markers == [200, 201]).all() + assert (exodus_mesh.exterior_facets.unique_markers == [200, 201]).all() diff --git a/tests/firedrake/regression/test_load_mesh.py b/tests/firedrake/regression/test_load_mesh.py index a6bc2ff2f9..19edcf5a40 100644 --- a/tests/firedrake/regression/test_load_mesh.py +++ b/tests/firedrake/regression/test_load_mesh.py @@ -1,6 +1,7 @@ from math import pi from firedrake import * +from firedrake.petsc import PETSc import numpy as np import pytest from os.path import abspath, dirname, join @@ -29,6 +30,79 @@ def test_load_mesh(filename, use_path_object): assert np.allclose(v, 1) +# --- Triangle meshes --- + + +def load_triangle_mesh(tmp_path, *, with_edge_file): + # Split the unit square into two triangles. + (tmp_path / "square.node").write_text("4 2 0 0\n1 0 0\n2 1 0\n3 1 1\n4 0 1\n") + (tmp_path / "square.ele").write_text("2 3 0\n1 1 2 3\n2 1 3 4\n") + if with_edge_file: + # Mark side k of the square with boundary id k, counterclockwise from + # the side on y = 0. + (tmp_path / "square.edge").write_text("4 1\n1 1 2 1\n2 2 3 2\n3 3 4 3\n4 4 1 4\n") + return Mesh(tmp_path / "square.node") + + +@pytest.mark.parametrize("with_edge_file", [True, False], ids=["with-edge-file", "without-edge-file"]) +def test_load_triangle_mesh_volume(tmp_path, with_edge_file): + mesh = load_triangle_mesh(tmp_path, with_edge_file=with_edge_file) + assert np.isclose(assemble(1*dx(domain=mesh)), 1) + + +@pytest.mark.parametrize("subdomain_id", [1, 2, 3, 4]) +def test_load_triangle_mesh_boundary_ids(tmp_path, subdomain_id): + mesh = load_triangle_mesh(tmp_path, with_edge_file=True) + assert np.isclose(assemble(1*ds(subdomain_id, domain=mesh)), 1) + + +# --- TetGen meshes --- + + +def load_tetgen_mesh(tmp_path, *, with_face_file): + # Write one tetrahedron with vertices at the origin and at the unit + # vectors. + (tmp_path / "tet.node").write_text("4 3 0 0\n1 0 0 0\n2 1 0 0\n3 0 1 0\n4 0 0 1\n") + (tmp_path / "tet.ele").write_text("1 4 0\n1 1 2 3 4\n") + if with_face_file: + # Mark the face opposite vertex k with boundary id k. + (tmp_path / "tet.face").write_text("4 1\n1 2 3 4 1\n2 1 3 4 2\n3 1 2 4 3\n4 1 2 3 4\n") + return Mesh(tmp_path / "tet.node") + + +@pytest.mark.parametrize("with_face_file", [True, False], ids=["with-face-file", "without-face-file"]) +def test_load_tetgen_mesh_volume(tmp_path, with_face_file): + mesh = load_tetgen_mesh(tmp_path, with_face_file=with_face_file) + assert np.isclose(assemble(1*dx(domain=mesh)), 1/6) + + +@pytest.mark.parametrize("subdomain_id, area", [(1, np.sqrt(3)/2), (2, 1/2), (3, 1/2), (4, 1/2)]) +def test_load_tetgen_mesh_boundary_ids(tmp_path, subdomain_id, area): + mesh = load_tetgen_mesh(tmp_path, with_face_file=True) + assert np.isclose(assemble(1*ds(subdomain_id, domain=mesh)), area) + + +# --- CGNS meshes --- + + +@pytest.mark.skipcgns +def test_load_cgns_mesh_volume(tmp_path): + # Write the two triangles of the unit square in a single CGNS section. + filename = str(tmp_path / "square.cgns") + cells = [[0, 1, 2], [0, 2, 3]] + coordinates = [[0, 0], [1, 0], [1, 1], [0, 1]] + plex = PETSc.DMPlex().createFromCellList(2, cells, coordinates) + viewer = PETSc.Viewer().create() + viewer.setType("cgns") + viewer.setFileMode("w") + viewer.setFileName(filename) + plex.view(viewer) + viewer.destroy() + + mesh = Mesh(filename) + assert np.isclose(assemble(1*dx(domain=mesh)), 1) + + # --- Periodic Gmsh meshes ---