*3.2. First-Stage Constraints*

The first-stage constraints, seen in (5) and (6), represent the DER flexibility for upward and downward reactive power. Similar constraints are applied to the mathematical relaxation of the external supplier flexibility.

$$R\_{DER(\mathfrak{g})}^{Q,IIP,Min} \le R\_{DER(\mathfrak{g})}^{Q,IIP} \le R\_{DER(\mathfrak{g})}^{Q,IIP,Min}, \quad \forall \mathfrak{g} \in \{1, \dots, N\_G\} \tag{5}$$

$$R\_{\rm DER(g)}^{Q,\rm DW,\rm Min} \le R\_{\rm DER(g)}^{Q,\rm DW} \le R\_{\rm DER(g)}^{Q,\rm DW,\rm Max}, \quad \forall g \in \{1, \ldots, N\_G\} \tag{6}$$

#### *3.3. Second-Stage Constraints*

The second-stage constraints refer to the operating stage constraints that are introduced by the uncertainty of RES production. DER active power relates to its operating point for the energy schedule. This value is assumed as fixed by the conditional mean forecast for active power generation. This leads to the active power curtailment in the operating stage to be limited by:

$$P\_{\text{DER}(\text{g},\omega)}^{\text{cut}} \le P\_{\text{DER}(\text{g})}^{\text{op}} + \Delta P\_{\text{DER}(\text{g},\omega)'} \,\forall \,\mathbf{g} \in \{1, \dots, N\_{\text{G}}\}, \forall \omega \in \{1, \dots, \Omega\} \tag{7}$$

The difference of active power between the realization scenario and the expected forecast in each scenario is represented as Δ*P*. The active power flowing from the upstream connection (TSO) is limited by the contracted boundaries between the TSO and the DSO and by the capacity of the transformers at the substation interconnection. Active power can be injected/absorbed by the TSO as seen in (8).

$$-P\_{TSO(\omega)}^{\text{Max}} \le P\_{TSO(\omega)} \le P\_{TSO(\omega)'}^{\text{Max}} \quad \forall \omega \in \{1, \ldots, \Omega\} \tag{8}$$

In addition, the second-stage also includes the bounds of the second-stage variables and the non-anticaptivity constraints, given by:

$$\tau\_{\text{DER}(g,\omega)}^{Q,\text{LIP}} \le R\_{\text{DER}(g)'}^{Q,\text{LIP}} \quad \forall g \in \{1, \dots, N\_G\}, \forall \omega \in \{1, \dots, \Omega\} \tag{9}$$

$$r\_{\text{DER}(\text{g},\omega)}^{\text{Q.DIN}} \le R\_{\text{DER}(\text{g})'}^{\text{Q.DIN}} \quad \forall \text{g} \in \{1, \dots, N\_{\text{G}}\}, \forall \omega \in \{1, \dots, \Omega\} \tag{10}$$

Constraints (9) and (10) are also applied to the mathematical relaxation represented through external suppliers.

Each DER has the possibility to provide inductive or capacitive reactive power under the operation limits defined in the Portuguese regulation.

$$\begin{cases} -\left(P\_{\text{DER}(\underline{\mathbf{y}})}^{\text{pp}} + \Delta P\_{\text{DER}(\underline{\mathbf{y}},\omega)} - P\_{\text{DER}(\underline{\mathbf{y}},\omega)}^{\text{ext}}\right)\tan\phi \le Q\_{\text{DER}(\underline{\mathbf{y}})}^{\text{pp}} + r\_{\text{DER}(\underline{\mathbf{y}},\omega)}^{\text{Q.LIP}} - r\_{\text{DER}(\underline{\mathbf{y}},\omega)}^{\text{Q.LIP}} \le \left(P\_{\text{DER}(\underline{\mathbf{y}})}^{\text{pp}} + \Delta P\_{\text{DER}(\underline{\mathbf{y}},\omega)} - P\_{\text{DER}(\underline{\mathbf{y}},\omega)}^{\text{ext}}\right)\tan\phi,\tag{11} \\ \forall \mathbf{g} \in \{1, \ldots, N\}, \forall \omega \in \{1, \ldots, \Omega\} \end{cases}$$

In (12) and (13), it is represented the upward/downward activation of the mathematical relaxation for the TSO. This relaxation considers a high penalty because the main goal is to provide the service for the TSO request.

$$\text{R.rlx}\_{TSO(\omega)}^{Q,III} \le RLX\_{TSO}^{Q,IIP}, \quad \forall \omega \in \{1, \dots, \Omega\} \tag{12}$$

$$\text{rl.} \mathbf{x}\_{TSO(\omega)}^{Q, DW} \le RL \mathbf{X}\_{TSO}^{Q, DW}, \quad \forall \omega \in \{1, \dots, \Omega\} \tag{13}$$

As a last resource to find a solution for congestion and voltage problems, demand response is used by the DSO, being constrained by:

$$P\_{L(l\omega)}^{DR} \le P\_{L(l)'} \quad \forall l \in \{1, \dots, N\_L\}, \forall \omega \in \{1, \dots, \Omega\} \tag{14}$$

Then, the actual reactive power consumption of consumer *l* is given by:

$$Q\_{L(l\omega)} = \left(P\_{L(l)} - P\_{L(l\omega)}^{DR}\right) \tan \phi\_{\prime} \quad \forall l \in \{1, \ldots, N\_L\}, \forall \omega \in \{1, \ldots, \Omega\} \tag{15}$$

where *tan* φ can be settled at 0.3 as assumed in [17].

Regarding the capacitor banks and transformers with OLTC, these devices are owned by the DSO and located in the substation. This means that the DSO has the knowledge of their characteristics. Capacitor banks are used to provide reactive power being modelled by levels of reactive power as in (16) and (17).

$$Q\_{\rm CB(cb\mu\lambda\mu\lambda)} = Q\_{\rm CB(cb\lambda\mu)}^{\rm lrels} X\_{\rm CR(cb\lambda\mu\lambda\nu)}, \quad \forall cb \in \{1, \dots, N\_{\rm CB}\}, \quad \forall \omega \in \{1, \dots, \Omega\}, \forall l \upsilon \in \{1, \dots, N\_{\rm lrels}\} \tag{16}$$

$$\sum\_{l=1}^{N\_{\text{lcm}}} X\_{\text{CB}(cb\mu\nu\mu l\nu)} = 1, \quad \forall cb \in \{1, \dots, N\_{\text{CB}}\}, \forall \omega \in \{1, \dots, \Omega\} \tag{17}$$

The cost of changing the tap of the capacitor banks is multiplied by *ZCB*, which represents the difference between the tap selection in the present period with the previous one, which is constrained by:

$$X\_{\rm CB(cb\,\mu\omega\,lv)}^{t-1} - X\_{\rm CB(cb\,\mu\omega\,lv)} \le Z\_{\rm CB(cb\,\mu\omega\,lv)}\tag{18}$$

$$X\_{\rm CB(cb,\omega/\nu)} - X\_{\rm CB(cb,\omega/\nu)}^{t-1} \le Z\_{\rm CB(cb,\omega/\nu)}, \quad \forall cb \in \{1, \dots, N\_{\rm CB}\}, \forall \omega \in \{1, \dots, \Omega\}, \forall lv \in \{1, \dots, N\_{\rm Ircls}\} \tag{19}$$

The transformers with OLTC constraints for voltage control are modelled as:

$$
\Delta V\_{\text{TRF}(trf,\omega\text{ ltr})} = V\_{\text{TRF}(trf,\text{ltr})}^{\text{Intrds}} X\_{\text{TRF}(trf,\omega\text{ ltr})} \quad \forall \omega \in \{1, \dots, \Omega\}, \forall trf \in \{1, \dots, N\_{\text{TRF}}\}, \forall l \nu \in \{1, \dots, N\_{\text{ltrrds}}\} \tag{20}
$$

$$\sum\_{lv=1}^{N\_{\text{lurls}}} X\_{TRF(trf\,\mu\omega lv)} = 1, \quad \forall \omega \in \{1, \dots, \Omega\}, \forall trf \in \{1, \dots, N\_{\text{TRF}}\} \tag{21}$$

$$\mathbb{V}\_{sb(\omega)} = \mathbb{V}\_{sb(\omega)}^{ref} + \sum\_{lv=1}^{N\_{l\text{rel}}} \Delta V\_{\text{TRF}(\text{tr}f \,\omega \, lv)^{\prime}} \quad \forall \omega \in \{1, \dots, \Omega\}, \forall trf \in \{1, \dots, N\_{\text{TRF}}\} \tag{22}$$

where Δ*VTRF* represents the voltage level to be activated in the transformer by the DSO. *Vlevels TRF* is a parameter representative of all possible taps of the transformer, and *XTRF* is the binary variable for selection of a unique tap level. *Vre f sb* is the reference of voltage magnitude at the substation before the use of OLTC ability by the transformer, while the final voltage value at the substation is denoted by *Vsb*. In addition, the cost for changing the tap of the transformer is included in the objective function (5), where *ZTRF* is the linearization of the absolute function, as the capacitor banks. Thus, the constraints are:

$$X\_{\rm TRF(trf,\omega\mu lv)}^{t-1} - X\_{\rm TRF(trf,\omega\mu lv)} \le Z\_{\rm TRF(trf,\omega\mu lv)}\tag{23}$$

$$X\_{\text{TRF}\left(\text{tr}f,\mu,\text{lb}\right)} - X\_{\text{TRF}\left(\text{tr}f,\mu,\text{lb}\right)}^{t-1} \le Z\_{\text{TRF}\left(\text{tr}f,\mu,\text{lb}\right)},\\\forall trf \in \{1, \dots, N\_{\text{TRF}}\},\\\forall \omega \in \{1, \dots, \Omega\},\\\forall lv \in \{1, \dots, N\_{\text{lrc}\&i}\} \tag{24}$$

Moreover, an AC-OPF is used to model the power flow in the distribution network. Therefore, the active power balance in each bus is modelled as:

$$\begin{split} &\sum\_{g=1}^{N\_{\mathcal{L}}} \left( P\_{\text{DER}(g)}^{\text{pr},i} + \Delta P\_{\text{DER}(g,\omega)}^{\text{int}} - P\_{\text{DER}(g,\omega)}^{\text{int}} \right) + P\_{\text{TSO}}^{i} + \sum\_{l=1}^{N\_{\mathcal{L}}} \left( P\_{\text{L}(l,\omega)}^{\text{DR},i} - P\_{\text{L}(l)}^{i} \right) - G\_{\text{il}} V\_{i(\omega)}^{2} + V\_{i(\omega)} \sum\_{j \neq \text{T}\mathcal{L}^{i}} V\_{j(\omega)} \left( G\_{lj} \cos \theta\_{j(\omega)} + B\_{lj} \sin \theta\_{j(\omega)} \right), \\ &\forall i \in \{1,\dots,N\_{\text{Bulk}}\}, \forall \omega \in \{1,\dots,\Omega\}, \theta\_{j(\omega)} - \theta\_{l(\omega)} - \theta\_{j(\omega)} \end{split} \tag{25}$$

Additionally, the reactive power balance is given by:


There is also the consideration that the energy flowing through the distribution lines has a thermal limit that should not be exceeded, being limited as in (27) and (28).

$$\left| \overline{V\_{i(\omega)}} \left[ \overline{y\_{i\dagger}} \overline{V\_{i\dagger(\omega)}} + \overline{y\_{i\hbar(\rm i)}} \overline{V\_{i(\omega)}} \right] \right| \leq S\_{\overline{\rm TL}}^{\text{Max}}, \ \overline{V\_{i\dagger(\omega)}} = \overline{V\_{i(\omega)}} - \overline{V\_{j(\omega)}}, \quad \forall i, j \in \{1, \dots, N\_{\text{Bus}}\}, \forall \omega \in \{1, \dots, \Omega\}, i \neq j \tag{27}$$

$$\left| \overline{V\_{j(\omega)}} \left[ \overline{y\_{i\bar{j}}} \overline{V\_{j(\omega)}} + \overline{y\_{i k(\bar{j})}} \overline{V\_{j(\omega)}} \right] \right| \le \mathcal{S}\_{\text{TL}}^{\text{Max}}, \; \overline{V\_{j(\omega)}} = \overline{V\_{j(\omega)}} - \overline{V\_{i(\omega)}}, \; \forall i, j \in \{1, \dots, N\_{\text{Bus}}\}, \forall \omega \in \{1, \dots, \Omega\}, i \ne j \tag{28}$$

Voltage magnitude must stay between the limits established by the DSO, assuming the slack bus voltage magnitude as fixed.

$$\mathcal{V}\_{Min}^{\dot{i}} \le \mathcal{V}\_{i(\omega)} \le \mathcal{V}\_{\text{Max}'}^{\dot{i}} \quad \forall \omega \in \{1, \ldots, \Omega\} \tag{29}$$
