description of adjointshapeoptimizationfoam and how to...

40
Overview Introduction Theory Implementation Modifications Results Description of AdjointShapeOptimizationFoam and how to implement new cost functions Ulf Nilsson A project work in the course CFD with OpenSource software, taught by akan Nilsson. Chalmers University of Technology, Gothenburg, Sweden December 10, 2013 Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 1 / 40

Upload: phamdieu

Post on 21-Jan-2019

256 views

Category:

Documents


0 download

TRANSCRIPT

Overview Introduction Theory Implementation Modifications Results

Description of AdjointShapeOptimizationFoamand how to implement new cost functions

Ulf Nilsson

A project work in the courseCFD with OpenSource software,

taught byHakan Nilsson.

Chalmers University of Technology,Gothenburg, Sweden

December 10, 2013

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 1 / 40

Overview Introduction Theory Implementation Modifications Results

Overview

I Introduction

I Theory

I Implementation

I Modifications

I Results

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 2 / 40

Overview Introduction Theory Implementation Modifications Results

Introduction

Optimization in CFD

I Increased importanceI Optimization algorithm

- Simplex- Evolution Strategies- Gradient based- ...

I Demanding

- Number of variables- Constraints

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 3 / 40

Overview Introduction Theory Implementation Modifications Results

Introduction

Adjoint approach

I Gradient based method

I Tool to compute sensitivity w.r.t. design variables

I Well behaved objective function

I Large number of design variables

I Constraints, number of functions

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 4 / 40

Overview Introduction Theory Implementation Modifications Results

Optimization problem

Objective function

minimize J = J(v, p, α) (1)

such that

(v · ∇)v +∇p−∇ · (2νD(v)) + αv = 0, (2)

∇ · v = 0. (3)

I Gerneral cost function

I Incompressible, steady state RANS equation

I Porosity α

I Topology optimization

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 5 / 40

Overview Introduction Theory Implementation Modifications Results

Optimization problem

Lagrangian relaxation

minimize L := J +

∫Ω

(u, q)RdΩ. (4)

I Lagrange multipliers, (u, q)

I A penalty to violate the constraints

I Adjoint velocity, u, and pressure, q

I No physical meaning

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 6 / 40

Overview Introduction Theory Implementation Modifications Results

Optimization problem

Variations w.r.t. flow and design variables

δL = δαL+ δvL+ δpL, (5)

δvL+ δpL = 0. (6)

I Total variation.I Choose adjoint variables so that the variation with respect to flow

variables vanishes.I Equation of the adjoint variables and an expression for the sensitivity

of L with respect to the porosity of each cell, derivations out of thescope of the project.

I Approximations needed to arrive at the adjoint equations.- Forzen turbulence.- Ducted flow specialization.

I Integration by parts to arrive at volume contribution and boundarycontribution.

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 7 / 40

Overview Introduction Theory Implementation Modifications Results

Resulting equations

Sensitivity

∂L

∂αi= ui · viVi, (7)

I Expression for cell i

I Vi the cell volume.

I Equation of the adjoint variables and an expression for the sensitivityof L with respect to the porosity of each cell

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 8 / 40

Overview Introduction Theory Implementation Modifications Results

Resulting equations

Adjoint equations

−2D(u)v = −∇q +∇ · (2νD(u))− αu− ∂JΩ

∂v, (8)

∇ · u =∂JΩ

∂p. (9)

I Contribution from volumetric part of the cost function, JΩ.

Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 9 / 40

Overview Introduction Theory Implementation Modifications Results

Resulting equations

Adjoint boundary conditions

Wall and inlet boundary conditions

ut = 0, (10)

un = −∂JΩ

∂p, (11)

n · ∇q = 0. (12)

Outlet boundary conditions

q = u · v + unvn + ν(n · ∇)un +∂JΓ

∂vn, (13)

0 = vnut + ν(n · ∇)ut +∂JΓ

∂vt. (14)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 10 /

40

Overview Introduction Theory Implementation Modifications Results

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.

I Only valid for specific primal boundary condition.

I Contribution from both the volume and boundary part of the costfunction, JΩ and JΓ respectively.

I Eq. 13 and 14 used to calculate the adjoint pressure and tangentialpart of the adjoint velocity at the outlet.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 11 /

40

Overview Introduction Theory Implementation Modifications Results

Resulting equations

Summary of the theory

I Introducing a Lagrange relaxation, with multipliers u and q.

I Choice of adjoint variables gives adjoint equations and an expressionfor the sensititivity.

I Lengthy derivations.

I Approximations and requirements to ensure a valid method.

I If the cost function satisfies Jω = 0 the dependence of J enters thesolver only through the boundary conditions.

I The optimization problem reduces to solving two flow equations, theprimal and the adjoint.

I When the sensitivity is calculated an algorithm is needed to updatethe porosity.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 12 /

40

Overview Introduction Theory Implementation Modifications Results

Steepest descent algorithm

Steepest descent

pk = −∇f(xk), (15)

αn+1 = αn − ui · viViδ. (16)

I General search direction in the opposite direction of the gradient ofthe objective function.

I Considering a linear problem a minimum will be found for smallenough steps in the search direction.

I Using the sensitivity, ∂L/∂α to update.

I The step length δ. An optimization problem of its own.

I Under relaxation and limits in the final implementation.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 13 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Solution procedure

Main-loop

Solve theprimal system

(v, p)

Solve theadjoint system

(u, q)

Update theporosity

Steepest descent

ConvergenceassessmentEnd time or

convergence condition

EndConverged

“Frozen tubulence”

Figure 1: Block scheme of the solution procedure used in adjointShapeOptimizationFoam.

1

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 14 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

$FOAM SOLVERS/incompressible/

adjointShapeOptimizationFoam/

adjointShapeOptimizationFoam.C

Let us study the implementation together, trying to identify the underlyingtheory we have covered so far. In the order it is implemented in the code.

I Porosity update

- Implementation of the steepest descent using under relaxation.- Correct?

I The primal pressure-velocity SIMPLE corrector

- How is the sink term implemented?

I The adjoint pressure-velocity SIMPLE corrector

- Is the current implementation done for objective functions withvolumteric contribution (JΩ 6= 0)?

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 15 /

40

Overview Introduction Theory Implementation Modifications Results

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 ( a l p h a + lambda ∗(Ua & U) , z e r o A l p h a ) , alphaMax ) − a l p h a ) ;

Listing 1: file adjointShapeOptimizationFoam.C

αn+1 = αn(1− γ) + γmin (max ((αn + ui · viViδ) .0) , αmax) , (17)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 16 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Porosity update

However the there seems to be an incorrect sign in the steepest descentimplementation. The correct eq. and implementation should be

αn+1 = αn(1− γ) + γmin (max ((αn − ui · viViδ) , 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 ( a l p h a − lambda ∗(Ua & U) , z e r o A l p h a ) , alphaMax ) − a l p h a ) ;

Listing 2: file adjointShapeOptimizationFoam.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 17 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Primal SIMPLE loop

103 // Momentum p r e d i c t o r104105 tmp<f v V e c t o r M a t r i x> UEqn106 (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 ( a lpha , U)110 ) ;111112 UEqn ( ) . r e l a x ( ) ;113114 s o l v e ( UEqn ( ) == −f v c : : g rad ( p ) ) ;

Listing 3: file adjointShapeOptimizationFoam.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 18 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Primal SIMPLE loop

I fvm::Sp(alpha, U) Implicit implementation of the extrasource term.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 19 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Adjoint SIMPLE loop

156 // A d j o i n t Momentum p r e d i c t o r157158 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 ( ( f v c : : 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 n160 // (161 // f v c : : r e c o n s t r u c t162 // (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 // ) ;166167 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 ) ;168169 tmp<f v V e c t o r M a t r i x> UaEqn170 (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 n173 + t u r b u l e n c e−>d i v D e v R e f f (Ua)174 + fvm : : Sp ( a lpha , Ua)175 ) ;176177 UaEqn ( ) . r e l a x ( ) ;178179 s o l v e ( UaEqn ( ) == −f v c : : g rad ( pa ) ) ;

Listing 4: file adjointShapeOptimizationFoam.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 20 /

40

Overview Introduction Theory Implementation Modifications Results

Solver

Adjoint SIMPLE loop

I Similar to the primal system.

I No additional terms containing information about the costfunction, i.e. current implementation treats cost functions ofthe type JΓ = 0

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 21 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Adjoint boundary conditions

The bad results when the sign in the steepest descent algorithm ischanged indicates additional problems with the current solver. Since theactual solver seems to agree with the theory, what remains to be examinedis the boundary conditions.First the boundary condition for a specific cost function need to becalculated.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 22 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Power dissipation

J := −∫Γ

(p+1

2v2)v · n dΓ. (19)

I Only boundary contribution.

I Energy loss, due to pressure drop and kinetic energy.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 23 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Inlet boundary conditions

ut = 0, (20)

un = −∂JΩ

∂p, (21)

n · ∇q = 0. (22)

ut = 0,

un =

0 at wall,

vn at inlet,

n · ∇q = 0.

(23)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 24 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Inlet boundary conditions

I Use existing conditions

- Prescribed value- No slip- Zero-gradient

I Could be useful to implement a inlet boundary condition forthe adjoint velocity, if the velocity profile of the primal velocityat the inlet is not easily described.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 25 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Outlet boundary conditions

q = u · v + unvn + ν(n · ∇)un +∂JΓ

∂vn, (24)

0 = vnut + ν(n · ∇)ut +∂JΓ

∂vt. (25)

q = u · v + unvn + ν(n · ∇)un − 1

2v2 − v2

n,

0 = vn(ut − vt) + ν(n · ∇)ut.(26)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 26 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Outlet boundary conditions

I Use eq. 26 to prescribe a value to the adjoint pressure

I Use eq. 26 to prescribe a value to the tangential componentof the adjoint velocity

ut =− ν

∆uneigh,t − vp,nvp,tvp,n + ν

, (27)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 27 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Implementation of pressure condition

86 // ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ Member F u n c t i o n s ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ //8788 void Foam : : a d j o i n t O u t l e t P r e s s u r e F v P a t c h S c a l a r F i e l d : : u p d a t e C o e f f s ( )89 90 i f ( updated ( ) )91 92 return ;93 9495 const f v s P a t c h F i e l d<s c a l a r>& p h i p =96 patch ( ) . l o o k u p P a t c h F i e l d<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" ) ;9798 const f v s P a t c h F i e l d<s c a l a r>& p h i a p =99 patch ( ) . l o o k u p P a t c h F i e l d<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" ) ;

100101 const f v P a t c h F i e l d<v e c t o r>& Up =102 patch ( ) . l o o k u p P a t c h F i e l d<v o l V e c t o r F i e l d , v e c t o r >("U" ) ;103104 const f v P a t c h F i e l d<v e c t o r>& Uap =105 patch ( ) . l o o k u p P a t c h F i e l d<v o l V e c t o r F i e l d , v e c t o r >("Ua" ) ;106107 operator==(( p h i a p / patch ( ) . magSf ( ) − 1 . 0 )∗ p h i p / patch ( ) . magSf ( ) + (Up & Uap ) ) ;108109 f i x e d V a l u e F v P a t c h S c a l a r F i e l d : : u p d a t e C o e f f s ( ) ;110

Listing 5: file adjointOutletPressureFvPatchScalarField.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 28 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Implementation of velocity condition

83 // ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ Member F u n c t i o n s ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ //8485 // Update th e c o e f f i c i e n t s a s s o c i a t e d w i t h th e patch f i e l d86 void Foam : : a d j o i n t O u t l e t V e l o c i t y F v P a t c h V e c t o r F i e l d : : u p d a t e C o e f f s ( )87 88 i f ( updated ( ) )89 90 return ;91 9293 const f v s P a t c h F i e l d<s c a l a r>& p h i a p =94 patch ( ) . l o o k u p P a t c h F i e l d<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" ) ;9596 const f v P a t c h F i e l d<v e c t o r>& Up =97 patch ( ) . l o o k u p P a t c h F i e l d<v o l V e c t o r F i e l d , v e c t o r >("U" ) ;9899 s c a l a r F i e l d Un(mag( patch ( ) . n f ( ) & Up ) ) ;

100 v e c t o r F i e l d UtHat ( ( Up − patch ( ) . n f ( )∗Un ) / ( Un + SMALL ) ) ;101102 v e c t o r F i e l d Uan ( patch ( ) . n f ( )∗ ( patch ( ) . n f ( ) & p a t c h I n t e r n a l F i e l d ( ) ) ) ;103104 v e c t o r F i e l d : : operator=( p h i a p ∗ patch ( ) . Sf ( ) / s q r ( patch ( ) . magSf ( ) ) + UtHat ) ;105 // v e c t o r F i e l d : : o p e r a t o r =(Uan + UtHat ) ;106107 f i x e d V a l u e F v P a t c h V e c t o r F i e l d : : u p d a t e C o e f f s ( ) ;108

Listing 6: file adjointOutletVelocityFvPatchVectorField.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 29 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Analyzing the implementation

Listing 5 gives an implementation of the pressure condition according to

q = (un − 1)vn + u · v. (28)

compare to

q = u · v + unvn + ν(n · ∇)un −1

2v2 − v2

n, (29)

Listing 6 gives an implementation of the velocity condition according to

up,t =vp − vp,n

up,n + SMALL. (30)

compare to

ut =− ν

∆uneigh,t − vp,nvp,tvp,n + ν

, (31)

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 30 /

40

Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Analyzing the theory

I Not equal to the theory.

I Another cost function, total pressure loss.

I Approximations or derivations done makes the cost functionhard to identify.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 31 /

40

Overview Introduction Theory Implementation Modifications Results

Analyzing the theory

I Changing the sign of the steepest descent implementation.

I Implementing the cost functions according to derivedequations.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 32 /

40

Overview Introduction Theory Implementation Modifications Results

Adjoint pressure boundary condition

I Similar to the existing implementationI A few additional terms

- Velocity in the neighbouring node- Effective viscosity

const s c a l a r F i e l d& d e l t a i n v = patch ( ) . d e l t a C o e f f s ( ) ; // d i s t a n c e ˆ(−1) between patch and n e i g h b o u r i n g node .

// c r e a t e t he o b j e c t needed to g e t th e v i s c o u s i t y :const i n c o m p r e s s i b l e : : RASModel& rasMode l =

db ( ) . look upOb ject<i n c o m p r e s s i b l e : : RASModel>("RASProperties" ) ;

// n u e f f from t he t u r u l e n c e model , rasMode l :s c a l a r F i e l d n u e f f = rasMode l . n u E f f ( ) ( ) . b o u n d a r y F i e l d ( ) [ patch ( ) . i n d e x ( ) ] ;

// N e i g h b o u r i n g node ’ s v e l o c i t y ( normal component ) :s c a l a r F i e l d Uane igh n = ( Uap . p a t c h I n t e r n a l F i e l d ( ) & patch ( ) . n f ( ) ) ;

Listing 7: file myAdjointOutletVelocityFvPatchVectorField.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 33 /

40

Overview Introduction Theory Implementation Modifications Results

Adjoint velocity boundary condition

I Vector fields instead of scalar fields

// p a t c h I n t e r n a l F i e l d to g e t th e a d j o i n t v e l o c i t y o f n e i g h b o u r i n g node .v e c t o r F i e l d Uaneigh = Uap . p a t c h I n t e r n a l F i e l d ( ) ;// I t s t a n g e n t i a l and normal componentsv e c t o r F i e l d Uane igh n = ( Uaneigh & normal )∗ normal ;v e c t o r F i e l d U a n e i g h t = Uaneigh − Uane igh n ;

Listing 8: file myAdjointOutletVelocityFvPatchVectorField.C

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 34 /

40

Overview Introduction Theory Implementation Modifications Results

Case settings

The boundary conditions of the primal and adjoint variables are setaccording to the table below. More detailed information and m4 script ofthe “box” example case can be found in the provided files.

Wall Inlet Outlet

u Prescribed value, 0 Prescribed value, un = vn adjointOutletVelocityPower

q Zero gradient Zero gradient adjointOutletPressurePower

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 35 /

40

Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily porosity field

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 36 /

40

Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily value of the objective function

0

0.001

0.002

0.003

0.004

0.005

0.006

0 500 1000 1500 2000 2500 3000

J

Iteration step

”valueObjFunc.xy”

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 37 /

40

Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily value of the objective function, without

porosity update

0

0.001

0.002

0.003

0.004

0.005

0.006

0 500 1000 1500 2000 2500 3000

J

Iteration step

”valueObjFunc.xy”

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 38 /

40

Overview Introduction Theory Implementation Modifications Results

Power dissipation

Pipe bend example

A movie can be found in the case file.

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 39 /

40

Overview Introduction Theory Implementation Modifications Results

Power dissipation

Thank you for listening!

Questions

Ulf Nilsson adjointShapeOptimizationFoamDecember 10, 2013 40 /

40