stce - euroad workshop - markus towara - an... · stce how to implement n ˛m, so adjoint mode is...

35
STCE An (more) effective Discrete Adjoint Model for OpenFOAM Markus Towara, Uwe Naumann Software and Tools for Computational Engineering Science RWTH Aachen University EuroAD 2013, Oxford, 10. December 2013

Upload: others

Post on 12-Oct-2019

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCE

An (more) effective Discrete Adjoint Model for OpenFOAM

Markus Towara, Uwe NaumannSoftware and Tools for Computational Engineering ScienceRWTH Aachen University

EuroAD 2013, Oxford, 10. December 2013

Page 2: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCECFD @ STCE

I A. Sen (aboutFLOW Project, discrete OpenFOAM)

I A. Daztouri (aboutFLOW Project, discrete ACE+)

I M. Foerster (fortissiFOAM Project, OpenFOAM in the cloud)

I me (discrete OpenFOAM)

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 2/ 35

Page 3: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 3/ 35

Page 4: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 4/ 35

Page 5: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOpenFOAM

I Open Field Operation and Manipulation

I Open-Source (GPLv3) CFD solver

I developed by OpenCFD Ltd., currently at version 2.2.x

I > 1 Mio. Lines of Code

I includes tools for meshing, pre-, post-processing

I rising adoption in industry and academia due to lack of licence costs → well suited for parallelarchitectures

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 5/ 35

Page 6: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCECFD Basics

I Navier Stokes equations for incompressible steady flow:

v · ∇v = ν∇2v− 1

ρ∇p momentum conservation

∇ · v = 0 mass conservation

I Or in three Dimensions:

vx∂vx

∂x+ vy

∂vx

∂y+ vz

∂vx

∂z= −

∂p′

∂x+ ν

∂2vx∂x2

+∂2vx

∂y2+∂2vx

∂z2

vx∂vy

∂x+ vy

∂vy

∂y+ vz

∂vy

∂z= −

∂p′

∂y+ ν

∂2vy∂x2

+∂2vy

∂y2+∂2vy

∂z2

vx∂vz

∂x+ vy

∂vz

∂y+ vz

∂vz

∂z= −

∂p′

∂z+ ν

∂2vz∂x2

+∂2vz

∂y2+∂2vz

∂z2

∂u

∂x+∂v

∂y+∂w

∂z= 0

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 6/ 35

Page 7: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCECFD Basics

(Decoupled) Solution of the partial differential equations (SIMPLE-Algorithm):

I discretize / linearize momentum conservation equations

I solve momentum equations for velocity v, assume pressure p as known

I obtained velocity field fulfills momentum equation but not mass conservation equation as thepressure field was guessed and not correct

I discretize mass conservation equation

I use mass conservation equation to correct pressure field

I use new pressure field to correct velocity field

I Loop...

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 7/ 35

Page 8: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCETopology Optimization

no optimization added ”material” reconstruction

Add penalty term α to Navier-Stokes equation1 :

(v · ∇) v = ν∇2v−∇p− αv1C. Othmer: A continuous adjoint formulation for the computation of topological and surface sensitivities of ducted

flows. Intern. J. f. Num. Meth. in Fluids. p. 861–877, 2008.,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 8/ 35

Page 9: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEHow to find appropriate α

I Define Cost Function J, e.g. total pressure loss between inlet and outlet:

J =

∫Γ

p+1

2v2n dΓ

I Calculate sensitivity of the Cost function w.r.t. parameters αi∂J

∂αi=???

I Calculate updated porosity field αn+1, e.g.:

αn+1i = αn

i − λ ·∂Jn

∂αni

, while insuring 0 < αi < αmax

I Loop until α converged...

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 9/ 35

Page 10: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 10/ 35

Page 11: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEMotivation 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?

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 11/ 35

Page 12: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEHow to implement

n � m, so Adjoint Mode is method of choice

I source code transformation

I operator overloadingI due to complexity of OpenFOAM code (templates, massively object oriented) the only suitable

optionI operator overloading tool dco/c++I every floating point value in OpenFOAM is replaced with custom adjoint datatype dco::a1s::typeI needed intermediate values are stored in a structure called tape

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 12/ 35

Page 13: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEHow to implement

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 ready to go

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 13/ 35

Page 14: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOpenFOAM a1s mode

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

namespace Foam{

typedef double doubleScalar;...

}

with:

#include ”dco.hpp”namespace Foam{

typedef dco::a1s::type doubleScalar;...

}

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 14/ 35

Page 15: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOpenFOAM t1s mode

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

namespace Foam{

typedef double doubleScalar;...

}

with:

#include ”dco.hpp”namespace Foam{

typedef dco::t1s::type doubleScalar;...

}

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 15/ 35

Page 16: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEin Reality

I some minor changes have to be made in the OpenFOAM code:I unions don’t support active datatypesI some casts are missing and have to be done by handI some functions (pow,max,min) have to be adapted

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 16/ 35

Page 17: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEusage of discrete OpenFOAM - t1s

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

:

double sens = 0;dco::t1s::set(alpha[i],1,1);

for (runTime++; !runTime.end(); runTime++){... // solve for U,p}// Sum pressure over inlet faces scaled with face areadoubleScalar J = gSum( p.boundaryField()∗patch.magSf() );

dco::t1s::get(J,sens,1);

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

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 17/ 35

Page 18: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEusage of discrete OpenFOAM - a1s

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

dco::a1s::static tape tape(tapeSize);double∗ sens = new double[alpha.size()];

for(int i=0; i<alpha.size(); i++)tape.register variable(alpha[i]);

for (runTime++; !runTime.end(); runTime++){... // solve for U,p}// Sum pressure over inlet faces scaled with face areadoubleScalar J = gSum( p.boundaryField()∗patch.magSf() );dco::a1s::set(J,1,−1);tape.interpret adjoint();

for(int i = 0; i<alpha.size(); i++)dco::a1s::get(alpha[i],sens[i],−1);

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 18/ 35

Page 19: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 19/ 35

Page 20: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEFirst Results

I Black-Box approach severely bound by memory bandwith! (Need to allocate around 20 GB)I By doing Checkpointing we can actually get faster...

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 20/ 35

Page 21: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEHow to improve?

I Knowledge and Profiling reveals that most of calculation time and tape memory is spent in(iterative) linear solvers

I Analytical insight allows us to treat the adjoints of linear solvers analytically

Lemma

For a Linear Equation Sytem Ax = b we can calculate the adjoints for A and b by: 2

b = A−T x→ AT b = x

A = −b · xT

I This gives us an additional Linear Equation System which we have to solve during the gatheringof the adjoints

2M. Giles, Collected Matrix Derivative Results for Forward and Reverse Mode Algorithmic Differentiation, Advancesin Automatic Differentiation 2008

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 21/ 35

Page 22: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCETreatment of Linear Solvers

Black-Box Continuous Linear Solver

I when we encounter an linear solver during the augmented forward run we can stop taping

I when we encounter the gap in the tape during the interpretation we have to fill in the gap byhand

I have to remember A and x!

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 22/ 35

Page 23: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEApplication to OpenFOAM

I Abstract Base Class for all Linear Solvers exists → can tackle all implemented solvers at once

I Discretization matrices are stored in a coordinate sparse matrix format → transposing matrixstraightforward

I take care not to mess up symmetry of matrices

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 23/ 35

Page 24: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 24/ 35

Page 25: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEResults

I we are seeing a nice improvement in both runtime and memory usage

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 25/ 35

Page 26: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEVerification

I We calculate a reference solution with solvers GAMG for U and smoothSolver for p, both withtolerance 1e− 10

I After that we calculate solutions with different tolerances for the forward and adjoint linearsolvers and compare to the reference solution

I 11k Cells in mesh

Velocity Sensitivity

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 26/ 35

Page 27: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEVerification

Runtime Tape-Memory

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 27/ 35

Page 28: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEError

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 28/ 35

Page 29: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCERuntime

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 29/ 35

Page 30: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEOutline

OpenFOAM Introduction

Discrete OpenFOAM

Improvements

Verification and Results

Work in Progress - PISO Foam

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 30/ 35

Page 31: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEWork in Progress - Unsteady Adjoints

Unsteady PISO solver with unsteady cost function (potentially depends on every timestep)

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 31/ 35

Page 32: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEWork in Progress - Unsteady Adjoints

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 32/ 35

Page 33: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCEWork in Progress - Unsteady Adjoints

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 33/ 35

Page 34: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCESummary and Outlook

I Fully discrete mode uses loads of memory

I Treatment of linear solvers greatly reduces this

I Its also faster

I Application to unsteady flow, heat transfer, turbulence in progress

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 34/ 35

Page 35: STCE - EuroAd Workshop - Markus Towara - An... · STCE How to implement n ˛m, so Adjoint Mode is method of choice I source code transformation I operator overloading I due to complexity

STCE

Thank you!Questions?

,

STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013 35/ 35