Adjoint code development and optimization using automatic differentiation (AD) Praveen. C Computational and Theoretical Fluid Dynamics Division National Aerospace Laboratories Bangalore - 560 037 CTFD Seminar, March 31, 2006 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 1 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 2 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 3 / 56
Mathematical formulation Constrained minimization problem Find δα such that δj < 0 min α J(α, u) subject to R(α, u) = 0 δj = J J δα + α u δu = J J u δα + α u α δα = Gδα Steepest descent δα = ɛg δj = ɛgg = ɛ G 2 < 0 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 4 / 56
Mathematical formulation Constrained minimization problem min α J(α, u) subject to R(α, u) = 0 Find δα such that δj < 0 δj = J J δα + α u δu = J J u δα + α u α δα = Gδα Steepest descent δα = ɛg δj = ɛgg = ɛ G 2 < 0 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 5 / 56
Sensitivity approach Linearized state equation or R R δα + α u δu = 0 R u u α = R α Solve sensitivity equation iteratively u t α + R u u α = R α Same eigenvalues as original system Costly if number of α are large Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 6 / 56
Adjoint approach We have δj = J J R R δα + δu and δα + α u α u δu = 0 Introduce a new unknown v δj = J ( ) J R δα + α u δu + v R δα + α u δu = Adjoint equation Iterative solution ( J α + v R α ) δα + ( ) R ( ) J v = u u v t + ( ) R v = u ( J u + v R u ( ) J u ) δu Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 7 / 56
Continuous vs Discrete Continuous approach: Start with governing PDE Derive adjoint PDE and boundary conditions Discretize adjoint PDE and solve Must be re-derived whenever cost function changes Not all cost functions admissible Discrete approach: Start with discrete approximation like FVM Derive discrete adjoint equations Solve discrete adjoint equations True sensitivity of discrete solution Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 8 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 9 / 56
Techniques for computing gradients Hand differentiation Finite difference method Complex variable method Automatic differentiation Computer code to compute some function Chain rule of differentiation Generates a code to compute derivatives ADIFOR, ADOLC, ODYSEE, TAMC, TAF, TAPENADE Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 10 / 56
Differentiation: Example Computer code, f = t 10 t 1 = x A simple example f = (xy + sin x + 4)(3y 2 + 6) t 2 = y t 3 = t 1 t 2 t 4 = sin t 1 t 5 = t 3 + t 4 t 6 = t 5 + 4 t 7 = t 2 2 t 8 = 3t 7 t 9 = t 8 + 6 t 10 = t 6 t 9 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 11 / 56
Automatic Differentiation: F77 code subroutine costfunc(x, y, f) t1 = x t2 = y t3 = t1*t2 t4 = sin(t1) t5 = t3 + t4 t6 = t5 + 4 t7 = t2**2 t8 = 3.0*t7 t9 = t8 + 6.0 t10 = t6*t9 f = t10 end Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 12 / 56
Differentiation: Direct mode Some definitions I k := {i : i < k and t k depends explicitly on t i } ṫ i := t i α, Apply chain rule of differentiation t i,k := t i t k α = x, or y ṫ k = t k α = i I k t i t k = ṫ i t k,i k = 1, 2,..., 9, 10 α t i i I k Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 13 / 56
Differentiation: Direct mode Apply chain rule of differentiation t 1 = x ṫ 1 = ẋ t 2 = y ṫ 2 = ẏ t 3 = t 1 t 2 ṫ 3 = ṫ 1 t 2 + t 1 ṫ 2 t 4 = sin(t 1 ) ṫ 4 = cos(t 1 )ṫ 1 t 5 = t 3 + t 4 ṫ 5 = ṫ 3 + ṫ 4 t 6 = t 5 + 4 ṫ 6 = ṫ 5 t 7 = t2 2 ṫ 7 = t 8 = 3t 7 ṫ 8 = 2t 2 ṫ 2 3ṫ 7 t 9 = t 8 + 6 ṫ 9 = ṫ 8 t 10 = t 6 t 9 ṫ 10 = ṫ 6 t 9 + t 6 ṫ 9 ẋ = 1, ẏ = 0, ṫ 10 = f x and ẋ = 0, ẏ = 1, ṫ 10 = f y tapenade -d -vars "x y" -outvars f costfunc.f Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 14 / 56
Automatic Differentiation: Direct mode SUBROUTINE COSTFUNC_D(x, xd, y, yd, f, fd) t1d = xd t1 = x t2d = yd t2 = y t3d = t1d*t2 + t1*t2d t3 = t1*t2 t4d = t1d*cos(t1) t4 = SIN(t1) t5d = t3d + t4d t5 = t3 + t4 t6d = t5d t6 = t5 + 4 t7d = 2*t2*t2d t7 = t2**2 t8d = 3.0*t7d t8 = 3.0*t7 t9d = t8d t9 = t8 + 6.0 t10d = t6d*t9 + t6*t9d t10 = t6*t9 fd = t10d f = t10 END Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 15 / 56
Differentiation: Reverse mode Some definitions J k := {i : i > k and t i depends explicitly on t k } t i := f t i = t 10 t i, t i,k := t i t k Apply chain rule of differentiation in reverse t k = t 10 = t 10 t i = t i t i,k k = 10, 9,..., 2, 1 t k t i t k i J k i J k Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 16 / 56
Differentiation: Reverse mode Apply chain rule of differentiation in reverse t 1 = x t 10 = 1 t 2 = y t 9 = t 10 t 10,9 = t 6 t 3 = t 1 t 2 t 8 = t 9 t 9,8 = t 6 t 4 = sin(t 1 ) t 7 = t 8 t 8,7 = 3t 6 t 5 = t 3 + t 4 t 6 = t 10 t 10,6 = t 9 t 6 = t 5 + 4 t 5 = t 6 t 6,5 = t 9 t 7 = t2 2 t 4 = t 5 t 5,4 = t 9 t 8 = 3t 7 t 3 = t 5 t 5,3 = t 9 t 9 = t 8 + 6 t 2 = t 7 t 7,2 + t 3 t 3,2 = 6t 2 t 6 + t 1 t 9 t 10 = t 6 t 9 t 1 = t 4 t 4,1 + t 3 t 3,1 = t 9 cos(t 1 ) + t 9 t 2 t 1 = f x, t 2 = f y tapenade -b -vars "x y" -outvars f costfunc.f Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 17 / 56
Automatic Differentiation: Reverse mode SUBROUTINE COSTFUNC_B(x, xb, y, yb, f, fb) t1 = x t2 = y t3 = t1*t2 t4 = SIN(t1) t5 = t3 + t4 t6 = t5 + 4 t7 = t2**2 t8 = 3.0*t7 t9 = t8 + 6.0 t10b = fb t6b = t9*t10b t9b = t6*t10b t8b = t9b t7b = 3.0*t8b t5b = t6b t3b = t5b t2b = t1*t3b + 2*t2*t7b t4b = t5b t1b = t2*t3b + COS(t1)*t4b yb = t2b xb = t1b fb = 0.0 END Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 18 / 56
Direct versus reverse AD Direct mode: cost is proportional to number of independent variables Reverse mode: cost does not depend on the number of independent variables Reverse mode preferred when number of independent variables is large Scalar output f Direct mode gives f s for given vector s Reverse mode gives f Vector output R R n Direct mode gives R s for given vector s Reverse mode gives ( R) s Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 19 / 56
Issues in reverse AD Intermediate variables required in reverse order Some variables may be over-written Iterative solvers Only final solution required AD differentiates the iterative loop Intermediate solutions stored in stack Huge memory requirements Not practical for large problems Piecemeal differentiation approach (Courty et al., Giles et al.): Modular flow solver Adjoint solver written manually AD for differentiating the modules Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 20 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 21 / 56
Discrete 1-D adjoint equations Finite volume residual for i th cell Numerical flux function Perturbation equation δr i = R i := F i+1/2 F i 1/2 = 0 F = F (X, Y ), F i+1/2 = F (U i, U i+1 ) X F i+1/2δu i + Y F i+1/2δu i+1 X F i 1/2δU i 1 Y F i 1/2δU i + R i α δα = 0 Introduce adjoint variable V i for i th cell δj = J α δα + i J U i δu i + i Vi δr i Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 22 / 56
Discrete adjoint equations Collecting terms containing δu i δj = i [ J U i + V i 1 ( + Vi Vi+1 Y F i 1/2 X F i+1/2 ) Y F i 1/2 ] X F i+1/2 δu i +... Adjoint equation for i th cell ( ) J ( ) + U i Y F i 1/2 V i 1 + ( X F i+1/2 ) Y F i 1/2 V i ( ) X F i+1/2 V i+1 = 0 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 23 / 56
Example flow solver While u is not converged res = 0.0 fluxinflow(u(1), res(1)) do i=1,n-1 fluxinterior(u(i), u(i+1), res(i), res(i+1)) enddo fluxoutflow(u(n), res(n)) do i=1,n u(i) = u(i) (dt/dx)*res(i) enddo endwhile cost=0.0 do i=1,n costfunc(u(i), cost) enddo Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 24 / 56
Example adjoint solver costb=1.0 do i=1,n costfunc_b(u(i), ub1(i), cost, costb) enddo v=0.0 While v is not converged ub2 = 0.0 fluxinflow_b(u(1), ub2(1), res(1), v(1)) do i=1,n-1 fluxinterior_b(u(i), ub2(i), u(i+1), ub2(i+1), res(i), v(i), res(i+1), v(i+1)) enddo fluxoutflow_b(u(n), ub2(n), res(n), v(n)) do i=1,n v(i) = v(i) (dt/dx)*(ub1(i) + ub2(i)) enddo endwhile Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 25 / 56
Quasi 1-D flow Quasi 1-D flow in a duct t (hu) + dh (hf ) = P, x dx x (a, b) t > 0 ρ ρu 0 U = ρu E, f = p + ρu 2 (E + p)u, P = p 0 h(x) = cross-section height of duct Inverse design: find shape h to get pressure distribution p Optimization problem: find the shape h which minimizes J = b a (p p ) 2 dx Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 26 / 56
Quasi 1-D flow Finite volume scheme h i du i dt + h i+1/2f i+1/2 h i 1/2 F i 1/2 x Discrete cost function = (h i+1/2 h i 1/2 ) P i x J = N (p i pi )2 i=1 Control variables N = 100 h 1/2, h 1+1/2,..., h i+1/2,..., h N+1/2 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 27 / 56
Duct shape 1.9 1.8 1.7 Duct shape Target shape Current shape 1.6 h 1.5 1.4 1.3 1.2 1.1 1 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 28 / 56
Target pressure distribution p 2.5 Target pressure AUSMDV KFVS LF 2 Pressure 1.5 1 0.5 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 29 / 56
Current pressure distribution 2.5 Starting pressure AUSMDV KFVS LF 2 Pressure 1.5 1 0.5 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 30 / 56
Adjoint density 5 0 AUSMDV KFVS LF 5 Adjoint density 10 15 20 25 30 35 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 31 / 56
Convergence history: Explicit Euler 0 1 Convergence history with AUSMDV flux Flow Adjoint Residue 2 3 4 5 6 7 8 9 10 1000 2000 3000 4000 5000 6000 7000 No of iterations Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 32 / 56
Shape gradient Gradient 35 30 AUSMDV KFVS LF 25 Gradient 20 15 10 5 0 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 33 / 56
Shape gradient Gradient 40 35 30 25 20 15 10 Gradient AUSMDV KFVS LF 5 0 5 2 2.5 3 3.5 4 4.5 5 5.5 6 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 34 / 56
Validation of Shape gradient 35 Gradient with AUSMDV flux 30 25 B Gradient 20 15 10 5 A C 0 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 35 / 56
Validation of shape gradient J J(h + h) J(h h) h 2 h h A B C 0.01 0.4191069499 35.18452823 2.545316345 0.001 0.4231223499 36.10982621 2.556461900 0.0001 0.4231624999 36.11933154 2.556573499 0.00001 0.4231599998 36.11942125 2.556575000 0.000001 0.4231999994 36.11942305 2.556550001 0.0000001 0.4229999817 36.11942329 2.556499971 AD 0.4231628330 36.11941951 2.556574450 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 36 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 37 / 56
Gradient smoothing Non-smooth gradients especially in the presence of shocks Smooth using an elliptic equation ( 1 ɛ i d 2 dx 2 ) ḡ i = g i L i = ɛ i = { g i+1 g i + g i g i 1 }L i g i+1 2g i + g i 1 max( g i+1 g i + g i g i 1, TOL) Finite difference with Jacobi iterations Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 38 / 56
Gradient smoothing Gradient using AUSMDV flux 35 30 Original gradient Smoothed gradient 25 Gradient 20 15 10 5 0 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 39 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 40 / 56
Quasi 1-D optimization: Shape 1.9 1.8 Target Initial 1.7 1.6 1.5 h 1.4 1.3 1.2 1.1 1 0 1 2 3 4 5 6 7 8 9 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 41 / 56
Quasi 1-D optimization: Final shape 1.9 1.8 1.7 1.6 1.5 h 1.4 1.3 Target Initial Final 1.2 1.1 1 0 1 2 3 4 5 6 7 8 9 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 42 / 56
Quasi 1-D optimization: Pressure 2.5 2 Pressure 1.5 Target Initial Final 1 0.5 0 1 2 3 4 5 6 7 8 9 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 43 / 56
Quasi 1-D optimization: Convergence 10 0 Cost Gradient 10 1 10 2 Cost/Gradient 10 3 10 4 10 5 10 6 0 5 10 15 20 25 30 35 40 45 50 No. of iterations Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 44 / 56
Quasi 1-D optimization: Adjoint density 0 5 Adjoint density 10 15 Initial Final 20 25 0 2 4 6 8 10 x Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 45 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 46 / 56
2-D finite volume flow solver Unstructured triangular grid Vertex-centroid scheme (Frink, Jameson) Vertex values obtained using inverse-distance averaging with correction for linear consistency p i u p = w i i r i u i w i i r i L R Convergence acceleration using matrix-free LUSGS with spectral radius approximation Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 47 / 56
2-D finite volume adjoint solver Vertex averaging including weights are differentiated For transonic flow limiter is also differentiated Adjoint solver requires 38% more memory compared to flow solver Time per adjoint iteration = Twice the time per flow solver iteration Convergence acceleration using LUSGS with spectral-radius approximation Automated using Makefile Adjoint validation for J(α) = C l Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 48 / 56
Grid for NACA-0012: 160 40 Number of vertices = 6560 Number of triangles = 12800 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 49 / 56
Validation of adjoints: Lift-curve slope, subsonic flow 0.35 0.3 0.25 C l 0.2 0.15 0.1 0.05 0 0 0.5 1 1.5 2 2.5 3 Angle of attack in degrees Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 50 / 56
Validation of adjoints: Lift-curve slope, transonic flow 0.8 0.7 0.6 0.5 C l 0.4 0.3 0.2 0.1 0 0 0.5 1 1.5 2 2.5 3 Angle of attack in degrees Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 51 / 56
Lift-curve slope: C lα AOA M = 0.4 M = 0.8 C lα C lα /Cl th α C lα C lα /Cl th α 0.0 7.2095 1.0516 13.7506 1.3130 0.5 7.2069 1.0512 14.0556 1.3422 1.0 7.2000 1.0502 13.9544 1.3325 1.5 7.1901 1.0487 13.2116 1.2616 2.0 7.1789 1.0471 12.8314 1.2253 2.5 7.1675 1.0455 11.4077 1.0893 3.0 7.1562 1.0438 10.7135 1.0230 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 52 / 56
Convergence history: α = 3 o M = 0.4 M = 0.8 100 Flow Adjoint 10 Flow Adjoint 10 1 1 0.1 0.1 0.01 Residue 0.01 0.001 Residue 0.001 1e-04 1e-04 1e-05 1e-05 1e-06 1e-06 1e-07 0 2000 4000 6000 8000 10000 12000 1e-07 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 No. of iterations No. of iterations Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 53 / 56
Adjoint density for lift coefficient: α = 3 o M = 0.4 M = 0.8 Adjoint_Density, min = -57.979, max = 58.0587 Adjoint_Density, min = -140.596, max = 134.291 Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 54 / 56
Outline 1 Mathematical formulation 2 Computing gradients 3 Quasi 1-D flow 4 Gradient smoothing 5 Quasi 1-D optimization: Pressure matching 6 2-D adjoint Euler solver 7 Future work Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 55 / 56
Future work How to deform the grid? How to account for grid perturbation? Smoothing of gradients in 2-D Application to 2-D optimization problems Extension to viscous flows Grid adaptation Praveen. C (CTFD, NAL) Adjoints and optimization CTFD Seminar 56 / 56