UFO
ufo_gnssroonedvarcheck_eval_derivs_mod.f90
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
2 ! (C) British Crown Copyright 2020 Met Office
3 !
4 ! This software is licensed under the terms of the Apache Licence Version 2.0
5 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 !-------------------------------------------------------------------------------
7 
8 !> Evaluate the 1st and 2nd deriv. of the cost function.
9 
11 
12 use kinds, only: kind_real
13 use missing_values_mod, only: missing_value
14 
15 private
17 
18 contains
19 
20 SUBROUTINE ops_gpsro_eval_derivs_ba (Nstate, & ! size ot state vector
21  Nobs, & ! no of obs
22  x, & ! current est. of soln
23  xb, & ! background vector
24  yobs, & ! obs. vector
25  ycalc, & ! y(x)
26  bm1, & ! inverse .of bsck cov matrix
27  om1, & ! inv. of obs+forw cov matrix
28  kmat, & ! gradient matrix
29  dj_dx, & ! -ve of first deriv. of cost function
30  d2j_dx2, & ! second deriv. of cost function.
31  diag_d2j) ! vector containing the diagonal values of the matrix above
32 
33 
34 IMPLICIT NONE
35 
36 ! Subroutine arguments:
37 INTEGER, INTENT(IN) :: nstate
38 INTEGER, INTENT(IN) :: nobs
39 REAL(kind_real), INTENT(IN) :: x(:)
40 REAL(kind_real), INTENT(IN) :: xb(:)
41 REAL(kind_real), INTENT(IN) :: yobs(:)
42 REAL(kind_real), INTENT(IN) :: ycalc(:)
43 REAL(kind_real), INTENT(IN) :: bm1(:,:)
44 REAL(kind_real), INTENT(IN) :: om1(:,:)
45 REAL(kind_real), INTENT(IN) :: kmat(:,:)
46 REAL(kind_real), INTENT(OUT) :: dj_dx(:)
47 REAL(kind_real), INTENT(OUT) :: d2j_dx2(:,:)
48 REAL(kind_real), INTENT(OUT) :: diag_d2j(:)
49 
50 ! Local declarations:
51 CHARACTER(len=*), PARAMETER :: routinename = "Ops_GPSRO_eval_derivs_BA"
52 INTEGER :: i
53 REAL(kind_real) :: dx(nstate)
54 REAL(kind_real) :: dy(nobs)
55 REAL(kind_real) :: bdx(nstate)
56 REAL(kind_real) :: ko(nstate,nobs)
57 
58 !--------------------------------------------------------
59 ! 1. Evaluate the 1st and 2nd deriv. of the cost function
60 !--------------------------------------------------------
61 
62 ! Deviation from background
63 
64 dx(:) = x(:) - xb(:)
65 
66 ! Obs. meas-calc
67 
68 dy(:) = yobs(:) - ycalc(:)
69 
70 ! If absolute difference is greater than 1 radian set difference to 0
71 
72 WHERE (abs(dy(:)) > 1.0)
73 
74  dy(:) = 0.0
75 
76 END WHERE
77 
78 ! calc. Bdx matrix ie B^-1(x-xb)
79 
80 bdx(:) = matmul(bm1(:,:), dx(:))
81 
82 ! K^T O^-1 matrix
83 
84 ko(:,:) = matmul(transpose(kmat(:,:)), om1(:,:))
85 
86 ! Calculate -dJ_dx vector -note the NEGATIVE sign
87 
88 dj_dx(:) = matmul(ko(:,:), dy(:)) - bdx(:)
89 !print*, 'dJ_dx components'
90 !write(*,'(10E15.8)') dJ_dx(11), Bdx(11)
91 !write(*,'(10E15.8)') KO(11,:)
92 !write(*,'(10E15.8)') dy(:)
93 
94 ! d2J_dx2 MATRIX
95 
96 d2j_dx2(:,:) = matmul(ko(:,:), kmat(:,:)) + bm1(:,:)
97 
98 ! Save the diagonal terms as a vector.
99 
100 DO i = 1,nstate
101 
102  diag_d2j(i) = d2j_dx2(i,i)
103 
104 END DO
105 
106 END SUBROUTINE ops_gpsro_eval_derivs_ba
107 
Evaluate the 1st and 2nd deriv. of the cost function.
subroutine, public ops_gpsro_eval_derivs_ba(Nstate, Nobs, x, xb, yobs, ycalc, BM1, OM1, Kmat, dJ_dx, d2J_dx2, diag_d2J)