Automatic Differentiation Tools for FreeFem++ Workshop FreeFem++ - - PowerPoint PPT Presentation

automatic differentiation tools for freefem
SMART_READER_LITE
LIVE PREVIEW

Automatic Differentiation Tools for FreeFem++ Workshop FreeFem++ - - PowerPoint PPT Presentation

Automatic Differentiation Tools for FreeFem++ Workshop FreeFem++ Sylvain Auliac ( s-auliac@netcourrier.com ) - LJLL September 16, 2009 An Overview of FAD (Forward Automatic Differentiation) FAD theoretical principles Informatic point of view


slide-1
SLIDE 1

Automatic Differentiation Tools for FreeFem++

Workshop FreeFem++ Sylvain Auliac (s-auliac@netcourrier.com) - LJLL September 16, 2009

slide-2
SLIDE 2

An Overview of FAD (Forward Automatic Differentiation) FAD theoretical principles Informatic point of view FAD in FreeFem++ FADed FreeFem++ in action Perspectives of development

slide-3
SLIDE 3

FAD principle

What is Automatic Differentiation (AD)?

AD denominate a set of technics that allows to calculate automatically and “exactly” the derivatives of the outputs of a programm with respect to some of its inputs. There are several kinds of AD, each of them shows up efficiency in a well defined field of aplication. ♥ FAD = Forward Automatic Differentiation

slide-4
SLIDE 4

FAD principle

Let P be a programm and call :

◮ f : (xi)1≤i≤n −

→ (yi)1≤i≤m the function implemented by P

◮ P′ the differentiated version of P ◮ f ′ the function implemented by P′

One derivative forward mode

◮ f ′ : (x1, dx1, x2, dx2, . . . , xn, dxn) −

→ (y1, dy1, . . . , ym, dym)

◮ ∀j ∈ {1, . . . , m}, dyj = n

  • i=1

∂yj ∂xi dxi

◮ with k fixed, if ∀i, dxi = δk,i then ∀j, dyj = ∂yj

∂xk

slide-5
SLIDE 5

FAD principle

Multi derivatives forward mode

Let p be an integer (1 ≤ p ≤ n).

◮ f ′ : (x1,

∇x1, x2, ∇x2, . . . , xn, ∇xn) → (y1, ∇y1, . . . , ym, ∇ym) where ∇xk ∈ Rp

◮ ∀j ∈ {1, . . . , m},

∇yj =

n

  • i=1

∂yj ∂xi

  • ∇xi

◮ Special case when p = n and ∀i,

∇xi = (δi,k)1≤k≤n then : ∀j, ∇yj = ∇yj =      

∂yj ∂x1 ∂yj ∂x2

. . .

∂yj ∂xn

      such that

  • ∇yj
  • 1≤j≤m = Jf

(1)

slide-6
SLIDE 6

FAD and programming

How to modifie a programm to support FAD:

◮ Add and associate a new variable to each variable of the

numeric type in the programm to store the derivative (p new variables are needed for each pre-existing variable for multi-derivative AD).

◮ write the update(s) for the associated derivative(s) just before

each change of each variable induced by a calculus, using the following derivations formulas (f , g : Rn − → R) : ∇(f ±g) = ∇f ±∇g ; ∇(f ∗g) = g∇f +f ∇g ; ∇(f /g) = g∇f − f ∇g g2 If u : R → R, v : Rn → R, ∇(u ◦ v) = (u′ ◦ v)∇v

slide-7
SLIDE 7

FAD and programming - exemple with 1 derivative

This is a C piece of code : and its AD associated one : //u’s computed earlier // so should be du double x=u-1./u; double dx=du + du/(u*u); double y=x+log(u); double dy=dx + du/u; double J=x+y; double dJ=dx+dy; Whole new code : dx=du + du/(u*u); x=u-1./u; dy=dx + du/u; y=x+log(u); dJ=dx+dy; J=x+y;

slide-8
SLIDE 8

Pros and cons of the forward mode

Advantages:

◮ Easy to handle.

Limitations

slide-9
SLIDE 9

Pros and cons of the forward mode

Advantages:

◮ Easy to handle. ◮ Allows fast development in (low-level) pre-existing codes.

Limitations

slide-10
SLIDE 10

Pros and cons of the forward mode

Advantages:

◮ Easy to handle. ◮ Allows fast development in (low-level) pre-existing codes. ◮ Modifications strongly reduced in programming languages

with overloading features.

Limitations

slide-11
SLIDE 11

Pros and cons of the forward mode

Advantages:

◮ Easy to handle. ◮ Allows fast development in (low-level) pre-existing codes. ◮ Modifications strongly reduced in programming languages

with overloading features.

Limitations

◮ Decrinsing efficiency with relatively modest number of

derivation parameters.

slide-12
SLIDE 12

Pros and cons of the forward mode

Advantages:

◮ Easy to handle. ◮ Allows fast development in (low-level) pre-existing codes. ◮ Modifications strongly reduced in programming languages

with overloading features.

Limitations

◮ Decrinsing efficiency with relatively modest number of

derivation parameters.

◮ Dramatic augmentation of needed memory.

slide-13
SLIDE 13

Optimal Control

Problem :

Let Ω be a domain of R2 partitioned in n subdomains Ωi. min

a∈Rn

|ua − ud|2 , −∆ua = fa and ua|∂Ω = 0 fa =

n

  • i=1

aiIΩi Let’s call J : Rn − → R the functionnal a − →

|ua − ud|2 Note that : J(a) = 1 2

|ua|2 −

uaud + 1 2ud2

L2(Ω)

slide-14
SLIDE 14

Optimal Control

Conjugued Gradient Algorithm:

Initialization:

◮ Rn ∋ d0 = ∇J(a0) ◮ ρ0 = (∇J(a0),d0) ua02

L2(Ω)

◮ a1 = a0 − ρ0d0

Iterations, if ak is known :

◮ dk = ∇J(ak) + ∇J(ak)2 ∇J(ak−1)2 dk−1 , we need uak : −∆uak = fak ◮ ρk = (∇J(ak),dk) udk 2

L2(Ω)

, we need udk : −∆udk = fdk

◮ ak+1 = ak − ρkdk

slide-15
SLIDE 15

Optimal Control - FreeFem Script

FreeFem Script for n=5

1:real[int] A(5),D(5),DD(5); 2:real h = 1./N; 3:for(int i=1;i<N;i++) { 4: A[i] = 0; // initializations 5: D[i] = 0; AAA[i] = 0; 6:

SetDiff(A[i],i);

7:} 8:A[0] = 1.;SetDiff(A[0],0); 9: // Definition of second member functions 10:func real R2(real xx,real yy) 11: {return xx*xx + yy*yy;} 12:func real FA(real xx,real yy) 13:{ 14:

int n = floor(R2(xx,yy)/h);

15: n = n>4 ? 4 : n; // can’t use ‘‘region’’ 16:

return A[n];

// ’cause of memmory leak :-( 17:} 18:func real FD ... // the same with D array

slide-16
SLIDE 16

19:func fA = FA(x,y); 20:func fD = FD(x,y); 21:func g = 0; // Dirichlet boundary condition 22: 23:border C0(t=0,2*pi) 24: {x=0.2*cos(t); y=0.2*sin(t);label=0;} 25:border C1(t=0,2*pi) 26: {x=0.4*cos(t); y=0.4*sin(t);label=1;} 27:border C2(t=0,2*pi) 28: {x=0.6*cos(t); y=0.6*sin(t);label=2;} 29:border C3(t=0,2*pi) 30: {x=0.8*cos(t); y=0.8*sin(t);label=3;} 31:border C4(t=0,2*pi) 32: {x=cos(t); y=sin(t);label=4;} 33:mesh Th = 34:

buildmesh(C0(10)+C1(20)+C2(30)+C3(40)+C4(50));

35:plot(Th,wait=1,ps="partitioned_disc.eps"); 36: 37:fespace Vh(Th,P1); 38:Vh ud = 1. - (x*x + y*y); // Exact solution for A=[4,4,4,4,4]

slide-17
SLIDE 17

40:Vh uhA,vh,uhD,fff; 41: 42:real Jm1 = 0; // to save J in CG iterations 43:real gradJ2 = 0, gradJ2m1 = 0; // to store and save square norm of grad(J) 44: 45:ofstream file("poisson5/donnees.dat"); 46: 47: 48:for(int iter=0;iter<60;iter++) 49:{ 50:

solve Poisson(uhA,vh) =

51:

int2d(Th)(dx(uhA)*dx(vh) + dy(uhA)*dy(vh))

52:

  • int2d(Th)(fA*vh)

53: + on(4,uhA=g); 54:

real J0 = int2d(Th)(uhA*uhA);

55:

real J1 = int2d(Th)(uhA*ud);

56:

real J = 0.5*J0 - J1 + 0.5*int2d(Th)(ud*ud);

57: gradJ2m1 = gradJ2; // Saving the square norm (to avoid recalculation)

slide-18
SLIDE 18

58: gradJ2 = Grad2(J); 59:

real rho;

// Conjugued Gradient algorithm 60:

if(iter==0){

// First CG iteration 61:

for(int i=0;i<N;i++) {DD[i] = J_i;}

62: rho = Grad2(J); 63: rho /= (J0>0 ? J0 : 1.); 64:

for(int i=0;i<N;i++) A[i] -= rho*DD[i];

65:

for(int i=0;i<N;i++){SetDiff(A[i],i);}}

66:

else{

// real CG iterations 67:

for(int i=0;i<N;i++){ //

new descent direction 68: D[i] = J_i + DD[i]*gradJ2/gradJ2m1;} 69:

solve PoissonD(uhD,vh) =

70:

int2d(Th)(dx(uhD)*dx(vh)+dy(uhD)*dy(vh))

71:

  • int2d(Th)(fD*vh) + on(4,uhD=0);

72:

real J0D = int2d(Th)(uhD*uhD);

73:

for(int i=0;i<N;i++) {rho += (J_i * D[i]);}

74: rho /= J0D; 75:

for(int i=0;i<N;i++){

76: A[i] -= rho*D[i]; 77:

SetDiff(A[i],i);}

78: } // etc.. etc.. 79:}

slide-19
SLIDE 19

Finding Dirichlet to fit Neumann

The problem:

Let Ω ⊂ R2 with ∂Ω = Γ1 ∪ Γ2, f ∈ L2(Ω) and gn ∈ L2(Γ2) Consider the J : L2(Γ2) − → R such that : J(g) = 1 2

  • Γ2
  • ∂ug

∂n − gn

  • 2

, ug :    −∆ug = f dans Ω ug = 0 sur Γ1 ug = g sur Γ2

Resolution algorithm:

◮ Same algorithm as the preceding problem with a new

functionnal.

◮ Control parameter living in an infinite dimensionnal space -¿

discretization needed.

slide-20
SLIDE 20

Scripting details

Here Ω is a square with 21 segments on each of its side.

The control parameter:

With P1 finite elements, the differentiation variables are the values taken by g on each vertices of Γ2. For FreeFem :

real[int] A(20);

// Initialize to zero and SetDiff...

func real g(real a,real b)

{

int k = floor(a/h);

// Uniform discretization

if(b>0) return 0.; else

{

real Akp1 = k<20 ? A[k] : 0; real Ak = k>0 ? (k<21 ? A[k-1]:0) : 0; return((Akp1-Ak)*(a - k*h)/h + Ak);//

P1 by pieces } }

slide-21
SLIDE 21

The functionnal J:

Decomposition with bilinear and linear forms : J(g) = 1 2

  • Γ2
  • ∂ug

∂n

  • 2

  • Γ2

∂ug ∂n gd + 1 2gd2

L2(Γ2)

Freefem script:

real J0 = int1d(Th,1)(dy(uhg)*dy(uhg)); real J1 = int1d(Th,1)(dy(uhg)*gd); real J2 = int1d(Th,1)(gd*gd);

J = 0.5*J0 - J1 + 0.5*J2;

slide-22
SLIDE 22

Perspectives of development

Immediate works:

◮ Some bugs to fix... ◮ Possible use with BFGS. ◮ Setting the number of derivatives in the script. ◮ Optimisation and improvment of the AD-related syntax.

More difficult works:

◮ Merging AD version of FreeFem++ with the real one. ◮ Compatibility with complex numbers. ◮ Differentiation with respect to the geometry. ◮ “Mathematization” of the syntax. ◮ Adding new AD styles (inverse and andjoint models).