Lesson 17: Broadcasting, np.meshgrid 2D Plume Fields & Solar Decarbonization Arrays

The “Boring Stuff” Problem: Nested (i, j) Grid Loops for Air Plumes & Aquifer Fields

Evaluating a 2D spatial grid (say \(200 \times 160 = 32,000\) ground-level air receptors downwind of a power plant stack) with nested Python loops is slow and cluttered. np.meshgrid(x, y) creates full 2D coordinate matrices (X, Y) so you write the Gaussian plume PDE solution verbatim once and get the full 2D concentration map in milliseconds!

Let’s apply np.meshgrid directly to a 3D/2D Ground-Level Gaussian Stack Plume AND use NumPy vector math on Solar_Irradiance_Collector_Hourly.csv (Renewable Energy I course).

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/")
import numpy as np
import matplotlib.pyplot as plt

# Example 1: Vectorized 2D Gaussian Plume Spatial Grid using np.meshgrid
Q_so2_g_s, u_ms, H_eff = 65.0, 4.2, 75.0
x_1d = np.linspace(150.0, 4000.0, 220)
y_1d = np.linspace(-600.0, 600.0, 140)
X, Y = np.meshgrid(x_1d, y_1d)

# Briggs Pasquill-Gifford Class C (Slightly Unstable) spread matrices
sy = 0.11 * X / np.sqrt(1.0 + 0.0001 * X)
sz = 0.08 * X / (1.0 + 0.0002 * X)

C_grid = (Q_so2_g_s * 1e6 / (np.pi * u_ms * sy * sz)) * np.exp(-0.5 * (Y / sy) ** 2 - 0.5 * (H_eff / sz) ** 2)
idx_max = np.unravel_index(np.argmax(C_grid), C_grid.shape)

print(f"Grid Shape : {C_grid.shape} ({C_grid.size:,} receptors computed instantaneously!)")
print(f"Peak Ground-Level SO2 : {C_grid.max():.1f} ug/m^3 at x = {X[idx_max]:.0f} m, y = {Y[idx_max]:.0f} m")

fig, ax = plt.subplots(figsize=(8.0, 3.8))
cf = ax.contourf(X / 1000.0, Y, C_grid, levels=16, cmap="inferno_r")
plt.colorbar(cf, ax=ax, label="Ground SO2 [ug/m^3]")
ax.plot(X[idx_max] / 1000.0, Y[idx_max], "w*", markersize=11, label=f"Peak {C_grid.max():.0f} ug/m^3")
ax.set_title("Vectorized 2D Gaussian Stack Plume Footprint (Stability C, H_eff = 75 m)")
ax.set_xlabel("Downwind Distance x [km]")
ax.set_ylabel("Crosswind Offset y [m]")
ax.legend(loc="upper right")
fig.tight_layout()
Broadcasting, np.meshgrid 2D Plume Fields & Solar Decarbonization Arrays Output Plot 1
Grid Shape : (140, 220) (30,800 receptors computed instantaneously!)
Peak Ground-Level SO2 : 421.7 ug/m^3 at x = 748 m, y = -4 m
# Example 2 (Solar Thermal Course Integration): Hourly Solar Heat vs Displaced Coal/Gas Carbon
# Columns: hour, G_T_W_m2, T_amb_C, U_L_W_m2K, efficiency_percent
sol = np.loadtxt(data_file("Solar_Irradiance_Collector_Hourly.csv"), delimiter=",", skiprows=1)
hrs, G_T, T_amb, U_L, eff_pct = sol.T
area_field_m2 = 250.0
q_hourly_kwh = area_field_m2 * (G_T / 1000.0) * (eff_pct / 100.0)   # 1-hr step kWh
daily_thermal_kwh = np.sum(q_hourly_kwh)
# Compare against 85%-eff gas boiler (~0.217 kg CO2eq/kWh delivered)
co2_avoided_kg = daily_thermal_kwh * 0.217
print(f"250 m^2 Solar Field Daily Heat Delivered : {daily_thermal_kwh:,.1f} kWh_th/day")
print(f"Daily Fossil CO2 Displaced               : {co2_avoided_kg:,.1f} kg CO2-eq/day ({co2_avoided_kg*365/1000:.1f} t/yr)")
250 m^2 Solar Field Daily Heat Delivered : 1,137.7 kWh_th/day
Daily Fossil CO2 Displaced               : 246.9 kg CO2-eq/day (90.1 t/yr)

Concept Check Questions

  1. Check dimensions cleanly: if x_1d has length 220 and y_1d has length 140, why does X, Y = np.meshgrid(x_1d, y_1d) have shape (140, 220)?
  2. What does np.unravel_index(np.argmax(C_grid), C_grid.shape) convert?
  3. Why did we divide by np.sqrt(1 + 0.0001 * X) instead of math.sqrt?

Guided Practice Problem 1: Effective Stack Height Sensitivity (75 m vs 110 m Plume Peak)

Using the exact same (X, Y) grid above, compute the peak ground concentration if the plant builds a taller H_eff = 110.0 m chimney (Q = 65 g/s, u = 4.2 m/s, Stability C), and print the percentage drop in peak ground exposure!

# Guided Practice 1 Starter
C_tall = (Q_so2_g_s * 1e6 / (np.pi * u_ms * sy * sz)) * np.exp(-0.5 * (Y / sy) ** 2 - 0.5 * (110.0 / sz) ** 2)
drop = (1.0 - C_tall.max() / C_grid.max()) * 100.0
print(f"H=110 m Peak = {C_tall.max():.1f} ug/m^3 ({drop:.1f}% reduction vs 75 m stack!)")
H=110 m Peak = 186.3 ug/m^3 (55.8% reduction vs 75 m stack!)