Crank-Nicolson Finite Difference Scheme Implementation for Convection-Diffusion Equation

Resource Overview

Implementation of Crank-Nicolson finite difference scheme for solving convection-diffusion equations with code structure and algorithmic details

Detailed Documentation

The convection-diffusion equation is widely applied in fluid dynamics, environmental science, and other fields to describe material transport processes under convection and diffusion effects. The Crank-Nicolson scheme is an unconditionally stable implicit difference method that combines advantages of forward and backward Euler schemes, achieving second-order accuracy.

Implementation Approach Equation Discretization: The time derivative in the convection-diffusion equation uses central differencing, while spatial derivatives employ second-order central differencing to form a semi-discrete scheme. Time step and spatial step selection must satisfy stability conditions - in code implementation, this typically involves calculating the Courant and Peclet numbers to ensure numerical stability.

Linear System Construction: The resulting algebraic equations after discretization form a tridiagonal system, which can be efficiently solved using the Thomas algorithm (TDMA). The solution at each time level depends on both current and previous time step values, reflecting the implicit scheme characteristic. Code implementation involves constructing coefficient matrices with convection and diffusion terms properly weighted at time levels n and n+1.

Boundary Treatment: Dirichlet or Neumann boundary conditions are set according to practical problems, typically implemented by modifying matrix coefficients and right-hand side terms. In programming, boundary values are incorporated by adjusting the first and last rows of the coefficient matrix and corresponding elements in the solution vector.

Advantage Analysis Unconditional Stability: Allows larger time steps compared to explicit schemes, significantly improving computational efficiency in long-time simulations. Balanced Accuracy: Second-order truncation error in time direction avoids excessive diffusion problems common in pure implicit schemes, maintaining better solution sharpness.

Application Extensions The method can be extended to multidimensional problems (such as ADI method) or coupled with other physical processes (like reaction terms). In practical programming, attention should be paid to the effect of grid Peclet number on numerical oscillations - when Peclet number exceeds critical values, upwind scheme modifications may be necessary to stabilize the solution. The code structure typically includes separate functions for matrix assembly, Thomas algorithm solver, and boundary condition implementation for modular development.