Lesson
30: Solving Environmental ODEs Symbolically (sp.dsolve) β
Reactors, Decay & Radon/Lake Transients
The βBoring Stuffβ Problem: Memorizing Special-Case Integrating Factors for Linear Tanks
When a toxic spill enters a retention pond (\(V \frac{dC}{dt} = Q(C_{\text{in}} - C) - k C
V\), \(C(0) = C_0\)),
sp.dsolve(ode, ics={C(0): C0}) solves the
exact differential equation WITH initial condition in two
lines, outputting an algebraic formula valid for ANY future
flow \(Q\) or decay rate \(k\)!
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
# Example 1: Exact Symbolic Solution of Transient Reactive Detention Basin (CSTR ODE) via sp.dsolve
t = sp.symbols("t", nonnegative=True)
theta, k, C_in, C_0 = sp.symbols("theta k C_in C_0", positive=True)
C = sp.Function("C")
ode_cstr = sp.Eq(sp.Derivative(C(t), t), (C_in - C(t)) / theta - k * C(t))
sol_cstr = sp.simplify(sp.dsolve(ode_cstr, ics={C(0): C_0}).rhs)
sol_clean_start = sp.simplify(sp.dsolve(ode_cstr, ics={C(0): 0}).rhs)
print("=== Exact SymPy CSTR Analytical Solutions ===")
print("General Initial C0 != 0 :", sol_cstr)
print("Clean Pond C(0) = 0 :", sol_clean_start)
print("Steady-State Limit t->inf:", sp.limit(sol_clean_start, t, sp.oo))
# Evaluate numeric transient pond example (V=4000, Q=1000 -> theta=4d, k=0.15/d, Cin=50 mg/L, C0=0)
f_pond = sp.lambdify(t, sol_clean_start.subs({theta: 4.0, k: 0.15, C_in: 50.0}), "numpy")
t_d = np.linspace(0.0, 12.0, 120)
fig, ax = plt.subplots(figsize=(7.2, 3.6))
ax.plot(t_d, f_pond(t_d), color="#0284c7", lw=2.4, label="SymPy Exact Transient C(t)")
ax.axhline(50.0 / (1.0 + 0.15 * 4.0), color="#dc2626", ls="--", label="Steady State C_ss = 31.25 mg/L")
ax.set_title("Transient Detention Pond Toxic Spill Response (Derived via sp.dsolve)")
ax.set_xlabel("Time t [days]"); ax.set_ylabel("Pond Concentration [mg/L]")
ax.grid(True, alpha=0.3); ax.legend(); fig.tight_layout()
print(f"Concentration at Day 4 = {f_pond(4.0):.4f} mg/L | Steady-State = {50/(1+0.6):.2f} mg/L")
=== Exact SymPy CSTR Analytical Solutions ===
General Initial C0 != 0 : (C_0*k*theta + C_0 + C_in*exp(t*(k*theta + 1)/theta) - C_in)*exp(-t*(k*theta + 1)/theta)/(k*theta + 1)
Clean Pond C(0) = 0 : -C_in*exp(-k*t - t/theta)/(k*theta + 1) + C_in/(k*theta + 1)
Steady-State Limit t->inf: C_in/(k*theta + 1)
Concentration at Day 4 = 24.9407 mg/L | Steady-State = 31.25 mg/L
Concept Check Questions
- Why did
sp.limit(sol_clean_start, t, sp.oo)automatically recover \(C_{\text{in}} / (1 + k\theta)\)? - What does
ics={C(0): C_0}pass tosp.dsolve? - Explain the difference between using
scipy.integrate.solve_ivp(numeric points, handles nonlinear Monod ODEs) vs.Βsympy.dsolve(exact analytical equation for linear ODEs).
Guided
Practice Problem 1: Symbolic 2-Stage Radioactive / PFAS Decay Chain
(A -> B) via sp.dsolve
Consider pollutant \(A\) decaying at
rate \(k_A\) directly into toxic
metabolite \(B\) (e.g., PCE \(\to\) TCE \(\to\) Vinyl Chloride) which decays at \(k_B\): \[\frac{dA}{dt} = -k_A A, \qquad \frac{dB}{dt} =
k_A A - k_B B, \qquad A(0) = A_0,\; B(0) = 0\] Solve this coupled
linear ODE system analytically with
sp.dsolve([eq1, eq2], ics={A(0): A0, B(0): 0}) in
SymPy!
# Guided Practice 1 Starter
kA, kB, A0 = sp.symbols("kA kB A0", positive=True)
A_fn, B_fn = sp.Function("A"), sp.Function("B")
eqs = [sp.Eq(sp.Derivative(A_fn(t), t), -kA * A_fn(t)), sp.Eq(sp.Derivative(B_fn(t), t), kA * A_fn(t) - kB * B_fn(t))]
chain = sp.dsolve(eqs, ics={A_fn(0): A0, B_fn(0): 0})
print("Exact Daughter Metabolite B(t) =", sp.simplify(chain[1].rhs))Exact Daughter Metabolite B(t) = A0*kA*(exp(kA*t) - exp(kB*t))*exp(-t*(kA + kB))/(kA - kB)