*Article* **FPGA-Based Implementation of an Optimization Algorithm to Maximize the Productivity of a Microbial Electrolysis Cell**

**José de Jesús Colín-Robles 1, Ixbalank Torres-Zúñiga 1,\*, Mario A. Ibarra-Manzano <sup>1</sup> and Víctor Alcaraz-González <sup>2</sup>**


**Abstract:** In this work, the design of the hardware architecture to implement an algorithm for optimizing the Hydrogen Productivity Rate (HPR) in a Microbial Electrolysis Cell (MEC) is presented. The HPR in the MEC is maximized by the golden section search algorithm in conjunction with a super-twisting controller. The development of the digital architecture in the implementation step of the optimization algorithm was developed in the Very High Description Language (VHDL) and synthesized in a Field Programmable Gate Array (FPGA). Numerical simulations demonstrated the feasibility of the proposed optimization strategy embedded in an FPGA Cyclone II. Results showed that only 21% of the total logic elements, 5.19% of dedicated logic registers, and 64% of the total eight-bits multipliers of the FPGA were used. On the other hand, the estimated power consumption required by the FPGA-embedded optimization algorithm was only 146 mW.

**Keywords:** MEC; hydrogen production; online optimization; golden section search; super-twisting controller; FPGA

#### **1. Introduction**

Nowadays, biotechnological systems represent a very attractive option for hydrogen production. The degradation of organic matter through the use of bacteria has gained great interest in the scientific community because hydrogen can be produced in a clean way [1,2]. In contrast to current industrial methods, in which unfortunately 90% of the hydrogen produced requires the use of fossil fuels generating a large amount of *CO*<sup>2</sup> (10 tonnes of *CO*<sup>2</sup> per ton of *H*2) [3], Microbial Electrolysis Cells (MEC) represent a great alternative to produce hydrogen because they require less energy compared to the classic techniques to produce hydrogen, such as the electrolysis of water [4,5].

A MEC is an electrochemical device which uses electroactive microorganisms as catalysts to convert the organic matter to hydrogen and provides a novel approach for producing economically viable hydrogen from a wide range of renewable biomass sources [6,7]. Furthermore, a waste biorefinery based on MECs to produce clean and renewable electrofuel and valuable chemical compounds holds the flexible potentials for pollutants removal and *CO*<sup>2</sup> capture [8]. Broadly speaking, unlike a Microbial Fuel Cell, a MEC requires the induction of a constant voltage supply generating a potential difference between the electrodes to produce a flow of hydrogen as a result of the degradation of the organic matter that is fed to the MEC.

Other widely biological approaches used for the production of hydrogen in a clean way include Dark Fermentation (DF) in which bioreactors are fed by wastewater with a high concentration of organic matter from domestic and industrial origin. However, its efficiency to produce hydrogen compared to a MEC is relatively low (40% or less) [9]. Generally a MEC is fed with a controlled flow of wastewater which is rich in Volatile Fatty

**Citation:** Colín-Robles, J.d.J.; Torres-Zúñiga, I.; Ibarra-Manzano, M.A.; Alcaraz-González, V. FPGA-Based Implementation of an Optimization Algorithm to Maximize the Productivity of a Microbial Electrolysis Cell. *Processes* **2021**, *9*, 1111. https://doi.org/10.3390/ pr9071111

Academic Editors: Philippe Bogaerts and Alain Vande Wouwer

Received: 19 May 2021 Accepted: 15 June 2021 Published: 25 June 2021

**Publisher's Note:** MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations.

**Copyright:** © 2021 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https:// creativecommons.org/licenses/by/ 4.0/).

Acids (VFAs) that in turn might come from another Wastewater Treatment Plant (WWTP) like a DF bioreactor.

The production of hydrogen at the industrial scale through biotechnological systems is a challenge that has been dealt with from different approaches. For instance, in [10] an optimization scheme to maximize the hydrogen productivity of a DF is presented. In such study the optimization is achieved by a heuristic strategy with a nonlinear observer consisting in a Luenberger observer coupled to a super-twisting observer. Then, a supertwisting controller is used to lead the DF process to its maximum hydrogen productivity rate. In [11] the optimization is focused on the effect of the operating conditions such as pH, temperature, nutrient availability and substrate concentration. This involves mathematical modeling of a fermentation process in such a way that biohydrogen production can be predicted. On the other hand in [12] the hydrogen productivity was reported to increase from 0.13 to 0.82 m3 [*H*2] per m3 per day improving the conductivity of the electrode in a MEC and increasing the population of bacteria in the cathode biofilm. Another work related to hydrogen optimization is presented in [13] where the authors demonstrated that the MEC efficiency can be improved through the reduction of the apparent resistance. The optimization strategy is integrated by both perturbation and observation algorithms designed to track the minimal apparent resistance and adjusting the applied voltage used as control input. Other works in literature are focused in MEC construction details, for example, in [14] an effective strategy to improve the productivity performance through an improved anode arrangement is presented. In such work, the anode is strategically located in such a way that the solution resistance, the biofilm and the whole physical system are reduced. The polarization of the MEC was considerably reduced, affecting directly 72–118% the rate of hydrogen production.

The possibility of being able to implement control algorithms using digital systems such as microcontrollers, Graphic Processing Units (GPUs) and Field Programmable Gate Arrays (FPGAs) has been of great interest due to its great processing capacity, resources optimization and low energy consumption. Besides, the parallelism in the execution of the algorithms has given to the FPGAs a great advantage over other digital systems based on microcontrollers and microprocessors. For example, in [15,16], an FPGA-based fuzzy-logic controller is implemented and analyzed, and it is concluded that this technology is a good choice for implementation. The parallelism offered by FPGAs is used in [17,18] to implement complex control algorithms for a AC-DC converter and a DC-DC converter, respectively. In these works the FPGA processing efficiency is highlighted. In [19] both, the optimization of 80% of the hardware and reduction of 40% of the power consumption of a distributed-arithmetic (DA)-based proportional-integral-derivative (PID) controller compared to a multiplier-based scheme is demonstrated for temperature control. The efficiency of the complete digital control system is demonstrated using a Xilinx Spartan-2E FPGA. More recently, in [20] the authors proposed a combination of a direct torque control, space vector modulation, input-output feedback linearisation, a second-order super-twisting speed controller, and sliding-mode-load torque and stator-flux observers with stator resistance estimation implemented in an FPGA. This control strategy demonstrated robustness in presence of stator resistance variations and uncertainties when it was applied to an induction motor drive. An interesting pipeline implementation of a super-twisting controller to control ground vehicles is presented in [21]. The super-twisting controller was used to control the lateral and yaw velocities in the vehicle dynamics that are described by a discrete time model. The resulting implementation required shorter sampling times and can be synthesized in a low-cost FPGA. A classical Proportional-Integral-Derivative (PID) controller implemented in FPGA is proposed in [22]. With the objective to accelerate the execution of the algorithm, to obtain great precision and to get highly commercial ability, the implementation was based on smooth motion interpolation. The results from numerical simulations and practical tests, demonstrated its correct performance. Nevertheless, to the best of the authors knowledge, there is not FPGA-based control implementations applied to bioprocesses.

In the present work the optimization problem of maximizing the Hydrogen Production Rate (HPR) in a MEC is addressed. The productivity function is approximated from the MEC model in steady state, for which, a point of maximum performance in a well-defined operating region is ensured. Using the golden section search optimization algorithm coupled to a robust super-twisting controller, the MEC is online brought to its maximum hydrogen production performance. The proposed optimization strategy is embedded in an FPGA throughout different digital architectures that are executed in parallel without hardware sharing. The resulting digital architecture has mainly two advantages, first, the portability to be synthesized in an FPGA card from any manufacturer, and second, the low power consumption compared to a personal computer. The implementation of the optimization algorithm in an FPGA has the great advantage of being described in hardware. This allows an easy adaptation in the use of communication protocols with external devices.

The rest of the paper is organized as follows: in Section 2 the mathematical model of the MEC is described, and the objective function as the HPR is presented. A description of the optimization problem is described in detail in Section 3. In Section 4 the optimization problem is addressed by using the Golden Section Search algorithm coupled to the discrete time super-twisting controller. In addition, the maximum HPR numerically computed is verified analytically. The FPGA-based implementation of the optimization algorithm is presented in Section 5 including numerical algorithms for the implementation of arithmetic operations like division, multiplication and square root. The results are presented in Section 6, where numerical simulations are carried out in an FPGA to verify the performance, including both the truncation error and the synthesis report of the digital architecture. Finally some conclusions are pointed out in Section 7.

#### **2. Mathematical Model**

One of the most used Microbial Electrolysis Cell (MEC) configurations currently consists mainly of two chambers that are separated by a cathode membrane (see Figure 1). In the anode chamber, the anode is covered by a biofilm where the existence of anodophilic and methanogenic bacteria is considered. The degradation of VFAs in the MEC takes place in the anode chamber, where hydrogen protons and electrons are produced. Protons pass through a ionic membrane to the cathodic chamber where the production of hydrogen occurs. A relatively small voltage is supplied to the system generating a potential difference between the two electrodes, which allows the electrons released in the anode by the anodophilic bacteria to circulate and pass to the cathode to combine with the hydrogen protons. In the degradation process there is a competition between two types of microorganisms, anodophilic and methanogenic, to decide who will consume the substrate.

This behavior is modeled by the following system of Ordinary Differential Equations (ODEs) [23]:

$$\dot{s} = (s\_{in} - s)D\_{in} - k\_d \mu\_a \mathbf{x}\_a - k\_m \mu\_m \mathbf{x}\_m \tag{1}$$

$$
\dot{\mathbf{x}}\_a = \mu\_a \mathbf{x}\_a - k\_{d,a} \mathbf{x}\_a - \alpha\_a D\_{in} \mathbf{x}\_a \tag{2}
$$

$$
\dot{\mathbf{x}}\_m = \mu\_m \mathbf{x}\_m - k\_{d,m} \mathbf{x}\_m - \alpha\_m D\_{in} \mathbf{x}\_{m\prime} \tag{3}
$$

where *s* is the acetate concentration (mg/L<sup>−</sup>1), while *xa* and *xm* are the concentration of the anodophilic and acetoclastic methanogenic microorganisms, respectively (mg/L−1); *Din* is the dilution rate, *Din* = *Fin*/*Vreac* (d−1), where *Fin* is the input flow rate (Ld−1) and *Vreac* is the reactor volume (L); *α<sup>a</sup>* and *α<sup>m</sup>* are the dimensionless biofilm retention constants. *μ<sup>a</sup>* and *μ<sup>m</sup>* are the growth rates (d−1) for anodophilic and acetoclastic methanogenic microorganisms, respectively, which are defined as follows:

$$
\mu\_d = \mu\_{\text{max},a} \frac{\text{s}}{k\_{s,a} + \text{s}} \frac{1}{1 + \text{e}^{-\frac{\mathcal{E}}{\text{RT}}\eta}} \tag{4}
$$

$$
\mu\_{\rm m} = \mu\_{\rm max,m} \frac{\rm s}{k\_{s,\rm m} + s} \,\rm \,\tag{5}
$$

where *μmax*,*<sup>a</sup>* and *μmax*,*<sup>m</sup>* are the maximum grown rates (d−1), *ks*,*<sup>a</sup>* and *ks*,*<sup>m</sup>* are the half-rate Monod constants (mg (s) L−1), *F* is the Faraday constant (C mol−<sup>1</sup> *e*−1), *R* is the ideal gas constant (J mol−1K−1), *<sup>T</sup>* is the temperature (K), *<sup>η</sup>* <sup>=</sup> *Eanode* <sup>−</sup> *EKa* is the local potential, where *Eanode* is the anode potential (V) and *EKa* is the half-maximum-rate anodic Electron Aceptor (EA) potential (V) i.e., the potential that occurs when *S* = *kS*,*<sup>a</sup>* and the rate is half of the maximum rate [24].

**Figure 1.** Schematic diagram of the MEC.

#### *MEC Productivity*

The hydrogen flow rate in the MEC is modeled by Equation (6), where it can be seen that the hydrogen produced is closely related to the current generated from the flow of electrons between the electrodes.

$$Q\_{H\_2} = \Upsilon\_{H\_2} A\_a \frac{I\_{MEC}}{mF} \frac{RT}{P} \,\mathrm{},\tag{6}$$

where *YH*<sup>2</sup> is the dimensionless cathode efficiency, *Aa* is the anode area (m2), *m* is the electrons per mol specie (mol e<sup>−</sup> mol−<sup>1</sup> M−1) and *P* is the pressure inside the cathodic chamber (atm). In the Equation (6) the methanogenic microorganisms consumption is neglected and it is considered that only anodophilic microorganisms are responsible for acetate degradation. The current in the MEC is modeled as:

$$I\_{\rm MEC} = \left(\gamma\_s k\_a \mu\_a \mathbf{x}\_a L\_f (1 - f\_s^0) + \gamma\_\mathbf{x} b \mathbf{x}\_a L\_f\right) A\_{a\prime} \tag{7}$$

where *γ<sup>s</sup>* and *γ<sup>x</sup>* (mFM<sup>−</sup>1W−<sup>1</sup> <sup>s</sup> ) are the yield coefficients related to the number of coulombs that it is possible to obtain from *Ws* (g mol−1) and *Wx* (g mol−1), i.e., the substrate, and the biomass respectively; *f* <sup>0</sup> *<sup>s</sup>* is the dimensionless fraction of electrons used for cell synthesis, *b* is the endogenous decay coefficient (d−1) and *Lf* is the biofilm thickness (m).

The hydrogen production rate (HPR) *QH*2,*<sup>p</sup>* is defined as the hydrogen flow rate produced per volume of reactor (*L*[*H*2] L<sup>−</sup>1d−<sup>1</sup> ):

$$Q\_{H\_{2,p}} = \frac{Q\_{H\_2}}{V\_{\text{recac}}},\tag{8}$$

where *QH*<sup>2</sup> is the hydrogen flow rate defined by Equation (6).

#### **3. Problem Statement**

The HPR is function of both, the dilution rate *Din* and the inlet acetate concentration *sin*. *Din* is the optimization variable, while *sin* is considered as a disturbance. As it can be seen in Figure 2, the HPR presents a maximum hydrogen productivity point related to an optimal dilution rate (*QH*2,*pmax*, *Din*,*opt*) within a range of concentrations for the inlet acetate *sin* [2000, 6000] mL<sup>−</sup>1. Therefore, the optimization problem consists in calculating the value of the optimal dilution rate *Din*,*opt* that ensures the maximum performance *QH*2,*pmax* in the MEC.

Maximizing the HPR in the MEC is possible if and only if a *Din*,*opt* of the productivity function *QH*2,*<sup>p</sup>* (*Din*,*sin*) can be computed in an open neighborhood region (Γ) for each acetate concentration in the inlet *sin*. Ensuring the existence of *Din*,*opt* implies the following assumptions [25]:

**Assumption 1.** *The function QH*2,*<sup>p</sup> is twice continuously differentiable in* Γ *with respect to Din such that:*

$$\begin{cases} \frac{\partial Q\_{H\_{2,p}}(D\_{in,opt}, s\_{in})}{\partial D\_{in}} = 0\\\\ \frac{\partial^2 Q\_{H\_{2,p}}(D\_{in}, s\_{in})}{\partial D\_{in}^2} < 0 \end{cases} \tag{9}$$

**Assumption 2.** *The function QH*2,*<sup>p</sup> is convex, unimodal and any Din*,*opt is a global maximizer for each sin in the operating region.*

The optimization problem to maximize the hydrogen production rate in the MEC is proposed as:

$$\begin{array}{ll}\max\_{D\_{\text{in}}} Q\_{H\_{2,p}}(D\_{\text{in}\prime}s\_{\text{in}})\\\text{such that:}\\\dot{x}(t) &=& f(\mathbf{x}\_{\prime}D\_{\text{in}\prime}s\_{\text{in}})\\y(t) &=& Q\_{H\_{2,p}}(\mathbf{x})\_{\prime}\end{array} \tag{10}$$

where *x* = [*s*, *xa*, *xm*] *<sup>T</sup>* is the state vector, *f*(*x*, *Din*,*sin*) is defined by Equations (1)–(5) and the measured output *QH*2,*<sup>p</sup>* (*x*) is the hydrogen production rate defined by Equations (6)–(8).

As it is shown in Figure 2, only a maximum *QH*2,*pmax* can be observed for each maximizer *Din*,*opt* in the operating region.

The optimization problem is online solved by the GSS algorithm coupled to a supertwisting controller. The GSS algorithm calculates the value *QH*2,*pmax* using a hydrogen productivity function in relation to both, the dilution rate and the inlet acetate concentration of the MEC. The super-twisting controller uses *QH*2,*pmax* as a reference to track the MEC productivity to the maximum value. The optimization scheme described before is depicted in Figure 3.

**Figure 3.** Optimization scheme of the MEC.

In order to optimize the hardware resources and to reduce the power consumption, the optimization strategy to maximize the HPR of the MEC is embedded in an FPGA. This way, the energy cost required to bring the MEC to its maximum HPR can be considerably reduced.

#### **4. Optimization of the MEC Productivity**

An optimum point (*QH*2,*pmax*,*Din*,*opt*) is possible if and only if the MEC achieves an steady state [*s*∗, *x*∗ *<sup>a</sup>* , *x*<sup>∗</sup> *<sup>m</sup>*]. The operating point of the system (1)–(3) as function of *sin* and *Din* is given in steady state as:

$$s^\* = \frac{k\_{s,a}k\_{d,a} + k\_{s,a}\alpha\_a D\_{in}}{\frac{\mu\_{max,a}}{\Psi} - k\_{d,a} - D\_{in}\alpha\_a} \tag{11}$$

$$\mathbf{x}\_a^\* = \frac{(s\_{in} - s^\*)D\_{in}}{k\_a \mu\_a} \tag{12}$$

$$\mathbf{x}\_m^\* = \mathbf{0},\tag{13}$$

with

$$
\psi = 1 + e^{-\frac{F}{RT}\eta}.\tag{14}
$$

The objective function *QH*2,*<sup>p</sup>* (*Din*,*sin*), defining the input-output map in steady state, is therefore expressed as:

$$Q\_{H\_{2p}}(D\_{\rm in}, s\_{\rm in}) = \frac{L\_f A\_{\rm sur} \chi\_{H\_2} A\_a R T D\_{\rm in}}{m F P V\_{\rm reac}} (s\_{\rm in} - s^\*) \left[ \gamma\_s (1 - f\_s^0) + \frac{\gamma\_x b \psi (k\_{\rm s} + s^\*)}{k\_a \mu\_{\rm max, a} s^\*} \right]. \tag{15}$$

In this work, the acetate concentration in the inlet *sin* is assumed as known.

#### *4.1. The Golden Section Search Algorithm*

Golden ratio (*ϕ*) has been of a great interest to mathematicians, physicists, philosophers and artists. In antiquity, civilizations like Egyptians used the *ϕ* number as the main criterion for the construction of the Great Pyramids. The Parthenon in Greece was also built based on *ϕ* [26].

In relation to nature, the golden ratio is considered a natural constant that sets the standard in reproduction, growth patterns of living beings such as plants and animals. Their geometric relationship is described in Figure 4, where a line A–C is divided into two segments *l* and *r* by a point B where *l* is greater than *r* such that the ratio *l*/*r* is equal to the ratio (*l* + *r*)/*l*.

The Golden Section Search (GSS) algorithm is an iterative process suggested to optimize one-dimensional, unimodal and well behaved functions [27], taking into account that the optimum value must be into a search region defined by a lower bound (A) and an upper bound (C), as described in Figure 4.

$$\varphi = \frac{l}{r} = \frac{l+r}{l} = 1.618033988... \tag{16}$$

<sup>l</sup> <sup>r</sup> A C B

**Figure 4.** The golden ratio.

The optimization of the HPR in the MEC begins defining the search region of Equation (15). In this case, the search region is defined by *Din*,*<sup>A</sup>* = 1 d−<sup>1</sup> and *Din*,*<sup>C</sup>* = 3 d−1. Then, two inner evaluation points *Din*,1 and *Din*,2 are selected as function of *ϕ*.

$$D\_{in,1} = D\_{in,A} + d \tag{17}$$

$$D\_{\rm in,2} = D\_{\rm in,C} - d\_{\prime} \tag{18}$$

with

$$d = (\varphi - 1)(D\_{\rm in,A} - D\_{\rm in,C}).\tag{19}$$

The error used by the GSS algorithm to stop its operation is defined as:

$$err = (\wp - 1) \left| \frac{D\_{\rm in,C} - D\_{\rm in,A}}{D\_{\rm in,opt}} \right|. \tag{20}$$

The complete GSS algorithm to calculate the optimum point (*Din*,*opt*, *QH*2,*pmax*) is presented in Algorithm 1.

#### *4.2. GSS Validation*

First, let us analyze the stability of the nonlinear system (1)–(3) by calculating the eigenvalues (*λi*) of its linear approximation. The indirect Lyapunov method establish conditions that allow us to obtain conclusions about the stability of the nonlinear system in an operating point.

Consider the nonlinear system (1)–(3) with the operating point *x*∗ = [*s*∗, *x*∗ *<sup>a</sup>* , *x*<sup>∗</sup> *<sup>m</sup>*] that has the following linear approximation

$$
\dot{\overline{\overline{x}}} = A\overline{\overline{x}} + B\_{\text{ll}}\overline{\overline{u}} + B\_{\text{ll}}\overline{\overline{w}},\tag{21}
$$

where *x* = *x* − *x*∗, *A*, *Bu* and *Bw* are the Jacobian matrices of the system, *u* = *Din* − *D*<sup>∗</sup> *in* and *w* = *sin* − *s*<sup>∗</sup> *in* respectively.

The indirect Lyapunov method states that the nonlinear system (1)–(3) is asymptotically stable if and only if *Re*(*λi*) < 0 of the matrix A, ∀*λi*, *i* = 1, 2, 3, defined as:

$$A = \frac{\partial f(\mathbf{x}, D\_{in}, \mathbf{s}\_{in})}{\partial \mathbf{x}} |\_{(\mathbf{x}^\*, D\_{in}^\*, \mathbf{s}\_{in}^\*)}. \tag{22}$$

As it can be seen in Figure 5 the eigenvalues of the matrix *A* are Hurwitz in the operating region of the MEC. It must be pointed out that the closer the dilution rate is to the value 3 d−1, the more the eigenvalues *λ*<sup>1</sup> and *λ*<sup>2</sup> approach the origin.

**Figure 5.** Eigenvalues of the MEC model (1)–(3) linearized in the operating region.


The optimum value *Din*,*opt* is then obtained by differentiating the objective function (15) with respect to *Din* and equating the result to zero (first-order optimally condition), which leads to

$$\frac{\partial Q\_{H\_{2,p}}}{\partial D\_{in}} = \left(\frac{Y\_{H\_2} A\_d RT}{m F P V\_{\text{rac}}}\right) \frac{\partial I\_{MEC}^\*}{\partial D\_{in}} = 0,\tag{2.3}$$

where

$$\begin{split} \frac{\partial I\_{\rm MEC}^{\*}}{\partial D\_{\rm in}} = L\_{f} A\_{\rm sur} [D\_{\rm in} (s\_{\rm in} - s^{\*}) (\frac{\Psi \gamma\_{\rm x} b}{k\_{\rm d} \mu\_{\rm max, a}} \frac{\partial \rho}{\partial D\_{\rm in}}) + (\gamma\_{\rm s} (1 - f\_{\rm s}^{0}) + \\ \frac{\gamma\_{\rm x} b}{k\_{\rm d} \mu\_{\rm a}} (s\_{\rm in} - s^{\*} - D\_{\rm in} \frac{\partial s^{\*}}{\partial D\_{\rm in}})] \end{split} \tag{24}$$

$$\frac{\partial \rho}{\partial D\_{in}} = \frac{\frac{\partial s^\*}{\partial D\_{in}} (s^\* - (k\_{sa} + s^\*))}{s^{\*2}} \tag{25}$$

$$\frac{\partial s^\*}{\partial D\_{in}} = \frac{k\_{sa}\alpha\_a \left(\frac{\mu\_{max,a}}{\Psi} - k\_{d,a} - D\_{in}\alpha\_a\right) + \alpha\_a (k\_{s,a}k\_{d,a} + k\_{s,a}\alpha\_a D\_{in})}{\left(\frac{\mu\_{max,a}}{\Psi} - k\_{d,a} - D\_{in}\alpha\_a\right)^2} \tag{26}$$

**Figure 6.** Hydrogen productivity for different *sin*.

Figure 6 shows the *QH*2,*pmax* value calculated both, by the GSS Algorithm 1 and by substituting *Din*,*opt*, calculated by setting the Equation (23) equal to zero (see Figure 7), in Equation (15). As it can be seen, the results of the GSS algorithm match the results obtained analytically.

**Figure 7.** Derivative of *QH*2,*<sup>p</sup>* respect to *Din*.

*4.3. Super-Twisting Controller*

The MEC model (1)–(3) can be rewritten as follows:

$$
\dot{\mathfrak{x}} = \gamma(\mathfrak{x}) + \mathfrak{g}(\mathfrak{x}) D\_{\text{in}} \tag{27}
$$

$$y = Q\_{H\_{2,p}}(\mathbf{x}),\tag{28}$$

where *γ*(*x*) and *g*(*x*) are vector functions defined as:

$$\gamma(\mathbf{x}) = \begin{pmatrix} -k\_d \mu\_d \mathbf{x}\_d - k\_m \mu\_m \mathbf{x}\_m \\ (\mu\_d - k\_{d,a}) \mathbf{x}\_a \\ (\mu\_m - k\_{d,m}) \mathbf{x}\_m \end{pmatrix} \tag{29}$$

$$\mathbf{g}(\mathbf{x}) = \begin{pmatrix} s\_{in} - s \\ -\alpha\_d \mathbf{x}\_d \\ -\alpha\_m \mathbf{x}\_m \end{pmatrix}. \tag{30}$$

The relative degree *σ* of System (27) and (28) is computed by differentiating the output with respect to time as [28]:

$$\dot{y} = \frac{\partial Q\_{H\_{2,p}}(\mathbf{x})}{\partial \mathbf{x}} \dot{\mathbf{x}} = \beta \gamma(\mathbf{x}) + \beta \mathbf{g}(\mathbf{x}) D\_{\text{in}\prime} \tag{31}$$

where *β* = *<sup>∂</sup>QH*2,*<sup>p</sup> <sup>∂</sup><sup>s</sup>* , *∂QH*2,*<sup>p</sup> <sup>∂</sup>xa* , *∂QH*2,*<sup>p</sup> ∂xm* . Hence, the relative degree of the system (27) and (28) is *σ* = 1.

In this work the super-twisting controller, Equations (32) and (33), is therefore considered to track the maximum hydrogen flow rate computed by the GSS algorithm with the sliding variable defined as the tracking error [29].

$$D\_{in, \mathcal{c}} = -\rho\_1 \sqrt{|\boldsymbol{\varepsilon\_{\mathcal{E}}}|} \dot{\operatorname{sign}}(\boldsymbol{\varepsilon\_{\mathcal{E}}}) + D\_{nom} \tag{32}$$

$$\frac{dD\_{\rm nom}}{dt} = -\rho\_2 \text{sign}(\epsilon\_c) \tag{33}$$

In the super-twisting controller (32) and (33), the tracking error is defined as:

$$
\varepsilon\_{\mathfrak{c}} = Q\_{H\_{2,p}\max} - Q\_{H\_{2,p}\prime} \tag{34}
$$

*ρ*<sup>1</sup> and *ρ*<sup>2</sup> are the controller gains that ensure the finite-time stability of the tracking error, while *Din*,*<sup>c</sup>* is the control input necessary to bring the MEC to the maximum value *QH*2,*pmax*.

For implementation purposes in an FPGA, the discrete time super-twisting controller (DT-STC) is considered. The representative numerical solution showed in the Equations (35) and (36) is obtained from Equations (32) and (33) using the Euler's method. The controller uses the value *QH*2,*pmax* as a reference to carry the real productivity to its maximum value in finite time.

$$D\_{\rm in,c}[k] = -\rho\_1 \sqrt{|\epsilon\_c|} \text{sign}(\epsilon\_c) + D\_{\rm nom}[k] \tag{35}$$

$$D\_{\rm nom}[k+1] = D\_{\rm nom}[k] - \tau \rho\_2 \dot{\mathfrak{s}} \text{sign}(\mathfrak{e}\_{\mathfrak{E}}),\tag{36}$$

In Equation (36), *τ* (*d*) is the sampling time considered.

#### **5. FPGA-Embedded Optimization Algorithm**

The FPGA-based implementation of the optimization algorithm is depicted in Figures 8 and 9. Following the scheme presented in Figure 3, the implementation block diagram is integrated by the GSS algorithm digital architecture coupled to the DTSTC digital architecture. A finite state machine (FSM) and a down programmable counter are used to ensure the proper operation of the optimization algorithm embedded in the FPGA.

**Figure 8.** FPGA-based implementation of the hydrogen optimization algorithm.

**Figure 9.** FSM\_CONT\_MEC module in the FPGA-based optimization algorithm.

The digital architecture of the optimization algorithm uses a fixed point format (16,24) to represent all the input-output signals and inner operations. The hardware description used to develop the digital architecture was VHDL and the target board used was the Cyclone II EP2C35F672C6 integrated in the ALTERA DE2 educational board with a clock frequency *fCLK* = 50 MHz.

The modules GSS\_MEC and ST\_CONTROLLER were designed for an easy interaction with the FSM\_CONT\_MEC module and any other external device through the STG, EOG, STCS and EOCS signals. When the input signals STG and STCS are assigned to the logical value '1' by the FSM\_VCONT\_MEC module, they will produce a busy mode of their respective modules due to the latency time in the calculation of their final results. The busy mode is indicated by the output signals EOG ='0' and EOCS = '0'. On the other hand, when EOG = '1' and EOCS = '1', it means that the modules GSS\_MEC and ST\_CONTROLLER have finished and the results are ready to be read.

#### *5.1. Operation of the FPGA-Embedded Optimization Algorithm*

The FSM depicted in the Figure 9 is a great help for understanding the operation of the digital architecture. The FPGA execution can be divided in two steps, the initialization step, which is controlled by the states S0 to S2, and the normal operation, which is controlled by the remanding states of the FSM\_CONT\_MEC module. The initialization is executed when the FPGA is energized and the INI signal has a binary value '1' . Otherwise, the FPGA remains in standby mode until an external source changes the value of that signal. In such case, the initialization is started by a push-button (see the state S0). When INI = '1' the FSM changes to the state S1 where STG = '1' and SEL = '0' in the GSS\_MEC module and the two-one multiplexer. This will start the calculation of *QH*2,*p*,*max* with the initial value *sin*,0. In the next clock cycle, the EOMEC signal in the GSS\_MEC module will change from logic '1' to logic '0' indicating that this module is in the process of calculating *QH*2,*p*,*max*. At the same time, without any condition, a transition is made to the state S2 where the FSM is waiting by the logic value '1' in the EOMEC signal indicating that the result is ready. When *QH*2,*p*,*max* is ready to be used by the ST\_CONTROLLER module, the FSM make a transition to the state S3 where the initialization step is done, and the system now is in the normal operation where SEL = '1' and it is waiting for an external device to set the value STOMEC = '1'. During the initialization step, the down counter is loaded with an initial value decreased by one every sampling period until reaching the optimization period.

In the normal operation, the ST\_CONTROLLER module and the down counter are executed every sampling period with the aim of controlling the HPR in the MEC, and decreasing the initial value of the counter. When the down counter reaches the value zero, this means that the optimization period has expired and the GSS\_MEC module is executed to generate a new *QH*2,*p*,*max*, after that, the down counter is reloaded with the initial value.

The normal operation starts in the state S3 and the digital architecture reads *sin* by SEL = '1' in the multiplexer. When the signal STOMEC = '1', the FPGA-based optimization algorithm generates the control input *Din*,*<sup>c</sup>* of the MEC after a latency time, otherwise, the system is in standby. The execution of the ST\_CONTROLLER and the down counter are managed by the states S5 to S7 in the FSM every sampling period, while the states S8 and S9 manage the GSS\_MEC MODULE and the reinitialization of the down counter when the optimization period has been reached. In order to know when the GSS\_MEC module should be executed, the FSM reads the signal Z from the down counter in the state S4. When Z = '0' this means that the optimization period has not yet elapsed and the FSM is currently executing the ST\_CONTROLLER module, otherwise, when Z = '1' the FSM executes one more time the GSS\_MODULE and generates a new *QH*2,*p*,*max* in function of the current value *sin*. The down counter is reinitialized as well.

The most used arithmetic operations in the optimization algorithm are product, addition, division and square root. The hardware description was developed using standard VHDL and therefore the designs presented in this work do not belong to any manufacturer.

#### *5.2. GSS Implementation*

The digital architecture of the GSS optimization strategy, described in Algorithm 1, is depicted in Figure 10. The digital architecture of such algorithm is made up of registers, full adders, 8-bit embedded multipliers, multiplexers and full comparators using the previously mentioned fixed point format. Notice that the objective function shown in Equation (15) was programmed in the block *QH*2,*<sup>p</sup>* . Its implementation needed a simplified representation with the objective to calculate the hydrogen productivity with few hardware resources and small latency time. By precalculating constant parameters and making a separation by variables the following objective function is obtained:

$$Q\_{H\_{2,p}} = \beta\_1 \mathbf{x}\_a^\*(s\_{in}, D\_{in}) (\beta\_2 \mu\_a(\mathbf{s}^\*(D\_{in})) + \beta\_3),\tag{37}$$

where the values of constants *β*1, *β*<sup>2</sup> and *β*<sup>3</sup> are defined in Table 1.


**Table 1.** Constant parameters in *QH*2,*<sup>p</sup>* .

**Figure 10.** Digital architecture of the GSS algorithm.

The complete comparator that determines if *f*<sup>1</sup> > *f*2, in Algorithm 1, was designed taking into account that the operation involves real numbers and therefore the classical definition of a complete comparator of binary numbers is not sufficient for this implementation.

#### *5.3. DTSTC Implementation*

The digital architecture of the DTSTC (see Figure 11) is simpler than that one of the GSS algorithm. Although only combinational elements are required, its response speed is quite fast to generate the control action compared to the speed of change to generate the reference computed by the GSS algorithm.

**Figure 11.** Digital architecture of the DTSTC.

The controller correction term *σ*<sup>1</sup> <sup>8</sup>|<sup>|</sup> requires a digital circuit capable of computing the square root of the tracking error. Particularly in this work, the Pencil and Paper algorithm [30] proved to be very useful as a basis for the design of the SQRT arithmetic circuit.

The arithmetic circuit of the multiplier in the DTSTC architecture is based on the Coordinate Digital Computer Algorithm (CORDIC) with its rotating linear version (see Figure 12) [31], i.e.,:

$$\begin{array}{rcl} x\_{j+1} &=& x\_{j\prime} \\ y\_{j+1} &=& y\_j + \sigma\_j 2^{-j} x\_{j\prime} \\ z\_{j+1} &=& z\_j + \sigma\_j 2^{-j} \end{array} \tag{38}$$

with

$$
\sigma\_{\hat{\jmath}} = \begin{cases} -1 & \text{if } z\_{\hat{\jmath}} \ge 0 \\ +1 & \text{otherwise.} \end{cases} \tag{39}
$$

The results obtained after a sequence of fixed micro-rotations are given in the following way:

$$\begin{array}{rcl} x\_f & = & x\_{in'} \\ y\_f & = & y\_{in} + x\_{in} z\_{in} \\ z\_f & = & 0. \end{array} \tag{40}$$

The resulting operation *yf* in Equation (40) has the necessary shape to implement the DTSTC. As it can be seen in Equation (35), *Din*,*<sup>c</sup>* can be calculated from the final result *yf* by these two arithmetic operations; i.e., the product and the addition. The CORDIC-based Multiplier Digital Circuit presented in the Figure 12 has the shape necessary to implement DTSTC without the need of using embedded multipliers in the FPGA and it has a short latency time.

**Figure 12.** Digital architecture of the linear vectoring CORDIC.

#### **6. Results**

The feasibility of the FPGA-embedded optimization algorithm was demonstrated through numerical simulations. The MEC model (1)–(6) was simulated in Matlab, the ODEs were solved by the stiff solver ode15s. The parameters used in the numerical simulations are listed Table 2. In order to demonstrate the robustness of the optimization strategy proposed, modified parameters between ±30% from their nominal value were considered. The hardware required for the verification test is depicted in the Figure 13. As it can be seen, a serial communication was used to communicate the FPGA with Matlab, which was executed in a personal computer with Windows 10, Intel Core i7 and memory RAM DDR3 of 32 GB. In these conditions, six hours were needed to perform the verification test of the optimization algorithm in a Cyclone II FPGA running at *fclock* = 50 MHz and a reception-transmission data rate of 70 Mbps. The operation time of the MEC simulated in the computer was of 200 *d* with a sampling period *τ* = 0.004 *d*. The hardware resources in the target board are summarized in Table 3.

**Figure 13.** Implementation scheme for numerical simulation tests


**Table 2.** MEC Model parameter with uncertainties.


**Table 3.** Specifications of the FPGA ALTERA DE2.

The inlet acetate concentration *sin* used to feed the MEC in the numerical simulations is depicted in Figure 14. The digital architecture verification test of the MEC optimization algorithm consists mainly in comparing the results obtained from the FPGA working with the fixed point format (16,24) with the results of the same algorithm executed in Matlab in a floating point representation format.

**Figure 14.** Inlet Acetate concentration (*sin*).

The resulting HPR obtained by executing the optimization algorithm both in the FPGA and in Matlab is shown in Figure 15. The green dashed-line represents the HPR by the MEC model, the red line represents the maximum HPR computed in Matlab, while the blue dashed-line represents the maximum HPR computed by the FPGA.

**Figure 15.** *QH*2,*pmax* results in Matlab and FPGA.

On the other hand, the dilution rate computed by the optimization algorithm both in the FPGA and in Matlab is shown in Figure 16. The green dashed-line represents the optimum dilution rate computed by the GSS algorithm, the red line represents the dilution rate computed by the DTSTC in Matlab, while the blue dashed-line represents the dilution rate computed by the DTSTC in the FPGA. As it can be seen, the numerical representation format used to design the optimizer's digital architecture reduces properly the truncation error due to the finite number of bits.

Initially, the optimization algorithm requires eighteen days to reach the optimal point, as shown in Figure 15. The super-twisting controller requires this period for the control error to converge to zero using the gains specified in Table 4. In this transitory period, the GSS algorithm is initialized with 105 mL[*H*2] mL−1d−<sup>1</sup> and this value was taken as the initial reference for the DTSTC.

**Table 4.** Discrete time super-twisting controller gains.


Once the tracking error has converged to zero, the GSS algorithm reads the inlet acetate concentration value *sin*, every optimization period equivalent to *D*−<sup>1</sup> *in*,*max* = 0.33 d to update the maximum productivity value *QH*2,*pmax* used as reference by the DTSTC.

The acetate concentration in the MEC is showed in the Figure 17. It is easy to see in Figures 18 and 19 that the most of the acetate used to feed the MEC is consumed by the anodophilic bacteria *xa* because there is a inhibition process in the methanogenic bacteria growing *xm*. As expected, the current between the MEC electrodes is closely related to the HPR (see Figure 20).

**Figure 17.** Acetate concentration *s* in the MEC.

**Figure 18.** Anodophilic biomass concentration *xa* in the MEC.

**Figure 19.** Methanogenic biomass concentration *xm* in the MEC.

**Figure 20.** Current intensity in the MEC.

#### *6.1. Error Analysis*

The truncation error in the digital architecture of the optimization algorithm is mainly due to the bits fixed quantity in the representation format established in this work. If the resolution in the intermediate operations required to run the optimization algorithm on the FPGA is not sufficient, the truncation error will propagate in such a way that the results obtained are greatly affected.

Figures 21 and 22 show the behavior of the truncation error throughout the simulation process. It can be seen that the error is small enough to determine that the (16,24) format is sufficient to implement the optimization algorithm architecture in the FPGA.

**Figure 21.** Truncation error in GSS algorithm implementation.

**Figure 22.** Truncation error in DTSTC implementation.

#### *6.2. Hardware Report*

The FPGA hardware resources needed for embedding the digital architecture of the optimization algorithm on Figure 8 are summarized in Table 5. Only a 21% of the total logic elements (L.E.), 5.19% of dedicated logic registers (D.L.R.) and 64% of total eight-bits multipliers (8b-Mult.) in the chip Cyclone II were used. The input to output delay in the implementation was of 150 μs. The estimated power consumption required by the EP2C35F672C6 device using the aforementioned hardware resources is 146 mW. This estimate was calculated by the PowerPlay Early Power Estimator spreadsheet for Cyclone II family v8.0 SP1.

**Table 5.** Hardware resources used by the optimization algorithm.


The hardware resources used by the most important functional blocks in the optimization algorithm are summarized in Tables 6–8. As it can be seen in Tables 6 and 7, 64% of the total 8-b multipliers in the FPGA are used in the GSS algorithm, where 48.57% is destined to the *QH*2,*<sup>p</sup>* functional block where the objective function defined by Equation (15) is processed. It should be pointed out that the *QH*2,*<sup>p</sup>* block is part of the GSS algorithm functional block (see Figure 10). The GSS algorithm needs at least 4 cycles in the worse of the cases to reach the tolerance error (err = 0.001) defined by Equation (20). Therefore, embedded multipliers most be used in the GSS algorithm digital architecture to have a short latency time.


**Table 6.** Hardware resources used by GSS algorithm.

**Table 7.** Hardware resources used by *QH*2,*<sup>p</sup>* block.


**Table 8.** Hardware resources used by DTSTC algorithm.


On the other hand, the hardware resources used in the DTSTC and its inner functional block, the CORDIC Multiplier, are summarized in Tables 8 and 9. Most DTSTC inner operations are implemented using a CORDIC-based multiplier that has a latency time of 1.48 μs in the worse of the cases, before the tracking error converges to zero. After that, the multiplier is executed faster than 1.48 μs. It should be noted that the CORDIC-based multiplier internally uses an 8-bit expansion in the fractional part to substantially improve the truncation error generated by the fixed-point format considered (see Figure 22).


**Table 9.** Hardware resources used by CORDIC multiplier.

Finally, the arithmetic operation <sup>8</sup>|<sup>|</sup> in the DTSTC is processed by the SQRT functional block, which is based on the Pencil and Paper algorithm. Its digital architecture is primarily based on bit additions and shifts. Table 10 shows the hardware resources needed.

**Table 10.** Hardware resources used by SQRT.


#### **7. Conclusions**

In this work an FPGA-embedded optimization algorithm to maximize the hydrogen production rate (HPR) of a microbial electrolysis cell (MEC) using the golden section search (GSS) algorithm coupled to a discrete-time super-twisting controller (DTSTC) was presented. The correct performance of the GSS algorithm was analyzed analytically. Furthermore, it was proven that the relative degree of the MEC model is one, a necessary condition to use the DTSTC to bring the HPR to its maximum performance point in finite time.

To reduce the power consumption required to bring the MEC to its maximum performance, a digital architecture of the optimization algorithm was designed and embedded in an FPGA. Although the FPGA used in this work was the Cyclone II of ALTERA, the digital architectures presented in this work were designed to be implemented in any FPGA, regardless of the manufacturer.

The results of the FPGA-embedded optimization algorithm showed a correct performance with low hardware resources and low power consumption compared with a personal computer. Besides, the truncation error generated by the fixed point format used in this work was practically negligible.

Such results allow us to conclude that the implementation of control and optimization algorithms in FPGAs represents an excellent alternative to replace personal computers. Particularly, as demonstrated in the previous section, the FPGA-embedded optimization algorithm proposed to maximize the HPR in the MEC, represents a lower cost alternative in terms of consumed power and resources.

**Author Contributions:** Conceptualization, I.T.-Z.; methodology, I.T.-Z.; software, J.d.J.C.-R.; validation, I.T.-Z., M.A.I.-M. and V.A.-G.; formal analysis, J.d.J.C.-R., I.T.-Z., M.A.I.-M. and V.A.-G.; investigation, J.d.J.C.-R.; resources, I.T.-Z. and M.A.I.-M.; writing—original draft preparation, J.d.J.C.-R.; writing—review and editing, I.T.-Z., M.A.I.-M. and V.A.-G.; supervision, I.T.-Z. and V.A.-G.; project administration, I.T.-Z.; funding acquisition, I.T.-Z. All authors have read and agreed to the published version of the manuscript.

**Funding:** This research was funded by the Mexican Council of Science and Technology (CONACyT) under the PhD Grant 614412/327289.

**Institutional Review Board Statement:** Not applicable.

**Informed Consent Statement:** Not applicable.

**Acknowledgments:** This study was funded by the University of Guanajuato and University of Guadalajara. It has been partially supported by the Mexican Council of Science and Technology (CONACyT) under Grant 614412/327289 and the National System of Researches (SNI) program.

**Conflicts of Interest:** The authors declare no conflict of interest.

#### **References**

