Subject: [RFC PATCH v2] gnu: Add mfem.

From: "Paul A. Patience" <[email-masked]>
Date: Fri, 29 Mar 2024 04:40:43 +0000

* gnu/packages/maths.scm (mfem): New variable.
* gnu/packages/patches/mfem-fix-mk-file-export.patch: New file.
* gnu/packages/patches/mfem-remove-source-dir-refs.patch: Same.
* gnu/local.mk (dist_patch_DATA): Register them.

Change-Id: Ida81f2b081a016e91426b68437909653020976f5
---
The package is basically ready now, except for the suspicious ownership
mentioned in the previous patch.
I've tested it on a local copy of MFEM.
(The OpenMPI version is trickier to compile.)

 gnu/local.mk                                  |   2 +
 gnu/packages/maths.scm                        | 171 ++++++++++++++++++
 .../patches/mfem-fix-mk-file-export.patch     |  29 +++
 .../patches/mfem-remove-source-dir-refs.patch | 101 +++++++++++
 4 files changed, 303 insertions(+)
 create mode 100644 gnu/packages/patches/mfem-fix-mk-file-export.patch
 create mode 100644 gnu/packages/patches/mfem-remove-source-dir-refs.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 5429dc498c..dcedef5f99 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1682,6 +1682,8 @@ dist_patch_DATA =						\
   %D%/packages/patches/memtest86+-build-reproducibly.patch	\
   %D%/packages/patches/mercurial-hg-extension-path.patch	\
   %D%/packages/patches/mercurial-openssl-compat.patch		\
+  %D%/packages/patches/mfem-fix-mk-file-export.patch		\
+  %D%/packages/patches/mfem-remove-source-dir-refs.patch	\
   %D%/packages/patches/mhash-keygen-test-segfault.patch		\
   %D%/packages/patches/mia-fix-boost-headers.patch		\
   %D%/packages/patches/mia-vtk9.patch				\
diff --git a/gnu/packages/maths.scm b/gnu/packages/maths.scm
index 1b4d325649..2191b5e2da 100644
--- a/gnu/packages/maths.scm
+++ b/gnu/packages/maths.scm
@@ -151,6 +151,7 @@ (define-module (gnu packages maths)
   #:use-module (gnu packages xorg)
   #:use-module (gnu packages gl)
   #:use-module (gnu packages imagemagick)
+  #:use-module (gnu packages libunwind)
   #:use-module (gnu packages m4)
   #:use-module (gnu packages mpi)
   #:use-module (gnu packages multiprecision)
@@ -6890,6 +6891,176 @@ (define-public dealii-openmpi
             (delete 'unset-cpath)))))
     (synopsis "Finite element library (with MPI support)")))
 
+(define-public mfem
+  (package
+    (name "mfem")
+    (version "4.6")
+    ;; (source
+    ;;  (origin
+    ;;    ;; (method url-fetch)
+    ;;    ;; (uri (string-append "https://github.com/mfem/mfem/archive/refs/tags/v"
+    ;;    ;;                     version ".tar.gz"))
+    ;;    ;; (file-name (string-append name "-" version ".tar.gz"))
+    ;;    (method git-fetch)
+    ;;    (uri (git-reference
+    ;;          (url "https://github.com/mfem/mfem")
+    ;;          (commit (string-append "v" version))))
+    ;;    (file-name (git-file-name name version))
+    ;;    (sha256
+    ;;     (base32 "0m57j5kd7glc8lfp5bdmmx4az4fs6lkqjwd970w4fx8gylxff3n9"))
+    ;;    (patches (search-patches "mfem-remove-source-dir-refs.patch"
+    ;;                             "mfem-fix-mk-file-export.patch"))))
+    (source (local-file "/tmp/mfem" #:recursive? #t))
+    (outputs '("out" "doc"))            ; 338 MiB of documentation
+    (build-system cmake-build-system)
+    (arguments
+     (list #:configure-flags
+           ;; Follow order of config/defaults.mk, which has a more exhaustive
+           ;; list than config/defaults.cmake.
+           #~(list "-DBUILD_SHARED_LIBS=ON"
+                   "-DMFEM_USE_MPI=OFF"
+                   "-DMFEM_USE_METIS=OFF" ; MPI only
+                   "-DMFEM_USE_METIS_5=OFF"
+                   "-DMFEM_PRECISION=double" ; For versions beyond 4.6
+                   "-DMFEM_DEBUG=OFF"
+                   "-DMFEM_USE_EXCEPTIONS=ON"
+                   "-DMFEM_USE_ZLIB=ON"
+                   "-DMFEM_USE_LIBUNWIND=ON"
+                   "-DMFEM_USE_LAPACK=ON"
+                   "-DMFEM_THREAD_SAFE=OFF"
+                   "-DMFEM_USE_OPENMP=ON"
+                   "-DMFEM_USE_LEGACY_OPENMP=OFF"
+                   "-DMFEM_USE_MEMALLOC=ON"
+                   "-DMFEM_TIMER_TYPE=2"
+                   "-DMFEM_USE_SUNDIALS=ON"
+                   "-DMFEM_USE_SUITESPARSE=ON"
+                   "-DMFEM_USE_SUPERLU=OFF" ; MPI only
+                   "-DMFEM_USE_SUPERLU5=OFF"
+                   "-DMFEM_USE_MUMPS=OFF"     ; MPI only
+                   "-DMFEM_USE_STRUMPACK=OFF" ; Missing dep, MPI only
+                   "-DMFEM_USE_GINKGO=OFF"    ; Missing dep
+                   "-DMFEM_USE_AMGX=OFF"      ; CUDA library
+                   "-DMFEM_USE_GNUTLS=ON"
+                   "-DMFEM_USE_NETCDF=ON"
+                   "-DMFEM_USE_PETSC=OFF" ; MPI only
+                   "-DMFEM_USE_SLEPC=OFF" ; MPI only
+                   "-DMFEM_USE_MPFR=ON"
+                   "-DMFEM_USE_SIDRE=OFF"   ; Missing dep
+                   "-DMFEM_USE_FMS=OFF"     ; Missing dep
+                   "-DMFEM_USE_CONDUIT=OFF" ; Missing dep
+                   "-DMFEM_USE_PUMI=OFF"    ; Missing dep, MPI only
+                   "-DMFEM_USE_HIOP=OFF"    ; Missing dep
+                   "-DMFEM_USE_GSLIB=OFF"   ; Missing dep
+                   "-DMFEM_USE_CUDA=OFF"
+                   "-DMFEM_USE_HIP=OFF"     ; GPU library
+                   "-DMFEM_USE_RAJA=OFF"    ; Missing dep
+                   "-DMFEM_USE_OCCA=OFF"    ; Missing dep
+                   "-DMFEM_USE_CEED=OFF"    ; Missing dep
+                   "-DMFEM_USE_CALIPER=OFF" ; Missing dep
+                   "-DMFEM_USE_ALGOIM=OFF"  ; Missing dep
+                   "-DMFEM_USE_UMPIRE=OFF"  ; Missing dep
+                   "-DMFEM_USE_SIMD=ON"
+                   "-DMFEM_USE_ADIOS2=OFF"       ; Missing dep
+                   "-DMFEM_USE_MKL_CPARDISO=OFF" ; Intel MKL
+                   "-DMFEM_USE_MKL_PARDISO=OFF"
+                   "-DMFEM_USE_MOONOLITH=OFF" ; Missing dep
+                   "-DMFEM_USE_ADFORWARD=OFF"
+                   "-DMFEM_USE_CODIPACK=OFF"  ; Missing dep
+                   "-DMFEM_USE_BENCHMARK=OFF" ; Only for benchmarks
+                   "-DMFEM_USE_PARELAG=OFF"   ; Missing dep, only for miniapps
+                   "-DMFEM_USE_ENZYME=OFF"    ; Missing dep, requires Clang
+                   "-DMFEM_ENABLE_TESTING=ON"
+                   "-DMFEM_ENABLE_EXAMPLES=ON"
+                   "-DMFEM_ENABLE_MINIAPPS=ON" ; Used in some tests
+                   "-DMFEM_ENABLE_GOOGLE_BENCHMARKS=OFF"
+                   (string-append "-DHDF5_DIR=" #$(this-package-input "hdf5")))
+           #:phases
+           #~(modify-phases %standard-phases
+               (add-after 'unpack 'clean-mpi-references
+                 (lambda _
+                   (substitute* (string-append "config/config.mk.in")
+                     (("(MFEM_MPIEXEC += ).*" _ prefix)
+                      ;; Set and non-empty, to avoid running unexpected
+                      ;; commands.
+                      (string-append prefix (which "false") "\n"))
+                     (("(MFEM_MPIEXEC_NP +=).*" _ prefix)
+                      (string-append prefix "\n"))
+                     (("(MFEM_MPI_NP +=).*" _ prefix)
+                      (string-append prefix "\n")))))
+               (add-after 'unpack 'clean-local-references
+                 (lambda _
+                   (substitute* (string-append "config/config.mk.in")
+                     (("(MFEM_BUILD_TAG += ).*" _ prefix)
+                      (string-append prefix "(none)\n")))))
+               (add-before 'check 'build-tests
+                 (lambda _
+                   (invoke "cmake" "--build" "." "--target" "tests")))
+               (add-after 'check 'build-doc
+                 (lambda _
+                   (setenv "HOME" "/tmp") ; Writable cache for fontconfig
+                   (invoke "cmake" "--build" "." "--target" "doc")))
+               (add-after 'install 'remove-examples
+                 (lambda _
+                   (delete-file-recursively
+                    (string-append #$output "/examples"))))
+               (add-after 'install 'install-doc
+                 (lambda _
+                   (let ((docdir (string-append #$output:doc "/share/doc/"
+                                                #$name "-" #$version)))
+                     (install-file "doc/CodeDocumentation.html" docdir)
+                     (copy-recursively
+                      "doc/CodeDocumentation"
+                      (string-append docdir "/CodeDocumentation")))))
+               (add-after 'install 'fix-library-paths
+                 (let ((mpfr #$(this-package-input "mpfr"))
+                       (libunwind #$(this-package-input "libunwind")))
+                   (lambda _
+                     (substitute* (string-append
+                                   #$output "/lib/cmake/mfem/MFEMTargets.cmake")
+                       ((";mpfr;")
+                        (string-append ";" mpfr "/lib/libmpfr.so;"))
+                       ((";unwind;")
+                        (string-append ";" libunwind "/lib/libunwind.so;")))
+                     (substitute* (string-append #$output "/share/mfem/config.mk")
+                       ((" mpfr ")
+                        (string-append " -Wl,-rpath," mpfr "/lib"
+                                       " -L" mpfr "/lib"
+                                       " -lmpfr "))
+                       ((" unwind ")
+                        (string-append " -Wl,-rpath," libunwind "/lib"
+                                       " -L" libunwind "/lib"
+                                       " -lunwind ")))))))))
+    (native-inputs
+     (list doxygen graphviz perl))      ; For documentation
+    ;; No propagated inputs necessary because the installed CMake files and
+    ;; share/mfem/config.mk provide the full list of include directories.
+    (inputs
+     (list gnutls
+           hdf5                         ; For NetCDF
+           lapack
+           libunwind
+           metis-suitesparse            ; For SuiteSparse
+           mpfr
+           netcdf
+           suitesparse-amd
+           suitesparse-btf
+           suitesparse-camd
+           suitesparse-ccolamd
+           suitesparse-cholmod
+           suitesparse-colamd
+           suitesparse-config
+           suitesparse-klu
+           suitesparse-umfpack
+           sundials
+           zlib))
+    (home-page "https://mfem.org/")
+    (synopsis "Finite element library")
+    (description "MFEM is a modular parallel C++ library for finite element
+methods.  Its goal is to enable high-performance scalable finite element
+discretization research and application development on a wide variety of
+platforms, ranging from laptops to supercomputers.")
+    (license license:bsd-3)))
+
 (define-public flann
   (package
     (name "flann")
diff --git a/gnu/packages/patches/mfem-fix-mk-file-export.patch b/gnu/packages/patches/mfem-fix-mk-file-export.patch
new file mode 100644
index 0000000000..fbd35a029a
--- /dev/null
+++ b/gnu/packages/patches/mfem-fix-mk-file-export.patch
@@ -0,0 +1,29 @@
+From ccba86c0259c2ae56ad44a53d5d5a2a40822cbe3 Mon Sep 17 00:00:00 2001
+From: "Paul A. Patience" <[email-masked]>
+Date: Thu, 28 Mar 2024 13:28:49 -0400
+Subject: [PATCH 2/2] Add forgotten variable to mfem_export_mk_files
+
+MFEM_USE_MKL_PARDISO was missing.
+---
+ config/cmake/modules/MfemCmakeUtilities.cmake | 5 +++--
+ 1 file changed, 3 insertions(+), 2 deletions(-)
+
+diff --git a/config/cmake/modules/MfemCmakeUtilities.cmake b/config/cmake/modules/MfemCmakeUtilities.cmake
+index 204b7d87f..217f4eec7 100644
+--- a/config/cmake/modules/MfemCmakeUtilities.cmake
++++ b/config/cmake/modules/MfemCmakeUtilities.cmake
+@@ -851,8 +851,9 @@ function(mfem_export_mk_files)
+       MFEM_USE_CONDUIT MFEM_USE_PUMI MFEM_USE_HIOP MFEM_USE_GSLIB MFEM_USE_CUDA
+       MFEM_USE_HIP MFEM_USE_RAJA MFEM_USE_OCCA MFEM_USE_CEED MFEM_USE_CALIPER
+       MFEM_USE_UMPIRE MFEM_USE_SIMD MFEM_USE_ADIOS2 MFEM_USE_MKL_CPARDISO
+-      MFEM_USE_ADFORWARD MFEM_USE_CODIPACK MFEM_USE_BENCHMARK MFEM_USE_PARELAG
+-      MFEM_USE_MOONOLITH MFEM_USE_ALGOIM MFEM_USE_ENZYME)
++      MFEM_USE_MKL_PARDISO MFEM_USE_ADFORWARD MFEM_USE_CODIPACK
++      MFEM_USE_BENCHMARK MFEM_USE_PARELAG MFEM_USE_MOONOLITH MFEM_USE_ALGOIM
++      MFEM_USE_ENZYME)
+   foreach(var ${CONFIG_MK_BOOL_VARS})
+     if (${var})
+       set(${var} YES)
+-- 
+2.41.0
+
diff --git a/gnu/packages/patches/mfem-remove-source-dir-refs.patch b/gnu/packages/patches/mfem-remove-source-dir-refs.patch
new file mode 100644
index 0000000000..f9898748c7
--- /dev/null
+++ b/gnu/packages/patches/mfem-remove-source-dir-refs.patch
@@ -0,0 +1,101 @@
+From 0ad647e045e9cbe456ad0f5f3bdb6e015566e067 Mon Sep 17 00:00:00 2001
+From: "Paul A. Patience" <[email-masked]>
+Date: Thu, 28 Mar 2024 13:22:48 -0400
+Subject: [PATCH 1/2] Remove references to MFEM_SOURCE_DIR
+
+---
+ config/cmake/config.hpp.in  | 3 ---
+ config/config.hpp.in        | 3 ---
+ config/config.mk.in         | 1 -
+ fem/ceed/interface/util.cpp | 8 +-------
+ general/device.cpp          | 6 +-----
+ 5 files changed, 2 insertions(+), 19 deletions(-)
+
+diff --git a/config/cmake/config.hpp.in b/config/cmake/config.hpp.in
+index 7cb2a958d..13f13c48d 100644
+--- a/config/cmake/config.hpp.in
++++ b/config/cmake/config.hpp.in
+@@ -30,9 +30,6 @@
+ #define MFEM_VERSION_MINOR (((MFEM_VERSION)/100)%100)
+ #define MFEM_VERSION_PATCH ((MFEM_VERSION)%100)
+ 
+-// MFEM source directory.
+-#define MFEM_SOURCE_DIR "@MFEM_SOURCE_DIR@"
+-
+ // MFEM install directory.
+ #define MFEM_INSTALL_DIR "@MFEM_INSTALL_DIR@"
+ 
+diff --git a/config/config.hpp.in b/config/config.hpp.in
+index 39d7737c7..dbee1f32f 100644
+--- a/config/config.hpp.in
++++ b/config/config.hpp.in
+@@ -30,9 +30,6 @@
+ #define MFEM_VERSION_MINOR (((MFEM_VERSION)/100)%100)
+ #define MFEM_VERSION_PATCH ((MFEM_VERSION)%100)
+ 
+-// The absolute path of the MFEM source prefix.
+-// #define MFEM_SOURCE_DIR "@MFEM_SOURCE_DIR@"
+-
+ // The absolute path of the MFEM installation prefix.
+ // #define MFEM_INSTALL_DIR "@MFEM_INSTALL_DIR@"
+ 
+diff --git a/config/config.mk.in b/config/config.mk.in
+index 303750bf5..d2f538666 100644
+--- a/config/config.mk.in
++++ b/config/config.mk.in
+@@ -12,7 +12,6 @@
+ # Variables corresponding to defines in config.hpp (YES, NO, or value)
+ MFEM_VERSION           = @MFEM_VERSION@
+ MFEM_VERSION_STRING    = @MFEM_VERSION_STRING@
+-MFEM_SOURCE_DIR        = @MFEM_SOURCE_DIR@
+ MFEM_INSTALL_DIR       = @MFEM_INSTALL_DIR@
+ MFEM_GIT_STRING        = @MFEM_GIT_STRING@
+ MFEM_USE_MPI           = @MFEM_USE_MPI@
+diff --git a/fem/ceed/interface/util.cpp b/fem/ceed/interface/util.cpp
+index d122c2ab5..f2bc900a5 100644
+--- a/fem/ceed/interface/util.cpp
++++ b/fem/ceed/interface/util.cpp
+@@ -257,20 +257,14 @@ const std::string &GetCeedPath()
+    if (ceed_path.empty())
+    {
+       const char *install_dir = MFEM_INSTALL_DIR "/include/mfem/fem/ceed";
+-      const char *source_dir = MFEM_SOURCE_DIR "/fem/ceed";
+       struct_stat m_stat;
+       if (stat(install_dir, &m_stat) == 0 && S_ISDIR(m_stat.st_mode))
+       {
+          ceed_path = install_dir;
+       }
+-      else if (stat(source_dir, &m_stat) == 0 && S_ISDIR(m_stat.st_mode))
+-      {
+-         ceed_path = source_dir;
+-      }
+       else
+       {
+-         MFEM_ABORT("Cannot find libCEED kernels in MFEM_INSTALL_DIR or "
+-                    "MFEM_SOURCE_DIR");
++         MFEM_ABORT("Cannot find libCEED kernels in MFEM_INSTALL_DIR");
+       }
+       // Could be useful for debugging:
+       // out << "Using libCEED dir: " << ceed_path << std::endl;
+diff --git a/general/device.cpp b/general/device.cpp
+index ccee71cd7..a68ba7191 100644
+--- a/general/device.cpp
++++ b/general/device.cpp
+@@ -464,13 +464,9 @@ static void OccaDeviceSetup(const int dev)
+    {
+       mfemDir = MFEM_INSTALL_DIR "/include/mfem/";
+    }
+-   else if (occa::io::exists(MFEM_SOURCE_DIR))
+-   {
+-      mfemDir = MFEM_SOURCE_DIR;
+-   }
+    else
+    {
+-      MFEM_ABORT("Cannot find OCCA kernels in MFEM_INSTALL_DIR or MFEM_SOURCE_DIR");
++      MFEM_ABORT("Cannot find OCCA kernels in MFEM_INSTALL_DIR");
+    }
+ 
+    occa::io::addLibraryPath("mfem", mfemDir);
+-- 
+2.41.0
+

base-commit: fb9549164520ad993c2fbbaedc899844d57baabc
-- 
2.41.0