Course: Materials Simulation Practical | FAU Erlangen-Nürnberg
Tools: Python · NumPy · Matplotlib
Finite-difference formulation of the steady-state anisotropic diffusion equation in 2D. The notebook covers the full derivation from Fick's second law, analytical transformation to the isotropic Laplace form, second-order discretisation on structured grids, and assembly of the global linear system with block-tridiagonal sparsity. Seven test problems are solved under varying boundary configurations and grid resolutions, with convergence validated against an analytical Fourier-series reference solution.
Starting from Fick's second law, the stationary anisotropic diffusion equation is reduced to a generalised Laplace equation by introducing the anisotropy ratio
A coordinate rescaling
The domain is discretised on a uniform
Dirichlet conditions (prescribed concentration) are imposed on the top and bottom boundaries. Neumann conditions (prescribed flux) are imposed on the left and right boundaries via ghost-node elimination, modifying the corresponding rows of
Three baseline cases are solved on the coarse 5×5 grid to verify the solver under different Dirichlet configurations. Test 1 uses symmetric equal-valued top and bottom boundaries, producing a spatially uniform field. Test 2 applies uniform but asymmetric Dirichlet values, recovering the expected horizontal-band gradient. Test 3 assigns randomised per-node boundary values, confirming solver generality under non-uniform conditions.
![]() |
![]() |
![]() |
| Test 1 — symmetric Dirichlet BCs, uniform field (5×5) | Test 2 — asymmetric Dirichlet BCs, horizontal gradient (5×5) | Test 3 — randomised per-node Dirichlet boundary values (5×5) |
An analytical solution is derived for mixed boundary conditions — zero-flux Neumann on the left and right, Dirichlet on top and bottom. The numerical and analytical concentration fields are compared directly on the 5×5 grid, quantifying the discretisation error introduced by the coarse resolution.
![]() |
![]() |
| Numerical solution — mixed Neumann/Dirichlet BCs (5×5) | Analytical solution — same boundary configuration |
The relative L2 error between the numerical and analytical solutions is computed as a function of grid resolution (
![]() |
| Relative L2 error vs. grid resolution — monotonic convergence from N=5 to N=75 |
When lateral fluxes vanish (
![]() |
![]() |
| Test 6 — 1D linear reduction replicated in 2D | Test 7 — 75×75 grid, randomised mixed boundary conditions |
A full Fourier sine-series solution is derived and evaluated for the isotropic Laplace equation with four-sided Dirichlet conditions, providing a high-resolution reference at 100×100 independent of the finite difference solver. The numerical solution at the same resolution is compared directly against this reference.
![]() |
![]() |
| Analytical Fourier series solution — four-sided Dirichlet BCs (100×100) | Numerical FDM solution — same configuration (100×100) |
pip install numpy matplotlibOpen diffusion_pde_fdm.ipynb in Jupyter and run all cells sequentially. No external dataset is required — all grids and boundary conditions are generated within the notebook. Grid resolution and boundary values are defined at the top of each test problem section and can be modified directly.









