from itertools import product
import numpy as np
#
angles_deg = np.array([0., 2., 4., 6., 8.])
re_factors = np.array([0.5, 1.0, 1.5])
training_points = list(product(angles_deg, re_factors))
test_points = [(4., 0.75), (5., 1.25)]
#Predicting airfoil flow across angle of attack and Reynolds number
Exercise — two-parameter POD with interpolation
This work is licensed under a Creative Commons Attribution 4.0 International License.
Learning objectives
The lecture predicted a steady airfoil flow at a new angle of attack. What if the angle is familiar but the Reynolds number changes? In this exercise we extend the same model to both inputs. By the end we should be able to design a two-parameter CFD sample, fit a shared POD basis, and test whether the second input improves predictions of velocity and pressure.
Files and preparation
Start from a working copy of the code companion. It contains the OpenFOAM v2606 case preparation, mesh-based chord measurement, snapshot reader, weighted POD, and plotting code. Run its original five-degree example first.
Follow Setting up your system, Step 4 and Step 8, Route A, to prepare the course Python environment and pull the OpenFOAM image into the repository root. From that root, run:
source ml-cfd/bin/activate
source setup-env
cd notebooks
export PODI_RENDERER=mathtext # Use Matplotlib’s built-in math renderer.
python reduced_order_modeling_code.pyFor interactive work, start python -m jupyter lab from the same activated shell and select the course Python interpreter. Keep the companion’s run(case, ...) helper when adding viscosity settings so that foamDictionary, simpleFoam, and postprocessing all use the same container.
Part 1 — Theory
Task 1 — Design the parameter study
- Starting with \(Re_c=U_\infty c/\nu\), derive the viscosity needed for a prescribed \(Re_c\) at fixed mesh and freestream speed. Use the companion’s measured chord, \(U_\infty=26\,\mathrm{m/s}\), and the tutorial viscosity \(\nu_0=10^{-5}\,\mathrm{m^2/s}\) to calculate the reference \(Re_0\). What happens to the viscosity if we halve \(Re_c\)?
- Use \(s=Re_c/Re_0\) as the second coordinate. Sketch the training grid and the two test points from the starter cell below. Which test point isolates interpolation in Reynolds number? Which requires interpolation in both coordinates?
- Predict where a change in viscosity might be easiest to see. Why could a difference close to the airfoil be difficult to detect in a volume-weighted error over the whole domain?
Task 2 — Decide what one snapshot represents
Write the discrete state vector for one parameter pair, using the lecture’s ordering of \(u_x/U_\infty\), \(u_y/U_\infty\), and \(p/U_\infty^2\). Give the dimensions of the training matrix \(\mathbf{X}\) for this grid. Explain why the two new test cases must be excluded when computing the training mean, modes, and modal coefficients.
Part 2 — Practical project
Task 3 — Extend the OpenFOAM case generator
- Adapt
solve(angle)to acceptangleandre_factor. Give each parameter pair its own case directory. In that copy, setnuinconstant/transportPropertiesbefore callingsimpleFoam. Reuse the companion’s mesh and velocity boundary conditions, including its mesh-chord angle correction. - Use the same ratio of freestream Spalart–Allmaras working viscosity to molecular viscosity as in the reference tutorial. Its initial and freestream
nuTildaare \(0.14\,\mathrm{m^2/s}\) at \(\nu_0\). Scale them by \(\nu/\nu_0\), keeping the wall value zero. Inspect0/nuTildaafter mesh renumbering: the freestream entries on both patches can contain explicit values. Check the written dictionaries for one nonreference case. - First reproduce the training case \((0^\circ,1)\). Keep its converged snapshot as the first training column and run the remaining 14 parameter pairs. Record angle, target and realized \(Re_c\), viscosity, and the iteration at which the SIMPLE residual criteria were met. Use the final converged field for each pair.
Task 4 — Interpolate in the parameter plane
- Assemble one training matrix in the order of
training_points. Fit a single volume-weighted POD basis and start with two modes so that you can compare with the lecture. Plot the singular values and report the fraction of centered training variation retained. - For each retained mode, arrange its 15 coefficients on a five-by-three grid, with angle along the first axis. Plot each coefficient against angle, with one curve per Reynolds number. Which mode responds most visibly to a change in \(s\)? Compare the actual coefficient scales when interpreting the curves.
- Implement bilinear interpolation on this rectangular grid. For each coefficient, interpolate first between the two enclosing angles on each neighboring Reynolds-number slice, then between the resulting values in \(s\). Verify that querying the training points recovers their stored coefficients. Why can a reconstructed training field still differ from CFD?
Task 5 — Does the Reynolds-number input improve the prediction?
- Predict both held-out cases with your two-parameter ROM. For comparison, build an angle-only baseline using the same basis and training mean, but interpolate its coefficients using only the \(s=1\) slice. This keeps the decoder identical and tests the effect of supplying Reynolds number to the predictor.
- Run independent CFD reference solutions at the two test points. For each field and model, report the volume-weighted relative error using the corresponding CFD component as the denominator. At the second point, show CFD, the two-parameter prediction, and their signed difference for all three dimensionless quantities. Match color limits for CFD and prediction and label the error scale separately.
- Let \(\mathbf{x}\) be the dimensionless CFD snapshot, \(\mathbf{x}_{\mathrm{proj}}\) its reconstruction from the projected coefficients, and \(\hat{\mathbf{x}}\) the two-parameter prediction. For each test case, compute the truncation, interpolation, and total errors in the joint norm \(\|\mathbf{e}\|_{\mathbf{W}}=\sqrt{\mathbf{e}^{\mathsf{T}}\mathbf{W}\mathbf{e}}\) from the lecture. Check the orthogonality identity \[ \|\mathbf{x}-\hat{\mathbf{x}}\|_{\mathbf{W}}^2 =\|\mathbf{x}-\mathbf{x}_{\mathrm{proj}}\|_{\mathbf{W}}^2 +\|\mathbf{x}_{\mathrm{proj}}-\hat{\mathbf{x}}\|_{\mathbf{W}}^2. \]
- Which contribution is larger at each test point? Use the joint errors to decide whether to test additional modes or improve coefficient interpolation first. Use the componentwise relative errors and the near-airfoil plots from step 2 to identify which fields and regions are harder to predict.