Sunteți pe pagina 1din 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

CFD @ STCE

A. Sen (aboutFLOW Project, discrete OpenFOAM)

A. Daztouri (aboutFLOW Project, discrete ACE+)

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

me (discrete OpenFOAM)

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

2/ 35

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

3/ 35

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

4/ 35

OpenFOAM

STCE

Open Field Operation and Manipulation

Open-Source (GPLv3) CFD solver

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

> 1 Mio. Lines of Code

includes tools for meshing, pre-, post-processing

rising adoption in industry and academia due to lack of licence costs well suited for parallel
architectures

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

5/ 35

STCE

CFD Basics
I

Navier Stokes equations for incompressible steady flow:


1
p

v=0

v v = 2 v

momentum conservation
mass conservation

Or in three Dimensions:
vx

vx

vx

vx
x
vy
x
vz
x

+ vy

+ vy

+ vy

vx
y
vy
y
vz
y

+ vz

+ vz

+ vz
v

+
x

vx
z
vy

p0
x

p0

vz

p0

2 vx
x2
2 vy
x2
2 vz
x2

2 vx
y 2
2 vy
y 2
2 vz
y 2

2 vx

z 2

2 vy

z 2

2 vz

z 2

w
+

= 0
z

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

6/ 35

CFD Basics

STCE

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


I

discretize / linearize momentum conservation equations

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

obtained velocity field fulfills momentum equation but not mass conservation equation as the
pressure field was guessed and not correct

discretize mass conservation equation

use mass conservation equation to correct pressure field

use new pressure field to correct velocity field

Loop...

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

7/ 35

STCE

Topology Optimization
no optimization

added material

reconstruction

Add penalty term to Navier-Stokes equation1 :


(v ) v = 2 v p v
1 C. Othmer: A continuous adjoint formulation for the computation of topological and surface sensitivities of ducted
flows. Intern. J. f. Num. Meth. in Fluids. p. 861877, 2008.

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

8/ 35

STCE

How to find appropriate


I

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

p+

1 2
vn d
2

Calculate sensitivity of the Cost function w.r.t. parameters i


J
=???
i

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


in+1 = in

J n
,
in

while insuring

0 < i < max

Loop until converged...

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

9/ 35

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

10/ 35

Motivation for AD
in Context of Shape Optimization

AD promises:
I
I
I
I

STCE

greater flexibility w.r.t new objectives


easy adaption to new solver types / generations
calculated derivatives are exact w.r.t the used discretization
higher order derivatives available

Problems:
I
I

memory requirements
how to retain parallelism?

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

11/ 35

How to implement

STCE

n  m, so Adjoint Mode is method of choice


I
I

source code transformation


operator overloading
I

I
I
I

due to complexity of OpenFOAM code (templates, massively object oriented) the only suitable
option
operator overloading tool dco/c++
every floating point value in OpenFOAM is replaced with custom adjoint datatype dco::a1s::type
needed intermediate values are stored in a structure called tape

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

12/ 35

How to implement

put dco into src/OpenFOAM

include dco.hpp

I
I

replace doubles with active datatype from dco


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

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

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

13/ 35

OpenFOAM a1s mode

STCE

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

OpenFOAM t1s mode

STCE

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

in Reality

STCE

some minor changes have to be made in the OpenFOAM code:


I
I
I

unions dont support active datatypes


some casts are missing and have to be done by hand
some functions (pow,max,min) have to be adapted

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

16/ 35

STCE

usage 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 area
doubleScalar 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

usage of discrete OpenFOAM - a1s

STCE

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 area
doubleScalar 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

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

19/ 35

First Results

I
I

STCE

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

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

20/ 35

STCE

How to improve?
I

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

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:
b = AT x
AT b = x

A = b xT
I

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

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

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

21/ 35

STCE

Treatment of Linear Solvers


Black-Box

Continuous Linear Solver

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

when we encounter the gap in the tape during the interpretation we have to fill in the gap by
hand

have to remember A and x!

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

22/ 35

Application to OpenFOAM

STCE

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

Discretization matrices are stored in a coordinate sparse matrix format transposing matrix
straightforward

take care not to mess up symmetry of matrices

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

23/ 35

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

24/ 35

Results

STCE

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

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

25/ 35

STCE

Verification
I
I
I

We calculate a reference solution with solvers GAMG for U and smoothSolver for p, both with
tolerance 1e 10
After that we calculate solutions with different tolerances for the forward and adjoint linear
solvers and compare to the reference solution
11k Cells in mesh
Velocity

Sensitivity

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

26/ 35

STCE

Verification
Runtime

Tape-Memory

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

27/ 35

Error

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

28/ 35

Runtime

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

29/ 35

Outline

STCE

OpenFOAM Introduction
Discrete OpenFOAM
Improvements
Verification and Results
Work in Progress - PISO Foam

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

30/ 35

Work in Progress - Unsteady Adjoints

STCE

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

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

31/ 35

Work in Progress - Unsteady Adjoints

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

32/ 35

Work in Progress - Unsteady Adjoints

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

33/ 35

Summary and Outlook

Fully discrete mode uses loads of memory

Treatment of linear solvers greatly reduces this

Its also faster

Application to unsteady flow, heat transfer, turbulence in progress

STCE

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

34/ 35

STCE

Thank you!
Questions?

,
STCE RWTH-Aachen, Discrete OpenFOAM, EuroAD 2013

35/ 35

S-ar putea să vă placă și