cds2.pde
Partial differential equations: heat and wave equations in 1D/2D.
Accelerated PDE solvers — numpy-vectorised FTCS and leapfrog schemes.
Port of :mod:cds.pde (zero-dependency, pure-Python loops) to the
NumPy/SciPy stack. The mathematics is unchanged; only the execution is
accelerated and the 2-D extensions are added:
Heat equation u_t = alpha * u_xx (parabolic, 1-D and 2-D)
Forward-Time Central-Space (FTCS). In 1-D every interior point is
blended with its two neighbours each step::
u[i] <- u[i] + r * (u[i-1] - 2*u[i] + u[i+1]), r = alpha*dt/dx**2
In 2-D the 5-point Laplacian is used::
u_new[1:-1,1:-1] <- u[1:-1,1:-1]
+ r_x*(u[1:-1,2:] - 2*u[1:-1,1:-1] + u[1:-1,:-2])
+ r_y*(u[2:,1:-1] - 2*u[1:-1,1:-1] + u[:-2,1:-1])
with ``r_x = alpha*dt/dx**2``, ``r_y = alpha*dt/dy**2``,
stable iff ``r_x + r_y <= 0.5`` (``r <= 0.5`` in 1-D). When ``dt``
is omitted the solver picks ``dt = 0.9 * dx**2 / (2*alpha)`` in 1-D
and ``dt = 0.9 * 0.5 / (alpha*(1/dx**2 + 1/dy**2))`` in 2-D — the
stability limit shrunk by a 10 % safety margin.
Wave equation u_tt = c**2 * u_xx (hyperbolic, 1-D and 2-D)
Explicit central differences in space and time (three-level leapfrog)::
u_new[i] <- 2*u[i] - u_old[i] + C**2 * (u[i-1] - 2*u[i] + u[i+1])
with Courant number ``C = c*dt/dx`` (1-D) or
``C = c*dt*sqrt(1/dx**2 + 1/dy**2)`` (2-D), subject to ``C <= 1``.
The first step is seeded from the initial velocity ``v0`` with the
matching second-order Taylor expansion
``u(dt) = u0 + dt*v0 + dt**2/2 * c**2 * u_xx``. When ``dt`` is
omitted the solver picks ``dt = 0.9*dx/c`` (1-D) or
``dt = 0.9/(c*sqrt(1/dx**2+1/dy**2))`` (2-D).
Both families march a whole number of uniform steps,
n_steps = ceil(t_final / dt), so the simulated horizon
n_steps * dt matches t_final up to one step. Boundary handling:
"dirichlet" pins border values at their initial levels;
"neumann" mirrors the adjacent interior point onto each border after
every step (zero-flux, insulated). Wave solvers are Dirichlet-pinned
at u0 edges to match the v1 contract. All failures surface as
lowercase :class:ValueError (or :class:TypeError for non-callables).
Vectorisation: every spatial update is a single NumPy slice assignment, so the cost per step is one array operation rather than a Python loop over grid points. Inputs are copied, never mutated.
HeatResult
dataclass
Outcome of a 1-D heat-equation integration.
Attributes:
u_final: temperature profile after the last step, shape (nx,).
dt: time step actually used (auto-chosen or caller-supplied).
n_steps: number of uniform FTCS steps taken; the simulated
horizon is n_steps * dt.
Source code in src/cds2/pde.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
WaveResult
dataclass
Outcome of a 1-D wave-equation integration.
Attributes:
u_final: displacement profile after the last step, shape (nx,).
dt: time step actually used.
n_steps: number of uniform leapfrog steps taken.
Source code in src/cds2/pde.py
98 99 100 101 102 103 104 105 106 107 108 109 110 | |
Heat2DResult
dataclass
Outcome of a 2-D heat-equation integration.
Attributes:
u_final: temperature field after the last step, shape
(ny, nx) (rows = y, cols = x).
dt: time step actually used.
n_steps: number of uniform steps taken.
dx: grid spacing in x.
dy: grid spacing in y.
Source code in src/cds2/pde.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
Wave2DResult
dataclass
Outcome of a 2-D wave-equation integration.
Attributes:
u_final: displacement field after the last step, shape
(ny, nx).
dt: time step actually used.
n_steps: number of uniform leapfrog steps taken.
dx: grid spacing in x.
dy: grid spacing in y.
Source code in src/cds2/pde.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
heat_equation_1d(u0, alpha, length, t_final, nx, *, boundary='dirichlet', dt=None)
Integrate the 1-D heat equation with vectorised FTCS.
Marches n_steps = ceil(t_final / dt) uniform applications of the
three-point stencil u += r*(left - 2*u + right) with
r = alpha*dt/dx**2. When dt is omitted it defaults to
0.9*dx**2/(2*alpha) — the FTCS stability limit
alpha*dt/dx**2 <= 0.5 shrunk by 10 % (r = 0.45).
Args:
u0: initial temperatures, one per grid point (array-like, shape
(nx,)), ordered left to right.
alpha: thermal diffusivity, must be positive.
length: domain length; grid spacing is length/(nx-1).
t_final: target simulation horizon.
nx: number of grid points, at least 3.
boundary: "dirichlet" pins ends at u0[0]/u0[-1];
"neumann" mirrors the neighbouring interior value onto
each end (zero flux).
dt: optional forced time step. Must satisfy
alpha*dt/dx**2 <= 0.5 or the call is rejected.
Returns:
A :class:HeatResult holding the final profile, the time step
used and the number of steps taken. The returned array is a copy.
Raises:
ValueError: if len(u0) != nx, nx < 3, alpha <= 0,
length <= 0, t_final <= 0, boundary is not
"dirichlet" or "neumann", dt is not strictly
positive, or dt breaks the stability condition.
Source code in src/cds2/pde.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
wave_equation_1d(u0, v0, c, length, t_final, nx, *, dt=None)
Integrate the 1-D wave equation with vectorised leapfrog.
Uses the three-level stencil
u_new = 2*u - u_old + C**2*(left - 2*u + right) with
C = c*dt/dx, seeded by the second-order Taylor step
u(dt) = u0 + dt*v0 + dt**2/2 * c**2 * u_xx. When dt is
omitted it defaults to 0.9*dx/c — the CFL limit c*dt/dx <= 1
shrunk by 10 %. Ends are Dirichlet-pinned at u0[0]/u0[-1].
Args:
u0: initial displacements, shape (nx,).
v0: initial velocities aligned with u0, shape (nx,).
c: wave speed, must be positive.
length: domain length; spacing length/(nx-1).
t_final: target simulation horizon.
nx: number of grid points, at least 3.
dt: optional forced time step. Must satisfy c*dt/dx <= 1.
Returns:
A :class:WaveResult.
Raises:
ValueError: if shapes mismatch, nx < 3, c <= 0,
length <= 0, t_final <= 0, dt not positive,
or dt breaks the CFL condition.
Source code in src/cds2/pde.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
heat_equation_2d(u0, alpha, lx, ly, t_final, nx, ny, *, boundary='dirichlet', dt=None)
Integrate the 2-D heat equation u_t = alpha*(u_xx+u_yy) with FTCS.
Uses the 5-point Laplacian on a uniform nx * ny grid covering
[0, lx] x [0, ly] with spacings dx = lx/(nx-1),
dy = ly/(ny-1). Stability requires
r_x + r_y <= 0.5 where r_x = alpha*dt/dx**2,
r_y = alpha*dt/dy**2. Auto dt is
0.9*0.5/(alpha*(1/dx**2+1/dy**2)) (r_x+r_y = 0.45).
Args:
u0: initial temperatures, shape (ny, nx) (rows = y, cols = x).
alpha: thermal diffusivity, must be positive.
lx: domain length in x.
ly: domain length in y.
t_final: target horizon.
nx: grid points in x, at least 3.
ny: grid points in y, at least 3.
boundary: "dirichlet" pins every border value at its initial
level; "neumann" mirrors the adjacent interior row/col
onto each border after every step (zero flux).
dt: optional forced step; must satisfy r_x+r_y <= 0.5.
Returns:
A :class:Heat2DResult with field shape (ny, nx).
Raises:
ValueError: on shape mismatch, non-positive geometry, unknown
boundary, non-positive dt, or stability violation.
Source code in src/cds2/pde.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
wave_equation_2d(u0, v0, c, lx, ly, t_final, nx, ny, *, dt=None)
Integrate the 2-D wave equation u_tt = c**2*(u_xx+u_yy) with leapfrog.
Explicit second-order central differences in space and time on a
uniform nx * ny grid. With Courant numbers C_x = c*dt/dx,
C_y = c*dt/dy the scheme is
u_new = 2*u - u_old + C_x**2*stencil_x + C_y**2*stencil_y
where stencil_x and stencil_y are the 1-D three-point
curvatures. CFL requires
C = c*dt*sqrt(1/dx**2 + 1/dy**2) = sqrt(C_x**2 + C_y**2) <= 1.
Auto dt is 0.9/(c*sqrt(1/dx**2+1/dy**2)).
Borders are Dirichlet-pinned at u0 values (as in the 1-D v1
solver).
Args:
u0: initial displacements, shape (ny, nx).
v0: initial velocities, same shape as u0.
c: wave speed, must be positive.
lx: domain length in x.
ly: domain length in y.
t_final: target horizon.
nx: grid points in x, at least 3.
ny: grid points in y, at least 3.
dt: optional forced step; must satisfy CFL C <= 1.
Returns:
A :class:Wave2DResult.
Raises:
ValueError: on shape mismatch, non-positive geometry, c <= 0,
non-positive dt, or CFL violation.
Source code in src/cds2/pde.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | |