Skip to content

Instantly share code, notes, and snippets.

@dineshadepu
Created March 12, 2026 11:28
Show Gist options
  • Select an option

  • Save dineshadepu/a08d531ea50c5755685abfb5022acaa5 to your computer and use it in GitHub Desktop.

Select an option

Save dineshadepu/a08d531ea50c5755685abfb5022acaa5 to your computer and use it in GitHub Desktop.
MFEM

Installing and Running MFEM using Spack

This guide documents how to install and run the MFEM examples using Spack.

Tested on MacOS (Apple Silicon) with MPI.


1. Install Spack

Clone Spack:

git clone https://github.com/spack/spack.git
cd spack

Activate Spack:

source share/spack/setup-env.sh

2. Detect Compilers

Let Spack detect available compilers.

spack compiler find

Example output:

gcc@15.2.0
apple-clang@17.0.0

3. Create a Spack Environment

Create an environment for FEM development.

spack env create fem-learning
spack env activate fem-learning

Verify:

spack env status

4. Install Required Packages

Install MFEM and dependencies.

spack install mfem
spack install glvis

These packages will also install dependencies such as:

  • HYPRE
  • Open MPI
  • METIS
  • zlib

5. Load the Libraries

Load required packages into the shell.

spack load mfem
spack load hypre
spack load openmpi
spack load metis
spack load zlib-ng

6. Define Library Paths

Set 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_DIR

7. Compile an MFEM Example

Compile 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 ex18

8. Run the Example

Run in serial:

./ex18

Run with MPI:

mpirun -n 4 ./ex18

9. Visualization with GLVis

Start GLVis in a separate terminal:

glvis

Then run the simulation to visualize results in real time.

GLVis is a visualization tool designed specifically for MFEM simulations.


10. What the Example Solves

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

11. Next Steps

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment