November 5, 2024

Dynamical Model of a Tank (Reservoir) Filled with Liquid and Simulation in Python


In this control engineering, control theory, and process control tutorial, we explain how to develop a dynamical model of a tank (reservoir) filled with a liquid and how to simulate such a model in Python. This is a classical problem for understanding the dynamics of fluid and process systems. The YouTube video accompanying this tutorial is given below.

Problem Formulation

The figure given below introduces the problem.

Fig 1: Tank with fluid.

We consider a tank with a cross-section area of A filled with an incompressible fluid. The fluid level at the time instant t is h. The input (volumetric) flow rate to the tank is denoted by Q_{in}. The output (volumetric) flow rate of the tank is denoted by Q_{out}. We assume that the output flow is passive (not actively controlled). We assume that the cross-section of the hole is much smaller than the cross-section A of the tank. We assume that we can control the input flow rate Q_{in}. Our goal is

  1. Derive a differential equation describing the change of the fluid level h as the function of time and as the function of Q_{in}.
  2. For a given initial fluid level h(0) and for a given time series of Q_{in}, numerically solve and simulate the solution of the derived differential equation in Python.

Modeling and Simulation of a Tank Model in Python

Consider the figure given below.

Fig 2: Left- fluid level at the time instant t. Right – fluid level at the time instant t+\text{d}t.

On the left part of the figure, we show the fluid level h at the time instant t. After an infinitesimally small amount of time \text{d}t, due to the input flow the fluid level changed to h+\text{d}h, where \text{d}h is the change of the fluid level. The volume change is \text{d}h \cdot A.

The rate of change of the fluid volume during this time instant is

(1)   \begin{align*}\frac{\text{d}h \cdot A}{\text{d}t}=\frac{\text{d}h}{\text{d}t}\cdot A\end{align*}

On the other hand, the term

(2)   \begin{align*}\frac{\text{d}h}{\text{d}t}\end{align*}

is the velocity of the fluid in the tank.

On the other hand, in the general case, the flow is the product of the fluid velocity and the area through which the fluid flows. That is

(3)   \begin{align*}Q=A_{f}\cdot v_{f}\end{align*}

where A_{f} is the area and v_{f} is the velocity of the fluid. Taking all these things into account, we conclude that the product in (1) is actually a total flow of the fluid in the tank. This flow is actually a difference between the flow coming in and the flow getting out of the tank. That is,

(4)   \begin{align*}\frac{\text{d}h}{\text{d}t}\cdot A=Q_{in}-Q_{out}\end{align*}

On the other hand, since the output flow is passive, we can use a formula in our previous tutorial for the output flow. This formula is

(5)   \begin{align*}Q_{out}=c_{D}\sqrt{2g}\sqrt{h}\end{align*}

where c_{D} is the discharge coefficient and g is the gravitational acceleration constant. We write the last equation as follows:

(6)   \begin{align*}Q_{out}=c\sqrt{h}\end{align*}

where the constant c is defined by

(7)   \begin{align*}c=c_{D}\sqrt{2g}\end{align*}

By substituting (6) in (4), we obtain the model of the tank system:

(8)   \begin{align*}\frac{\text{d}h}{\text{d}t}\cdot A=Q_{in}-c\sqrt{h}\end{align*}

The last equation can be written in the following form

(9)   \begin{align*}\frac{\text{d}h}{\text{d}t}=\frac{1}{A}\cdot Q_{in}- \frac{c}{A}\sqrt{h}\end{align*}

This is the final form we use for the simulation of the system in Python. The Python code for simulating the system is explained in the video above and after paying a small fee, the code can be accessed by using this link.

We assume the following simulation conditions. The initial height h(0)=1 (all the units are SI units). The area is A=3.1416. The constant c=2.6577

The input flow is given in the figure below.

Fig 3: Input flow Q_{in} used for simulation.

The simulated height h is given in the figure below.

Fig 4: simulated height of the fluid.

Next, we simulate the response of the system to the initial state. The results are shown below.

Fig. 4: Height of the fluid when the input flow is zero. This is the initial state response.