## 5 Simulator Implementation  
For the implementation of the simulator, the Matlab/Simulink tool was used using the C language to encode the functions that describe the behavior of the model. The simulation code was programmed and encapsulated in an S-Function (Level 2) in Simulink environment and the interface was adapted to be able to contrast data from the output of the supervision and control software (SCADA) with the output of the simulation. The proposed simulator could be configured for any programmable logic  
Figure 8: Diagram representing the diffusion effect respect time and space (not at scale) in the tank for a water inflow \(Fin\). At different instant times (t\({}_{0}\), t\({}_{1}\), t\({}_{2}\)... ), the concentration of chlorine is different in the tank (c\({}_{0}\), c\({}_{i}\), c\({}_{j}\)... ) depending on the distance to the tank inlet (function of \(x\)). controller (PLC) and SCADA controller with an OPC interface (OLE for process control, object linking, and embedding). One of the advantages of using a S-Function is that it allows for the creation of a general-purpose block that can be used in all the iterations of the model, varying the parameters with each instance of the block and thus interacting in real time with the controller (PLC). The simulator implements the central finite differences for the discretized model in Equation (8) [28]. Simulation main screen (in Simulink environment) for the interaction scheme between the simulator and the PLC controller according to the simulator architecture (Figure 2) is shown in Figure 9. The data from the controller are evaluated by the _estimateCd_ function that calculates the chlorine demand and, thus, the value of the rapid decay coefficient (\(k_{R}\)) for each iteration. Depending on the velocity of the flow, we determined the diffusion coefficients that are part of the input variables of the simulator, together with the dose, \(k_{R}\), and velocity. The designed interface allows for the setting of the distance to the reading point, the rapid reaction fraction for \(k_{R}\), and constant values \(k_{r}\) and \(k_{s}\). The simulator comprises two working zones. If there is less than 0.02 ppm of ammonium in the treated water (sensed with analyzer An1), the chlorine demand is calculated on the basis of the temperature and the source of treated water. We developed a classification of the chlorine demand on the basis of the water temperature (Temp) and the percentage of water passing through the carbon filter treatment (TPGacf) with respect to the total treated flow. In the tree, each child node represents a chlorine demand value based on the classification by temperature and source and percentage of treated water. This study was generated through taking real data from the last 5 years in the plant. Figure 10 (left) shows the decision tree for classified data. The study was implemented with RStudio. For values greater than 0.02 ppm in the ammonium concentration, the meter yields accurate and reliable results, having proven experimentally that there is a linear correlation (greater than 0.93) between the ammonium concentration and the chlorine demand for treated water (see Figure 10, right). The validation of the simulator is an important part of this work, because it allows for the measurement of the goodness of such a simulator in its use in forecasting different situations in the real plant in front unknown events that can happen in the future. Results are shown in the next section, but here the implementation of how the simulation is validated is presented. Figure 9: Connection between the simulator and a programmable logic controller (PLC) in the OPC (object linking and embedding (OLE) for process control) platform. The procedure to compare real data obtained with the SCADA and simulated data is shown in Figure 11. Two blocks (_Tank_secA_ and _Tank_secB_) are instantiated in the test, one for each installed analyzer, An2 and An3, at 87 and 163 m away from the dosing point, respectively.