# Hydraulic simulation¶

WNTR contains two simulators: the **WNTRSimulator** and the **EpanetSimulator**.
See Software framework and limitations for more information on features and limitations of these simulators.

The EpanetSimulator can be used to run demand-driven hydraulic simulations using the EPANET 2 Programmer’s Toolkit. The simulator can also be used to run water quality simulations, as described in Water quality simulation. A hydraulic simulation using the EpanetSimulator is run using the following code:

```
epanet_sim = wntr.sim.EpanetSimulator(wn)
epanet_sim_results = epanet_sim.run_sim()
```

The WNTRSimulator is a pure Python simulation engine based on the same equations as EPANET. The WNTRSimulator does not include equations to run water quality simulations. The WNTRSimulator includes the option to simulate leaks, and run hydraulic simulation in demand-driven or pressure-driven demand mode. A hydraulic simulation using the WNTRSimulator is run using the following code:

```
wntr_sim = wntr.sim.WNTRSimulator(wn)
wntr_sim_results = wntr_sim.run_sim()
```

The example **hydraulic_simulation.py** can be used to run both simulators.

More information on the simulators can be found in the API documentation, under
`EpanetSimulator`

and
`WNTRSimulator`

.

## Options¶

Hydraulic simulation options are defined in the `WaterNetworkOptions`

class.
These options include
duration,
hydraulic timestep,
rule timestep,
pattern timestep,
pattern start,
default pattern,
report timestep,
report start,
start clocktime,
headloss,
trails,
accuracy,
unbalenced,
demand multiplier, and
emitter exponent.
All options are used with the EpanetSimulator.
Options that are not used with the WNTRSimulator are described in Limitations.

## Mass balance at nodes¶

Both simulators use the mass balance equations from EPANET [Ross00]:

where \(P_{n}\) is the set of pipes connected to node \(n\), \(q_{p,n}\) is the flow rate of water into node \(n\) from pipe \(p\) (m³/s), \(D_{n}^{act}\) is the actual demand out of node \(n\) (m³/s), and \(N\) is the set of all nodes. If water is flowing out of node \(n\) and into pipe \(p\), then \(q_{p,n}\) is negative. Otherwise, it is positive.

## Headloss in pipes¶

Both simulators use the Hazen-Williams headloss formula from EPANET [Ross00]:

where \(h_{L}\) is the headloss in the pipe (m), \(C\) is the Hazen-Williams roughness coefficient (unitless), \(d\) is the pipe diameter (m), \(L\) is the pipe length (m), \(q\) is the flow rate of water in the pipe (m³/s), \(H_{n_{j}}\) is the head at the starting node (m), and \(H_{n_{i}}\) is the head at the ending node (m).

The flow rate in a pipe is positive if water is flowing from the starting node to the ending node and negative if water is flowing from the ending node to the starting node. However, this equation is not valid for negative flow rates. Therefore, WNTR uses a reformulation of this constraint.

For \(q<0\):

For \(q \geq 0\):

These equations are symmetric across the origin and valid for any \(q\). Thus, this equation can be used for flow in either direction. However, the derivative with respect to \(q\) at \(q = 0\) is \(0\). In certain scenarios, this can cause the Jacobian of the set of hydraulic equations to become singular (when \(q=0\)). The WNTRSimulator uses a modified Hazen-Williams formula. The modified Hazen-Williams formula splits the domain of \(q\) into six segments to create a piecewise smooth function.

## Demand-driven simulation¶

In demand-driven simulation, pressure in the system depends on the node demands. The mass balance and headloss equations described above are solved assuming that node demands are known and satisfied. This assumption is reasonable under normal operating conditions and for use in network design. Both simulators can run hydraulics using demand-driven simulation.

## Pressure-driven demand simulation¶

In situations that lead to low pressure conditions (i.e., fire fighting, power outages, pipe leaks), consumers do not always receive their requested demand and pressure-driven demand simulation is recommended. In pressure-driven demand simulation, the delivered demand depends on pressure. The mass balance and headloss equations described above are solved by simultaneously determining demand along with the network pressures and flow rates.

The WNTRSimulator can run hydraulics using pressure-driven demand simulation using the following pressure-demand relationship [WaSM88]:

where \(d\) is the actual demand (m³/s), \(D_f\) is the desired demand (m³/s), \(p\) is the pressure (Pa), \(P_f\) is the pressure above which the consumer should receive the desired demand (Pa), and \(P_0\) is the pressure below which the consumer cannot receive any water (Pa). The set of nonlinear equations comprising the hydraulic model and the pressure-demand relationship is solved directly using a Newton-Raphson algorithm.

Figure 6 illustrates the pressure demand relationship using demand-driven and pressure-driven demand simulation. In the example, \(D_f\) is 0.0025 m³/s (39.6 GPM), \(P_f\) is 30 psi, and \(P_0\) is 5 psi. Using demand-driven simulation, the demand is equal to \(D_f\) regardless of pressure. Using pressure-driven demand simulation, the demand starts to decrease when pressure is below \(P_f\) and goes to 0 when pressure is below \(P_0\).

## Leak model¶

The WNTRSimulator includes the ability to add leaks to the network. The leak is modeled with a general form of the equation proposed by Crowl and Louvar [CrLo02] where the mass flow rate of fluid through the hole is expressed as:

where \(d_{leak}\) is the leak demand (m³/s), \(C_d\) is the discharge coefficient (unitless), \(A\) is the area of the hole (m²), \(p\) is the gauge pressure inside the pipe (Pa), \(\alpha\) is the discharge coefficient, and \(\rho\) is the density of the fluid. The default discharge coefficient is 0.75 (assuming turbulent flow), but the user can specify other values if needed. The value of \(\alpha\) is set to 0.5 (assuming large leaks out of steel pipes). Leaks can be added to junctions and tanks. A pipe break is modeled using a leak area large enough to drain the pipe. WNTR includes methods to add leaks to any location along a pipe by splitting the pipe into two sections and adding a node.

Figure 7 illustrates leak demand. In the example, the diameter of the leak is set to 0.5 cm, 1.0 cm, and 1.5 cm.

## Pause and restart¶

The WNTRSimulator includes the ability to

- Reset initial values and re-simulate using the same water network model. Initial values include tank head, reservoir head, pipe status, pump status, and valve status.
- Pause a hydraulic simulation, change network operations, and then restart the simulation
- Save the water network model and results to files and reload for future analysis

These features are helpful when evaluating various response action plans or when
simulating long periods of time where the time resolution might vary.
The file **hydraulic_simulation.py** includes examples of these features.