Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 1. Finite-volume grids, stretched meshes and the discrete Gauss theorem

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 1. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Verify the β→0+\beta \to 0^+ uniform limit (beta_cluster=1e-5) and two_sided=True mirror symmetry of tanh_stretched_grid_1d, then confirm O(h2)O(h^2) convergence of verify_discrete_gauss_divergence_2d against analytical ∇⋅F\nabla\cdot\mathbf{F} for F=(sin⁡(2x)cos⁡(y), cos⁡(x)sin⁡(2y))\mathbf{F} = (\sin(2x)\cos(y),\, \cos(x)\sin(2y)) across N∈{16,32,64,128}N \in \{16, 32, 64, 128\}.


Exercise 1: Asymptotic Tanh Grid Identities & O(h^2) 2-D Divergence Convergence
===============================================================================
two_sided=True  | beta=1e-5 max face diff vs uniform = 6.40e-12
two_sided=False | beta=1e-5 max face diff vs uniform = 1.28e-11
N =  16x16  | max |div_h - div_exact| = 2.5973e-03
N =  32x32  | max |div_h - div_exact| = 6.5061e-04
N =  64x64  | max |div_h - div_exact| = 1.6273e-04
N = 128x128 | max |div_h - div_exact| = 4.0688e-05
Observed convergence orders: [1.9971, 1.9993, 1.9998]

Explanation. Since tanh⁡(βz)=βz−13β3z3+O(β5)\tanh(\beta z) = \beta z - \frac{1}{3}\beta^3 z^3 + O(\beta^5), the stretching map differs from uniform spacing by O(β2)≈10−10O(\beta^2) \approx 10^{-10} at β=10−5\beta = 10^{-5}. On staggered face midpoints, centered differences approximate ∂xFx+∂yFy\partial_x F_x + \partial_y F_y at cell centroids with clean O(Δx2+Δy2)O(\Delta x^2 + \Delta y^2) truncation error (observed order 2.000).


Exercise 2

Problem. Demonstrate across a discontinuous shock-like step uL=2.0 m/su_L = 2.0\ \mathrm{m/s}, uR=0.5 m/su_R = 0.5\ \mathrm{m/s} on [0,1][0, 1] that conservative FVM flux telescoping reproduces the exact momentum flux jump 12(uR2−uL2)=−1.875 m2/s2\frac{1}{2}(u_R^2 - u_L^2) = -1.875\ \mathrm{m^2/s^2} on every grid N∈{20,200,2000}N \in \{20, 200, 2000\}, whereas non-conservative ui(ui+1/2−ui−1/2)u_i(u_{i+1/2}-u_{i-1/2}) fails with an O(1)O(1) conservation error that never vanishes as N→∞N \to \infty. Also verify staggered shape ValueError guards.


Exercise 2: Conservative Telescoping vs Non-Conservative Shock Failure
======================================================================
N =   20 | Conservative = -1.875000 (exact -1.875000) | Non-conservative = -0.750000 (spurious source = +1.125000)
N =  200 | Conservative = -1.875000 (exact -1.875000) | Non-conservative = -0.750000 (spurious source = +1.125000)
N = 2000 | Conservative = -1.875000 (exact -1.875000) | Non-conservative = -0.750000 (spurious source = +1.125000)

Explanation. At the jump cell where ui=uR=0.5u_i = u_R = 0.5 and ui−1/2=uL=2.0u_{i-1/2} = u_L = 2.0, the non-conservative product ui(ui+1/2−ui−1/2)=uR(uR−uL)=−0.75u_i(u_{i+1/2} - u_{i-1/2}) = u_R(u_R - u_L) = -0.75 misses the true Rankine–Hugoniot average 12(uL+uR)(uR−uL)=−1.875\frac{1}{2}(u_L + u_R)(u_R - u_L) = -1.875 by 12(uR−uL)2=+1.125 m2/s2\frac{1}{2}(u_R - u_L)^2 = +1.125\ \mathrm{m^2/s^2}, no matter how fine Δx\Delta x becomes. That is why shock-capturing solvers (Lectures 8 to 10) must discretize the divergence (flux) form.


Exercise 3

Problem. Implement design_channel_mesh_for_yplus(H_m, n_cells, u_tau_m_s, nu_m2_s, target_yplus=1.0) using bisection on beta_cluster in tanh_stretched_grid_1d(..., two_sided=True) so y1+=uτy0center/ν=target_yplusy_1^+ = u_\tau y_0^{\mathrm{center}} / \nu = \mathrm{target\_yplus}, and test on a 298 K298\ \mathrm{K} water channel (H=0.04 m,N=64,uτ=0.045 m/sH = 0.04\ \mathrm{m}, N = 64, u_\tau = 0.045\ \mathrm{m/s}).


Exercise 3: Implement It Yourself — Automated y+ <= 1 Turbulent Channel Mesh Designer
=====================================================================================
Uniform N=64 water channel y1+ = 15.75 (far in buffer layer)
Optimal two-sided tanh beta    = 2.5875 -> y1+ = 1.000000, stretching_ratio = 41.14

Summary. On N=64N = 64 cells, a uniform grid lands in the turbulent buffer layer (y1+=15.8y_1^+ = 15.8), whereas a smooth two-sided tanh⁡\tanh mesh with β=2.59\beta = 2.59 hits y1+=1.000y_1^+ = 1.000 inside the linear viscous sublayer (u+=y+u^+ = y^+) without adding a single extra unknown.