# A Neural Network-Inspired Matrix Formulation of Chemical Kinetics for Acceleration on GPUs
## 2 Methodology
### Matrix-Based Kinetics Equations  
In the following, the quantities \(N_{C}\), \(N_{S}\), and \(N_{R}\) denote the batch size (which can be interpreted as the number of reacting cells in a domain offloaded to the GPU), number of species, and number of reactions, respectively. Unless otherwise indicated, matrices are denoted by bold symbols (e.g., **A**) and vectors by non-bold symbols (e.g., _a_). The scalar entry of matrix **A** in row \(i\) and column \(j\) is denoted \(\textbf{A}_{ij}\); similarly, the scalar \(i\)th entry of vector \(a\) is denoted \(a_{i}\). Further, the quantities \(i\), \(j\), and \(k\) index \(N_{C}\), \(N_{R}\), and \(N_{S}\), respectively (i.e., \(i=1,\ldots,N_{C}\), \(j=1,\ldots,N_{R}\), and \(k=1,\ldots,N_{S}\)). For the set of species \(\{\mathcal{S}_{1},\ldots,\mathcal{S}_{N_{S}}\}\), a general chemical mechanism is represented as  
\[\sum_{k=1}^{N_{S}}\
u^{\prime}_{kj}\mathcal{S}_{k}\equiv\sum_{k=1}^{N_{S}}\
u ^{\prime\prime}_{kj}\mathcal{S}_{k},\quad j=1,\ldots,N_{R}, \tag{1}\]  
where \(\
u^{\prime}\in\mathbb{R}^{N_{S}\times N_{R}}\) (respectively, \(\
u^{\prime\prime}\)) is the reactant (respectively, product) stoichiometric coefficient matrix and \(\mathbf{v}=\mathbf{v}^{\prime\prime}-\mathbf{v}^{\prime}\). The formulations below proceed, without loss of generality, in the context that all \(N_{R}\) reactions are reversible. In practice, as discussed in greater detail in Section 2.3, this may not be the case. The molar net production rate (kmol/m\({}^{3}\)s) for species \(k\) in cell \(i\) is  
\[\mathbf{\Omega}_{ik}=\sum_{j=1}^{N_{R}}\mathbf{v}_{kj}\mathbf{Q}_{net_{ij}}, \tag{2}\]  
where \(\mathbf{\Omega}\in\mathbb{R}^{N_{C}\times N_{S}}\) contains the source terms and \(\mathbf{Q}_{net}\in\mathbb{R}^{N_{C}\times N_{R}}\) contains the net reaction rates. Note that Equation (2) can be expressed concisely through the matrix multiplication \(\mathbf{\Omega}=\mathbf{Q}_{net}\mathbf{v}^{T}\). The complexity comes from the net reaction rate, which is expressed as  
\[\mathbf{Q}_{net_{ij}}=\mathbf{Q}_{f_{ij}}-\mathbf{Q}_{r_{ij}}=\mathbf{K}_{f_{ij}}\prod_{k=1}^{ N_{S}}\mathbf{c}_{ik}^{\dagger_{ij}}-\mathbf{K}_{r_{ij}}\prod_{k=1}^{N_{S}}\mathbf{c}_{ik}^{ \dagger_{ij}^{\prime\prime}}. \tag{3}\]  
Above, \(\mathbf{Q}_{f}\) and \(\mathbf{Q}_{r}\in\mathbb{R}^{N_{C}\times N_{R}}\) are the forward and reverse reaction rate matrices, respectively; \(\mathbf{K}_{f}\) and \(\mathbf{K}_{r}\in\mathbb{R}^{N_{C}\times N_{R}}\) are the forward and reverse rate constants, respectively; and \(\mathbf{C}\in\mathbb{R}^{N_{C}\times N_{S}}\) contains the species molar concentrations. Since \(\mathbf{Q}_{f}\) and \(\mathbf{Q}_{r}\) are non-negative, Equation (3) can be interpreted as a summation of two ANN layers by enabling matrix multiplications in the logarithm space:  
\[\mathbf{Q}_{net}=\exp\big{(}\log(\mathbf{C})\mathbf{v}^{\prime}+\log(\mathbf{K}_{f})\big{)}- \exp\big{(}\log(\mathbf{C})\mathbf{v}^{\prime\prime}+\log(\mathbf{K}_{r})\big{)}. \tag{4}\]  
It can be seen through Equation (4) that the forward and reverse contributions are ANN layers with exponential activation functions, where the input is the logarithm of the concentration matrix \(\mathbf{C}\), the weight matrices are known stoichiometric coefficients \(\mathbf{v}^{\prime}\) and \(\mathbf{v}^{\prime\prime}\), and the biases are the logarithms of rate constants \(\mathbf{K}_{f}\) and \(\mathbf{K}_{r}\) for the forward and reverse contributions, respectively. These rate constant bias terms can also be interpreted as neural network layers and are the subjects of discussion further below. Figure 1a summarizes the above formulation (Equations (2) and (4)) through an ANN architecture. Note that the leading matrix dimension of all input and output variables, which constitutes the batch size in the forward pass, is \(N_{C}\). This allows for efficient threading and fast execution in high fidelity settings, assuming optimized linear algebra libraries (such as cuBLAS [24]) are utilized by the user.