Description of AdjointShapeOptimizationFoam and how to implement new cost functions A project work in the course CFD with OpenSource software, taught by Håkan Nilsson. Chalmers University of Technology, Gothenburg, Sweden December 10, 2013 December 10, 2013 1 /
Overview Introduction Theory Implementation Modifications Results December 10, 2013 2 /
Introduction Optimization in CFD Increased importance Optimization algorithm - Simplex - Evolution Strategies - Gradient based -... Demanding - Number of variables - Constraints December 10, 2013 3 /
Introduction Adjoint approach Gradient based method Tool to compute sensitivity w.r.t. design variables Well behaved objective function Large number of design variables Constraints, number of functions December 10, 2013 4 /
Optimization problem Objective function minimize J = J(v, p, α) (1) such that (v )v + p (2νD(v)) + αv = 0, (2) v = 0. (3) Gerneral cost function Incompressible, steady state RANS equation Porosity α Topology optimization December 10, 2013 5 /
Optimization problem Lagrangian relaxation minimize L := J + Lagrange multipliers, (u, q) A penalty to violate the constraints Adjoint velocity, u, and pressure, q No physical meaning Ω (u, q)rdω. (4) December 10, 2013 6 /
Optimization problem Variations w.r.t. flow and design variables δl = δ α L + δ v L + δ p L, (5) δ v L + δ p L = 0. (6) Total variation. Choose adjoint variables so that the variation with respect to flow variables vanishes. Equation of the adjoint variables and an expression for the sensitivity of L with respect to the porosity of each cell, derivations out of the scope of the project. Approximations needed to arrive at the adjoint equations. - Forzen turbulence. - Ducted flow specialization. Integration by parts to arrive at volume contribution and boundary contribution. December 10, 2013 7 /
Resulting equations Sensitivity Expression for cell i V i the cell volume. L α i = u i v i V i, (7) Equation of the adjoint variables and an expression for the sensitivity of L with respect to the porosity of each cell December 10, 2013 8 /
Resulting equations Adjoint equations 2D(u)v = q + (2νD(u)) αu J Ω v, (8) u = J Ω p. (9) Contribution from volumetric part of the cost function, J Ω. December 10, 2013 9 /
Resulting equations Adjoint boundary conditions Wall and inlet boundary conditions Outlet boundary conditions u t = 0, (10) u n = J Ω p, (11) n q = 0. (12) q = u v + u n v n + ν(n )u n + J Γ v n, (13) 0 = v n u t + ν(n )u t + J Γ v t. (14) December 10, 2013 10 /
Resulting equations Adjoint boundary conditions Wall Inlet Outlet v No-slip Prescribed value Zero gradient p Zero gradient Zero gradient p = 0 Table: Boundary conditions for the primal quantities v and p. Only valid for specific primal boundary condition. Contribution from both the volume and boundary part of the cost function, J Ω and J Γ respectively. Eq. 13 and 14 used to calculate the adjoint pressure and tangential part of the adjoint velocity at the outlet. December 10, 2013 11 /
Resulting equations Summary of the theory Introducing a Lagrange relaxation, with multipliers u and q. Choice of adjoint variables gives adjoint equations and an expression for the sensititivity. Lengthy derivations. Approximations and requirements to ensure a valid method. If the cost function satisfies J ω = 0 the dependence of J enters the solver only through the boundary conditions. The optimization problem reduces to solving two flow equations, the primal and the adjoint. When the sensitivity is calculated an algorithm is needed to update the porosity. December 10, 2013 12 /
Steepest descent algorithm Steepest descent p k = f(x k ), (15) α n+1 = α n u i v i V i δ. (16) General search direction in the opposite direction of the gradient of the objective function. Considering a linear problem a minimum will be found for small enough steps in the search direction. Using the sensitivity, L/ α to update. The step length δ. An optimization problem of its own. Under relaxation and limits in the final implementation. December 10, 2013 13 /
Solver Solution procedure Main-loop Update the porosity Steepest descent Solve the primal system (v, p) Solve the adjoint system (u, q) Frozen tubulence Convergence assessment End time or convergence condition Converged End Figure 1: Block scheme of the solution procedure used in. December 10, 2013 14 /
Solver $FOAM SOLVERS/incompressible/ /.C Let us study the implementation together, trying to identify the underlying theory we have covered so far. In the order it is implemented in the code. Porosity update - Implementation of the steepest descent using under relaxation. - Correct? The primal pressure-velocity SIMPLE corrector - How is the sink term implemented? The adjoint pressure-velocity SIMPLE corrector - Is the current implementation done for objective functions with volumteric contribution (J Ω 0)? December 10, 2013 15 /
Solver Porosity update 94 a l p h a += 95 mesh. f i e l d R e l a x a t i o n F a c t o r ( "alpha" ) 96 ( min ( max( alpha + lambda (Ua & U), zeroalpha ), alphamax ) alpha ) ; Listing 1: file.c α n+1 = α n (1 γ) + γmin (max ((α n + u i v i V i δ).0), α max ), (17) December 10, 2013 16 /
Solver Porosity update However the there seems to be an incorrect sign in the steepest descent implementation. The correct eq. and implementation should be α n+1 = α n (1 γ) + γmin (max ((α n u i v i V i δ), 0), α max ). (18) 94 a l p h a += 95 mesh. f i e l d R e l a x a t i o n F a c t o r ( "alpha" ) 96 ( min ( max( alpha lambda (Ua & U), zeroalpha ), alphamax ) alpha ) ; Listing 2: file.c December 10, 2013 17 /
Solver Primal SIMPLE loop 103 // Momentum p r e d i c t o r 104 105 tmp<f v V e c t o r M a t r i x > UEqn 106 ( 107 fvm : : d i v ( phi, U) 108 + t u r b u l e n c e >d i v D e v R e f f (U) 109 + fvm : : Sp ( alpha, U) 110 ) ; 111 112 UEqn ( ). r e l a x ( ) ; 113 114 s o l v e (UEqn ( ) == f v c : : grad ( p ) ) ; Listing 3: file.c December 10, 2013 18 /
Solver Primal SIMPLE loop fvm::sp(alpha, U) Implicit implementation of the extra source term. December 10, 2013 19 /
Solver Adjoint SIMPLE loop 156 // A d j o i n t Momentum p r e d i c t o r 157 158 volvectorfield adjointtransposeconvection ( ( fvc : : grad (Ua) & U ) ) ; 159 // v o l V e c t o r F i e l d a d j o i n t T r a n s p o s e C o n v e c t i o n 160 // ( 161 // fvc : : reconstruct 162 // ( 163 // mesh. magsf ( ) ( f v c : : sngrad (Ua) & f v c : : i n t e r p o l a t e (U) ) 164 // ) 165 // ) ; 166 167 z e r o C e l l s ( a d j o i n t T r a n s p o s e C o n v e c t i o n, i n l e t C e l l s ) ; 168 169 tmp<f v V e c t o r M a t r i x > UaEqn 170 ( 171 fvm : : d i v ( phi, Ua) 172 a d j o i n t T r a n s p o s e C o n v e c t i o n 173 + t u r b u l e n c e >d i v D e v R e f f (Ua) 174 + fvm : : Sp ( alpha, Ua) 175 ) ; 176 177 UaEqn ( ). r e l a x ( ) ; 178 179 s o l v e ( UaEqn ( ) == f v c : : grad ( pa ) ) ; Listing 4: file.c December 10, 2013 20 /
Solver Adjoint SIMPLE loop Similar to the primal system. No additional terms containing information about the cost function, i.e. current implementation treats cost functions of the type J Γ = 0 December 10, 2013 21 /
Boundary conditions of the adjoint variables Adjoint boundary conditions The bad results when the sign in the steepest descent algorithm is changed indicates additional problems with the current solver. Since the actual solver seems to agree with the theory, what remains to be examined is the boundary conditions. First the boundary condition for a specific cost function need to be calculated. December 10, 2013 22 /
Boundary conditions of the adjoint variables Power dissipation J := Only boundary contribution. Γ (p + 1 2 v2 )v n dγ. (19) Energy loss, due to pressure drop and kinetic energy. December 10, 2013 23 /
Boundary conditions of the adjoint variables Inlet boundary conditions u t = 0, (20) u n = J Ω p, (21) n q = 0. (22) u t = { 0, u n = n q = 0. 0 at wall, v n at inlet, (23) December 10, 2013 24 /
Boundary conditions of the adjoint variables Inlet boundary conditions Use existing conditions - Prescribed value - No slip - Zero-gradient Could be useful to implement a inlet boundary condition for the adjoint velocity, if the velocity profile of the primal velocity at the inlet is not easily described. December 10, 2013 25 /
Boundary conditions of the adjoint variables Outlet boundary conditions q = u v + u n v n + ν(n )u n + J Γ v n, (24) 0 = v n u t + ν(n )u t + J Γ v t. (25) { q = u v + u n v n + ν(n )u n 1 2 v2 v 2 n, 0 = v n (u t v t ) + ν(n )u t. (26) December 10, 2013 26 /
Boundary conditions of the adjoint variables Outlet boundary conditions Use eq. 26 to prescribe a value to the adjoint pressure Use eq. 26 to prescribe a value to the tangential component of the adjoint velocity u t = ν u neigh,t v p,n v p,t v p,n + ν, (27) December 10, 2013 27 /
Boundary conditions of the adjoint variables Implementation of pressure condition 86 // Member F u n c t i o n s // 87 88 void Foam : : adjointoutletpressurefvpatchscalarfield : : updatecoeffs ( ) 89 { 90 i f ( updated ( ) ) 91 { 92 return ; 93 } 94 95 const fvspatchfield <scalar>& phip = 96 patch ( ). lookuppatchfield <s u r f a c e S c a l a r F i e l d, s c a l a r >("phi" ) ; 97 98 const fvspatchfield <scalar>& phiap = 99 patch ( ). lookuppatchfield <s u r f a c e S c a l a r F i e l d, s c a l a r >("phia" ) ; 100 101 const f v P a t c h F i e l d <v e c t o r >& Up = 102 patch ( ). lookuppatchfield <v o l V e c t o r F i e l d, vector >("U" ) ; 103 104 const f v P a t c h F i e l d <v e c t o r >& Uap = 105 patch ( ). lookuppatchfield <v o l V e c t o r F i e l d, vector >("Ua" ) ; 106 107 operator==((phiap /patch ( ). magsf ( ) 1.0) phip /patch ( ). magsf ( ) + (Up & Uap ) ) ; 108 109 fixedvaluefvpatchscalarfield : : updatecoeffs ( ) ; 110 } Listing 5: file adjointoutletpressurefvpatchscalarfield.c December 10, 2013 28 /
Boundary conditions of the adjoint variables Implementation of velocity condition 83 // Member F u n c t i o n s // 84 85 // Update the coefficients associated with the patch f i e l d 86 void Foam : : adjointoutletvelocityfvpatchvectorfield : : updatecoeffs ( ) 87 { 88 i f ( updated ( ) ) 89 { 90 return ; 91 } 92 93 const fvspatchfield <scalar>& phiap = 94 patch ( ). lookuppatchfield <s u r f a c e S c a l a r F i e l d, s c a l a r >("phia" ) ; 95 96 const f v P a t c h F i e l d <v e c t o r >& Up = 97 patch ( ). lookuppatchfield <v o l V e c t o r F i e l d, vector >("U" ) ; 98 99 s c a l a r F i e l d Un(mag( patch ( ). n f ( ) & Up ) ) ; 100 vectorfield UtHat ( (Up patch ( ). nf ( ) Un ) / (Un + SMALL ) ) ; 101 102 v e c t o r F i e l d Uan ( patch ( ). nf ( ) ( patch ( ). n f ( ) & p a t c h I n t e r n a l F i e l d ( ) ) ) ; 103 104 vectorfield : : operator=(phiap patch ( ). Sf ( ) / sqr ( patch ( ). magsf ( ) ) + UtHat ) ; 105 // vectorfield : : operator=(uan + UtHat ) ; 106 107 fixedvaluefvpatchvectorfield : : updatecoeffs ( ) ; 108 } Listing 6: file adjointoutletvelocityfvpatchvectorfield.c December 10, 2013 29 /
Boundary conditions of the adjoint variables Analyzing the implementation Listing 5 gives an implementation of the pressure condition according to compare to q = (u n 1)v n + u v. (28) q = u v + u n v n + ν(n )u n 1 2 v2 v 2 n, (29) Listing 6 gives an implementation of the velocity condition according to compare to u p,t = v p v p,n u p,n + SMALL. (30) u t = ν u neigh,t v p,n v p,t v p,n + ν, (31) December 10, 2013 30 /
Boundary conditions of the adjoint variables Analyzing the theory Not equal to the theory. Another cost function, total pressure loss. Approximations or derivations done makes the cost function hard to identify. December 10, 2013 31 /
Analyzing the theory Changing the sign of the steepest descent implementation. Implementing the cost functions according to derived equations. December 10, 2013 32 /
Adjoint pressure boundary condition Similar to the existing implementation A few additional terms - Velocity in the neighbouring node - Effective viscosity const scalarfield& deltainv = patch ( ). deltacoeffs ( ) ; // distance ˆ( 1) between patch and neighbouring node. // create the object needed to get the v i s c o u s i t y : const incompressible : : RASModel& rasmodel = db ( ). lookupobject<incompressible : : RASModel>("RASProperties" ) ; // n u e f f from the t u r u l e n c e model, rasmodel : s c a l a r F i e l d n u e f f = rasmodel. n u E f f ( ) ( ). b o u n d a r y F i e l d ( ) [ patch ( ). i n d e x ( ) ] ; // Neighbouring node s v e l o c i t y ( normal component ) : s c a l a r F i e l d Uaneigh n = (Uap. p a t c h I n t e r n a l F i e l d ( ) & patch ( ). nf ( ) ) ; Listing 7: file myadjointoutletvelocityfvpatchvectorfield.c December 10, 2013 33 /
Adjoint velocity boundary condition Vector fields instead of scalar fields // patchinternalfield to get the adjoint v e l o c i t y of neighbouring node. vectorfield Uaneigh = Uap. patchinternalfield ( ) ; // I t s tangential and normal components vectorfield Uaneigh n = ( Uaneigh & normal ) normal ; v e c t o r F i e l d Uaneigh t = Uaneigh Uaneigh n ; Listing 8: file myadjointoutletvelocityfvpatchvectorfield.c December 10, 2013 34 /
Case settings The boundary conditions of the primal and adjoint variables are set according to the table below. More detailed information and m4 script of the box example case can be found in the provided files. Wall Inlet Outlet u Prescribed value, 0 Prescribed value, u n = v n adjointoutletvelocitypower q Zero gradient Zero gradient adjointoutletpressurepower December 10, 2013 35 /
Power dissipation pitzdaily porosity field December 10, 2013 36 /
Power dissipation pitzdaily value of the objective function 0.006 valueobjfunc.xy 0.005 0.004 J 0.003 0.002 0.001 0 0 500 1000 1500 2000 2500 3000 Iteration step December 10, 2013 37 /
Power dissipation pitzdaily value of the objective function, without porosity update 0.006 valueobjfunc.xy 0.005 0.004 J 0.003 0.002 0.001 0 0 500 1000 1500 2000 2500 3000 Iteration step December 10, 2013 38 /
Power dissipation Pipe bend example December 10, 2013 39 /
Power dissipation Thank you for listening! Questions December 10, 2013 /