教学主线与目标:面向学过微积分、线性代数和数值方法的本科生,从模型问题的求解、前向和伴随逐句实现,走向2D非结构网格 RANS 的完整离散伴随。
Learning path: for undergraduates familiar with calculus, linear algebra and numerical methods, implement solvers, tangents and adjoints for model problems statement by statement, toward a complete discrete adjoint for 2D unstructured-grid RANS.
二十个单文件、可交互的网页,把有限体积法的全离散伴随从头到尾讲一遍:从一个面上的一维通量开始,把同一个循环依次改写成 primal、matrix-free 前向与 matrix-free 伴随,再做前向与伴随求解、几何导数,最后用有限差分与复数步长逐项校验。八组算例(一维 Burgers、二维标量对流、二维标量对流扩散、拟一维 Euler、一维层流 N–S、二维 Euler、二维层流 N–S与二维 RANS–SA)各有格心与格点两版,二维 RANS–SA 另有伴随一致的一对,十八页成对教学主题相互对应,另有独立的黏性 Burgers 两页(格心与格点),可以任意两两并排对照。第一次接触离散伴随,请从一维 Burgers 那一对读起。
Twenty single-file, interactive web pages that teach the fully discrete adjoint of a finite-volume scheme from start to finish. Starting from a one-dimensional flux on a single face, the same loop is rewritten in turn as the primal, a matrix-free forward mode and a matrix-free adjoint; then come the forward and adjoint solves and the geometric derivatives, and finally everything is checked term by term with finite differences and the complex step. Eight model problems (one-dimensional Burgers, two-dimensional scalar advection, two-dimensional scalar advection–diffusion, quasi-one-dimensional Euler, one-dimensional laminar N–S, two-dimensional Euler, two-dimensional laminar N–S and two-dimensional RANS–SA) each come in a cell-centred and a node-centred version, and the RANS–SA problem has an extra adjoint-consistent pair. The eighteen pages have corresponding teaching topics in pairs, and an independent pair of viscous Burgers pages (cell-centred and node-centred) stands apart, so any two can be compared side by side. If the discrete adjoint is new to you, start with the one-dimensional Burgers pair.
带源项的定常 Burgers 方程,支持一阶/二阶空间精度切换。二阶线性重构的完整 tangent 与 adjoint 包含斜率和边界导数。原始方程用 GS 预估矫正,线性方程可选 GS 固定点或 GS 预处理 GMRES,并比较网格收敛、求解成本与连续伴随一致性。
Steady Burgers with a source, selectable first-/second-order space, exact reconstruction derivatives, GS predictor–corrector and GS-preconditioned GMRES. Interactive studies compare mesh convergence, solver work and continuous-adjoint consistency.
未知量是单元平均值。入口值 uin 是入口面左侧的 ghost 状态,在通量里弱加。The unknowns are cell averages. The inlet value uin is the ghost state to the left of the inlet face and is imposed weakly, inside the flux.
8 / 16 / 32 个单元 · 一阶 / 二阶 · GS / GMRES / LU(附录 C)8 / 16 / 32 cells · first / second order · GS / GMRES / LU (Appendix C)
adjoint_1d_cell.html
入口节点的整行换成 u0 − uin,强加;伴随里「循环前清零、循环后加回」,顺序不能反。The inlet node’s whole row is replaced by u0 − uin and imposed strongly; in the adjoint the row is “zeroed before the loop and added back after it”, and the order cannot be reversed.
9 / 17 / 33 个节点 · 一阶 / 二阶 · GS / GMRES / LU(附录 C)9 / 17 / 33 nodes · first / second order · GS / GMRES / LU (Appendix C)
adjoint_1d_node.html
固定正黏性的定常 Burgers 方程,用构造解使每个导数都能与精确值比较。未知量是单元平均值,边界值进入两个边界面的通量;残差连同它的切线与伴随,按语句逐条写出,记号与其他各页一致;用带状 Newton、GS 与 GMRES 求解,并与复数步长、有限差分、精度阶实验、Python 实跑到 65,536 单元的结果和连续伴随逐项核对。附录:连续伴随、与格点格式的对照、复现每个数字的方法。The steady viscous Burgers equation with a fixed positive viscosity and a manufactured solution, so that every derivative can be compared with the exact one. The unknowns are cell averages and the boundary values enter the flux of the two boundary faces. The residual, its tangent and its adjoint are written statement by statement in the notation of the other pages, solved with banded Newton, GS and GMRES, and checked against the complex step, finite differences, order-of-accuracy runs, Python runs up to 65,536 cells and the continuous adjoint. Appendices: the continuous adjoint, a comparison with the node-centred scheme, and how to reproduce every number.
浏览器 8–128 单元 · Python 已实跑 65,536 单元
84 幅图、67 个实验与逐步播放器 · 数值证据与待证数学义务分列8–128 cells in the browser · Python run up to 65,536 cells
84 figures, 67 labs and step players · numerical evidence and open mathematical obligations listed separately
adjoint_burgers_viscous_cell.html
同一个问题,未知量放在网格节点上:两端的行换成边界约束,其余每个节点的行是它周围对偶区间上的守恒平衡。页面与格心页平行(同样的 18 节与三个附录),每个数字都针对这一格式重新推导,并与独立的稀疏 Python 实跑到 65,536 个区间的结果核对,两种格式的差别在页中处处量出。附录:连续伴随、论文中的 Dirichlet–Neumann 格点基准、复现每个数字的方法。The same problem with the unknowns at the mesh nodes: the rows of the two end nodes are replaced by the boundary constraints, and the row of every other node balances the dual interval around it. The page runs parallel to the cell-centred one (the same 18 sections and three appendices); every number is re-derived for this scheme and checked against independent sparse Python runs up to 65,536 intervals, and the pages measure where the two schemes differ. Appendices: the continuous adjoint, the Dirichlet–Neumann nodal benchmark of the paper, and how to reproduce every number.
浏览器 8–128 个区间(9–129 个节点) · Python 已实跑 65,536 个区间
116 幅图、88 个实验与逐步播放器 · 数值证据与待证数学义务分列8–128 intervals (9–129 nodes) in the browser · Python run up to 65,536 intervals
116 figures, 88 labs and step players · numerical evidence and open mathematical obligations listed separately
adjoint_burgers_viscous_node.html
黏性 Burgers 的 Python 程序与实测数据Python program and measured data for viscous Burgers
保留原有正文、网格图和逐句播放器,各节新增光滑特征边界模型的一阶/二阶扩展;同页附录 B 提供对应连续伴随。精度切换仅作用于新增模型。
Original prose, mesh diagrams and players are preserved. Each section adds a first/second-order smooth characteristic-boundary model; Appendix B supplies its continuous adjoint. The order selector controls only the added model.
环形 O 型混合网格,24 个未知量。最小的模型问题:局部导数就是两个数。纯对流,边界条件全部写在通量里。
A hybrid annular O-mesh with 24 unknowns — the smallest model problem, where a local derivative is just two numbers. Pure advection, with every boundary condition inside the flux.
未知量是单元平均值。物面采用零数值通量闭合(连续兼容性限制见附录 A),远场按特征方向取来流值或外推——全部在通量里弱加。The unknowns are cell averages. The body surface is closed by a zero numerical flux (the continuous-compatibility limits are in Appendix A); the far field takes the freestream value or extrapolates along the characteristic direction — all imposed weakly, inside the flux.
24 个单元 · 48 个面 · nnz(A) = 8824 cells · 48 faces · nnz(A) = 88
adjoint_cell.html
未知量在节点上,控制体是中位对偶体。边界条件同样全部在通量里:纯对流方程放不下 Dirichlet 壁面。The unknowns sit at the nodes and the control volumes are median duals. The boundary conditions again live entirely in the flux: a pure advection equation has no room for a Dirichlet wall.
24 个节点 · 80 个面 · nnz(A) = 12024 nodes · 80 faces · nnz(A) = 120
adjoint_node.html
同一张网格、同一个对流通量,加上常数扩散。扩散通量要用最小二乘梯度,残差变成两遍循环;精确 Jacobian 伸到邻居的邻居,Jacobi 迭代只用它的对角元,伴随用精确算子。物面是 Dirichlet 壁面 u = uw。
The same mesh and convective flux, with a constant diffusion added. The diffusive flux needs least-squares gradients, so the residual becomes two loops; the exact Jacobian reaches the neighbours of neighbours, the Jacobi iteration uses only its diagonal, and the adjoint the exact operator. The body is a Dirichlet wall, u = uw.
壁面弱加:uw 进入壁面面的通量与最小二乘模板。目标是流进物面的总通量。The wall is imposed weakly: uw enters the wall-face flux and the least-squares stencil. The objective is the total flux into the body surface.
24 个单元 · nnz(A) = 200 · 一阶 Ã:8824 cells · nnz(A) = 200 · first-order Ã: 88
adjoint_ad_cell.html
壁面节点的整行换成 ui − uw,强加;伴随里「循环前清零、循环后加回」,顺序不能反。The wall node’s whole row is replaced by ui − uw and imposed strongly; in the adjoint the row is “zeroed before the loop and added back after it”, and the order cannot be reversed.
24 个节点 · nnz(A) = 264 · 一阶 Ãbc:9624 nodes · nnz(A) = 264 · first-order Ãbc: 96
adjoint_ad_node.html
变截面流道,入口给总压总温、出口给背压,全场亚声速。方程组版本:局部导数是 3×3 块,一阶/二阶重构可切换,提供块 GS 与 GS 预处理 GMRES、完整差分扫描,以及同页连续伴随推导、独立交互计算和附录 C 的 LU 直接伴随求解。
A variable-area duct with total conditions in, a back pressure out and subsonic flow throughout. For the system, local derivatives are 3×3 blocks; switch first/second-order reconstruction, solve with block GS or GS-preconditioned GMRES, and explore full finite-difference sweeps, an independent continuous adjoint and Appendix C’s direct LU adjoint solve.
边界条件弱加:用特征关系重构一个 ghost 状态喂给通量。边界 Jacobian 的秩等于出域特征条数。Boundary conditions are imposed weakly: a ghost state is reconstructed from the characteristic relations and fed to the flux. The rank of the boundary Jacobian equals the number of outgoing characteristics.
12 个单元 · 36 个未知量 · 一阶/二阶 · matrix-free12 cells · 36 unknowns · first/second order · matrix-free
adjoint_q1d_cell.html
边界节点就在边界上,所以强加——而且是部分行替换:入口 3 行换 2 行,出口换 1 行。The boundary nodes lie on the boundary, so conditions are imposed strongly — by partial row replacement: at the inlet 2 of the 3 rows are replaced, at the outlet 1.
13 个节点 · 39 个未知量 · 3 条约束行13 nodes · 39 unknowns · 3 constraint rows
adjoint_q1d_node.html
等截面的光滑可压缩流:在 Euler 基础上加入轴向黏性应力、黏性功与热传导,不含湍流模型。常物性/Sutherland 物性、一阶/二阶重构、局部加热和制造解,完整求导梯度、物性与边界。
Smooth constant-area compressible flow with molecular stress, viscous work and conduction, without turbulence. Explore constant/Sutherland transport, first/second-order convection, heating and manufactured solutions, with exact derivatives of gradients, transport and boundaries.
逐面演示对流、应力和热流的装配与反传;完整差分、复数残差、独立连续伴随及 LU 对照。Face by face, the assembly and the reverse pass of the convective, stress and heat fluxes; full finite differences, complex residuals, an independent continuous adjoint and an LU comparison.
12 个单元 · 36 个未知量 · 一阶/二阶 · matrix-free12 cells · 36 unknowns · first/second order · matrix-free
adjoint_ns1d_cell.html
真实对偶控制体与端点梯度;入口三条、出口两条约束行,完整保留边界转置。True dual control volumes and endpoint gradients; three constraint rows at the inlet and two at the outlet, with the boundary transpose kept in full.
13 个节点 · 39 个未知量 · 五条约束行13 nodes · 39 unknowns · five constraint rows
adjoint_ns1d_node.html
沿用二维标量的同一张混合 O 型网格,承接拟一维 Euler 的热力学与特征边界。低马赫数绕流,滑移壁面保留压力动量通量;一阶通量、matrix-free tangent/adjoint、GS 预处理 GMRES、物面坐标与来流梯度,以及完整重算差分和复数残差验证。
The scalar example’s identical hybrid mesh, with four Euler variables, slip-wall pressure flux and subsonic characteristic far field. Matrix-free derivatives and GS-preconditioned GMRES are checked by full-rerun finite differences and complex residuals.
每个单元四个守恒量;逐面查看通量、状态与几何导数。压力目标包含直接法向导数。Four conserved quantities per cell; inspect the flux, the state and the geometric derivatives face by face. The pressure objective includes the direct normal derivative.
24 个单元 · 48 个面 · 96 个状态自由度24 cells · 48 faces · 96 state degrees of freedom
adjoint_euler_cell.html
中值对偶面与边界半面;滑移壁面和特征远场均通过通量弱加,完整反传边界依赖。Median-dual faces and boundary half-faces; the slip wall and the characteristic far field are both imposed weakly through the flux, and the boundary dependence is propagated back in full.
24 个节点 · 80 个面 · 96 个状态自由度24 nodes · 80 faces · 96 state degrees of freedom
adjoint_euler_node.html
沿用二维标量混合网格与页面风格,加入完整应力张量、黏性功和热流。光滑制造解与无滑移绕流分别验证离散精度和边界实现,支持一阶/二阶对流重构、常物性/Sutherland、等温/绝热壁面。
The scalar mesh and lesson style with full stress, viscous work and heat flux. Explore smooth manufactured solutions and no-slip flow, first/second-order convection, constant/Sutherland transport and isothermal/adiabatic walls.
守恒平均与几何矩重构;逐面执行原始、tangent 与 adjoint,检查物性、边界和全部节点坐标导数。Conservative averages and geometric-moment reconstruction; the primal, tangent and adjoint are run face by face, checking transport properties, boundaries and the derivatives with respect to all node coordinates.
24 个单元 · 48 个面 · 96 个未知量24 cells · 48 faces · 96 unknowns
adjoint_ns2d_cell.html
中值对偶子面求积与强边界行;差分步长扫描、独立复数残差、GS/GMRES 和显式 LU 对照。Quadrature on the median-dual sub-faces and strong boundary rows; finite-difference step sweeps, independent complex residuals, GS / GMRES and an explicit-LU comparison.
24 个节点 · 80 个面 · 96 个未知量24 nodes · 80 faces · 96 unknowns
adjoint_ns2d_node.html
沿用二维标量对流页的同一张混合网格与页面结构:可压缩 RANS 与标准 SA 模型,Roe 通量、无权最小二乘重构(不加限制器)与 SU2 式边上粘性通量,页首切换一阶/二阶。伪时间加对称块 Gauss–Seidel(近似 Jacobian 为一阶 Roe 与薄剪切层);matrix-free tangent/adjoint 逐遍播放,前向、复数步长与重算差分三种办法核对全部 51 个设计导数。来流边界有两种处理:原来的一对伴随不一致(附录 B 说明原因);另一对伴随一致(格点页来流节点保留质量行、能量行加上动量反作用的功,格心页来流面取边界状态的物理通量),加密时离散伴随在来流附近也趋于连续伴随。四页都有附录 A:同一问题的连续伴随推导、P1 有限元求解与逐级对照。
The scalar-advection pages’ hybrid mesh and lesson structure for compressible RANS with the standard SA model: Roe flux, unweighted least squares without a limiter, SU2-style edge viscous flux, and a first/second-order switch. Pseudo-time with symmetric block Gauss–Seidel on a first-order Roe plus thin-shear-layer Jacobian; matrix-free tangent and adjoint played pass by pass; all 51 design derivatives checked by the tangent, the complex step and re-solved finite differences. The inflow boundary comes in two treatments: the original pair is adjoint-inconsistent (Appendix B explains why), the other pair is adjoint-consistent (inflow nodes keep their mass row and the energy row takes the work of the momentum reaction; cell inflow faces take the boundary state’s physical flux), and under refinement its discrete adjoint approaches the continuous one next to the inflow as well. All four pages carry Appendix A: the continuous adjoint of the same problem, derived, solved by P1 finite elements and compared level by level.
弱条件的无滑移绝热壁:壁面面通量与两组最小二乘权重;梯度、面通量与 SA 源项逐遍执行,伴随倒序转置。来流取 Roe 通量对虚拟状态(伴随不一致)。A weakly imposed no-slip adiabatic wall: the wall-face flux and two sets of least-squares weights; the gradients, face fluxes and SA source term are executed pass by pass, and the adjoint transposes them in reverse order. The inflow takes the Roe flux against a ghost state (adjoint-inconsistent).
24 个单元 · 48 个面 · 120 个未知量24 cells · 48 faces · 120 unknowns
adjoint_rans_cell.html
强条件壁面行与反作用力目标;伴随先处理被替换的行,再倒序执行各遍。来流节点替换四行(伴随不一致)。Strongly imposed wall rows and a reaction-force objective; the adjoint treats the replaced rows first, then runs the passes in reverse. The inflow nodes replace four rows (adjoint-inconsistent).
24 个节点 · 80 个面 · 120 个未知量24 nodes · 80 faces · 120 unknowns
adjoint_rans_node.html
来流面取边界状态的物理通量,不加 Roe 耗散;附录 B 逐级比较两种来流处理下离散伴随与连续伴随之差。The inflow faces take the physical flux of the boundary state, with no Roe dissipation; Appendix B compares, level by level, how far the discrete adjoint is from the continuous one under the two inflow treatments.
24 个单元 · 48 个面 · 120 个未知量24 cells · 48 faces · 120 unknowns
adjoint_rans_dc_cell.html
来流节点保留质量行,只替换两个速度分量与 SA 变量这三行,能量行加上动量反作用的功;离散伴随在来流附近逐级趋于连续伴随。The inflow nodes keep their mass row and replace only the three rows of the two velocity components and the SA variable; the energy row takes the work of the momentum reaction. Near the inflow the discrete adjoint approaches the continuous adjoint level by level.
24 个节点 · 80 个面 · 120 个未知量24 nodes · 80 faces · 120 unknowns
adjoint_rans_dc_node.html
每个页面都是自包含的单个 HTML 文件:不联网、不需要构建、不需要服务器,直接打开即可。页面右上角可切换中文 / English。
Every page is a self-contained single HTML file — no network, no build step, no server. A language toggle (Chinese / English) sits in the top-right corner of each page.
离散交互与 Burgers 连续求积在浏览器现场计算;其他连续参考及加密图浏览独立求解后内嵌的离线结果。正文数值均来自实际计算。
The discrete interactive parts and the Burgers continuous quadrature are computed live in the browser; the other continuous references and the refinement plots show offline results that were solved independently and embedded in the page. Every number in the text comes from an actual computation.