Solve with PETSc and SLEPc#

gfem can solve its linear systems with PETSc and find eigenmodes with SLEPc in place of its built-in solvers. linear_solver="petsc" runs the linear solves of driven, electrostatic and eigenmode problems through PETSc: with no petsc_options an exact MUMPS factorization, like "direct", and otherwise any PETSc Krylov method and preconditioner. eigen_solver="slepc" finds an eigenmode problem’s modes near the target with SLEPc’s Krylov-Schur instead of ARPACK; gfem refines them, computes their Q and differentiates them as it does after ARPACK, so you get the same modes.

Both run on the CPU. This page’s stack is the no-MPI PETSc stack, which runs in one process. "auto" never picks either, and neither is a pip extra: you build the PETSc stack once, from source. For solves across ranks, see With MPI.

When to choose PETSc#

  • For speed, keep "direct" with the mkl extra: on gfem’s driven problems its PARDISO factorization is faster than MUMPS through PETSc, at every frequency of a sweep, in about the same memory. Both give the same S to solver precision.

  • Without the mkl extra, "direct" uses NGSolve’s sparse Cholesky. "petsc" with no petsc_options is faster than it and needs less memory, but installing the mkl extra gains more, for less effort.

  • Choose "petsc" for what only PETSc gives: MUMPS and its options, or your own Krylov method and preconditioner through petsc_options.

  • For a uniform sweep over a narrow band, GMRES on one reused MUMPS factorization (Krylov methods) skips the refactorization at every later frequency. Over a wide band, keep the default factorization.

  • For eigenmodes, eigen_solver="slepc" finds the same modes and Q as ARPACK, damped and nonlinear problems included, after about as many shift-invert solves, in about the same time and memory. Choose it for SLEPc’s own controls (eps_ncv, eps_monitor, eps_view and their kin), not for speed. An eigenmode solve’s time goes into its linear solves, whichever eigen solver runs, so for speed pair it with "direct" and the mkl extra.

Build the PETSc stack#

You need Linux (macOS is not tested, Windows is not supported), uv, curl, make, cmake, the system’s gcc, g++ and gfortran compilers, and a Python not built by conda (for example uv python install 3.13 and a uv virtual environment). The script takes an active conda environment off PATH. gfem needs PETSc with complex scalars, and conda-forge’s PETSc depends on ParMETIS, which is not free for commercial use (Licence).

The script petsc_wheelhouse.sh (download it, or read it at the end of this page) builds PETSc, SLEPc, petsc4py and slepc4py, with MUMPS, METIS and OpenBLAS, into a wheelhouse: a folder of four wheels. It compiles all of them from source, once per Python version: about 22 minutes on a GitHub Actions ubuntu-24.04 runner, 26 with --mpi. Run it with the environment that runs gfem active, since the wheels are built for its Python:

bash petsc_wheelhouse.sh --check      # check the prerequisites and the target Python
bash petsc_wheelhouse.sh wheelhouse   # build the four wheels into ./wheelhouse
pip install --no-index --find-links wheelhouse petsc petsc4py slepc slepc4py
  • PYTHON picks another target Python, as a path or a version (PYTHON=3.12); MAKE_NP sets the number of parallel compile jobs.

  • In a uv project, install with uv pip install. A plain uv sync removes packages its lock file does not list, these four included: sync with uv sync --inexact, or rerun the install line after an exact sync. uv run leaves them in place.

  • Rebuild for another Python version, and when gfem raises the PETSc version it supports: the error message names the oldest one it accepts.

  • Rebuild a stack built by gfem 0.1.0’s script, which gfem refuses (Release notes). Run the current script into a new folder, then rerun the install line with --force-reinstall, since the package versions do not change. An MPI stack rebuilds the same way, with --mpi.

With MPI#

bash petsc_wheelhouse.sh --mpi wheelhouse builds the MPI PETSc stack beside the no-MPI one, on the Open MPI whose mpicc, mpicxx and mpifort are on PATH. Run a distributed solve is what it is for. It has complex scalars, 64-bit indices, MUMPS, METIS, ScaLAPACK and PT-Scotch, and no ParMETIS. It also needs bison 3 or newer, flex and cmake 3.26 or newer. --check names what is missing, and refuses an MPI other than Open MPI.

  • --fetch-sources DIR downloads what a build needs, on a machine with internet; --sources DIR builds from it, offline.

  • The stack runs one-process solves too, through the same linear_solver="petsc".

  • An eigenmode solve across ranks finds its modes with SLEPc from this stack: eigen_solver="auto" names it there, and "arpack" raises.

  • The script’s MUMPS orders with PT-Scotch on at most 32 processes, so the fill of BDDC’s coarse factorization does not grow with the ranks. A stack built by an earlier script lacks this: build it again to get it.

  • A stack runs under the Open MPI it was built on: load the same one to use it.

  • The no-MPI stack under a launcher serves rank copies only, and a distributed solve naming PETSc refuses it, with gfem.SolverUnavailableError.

ParMETIS is part of neither stack: it is free only for educational and research use by non-profit institutions and US government agencies, and for evaluation by others; it may not be redistributed. --mpi --with-parmetis adds it to the MPI stack, for users its terms allow; that stack is for its builder alone and must not be shared. gfem then orders BDDC’s coarse problem with it.

Check the build#

import numpy as np
from petsc4py import PETSc
from slepc4py import SLEPc

np.dtype(PETSc.ScalarType)  # dtype('complex128')
PETSc.Sys.hasExternalPackage("mumps")  # True

If gfem cannot use the stack, the solve raises gfem.SolverUnavailableError with the reason (a package that does not import, real scalars, a version older than gfem supports, a stack built by gfem 0.1.0’s script) and the fix. gfem never falls back to another solver.

Choose the solvers#

import gfem

options = gfem.SolverOptions(linear_solver="petsc", eigen_solver="slepc")
modes = gfem.solve(model, gfem.Eigenmode(target=7.8e9, n_modes=2), options=options)
modes.frequency, modes.q
  • The two fields are independent: SLEPc works with every linear solver, "direct" on a GPU included, and "petsc" solves every problem type.

  • SolverOptions refuses device="cuda" with "petsc". Wave port modes are 2D problems that always run SuperLU and SciPy on the CPU, so a standalone gfem.PortMode solve ignores both fields.

  • frequency_workers work: each worker process starts PETSc itself.

  • gfem checks the stack when the solve starts, never when the options are built, so a job naming PETSc can be written anywhere and run where the stack is installed. The options of such a job file:

{
  "linear_solver": "petsc",
  "eigen_solver": "slepc",
  "petsc_options": {"ksp_view": null, "pc_type": "lu"}
}

Pass PETSc and SLEPc options#

petsc_options maps PETSc option names, without the leading -, to values; None is a flag. Names starting eps_, bv_, ds_, rg_ or fn_ go to SLEPc and need eigen_solver="slepc"; every other name goes to the linear solver and needs linear_solver="petsc".

options = gfem.SolverOptions(
    linear_solver="petsc",
    petsc_options={"pc_type": "lu", "mat_mumps_icntl_14": 50},  # more MUMPS workspace
)
  • {"ksp_view": None} prints the linear solver PETSc set up.

  • {"eps_ncv": 40, "eps_monitor": None} gives SLEPc a larger subspace and prints its progress; eps_mpd and eps_max_it also reach SLEPc.

  • {"eps_view": None} prints the eigensolver SLEPc ran, at the end of each of its solves; so do SLEPc’s other end-of-solve viewers, eps_converged_reason and eps_error_*.

  • Never write an options prefix: gfem gives each solver its own and holds its options in PETSc’s database only while that solver reads them. Command-line PETSc options and your own petsc4py options do not reach gfem’s solvers.

  • An option the linear solver does not read raises ValueError when the solve starts: a typo, or an option of another configuration, such as a MUMPS option with pc_type jacobi. So does an option PETSc or SLEPc refuses, with its reason. An option SLEPc does not read, such as eps_mpdd, raises ValueError after SLEPc’s first pass.

Krylov methods#

With only ksp_type set, gfem’s MUMPS factorization is the preconditioner. Keep the first frequency’s factors, and GMRES runs a whole uniform sweep on one factorization:

options = gfem.SolverOptions(
    linear_solver="petsc",
    petsc_options={"ksp_type": "gmres", "ksp_reuse_preconditioner": True},
)

This pays off over a narrow band, where GMRES needs a few iterations per frequency in place of a refactorization. Further from the factored frequency the iterations grow. Where GMRES does not converge (for example at ksp_max_it), gfem factorizes again at that frequency, finishes its solve from where GMRES stopped, and reuses the new factors for the frequencies after it. Over a wide band reuse therefore pays only between these refactorizations, each of which also costs the iterations spent before it: lower ksp_max_it to refactorize sooner. A solve that still does not converge on the new factors raises gfem.LinearSolverConvergenceError, which carries the frequencies the sweep solved. Adaptive sweeps set the preconditioner up at every sample.

  • tolerance (adjoint_tolerance for adjoint solves) becomes ksp_rtol: the true, unpreconditioned residual relative to the right-hand side. bddc_gmres and pmg_gmres measure their preconditioned residual instead, so the same tolerance is a different norm per backend. ksp_max_it defaults to 400. Set ksp_norm_type for PETSc’s own norm.

  • Driven and eigenmode operators are complex symmetric, not Hermitian: use GMRES-type methods (gmres, fgmres, bcgs, tfqmr). PETSc’s CG assumes a Hermitian operator, and its ksp_cg_type symmetric variant stops when the real part of \(p^T A p\) changes sign, which these operators allow. CG suits the electrostatic problem, whose operator is real symmetric positive definite.

  • gfem brings no Krylov preconditioner for curl-curl operators to PETSc (its own bddc_gmres and pmg_gmres are built for them): PETSc’s algebraic multigrid does not know the curl’s null space, and the build has no hypre. Incomplete factorizations fail too: GMRES with PETSc’s ILU, with or without fill levels, does not converge on gfem’s driven problems. To try one anyway, also set pc_factor_mat_solver_type to petsc: gfem’s default MUMPS has no ILU, so pc_type ilu alone is refused.

  • Below the low-frequency floor (gfem.LowFrequencyWarning), a Krylov method on the MUMPS factorization loses the same digits of the capacitive part of S as the factorization alone, whatever the tolerance, and \(S_{12}\) and \(S_{21}\) agree to about \(10^{-9}\) instead of \(10^{-15}\). "petsc" solves are not gauged. linear_solver="direct" on one process goes through the tree-cotree gauge and keeps these digits; MUMPS through PETSc does not.

What gfem refuses, and what to set instead#

SolverOptions raises ValueError for these options, naming what to set:

Option

Why

Set instead

-ksp_type

names are written without -

ksp_type

ksp_rtol

gfem sets it

tolerance, adjoint_tolerance

ksp_atol

gfem stops on the relative residual only

tolerance, adjoint_tolerance

ksp_initial_guess_nonzero

gfem sets it per solve

nothing: remove it

eps_tol

gfem sets it

eigen_tolerance

eps_nev

gfem sets it

Eigenmode.n_modes

eps_target, eps_largest_real, other selections

gfem keeps the modes nearest the target

Eigenmode.target

st_ksp_type, any st_ option

gfem applies the shift-invert itself

linear_solver; with "petsc", its ksp_ and pc_ options

eps_gen_hermitian, eps_conv_abs, eps_two_sided, eps_harmonic and their kin

SLEPc sees a non-Hermitian shift-inverted operator

eigen_tolerance for convergence

eps_type other than krylovschur or arnoldi

the others do not suit that operator

krylovschur (the default) or arnoldi

ksp_type with linear_solver="direct"

the option’s solver is not selected

select it

When the solve starts, gfem also refuses an option the linear solver did not read, an option PETSc refuses, and ksp_reuse_preconditioner with ksp_type preonly, which would solve with a stale operator. ksp_type preonly (gfem’s default) applies the preconditioner once and measures no residual, so gfem refuses it with a pc_type other than lu or cholesky, and with a pc_factor_shift_type other than none: use a Krylov ksp_type such as gmres with any other preconditioner. A Krylov solve that PETSc ends on its iteration count without measuring a residual (ksp_convergence_test skip) raises ValueError too.

Threads#

linear_solver="petsc" does not read SolverOptions.threads. MUMPS runs its dense kernels on the threaded OpenBLAS the script builds: set OPENBLAS_NUM_THREADS before Python starts. Frequency workers already give each worker its share of the threads.

Check your results#

  • Two linear solvers. With no petsc_options, "petsc" is an exact factorization: solve once with "petsc" and once with "direct", and S should agree to solver precision.

  • Two eigen solvers. gfem refines SLEPc’s modes as it does ARPACK’s: with eigen_solver="slepc" and with "arpack", the modes, Q, fields and gradients should agree to solver precision.

How gfem itself is checked on PETSc and SLEPc: verification. The Capabilities gradient matrix does not list PETSc, because those checks run only where the stack is installed.

Limits#

  • CPU only. One process on the no-MPI stack: spread a uniform sweep with frequency_workers, or see Run a distributed solve for the MPI stack. Tested on Linux only.

  • SLEPc’s polynomial and nonlinear eigensolvers are not used: damped and nonlinear eigenmode problems go through the same shift-invert iteration as with ARPACK.

  • An eigenmode solve with linear_solver="petsc" differentiates its eigenfrequencies only. A gradient through a mode’s field (participations, port Q, the field itself) raises gfem.NonDifferentiableError: use linear_solver="direct" for those (Differentiate eigenfrequencies, Q and participation ratios).

  • Your own petsc4py code in the same process: gfem starts PETSc without command-line options (or uses the PETSc you started), keeps its options to itself, and serializes only its own PETSc calls. Do not call PETSc from another thread during a gfem solve.

Next#

gfem.SolverOptions, gfem.SolverUnavailableError, Differentiate eigenfrequencies, Q and participation ratios, Run the direct solver on an NVIDIA GPU, Run a distributed solve, Licence.

The build script#

#!/usr/bin/env bash
# Build a PETSc stack that gfem's PETSc and SLEPc solvers use: PETSc and SLEPc
# with complex scalars, with their Python bindings petsc4py and slepc4py, as a
# wheelhouse (a folder of four wheels) to install into the environment that
# runs gfem. The default stack has no MPI and serves one process, or copies of
# one process per rank. With --mpi it is built on the machine's Open MPI and
# also serves distributed solves. --with-parmetis adds ParMETIS to the MPI stack,
# for users its licence allows (see parmetis_terms): such a stack is for its
# builder alone and is never shared.
#
# Usage:
#   bash petsc_wheelhouse.sh [--mpi [--with-parmetis]] [--sources DIR] OUTPUT_DIR
#                                   build the four wheels into OUTPUT_DIR (a new
#                                   or empty folder)
#   bash petsc_wheelhouse.sh [--mpi [--with-parmetis]] [--sources DIR] --check
#                                   check the prerequisites only, and print the
#                                   configure options
#   bash petsc_wheelhouse.sh [--with-parmetis] --fetch-sources DIR
#                                   on a machine with internet: download into DIR
#                                   what --sources reads on one without it
# then, in the environment that runs gfem:
#   pip install --no-index --find-links OUTPUT_DIR petsc petsc4py slepc slepc4py
#
# Prerequisites: Linux (macOS is not tested), uv, curl (not with --sources), make,
# cmake, and the system's C, C++ and Fortran compilers gcc, g++ and gfortran (the
# script takes an active conda environment off PATH). With --mpi also Open MPI's
# mpicc, mpicxx and mpifort on PATH, bison 3 or newer, flex, and cmake 3.26 or
# newer. The script downloads the four source distributions from PyPI, and
# PETSc's configure downloads METIS and OpenBLAS, with --mpi also ScaLAPACK;
# the script fetches MUMPS, which it patches (see patch_mumps), and Scotch
# itself. All of them are compiled from source, and --sources reads them from
# DIR instead. It is a long build, done
# once per Python version. A stack runs under the Open MPI it was built on.
#
# Target Python: the bindings are compiled for one CPython version (3.12 or
# newer, not built by conda), so build with the Python of the environment
# that runs gfem. The script uses the Python that `uv python find` gives:
# the active virtual environment's, or the .venv of the project in the
# current folder, if any.
# Set PYTHON to choose another, as a path or a version, for example
# PYTHON=/path/to/venv/bin/python or PYTHON=3.12.
#
# MAKE_NP sets the number of parallel compile jobs (default: PETSc's own
# choice from the number of cores).
set -euo pipefail
# make runs each recipe through /bin/sh, which is bash on many Linux systems,
# and every such bash runs the file BASH_ENV names: one that runs `conda init`
# puts conda's compilers back on PATH inside the build, and conda's linker
# cannot link what the system's gcc compiled. No child shell reads one.
unset BASH_ENV ENV

# The release gfem's PETSc tests run on: gfem needs this minor or newer.
PETSC_VERSION=3.26.0
SLEPC_VERSION=3.26.0
# sha256 of each source distribution on PyPI
SHA256_petsc=f5230023e6e22ee607802a13c82bc25f3d81e71654ad386a5b9bdff17fed93df
SHA256_petsc4py=0a34bd083e24bee1a6759c4639a45d10c0117855eb9f0466834450245f75d7e5
SHA256_slepc=a2f4cc2af76d55c078c30ad8bc66b44736dac921a7912266eb44136fc1b6029d
SHA256_slepc4py=07bf970c861736b9b977dfecd4ad46415d6c4a6f26b0ed128ecffae59613d6ff

# Each tarball PETSc's configure reads, with its package: name, URL, sha256. The
# stack holds what these build and nothing else, the set the licence page names.
TARBALLS=(
  "MUMPS_5.9.1.tar.gz https://mumps-solver.org/MUMPS_5.9.1.tar.gz 659c9b57646b5a003ac618baa1faf9dd2044e46c732b3daaccbc7158003e1b46"
  "scalapack-v2.2.3.tar.gz https://github.com/Reference-ScaLAPACK/scalapack/archive/v2.2.3.tar.gz 5d93701eca663925e98010dd8d0f45fd79b2191d74e5afa5711d587370a8b9dd"
  "scotch-v7.0.18.tar.gz https://gitlab.inria.fr/scotch/scotch/-/archive/v7.0.18/scotch-v7.0.18.tar.gz 2cbdcf6da5a08ca31437d7c967b243b3c96d621409ecb710768de2aa2e09802d"
  "petsc-pkg-metis-v5.1.0-p12.tar.gz https://bitbucket.org/petsc/pkg-metis/get/v5.1.0-p12.tar.gz 2f16abe87394d4fd1430d66a8995076c352af40f7a4d1c5300b3b1cc9d545663"
  "OpenBLAS-0.3.34.tar.gz https://github.com/xianyi/OpenBLAS/archive/v0.3.34.tar.gz cd7e129868320cc2d033afa920e31202dfe0b8066a5b66661900ccc0f197dfed"
)
# MUMPS (CeCILL-C), its ordering METIS (Apache-2.0), BLAS and LAPACK (BSD-3-Clause)
NO_MPI_PACKAGES=(mumps metis openblas)
# Also MUMPS's root ScaLAPACK (BSD-3-Clause) and its parallel ordering PT-Scotch
# (CeCILL-C). ParMETIS only with --with-parmetis: it is free only for educational
# and research use by non-profit institutions and US government agencies, and for
# evaluation by others; it may not be redistributed. So a stack with it is for its
# builder alone and is never shared.
MPI_PACKAGES=(mumps scalapack ptscotch metis openblas)
# ParMETIS's tarball, outside TARBALLS (the licence page's set): name, URL, sha256
PARMETIS_TARBALL="petsc-pkg-parmetis-v4.0.3-p9.tar.gz https://bitbucket.org/petsc/pkg-parmetis/get/v4.0.3-p9.tar.gz 612717e85992c984f09b0f5670be421bbb90a4c04145ab5b9a3358b92765d891"
# PETSc 3.26.0 pins Scotch 7.0.15, whose hdgraphFold2 crashes MUMPS's parallel
# analysis (fixed upstream in 7.0.18), so the MPI stack always takes this tarball.
SCOTCH_TARBALL=scotch-v7.0.18.tar.gz
# Both stacks build MUMPS from this tarball, after patch_mumps caps its ordering.
MUMPS_TARBALL=MUMPS_5.9.1.tar.gz

usage() {
  sed -n '/^# Usage:/,/^# then,/p' "${BASH_SOURCE[0]}" | sed '$d' | cut -c3- >&2
  exit 2
}

fail() {
  echo "petsc_wheelhouse.sh: $*" >&2
  exit 1
}

# ParMETIS's terms (its LICENSE.txt), on stderr. No prompt: builds run in batch
# jobs, and the flag is the explicit choice.
parmetis_terms() {
  cat >&2 << 'EOF'
warning: --with-parmetis adds ParMETIS, copyrighted by the Regents of the University of Minnesota. It can be freely used for educational and research purposes by non-profit institutions and US government agencies only; other organizations may use it for evaluation only, and any further use needs the University's prior approval. It may not be sold or redistributed without that approval. The wheels this build makes hold ParMETIS: they are for your own use and must not be shared or uploaded anywhere. Building with this flag is your acceptance of ParMETIS's terms.
EOF
}

# An active conda environment puts its own gcc, g++ and gfortran first on
# PATH. They search conda's include and lib folders by default, so the
# petsc4py and slepc4py builds compile and link against conda's PETSc,
# OpenBLAS and MPI, and give bindings that do not import. Take every folder
# of the environment, and of the conda installation, off PATH, so that the
# build uses the system's compilers and tools, and unset every other
# exported variable that names one of them: the activation's tools (AR, LD,
# NM, ...) and search paths (CMAKE_PREFIX_PATH, CMAKE_ARGS, ...) would pull
# conda in too. PYTHON and VIRTUAL_ENV stay: they choose the target Python,
# which the check then refuses if it is conda's. CONDA_ROOTS keeps those two
# folders for that check. The installation is the folder above CONDA_EXE's
# bin, if it holds conda-meta (not /usr for a system conda).
leave_conda() {
  CONDA_ROOTS=()
  [ -n "${CONDA_PREFIX:-}" ] || return 0
  CONDA_ROOTS=("${CONDA_PREFIX%/}")
  local base IFS=: entries entry root kept=() name
  base=$(dirname "$(dirname "${CONDA_EXE:-/}")")
  [ ! -d "$base/conda-meta" ] || CONDA_ROOTS+=("${base%/}")
  read -ra entries <<< "$PATH"
  for entry in "${entries[@]}"; do
    for root in "${CONDA_ROOTS[@]}"; do
      case $entry/ in "$root"/*) continue 2 ;; esac
    done
    kept+=("$entry")
  done
  PATH="${kept[*]-}"
  hash -r
  echo "warning: conda environment $CONDA_PREFIX is active: the build takes its folders off PATH, unsets the variables that name them, ignores its compilers and flags (CC, CFLAGS, LDFLAGS, ...) and uses the system's gcc, g++ and gfortran" >&2
  while IFS= read -r name; do
    case $name in PATH | PYTHON | VIRTUAL_ENV | [0-9]* | *[!A-Za-z0-9_]*) continue ;; esac
    for root in "${CONDA_ROOTS[@]}"; do
      case ${!name-} in *"$root"*)
        unset "$name" 2> /dev/null || true # readonly: left as is
        continue 2
        ;;
      esac
    done
  done < <(compgen -e)
  unset CONDA_PREFIX CONDA_DEFAULT_ENV CONDA_SHLVL
}

# What --mpi needs on top of the rest: refuse what is known not to work.
check_mpi_prerequisites() {
  local version
  version=$(mpicc --showme:version 2>&1) || true
  case $version in
    *"Open MPI"*) echo "mpicc: $(command -v mpicc) ($version)" ;;
    *) fail "mpicc is not Open MPI's ('mpicc --showme:version' says: $version): only Open MPI gives right numbers with NGSolve's wheel. Load Open MPI and run again" ;;
  esac
  mpicc --version | head -1 | grep -qi -E 'gcc|gnu' ||
    echo "warning: mpicc does not wrap gcc: OpenBLAS did not build under Intel's wrappers on MareNostrum 5, for a cause not known" >&2
  version=$(bison --version | head -1)
  case ${version##* } in
    [3-9].* | [1-9][0-9].*) ;;
    *) fail "PT-Scotch needs bison 3 or newer, not: $version" ;;
  esac
  version=$(cmake --version | head -1)
  version=${version##* }
  "$PY" -c 'import sys; sys.exit(tuple(map(int, sys.argv[1].split(".")[:2])) < (3, 26))' "$version" ||
    fail "ScaLAPACK needs cmake 3.26 or newer, not $version"
}

# Check every prerequisite, report all that are missing, and set PY to the
# target Python.
check_prerequisites() {
  local missing=() tool
  local tools=(uv curl)
  if [ -z "$FETCH_SOURCES" ]; then
    tools=(uv make cmake gcc g++ gfortran)
    [ -n "$SOURCES" ] || tools+=(curl)
  fi
  ((MPI == 0)) || tools+=(mpicc mpicxx mpifort bison flex)
  for tool in "${tools[@]}"; do
    command -v "$tool" > /dev/null || missing+=("$tool")
  done
  [ "$(uname -s)" = Linux ] || echo "warning: only Linux is tested, not $(uname -s)" >&2
  ((${#missing[@]} == 0)) || fail "missing prerequisites: ${missing[*]} (install them and run again)"
  PY=$(UV_NO_CONFIG=1 uv python find ${PYTHON:+"$PYTHON"}) ||
    fail "no Python found for PYTHON='${PYTHON:-}' (set PYTHON to the Python of the environment that runs gfem)"
  "$PY" -c 'import sys; sys.exit(sys.implementation.name != "cpython" or sys.version_info < (3, 12))' ||
    fail "$PY is not CPython 3.12 or newer, which gfem needs"
  # A conda Python, or a virtual environment made from one, builds the
  # bindings with its own compiler flags, which point at conda's headers and
  # libraries: the same failure as conda's compilers.
  "$PY" - ${CONDA_ROOTS[@]+"${CONDA_ROOTS[@]}"} << 'EOF' || fail "$PY is a conda Python: a conda-built Python's compiler flags point at conda's headers and libraries, and the bindings built with them do not import. Use a Python not built by conda, for example: uv python install 3.13, then uv venv --python-preference only-managed --python 3.13 and run gfem from that environment"
import os, sys
roots = [os.path.realpath(root) for root in sys.argv[1:]]
for prefix in {os.path.realpath(sys.prefix), os.path.realpath(sys.base_prefix)}:
    if os.path.isdir(os.path.join(prefix, "conda-meta")) or any(
        prefix == root or prefix.startswith(root + os.sep) for root in roots
    ):
        sys.exit(f"{prefix} is a conda environment")
EOF
  if [ -z "$FETCH_SOURCES" ]; then
    for tool in gcc g++ gfortran; do
      echo "$tool: $(command -v "$tool") ($("$tool" -dumpfullversion -dumpversion))"
    done
  fi
  echo "target Python: $PY ($("$PY" -c 'import sys; print(sys.version.split()[0])'))"
  ((MPI == 0)) || check_mpi_prerequisites
  [ -z "$SOURCES" ] || check_sources
}

# Raise unless a file's sha256 is the expected one.
check_sha256() {
  "$PY" - "$1" "$2" << 'EOF'
import hashlib, sys
path, expected = sys.argv[1:]
with open(path, "rb") as f:
    digest = hashlib.sha256(f.read()).hexdigest()
sys.exit(f"{path}: sha256 {digest}, expected {expected}" if digest != expected else 0)
EOF
}

# Every file a build from SOURCES reads, with its hash.
check_sources() {
  local name version sha256 line url
  [ -d "$SOURCES/wheels" ] || fail "$SOURCES/wheels is missing: --fetch-sources writes it"
  for line in "petsc $PETSC_VERSION $SHA256_petsc" "petsc4py $PETSC_VERSION $SHA256_petsc4py" \
    "slepc $SLEPC_VERSION $SHA256_slepc" "slepc4py $SLEPC_VERSION $SHA256_slepc4py"; do
    read -r name version sha256 <<< "$line"
    [ -f "$SOURCES/$name-$version.tar.gz" ] || fail "$SOURCES/$name-$version.tar.gz is missing"
    check_sha256 "$SOURCES/$name-$version.tar.gz" "$sha256"
  done
  for line in "${TARBALLS[@]}" ${WITH_PARMETIS:+"$PARMETIS_TARBALL"}; do
    read -r name url sha256 <<< "$line"
    case $name in
      scalapack* | scotch*) ((MPI == 1)) || continue ;;
    esac
    [ -f "$SOURCES/$name" ] || fail "$SOURCES/$name is missing"
    check_sha256 "$SOURCES/$name" "$sha256"
  done
  echo "sources: $SOURCES"
}

# The name of a package's tarball.
tarball() {
  local line
  for line in "${TARBALLS[@]}" "$PARMETIS_TARBALL"; do
    case $1:$line in
      mumps:MUMPS* | scalapack:scalapack* | ptscotch:scotch* | metis:petsc-pkg-metis* | parmetis:petsc-pkg-parmetis* | openblas:OpenBLAS*)
        echo "${line%% *}"
        return
        ;;
    esac
  done
}

# Set CONFIGURE_OPTIONS. A package comes from SOURCES when given, and MUMPS and
# PT-Scotch always from the tarballs the script checked (in ``$1``), MUMPS's
# patched.
set_configure_options() {
  local packages=("${NO_MPI_PACKAGES[@]}") package
  CONFIGURE_OPTIONS=(--with-scalar-type=complex) # gfem's operators are complex
  if ((MPI == 1)); then
    packages=("${MPI_PACKAGES[@]}")
    # 64-bit: the graph passes 2^31 near 54 M DOF at order 2
    CONFIGURE_OPTIONS+=(--with-64-bit-indices)
    if [ -n "$WITH_PARMETIS" ]; then
      packages+=(parmetis)
    else
      CONFIGURE_OPTIONS+=(--with-parmetis=0)
    fi
  else
    CONFIGURE_OPTIONS+=(--with-mpi=0) # one process: MUMPS on its serial libseq
  fi
  for package in "${packages[@]}"; do
    if [ "$package" = mumps ]; then
      CONFIGURE_OPTIONS+=("--download-mumps=$1/${MUMPS_TARBALL%.tar.gz}-gfem.tar.gz")
    elif [ -n "$SOURCES" ]; then
      CONFIGURE_OPTIONS+=("--download-$package=$SOURCES/$(tarball "$package")")
    elif [ "$package" = ptscotch ]; then
      CONFIGURE_OPTIONS+=("--download-ptscotch=$1/$SCOTCH_TARBALL")
    else
      CONFIGURE_OPTIONS+=("--download-$package")
    fi
  done
  CONFIGURE_OPTIONS+=(--download-openblas-use-pthreads) # OPENBLAS_NUM_THREADS sets the threads
  CONFIGURE_OPTIONS+=(--with-x=0)                       # no X11 graphics
  ((MPI == 0)) || CONFIGURE_OPTIONS+=(COPTFLAGS=-O2 CXXOPTFLAGS=-O2 FOPTFLAGS=-O2)
}

# Download one file and check its sha256.
download() {
  curl -fsSL --retry 3 -o "$3" "$1"
  check_sha256 "$3" "$2" ||
    fail "$1 changed: a host may rebuild its archives, so compare it by hand before trusting it"
}

# The sdist's URL on PyPI.
pypi_url() {
  echo "https://files.pythonhosted.org/packages/source/${1:0:1}/$1/$1-$2.tar.gz"
}

# Write into DIR what a build reads with --sources DIR.
fetch_sources() {
  local dir=$1 name url sha256 line version
  mkdir -p "$dir/wheels"
  dir=$(cd "$dir" && pwd)
  download "$(pypi_url petsc "$PETSC_VERSION")" "$SHA256_petsc" "$dir/petsc-$PETSC_VERSION.tar.gz"
  download "$(pypi_url petsc4py "$PETSC_VERSION")" "$SHA256_petsc4py" "$dir/petsc4py-$PETSC_VERSION.tar.gz"
  download "$(pypi_url slepc "$SLEPC_VERSION")" "$SHA256_slepc" "$dir/slepc-$SLEPC_VERSION.tar.gz"
  download "$(pypi_url slepc4py "$SLEPC_VERSION")" "$SHA256_slepc4py" "$dir/slepc4py-$SLEPC_VERSION.tar.gz"
  for line in "${TARBALLS[@]}" ${WITH_PARMETIS:+"$PARMETIS_TARBALL"}; do
    read -r name url sha256 <<< "$line"
    download "$url" "$sha256" "$dir/$name"
  done
  version=$("$PY" -c 'import sys; print("%d.%d" % sys.version_info[:2])')
  # the build tools of the four builds, for this Python on any glibc
  uvx pip download -d "$dir/wheels" --python-version "$version" --implementation cp \
    --only-binary=:all: --platform manylinux_2_28_x86_64 --platform manylinux_2_27_x86_64 \
    --platform manylinux_2_17_x86_64 --platform manylinux2014_x86_64 \
    --platform manylinux_2_5_x86_64 --platform manylinux1_x86_64 \
    setuptools wheel patchelf cython packaging numpy uv
  echo "sources: $dir"
}

# One source distribution, from PyPI or SOURCES, with its sha256 checked.
fetch() {
  local name=$1 version=$2 sha256=$3
  local sdist=$WORK/$name-$version.tar.gz
  if [ -n "$SOURCES" ]; then
    cp "$SOURCES/$name-$version.tar.gz" "$sdist"
    check_sha256 "$sdist" "$sha256"
  else
    download "$(pypi_url "$name" "$version")" "$sha256" "$sdist"
  fi
}

# Build one wheel into the wheelhouse. The wheelhouse itself provides the
# petsc and slepc a binding builds against; no cache, so a wheel built
# with other configure options is never reused.
build() {
  local name=$1 version=$2 start=$SECONDS
  echo "=== building $name $version"
  local offline=()
  [ -z "$SOURCES" ] || offline=(--no-index --find-links "$SOURCES/wheels")
  uv build --no-cache --wheel --python "$PY" --out-dir "$OUT" --find-links "$OUT" \
    ${offline[@]+"${offline[@]}"} \
    --build-constraints "$WORK/constraints.txt" "$WORK/$name-$version.tar.gz"
  echo "=== $name built in $((SECONDS - start)) s"
}

# PETSc's configure adds -DGEMMT_AVAILABLE to MUMPS when the BLAS has GEMMT, and
# MUMPS then sends every symmetric update through ZGEMMT. OpenBLAS's ZGEMMT, up to
# at least 0.3.34, computes each column's offset in a 32-bit int, which wraps on a
# complex front of 32,768 or more: wrong factors with no error, or a SEGV. Which
# OpenBLAS runs is not this build's to choose: libpetsc binds to NGSolve's by
# SONAME. Without the flag MUMPS uses its own blocked ZGEMM loop, on 64-bit
# offsets. configure has no option for this, so edit the sdist's MUMPS.py.
patch_petsc_sdist() {
  "$PY" - "$WORK/petsc-$PETSC_VERSION.tar.gz" << 'EOF'
import io, os, sys, tarfile

sdist = sys.argv[1]
line = b"if self.blasLapack.checkForRoutine('dgemmt'):"
hits = 0
with tarfile.open(sdist) as old, tarfile.open(sdist + ".patched", "w:gz") as new:
    for member in old:
        data = old.extractfile(member).read() if member.isfile() else None
        if member.name.endswith("config/BuildSystem/config/packages/MUMPS.py"):
            hits += data.count(line)
            data = data.replace(line, b"if False:  # gfem: OpenBLAS's ZGEMMT overflows")
            member.size = len(data)
        new.addfile(member, io.BytesIO(data) if data is not None else None)
if hits != 1:
    os.remove(sdist + ".patched")
    sys.exit(f"{sdist}: MUMPS.py's GEMMT check found {hits} times, expected once")
os.replace(sdist + ".patched", sdist)
EOF
}

# MUMPS orders with PT-Scotch on the largest power of two of processes up to
# min(NPROCS, N/16), with no control to cap it, and the factor's fill grows with
# that count: on a 30 M unknown coarse problem 8.9e9 entries on 32 processes,
# 1.1e10 on 64 and about 3e10 on 128, against 44-47 s of analysis at 32 for any
# number of ranks. So the stack caps PT-Scotch's ordering processes at 32 (not
# ParMETIS's, whose fill does not grow with them). The cap holds where every
# node holds as many processes: on another layout (direct with unequal ranks per
# node, say) MUMPS orders on every process, capped or not. MUMPS_PARAORD_MAX
# overrides the cap at run time, for measurement: 0 restores MUMPS's own count,
# N of 2 and more caps at N, and 1 caps at 2 (MUMPS orders on every process when
# its count is 1); a value that is not a whole number of 0 and more keeps 32.
# Rank 0 writes a "PARAORD cap C nprocs P paraord W" line to MUMPS's output
# stream ICNTL(3) when ICNTL(4) is 2 or more. Edit src/tools_common.F
# (MUMPS_BUILD_COMM_PARA_ANA) at three lines, each found exactly once, into a
# repacked tarball beside the pristine one. A MUMPS bump fails the sha256 or
# that count; the edit is then derived again from the new source, or dropped if
# MUMPS has gained a control of its own. The name MUMPS_PARAORD_MAX is the
# marker check_paraord_cap reads in libpetsc: a new meaning takes a new name.
patch_mumps() {
  "$PY" - "$WORK/$MUMPS_TARBALL" "$WORK/${MUMPS_TARBALL%.tar.gz}-gfem.tar.gz" << 'EOF'
import io, os, sys, tarfile

pristine, patched = sys.argv[1:]
# Each anchor, and the lines that go after the line holding it.
edits = [
    (b"ROOT=0", b"""\
C     gfem: cap on PT-Scotch parallel-ordering processes, default 32;
C     MUMPS_PARAORD_MAX overrides it, 0 for no cap, 1 means 2
      CHARACTER(LEN=16) :: CAPSTR
      INTEGER :: CAPLEN, CAPST, CAPVAL, CAPIN
      CAPVAL = 32
      CALL GET_ENVIRONMENT_VARIABLE('MUMPS_PARAORD_MAX', CAPSTR,
     &     CAPLEN, CAPST)
      IF (CAPST .EQ. 0 .AND. CAPLEN .GT. 0) THEN
        READ(CAPSTR(1:CAPLEN), *, IOSTAT=CAPST) CAPIN
        IF (CAPST .EQ. 0 .AND. CAPIN .GE. 0) CAPVAL = CAPIN
      END IF
C     MUMPS orders on all processes when the count is 1, so 1 means 2
      IF (CAPVAL .EQ. 1) CAPVAL = 2
"""),
    (b"WORKERS = min(NPROCS,N/16)", b"""\
        IF (KEEP(245).EQ.1 .AND. CAPVAL.GE.1)
     &     WORKERS = min(WORKERS, CAPVAL)
"""),
    (b" 500  CONTINUE", b"""\
      IF (MYID.EQ.0 .AND. ICNTL(3).GT.0 .AND. ICNTL(4).GE.2)
     &  WRITE(ICNTL(3),'(A,I8,A,I8,A,I8)') ' PARAORD cap', CAPVAL,
     &  ' nprocs', NPROCS, ' paraord', NPROCS_PARAORD
"""),
]
hits = dict.fromkeys((anchor for anchor, _ in edits), 0)
with tarfile.open(pristine) as old, tarfile.open(patched, "w:gz") as new:
    for member in old:
        data = old.extractfile(member).read() if member.isfile() else None
        if member.name.endswith("/src/tools_common.F"):
            for anchor, lines in edits:
                hits[anchor] += data.count(anchor)
                if data.count(anchor) == 1:
                    end = data.index(b"\n", data.index(anchor)) + 1
                    data = data[:end] + lines + data[end:]
            member.size = len(data)
        new.addfile(member, io.BytesIO(data) if data is not None else None)
wrong = [f"'{a.decode()}' found {n} times" for a, n in hits.items() if n != 1]
if wrong:
    os.remove(patched)
    sys.exit(f"{pristine}: src/tools_common.F: {', '.join(wrong)}, expected once each")
EOF
}

# The built libpetsc must not import ZGEMMT: MUMPS is folded into it, so the name in
# its dynamic symbols is what the flag leaves behind. gfem refuses such a stack.
check_no_gemmt() {
  "$PY" - "$OUT"/petsc-"$PETSC_VERSION"-*.whl << 'EOF'
import os, sys, zipfile

with zipfile.ZipFile(sys.argv[1]) as wheel:
    names = [n for n in wheel.namelist() if os.path.basename(n).startswith("libpetsc.so")]
    if not names:
        sys.exit(f"{sys.argv[1]}: no libpetsc to check")
    for name in names:
        if b"\0zgemmt_\0" in wheel.read(name):
            sys.exit(f"{name} still imports zgemmt_: MUMPS was built with -DGEMMT_AVAILABLE")
        print(f"{name}: no zgemmt_")
EOF
}

# Every libpetsc must hold the ordering cap of patch_mumps: MUMPS is folded into
# it, and the cap's reading of MUMPS_PARAORD_MAX leaves that name in it. The
# wheel's libpetsc.so is a linker script naming the library, so not one itself.
check_paraord_cap() {
  "$PY" - "$OUT"/petsc-"$PETSC_VERSION"-*.whl << 'EOF'
import os, sys, zipfile

with zipfile.ZipFile(sys.argv[1]) as wheel:
    names = [n for n in wheel.namelist() if os.path.basename(n).startswith("libpetsc.so")]
    images = {n: wheel.read(n) for n in names}
    for name in [n for n, data in images.items() if data.startswith(b"INPUT(")]:
        print(f"{name}: linker script, skipped")
        del images[name]
    if not images:
        sys.exit(f"{sys.argv[1]}: no libpetsc to check")
    for name, data in images.items():
        if b"MUMPS_PARAORD_MAX" not in data:
            sys.exit(f"{name} lacks MUMPS_PARAORD_MAX: MUMPS was built without the ordering cap")
        print(f"{name}: MUMPS_PARAORD_MAX")
EOF
}

# The petsc wheel folds the static MUMPS, PORD and libseq libraries into
# libpetsc.so and then deletes them, but its petscvariables, petsc.pc and
# petscmachineinfo.h still list them, so SLEPc fails to link against it
# ("Unable to link with PETSc"). Remove those flags from the wheel's text
# files (never from a binary) and update their hashes in its RECORD.
patch_petsc_wheel() {
  "$PY" - "$OUT"/petsc-"$PETSC_VERSION"-*.whl << 'EOF'
import base64, csv, hashlib, io, os, re, sys, zipfile

wheel = sys.argv[1]
flags = re.compile(rb" ?-l(?:smumps|dmumps|cmumps|zmumps|mumps_common|pord|mpiseq)\b")
with zipfile.ZipFile(wheel) as old:
    (record,) = [n for n in old.namelist() if n.endswith(".dist-info/RECORD")]
    changed = {}
    for name in old.namelist():
        data = old.read(name)
        if name != record and b"\0" not in data and flags.search(data):
            changed[name] = flags.sub(b"", data)
            print(f"patched {name}")
    rows = list(csv.reader(io.StringIO(old.read(record).decode())))
    for row in rows:
        if row[0] in changed:
            digest = base64.urlsafe_b64encode(hashlib.sha256(changed[row[0]]).digest())
            row[1:] = ["sha256=" + digest.rstrip(b"=").decode(), str(len(changed[row[0]]))]
    text = io.StringIO()
    csv.writer(text, lineterminator="\n").writerows(rows)
    changed[record] = text.getvalue().encode()
    patched = wheel + ".patched"
    with zipfile.ZipFile(patched, "w") as new:
        for info in old.infolist():  # same names, order, permissions, compression
            data = changed[info.filename] if info.filename in changed else old.read(info)
            new.writestr(info, data)
os.replace(patched, wheel)
EOF
}

MPI=0 CHECK=0 SOURCES="" FETCH_SOURCES="" OUTPUT="" WITH_PARMETIS=""
while (($#)); do
  case $1 in
    --mpi) MPI=1 ;;
    --with-parmetis) WITH_PARMETIS=1 ;;
    --check) CHECK=1 ;;
    --sources)
      (($# >= 2)) || usage
      SOURCES=$2
      shift
      ;;
    --fetch-sources)
      (($# >= 2)) || usage
      FETCH_SOURCES=$2
      shift
      ;;
    -*) usage ;;
    *)
      [ -z "$OUTPUT" ] || usage
      OUTPUT=$1
      ;;
  esac
  shift
done
# exactly one of the three forms
FORMS=0
((CHECK == 0)) || FORMS=$((FORMS + 1))
[ -z "$OUTPUT" ] || FORMS=$((FORMS + 1))
[ -z "$FETCH_SOURCES" ] || FORMS=$((FORMS + 1))
((FORMS == 1)) || usage
if [ -n "$WITH_PARMETIS" ]; then
  # --fetch-sources fetches what either stack reads, so it takes no --mpi
  ((MPI == 1)) || [ -n "$FETCH_SOURCES" ] ||
    fail "ParMETIS orders only across ranks: --with-parmetis needs --mpi"
  parmetis_terms # before anything is downloaded
fi
if [ -n "$SOURCES" ]; then
  [ -d "$SOURCES" ] || fail "$SOURCES is not a folder"
  SOURCES=$(cd "$SOURCES" && pwd)
fi

leave_conda
if [ -n "$FETCH_SOURCES" ]; then
  check_prerequisites
  fetch_sources "$FETCH_SOURCES"
  exit 0
fi
check_prerequisites
if ((CHECK == 1)); then
  set_configure_options WORK
  echo "configure options: ${CONFIGURE_OPTIONS[*]}"
  echo "all prerequisites found"
  exit 0
fi

mkdir -p "$OUTPUT"
OUT=$(cd "$OUTPUT" && pwd)
[ -z "$(ls -A "$OUT")" ] || fail "$OUT is not empty: give a new or empty folder"
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

# A PETSC_DIR / PETSC_ARCH of another PETSc would leak into the build.
unset PETSC_DIR PETSC_ARCH SLEPC_DIR
# So would the compilers and flags an active conda environment exports:
# PETSc's configure ignores them, but the petsc4py and slepc4py builds use
# them, compile against conda's own PETSc headers, link conda's runtime
# libraries, and give bindings that do not import. Build with the gcc, g++
# and gfortran on PATH only.
unset CC CXX FC F77 F90 CPP CFLAGS CPPFLAGS CXXFLAGS FFLAGS FCFLAGS LDFLAGS
# MUMPS's PORD makefile reads this one from the environment, and stops on it.
unset OPTFLAGS
unset CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH LIBRARY_PATH
export UV_NO_CONFIG=1
if ((MPI == 1)) && [ -z "$SOURCES" ]; then
  read -r _ url sha256 <<< "$(printf '%s\n' "${TARBALLS[@]}" | grep "^$SCOTCH_TARBALL ")"
  download "$url" "$sha256" "$WORK/$SCOTCH_TARBALL"
fi
read -r _ url sha256 <<< "$(printf '%s\n' "${TARBALLS[@]}" | grep "^$MUMPS_TARBALL ")"
if [ -n "$SOURCES" ]; then
  cp "$SOURCES/$MUMPS_TARBALL" "$WORK/$MUMPS_TARBALL"
  check_sha256 "$WORK/$MUMPS_TARBALL" "$sha256"
else
  download "$url" "$sha256" "$WORK/$MUMPS_TARBALL"
fi
patch_mumps
set_configure_options "$WORK"
export PETSC_CONFIGURE_OPTIONS="${CONFIGURE_OPTIONS[*]}${MAKE_NP:+ --with-make-np=$MAKE_NP}"
printf '%s\n' "petsc==$PETSC_VERSION" "petsc4py==$PETSC_VERSION" \
  "slepc==$SLEPC_VERSION" "slepc4py==$SLEPC_VERSION" > "$WORK/constraints.txt"

fetch petsc "$PETSC_VERSION" "$SHA256_petsc"
fetch petsc4py "$PETSC_VERSION" "$SHA256_petsc4py"
fetch slepc "$SLEPC_VERSION" "$SHA256_slepc"
fetch slepc4py "$SLEPC_VERSION" "$SHA256_slepc4py"
patch_petsc_sdist

build petsc "$PETSC_VERSION"
patch_petsc_wheel
check_no_gemmt
check_paraord_cap
build petsc4py "$PETSC_VERSION"
build slepc "$SLEPC_VERSION"
build slepc4py "$SLEPC_VERSION"

echo "wheelhouse: $OUT"
ls "$OUT"
echo "install: pip install --no-index --find-links $OUT petsc petsc4py slepc slepc4py"