*3.1. Simulation Algorithm*

Cogeneration facilities are designed to supply the user's thermal energy demand and to provide electricity as a secondary energy source [15]. Industrial-scale CHPs use the heat directly, but residential-scale CHPs require a thermal energy storage system (TES) to manage the energy demand avoiding system oversizing [12,34]. Because of this, two simplifications have been assumed for the simulation analysis. The first simplification is that thermal energy demand is supplied daily from a thermal energy buffer (Figure 4), that can be charged with the CHP unit and discharged by the user without any time dependence. The second simplification is that the electrical energy is also managed using an electrical energy storage system (EES) with the suitable capacity to manage the daily consumption. EES ensures the generation–consumption correlation [24]. Electrical energy surplus is daily exported to the electrical grid.

**Figure 4.** Cogeneration functioning block diagram for the simulation algorithm.

Figure 4 shows the functioning block diagram of the CHP system for the simulation algorithm programmed in a Python [35] script that is graphically described in Figure 5. The code is used to evaluate a day-by-day energy balance from the user dataset for each individual dwelling and the 10-dwelling buildings.

**Figure 5.** Python script code algorithm for day-to-day energy analysis decision diagram.

The calculation process starts with the evaluation of the total time that the CHP requires to produce the daily thermal demand, which will depend on the rated thermal power of the CHP technology used. This time can be calculated with the equation:

$$\mathbf{t} = \frac{\mathbf{Q}\_{\rm D}}{\dot{\mathbf{Q}}\_{\rm CHF} \cdot \mathbf{\eta}\_{\rm Q}},\tag{1}$$

where QD is the daily thermal energy demand, . QCHP is the rated CHP thermal power and η<sup>Q</sup> the thermal efficiency of the energy conversions required to feed the demand. Energy conversions are due to the energy extraction process of the thermal energy from the buffer used to match the energy production and demand (Figure 4). The same efficiency (85%) is considered for all the CHP technologies. The result of Equation (1) is the time that the CHP requires to produce the daily thermal energy, where two different situations can occur. If the total time calculated is lower than 24 h, the CHP technology is capable to provide the entire daily thermal demand and no back-up energy system will be required. In case of a CHP technology that is not suitably sized the total estimated time can be greater than 24 h, and the maximum achievable energy will be the obtained from the CHP unit working at the rated power the entire day. In this case, it is considered that the shortage of thermal energy will be compensated by a back-up system, e.g., a boiler, using natural gas as fuel.

The fuel consumption to produce the thermal energy with the CHP can be determined as:

$$\mathbf{Q\_{fuel}} = \frac{\mathbf{Q\_{CHP}}}{\mathbf{\eta\_{CHP}}},\tag{2}$$

where QCHP stands for the thermal energy produced by the CHP unit, and <sup>η</sup><sup>Q</sup> CHP is the energy performance for the thermal energy flow in the CHP system. The back-up energy (Qbackup) equals the lack of thermal energy because the value comes from the boiler consumption as can be read in Figure 5.

The electrical energy produced with the CHP unit depends on the CHP gas consumption and the electrical efficiency of the unit, η<sup>W</sup> CHP, as:

$$\mathsf{W\_{CHP}} = \mathsf{Q\_{fuel}} \cdot \mathsf{η\_{CHP}} \cdot \mathsf{η\_{W\_{\prime}}} \tag{3}$$

where η<sup>W</sup> is additional electrical efficiency due to the energy conversions. The electrical energy produced will be used to provide the daily electrical energy demand (WD). The electrical energy balance is determined as:

$$\mathcal{W}\_{\text{bal}} = \mathcal{W}\_{\text{CHP}} - \mathcal{W}\_{\text{D}}.\tag{4}$$

Attending to the sign value reported from this equation, electrical energy will be imported from the grid when it is negative and exported or sold to the grid if it is positive.
