Lesson
26: Solving Coupled Environmental ODEs
(scipy.integrate.solve_ivp) — BOD-DO & Monod
Growth
The “Boring Stuff” Problem: Nonlinear Microbial Kinetics With No Textbook Closed-Form Solution
Real biological wastewater reactors follow coupled nonlinear
Monod ODEs: \[\frac{dS}{dt} =
-\frac{\mu_{\max}}{Y}\frac{S}{K_s + S} X, \qquad \frac{dX}{dt} =
\left(\mu_{\max}\frac{S}{K_s + S} - k_d\right) X\] You cannot
solve this system by integrating on paper!
scipy.integrate.solve_ivp(rhs, (t0, t_end), y0, t_eval=...)
integrates nonlinear environmental ODE systems cleanly across any time
span—let’s compare Coupled Streeter–Phelps BOD + DO + Benthic
Demand AND Activated Sludge Microbial Monod
Kinetics!
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# Example 1: Coupled Monod Microbial Growth & Substrate Depletion Batch Bioreactor
mu_max, Ks, Y_yield, kd_decay = 4.2, 45.0, 0.52, 0.12 # 1/day, mg/L, mg VSS/mg BOD, 1/day
def monod_rhs(t, y):
S, X = y
S_pos = max(S, 0.0)
mu = mu_max * S_pos / (Ks + S_pos)
dS_dt = -(mu / Y_yield) * X
dX_dt = (mu - kd_decay) * X
return [dS_dt, dX_dt]
t_grid = np.linspace(0.0, 1.5, 160) # 0 to 1.5 days (36 hours)
sol = solve_ivp(monod_rhs, (0.0, 1.5), y0=[320.0, 25.0], t_eval=t_grid, rtol=1e-8)
assert sol.success
S_t, X_t = sol.y
fig, ax1 = plt.subplots(figsize=(7.6, 4.0))
ax1.plot(sol.t * 24, S_t, color="#dc2626", lw=2.3, label="Substrate BOD S(t) [mg/L]")
ax1.set_xlabel("Batch Aeration Time [hours]"); ax1.set_ylabel("Substrate BOD [mg/L]", color="#dc2626")
ax1.grid(True, alpha=0.3)
ax2 = ax1.twinx()
ax2.plot(sol.t * 24, X_t, color="#059669", lw=2.3, ls="--", label="Biomass MLVSS X(t) [mg/L]")
ax2.set_ylabel("Active Biomass X [mg/L]", color="#059669")
ax1.set_title("Activated Sludge Monod Substrate Consumption & Biomass Growth (solve_ivp)")
fig.tight_layout()
print(f"At 12 hours: S = {np.interp(0.5, sol.t, S_t):.1f} mg/L | Biomass X = {np.interp(0.5, sol.t, X_t):.1f} mg/L")
print(f"Peak Biomass = {X_t.max():.1f} mg/L @ t = {sol.t[np.argmax(X_t)]*24:.1f} h (before endogenous decay!)")
At 12 hours: S = 101.6 mg/L | Biomass X = 134.6 mg/L
Peak Biomass = 182.3 mg/L @ t = 17.2 h (before endogenous decay!)
# Example 2: Coupled Multi-Factor Streeter-Phelps ODE (Carbonaceous CBOD + Nitrogenous NBOD + Benthic Sediments!)
# Textbook algebraic formula ONLY handles CBOD! Adding NBOD & benthic SOD takes 3 extra lines in solve_ivp!
def full_river_do_rhs(t, y, kc=0.28, kn=0.14, kr=0.62, sod_rate=0.45):
Lc, Ln, D = y
dLc = -kc * Lc
dLn = -kn * Ln if t > 1.2 else 0.0 # Nitrification starts ~1.2 days downstream!
dD = kc * Lc + (kn * Ln if t > 1.2 else 0.0) + sod_rate - kr * D
return [dLc, dLn, dD]
t_riv = np.linspace(0.0, 8.0, 200)
sol_r = solve_ivp(full_river_do_rhs, (0.0, 8.0), y0=[22.0, 14.0, 1.0], t_eval=t_riv, max_step=0.05)
DO_riv = 9.2 - sol_r.y[2]
print(f"Multi-Stage River Minimum DO (with delayed NBOD + Benthic SOD) = {DO_riv.min():.2f} mg/L @ day {sol_r.t[np.argmin(DO_riv)]:.2f}")Multi-Stage River Minimum DO (with delayed NBOD + Benthic SOD) = 1.59 mg/L @ day 2.89
Concept Check Questions
- Notice
def monod_rhs(t, y):: why musttappear FIRST insolve_ivpODE functions, whereas legacyodeintputyfirst? - Look at Example 2: why could the classic analytical Streeter–Phelps
textbook formula NEVER capture nitrification (
NBOD) turning on att > 1.2 days, whilesolve_ivpsolved it effortlessly? - Explain why biomass \(X(t)\)
reaches a peak and then slowly declines at
t > 20 hoursin Example 1.
Guided
Practice Problem 1: Transient CSTR Spill Washout Verification
(solve_ivp)
A \(5,000\text{ m}^3\) lake
(Q = 1,250 m^3/day) receives a toxic chemical spill
starting at \(t=0\) with clean lake
\(C(0)=0\text{ mg/L}\), constant inflow
\(C_{\text{in}}=40\text{ mg/L}\), and
decay \(k = 0.12\text{ day}^{-1}\):
\[\frac{dC}{dt} = \frac{Q}{V}(C_{\text{in}} -
C) - kC\] Solve with solve_ivp over \(0..15\text{ days}\), print \(C(4\text{ d})\), and verify it matches the
exact exponential formula \(C_{\text{ss}}(1 -
e^{-(1/\theta + k)t})\)!
# Guided Practice 1 Starter
tau_l, k_d, c_in = 5000.0 / 1250.0, 0.12, 40.0
sol_c = solve_ivp(lambda t, y: [(c_in - y[0]) / tau_l - k_d * y[0]], (0, 15), [0.0], t_eval=[0, 4, 8, 15])
c_ss = c_in / (1.0 + k_d * tau_l)
c_exact_4 = c_ss * (1.0 - np.exp(-(1.0 / tau_l + k_d) * 4.0))
print(f"ODE @ Day 4 = {sol_c.y[0][1]:.4f} mg/L vs Exact = {c_exact_4:.4f} mg/L (Steady-State = {c_ss:.2f} mg/L)")ODE @ Day 4 = 20.8770 mg/L vs Exact = 20.8747 mg/L (Steady-State = 27.03 mg/L)