2025-06-21 05:35:40 -04:00
<p align="center">
2025-07-31 15:04:57 -04:00
<img src="https://github.com/4D-STAR/GridFire/blob/main/assets/logo/GridFire.png?raw=true" width="300" alt="OPAT Core Libraries Logo">
2025-06-21 05:35:40 -04:00
</p>
2025-07-31 15:41:27 -04:00
---




'&style=for-the-badge&label=GitHub%20Main%20Branch)

2025-06-21 05:35:40 -04:00
---
2025-07-31 10:54:31 -04:00
# Introduction
GridFire is a C++ library designed to perform general nuclear network
evolution. It is part of the larger SERiF project within the 4D-STAR
collaboration. GridFire is primarily focused on modeling the most relevant
burning stages for stellar evolution modeling. Currently, there is limited
support for inverse reactions. Therefore, GridFire has a limited set of tools
2025-07-31 18:10:13 -04:00
to evolve a fusing plasma in NSE; however, this is not the primary focus of
the library and has therefore not had significant development. For those
2025-07-31 10:54:31 -04:00
interested in modeling super nova, neutron star mergers, or other high-energy
2025-07-31 18:10:13 -04:00
astrophysical phenomena, we **strongly ** recommend using
2025-07-31 10:54:31 -04:00
[SkyNet ](https://bitbucket.org/jlippuner/skynet/src/master/ ).
## Design Philosophy and Workflow
GridFire is architected to balance physical fidelity, computational efficiency,
and extensibility when simulating complex nuclear reaction networks. Users
begin by defining a composition, which is used to construct a full GraphEngine
representation of the reaction network. A GraphNetwork uses [JINA
Reaclib](https://reaclib.jinaweb.org/index.php) reaction rates ([Cyburt et al., ApJS 189
(2010) 240.](https://iopscience.iop.org/article/10.1088/0067-0049/189/1/240)) along
with a dynamically constructed network topology. To manage the inherent
stiffness and multiscale nature of these networks, GridFire employs a layered
view strategy: partitioning algorithms isolate fast and slow processes,
adaptive culling removes negligible reactions at runtime, and implicit solvers
stably integrate the remaining stiff system. This modular pipeline allows
researchers to tailor accuracy versus performance trade-offs, reuse common
engine components, and extend screening or partitioning models without
modifying core integration routines.
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
## Funding
GridFire is a part of the 4D-STAR collaboration.
2025-06-21 16:54:23 -04:00
2025-07-31 10:54:31 -04:00
4D-STAR is funded by European Research Council (ERC) under the Horizon Europe
programme (Synergy Grant agreement No. 101071505: 4D-STAR) Work for this
project is funded by the European Union. Views and opinions expressed are
however those of the author(s) only and do not necessarily reflect those of the
European Union or the European Research Council.
# Usage
2025-06-21 16:54:23 -04:00
2025-07-30 15:15:37 -04:00
## Python installation
By far the easiest way to install is with pip. This will install either
pre-compiled wheels or, if your system has not had a wheel compiled for it, it
will try to build locally (this may take **a long time ** ). The python bindings
are just that and should maintain nearly the same speed as the C++ code. End
2025-07-31 18:10:13 -04:00
users are strongly encouraged to use the python module rather than the C++ code.
2025-07-30 15:15:37 -04:00
### pypi
2025-07-31 15:41:27 -04:00
Installing from pip is as simple as
2025-07-30 15:15:37 -04:00
```bash
pip install gridfire
```
2025-07-31 10:54:31 -04:00
These wheels have been compiled on many systems
| Version | Platform | Architecture | CPython Versions | PyPy Versions |
2025-07-31 15:41:27 -04:00
|---------|----------|--------------|------------------------------------------------------------|---------------|
2025-07-31 10:54:31 -04:00
| 0.5.0 | macOS | arm64 | 3.8, 3.9, 3.10, 3.11, 3.12, 3.13 (std & t), 3.14 (std & t) | 3.10, 3.11 |
| 0.5.0 | Linux | aarch64 | 3.8, 3.9, 3.10, 3.11, 3.12, 3.13 (std & t), 3.14 (std & t) | 3.10, 3.11 |
| 0.5.0 | Linux | x86\_64 | 3.8, 3.9, 3.10, 3.11, 3.12, 3.13 (std & t), 3.14 (std & t) | 3.10, 3.11 |
2025-07-31 18:10:13 -04:00
> **Note**: Currently macOS x86\_64 does **not** have a precompiled wheel. Due
2025-07-31 10:54:31 -04:00
> to that platform being phased out it is likely that there will never be
2025-07-31 15:41:27 -04:00
> precompiled wheels or releases for it.
2025-07-31 10:54:31 -04:00
2025-07-31 15:41:27 -04:00
> **Note:** macOS wheels were targeted to macOS 12 Monterey and should work on
2025-07-31 10:54:31 -04:00
> any version more recent than that (at least as of August 2025).
> **Note:** Linux wheels were compiled using manylinux_2_28 and are expected to
> work on Debian 10+, Ubuntu 18.10+, Fedora 29+, or CentOS/RHEL 8+
> **Note:** If your system does not have a prebuilt wheel the source
> distribution will download from pypi and try to build. This may simply not
> work if you do not have the correct system dependencies installed. If it
> fails the best bet is to try to build boost >= 1.83.0 from source and install
> (https://www.boost.org/) as that is the most common broken dependency.
### source
2025-07-31 15:41:27 -04:00
The user may also build the python bindings directly from source
2025-07-31 10:54:31 -04:00
```bash
git clone https://github.com/4D-STAR/GridFire
cd GridFire
pip install .
```
> **Note:** that if you do not have all system dependencies installed this will
> fail, the steps in further sections address these in more detail.
### source for developers
2025-07-31 15:41:27 -04:00
If you are a developer and would like an editable and incremental python
2025-07-31 10:54:31 -04:00
install `meson-python` makes this very easy
```bash
git clone https://github.com/4D-STAR/GridFire
cd GridFire
pip install -e . --no-build-isolation -vv
```
2025-07-31 15:41:27 -04:00
This will generate incremental builds whenever source code changes, and you run
a python script automatically (note that since `meson setup` must run for each
2025-07-31 10:54:31 -04:00
of these it does still take a few seconds to recompile regardless of how small
2025-07-31 15:41:27 -04:00
a source code change you have made). It is **strongly ** recommended that
2025-07-31 10:54:31 -04:00
developers use this approach and end users * do not * .
2025-07-30 15:15:37 -04:00
2025-07-29 13:44:43 -04:00
## Automatic Build and Installation
### Script Build and Installation Instructions
2025-06-21 16:54:23 -04:00
2025-07-31 10:54:31 -04:00
The easiest way to build GridFire is using the `install.sh` or `install-tui.sh`
scripts in the root directory. To use these scripts, simply run:
2025-07-29 09:25:44 -04:00
```bash
./install.sh
# or
./install-tui.sh
```
2025-07-31 10:54:31 -04:00
The regular installation script will select a standard "ideal" set of build
options for you. If you want more control over the build options, you can use
the `install-tui.sh` script, which will provide a text-based user interface to
2025-07-29 09:25:44 -04:00
select the build options you want.
2025-07-31 10:54:31 -04:00
Generally, both are intended to be easy to use and will prompt you
automatically to install any missing dependencies.
2025-07-29 09:25:44 -04:00
2025-07-29 13:44:43 -04:00
2025-07-31 15:41:27 -04:00
### Currently, known good platforms
2025-07-31 10:54:31 -04:00
The installation script has been tested and found to work on clean
installations of the following platforms:
2025-07-29 13:44:43 -04:00
- MacOS 15.3.2 (Apple Silicon + brew installed)
- Fedora 42.0 (aarch64)
- Ubuntu 25.04 (aarch64)
- Ubuntu 22.04 (X86_64)
2025-07-31 10:54:31 -04:00
> **Note:** On Ubuntu 22.04 the user needs to install boost libraries manually
> as the versions in the Ubuntu repositories
> are too old. The installer automatically detects this and will instruct the
> user in how to do this.
2025-07-29 13:44:43 -04:00
## Manual Build Instructions
2025-07-24 11:10:45 -04:00
### Prerequisites
2025-07-31 10:54:31 -04:00
These only need to be manually installed if the user is not making use of the
`install.sh` or `install-tui.sh`
2025-07-29 13:44:43 -04:00
2025-07-31 10:54:31 -04:00
#### Required
2025-07-24 11:10:45 -04:00
- C++ compiler supporting C++23 standard
- Meson build system (>= 1.5.0)
2025-07-31 10:54:31 -04:00
- Python 3.8 or newer
2025-07-29 08:31:50 -04:00
- CMake 3.20 or newer
2025-07-31 10:54:31 -04:00
- ninja 1.10.0 or newer
2025-07-24 11:10:45 -04:00
- Python packages: `meson-python>=0.15.0`
2025-07-31 10:54:31 -04:00
- Boost libraries (>= 1.83.0) installed system-wide (or at least findable by
2025-07-31 15:41:27 -04:00
meson with pkg-config)
2025-07-31 10:54:31 -04:00
#### Optional
- dialog (used by the `install.sh` script, not needed if using pip or meson
2025-07-31 15:41:27 -04:00
directly)
2025-07-31 10:54:31 -04:00
- pip (used by the `install.sh` script or by calling pip directly, not needed
2025-07-31 15:41:27 -04:00
if using meson directly)
2025-07-31 10:54:31 -04:00
> **Note:** Boost is the only external library dependency used by GridFire directly.
> **Note:** Windows is not supported at this time and *there are no plans to
> support it in the future*. Windows users are encouraged to use WSL2 or a
> Linux VM.
> **Note:** If `install-tui.sh` is not able to find a usable version of boost
> it will provide directions to fetch, compile, and install a usable version.
### Install Scripts
GridFire ships with an installer (`install.sh` ) which is intended to make the
2025-07-31 15:41:27 -04:00
process of installation both easier and more repeatable.
2025-07-31 10:54:31 -04:00
#### Ease of Installation
2025-07-31 15:41:27 -04:00
Both scripts are intended to automate installation more or less completely. This
2025-07-31 10:54:31 -04:00
includes dependency checking. In the event that a dependency cannot be found
they try to install (after explicitly asking for user permission). If that does
2025-07-31 15:41:27 -04:00
not work they will provide a clear message as to what went wrong.
2025-07-31 10:54:31 -04:00
#### Reproducibility
The TUI mode provides easy modification of meson build system and compiler
settings which can then be saved to a config file. This config file can then be
loaded by either tui mode or cli mode (with the `--config` ) flag meaning that
build configurations can be made and reused. Note that this is **not ** a
2025-07-31 15:41:27 -04:00
deterministically reproducible build system as it does not interact with any
2025-07-31 10:54:31 -04:00
system dependencies or settings, only meson and compiler settings.
#### Examples
##### TUI config and saving
[](https://asciinema.org/a/ahIrQPL71ErZv5EKKujfO1ZEW)
##### TUI config loading and meson setup
[](https://asciinema.org/a/zGdzt9kYsETltG0TJKC50g3BK)
##### CLI config loading, setup, and build
[](https://asciinema.org/a/GYaWTXZbDJRD4ohde0s3DkFMC)
2025-07-31 15:41:27 -04:00
> **Note:** `install-tui.sh` is simply a script which calls `install.sh` with
2025-07-31 10:54:31 -04:00
> the `--tui` flag. You can get the exact same results by running `install.sh
> --tui`.
> **Note:** Call `install.sh` with the `--help` or `--h` flag to see command
2025-07-31 15:41:27 -04:00
> line options
2025-06-21 16:54:23 -04:00
2025-07-31 10:54:31 -04:00
> **Note:** `clang` tends to compile GridFire much faster than `gcc` thus why I
> select it in the above asciinema recording.
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
### Dependency Installation on Common Platforms
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
- **Ubuntu/Debian:**
2025-07-31 10:54:31 -04:00
```bash
sudo apt-get update
sudo apt-get install -y build-essential meson python3 python3-pip libboost-all-dev
```
> **Note:** Depending on the ubuntu version you have the libboost-all-dev
> libraries may be too old. If this is the case refer to the boost
2025-07-31 15:41:27 -04:00
> documentation for how to download and install a version `>=1.83.0`
2025-07-31 10:54:31 -04:00
> **Note:** On recent versions of ubuntu python has switched to being
2025-07-31 15:41:27 -04:00
> externally managed by the system. We **strongly** recommend that if you
> install manually all python packages are installed inside some kind of
> virtual environment (e.g. `pyenv`, `conda`, `python-venv`, etc...). When using
2025-07-31 10:54:31 -04:00
> the installer script this is handled automatically using `python-venv`.
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
- **Fedora/CentOS/RHEL:**
2025-07-31 10:54:31 -04:00
```bash
sudo dnf install -y gcc-c++ meson python3 python3-pip boost-devel
```
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
- **macOS (Homebrew):**
2025-07-31 10:54:31 -04:00
```bash
brew update
brew install boost meson python
```
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
### Building the C++ Library
2025-06-21 16:54:23 -04:00
```bash
2025-07-24 11:10:45 -04:00
meson setup build
2025-06-21 16:54:23 -04:00
meson compile -C build
2025-07-24 11:10:45 -04:00
```
2025-07-31 10:54:31 -04:00
#### Clang vs. GCC
As noted above `clang` tends to compile GridFire much faster than `gcc` . If
your system has both `clang` and `gcc` installed you may force meson to use
2025-07-31 15:41:27 -04:00
clang via environmental variables
2025-07-31 10:54:31 -04:00
2025-07-24 11:10:45 -04:00
```bash
2025-07-31 10:54:31 -04:00
CC=clang CXX=clang++ meson setup build_clang
meson compile -C build_clang
2025-06-21 16:54:23 -04:00
```
2025-07-31 10:54:31 -04:00
### Installing the Library
2025-07-24 11:10:45 -04:00
```bash
2025-07-31 10:54:31 -04:00
meson install -C build
2025-07-24 11:10:45 -04:00
```
2025-07-31 10:54:31 -04:00
### Minimum compiler versions
GridFire uses C++23 features and therefore only compilers and standard library
2025-07-31 15:41:27 -04:00
implementations which support C++23 are supported. Generally we have found that
`gcc >= 13.0.0` or `clang >= 16.0.0` work well.
2025-07-31 10:54:31 -04:00
2025-07-24 11:10:45 -04:00
## Code Architecture and Logical Flow
2025-07-31 10:54:31 -04:00
GridFire is organized into a series of composable modules, each responsible for
a specific aspect of nuclear reaction network modeling. The core components
include:
- **Engine Module:** Core interfaces and implementations (e.g., `GraphEngine` )
2025-07-31 15:41:27 -04:00
that evaluate reaction network rate equations and energy generation. Also
implemented `Views` submodule.
2025-07-31 10:54:31 -04:00
- **Engine::Views Module:** Composable engine optimization and modification
2025-07-31 15:41:27 -04:00
(e.g. `MultiscalePartitioningEngineView` ) which can be used to make a problem
more tractable or applicable.
2025-07-31 10:54:31 -04:00
- **Screening Module:** Implements nuclear reaction screening corrections (e.g.
2025-07-31 15:41:27 -04:00
`WeakScreening` ([Salpeter,
1954](https://adsabs.harvard.edu/full/1954AuJPh...7..373S)), `BareScreening` )
affecting reaction rates.
2025-07-31 10:54:31 -04:00
- **Reaction Module:** Parses and manages Reaclib reaction rate data, providing
2025-07-31 15:41:27 -04:00
temperature- and density-dependent rate evaluations.
2025-07-31 10:54:31 -04:00
- **Partition Module:** Implements partition functions (e.g.,
2025-07-31 15:41:27 -04:00
`GroundStatePartitionFunction` , `RauscherThielemannPartitionFunction`
([Rauscher & Thielemann,
2000](https://www.sciencedirect.com/science/article/pii/S0092640X00908349?via%3Dihub]))
to weight reaction rates based on nuclear properties.
2025-07-31 10:54:31 -04:00
- **Solver Module:** Defines numerical integration strategies (e.g.,
2025-07-31 15:41:27 -04:00
`DirectNetworkSolver` ) for solving the stiff ODE systems arising from reaction
networks.
2025-07-31 10:54:31 -04:00
- **Python Interface:** Exposes * almost * all C++ functionality to Python,
2025-07-31 15:41:27 -04:00
allowing users to define compositions, configure engines, and run simulations
directly from Python scripts.
2025-07-31 10:54:31 -04:00
Generally a user will start by selecting a base engine (currently we only offer
`GraphEngine` ), which constructs the full reaction network graph from a given
composition. The user can then apply various engine views to adapt the network
topology, such as partitioning fast and slow reactions, adaptively culling
low-flow pathways, or priming the network with specific species. Finally, a
numerical solver is selected to integrate the network over time, producing
updated
abundances and diagnostics.
## Engines
GridFire is, at its core, based on a series of `Engines` . These are constructs
which know how to report information on series of ODEs which need to be solved
2025-07-31 18:10:13 -04:00
to evolve abundances. The important thing to understand about `Engines` is
2025-07-31 15:41:27 -04:00
that they contain all the detailed physics GridFire uses. For example a
2025-07-31 10:54:31 -04:00
`Solver` takes an `Engine` but does not compute physics itself. Rather, it asks
the `Engine` for stuff like the jacobian matrix, stoichiometry, nuclear energy
generation rate, and change in abundance with time.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
Refer to the API documentation for the exact interface which an `Engine` must
2025-07-31 15:41:27 -04:00
implement to be compatible with GridFire solvers.
2025-07-31 10:54:31 -04:00
2025-07-31 15:41:27 -04:00
Currently, we only implement `GraphEngine` which is intended to be a very general and
2025-07-31 10:54:31 -04:00
adaptable `Engine` .
### GraphEngine
In GridFire the `GraphEngine` will generally be the most fundamental building
block of a nuclear network. A `GraphEngine` represents a directional hypergraph
connecting some set of atomic species through reactions listed in the [JINA
2025-07-31 15:41:27 -04:00
Reaclib database](https://reaclib.jinaweb.org/index.php).
2025-07-31 10:54:31 -04:00
`GraphEngine` s are constructed from a seed composition of species from which
2025-07-31 15:41:27 -04:00
they recursively expand their topology outward, following known reaction
pathways and adding new species to the tracked list as they expand.
2025-07-29 08:22:31 -04:00
2025-07-24 11:10:45 -04:00
### GraphEngine Configuration Options
2025-07-31 10:54:31 -04:00
GraphEngine exposes runtime configuration methods to tailor network
construction and rate evaluations:
2025-07-24 11:10:45 -04:00
- **Constructor Parameters:**
2025-07-31 15:41:27 -04:00
- `composition` : The initial seed composition to start network construction from.
- `BuildDepthType` (`Full` , `Shallow` , `SecondOrder` , etc...): controls
number of recursions used to construct the network topology. Can either be a
member of the `NetworkBuildDepth` enum or an integer.
- `partition::PartitionFunction` : Partition function used when evaluating
detailed balance for inverse rates.
2025-07-24 11:10:45 -04:00
- **setPrecomputation(bool precompute):**
2025-07-31 15:41:27 -04:00
- Enable/disable caching of reaction rates and stoichiometric data at initialization.
- *Effect:* Reduces per-step overhead; increases memory and setup time.
2025-07-24 11:10:45 -04:00
- **setScreeningModel(ScreeningType type):**
2025-07-31 15:41:27 -04:00
- Choose plasma screening (models: `BARE` , `WEAK` ).
- *Effect:* Alters rate enhancement under dense/low-T conditions, impacting stiffness.
2025-07-24 11:10:45 -04:00
- **setUseReverseReactions(bool useReverse):**
2025-07-31 15:41:27 -04:00
- Toggle inclusion of reverse (detailed balance) reactions.
- *Effect:* Improves equilibrium fidelity; increases network size and stiffness.
2025-07-24 11:10:45 -04:00
### Available Partition Functions
2025-07-31 15:41:27 -04:00
| Function Name | Identifier / Enum | Description |
|---------------------------------------|----------------------|--------------------------------------------------------------------------------------------------------------------------|
| `GroundStatePartitionFunction` | "GroundState" | Weights using nuclear ground-state spin factors. |
| `RauscherThielemannPartitionFunction` | "RauscherThielemann" | Interpolates normalized g-factors per Rauscher & Thielemann. |
| `CompositePartitionFunction` | "Composite" | Combines multiple partition functions for situations where different partitions functions are used for different domains |
2025-07-31 10:54:31 -04:00
### AutoDiff
One of the primary tasks any engine must accomplish is to report the jacobian
matrix of the system to the solver. `GraphEngine` uses `CppAD` , a C++ auto
2025-07-31 15:41:27 -04:00
differentiation library, to generate analytic jacobian matrices very
efficiently.
2025-07-31 10:54:31 -04:00
## Reaclib in GridFire
All reactions in JINA Reaclib which only include reactants iron and lighter
were downloaded on June 17th, 2025 where the most recent documented change on
the JINA Reaclib site was on June 24th, 2021.
2025-07-31 15:41:27 -04:00
All of these reactions have been compiled into a header file which is then
2025-07-31 10:54:31 -04:00
statically compiled into the gridfire binaries (specifically into
lib_reaction_reaclib.cpp.o). This does increase the binary size by a few MB;
2025-07-31 15:41:27 -04:00
however, the benefit is faster load times and more importantly no need for end
2025-07-31 10:54:31 -04:00
users to manage resource files.
If a developer wants to add new reaclib reactions we include a script at
2025-07-31 15:41:27 -04:00
`utils/reaclib/format.py` which can ingest a reaclib data file and produce the
2025-07-31 10:54:31 -04:00
needed header file. More details on this process are included in
`utils/reaclib/readme.md`
2025-07-24 11:10:45 -04:00
2025-06-21 16:54:23 -04:00
2025-07-24 11:10:45 -04:00
## Engine Views
2025-06-21 16:54:23 -04:00
2025-07-31 10:54:31 -04:00
The GridFire engine supports multiple engine view strategies to adapt or
restrict network topology. Generally when extending GridFire the approach is
likely to be one of adding new `EngineViews` .
2025-07-24 11:10:45 -04:00
2025-07-31 15:41:27 -04:00
| View Name | Purpose | Algorithm / Reference | When to Use |
|----------------------------------|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|
| AdaptiveEngineView | Dynamically culls low-flow species and reactions during runtime | Iterative flux thresholding to remove reactions below a flow threshold | Large networks to reduce computational cost |
| DefinedEngineView | Restricts the network to a user-specified subset of species and reactions | Static network masking based on user-provided species/reaction lists | Targeted pathway studies or code-to-code comparisons |
| FileDefinedEngineView | Load a defined engine view from a file using some parser | Same as DefinedEngineView but loads from a file | Same as DefinedEngineView |
| MultiscalePartitioningEngineView | Partitions the network into fast and slow subsets based on reaction timescales | Network partitioning following Hix & Thielemann Silicon Burning I & II (DOI:10.1086/177016,10.1086/306692) | Stiff, multi-scale networks requiring tailored integration |
| NetworkPrimingEngineView | Primes the network with an initial species or set of species for ignition studies | Single-species ignition and network priming | Investigations of ignition triggers or initial seed sensitivities |
2025-07-31 10:54:31 -04:00
These engine views implement the common Engine interface and may be composed in
any order to build complex network pipelines. New view types can be added by
deriving from the `EngineView` base class, and linked into the composition
chain without modifying core engine code.
### A Note about composability
There are certain functions for which it is expected that a call to an engine
2025-07-31 15:41:27 -04:00
view will propagate the result down the chain of engine views, eventually
2025-07-31 10:54:31 -04:00
reaching the base engine (e.g. `DynamicEngine::update` ). We do not strongly
enforce this as it is not hard to contrive a situation where that is not the
2025-07-31 15:41:27 -04:00
mose useful behavior; however, we do strongly encourage developers to think
carefully about passing along calls to base engine methods when implementing
2025-07-31 10:54:31 -04:00
new views.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
## Numerical Solver Strategies
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
GridFire defines a flexible solver architecture through the
`networkfire::solver::NetworkSolverStrategy` interface, enabling multiple ODE
integration algorithms to be used interchangeably with any engine that
implements the `Engine` or `DynamicEngine` contract.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
### NetworkSolverStrategy<EngineT>:
2025-07-31 15:41:27 -04:00
All GridFire solvers implement the abstract strategy templated by
2025-07-31 10:54:31 -04:00
`NetworkSolverStrategy` which enforces only that there is some `evaluate`
method with the following signature
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
```cpp
NetOut evaluate(const NetIn& netIn);
```
Which is intended to integrate some network over some time and returns updated
abundances, temperature, density, and diagnostics.
### NetIn and NetOut
GridFire solvers use a unified input and output type for their public interface
2025-07-31 15:41:27 -04:00
(though as developers will quickly learn, internally these are immediately
2025-07-31 10:54:31 -04:00
broken down into simpler data structures). All solvers expect a `NetIn` struct
for the input type to the `evaluate` method and return a `NetOut` struct.
#### NetIn
A `NetIn` struct contains
- The composition to start the timestep at. (`NetIn::composition` )
- The temperature in Kelvin (`NetIn::temperature` )
- The density in g/cm^3 (`NetIn::density` )
2025-07-31 18:10:13 -04:00
- The max time to evolve the network to in seconds (`NetIn::tMax` )
2025-07-31 10:54:31 -04:00
- The initial timestep to use in seconds (`NetIn::dt0` )
- The initial energy in the system in ergs (`NetIn::energy` )
>**Note:** It is often useful to set `NetIn::dt0` to something * very * small and
2025-07-31 15:41:27 -04:00
>let an iterative time stepper push the timestep up. Often for main sequence
2025-07-31 10:54:31 -04:00
>burning I use ~1e-12 for dt0
>**Note:** The composition must be a `fourdst::composition::Composition`
2025-07-31 15:41:27 -04:00
>object. This is made available through the `foursdt` library and the
2025-07-31 10:54:31 -04:00
>`fourdst/composition/Composition.h` header. `fourdst` is installed
>automatically with GridFire
>**Note:** In Python composition comes from `fourdst.composition.Composition`
>and similarly is installed automatically when building GridFire python
>bindings.
#### NetOut
A `NetOut` struct contains
- The final composition after evolving to `tMax` (`NetOut::composition` )
- The number of steps the solver took to evolve to `tmax` (`NetOut::num_steps` )
- The final energy generated by the network while evolving to `tMax`
2025-07-31 15:41:27 -04:00
(`NetOut::energy` )
2025-07-31 10:54:31 -04:00
>**Note:** Currently `GraphEngine` only considers energy due to nuclear mass
>defect and not neutrino loss.
2025-07-24 11:10:45 -04:00
### DirectNetworkSolver (Implicit Rosenbrock Method)
2025-07-31 10:54:31 -04:00
- **Integrator:** Implicit Rosenbrock4 scheme (order 4) via `Boost.Odeint` ’ s
2025-07-31 15:41:27 -04:00
`rosenbrock4<double>` , optimized for stiff reaction networks with adaptive step
size control using configurable absolute and relative tolerances.
2025-07-31 10:54:31 -04:00
- **Jacobian Assembly:** Asks the base engine for the Jacobian Matrix
2025-07-31 15:41:27 -04:00
- **RHS Evaluation:** Asks the base engine for RHS of the abundance evolution
equations
2025-07-31 10:54:31 -04:00
- **Linear Algebra:** Utilizes `Boost.uBLAS` for state vectors and dense Jacobian
2025-07-31 15:41:27 -04:00
matrices, with sparse access patterns supported via coordinate lists of nonzero
entries.
2025-07-31 10:54:31 -04:00
- **Error Control and Logging:** Absolute and relative tolerance parameters
2025-07-31 15:41:27 -04:00
(`absTol` , `relTol` ) are read from configuration; Quill loggers, which run in a
separate non blocking thread, capture integration diagnostics and step
statistics.
2025-07-24 11:10:45 -04:00
### Algorithmic Workflow in DirectNetworkSolver
2025-07-31 10:54:31 -04:00
1. **Initialization: ** Convert input temperature to T9 units, retrieve
tolerances, and initialize state vector `Y` from equilibrated composition.
2. **Integrator Setup: ** Construct the controlled Rosenbrock4 stepper and bind
`RHSManager` and `JacobianFunctor` .
2025-07-24 11:10:45 -04:00
3. **Adaptive Integration Loop: **
2025-07-31 15:41:27 -04:00
- Perform `integrate_adaptive` advancing until `tMax` , catching any
`StaleEngineTrigger` to repartition the network and update composition.
- On each substep, observe states and log via `RHSManager::observe` .
2025-07-31 10:54:31 -04:00
4. **Finalization: ** Assemble final mass fractions, compute accumulated energy,
and populate `NetOut` with updated composition and diagnostics.
2025-07-24 11:10:45 -04:00
### Future Solver Implementations
2025-07-31 10:54:31 -04:00
- **Operator Splitting Solvers:** Strategies to decouple thermodynamics,
2025-07-31 15:41:27 -04:00
screening, and reaction substeps for performance on stiff, multiscale
networks.
2025-07-31 10:54:31 -04:00
- **GPU-Accelerated Solvers:** Planned use of CUDA/OpenCL backends for
2025-07-31 15:41:27 -04:00
large-scale network integration.
2025-07-31 10:54:31 -04:00
- **Callback observer support:** Currently we use an observer built into our
2025-07-31 15:41:27 -04:00
`RHSManager` (`RHSManager::observe` ); however, we intend to include support for
custom, user defined, observer method.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
These strategies can be developed by inheriting from `NetworkSolverStrategy`
and registering against the same engine types without modifying existing engine
code.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
## Python Extensibility
Through the Python bindings, users can subclass engine view classes directly in
Python, override methods like `evaluate` or `generateStoichiometryMatrix` , and
pass instances back into C++ solvers. This enables rapid prototyping of custom
view strategies without touching C++ sources.
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
# Usage Examples
## C++
### GraphEngine Initialization
2025-07-31 15:04:57 -04:00
```c++
2025-07-24 11:10:45 -04:00
#include "gridfire/engine/engine_graph.h"
#include "fourdst/composition/composition.h"
2025-07-31 15:04:57 -04:00
int main(){
// Define a composition and initialize the engine
fourdst::composition::Composition comp;
gridfire::GraphEngine engine(comp);
}
2025-06-21 16:54:23 -04:00
```
2025-07-31 10:54:31 -04:00
### Adaptive Network View
2025-07-31 15:04:57 -04:00
```c++
2025-07-24 11:10:45 -04:00
#include "gridfire/engine/views/engine_adaptive.h"
#include "gridfire/engine/engine_graph.h"
2025-06-21 16:54:23 -04:00
2025-07-31 15:04:57 -04:00
int main(){
fourdst::composition::Composition comp;
gridfire::GraphEngine baseEngine(comp);
// Dynamically adapt network topology based on reaction flows
gridfire::AdaptiveEngineView adaptiveView(baseEngine);
}
2025-06-21 16:54:23 -04:00
```
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
### Composition Initialization
2025-07-31 15:04:57 -04:00
```c++
2025-07-31 10:54:31 -04:00
#include "fourdst/composition/composition.h"
#include <vector>
#include <string>
2025-07-30 15:12:46 -04:00
2025-07-31 10:54:31 -04:00
#include <iostream>
2025-07-30 15:12:46 -04:00
2025-07-31 15:04:57 -04:00
int main() {
fourdst::composition::Composition comp;
2025-07-30 15:12:46 -04:00
2025-07-31 15:04:57 -04:00
std::vector<std::string> symbols = {"H-1", "He-4", "C-12"};
std::vector<double> massFractions = {0.7, 0.29, 0.01};
2025-07-30 15:12:46 -04:00
2025-07-31 15:04:57 -04:00
comp.registerSymbols(symbols);
comp.setMassFraction(symbols, massFractions);
2025-07-30 15:12:46 -04:00
2025-07-31 15:04:57 -04:00
comp.finalize(true);
2025-07-31 10:54:31 -04:00
2025-07-31 15:04:57 -04:00
std::cout << comp << std::endl;
}
2025-06-21 16:54:23 -04:00
```
2025-06-23 15:18:56 -04:00
2025-07-31 10:54:31 -04:00
### Common Workflow Example
2025-06-21 16:54:23 -04:00
2025-07-31 10:54:31 -04:00
A representative workflow often composes multiple engine views to balance
accuracy, stability, and performance when integrating stiff nuclear networks:
2025-06-21 16:54:23 -04:00
2025-07-31 15:04:57 -04:00
```c++
2025-07-31 10:54:31 -04:00
#include "gridfire/engine/engine.h" // Unified header for real usage
2025-07-31 15:04:57 -04:00
#include "gridfire/solver/solver.h" // Unified header for solvers
2025-07-24 11:10:45 -04:00
#include "fourdst/composition/composition.h"
2025-06-21 16:54:23 -04:00
2025-07-31 15:04:57 -04:00
int main(){
// 1. Define initial composition
fourdst::composition::Composition comp;
2025-07-31 10:54:31 -04:00
2025-07-31 15:04:57 -04:00
std::vector<std::string> symbols = {"H-1", "He-4", "C-12"};
std::vector<double> massFractions = {0.7, 0.29, 0.01};
2025-07-31 10:54:31 -04:00
2025-07-31 15:04:57 -04:00
comp.registerSymbols(symbols);
comp.setMassFraction(symbols, massFractions);
2025-07-31 10:54:31 -04:00
2025-07-31 15:04:57 -04:00
comp.finalize(true);
2025-07-31 10:54:31 -04:00
2025-07-31 15:04:57 -04:00
// 2. Create base network engine (full reaction graph)
gridfire::GraphEngine baseEngine(comp, NetworkBuildDepth::SecondOrder)
2025-06-21 16:54:23 -04:00
2025-07-31 15:04:57 -04:00
// 3. Partition network into fast/slow subsets (reduces stiffness)
gridfire::MultiscalePartitioningEngineView msView(baseEngine);
2025-07-24 11:10:45 -04:00
2025-07-31 15:04:57 -04:00
// 4. Adaptively cull negligible flux pathways (reduces dimension & stiffness)
gridfire::AdaptiveEngineView adaptView(msView);
2025-07-24 11:10:45 -04:00
2025-07-31 15:04:57 -04:00
// 5. Construct implicit solver (handles remaining stiffness)
gridfire::DirectNetworkSolver solver(adaptView);
2025-07-24 11:10:45 -04:00
2025-07-31 15:04:57 -04:00
// 6. Prepare input conditions
NetIn input{
comp, // composition
1.5e7, // temperature [K]
1.5e2, // density [g/cm^3]
1e-12, // initial timestep [s]
3e17 // integration end time [s]
};
2025-07-24 11:10:45 -04:00
2025-07-31 15:04:57 -04:00
// 7. Execute integration
NetOut output = solver.evaluate(input);
std::cout << "Final results are: " << output << std::endl;
}
2025-07-31 10:54:31 -04:00
```
#### Workflow Components and Effects
- **GraphEngine** constructs the full reaction network, capturing all species
2025-07-31 15:41:27 -04:00
and reactions.
2025-07-31 10:54:31 -04:00
- **MultiscalePartitioningEngineView** segregates reactions by characteristic
2025-07-31 15:41:27 -04:00
timescales (Hix & Thielemann), reducing the effective stiffness by treating
fast processes separately.
2025-07-31 10:54:31 -04:00
- **AdaptiveEngineView** prunes low-flux species/reactions at runtime,
2025-07-31 15:41:27 -04:00
decreasing dimensionality and improving computational efficiency.
2025-07-31 10:54:31 -04:00
- **DirectNetworkSolver** employs an implicit Rosenbrock method to stably
2025-07-31 15:41:27 -04:00
integrate the remaining stiff system with adaptive step control.
2025-07-31 10:54:31 -04:00
This layered approach enhances stability for stiff networks while maintaining
accuracy and performance.
2025-07-31 15:04:57 -04:00
### Callback Example
Custom callback functions can be registered with any solver. Because it might make sense for each solver to provide
2025-07-31 15:41:27 -04:00
different context to the callback function, you should use the struct `gridfire::solver::<SolverName>::TimestepContext`
as the argument type for the callback function. This struct contains all the information provided by that solver to
2025-07-31 15:04:57 -04:00
the callback function.
```c++
#include "gridfire/engine/engine.h" // Unified header for real usage
#include "gridfire/solver/solver.h" // Unified header for solvers
#include "fourdst/composition/composition.h"
#include "fourdst/atomic/species.h"
#include <iostream>
void callback(const gridfire::solver::DirectNetworkSolver::TimestepContext& context) {
int H1Index = context.engine.getSpeciesIndex(fourdst::atomic::H_1);
int He4Index = context.engine.getSpeciesIndex(fourdst::atomic::He_4);
std::cout << context.t << "," << context.state(H1Index) << "," << context.state(He4Index) << "\n";
}
int main(){
// 1. Define initial composition
fourdst::composition::Composition comp;
std::vector<std::string> symbols = {"H-1", "He-4", "C-12"};
std::vector<double> massFractions = {0.7, 0.29, 0.01};
comp.registerSymbols(symbols);
comp.setMassFraction(symbols, massFractions);
comp.finalize(true);
// 2. Create base network engine (full reaction graph)
gridfire::GraphEngine baseEngine(comp, NetworkBuildDepth::SecondOrder)
// 3. Partition network into fast/slow subsets (reduces stiffness)
gridfire::MultiscalePartitioningEngineView msView(baseEngine);
// 4. Adaptively cull negligible flux pathways (reduces dimension & stiffness)
gridfire::AdaptiveEngineView adaptView(msView);
// 5. Construct implicit solver (handles remaining stiffness)
gridfire::DirectNetworkSolver solver(adaptView);
solver.set_callback(callback);
// 6. Prepare input conditions
NetIn input{
comp, // composition
1.5e7, // temperature [K]
1.5e2, // density [g/cm^3]
1e-12, // initial timestep [s]
3e17 // integration end time [s]
};
// 7. Execute integration
NetOut output = solver.evaluate(input);
std::cout << "Final results are: " << output << std::endl;
}
```
>**Note:** A fully detailed list of all available information in the TimestepContext struct is available in the API documentation.
2025-07-31 15:41:27 -04:00
>**Note:** The order of species in the boost state vector (`ctx.state` ) is **not guaranteed ** to be any particular order run over run. Therefore, in order to reliably extract
2025-07-31 15:04:57 -04:00
> values from it, you **must** use the `getSpeciesIndex` method of the engine to get the index of the species you are interested in (these will always be in the same order).
2025-07-31 15:41:27 -04:00
#### Callback Context
Since each solver may provide different context to the callback function, and it may be frustrating to refer to the
documentation every time, we also enforce that all solvers must implement a `descripe_callback_context` method which
returns a vector of tuples<string, string> where the first element is the name of the field and the second is its
datatype. It is on the developer to ensure that this information is accurate.
```c++
...
std::cout << solver.describe_callback_context() << std::endl;
```
2025-07-31 10:54:31 -04:00
## Python
The python bindings intentionally look **very ** similar to the C++ code.
Generally all examples can be adapted to python by replacing includes of paths
2025-07-31 15:41:27 -04:00
with imports of modules such that
2025-07-31 10:54:31 -04:00
2025-07-31 11:10:02 -04:00
`#include "gridfire/engine/GraphEngine.h"` becomes `import gridfire.engine.GraphEngine`
2025-07-31 10:54:31 -04:00
All GridFire C++ types have been bound and can be passed around as one would expect.
2025-07-31 15:41:27 -04:00
### Common Workflow Example
This example implements the same logic as the above C++ example
2025-07-31 10:54:31 -04:00
```python
2025-07-31 15:04:57 -04:00
from gridfire.engine import GraphEngine, MultiscalePartitioningEngineView, AdaptiveEngineView
from gridfire.solver import DirectNetworkSolver
from gridfire.type import NetIn
2025-07-31 10:54:31 -04:00
from fourdst.composition import Composition
2025-07-31 15:04:57 -04:00
symbols : list[str] = ["H-1", "He-3", "He-4", "C-12", "N-14", "O-16", "Ne-20", "Mg-24"]
X : list[float] = [0.708, 2.94e-5, 0.276, 0.003, 0.0011, 9.62e-3, 1.62e-3, 5.16e-4]
2025-07-31 10:54:31 -04:00
comp = Composition()
2025-07-31 15:04:57 -04:00
comp.registerSymbol(symbols)
comp.setMassFraction(symbols, X)
comp.finalize(True)
print(f"Initial H-1 mass fraction {comp.getMassFraction("H-1")}")
netIn = NetIn()
2025-07-31 10:54:31 -04:00
netIn.composition = comp
netIn.temperature = 1.5e7
netIn.density = 1.6e2
2025-07-31 15:04:57 -04:00
netIn.tMax = 1e-9
2025-07-31 10:54:31 -04:00
netIn.dt0 = 1e-12
2025-07-31 15:04:57 -04:00
baseEngine = GraphEngine(netIn.composition, 2)
baseEngine.setUseReverseReactions(False)
qseEngine = MultiscalePartitioningEngineView(baseEngine)
adaptiveEngine = AdaptiveEngineView(qseEngine)
solver = DirectNetworkSolver(adaptiveEngine)
results = solver.evaluate(netIn)
print(f"Final H-1 mass fraction {results.composition.getMassFraction("H-1")}")
2025-07-24 11:10:45 -04:00
```
2025-07-31 15:04:57 -04:00
### Python callbacks
Just like in C++, python users can register callbacks to be called at the end of each successful timestep. Note that
these may slow down code significantly as the interpreter needs to jump up into the slower python code therefore these
should likely only be used for debugging purposes.
The syntax for registration is very similar to C++
```python
from gridfire.engine import GraphEngine, MultiscalePartitioningEngineView, AdaptiveEngineView
from gridfire.solver import DirectNetworkSolver
from gridfire.type import NetIn
from fourdst.composition import Composition
from fourdst.atomic import species
symbols : list[str] = ["H-1", "He-3", "He-4", "C-12", "N-14", "O-16", "Ne-20", "Mg-24"]
X : list[float] = [0.708, 2.94e-5, 0.276, 0.003, 0.0011, 9.62e-3, 1.62e-3, 5.16e-4]
comp = Composition()
comp.registerSymbol(symbols)
comp.setMassFraction(symbols, X)
comp.finalize(True)
print(f"Initial H-1 mass fraction {comp.getMassFraction("H-1")}")
netIn = NetIn()
netIn.composition = comp
netIn.temperature = 1.5e7
netIn.density = 1.6e2
netIn.tMax = 1e-9
netIn.dt0 = 1e-12
baseEngine = GraphEngine(netIn.composition, 2)
baseEngine.setUseReverseReactions(False)
qseEngine = MultiscalePartitioningEngineView(baseEngine)
adaptiveEngine = AdaptiveEngineView(qseEngine)
solver = DirectNetworkSolver(adaptiveEngine)
def callback(context):
H1Index = context.engine.getSpeciesIndex(species["H-1"])
He4Index = context.engine.getSpeciesIndex(species["He-4"])
C12ndex = context.engine.getSpeciesIndex(species["C-12"])
Mgh24ndex = context.engine.getSpeciesIndex(species["Mg-24"])
print(f"Time: {context.t}, H-1: {context.state[H1Index]}, He-4: {context.state[He4Index]}, C-12: {context.state[C12ndex]}, Mg-24: {context.state[Mgh24ndex]}")
solver.set_callback(callback)
results = solver.evaluate(netIn)
print(f"Final H-1 mass fraction {results.composition.getMassFraction("H-1")}")
```
2025-07-24 11:10:45 -04:00
2025-07-31 10:54:31 -04:00
# Related Projects
2025-07-24 11:10:45 -04:00
GridFire integrates with and builds upon several key 4D-STAR libraries:
2025-07-31 10:54:31 -04:00
- [fourdst ](https://github.com/4D-STAR/fourdst ): hub module managing versioning
2025-07-31 15:41:27 -04:00
of `libcomposition` , `libconfig` , `liblogging` , and `libconstants`
2025-07-31 10:54:31 -04:00
- [libcomposition ](https://github.com/4D-STAR/libcomposition )
2025-07-31 15:41:27 -04:00
([docs ](https://4d-star.github.io/libcomposition/ )): Composition management
toolkit.
2025-07-31 10:54:31 -04:00
- [libconfig ](https://github.com/4D-STAR/libconfig ): Configuration file parsing
2025-07-31 15:41:27 -04:00
utilities.
2025-07-24 11:10:45 -04:00
- [liblogging ](https://github.com/4D-STAR/liblogging ): Flexible logging framework.
2025-07-30 15:12:46 -04:00
- [libconstants ](https://github.com/4D-STAR/libconstants ): Physical constants