A real-time, interactive CFD (Computational Fluid Dynamics) simulator built with React/Next.js that solves the incompressible Navier-Stokes equations using numerical methods. This simulator features boundary layer visualization, particle tracing, pressure field analysis, and interactive obstacle drawing.
- Overview
- Mathematical Foundation
- Architecture
- Numerical Methods
- Implementation Details
- Features
- Installation
- Usage
- Building Your Own CFD
- Performance Optimization
- Troubleshooting
This CFD simulator implements a 2D incompressible Navier-Stokes solver using:
- Projection Method for pressure-velocity coupling
- Semi-Lagrangian Advection for stability at high Reynolds numbers
- Explicit Viscous Diffusion with boundary layer enhancement
- Real-time Visualization with multiple rendering modes
The simulator solves the incompressible Navier-Stokes equations:
∂u/∂t + (u·∇)u = -∇p/ρ + ν∇²u + f
∇·u = 0
Where:
u= velocity field (u_x, u_y)p= pressure fieldρ= fluid densityν= kinematic viscosityf= external forces
Computational Fluid Dynamics transforms continuous partial differential equations into discrete algebraic equations that computers can solve. The journey from physics to code involves several key steps:
- Physical Laws → Mathematical equations (Navier-Stokes)
- Continuous Domain → Discrete grid (finite differences)
- Time Derivatives → Time stepping schemes
- Spatial Derivatives → Finite difference approximations
- Coupled System → Operator splitting (solve parts separately)
∇·u = ∂u/∂x + ∂v/∂y = 0
Physical meaning: "What flows in must flow out" - mass is conserved.
Example: If fluid enters a pipe segment faster than it leaves, fluid would accumulate, violating mass conservation. The continuity equation prevents this.
∂u/∂t + (u·∇)u = -∇p/ρ + ν∇²u + f
Breaking this down term by term:
- ∂u/∂t: Time rate of change of velocity (acceleration)
- (u·∇)u: Convective acceleration (fluid accelerating itself)
- -∇p/ρ: Pressure gradient force per unit mass
- ν∇²u: Viscous diffusion (smoothing effect)
- f: External forces (gravity, etc.)
Physical meaning: Newton's second law for fluid parcels.
Let's trace how these equations describe flow around a circular obstacle:
t = 0: Uniform flow approaches cylinder
u(x,y,0) = [U₀, 0] for x < 0 (upstream)
u(x,y,0) = [0, 0] at obstacle boundary
As fluid approaches the cylinder, it must slow down (no-slip condition).
- Velocity decreases → Pressure increases (Bernoulli's principle)
- High pressure forms at stagnation point (front of cylinder)
Fluid flows around the cylinder:
- Top/bottom: velocity increases, pressure drops
- Rear: adverse pressure gradient may cause flow separation
- Wake formation: low pressure region behind cylinder
Instead of solving the full coupled system simultaneously, we split it into simpler sub-problems:
Solve simultaneously:
∂u/∂t + (u·∇)u + ∇p/ρ = ν∇²u
∇·u = 0
This requires solving a large coupled system - computationally expensive!
Split into sequential steps:
- Advection:
∂u/∂t + (u·∇)u = 0 - Diffusion:
∂u/∂t = ν∇²u - Projection: Make velocity field divergence-free
Why this works: Each sub-problem is easier to solve, and the splitting error is small for small time steps.
Ω = [0, L] × [0, H] (rectangular domain)
u(x,y,t) defined everywhere in Ω
Grid points: x_i = i·Δx, y_j = j·Δy
i = 0, 1, ..., N_x-1
j = 0, 1, ..., N_y-1
First Derivative (Central Difference):
∂u/∂x ≈ (u_{i+1,j} - u_{i-1,j})/(2Δx)
Example: If u = [1, 2, 5] at points [0, 1, 2]:
∂u/∂x at point 1 ≈ (5 - 1)/(2·1) = 2
Second Derivative (Laplacian):
∂²u/∂x² ≈ (u_{i+1,j} - 2u_{i,j} + u_{i-1,j})/Δx²
2D Laplacian (5-point stencil):
∇²u ≈ (u_{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1} - 4u_{i,j})/Δx²
Example: For a 3×3 grid with center value 4 and neighbors all equal to 1:
∇²u = (1 + 1 + 1 + 1 - 4·4)/1² = 4 - 16 = -12
This means the center point will decrease (diffusion smooths the peak).
u^{n+1} = u^n + Δt · f(u^n, t^n)
Pros: Simple, fast per timestep Cons: Stability restrictions (CFL condition)
∂u/∂t = ν∇²u
Explicit: u^{n+1} = u^n + ν·Δt·∇²u^n
Stability: ν·Δt/Δx² < 0.25 (2D)
If Δx = 0.1 and ν = 0.01:
Δt < 0.25 · (0.1)²/0.01 = 0.25 seconds
Instead of Eulerian grid updates, trace particles backward:
u^{n+1}(x) = u^n(x - u^n·Δt)
Advantage: Unconditionally stable - can use larger time steps!
The simulation uses a staggered grid (MAC grid) where:
- Velocities are stored at cell faces
- Pressure is stored at cell centers
- Grid spacing:
dx = dy = CELL_SIZE
Why Staggered Grid?
Regular Grid Problem:
p₁ ---- u ---- p₂ (pressure and velocity at same points)
Could lead to checkerboard pressure patterns!
Staggered Grid Solution:
u_{i+1/2}
p_i ——————— p_{i+1} (pressure at centers, velocity at faces)
v_{i,j+1/2}
The solver uses operator splitting with the following steps:
- Boundary Conditions: Apply inlet/outlet/wall conditions
- Viscous Step: Solve
∂u/∂t = ν∇²u - Projection: Enforce incompressibility
∇·u = 0 - Advection: Solve
∂u/∂t + (u·∇)u = 0
Detailed Algorithm:
For each time step n → n+1:
1. u* = applyBoundaryConditions(u^n)
2. u** = u* + Δt·ν·∇²u* (viscous diffusion)
3. Solve ∇²φ = ∇·u**/Δt (pressure Poisson equation)
u*** = u** - Δt·∇φ (pressure correction)
4. u^{n+1} = advect(u***, u***) (semi-Lagrangian)
5. u^{n+1} = applyBoundaryConditions(u^{n+1})
- Inlet: Fixed velocity
u = U_inlet - Outlet: Zero gradient
∂u/∂n = 0 - Walls: No-slip
u = 0(enhanced for boundary layers) - Obstacles: No-slip with smooth velocity transition
app/
├── page.tsx # Main CFD simulator component
├── components/
│ ├── EducationalContent.tsx # Educational UI and Reynolds number controls
│ ├── AirfoilAnalyzer.tsx # Airfoil geometry analysis
│ ├── PDEStepVisualizer.tsx # Step-by-step PDE visualization
│ └── SimplifiedPDEVisualizer.tsx # Simplified educational visualization
└── globals.css # Styling
interface SimulationState {
velocityX: Float32Array; // X-component velocity field
velocityY: Float32Array; // Y-component velocity field
pressure: Float32Array; // Pressure field
obstacles: boolean[]; // Obstacle mask
particles: Particle[]; // Lagrangian particles
streamlines: Point[][]; // Streamline paths
smokeField: Float32Array; // Smoke density (optional)
}const GRID_WIDTH = 160; // Grid cells in X
const GRID_HEIGHT = 80; // Grid cells in Y
const CELL_SIZE = 6; // Pixels per cell
const PHYSICAL_DIMENSIONS = {
LENGTH: 2.0, // Physical domain width (m)
HEIGHT: 1.0, // Physical domain height (m)
GRID_SPACING: 0.0125, // Physical grid spacing (m)
};Each step of our operator splitting approach solves a specific type of PDE. Let's understand the mathematical and physical meaning of each step, then see the implementation.
∂u/∂t = ν∇²u
This is the diffusion equation - identical to heat conduction!
- High viscosity: Fluid acts like honey - velocities smooth out quickly
- Low viscosity: Fluid acts like water - sharp velocity gradients persist
- Boundary layers: Near walls, viscosity creates smooth velocity transition from 0 to free-stream
Consider velocity profile near a wall:
Initial: u(y,0) = U₀ for y > 0, u(0,0) = 0 (no-slip)
Solution: u(y,t) = U₀ · erf(y/√(4νt))
As time increases, the velocity profile becomes smoother - this is viscous diffusion.
Discretization:
∂u/∂t ≈ (u^{n+1} - u^n)/Δt
∇²u ≈ (u_{i+1} + u_{i-1} + u_{j+1} + u_{j-1} - 4u_{i,j})/Δx²
Update Formula:
u^{n+1}_{i,j} = u^n_{i,j} + α(u^n_{i+1,j} + u^n_{i-1,j} + u^n_{i,j+1} + u^n_{i,j-1} - 4u^n_{i,j})
where α = ν·Δt/Δx²
Stability Condition: α ≤ 0.25 (for 2D explicit scheme)
Grid with values: [1, 4, 1] (center cell has value 4, neighbors have value 1)
Before: [1] [4] [1]
Laplacian at center: (1 + 1 - 2·4)/Δx² = -6/Δx²
After: 4 + α·(-6/Δx²) = 4 - 6α
The high value diffuses toward neighbors - physical smoothing!
const applyViscosity = () => {
const dt = 0.016; // Time step (~60fps)
const dx = 1.0; // Grid spacing
const nu = viscosity * 1000000; // Scaled viscosity
const alpha = (nu * dt) / (dx * dx); // Diffusion coefficient
const maxAlpha = 0.24; // Stability limit for 2D
const clampedAlpha = Math.min(alpha, maxAlpha);
// 5-point stencil Laplacian
for (let field of [velocityX, velocityY]) {
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const laplacian =
field[idx - 1] +
field[idx + 1] +
field[idx - GRID_WIDTH] +
field[idx + GRID_WIDTH] -
4 * field[idx];
newField[idx] = field[idx] + clampedAlpha * laplacian;
// Enhanced boundary layer damping
if (nearWall(i, j)) {
newField[idx] *= 0.95;
}
}
}
}
}
};The continuity equation ∇·u = 0 must be satisfied. After diffusion, our velocity field may have non-zero divergence. We correct this using the Helmholtz-Hodge decomposition:
u = u_div-free + ∇φ
To project out the divergent part:
- Solve Poisson equation: ∇²φ = ∇·u
- Subtract gradient: u_new = u - ∇φ
- Divergence > 0: More fluid flowing out than in (compression)
- Divergence < 0: More fluid flowing in than out (expansion)
- Pressure correction: High pressure pushes fluid away, low pressure draws it in
For a point source at origin injecting fluid:
∇·u = δ(x,y) (delta function)
Solution: φ(x,y) = -1/(2π) · ln(r) where r = √(x² + y²)
Pressure field creates radial outflow to satisfy mass conservation
Poisson Equation Discretization:
∇²φ ≈ (φ_{i+1,j} + φ_{i-1,j} + φ_{i,j+1} + φ_{i,j-1} - 4φ_{i,j})/Δx² = div_{i,j}
Jacobi Update:
φ^{k+1}_{i,j} = 0.25 · (φ^k_{i+1,j} + φ^k_{i-1,j} + φ^k_{i,j+1} + φ^k_{i,j-1} - Δx²·div_{i,j})
Grid with divergence field:
div = [0, 1, 0] (center cell needs correction)
[0, 0, 0]
[0, 0, 0]
Iteration 1: φ_center = 0.25 · (0+0+0+0 - 1) = -0.25
Iteration 2: φ_center = 0.25 · (neighbors affected by -0.25) ≈ -0.125
...continues until convergence
The pressure field develops to eliminate the divergence!
const projectVelocity = () => {
// Compute divergence
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const dvx = velocityX[idx + 1] - velocityX[idx - 1];
const dvy = velocityY[idx + GRID_WIDTH] - velocityY[idx - GRID_WIDTH];
divergence[idx] = 0.5 * (dvx + dvy);
}
}
}
// Jacobi iteration for pressure
for (let iter = 0; iter < 20; iter++) {
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const neighbors =
pressure[idx - 1] +
pressure[idx + 1] +
pressure[idx - GRID_WIDTH] +
pressure[idx + GRID_WIDTH];
newPressure[idx] = 0.25 * (neighbors - divergence[idx]);
}
}
}
}
// Subtract pressure gradient from velocity
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const dpx = 0.5 * (pressure[idx + 1] - pressure[idx - 1]);
const dpy =
0.5 * (pressure[idx + GRID_WIDTH] - pressure[idx - GRID_WIDTH]);
velocityX[idx] -= dpx;
velocityY[idx] -= dpy;
}
}
}
};∂u/∂t + (u·∇)u = 0
This represents convection - how the velocity field transports itself.
- Self-advection: Fluid with high velocity "carries" that velocity downstream
- Nonlinear: The transport velocity depends on the field being transported
- Characteristic curves: Solutions follow particle paths in the flow
Eulerian (Traditional):
∂u/∂t = -u·∇u
Problem: Can be unstable for large velocities (CFL condition)
Lagrangian (Our Method):
Track particles: dx/dt = u(x,t)
u^{n+1}(x) = u^n(x - u·Δt)
Advantage: Unconditionally stable!
Consider 1D advection u_t + c·u_x = 0 with constant speed c:
Solution: u(x,t) = u₀(x - ct)
A wave traveling at speed c without changing shape
For each grid point at time n+1:
-
Trace backward: Where was this fluid parcel at time n?
x_departure = x_current - u(x_current)·Δt -
Interpolate: What was the velocity at the departure point?
u^{n+1}(x_current) = interpolate(u^n, x_departure)
Current position: x = 5
Current velocity: u = 2
Time step: Δt = 0.5
Departure point: x_dep = 5 - 2·0.5 = 4
If u^n(4) = 3, then u^{n+1}(5) = 3
The velocity "3" has been advected from position 4 to position 5.
const advectVelocity = () => {
const dt = 0.016;
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
// Trace particle backwards in time
const vx = velocityX[idx];
const vy = velocityY[idx];
const x = i - dt * vx; // Departure point
const y = j - dt * vy;
// Bilinear interpolation at departure point
newVelX[idx] = interpolateVelocity(x, y, velocityX);
newVelY[idx] = interpolateVelocity(x, y, velocityY);
}
}
};const dt = 0.016; // Time step (~60fps) const dx = 1.0; // Grid spacing const nu = viscosity _ 1000000; // Scaled viscosity const alpha = (nu _ dt) / (dx * dx); // Diffusion coefficient const maxAlpha = 0.24; // Stability limit for 2D
const clampedAlpha = Math.min(alpha, maxAlpha);
// 5-point stencil Laplacian for (let field of [velocityX, velocityY]) { for (let i = 1; i < GRIDWIDTH - 1; i++) { for (let j = 1; j < GRID_HEIGHT - 1; j++) { const idx = j * GRIDWIDTH + i; if (!obstacles[idx]) { const laplacian = field[idx - 1] + field[idx + 1] + field[idx - GRID_WIDTH] + field[idx + GRID_WIDTH] - 4 * field[idx];
newField[idx] = field[idx] + clampedAlpha * laplacian;
// Enhanced boundary layer damping
if (nearWall(i, j)) {
newField[idx] *= 0.95;
}
}
}
}
} };
### 2. Pressure Projection
Solves the Poisson equation `∇²p = ∇·u*` using Jacobi iteration:
```typescript
const projectVelocity = () => {
// Compute divergence
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const dvx = velocityX[idx + 1] - velocityX[idx - 1];
const dvy = velocityY[idx + GRID_WIDTH] - velocityY[idx - GRID_WIDTH];
divergence[idx] = 0.5 * (dvx + dvy);
}
}
}
// Jacobi iteration for pressure
for (let iter = 0; iter < 20; iter++) {
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const neighbors =
pressure[idx - 1] +
pressure[idx + 1] +
pressure[idx - GRID_WIDTH] +
pressure[idx + GRID_WIDTH];
newPressure[idx] = 0.25 * (neighbors - divergence[idx]);
}
}
}
}
// Subtract pressure gradient from velocity
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
if (!obstacles[idx]) {
const dpx = 0.5 * (pressure[idx + 1] - pressure[idx - 1]);
const dpy =
0.5 * (pressure[idx + GRID_WIDTH] - pressure[idx - GRID_WIDTH]);
velocityX[idx] -= dpx;
velocityY[idx] -= dpy;
}
}
}
};
Unconditionally stable advection scheme:
const advectVelocity = () => {
const dt = 0.016;
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = j * GRID_WIDTH + i;
// Trace particle backwards in time
const vx = velocityX[idx];
const vy = velocityY[idx];
const x = i - dt * vx; // Departure point
const y = j - dt * vy;
// Bilinear interpolation at departure point
newVelX[idx] = interpolateVelocity(x, y, velocityX);
newVelY[idx] = interpolateVelocity(x, y, velocityY);
}
}
};When we trace particles backward in semi-Lagrangian advection, they rarely land exactly on grid points. We need to interpolate field values at arbitrary positions.
Interpolation provides smooth transitions between discrete grid values, preventing numerical artifacts and maintaining physical accuracy.
Given four surrounding grid points, interpolate smoothly:
v01 ——— v11
| |
| P | P = query point (x,y)
| |
v00 ——— v10
Algorithm:
- Find surrounding grid cell: (x₀, y₀) to (x₁, y₁)
- Compute fractional distances: fx = x - x₀, fy = y - y₀
- Bilinear interpolation:
v(x,y) = v₀₀(1-fx)(1-fy) + v₁₀·fx(1-fy) + v₀₁(1-fx)fy + v₁₁·fx·fy
Query point: (1.3, 2.7) Grid values:
v01=5 ——— v11=8
| |
| P |
| |
v00=2 ——— v10=6
Calculations:
x₀=1, y₀=2, x₁=2, y₁=3
fx = 1.3 - 1 = 0.3
fy = 2.7 - 2 = 0.7
v = 2·(1-0.3)·(1-0.7) + 6·0.3·(1-0.7) + 5·(1-0.3)·0.7 + 8·0.3·0.7
= 2·0.7·0.3 + 6·0.3·0.3 + 5·0.7·0.7 + 8·0.3·0.7
= 0.42 + 0.54 + 2.45 + 1.68 = 5.09
The interpolated value smoothly blends the four neighbors based on distance.
const interpolateVelocity = (x, y, field) => {
const x0 = Math.floor(x);
const y0 = Math.floor(y);
const x1 = Math.min(x0 + 1, GRID_WIDTH - 1);
const y1 = Math.min(y0 + 1, GRID_HEIGHT - 1);
const fx = x - x0;
const fy = y - y0;
const v00 = field[y0 * GRID_WIDTH + x0] || 0;
const v10 = field[y0 * GRID_WIDTH + x1] || 0;
const v01 = field[y1 * GRID_WIDTH + x0] || 0;
const v11 = field[y1 * GRID_WIDTH + x1] || 0;
return (
v00 * (1 - fx) * (1 - fy) +
v10 * fx * (1 - fy) +
v01 * (1 - fx) * fy +
v11 * fx * fy
);
};Let's trace how our algorithm handles this classic CFD problem:
Initial Conditions:
t = 0: Uniform flow u = [10, 0] m/s
Cylinder: radius = 0.2m at center of domain
Reynolds number: Re = U·D/ν = 10·0.4/0.00005 = 80,000
Time Step 1: Boundary Conditions
Inlet (left): u = [10, 0] m/s (fixed)
Cylinder surface: u = [0, 0] m/s (no-slip)
Outlet (right): ∂u/∂n = 0 (free outflow)
Time Step 2: Viscous Diffusion Near the cylinder surface, viscosity smooths velocity gradients:
Before diffusion: u jumps from [10,0] to [0,0] at surface
After diffusion: u transitions smoothly: [10,0] → [8,0] → [5,0] → [2,0] → [0,0]
This creates the boundary layer - a thin region where viscous effects dominate.
Time Step 3: Pressure Projection The no-slip condition creates flow deceleration, leading to pressure buildup:
Continuity violation: ∇·u ≠ 0 (flow piles up against cylinder)
Pressure Poisson: ∇²p = ∇·u (high divergence → high pressure)
Pressure correction: u = u - ∇p (pressure pushes flow around cylinder)
Time Step 4: Advection The corrected velocity field advects itself:
High-speed flow regions advect downstream
Low-speed wake regions remain behind cylinder
Vorticity concentrates at separation points
Result: Flow separation, wake formation, and periodic vortex shedding (Kármán vortex street) for Re > 40.
Consider flow over a flat plate:
Physical Process:
t = 0: Uniform flow approaches plate
t > 0: Fluid "sticks" to plate (no-slip condition)
Viscous diffusion spreads velocity deficit upstream
Boundary layer thickness grows: δ ∝ √(νx/U)
Our Algorithm:
-
Diffusion Step: Creates velocity gradient near wall
u_wall = 0 (no-slip) u_freestream = U₀ Viscous diffusion: ∂u/∂t = ν∂²u/∂y² -
Pressure Correction: Maintains mass conservation
As boundary layer grows, displacement effect increases pressure Pressure gradient accelerates outer flow slightly -
Advection: Transports boundary layer downstream
Thicker boundary layer advects from upstream Sharp velocity profiles become smoother downstream
Validation: Blasius boundary layer profile
Analytical: u/U = f'(η) where η = y√(U/νx)
Our simulation: Matches within 5% for sufficient grid resolution
Flow between parallel plates driven by pressure gradient:
Setup:
Geometry: Channel height h = 0.1m
Pressure gradient: dp/dx = -100 Pa/m
Boundary conditions: u = 0 at walls (y = ±h/2)
Analytical Solution:
u(y) = -(dp/dx)·y(h-y)/(2μ)
Maximum velocity at center: u_max = (dp/dx)h²/(8μ)
Our Algorithm:
- Initial: Zero velocity everywhere
- Pressure Step: Apply pressure gradient
Pressure field: p(x) = p₀ - 100x Pressure force: F = -∇p = [100, 0] Pa/m - Viscous Step: Diffusion develops parabolic profile
No-slip walls constrain flow Viscosity balances pressure gradient: ν∇²u = ∇p/ρ - Steady State: Parabolic velocity profile emerges
Result: Our simulation converges to the analytical Poiseuille profile within numerical accuracy.
Each physical process operates on different time scales:
- Advection: Fast (convective time scale: L/U)
- Pressure: Instantaneous (sound speed >> flow speed)
- Diffusion: Slow (viscous time scale: L²/ν)
Splitting errors are small when Δt is smaller than the fastest time scale.
Traditional explicit advection requires: Δt < Δx/|u| (CFL condition) Semi-Lagrangian removes this restriction by following particle paths exactly.
The Helmholtz decomposition theorem guarantees that any vector field can be decomposed into divergence-free and gradient parts. Our projection isolates the divergence-free (physical) component.
Enhanced viscosity near walls + no-slip conditions correctly capture:
- Velocity gradient: ∂u/∂y|wall ∝ shear stress
- Displacement effect: Boundary layer thickness affects outer flow
- Separation: Adverse pressure gradients cause flow reversal
This comprehensive approach captures the essential physics of incompressible viscous flow while remaining computationally tractable for real-time simulation. v11 _ fx _ fy ); };
## Implementation Details
### Boundary Layer Visualization
The enhanced boundary layer visualization shows:
- **Thickness indicators**: Yellow lines showing δ₉₉ (99% velocity recovery)
- **Velocity profiles**: Curved lines showing velocity distribution near walls
```typescript
const renderBoundaryLayer = () => {
for (let i = 5; i < GRID_WIDTH - 5; i += 3) {
let blThickness = 0;
// Find boundary layer thickness (99% of free stream)
for (let j = 1; j < 15; j++) {
const idx = j * GRID_WIDTH + i;
const speed = Math.sqrt(velocityX[idx] * velocityX[idx]);
const freeStreamSpeed = windSpeed;
if (speed < 0.99 * freeStreamSpeed && speed > 0.01 * freeStreamSpeed) {
blThickness = j;
} else if (speed >= 0.99 * freeStreamSpeed) {
break;
}
}
// Draw thickness indicator
if (blThickness > 1) {
ctx.beginPath();
ctx.moveTo(i * CELL_SIZE, 0);
ctx.lineTo(i * CELL_SIZE, blThickness * CELL_SIZE);
ctx.stroke();
}
}
};
Lagrangian particles for flow visualization:
interface Particle {
x: number; // Position X
y: number; // Position Y
vx: number; // Velocity X
vy: number; // Velocity Y
life: number; // Lifetime [0,1]
}
const updateParticles = () => {
const dt = 0.016;
// Generate new particles at inlet
const particleInterval = 1.0 / particlesPerSecond;
while (particleGenerationTimer >= particleInterval) {
particles.push({
x: Math.random() * CELL_SIZE * 2,
y: Math.random() * GRID_HEIGHT * CELL_SIZE,
vx: windSpeed,
vy: (Math.random() - 0.5) * 0.5,
life: 1.0,
});
particleGenerationTimer -= particleInterval;
}
// Update existing particles
particles.forEach((particle, i) => {
const gridX = particle.x / CELL_SIZE;
const gridY = particle.y / CELL_SIZE;
// Get velocity from flow field
const vx = interpolateVelocity(gridX, gridY, velocityX);
const vy = interpolateVelocity(gridX, gridY, velocityY);
// Update position and velocity
particle.vx = vx;
particle.vy = vy;
particle.x += vx * dt * CELL_SIZE;
particle.y += vy * dt * CELL_SIZE;
particle.life -= dt * 0.5;
// Remove dead or escaped particles
if (particle.life <= 0 || particle.x > GRID_WIDTH * CELL_SIZE) {
particles.splice(i, 1);
}
});
};Real-time Reynolds number based on physical parameters:
const calculateReynolds = (velocity, viscosity) => {
const characteristicLength = 0.5; // Obstacle diameter (m)
return (velocity * characteristicLength) / viscosity;
};
const getFlowDescription = (Re) => {
if (Re < 1) return "Creeping flow - viscous forces dominate";
if (Re < 40) return "Steady laminar flow - predictable streamlines";
if (Re < 200) return "Vortex shedding begins - oscillatory behavior";
if (Re < 1000) return "Turbulent wake - chaotic flow patterns";
return "Fully turbulent - complex vortex structures";
};-
Standard Mode
- Velocity field vectors
- Particle traces with speed/pressure coloring
- Streamlines with flow direction indicators
- Boundary layer thickness visualization
-
Pressure Mode
- Enhanced pressure field with contours
- Pressure gradient indicators
- Minimal velocity vectors for reference
-
Smoke Mode
- Advected scalar field visualization
- Multiple color schemes (thermal, rainbow, plasma)
- Density-based opacity
- Drawing Tools: Sketch obstacles with brush or Bezier curves
- Real-time Analysis: Click anywhere for local flow analysis
- Parameter Controls: Adjust viscosity, velocity, density in real-time
- Educational Mode: Step-by-step PDE visualization
- PDE Step Visualizer: Shows each numerical step
- Reynolds Number Calculator: Real-time Re calculation with flow regime description
- Airfoil Analyzer: Import and analyze airfoil geometries
# Clone repository
git clone https://github.com/rongeld/cfd-browser
cd cfd-browser
# Install dependencies
npm install
# Run development server
npm run dev
# Build for production
npm run build
npm start{
"next": "^14.0.0",
"react": "^18.0.0",
"typescript": "^5.0.0",
"lucide-react": "^0.263.1",
"recharts": "^2.8.0"
}Before diving into implementation, let's establish the key concepts you'll need:
- Physical Laws: Navier-Stokes equations govern fluid motion
- Numerical Methods: Transform continuous equations into discrete algorithms
- Data Structures: Organize velocity, pressure, and geometry data efficiently
- Visualization: Render flow fields for analysis and debugging
- Stability: Ensure numerical schemes remain stable over time
Physical Considerations:
Domain size: Balance computational cost vs. flow development
- Too small: Boundary effects dominate
- Too large: Wasted computational resources
- Rule of thumb: 10-20 characteristic lengths downstream
Grid Resolution:
Cell size determines accuracy and stability:
- Finer grid: Better resolution, higher computational cost
- Coarser grid: Faster computation, potential numerical diffusion
- Boundary layers need ~10-20 cells for proper resolution
// Define computational domain
const GRID_WIDTH = 100; // Number of cells in X direction
const GRID_HEIGHT = 50; // Number of cells in Y direction
const CELL_SIZE = 8; // Pixels per cell (visualization)
// Physical domain mapping
const PHYSICAL_WIDTH = 2.0; // Domain width in meters
const PHYSICAL_HEIGHT = 1.0; // Domain height in meters
const DX = PHYSICAL_WIDTH / GRID_WIDTH; // Physical cell size: 0.02m
const DY = PHYSICAL_HEIGHT / GRID_HEIGHT; // Should equal DX for square cells
// Grid indexing function (row-major order)
const getIndex = (i, j) => j * GRID_WIDTH + i;
// Boundary identification
const isLeftBoundary = (i) => i === 0;
const isRightBoundary = (i) => i === GRID_WIDTH - 1;
const isTopBoundary = (j) => j === 0;
const isBottomBoundary = (j) => j === GRID_HEIGHT - 1;Memory Layout Strategy:
Use typed arrays for performance:
- Float32Array: 32-bit floats for velocity/pressure (sufficient precision)
- Uint8Array: Boolean arrays for obstacles (memory efficient)
- Array indexing: row-major order for cache efficiency
// Initialize fields with proper types
const velocityX = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
const velocityY = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
const pressure = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
const obstacles = new Uint8Array(GRID_WIDTH * GRID_HEIGHT); // 0=fluid, 1=solid
// Temporary arrays for numerical schemes
const tempVelX = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
const tempVelY = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
const divergence = new Float32Array(GRID_WIDTH * GRID_HEIGHT);
// Initialize with physical values
const initializeFields = () => {
velocityX.fill(0.0); // Start at rest
velocityY.fill(0.0);
pressure.fill(0.0); // Atmospheric pressure reference
obstacles.fill(0); // Start with no obstacles
// Set inlet conditions (left boundary)
const INLET_VELOCITY = 10.0; // m/s
for (let j = 0; j < GRID_HEIGHT; j++) {
const idx = getIndex(0, j);
velocityX[idx] = INLET_VELOCITY;
velocityY[idx] = 0.0;
}
};Physics: Obstacles represent solid boundaries where fluid must satisfy no-slip conditions.
// Add circular obstacle (cylinder)
const addCircularObstacle = (centerX, centerY, radius) => {
const gridCenterX = Math.floor(centerX / DX);
const gridCenterY = Math.floor(centerY / DY);
const gridRadius = radius / DX;
for (let i = 0; i < GRID_WIDTH; i++) {
for (let j = 0; j < GRID_HEIGHT; j++) {
const dx = i - gridCenterX;
const dy = j - gridCenterY;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= gridRadius) {
const idx = getIndex(i, j);
obstacles[idx] = 1;
// Immediately enforce no-slip condition
velocityX[idx] = 0.0;
velocityY[idx] = 0.0;
}
}
}
};
// Example: Add cylinder at domain center
addCircularObstacle(PHYSICAL_WIDTH * 0.3, PHYSICAL_HEIGHT * 0.5, 0.1);Physical Meaning:
1. Inlet: Where fluid enters (Dirichlet condition: u = u_specified)
2. Outlet: Where fluid exits (Neumann condition: ∂u/∂n = 0)
3. Walls: Solid boundaries (No-slip: u = 0)
4. Symmetry: Mirror conditions (∂u_n/∂n = 0, u_t = 0)
Mathematical Implementation:
Dirichlet: Value specified directly
Neumann: Gradient specified (use finite differences)
Mixed: Combination based on physical requirements
const applyBoundaryConditions = () => {
// Left boundary (inlet): Fixed velocity
for (let j = 0; j < GRID_HEIGHT; j++) {
const idx = getIndex(0, j);
if (obstacles[idx] === 0) {
// Only apply to fluid cells
velocityX[idx] = 10.0; // m/s inlet velocity
velocityY[idx] = 0.0; // No vertical component
}
}
// Right boundary (outlet): Zero gradient (extrapolation)
for (let j = 0; j < GRID_HEIGHT; j++) {
const idx = getIndex(GRID_WIDTH - 1, j);
const prevIdx = getIndex(GRID_WIDTH - 2, j);
if (obstacles[idx] === 0) {
velocityX[idx] = velocityX[prevIdx]; // ∂u/∂x = 0
velocityY[idx] = velocityY[prevIdx]; // ∂v/∂x = 0
}
}
// Top and bottom boundaries (walls): No-slip
for (let i = 0; i < GRID_WIDTH; i++) {
// Top wall
const topIdx = getIndex(i, 0);
if (obstacles[topIdx] === 0) {
velocityX[topIdx] = 0.0;
velocityY[topIdx] = 0.0;
}
// Bottom wall
const bottomIdx = getIndex(i, GRID_HEIGHT - 1);
if (obstacles[bottomIdx] === 0) {
velocityX[bottomIdx] = 0.0;
velocityY[bottomIdx] = 0.0;
}
}
// Obstacle boundaries: No-slip condition
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
if (obstacles[idx] === 1) {
velocityX[idx] = 0.0;
velocityY[idx] = 0.0;
}
}
}
};Viscosity Physics:
Molecular momentum transfer between fluid layers
- High viscosity: Strong momentum diffusion (honey-like)
- Low viscosity: Weak momentum diffusion (water-like)
- Boundary layer formation: Velocity smoothly transitions from wall to free-stream
- Dissipation: Kinetic energy converts to heat through friction
Mathematical Form:
∂u/∂t = ν∇²u (diffusion equation)
where ν = kinematic viscosity (m²/s)
Finite Difference Stencil:
2D Laplacian (5-point stencil):
∇²u ≈ (u_{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1} - 4u_{i,j}) / Δx²
Physical interpretation:
- Positive laplacian: surrounded by higher values → increase
- Negative laplacian: surrounded by lower values → decrease
- Zero laplacian: in equilibrium with neighbors
Stability Analysis:
Explicit time stepping requires: α = ν·Δt/Δx² ≤ 0.25 (2D)
If violated: Solution oscillates and grows exponentially!
Example: ν = 1e-5 m²/s, Δx = 0.02m
Maximum Δt = 0.25 × (0.02)² / 1e-5 = 10 seconds
For 60fps simulation: Δt = 1/60 = 0.0167s ≪ 10s ✓ (stable)
const applyViscosity = () => {
const dt = 1.0 / 60.0; // Time step (seconds)
const nu = 1.5e-5; // Kinematic viscosity of air (m²/s)
const dx = DX; // Physical grid spacing
// Stability check
const alpha = (nu * dt) / (dx * dx);
const maxAlpha = 0.24; // Slightly under theoretical limit
if (alpha > maxAlpha) {
console.warn(`Viscous instability! α=${alpha} > ${maxAlpha}`);
// Could implement sub-stepping or implicit method here
}
const clampedAlpha = Math.min(alpha, maxAlpha);
// Apply diffusion to both velocity components
[velocityX, velocityY].forEach((field, fieldIndex) => {
const tempField = fieldIndex === 0 ? tempVelX : tempVelY;
// Copy current values
tempField.set(field);
// Apply Laplacian operator
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
// Skip obstacle cells
if (obstacles[idx] === 1) continue;
// 5-point stencil
const left = tempField[getIndex(i - 1, j)];
const right = tempField[getIndex(i + 1, j)];
const top = tempField[getIndex(i, j - 1)];
const bottom = tempField[getIndex(i, j + 1)];
const center = tempField[idx];
const laplacian = left + right + top + bottom - 4.0 * center;
field[idx] = center + clampedAlpha * laplacian;
// Enhanced boundary layer treatment near walls
const nearWall = checkNearWall(i, j);
if (nearWall) {
field[idx] *= 0.98; // Additional damping for boundary layer
}
}
}
});
};
// Helper function to detect near-wall regions
const checkNearWall = (i, j) => {
// Check if within 2 cells of any boundary or obstacle
if (j <= 2 || j >= GRID_HEIGHT - 3) return true; // Top/bottom walls
// Check nearby obstacles
for (let di = -2; di <= 2; di++) {
for (let dj = -2; dj <= 2; dj++) {
const ni = i + di;
const nj = j + dj;
if (ni >= 0 && ni < GRID_WIDTH && nj >= 0 && nj < GRID_HEIGHT) {
if (obstacles[getIndex(ni, nj)] === 1) return true;
}
}
}
return false;
};Physical Law: Mass conservation for incompressible flow
∇·u = ∂u/∂x + ∂v/∂y = 0
Physical meaning:
- Divergence > 0: Fluid expanding (unphysical for liquids)
- Divergence < 0: Fluid compressing (unphysical for liquids)
- Divergence = 0: Mass conserved (physical requirement)
Helmholtz-Hodge Decomposition:
Any vector field can be decomposed:
u = u_solenoidal + ∇φ
where:
- u_solenoidal: divergence-free (physical velocity)
- ∇φ: curl-free (pressure gradient)
Mathematical Derivation:
1. Take divergence: ∇·u = ∇·u_solenoidal + ∇·(∇φ) = 0 + ∇²φ
2. Since we want ∇·u_solenoidal = 0: ∇²φ = ∇·u
3. Solve for φ, then correct: u_new = u - ∇φ
const projectVelocity = () => {
// Step 1: Compute velocity divergence
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
if (obstacles[idx] === 1) {
divergence[idx] = 0.0; // No divergence in solid regions
continue;
}
// Central difference approximation
const dudx =
(velocityX[getIndex(i + 1, j)] - velocityX[getIndex(i - 1, j)]) /
(2.0 * DX);
const dvdy =
(velocityY[getIndex(i, j + 1)] - velocityY[getIndex(i, j - 1)]) /
(2.0 * DY);
divergence[idx] = dudx + dvdy;
}
}
// Step 2: Solve Poisson equation ∇²p = ρ·∇·u/Δt
const rho = 1.225; // Air density (kg/m³)
const dt = 1.0 / 60.0;
// Initialize pressure field
pressure.fill(0.0);
// Jacobi iteration (could use SOR or multigrid for faster convergence)
const maxIterations = 50;
const tolerance = 1e-6;
for (let iter = 0; iter < maxIterations; iter++) {
let maxChange = 0.0;
// Copy current pressure for Jacobi method
const tempPressure = new Float32Array(pressure);
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
if (obstacles[idx] === 1) {
pressure[idx] = 0.0; // Zero pressure inside obstacles
continue;
}
// Jacobi update: p_new = (sum_neighbors - source) / 4
const neighbors =
tempPressure[getIndex(i - 1, j)] +
tempPressure[getIndex(i + 1, j)] +
tempPressure[getIndex(i, j - 1)] +
tempPressure[getIndex(i, j + 1)];
const source = (rho * divergence[idx] * DX * DX) / dt;
const newPressure = (neighbors - source) / 4.0;
const change = Math.abs(newPressure - pressure[idx]);
maxChange = Math.max(maxChange, change);
pressure[idx] = newPressure;
}
}
// Check convergence
if (maxChange < tolerance) {
console.log(`Pressure converged in ${iter} iterations`);
break;
}
}
// Step 3: Apply pressure correction
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
if (obstacles[idx] === 1) continue;
// Pressure gradient
const dpdx =
(pressure[getIndex(i + 1, j)] - pressure[getIndex(i - 1, j)]) /
(2.0 * DX);
const dpdy =
(pressure[getIndex(i, j + 1)] - pressure[getIndex(i, j - 1)]) /
(2.0 * DY);
// Apply correction: u_new = u_old - (Δt/ρ)·∇p
velocityX[idx] -= (dt / rho) * dpdx;
velocityY[idx] -= (dt / rho) * dpdy;
}
}
};Traditional Eulerian Problem:
∂u/∂t + u·∇u = 0 (advection equation)
Explicit discretization: unstable for large velocities
CFL condition: Δt ≤ Δx/|u_max| (very restrictive!)
Semi-Lagrangian Solution:
Follow fluid particles: D/Dt = ∂/∂t + u·∇
Along characteristic curves: dx/dt = u(x,t)
Solution: u(x,t+Δt) = u(x-u·Δt, t)
const advectVelocity = () => {
const dt = 1.0 / 60.0;
// Store current velocity field
tempVelX.set(velocityX);
tempVelY.set(velocityY);
for (let i = 1; i < GRID_WIDTH - 1; i++) {
for (let j = 1; j < GRID_HEIGHT - 1; j++) {
const idx = getIndex(i, j);
if (obstacles[idx] === 1) continue;
// Current grid position in physical coordinates
const x = (i + 0.5) * DX; // Cell centers
const y = (j + 0.5) * DY;
// Current velocity at this position
const u = tempVelX[idx];
const v = tempVelY[idx];
// Trace backwards to departure point
const departureX = x - u * dt;
const departureY = y - v * dt;
// Convert back to grid coordinates
const gridX = departureX / DX - 0.5;
const gridY = departureY / DY - 0.5;
// Interpolate velocity at departure point
velocityX[idx] = bilinearInterpolate(gridX, gridY, tempVelX);
velocityY[idx] = bilinearInterpolate(gridX, gridY, tempVelY);
}
}
};const simulationStep = () => {
// Step 1: Apply boundary conditions
// Physics: Enforce physical constraints at domain boundaries
applyBoundaryConditions();
// Step 2: Viscous diffusion
// Physics: Molecular momentum transfer (smoothing)
applyViscosity();
// Step 3: Pressure projection
// Physics: Enforce mass conservation (incompressibility)
projectVelocity();
// Step 4: Advection
// Physics: Transport velocity field with flow
advectVelocity();
// Step 5: Re-apply boundary conditions
// Physics: Maintain boundary constraints after advection
applyBoundaryConditions();
// Optional: Update particles, compute derived quantities
updateParticles();
computeDerivedFields(); // Vorticity, pressure coefficients, etc.
};
// Main animation loop
let lastTime = 0;
const animate = (currentTime) => {
const deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Run multiple simulation steps per frame if needed
const targetFPS = 60;
const simStepsPerFrame = 1;
for (let step = 0; step < simStepsPerFrame; step++) {
simulationStep();
}
// Render current state
render();
// Continue animation
requestAnimationFrame(animate);
};
// Start simulation
requestAnimationFrame(animate);interface Particle {
x: number; // Physical x position (meters)
y: number; // Physical y position (meters)
age: number; // Time since creation (seconds)
maxAge: number; // Lifetime before removal
}
const updateParticles = () => {
const dt = 1.0 / 60.0;
// Generate new particles at inlet
if (Math.random() < 0.1) {
// 10% chance per frame
particles.push({
x: 0.01, // Just inside domain
y: Math.random() * PHYSICAL_HEIGHT,
age: 0.0,
maxAge: 5.0, // 5 second lifetime
});
}
// Update existing particles
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
// Get velocity at particle position
const gridX = particle.x / DX - 0.5;
const gridY = particle.y / DY - 0.5;
const u = bilinearInterpolate(gridX, gridY, velocityX);
const v = bilinearInterpolate(gridX, gridY, velocityY);
// Integrate position (RK2 for better accuracy)
const k1x = u * dt;
const k1y = v * dt;
const midX = particle.x + 0.5 * k1x;
const midY = particle.y + 0.5 * k1y;
const midGridX = midX / DX - 0.5;
const midGridY = midY / DY - 0.5;
const uMid = bilinearInterpolate(midGridX, midGridY, velocityX);
const vMid = bilinearInterpolate(midGridY, midGridY, velocityY);
const k2x = uMid * dt;
const k2y = vMid * dt;
particle.x += k2x;
particle.y += k2y;
particle.age += dt;
// Remove particles outside domain or too old
if (
particle.x > PHYSICAL_WIDTH ||
particle.x < 0 ||
particle.y > PHYSICAL_HEIGHT ||
particle.y < 0 ||
particle.age > particle.maxAge
) {
particles.splice(i, 1);
}
}
};This comprehensive guide provides the complete foundation for building a CFD simulator from scratch, with detailed physics explanations, mathematical derivations, and practical implementation considerations at every step. for (let i = 0; i < GRIDWIDTH; i++) { for (let j = 0; j < GRID_HEIGHT; j++) { if (obstacles[j * GRID_WIDTH + i]) { ctx.fillRect(i * CELLSIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE); } } } };
### 4. Advanced Features
#### Streamline Generation
```typescript
const generateStreamlines = () => {
const streamlines = [];
for (let i = 2; i < GRID_HEIGHT - 2; i += 4) {
const streamline = traceStreamline(1, i, 200, 0.1);
if (streamline.length > 5) {
streamlines.push(streamline);
}
}
return streamlines;
};
const traceStreamline = (startX, startY, maxSteps, stepSize) => {
const points = [];
let x = startX,
y = startY;
for (let step = 0; step < maxSteps; step++) {
// Check bounds
if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT) break;
// Check obstacles
const gridX = Math.floor(x);
const gridY = Math.floor(y);
if (obstacles[gridY * GRID_WIDTH + gridX]) break;
points.push({ x: x * CELL_SIZE, y: y * CELL_SIZE });
// Get velocity at current position
const vx = interpolateVelocity(x, y, velocityX);
const vy = interpolateVelocity(x, y, velocityY);
// Check for stagnation
const speed = Math.sqrt(vx * vx + vy * vy);
if (speed < 0.01) break;
// Runge-Kutta 2nd order integration
const k1x = vx * stepSize;
const k1y = vy * stepSize;
const midX = x + k1x * 0.5;
const midY = y + k1y * 0.5;
const vx_mid = interpolateVelocity(midX, midY, velocityX);
const vy_mid = interpolateVelocity(midX, midY, velocityY);
const k2x = vx_mid * stepSize;
const k2y = vy_mid * stepSize;
x += k2x;
y += k2y;
}
return points;
};
- Use
Float32Arrayfor large numerical arrays - Pool particle objects to avoid garbage collection
- Limit maximum particle count
- Skip calculations in obstacle cells
- Use appropriate iteration counts for pressure solver
- Implement adaptive time stepping for stability
- Use
requestAnimationFramefor smooth animation - Skip expensive operations when not visible
- Implement level-of-detail for particle rendering
// CFL condition for advection
const maxVelocity = Math.max(
...velocityX.map(Math.abs),
...velocityY.map(Math.abs)
);
const maxTimeStep = (0.5 * CELL_SIZE) / maxVelocity;
const dt = Math.min(0.016, maxTimeStep); // Don't exceed display rate
// Viscous stability constraint
const viscousTimeStep = (0.25 * CELL_SIZE * CELL_SIZE) / viscosity;
const stableDt = Math.min(dt, viscousTimeStep);- Simulation Explodes: Check CFL condition and reduce time step
- Poor Convergence: Increase pressure iteration count
- Boundary Layer Not Visible: Increase viscosity or enable boundary layer visualization
- Performance Issues: Reduce grid resolution or particle count
- Add velocity magnitude visualization
- Monitor maximum velocities and pressures
- Implement divergence checking for mass conservation
- Use console logging for critical parameters
- Verify Reynolds number calculations
- Check mass conservation (
∇·u ≈ 0) - Validate boundary conditions
- Compare results with analytical solutions for simple cases