2D Convection-Diffusion CFD Solver in Python
As part of my CFD development work, I built a 2D convection-diffusion solver from scratch in Python. The main aim was to better understand how the governing equations are discretised and solved numerically, using a structured grid, custom boundary conditions and a Gauss-Seidel iterative solver to calculate and visualise the resulting temperature field.

Project write-up
As part of my CFD development work, I built a 2D convection-diffusion solver from scratch in Python. The main aim was to better understand what actually happens behind commercial CFD software, from discretising the governing equation and assembling the coefficient matrix to solving the system iteratively and post-processing the final temperature field.
The solver uses a structured Cartesian mesh over a 0.3 m × 0.3 m domain, with a default cell size of 0.01 m, giving a total of 900 cells. Boundary conditions are assigned directly around the domain, with the left boundary set to 353 K and the remaining boundaries set to 273 K. Flow velocity and direction can also be changed, allowing the effect of convection on the temperature field to be investigated.
For each cell, I calculate the contribution from its left, right, top and bottom neighbours and use these values to build the global system of equations. The convection and diffusion terms are included directly in the coefficients, while boundary cells contribute to the source vector instead.
I also wrote my own Gauss-Seidel iterative solver rather than relying entirely on an existing numerical library. After every iteration, the residual is calculated and normalised against its initial value, making it possible to monitor how the solution converges.
Finally, the solved values are reshaped back into the 2D domain and plotted as a temperature contour using Matplotlib. This made the project useful not only for learning the numerical side of CFD, but also for understanding how mesh indexing, boundary conditions, matrix assembly, convergence and post-processing all connect together inside a basic CFD solver.
There is still plenty that could be developed further, particularly moving towards sparse matrix storage, more efficient iterative methods, different discretisation schemes and eventually solving more complete fluid-flow equations.
Download the Python source↓Images

