Chemistry Backends & C++ Bridges

Chemistry Backends & C++ Bridges

This section covers the implementation details of the C++ chemistry bridges linking the Fortran solver with external chemical kinetics, thermodynamics, and transport libraries.

1. Cantera C++ Bridge with State-Caching

Thermodynamic and transport property evaluations inside Cantera represent a primary computational bottleneck. The cantera_interface.cpp implements a State Cache Layer that stores physical states and corresponding properties, achieving >93% cache hit rates.

1.1 Cache Data Structure

The cache utilizes contiguous memory arrays to store species mass fractions , temperature , and background pressure for every grid cell:

struct ThermoStateCache {
    std::vector<double> Y;
    std::vector<double> T;
    std::vector<double> mu;
    std::vector<double> cp;
    std::vector<double> lambda;
    std::vector<double> rho;
    int ncells;
    int nspecies;
};

1.2 Cache Memory Management & Thrashing Protection

  • Reallocations are restricted to size-exceeding queries.
  • If a grid is loaded or refined, the cache checks if the current capacity is sufficient: cpp void check_and_reserve(ThermoStateCache& cache, int required_cells, int nspecies) { if (cache.Y.size() < (size_t)required_cells * nspecies) { cache.Y.resize(required_cells * nspecies); cache.T.resize(required_cells); cache.mu.resize(required_cells); } } This preserves the pre-allocated capacity across steps, eliminating memory allocation overhead.

2. PelePhysics C++ Bridge with SUNDIALS CVODE

PelePhysics integrates AMReX (portable CPU/GPU grid managers) and SUNDIALS CVODE (stiff implicit ODE integration) into a unified thermodynamics and reaction network.

2.1 Stiff ODE Constant-Pressure Reactor

Reaction rates are modeled as a constant-pressure stiff ODE network: This system is integrated step-by-step using CVODE's Backward Differentiation Formulas (BDF) with Newton iteration.

2.2 Pre-Allocated Static Memory Structures

pelephysics_interface.cpp pre-allocates matrix, linear solver, and state vector objects during startup, caching them in persistent static memory:

static SUNMatrix A = SUNDenseMatrix(nspecies + 1, nspecies + 1);
static SUNLinearSolver LS = SUNLinSol_Dense(y, A);
static void* cvode_mem = CVodeCreate(CV_BDF);
  • At each timestep, the solver binds the active cell state vector to the pre-allocated structures, marching the ODEs without runtime allocations.
  • For GPU execution, SUNDIALS binds to GPU-native vector classes (nvector_cuda), running matrix solves inside the GPU cores.