Download presentation

Presentation is loading. Please wait.

Published byAlexander Edsell Modified about 1 year ago

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

2
Y.-R. Guo INTRODUCTION

3
Y.-R. Guo Why do we have this talk about the adjoint code development manually? To develop the adjoint of the model Already done in MPG/MMM/NCAR for MM5 and WRF To develop the adjoint of the observation operators Must be done by users themselves About the automatic tangent linear and adjoint generators

4
Y.-R. Guo Formulation Cost function: Gradient, of with respect to the control variable x ’ Forcing term to adjoint model:

5
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.

6
Y.-R. Guo Availabe Automatical Tangent and Adjoint generators: TAF --- Transformation of Algorithms in Fortran TAPENADE TAMC --- Tangent linear and Adjoint Model Compiler 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.

7
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., 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, Huang, X.-Y., et al.,2009: Four-dimensional variational data assimilation for WRF: Formulation and preliminary results. Mon. Wea. Rev., 137, MM5 4DVAR code and documentation ftp://ftp.ucar.edu/mesouser/MM5_ADJ References

8
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.

9
Y.-R. Guo In WRFVar, the incremental approach is used. For example, the ZTD operator is located in WRFDA/var/da/da_physics, the d(t r ) and are located in WRFDA/var/da/da_gpspw.

10
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 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.

11
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.

12
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)

13
Y.-R. Guo TANGENT LINEAR CODING

14
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.

15
Y.-R. Guo To identify the independent variables and their functions, the constant variables in a Fortran code

16
Y.-R. Guo 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.

17
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.

18
Y.-R. Guo Correctness (or tangent linear validation) check From Taylor expansion, set, we can obtain 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.

19
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.

20
Y.-R. Guo Example: dew point TD The observation operator N for TD is 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

21
Y.-R. Guo SUBROUTINE TQTD(T,TD,P,Q) and when TD > T, TD = T.

22
Y.-R. Guo Tangent linear code of TD Input variable: T, P, Q Basic state: T9, P9, Q9 Output variable: TD

23
Y.-R. Guo Exercise: (i) relative humidity --- p (cb), T (k), and q (kg/kg) where 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.

24
Y.-R. Guo FORTRAN STOP Z0: t,p,q,es,qs,rh: E E E E E E+02 Tangent linear Check for qs ================ 1 alpha=0.020 qs, qs0, dqs: E E E-02 accu.= E+01 2 alpha=0.040 qs, qs0, dqs: E E E-02 accu.= E+01 3 alpha=0.060 qs, qs0, dqs: E E E-02 accu.= E+01 4 alpha=0.080 qs, qs0, dqs: E E E-02 accu.= E+01 5 alpha=0.100 qs, qs0, dqs: E E E-02 accu.= E+01 6 alpha=0.120 qs, qs0, dqs: E E E-02 accu.= E+01 7 alpha=0.140 qs, qs0, dqs: E E E-02 accu.= E+01 8 alpha=0.160 qs, qs0, dqs: E E E-02 accu.= E+01 9 alpha=0.180 qs, qs0, dqs: E E E-02 accu.= E alpha=0.200 qs, qs0, dqs: E E E-02 accu.= E alpha=0.220 qs, qs0, dqs: E E E-02 accu.= E alpha=0.240 qs, qs0, dqs: E E E-02 accu.= E alpha=0.260 qs, qs0, dqs: E E E-02 accu.= E alpha=0.280 qs, qs0, dqs: E E E-02 accu.= E alpha=0.300 qs, qs0, dqs: E E E-02 accu.= E alpha=0.320 qs, qs0, dqs: E E E-02 accu.= E alpha=0.340 qs, qs0, dqs: E E E-02 accu.= E alpha=0.360 qs, qs0, dqs: E E E-02 accu.= E alpha=0.380 qs, qs0, dqs: E E E-02 accu.= E alpha=0.400 qs, qs0, dqs: E E E-02 accu.= E+01 Tangent linear Check for rh ================ 1 alpha=0.020 rh, rh0, drh: E E E+01 accu.= E+01 2 alpha=0.040 rh, rh0, drh: E E E+01 accu.= E+01 3 alpha=0.060 rh, rh0, drh: E E E+01 accu.= E+01 4 alpha=0.080 rh, rh0, drh: E E E+01 accu.= E+01 5 alpha=0.100 rh, rh0, drh: E E E+01 accu.= E+01 6 alpha=0.120 rh, rh0, drh: E E E+01 accu.= E+01 7 alpha=0.140 rh, rh0, drh: E E E+01 accu.= E+01 8 alpha=0.160 rh, rh0, drh: E E E+01 accu.= E+01 9 alpha=0.180 rh, rh0, drh: E E E+01 accu.= E alpha=0.200 rh, rh0, drh: E E E+01 accu.= E alpha=0.220 rh, rh0, drh: E E E+01 accu.= E alpha=0.240 rh, rh0, drh: E E E+01 accu.= E alpha=0.260 rh, rh0, drh: E E E+01 accu.= E alpha=0.280 rh, rh0, drh: E E E+01 accu.= E alpha=0.300 rh, rh0, drh: E E E+01 accu.= E alpha=0.320 rh, rh0, drh: E E E+01 accu.= E alpha=0.340 rh, rh0, drh: E E E+01 accu.= E alpha=0.360 rh, rh0, drh: E E E+01 accu.= E alpha=0.380 rh, rh0, drh: E E E+01 accu.= E alpha=0.400 rh, rh0, drh: E E E+01 accu.= E+01

25
Y.-R. Guo (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.

26
Y.-R. Guo ADJOINT CODING

27
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.

28
Y.-R. Guo The Fortran statement of the tangent linear: The corresponding Fortran code of its adjoint:

29
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),

30
Y.-R. Guo Basic N Tangent linear code Statement 1 Statement 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.

31
Y.-R. Guo adjoint: i j subroutine a_fwd(x,y,a,n,m) ! integer :: i, j, n, m real, dimension(1:m) :: x real, dimension(1:n) :: y real, 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, m real, dimension(1:m) :: x real, dimension(1:n) :: y real, 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 i j forward:

32
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 code Adjoint code Here dx used twice.

33
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) Adjoint code Here dy is redefined. Not need these lines of code

34
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.

35
Y.-R. Guo Correctness check 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.

36
Y.-R. Guo program adj_check integer, parameter :: n = 3, m = 6 integer :: i, j real :: Righy, Left real, dimension(1:m) :: x0, x real, dimension(1:n) :: y real, dimension(n,m) :: a ! Initialize x: do j = 1,m x(j) = sin(( /180.)*float(j-1)*15.) ! To keep a copy of x: x0(j) = x(j) ! Matrix a: do i = 1,n a(i,j) = 5.25*float(i) + float(j) enddo ! print 10, ((a(i,j),j=1,m),i=1,n) 10format('Matrix:a:'/(6f8.2)) print 11, (x (j),j=1,m) 11format('input vector x :'/6e15.5) 12format('input vector x0:'/6e15.5) !======================================= ! Forward: call fwd(x,y,a,n,m) ! Adjoint: call a_fwd(x,y,a,n,m) !======================================= ! ! 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, Left 20format(/'Adjoint check:'/ & 10x,' Right=',E20.14/ & 10x,' Left =',E20.14)

37
Y.-R. Guo FORTRAN STOP Matrix:a: input vector x : E E E E E E+00 Adjoint check: Right= E+04 Left = E+04 Remember to have compiler option –r8, i.e. in Linux/PGI, pgf90 –r8 adj_check.f90 –o adj_check.exe adj_check.exe

38
Y.-R. Guo Examples: (a)precipitable water PW The observation operator N for PW is 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.

39
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

40
Y.-R. Guo Adjoint code of TDTangent linear code of TD Input variable: T, P, Q Basic state: T9, P9, Q9 Output variable: TD

41
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.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google