23
Toward a Discrete Adjoint Model for OpenFOAM Markus Towara, Uwe Naumann {towara,naumann}@stce.rwth-aachen.de LuFG Informatik 12: Software and Tools for Computational Engineering March 28, 2012

Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Embed Size (px)

Citation preview

Page 1: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Toward a Discrete Adjoint Model for OpenFOAM

Markus Towara, Uwe Naumann{towara,naumann}@stce.rwth-aachen.de

LuFG Informatik 12: Software and Tools for Computational Engineering

March 28, 2012

Page 2: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Vita

2006 - 2011 Computational Engineering Science @ RWTH-Aachen

2010 - 2011 Internship / Diploma thesis @ Volkswagen AG - Kassel

2012 PhD Student @ STCE, RWTH-Aachen

2 / 23

Page 3: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Acknowledgment

I Niloofar Sarafin for adding dco support to OpenFOAM

I Carsten Othmer for the introduction to adjointSimpleFoam

3 / 23

Page 4: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Topics

Motivation for AD in Context of Shape Optimization

black-box AD / white-box AD

application to simpleFoam

treatment of linear solver

4 / 23

Page 5: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Motivation for ADin Context of Shape Optimization

I AD promises:I greater flexibility w.r.t new objectivesI easy adaption to new solver types / generationsI calculated derivatives are exact w.r.t the used discretizationI higher order derivatives available

I Problems:I memory requirementsI how to retain parallelism?

5 / 23

Page 6: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Black-box AD

p0

U0

α0

p

U

sensitivities

I black-box approach can deliver sensitivities without looking at theinside of the code

I pro: very versatile and fast turnaround time

I contra: for iterative solvers: huge memory requirements

I take a closer look at the code to identify potential simplifications

6 / 23

Page 7: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Reminder Topology Optimization

no optimization added ”material” reconstructed geometry

Added penalty term1 α :

(v · ∇) v = ν∇2v−∇p − αv

1C. Othmer: A continuous adjoint formulation for the computation oftopological and surface sensitivities of ducted flows. Intern. J. f. Num. Meth.in Fluids. p. 861–877, 2008.

7 / 23

Page 8: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Discrete OpenFOAM

I put dco into src/OpenFOAM

I include dco.hpp

I replace doubles with active datatype from dco

I OpenFOAM has own typedef for scalar floating point values→ just one substitution

I in theory we now just need to recompile OpenFOAM and are readyto go

8 / 23

Page 9: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

OpenFOAM a1s mode

in src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.h: replace:

namespace Foam{

t y p ed e f doub l e d oub l e S c a l a r ;. . .

}

with:

#i n c l u d e ”dco . hpp”namespace Foam{

t y p ed e f dco : : a1s : : t ype d oub l e S c a l a r ;. . .

}

9 / 23

Page 10: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

OpenFOAM t1s mode

in src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.h: replace:

namespace Foam{

t y p ed e f doub l e d oub l e S c a l a r ;. . .

}

with:

#i n c l u d e ”dco . hpp”namespace Foam{

t y p ed e f dco : : t 1 s : : t ype d oub l e S c a l a r ;. . .

}

10 / 23

Page 11: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

in Reality

I some minor changes have to be made in the OpenFOAM code:I unions don’t support active datatypesI no cast from dco::type to int available, use value v(d) insteadI some functions (pow,max,min) don’t use the doubleScalar typedef

and need to be adjusted

11 / 23

Page 12: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

usage of discrete OpenFOAM - t1s

Black-Box tangent-linear Version of simpleFoam, calculates ∂J∂αi

:

doub l e s en s = 0 ;dco : : t 1 s : : s e t ( a lpha [ i ] , 1 , 1 ) ;

f o r ( runTime++; ! runTime . end ( ) ; runTime++){

. . . // s o l v e f o r U, p}// Sum p r e s s u r e ove r i n l e t f a c e s s c a l e d wi th f a c e a r ead oub l e S c a l a r J = gSum( p . bounda r yF i e l d ( )∗ patch . magSf ( ) ) ;

dco : : t 1 s : : ge t ( J , sens , 1 ) ;

Need to do this N-times to get full sensitivity field!

12 / 23

Page 13: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

usage of discrete OpenFOAM - a1s

Black-Box adjoint Version of simpleFoam, calculates gradient of J:

dco : : a1s : : s t a t i c t a p e tape ( t a p eS i z e ) ;doub l e ∗ s en s = new doub l e [ a l pha . s i z e ( ) ] ;

f o r ( i n t i =0; i<a lpha . s i z e ( ) ; i++)tape . r e g i s t e r v a r i a b l e ( a l pha [ i ] ) ;

f o r ( runTime++; ! runTime . end ( ) ; runTime++){

. . . // s o l v e f o r U, p}// Sum p r e s s u r e ove r i n l e t f a c e s s c a l e d wi th f a c e a r ead oub l e S c a l a r J = gSum( p . bounda r yF i e l d ( )∗ patch . magSf ( ) ) ;dco : : a1s : : s e t ( J ,1 , −1) ;tape . i n t e r p r e t r e v e r s e ( ) ;

f o r ( i n t i = 0 ; i<a lpha . s i z e ( ) ; i++)dco : : a1s : : ge t ( a l pha [ i ] , s en s [ i ] , −1) ;

13 / 23

Page 14: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Test-Case Geometry

I laminar flow with Re = 100

I vortex areas in the corners

I J =∫

Γ p dΓ

I calculate sensitivity ∂J∂α

I black-box approach is used

Re = 100

14 / 23

Page 15: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Results

I 1300 cubic cells

I differentiate over all (pseudo)-timesteps

I plotted: sensitivity with respect to flow resistance α

adjointSimpleFoam2 simpleFoam t1s simpleFoam a1s

2adjointShapeOptimizationFoam in OpenFOAM 2.1.015 / 23

Page 16: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Results

I plotted: sensitivity w.r.t. flow resistance α, capped above zero

I finite-difference version available, but finding right ∆α is not trivial

adjointSimpleFoam simpleFoam t1s simpleFoam a1s

16 / 23

Page 17: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Strategies to Reduce Memory Usage

gray-box: use strategies like checkpointing to store only parts of theprogram run

white-box: exploit the structure of the program

17 / 23

Page 18: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Checkpointing

Program execution

C

C

C

Tape

I save states of the forward run to reexecute the program from thereto generate a new chunk of tape

I for efficient placement of the checkpoints see3

3A. Griewank, A. Walther: Revolve: An Implementation of Checkpointingfor the Reverse or Adjoint Mode of Computational Differentiation.Transactionson Mathematical Software Vol. 26.1, 2000.

18 / 23

Page 19: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

White-box AD

Ax = b→ iterative solver

p0

U0

α0

p

U

sensitivities

I most of the time is spent inside the solver loop

I this leads to the memory requirements

I if A is linear we can stop taping inside the loop → semi-discrete

I but further iteration is needed in the backward run

19 / 23

Page 20: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Semi-Discrete Mode

A=...x=...b=...setup

evaluation

solving

while(){

...

}

J=J(x)J = 1interpret_tape()

A=...x=...b=...setup

evaluation

solving

while(){

...

}

J=J(x)J = 1interpret_tape()

J J

???

20 / 23

Page 21: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Semi-Discrete Mode

Tape On:

Data is stored in forward runand interpreted in backwardrun

Tape Off:

No data is stored in forwardrun, another equation systemneeds to be solved inbackward run

21 / 23

Page 22: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Conclusion

I black-box approach works but is limited to small problems

I checkpointing schemes can help to tackle bigger problem sizes, butat the expense of computing time

I white-box ad approach has the potential to enable much biggerproblem sizes

22 / 23

Page 23: Toward a Discrete Adjoint Model for OpenFOAM - …flowhead.sems.qmul.ac.uk/events/conference2012/downloads/... · Toward a Discrete Adjoint Model for OpenFOAM ... calculates gradient

Thank you for your attention!

23 / 23