Lesson 03: Jupyter Workflows, Markdown Engineering Reports & Mass-Balance Thinking

The “Boring Stuff” Problem: Disconnected Equations in Word + Mystery Numbers in Scripts

In traditional lab reports, students write governing equations in Microsoft Word, compute numbers in a separate spreadsheet, and manually type answers back into Word—so when one assumption changes (say, pond volume drops by \(15\%\) due to sediment buildup), half the report goes stale.

Jupyter Notebooks eliminate that manual sync completely because narrative Markdown (with LaTeX equations) and live executable Python cells live in the same document!

Golden Jupyter Rule: Always Test With Kernel -> Restart & Run All

Because code cells share memory (ns namespace), running Cell 4 before Cell 2 can succeed temporarily using old values in RAM—then fail completely when graded top-to-bottom. Clicking Restart & Run All proves your environmental calculation reproduces cleanly from scratch.

import math

# Example 1 (Environmental Systems & Calculus I): Well-Mixed Detention Pond Mass Balance
# Governing conservation ODE: d(V*C)/dt = Q_in * C_in - Q_out * C - k_decay * C * V
# Steady-state (dC/dt = 0, Q_in = Q_out = Q): C_ss = C_in / (1 + k_decay * theta)

V_pond_m3 = 24000.0          # Constructed wetland detention pond volume [m^3]
Q_stream_m3_d = 6000.0       # Steady storm-water inflow = outflow [m^3/day]
C_in_nitrate_mgL = 18.0      # Agricultural runoff nitrate [mg/L]
k_denit_1_d = 0.35           # First-order denitrification rate constant [1/day]

theta_days = V_pond_m3 / Q_stream_m3_d      # Hydraulic residence time [days]
damkohler_Da = k_denit_1_d * theta_days      # Dimensionless Damkohler number (reaction vs flow)
C_out_mgL = C_in_nitrate_mgL / (1.0 + damkohler_Da)
removal_pct = (1.0 - C_out_mgL / C_in_nitrate_mgL) * 100.0
nitrate_removed_kg_d = Q_stream_m3_d * (C_in_nitrate_mgL - C_out_mgL) * 1e-3

print("=== Constructed Wetland Nitrate Removal Report ===")
print(f"Residence Time theta  : {theta_days:.2f} days")
print(f"Damkohler No. (k*tau) : {damkohler_Da:.2f} [-]")
print(f"Outlet Nitrate        : {C_out_mgL:.2f} mg/L ({removal_pct:.1f}% removed)")
print(f"Daily N Mass Removed  : {nitrate_removed_kg_d:.1f} kg NO3-N/day")
=== Constructed Wetland Nitrate Removal Report ===
Residence Time theta  : 4.00 days
Damkohler No. (k*tau) : 1.40 [-]
Outlet Nitrate        : 7.50 mg/L (58.3% removed)
Daily N Mass Removed  : 63.0 kg NO3-N/day
from pathlib import Path
def data_file(name):
    """Locate a dataset file whether running from SRC_DIR, module folder, HOMEWORK, or EXAMS."""
    for p in [
        Path("datasets") / name,
        Path("../datasets") / name,
        Path("../../datasets") / name,
    ]:
        if p.exists():
            return p
    raise FileNotFoundError(f"Missing dataset {name} in datasets/")
# Example 2 (Solar Thermal Course Integration): Solar Collector Daily Carbon Displacement
# Cross-reference: Solar_Irradiance_Collector_Hourly.csv (Renewable Energy I)
# Suppose a 40 m^2 industrial solar preheater collects heat on a clear afternoon hour (hour 13)

lines = data_file("Solar_Irradiance_Collector_Hourly.csv").read_text(encoding="utf-8").splitlines()[1:]
# Find midday hour 12 in CSV: columns [hour, G_T_W_m2, T_amb_C, U_L_W_m2K, efficiency_percent]
for ln in lines:
    cols = [float(x) for x in ln.split(",")]
    if int(cols[0]) == 12:
        hr, G_T, T_amb, U_L, eff_pct = cols
        A_coll_m2 = 40.0
        q_solar_kw = A_coll_m2 * G_T * (eff_pct / 100.0) / 1000.0
        # Compare against 85% efficient natural gas boiler (0.185 kg CO2 / kWh_th fuel)
        co2_saved_kg_h = (q_solar_kw / 0.85) * 0.185
        print("=== Midday Solar Thermal Decarbonization Check (Hour 12) ===")
        print(f"Irradiance G_T={G_T:.1f} W/m^2 | Efficiency={eff_pct:.1f}%")
        print(f"Thermal Output = {q_solar_kw:.2f} kW_th | CO2 Avoided = {co2_saved_kg_h:.2f} kg CO2/hour")
        break
=== Midday Solar Thermal Decarbonization Check (Hour 12) ===
Irradiance G_T=900.0 W/m^2 | Efficiency=66.2%
Thermal Output = 23.83 kW_th | CO2 Avoided = 5.19 kg CO2/hour

Concept Check Questions

  1. Why is it dangerous to test Cell 3 -> Cell 2 -> Cell 1 in Jupyter before submitting a report?
  2. What does the dimensionless Damköhler number \(\text{Da} = k \theta\) tell you physically when \(\text{Da} \gg 1\) vs. \(\text{Da} \ll 1\)?
  3. How do Jupyter Markdown cells complement Python f-strings in regulatory deliverables?

Guided Practice Problem 1: Wetland Sedimentation Capacity Sensitivity Check

If 35% of the wetland pond fills with silt (V_pond_m3 = 24000 * 0.65), calculate the new residence time \(\theta_{\text{silted}}\), new outlet nitrate concentration \(C_{\text{out,silted}}\), and how much extra daily nitrate (\(\text{kg/day}\)) escapes into the downstream river compared to the clean wetland above.

# Guided Practice 1 Starter — Compute Silted Wetland Loss!
V_silted = 24000.0 * 0.65
theta_silted = V_silted / 6000.0
C_silted = 18.0 / (1.0 + 0.35 * theta_silted)
print(f"Silted theta = {theta_silted:.2f} d -> Outlet NO3 = {C_silted:.2f} mg/L (vs {C_out_mgL:.2f} mg/L clean)")
Silted theta = 2.60 d -> Outlet NO3 = 9.42 mg/L (vs 7.50 mg/L clean)