## 2 Matrix-Based Model Formulation for MES
### District Gas Network
#### 2.3.1 Matrix-Based Universal DGN Model
\begin{array}{ll}\rho_{i}^{\text{out}}\left(f_{\text{D},i}+\sum\limits _{j\in\mathbb{G}_{\text{C}}^{\text{tot}}}\tau_{\text{C},j}+\sum\limits_{k\in \mathbb{G}_{\text{C}}^{\text{tot}}}f_{\text{C},k}^{\text{out}}+\sum\limits_{l \in\mathbb{G}_{\text{P}}^{\text{equ}}}f_{\text{P},l}^{\text{out}}\right) \cdots\\ \cdots-\rho_{\text{S},i}f_{\text{S},i}-\sum\limits_{l\in\mathbb{G}_{\text{P} }^{\text{equ}}}\left(\rho^{\ell}f_{\text{L}}^{\ell}\right)-\sum\limits_{m\in \mathbb{G}_{\text{C}}^{\text{tot}}}\left(\rho_{i,m}^{\text{in}}/\mathbb{G}_{ \text{C},m}^{\text{in}}\right)-\sum\limits_{n\in\mathbb{G}_{\text{P}}^{\text {equ}}}\left(\rho_{i,n}^{\text{in}}/\mathbb{F}_{\text{P},n}^{\text{in}}\right) =0\\ q_{i}^{\text{out}}\left(f_{\text{D},i}+\sum\limits_{j\in\mathbb{G}_{\text{P} }^{\text{tot}}}\tau_{\text{C},j}+\sum\limits_{k\in\mathbb{G}_{\text{C}}^{ \text{tot}}}f_{\text{C},k}^{\text{out}}+\sum\limits_{l\in\mathbb{G}_{\text{P} }^{\text{out}}}f_{\text{P},l}^{\text{out}}\right)\cdots\\ \cdots-q_{\text{S},i}f_{\text{S},i}-\sum\limits_{l\in\mathbb{G}_{\text{P} }^{\text{equ}}}\left(q^{\ell}f_{\text{L}}^{\ell}\right)-\sum\limits_{m\in \mathbb{G}_{\text{C}}^{\text{tot}}}\left(q_{i,m}^{\text{in}}/\mathbb{G}_{ \text{C},m}^{\text{in}}\right)-\sum\limits_{n\in\mathbb{G}_{\text{P}}^{\text {equ}}}\left(q_{i,m}^{\text{in}}/\mathbb{F}_{\text{P},n}^{\text{in}}\right) =0\end{array} \right. \tag{25}\]  
where \(\mathbb{G}_{\text{C}}^{\text{tap},i}/\mathbb{G}_{\text{C}}^{\text{in},i}/ \mathbb{G}_{\text{C}}^{\text{out},i}\) is the set of compressors extracting from/entering/leaving node \(i\); \(\mathbb{G}^{\text{type}}\) is the set of gas source types; \(\mathbb{G}_{\text{P}}^{\text{in},i}/\mathbb{G}_{\text{P}}^{\text{out},i}\) is the set of pipelines entering/leaving node \(i\); \(\mathbb{G}^{\text{node}}\) is the set of DGN nodes; \(f_{\text{C}}^{\text{in}}/f_{\text{C}}^{\text{out}}\) is the compressor flow entering/leaving node \(i\); \(f_{\text{P}}^{\text{in}}/f_{\text{P}}^{\text{out}}\) is the pipeline flow entering/leaving node \(i\); \(\rho^{\text{in}}/\rho^{\text{out}}\) is the specific gravity of gas entering/leaving node \(i\); \(q^{\text{in}}/q^{\text{out}}\) is the GCV of gas entering/leaving node \(i\); \(\rho_{\text{S}}/q_{\text{S}}\) is the specific gravity/GCV of gas injected at node \(i\); \(\rho^{\ell}/q^{\ell}\) is the gas specific gravity/GCV of type \(\ell\);\(f_{\text{S}}\) is the known pressure source flow at node \(i\); and \(f_{1}^{\ell}\) is the known injection source flow of type \(\ell\) at node \(i\). Note that Equation (25) is formulated recursively, where the gas flow direction plays a crucial role. Therefore, drawing on the strategy we employed in the DHN model formulation, the matrix representation of the gas composition tracking equations can be constructed through the incidence matrices as  
\[\left\{ \begin{array}{ll}\text{diag}(\rho)\left(f_{\text{D}}+T_{\text{C}}\tau_ {\text{C}}+\overline{B_{\text{S}}^{\text{C}}}f_{\text{C}}+-\overline{A_{ \text{gd}}}|f_{\text{P}}|\right)+B_{\text{S}}^{\text{S}}\text{diag}(\rho_{ \text{S}})f_{\text{S}}\cdots\\ \cdots-\sum\limits_{\ell\in\mathbb{G}^{\text{type}}}\left(-B_{\text{S}}^{\ell}f _{\text{P}}^{\ell}\rho^{\ell}\right)-\overline{B_{\text{S}}^{\text{C}}}\text{ diag}(f_{\text{C}})\overline{B_{\text{S}}^{\text{C}}}\rho-\overline{A_{\text{gd}}} \text{diag}(|f_{\text{P}}|)\overline{-A_{\text{gd}}}^{\text{T}}\rho=0\\ \text{diag}(q)\left(f_{\text{D}}+T_{\text{C}}\tau_{\text{C}}+\overline{B_{\text{S} }^{\text{C}}}f_{\text{C}}+-\overline{A_{\text{gd}}}|f_{\text{P}}|\right)+B_{ \text{S}}^{\text{S}}\text{diag}(q_{\text{S}})f_{\text{S}}\cdots\\ \cdots-\sum\limits_{\ell\in\mathbb{G}^{\text{type}}}\left(-B_{\text{S}}^{\ell}f _{\text{P}}^{\ell}q^{\ell}\right)-\overline{B_{\text{S}}^{\text{C}}}\text{ diag}(f_{\text{C}})\overline{B_{\text{S}}^{\text{C}}}\text{ }q-\overline{A_{\text{gd}}}\text{diag}(|f_{\text{P}}|)\overline{-A_{\text{gd}}}^{ \text{T}}\mathbf{q}=0\end{array} \right. \tag{26}\]