28.06.2013 Views

x - Aerospace Computing Lab - Stanford University

x - Aerospace Computing Lab - Stanford University

x - Aerospace Computing Lab - Stanford University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AN AUTOMATED METHOD<br />

FOR SENSITIVITY ANALYSIS<br />

USING COMPLEX VARIABLES<br />

Joaquim R. R. A. Martins<br />

Ilan M. Kroo<br />

Juan J. Alonso<br />

Department of Aeronautics and Astronautics<br />

<strong>Stanford</strong> <strong>University</strong><br />

January 12, 2000<br />

– Joaquim Martins – January 12, 2000 –


Outline<br />

• Background on complex-step and other sensitivity<br />

analysis methods<br />

• Theory<br />

– First derivative approximations<br />

– Higher derivative approximations<br />

• Implementation of the complex-step in algorithms<br />

– Fortran functions and operators<br />

– Automatic implementation<br />

• Results<br />

– Structural sensitivities<br />

– Aerodynamic sensitivities<br />

• Conclusions<br />

– Joaquim Martins – January 12, 2000 – 1


Sensitivity Analysis Methods<br />

• Finite-Difference Methods<br />

• ADIFOR<br />

• Analytic Methods<br />

• Complex-Step:<br />

– Lyness & Moler, 1967:<br />

n th derivative approximation by integration in the<br />

complex plane<br />

– Squire & Trapp, 1998:<br />

Simple formula for first derivative,<br />

f ′ ≈ Im[f(x + ih)]/h<br />

– Newman, Anderson & Whitfield, 1998:<br />

Aero-structural code<br />

– Anderson, Whitfield & Nielsen, 1999:<br />

3D Navier-Stokes with turbulence model<br />

– Joaquim Martins – January 12, 2000 – 2


Finite-Difference Derivative<br />

Approximations<br />

From Taylor series expansion.<br />

• Forward-difference:<br />

f ′ (x) ≈<br />

• Central-difference:<br />

f ′ (x) ≈<br />

f(x + h) − f(x)<br />

h<br />

f(x + h) − f(x − h)<br />

2h<br />

+ O(h)<br />

+ O(h 2 )<br />

Any finite-difference formula subject to subtractive<br />

cancellation.<br />

– Joaquim Martins – January 12, 2000 – 3


Cauchy-Riemann Equations<br />

• Extension of rational numbers to solve x 2 = 2:<br />

Define w = a + √ 2b where a, b are rational.<br />

Derivative of f(w):<br />

∂f<br />

∂w<br />

= ∂f<br />

∂a = √ 2 ∂f<br />

∂b .<br />

• Extension of real numbers to solve x 2 = −1:<br />

Define z = x + iy, where x, y are real.<br />

Derivative of f(z):<br />

∂f<br />

∂z<br />

= ∂f<br />

∂y<br />

. Define f(z) = u(z) + iv(z),<br />

∂u<br />

∂x<br />

= ∂v<br />

∂y ,<br />

= i∂f<br />

∂x<br />

∂u<br />

∂y<br />

= −∂v<br />

∂x .<br />

Complex number really just one number.<br />

– Joaquim Martins – January 12, 2000 – 4


Complex-Step Derivative Approximation<br />

• From Cauchy-Riemann:<br />

∂u<br />

∂x<br />

= lim<br />

h→0<br />

v(x + i(y + h)) − v(x + iy)<br />

.<br />

h<br />

For a real functions of a real variable, y = 0,<br />

u(x) = f(x) and v(x) = 0, then,<br />

∂f<br />

∂x<br />

= lim<br />

h→0<br />

Im [f (x + ih)]<br />

.<br />

h<br />

• From Taylor series expansion, step ih:<br />

f(x+ih) = f(x)+ihf ′ (x)−h 2f ′′ (x)<br />

2! −ih3f ′′′ (x)<br />

+. . .<br />

3!<br />

⇒ f ′ Im [f(x + ih)]<br />

(x) =<br />

h<br />

No subtraction!<br />

+ h 2f ′′′ (x)<br />

3!<br />

+ . . .<br />

– Joaquim Martins – January 12, 2000 – 5


Simple Numerical Example<br />

• Estimate derivative at x = 1.5 of the function,<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

f(x) =<br />

<br />

<br />

<br />

e x<br />

√ sin 3 x + cos 3 x<br />

ε = f ′ − f ′ <br />

<br />

ref / f ′ <br />

<br />

ref<br />

¢¡¤£¦¥§ ©¨£<br />

– Joaquim Martins – January 12, 2000 – 6


Higher Derivative Approximations<br />

• General form of Cauchy’s Integral,<br />

f (n) (z) = n!<br />

<br />

f(ξ)<br />

2πi (ξ − z) n+1dξ.<br />

• Discretize using a trapezoidal-rule, integrate around<br />

circle z + rei ,<br />

f (n) (z) ≈ n!<br />

<br />

m−1 f z + re<br />

mr<br />

i2πj<br />

<br />

m<br />

.<br />

Γ<br />

j=0<br />

ei2πjn m<br />

• Fixed r, use more points for better approximation.<br />

Bound on error.<br />

• With finite-difference, have to decrease h for better<br />

accuracy.<br />

• First derivative formula can be derived from this...<br />

...but it is the only one that does not involve<br />

subtraction.<br />

– Joaquim Martins – January 12, 2000 – 7


Complex Functions and Operators in<br />

Fortran<br />

• Relational operators<br />

– Used with if statements to direct the execution<br />

thread.<br />

– Complex algorithm must follow same thread.<br />

– Therefore, compare only the real parts.<br />

– Also, max, min, etc.<br />

• Arithmetic functions and operators:<br />

– Most of these have a mathematical standard<br />

definition that is analytic.<br />

– Some of them are implemented in Fortran.<br />

– Exception: abs<br />

∂u<br />

∂x<br />

= ∂v<br />

∂y =<br />

abs(x + iy) =<br />

<br />

−1 ⇐ x < 0<br />

+1 ⇐ x > 0<br />

<br />

−x − iy ⇐ x < 0<br />

+x + iy ⇐ x ≥ 0 .<br />

– Joaquim Martins – January 12, 2000 – 8


Overloaded Functions<br />

abs ✘ abs(z) =<br />

<br />

−z ⇐ x < 0<br />

+z ⇐ x ≥ 0<br />

tan ✔ tan(z) = e−iz−e iz<br />

e−iz +eiz asin ✔ arcsin(z) = −i log[iz + (1 − z2 ) 1 2]<br />

acos ✔ arccos(z) = −i log[z + (z2 − 1) 1 atan ✔<br />

2]<br />

arctan(z) = i<br />

2 log<br />

<br />

1−iz<br />

1+iz<br />

sinh ✔ sinh(z) = ez−e −z<br />

cosh ✔<br />

2<br />

cosh(z) = ez +e −z<br />

tanh ✔<br />

2<br />

tanh(z) = ez−e −z<br />

ez +e−z <br />

dim ✘<br />

z1 − z2<br />

dim(z1, z2) =<br />

0<br />

<br />

⇐ x1 > x2<br />

⇐ x1 ≤ x2<br />

sign ✘<br />

+|x1|<br />

sign(z1, z2) =<br />

−|x1|<br />

<br />

⇐ x2 ≥ 0<br />

⇐ x2 < 0<br />

max ✘<br />

z1<br />

max(z1, z2) =<br />

z2<br />

<br />

⇐ x1 ≥ x2<br />

⇐ x1 < x2<br />

min ✘ min(z1, z2) =<br />

z1<br />

z2<br />

⇐ x1 ≤ x2<br />

⇐ x1 > x2<br />

– Joaquim Martins – January 12, 2000 – 9


Automatic Implementation<br />

• Cookbook procedure:<br />

– Substitute all real type variable declarations with<br />

complex declarations.<br />

– Define all functions and operators that are not<br />

defined for complex arguments.<br />

– A complex-step can then be added to the desired<br />

variable and the derivative can be estimated by<br />

f ′ ≈ Im[f(x + ih)]/h.<br />

• Fortran 77: write new subroutines, substitute some<br />

of the intrinsic function calls by the subroutine<br />

names, e.g. abs by c abs. But ... need to know<br />

variable types in original code.<br />

• Fortran 90: can overload intrinsic functions<br />

and operators, including comparison operators.<br />

Compiler knows variable types and chooses correct<br />

version of the function or operator.<br />

• Complexify.py: Python script processes code.<br />

complexify.f90: overloaded definitions.<br />

– Joaquim Martins – January 12, 2000 – 10


Structural Sensitivities:<br />

Finite Element Solver<br />

• FESMEH, finite element solver used in MDO.<br />

• Triangular plates and truss elements.<br />

• Wing modeled with spars, ribs and skin.<br />

• Spars and ribs modeled with plates and trusses.<br />

– Joaquim Martins – January 12, 2000 – 11


Sensitivity Estimates vs. Step Size<br />

• Sensitivity of truss stress w.r.t. truss area.<br />

<br />

<br />

<br />

¢¡¤£¦¥§ ©¨£<br />

• Finite-difference: reasonable estimate, if you’re<br />

lucky...<br />

• Complex-step: practically insensitive to step size.<br />

– Joaquim Martins – January 12, 2000 – 12


Computational Accuracy and Cost<br />

Method Sample Sensitivity<br />

Complex –39.049760045804646<br />

ADIFOR –39.049760045809059<br />

Analytic –39.049760045805281<br />

FD –39.049724352820375<br />

• Finite-difference is worst.<br />

• Other methods achieve the solver’s precision.<br />

Method Time Memory<br />

Complex 1.00 1.00<br />

ADIFOR 2.33 8.09<br />

Analytic 0.58 2.42<br />

FD 0.88 0.72<br />

• Analytic: best, but not easy to implement.<br />

• ADIFOR: costly.<br />

• Complex-step: good compromise.<br />

• Caveat: ratios depend on problem.<br />

– Joaquim Martins – January 12, 2000 – 13


Aerodynamic Sensitivities: FLO82<br />

RAE 2822 Airfoil<br />

α = 3.0 o , M∞ = 0.70<br />

Cp<br />

1.20 0.80 0.40 0.00 -0.40 -0.80 -1.20 -1.60 -2.00<br />

+<br />

+<br />

++ +++++++++++++++++++++++++<br />

+<br />

+ + + +<br />

+<br />

+ + +<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

• 2D, cell-centered, finite volume Euler solver.<br />

• Multigrid.<br />

• Implicit residual smoothing.<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+<br />

+ +++<br />

+<br />

+<br />

+<br />

+++ ++++++++++++++++++++++++<br />

• Artificial dissipation options: JST, CUSP, ECUSP<br />

and HCUSP.<br />

• Same numerical schemes as FLO87 and FLO107.<br />

– Joaquim Martins – January 12, 2000 – 14<br />

+ +++++ ++++++++++++++++++++++


Normalized Error, ε<br />

10 0<br />

10 −2<br />

10 −4<br />

10 −6<br />

10 −8<br />

Sensitivity Estimates vs. Step Size<br />

10 −10<br />

∂CD/∂M∞<br />

10 −20<br />

Step Size, h<br />

Complex−Step<br />

Finite−difference<br />

10 −30<br />

– Joaquim Martins – January 12, 2000 – 15


Residual<br />

Convergence of Sensitivity Estimates<br />

10 10<br />

10 5<br />

10 0<br />

10 −5<br />

Complex−Step<br />

Finite−difference<br />

Average density residual<br />

10<br />

0 50 100 150<br />

−10<br />

Number of Iterations<br />

• Both estimates converge at same rate as solver.<br />

– Joaquim Martins – January 12, 2000 – 16


Aerodynamic Sensitivity Results<br />

Sensitivity Complex FD<br />

∂CL/∂α 12.37054751691092 12.3707266652<br />

∂CD/∂α 0.8602380269441042 0.86024234610<br />

∂CM/∂α –0.5026301652982372 –0.5026313670<br />

∂CL/∂M∞ 3.2499722985150532 3.24994475775<br />

∂CD/∂M∞ 0.43978671505102005 0.43978998888<br />

∂CM/∂M∞ –0.99037388394690016 –0.9903747405<br />

• Imaginary part of result converged to the same number of<br />

real part.<br />

– Joaquim Martins – January 12, 2000 –


Drag Coefficient Sensitivity<br />

11<br />

10<br />

9<br />

8<br />

7<br />

6<br />

5<br />

4<br />

3<br />

2<br />

1<br />

0<br />

Drag Coefficient Sensitivity to<br />

Hicks-Henne “Bump” Functions<br />

Complex−Step<br />

Finite−difference<br />

10 20 30 40 50<br />

Design Points<br />

Method Time Memory<br />

Complex 1.00 1.00<br />

FD 0.31 0.55<br />

– Joaquim Martins – January 12, 2000 – 18


Conclusions<br />

• The theory behind the application of the complexstep<br />

method to real-world numerical algorithms was<br />

introduced.<br />

• The implementation of the complex-step method<br />

was successfully automated.<br />

• The sensitivity results given by this method have<br />

been further validated and shown to be very<br />

accurate.<br />

• Unlike finite-differencing, the complex-step method<br />

is relatively insensitive to the step size.<br />

• The complex-step method is now an attractive<br />

alternative to ADIFOR.<br />

– Joaquim Martins – January 12, 2000 – 19


Further Work<br />

• Continue to work on Complexify.py.<br />

Need feedback from users.<br />

• Apply the method to other solvers.<br />

• More detailed stability and convergence analysis.<br />

• Compare with CFD adjoint results.<br />

– Joaquim Martins – January 12, 2000 – 20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!