|
| 1 | +"""Test verification for code examples in the book. |
| 2 | +
|
| 3 | +This module verifies that code examples shown in the book: |
| 4 | +1. Actually run without errors |
| 5 | +2. Produce correct results |
| 6 | +3. Follow Devito best practices |
| 7 | +
|
| 8 | +Per CONTRIBUTING.md: All results must be reproducible with fixed random seeds, |
| 9 | +version-pinned dependencies, and automated tests validating examples. |
| 10 | +""" |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import pytest |
| 14 | + |
| 15 | +try: |
| 16 | + from devito import Constant, Eq, Grid, Operator, TimeFunction, solve |
| 17 | + DEVITO_AVAILABLE = True |
| 18 | +except ImportError: |
| 19 | + DEVITO_AVAILABLE = False |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.devito |
| 23 | +class TestIndexQmdWaveEquation: |
| 24 | + """Test the wave equation code shown in index.qmd (the landing page).""" |
| 25 | + |
| 26 | + def test_symbolic_pde_form_needs_solve(self): |
| 27 | + """Test that Eq(u.dt2, c**2 * u.dx2) needs solve() for correct behavior. |
| 28 | +
|
| 29 | + The code in index.qmd line 39 uses: |
| 30 | + eq = Eq(u.dt2, c**2 * u.dx2) |
| 31 | +
|
| 32 | + This is the symbolic PDE form. Devito needs an explicit update |
| 33 | + equation, so we should use solve() to derive the stencil: |
| 34 | + pde = Eq(u.dt2, c**2 * u.dx2) |
| 35 | + update = Eq(u.forward, solve(pde, u.forward)) |
| 36 | + """ |
| 37 | + if not DEVITO_AVAILABLE: |
| 38 | + pytest.skip("Devito not available") |
| 39 | + |
| 40 | + grid = Grid(shape=(101,), extent=(1.0,)) |
| 41 | + u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2) |
| 42 | + c = 1.0 |
| 43 | + |
| 44 | + # Set initial condition (Gaussian pulse) |
| 45 | + x_coords = np.linspace(0, 1.0, 101) |
| 46 | + u.data[0, :] = np.exp(-((x_coords - 0.5) ** 2) / (2 * 0.1**2)) |
| 47 | + u.data[1, :] = u.data[0, :] |
| 48 | + |
| 49 | + # Method 1: Original code from index.qmd (symbolic PDE form) |
| 50 | + # This may not work correctly as it's not an explicit update |
| 51 | + eq_symbolic = Eq(u.dt2, c**2 * u.dx2) |
| 52 | + |
| 53 | + # Method 2: Correct approach using solve() |
| 54 | + dt_const = Constant(name='dt') |
| 55 | + pde = Eq(u.dt2, c**2 * u.dx2) |
| 56 | + update = Eq(u.forward, solve(pde, u.forward)) |
| 57 | + |
| 58 | + # Both should create operators, but behavior differs |
| 59 | + try: |
| 60 | + op_symbolic = Operator([eq_symbolic]) |
| 61 | + # This test documents that the symbolic form may not work as expected |
| 62 | + # The operator might be created but not produce a useful update |
| 63 | + except Exception as e: |
| 64 | + pytest.fail(f"Symbolic PDE form failed to create operator: {e}") |
| 65 | + |
| 66 | + op_correct = Operator([update]) |
| 67 | + |
| 68 | + # Run the correct version and verify it works |
| 69 | + u.data[0, :] = np.exp(-((x_coords - 0.5) ** 2) / (2 * 0.1**2)) |
| 70 | + u.data[1, :] = u.data[0, :] |
| 71 | + op_correct.apply(time_M=10, dt=0.001) |
| 72 | + |
| 73 | + # Solution should still be bounded (not exploding) |
| 74 | + assert np.max(np.abs(u.data[0, :])) < 10.0, "Solution exploded" |
| 75 | + |
| 76 | + def test_correct_wave_equation_pattern(self): |
| 77 | + """Test the recommended pattern for wave equation in Devito. |
| 78 | +
|
| 79 | + This is the pattern that should be used in index.qmd: |
| 80 | + pde = Eq(u.dt2, c**2 * u.dx2) |
| 81 | + update = Eq(u.forward, solve(pde, u.forward)) |
| 82 | + op = Operator([update]) |
| 83 | + """ |
| 84 | + if not DEVITO_AVAILABLE: |
| 85 | + pytest.skip("Devito not available") |
| 86 | + |
| 87 | + # Problem setup |
| 88 | + L = 1.0 |
| 89 | + Nx = 100 |
| 90 | + c = 1.0 |
| 91 | + C = 0.5 # Courant number |
| 92 | + |
| 93 | + dx = L / Nx |
| 94 | + dt = C * dx / c |
| 95 | + Nt = 100 |
| 96 | + |
| 97 | + grid = Grid(shape=(Nx + 1,), extent=(L,)) |
| 98 | + u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2) |
| 99 | + |
| 100 | + # Initial condition: Gaussian pulse |
| 101 | + x_coords = np.linspace(0, L, Nx + 1) |
| 102 | + u.data[0, :] = np.exp(-((x_coords - 0.5 * L) ** 2) / (2 * 0.1**2)) |
| 103 | + u.data[1, :] = u.data[0, :] |
| 104 | + |
| 105 | + # Correct pattern: Define PDE symbolically, then solve for u.forward |
| 106 | + c_const = Constant(name='c') |
| 107 | + pde = Eq(u.dt2, c_const**2 * u.dx2) |
| 108 | + update = Eq(u.forward, solve(pde, u.forward)) |
| 109 | + |
| 110 | + op = Operator([update]) |
| 111 | + op.apply(time_M=Nt, dt=dt, c=c) |
| 112 | + |
| 113 | + # Verify solution is reasonable |
| 114 | + max_val = np.max(np.abs(u.data[0, :])) |
| 115 | + assert max_val < 2.0, f"Solution amplitude {max_val} too large" |
| 116 | + assert max_val > 0.01, f"Solution amplitude {max_val} too small" |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.devito |
| 120 | +class TestWhatIsDevitoQmdDiffusion: |
| 121 | + """Test the diffusion code shown in what_is_devito.qmd.""" |
| 122 | + |
| 123 | + def test_diffusion_missing_variables(self): |
| 124 | + """Test that the diffusion code in what_is_devito.qmd has issues. |
| 125 | +
|
| 126 | + The code at lines 51-69 uses `alpha` and `dt` without defining them: |
| 127 | + eq = Eq(u.forward, u + alpha * dt * u.dx2) |
| 128 | +
|
| 129 | + This test verifies the corrected version works. |
| 130 | + """ |
| 131 | + if not DEVITO_AVAILABLE: |
| 132 | + pytest.skip("Devito not available") |
| 133 | + |
| 134 | + # Create computational grid |
| 135 | + grid = Grid(shape=(101,), extent=(1.0,)) |
| 136 | + |
| 137 | + # Define the unknown field |
| 138 | + u = TimeFunction(name='u', grid=grid, time_order=1, space_order=2) |
| 139 | + |
| 140 | + # Set initial condition |
| 141 | + u.data[0, 50] = 1.0 |
| 142 | + |
| 143 | + # CORRECTED: Define alpha and dt properly |
| 144 | + alpha = 1.0 # diffusion coefficient |
| 145 | + Nx = 100 |
| 146 | + L = 1.0 |
| 147 | + dx = L / Nx |
| 148 | + F = 0.5 # Fourier number for stability |
| 149 | + dt = F * dx**2 / alpha # Time step from stability |
| 150 | + |
| 151 | + # Define the PDE update equation (CORRECTED) |
| 152 | + eq = Eq(u.forward, u + alpha * dt * u.dx2) |
| 153 | + |
| 154 | + # Create and run the operator |
| 155 | + op = Operator([eq]) |
| 156 | + op(time=100, dt=dt) |
| 157 | + |
| 158 | + # Verify solution is bounded and diffusing |
| 159 | + max_val = np.max(u.data[0, :]) |
| 160 | + assert max_val < 1.0, "Diffusion should reduce peak" |
| 161 | + assert max_val > 0.0, "Solution should not go to zero immediately" |
| 162 | + |
| 163 | + def test_diffusion_with_solve_pattern(self): |
| 164 | + """Test the recommended pattern using solve() for diffusion. |
| 165 | +
|
| 166 | + Better approach: |
| 167 | + a_const = Constant(name='a') |
| 168 | + pde = u.dt - a_const * u.dx2 |
| 169 | + stencil = Eq(u.forward, solve(pde, u.forward)) |
| 170 | + """ |
| 171 | + if not DEVITO_AVAILABLE: |
| 172 | + pytest.skip("Devito not available") |
| 173 | + |
| 174 | + # Parameters |
| 175 | + L = 1.0 |
| 176 | + Nx = 100 |
| 177 | + alpha = 1.0 |
| 178 | + F = 0.5 |
| 179 | + |
| 180 | + dx = L / Nx |
| 181 | + dt = F * dx**2 / alpha |
| 182 | + |
| 183 | + grid = Grid(shape=(Nx + 1,), extent=(L,)) |
| 184 | + u = TimeFunction(name='u', grid=grid, time_order=1, space_order=2) |
| 185 | + |
| 186 | + # Initial condition: sinusoidal |
| 187 | + x_coords = np.linspace(0, L, Nx + 1) |
| 188 | + u.data[0, :] = np.sin(np.pi * x_coords / L) |
| 189 | + |
| 190 | + # Use solve() pattern |
| 191 | + a_const = Constant(name='a') |
| 192 | + pde = u.dt - a_const * u.dx2 |
| 193 | + stencil = Eq(u.forward, solve(pde, u.forward)) |
| 194 | + |
| 195 | + # Boundary conditions |
| 196 | + bc_left = Eq(u[grid.stepping_dim + 1, 0], 0) |
| 197 | + bc_right = Eq(u[grid.stepping_dim + 1, Nx], 0) |
| 198 | + |
| 199 | + op = Operator([stencil, bc_left, bc_right]) |
| 200 | + |
| 201 | + # Run for a short time |
| 202 | + T = 0.1 |
| 203 | + Nt = int(T / dt) |
| 204 | + for _ in range(Nt): |
| 205 | + op.apply(time_m=0, time_M=0, dt=dt, a=alpha) |
| 206 | + u.data[0, :] = u.data[1, :] |
| 207 | + |
| 208 | + # Verify against exact solution |
| 209 | + exact = np.exp(-alpha * (np.pi / L) ** 2 * T) * np.sin(np.pi * x_coords / L) |
| 210 | + error = np.max(np.abs(u.data[0, :] - exact)) |
| 211 | + |
| 212 | + assert error < 0.01, f"Error {error:.4f} too large" |
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.devito |
| 216 | +class TestFirstPdeQmdWaveEquation: |
| 217 | + """Test the wave equation code shown in first_pde.qmd.""" |
| 218 | + |
| 219 | + def test_first_pde_manual_stencil_correct(self): |
| 220 | + """Verify the manual stencil form in first_pde.qmd works. |
| 221 | +
|
| 222 | + The code uses the manually derived stencil: |
| 223 | + eq = Eq(u.forward, 2*u - u.backward + (c*dt)**2 * u.dx2) |
| 224 | +
|
| 225 | + This is correct because it explicitly defines u.forward. |
| 226 | + """ |
| 227 | + if not DEVITO_AVAILABLE: |
| 228 | + pytest.skip("Devito not available") |
| 229 | + |
| 230 | + # Problem parameters |
| 231 | + L = 1.0 |
| 232 | + c = 1.0 |
| 233 | + T = 1.0 |
| 234 | + Nx = 100 |
| 235 | + C = 0.5 # Courant number |
| 236 | + |
| 237 | + # Derived parameters |
| 238 | + dx = L / Nx |
| 239 | + dt = C * dx / c |
| 240 | + Nt = int(T / dt) |
| 241 | + |
| 242 | + # Create the computational grid |
| 243 | + grid = Grid(shape=(Nx + 1,), extent=(L,)) |
| 244 | + |
| 245 | + # Create a time-varying field |
| 246 | + u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2) |
| 247 | + |
| 248 | + # Set initial condition: a Gaussian pulse |
| 249 | + x_coord = 0.5 * L |
| 250 | + sigma = 0.1 |
| 251 | + x_coords = np.linspace(0, L, Nx + 1) |
| 252 | + u.data[0, :] = np.exp(-((x_coords - x_coord) ** 2) / (2 * sigma**2)) |
| 253 | + u.data[1, :] = u.data[0, :] # Zero initial velocity |
| 254 | + |
| 255 | + # Define the update equation (manual stencil form) |
| 256 | + eq = Eq(u.forward, 2 * u - u.backward + (c * dt) ** 2 * u.dx2) |
| 257 | + |
| 258 | + # Create the operator |
| 259 | + op = Operator([eq]) |
| 260 | + |
| 261 | + # Run the simulation |
| 262 | + op(time=Nt, dt=dt) |
| 263 | + |
| 264 | + # Verify solution is reasonable |
| 265 | + max_val = np.max(np.abs(u.data[0, :])) |
| 266 | + assert max_val < 2.0, f"Solution amplitude {max_val} too large" |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.devito |
| 270 | +class TestDiffu1DDevitoQmd: |
| 271 | + """Test the diffusion code shown in diffu1D_devito.qmd.""" |
| 272 | + |
| 273 | + def test_diffu1d_devito_dt_defined(self): |
| 274 | + """Verify dt is properly defined before use in the diffusion example. |
| 275 | +
|
| 276 | + The code should define dt from the Fourier number: |
| 277 | + dt = F * dx**2 / a |
| 278 | + """ |
| 279 | + if not DEVITO_AVAILABLE: |
| 280 | + pytest.skip("Devito not available") |
| 281 | + |
| 282 | + # Domain and discretization |
| 283 | + L = 1.0 |
| 284 | + Nx = 100 |
| 285 | + a = 1.0 # Diffusion coefficient |
| 286 | + F = 0.5 # Fourier number |
| 287 | + |
| 288 | + dx = L / Nx |
| 289 | + dt = F * dx**2 / a # Time step from stability condition |
| 290 | + |
| 291 | + # Verify dt is defined and reasonable |
| 292 | + assert dt > 0, "dt must be positive" |
| 293 | + assert dt < 1.0, "dt seems too large" |
| 294 | + |
| 295 | + # Create Devito grid |
| 296 | + grid = Grid(shape=(Nx + 1,), extent=(L,)) |
| 297 | + u = TimeFunction(name='u', grid=grid, time_order=1, space_order=2) |
| 298 | + |
| 299 | + # Set initial condition |
| 300 | + x_coords = np.linspace(0, L, Nx + 1) |
| 301 | + u.data[0, :] = np.sin(np.pi * x_coords) |
| 302 | + |
| 303 | + # Using solve() pattern |
| 304 | + a_const = Constant(name='a_const') |
| 305 | + pde = u.dt - a_const * u.dx2 |
| 306 | + stencil = Eq(u.forward, solve(pde, u.forward)) |
| 307 | + |
| 308 | + op = Operator([stencil]) |
| 309 | + |
| 310 | + # Run one time step to verify operator works |
| 311 | + op.apply(time_m=0, time_M=0, dt=dt, a_const=a) |
| 312 | + |
| 313 | + # Solution should be bounded |
| 314 | + assert np.all(np.isfinite(u.data[1, :])), "Solution contains NaN/Inf" |
| 315 | + |
| 316 | + |
| 317 | +if __name__ == "__main__": |
| 318 | + pytest.main([__file__, "-v"]) |
0 commit comments