SABER
tools_wrfda.F90
Go to the documentation of this file.
1 # 1 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/tools_wrfda.fypp"
2 # 1 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/../instrumentation.fypp" 1
3 # 1 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/../subr_list.fypp" 1
4 !----------------------------------------------------------------------
5 ! Header: subr_list
6 !> Subroutines/functions list
7 ! Author: Benjamin Menetrier
8 ! Licensing: this code is distributed under the CeCILL-C license
9 ! Copyright 2015-... UCAR, CERFACS, METEO-FRANCE and IRIT
10 !----------------------------------------------------------------------
11 
12 # 726 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/../subr_list.fypp"
13 # 2 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/../instrumentation.fypp" 2
14 !----------------------------------------------------------------------
15 ! Header: instrumentation
16 !> Instrumentation functions
17 ! Author: Benjamin Menetrier
18 ! Licensing: this code is distributed under the CeCILL-C license
19 ! Copyright 2015-... UCAR, CERFACS, METEO-FRANCE and IRIT
20 !----------------------------------------------------------------------
21 
22 # 112 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/../instrumentation.fypp"
23 # 2 "/Users/miesch/JEDI/code/working_copy/public/fv3-bundle/saber/src/saber/external/tools_wrfda.fypp" 2
24 !----------------------------------------------------------------------
25 ! Module: tools_wrfda
26 !> WRFDA functions
27 ! Source: https://github.com/wrf-model/WRFDA
28 ! Author: This routine is from WRFDA
29 ! Original licensing: none
30 ! Modified by Benjamin Menetrier for BUMP
31 ! Licensing: this code is distributed under the CeCILL-C license
32 ! Copyright 2015-... UCAR, CERFACS, METEO-FRANCE and IRIT
33 !----------------------------------------------------------------------
35 
36 use tools_const, only: zero,one
37 use tools_kinds, only: kind_real
38 use type_mpl, only: mpl_type
39 
40 
41 implicit none
42 
43 interface pseudoinv
44  module procedure wrfda_pseudoinv
45 end interface
47  module procedure wrfda_da_eof_decomposition
48 end interface
49 
50 private
52 
53 contains
54 
55 !----------------------------------------------------------------------
56 ! Subroutine: wrfda_pseudoinv
57 !> Compute pseudo inverse of a symmetric matrix.
58 !----------------------------------------------------------------------
59 subroutine wrfda_pseudoinv(mpl,n,a,c,mmax,var_th)
60 
61 implicit none
62 
63 ! Passed variables
64 type(mpl_type),intent(inout) :: mpl !< MPI data
65 integer,intent(in) :: n !< Matrix rank
66 real(kind_real),intent(in) :: a(n,n) !< Matrix
67 real(kind_real),intent(out) :: c(n,n) !< Matrix inverse
68 integer,intent(in),optional :: mmax !< Dominant mode
69 real(kind_real),intent(in),optional :: var_th !< Variance threshold
70 
71 ! Local variables
72 integer :: k,k2,m,lmmax
73 real(kind_real),allocatable :: work(:,:),evec(:,:),eval(:),laminvet(:,:)
74 real(kind_real),allocatable :: summ,total_variance,cumul_variance
75 
76 ! Set name
77 
78 
79 ! Probe in
80 
81 
82 ! Allocation
83 allocate(work(n,n))
84 allocate(evec(n,n))
85 allocate(eval(n))
86 allocate(laminvet(n,n))
87 
88 ! Initialization
89 work = a
90 laminvet = zero
91 
92 ! EOF decomposition
93 call da_eof_decomposition(mpl,n,work,evec,eval)
94 
95 ! Select dominant mode
96 if (present(mmax)) then
97  ! Input argument
98  lmmax = mmax
99 else
100  if (present(var_th)) then
101  ! Based on variance threshold
102  summ = zero
103  do m=1,n
104  summ = summ+eval(m)
105  end do
106  total_variance = summ
107  cumul_variance = zero
108  lmmax = n
109  do m=1,n
110  cumul_variance = cumul_variance+eval(m)/total_variance
111  if (cumul_variance>one-var_th ) then
112  lmmax = m-1
113  exit
114  end if
115  end do
116  else
117  call mpl%abort('wrfda_pseudoinv','either dominant mode or variance threshold should be specified')
118  end if
119 end if
120 if (lmmax>n) call mpl%abort('wrfda_pseudoinv','dominant mode should smaller than the matrix rank')
121 
122 ! Lam{-1} . E^T:
123 do k=1,n
124  do m=1,lmmax
125  laminvet(m,k) = evec(k,m)/eval(m)
126  end do
127 end do
128 
129 ! <a,a>^{-1} = E . Lam{-1} . E^T:
130 do k=1,n
131  do k2=1,k
132  summ = zero
133  do m=1,n
134  summ = summ+evec(k,m)*laminvet(m,k2)
135  end do
136  c(k,k2) = summ
137  end do
138 end do
139 
140 ! Symmetry
141 do k=1,n
142  do k2=k+1,n
143  c(k,k2) = c(k2,k)
144  end do
145 end do
146 
147 ! Release memory
148 deallocate(work)
149 deallocate(evec)
150 deallocate(eval)
151 deallocate(laminvet)
152 
153 ! Probe out
154 
155 
156 end subroutine wrfda_pseudoinv
157 
158 !----------------------------------------------------------------------
159 ! Subroutine: wrfda_da_eof_decomposition
160 !> Compute eigenvectors E and eigenvalues L of covariance matrix.
161 !! B_{x} defined by equation: E^{T} B_{x} E = L, given input kz x kz matrix.
162 !----------------------------------------------------------------------
163 subroutine wrfda_da_eof_decomposition(mpl,kz,bx,e,l)
164 
165 implicit none
166 
167 ! Passed variables
168 type(mpl_type),intent(inout) :: mpl !< MPI data
169 integer, intent(in) :: kz !< Dimension of error matrix
170 real(kind_real),intent(in) :: bx(1:kz,1:kz) !< Vert. background error
171 real(kind_real),intent(out) :: e(1:kz,1:kz) !< Eigenvectors of Bx
172 real(kind_real),intent(out) :: l(1:kz) !< Eigenvalues of Bx
173 
174 ! Local variables
175 integer :: work,ierr,m
176 real(kind_real) :: work_array(1:3*kz-1),ecopy(1:kz,1:kz),lcopy(1:kz)
177 
178 ! Set name
179 
180 
181 ! Probe in
182 
183 
184 ! Initialization
185 work = 3*kz-1
186 ecopy = bx
187 lcopy = zero
188 
189 ! Perform global eigenvalue decomposition using LAPACK software
190 call dsyev('V','U',kz,ecopy,kz,lcopy,work_array,work,ierr)
191 if (ierr/=0) call mpl%abort('wrfda_da_eof_decomposition','dsyev failed')
192 
193 ! Swap order of eigenvalues, vectors so 1st is one with most variance
194 do m=1,kz
195  l(m) = lcopy(kz+1-m)
196  e(1:kz,m) = ecopy(1:kz,kz+1-m)
197 end do
198 
199 ! Probe out
200 
201 
202 end subroutine wrfda_da_eof_decomposition
203 
204 end module tools_wrfda
Subroutines/functions list.
Definition: tools_const.F90:31
real(kind_real), parameter, public one
One.
Definition: tools_const.F90:42
real(kind_real), parameter, public zero
Zero.
Definition: tools_const.F90:37
Kinds definition.
Definition: tools_kinds.F90:9
integer, parameter, public kind_real
Real kind alias for the whole code.
Definition: tools_kinds.F90:25
Subroutines/functions list.
Definition: tools_wrfda.F90:34
subroutine wrfda_pseudoinv(mpl, n, a, c, mmax, var_th)
Compute pseudo inverse of a symmetric matrix.
Definition: tools_wrfda.F90:60
subroutine wrfda_da_eof_decomposition(mpl, kz, bx, e, l)
Compute eigenvectors E and eigenvalues L of covariance matrix. B_{x} defined by equation: E^{T} B_{x}...
Generic ranks, dimensions and types.
Definition: type_mpl.F90:42