This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
esys-escript is a Python-based finite element method (FEM) module for implementing mathematical models. It supports sequential, OpenMP (multi-core), and MPI (distributed) execution modes with a hybrid OpenMP+MPI option. The project is primarily C++ with Python bindings, targeting geophysical modeling applications including inversion, earthquakes, porous media flow, and mantle convection.
The project uses SCons as its build system, not Make or CMake for the main project.
-
Create an options file: Copy a template from
scons/templates/toscons/<hostname>_options.py- Get hostname:
uname -n - Available templates: debian_options.py, ubuntu_options.py, bunya_options.py, etc.
- Get hostname:
-
Build commands:
# Standard build (uses hostname-based options file) scons -j4 # Build with specific options file scons -j4 options_file=scons/templates/debian_options.py # Build with tests scons -j4 build_full # Build and run Python tests scons -j4 py_tests # Install (default target if all domains are built) scons install
-
Important build options (configured in options file):
openmp: Enable OpenMP parallelizationmpi: MPI flavor ('OPENMPI', 'MPICH', 'INTELMPI', or 'none')trilinos: Enable Trilinos solver librarybuild_trilinos: Build Trilinos from bundled source ('make', 'always', 'never')paso: Build Paso solver librarydomains: List of domain libraries to build ('finley', 'oxley', 'ripley', 'speckley')debug: Enable debug flagsprefix: Installation prefix (default: current directory)
# Run all tests using the generated test script
sh utest.sh /path/to/build -t8 -n1 -p1
# Run sanity check after install
scons sanity
# Run Python tests only
scons py_testsThe project generates multiple types of documentation:
-
User Guide (PDF from LaTeX):
# Build user guide PDF from doc/user/ scons user_pdf # Build all PDF documentation scons pdfdocs
- Source:
doc/user/*.tex - Requires: pdflatex, bibtex, makeindex
- Output:
<prefix>/release/doc/user/user.pdf
- Source:
-
Python API Documentation (Sphinx) - Integrated Web Documentation:
# Build Sphinx-based API documentation with integrated resources scons sphinxdoc- Uses Sphinx with autodoc to extract Python docstrings
- Source: Python modules in
esys/after installation - Script:
doc/sphinx_api/genrst.pygenerates RST files from installed modules - Requires: sphinx, sphinx-build
- Configuration:
doc/sphinx_api/conf.py - Output:
<prefix>/release/doc/sphinx_api/(HTML) - Optional: Set
mathjax_pathin options file for math rendering
Integrated Documentation Features:
- The Sphinx documentation now provides a unified landing page integrating:
- Python API reference (auto-generated from docstrings)
- User Guide PDF (linked from
../user/user.pdf) - Example scripts (linked as ZIP and TAR.GZ archives)
- Custom styling via
doc/sphinx_api/_static/custom.css - The build process automatically copies the user guide PDF and example archives into the documentation tree for easy access
- Start page:
<prefix>/release/doc/sphinx_api/index.html
-
C++ API Documentation (Doxygen):
# Build Doxygen documentation for C++ classes scons api_doxygen- Requires: doxygen
- Configuration:
doc/doxygen/doxygen_esys - Output:
<prefix>/release/doc/doxygen/(HTML)
-
Example Files:
# Package examples as archives scons examples_tarfile # Creates .tar.gz scons examples_zipfile # Creates .zip
- Source:
doc/examples/(usersguide and cookbook examples) - Output:
<prefix>/release/doc/escript_examples.{tar.gz,zip}
- Source:
-
Build All Documentation:
# Build base documentation (PDF, examples, doxygen) scons basedocs # Build all documentation (base + sphinx) scons docs # Build everything for release (docs + install) scons release_prep
Documentation Directory Structure:
doc/
├── user/ # User guide LaTeX source
├── sphinx_api/ # Sphinx configuration and generation script
├── doxygen/ # Doxygen configuration
└── examples/ # Example scripts and tests
├── usersguide/
└── cookbook/
Important Notes:
- Sphinx documentation requires the project to be installed first (depends on
env['pyinstall']) - The documentation build targets use
AlwaysBuild(), so they rebuild each time - PDF generation uses custom PDFLaTeX flags to embed the git revision number
- Example files are tested as part of the build process (see
doc/SConscript)
After building, you need to set environment variables:
export PYTHONPATH=$ESYSESCRIPT
export LD_LIBRARY_PATH=$ESYSESCRIPT/lib/esys:$ESYSESCRIPT/esys.trilinos/libWhere ESYSESCRIPT is the project root directory.
The project is organized into several independent domain implementations, each providing different FEM discretization methods:
- finley: Traditional unstructured FEM
- oxley: Octree-based adaptive mesh refinement (uses p4est)
- ripley: Regular rectangular grids
- speckley: Spectral element methods
Each domain follows the same structure:
<domain>/
├── src/ # C++ implementation
├── py_src/ # Python bindings and interfaces
└── test/ # Unit tests (C++ and Python)
-
escriptcore: Core data structures and abstractions
Dataobjects: Distributed field data on FEM meshesDomainabstract interface- Function spaces and domain couplers
-
escript: High-level PDE solving interface
LinearPDEandNonLinearPDEclasses- PDE tools and utilities
- Cost functions and minimizers
-
paso: Native iterative solver library (C++)
- Sparse matrix operations
- Iterative solvers (PCG, BiCGStab, GMRES)
- Preconditioners
-
trilinoswrap: Wrapper for Trilinos solvers
- Provides access to Trilinos linear algebra
- Only for single PDEs (not systems) due to block matrix limitations
-
weipa: Data export library
- Supports SILO and VTK formats
- Visit simulation interface
-
pythonMPI: MPI wrapper for Python (when MPI is enabled)
- Python modules installed to:
<prefix>/esys/ - Shared libraries installed to:
<prefix>/lib/esys/ - Executables installed to:
<prefix>/bin/run-escript: Main launcher script (handles MPI/OpenMP setup)
- Trilinos libraries (if built):
<prefix>/esys.trilinos/lib/
The build uses a two-stage configuration:
-
Options file (
scons/*_options.py): User-specified build options- Must set
escript_opts_version = 203(current version) - Configures compilers, libraries, features
- Must set
-
Runtime configuration (
<prefix>/lib/esys/buildvars): Generated at build time- Contains build flags and paths
- Read by
run-escriptlauncher
The project includes Trilinos source code (version 16) in trilinos_source16/ and can build it automatically:
- Set
build_trilinos = "make"in options file to build Trilinos - Use
scripts/trilinos_mpi.shorscripts/trilinos_nompi.shfor build configuration - Build happens before main esys-escript build
- Trilinos is installed to
esys.trilinos/directory
The project uses Boost.Python for Python bindings:
- C++ classes wrapped in
py_src/directories - Each domain provides Python factory functions
- Data objects can be manipulated from Python but computation happens in C++
- C++ tests: Add to
<domain>/test/and update correspondingSConscript - Python tests: Add to
<domain>/test/python/withSConscriptentry
- Edit your options file in
scons/<hostname>_options.py - Rebuild with
scons -j4 - For Trilinos options, modify
scripts/trilinos_*.shscripts
- MPI must be enabled at build time (
mpi='OPENMPI'etc.) - ALWAYS use
run-escriptlauncher for MPI programs - DO NOT usempirun python3directly - Launcher flags:
-n(processes),-p(processes per node),-t(threads)
Critical: The pythonMPI wrapper (used by run-escript) initializes MPI before Python starts, which is essential for proper Trilinos/Tpetra initialization. Using mpirun python3 directly causes MPI initialization order issues that lead to index errors and crashes.
Sub-communicators with mpi4py:
- You CAN use mpi4py to create sub-communicators and pass them to escript domains
- Example:
sub_comm = MPI.COMM_WORLD.Split(color, rank); domain = Rectangle(n0=20, n1=10, comm=sub_comm) - See
doc/user/mpi4py.texfor detailed documentation and examples
The build system detects compilers and applies appropriate flags:
- GNU: g++ (default flags in SConstruct lines 384-398)
- Intel: icpc (lines 355-369)
- Clang: clang++ (lines 412-427)
- MSVC: cl/icl for Windows builds
SConstruct: Main build script (reads options, configures environment, builds all modules)scons/site_init.py: Build system initialization and helper functionsscons/dependencies.py: Dependency checking functionsbin/run-escript: Main launcher (generated during build)scripts/trilinos_mpi.sh/scripts/trilinos_nompi.sh: Trilinos build scriptsdoc/SConscript: Documentation build orchestrationdoc/sphinx_api/genrst.py: Auto-generates RST files from installed Python modulesdoc/sphinx_api/conf.py: Sphinx configuration for API documentationdoc/user/user.tex: Main LaTeX source for user guide
- The build system assumes you will NOT move Python after building
- Library paths are set at build time in the launcher script
- Each domain is largely independent but shares the core abstractions
- Changes to C++ code require rebuild, Python-only changes do not
- The project uses C++17 standard