## Materials and Methods  
### Simulator Design  
This is one of the most important contributions of the presented research. The design of the simulator starts with defining the requirement, and proposing a novel architecture that will be able to fulfill them. This proposal involves the software implementation of different mathematical models (hydraulic and transportation models) and a hardware interconnection between plant computers, that is, programmable logic controller (PLC) and the main computer executing the simulator. The chlorination process in the plant has been subjected to different phases of improvement at the automation level in recent years. The problems inherent in the automatic control have been diverse: variability in the chemistry of chlorine, punctual existence in the water to deal with significant amounts of ammonium, large dead times due to the hydraulics of its facilities, or disturbances caused by sudden changes in flow. The use of a simulator that reflects in the most realistic way the behavior of the chlorination process greatly facilitates the design of the control system in the effluent and allows decisions to be made on the optimization of the actual installation. Simulation has been seen over time as a modelling tool that has a very broad development and does not require sophisticated mathematics or statistics to develop a model and its usage [10]. On the other hand, the realization of experiments or tests on the real plant was expensive because of the need to have additional electronics and infrastructure, and moreover it mainly is especially dangerous because this is the last process of the DWTP and there is no correction mechanism in case of error (overdosage or underdosage of chlorine in tap water). The DWTP cannot be closed for testing new chlorination strategies, and the large duration of tests cannot be afforded by all the citizens and small industries that need drinking water uninterruptedly. Therefore, we decided to use a simulator that would allow for the study and analysis of the most appropriate online controller to later transfer the simulation results to the real control system of the plant. When selecting a simulator to implement the desired model, we defined four strong requirements due to the complexity of the plant and the implications of stopping it:  
1. The simulation tool has to be able to simulate a hydraulic model considering the turbulent flow rate. 2. The tool has to simulate a model of transport of diluted species where the reaction of chlorine with compounds of different sources would take place. 3. The tool needs a connection to the plant control system in real time through the OPC (object linking and embedding (OLE) for process control) platform that allows testing. 4. The tool has to allow importing Supervisory Control and Data Acquisition (SCADA) data and be able to compare it, offline, with those produced by the model implemented in the simulator. In recent years, there is a growing interest by large automation companies to integrate computational fluid dynamics (CFD) software into online or real-time control scenarios. Through analyzing different software or platforms existing in the market to be used as a simulator in the chlorine automatic dosage, we finally decided to implement our own simulator, mainly for the simplicity of equations that we developed in this research. With our proposal, it is possible to reduce the sampling time to near 1 second. This fact is very important in order to have a good description and knowledge of water behavior and chlorine diffusion into tanks. After accurate research among many commercial simulators (18 software packages in total), we did not find any commercial simulator fulfilling the aforementioned conditions with such a reduced sampling time that was open-source and internally customized. Next, the architecture of the proposed simulator is presented, which would allow testing chlorination control algorithms for treatment plants (DWTP), fulfilling the following features:* Large contact tanks.