Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Step 7 -- 2D Diffusion

Authors
Affiliations
University of Valencia

The 2D diffusion (heat) equation:

∂u∂t=ν(∂2u∂x2+∂2u∂y2)\frac{\partial u}{\partial t} = \nu \left( \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} \right)

What you will learn:

  1. How diffusion smooths a 2D field

  2. Validation against the analytical Gaussian solution

  3. Energy dissipation in 2D

  4. Differentiating through the diffusion coefficient ν\nu

Physics

Diffusion spreads concentration from high to low regions. A 2D Gaussian initial condition remains Gaussian for all time, with a width that grows as σ(t)=σ02+2νt\sigma(t) = \sqrt{\sigma_0^2 + 2\nu t}. The peak decays as σ02/σ(t)2\sigma_0^2 / \sigma(t)^2 so that total mass is conserved. This analytical solution provides an exact benchmark for the numerical solver.

Grid layout and boundary conditions

In 2D, somax uses the Arakawa C-grid where different variables live at different positions within each cell:

       V_{j+1/2}
    ┌─────●─────┐
    │           │
 U  ●     T     ●  U
i-1/2   (i,j)    i+1/2
    │           │
    └─────●─────┘
       V_{j-1/2}
  • T-points (cell centres): scalars (uu, pp, ω\omega)

  • U-points (east/west faces): x-fluxes

  • V-points (north/south faces): y-fluxes

  • X-points (corners): vorticity (in some formulations)

The full array has shape (Ny, Nx) with a 1-cell ghost ring on all sides:

 ┌───────────────────────────────────┐
 │  ghost  ghost  ghost  ghost  ghost│  ← row 0
 │  ghost  T(1,1) T(1,2) ···   ghost│  ← row 1
 │  ghost  T(2,1) T(2,2) ···   ghost│
 │   ⋮      ⋮      ⋮           ⋮    │
 │  ghost  T(n,1) T(n,2) ···   ghost│  ← row Ny-2
 │  ghost  ghost  ghost  ghost  ghost│  ← row Ny-1
 └───────────────────────────────────┘
        col 0  col 1  ···  col Nx-1

Interior cells are [1:-1, 1:-1]. Ghost cells are filled by boundary conditions before each RHS evaluation.

Periodic BCs (enforce_periodic) copy the last interior row/column to the opposite ghost row/column:

 field[0, :]  = field[-2, :]   (south ghost ← north interior)
 field[-1, :] = field[1, :]    (north ghost ← south interior)
 field[:, 0]  = field[:, -2]   (west ghost ← east interior)
 field[:, -1] = field[:, 1]    (east ghost ← west interior)

1. Create the model

Diffusion2D(
  params=Diffusion2DParams(nu=weak_f32[]),
  grid=ArakawaCGrid2D(Nx=66, Ny=66, Lx=4.0, Ly=4.0, dx=0.0625, dy=0.0625),
  diff=Difference2D(
    grid=ArakawaCGrid2D(Nx=66, Ny=66, Lx=4.0, Ly=4.0, dx=0.0625, dy=0.0625)
  )
)
Grid: Nx=66, Ny=66
Spacing: dx=0.0625, dy=0.0625

2. Initial condition

A 2D Gaussian centred at (2.0,2.0)(2.0, 2.0):

State shape: u=(66, 66)
Peak value: 1.0000

3. Run the simulation

Solution shape: (500, 66, 66)

4. Visualize spreading

<Figure size 1600x450 with 6 Axes>

5. Analytical comparison

The exact solution for a Gaussian initial condition under 2D diffusion is:

uexact(x,y,t)=σ02σ(t)2exp⁡ ⁣[−(x−μx)2+(y−μy)22 σ(t)2],σ(t)=σ02+2νtu_{\text{exact}}(x, y, t) = \frac{\sigma_0^2}{\sigma(t)^2} \exp\!\left[ -\frac{(x - \mu_x)^2 + (y - \mu_y)^2}{2\,\sigma(t)^2} \right], \qquad \sigma(t) = \sqrt{\sigma_0^2 + 2\nu t}
<Figure size 1600x450 with 6 Axes>
Max absolute error: 0.001290

6. Energy decay

Diffusion dissipates energy. The total energy E(t)=12∑u2 dx dyE(t) = \tfrac{1}{2}\sum u^2\,dx\,dy should decrease monotonically.

<Figure size 800x400 with 1 Axes>
Energy: initial = 0.141372, final = 0.067127
Ratio: 0.4748

7. Gradient through the diffusion coefficient

We can compute ∂L/∂ν\partial \mathcal{L} / \partial \nu to answer: “how does the total energy at final time change if we increase the diffusion coefficient?”

--- Gradient w.r.t. diffusion coefficient ---
  dL/d(nu) = -332.448364
  (negative => increasing nu decreases total energy, as expected)

8. Peak decay over time

The analytical peak decays as σ02/(σ02+2νt)\sigma_0^2 / (\sigma_0^2 + 2\nu t). We compare this prediction against the numerical peak.

<Figure size 800x400 with 1 Axes>

Summary

ConceptAPI
Create modelDiffusion2D.create(nx=64, ny=64, Lx=4.0, Ly=4.0, nu=0.05)
StateDiffusion2DState(u=u0)
Analytical solutionσ(t)=σ02+2νt\sigma(t) = \sqrt{\sigma_0^2 + 2\nu t}
Energy dissipationmonotonic decay of ∑u2\sum u^2
Grad w.r.t. ν\nueqx.filter_grad(loss)(model)

Next: Step 8 combines nonlinear convection and diffusion into the 2D Burgers equation.