*3.1. Diesel Engine Model*

The diesel engine dynamics is obtained energy conversion principle, by defining the system's Hamiltonian *<sup>H</sup>*(*x*) as given in Equations (7)–(9)

$$H(\mathbf{x}) = \mathcal{W}\_{\mathbf{i}} - \sum\_{\ell=1}^{n} E \mathbf{c}\_{\ell} + \mathcal{W}\_{\mathbf{I}} \tag{7}$$

where *Wi* is the chemical energy also known as indicated energy, *Ec* correspond to the system loses, and *WI* is the stored energy in the inertia. The indicated energy can be defined as a nonlinear function of the fuel enthalpy *h*, the fuel flow rate *g*, and shaft rotational speed ˙ *θr*

$$\mathcal{W}\_{\hat{\imath}} = f\_{\hat{\varepsilon}}\left(\boldsymbol{h}, \,\!\!\!g, \,\!\!\dot{\theta}\_{\prime}\right) \tag{8}$$

and the energy stored in the inertia is given as in Equation (9)

$$\mathcal{W}\_{\rm I} = \frac{1}{2} L \frac{d}{dt} \theta\_{\rm r} \tag{9}$$

where *L* stands for the rotational momentum. The total converted energy into mechanical torque is given by Equations (10) and (11).

$$\frac{\partial}{\partial \theta\_r} H(\mathbf{x}) = 0 \tag{10}$$

$$J\frac{d^2}{dt^2}\theta\_r = T\_i - T\_p - T\_f \tag{11}$$

where *Ti* corresponds to engine's indicated torque, *Tp* the pumping torque, and *Tf* the friction torque.

The indicated torque is dependent on the amount of fuel injected into each of cylinders per cycle as given in Equation (12)

$$T\_i = \frac{m\_{cy}\,n\_{cy}\,\rho\_h\,\eta\_{ig}}{2\,\pi\,n\_{cs}}\tag{12}$$

where *mcy* corresponds to the fuel delivery per cycle per cylinder, *ncy* to number of cylinders, *ρh* is the heating value of fuel, *ηig* is the indicated efficiency, and *ncs* the number of crank revolutions. The indicated efficiency is given as in Equation (13)

$$
\eta\_{i\bar{g}} = \eta\_{c\bar{c}} \left( 1 - \frac{1}{r\_c^{\gamma - 1}} \right) \tag{13}
$$

where *rc* is the compression ratio, *γ* the gas specific heat capacity ratio in the cylinder, and *ηcc* represents the combustion chamber efficiency.

As presented, the indicated torque is highly dependent on several engine parameters, such as the number of cylinders, the fuel delivery per cycle, the compression ratio, and combustion chamber efficiency. On the other hand, the diesel engine emissions are defined by its residual gas fraction *χ<sup>r</sup>*, which represents a measure of CO2 concentrations of the working gas in the compression stroke, during the energy conversion process, as defined in Equation (14)

$$\chi\_{\mathcal{I}} = \frac{(\mathfrak{X}\_{\text{co}\_2})\_{\mathcal{C}}}{(\mathfrak{X}\_{\text{co}\_2})\_E} \tag{14}$$

where (*χ*˜co2 )*C* and (*χ*˜co2 )*E* stands for the CO2 fractions during compression and exhaust, respectively. The torque component corresponding to energy losses *TC*is given by Equations (15) and (16)

$$T\_{\mathbb{C}} = \frac{\partial}{\partial \theta\_r} \sum\_{\ell=1}^n Ec\_{\ell} \tag{15}$$

$$T\_{\mathbb{C}} = T\_p + T\_f \tag{16}$$

where the pumping torque *Tp* and friction torque *Tf* can be expressed as in Equations (17) and (18), respectively.

$$T\_p = \frac{V\_d}{2\pi n\_{\rm cs}} \left(p\_{\rm cm} - p\_{\rm im}\right) \tag{17}$$

$$T\_f = \frac{V\_d}{2\pi n\_{cs}} \left( c\_0 + c\_1 n\_r + c\_2 n\_r^2 \right) \tag{18}$$

here *Vd* corresponds to the engine displacement volume, *pem* is the exhaust pressure to the manifold, and *pim* the manifold inlet pressure. The friction torque *Tf* ,on the other hand, may be assumed to be a quadratic polynomial depended on the engine revolutions *nr*, with *c*0, *c*1, and *c*2 fitting constants.

From Equations (7)–(17) it becomes self-evident that the diesel engine mathematical model is highly nonlinear and dependent on several specific construction parameters. However, a linearized model may be used, considering all important nonlinear characteristics [17–20], which can be modeled as dead-times and time-delays contained in *τ*1 and *τ*2, respectively, and constant parameters *k*1, *k*2, as presented in Equations (19)–(20)

$$\frac{d}{dt}y = -\frac{1}{\tau\_1}y + \frac{k\_1}{\tau\_1}u \tag{19}$$

$$J\frac{d}{dt}\,\omega\_r = -B\,\omega\_r + k\_2\,y\left(t - \tau\_2\right) - T\_L\tag{20}$$

where *J* stands for the engine's inertia, *B* is the friction coefficient, *ωr* corresponds to the engine rotational speed, *u* to the speed controller output, *y* the position of the fuel rail, and *TL* the external load torque. Values for *k*1, *k*2, *τ*1, and *τ*2 may be found empirically or from the data provided by the manufacturer using a model fitting algorithm, as stated in [21].

Considering the previously made considerations and modeling restrictions, it is possible to build an emissions model, on the basis of the data provided by the manufacturer, using an appropriate polynomial approximation with squares regression.

Given *m* data points { *xi yi* }*mi*=<sup>1</sup> with *xi* given output power and *yi* corresponding CO2 emissions rate; the best fit polynomial for the CO2 emissions *e*(*x*) could be developed using Equation (21)

$$\mathfrak{e}(\mathbf{x}) = \sum\_{k=0}^{n} a\_k \mathbf{x}^k \quad n < m - 1 \tag{21}$$

where *αk* ∀ *k* coefficients may be found by minimizing the least square error using Equation (22)

$$A^T A \,\, \mathbf{a} = A^T \,\, \mathbf{y} \tag{22}$$

with the coefficients vector *a* = [*<sup>α</sup>*0 ... *<sup>α</sup>n*]*<sup>T</sup>*, the sample value vector *y* = [*y*0 ... *yn*]*<sup>T</sup>*, and *A* the Vandermonde matrix, given as in Equations (23) and (24)

$$A = \begin{bmatrix} 1 & \mathbf{x}\_1 & \mathbf{x}\_1^2 & \dots & \mathbf{x}\_1^n \\ 1 & \mathbf{x}\_2 & \mathbf{x}\_2^2 & \dots & \mathbf{x}\_2^n \\ \vdots & \vdots & \vdots & \vdots \\ 1 & \mathbf{x}\_m & \mathbf{x}\_m^2 & \dots & \mathbf{x}\_m^n \end{bmatrix} \quad \forall \mathbf{x}\_i \ i = 1, \ i = 1, \dots, m \tag{23}$$

$$\mathbf{a} = (A^T A)^{-1} A^T \mathbf{y} \tag{24}$$

obtaining finally an *n* degree polynomial representing the CO2 emissions profile, as given in Equation (25)

$$\text{If } y(\mathbf{x}) = a\_0 + a\_1 \mathbf{x} + a\_2 \mathbf{x}^2 + \dots + a\_{\text{ll}} \mathbf{x}^{\text{n}} \tag{25}$$

### *3.2. Electric Drive Model and Control*

The anisotropic permanent magne<sup>t</sup> synchronous machine (PMSM) mathematical model in an arbitrary synchronous reference frame *d q* is described as in Equations (26) and (27) [22],

$$
\psi\_s^{(dq)} = R\_s \, i\_s^{(dq)} + \frac{d}{dt} \psi\_s^{(dq)} + F \, \psi\_s^{(dq)} \tag{26}
$$

$$F = \begin{bmatrix} 0 & -\omega\_k \\ \omega\_k & 0 \end{bmatrix} \tag{27}$$

where *vs* corresponds to the stator voltage, *Rs* to the stator resistance, *is* the stator current, *ψs* the stator flux linkages, and *ωk* to the shaft synchronous speed. The stator flux linkages are given as in Equations (28) and (29):

$$
\Psi\_s^{(dq)} = \mathbf{G} \, i\_s^{(dq)} + \mathbf{P} \psi\_m \tag{28}
$$

$$\mathbf{G} = \begin{bmatrix} L\_d & 0 \\ 0 & L\_q \end{bmatrix} \quad \mathbf{P} = \begin{bmatrix} 1 \\ 0 \end{bmatrix} \tag{29}$$

*Ld* and *Lq* are the direct and quadrature reference frame inductances, respectively, and *ψm* the permanent magne<sup>t</sup> flux linkage. The electromechanical torque developed by the PMSM is given in Equations (30) and (31)

$$T\_{\mathfrak{c}} = \frac{\partial}{\partial \theta\_r} \, \mathcal{W}\_{fil} \left( \, \psi\_{\mathfrak{s}}^{(dq)} \, , \, i\_{\mathfrak{s}}^{(dq)} \, , \, \theta\_r \right) \tag{30}$$

$$T\_{\varepsilon} = \frac{3}{2} \operatorname{p} \left\{ \psi\_{\mathfrak{m}} \, i\_{\mathfrak{s}}^{q} + (L\_{d} - L\_{q}) \, i\_{\mathfrak{s}}^{d} \, i\_{\mathfrak{s}}^{q} \right\} \tag{31}$$

where *p* are the number of pole pairs.

Despite the classical FOC control scheme, which is used to control the drive shaft speed, in this case, the control objective is to control the torque developed by the electric drive [23], which is achieved by means of the electric torque reference, provided by the optimization algorithm output. The implementation of the electric drive control scheme is shown in Figure 4.

**Figure 4.** Torque field-oriented control scheme.

### *3.3. Grid Side Power Flow Control*

Grid side control is achieved by implementing active and reactive power control [24,25], using a virtual flux voltage oriented control (VF-VOC) strategy. Active and reactive power, *P* and *Q*, respectively, in a synchronous rotating reference frame, grid side-oriented *u v* are given in Equations (32) and (33), as a result of using a voltage orientation in *u* coordinate.

$$P = \frac{3}{2} \operatorname{Re} \left\{ \upsilon^{\mu} \left( i^{\mu} + j i^{\upsilon} \right) \right\} \tag{32}$$

$$Q = \frac{3}{2} \operatorname{Im} \left\{ v^{\mu} \left( \dot{i}^{\mu} + j \dot{i}^{v} \right) \right\} \tag{33}$$

Thus, by setting the reactive component of the grid current *iv* = 0 it is possible to maximize the active power flow into the grid. Orientation into the grid side synchronous reference frame *u v* is achieved by extracting the orientation angle *θp* provided by a virtual-flux space vector *ψ*(*xy*) referred to the voltage drop in the output inductance *v*(*xy*) *o* as in Equations (34) and (35). Implementation of the grid side control scheme is provided in Figure 5.

$$
\psi^{(xy)} = \int v\_o^{(xy)}(t) \, dt \tag{34}
$$

$$\theta\_p = \operatorname{atan2}\left(\psi^x, \psi^y\right) \tag{35}$$

The corresponding grid side dynamic model in the *u v* synchronous reference frame, is given in Equation (36),

$$
\sigma^{(uv)} = R \, i^{(uv)} + L \frac{d}{dt} \, i^{(uv)} + F \, L \, i^{(uv)} + v\_{\mathcal{g}}^{(uv)} \tag{36}
$$

where *v*(*uv*) corresponds to the converter output voltage, *i*(*uv*) stands for the grid side current, and *v*(*uv*) *g* to the main busbar voltage, in the *u v* reference frame. Line parameters of resistance and inductance are given as *R* and *L*, respectively, and matrix *F* has been defined in Equation (27).

**Figure 5.** Voltage-oriented control scheme.
