Extended Model Predictive Controller to Develop Energy Management Systems in Renewable Source-Based Smart Microgrids with Hydrogen as Backup. Theoretical Foundation and Case Study

This article presents a methodological foundation to design and experimentally test a Model Predictive Controller (MPC) to be applied in renewable source-based microgrids with hydrogen as backup. The Model Predictive Controller has been developed with the aim to guarantee the best energy distribution while the microgrid operation is optimized considering both technical and economic parameters. As a differentiating element, this proposal provides a solution to the problem of energy management in real systems, addressing technological challenges such as charge management in topologies with direct battery connection, or loss of performance associated with equipment degradation or the required dynamics in the operation of hydrogen systems. That is, the proposed Model Predictive Controller achieves the optimization of microgrid operation both in the short and in the long-term basis. For this purpose, a generalized multi-objective function has been defined that considers the energy demand, operating costs, system performance as well as the suffered and accumulated degradation by microgrid elements throughout their lifespan. The generality in the definition of the model and cost function, allows multi-objective optimization problems to be raised depending on the application, topology or design criteria to be considered. For this purpose, a heuristic methodology based on artificial intelligence techniques is presented for the tuning of the controller parameters. The Model Predictive Controller has been validated by simulation and experimental tests in a case study, where the performance of the microgrid under energy excess and deficit situations has been tested, considering the constrains defined by the degradation of the systems that make up the microgrid. The designed controller always made it possible to guarantee both the power balance and the optimal energy distribution between systems according to the predefined priority and accumulated degradation, while guaranteeing the maximum operating voltage of the system with a margin of error less than 1%. The simulation and experimental results for the case study showed the validity of the controller and the design methodology used.


Introduction
In general, a smart grid is a concept that comprises an efficient way to manage electricity that uses different technologies and tools to optimize the production and distribution of electricity, pursuing the balance between producers and consumers [1,2].
Specifically, in the case of renewable source-based microgrids with hydrogen as backup ( Figure  1 shows the general architecture of a hydrogen-based microgrid), the intelligent management system must be responsible not only for satisfying the load at all times, optimizing production and distribution but it must accomplish pivotal goals in the case of hydrogen systems as lifespan, degradation, costs, operating time and losses [3,4]. The energy management system (EMS) provides a wide variety of control solutions from simple heuristic strategies based on the hysteresis operation mode [5][6][7], methods focused on artificial intelligence by the use of fuzzy logic [8][9][10][11], as well as complex control algorithms aimed at optimizing multi-objective functions [12][13][14]. In the latter cited category, the use of techniques based on Model Predictive Control (MPC) theory has been gaining relevance in recent years, due to its simplicity and good performance [15][16][17].
MPC theory is a multivariable control method which is based on an optimization function, so if a suitable model plant is available, MPC theory drives the predicted plant output to the desired reference as close as possible, taking into account the constraints supported by the plant [18,19].
It is possible to find in the scientific literature configurations of microgrid with similar structure to that presented in Figure 1, where the MPC controller is based on a simplified LTI model of the plant. In these cases, the controller function is to determine the energy distribution of the microgrid, taking into account exclusively the power balance and predefined set points for the energy storage system [17,[20][21][22][23][24][25]. Then, these solutions present a very simple proposal because it obviates the influence of economic cost, operating efficiency, equipment degradation, as well as criteria associated with battery charge/discharge management.
By contrast, other works based on the economic optimization of the microgrid have been presented. For this purpose, a cost function that includes economic terms associated with the grid energy flow and the operation and maintenance cost of the systems is proposed. In these works, no actions are carried out with the objective of establishing technical optimization criteria.
Therefore, in References [18] and [26] an MPC controller is used based on simplified LTI models which seeks to maximize the economic performance of the microgrid. For this, the behavior of the microgrid is validated through simulations in a residential application for different annual generation and consumption profiles. Variants of previous MPC controllers are shown in Reference [27]. This paper presents an MPC controller based on evolutionary algorithms, which starts from a complex nonlinear model of the microgrid and proposes an objective function that minimizes its operating costs in economic terms. Unlike previous work, Reference [28] presents an objective function which The energy management system (EMS) provides a wide variety of control solutions from simple heuristic strategies based on the hysteresis operation mode [5][6][7], methods focused on artificial intelligence by the use of fuzzy logic [8][9][10][11], as well as complex control algorithms aimed at optimizing multi-objective functions [12][13][14]. In the latter cited category, the use of techniques based on Model Predictive Control (MPC) theory has been gaining relevance in recent years, due to its simplicity and good performance [15][16][17].
MPC theory is a multivariable control method which is based on an optimization function, so if a suitable model plant is available, MPC theory drives the predicted plant output to the desired reference as close as possible, taking into account the constraints supported by the plant [18,19].
It is possible to find in the scientific literature configurations of microgrid with similar structure to that presented in Figure 1, where the MPC controller is based on a simplified LTI model of the plant. In these cases, the controller function is to determine the energy distribution of the microgrid, taking into account exclusively the power balance and predefined set points for the energy storage system [17,[20][21][22][23][24][25]. Then, these solutions present a very simple proposal because it obviates the influence of economic cost, operating efficiency, equipment degradation, as well as criteria associated with battery charge/discharge management.
By contrast, other works based on the economic optimization of the microgrid have been presented. For this purpose, a cost function that includes economic terms associated with the grid energy flow and the operation and maintenance cost of the systems is proposed. In these works, no actions are carried out with the objective of establishing technical optimization criteria.
Therefore, in References [18,26] an MPC controller is used based on simplified LTI models which seeks to maximize the economic performance of the microgrid. For this, the behavior of the microgrid is validated through simulations in a residential application for different annual generation and consumption profiles. Variants of previous MPC controllers are shown in Reference [27]. This paper presents an MPC controller based on evolutionary algorithms, which starts from a complex nonlinear model of the microgrid and proposes an objective function that minimizes its operating costs in economic terms. Unlike previous work, Reference [28] presents an objective function which takes into account the microgrid operating costs based on equipment depreciation costs. The cost function includes terms associated with the amortization of the systems, without addressing specific solutions based on the technical criteria presented. This work was validated through experimentation in a residential application. Finally, in References [29][30][31][32], strategies based on MILP (Mixed Integer Linear Programming) and MIQP (Mixed Integer Quadratic Programming) theory are presented. These solutions are used to solve the optimization problem posed using a hybrid model, which makes use of discrete variables to define operating cycles of batteries and hydrogen system, without considering technical optimization criteria. The results were validated through experimentation and simulation in residential applications.
In the last instance, there are different solutions in the scientific literature in which the objective function allows optimizing not only the operating cost, but also the equipment lifetime. These solutions present a more complex multi-objective optimization problem based on system cost functions and MIQP, dependent on the current operating parameters of the system.
In the papers presented in References [12,[33][34][35][36][37], the optimization problem is based on complex particular cost functions for all systems, defining an objective function that integrates the economic costs associated with the use of the electrical grid, as well as the equipment's amortization costs. The proposed energy management problem is based on cost minimization, as well as the maintenance of certain levels of energy in the energy storage systems, posing a tracking problem. These solutions are based on determining the degradation of the equipment in terms of operating cycles and changes in the power set point for each discretization period, without considering the historical state of the system.
Based on the literature review, most of the solutions reviewed are based on simple LTI, MILP or MIQP models, which only include the model of the energy state of the system in terms of battery SOC, as well as the level of hydrogen stored. The use of these types of solutions can have a negative impact on the prediction capability of the controller, and therefore on the system's behavior [38][39][40].
Based on the application of the different proposals, most of the works focus their studies and validations on simulations [18,[25][26][27][30][31][32][33], presenting great difficulties in real applications in terms of generation, demand, equipment performance, safety criteria, etc. In this sense, attending to the safety and management of batteries, no scientific work proposes solutions considering battery charge management beyond the limitation of the battery SOC, which may be insufficient in terms of efficiency and safety in real battery operation.
Similarly, the solutions that include the degradation of the systems, are based solely on the short term [12,28,[33][34][35][36][37]41]. That is, the equipment degradation in each sample time is evaluated, without considering the system's past history. Therefore, the controller bases its operation on the assumption that the devices' performance remains constant throughout their lifetime, and therefore, the reduction of efficiency and nominal power in accordance with the accumulated degradation are not considered. Based on this assumption, the controller design will not be suitable for long-term optimization purposes.
Finally, most of the reviewed works present particular solutions according to the topology, and application. This particularity makes difficult to extrapolate the methodology proposed in each paper to any other configuration that differs from the used model in each case.
To respond to the gaps found in the literature review, this paper presents a generalized formulation for the design of MPC controllers applied to renewable energy microgrids with hydrogen as energy vector. According to the controller design, a simple and effective cause-effect tuning methodology enables the adaptation of the controller's parameters according to the objectives, topology and application of the microgrid. The proposed solution makes use of the model presented in Reference [42], which integrates and defines all the technical and economic parameters necessary in the output vector for its correct and direct application in the proposed cost function. The use of an LPV (Linear Parameter Variant) type model allows to have a linear model of the system in each sampling period, solving the need of applying non-linear/hybrid control techniques, which are characterized by a higher computational complexity and cost.
To guarantee the correct application in real systems, the proposed algorithm is hybridized with artificial intelligence techniques (Fuzzy Logic) to provide a safe solution to the battery charge management in topologies with direct battery connection (Figure 1). Similarly, the tuning methodology proposed in conjunction with the LPV model used allows the system to make the optimal energy distribution considering the associated degradation and loss of performance of the energy storage equipment. All this will have a positive impact on the lifetime of the system. Table 1 shows the novelty of this work highlighting its main contributions regarding previous scientific works.
The paper is organized as follows: Section 2 presents the fundamentals of the developed MPC controller, and the tuning guideline. Below, Section 3 solves a real case study to demonstrate the good performance of the designed MPC controller (both by simulation and experimentally). The results are discussed in Section 4 and, finally, conclusions are addressed in Section 5.

MPC Controller Structure
The design of the MPC controller, is based on the availability of a good microgrid model. In fact, this Linear Parameter Variant (LPV) discrete state-space model was developed by the authors in Reference [42] and it is reproduced again in (1). In Reference [42], the way to obtain all the state vector coordinates, as well as the control matrices parameters, is carefully explained.
The meaning of the state and output vector coordinates are as follows: battery SOC (SOC), hydrogen level (HL), battery voltage (V bat ), operating costs (C sys ), battery degradation (D bat ), H2 system degradation (D H2 ), microgrid operating losses (Loss), H2 system power (P H2 ), power exchanged with the electrical grid (P grid ), and net power of the microgrid (P net ), i.e., according to Figure 1, available renewable power minus that consumed by loads and losses.
Regarding the control vector, its first coordinate is the battery power (P bat ) and the following two the state vector coordinates P H2 and P grid . Finally, v and d vectors represent the model and output disturbances respectively.
Model (1) is not static but is continually changing based on the microgrid state. Regarding the latter (see Figure 1), the used signs criterion is that the delivered power to the DC bus is negative while the consumed one is positive. In practice, to carry out the MPC control strategy, it is necessary to define the constraints related to the physical limits of the microgrid. Therefore, to illustrate the procedure and without any loss of generality, the microgrid and MPC controller topology presented in Figures 1 and 2, respectively, will be used. This one, modelled on (1), will be subjected to the constraints gathered in (2): To match the constraints shown in (2) with those of the standard form , the problem is reduced to three inequalities: future control action constraints (∆ , ∆ , ∆ ); plant input constraints ( , , , ); and plant output constraints ( , , , ) according to the sign criteria (the criterion defined regarding Figure 1, following the state-space discrete model (1) was that the delivered power had to be negative and the consumed power positive): In the case of batteries, the minimum and maximum value will be given by the maximum charging and discharging powers of the battery bank ( , ∆ _ and , ∆ respectively), for the hydrogen system, the minimum and maximum value will be given by the nominal powers of the fuel cell and electrolyzer ( , ∆ and , ∆ , respectively) and related to the use of the electricity grid, the minimum and maximum value will be given by the maximum values for the purchase/sale of energy to the grid ( , ∆ and , ∆ , respectively). In practice, to carry out the MPC control strategy, it is necessary to define the constraints related to the physical limits of the microgrid. Therefore, to illustrate the procedure and without any loss of generality, the microgrid and MPC controller topology presented in Figures 1 and 2, respectively, will be used. This one, modelled on (1), will be subjected to the constraints gathered in (2):

MPC Cost Function. Guidelines for Parameter Tuning
To match the constraints shown in (2) with those of the standard form Pz ≤ δ, the problem is reduced to three inequalities: future control action constraints (∆P bat , ∆P H2 , ∆P grid ); plant input constraints (P bat , P H2 , P grid , P net ); and plant output constraints (SOC, HL, V bat , C sys , D bat , D H2 , LOSS).
The limit values SOCmin, SOCmax, HLmin, HLmax, V bat min and V bat max are associated with the minimum and maximum operating values defined by the safe operating margins of batteries and hydrogen-based storage systems. On the other hand, restrictions on operating powers are defined as the maximum power values (P bat , P H2 and P grid ) and variation in the power setpoint (∆P bat , ∆P H2 and ∆P grid ) according to the sign criteria (the criterion defined regarding Figure 1, following the state-space discrete model (1) was that the delivered power had to be negative and the consumed power positive): In the case of batteries, the minimum and maximum value will be given by the maximum charging and discharging powers of the battery bank (Pmax bat disc , ∆Pmax bat_disc and Pmax bat char , ∆Pmax bat char respectively), for the hydrogen system, the minimum and maximum value will be given by the nominal powers of the fuel cell and electrolyzer (Pmax fc , ∆Pmax fc and Pmax els , ∆Pmax els , respectively) and related to the use of the electricity grid, the minimum and maximum value will be given by the maximum values for the purchase/sale of energy to the grid (Pmax grid in , ∆Pmax grid in and Pmax grid out , ∆Pmax grid out , respectively).

MPC Cost Function. Guidelines for Parameter Tuning
The optimization problem to be solved at each time over the microgrid model can be included into the general expression of the cost function as (3).
where at sampling time k, α i are weighting factors corresponding to outputs deviations; and λ i to the control efforts, and SOC r (k), HL r (k) and V bat r (k) are the reference values or desired values associated with the battery state of charge, hydrogen level and bus voltage, respectively. Once the objective function to be minimized has been defined in (3), and the constraints from (2) have also been considered, it is necessary to calculate the values for the weighting factors α i and λ i , as well as the control and the prediction horizons N u and N p , respectively, to obtain a desired system response on the short-and long-term basis [40,43].
The huge knowledge that authors have of the microgrid [42] allows the establishment of the following set of cause-effect relationships that help the parameters tuning process. Although it is a heuristic process, it is very intuitive and efficient, as will be seen in the different test that will be carried out. Table 2 summarizes the cause-effect relationships of each weighting factor on the performance of the microgrid MPC controller.

Short-Term Optimization
In the short-term operation, the aim is to guarantee the power balance, maximizing renewable utilization, while it is implemented a proper battery charging protocol, considering the role of each ESS and its response time horizon. Then, the parameters to be taken into account are control and prediction horizons N u and N p , and from the objective function (3) are: the weighting factors related to the battery (α 1 , α 3 ); hydrogen level (α 2 ), general losses (α 7 ), H2 system operation (α 8 ), power balance (α 10 ) and the control law effort (λ 1 , λ 2 , λ 3 ). Additionally, the weighting factor α 9 related to P grid should be included in case of grid-connected topologies.
In the first instance, the selection of the control and prediction horizons N u and N p , respectively, are intimately linked to the system dynamics and the closed loop stability. In the case of a fast-dynamic system, it is unnecessary to use high control and prediction horizons because it must reach shortly its permanent regime, and the subsequent temporal evaluation is needless. In contrast, for a slow dynamic system, the use of a relatively high control and prediction horizons is necessary to evaluate the system performance throughout its transient response. In this case, it is advisable to use medium control and prediction horizons, due to the need to include the constraints of the control signal variation, which will impose a slower system dynamic. If these constraints are very strict, it can cause the multi-objective problem to become unfeasible. To find a reasonable solution, it is recommended to increase the control and prediction horizons so that the controller can evaluate the new transient response impose by the constraints.
On the other hand, the weighting factors of the reference tracking error (α 1 , α 2 , α 3 , α 10 ) enable the controller to prioritize its objectives according to the current state of the microgrid. The main objective of short-term optimization is always to safeguard the power balance. For this reason, the weighting factor associated with the net power tracking error (α 10 ) must have a high value (it represents a strong constraint), so any difference between P net and P net r should be strongly penalized.
Weighting factors α 1 and α 2 are related to ESS, so they must guarantee the following: α 1 is responsible for maintaining an optimum battery SOC; and α 2 preserves an optimum HL. Therefore, both weighing factors need to have high values because the MPC controller must respond quickly if there is an increasing error in the tracking reference. However, the key is the ratio between both, which defines the optimal energy distribution between batteries and H2 storage system.
In this sense, in special applications, where it is required to maintain a certain level of hydrogen, α 2 must be considered a high value. In these circumstances, electrolyzer and fuel cell will operate when the hydrogen level is lower or higher than the reference level respectively, independently of the power balance and energy requirements. On the other hand, in cases when hydrogen production is not crucial, but maintaining a desired reference hydrogen level is interesting, α 2 will have a low value, and the use of H2 system will depend mainly on the battery SOC and the current energy situation.
Finally, the weighting factor α 3 regulates the battery charge voltage control necessary to ensure a safe and efficient charging process. High values of this parameter will only make sense when the battery voltage reaches a high level and there is an excess of energy. For all other situations, this parameter must have a low value to avoid indiscriminate use of the remaining elements to impose the reference charge voltage. Hence, α 3 must be adapted according to the energy situation. A way to tuning α 3 can be solved using a fuzzy controller (see Section 3.2).
Considering the micro grid operation, the term in (3) related to operating power of H2 system, α 8 , must be low. In this sense, a reduced value of the parameter α 8 , will favour the use of the H2 storage system when the battery bank cannot cope with the energy imbalance. On the other hand, the weighting factor α 7 , linked to total Loss must be small, in this way, the losses in the microgrid must not determine its operation.
Considering the grid exchange energy flow, the weighting factor α 9 will be determined by the interaction between the microgrid and the electrical grid. Then, it is possible to define as many cases as possibilities, depending on the system topology, regulatory framework, etc. Thus, isolated topologies or when it is necessary to prioritize the use of the renewable resources instead of the electrical grid, α 9 must be represented by a high value. The purchase/sale of energy is penalized to the detriment of a more intensive use of the battery and H2 system. By contrast, if bidirectional energy exchange is allowed, the weighting factor α 9 should be reduced. Certainly, hybrid solutions can be implemented, where the energy exchange is only penalized in one direction.
Finally, the weighting factor of the control vector, λ 1 , λ 2 and λ 3 , allows the MPC controller to define the desired dynamics of the microgrid penalizing major variations of the operating point in systems that work better under low dynamic operation, i.e., electrolyser and fuel cell.
Then, a reduced value of the weighting factor related to the battery power, λ 1 , is recommended (λ 1 > 0 to guarantee the convexity of the objective function). The battery must present fast response dynamics versus transients. In contrast, the H2 system requires a more conservative operation, so, λ 2 , will have a medium-high value. The weighting factor associated with the use of the external electrical grid, λ 3 , will depend on its role and the priority of the economic term in the cost function.

Long-Term Optimization
The weighting factors of the long-term optimization (α 4 , α 5 , α 6 ) seek to maximize the microgrid's performance while prolonging its useful lifespan. The optimization should respond to the objective of cost minimization (α 4 C 2 sys ) and the minimization of the battery and H2 system degradation (α 5 D 2 bat and α 6 D 2 H2 ). Based on these objectives, the weighting factor α 4 determines the weight of the economic objective in the ESS. Therefore, this parameter should have a high value when the microgrid is designed to provide maximum economic benefits.  The operating cost decreases.

↓ •
The selection of the equipment and its operating power barely meets economic criteria.

•
The operating cost increases.  On the other hand, the weighting factors that represent the battery and H2 system degradation (α 5 , α 6 ), should not have fixed values but adapt according to the cumulative degradation in order to make a more conservative use of the most degraded equipment. For this purpose, a linear function is proposed (4), which allows calculating the updated weighting factor based on the accumulated degradation.
where: α x (k + 1): Degradation weighting factor of the element x at sampling time k + 1, with x = 5 (battery) or 6 (H2 system). D x (i): Current degradation of the element x at sampling time i, with x = 5 (battery) or 6 (H2 system). D x max : Maximum expected degradation of the element x, with x = 5 (battery), 6 (H2 system). Expression (4) can be tailored to the case of the battery (5), electrolyzer (6) and fuel cell (7), respectively.
Scalar coefficients, from a 1 to b 3 , can be estimated through heuristic procedures based on experimental tests.

Results
In order to validate the behavior of the proposed MPC controller and tuning methodology, a case study is used that consists on the renewable source-based microgrid with hydrogen as backup, made available for this research by our Research Group at the University of Huelva (UHU). In this section, the microgrid is firstly described giving its technical characteristics. Next, the parameters tuning calculation used in the MPC controller as well as the constraints considered will be justified. Finally, simulations results corroborated with experimentation will show the performance of the microgrid both on the short-and long-term basis. The software tool used to implement the state-space model developed in Reference [30] and the MPC controller designed in Section 2 is Matlab ® .

Description of the Renewable Source-Based Microgrid with Hydrogen as Backup
The renewable source-based microgrid with hydrogen as backup used in this paper is implemented at the University of Huelva and is shown in Figure 3.
space model developed in Reference [30] and the MPC controller designed in Section II is Matlab®.

Description of the Renewable Source-based Microgrid with Hydrogen as Backup
The renewable source-based microgrid with hydrogen as backup used in this paper is implemented at the University of Huelva and is shown in Figure 3. In the first instance, the renewable generation is represented by a monocrystalline photovoltaic (PV) array and the ESS is a lead-acid battery bank with direct connection to the DC bus. This connection involves the use of a proper charging control protocol to guarantee a safe voltage operating range. Additionally, the microgrid includes an H2 backup system, integrated by an alkaline electrolyzer, a Proton-Exchange Membrane (PEM) fuel cell and a medium pressure hydrogen tank for hydrogen storage. To enable bidirectional energy exchange between the electrical grid, there is a commercial inverter, and a programmable DC source. The programmable DC load can be used to simulate the load demand profile.
The technical characteristics of the different sub-systems are summarized in Table 3.  In the first instance, the renewable generation is represented by a monocrystalline photovoltaic (PV) array and the ESS is a lead-acid battery bank with direct connection to the DC bus. This connection involves the use of a proper charging control protocol to guarantee a safe voltage operating range. Additionally, the microgrid includes an H2 backup system, integrated by an alkaline electrolyzer, a Proton-Exchange Membrane (PEM) fuel cell and a medium pressure hydrogen tank for hydrogen storage. To enable bidirectional energy exchange between the electrical grid, there is a commercial inverter, and a programmable DC source. The programmable DC load can be used to simulate the load demand profile.
The technical characteristics of the different sub-systems are summarized in Table 3.

MPC Controller and Tuning Parameters
The MPC controller is based on the developed state-space model (1) and the developed theoretical MPC foundation in Section 2.
The weighting factors have been calculated following the design guidelines presented in Section 2.2. To implement the proposed tuning methodology, all system outputs have been scaled at a rate of 0-1 based on the maximum and minimum limits established by the microgrid constraints (2).
In this case, the main objective of the microgrid is always to guarantee the power balance, from a conservative and efficient use of the ESS, prioritizing the use of the microgrid's resources, over the use of the electrical grid. For this, the battery operation is defined as a short-medium term ESS, while the H2 system will act as a long-term ESS.
Finally, the use of the external grid will be limited to ensure a more conservative use of electrolyzer and fuel cell, while at the same time it is possible to obtain an economic profit with the sale of the energy surplus.
Based on the above, a high value of the net power weighting factor (α 10 ) was defined, to guarantee the power balance in each sampling period. Similarly, a fuzzy logic controller has been proposed for the calculation of the weighting factor of the battery voltage (α 3 ), so that depending on the power balance, the working voltage and the energy surplus, it allows control of the battery charging voltage in accordance with the manufacturer's specifications.
In order to define the operation of the ESS, a high value of the weighting factor of the battery SOC (α 1 ), and a reduced value of the weighting factor of the hydrogen level (α 2 ) have been used. With this, the use of H2 system is determined exclusively by the energy balance. Based on the above, the operation around the SOC reference defines the role of batteries as a short-and medium-term storage system, while the H2 system acts as a long-term storage system, operating under high and reduced SOC situations.
To define the priority in the use of the equipment, H2 system vs. external grid, a very low value of the weighting factor of H2 system (α 8 ) has been opted against a high value of the weighting factor of the grid (α 9 ), in such a way that the use of the external grid is strongly penalized against the H2 system, promoting the use of system resources.
Attending to the economic term, in this case, the economic optimization is not the main objective of the microgrid operation, and therefore a low-medium value of the operation cost weighting factor (α 4 ) has been considered. On the other hand, the degradation parameters for battery (α 5 ), electrolyser (α 6 els ) and fuel cell (α 6 f c ) have been defined based on the linear function presented in (5)- (7). In this case, considering the high cost associated with electrolyzer and fuel cell, the slopes of their associated degradation functions (a 2 and a 3 ) are considerably higher than the battery degradation (a 1 ). Then, H2 system degradation is more penalized and allows a longer lifespan. Similar function for electrolyzer and fuel cell has been chosen because they have similar operating characteristics.
Finally, to prioritize the use of the internal resources of the microgrid, the weighting factor associated with the operating losses (α 7 ) has been kept very low.
Based on the weighting factors of the control signal variation (λ 1 , λ 2 and λ 3 ), very small values have been chosen. In this case, the use of the battery as a short-term storage system has been chosen, acting against any transient in the microgrid, and that is why the weighting factor λ 1 , acquires the lowest value. To maintain a safe and efficient operation of the electrolyser and fuel cell, a relatively high weighting factor of λ 2 has been chosen, which ensures a slow dynamic operation. Finally, to guarantee the use of the battery in the first instance, as well as to preserve the slow dynamics of the H2 system, the weighting factor of the grid power variation, λ 3 , has a value ten times lower than the parameter λ 2 .
The main parameters of the MPC controller are summarized in Table 4.
V bat re f 400 V

P net P ren − P load
In order to fit the weighting factor of the battery voltage α 3 , a Mandani fuzzy controller was implemented that enables the calculation of the optimal weighting factor to stablish the battery charging voltage protocol. In this sense, due to the large number of parameters associated with the objective function and therefore the controller, a heuristic process has been chosen for the design of the fuzzy controller, which is based on multiple simulations and experimental test of the system under different energy conditions. The fuzzy rules have been designed in accordance with the battery charging control criteria. According to the above, the parameter α 3 will have a low value when the battery voltage is considerably lower than the reference value or when the battery is under discharging process. On the other hand, during the charging process, the value of the parameter α 3 is calculated on the basis of the amount of energy intended for the battery charging, as well as the relationship between the current battery voltage and the reference value. In this respect, the fuzzy rules allow the calculation of the increase or decrease of the α 3 parameter to ensure a safe operating margin as well as a smooth charging process.
The proposed fuzzy controller has the effect that in adverse conditions (high battery voltage and/or high charging power), the term associated to the battery voltage tracking problem is considered a strong constraint in the proposed multi-objective problem, guaranteeing the optimal charge control, thanks to the distribution of the energy surplus among the rest of the system equipment. Table 5 and Figure 4 show, respectively, the inference matrix as well as the membership functions and the output surface of the proposed fuzzy controller.  Finally, considering the size of the microgrid, its maximum power capability (Table 3) and the manufacturer's recommendations, the multi-objective function is subjected to the following constraints, (8

Simulation and Experimental Tests
To validate the design methodology of the MPC controller and tuning guidelines, the proposed algorithm has been evaluated both on the short-and long-term basis. Under simulation on the shortterm basis, the plant is checked for excess energy and deficit situations (Case I). In the long-term case, the response of the plant is analyzed when hydrogen-based subsystems suffer high degradation Finally, considering the size of the microgrid, its maximum power capability (Table 3) and the manufacturer's recommendations, the multi-objective function is subjected to the following constraints, (8):

Simulation and Experimental Tests
To validate the design methodology of the MPC controller and tuning guidelines, the proposed algorithm has been evaluated both on the short-and long-term basis. Under simulation on the short-term basis, the plant is checked for excess energy and deficit situations (Case I). In the long-term case, the response of the plant is analyzed when hydrogen-based subsystems suffer high degradation (Case II). Next, experimental testing is carried out considering energy excess and deficit cases (Cases III and IV). Due to the high cost of the equipment and the need for many tests across several months or even years, experimental tests are not carried out in the long-term tests.

Case I. Simulation Test. Short-term Optimization. Energy Excess and Deficit
In this case, the aim is to validate the performance of the microgrid under conditions of high excess energy and high deficit. This case represents the most unfavorable situation for the controller. The initial conditions of the plant are defined by a battery SOC of 65% and a hydrogen level of 4.5 Nm 3 . An equivalent degradation (0%) is considered in all systems. The renewable generation and load profile are shown in Figure 5a.
Sustainability 2020, 12, x FOR PEER REVIEW 18 of 28 (Case II). Next, experimental testing is carried out considering energy excess and deficit cases (Cases III and IV). Due to the high cost of the equipment and the need for many tests across several months or even years, experimental tests are not carried out in the long-term tests.

Case I. Simulation Test. Short-term Optimization. Energy Excess and Deficit
In this case, the aim is to validate the performance of the microgrid under conditions of high excess energy and high deficit. This case represents the most unfavorable situation for the controller. The initial conditions of the plant are defined by a battery SOC of 65% and a hydrogen level of 4.5 Nm 3 . An equivalent degradation (0%) is considered in all systems. The renewable generation and load profile are shown in Figure 5a.    The objective of the simulations presented in Case II is to validate the tuning methodology of the MPC controller parameters for long-term optimization. For this purpose, the controller and the microgrid are subjected to situations of high hydrogen-based subsystems degradation.
In this case, a high degradation in H2 system is considered (75% of maximum degradation), while the degradation associated to the battery is considered low (25% of maximum degradation). The initial conditions of the system are like those used in Case I.

Case III. Experimental Test. Short-term Optimization. Excess Energy Situation
In this case, the plant has been subjected to an energy excess condition under a stored energy level close to their higher limit. For this, two different tests have been carried out in which the microgrid is studied against a situation of excess energy for a variable operation (1); and another low dynamic operation with saturation in hydrogen production (2). The initial conditions of the microgrid are defined by a battery SOC of 70% and 80%, and a hydrogen level of 4.5 Nm 3 and 4.8 Nm 3 . An equivalent degradation (10%) is considered in all systems.

Case IV. Experimental Test. Short-term Optimization. Energy Deficit Situation
Unlike the previous case, in this test the system has been subjected to an energy deficit condition under a stored energy level close to their lower limit. The objective of this case study is to validate the behavior of the MPC controller in the face of a limit discharge situation. The initial conditions of the system are defined by a battery SOC of 57.5% and a hydrogen level of 3.6 Nm 3 . An equivalent degradation (10%) is considered in all systems.

Discussion
Regardless of the case study, on energy excess situation, the battery deals firstly with excess energy, due to the reduced value of the weighting factor of the control signal variation (λ 1 ), until it reaches the desired charging voltage (400 VDC). (Please see Figure 5a,b (t < 10 h) and Figure 6a,b (t < 10 h).) Thereafter, the MPC controller regulates the charging power to keep the battery voltage around the reference with an error lower than 1%, regardless of the case under study (Cases I-IV) and the system operating conditions: low dynamic (Cases I, II, III) or variable operation (Case IV). From this moment, the charging battery protocol based on a fuzzy logic controller guarantees a safe and efficient charging process according to the battery charge acceptance curve, up to the maximum SOC constraint (SOC = 90%). (Please see Figures 5a and 6a (7.5 h < t < 10 h).) Based on the proposed tuning method, the MPC controller regulates the charging process through a perfect synchronism between battery, electrolyzer and external grid; according to the weighting factors of control signal variation (λ 1 , λ 2 and λ 3 ), battery and H2 system accumulated degradation and operating priority (determined by α 5 and α 6 ). In this sense, the energy excess is used in the safest and most efficient way following design considerations, i.e., prioritizing the hydrogen production instead of selling energy to the external grid. This can be clearly seen in the battery, electrolyzer and external grid operating power profiles presented in Figure 5a (t < 10 h) and 6a (t < 10 h), as well as even faster in Figures 7a and 8a. The objective of the simulations presented in Case II is to validate the tuning methodology of the MPC controller parameters for long-term optimization. For this purpose, the controller and the microgrid are subjected to situations of high hydrogen-based subsystems degradation.
In this case, a high degradation in H2 system is considered (75% of maximum degradation), while the degradation associated to the battery is considered low (25% of maximum degradation). The initial conditions of the system are like those used in Case I.  level close to their higher limit. For this, two different tests have been carried out in which the microgrid is studied against a situation of excess energy for a variable operation (1); and another low dynamic operation with saturation in hydrogen production (2). The initial conditions of the microgrid are defined by a battery SOC of 70% and 80%, and a hydrogen level of 4.5 Nm 3 and 4.8 Nm 3 . An equivalent degradation (10%) is considered in all systems.  Unlike the previous case, in this test the system has been subjected to an energy deficit condition under a stored energy level close to their lower limit. The objective of this case study is to validate the behavior of the MPC controller in the face of a limit discharge situation. The initial conditions of the system are defined by a battery SOC of 57.5% and a hydrogen level of 3.6 Nm 3 . An equivalent degradation (10%) is considered in all systems. Attending exclusively to the control signal variation, it is verified that under dynamic operating conditions (Case III, energy excess situation (1)), the use of the electrolyzer is heavily penalized due to the specified constrains in the operating power variation. In accordance with the above, the external grid acts virtually as a battery charging regulation system, Figure 7 (t > 215 min).
In contrast, being an operation with a much lower dynamics (Case I and Case III excess energy situation), the operation of the electrolyzer is prioritised with respect to a small percentage of energy destined for sale to the external grid, Figure 5a  Finally, in the case that the hydrogen level approaches the maximum capacity of the hydrogen tank (5 Nm 3 ), a controlled switch-off of the electrolyzer is carried out. Then, the external grid is responsible for guaranteeing the power balance and the management of the battery charging protocol. (Please see Figure 5a (8 h < t < 16 h) and Figure 8a (t > 70 min).) Given the energy deficit situation (Case I, Case II and Case IV), the reduced weighting factor of the battery power variation term (λ 1 ) makes the battery absorb the transients in the net power set point instantaneously in order to cover the demand. Please see Figure 5a (t > 10 h), Figure 6a (t > 10 h) and 9a (around t = 50 min). Once the battery SOC is lower than that designated as optimal by the reference (SOC = 85%), the fuel cell and, with less intensity, the external grid, go into operation to reduce the excessive use of batteries. (Please see Figure 5a (t > 15 h), Figure 6a (t > 10 h) and Figure 9a,b (t > 0 min).) The energy distribution between battery and fuel cell is defined mainly from the term associated with the tracking error of the reference of the battery SOC, the dynamic imposed by the weighting factors associated with the control signal variation (λ 1 and λ 2 ) and the current degradation, which directly influences the weighting factors of the degradation of batteries and the H2 system (α 5 and α 6 ).

Discussion
Regardless of the case study, on energy excess situation, the battery deals firstly with excess energy, due to the reduced value of the weighting factor of the control signal variation ( ), until it reaches the desired charging voltage (400 VDC). (Please see Figure 5a,b (t < 10 h) and Figure 6a,b (t < 10 h).) Thereafter, the MPC controller regulates the charging power to keep the battery voltage Reaching the minimum battery SOC (55%), it is the fuel cell and the external grid which guarantees the power balance. (Please see Figure 5a (t > 17 h), Figure 6a (t > 18 h), and Figure 9a,b (t > 47 min).) The energy distribution between fuel cell and grid cell is defined mainly from the weighting factors associated to the use of each system (α 8 and α 9 ), the dynamic impose by the weighting factors associated with the control signal variation (λ 2 and λ 3 ) and the current degradation which directly influences the weighting factor of the H2 system (α 6 ).
Thus, in the case of reduced degradation of the fuel cell, its operation prevails over the use of the external grid, thanks to the use of a lower weighting factor (α 8 < α 9 ) to maximize the use of the energy resources available. (Please see Figure 5a (t > 10 h) and Figure 9a (5 min < t < 90 min).) Although the priority is established by the design considerations, the external grid will act against changes in the net power set point, thanks to the lower restriction in the variation of the working power (λ 3 < λ 2 ), in order to maintain a low dynamic and more conservative fuel cell operation.
Once the hydrogen level approaches the lowest limit, Figure 5a (t > 20 h), the controlled order to shut down the fuel cell, and it is ultimately the external grid which meets the demand. Unlike the previous case, the implementation of battery charge voltage controller is not required.
Finally, based on the influence of systems degradation, the increase in the use of the battery or hydrogen-based subsystems will be associated with an increase in its degradation and, therefore, a more conservative use will be required. The proposed adaptive tuning method for the weighting factor of the electrolyzer degradation (α 6 els ), battery degradation (α 5 ) and fuel cell degradation (α 6 f c ), will allow the increase of the associated term on the cost function (4), which will result in less use of the most vulnerable equipment to the detriment of greater use of batteries/hydrogen system (depending on critical system) and external grid. Please see Case II in Figure 6a (10 h < t) for electrolyzer, battery and fuel cell degradation influence analysis respectively.

Conclusions
To ensure a safe and efficient operation of renewable energy microgrids, it is necessary to implement energy management strategies, which take into account all the characteristics of the microgrid and allow the supply to be guaranteed at all times, optimizing the response of the microgrid based on technical and economic criteria. This is especially important when the microgrid includes hydrogen systems (electrolyzers and fuel cells), due to its high cost and rapid degradation due to misuse.
Based on the needs raised, MPC-based techniques stand as a powerful tool for the design of multivariable controllers that optimize the response of the closed-loop system (microgrid + controller), taking into account the restrictions associated with the physical or operating limits of the systems connected to the microgrid. All this, together with the ability to predict any change in the output, makes the use of MPC controllers provide great advantages over traditional control techniques.
Although previous works can be found where the theory of predictive control is applied to hydrogen-based microgrids, most display particularized solutions have been adapted to the topology and application in question, thus greatly hindering their applicability to other cases. These works present concrete solutions, which focus on the short-term operation of the microgrid, ignoring pivotal technical-economic criteria for the optimization and operation of actual microgrids, both in the short and the long term.
In order to respond to the lack of general solutions based on predictive control applied to renewable source-based microgrid, this paper has presented a methodological foundation to design a MPC controller that can be applied to renewable source-based microgrid with hydrogen as backup. The methodology is sufficiently general to be used independent of the microgrid topology.
The resolution of the multi-objective optimization problem was carried out using a generalized quadratic cost function. The weighting factors have been calculated taking into account the role of each system connected to the microgrid, as well as technical and economic parameters based on the performance of the actual systems, including considerations related to the battery bank charging strategy, system efficiency, operating degradation or system costs in accordance with the selected topology and application both in the short and long-term.
The intrinsic difficulty in optimizing the multi-objective functions, as well as the large number of parameters to be optimized, make the tuning process a very complex task. Although a mathematical method to calculate the weighting factors is not presented, because it depends strongly on the microgrid topology and the final application, different cause-effect relationships have been explained and guidelines have been offered to facilitate the tuning process.
The MPC controller and the tuning methodology proposed have been checked in a case study, both under simulation and experimental tests.
Considering the test results, thanks to the predictive action of the MPC controller, the goodness of the model used, as well as the proposed tuning method, the ability of the controller to establish the proposed energy management strategy has been demonstrated, based on the design criteria and the operating limits defined by the equipment restrictions.
Results obtained in the case study show the validity of the model and tuning methodology of the MPC controller, offering a powerful tool to other researchers in the tasks of designing, controlling and managing microgrids with renewable generation and the use of hydrogen as energy vector. Funding: This work is a contribution of the DPI2017-85540-R Project supported by the Spanish Ministry of Economy and Competitiveness and by the European Union Regional Development Fund.

HL r
Reference of the hydrogen level (Nm 3 ).

P bat
Battery power (W).

P H2
Hydrogen system power consumed (if electrolyser is on, P els )/supplied (if fuel cell is on, P f c ) (W).

P grid
Power consumed (P grid in )/supplied (P gridout ) from/to the electrical grid (W).

P net
Net power in the microgrid (W).

P netr
Reference net power in the microgrid (W). P PV Photovoltaic power generation (W).

SOC
Battery state of charge (%).

SOC r
Reference of the battery state of charge (%).

T s
Sampling time (s).

V bat
Battery voltage (V).

V batr
Reference battery voltage (V).