This guide documents how to install and run the MFEM examples using Spack.
Tested on MacOS (Apple Silicon) with MPI.
Clone Spack:
git clone https://github.com/spack/spack.git
cd spackActivate Spack:
source share/spack/setup-env.shLet Spack detect available compilers.
spack compiler findExample output:
gcc@15.2.0
apple-clang@17.0.0
Create an environment for FEM development.
spack env create fem-learning
spack env activate fem-learningVerify:
spack env statusInstall MFEM and dependencies.
spack install mfem
spack install glvisThese packages will also install dependencies such as:
- HYPRE
- Open MPI
- METIS
- zlib
Load required packages into the shell.
spack load mfem
spack load hypre
spack load openmpi
spack load metis
spack load zlib-ngSet paths to installed libraries.
export MFEM_DIR=$(spack location -i mfem)
export HYPRE_DIR=$(spack location -i hypre)
export METIS_DIR=$(spack location -i metis)
export ZLIB_DIR=$(spack location -i zlib-ng)Verify:
echo $MFEM_DIRCompile example ex18 (DG advection solver).
mpicxx ex18.cpp \
-I$MFEM_DIR/include \
-I$HYPRE_DIR/include \
-I$METIS_DIR/include \
-L$MFEM_DIR/lib \
-L$HYPRE_DIR/lib \
-L$METIS_DIR/lib \
-L$ZLIB_DIR/lib \
-lmfem -lHYPRE -lmetis -lz \
-o ex18Run in serial:
./ex18Run with MPI:
mpirun -n 4 ./ex18Start GLVis in a separate terminal:
glvisThen run the simulation to visualize results in real time.
GLVis is a visualization tool designed specifically for MFEM simulations.
Example 18 solves the advection equation using a discontinuous Galerkin method.
[ \frac{\partial u}{\partial t} + \mathbf{v} \cdot \nabla u = 0 ]
This equation models transport phenomena such as:
- temperature transport
- species concentration
- vorticity transport
After successfully running MFEM examples, explore:
- Poisson solver (
ex1) - diffusion solver (
ex9) - advection solver (
ex18) - Navier–Stokes miniapps
These cover the main PDE classes used in CFD.
If you'd like, I can also give you a clean “MFEM learning roadmap” Gist (which examples to run and in what order) so you don't get lost in the 20+ examples.