41
22/3/21 22/3/21 WRFVar code development Tangent Linear and Adjoint Code Development Yong-Run Guo 1 National Center for Atmospheric Research P.O.Box 3000, Boulder, CO 80307 Presented at Central Weather Bureau, Taipei, Taiwan 25 February 2010

2015-4-18 Y.-R. Guo 2015-4-18 WRFVar code development Tangent Linear and Adjoint Code Development Yong-Run Guo 1 National Center for Atmospheric Research

Embed Size (px)

Citation preview

23/4/18 Y.-R. Guo23/4/18

WRFVar code development

Tangent Linear and Adjoint Code Development

Yong-Run Guo

1National Center for Atmospheric ResearchP.O.Box 3000, Boulder, CO 80307

Presented at Central Weather Bureau, Taipei, Taiwan

25 February 2010

23/4/18 Y.-R. Guo

INTRODUCTION

23/4/18 Y.-R. Guo

Why do we have this talk about the adjoint code development

manually?

To develop the adjoint of the modelAlready done in MPG/MMM/NCAR for MM5 and WRF

To develop the adjoint of the observation operatorsMust be done by users themselves

About the automatic tangent linear and adjoint generators

23/4/18 Y.-R. Guo

Formulation

Cost function:

Gradient, of with respect to the control variable x’

,J J

T T T T 1 T0 0 0

0

Q N ) C Q( ) ( [ ]{ }K

obsr r

r

CN XrJ 2R NX t X

Forcing term to adjoint model:

T T 1(N ) 2 [N CN(X( ))]-obs

r rC R tt

1

1

10 0

1

Q Q

K Tobs obsr r r r

r

K Tobs obsr r r r

r

J N t CN X t R N t CN X t

N t CN X R N t CN X

23/4/18 Y.-R. Guo

Description of the variables

is a vector of the control variable, at t=0, and

is the non-linear model operator at time is the perturbation vector of the control variable C is the spatial interpolation operator is the observation operator is a observation vector is the observation error covariance matrix.

The superscript means the tangent linear operator, and the superscript is the transpose or adjoint operator.

X 0X

0X( ) Q Xr rt Qr rt tX

NobsN

R

" "T" "

23/4/18 Y.-R. Guo

Availabe Automatical Tangent and Adjoint generators:

TAF --- Transformation of Algorithms in Fortranhttp://www.autodiff.org/?module=Tools&tool=TAF

TAPENADEhttp://www.autodiff.org/?module=Tools&tool=TAPENADE

TAMC --- Tangent linear and Adjoint Model Compilerhttp://autodiff.com/tamc

Still few manual work needed: Assign the independent input variables and the output variables;Check the correctness of tangent linear and adjoint code; Need the manually review, etc.

Do not fully understand the physical meaning and the logic of the code.

23/4/18 Y.-R. Guo

Navon, L. M., X. Zou, J. Derber, and J. Sela, 1992: Variational data assimilation with an adiabatic version of the NMC spectral model. Mon. Wea. Rev.,120.1433-1446.

Guo, Y.-R., Y.-H. Kuo, J. Dudhia, D. Parsons, and C. Rocken, 2000: Four-dimensional variational data assimilation of heterogeneous mesoscale observations for a strong convective case. Mon. Wea. Rev., 128, 619-643.

Huang, X.-Y., et al.,2009: Four-dimensional variational data assimilation for WRF: Formulation and preliminary results. Mon. Wea. Rev., 137, 299-314.

MM5 4DVAR code and documentation

ftp://ftp.ucar.edu/mesouser/MM5_ADJ

References

23/4/18 Y.-R. Guo

During the forward model integration, compute and save the term from , , C, and :

where and are from the observed data. In addition,

users should also develop the code of C and N, and insert the calculations of the above forcing term and cost function in the subroutines. In WRFVar, the 2D and 3D spatial interpolation C and their adjoint code has already developed and located in WRFDA/var/da/da_interpolation. N should be developed if users want to assimilate a new type of observations.

NNobs1R

12 [N -CN(X( ))]obsr rfc R t t

1R Nobs

23/4/18 Y.-R. Guo

In WRFVar, the incremental approach is used.

1

1

1

1

2 [N -CN(X( ))]

2 N -CN X X

2 N CN X CN X

2 CN X

N CN X

obsr r

obsr b r r

obsr b r r

r r

obsr r b r

fc R t t

R t t t

R t t t

R d t t

Innovation

d t t t

For example, the ZTD operator is located in WRFDA/var/da/da_physics, the d(tr) and are located in WRFDA/var/da/da_gpspw.

TN, N , and N CN X rt

23/4/18 Y.-R. Guo

Transformation from control variables V to the increment of the analysis variables: must be completed first:

Cost function:

Gradient, of with respect to the control variable V,J J

T T T T T 1 T

0

U Q N ) C Q( ) ( [v Uv ] v{ }K

obsr r

r

CNrJ 2R N t

10 0

1

1

1

Q Q

Q Uv Q Uv

K Tobs obsr r r r

r

K Tobs obsr r r r

r

J N t CN X R N t CN X

N t CN R N t CN

T T T T T0X Uv, UU B, U=U U U , U U U Up v h h v p

This transformation is completed in the directory: /WRFDA/var/da/da_vtox_transform. Note that the background error covariance matrix B is only appeared here.

0X

23/4/18 Y.-R. Guo

During the adjoint model integration, compute and add the forcing term FC to adjoint model:

where fc was mentioned above, but the adjoint code of and should be developed by users.

In order to develop the adjoint code, the tangent linear code should be developed first.

fcFC TTCN

T(N )TC

23/4/18 Y.-R. Guo

Summary of WRFDA code

The core part of the WRFVar codes:

var/da/da_physics: Observation operators

var/da/_da_{obs-type}: Innovation and residuals

var/da/da_vtox_transform: transformation by using BE

var/da/da_minimization: Conjugate Gradient (CG)

23/4/18 Y.-R. Guo

TANGENT LINEAR CODING

23/4/18 Y.-R. Guo

Tangent linear code development ( see Tech. Note NCAR/TN-435-STR, 71-78, and 59)

The tangent linear operator is the first order approximation of the non-linear operator.

The Taylor expansion

where z is a vector of control variable, and z’ is a vector of the

perturbation of control variable. is a Jacobian matrix.

So the tangent linear operator is valid only when the z’ is very small, and the higher (than the second) order derivatives are very small.

2zOzzz

zzz rrr

QQQ

zz rQ

23/4/18 Y.-R. Guo

To identify the independent variables and their functions, the constant variables in a Fortran code

nAxy

dxAnxdy n 1

BAxy n

dxAnxdy n 1

23/4/18 Y.-R. Guo

( )

( )

12

{ ( )}

( )

( )

( ) ( ) ( ) ( )

[ ( )]{ }

Bf x

Bf x

n

n

y Ae Cx D

dy Ae Bf x dx Cdx

Af xy Cx D

Bg z

A g z f x dx f x g z dzdy Cnx dx

B g z

Where are basic variables, are the perturbations, are the first derivatives. A, B, C, D are constants here. Note that there are no constants appearing in the tangent linear equations because the constants can never be perturbed.

, ,x y z , ,dx dy dz( ), ( )f x g z

23/4/18 Y.-R. Guo

Keep the values of the basic state exactly the same as

those in the non-linear forward operator (model)

because that is the starting point of the linearization in

developing the tangent linear operator (model). In a

practical application, to save the disk space to store the

basic state, one may use some kind of approximation of

the basic state (such as time-averaging values over

certain intervals, or time-interpolation during the

interval). But before doing that, the validation check of

the tangent linear code must be done to guarantee that

the results are making sense.

23/4/18 Y.-R. Guo

Correctness (or tangent linear validation) check

From Taylor expansion, set , we can obtain

'z h

r

r

Q ( ) Q ( )( ) 1 ( )

Pr z h z

Oh

Where is a parameter, and is a constant vector. When is small enough, but larger than the machine accuracy, the values of should be very close to 1.0 if the tangent linear code is correct. If the values of is not close to 1.0 with small , the tangent linear code is incorrect (or invalid).

In the correctness check, the Fortran compiler must be with the option –r8.

h

( )

( )

23/4/18 Y.-R. Guo

Although the tangent linear operator is not used in the minimization process in a full, not incremental, 4DVAR system, the adjoint of the observation operator is developed based on the tangent linear operator (the first order approximation of the non-linear operator). So, the correctness of the tangent linear code must be guaranteed in developing the adjoint code.

In increment 3DVAR, the tangent linear operator will be used as the forward observation operator.

23/4/18 Y.-R. Guo

Example: dew point TD

The observation operator N for TD is

3

2 log(1(0.622 )

( ))S

qpS

S q

TD

And when TD > T, TD = T.

Where the T, p, q are temperature, pressure and specific humidity. S1, S2, S3 are constants.

Fortran code for TD: subroutine TQTD

Input variable: T, P, Q

Output variable: TD

23/4/18 Y.-R. Guo

SUBROUTINE TQTD(T,TD,P,Q)

3

2 log(1(0.622 )

( ))S

qpS

S q

TD

and when TD > T, TD = T.

23/4/18 Y.-R. Guo

Tangent linear code of TD

Input variable: T, P, Q

Basic state: T9, P9,

Q9

Output variable: TD

23/4/18 Y.-R. Guo

Exercise:

(i) relative humidity --- p (cb), T (k), and q (kg/kg)

21

3

( )

( )

( )

s

s s

ss

s

s

q eRH A

q p e

eq A

p e

Te Exp

T

where 1 2 30.622, 0.378, 611.2, 17.67, 243.5.A and

Note: the units for temperature, specific humidity, and pressue are C, kg/kg, and Pa, respectively. The subroutines are located in WRFDA/var/da/da_physics:

da_tp_to_qs.inc da_tp_to_qs_lin1.inc da_tp_to_qs_adj1.inc da_tpq_to_rh.inc da_tpq_to_rh_lin1.inc

A Fortran program:RH.f90 for the tangent linear accuracy check.

The results are in the next slide.

23/4/18 Y.-R. Guo

FORTRAN STOPZ0: t,p,q,es,qs,rh: 0.29000E+03 0.10000E+06 0.10000E-01 0.19180E+04 0.12012E-01 0.83248E+02

Tangent linear Check for qs ================ 1 alpha=0.020 qs, qs0, dqs: 0.120881E-01 0.120123E-01 0.377992E-02 accu.=0.100278457E+01 2 alpha=0.040 qs, qs0, dqs: 0.121643E-01 0.120123E-01 0.377992E-02 accu.=0.100557812E+01 3 alpha=0.060 qs, qs0, dqs: 0.122410E-01 0.120123E-01 0.377992E-02 accu.=0.100838067E+01 4 alpha=0.080 qs, qs0, dqs: 0.123181E-01 0.120123E-01 0.377992E-02 accu.=0.101119225E+01 5 alpha=0.100 qs, qs0, dqs: 0.123956E-01 0.120123E-01 0.377992E-02 accu.=0.101401289E+01 6 alpha=0.120 qs, qs0, dqs: 0.124735E-01 0.120123E-01 0.377992E-02 accu.=0.101684263E+01 7 alpha=0.140 qs, qs0, dqs: 0.125519E-01 0.120123E-01 0.377992E-02 accu.=0.101968148E+01 8 alpha=0.160 qs, qs0, dqs: 0.126307E-01 0.120123E-01 0.377992E-02 accu.=0.102252947E+01 9 alpha=0.180 qs, qs0, dqs: 0.127100E-01 0.120123E-01 0.377992E-02 accu.=0.102538664E+01 10 alpha=0.200 qs, qs0, dqs: 0.127896E-01 0.120123E-01 0.377992E-02 accu.=0.102825302E+01 11 alpha=0.220 qs, qs0, dqs: 0.128698E-01 0.120123E-01 0.377992E-02 accu.=0.103112863E+01 12 alpha=0.240 qs, qs0, dqs: 0.129503E-01 0.120123E-01 0.377992E-02 accu.=0.103401349E+01 13 alpha=0.260 qs, qs0, dqs: 0.130314E-01 0.120123E-01 0.377992E-02 accu.=0.103690765E+01 14 alpha=0.280 qs, qs0, dqs: 0.131128E-01 0.120123E-01 0.377992E-02 accu.=0.103981113E+01 15 alpha=0.300 qs, qs0, dqs: 0.131947E-01 0.120123E-01 0.377992E-02 accu.=0.104272395E+01 16 alpha=0.320 qs, qs0, dqs: 0.132771E-01 0.120123E-01 0.377992E-02 accu.=0.104564616E+01 17 alpha=0.340 qs, qs0, dqs: 0.133599E-01 0.120123E-01 0.377992E-02 accu.=0.104857777E+01 18 alpha=0.360 qs, qs0, dqs: 0.134432E-01 0.120123E-01 0.377992E-02 accu.=0.105151881E+01 19 alpha=0.380 qs, qs0, dqs: 0.135269E-01 0.120123E-01 0.377992E-02 accu.=0.105446932E+01 20 alpha=0.400 qs, qs0, dqs: 0.136111E-01 0.120123E-01 0.377992E-02 accu.=0.105742933E+01

Tangent linear Check for rh ================ 1 alpha=0.020 rh, rh0, drh: 0.830568E+02 0.832480E+02 -0.954611E+01 accu.=0.100132192E+01 2 alpha=0.040 rh, rh0, drh: 0.828651E+02 0.832480E+02 -0.954611E+01 accu.=0.100261687E+01 3 alpha=0.060 rh, rh0, drh: 0.826730E+02 0.832480E+02 -0.954611E+01 accu.=0.100388514E+01 4 alpha=0.080 rh, rh0, drh: 0.824804E+02 0.832480E+02 -0.954611E+01 accu.=0.100512699E+01 5 alpha=0.100 rh, rh0, drh: 0.822873E+02 0.832480E+02 -0.954611E+01 accu.=0.100634271E+01 6 alpha=0.120 rh, rh0, drh: 0.820938E+02 0.832480E+02 -0.954611E+01 accu.=0.100753255E+01 7 alpha=0.140 rh, rh0, drh: 0.818999E+02 0.832480E+02 -0.954611E+01 accu.=0.100869678E+01 8 alpha=0.160 rh, rh0, drh: 0.817056E+02 0.832480E+02 -0.954611E+01 accu.=0.100983568E+01 9 alpha=0.180 rh, rh0, drh: 0.815109E+02 0.832480E+02 -0.954611E+01 accu.=0.101094950E+01 10 alpha=0.200 rh, rh0, drh: 0.813158E+02 0.832480E+02 -0.954611E+01 accu.=0.101203851E+01 11 alpha=0.220 rh, rh0, drh: 0.811203E+02 0.832480E+02 -0.954611E+01 accu.=0.101310295E+01 12 alpha=0.240 rh, rh0, drh: 0.809245E+02 0.832480E+02 -0.954611E+01 accu.=0.101414309E+01 13 alpha=0.260 rh, rh0, drh: 0.807284E+02 0.832480E+02 -0.954611E+01 accu.=0.101515917E+01 14 alpha=0.280 rh, rh0, drh: 0.805319E+02 0.832480E+02 -0.954611E+01 accu.=0.101615145E+01 15 alpha=0.300 rh, rh0, drh: 0.803351E+02 0.832480E+02 -0.954611E+01 accu.=0.101712017E+01 16 alpha=0.320 rh, rh0, drh: 0.801381E+02 0.832480E+02 -0.954611E+01 accu.=0.101806558E+01 17 alpha=0.340 rh, rh0, drh: 0.799407E+02 0.832480E+02 -0.954611E+01 accu.=0.101898793E+01 18 alpha=0.360 rh, rh0, drh: 0.797430E+02 0.832480E+02 -0.954611E+01 accu.=0.101988744E+01 19 alpha=0.380 rh, rh0, drh: 0.795451E+02 0.832480E+02 -0.954611E+01 accu.=0.102076436E+01 20 alpha=0.400 rh, rh0, drh: 0.793470E+02 0.832480E+02 -0.954611E+01 accu.=0.102161892E+01

23/4/18 Y.-R. Guo

1 top

s

Z

Z

pq dz

TIWV

R

(ii) precipitable water --- accurate formula

Assuming that the model has KX layers in vertical, and the heights of surface and model top, , , and the thickness of the layers, are known.

szkz

topZ

23/4/18 Y.-R. Guo

ADJOINT CODING

23/4/18 Y.-R. Guo

Adjoint code development (see Tech. Note NCAR/TN-435-STR, 79-99, and 59-60)

Adjoint matrix is a transpose of the TLM matrix.

The basic rule in writing the adjoint code from a tangent linear code is called the “Reverse rule”:

In a Fortran statement,

each of the left-side perturbation variable is moved to the right-side, and place the right-side variable in the position of this perturbation variable, with no change of its coefficient, to form each of the Fortran statements. One statement in the tangent linear code may become several statements in the adjoint code.

23/4/18 Y.-R. Guo

The Fortran statement of the tangent linear:

dy Adx Bdz Cdw Ddx

The corresponding Fortran code of its adjoint:

dx Ady

dz Bdy

dw Cdy

dx Ddy dx

23/4/18 Y.-R. Guo

The last statement in the tangent linear code corresponds to the first statement in the adjoint code, and the first statement in the tangent linear code corresponds to the last statement in the adjoint code. The output variables from tangent linear code become the input variables to the adjoint code.

In a segment of Fortran code (a series of statements in sub-routines, or main program),

23/4/18 Y.-R. Guo

Basic N

Tangent linear code

Statement 1Statement 2……………..

Statement N

Basic 1

Basic 2

Basic N

Adjoint code

Statement N(i)

……………..

Statement 2(i)Statement 1(i)

Basic 2

Basic 1

The following example clearly shows that the “reverse rule” is equivalent to the transpose of the matrix.

23/4/18 Y.-R. Guo

adjoint:

ij 11 21 311

112 22 322 T

213 23 333

314 24 344

a a axy

a a axX y P Y

a a axy

a a ax

subroutine a_fwd(x,y,a,n,m)!

integer :: i, j, n, mreal, dimension(1:m) :: xreal, dimension(1:n) :: yreal, dimension(n,m) :: a

!x = 0.0

! do j = 1,m! Fortran 77:! do i = 1,n! x(j) = a(i,j)*y(i) + x(j)! enddo! Fortran 90: x(j) = sum(a(:,j)*y(:))

enddo!end subroutine a_fwd

subroutine fwd(x,y,a,n,m)!

integer :: i, j, n, mreal, dimension(1:m) :: xreal, dimension(1:n) :: yreal, dimension(n,m) :: a

!y = 0.0

! do i = 1,n! Fortran 77:! do j = 1,m! y(i) = a(i,j)*x(j) + y(i)! enddo! Fortran 90: y(i) = sum(a(i,:)*x(:)) enddo!end subroutine fwd

11 11 12 13 14

22 21 22 23 24

331 32 33 343

4

xy a a a a

xY y a a a a PX

xa a a ay

x

ij

forward:

23/4/18 Y.-R. Guo

To count the contributions from the input variables in the tangent linear code correctly

‘reuse’ problem --- All the contributions from an input variable in the TLM should be counted in the adjoint code, neither less nor more feedback to that TLM input variable. For example,

Tangent linear codedy Adx Bdz

df Cdx

Adjoint code

dx Cdf

dx Ady dx

dz Bdy

Here dx used twice.

23/4/18 Y.-R. Guo

‘re-defined’ problem --- Do not mix the feedback in the adjoint code when the input variables were re-defined in TLM.

Tangent linear code (redundant)dy Adx Bdz Cdx

dy Ddx Edw

Adjoint code

dx Ddy

dw Edy

dx Ady dx

dz Bdy

dx Cdy dx

Here dy is redefined.

Not need these lines of code

23/4/18 Y.-R. Guo

* Re-calculate the basic state to guarantee it is exactly the same as that in the tangent linear model at the same point in the code sequence. This means that coefficients in the statements must be exactly the same in both tangent linear and its adjoint code.

23/4/18 Y.-R. Guo

Correctness check

r r r r(P ) (P ) P (P )T T Tz z z z

where is the arbitrary perturbation vector, is the tangent linear operator, and is the adjoint operator.

Note: There is no approximation from TLM to Adjoint, the correctness check must have ’13-digit same’ accuracy for 64-bit data (with the compiler option –r8). Otherwise, the adjoint code is wrong.

z rP

rPT

23/4/18 Y.-R. Guo

program adj_checkinteger, parameter :: n = 3, m = 6integer :: i, jreal :: Righy, Leftreal, dimension(1:m) :: x0, xreal, dimension(1:n) :: yreal, dimension(n,m) :: a

! Initialize x:do j = 1,m x(j) = sin((3.14156/180.)*float(j-1)*15.)

! To keep a copy of x: x0(j) = x(j)

! Matrix a:do i = 1,na(i,j) = 5.25*float(i) + float(j)enddoenddo

!print 10, ((a(i,j),j=1,m),i=1,n)

10 format('Matrix:a:'/(6f8.2))print 11, (x (j),j=1,m)

11 format('input vector x :'/6e15.5)12 format('input vector x0:'/6e15.5)

!=======================================! Forward:

call fwd(x,y,a,n,m)! Adjoint:

call a_fwd(x,y,a,n,m)!=======================================! ! T

Px Px

T Tx P Px

!

!Right = sum(y*y)

!

!! print 11, (x (j),j=1,m)! print 12, (x0(j),j=1,m) ! !

Left = sum(x0*x)!

print 20, Right, Left20 format(/'Adjoint check:'/ & 10x,' Right=',E20.14/ & 10x,' Left =',E20.14)

23/4/18 Y.-R. Guo

FORTRAN STOPMatrix:a: 6.25 7.25 8.25 9.25 10.25 11.25 11.50 12.50 13.50 14.50 15.50 16.50 16.75 17.75 18.75 19.75 20.75 21.75input vector x : 0.00000E+00 0.25882E+00 0.50000E+00 0.70710E+00 0.86602E+00 0.96592E+00Adjoint check: Right=0.79797532647807E+04 Left =0.79797532647807E+04

Remember to have compiler option –r8, i.e. in Linux/PGI,pgf90 –r8 adj_check.f90 –o adj_check.exe

adj_check.exe

23/4/18 Y.-R. Guo

Examples:

(a) precipitable water PW

The observation operator N for PW is

1

( )KX

k

q kp

PWg

Where the control variable q(k) is the specific humidity

At k. , and is the acceleration of gravity.

In this case, N = N’ because the observation operator is linear.

level s tp p p g

23/4/18 Y.-R. Guo

Fortran code for PW: subroutine PWATER:

Input variable: QVA

Output variable: PW

Adjoint code of PW:

Input variable: PW

Output variable: QVA

23/4/18 Y.-R. Guo

Adjoint code of TDTangent linear code of TD

Input variable: T, P, Q

Basic state: T9, P9, Q9

Output variable: TD

23/4/18 Y.-R. Guo

Exercise:

(i) Can you point out which variables are re-used or re-defined in subroutine LTQTD (ATQTD)?

(ii) Write the adjoint code for relative humidity.

(iii)Write the adjoint code for accurate formula of precipitable water.