← All posts

10 November 2026 · 11 min read

Sizing an A²O plant in a Python notebook

Sizing an activated sludge plant by hand follows a well-worn sequence: size the anaerobic zone for bio-P, find the aerobic sludge age that nitrifies at the design temperature, size the anoxic zone and the internal recycle for denitrification, set the volume from the MLSS the clarifier can carry, and dose a metal salt for the phosphorus that is left. Each step leans on a steady-state formula and a handful of kinetic constants.

With Theo's Python API, every one of those formulas can be a simulation instead: of the whole plant, with its pH chemistry, its phosphorus-accumulating organisms, its precipitation chemistry and its clarifier. The sequence stays the one you know, and the code stays short. Here is what that looks like for an A²O plant.

The design basis

  • Theo's default influent, held constant: 10,000 m³/d, COD 551, TKN 54 and TP 11.1 g/m³.
  • 12 °C, the cold month.
  • Targets below the limits, to leave room for the dynamic check: effluent NH₄ 0.7 gN/m³ (limit 1), TN 8 gN/m³ (limit 10) and TP 1.5 gP/m³ (limit 2).
  • 3,500 gTSS/m³ in the aerobic tank, what the clarifier can carry, and 2 gO₂/m³ held there by a PID controller on the air.

The notebook and the plant

All of this lives in a Jupyter notebook: a document that mixes text, Python code and its results, run one cell at a time in a web browser. The sizing reads like a calculation note, except that every number in it can be run again. In the notebook, the nonlineum.theo package starts a Theo engine of its own and talks to it the way the Theo application does.

The first thing the notebook does is build the plant: anaerobic, anoxic and aerobic tanks, a clarifier, and ferric chloride (a 40 % solution) dosed into the aerobic tank. Every unit is one line, and so is every connection:

plant.add_influent("Influent")
plant.add_combiner("RAS_combiner")
plant.add_cstr("AN")
plant.add_combiner("IR_combiner")
plant.add_cstr("AX")
plant.add_unit("FerricChlorideDosing", "Ferric")
plant.add_combiner("Fe_combiner")
plant.add_cstr("AE")
plant.add_splitter("WAS_pump", kind="pumped")
plant.add_splitter("IR_pump", kind="pumped")
plant.add_clarifier("Clarifier")
plant.add_effluent("Effluent")
plant.add_effluent("Sludge")

plant.connect("Influent.outlet",     "RAS_combiner.inlet1")
plant.connect("RAS_combiner.outlet", "AN.inlet")
plant.connect("AN.outlet",           "IR_combiner.inlet1")
plant.connect("IR_combiner.outlet",  "AX.inlet")
plant.connect("AX.outlet",           "Fe_combiner.inlet1")
plant.connect("Ferric.outlet",       "Fe_combiner.inlet2")
plant.connect("Fe_combiner.outlet",  "AE.inlet")
plant.connect("AE.outlet",           "WAS_pump.inlet")
plant.connect("WAS_pump.ppd",        "Sludge.inlet")
plant.connect("WAS_pump.ofl",        "IR_pump.inlet")
plant.connect("IR_pump.ppd",         "IR_combiner.inlet2")
plant.connect("IR_pump.ofl",         "Clarifier.inlet")
plant.connect("Clarifier.ufl",       "RAS_combiner.inlet2")
plant.connect("Clarifier.ofl",       "Effluent.inlet")

Here is that plant in Theo's flowsheet editor, laid out, with the oxygen controller (DO_AE) that the next lines add:

The A²O plant the code builds, in Theo's flowsheet editor: the influent, a combiner for the return sludge, the anaerobic tank AN, a combiner for the internal recycle, the anoxic tank AX, the ferric chloride doser joining through a combiner, the aerobic tank AE with its oxygen controller above it, the waste-sludge pump with the sludge sink below it, the internal-recycle pump, the clarifier and the effluent. The internal recycle runs above the train, the return sludge below it.

The return sludge goes to the anaerobic tank, the internal recycle from the aerobic tank to the anoxic one. The waste sludge is drawn from the aerobic tank, so the aerobic sludge age is simply VAE / QWAS. A few more lines switch the air off in the anaerobic and anoxic tanks, set the return sludge to 1 × Q, and add the PID controller that holds the oxygen.

One evaluation

A design point is the aerobic sludge age, the anaerobic and anoxic shares of the bioreactor, the internal recycle and the ferric dose. One function, evaluate, sets the plant to it, runs it from where the last run ended until it is steady, and returns what came out. Theo's own steady-state rule does the waiting, in a single line.

Steady, here, means that the plant, taken as a whole, moves by less than 0.1 % over 20 days, twice in a row. The window matters: after a small change in the ferric dose, effluent phosphorus takes about 40 days to settle, and a one-day window ends the run long before that.

Step 1: bio-P first

Phosphorus-accumulating organisms take up volatile fatty acids in the anaerobic zone, and the zone has to be large enough for the fermentation that makes them. Dold and Conidi argue that a common reason for poor bio-P performance is simply an anaerobic zone that is too small. They cite the WRC (1984) guideline for the anaerobic share of the biomass: 0.15 to 0.20 for an influent COD of 400 to 700 g/m³. Ours has 551.

In an A²O plant the MLSS is about the same in every zone, so the share of the biomass is the share of the volume. A plain loop shows what it does:

for f in (0.05, 0.10, 0.15, 0.20, 0.25):
    row = evaluate(SRT_aer, f, f_AX, R_IR)
Anaerobic share Volume, m³ PO₄ in the anaerobic tank, gP/m³ Effluent TP, gP/m³
0.05 625 7.5 9.00
0.10 1,346 11.4 7.57
0.15 2,187 18.8 5.09
0.20 3,181 24.6 3.20
0.25 4,373 29.9 1.60

That is the paper's point in one table: the larger the anaerobic zone, the more phosphorus the PAOs release in it, and the less of it reaches the effluent. At 5 %, bio-P does not get going at all. The notebook takes 0.20, the upper end of the guideline, and comes back to that choice at the end.

Steps 2 and 3: nitrogen

A loop over the internal recycle shows effluent TN falling from 14.5 to 11.7, 9.9, 8.8 and 8.0 gN/m³ at 1 to 5 × Q. Even the last 1 × Q still gains 0.8 gN/m³, so the recycle goes to 5 × Q, the most the design allows to pump.

Effluent ammonia falls as the aerobic sludge age grows, so SciPy's brentq finds the sludge age where ammonia meets its target (Brent's method), and then the smallest anoxic share that meets TN. Here is the first of those searches, step by step:

Nine simulations of the whole plant. Bisection keeps the search safe while the curve misbehaves; interpolation makes it fast once the bracket is tight.

Steps 4 and 5: the volume and ferric chloride

One correction of the waste flow holds the design MLSS, and the volumes follow from it. Then brentq again, for the ferric dose that brings TP to its target.

Ferric chloride does more than precipitate phosphorus. It uses up alkalinity, which slows nitrification, and its precipitate is sludge too. So steps 3 to 5 run twice in turn, slightly trimmed:

for n in (1, 2):
    SRT_aer = brentq(ammonia_excess, 5, 60, xtol=0.05) + 0.05
    f_AX = brentq(tn_excess, 0.05, 0.35, xtol=0.002) + 0.002
    hold_mlss()
    TP_bio = dose_ferric()

brentq stops within its tolerance of the answer, on either side of it, so each result is rounded up by that tolerance, to the safe side. The first round settled on an aerobic sludge age of 25.7 days, an anoxic share of 0.147 and 0.49 m³/d of ferric chloride solution; the second on 24.6 days, 0.150 and 0.45 m³/d.

Step 6: a second opinion

Nelder–Mead then moves the sludge age, the anoxic share and the recycle together, looking for a smaller bioreactor that still meets every target.

The method is easier to watch than to read. The animation below is Miles Chen's, not our run: it shows Nelder–Mead in general, on a simple function of two variables, where the simplex it walks with is a triangle. Our search moves three variables, so its simplex has four corners.

In the notebook, our search is one call:

minimize(penalised_volume, [SRT_aer, f_AX, R_IR], method="Nelder-Mead",
         options={"maxfev": 30, "xatol": 0.05, "fatol": 10})

Of its 27 evaluations, eight met every target, and the smallest of them was 23,975 m³, 1.5 % below the design, with the sludge age and the recycle trimmed a little. Small enough that the stepwise design stands.

The path, evaluation by evaluation

Every dot below is one evaluation. A filled dot meets every target; a hollow one misses at least one. Until step 5 doses ferric chloride, TP keeps almost every dot hollow.

The first figure shows what the search moves: the aerobic sludge age, the tank volumes, the recycle flows and the waste sludge.

For each of the 102 evaluations: the aerobic sludge age, the anaerobic, anoxic and aerobic volumes, the internal recycle and return sludge flows, and the waste sludge flow, with the sizing steps shaded and the design at evaluation 75

The second shows what each point costs in air and ferric chloride, and what it delivers in the effluent. The two dips in the air are the 5-day sludge age at the bottom of each round's search, where the nitrifiers wash out. The waste flow steps up in the second round, when the ferric sludge has to leave the plant as well.

For each of the 102 evaluations: the air to the aerobic tank, the ferric chloride dose, and effluent TN, TP and NH₄ on a log scale against their targets of 8, 1.5 and 0.7 g/m³

The design

Quantity Design
Anaerobic, anoxic, aerobic volume 4,869, 3,657 and 15,821 m³
Bioreactor 24,347 m³, a 58-hour HRT
Aerobic sludge age 24.6 days
MLSS in the aerobic tank 3,500 gTSS/m³
Waste sludge, internal recycle, RAS 644, 50,000 and 10,000 m³/d
Air 117,500 Nm³/d
Ferric chloride 0.45 m³/d of solution, 88 kg Fe/d, 1.6 mol Fe per mol P it removes
Effluent NH₄, TN, TP 0.69, 7.74 and 1.49 g/m³

That took 102 evaluations, each a settled simulation of the whole plant.

Your call

f_AN = 0.20 is the guideline's upper end for this COD. Bio-P leaves 4.5 gP/m³, and 88 kg Fe/d of ferric chloride take it to 1.5. Change that one line of the notebook to 0.25 and run it again: bio-P leaves 3.5 gP/m³, and 60 kg Fe/d do the rest, for about 1,050 m³ more bioreactor. The anaerobic tank grows by about 1,500 m³ while the other two shrink a little. That's your call as the process engineer.

Anaerobic share 0.20 Anaerobic share 0.25
Anaerobic, anoxic, aerobic volume 4,869, 3,657, 15,821 m³ 6,349, 3,441, 15,607 m³
Bioreactor 24,347 m³ 25,397 m³
TP that bio-P leaves 4.5 gP/m³ 3.5 gP/m³
Ferric chloride 88 kg Fe/d 60 kg Fe/d

Neither does without ferric chloride. Step 1's loop showed TP 1.6 at 0.25, but with a large anoxic zone; the TN target asks for a smaller one, and that costs bio-P some ground.

What it is, and what it is not

This is a steady-state design for a constant influent: the answer a careful hand calculation aims at, with every number coming from the full plant model instead of a formula. The next step is the dynamic check against the design just found (the diurnal load, a storm, a cold snap), and that is a notebook of its own.

Try it

Download the notebook and run it yourself. It comes without saved results, so every number you see is your own engine's. It needs the Python client from PyPI, and SciPy:

pip install --pre "nonlineum-theo[notebook]" scipy

The client drives the simulation engine that comes with Theo. The sizing runs for well over an hour, far past the ten-minute demo, so it needs a licensed Theo. The notebook itself is yours to use and change, under the MIT License.

Reference

Dold, P. and Conidi, D. (2019). Achieving Enhanced Biological P Removal: Have we forgotten how to design a bioP plant? WEFTEC 2019 Proceedings, Water Environment Federation, p. 1452.