Module 3: Discretization from Scratch
Learn how continuous governing equations become numerical equations. Explore Taylor series, finite-difference approximations, truncation error, spatial discretization, and the foundations of building a CFD solver from scratch.
In Module 1, we explored the mathematical language of CFD. In Module 2, we derived the governing equations from conservation of mass, momentum, and energy. We now face the central challenge of computational fluid dynamics: how can a computer solve equations that describe continuous physical fields?
Consider the temperature distribution inside a metal plate. Mathematically, temperature is a continuous function of position and time, . In a numerical simulation, however, we cannot store and calculate the temperature at infinitely many points.
Instead, we represent the physical domain using a finite collection of points or cells. We then approximate the derivatives and integrals in the governing equations using values associated with this discrete representation.
This process is called discretization. It is the bridge between the mathematical description of physics and the numerical algorithms implemented in a CFD solver.
Whether we are developing a simple finite-difference program in C++ or using a professional package such as OpenFOAM, the fundamental idea remains the same: replace continuous mathematical operations with carefully constructed numerical approximations.
Learning Objectives
By the end of this module, you should be able to:
- Explain why discretization is necessary in CFD.
- Understand computational grids and discrete field values.
- Use the Taylor series to derive finite-difference formulas.
- Approximate first and second derivatives numerically.
- Understand truncation error and the order of accuracy.
- Distinguish finite-difference and finite-volume discretization.
- Recognize the importance of grid spacing and boundary treatment.
- Implement basic derivative approximations in a programming language.
1. Why Do We Need Discretization?
The governing equations derived in Module 2 contain derivatives of continuous fields. For example, consider the one-dimensional steady heat-conduction equation without internal heat generation:
The equation states that the second spatial derivative of temperature is zero. Its solution describes a linear temperature distribution under the corresponding boundary conditions.
A mathematical solution can describe temperature at every point along the domain. A computer, however, must work with a finite number of numerical values.
We therefore divide the domain into discrete locations and approximate the derivatives using neighbouring values.
For example, instead of representing temperature everywhere along a plate, we might store:
Each value represents the temperature at a specific computational location.
The continuous function is now represented by a finite set of values. The differential equation can be approximated using relationships between these values.
A Simple Analogy
Measuring a curved road — Imagine trying to describe the shape of a curved road using only a few measurements. With measurements taken at widely spaced locations, small changes in curvature may be missed. Adding more measurement points gives a more detailed representation, although the quality also depends on how the measurements are used.
A computational mesh serves a similar purpose. It provides the locations or cells where the numerical solution is represented.
However, adding more points does not automatically guarantee an accurate solution. The discretization method, boundary conditions, numerical errors, and physical model also matter.
Continuous Versus Discrete Representation
| Continuous description | Discrete representation |
|---|---|
| Temperature is a function | Temperature is stored as values |
| Derivatives describe local changes | Derivatives are approximated using numerical formulas |
| The domain contains infinitely many locations | The computational representation contains finitely many points or cells |
| Governing equations are differential equations | Discretization produces algebraic equations or discrete update formulas |
Discretization transforms a continuous mathematical problem into a form that can be processed numerically.
2. Constructing a One-Dimensional Computational Grid
Before approximating derivatives, we need to define where the solution values are located.
Consider a one-dimensional domain extending from to . We divide it into equal intervals.
The grid spacing is:
The grid locations are:
Here, is the grid-point index.
For example, if the domain length is and we use intervals, then:
The grid points are located at:
We can store a field such as temperature in an array:
In C++, this could be represented using a vector:
std::vector<double> T(N + 1);
The array stores the numerical values, while the grid definition tells us where those values belong.
Grid Points and Grid Cells
It is important to distinguish between grid points and grid cells.
In a simple finite-difference method, unknowns are commonly associated with grid points. In a finite-volume method, unknowns are commonly associated with control volumes, and fluxes are evaluated across their faces.
These approaches are related but not identical. The choice affects how derivatives, boundary conditions, and conservation laws are represented.
For the first examples in this module, we will use a uniform one-dimensional grid to derive finite-difference formulas.
3. The Taylor Series: The Foundation of Finite Differences
The Taylor series allows us to approximate a function at a nearby location using its value and derivatives at a known location.
For a sufficiently smooth function , the Taylor expansion around the point is:
Here:
- is the function value at the current location.
- is the first derivative.
- is the second derivative.
- is the distance to the neighbouring location.
The expansion tells us how the function changes as we move away from the original point.
In numerical methods, we use this relationship in reverse: we know the function values at discrete locations and use them to estimate the derivatives.
Expanding in the Opposite Direction
The Taylor expansion toward the left is:
Notice that the signs of the odd-order derivative terms change.
Having expansions in both directions allows us to combine neighbouring values to construct more accurate derivative approximations.
This is one of the most important ideas in finite-difference methods.
4. Deriving the First Derivative
The first derivative describes how rapidly a field changes with position.
For example, the temperature gradient indicates how quickly temperature varies along a material.
We can approximate the derivative in several ways, depending on which neighbouring values are available.
4.1 Forward Difference
Start with the Taylor expansion toward the right:
Rearranging:
If we neglect the higher-order terms, we obtain the forward-difference approximation:
On a computational grid, this becomes:
The approximation uses the current value and the value immediately to its right.
Its leading truncation error is proportional to , so it is a first-order accurate approximation.
4.2 Backward Difference
Using the Taylor expansion toward the left:
Rearranging and neglecting higher-order terms gives:
This is the backward-difference approximation. Like the forward difference, it is first-order accurate.
Forward and backward differences are useful when information is available primarily from one side of a point. They also appear in numerical transport methods, where the choice of direction can influence stability and numerical diffusion.
4.3 Central Difference
We can obtain a more accurate approximation by combining the Taylor expansions on both sides:
Subtract the second equation from the first:
Divide by :
Neglecting the higher-order terms gives the central-difference formula:
The leading error is proportional to . Therefore, the central difference is second-order accurate for a sufficiently smooth function on a uniform grid.
Comparing the First-Derivative Approximations
| Method | Approximation | Order of accuracy |
|---|---|---|
| Forward difference | First order | |
| Backward difference | First order | |
| Central difference | Second order |
For smooth fields on a uniform grid, the central difference generally gives a more accurate derivative for the same grid spacing. However, it is not automatically the best choice for every CFD equation. The appropriate method depends on the equation, boundary conditions, stability requirements, and transport behaviour.
5. Deriving the Second Derivative
The second derivative measures the change in the first derivative. In heat transfer, it appears in the diffusion equation and describes how temperature curvature drives conductive heat transfer.
To derive a numerical approximation, add the two Taylor expansions:
Adding these expressions cancels the odd-order derivative terms:
Rearrange to isolate the second derivative:
Neglecting higher-order terms gives:
This is the standard central-difference approximation for the second derivative on a uniform grid. Its leading truncation error is proportional to , making it second-order accurate for a sufficiently smooth function.
Why the Second Derivative Matters in CFD
Consider the one-dimensional heat-diffusion equation with constant thermal diffusivity and no internal heat generation:
Using the central-difference approximation for the spatial derivative gives:
The continuous differential equation now contains a numerical expression involving three neighbouring grid values.
This is a key step toward a numerical solver. The spatial derivative has been replaced by an algebraic expression that a computer can evaluate.
We have not yet chosen how to approximate the time derivative. That choice determines how the solution will advance in time, which we will explore in a later module.
6. Understanding Truncation Error and Order of Accuracy
Every finite-difference formula approximates a mathematical derivative. The difference between the exact derivative and the approximation is called the discretization error. When we derive a formula by truncating an infinite Taylor series, the neglected terms describe its truncation error.
For the central-difference first derivative:
For the central-difference second derivative:
The notation means that the leading error decreases proportionally to the square of the grid spacing, assuming sufficient smoothness and that the asymptotic error behaviour applies.
What Does Second-Order Accuracy Mean?
Suppose a numerical method has an error approximately proportional to :
If we halve the grid spacing, then:
The leading error from that approximation becomes roughly one-quarter of its previous value.
For a first-order method, where , halving the grid spacing approximately halves the leading error instead.
This gives us a way to reason about the benefits of mesh refinement.
However, the error in an entire CFD simulation does not always follow the order of a single derivative approximation. Boundary treatment, iterative convergence, nonlinearities, mesh quality, and other error sources can influence the observed behaviour.
Truncation Error Is Not the Only Numerical Error
Several types of error can affect a CFD calculation:
- Truncation error: Introduced by approximating continuous mathematical operations.
- Round-off error: Arises from finite-precision arithmetic.
- Iterative error: Occurs when an algebraic system or nonlinear problem is not solved to sufficient convergence.
- Implementation error: Results from mistakes in the numerical algorithm or its code.
- Modelling error: Arises when the physical model or its assumptions do not fully represent the real system.
A fine mesh can reduce some discretization errors, but it cannot correct an incorrect governing equation, unsuitable physical model, or programming mistake.
7. Discretization of a Simple Diffusion Equation
We can now combine the ideas developed so far into a basic numerical formulation.
Consider the steady one-dimensional diffusion equation:
Assume a uniform grid with spacing . At an interior grid point , approximate the second derivative using the central-difference formula:
Multiply both sides by :
Rearranging:
This result tells us that, at an interior point of this simple steady diffusion problem, the discrete temperature equals the average of its two neighbouring values.
The equation is no longer a differential equation. It is an algebraic relationship between three discrete unknowns or values.
Applying Boundary Conditions
Suppose the temperature at the two ends of a plate is prescribed:
The boundary values are known, and the interior equations connect the unknown temperatures between them.
For a uniform grid, the exact solution of this particular problem is linear:
The central-difference approximation reproduces a linear temperature distribution exactly in exact arithmetic when the boundary values are applied consistently. This makes the problem a useful first verification test for a numerical implementation.
From Local Equations to a System of Equations
For multiple interior points, each discrete equation connects neighbouring unknowns. Collecting all these equations produces a linear algebraic system:
Here:
- is the coefficient matrix.
- is the vector of unknown temperatures.
- contains contributions from prescribed boundary values and other known terms.
This is the general direction in which many CFD discretizations lead: continuous governing equations become a finite system of algebraic equations.
The structure of the matrix depends on the equation, discretization method, mesh, and boundary conditions. For this one-dimensional diffusion problem, each interior equation couples only to its immediate neighbours, giving a sparse, tridiagonal structure.
8. Finite Difference Versus Finite Volume
Finite differences are a useful way to learn numerical approximation because their formulas can be derived directly from Taylor series. However, many engineering CFD solvers use the finite-volume method, which starts from the integral form of conservation laws.
Understanding the distinction is important before building more complex solvers.
Finite-Difference Method
Finite differences approximate derivatives at discrete locations.
For example:
The method is often straightforward to implement on structured grids, especially for simple geometries and introductory problems.
Finite-Volume Method
Finite volume starts by integrating a governing equation over a control volume. Fluxes through the faces of that volume determine how conserved quantities change.
For a steady one-dimensional conservation equation without sources:
Integrating over a cell from its west face to its east face gives:
Therefore:
The net flux leaving the cell must equal zero when there is no accumulation or source.
Here, and are the fluxes at the east and west faces.
In more general transport equations, the balance includes accumulation, sources, and possibly several flux contributions. The finite-volume method is built around maintaining these discrete balances.
| Feature | Finite difference | Finite volume |
|---|---|---|
| Starting point | Approximation of derivatives | Integral conservation over a control volume |
| Typical solution location | Grid points | Cell centres or other defined locations |
| Main numerical operation | Difference formulas | Face-flux evaluation and cell balance |
| Conservation | Depends on formulation | Naturally expresses local integral conservation when fluxes are shared consistently |
| Common applications | Structured grids, introductory methods, many specialized solvers | Widely used in industrial CFD, including complex flow problems |
Neither method is universally best for every problem. Both require suitable discretization choices, boundary conditions, and numerical analysis.
For our longer-term goal of understanding and implementing a CFD solver similar in structure to OpenFOAM, finite volume will become particularly important. The finite-difference derivations in this module provide the mathematical foundation for understanding how derivatives and numerical errors are handled.
9. What Happens at the Boundaries?
Most central-difference formulas use values on both sides of a grid point. At the first and last points of a one-dimensional domain, one of those neighbouring values lies outside the domain.
For example, the central-difference approximation at point would require :
But is outside the defined grid.
A numerical method must therefore handle boundary locations differently. Common approaches include:
- Using a one-sided finite-difference formula.
- Applying the prescribed boundary value directly.
- Introducing ghost cells outside the physical domain.
- Constructing a boundary-specific discretization consistent with the governing equation and boundary condition.
The correct choice depends on the numerical method and the physical boundary condition.
For example, a prescribed temperature boundary condition specifies the temperature itself, whereas a prescribed heat flux condition constrains the temperature gradient through Fourier’s law.
Boundary treatment is not merely a coding detail. It is part of the mathematical definition of the numerical problem.
10. Implementing a Derivative Approximation in C++
The mathematical formulas can now be translated into a small program. The following example calculates the first derivative of a known function using a central-difference approximation.
We will use:
Its exact derivative is:
At , the exact derivative is .
The central-difference approximation is:
A simple C++ implementation is:
#include <cmath>
#include <iomanip>
#include <iostream>
double f(double x)
{
return x * x;
}
double centralDifference(double x, double dx)
{
return (f(x + dx) - f(x - dx)) / (2.0 * dx);
}
int main()
{
const double x = 2.0;
const double dx = 0.1;
const double exact = 2.0 * x;
const double numerical = centralDifference(x, dx);
const double error = std::abs(numerical - exact);
std::cout << std::setprecision(10);
std::cout << "Location: " << x << '\n';
std::cout << "Grid spacing: " << dx << '\n';
std::cout << "Exact derivative: " << exact << '\n';
std::cout << "Numerical derivative: " << numerical << '\n';
std::cout << "Absolute error: " << error << '\n';
return 0;
}
For this particular function, the central-difference formula returns the exact derivative in exact arithmetic. This happens because the Taylor expansion of a quadratic function has no nonzero derivatives above the second order, so the odd-order terms that contribute to the central-difference error vanish.
That makes a useful test of the implementation, but it does not demonstrate the general second-order convergence rate.
To investigate truncation error, try a cubic function:
The central-difference approximation for this function has a nonzero leading error proportional to .
You can then run the program with progressively smaller values of dx and compare the numerical derivative with the exact derivative.
Compile and Run
Save the program as derivative.cpp. If a C++ compiler is installed, compile it using:
g++ -std=c++20 -O2 -Wall -Wextra -pedantic derivative.cpp -o derivative
Run it with:
./derivative
On Windows, the executable can be run as:
.\derivative.exe
This small program is not a CFD solver yet. It is a numerical experiment that connects a mathematical approximation to an executable algorithm.
The same development process will be useful as we progress toward solving diffusion and transport equations on computational grids.
11. From a Derivative Formula to a CFD Solver
A derivative approximation is one building block of a numerical solver. A complete CFD calculation requires several additional components.
The general development path is:
- Define the physical problem and governing equations.
- Choose a computational domain and discretization.
- Specify initial and boundary conditions.
- Construct discrete equations for the unknown fields.
- Assemble or evaluate the resulting algebraic system.
- Solve the system or advance the solution in time.
- Check convergence, conservation, and numerical accuracy.
- Compare the results against analytical solutions, established benchmarks, or experimental data where appropriate.
For example, the steady diffusion equation can be discretized into a linear algebraic system. A convection–diffusion equation adds transport by fluid motion, which requires additional decisions about how convective fluxes are approximated. The incompressible Navier–Stokes equations introduce further challenges, including nonlinear momentum transport and pressure–velocity coupling.
These are not entirely separate ideas. They build on the same fundamental task we performed in this module: converting continuous mathematical operations into discrete numerical relationships.
As we move toward finite-volume methods, we will focus increasingly on fluxes, conservation, matrix systems, and the design of a maintainable C++ solver.
Key Takeaways
- Discretization converts continuous governing equations into numerical relationships that a computer can evaluate.
- A computational grid defines the locations or cells where the numerical solution is represented.
- Taylor series provides a systematic way to derive finite-difference approximations.
- Forward and backward differences are first-order accurate, while standard central differences are second-order accurate for sufficiently smooth functions on a uniform grid.
- Truncation error describes the effect of neglected terms in a numerical approximation.
- Halving the grid spacing reduces the leading error by approximately a factor of four for a second-order method, when the expected convergence regime applies.
- Boundary points require appropriate treatment because standard central differences may need values outside the physical domain.
- Finite differences approximate derivatives, while finite volume constructs discrete conservation balances over cells.
- A derivative approximation is a building block; a CFD solver must also handle governing equations, boundary conditions, algebraic systems, and verification.
What’s Next?
In Module 4: Algebraic Systems and Numerical Solvers, we will investigate what happens after discretization produces a set of equations. We will explore coefficient matrices, unknown vectors, linear systems, iterative solution methods, and the role of convergence criteria.
These ideas will help us understand how numerical CFD codes turn discrete equations into computed fields—and prepare us to implement the algebraic components of a CFD solver in C++.