FV3 Bundle
type_hdiag.F90
Go to the documentation of this file.
1 !----------------------------------------------------------------------
2 ! Module: type_hdiag
3 ! Purpose: hybrid diagnostics derived type
4 ! Author: Benjamin Menetrier
5 ! Licensing: this code is distributed under the CeCILL-C license
6 ! Copyright © 2015-... UCAR, CERFACS, METEO-FRANCE and IRIT
7 !----------------------------------------------------------------------
8 module type_hdiag
9 
10 use tools_kinds, only: kind_real
11 use type_avg, only: avg_type
12 use type_bpar, only: bpar_type
13 use type_diag, only: diag_type
14 use type_displ, only: displ_type
15 use type_ens, only: ens_type
16 use type_geom, only: geom_type
17 use type_io, only: io_type
18 use type_mom, only: mom_type
19 use type_mpl, only: mpl_type
20 use type_nam, only: nam_type
21 use type_rng, only: rng_type
22 use type_samp, only: samp_type
23 
24 implicit none
25 
26 ! Hybrid diagnostics derived type
28  type(avg_type) :: avg_1 ! Averaged statistics, first ensemble
29  type(avg_type) :: avg_2 ! Averaged statistics, second ensemble
30  type(avg_type) :: avg_wgt ! Averaged statistics weights
31  type(samp_type) :: samp ! Sampling
32  type(displ_type) :: displ ! Displacement
33  type(mom_type) :: mom_1 ! Moments, first ensemble
34  type(mom_type) :: mom_2 ! Moments, second ensemble
35  type(diag_type) :: cov_1 ! Covariance, first ensemble
36  type(diag_type) :: cov_2 ! Covariance, second ensemble
37  type(diag_type) :: cor_1 ! Correlation, first ensemble
38  type(diag_type) :: cor_2 ! Correlation, second ensemble
39  type(diag_type) :: loc_1 ! Localization, first ensemble
40  type(diag_type) :: loc_2 ! Localization, second ensemble
41  type(diag_type) :: loc_3 ! Localization, low-resolution ensemble
42 contains
43  procedure :: run_hdiag => hdiag_run_hdiag
44 end type hdiag_type
45 
46 private
47 public :: hdiag_type
48 
49 contains
50 
51 !----------------------------------------------------------------------
52 ! Subroutine: hdiag_run_hdiag
53 ! Purpose: HDIAG driver
54 !----------------------------------------------------------------------
55 subroutine hdiag_run_hdiag(hdiag,mpl,rng,nam,geom,bpar,io,ens1,ens2)
56 
57 implicit none
58 
59 ! Passed variables
60 class(hdiag_type),intent(inout) :: hdiag ! Hybrid diagnostics
61 type(mpl_type),intent(inout) :: mpl ! MPI data
62 type(rng_type),intent(inout) :: rng ! Random number generator
63 type(nam_type),intent(inout) :: nam ! Namelist
64 type(geom_type),intent(in) :: geom ! Geometry
65 type(bpar_type),intent(in) :: bpar ! Block parameters
66 type(io_type),intent(in) :: io ! I/O
67 type(ens_type),intent(in) :: ens1 ! Ensemble 1
68 type(ens_type),intent(in),optional :: ens2 ! Ensemble 2
69 
70 ! Local variables
71 integer :: ib
72 character(len=1024) :: filename
73 
74 ! Setup sampling
75 write(mpl%info,'(a)') '-------------------------------------------------------------------'
76 write(mpl%info,'(a,i5,a)') '--- Setup sampling (nc1 = ',nam%nc1,')'
77 call flush(mpl%info)
78 call hdiag%samp%setup_sampling(mpl,rng,nam,geom,bpar,io,ens1)
79 
80 ! Compute MPI distribution, halo A
81 write(mpl%info,'(a)') '-------------------------------------------------------------------'
82 write(mpl%info,'(a)') '--- Compute MPI distribution, halos A'
83 call flush(mpl%info)
84 call hdiag%samp%compute_mpi_a(mpl,nam,geom)
85 
86 if (nam%new_lct.or.nam%var_diag.or.nam%local_diag.or.nam%displ_diag) then
87  ! Compute MPI distribution, halos A-B
88  write(mpl%info,'(a)') '-------------------------------------------------------------------'
89  write(mpl%info,'(a)') '--- Compute MPI distribution, halos A-B'
90  call flush(mpl%info)
91  call hdiag%samp%compute_mpi_ab(mpl,nam,geom)
92 end if
93 
94 if (nam%displ_diag) then
95  ! Compute MPI distribution, halo D
96  write(mpl%info,'(a)') '-------------------------------------------------------------------'
97  write(mpl%info,'(a)') '--- Compute MPI distribution, halo D'
98  call flush(mpl%info)
99  call hdiag%samp%compute_mpi_d(mpl,nam,geom)
100 
101  ! Compute displacement diagnostic
102  write(mpl%info,'(a)') '-------------------------------------------------------------------'
103  write(mpl%info,'(a)') '--- Compute displacement diagnostic'
104  call flush(mpl%info)
105  call hdiag%displ%compute(mpl,nam,geom,hdiag%samp,ens1)
106 end if
107 
108 ! Compute MPI distribution, halo C
109 write(mpl%info,'(a)') '-------------------------------------------------------------------'
110 write(mpl%info,'(a)') '--- Compute MPI distribution, halo C'
111 call flush(mpl%info)
112 call hdiag%samp%compute_mpi_c(mpl,nam,geom)
113 
114 if ((nam%local_diag.or.nam%displ_diag).and.(nam%diag_rhflt>0.0)) then
115  ! Compute MPI distribution, halo F
116  write(mpl%info,'(a)') '-------------------------------------------------------------------'
117  write(mpl%info,'(a)') '--- Compute MPI distribution, halo F'
118  call flush(mpl%info)
119  call hdiag%samp%compute_mpi_f(mpl,nam,geom)
120 end if
121 
122 ! Compute sample moments
123 write(mpl%info,'(a)') '-------------------------------------------------------------------'
124 write(mpl%info,'(a)') '--- Compute sample moments'
125 call flush(mpl%info)
126 
127 ! Compute ensemble 1 sample moments
128 write(mpl%info,'(a7,a)') '','Ensemble 1:'
129 call flush(mpl%info)
130 call hdiag%mom_1%compute(mpl,nam,geom,bpar,hdiag%samp,ens1)
131 
132 select case(trim(nam%method))
133 case ('hyb-rnd','dual-ens')
134  ! Compute randomized sample moments
135  write(mpl%info,'(a7,a)') '','Ensemble 2:'
136  call flush(mpl%info)
137  call hdiag%mom_2%compute(mpl,nam,geom,bpar,hdiag%samp,ens2)
138 end select
139 
140 ! Compute statistics
141 write(mpl%info,'(a)') '-------------------------------------------------------------------'
142 write(mpl%info,'(a)') '--- Compute statistics'
143 call flush(mpl%info)
144 
145 ! Compute ensemble 1 statistics
146 write(mpl%info,'(a7,a)') '','Ensemble 1:'
147 call flush(mpl%info)
148 call hdiag%avg_1%compute(mpl,nam,geom,bpar,hdiag%samp,hdiag%mom_1,nam%ne)
149 
150 select case(trim(nam%method))
151 case ('hyb-rnd','dual-ens')
152  ! Compute ensemble 2 statistics
153  write(mpl%info,'(a7,a)') '','Ensemble 2:'
154  call flush(mpl%info)
155  call hdiag%avg_2%compute(mpl,nam,geom,bpar,hdiag%samp,hdiag%mom_2,nam%ens2_ne)
156 case ('hyb-avg')
157  ! Copy ensemble 1 statistics
158  hdiag%avg_2 = hdiag%avg_1%copy(nam,geom,bpar)
159 end select
160 
161 select case (trim(nam%method))
162 case ('hyb-avg','hyb-rnd','dual-ens')
163  ! Compute hybrid statistics
164  write(mpl%info,'(a)') '-------------------------------------------------------------------'
165  write(mpl%info,'(a)') '--- Compute hybrid statistics'
166  call flush(mpl%info)
167  call hdiag%avg_2%compute_hyb(mpl,nam,geom,bpar,hdiag%samp,hdiag%mom_1,hdiag%mom_2,hdiag%avg_1)
168 end select
169 
170 if ((bpar%nbe>bpar%nb).and.bpar%diag_block(bpar%nbe)) then
171  ! Compute block-averaged statistics
172  write(mpl%info,'(a)') '-------------------------------------------------------------------'
173  write(mpl%info,'(a)') '--- Compute block-averaged statistics'
174  call flush(mpl%info)
175  hdiag%avg_wgt = hdiag%avg_1%copy_wgt(geom,bpar)
176  call hdiag%avg_1%compute_bwavg(mpl,nam,geom,bpar,hdiag%avg_wgt)
177  if ((trim(nam%method)=='hyb-rnd').or.(trim(nam%method)=='dual-ens')) &
178  & call hdiag%avg_2%compute_bwavg(mpl,nam,geom,bpar,hdiag%avg_wgt)
179 end if
180 
181 write(mpl%info,'(a)') '-------------------------------------------------------------------'
182 write(mpl%info,'(a)') '--- Compute covariance'
183 call flush(mpl%info)
184 
185 ! Compute ensemble 1 covariance
186 write(mpl%info,'(a7,a)') '','Ensemble 1:'
187 call flush(mpl%info)
188 call hdiag%cov_1%covariance(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_1,'cov')
189 
190 select case (trim(nam%method))
191 case ('hyb-avg','hyb-rnd','dual-ens')
192  ! Compute ensemble 2 covariance
193  write(mpl%info,'(a7,a)') '','Ensemble 2:'
194  call flush(mpl%info)
195  select case (trim(nam%method))
196  case ('hyb-avg','hyb-rnd')
197  call hdiag%cov_2%covariance(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_2,'cov_sta')
198  case ('dual-ens')
199  call hdiag%cov_2%covariance(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_2,'cov_lr')
200  end select
201 end select
202 
203 write(mpl%info,'(a)') '-------------------------------------------------------------------'
204 write(mpl%info,'(a)') '--- Compute correlation'
205 call flush(mpl%info)
206 
207 ! Compute ensemble 1 correlation
208 write(mpl%info,'(a7,a)') '','Ensemble 1:'
209 call flush(mpl%info)
210 call hdiag%cor_1%correlation(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_1,'cor')
211 
212 select case (trim(nam%method))
213 case ('hyb-avg','hyb-rnd','dual-ens')
214  ! Compute ensemble 2 correlation
215  write(mpl%info,'(a7,a)') '','Ensemble 2:'
216  call flush(mpl%info)
217  select case (trim(nam%method))
218  case ('hyb-avg','hyb-rnd')
219  call hdiag%cor_2%correlation(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_2,'cor_sta')
220  case ('dual-ens')
221  call hdiag%cor_2%correlation(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_2,'cor_lr')
222  end select
223 end select
224 
225 select case (trim(nam%method))
226 case ('loc_norm','loc','hyb-avg','hyb-rnd','dual-ens')
227  ! Compute localization
228  write(mpl%info,'(a)') '-------------------------------------------------------------------'
229  write(mpl%info,'(a)') '--- Compute localization'
230  write(mpl%info,'(a7,a)') '','Ensemble 1:'
231  call flush(mpl%info)
232  call hdiag%loc_1%localization(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_1,'loc')
233 end select
234 
235 select case (trim(nam%method))
236 case ('hyb-avg','hyb-rnd')
237  ! Compute static hybridization
238  write(mpl%info,'(a)') '-------------------------------------------------------------------'
239  write(mpl%info,'(a)') '--- Compute static hybridization'
240  write(mpl%info,'(a7,a)') '','Ensemble 1 and 2:'
241  call flush(mpl%info)
242  call hdiag%loc_2%hybridization(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_1,hdiag%avg_2,'loc_hyb')
243 end select
244 
245 if (trim(nam%method)=='dual-ens') then
246  ! Compute dual-ensemble hybridization diagnostic and fit
247  write(mpl%info,'(a)') '-------------------------------------------------------------------'
248  write(mpl%info,'(a)') '--- Compute dual-ensemble hybridization'
249  write(mpl%info,'(a7,a)') '','Ensembles 1 and 2:'
250  call flush(mpl%info)
251  call hdiag%loc_2%dualens(mpl,nam,geom,bpar,io,hdiag%samp,hdiag%avg_1,hdiag%avg_2,hdiag%loc_3,'loc_deh','loc_deh_lr')
252 end if
253 
254 ! Write data
255 write(mpl%info,'(a)') '-------------------------------------------------------------------'
256 write(mpl%info,'(a)') '--- Write data'
257 call flush(mpl%info)
258 
259 ! Displacement
260 if (nam%displ_diag) call hdiag%displ%write(mpl,nam,geom,hdiag%samp,trim(nam%prefix)//'_displ_diag.nc')
261 
262 ! Full variances
263 if (nam%var_full) then
264  filename = trim(nam%prefix)//'_var_full'
265  do ib=1,bpar%nb
266  if (bpar%diag_block(ib)) call io%fld_write(mpl,nam,geom,filename,trim(bpar%blockname(ib))//'_var', &
267  & sum(hdiag%mom_1%blk(ib)%m2full,dim=3)/real(hdiag%mom_1%blk(ib)%nsub,kind_real))
268  end do
269 end if
270 
271 end subroutine hdiag_run_hdiag
272 
273 end module type_hdiag
subroutine hdiag_run_hdiag(hdiag, mpl, rng, nam, geom, bpar, io, ens1, ens2)
Definition: type_hdiag.F90:56
integer, parameter, public kind_real