## 2 Matrix-Based Model Formulation for MES
### District Electricity Network
#### 2.2.2 Thermal Model  
The hydraulic model depicts the distribution of the transmission medium, while the thermal model focuses on the thermal energy exchange. The transferred heat power and the transmission medium temperature are linked via  
\[\left\{ \begin{array}{l}\mathbf{\Phi}_{\text{n}}=C_{\text{p}}\mathbf{m}_{\text{q}} \cdot\left(\mathbf{T}_{\text{n}}^{\text{g}}-\mathbf{T}_{\text{sp}}^{\text{g}}\right) \\ \mathbf{\Phi}_{\text{s}}=C_{\text{p}}\mathbf{m}_{\text{q}}\cdot\left(\mathbf{T}_{\text{sp }}^{\text{g}}-\mathbf{T}_{\text{s}}^{\text{g}}\right)\end{array} \right. \tag{4}\]  
where \(\mathbf{\Phi}_{\text{n}}/\mathbf{\Phi}_{\text{s}}\) is the consumed heat power vector of non-source/source nodes; \(C_{\text{p}}\) is the specific heat capacity of water; the superscript S/R denotes the supply/return network; \(\mathbf{T}_{\text{n}}^{\text{g}}/\mathbf{T}_{\text{s}}^{\text{g}}\) is the outflow temperature vector of non-source/source nodes; and \(\mathbf{T}_{\text{sp}}^{\text{g}}/\mathbf{T}_{\text{sp}}^{\text{g}}\) is the specified inflow temperature vector of supply/return nodes. Due to the existence of heat loss, the transmission medium temperature decreases exponentially along the mass flow direction within the pipeline:  
\[\left\{ \begin{array}{l}T_{\text{out},k}^{\text{P}}=\left(T_{\text{in},k}^{ \text{P}}-T_{\text{a},k}\right)e^{-\lambda_{\text{h}}kL_{\text{h},k}\left(C_{ \text{p}}|\mathbf{m}_{k}\right|)^{-1}}+T_{\text{a},k}\\ {T_{\text{out},k}^{\text{P}}}^{\text{T}}=T_{\text{in},k}^{\text{P}}e^{- \lambda_{\text{h}}kL_{\text{h},k}\left(C_{\text{p}}|\mathbf{m}_{k}\right|)^{-1}} \end{array} \right.\qquad\qquad\forall k\in\mathbb{H}^{\text{pipe}} \tag{5}\]  
where \(T_{\text{out},k}^{\text{P}}/T_{\text{in},k}^{\text{P}}\) is the outlet/inlet temperature of pipeline \(k\); \(T_{\text{a},k}\), \(\lambda_{\text{h},k}\), \(L_{\text{h},k}\), and \(m_{k}\) are the ambient temperature, heat transfer coefficient, length, and mass flow of pipeline \(k\), respectively; and \(\mathbb{H}^{\text{pipe}}\) is a set of heat pipelines. In Equation (5), we use \(T_{\text{out},k}^{\text{P}}\) and \(T_{\text{in},k}^{\text{P}}\) to substitute \(T_{\text{out},k}^{\text{P}}-T_{\text{a},k}\) and \(T_{\text{in},k}^{\text{P}}-T_{\text{a},k}\) for brevity. For the pipeline \(k\) where the mass flow leaves at node \(i\) and enters at node \(j\), its outlet temperature corresponds to the inflow temperature of node \(i\), and its inlet temperature is equal to the outflow temperature of node \(j\). Thus, Equation (5) can be reformulated as  
\[T_{\text{in},j,k}^{\text{N}}=T_{\text{out},j}^{\text{N}}e^{- \lambda_{\text{h}}kL_{\text{h},k}\left(C_{\text{p}}|\mathbf{m}_{k}\right|)^{-1}} \qquad\forall k\in\mathbb{H}^{\text{pipe}} \tag{6}\]  
Figure 2: Typical schematic diagram of the DHN. where \(T_{\text{in},i,k}^{\text{N},\text{ }\prime}/T_{\text{out},i}^{\text{N},\text{ }\prime}\) is the inflow/outflow temperature of node \(i/j\). In this way, the temperature profile of the DHN can be characterized solely by the node temperature. By introducing the directional node-branch incidence matrix \(A_{\text{hd}}\), as defined in Equation (7), the relationship between each node temperature can be represented in a compact matrix form, as shown in Equation (8). \[\left\{ \begin{array}{l}A_{\text{hd}}=A_{\text{h}}S_{\text{h}}\\ S_{\text{h}}=\text{diag}\big{(}m\text{ }\cdot|m\text{ }|^{-1}\big{)}\end{array} \right. \tag{7}\]  
\[\left\{ \begin{array}{l}T_{\text{in}}^{\text{N}\prime}=R_{1}(\overline{\pm A _{\text{hd}}})^{\text{T}}T_{\text{out}}^{\text{N}\text{ }\prime}\\ R_{1}=\text{diag}\big{(}e^{-\lambda_{\text{h}}\cdot L_{\text{h}}\cdot(C_{\text{ p}}|m\text{ }|)^{-1}}\big{)}\end{array} \right. \tag{8}\]  
where \(S_{\text{h}}\) is the directional factor vector of heat pipelines; \(-A_{\text{hd}}/+A_{\text{hd}}\) is applied for the supply/return network; diag(\(\cdot\)) denotes the diagonal matrix transformation; and \"\(\cdot\)\" represents a newly defined matrix operator that sets all negative elements of the matrix to zero.