Anda di halaman 1dari 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
Håkan 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 importance
I 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

Z
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 + δv L + δp L, (5)
δv L + δp L = 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 the
scope 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 boundary
contribution.
Ulf Nilsson adjointShapeOptimizationFoam December 10, 2013 7 / 40
Overview Introduction Theory Implementation Modifications Results

Resulting equations

Sensitivity

∂L
= ui · vi Vi , (7)
∂αi
I Expression for cell i
I Vi the cell volume.
I Equation of the adjoint variables and an expression for the sensitivity
of 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

∂JΩ
−2D(u)v = −∇q + ∇ · (2νD(u)) − αu − , (8)
∂v
∂JΩ
∇·u= . (9)
∂p

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)
∂JΩ
un = − , (11)
∂p
n · ∇q = 0. (12)

Outlet boundary conditions


∂JΓ
q = u · v + un vn + ν(n · ∇)un + , (13)
∂vn
∂JΓ
0 = vn ut + ν(n · ∇)ut + . (14)
∂vt

December 10, 2013 10 /


Ulf Nilsson adjointShapeOptimizationFoam 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 cost
function, JΩ and JΓ respectively.
I Eq. 13 and 14 used to calculate the adjoint pressure and tangential
part of the adjoint velocity at the outlet.

December 10, 2013 11 /


Ulf Nilsson adjointShapeOptimizationFoam 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 expression
for 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 the
solver only through the boundary conditions.
I The optimization problem reduces to solving two flow equations, the
primal and the adjoint.
I When the sensitivity is calculated an algorithm is needed to update
the porosity.

December 10, 2013 12 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Steepest descent algorithm

Steepest descent

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

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

I General search direction in the opposite direction of the gradient of


the objective function.
I Considering a linear problem a minimum will be found for small
enough 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.

December 10, 2013 13 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Solver

Solution procedure

Main-loop

Solve the
primal system
(v, p)

“Frozen tubulence”
Update the Solve the
porosity adjoint system
Steepest descent (u, q)

Convergence
assessment Converged
End
End time or
convergence condition

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

December 10, 2013 14 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Solver

$FOAM SOLVERS/incompressible/
adjointShapeOptimizationFoam/
adjointShapeOptimizationFoam.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.
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 with
volumteric contribution (JΩ 6= 0)?

December 10, 2013 15 /


Ulf Nilsson adjointShapeOptimizationFoam 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 · vi Vi δ) .0) , αmax ) , (17)

December 10, 2013 16 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

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 − ui · vi Vi δ) , 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

December 10, 2013 17 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

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 ( p h i , 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 l p h a , U)
110 );
111
112 UEqn ( ) . r e l a x ( ) ;
113
114 s o l v e ( UEqn ( ) == −f v c : : g r a d ( p ) ) ;
Listing 3: file adjointShapeOptimizationFoam.C

December 10, 2013 18 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Solver

Primal SIMPLE loop

I fvm::Sp(alpha, U) Implicit implementation of the extra


source term.

December 10, 2013 19 /


Ulf Nilsson adjointShapeOptimizationFoam 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 r
157
158 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 : : g r a d ( 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 // f v c : : r e c o n s t r u c t
162 // (
163 // mesh . magSf ( ) ∗ ( f v c : : sn Grad ( Ua ) & f v c : : i n t e r p o l a t e (U) )
164 // )
165 // ) ;
166
167 zeroCells ( adjointTransposeConvection , 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 (− p h i , Ua )
172 − adjointTransposeConvection
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 ( a l p h a , Ua )
175 );
176
177 UaEqn ( ) . r e l a x ( ) ;
178
179 s o l v e ( UaEqn ( ) == −f v c : : g r a d ( pa ) ) ;
Listing 4: file adjointShapeOptimizationFoam.C

December 10, 2013 20 /


Ulf Nilsson adjointShapeOptimizationFoam 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 cost
function, i.e. current implementation treats cost functions of
the type JΓ = 0

December 10, 2013 21 /


Ulf Nilsson adjointShapeOptimizationFoam 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 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 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Power dissipation

1
Z
J := − (p + v 2 )v · n dΓ. (19)
2
Γ

I Only boundary contribution.


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

December 10, 2013 23 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Inlet boundary conditions

ut = 0, (20)
∂JΩ
un = − , (21)
∂p
n · ∇q = 0. (22)




ut =(
0,

 0 at wall,
un = (23)


 vn at inlet,

n · ∇q = 0.

December 10, 2013 24 /


Ulf Nilsson adjointShapeOptimizationFoam 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 for
the adjoint velocity, if the velocity profile of the primal velocity
at the inlet is not easily described.

December 10, 2013 25 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Boundary conditions of the adjoint variables

Outlet boundary conditions

∂JΓ
q = u · v + un vn + ν(n · ∇)un + , (24)
∂vn
∂JΓ
0 = vn ut + ν(n · ∇)ut + . (25)
∂vt

(
q = u · v + un vn + ν(n · ∇)un − 21 v 2 − vn2 ,
(26)
0 = vn (ut − vt ) + ν(n · ∇)ut .

December 10, 2013 26 /


Ulf Nilsson adjointShapeOptimizationFoam 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 component
of the adjoint velocity

ν
−∆ uneigh,t − vp,n vp,t
ut = ν , (27)
vp,n + ∆

December 10, 2013 27 /


Ulf Nilsson adjointShapeOptimizationFoam 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 ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ //
87
88 v o i d 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 }
94
95 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 p a t c h ( ) . 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" ) ;
97
98 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 p a t c h ( ) . 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" ) ;
100
101 const f v P a t c h F i e l d <v e c t o r >& Up =
102 p a t c h ( ) . 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" ) ;
103
104 const f v P a t c h F i e l d <v e c t o r >& Uap =
105 p a t c h ( ) . 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" ) ;
106
107 o p e r a t o r ==(( p h i a p / p a t c h ( ) . magSf ( ) − 1 . 0 ) ∗ p h i p / p a t c h ( ) . magSf ( ) + ( Up & Uap ) ) ;
108
109 fixedValueFvPatchScalarField : : updateCoeffs ( ) ;
110 }
Listing 5: file adjointOutletPressureFvPatchScalarField.C

December 10, 2013 28 /


Ulf Nilsson adjointShapeOptimizationFoam 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 ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ //
84
85 // Update t h 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 t h e p a t c h f i e l d
86 v o i d 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 }
92
93 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 p a t c h ( ) . 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" ) ;
95
96 const f v P a t c h F i e l d <v e c t o r >& Up =
97 p a t c h ( ) . 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" ) ;
98
99 s c a l a r F i e l d Un ( mag ( p a t c h ( ) . n f ( ) & Up ) ) ;
100 v e c t o r F i e l d UtHat ( ( Up − p a t c h ( ) . n f ( ) ∗ Un ) / ( Un + SMALL ) ) ;
101
102 v e c t o r F i e l d Uan ( p a t c h ( ) . n f ( ) ∗ ( p a t c h ( ) . n f ( ) & p a t c h I n t e r n a l F i e l d ( ) ) ) ;
103
104 v e c t o r F i e l d : : o p e r a t o r =( p h i a p ∗ p a t c h ( ) . S f ( ) / s q r ( p a t c h ( ) . magSf ( ) ) + UtHat ) ;
105 // v e c t o r F i e l d : : o p e r a t o r =(Uan + UtHat ) ;
106
107 fixedValueFvPatchVectorField : : updateCoeffs ( ) ;
108 }
Listing 6: file adjointOutletVelocityFvPatchVectorField.C

December 10, 2013 29 /


Ulf Nilsson adjointShapeOptimizationFoam 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
1
q = u · v + un vn + ν(n · ∇)un − v 2 − vn2 , (29)
2
Listing 6 gives an implementation of the velocity condition according to
vp − vp,n
up,t = . (30)
up,n + SMALL
compare to
ν
−∆ uneigh,t − vp,n vp,t
ut = ν , (31)
vp,n + ∆
December 10, 2013 30 /
Ulf Nilsson adjointShapeOptimizationFoam 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 function
hard to identify.

December 10, 2013 31 /


Ulf Nilsson adjointShapeOptimizationFoam 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 derived
equations.

December 10, 2013 32 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Adjoint pressure boundary condition

I Similar to the existing implementation


I 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 = p a t c h ( ) . d e l t a C o e f f s ( ) ; // d i s t a n c e ˆ( −1) between p a t c h and n e i g h b o u r i n g node .

// c r e a t e t h e o b j e c t n e e d e d t o g e t t h 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& r a s M o d e l =
db ( ) . l o o k u p O b j e c t <i n c o m p r e s s i b l e : : RASModel>(" RASProperties " ) ;

// n u e f f from t h e t u r u l e n c e model , r a s M o d e l :
s c a l a r F i e l d n u e f f = rasModel . nuEff ( ) ( ) . boundaryField ( ) [ patch ( ) . index ( ) ] ;

// N e i g h b o u r i n g node ’ s v e l o c i t y ( n o r m a l component ) :
s c a l a r F i e l d U a n e i g h n = ( Uap . p a t c h I n t e r n a l F i e l d ( ) & p a t c h ( ) . n f ( ) ) ;
Listing 7: file myAdjointOutletVelocityFvPatchVectorField.C

December 10, 2013 33 /


Ulf Nilsson adjointShapeOptimizationFoam 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 t o g e t t h 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 U a n e i g h = 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 n o r m a l components
v e c t o r F i e l d 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 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

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, un = vn adjointOutletVelocityPower
q Zero gradient Zero gradient adjointOutletPressurePower

December 10, 2013 35 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily porosity field

December 10, 2013 36 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily value of the objective function

0.006
”valueObjFunc.xy”

0.005

0.004

0.003
J

0.002

0.001

0
0 500 1000 1500 2000 2500 3000
Iteration step

December 10, 2013 37 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Power dissipation

pitzDaily value of the objective function, without


porosity update

0.006
”valueObjFunc.xy”

0.005

0.004

0.003
J

0.002

0.001

0
0 500 1000 1500 2000 2500 3000
Iteration step

December 10, 2013 38 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Power dissipation

Pipe bend example

December 10, 2013 39 /


Ulf Nilsson adjointShapeOptimizationFoam 40
Overview Introduction Theory Implementation Modifications Results

Power dissipation

Thank you for listening!

Questions

December 10, 2013 40 /


Ulf Nilsson adjointShapeOptimizationFoam 40

Anda mungkin juga menyukai