Interactive CFD Book

Modern CFD: From Theory to Code

An interactive platform bridging fluid dynamics, AI, and High-Performance Computing.

  • Micro-Projects: Complex solvers broken down into interactive, bite-sized codes.
  • Just-In-Time Math: Calculus and Tensor algebra introduced exactly when needed.
  • Modern Stack: Implementation using C++, CUDA, MPI, and PyTorch.

Key Features

  • Micro-Projects: Complex solvers broken down into interactive, bite-sized codes.
  • Just-In-Time Math: Calculus and Tensor algebra introduced exactly when needed.
  • Modern Stack: Implementation using C++, CUDA, MPI, and PyTorch.

Roadmap Highlights

  • Core CFD: FDM/FVM, Navier-Stokes, Stability (CFL).
  • AI Meets CFD: Physics-Informed Neural Networks (PINNs).
  • Advanced HPC: GPU Computing and Discontinuous Galerkin (DG).

Phase 0 – Foundations

Micro-Project 1: Discretizing a Domain for Heat Transfer

Objective

Before solving transport equations like the Heat Equation or Navier–Stokes, the continuous domain must be transformed into a finite set of computational nodes. This discretization determines resolution, stability, accuracy, and computational cost.

Mathematical Formulation (1D)

For a simple 1D rod spanning from $x_{start}$ to $x_{end}$ with $N$ total nodes, the uniform grid spacing (step size) is defined as:

$$ \Delta x = \frac{x_{end} - x_{start}}{N - 1} $$

The coordinate of any specific node $i$ (where $i = 0, 1, ..., N-1$) is:

$$ x_i = x_{start} + i \cdot \Delta x $$

The continuous temperature field $T(x, t)$ is then approximated by an array of discrete values $T_i^n$. Python typically uses NumPy for prototyping this, while production CFD solvers rely on C++ for memory performance.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 0 – Foundations

Micro-Project 2: Numerical Differentiation & Truncation Errors

Objective

To evaluate spatial derivatives on a discrete mesh using Finite Difference schemes derived from Taylor Series expansions, and to critically analyze their corresponding truncation errors (order of accuracy).

1. Forward Difference (First-Order)

Derived directly from a forward Taylor series expansion, this scheme computes the gradient by looking ahead to node $i+1$. It is primarily used at Dirichlet inlet boundaries:

$$ \left. \frac{dT}{dx} \right|_i = \frac{T_{i+1} - T_i}{\Delta x} + \mathcal{O}(\Delta x) $$

2. Backward Difference (First-Order)

Conversely, for outflow boundaries (e.g., $i=N-1$), information cannot be drawn from outside the domain. We utilize a backward expansion:

$$ \left. \frac{dT}{dx} \right|_i = \frac{T_i - T_{i-1}}{\Delta x} + \mathcal{O}(\Delta x) $$

3. Central Difference (Second-Order)

Subtracting the backward Taylor expansion from the forward expansion eliminates the first-order error terms, yielding a symmetrically balanced and highly accurate gradient approximation for interior nodes:

$$ \left. \frac{dT}{dx} \right|_i = \frac{T_{i+1} - T_{i-1}}{2 \Delta x} + \mathcal{O}(\Delta x^2) $$

💻 Interactive Implementation

Python Execution Results & Chart
>_ Console Output...

Phase 0 – Foundations

Micro-Project 3: The Second Derivative & Discrete Laplacian

Objective

Transitioning from first-order gradients, we evaluate the second spatial derivative (1D Laplacian). In transport phenomena, governed by Fourier's Law, this term represents the curvature of the scalar field and acts as the fundamental thermodynamic driving force for diffusion: $$\frac{\partial T}{\partial t} = \alpha \frac{\partial^2 T}{\partial x^2}$$

Finite Difference Approximation

By combining forward and backward Taylor series expansions, we derive the second-order accurate $\mathcal{O}(\Delta x^2)$ central difference scheme for an interior node $i$:

$$\left.\frac{d^2T}{dx^2}\right|_{i} = \frac{T_{i+1} - 2T_i + T_{i-1}}{\Delta x^2} + \mathcal{O}(\Delta x^2)$$

This stencil utilizes symmetrically distributed nodal information, making it naturally isotropic—an essential property for simulating diffusion processes objectively.

Interpretation of Curvature

The term $\frac{d^2T}{dx^2}$ dictates the concavity of the temperature field, determining the net divergence of the thermal energy flux:

  • Positive Curvature ($\cup$): The local node is colder than its surroundings. Heat diffuses inward (sink).
  • Negative Curvature ($\cap$): The local node is hotter than its surroundings. Heat diffuses outward (source).
  • Zero Curvature (Linear): Steady-state condition. The net heat flux gradient is zero.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 0 – Foundations

Micro-Project 4: The Heat Equation & Transient Evolution

Objective

Transitioning from steady spatial properties to resolving time-dependent partial differential equations (PDEs). Here, we introduce the Forward Time Central Space (FTCS) scheme to model heat diffusion and investigate the critical constraint of Numerical Stability.

Theory 1: Explicit Time Marching

By discretizing the 1D Heat Equation using a forward difference in time and a central difference in space, the future thermodynamic state is explicitly predicted by the current state and the local curvature over a discrete time step $\Delta t$.

$$T^{n+1}_i = T^n_i + \alpha \Delta t \left( \frac{T^n_{i+1} - 2T^n_i + T^n_{i-1}}{\Delta x^2} \right)$$

Theory 2: Fourier Number & Stability

Explicit temporal schemes are strictly conditionally stable. If the time step exceeds the physical diffusion rate, truncation errors amplify exponentially. This boundary is defined by the dimensionless Fourier Number ($\lambda$):

$$\lambda = \alpha \frac{\Delta t}{\Delta x^2} \le 0.5 \implies \Delta t \le \frac{\Delta x^2}{2\alpha}$$
[+] Deep Dive: Von Neumann Stability Analysis

According to Von Neumann linear stability analysis, the amplification factor $G$ for the FTCS scheme must satisfy $|G| \le 1$. This mathematically strictly requires $\lambda \le 1/2$ to prevent non-physical negative coefficients, which manifest as unbounded, high-frequency spatial oscillations (numerical blow-up).

💻 Interactive Implementation

Task: Run the stable configuration first. Then, deliberately set dt = 10.0 in the Python tab to observe numerical explosion.

Output
>_ Console Output...

Phase 2 – CFD Basics

1D Convection & Upwind Schemes

Objective

To analyze the hyperbolic nature of the 1D linear convection equation, its spatial discretization, and the critical role of characteristic lines (information propagation) in formulating stable Upwind schemes.

Theory: 1. The Hyperbolic Convection Equation

The governing equation for 1D linear convection represents a pure wave transport phenomenon, given by:

$$ \frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = 0 $$

where $c$ is the constant wave propagation speed. Mathematically, this is a hyperbolic Partial Differential Equation (PDE) where information strictly travels along characteristic lines defined by $dx/dt = c$.

Theory: 2. Discretization & Causality

For $c > 0$, the physical information travels from left to right. Discretization schemes must respect this causality. The Forward Time, Backward Space (FTBS) method achieves this by utilizing the upstream node ($i-1$) for spatial derivatives, yielding the Upwind Scheme:

$$ u_i^{n+1} = u_i^n - c \frac{\Delta t}{\Delta x}(u_i^n - u_{i-1}^n) $$

Applying Forward Space (FTFS) for $c > 0$ samples data from outside the physical domain of dependence, triggering unconditional numerical instability.

Theory: 3. The Courant-Friedrichs-Lewy (CFL) Condition

Stability for explicit solvers like FTBS is governed by the CFL condition, which mandates that the numerical domain of dependence must encompass the physical one:

$$ C = \frac{c \Delta t}{\Delta x} \le 1 $$

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Convection–Diffusion & Grid Péclet Number

Objective

To systematically evaluate the interplay between convective and diffusive fluxes, and to dynamically select bounded discretization schemes based on the dimensionless Grid Péclet number in 1D.

Theory: 1. The Péclet Number ($Pe$)

The Grid Péclet number ($Pe$) quantifies the ratio of advective transport rate to diffusive transport rate at the mesh resolution level:

$$Pe = \frac{\text{Convection}}{\text{Diffusion}} = \frac{\rho c \Delta x}{\Gamma}$$

where $c$ is the local fluid velocity, $\Delta x$ is the grid cell width, and $\Gamma$ is the dynamic diffusion coefficient.

Theory: 2. Discretization Switching

Numerical stability in Navier-Stokes solvers relies strictly on local bounding via $Pe$ analysis:

  • $Pe \le 2$: Diffusion is sufficient to smear sharp gradients. Central Differencing Scheme (CDS) is stable, offering 2nd-order spatial accuracy without generating non-physical wiggles.
  • $Pe > 2$: Convection dominates. Using CDS violates the macroscopic transportive property. The Upwind Differencing Scheme (UDS) is required to maintain boundness, albeit at the cost of introducing 1st-order numerical diffusion (artificial viscosity).

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Iterative Solvers & Matrix Linearization

Objective

To establish the mathematical necessity of iterative algorithms (like Gauss-Seidel, CG, or GMRES) over direct inversions in managing the immense, sparse matrices generated by linearized Navier-Stokes equations.

Theory: 1. Non-Linearity & Discretized Systems

The convective acceleration term ($\mathbf{u} \cdot \nabla \mathbf{u}$) inherently non-linearizes fluid equations. Picard or Newton linearization strategies are employed to reduce the system locally to the form:

$$[A][\phi]=[b]$$

where $[A]$ is a massive, highly sparse, and ideally diagonally dominant coefficient matrix.

Theory: 2. Algorithmic Complexity

Industrial meshes contain millions of cells. Direct solvers (e.g., LU decomposition) require memory and operations scaling proportionally to $O(N^3)$, making them computationally prohibitive. Iterative algorithms, heavily utilized in OpenFOAM or Fluent, approach the residual tolerance iteratively, drastically reducing memory overhead to roughly $O(N)$ for sparse formats.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Information Propagation & Multi-Dimensional Upwinding

Objective

To map the Upwind formulation across a 1D characteristic domain and rigorously associate fluid inlet/outlet boundaries with Riemann information states.

Theory: 1. Universal Upwind Tensor Logic

In Cartesian 1D flows, convection discretization is straightforward. Spatial derivatives are computed strictly from the upstream direction relative to the node:

  • If $u>0$, discretize $\frac{\partial\phi}{\partial x}$ using FTBS ($x_{i}-x_{i-1}$).
  • If $u<0$< /span>, discretize $\frac{\partial\phi}{\partial x}$ using FTFS ($x_{i+1}-x_{i}$).

Theory: 2. The Physical Contradiction of Downwinding

Numerically sampling downstream ($i+1$) when $c>0$ attempts to extract transport characteristics from a region the wave has mathematically not yet interacted with. This causality breach manifests explicitly in the discrete matrix as negative diagonal coefficients, fundamentally guaranteeing exponential numerical explosion.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

2.5 The Golden Rule & Branchless Dynamic Upwinding

Objective

To decouple spatial discretization physics from algebraic solvers, and to implement Riemann-solver-like directional routing using mathematically branchless formulations to maximize CPU/GPU instruction throughput.

Theory: Discretization vs. Linear Algebra

A fundamental paradigm in high-performance CFD is isolating the Discretization (mapping PDEs to algebraic forms) from the Solver (Krylov subspace methods, Multigrid). The physics—such as capturing entropy-satisfying shocks and ensuring boundedness—arise purely from the spatial discretization (e.g., Upwinding). The linear solver merely resolves the resulting matrix $Ax=b$.

Theory: Branchless Upwinding Architecture

In transient simulations, local velocity vector $\mathbf{u}$ fluctuates continuously. Implementing spatial conditionality via if/else statements severely degrades performance by causing Branch Prediction Failures and preventing SIMD Vectorization on modern hardware. Production solvers isolate directional fluxes algebraically:

$$u^+ = \max(u, 0) \quad \text{and} \quad u^- = \min(u, 0)$$

This ensures the convective flux inherently biases towards the upstream characteristic direction without interrupting the computational pipeline.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

2.6 Linearization, Stability & Under-Relaxation (URF)

Objective

To control the spectral radius of the iteration matrix in highly non-linear Navier-Stokes systems, ensuring monotonic convergence through Picard iteration and explicit Under-Relaxation.

Theory: The Linearization Paradigm

The convective acceleration term $(\mathbf{u} \cdot \nabla \mathbf{u})$ renders the momentum equations inherently non-linear. Direct inversion is mathematically impossible. Solvers employ Picard Linearization (lagging the convective velocity) or Newton-Raphson methods, resulting in sequential iterations predicting future fields based on current local states.

Theory: Under-Relaxation Factor (URF)

Unrestricted updates in highly coupled non-linear systems often overshoot the solution manifold, leading to unbound numerical divergence. The Under-Relaxation Factor ($\alpha$) limits the step size in the phase space:

$$ \phi^{n+1} = \phi^n + \alpha \Delta\phi_{calc} = \alpha \phi_{calc} + (1 - \alpha)\phi^n $$

Implicit solvers integrate this directly into the matrix diagonals, enhancing diagonal dominance and thereby ensuring the convergence of iterative algebraic solvers (like AMG or BiCGStab).

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

2.7 The Upwind Flux Formula

Objective

To resolve the non-linear convection paradox using lagged field approximations, and introduce the absolute-value algebraic formulation utilized in High-Resolution Schemes.

Theory: The Convection Paradox

In the non-linear convective term, the velocity acts simultaneously as the transporting field and the transported scalar. Determining the upwind footprint requires knowing flow direction a priori. Solvers resolve this via Time-Lagging: evaluating the convective flux using the advecting velocity from the previous iteration step.

Theory: The Absolute Value Formulation

Rather than conditional branching, enterprise C++ codes calculate the convective numerical flux interface states by decomposing velocities into strictly non-negative and non-positive vectors:

$$u = \frac{1}{2}(u + |u|) + \frac{1}{2}(u - |u|)$$

This algebraic splitting inherently assigns appropriate numerical weights to upstream nodal values, providing the foundation for more advanced Total Variation Diminishing (TVD) flux limiters.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

2.8 Governing Equations, Temporal Schemes & Mesh Metrics

Objective

To contextualize the full Navier-Stokes tensor formulation, establish temporal integration strategies (Steady vs Transient), and quantify unstructured mesh geometric metrics (e.g., cell skewness).

Theory: The Tensor Formulation

The conservation of momentum is dictated by the Navier-Stokes system. In compact tensor notation, capturing temporal evolution, convective momentum flux, pressure gradients, and viscous dissipation:

$$\frac{\partial (\rho u_i)}{\partial t} + \frac{\partial (\rho u_i u_j)}{\partial x_j} = -\frac{\partial p}{\partial x_i} + \frac{\partial \tau_{ij}}{\partial x_j} + S_i$$

Theory: Temporal vs. Steady Iteration

Steady-State methods drop the $\partial / \partial t$ term, functioning as pseudo-time marching algorithms focused entirely on achieving asymptotic zero residuals. Transient methods preserve the time derivative, demanding strict CFL conformance or implicit dual-time stepping for physical accuracy.

Theory: Orthogonality & Mesh Skewness

Finite Volume integration accuracy hinges on mesh topology. Skewness quantifies the vector deviation between the face area vector and the cell-center connecting vector. Severe non-orthogonality necessitates deferred correction terms in the gradient calculations to prevent divergence.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Computational Core

2.8 Direct vs. Iterative Solvers: The Role of TDMA

Objective

To elucidate why production-grade CFD solvers rely heavily on iterative methods (Krylov subspace, Multigrid) for large-scale systems, while identifying specific topological regimes—such as 1D domains—where direct algorithms like the Thomas Algorithm (TDMA) remain computationally dominant.

Theoretical Framework

Direct matrix inversion algorithms are often prohibitive in commercial CFD for three primary reasons:

  • The Non-Linearity Bottleneck: The convective acceleration term in the Navier-Stokes equations creates a non-linear algebraic system. Linearization techniques (Picard or Newton-Raphson) yield:
    $$ A(x^n) \cdot x^{n+1} = b $$
    Since the coefficient matrix $A$ depends on the current solution state $n$, direct inversion at every time-step or outer iteration is computationally unfeasible.
  • Pressure-Velocity Coupling: In incompressible flows, pressure lacks an independent evolution equation. Algorithms like SIMPLE or PISO circumvent this by guessing a pressure field, updating velocities, and solving a Poisson equation for pressure correction—an inherently iterative process.
  • Memory and Sparse Topologies: A typical 3D mesh generates a massive, highly sparse matrix. Direct solvers scale poorly ($O(N^3)$ complexity), whereas iterative solvers (like BiCGSTAB or GMRES) leverage sparse matrix-vector multiplications ($O(N)$) to achieve rapid convergence with minimal memory footprint.

⚡ The Exception: Thomas Algorithm (TDMA)

When the spatial discretization yields a Tridiagonal Matrix (standard in 1D schemes or implicit directional sweeps), the Thomas Algorithm calculates the exact algebraic solution directly. With a linear algorithmic complexity of $O(N)$, TDMA operates without convergence thresholds or outer iteration loops, establishing itself as the gold standard for 1D numerical diffusion and advection-diffusion formulations.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

2.9 The Pressure-Velocity Dilemma & The SIMPLE Algorithm

Objective

To understand the fundamental mathematical decoupling in incompressible flows (often formulated as a saddle-point problem) and how the SIMPLE algorithm utilizes a predictor-corrector approach to enforce mass conservation via the Pressure Poisson Equation.

1. The Compressibility Factor

The mathematical nature of the Navier-Stokes equations dictates entirely different solution strategies based on the flow regime:

  • Compressible Flows: Density ($\rho$) is a primary variable. The Thermodynamic Equation of State ($p = \rho R T$) provides an explicit algebraic linkage between pressure, density, and temperature.
  • Incompressible Flows: Density is strictly constant ($\rho = C$). Pressure loses its thermodynamic meaning and becomes purely a kinematic variable—a dynamic Lagrange multiplier whose sole physical purpose is to adjust the velocity field to satisfy the divergence-free constraint ($\nabla \cdot \vec{V} = 0$).

2. The Decoupling Challenge

In incompressible regimes, the momentum equations yield velocity components ($u, v, w$), but the continuity equation lacks an explicit pressure term ($p$):

$$ \nabla \cdot \vec{V} = 0 $$

This absence means we cannot form a direct linear system $A \cdot p = b$. This decoupling is the primary numerical hurdle in incompressible CFD solver design.

3. The SIMPLE Framework (Patankar & Spalding, 1972)

SIMPLE (Semi-Implicit Method for Pressure Linked Equations) resolves this by iterative coupling:

  1. Predictor: Solve momentum equations using a guessed pressure field ($p^*$) to yield an intermediate, non-conservative velocity field ($\vec{V}^*$).
  2. Divergence Error: Evaluate the mass residual $\nabla \cdot \vec{V}^* \neq 0$.
  3. Corrector Formulation: Define true fields as $\vec{V} = \vec{V}^* + \vec{V}'$ and $p = p^* + p'$. Substituting these into the continuity equation derives the Pressure Poisson Equation (PPE):
$$ \nabla^2 p' = \frac{\rho}{\Delta t} (\nabla \cdot \vec{V}^*) $$

Solving the PPE yields the pressure correction ($p'$), which is then used to update both pressure and velocity fields, driving the mass residual toward zero.

[+] Deep Dive: Production Solver Quirks
  1. Gauge Pressure (Fluent): Computes $P_{solver} = P_{abs} - P_{operating}$ to mitigate catastrophic cancellation and floating-point errors.
  2. Kinematic Pressure (OpenFOAM): Normalizes pressure by constant density, computing $p_k = p / \rho$ (Units: $m^2/s^2$).
  3. Hydrostatic Correction: Subtracts hydrostatic head dynamically: $p^* = p - \rho \vec{g} \cdot \vec{r}$.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Marching: Explicit vs. Implicit & The Transient Reality

Objective

To evaluate temporal discretization strategies for non-linear conservation laws in 1D. We contrast the restrictive stability limits of explicit formulations with the unconditionally stable, yet computationally intensive, implicit formulations, ultimately introducing the industry-standard "Dual Time Stepping" architecture.

1. Explicit Integration

Explicit schemes compute the state at $t^{n+1}$ utilizing solely the known information at $t^n$.

$$ \mathbf{U}^{n+1} = \mathcal{L}_{exp}(\mathbf{U}^n) $$

Dynamics: Highly efficient per time step and easily parallelizable. However, they are mathematically constrained by the Courant-Friedrichs-Lewy (CFL) condition. If $\Delta t$ exceeds the critical limit dictated by the spectral radius of the system, exponential error growth occurs.

2. Implicit Formulation

Implicit schemes couple the future state of a node with the unknown future states of its neighbors, requiring the solution of a global algebraic system.

$$ \mathbf{F}(\mathbf{U}^{n+1}) = 0 \implies \mathbf{J} \Delta \mathbf{U} = -\mathbf{R}(\mathbf{U}^n) $$

Dynamics: Grants unconditional linear stability (CFL $\gg 1$). However, solving the Jacobian matrix ($\mathbf{J}$) at each step is memory-intensive and computationally heavy.

3. The Reality: Dual Time Stepping

For fully transient, non-linear Navier-Stokes equations, pressure-velocity coupling and non-linearities cannot be solved in a single implicit leap. Solvers employ a nested integration strategy introducing a fictitious "pseudo-time" ($\tau$):

  • Outer Loop (Physical Time, $t$): Advances the physical state from $t^n$ to $t^{n+1}$.
  • Inner Loop (Pseudo-Time, $\tau$): Inside each $\Delta t$, the solver drives the non-linear residuals to zero (e.g., $10^{-5}$) using sub-iterations. The physical time only advances once this inner pseudo-steady state is reached.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Practical CFD Workflow: Initialization & Diagnostics

Objective

Beyond governing equations, robust numerical simulation requires rigorous initialization and convergence diagnostics. A critical skill is evaluating whether a flow field admits a steady-state solution or inherently exhibits transient dynamics (e.g., vortex shedding, instabilities).

1. Domain Initialization ($t=0$)

Iterative solvers require an initial mathematical guess. Higher quality initializations drastically reduce required CPU cycles:

  • Quiescent State: Domain initialized to zero velocity ($u = 0, p = 0$).
  • Freestream Initialization: Propagating inlet boundary conditions uniformly across the domain.
  • Potential Flow Initialization: Solving a fast, inviscid Laplace equation ($\nabla^2 \phi = 0$) to generate a physically consistent initial velocity field.

2. Steady vs. Transient Diagnostics

Standard industrial practice dictates starting with a Steady-State formulation (neglecting the $\frac{\partial}{\partial t}$ term). Convergence is assessed via:

  • Norm Residuals: $L_2$ or $L_\infty$ norms of the discretized algebraic system should monotonically decrease (typically below $10^{-4}$).
  • Point Monitors: Velocity or pressure probes in critical regions (e.g., wakes) must asymptote to a constant value.
Identifying Inherent Unsteadiness: If residuals stagnate (e.g., hover around $10^{-2}$) and point monitors exhibit sustained sinusoidal oscillations, the fundamental physics dictates a transient nature (e.g., von Kármán vortex street).

3. Solver Transition

Upon diagnosing transient behavior:

  1. Transition to a time-marching scheme (URANS or LES).
  2. Utilize the pseudo-steady scalar fields as a highly accelerated initial condition.
  3. Ensure the Courant–Friedrichs–Lewy (CFL) number remains bounded ($\text{CFL} \le 1$) for temporal accuracy.

💻 Interactive Implementation

Tip: This script simulates the iterative diagnostic process. Notice how the flow diverges from a steady trajectory after iteration 50.

Output
>_ Console Output...

Phase 2 – CFD Basics

The Startup Effect: Washout Time & Numerical Transients

Objective

To robustly separate mathematical transients (error propagation from initial guesses) from physical transients, quantify the characteristic Washout Time, and establish rigorous criteria for statistical averaging in 1D unsteady simulations.

1. Kinematic Wave Propagation & Artifacts

When a transient solver initializes, the internal nodes contain an arbitrary guess. As the solver marches in time, the boundary conditions (the "ground truth") propagate through the domain. The immediate gradients formed between the boundary and the internal guess generate Numerical Transients—mathematically valid per the discretized equations, but physically fictitious.

2. Convective Washout Time

Physical validity is achieved only when the characteristic waves from the inlet have entirely traversed the domain, flushing out the initial guess. This period is the Convective Flow-through Time:

$$ \tau_{washout} \approx \frac{L}{U_{bulk}} $$

Post-washout, the domain's state is entirely dictated by the continuous imposition of boundary conditions and genuine internal source terms.

3. Industrial Standard Practice

URANS/LES Workflow: In high-fidelity simulations (e.g., flow over a cylinder), practitioners simulate for 2 to 3 convective times ($\tau$) and strictly discard this data. Statistical sampling (mean, RMS) commences only after reaching a Statistically Stationary State.

[⚠️] The Exception: Truly Unsteady Physics

In cases like a Dam Break or Shock Tube (Riemann problem), the initial discontinuity at $t=0$ is the exact physical reality. Here, the initial condition is not a "guess", and every discrete time step captures true, unrepeatable physical evolution. No data is discarded.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Math of Time Marching: Indices, Stability & The Explicit vs. Implicit Battle

Objective

To systematically decouple temporal advancement (time marching) from algebraic root-finding (iteration). Understanding the interplay between the time index ($n$) and iteration index ($k$) is fundamental for evaluating the stability and computational economics of Explicit vs. Implicit solvers.

1. The Nomenclature of Indices ($n$ vs. $k$)

A robust CFD framework distinguishes between physical time and numerical convergence:

  • $n$: The previous time step (known historical state vector).
  • $n+1$: The future time step being resolved.
  • $k$: The current iteration within the inner linear solver loop.
  • $k+1$: The updated iteration state. Convergence is achieved when $\Vert u^{(k+1)} - u^{(k)} \Vert \to 0$.

2. The Implicit Framework: Mathematical Anchoring

Incorporating the implicit transient term yields a coupled system. Defining $T = \frac{1}{\Delta t}$ and $C = \frac{u}{\Delta x}$, the nodal algebraic equation becomes:

$$u_i^{(k+1)} = \frac{T u_i^n + C u_{i-1}^{(k+1)}}{T + C}$$

Convergence Mechanism: For finite $\Delta t$, the coefficient $T$ amplifies the principal diagonal of the system matrix, ensuring Diagonal Dominance. This numerical inertia "anchors" the solution to the known state $u_i^n$, granting unconditional stability (A-stable) regardless of the timestep size.

3. The Explicit Framework: Fast but Fragile

Explicit schemes evaluate spatial gradients purely from the historical state ($n$), bypassing matrix inversions entirely:

$$u_i^{(n+1)} = u_i^n \left( 1 - u_i^n \frac{\Delta t}{\Delta x} \right) + u_{i-1}^n \left( u_i^n \frac{\Delta t}{\Delta x} \right)$$

Physical consistency demands non-negative coefficients, imposing the strict Courant–Friedrichs–Lewy (CFL) $\le 1$ condition. Violating this constraint leads to exponential error amplification.

⚖️ Architectural Verdict

Implicit: Industry standard for incompressible/low-Mach flows (e.g., SIMPLE family). The capacity for massive $\Delta t$ heavily offsets the cost of solving $Ax=b$.

Explicit: Optimal for highly transient, compressible shock dynamics where physics strictly dictates microsecond resolution, making implicit iterations redundant.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Economy of CFD: Outer Loops vs. Inner Loops

Objective

To mathematically rationalize why Implicit algorithms, despite imposing heavy algebraic workloads per step, yield vastly superior computational economy for long-duration transient simulations compared to Explicit limits in basic 1D domains.

1. The Anatomy of CFD Chronology

Conflating physical time advancement with matrix iterations is a common architectural flaw. We enforce strict separation:

1. Outer Loop (Physical Time): The macroscopic advancement of reality:
$$t \to t + \Delta t$$
Evaluates unsteadiness and macroscopic domain evolution.
2. Inner Loop (Algebraic Resolution): Freezing physical time to iteratively solve the linearized tensor field:
$$Ax=b$$
via Krylov solvers or Multigrid methods.

2. Algorithmic Cost Function

Simulating 1.0 s of physical phenomena exposes the true cost vector of CFD solvers:

Algorithm Class Optimal $\Delta t$ Total FLOPs Equivalent
Explicit (CFL Constrained)
$$\mathcal{O}(10^{-4})\text{ s}$$
$200,000$
Implicit (Unconditionally Stable)
$$\mathcal{O}(10^{-2})\text{ s}$$
$5,000$
[+] Advanced Profiling: Implicit Acceleration Techniques
  • Warm Starting: Solutions at
    $$t^n$$
    provide an extremely accurate initial guess for
    $$t^{n+1}$$
    .
  • Algebraic Multigrid (AMG): Hierarchical matrix coarsening mathematically damps high-frequency errors, achieving
    $$\mathcal{O}(N)$$
    scaling.
  • Preconditioning: ILU/Jacobi preconditioners drastically reduce condition numbers, minimizing inner Krylov iterations.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Art of Diagnosing Transients in Steady Solvers

Objective

To develop a systematic engineering judgment for distinguishing between true physical transient behavior (e.g., vortex shedding, macro-instabilities) and purely numerical oscillations when a steady-state RANS solver fails to converge monotonically.

Theory: 1. Signs of Transient Physics

If the fluid flow is inherently unsteady, a steady solver (which assumes the following state) will fail to "freeze" the phenomena:

$$ \frac{\partial \phi}{\partial t} = 0 $$

Key macroscopic indicators include:

  • Limit-Cycle Residuals: Global residuals drop to a baseline (e.g., $10^{-3}$) but exhibit high-frequency, repeatable bouncing.
  • Oscillating Probes: Placing virtual probes in high-gradient regions (like wakes or shear layers). Regular harmonic oscillations in scalar fields ($V$, $P$) are strong indicators of unsteadiness.

Theory: 2. The Pre-Futility Checklist

Before abandoning a steady run and paying the computational penalty of URANS/LES, systematically eliminate numerical artifacts:

  • Reduce Under-Relaxation Factors (URFs): Lower the update step size. If the oscillations dampen out, the flow was steady but numerically stiff.
  • Mesh Diagnostics: High local skewness or extreme aspect ratios can create localized artificial instabilities that pollute the global domain.

✅ Verification (The Ultimate Test)

Switch to a Transient solver using a temporally resolved time step (

$$ \Delta t $$
) based on the local Courant number. If the "noise" organizes into a coherent physical structure (e.g., von Kármán street), the diagnosis is robust.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Pillars of Convergence: URF, Skewness, and Time Strategies

Objective

To master the numerical controls that dictate stability in iterative CFD solvers. Understanding Picard iterations prevents violent divergence in highly non-linear Navier-Stokes systems.

Theory: Under-Relaxation Factor (URF)

The discretized Navier-Stokes equations form a non-linear algebraic system. Iterative methods (e.g., SIMPLE algorithm) require implicit or explicit damping to prevent overcorrection during variable updates.

Mathematically, explicit under-relaxation is defined as:

$$\phi^{n+1} = \phi^n + \alpha (\phi^*_{calculated} - \phi^n)$$
  • $\alpha = 1.0$: No relaxation. Prone to mathematical divergence in stiff systems.
  • $\alpha < 1.0$: Dampens the update vector, expanding the spectral radius of stability at the cost of requiring more iterations.

💻 Interactive Implementation

Tip: Observe how an aggressive URF ($\alpha=1.0$) causes the non-linear equation to diverge, while $\alpha=0.5$ stabilizes it.

Output
>_ Console Output...

Phase 2 – CFD Basics

From Physics to Matrix: Discretizing Heat Conduction

Engineering Objective

To trace the mathematical transformation of the 1D Transient Heat Conduction PDE into the discrete linear system $Ax=b$, establishing the algebraic framework required for implicit CFD solvers.

Mathematical Formulation

The governing physics of transient heat diffusion in 1D is dictated by the parabolic PDE:

$$ \frac{\partial T}{\partial t} = \alpha \frac{\partial^2 T}{\partial x^2} $$

Utilizing an Implicit Central Difference scheme, we approximate the derivatives evaluated at the future time level $(n+1)$:

  • Temporal: Forward Euler approximation $O(\Delta t)$.
  • Spatial: Central differencing $O(\Delta x^2)$.

Defining the grid Fourier Number as $r = \frac{\alpha \Delta t}{(\Delta x)^2}$, we assemble the algebraic relations into a matrix format. For a 3-node internal grid (assuming $r=1$, with Dirichlet BCs mapped to the RHS vector $b$), the system $[A]x = b$ is formulated as:

$$ \begin{pmatrix} 3 & -1 & 0 \\ -1 & 3 & -1 \\ 0 & -1 & 3 \end{pmatrix} \begin{pmatrix} T_1^{new} \\ T_2^{new} \\ T_3^{new} \end{pmatrix} = \begin{pmatrix} 100 \\ 0 \\ 0 \end{pmatrix} $$

Numerical Solvability: Notice that the matrix $[A]$ exhibits Strict Diagonal Dominance ($|a_{ii}| > \sum |a_{ij}|$). This critical property guarantees that the spectral radius of the iteration matrix is strictly less than unity, ensuring unconditional stability and monotonic convergence for iterative methods (e.g., Gauss-Seidel, or advanced Krylov subspace methods like GMRES).

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Truncation Error & The Crank-Nicolson Scheme

Objective

To rigorously evaluate numerical Truncation Error via Taylor Series expansion, and implement the 1D Crank-Nicolson formulation to achieve $O(\Delta t^2)$ temporal precision.

Theory: 1. The Origin of Discretization Error

Translating analytical derivatives into discrete algebraic operators inevitably introduces a remainder dictated by Taylor Series truncation. These leading error terms manifest physically:

  • Explicit (Forward Euler): Accuracy $O(\Delta t)$. Susceptible to stability constraints ($r \leq 0.5$).
  • Implicit (Backward Euler): Accuracy $O(\Delta t)$. Unconditionally stable, but large time steps introduce severe numerical diffusion (smearing of gradients) due to the first-order truncation term $\frac{\Delta t}{2} \frac{\partial^2 T}{\partial t^2}$.

Theory: 2. The Crank-Nicolson Paradigm

To construct a purely second-order accurate temporal scheme, Crank-Nicolson evaluates the spatial derivative algebraically at the midpoint $(n + 1/2)$ via the Trapezoidal rule:

$$ \frac{T^{n+1} - T^n}{\Delta t} = \frac{\alpha}{2} \left[ \left(\frac{\partial^2 T}{\partial x^2}\right)^n + \left(\frac{\partial^2 T}{\partial x^2}\right)^{n+1} \right] $$

This 50/50 temporal weighting strategically forces the $O(\Delta t)$ error terms to perfectly cancel out, elevating the scheme to $O(\Delta t^2, \Delta x^2)$ accuracy, making it highly desirable for transient LES (Large Eddy Simulation) and DNS workflows.

[+] Advanced Insight: Industrial CFD Solvers

Because Crank-Nicolson still relies on the unknown spatial field at $T^{n+1}$, it inherently generates an algebraic system $[A]x=b$. Thus, when selecting "Second Order Transient" in commercial solvers like ANSYS Fluent or OpenFOAM, the core architecture still executes an implicit matrix-inversion sequence, albeit with modified $[A]$ and $b$ coefficients.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

The Implicit Solver Architecture

Objective

To architect a fully functional 1D Implicit Solver, detailing coefficient matrix assembly, boundary condition enforcement, and high-performance linear algebra resolutions.

Theory: 1. Discretized Matrix Assembly

For internal nodes, the fully implicit formulation yields a set of simultaneous equations characterized by the relationship:

$$-r T_{i-1}^{n+1} + (1+2r) T_i^{n+1} - r T_{i+1}^{n+1} = T_i^n$$

Mapping these coefficients to matrix indices A[row, col] logically yields a Tridiagonal Matrix. In production CFD, storing dense zero-elements is highly inefficient. Industrial codes leverage Sparse Formats (CSR/COO) and specialized solvers.

Theory: 2. Boundary Condition Transfer

When solving $[A]x = b$, the system matrix $[A]$ must contain strictly unknown variable coefficients. Upon encountering a Dirichlet boundary ($T_{wall}$ is known), its coefficient $-r$ is multiplied by the explicit boundary value and transferred algebraically to the RHS $\{b\}$ array as a thermal source equivalent.

Theory: 3. The Thomas Algorithm (TDMA)

While Python's np.linalg.solve utilizes robust LAPACK routines, 1D implicitly formulated grids optimally resolve in $O(N)$ operations via the Thomas Algorithm (TDMA). C++ implementations heavily rely on TDMA to achieve maximum computational throughput for tridiagonal systems.

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Handling Boundaries: Topological Distinctions (FDM vs. FVM)

Objective

To mathematically formalize the fundamental topological distinctions between Finite Difference (nodal) and Finite Volume (cell-centered) frameworks, and demonstrate how this geometry dictates strict flux conservation at domain boundaries.

Topo-Mathematical Impact

The core disparity between FDM and FVM surfaces precisely at the domain boundaries due to geometric mapping:

  • FDM (Nodal Approximation): Points are infinitely small. The distance from a physical boundary node to the first internal computational node is exactly
    $ \Delta x $
    .
  • FVM (Control Volumes): Space is discretized into contiguous finite cells. The physical boundary coincides with the face of the first cell, meaning the distance from the cell centroid to the boundary face is
    $ \Delta x / 2 $
    .

Because the geometric distance in FVM is halved, the numerical gradient evaluating diffusive flux is dynamically steeper. This drastically modifies the structural matrix coefficients at the boundaries:

FDM Paradigm
Main Diagonal:
$ (1+2r) $

RHS Vector
$\{b\}$
Multiplier:
$ r $
FVM Paradigm
Main Diagonal:
$ (1+3r) $

RHS Vector
$\{b\}$
Multiplier:
$ 2r $

This strict geometric formulation in FVM ensures absolute Conservation of Flux across the domain, rendering it the de facto standard for commercial Navier-Stokes solvers (e.g., Ansys Fluent, OpenFOAM).

💻 Interactive Implementation

Output
>_ Console Output...

Phase 2 – CFD Basics

Mixed Constraints & Transient Boundary Dynamics

Objective

To formulate mixed boundary constraints (Dirichlet/Neumann) mathematically and orchestrate dynamic, time-dependent boundaries seamlessly within a static implicit algebraic framework

$[A]\{x\} = \{b\}$
.

PDE Well-Posedness & Constraints

Physical boundaries dictate the uniqueness of the PDE solution. Robust CFD solvers must routinely handle mixed configurations seamlessly:

  • Dirichlet Constraints: Specified field values (e.g.,
    $T = 100^\circ C$
    ).
  • Neumann Constraints: Specified derivative/flux (e.g.,
    $\frac{\partial T}{\partial n} = 0$
    for adiabatic walls).
⚠️ The Singular Matrix Condition:
Attempting to apply pure Neumann constraints to all domain boundaries in a steady-state elliptic or parabolic solver results in a singular (ill-posed) coefficient matrix
$[A]$
. Mathematically, the matrix possesses a zero eigenvalue. A unique solution cannot exist unless at least one Dirichlet condition is present to "anchor" the integral field level.

Transient Implementation Protocol

For time-varying phenomena (e.g., pulsatile inlet velocity, diurnal thermal cycling

$T(t) = f(t)$
):

  1. The coefficient matrix
    $[A]$
    remains structurally static (assuming no grid deformation or nonlinearity).
  2. At each physical timestep
    $\Delta t$
    , the dynamic function
    $f(t)$
    is evaluated.
  3. Only the RHS source vector
    $\{b\}$
    is updated via memory injection.
  4. The linear system is resolved iteratively or directly (e.g., via LU Decomposition).

💻 Interactive Implementation

Output
>_ Console Output...