FV3 Bundle
fv3jedi_lm_moist_mod.F90
Go to the documentation of this file.
2 
6 
8 
9 ! Moist Schemes
10 use convection
11 use convection_ad
12 use convection_tl
13 use cloud
14 use cloud_ad
15 use cloud_tl
16 
17 ! Saturation table
18 use qsat_util
19 
22 
23 !> Top level for fv3jedi linearized model
24 
25 implicit none
26 private
27 public :: fv3jedi_lm_moist_type
28 
29 !> Local trajectory objects
31  real(8), allocatable, dimension(:,:,:) :: ut, vt, ptt, qvt
32  real(8), allocatable, dimension(:,:) :: ts, frland
33  integer, allocatable, dimension(:,:) :: kcbl, khu, khl
34  real(8), allocatable, dimension(:,:,:) :: ple, cnv_ple, pk
35  real(8), allocatable, dimension(:,:,:) :: cnv_dqldtt, cnv_mfdt, cnv_prc3t, cnv_updft
36  real(8), allocatable, dimension(:,:,:) :: ptt_c, qvt_c
37  real(8), allocatable, dimension(:,:,:) :: cnv_dqldtt_c, cnv_mfdt_c, cnv_prc3t_c, cnv_updft_c
38  integer, allocatable, dimension(:,:) :: seedras
39  real(8), allocatable, dimension(:,:) :: co_auto
40  integer, allocatable, dimension(:,:) :: doconvec
41  real(8), allocatable, dimension(:,:,:) :: wgt0, wgt1
42  real(8), allocatable, dimension(:,:,:) :: qilst, qllst, qicnt, qlcnt
43  real(8), allocatable, dimension(:,:,:) :: cflst, cfcnt
44  real(8), allocatable, dimension(:,:,:) :: ilsf, icnf, llsf, lcnf
45  logical :: set = .false.
46 endtype local_traj_moist
47 
48 !> Local perturbation objects
50  real(8), allocatable, dimension(:,:,:) :: up, vp, ptp, qvp
51  real(8), allocatable, dimension(:,:,:) :: cnv_dqldtp, cnv_mfdp, cnv_prc3p, cnv_updfp
52  real(8), allocatable, dimension(:,:,:) :: qilsp, qllsp, qicnp, qlcnp
53  real(8), allocatable, dimension(:,:,:) :: cflsp, cfcnp
54 endtype local_pert_moist
55 
56 !> Local constants objects
58  integer :: icmin
59  real(8), allocatable, dimension(:) :: estblx, sige
60  real(8) :: rasparams(25), cloudparams (57)
61  real(8) :: mapl8_cp, mapl8_alhl, mapl8_grav, mapl8_p00, mapl8_kappa
62  real(8) :: mapl8_rgas, mapl8_h2omw, mapl8_airmw, mapl8_vireps
63  real(8) :: mapl8_runiv, mapl8_alhf, mapl8_pi, mapl8_alhs
64  real(8) :: mapl8_tice, mapl8_rvap
65 endtype local_cnst_moist
66 
67 !> Moist class (self)
69  type(local_traj_moist), allocatable :: ltraj(:)
70  type(local_pert_moist) :: lpert
71  type(local_cnst_moist) :: lcnst
72  contains
73  procedure :: create
74  procedure :: init_nl
75  procedure :: init_tl
76  procedure :: init_ad
77  procedure :: step_nl
78  procedure :: step_tl
79  procedure :: step_ad
80  procedure :: delete
81 end type fv3jedi_lm_moist_type
82 
83 contains
84 
85 ! ------------------------------------------------------------------------------
86 
87 subroutine create(self,conf)
88 
89  implicit none
90 
91  class(fv3jedi_lm_moist_type), target, intent(inout) :: self
92  type(fv3jedi_lm_conf), intent(in) :: conf
93 
94  integer :: imsize, L, n
95  real(8), allocatable, dimension(:) :: pref
96  real(8), parameter :: PMIN_DET = 3000.0
97  integer, parameter :: DEGSUBS = 100
98  real(8), parameter :: TMINTBL = 150.0, tmaxtbl = 333.0
99  integer, parameter :: TABLESIZE = nint(tmaxtbl-tmintbl)*degsubs + 1
100 
101  self%lcnst%MAPL8_CP = dble(mapl_cp)
102  self%lcnst%MAPL8_ALHL = dble(mapl_alhl)
103  self%lcnst%MAPL8_GRAV = dble(mapl_grav)
104  self%lcnst%MAPL8_P00 = dble(mapl_p00)
105  self%lcnst%MAPL8_KAPPA = dble(mapl_kappa)
106  self%lcnst%MAPL8_RGAS = dble(mapl_rgas)
107  self%lcnst%MAPL8_H2OMW = dble(mapl_h2omw)
108  self%lcnst%MAPL8_AIRMW = dble(mapl_airmw)
109  self%lcnst%MAPL8_VIREPS = dble(mapl_vireps)
110  self%lcnst%MAPL8_RUNIV = dble(mapl_runiv)
111  self%lcnst%MAPL8_ALHF = dble(mapl_alhf)
112  self%lcnst%MAPL8_PI = dble(mapl_pi)
113  self%lcnst%MAPL8_ALHS = dble(mapl_alhs)
114  self%lcnst%MAPL8_TICE = dble(mapl_tice)
115  self%lcnst%MAPL8_RVAP = dble(mapl_rvap)
116 
117  imsize = conf%im*4
118 
119  !RAS Parameters
120  self%lcnst%RASPARAMS( 1) = 1.000
121  self%lcnst%RASPARAMS( 2) = 0.05
122  self%lcnst%RASPARAMS( 3) = 0.0 ! NOW IN CO_AUTO (CONTAINED WITHIN SUBROUTINE)
123  self%lcnst%RASPARAMS( 4) = 8.0e-4
124  self%lcnst%RASPARAMS( 5) = 1800.
125  self%lcnst%RASPARAMS( 6) = 43200.0
126  self%lcnst%RASPARAMS( 7) = -300. !RASNCL, CONTROLS FINDDTLS, USE OF RANDOM NUMBER
127  self%lcnst%RASPARAMS( 8) = 4.0
128  self%lcnst%RASPARAMS( 9) = 0.0
129  self%lcnst%RASPARAMS(10) = 200.
130  self%lcnst%RASPARAMS(11) = 7.5e-4
131  self%lcnst%RASPARAMS(12) = 1.0
132  self%lcnst%RASPARAMS(13) =-1.0
133  self%lcnst%RASPARAMS(14) = 1.3
134  self%lcnst%RASPARAMS(15) = 1.3
135  self%lcnst%RASPARAMS(16) = 263.
136  self%lcnst%RASPARAMS(17) = 0.5
137  self%lcnst%RASPARAMS(18) = 1.0
138  self%lcnst%RASPARAMS(19) = 0.0
139  self%lcnst%RASPARAMS(20) = 0.1
140  self%lcnst%RASPARAMS(21) = 0.8
141  self%lcnst%RASPARAMS(22) = 1.0
142  if( imsize .le. 200 ) self%lcnst%RASPARAMS(23) = 4000.0
143  if( imsize .gt. 200 .and. imsize.le.400 ) self%lcnst%RASPARAMS(23) = 2000.0
144  if( imsize .gt. 400 .and. imsize.le.800 ) self%lcnst%RASPARAMS(23) = 700.0
145  if( imsize .gt. 800 .and. imsize.le.1600 ) self%lcnst%RASPARAMS(23) = 450.0
146  if( imsize .gt. 1600 ) self%lcnst%RASPARAMS(23) = 450.0
147  self%lcnst%RASPARAMS(24) = 0.5
148  self%lcnst%RASPARAMS(25) = 0.65
149 
150  !SET SBAC PARAMETERS
151  self%lcnst%CLOUDPARAMS( 1) = 10.0
152  self%lcnst%CLOUDPARAMS( 2) = 4.0
153  self%lcnst%CLOUDPARAMS( 3) = 4.0
154  self%lcnst%CLOUDPARAMS( 4) = 1.0
155  self%lcnst%CLOUDPARAMS( 5) = 2.0e-3
156  self%lcnst%CLOUDPARAMS( 6) = 8.0e-4
157  self%lcnst%CLOUDPARAMS( 7) = 2.0
158  self%lcnst%CLOUDPARAMS( 8) = 1.0
159  self%lcnst%CLOUDPARAMS( 9) = -1.0
160  self%lcnst%CLOUDPARAMS(10) = 0.0
161  self%lcnst%CLOUDPARAMS(11) = 1.3
162  self%lcnst%CLOUDPARAMS(12) = 1.0e-9
163  self%lcnst%CLOUDPARAMS(13) = 3.3e-4
164  self%lcnst%CLOUDPARAMS(14) = 20.
165  self%lcnst%CLOUDPARAMS(15) = 4.8
166  self%lcnst%CLOUDPARAMS(16) = 4.8
167  self%lcnst%CLOUDPARAMS(17) = 230.
168  self%lcnst%CLOUDPARAMS(18) = 1.0
169  self%lcnst%CLOUDPARAMS(19) = 1.0
170  self%lcnst%CLOUDPARAMS(20) = 230.
171  self%lcnst%CLOUDPARAMS(21) = 14400.
172  self%lcnst%CLOUDPARAMS(22) = 50.
173  self%lcnst%CLOUDPARAMS(23) = 0.01
174  self%lcnst%CLOUDPARAMS(24) = 0.1
175  self%lcnst%CLOUDPARAMS(25) = 200.
176  self%lcnst%CLOUDPARAMS(26) = 0.
177  self%lcnst%CLOUDPARAMS(27) = 0.
178  self%lcnst%CLOUDPARAMS(28) = 0.5
179  self%lcnst%CLOUDPARAMS(29) = 0.5
180  self%lcnst%CLOUDPARAMS(30) = 2000.
181  self%lcnst%CLOUDPARAMS(31) = 0.8
182  self%lcnst%CLOUDPARAMS(32) = 0.5
183  self%lcnst%CLOUDPARAMS(33) = -40.0
184  self%lcnst%CLOUDPARAMS(34) = 1.0
185  self%lcnst%CLOUDPARAMS(35) = 4.0
186  self%lcnst%CLOUDPARAMS(36) = 0.0
187  self%lcnst%CLOUDPARAMS(37) = 0.0
188  self%lcnst%CLOUDPARAMS(38) = 0.0
189  self%lcnst%CLOUDPARAMS(39) = 1.0e-3
190  self%lcnst%CLOUDPARAMS(40) = 8.0e-4
191  self%lcnst%CLOUDPARAMS(41) = 1.0
192  if( imsize .le. 200 ) self%lcnst%CLOUDPARAMS(42) = 0.80
193  if( imsize .gt. 200 .and. imsize.le.400 ) self%lcnst%CLOUDPARAMS(42) = 0.90
194  if( imsize .gt. 400 .and. imsize.le.800 ) self%lcnst%CLOUDPARAMS(42) = 0.93
195  if( imsize .gt. 800 .and. imsize.le.1600 ) self%lcnst%CLOUDPARAMS(42) = 0.95
196  if( imsize .gt. 1600 ) self%lcnst%CLOUDPARAMS(42) = 0.97
197  self%lcnst%CLOUDPARAMS(43) = 1.0
198  self%lcnst%CLOUDPARAMS(44) = 0.0
199  self%lcnst%CLOUDPARAMS(45) = 750.0
200  self%lcnst%CLOUDPARAMS(46) = self%lcnst%CLOUDPARAMS(42)+0.01
201  self%lcnst%CLOUDPARAMS(47) = 1.0
202  self%lcnst%CLOUDPARAMS(48) = 1.0
203  self%lcnst%CLOUDPARAMS(49) = 0.0
204  self%lcnst%CLOUDPARAMS(50) = 0.0
205  self%lcnst%CLOUDPARAMS(51) = 10.e-6
206  self%lcnst%CLOUDPARAMS(52) = 20.e-6
207  self%lcnst%CLOUDPARAMS(53) = 21.e-6
208  self%lcnst%CLOUDPARAMS(54) = 40.e-6
209  self%lcnst%CLOUDPARAMS(55) = 30.e-6
210  self%lcnst%CLOUDPARAMS(56) = 1.0
211  self%lcnst%CLOUDPARAMS(57) = 1.0
212 
213  if (conf%saveltraj) then
214  allocate(self%ltraj(conf%nt))
215  do n = 1,conf%nt
216  call allocate_ltraj(conf%im,conf%jm,conf%lm,self%ltraj(n))
217  enddo
218  else
219  allocate(self%ltraj(1))
220  call allocate_ltraj(conf%im,conf%jm,conf%lm,self%ltraj(1))
221  endif
222 
223  call allocate_lpert(conf%im,conf%jm,conf%lm,self%lpert)
224 
225  !Allocate self
226  allocate(self%lcnst%ESTBLX(tablesize))
227  allocate(self%lcnst%SIGE(0:conf%LM))
228 
229  !ESTBLX
230  call esinit(self%lcnst%ESTBLX)
231 
232  allocate(pref(0:conf%lm))
233  do l = 0,conf%lm
234  pref(l) = conf%ak(l+1) + conf%bk(l+1)*self%lcnst%MAPL8_P00
235  enddo
236 
237  self%lcnst%ICMIN = max(1,count(pref < pmin_det))
238  self%lcnst%SIGE = pref/pref(conf%LM)
239 
240  deallocate(pref)
241 
242 endsubroutine create
243 
244 ! ------------------------------------------------------------------------------
245 
246 subroutine init_nl(self,pert,traj)
248  implicit none
249 
250  class(fv3jedi_lm_moist_type), intent(inout) :: self
251  type(fv3jedi_lm_pert), intent(inout) :: pert
252  type(fv3jedi_lm_traj), intent(in) :: traj
253 
254 endsubroutine init_nl
255 
256 ! ------------------------------------------------------------------------------
257 
258 subroutine init_tl(self,pert,traj)
260  implicit none
261 
262  class(fv3jedi_lm_moist_type), intent(inout) :: self
263  type(fv3jedi_lm_pert), intent(inout) :: pert
264  type(fv3jedi_lm_traj), intent(in) :: traj
265 
266 endsubroutine init_tl
267 
268 ! ------------------------------------------------------------------------------
269 
270 subroutine init_ad(self,pert,traj)
272  implicit none
273 
274  class(fv3jedi_lm_moist_type), intent(inout) :: self
275  type(fv3jedi_lm_pert), intent(inout) :: pert
276  type(fv3jedi_lm_traj), intent(in) :: traj
277 
278 endsubroutine init_ad
279 
280 ! ------------------------------------------------------------------------------
281 
282 subroutine step_nl(self,conf,traj)
284  implicit none
285 
286  class(fv3jedi_lm_moist_type), target, intent(inout) :: self
287  type(fv3jedi_lm_traj), target, intent(inout) :: traj
288  type(fv3jedi_lm_conf), intent(in) :: conf
289 
290  integer :: i,j,im,jm,lm,isc,iec,jsc,jec
291  type(local_traj_moist), pointer :: ltraj
292  type(local_pert_moist), pointer :: lpert
293  type(local_cnst_moist), pointer :: lcnst
294 
295  im = conf%im
296  jm = conf%jm
297  lm = conf%lm
298  isc = conf%isc
299  iec = conf%iec
300  jsc = conf%jsc
301  jec = conf%jec
302 
303  !Convenience pointers
304  lpert => self%lpert
305  lcnst => self%lcnst
306  if (conf%saveltraj) then
307  ltraj => self%ltraj(conf%n)
308  else
309  ltraj => self%ltraj(1)
310  endif
311 
312  !Set up the local trajectory
313  if (.not. ltraj%set) call set_ltraj(conf,lcnst,traj,ltraj)
314 
315  !Local pert (not really needed)
316  lpert%up = 0.0_8
317  lpert%vp = 0.0_8
318  lpert%ptp = 0.0_8
319  lpert%qvp = 0.0_8
320  lpert%cflsp = 0.0_8
321  lpert%cfcnp = 0.0_8
322  lpert%qilsp = 0.0_8
323  lpert%qicnp = 0.0_8
324  lpert%qllsp = 0.0_8
325  lpert%qlcnp = 0.0_8
326 
327  ltraj%cnv_dqldtt = 0.0_8
328  ltraj%cnv_mfdt = 0.0_8
329  ltraj%cnv_prc3t = 0.0_8
330  ltraj%cnv_updft = 0.0_8
331  lpert%cnv_dqldtp = 0.0_8
332  lpert%cnv_mfdp = 0.0_8
333  lpert%cnv_prc3p = 0.0_8
334  lpert%cnv_updfp = 0.0_8
335 
336  !Call the tangent linear convection scheme
337  do i = 1,conf%im
338  do j = 1,conf%jm
339 
340  if ( ltraj%doconvec(i,j) == 1) then
341  call rase_d( 1, 1, conf%lm, lcnst%icmin, conf%dt, &
342  lcnst%mapl8_cp, lcnst%mapl8_alhl, &
343  lcnst%mapl8_grav, lcnst%mapl8_rgas, &
344  lcnst%mapl8_h2omw, lcnst%mapl8_airmw, &
345  lcnst%mapl8_vireps, &
346  ltraj%seedras(i,j), lcnst%sige, &
347  ltraj%kcbl(i,j), &
348  ltraj%wgt0(i,j,:), ltraj%wgt1(i,j,:), &
349  ltraj%frland(i,j), ltraj%ts(i,j), &
350  ltraj%ptt(i,j,:), lpert%ptp(i,j,:), &
351  ltraj%qvt(i,j,:), lpert%qvp(i,j,:), &
352  ltraj%ut(i,j,:), lpert%up(i,j,:), &
353  ltraj%vt(i,j,:), lpert%vp(i,j,:), &
354  ltraj%co_auto(i,j), ltraj%cnv_ple(i,j,:), &
355  ltraj%cnv_dqldtt(i,j,:), lpert%cnv_dqldtp(i,j,:), &
356  ltraj%cnv_mfdt(i,j,:), lpert%cnv_mfdp(i,j,:), &
357  ltraj%cnv_prc3t(i,j,:), lpert%cnv_prc3p(i,j,:), &
358  ltraj%cnv_updft(i,j,:), lpert%cnv_updfp(i,j,:), &
359  lcnst%rasparams, lcnst%estblx )
360  endif
361 
362  enddo
363  enddo
364 
365  !Call the tangent linear cloud scheme.
366  call cloud_driver_d ( conf%dt, conf%im, conf%jm, conf%lm, &
367  ltraj%ptt_c, lpert%ptp, &
368  ltraj%qvt_c, lpert%qvp, &
369  ltraj%ple, &
370  ltraj%cnv_dqldtt_c, lpert%cnv_dqldtp, ltraj%cnv_mfdt_c, lpert%cnv_mfdp, &
371  ltraj%cnv_prc3t_c, lpert%cnv_prc3p, ltraj%cnv_updft_c, lpert%cnv_updfp, &
372  ltraj%qilst, lpert%qilsp, ltraj%qllst, lpert%qllsp, &
373  ltraj%qicnt, lpert%qicnp, ltraj%qlcnt, lpert%qlcnp, &
374  ltraj%cflst, lpert%cflsp, ltraj%cfcnt, lpert%cfcnp, &
375  ltraj%frland, lcnst%cloudparams, lcnst%estblx, ltraj%khu, ltraj%khl, &
376  lcnst%mapl8_runiv, lcnst%mapl8_kappa, lcnst%mapl8_airmw, lcnst%mapl8_h2omw, &
377  lcnst%mapl8_grav, lcnst%mapl8_alhl, lcnst%mapl8_alhf, lcnst%mapl8_pi, &
378  lcnst%mapl8_rgas, lcnst%mapl8_cp, lcnst%mapl8_vireps, lcnst%mapl8_alhs, &
379  lcnst%mapl8_tice, lcnst%mapl8_rvap, lcnst%mapl8_p00, conf%do_phy_mst )
380 
381  !Back to traj
382  traj%u(isc:iec,jsc:jec,:) = real(ltraj%ut(1:im,1:jm,:),kind_real)
383  traj%v(isc:iec,jsc:jec,:) = real(ltraj%vt(1:im,1:jm,:),kind_real)
384  traj%t(isc:iec,jsc:jec,:) = real(ltraj%pk(1:im,1:jm,:) * ltraj%PTT(1:im,1:jm,:) / p00**kappa,kind_real)
385  traj%qv(isc:iec,jsc:jec,:) = real(ltraj%qvt(1:im,1:jm,:),kind_real)
386  traj%qi(isc:iec,jsc:jec,:) = real(ltraj%qilst(1:im,1:jm,:) + ltraj%qicnt(1:im,1:jm,:),kind_real)
387  traj%ql(isc:iec,jsc:jec,:) = real(ltraj%qllst(1:im,1:jm,:) + ltraj%qlcnt(1:im,1:jm,:),kind_real)
388  traj%cfcn(isc:iec,jsc:jec,:) = real(ltraj%cfcnt(1:im,1:jm,:),kind_real)
389 
390 endsubroutine step_nl
391 
392 ! ------------------------------------------------------------------------------
393 
394 subroutine step_tl(self,conf,traj,pert)
396  implicit none
397 
398  class(fv3jedi_lm_moist_type), target, intent(inout) :: self
399  type(fv3jedi_lm_conf), intent(in) :: conf
400  type(fv3jedi_lm_traj), intent(in) :: traj
401  type(fv3jedi_lm_pert), intent(inout) :: pert
402 
403  integer :: i,j,im,jm,lm,isc,iec,jsc,jec
404  type(local_traj_moist), pointer :: ltraj
405  type(local_pert_moist), pointer :: lpert
406  type(local_cnst_moist), pointer :: lcnst
407 
408  im = conf%im
409  jm = conf%jm
410  lm = conf%lm
411  isc = conf%isc
412  iec = conf%iec
413  jsc = conf%jsc
414  jec = conf%jec
415 
416  !Convenience pointers
417  lpert => self%lpert
418  lcnst => self%lcnst
419  if (conf%saveltraj) then
420  ltraj => self%ltraj(conf%n)
421  else
422  ltraj => self%ltraj(1)
423  endif
424 
425  !Set up the local trajectory
426  if (.not. ltraj%set) call set_ltraj(conf,lcnst,traj,ltraj)
427 
428  !Local pert
429  lpert%up(1:im,1:jm,:) = dble(pert%u (isc:iec,jsc:jec,:))
430  lpert%vp(1:im,1:jm,:) = dble(pert%v (isc:iec,jsc:jec,:))
431  lpert%ptp(1:im,1:jm,:) = dble(pert%t (isc:iec,jsc:jec,:)) * p00**kappa / ltraj%pk(1:im,1:jm,:)
432  lpert%qvp(1:im,1:jm,:) = dble(pert%qv(isc:iec,jsc:jec,:))
433  lpert%qilsp(1:im,1:jm,:) = dble(pert%qi(isc:iec,jsc:jec,:)) * ltraj%ilsf(1:im,1:jm,:)
434  lpert%qicnp(1:im,1:jm,:) = dble(pert%qi(isc:iec,jsc:jec,:)) * ltraj%icnf(1:im,1:jm,:)
435  lpert%qllsp(1:im,1:jm,:) = dble(pert%ql(isc:iec,jsc:jec,:)) * ltraj%llsf(1:im,1:jm,:)
436  lpert%qlcnp(1:im,1:jm,:) = dble(pert%ql(isc:iec,jsc:jec,:)) * ltraj%lcnf(1:im,1:jm,:)
437  lpert%cflsp(1:im,1:jm,:) = 0.0_8
438  lpert%cfcnp(1:im,1:jm,:) = dble(pert%cfcn(isc:iec,jsc:jec,:))
439 
440  ltraj%cnv_dqldtt = 0.0_8
441  ltraj%cnv_mfdt = 0.0_8
442  ltraj%cnv_prc3t = 0.0_8
443  ltraj%cnv_updft = 0.0_8
444  lpert%cnv_dqldtp = 0.0_8
445  lpert%cnv_mfdp = 0.0_8
446  lpert%cnv_prc3p = 0.0_8
447  lpert%cnv_updfp = 0.0_8
448 
449  !Call the tangent linear convection scheme
450  do i = 1,conf%im
451  do j = 1,conf%jm
452 
453  if ( ltraj%doconvec(i,j) == 1) then
454  call rase_d( 1, 1, conf%lm, lcnst%icmin, conf%dt, &
455  lcnst%mapl8_cp, lcnst%mapl8_alhl, &
456  lcnst%mapl8_grav, lcnst%mapl8_rgas, &
457  lcnst%mapl8_h2omw, lcnst%mapl8_airmw, &
458  lcnst%mapl8_vireps, &
459  ltraj%seedras(i,j), lcnst%sige, &
460  ltraj%kcbl(i,j), &
461  ltraj%wgt0(i,j,:), ltraj%wgt1(i,j,:), &
462  ltraj%frland(i,j), ltraj%ts(i,j), &
463  ltraj%ptt(i,j,:), lpert%ptp(i,j,:), &
464  ltraj%qvt(i,j,:), lpert%qvp(i,j,:), &
465  ltraj%ut(i,j,:), lpert%up(i,j,:), &
466  ltraj%vt(i,j,:), lpert%vp(i,j,:), &
467  ltraj%co_auto(i,j), ltraj%cnv_ple(i,j,:), &
468  ltraj%cnv_dqldtt(i,j,:), lpert%cnv_dqldtp(i,j,:), &
469  ltraj%cnv_mfdt(i,j,:), lpert%cnv_mfdp(i,j,:), &
470  ltraj%cnv_prc3t(i,j,:), lpert%cnv_prc3p(i,j,:), &
471  ltraj%cnv_updft(i,j,:), lpert%cnv_updfp(i,j,:), &
472  lcnst%rasparams, lcnst%estblx )
473  endif
474 
475  enddo
476  enddo
477 
478  !Call the tangent linear cloud scheme.
479  call cloud_driver_d ( conf%dt, conf%im, conf%jm, conf%lm, &
480  ltraj%ptt_c, lpert%ptp, &
481  ltraj%qvt_c, lpert%qvp, &
482  ltraj%ple, &
483  ltraj%cnv_dqldtt_c, lpert%cnv_dqldtp, ltraj%cnv_mfdt_c, lpert%cnv_mfdp, &
484  ltraj%cnv_prc3t_c, lpert%cnv_prc3p, ltraj%cnv_updft_c, lpert%cnv_updfp, &
485  ltraj%qilst, lpert%qilsp, ltraj%qllst, lpert%qllsp, &
486  ltraj%qicnt, lpert%qicnp, ltraj%qlcnt, lpert%qlcnp, &
487  ltraj%cflst, lpert%cflsp, ltraj%cfcnt, lpert%cfcnp, &
488  ltraj%frland, lcnst%cloudparams, lcnst%estblx, ltraj%khu, ltraj%khl, &
489  lcnst%mapl8_runiv, lcnst%mapl8_kappa, lcnst%mapl8_airmw, lcnst%mapl8_h2omw, &
490  lcnst%mapl8_grav, lcnst%mapl8_alhl, lcnst%mapl8_alhf, lcnst%mapl8_pi, &
491  lcnst%mapl8_rgas, lcnst%mapl8_cp, lcnst%mapl8_vireps, lcnst%mapl8_alhs, &
492  lcnst%mapl8_tice, lcnst%mapl8_rvap, lcnst%mapl8_p00, conf%do_phy_mst )
493 
494  !Back to pert
495  pert%u(isc:iec,jsc:jec,:) = real(lpert%up (1:im,1:jm,:),kind_real)
496  pert%v(isc:iec,jsc:jec,:) = real(lpert%vp (1:im,1:jm,:),kind_real)
497  pert%t(isc:iec,jsc:jec,:) = real(lpert%ptp(1:im,1:jm,:) * ltraj%pk(1:im,1:jm,:) / p00**kappa,kind_real)
498  pert%qv(isc:iec,jsc:jec,:) = real(lpert%qvp(1:im,1:jm,:),kind_real)
499  pert%qi(isc:iec,jsc:jec,:) = real(lpert%qilsp(1:im,1:jm,:) + lpert%qicnp(1:im,1:jm,:),kind_real)
500  pert%ql(isc:iec,jsc:jec,:) = real(lpert%qllsp(1:im,1:jm,:) + lpert%qlcnp(1:im,1:jm,:),kind_real)
501  pert%cfcn(isc:iec,jsc:jec,:) = real(lpert%cfcnp(1:im,1:jm,:),kind_real)
502 
503 endsubroutine step_tl
504 
505 ! ------------------------------------------------------------------------------
506 
507 subroutine step_ad(self,conf,traj,pert)
509  implicit none
510 
511  class(fv3jedi_lm_moist_type), target, intent(inout) :: self
512  type(fv3jedi_lm_conf), intent(in) :: conf
513  type(fv3jedi_lm_traj), intent(in) :: traj
514  type(fv3jedi_lm_pert), intent(inout) :: pert
515 
516  integer :: i,j,im,jm,lm,isc,iec,jsc,jec
517  type(local_traj_moist), pointer :: ltraj
518  type(local_pert_moist), pointer :: lpert
519  type(local_cnst_moist), pointer :: lcnst
520 
521  im = conf%im
522  jm = conf%jm
523  lm = conf%lm
524  isc = conf%isc
525  iec = conf%iec
526  jsc = conf%jsc
527  jec = conf%jec
528 
529  !Convenience pointers
530  lpert => self%lpert
531  lcnst => self%lcnst
532  if (conf%saveltraj) then
533  ltraj => self%ltraj(conf%n)
534  else
535  ltraj => self%ltraj(1)
536  endif
537 
538  !Set up the local trajectory
539  if (.not. ltraj%set) call set_ltraj(conf,lcnst,traj,ltraj)
540 
541  !Local pert
542  lpert%up(1:im,1:jm,:) = dble(pert%u (isc:iec,jsc:jec,:))
543  lpert%vp(1:im,1:jm,:) = dble(pert%v (isc:iec,jsc:jec,:))
544  lpert%ptp(1:im,1:jm,:) = dble(pert%t (isc:iec,jsc:jec,:)) * ltraj%pk(1:im,1:jm,:) / p00**kappa
545  lpert%qvp(1:im,1:jm,:) = dble(pert%qv(isc:iec,jsc:jec,:))
546  lpert%qilsp(1:im,1:jm,:) = dble(pert%qi(isc:iec,jsc:jec,:))
547  lpert%qicnp(1:im,1:jm,:) = dble(pert%qi(isc:iec,jsc:jec,:))
548  lpert%qllsp(1:im,1:jm,:) = dble(pert%ql(isc:iec,jsc:jec,:))
549  lpert%qlcnp(1:im,1:jm,:) = dble(pert%ql(isc:iec,jsc:jec,:))
550  lpert%cflsp(1:im,1:jm,:) = 0.0_8
551  lpert%cfcnp(1:im,1:jm,:) = dble(pert%cfcn(isc:iec,jsc:jec,:))
552 
553  ltraj%cnv_dqldtt = 0.0_8
554  ltraj%cnv_mfdt = 0.0_8
555  ltraj%cnv_prc3t = 0.0_8
556  ltraj%cnv_updft = 0.0_8
557  lpert%cnv_dqldtp = 0.0_8
558  lpert%cnv_mfdp = 0.0_8
559  lpert%cnv_prc3p = 0.0_8
560  lpert%cnv_updfp = 0.0_8
561 
562  !Call the tangent linear cloud scheme.
563  call cloud_driver_b ( conf%dt, conf%im, conf%jm, conf%lm, &
564  ltraj%ptt_c, lpert%ptp, &
565  ltraj%qvt_c, lpert%qvp, &
566  ltraj%ple, &
567  ltraj%cnv_dqldtt_c, lpert%cnv_dqldtp, ltraj%cnv_mfdt_c, lpert%cnv_mfdp, &
568  ltraj%cnv_prc3t_c, lpert%cnv_prc3p, ltraj%cnv_updft_c, lpert%cnv_updfp, &
569  ltraj%qilst, lpert%qilsp, ltraj%qllst, lpert%qllsp, &
570  ltraj%qicnt, lpert%qicnp, ltraj%qlcnt, lpert%qlcnp, &
571  ltraj%cflst, lpert%cflsp, ltraj%cfcnt, lpert%cfcnp, &
572  ltraj%frland, lcnst%cloudparams, lcnst%estblx, ltraj%khu, ltraj%khl, &
573  lcnst%mapl8_runiv, lcnst%mapl8_kappa, lcnst%mapl8_airmw, lcnst%mapl8_h2omw, &
574  lcnst%mapl8_grav, lcnst%mapl8_alhl, lcnst%mapl8_alhf, lcnst%mapl8_pi, &
575  lcnst%mapl8_rgas, lcnst%mapl8_cp, lcnst%mapl8_vireps, lcnst%mapl8_alhs, &
576  lcnst%mapl8_tice, lcnst%mapl8_rvap, lcnst%mapl8_p00, conf%do_phy_mst )
577 
578  !Call the tangent linear convection scheme
579  do i = 1,conf%im
580  do j = 1,conf%jm
581 
582  if ( ltraj%doconvec(i,j) == 1) then
583  call rase_b( 1, 1, conf%lm, lcnst%icmin, conf%dt, &
584  lcnst%mapl8_cp, lcnst%mapl8_alhl, &
585  lcnst%mapl8_grav, lcnst%mapl8_rgas, &
586  lcnst%mapl8_h2omw, lcnst%mapl8_airmw, &
587  lcnst%mapl8_vireps, &
588  ltraj%seedras(i,j), lcnst%sige, &
589  ltraj%kcbl(i,j), &
590  ltraj%wgt0(i,j,:), ltraj%wgt1(i,j,:), &
591  ltraj%frland(i,j), ltraj%ts(i,j), &
592  ltraj%ptt(i,j,:), lpert%ptp(i,j,:), &
593  ltraj%qvt(i,j,:), lpert%qvp(i,j,:), &
594  ltraj%ut(i,j,:), lpert%up(i,j,:), &
595  ltraj%vt(i,j,:), lpert%vp(i,j,:), &
596  ltraj%co_auto(i,j), ltraj%cnv_ple(i,j,:), &
597  ltraj%cnv_dqldtt(i,j,:), lpert%cnv_dqldtp(i,j,:), &
598  ltraj%cnv_mfdt(i,j,:), lpert%cnv_mfdp(i,j,:), &
599  ltraj%cnv_prc3t(i,j,:), lpert%cnv_prc3p(i,j,:), &
600  ltraj%cnv_updft(i,j,:), lpert%cnv_updfp(i,j,:), &
601  lcnst%rasparams, lcnst%estblx )
602  endif
603 
604  enddo
605  enddo
606 
607  !Back to pert
608  pert%u (isc:iec,jsc:jec,:) = real(lpert%up (1:im,1:jm,:),kind_real)
609  pert%v (isc:iec,jsc:jec,:) = real(lpert%vp (1:im,1:jm,:),kind_real)
610  pert%t (isc:iec,jsc:jec,:) = real(lpert%ptp(1:im,1:jm,:) * p00**kappa,kind_real) / ltraj%pk(1:im,1:jm,:)
611  pert%qv (isc:iec,jsc:jec,:) = real(lpert%qvp(1:im,1:jm,:),kind_real)
612  pert%qi (isc:iec,jsc:jec,:) = real(lpert%qilsp(1:im,1:jm,:)*ltraj%ilsf(1:im,1:jm,:) + & lpert%qicnp(1:im,1:jm,:)*ltraj%icnf(1:im,1:jm,:),kind_real)
613  pert%ql (isc:iec,jsc:jec,:) = real(lpert%qllsp(1:im,1:jm,:)*ltraj%llsf(1:im,1:jm,:) + & lpert%qlcnp(1:im,1:jm,:)*ltraj%lcnf(1:im,1:jm,:),kind_real)
614  pert%cfcn(isc:iec,jsc:jec,:) = real(lpert%cfcnp(1:im,1:jm,:),kind_real)
615 
616 endsubroutine step_ad
617 
618 ! ------------------------------------------------------------------------------
619 
620 subroutine delete(self,conf)
621 
622  implicit none
623  class(fv3jedi_lm_moist_type), intent(inout) :: self
624  type(fv3jedi_lm_conf), intent(in) :: conf
625 
626  integer :: n
627 
628  deallocate(self%lcnst%ESTBLX)
629  deallocate(self%lcnst%SIGE)
630 
631  call deallocate_lpert(self%lpert)
632 
633  if (conf%saveltraj) then
634  do n = 1,conf%nt
635  call deallocate_ltraj(self%ltraj(n))
636  enddo
637  else
638  call deallocate_ltraj(self%ltraj(1))
639  endif
640 
641  deallocate(self%ltraj)
642 
643 endsubroutine delete
644 
645 ! ------------------------------------------------------------------------------
646 
647 subroutine set_ltraj(conf,lcnst,traj,ltraj)
648 
649  type(fv3jedi_lm_conf), intent(in) :: conf
650  type(local_cnst_moist), intent(in) :: lcnst
651  type(fv3jedi_lm_traj), target, intent(in) :: traj
652  type(local_traj_moist), intent(inout) :: ltraj
653 
654  real(8), allocatable, dimension(:,:,:) :: PLO, PK
655  real(8), allocatable, dimension(:,:,:) :: TEMP
656  real(8), allocatable, dimension(:,:,:) :: PTT_F, QVT_F
657  real(8), allocatable, dimension(:,:,:) :: PTT_L, QVT_L
658  real(8), allocatable, dimension(:,:,:) :: HEAT
659  integer, allocatable, dimension(:,:) :: CTOP
660  real(8), allocatable, dimension(:,:) :: sumHEAT
661  real(8), allocatable, dimension(:,:) :: JACOBIAN
662  real(8), allocatable, dimension(:) :: PT_pert, QV_pert
663  real(8), allocatable, dimension(:) :: PT_pert_in, QV_pert_in
664  real(8), allocatable, dimension(:) :: H_pert, M_pert
665  real(8), allocatable, dimension(:,:,:) :: fQi
666 
667  integer :: i,j,l,im,jm,lm,isc,iec,jsc,jec
668  real(8), parameter :: PMIN_DET = 3000.0, autoc_cn_ocn = 2.5e-3, autoc_cn_land = autoc_cn_ocn
669  integer :: maxcondep
670 
671  im = conf%im
672  jm = conf%jm
673  lm = conf%lm
674  isc = conf%isc
675  iec = conf%iec
676  jsc = conf%jsc
677  jec = conf%jec
678 
679  allocate(plo(im,jm,lm) )
680  allocate(pk(im,jm,lm) )
681  allocate(temp(im,jm,lm) )
682  allocate(ptt_f(im,jm,lm) )
683  allocate(qvt_f(im,jm,lm) )
684  allocate(ptt_l(im,jm,lm) )
685  allocate(qvt_l(im,jm,lm) )
686  allocate(heat(im,jm,lm) )
687  allocate(ctop(im,jm) )
688  allocate(sumheat(im,jm) )
689  allocate(jacobian(2*lm,2))
690  allocate(h_pert(lm) )
691  allocate(m_pert(lm) )
692  allocate(pt_pert(lm) )
693  allocate(qv_pert(lm) )
694  allocate(pt_pert_in(lm) )
695  allocate(qv_pert_in(lm) )
696  allocate(fqi(im,jm,lm) )
697 
698  !!Still on the D-Grid!!
699  ltraj%ut(1:im,1:jm,:) = dble(traj%u(isc:iec,jsc:jec,:))
700  ltraj%vt(1:im,1:jm,:) = dble(traj%v(isc:iec,jsc:jec,:))
701 
702  !Pressure hPa, half levels and p^kappa
703  call compute_pressures(im,jm,lm,conf%ptop,traj%delp(isc:iec,jsc:jec,:),ltraj%ple,plo,ltraj%pk)
704 
705  !Potential temperature from temperature (use proper pk form to get pt)
706  ltraj%PTT(1:im,1:jm,:) = p00**kappa * dble(traj%t(isc:iec,jsc:jec,:)) / ltraj%pk(1:im,1:jm,:)
707 
708  !Pressures in form used be GEOS moist physics
709  ltraj%cnv_ple = 0.01_8*ltraj%ple
710  plo = 0.5_8*(ltraj%CNV_PLE(:,:,0:lm-1) + ltraj%CNV_PLE(:,:,1:lm ) )
711  pk = (plo/1000.0_8)**(lcnst%MAPL8_RGAS/lcnst%MAPL8_CP) !Formulation in GEOS moist
712  temp = ltraj%PTT*pk
713 
714  !Some moist vars
715  ltraj%qvt(1:im,1:jm,:) = dble(traj%qv(isc:iec,jsc:jec,:))
716  ltraj%cflst = 0.0_8
717 
718  ltraj%cfcnt(1:im,1:jm,:) = dble(traj%cfcn(isc:iec,jsc:jec,:))
719 
720  ltraj%ts (1:im,1:jm) = dble(traj%ts (isc:iec,jsc:jec))
721  ltraj%frland(1:im,1:jm) = dble(traj%frland(isc:iec,jsc:jec))
722  ltraj%kcbl (1:im,1:jm) = nint(traj%kcbl (isc:iec,jsc:jec))
723  ltraj%khl (1:im,1:jm) = nint(traj%khl (isc:iec,jsc:jec))
724  ltraj%khu (1:im,1:jm) = nint(traj%khu (isc:iec,jsc:jec))
725 
726  ltraj%PTT_C = ltraj%PTT
727  ltraj%QVT_C = ltraj%QVT
728 
729  ltraj%CNV_DQLDTT_C = 0.0_8
730  ltraj%CNV_MFDT_C = 0.0_8
731  ltraj%CNV_PRC3T_C = 0.0_8
732  ltraj%CNV_UPDFT_C = 0.0_8
733 
734  ptt_f = ltraj%PTT
735  qvt_f = ltraj%QVT
736 
737  ptt_l = ltraj%PTT
738  qvt_l = ltraj%QVT
739 
740  !Not linearised as could produce unpredictable behaviour (and very sensitive).
741  ltraj%SEEDRAS(:,:) = 1000000 * ( 100*temp(:,:,lm) - int( 100*temp(:,:,lm) ) )
742 
743  !Strapping levels
744  DO i = 1,im
745  DO j = 1,jm
746  ltraj%WGT0(i,j,:) = 0.0_8
747  ltraj%WGT0(i,j,ltraj%KCBL(i,j):lm) = 1.0_8
748  ltraj%WGT1(i,j,:) = 0.0_8
749  ltraj%WGT1(i,j,ltraj%KCBL(i,j):lm) = 1.0_8
750  ENDDO
751  ENDDO
752 
753  where (ltraj%FRLAND<0.1_8)
754  ltraj%CO_AUTO = autoc_cn_ocn ! ocean value
755  elsewhere
756  ltraj%CO_AUTO = autoc_cn_land ! land value
757  end where
758 
759  !Call nonlinear convection scheme. We need to do this because the
760  !cloud adjoint scheme needs the outputs from the convection.
761  !We will also only call the convective linearizations for profiles
762  !where convection is occuring for efficiency.
763  CALL rase0(im*jm, im*jm, lm, lcnst%ICMIN, conf%DT, &
764  lcnst%MAPL8_CP, lcnst%MAPL8_ALHL, lcnst%MAPL8_GRAV, lcnst%MAPL8_RGAS, &
765  lcnst%MAPL8_H2OMW, lcnst%MAPL8_AIRMW, lcnst%MAPL8_VIREPS, &
766  ltraj%SEEDRAS, lcnst%SIGE, &
767  ltraj%KCBL, ltraj%WGT0, ltraj%WGT1, ltraj%FRLAND, ltraj%TS, &
768  ltraj%PTT_C, ltraj%QVT_C, &
769  ltraj%CO_AUTO, ltraj%CNV_PLE, &
770  ltraj%CNV_DQLDTT_C, ltraj%CNV_MFDT_C, &
771  ltraj%CNV_PRC3T_C, ltraj%CNV_UPDFT_C, &
772  lcnst%RASPARAMS, lcnst%ESTBLX )
773 
774  ! Do the filtering to determine whether linear convection should be called
775  ! ------------------------------------------------------------------------
776  !Figure out whether or not each convective profile should be linearized.
777  !Conditions - Convection is happening (heating rate is nonzero)
778  ! - Convection is deep enough (>= 10 levels)
779  ! - The heating rate profile is not just a spike at one level
780  ! - Gradients are not too steep (Jacobian filtering).
781  ltraj%DOCONVEC = 0
782  heat = 0.0
783  ctop = lm
784  sumheat = 0.0
785 
786  !Be more lenient on profiles let in for 4DVAR, less likely to encounter problems
787  !at shorter lead times and gives more realistic low level cloud perturbations.
788  if (conf%do_phy_mst == 1) then
789  maxcondep = 1
790  elseif (conf%do_phy_mst == 2) then
791  maxcondep = 10
792  endif
793 
794  DO i = 1,im
795  DO j = 1,jm
796 
797  !Compute the heating rate.
798  heat(i,j,:) = (ltraj%PTT_C(i,j,:) - ltraj%PTT(i,j,:))/conf%DT
799 
800  !Starting at the top scan downwards to look for nonzero heating rate,
801  !record index of highest level convection reaches (ignoring v.small heating rate)
802  DO l = 1,lm
803  IF ( abs(heat(i,j,l)) .gt. 0.01*maxval(abs(heat(i,j,:)),1) ) THEN
804  ctop(i,j) = l
805  exit
806  ENDIF
807  ENDDO
808 
809  !Compute sort of integral of the heating rate.
810  if ( (ctop(i,j) .ne. lm) .and. (ltraj%KCBL(i,j) - ctop(i,j) > 0) ) then
811  sumheat(i,j) = ( sum(abs(heat(i,j,ctop(i,j):ltraj%KCBL(i,j)-1))) &
812  - maxval(abs(heat(i,j,ctop(i,j):ltraj%KCBL(i,j)-1)),1) ) &
813  / ( ltraj%KCBL(i,j) - ctop(i,j) )
814  endif
815 
816  !Compare `integral` to maximum absolute heating rate
817  IF ( ltraj%KCBL(i,j) - ctop(i,j) >= maxcondep ) THEN
818  IF (sumheat(i,j) / maxval(abs(heat(i,j,1:ltraj%KCBL(i,j)-1)),1) > 0.125 ) then
819  ltraj%DOCONVEC(i,j) = 1
820  endif
821  endif
822 
823  !Compute two columns of the Jacobian to check for steep gradients
824  !This prevents instability and floating point issues that can cause failure of the TLM/ADJ dot prod test
825  IF ( ltraj%DOCONVEC(i,j) == 1 ) THEN
827  ENDIF
828 
829  enddo
830  enddo
831 
832  !Prepare the inputs for the cloud scheme
833  !---------------------------------------
834  !Compute the ice fraction for each grid box
835  DO i = 1,im
836  DO j = 1,jm
837  DO l = 1,lm
838  call icefraction( temp(i,j,l), fqi(i,j,l) )
839  enddo
840  enddo
841  enddo
842 
843  !Split the input large scale and convective cloud into ice and liquid parts.
844  ltraj%QILST(1:im,1:jm,:) = dble(traj%QLS(isc:iec,jsc:jec,:)) * fqi(1:im,1:jm,:)
845  ltraj%QLLST(1:im,1:jm,:) = dble(traj%QLS(isc:iec,jsc:jec,:)) * (1-fqi(1:im,1:jm,:))
846  ltraj%QICNT(1:im,1:jm,:) = dble(traj%QCN(isc:iec,jsc:jec,:)) * fqi(1:im,1:jm,:)
847  ltraj%QLCNT(1:im,1:jm,:) = dble(traj%QCN(isc:iec,jsc:jec,:)) * (1-fqi(1:im,1:jm,:))
848 
849  !Split the perturbations for total cloud water and ice into the convective and large scale parts.
850  !Spilitting is based on fraction of total cloud ice/water that is attributed to large scale and anvil
851  !in the trajectory at this time step. Note that we don't really need to protect for small values of
852  !total cloud sum, if the denominator is small then so is the numerator, though compiler may complain.
853  ltraj%ILSF = 0.0
854  ltraj%ICNF = 0.0
855  ltraj%LLSF = 0.0
856  ltraj%LCNF = 0.0
857  DO i = 1,im
858  DO j = 1,jm
859  DO l = 1,lm
860 
861  if ( ltraj%QILST(i,j,l) + ltraj%QICNT(i,j,l) .gt. 0.0_8 ) then
862  ltraj%ILSF(i,j,l) = ltraj%QILST(i,j,l) / ( ltraj%QILST(i,j,l) + ltraj%QICNT(i,j,l) )
863  ltraj%ICNF(i,j,l) = ltraj%QICNT(i,j,l) / ( ltraj%QILST(i,j,l) + ltraj%QICNT(i,j,l) )
864  endif
865  if ( ltraj%QLLST(i,j,l) + ltraj%QLCNT(i,j,l) .gt. 0.0_8 ) then
866  ltraj%LLSF(i,j,l) = ltraj%QLLST(i,j,l) / ( ltraj%QLLST(i,j,l) + ltraj%QLCNT(i,j,l) )
867  ltraj%LCNF(i,j,l) = ltraj%QLCNT(i,j,l) / ( ltraj%QLLST(i,j,l) + ltraj%QLCNT(i,j,l) )
868  endif
869 
870  enddo
871  enddo
872  enddo
873 
874  deallocate(plo)
875  deallocate(pk)
876  deallocate(temp)
877  deallocate(ptt_f)
878  deallocate(qvt_f)
879  deallocate(ptt_l)
880  deallocate(qvt_l)
881  deallocate(heat)
882  deallocate(ctop)
883  deallocate(sumheat)
884  deallocate(jacobian)
885  deallocate(h_pert)
886  deallocate(m_pert)
887  deallocate(pt_pert)
888  deallocate(qv_pert)
889  deallocate(pt_pert_in)
890  deallocate(qv_pert_in)
891  deallocate(fqi)
892 
893  contains
894 
895  subroutine jacobian_filter_tlm
896 
897  !Compute two specific columns of the Jacobian for a single profile. Then filter if values in those
898  !columns are over a certain value, implying too steep gradients.
899 
900  jacobian = 0.0_8
901 
902  DO l = 1,1 !Perturb level by level
903 
904  pt_pert = 0.0_8
905  qv_pert = 0.0_8
906 
907  if (l == 1) then
908 
909  pt_pert(ltraj%KCBL(i,j)) = 1.0_8
910 
911  elseif (l == 2) then
912 
913  if (ltraj%KCBL(i,j) == lm) then
914  qv_pert(ltraj%KCBL(i,j)) = 1.0_8
915  else
916  qv_pert(ltraj%KCBL(i,j) + 1) = 1.0_8
917  endif
918 
919  endif
920 
921  !Save precall prognostic variables
922  pt_pert_in = pt_pert
923  qv_pert_in = qv_pert
924 
925  !Do nonlinear convection to create inputs trajectory for large scale adjoint
926  call rase0_d ( 1, 1, lm, lcnst%icmin, conf%dt, &
927  lcnst%mapl8_cp, lcnst%mapl8_alhl, &
928  lcnst%mapl8_grav, lcnst%mapl8_rgas, &
929  lcnst%mapl8_h2omw, lcnst%mapl8_airmw, &
930  lcnst%mapl8_vireps, &
931  ltraj%seedras(i,j), lcnst%sige, &
932  ltraj%kcbl(i,j), &
933  ltraj%wgt0(i,j,:), ltraj%wgt1(i,j,:), &
934  ltraj%frland(i,j), ltraj%ts(i,j), &
935  ptt_f(i,j,:), pt_pert, &
936  qvt_f(i,j,:), qv_pert, &
937  ltraj%co_auto(i,j), ltraj%cnv_ple(i,j,:), &
938  lcnst%rasparams, lcnst%estblx )
939 
940  !Compute perturbation heating and moistening rates
941  h_pert = (pt_pert - pt_pert_in)/conf%DT
942  m_pert = (qv_pert - qv_pert_in)/conf%DT
943 
944  !Uncomment here if just doing two columns of the Jacobian
945  if (l == 1) then
946  jacobian(0*lm+1:1*lm,1) = h_pert
947  jacobian(1*lm+1:2*lm,1) = m_pert
948  elseif (l == 2) then
949  jacobian(0*lm+1:1*lm,2) = h_pert
950  jacobian(1*lm+1:2*lm,2) = m_pert
951  endif
952 
953  endDO
954 
955  !Constants here determined so as to remove as many of the problematic points as possible.
956  !The constants used in this if loop are tuned from looking at many Jacobians for many time steps. Values are choosen
957  !so as to balance between keeping the natural behahiour for as many points as possible without
958  if ( (maxval(abs(jacobian(1:lm ,1))) .gt. 0.00010 ) .or. &
959  (maxval(abs(jacobian(1:lm ,2))) .gt. 0.25 ) .or. &
960  (maxval(abs(jacobian(lm+1:2*lm,1))) .gt. 1.0e-07 ) .or. &
961  (maxval(abs(jacobian(lm+1:2*lm,2))) .gt. 0.000250 ) ) then
962 
963  ltraj%doconvec(i,j) = 0
964 
965  else
966 
967  ltraj%doconvec(i,j) = 1
968 
969  endif
970 
971  endsubroutine jacobian_filter_tlm
972 
973 endsubroutine set_ltraj
974 
975 ! ------------------------------------------------------------------------------
976 
977 subroutine allocate_ltraj(im,jm,lm,ltraj)
978 
979  integer, intent(in) :: im,jm,lm
980  type(local_traj_moist), intent(inout) :: ltraj
981 
982  !double precision trajectories
983  allocate(ltraj%ut(im,jm,lm) )
984  allocate(ltraj%vt(im,jm,lm) )
985  allocate(ltraj%ptt(im,jm,lm) )
986  allocate(ltraj%qvt(im,jm,lm) )
987  allocate(ltraj%ts(im,jm) )
988  allocate(ltraj%frland(im,jm) )
989  allocate(ltraj%kcbl(im,jm) )
990  allocate(ltraj%khu(im,jm) )
991  allocate(ltraj%khl(im,jm) )
992 
993  !pressure and temperature variables
994  allocate(ltraj%ple(im,jm,0:lm) )
995  allocate(ltraj%cnv_ple(im,jm,0:lm) )
996  allocate(ltraj%pk(im,jm,lm) )
997 
998  !convection varaibles
999  allocate(ltraj%cnv_dqldtt(im,jm,lm) )
1000  allocate(ltraj%cnv_mfdt(im,jm,lm) )
1001  allocate(ltraj%cnv_prc3t(im,jm,lm) )
1002  allocate(ltraj%cnv_updft(im,jm,lm) )
1003  allocate(ltraj%ptt_c(im,jm,lm) )
1004  allocate(ltraj%qvt_c(im,jm,lm) )
1005  allocate(ltraj%cnv_dqldtt_c(im,jm,lm))
1006  allocate(ltraj%cnv_mfdt_c(im,jm,lm) )
1007  allocate(ltraj%cnv_prc3t_c(im,jm,lm) )
1008  allocate(ltraj%cnv_updft_c(im,jm,lm) )
1009  allocate(ltraj%seedras(im,jm) )
1010  allocate(ltraj%co_auto(im,jm) )
1011  allocate(ltraj%doconvec(im,jm) )
1012  allocate(ltraj%wgt0(im,jm,lm) )
1013  allocate(ltraj%wgt1(im,jm,lm) )
1014 
1015  !Cloud variables
1016  allocate(ltraj%qilst(im,jm,lm) )
1017  allocate(ltraj%qllst(im,jm,lm) )
1018  allocate(ltraj%qicnt(im,jm,lm) )
1019  allocate(ltraj%qlcnt(im,jm,lm) )
1020  allocate(ltraj%cflst(im,jm,lm) )
1021  allocate(ltraj%cfcnt(im,jm,lm) )
1022  allocate(ltraj%ilsf(im,jm,lm) )
1023  allocate(ltraj%icnf(im,jm,lm) )
1024  allocate(ltraj%llsf(im,jm,lm) )
1025  allocate(ltraj%lcnf(im,jm,lm) )
1026 
1027 endsubroutine allocate_ltraj
1028 
1029 ! ------------------------------------------------------------------------------
1030 
1031 subroutine allocate_lpert(im,jm,lm,lpert)
1032 
1033  integer, intent(in) :: im,jm,lm
1034  type(local_pert_moist), intent(inout) :: lpert
1035 
1036  allocate(lpert%up(im,jm,lm) )
1037  allocate(lpert%vp(im,jm,lm) )
1038  allocate(lpert%ptp(im,jm,lm) )
1039  allocate(lpert%qvp(im,jm,lm) )
1040  allocate(lpert%cnv_dqldtp(im,jm,lm) )
1041  allocate(lpert%cnv_mfdp(im,jm,lm) )
1042  allocate(lpert%cnv_prc3p(im,jm,lm) )
1043  allocate(lpert%cnv_updfp(im,jm,lm) )
1044  allocate(lpert%qilsp(im,jm,lm) )
1045  allocate(lpert%qllsp(im,jm,lm) )
1046  allocate(lpert%qicnp(im,jm,lm) )
1047  allocate(lpert%qlcnp(im,jm,lm) )
1048  allocate(lpert%cflsp(im,jm,lm) )
1049  allocate(lpert%cfcnp(im,jm,lm) )
1050 
1051 endsubroutine allocate_lpert
1052 
1053 ! ------------------------------------------------------------------------------
1054 
1055 subroutine deallocate_ltraj(ltraj)
1056 
1057  type(local_traj_moist), intent(inout) :: ltraj
1059  deallocate(ltraj%ut)
1060  deallocate(ltraj%vt)
1061  deallocate(ltraj%ptt)
1062  deallocate(ltraj%qvt)
1063  deallocate(ltraj%ts)
1064  deallocate(ltraj%frland)
1065  deallocate(ltraj%kcbl)
1066  deallocate(ltraj%khu)
1067  deallocate(ltraj%khl)
1068  deallocate(ltraj%ple)
1069  deallocate(ltraj%cnv_ple)
1070  deallocate(ltraj%pk)
1071  deallocate(ltraj%cnv_dqldtt)
1072  deallocate(ltraj%cnv_mfdt)
1073  deallocate(ltraj%cnv_prc3t)
1074  deallocate(ltraj%cnv_updft)
1075  deallocate(ltraj%ptt_c)
1076  deallocate(ltraj%qvt_c)
1077  deallocate(ltraj%cnv_dqldtt_c)
1078  deallocate(ltraj%cnv_mfdt_c)
1079  deallocate(ltraj%cnv_prc3t_c)
1080  deallocate(ltraj%cnv_updft_c)
1081  deallocate(ltraj%seedras)
1082  deallocate(ltraj%co_auto)
1083  deallocate(ltraj%doconvec)
1084  deallocate(ltraj%wgt0)
1085  deallocate(ltraj%wgt1)
1086  deallocate(ltraj%qilst)
1087  deallocate(ltraj%qllst)
1088  deallocate(ltraj%qicnt)
1089  deallocate(ltraj%qlcnt)
1090  deallocate(ltraj%cflst)
1091  deallocate(ltraj%cfcnt)
1092  deallocate(ltraj%ilsf)
1093  deallocate(ltraj%icnf)
1094  deallocate(ltraj%llsf)
1095  deallocate(ltraj%lcnf)
1096 
1097 endsubroutine deallocate_ltraj
1098 
1099 ! ------------------------------------------------------------------------------
1100 
1101 subroutine deallocate_lpert(lpert)
1102 
1103  type(local_pert_moist), intent(inout) :: lpert
1105  deallocate(lpert%up)
1106  deallocate(lpert%vp)
1107  deallocate(lpert%ptp)
1108  deallocate(lpert%qvp)
1109  deallocate(lpert%cnv_dqldtp)
1110  deallocate(lpert%cnv_mfdp)
1111  deallocate(lpert%cnv_prc3p)
1112  deallocate(lpert%cnv_updfp)
1113  deallocate(lpert%qilsp)
1114  deallocate(lpert%qllsp)
1115  deallocate(lpert%qicnp)
1116  deallocate(lpert%qlcnp)
1117  deallocate(lpert%cflsp)
1118  deallocate(lpert%cfcnp)
1119 
1120 endsubroutine deallocate_lpert
1121 
1122 ! ------------------------------------------------------------------------------
1123 
1124 end module fv3jedi_lm_moist_mod
1125 
integer, parameter, public set
real(kind=kind_real), parameter, public p00
Compute ice fraction from temperature.
real(8), parameter tmaxtbl
Definition: qsat_util.F90:14
real, parameter, public mapl_alhs
subroutine, public cloud_driver_b(dt, im, jm, lm, th, thb, q, qb, ple, cnv_dqldt, cnv_dqldtb, cnv_mfd, cnv_mfdb, cnv_prc3, cnv_prc3b, cnv_updf, cnv_updfb, qi_ls, qi_lsb, ql_ls, ql_lsb, qi_con, qi_conb, ql_con, ql_conb, cf_ls, cf_lsb, cf_con, cf_conb, frland, physparams, estblx, khu, khl, cons_runiv, cons_kappa, cons_airmw, cons_h2omw, cons_grav, cons_alhl, cons_alhf, cons_pi, cons_rgas, cons_cp, cons_vireps, cons_alhs, cons_tice, cons_rvap, cons_p00, do_moist_physics)
Definition: cloud_ad.F90:31
real, parameter, public mapl_rvap
integer, parameter, public up
subroutine, public rase0_d(idim, irun, k0, icmin, dt, cons_cp, cons_alhl, cons_grav, cons_rgas, cons_h2omw, cons_airmw, cons_vireps, seedras, sige, kcbl, wgt0, wgt1, frland, ts, tho, thod, qho, qhod, co_auto, ple, rasparams, estblx)
type(cp_iter_controls_type), target, public cp_iter_controls
subroutine, public rase0(IDIM, IRUN, K0, ICMIN, DT, CONS_CP, CONS_ALHL, CONS_GRAV, CONS_RGAS, CONS_H2OMW, CONS_AIRMW, CONS_VIREPS, SEEDRAS, SIGE, KCBL, WGT0, WGT1, FRLAND, TS, THO, QHO, CO_AUTO, PLE, CLW, FLXD, CNV_PRC3, CNV_UPDFRC, RASPARAMS, ESTBLX)
Definition: convection.F90:843
real, parameter, public mapl_tice
Definition: conf.py:1
subroutine, public cp_mod_ini(cp_mod_index)
real, parameter, public mapl_cp
subroutine jacobian_filter_tlm
real, parameter, public mapl_vireps
subroutine, public cp_mod_mid
real, parameter, public mapl_airmw
real, parameter, public mapl_alhl
subroutine, public rase_d(idim, irun, k0, icmin, dt, cons_cp, cons_alhl, cons_grav, cons_rgas, cons_h2omw, cons_airmw, cons_vireps, seedras, sige, kcbl, wgt0, wgt1, frland, ts, tho, thod, qho, qhod, uho, uhod, vho, vhod, co_auto, ple, clw, clwd, flxd, flxdd, cnv_prc3, cnv_prc3d, cnv_updfrc, cnv_updfrcd, rasparams, estblx)
subroutine, public cloud_driver_d(dt, im, jm, lm, th, thd, q, qd, ple, cnv_dqldt, cnv_dqldtd, cnv_mfd, cnv_mfdd, cnv_prc3, cnv_prc3d, cnv_updf, cnv_updfd, qi_ls, qi_lsd, ql_ls, ql_lsd, qi_con, qi_cond, ql_con, ql_cond, cf_ls, cf_lsd, cf_con, cf_cond, frland, physparams, estblx, khu, khl, cons_runiv, cons_kappa, cons_airmw, cons_h2omw, cons_grav, cons_alhl, cons_alhf, cons_pi, cons_rgas, cons_cp, cons_vireps, cons_alhs, cons_tice, cons_rvap, cons_p00, do_moist_physics)
Definition: cloud_tl.F90:30
Definition: cloud.F90:1
subroutine, public cp_mod_end
subroutine step_tl(self, conf, traj, pert)
real, parameter, public mapl_kappa
real, parameter, public mapl_h2omw
subroutine, public delete(self)
Top level for fv3jedi linearized model.
real, parameter, public mapl_p00
subroutine allocate_lpert(im, jm, lm, lpert)
subroutine step_ad(self, conf, traj, pert)
type(cp_iter_type), dimension(:), allocatable, target, public cp_iter
real, parameter, public mapl_alhf
subroutine, public finalize_cp_iter
subroutine init_tl(self, pert, traj)
subroutine, public rase_b(idim, irun, k0, icmin, dt, cons_cp, cons_alhl, cons_grav, cons_rgas, cons_h2omw, cons_airmw, cons_vireps, seedras, sige, kcbl, wgt0, wgt1, frland, ts, tho, thob, qho, qhob, uho, uhob, vho, vhob, co_auto, ple, clw, clwb, flxd, flxdb, cnv_prc3, cnv_prc3b, cnv_updfrc, cnv_updfrcb, rasparams, estblx)
subroutine deallocate_ltraj(ltraj)
#define max(a, b)
Definition: mosaic_util.h:33
subroutine set_ltraj(conf, lcnst, traj, ltraj)
real, parameter, public mapl_rgas
subroutine, public esinit(ESTBLX)
Definition: qsat_util.F90:20
real, parameter, public mapl_grav
subroutine init_ad(self, pert, traj)
subroutine deallocate_lpert(lpert)
subroutine, public initialize_cp_iter
real(kind=mapl_r4), parameter, public mapl_pi
subroutine, public create(self, geom, vars)
subroutine init_nl(self, pert, traj)
real, parameter, public mapl_runiv
real(kind=kind_real), parameter, public kappa
subroutine allocate_ltraj(im, jm, lm, ltraj)
subroutine step_nl(self, conf, traj)
Constants for the FV3 model.