# Water quality simulation¶

Water quality simulations can only be run using the EpanetSimulator. As listed in the Software framework and limitations section, this means that the hydraulic simulation must use demand-driven simulation. Note that the WNTRSimulator can be used to compute demands under pressure dependent demand conditions and those demands can be used in the EpanetSimulator. The following code illustrates how to reset demands in a water network model using a pressure dependent demand simulation:

sim = wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
wn.reset_demand(results.node['demand'], 'PDD')
sim = wntr.sim.EpanetSimulator(wn)
results_withPDdemands = sim.run_sim()


After defining water quality options and sources (described in the Options and Sources sections below), a hydraulic and water quality simulation using the EpanetSimualtor is run using the following code:

sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()


The example water_quality_simulation.py can be used to run water quality simulations and plot results.

## Options¶

Water quality simulation options are defined in the WaterNetworkOptions class. Three types of water quality analysis are supported. These options include water age, tracer, and chemical concentration.

• Water age: Water quality simulation can be used to compute water age at every node. To compute water age, set the ‘quality’ option as follows:
wn.options.quality = 'AGE'

• Tracer: Water quality simulation can be used to compute the percent of flow originating from a specific location. The results include tracer percent values at each node. For example, to track a tracer from node ‘111’, set the ‘quality’ and ‘tracer_node’ options as follows:
wn.options.quality = 'TRACE'
wn.options.quality_value = '111'

• Chemical concentration: Water quality simulation can be used to compute chemical concentration given a set of source injections. The results include chemical concentration values at each node. To compute chemical concentration, define sources (described in the Sources section below) and set the ‘quality’ options as follows:
wn.options.quality = 'CHEMICAL'

• To skip the water quality simulation, set the ‘quality’ options as follows:
wn.options.quality = 'NONE'


Additional water quality options include viscosity, diffusivity, specific gravity, tolerance, bulk reaction order, wall reaction order, tank reaction order, bulk reaction coefficient, wall reaction coefficient, limiting potential, and roughness correlation. These parameters are defined in the WaterNetworkOptions API documentation.

When creating a water network model from an EPANET INP file, water quality options are populated from the [OPTIONS] and [REACTIONS] sections of EPANET INP file. All of these options can be modified in WNTR and then written to an EPANET INP file.

## Sources¶

Sources are required for CHEMICAL water quality analysis. Sources can still be defined, but will not be used if AGE, TRACE, or NONE water quality analysis is selected. Sources are added to the water network model using the add_source method. Sources include the following information:

• Source name: A unique source name used to reference the source in the water network model.
• Node name: The injection node.
• Source type: Options include ‘CONCEN,’ ‘MASS,’ ‘FLOWPACED,’ or ‘SETPOINT.’
• CONCEN source represents injection of a specific concentration.
• MASS source represents a booster source with a fixed mass flow rate.
• FLOWPACED source represents a booster source with a fixed concentration at the inflow of the node.
• SETPOINT source represents a booster source with a fixed concentration at the outflow of the node.
• Strength: Baseline source strength (in mass/time for MASS and mass/volume for CONCEN, FLOWPACED, and SETPOINT).
• Pattern: The pattern name associated with the injection.

For example, the following code can be used to add a source, and associated pattern, to the water network model:

wn.add_pattern('SourcePattern', start_time=2*3600, end_time=15*3600)

In the above example, the pattern is given a value of 1 between 2 and 15 hours, and 0 otherwise. The method remove_source can be used to remove sources from the water network model.