FV3 Bundle
fv_dynamics_adm.F90
Go to the documentation of this file.
1 !***********************************************************************
2 !* GNU General Public License *
3 !* This file is a part of fvGFS. *
4 !* *
5 !* fvGFS is free software; you can redistribute it and/or modify it *
6 !* and are expected to follow the terms of the GNU General Public *
7 !* License as published by the Free Software Foundation; either *
8 !* version 2 of the License, or (at your option) any later version. *
9 !* *
10 !* fvGFS is distributed in the hope that it will be useful, but *
11 !* WITHOUT ANY WARRANTY; without even the implied warranty of *
12 !* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 !* General Public License for more details. *
14 !* *
15 !* For the full text of the GNU General Public License, *
16 !* write to: Free Software Foundation, Inc., *
17 !* 675 Mass Ave, Cambridge, MA 02139, USA. *
18 !* or see: http://www.gnu.org/licenses/gpl.html *
19 !***********************************************************************
33  use fv_fill_nlm_mod, only: fill2d
34  use fv_mp_nlm_mod, only: is_master
35  use fv_mp_nlm_mod, only: group_halo_update_type
39  use diag_manager_mod, only: send_data
41  use mpp_domains_mod, only: mpp_update_domains, dgrid_ne, cgrid_ne, domain2d
43  use mpp_mod, only: mpp_pe
46  use fv_sg_nlm_mod, only: neg_adj3
52  use fv_arrays_nlm_mod, only: r_grid
54 !#ifdef MAPL_MODE
55 ! use fv_control_nlm_mod, only: dyn_timer, comm_timer
56 !#endif
57  use fv_arrays_nlm_mod, only: fvprc
58 
61 
63 
64 implicit none
65 
66 !#ifdef MAPL_MODE
67 ! ! Include the MPI library definitons:
68 ! include 'mpif.h'
69 !#endif
70 
71  logical :: rf_initialized = .false.
72  logical :: pt_initialized = .false.
73  logical :: bad_range = .false.
74  real, allocatable :: rf(:)
75  integer :: kmax=1
76  real :: agrav
77  logical, public, save :: idealtest=.false.
78 #ifdef HIWPP
79  real, allocatable:: u00(:,:,:), v00(:,:,:)
80 #endif
81 private
83 
84 CONTAINS
85 ! Differentiation of fv_dynamics in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
86 !mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_core
87 !_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_mod.
88 !mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayleig
89 !h_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_or
90 !d4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.r
91 !emap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap_2d
92 !fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiters
93 ! fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic fv_
94 !restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subgr
95 !id_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_utils
96 !_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_utils_m
97 !od.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_mo
98 !d.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_mod.d
99 !2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.ytp_v
100 !_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_core
101 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
102 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
103 ! gradient of useful results: peln q u v w delp ua delz va
104 ! pkz pe pt
105 ! with respect to varying inputs: peln q u v w delp delz pkz
106 ! pe pk pt
107 !-----------------------------------------------------------------------
108 ! fv_dynamics :: FV dynamical core driver
109 !-----------------------------------------------------------------------
110  SUBROUTINE fv_dynamics_fwd(npx, npy, npz, nq_tot, ng, bdt, consv_te, &
111 & fill, reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, &
112 & q_split, u, v, w, delz, hydrostatic, pt, delp, q, ps, pe, pk, peln, &
113 & pkz, phis, q_con, omga, ua, va, uc, vc, ak, bk, mfx, mfy, cx, cy, &
114 & ze0, hybrid_z, gridstruct, flagstruct, flagstructp, neststruct, &
115 & idiag, bd, parent_grid, domain, time_total)
116  IMPLICIT NONE
117 ! n_map loop
118 ! Large time-step
119  REAL, INTENT(IN) :: bdt
120  REAL, INTENT(IN) :: consv_te
121  REAL, INTENT(IN) :: kappa, cp_air
122  REAL, INTENT(IN) :: zvir, ptop
123  REAL, INTENT(IN), OPTIONAL :: time_total
124  INTEGER, INTENT(IN) :: npx
125  INTEGER, INTENT(IN) :: npy
126  INTEGER, INTENT(IN) :: npz
127 ! transported tracers
128  INTEGER, INTENT(IN) :: nq_tot
129  INTEGER, INTENT(IN) :: ng
130  INTEGER, INTENT(IN) :: ks
131  INTEGER, INTENT(IN) :: ncnst
132 ! small-step horizontal dynamics
133  INTEGER, INTENT(IN) :: n_split
134 ! tracer
135  INTEGER, INTENT(IN) :: q_split
136  LOGICAL, INTENT(IN) :: fill
137  LOGICAL, INTENT(IN) :: reproduce_sum
138  LOGICAL, INTENT(IN) :: hydrostatic
139 ! Using hybrid_z for remapping
140  LOGICAL, INTENT(IN) :: hybrid_z
141  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
142 ! D grid zonal wind (m/s)
143  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz), INTENT(INOUT) &
144 & :: u
145 ! D grid meridional wind (m/s)
146  REAL, DIMENSION(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz), INTENT(INOUT) &
147 & :: v
148 ! W (m/s)
149  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
150 ! temperature (K)
151  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
152 ! pressure thickness (pascal)
153  REAL, INTENT(INOUT) :: delp(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
154 ! specific humidity and constituents
155  REAL, INTENT(INOUT) :: q(bd%isd:bd%ied, bd%jsd:bd%jed, npz, ncnst)
156 ! delta-height (m); non-hydrostatic only
157  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
158 ! height at edges (m); non-hydrostatic
159  REAL, INTENT(INOUT) :: ze0(bd%is:bd%is, bd%js:bd%js, 1)
160 ! ze0 no longer used
161 !-----------------------------------------------------------------------
162 ! Auxilliary pressure arrays:
163 ! The 5 vars below can be re-computed from delp and ptop.
164 !-----------------------------------------------------------------------
165 ! dyn_aux:
166 ! Surface pressure (pascal)
167  REAL, INTENT(INOUT) :: ps(bd%isd:bd%ied, bd%jsd:bd%jed)
168 ! edge pressure (pascal)
169  REAL, INTENT(INOUT) :: pe(bd%is-1:bd%ie+1, npz+1, bd%js-1:bd%je+1)
170 ! pe**kappa
171  REAL, INTENT(INOUT) :: pk(bd%is:bd%ie, bd%js:bd%je, npz+1)
172 ! ln(pe)
173  REAL, INTENT(INOUT) :: peln(bd%is:bd%ie, npz+1, bd%js:bd%je)
174 ! finite-volume mean pk
175  REAL, INTENT(INOUT) :: pkz(bd%is:bd%ie, bd%js:bd%je, npz)
176  REAL, INTENT(INOUT) :: q_con(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
177 !-----------------------------------------------------------------------
178 ! Others:
179 !-----------------------------------------------------------------------
180 ! Surface geopotential (g*Z_surf)
181  REAL, INTENT(INOUT) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
182 ! Vertical pressure velocity (pa/s)
183  REAL, INTENT(INOUT) :: omga(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
184 ! (uc,vc) mostly used as the C grid winds
185  REAL, INTENT(INOUT) :: uc(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
186  REAL, INTENT(INOUT) :: vc(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
187  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(INOUT) ::&
188 & ua, va
189  REAL, DIMENSION(npz+1), INTENT(IN) :: ak, bk
190 ! Accumulated Mass flux arrays: the "Flux Capacitor"
191  REAL, INTENT(INOUT) :: mfx(bd%is:bd%ie+1, bd%js:bd%je, npz)
192  REAL, INTENT(INOUT) :: mfy(bd%is:bd%ie, bd%js:bd%je+1, npz)
193 ! Accumulated Courant number arrays
194  REAL, INTENT(INOUT) :: cx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
195  REAL, INTENT(INOUT) :: cy(bd%isd:bd%ied, bd%js:bd%je+1, npz)
196  TYPE(fv_grid_type), INTENT(INOUT), TARGET :: gridstruct
197  TYPE(fv_flags_type), INTENT(INOUT) :: flagstruct
198  TYPE(fv_flags_pert_type), INTENT(INOUT) :: flagstructp
199  TYPE(fv_nest_type), INTENT(INOUT) :: neststruct
200  TYPE(domain2d), INTENT(INOUT) :: domain
201  TYPE(fv_atmos_type), INTENT(INOUT) :: parent_grid
202  TYPE(fv_diag_type), INTENT(IN) :: idiag
203 ! Local Arrays
204  REAL :: ws(bd%is:bd%ie, bd%js:bd%je)
205  REAL :: te_2d(bd%is:bd%ie, bd%js:bd%je)
206  REAL :: teq(bd%is:bd%ie, bd%js:bd%je)
207  REAL :: ps2(bd%isd:bd%ied, bd%jsd:bd%jed)
208  REAL :: m_fac(bd%is:bd%ie, bd%js:bd%je)
209  REAL :: pfull(npz)
210  REAL, DIMENSION(bd%is:bd%ie) :: cvm
211  REAL :: dp1(bd%isd:bd%ied, bd%jsd:bd%jed, npz), dtdt_m(bd%is:bd%ie, &
212 & bd%js:bd%je, npz), cappa(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
213  REAL(kind=8) :: psx(bd%isd:bd%ied, bd%jsd:bd%jed)
214  REAL(kind=8) :: dpx(bd%is:bd%ie, bd%js:bd%je)
215  REAL :: akap, rdg, ph1, ph2, mdt, gam, amdt, u0
216  INTEGER :: kord_tracer(ncnst), kord_mt, kord_wz, kord_tm
217  INTEGER :: kord_tracer_pert(ncnst), kord_mt_pert, kord_wz_pert, &
218 & kord_tm_pert
219  INTEGER :: i, j, k, n, iq, n_map, nq, nwat, k_split
220 ! GFDL physics
221  INTEGER :: sphum
222  INTEGER, SAVE :: liq_wat=-999
223  INTEGER, SAVE :: ice_wat=-999
224  INTEGER, SAVE :: rainwat=-999
225  INTEGER, SAVE :: snowwat=-999
226  INTEGER, SAVE :: graupel=-999
227  INTEGER, SAVE :: cld_amt=-999
228  INTEGER, SAVE :: theta_d=-999
229  LOGICAL :: used, last_step, do_omega
230  INTEGER, PARAMETER :: max_packs=12
231  TYPE(group_halo_update_type), SAVE :: i_pack(max_packs)
232  INTEGER :: is, ie, js, je
233  INTEGER :: isd, ied, jsd, jed
234  REAL :: dt2
235  REAL(kind=8) :: t1, t2
236  INTEGER :: status
237  REAL :: rf(npz)
238  REAL :: gz(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
239  REAL :: pkc(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
240  REAL :: ptc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
241  REAL :: crx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
242  REAL :: xfx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
243  REAL :: cry(bd%isd:bd%ied, bd%js:bd%je+1, npz)
244  REAL :: yfx(bd%isd:bd%ied, bd%js:bd%je+1, npz)
245  REAL :: divgd(bd%isd:bd%ied+1, bd%jsd:bd%jed+1, npz)
246  REAL :: delpc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
247  REAL :: ut(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
248  REAL :: vt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
249  REAL :: zh(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
250  REAL :: pk3(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
251  REAL :: du(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
252  REAL :: dv(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
253  INTRINSIC any
254  INTRINSIC log
255  INTRINSIC exp
256  INTRINSIC abs
257  INTRINSIC real
258  INTRINSIC cos
259  REAL :: abs0
260  REAL :: abs1
261  INTEGER :: arg1
262  LOGICAL :: arg10
263  REAL*8 :: arg11
264  REAL, DIMENSION(bd%is:bd%ie, bd%js:bd%je) :: arg12
265  REAL :: result1
266  LOGICAL :: res
267 
268  ws = 0.0
269  te_2d = 0.0
270  teq = 0.0
271  ps2 = 0.0
272  m_fac = 0.0
273  pfull = 0.0
274  cvm = 0.0
275  dp1 = 0.0
276  dtdt_m = 0.0
277  cappa = 0.0
278  akap = 0.0
279  rdg = 0.0
280  ph1 = 0.0
281  ph2 = 0.0
282  mdt = 0.0
283  gam = 0.0
284  amdt = 0.0
285  u0 = 0.0
286  kord_tracer = 0
287  kord_mt = 0
288  kord_wz = 0
289  kord_tm = 0
290  kord_tracer_pert = 0
291  kord_mt_pert = 0
292  kord_wz_pert = 0
293  kord_tm_pert = 0
294  iq = 0
295  n_map = 0
296  nq = 0
297  nwat = 0
298  k_split = 0
299  sphum = 0
300  is = 0
301  ie = 0
302  js = 0
303  je = 0
304  isd = 0
305  ied = 0
306  jsd = 0
307  jed = 0
308  dt2 = 0.0
309  t1 = 0.0_8
310  t2 = 0.0_8
311  rf = 0.0
312  gz = 0.0
313  pkc = 0.0
314  ptc = 0.0
315  crx = 0.0
316  xfx = 0.0
317  cry = 0.0
318  yfx = 0.0
319  divgd = 0.0
320  delpc = 0.0
321  ut = 0.0
322  vt = 0.0
323  zh = 0.0
324  pk3 = 0.0
325  du = 0.0
326  dv = 0.0
327  is = bd%is
328  ie = bd%ie
329  js = bd%js
330  je = bd%je
331  isd = bd%isd
332  ied = bd%ied
333  jsd = bd%jsd
334  jed = bd%jed
335 ! cv_air = cp_air - rdgas
336  agrav = 1./grav
337  dt2 = 0.5*bdt
338  k_split = flagstruct%k_split
339  nwat = flagstruct%nwat
340  nq = nq_tot - flagstruct%dnats
341  rdg = -(rdgas*agrav)
342 !allocate ( dp1(isd:ied, jsd:jed, 1:npz) )
343 ! Begin Dynamics timer for GEOS history processing
344 !t1 = MPI_Wtime(status)
345 !allocate ( cappa(isd:isd,jsd:jsd,1) )
346 !We call this BEFORE converting pt to virtual potential temperature,
347 !since we interpolate on (regular) temperature rather than theta.
348  IF (gridstruct%nested .OR. any(neststruct%child_grids)) THEN
349  CALL setup_nested_grid_bcs(npx, npy, npz, zvir, ncnst, u, v, w, pt&
350 & , delp, delz, q, uc, vc, pkz, neststruct%&
351 & nested, flagstruct%inline_q, flagstruct%&
352 & make_nh, ng, gridstruct, flagstruct, &
353 & neststruct, neststruct%nest_timestep, &
354 & neststruct%tracer_nest_timestep, domain, bd, &
355 & nwat)
356  IF (gridstruct%nested) THEN
357 !Correct halo values have now been set up for BCs; we can go ahead and apply them too...
358  CALL nested_grid_bc_apply_intt(pt, 0, 0, npx, npy, npz, bd, 1., &
359 & 1., neststruct%pt_bc, bctype=neststruct&
360 & %nestbctype)
361  CALL pushcontrol(2,0)
362  ELSE
363  CALL pushcontrol(2,1)
364  END IF
365  ELSE
366  CALL pushcontrol(2,2)
367  END IF
368  IF (flagstruct%no_dycore) THEN
369  IF (nwat .EQ. 2 .AND. (.NOT.hydrostatic)) THEN
370  CALL pushcontrol(1,0)
371  sphum = get_tracer_index(model_atmos, 'sphum')
372  ELSE
373  CALL pushcontrol(1,0)
374  END IF
375  ELSE
376  CALL pushcontrol(1,1)
377  END IF
378 !goto 911
379  IF (fpp%fpp_mapl_mode) THEN
380  SELECT CASE (nwat)
381  CASE (0)
382  CALL pushcontrol(1,0)
383  sphum = 1
384 ! to cause trouble if (mis)used
385  cld_amt = -1
386  CASE (1)
387  CALL pushcontrol(1,0)
388  sphum = 1
389 ! to cause trouble if (mis)used
390 ! to cause trouble if (mis)used
391 ! to cause trouble if (mis)used
392 ! to cause trouble if (mis)used
393 ! to cause trouble if (mis)used
394 ! to cause trouble if (mis)used
395  cld_amt = -1
396 ! to cause trouble if (mis)used
397  theta_d = -1
398  CASE (3)
399  CALL pushcontrol(1,0)
400  sphum = 1
401 ! to cause trouble if (mis)used
402 ! to cause trouble if (mis)used
403 ! to cause trouble if (mis)used
404 ! to cause trouble if (mis)used
405  cld_amt = -1
406 ! to cause trouble if (mis)used
407  theta_d = -1
408  CASE DEFAULT
409  CALL pushcontrol(1,0)
410  END SELECT
411  ELSE
412  IF (nwat .EQ. 0) THEN
413  CALL pushcontrol(1,1)
414  sphum = 1
415 ! to cause trouble if (mis)used
416  cld_amt = -1
417  ELSE
418  CALL pushcontrol(1,1)
419  sphum = get_tracer_index(model_atmos, 'sphum')
420  cld_amt = get_tracer_index(model_atmos, 'cld_amt')
421  END IF
422  theta_d = get_tracer_index(model_atmos, 'theta_d')
423  END IF
424  akap = kappa
425 !$OMP parallel do default(none) shared(npz,ak,bk,flagstruct,pfull) &
426 !$OMP private(ph1, ph2)
427  DO k=1,npz
428  ph1 = ak(k) + bk(k)*flagstruct%p_ref
429  ph2 = ak(k+1) + bk(k+1)*flagstruct%p_ref
430  pfull(k) = (ph2-ph1)/log(ph2/ph1)
431  END DO
432  IF (hydrostatic) THEN
433 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,zvir,nwat,q,q_con,sphum,liq_wat, &
434 !$OMP rainwat,ice_wat,snowwat,graupel) private(cvm)
435  DO k=1,npz
436  DO j=js,je
437  DO i=is,ie
438  dp1(i, j, k) = zvir*q(i, j, k, sphum)
439  END DO
440  END DO
441  END DO
442  CALL pushcontrol(1,0)
443  ELSE
444 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,zvir,q,q_con,sphum,liq_wat, &
445 !$OMP rainwat,ice_wat,snowwat,graupel,pkz,flagstruct, &
446 !$OMP cappa,kappa,rdg,delp,pt,delz,nwat) &
447 !$OMP private(cvm)
448  DO k=1,npz
449  IF (flagstruct%moist_phys) THEN
450  DO j=js,je
451  DO i=is,ie
452  dp1(i, j, k) = zvir*q(i, j, k, sphum)
453  CALL pushrealarray(pkz(i, j, k))
454  pkz(i, j, k) = exp(kappa*log(rdg*delp(i, j, k)*pt(i, j, k)&
455 & *(1.+dp1(i, j, k))/delz(i, j, k)))
456  END DO
457  END DO
458  CALL pushcontrol(1,1)
459  ELSE
460 ! Using dry pressure for the definition of the virtual potential temperature
461 ! pkz(i,j,k) = exp( kappa*log(rdg*delp(i,j,k)*pt(i,j,k)* &
462 ! (1.-q(i,j,k,sphum))/delz(i,j,k)) )
463  DO j=js,je
464  DO i=is,ie
465  dp1(i, j, k) = 0.
466  CALL pushrealarray(pkz(i, j, k))
467  pkz(i, j, k) = exp(kappa*log(rdg*delp(i, j, k)*pt(i, j, k)&
468 & /delz(i, j, k)))
469  END DO
470  END DO
471  CALL pushcontrol(1,0)
472  END IF
473  END DO
474  CALL pushcontrol(1,1)
475  END IF
476  IF (flagstruct%fv_debug) THEN
477  IF (.NOT.hydrostatic) THEN
478  CALL pushcontrol(1,0)
479  ELSE
480  CALL pushcontrol(1,0)
481  END IF
482  ELSE
483  CALL pushcontrol(1,1)
484  END IF
485 !---------------------
486 ! Compute Total Energy
487 !---------------------
488  IF (consv_te .GT. 0. .AND. (.NOT.do_adiabatic_init)) THEN
489  CALL compute_total_energy_fwd(is, ie, js, je, isd, ied, jsd, jed, &
490 & npz, u, v, w, delz, pt, delp, q, dp1, pe, &
491 & peln, phis, gridstruct%rsin2, gridstruct%&
492 & cosa_s, zvir, cp_air, rdgas, hlv, te_2d, &
493 & ua, va, teq, flagstruct%moist_phys, nwat, &
494 & sphum, liq_wat, rainwat, ice_wat, snowwat&
495 & , graupel, hydrostatic, idiag%id_te)
496  IF (idiag%id_te .GT. 0) THEN
497  CALL pushcontrol(1,0)
498  ELSE
499  CALL pushcontrol(1,0)
500  END IF
501  ELSE
502  CALL pushcontrol(1,1)
503  END IF
504  IF ((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .AND. (.NOT.&
505 & do_adiabatic_init)) THEN
506  CALL compute_aam_fwd(npz, is, ie, js, je, isd, ied, jsd, jed, &
507 & gridstruct, bd, ptop, ua, va, u, v, delp, teq, ps2&
508 & , m_fac)
509  CALL pushcontrol(1,0)
510  ELSE
511  CALL pushcontrol(1,1)
512  END IF
513  IF (flagstruct%tau .GT. 0.) THEN
514  IF (gridstruct%grid_type .LT. 4) THEN
515  IF (bdt .GE. 0.) THEN
516  abs0 = bdt
517  ELSE
518  abs0 = -bdt
519  END IF
520  arg10 = .NOT.neststruct%nested
521  CALL rayleigh_super_fwd(abs0, npx, npy, npz, ks, pfull, phis, &
522 & flagstruct%tau, u, v, w, pt, ua, va, delz, &
523 & gridstruct%agrid, cp_air, rdgas, ptop, &
524 & hydrostatic, arg10, flagstruct%rf_cutoff, rf, &
525 & gridstruct, domain, bd)
526  CALL pushcontrol(2,0)
527  ELSE
528  IF (bdt .GE. 0.) THEN
529  abs1 = bdt
530  ELSE
531  abs1 = -bdt
532  END IF
533  CALL rayleigh_friction_fwd(abs1, npx, npy, npz, ks, pfull, &
534 & flagstruct%tau, u, v, w, pt, ua, va, delz, &
535 & cp_air, rdgas, ptop, hydrostatic, .true., &
536 & flagstruct%rf_cutoff, rf, gridstruct, &
537 & domain, bd)
538  CALL pushcontrol(2,1)
539  END IF
540  ELSE
541  CALL pushcontrol(2,2)
542  END IF
543 ! Convert pt to virtual potential temperature on the first timestep
544  IF (flagstruct%adiabatic) THEN
545 !$OMP parallel do default(none) shared(theta_d,is,ie,js,je,npz,pt,pkz,q)
546  DO k=1,npz
547  DO j=js,je
548  DO i=is,ie
549  CALL pushrealarray(pt(i, j, k))
550  pt(i, j, k) = pt(i, j, k)/pkz(i, j, k)
551  END DO
552  END DO
553  IF (theta_d .GT. 0) THEN
554  DO j=js,je
555  DO i=is,ie
556  CALL pushrealarray(q(i, j, k, theta_d))
557  q(i, j, k, theta_d) = pt(i, j, k)
558  END DO
559  END DO
560  CALL pushcontrol(1,1)
561  ELSE
562  CALL pushcontrol(1,0)
563  END IF
564  END DO
565  CALL pushcontrol(1,0)
566  ELSE
567 !$OMP parallel do default(none) shared(is,ie,js,je,npz,pt,dp1,pkz,q_con)
568  DO k=1,npz
569  DO j=js,je
570  DO i=is,ie
571  CALL pushrealarray(pt(i, j, k))
572  pt(i, j, k) = pt(i, j, k)*(1.+dp1(i, j, k))/pkz(i, j, k)
573  END DO
574  END DO
575  END DO
576  CALL pushcontrol(1,1)
577  END IF
578  last_step = .false.
579  mdt = bdt/REAL(k_split)
580 !DryMassRoundoffControl
581 !allocate(psx(isd:ied,jsd:jed),dpx(is:ie,js:je))
582  IF (fpp%fpp_overload_r4) THEN
583  DO j=js,je
584  DO i=is,ie
585  psx(i, j) = pe(i, npz+1, j)
586  dpx(i, j) = 0.0
587  END DO
588  END DO
589  CALL pushcontrol(1,0)
590  ELSE
591  CALL pushcontrol(1,1)
592  END IF
593 ! first level of time-split
594  DO n_map=1,k_split
595  CALL pushrealarray(delp, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
596  CALL start_group_halo_update(i_pack(1), delp, domain, complete=&
597 & .true.)
598  CALL pushrealarray(pt, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
599  CALL start_group_halo_update(i_pack(1), pt, domain, complete=&
600 & .true.)
601  CALL pushrealarray(v, (bd%ied-bd%isd+2)*(bd%jed-bd%jsd+1)*npz)
602  CALL pushrealarray(u, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+2)*npz)
603  CALL start_group_halo_update(i_pack(8), u, v, domain, gridtype=&
604 & dgrid_ne)
605 !$OMP parallel do default(none) shared(isd,ied,jsd,jed,npz,dp1,delp)
606  DO k=1,npz
607  DO j=jsd,jed
608  DO i=isd,ied
609  CALL pushrealarray(dp1(i, j, k))
610  dp1(i, j, k) = delp(i, j, k)
611  END DO
612  END DO
613  END DO
614  IF (n_map .EQ. k_split) last_step = .true.
615  CALL dyn_core_fwd(npx, npy, npz, ng, sphum, nq, mdt, n_split, zvir&
616 & , cp_air, akap, cappa, grav, hydrostatic, u, v, w, &
617 & delz, pt, q, delp, pe, pk, phis, ws, omga, ptop, pfull&
618 & , ua, va, uc, vc, mfx, mfy, cx, cy, pkz, peln, q_con, &
619 & ak, bk, dpx, ks, gridstruct, flagstruct, flagstructp, &
620 & neststruct, idiag, bd, domain, arg10, i_pack, &
621 & last_step, gz, pkc, ptc, crx, xfx, cry, yfx, divgd, &
622 & delpc, ut, vt, zh, pk3, du, dv, time_total)
623 !DryMassRoundoffControl
624  IF (last_step) THEN
625  IF (fpp%fpp_overload_r4) THEN
626  DO j=js,je
627  DO i=is,ie
628  psx(i, j) = psx(i, j) + dpx(i, j)
629  END DO
630  END DO
631  CALL mpp_update_domains(psx, domain)
632  DO j=js-1,je+1
633  DO i=is-1,ie+1
634  CALL pushrealarray(pe(i, npz+1, j))
635  pe(i, npz+1, j) = psx(i, j)
636  END DO
637  END DO
638  CALL pushcontrol(2,0)
639  ELSE
640  CALL pushcontrol(2,1)
641  END IF
642  ELSE
643  CALL pushcontrol(2,2)
644  END IF
645 !deallocate(psx,dpx)
646  IF (.NOT.flagstruct%inline_q .AND. nq .NE. 0) THEN
647 !--------------------------------------------------------
648 ! Perform large-time-step scalar transport using the accumulated CFL and
649 ! mass fluxes
650 !!! CLEANUP: merge these two calls?
651  IF (gridstruct%nested) THEN
652  CALL tracer_2d_nested_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct&
653 & , bd, domain, npx, npy, npz, nq, &
654 & flagstruct%hord_tr, q_split, mdt, idiag%&
655 & id_divg, i_pack(10), flagstruct%nord_tr, &
656 & flagstruct%trdm2, k_split, neststruct, &
657 & parent_grid, flagstructp%hord_tr_pert, &
658 & flagstructp%nord_tr_pert, flagstructp%&
659 & trdm2_pert, flagstructp%split_damp_tr)
660  CALL pushcontrol(2,0)
661  ELSE IF (flagstruct%z_tracer) THEN
662  CALL tracer_2d_1l_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct, bd&
663 & , domain, npx, npy, npz, nq, flagstruct%&
664 & hord_tr, q_split, mdt, idiag%id_divg, i_pack(&
665 & 10), flagstruct%nord_tr, flagstruct%trdm2, &
666 & flagstructp%hord_tr_pert, flagstructp%&
667 & nord_tr_pert, flagstructp%trdm2_pert, &
668 & flagstructp%split_damp_tr)
669  CALL pushcontrol(2,1)
670  ELSE
671  CALL tracer_2d_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, &
672 & domain, npx, npy, npz, nq, flagstruct%hord_tr, &
673 & q_split, mdt, idiag%id_divg, i_pack(10), &
674 & flagstruct%nord_tr, flagstruct%trdm2, flagstructp&
675 & %hord_tr_pert, flagstructp%nord_tr_pert, &
676 & flagstructp%trdm2_pert, flagstructp%split_damp_tr&
677 & )
678  CALL pushcontrol(2,2)
679  END IF
680  ELSE
681  CALL pushcontrol(2,3)
682  END IF
683  IF (npz .GT. 4) THEN
684 !------------------------------------------------------------------------
685 ! Perform vertical remapping from Lagrangian control-volume to
686 ! the Eulerian coordinate as specified by the routine set_eta.
687 ! Note that this finite-volume dycore is otherwise independent of the vertical
688 ! Eulerian coordinate.
689 !------------------------------------------------------------------------
690  DO iq=1,nq
691  kord_tracer(iq) = flagstruct%kord_tr
692 ! monotonic
693  IF (iq .EQ. cld_amt) kord_tracer(iq) = 9
694  CALL pushinteger(kord_tracer_pert(iq))
695  kord_tracer_pert(iq) = flagstructp%kord_tr_pert
696 ! linear
697  IF (iq .EQ. cld_amt) THEN
698  CALL pushcontrol(1,1)
699  kord_tracer_pert(iq) = 17
700  ELSE
701  CALL pushcontrol(1,0)
702  END IF
703  END DO
704  do_omega = hydrostatic .AND. last_step
705  kord_mt = flagstruct%kord_mt
706  kord_wz = flagstruct%kord_wz
707  kord_tm = flagstruct%kord_tm
708  kord_mt_pert = flagstructp%kord_mt_pert
709  kord_wz_pert = flagstructp%kord_wz_pert
710  kord_tm_pert = flagstructp%kord_tm_pert
711  IF (n_map .EQ. k_split) THEN
712  kord_mt = kord_mt_pert
713  kord_wz = kord_wz_pert
714  kord_tm = kord_tm_pert
715  kord_tracer = kord_tracer_pert
716  END IF
717  CALL lagrangian_to_eulerian_fwd(last_step, consv_te, ps, pe, &
718 & delp, pkz, pk, mdt, bdt, npz, is, ie, &
719 & js, je, isd, ied, jsd, jed, nq, nwat, &
720 & sphum, q_con, u, v, w, delz, pt, q, &
721 & phis, zvir, cp_air, akap, cappa, &
722 & kord_mt, kord_wz, kord_tracer, kord_tm&
723 & , peln, te_2d, ng, ua, va, omga, dp1, &
724 & ws, fill, reproduce_sum, arg10, dtdt_m&
725 & , ptop, ak, bk, pfull, flagstruct, &
726 & gridstruct, domain, flagstruct%&
727 & do_sat_adj, hydrostatic, hybrid_z, &
728 & do_omega, flagstruct%adiabatic, &
729 & do_adiabatic_init, mfx, mfy, &
730 & flagstruct%remap_option, kord_mt_pert&
731 & , kord_wz_pert, kord_tracer_pert, &
732 & kord_tm_pert)
733  IF (last_step) THEN
734  IF (.NOT.hydrostatic) THEN
735 !$OMP parallel do default(none) shared(is,ie,js,je,npz,omga,delp,delz,w)
736  DO k=1,npz
737  DO j=js,je
738  DO i=is,ie
739  CALL pushrealarray(omga(i, j, k))
740  omga(i, j, k) = delp(i, j, k)/delz(i, j, k)*w(i, j, k)
741  END DO
742  END DO
743  END DO
744  CALL pushcontrol(1,0)
745  ELSE
746  CALL pushcontrol(1,1)
747  END IF
748 !--------------------------
749 ! Filter omega for physics:
750 !--------------------------
751  IF (flagstruct%nf_omega .GT. 0) THEN
752  arg11 = 0.18*gridstruct%da_min
753  CALL del2_cubed_fwd(omga, arg11, gridstruct, domain, npx, &
754 & npy, npz, flagstruct%nf_omega, bd)
755  CALL pushcontrol(2,3)
756  ELSE
757  CALL pushcontrol(2,2)
758  END IF
759  ELSE
760  CALL pushcontrol(2,1)
761  END IF
762  ELSE
763  CALL pushcontrol(2,0)
764  END IF
765  END DO
766  IF (nwat .EQ. 6) THEN
767  IF (flagstruct%fv_debug) THEN
768  CALL pushcontrol(1,0)
769  ELSE
770  CALL pushcontrol(1,0)
771  END IF
772  ELSE
773  CALL pushcontrol(1,1)
774  END IF
775  IF (((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .OR. idiag%&
776 & id_aam .GT. 0) .AND. (.NOT.do_adiabatic_init)) THEN
777  CALL compute_aam_fwd(npz, is, ie, js, je, isd, ied, jsd, jed, &
778 & gridstruct, bd, ptop, ua, va, u, v, delp, te_2d, ps&
779 & , m_fac)
780  CALL pushcontrol(1,0)
781  ELSE
782  CALL pushcontrol(1,1)
783  END IF
784  IF ((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .AND. (.NOT.&
785 & do_adiabatic_init)) THEN
786 !$OMP parallel do default(none) shared(is,ie,js,je,te_2d,teq,dt2,ps2,ps,idiag)
787  DO j=js,je
788  DO i=is,ie
789 ! Note: the mountain torque computation contains also numerical error
790 ! The numerical error is mostly from the zonal gradient of the terrain (zxg)
791  te_2d(i, j) = te_2d(i, j) - teq(i, j) + dt2*(ps2(i, j)+ps(i, j&
792 & ))*idiag%zxg(i, j)
793  END DO
794  END DO
795  IF (flagstruct%consv_am .OR. prt_minmax) THEN
796  amdt = g_sum(domain, te_2d, is, ie, js, je, ng, gridstruct%&
797 & area_64, 0, reproduce=.true.)
798  result1 = g_sum(domain, m_fac, is, ie, js, je, ng, gridstruct%&
799 & area_64, 0, reproduce=.true.)
800  u0 = -(radius*amdt/result1)
801  res = is_master()
802  IF (res .AND. prt_minmax) THEN
803  CALL pushcontrol(1,0)
804  WRITE(6, *) 'Dynamic AM tendency (Hadleys)=', amdt/(bdt*1.e18)&
805 & , 'del-u (per day)=', u0*86400./bdt
806  ELSE
807  CALL pushcontrol(1,0)
808  END IF
809  ELSE
810  CALL pushcontrol(1,1)
811  END IF
812 ! consv_am
813  IF (flagstruct%consv_am) THEN
814  CALL pushcontrol(2,0)
815  ELSE
816  CALL pushcontrol(2,1)
817  END IF
818  ELSE
819  CALL pushcontrol(2,2)
820  END IF
821  CALL pushinteger(jed)
822  CALL pushrealarray(cry, (bd%ied-bd%isd+1)*(bd%je-bd%js+2)*npz)
823  CALL pushrealarray(crx, (bd%ie-bd%is+2)*(bd%jed-bd%jsd+1)*npz)
824  CALL pushrealarray(amdt)
825  CALL pushinteger(sphum)
826  CALL pushrealarray(result1)
827  CALL pushrealarray(vt, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
828  CALL pushrealarray(te_2d, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
829  CALL pushinteger(isd)
830  CALL pushrealarray(yfx, (bd%ied-bd%isd+1)*(bd%je-bd%js+2)*npz)
831  CALL pushrealarray(m_fac, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
832  CALL pushinteger(kord_tracer_pert, ncnst)
833  CALL pushrealarray(delpc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
834  CALL pushrealarray(pkc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1)&
835 & )
836  CALL pushrealarray(ut, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
837  CALL pushinteger(ied)
838  CALL pushrealarray(rf, npz)
839  CALL pushinteger(jsd)
840  CALL pushrealarray(pfull, npz)
841  CALL pushrealarray(ptc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
842  CALL pushrealarray(dp1, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
843  CALL pushrealarray(gz, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1))
844  CALL pushrealarray(ws, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
845  CALL pushrealarray(xfx, (bd%ie-bd%is+2)*(bd%jed-bd%jsd+1)*npz)
846  CALL pushrealarray(pk3, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1)&
847 & )
848  END SUBROUTINE fv_dynamics_fwd
849 ! Differentiation of fv_dynamics in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
850 !_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_cor
851 !e_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_mod
852 !.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Raylei
853 !gh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_o
854 !rd4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.
855 !remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap_2d
856 ! fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiter
857 !s fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic fv
858 !_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subg
859 !rid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_util
860 !s_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_utils_
861 !mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_m
862 !od.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_mod.
863 !d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.ytp_
864 !v_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_cor
865 !e_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_uti
866 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
867 ! gradient of useful results: peln q u v w delp ua delz va
868 ! pkz pe pt
869 ! with respect to varying inputs: peln q u v w delp delz pkz
870 ! pe pk pt
871 !-----------------------------------------------------------------------
872 ! fv_dynamics :: FV dynamical core driver
873 !-----------------------------------------------------------------------
874  SUBROUTINE fv_dynamics_bwd(npx, npy, npz, nq_tot, ng, bdt, consv_te, &
875 & fill, reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, &
876 & q_split, u, u_ad, v, v_ad, w, w_ad, delz, delz_ad, hydrostatic, pt, &
877 & pt_ad, delp, delp_ad, q, q_ad, ps, ps_ad, pe, pe_ad, pk, pk_ad, peln&
878 & , peln_ad, pkz, pkz_ad, phis, q_con, omga, omga_ad, ua, ua_ad, va, &
879 & va_ad, uc, uc_ad, vc, vc_ad, ak, bk, mfx, mfx_ad, mfy, mfy_ad, cx, &
880 & cx_ad, cy, cy_ad, ze0, hybrid_z, gridstruct, flagstruct, flagstructp&
881 & , neststruct, idiag, bd, parent_grid, domain, time_total)
882  IMPLICIT NONE
883  REAL, INTENT(IN) :: bdt
884  REAL, INTENT(IN) :: consv_te
885  REAL, INTENT(IN) :: kappa, cp_air
886  REAL, INTENT(IN) :: zvir, ptop
887  REAL, INTENT(IN), OPTIONAL :: time_total
888  INTEGER, INTENT(IN) :: npx
889  INTEGER, INTENT(IN) :: npy
890  INTEGER, INTENT(IN) :: npz
891  INTEGER, INTENT(IN) :: nq_tot
892  INTEGER, INTENT(IN) :: ng
893  INTEGER, INTENT(IN) :: ks
894  INTEGER, INTENT(IN) :: ncnst
895  INTEGER, INTENT(IN) :: n_split
896  INTEGER, INTENT(IN) :: q_split
897  LOGICAL, INTENT(IN) :: fill
898  LOGICAL, INTENT(IN) :: reproduce_sum
899  LOGICAL, INTENT(IN) :: hydrostatic
900  LOGICAL, INTENT(IN) :: hybrid_z
901  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
902  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz), INTENT(INOUT) &
903 & :: u
904  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz), INTENT(INOUT) &
905 & :: u_ad
906  REAL, DIMENSION(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz), INTENT(INOUT) &
907 & :: v
908  REAL, DIMENSION(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz), INTENT(INOUT) &
909 & :: v_ad
910  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
911  REAL, INTENT(INOUT) :: w_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
912  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
913  REAL, INTENT(INOUT) :: pt_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
914  REAL, INTENT(INOUT) :: delp(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
915  REAL, INTENT(INOUT) :: delp_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
916  REAL, INTENT(INOUT) :: q(bd%isd:bd%ied, bd%jsd:bd%jed, npz, ncnst)
917  REAL, INTENT(INOUT) :: q_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz, ncnst&
918 & )
919  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
920  REAL, INTENT(INOUT) :: delz_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
921  REAL, INTENT(INOUT) :: ze0(bd%is:bd%is, bd%js:bd%js, 1)
922  REAL, INTENT(INOUT) :: ps(bd%isd:bd%ied, bd%jsd:bd%jed)
923  REAL, INTENT(INOUT) :: ps_ad(bd%isd:bd%ied, bd%jsd:bd%jed)
924  REAL, INTENT(INOUT) :: pe(bd%is-1:bd%ie+1, npz+1, bd%js-1:bd%je+1)
925  REAL, INTENT(INOUT) :: pe_ad(bd%is-1:bd%ie+1, npz+1, bd%js-1:bd%je+1&
926 & )
927  REAL, INTENT(INOUT) :: pk(bd%is:bd%ie, bd%js:bd%je, npz+1)
928  REAL, INTENT(INOUT) :: pk_ad(bd%is:bd%ie, bd%js:bd%je, npz+1)
929  REAL, INTENT(INOUT) :: peln(bd%is:bd%ie, npz+1, bd%js:bd%je)
930  REAL, INTENT(INOUT) :: peln_ad(bd%is:bd%ie, npz+1, bd%js:bd%je)
931  REAL, INTENT(INOUT) :: pkz(bd%is:bd%ie, bd%js:bd%je, npz)
932  REAL, INTENT(INOUT) :: pkz_ad(bd%is:bd%ie, bd%js:bd%je, npz)
933  REAL, INTENT(INOUT) :: q_con(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
934  REAL, INTENT(INOUT) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
935  REAL, INTENT(INOUT) :: omga(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
936  REAL, INTENT(INOUT) :: omga_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
937  REAL, INTENT(INOUT) :: uc(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
938  REAL, INTENT(INOUT) :: uc_ad(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
939  REAL, INTENT(INOUT) :: vc(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
940  REAL, INTENT(INOUT) :: vc_ad(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
941  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(INOUT) ::&
942 & ua, va
943  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(INOUT) ::&
944 & ua_ad, va_ad
945  REAL, DIMENSION(npz+1), INTENT(IN) :: ak, bk
946  REAL, INTENT(INOUT) :: mfx(bd%is:bd%ie+1, bd%js:bd%je, npz)
947  REAL, INTENT(INOUT) :: mfx_ad(bd%is:bd%ie+1, bd%js:bd%je, npz)
948  REAL, INTENT(INOUT) :: mfy(bd%is:bd%ie, bd%js:bd%je+1, npz)
949  REAL, INTENT(INOUT) :: mfy_ad(bd%is:bd%ie, bd%js:bd%je+1, npz)
950  REAL, INTENT(INOUT) :: cx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
951  REAL, INTENT(INOUT) :: cx_ad(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
952  REAL, INTENT(INOUT) :: cy(bd%isd:bd%ied, bd%js:bd%je+1, npz)
953  REAL, INTENT(INOUT) :: cy_ad(bd%isd:bd%ied, bd%js:bd%je+1, npz)
954  TYPE(fv_grid_type), INTENT(INOUT), TARGET :: gridstruct
955  TYPE(fv_flags_type), INTENT(INOUT) :: flagstruct
956  TYPE(fv_flags_pert_type), INTENT(INOUT) :: flagstructp
957  TYPE(fv_nest_type), INTENT(INOUT) :: neststruct
958  TYPE(domain2d), INTENT(INOUT) :: domain
959  TYPE(fv_atmos_type), INTENT(INOUT) :: parent_grid
960  TYPE(fv_diag_type), INTENT(IN) :: idiag
961  REAL :: ws(bd%is:bd%ie, bd%js:bd%je)
962  REAL :: ws_ad(bd%is:bd%ie, bd%js:bd%je)
963  REAL :: te_2d(bd%is:bd%ie, bd%js:bd%je)
964  REAL :: te_2d_ad(bd%is:bd%ie, bd%js:bd%je)
965  REAL :: teq(bd%is:bd%ie, bd%js:bd%je)
966  REAL :: teq_ad(bd%is:bd%ie, bd%js:bd%je)
967  REAL :: ps2(bd%isd:bd%ied, bd%jsd:bd%jed)
968  REAL :: ps2_ad(bd%isd:bd%ied, bd%jsd:bd%jed)
969  REAL :: m_fac(bd%is:bd%ie, bd%js:bd%je)
970  REAL :: m_fac_ad(bd%is:bd%ie, bd%js:bd%je)
971  REAL :: pfull(npz)
972  REAL, DIMENSION(bd%is:bd%ie) :: cvm
973  REAL :: dp1(bd%isd:bd%ied, bd%jsd:bd%jed, npz), dtdt_m(bd%is:bd%ie, &
974 & bd%js:bd%je, npz), cappa(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
975  REAL :: dp1_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
976  REAL(kind=8) :: psx(bd%isd:bd%ied, bd%jsd:bd%jed)
977  REAL(kind=8) :: psx_ad(bd%isd:bd%ied, bd%jsd:bd%jed)
978  REAL(kind=8) :: dpx(bd%is:bd%ie, bd%js:bd%je)
979  REAL(kind=8) :: dpx_ad(bd%is:bd%ie, bd%js:bd%je)
980  REAL :: akap, rdg, ph1, ph2, mdt, gam, amdt, u0
981  REAL :: amdt_ad, u0_ad
982  INTEGER :: kord_tracer(ncnst), kord_mt, kord_wz, kord_tm
983  INTEGER :: kord_tracer_pert(ncnst), kord_mt_pert, kord_wz_pert, &
984 & kord_tm_pert
985  INTEGER :: i, j, k, n, iq, n_map, nq, nwat, k_split
986  INTEGER :: sphum
987  INTEGER, SAVE :: liq_wat=-999
988  INTEGER, SAVE :: ice_wat=-999
989  INTEGER, SAVE :: rainwat=-999
990  INTEGER, SAVE :: snowwat=-999
991  INTEGER, SAVE :: graupel=-999
992  INTEGER, SAVE :: cld_amt=-999
993  INTEGER, SAVE :: theta_d=-999
994  LOGICAL :: used, last_step, do_omega
995  INTEGER, PARAMETER :: max_packs=12
996  TYPE(group_halo_update_type), SAVE :: i_pack(max_packs)
997  INTEGER :: is, ie, js, je
998  INTEGER :: isd, ied, jsd, jed
999  REAL :: dt2
1000  REAL(kind=8) :: t1, t2
1001  INTEGER :: status
1002  REAL :: rf(npz)
1003  REAL :: gz(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1004  REAL :: gz_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1005  REAL :: pkc(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1006  REAL :: pkc_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1007  REAL :: ptc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1008  REAL :: ptc_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1009  REAL :: crx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1010  REAL :: crx_ad(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1011  REAL :: xfx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1012  REAL :: xfx_ad(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1013  REAL :: cry(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1014  REAL :: cry_ad(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1015  REAL :: yfx(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1016  REAL :: yfx_ad(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1017  REAL :: divgd(bd%isd:bd%ied+1, bd%jsd:bd%jed+1, npz)
1018  REAL :: divgd_ad(bd%isd:bd%ied+1, bd%jsd:bd%jed+1, npz)
1019  REAL :: delpc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1020  REAL :: delpc_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1021  REAL :: ut(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1022  REAL :: ut_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1023  REAL :: vt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1024  REAL :: vt_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1025  REAL :: zh(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1026  REAL :: zh_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1027  REAL :: pk3(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1028  REAL :: pk3_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1029  REAL :: du(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
1030  REAL :: du_ad(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
1031  REAL :: dv(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
1032  REAL :: dv_ad(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
1033  INTRINSIC any
1034  INTRINSIC log
1035  INTRINSIC exp
1036  INTRINSIC abs
1037  INTRINSIC real
1038  INTRINSIC cos
1039  REAL :: abs0
1040  REAL :: abs1
1041  INTEGER :: arg1
1042  LOGICAL :: arg10
1043  REAL*8 :: arg11
1044  REAL, DIMENSION(bd%is:bd%ie, bd%js:bd%je) :: arg12
1045  REAL :: result1
1046  REAL :: result1_ad
1047  REAL :: temp
1048  REAL :: temp0
1049  REAL :: temp1
1050  REAL :: temp2
1051  REAL :: temp_ad
1052  REAL :: temp_ad0
1053  REAL :: temp3
1054  REAL :: temp4
1055  REAL :: temp5
1056  REAL :: temp_ad1
1057  REAL :: temp_ad2
1058  REAL :: temp6
1059  REAL :: temp7
1060  REAL :: temp_ad3
1061  REAL :: temp_ad4
1062  REAL :: temp_ad5
1063  REAL :: temp_ad6
1064  INTEGER :: branch
1065 
1066  ws = 0.0
1067  te_2d = 0.0
1068  teq = 0.0
1069  ps2 = 0.0
1070  m_fac = 0.0
1071  pfull = 0.0
1072  cvm = 0.0
1073  dp1 = 0.0
1074  dtdt_m = 0.0
1075  cappa = 0.0
1076  akap = 0.0
1077  rdg = 0.0
1078  ph1 = 0.0
1079  ph2 = 0.0
1080  mdt = 0.0
1081  gam = 0.0
1082  amdt = 0.0
1083  u0 = 0.0
1084  kord_tracer = 0
1085  kord_mt = 0
1086  kord_wz = 0
1087  kord_tm = 0
1088  kord_tracer_pert = 0
1089  kord_mt_pert = 0
1090  kord_wz_pert = 0
1091  kord_tm_pert = 0
1092  iq = 0
1093  n_map = 0
1094  nq = 0
1095  nwat = 0
1096  k_split = 0
1097  sphum = 0
1098  is = 0
1099  ie = 0
1100  js = 0
1101  je = 0
1102  isd = 0
1103  ied = 0
1104  jsd = 0
1105  jed = 0
1106  dt2 = 0.0
1107  t1 = 0.0_8
1108  t2 = 0.0_8
1109  rf = 0.0
1110  gz = 0.0
1111  pkc = 0.0
1112  ptc = 0.0
1113  crx = 0.0
1114  xfx = 0.0
1115  cry = 0.0
1116  yfx = 0.0
1117  divgd = 0.0
1118  delpc = 0.0
1119  ut = 0.0
1120  vt = 0.0
1121  zh = 0.0
1122  pk3 = 0.0
1123  du = 0.0
1124  dv = 0.0
1125  abs0 = 0.0
1126  abs1 = 0.0
1127  arg1 = 0
1128  arg11 = 0.0_r_grid
1129  arg12 = 0.0
1130  result1 = 0.0
1131  branch = 0
1132 
1133  CALL poprealarray(pk3, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1))
1134  CALL poprealarray(xfx, (bd%ie-bd%is+2)*(bd%jed-bd%jsd+1)*npz)
1135  CALL poprealarray(ws, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
1136  CALL poprealarray(gz, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1))
1137  CALL poprealarray(dp1, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1138  CALL poprealarray(ptc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1139  CALL poprealarray(pfull, npz)
1140  CALL popinteger(jsd)
1141  CALL poprealarray(rf, npz)
1142  CALL popinteger(ied)
1143  CALL poprealarray(ut, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1144  CALL poprealarray(pkc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*(npz+1))
1145  CALL poprealarray(delpc, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1146  CALL popinteger(kord_tracer_pert, ncnst)
1147  CALL poprealarray(m_fac, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
1148  CALL poprealarray(yfx, (bd%ied-bd%isd+1)*(bd%je-bd%js+2)*npz)
1149  CALL popinteger(isd)
1150  CALL poprealarray(te_2d, (bd%ie-bd%is+1)*(bd%je-bd%js+1))
1151  CALL poprealarray(vt, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1152  CALL poprealarray(result1)
1153  CALL popinteger(sphum)
1154  CALL poprealarray(amdt)
1155  CALL poprealarray(crx, (bd%ie-bd%is+2)*(bd%jed-bd%jsd+1)*npz)
1156  CALL poprealarray(cry, (bd%ied-bd%isd+1)*(bd%je-bd%js+2)*npz)
1157  CALL popinteger(jed)
1158  js = bd%js
1159  ie = bd%ie
1160  is = bd%is
1161  je = bd%je
1162  CALL popcontrol(2,branch)
1163  IF (branch .EQ. 0) THEN
1164  u0_ad = 0.0
1165  DO k=npz,1,-1
1166  DO j=je,js,-1
1167  DO i=ie+1,is,-1
1168  u0_ad = u0_ad + gridstruct%l2c_v(i, j)*v_ad(i, j, k)
1169  END DO
1170  END DO
1171  DO j=je+1,js,-1
1172  DO i=ie,is,-1
1173  u0_ad = u0_ad + gridstruct%l2c_u(i, j)*u_ad(i, j, k)
1174  END DO
1175  END DO
1176  END DO
1177  ELSE IF (branch .EQ. 1) THEN
1178  u0_ad = 0.0
1179  ELSE
1180  ps_ad = 0.0
1181  teq_ad = 0.0
1182  ps2_ad = 0.0
1183  m_fac_ad = 0.0
1184  te_2d_ad = 0.0
1185  GOTO 100
1186  END IF
1187  CALL popcontrol(1,branch)
1188  IF (branch .EQ. 0) THEN
1189  temp_ad6 = -(radius*u0_ad/result1)
1190  amdt_ad = temp_ad6
1191  result1_ad = -(amdt*temp_ad6/result1)
1192  CALL g_sum_adm(domain, m_fac, m_fac_ad, is, ie, js, je, ng, &
1193 & gridstruct%area_64, 0, reproduce=.true., g_sum_ad=&
1194 & result1_ad)
1195  CALL g_sum_adm(domain, te_2d, te_2d_ad, is, ie, js, je, ng, &
1196 & gridstruct%area_64, 0, reproduce=.true., g_sum_ad=amdt_ad&
1197 & )
1198  ELSE
1199  m_fac_ad = 0.0
1200  te_2d_ad = 0.0
1201  END IF
1202  dt2 = 0.5*bdt
1203  ps_ad = 0.0
1204  teq_ad = 0.0
1205  ps2_ad = 0.0
1206  DO j=je,js,-1
1207  DO i=ie,is,-1
1208  temp_ad5 = dt2*idiag%zxg(i, j)*te_2d_ad(i, j)
1209  teq_ad(i, j) = teq_ad(i, j) - te_2d_ad(i, j)
1210  ps2_ad(i, j) = ps2_ad(i, j) + temp_ad5
1211  ps_ad(i, j) = ps_ad(i, j) + temp_ad5
1212  END DO
1213  END DO
1214  100 CALL popcontrol(1,branch)
1215  IF (branch .EQ. 0) THEN
1216  jsd = bd%jsd
1217  ied = bd%ied
1218  isd = bd%isd
1219  jed = bd%jed
1220  CALL compute_aam_bwd(npz, is, ie, js, je, isd, ied, jsd, jed, &
1221 & gridstruct, bd, ptop, ua, ua_ad, va, va_ad, u, u_ad&
1222 & , v, v_ad, delp, delp_ad, te_2d, te_2d_ad, ps, &
1223 & ps_ad, m_fac, m_fac_ad)
1224  END IF
1225  CALL popcontrol(1,branch)
1226  nq = nq_tot - flagstruct%dnats
1227  k_split = flagstruct%k_split
1228  akap = kappa
1229  uc_ad = 0.0
1230  omga_ad = 0.0
1231  vc_ad = 0.0
1232  pk_ad = 0.0
1233  pk3_ad = 0.0
1234  xfx_ad = 0.0
1235  ws_ad = 0.0
1236  gz_ad = 0.0
1237  du_ad = 0.0
1238  psx_ad = 0.0_8
1239  dv_ad = 0.0
1240  dp1_ad = 0.0
1241  ptc_ad = 0.0
1242  ut_ad = 0.0
1243  divgd_ad = 0.0
1244  pkc_ad = 0.0
1245  delpc_ad = 0.0
1246  yfx_ad = 0.0
1247  vt_ad = 0.0
1248  zh_ad = 0.0
1249  dpx_ad = 0.0_8
1250  crx_ad = 0.0
1251  cry_ad = 0.0
1252  DO n_map=k_split,1,-1
1253  CALL popcontrol(2,branch)
1254  IF (branch .LT. 2) THEN
1255  IF (branch .EQ. 0) GOTO 110
1256  ELSE
1257  IF (branch .NE. 2) THEN
1258  arg11 = 0.18*gridstruct%da_min
1259  CALL del2_cubed_bwd(omga, omga_ad, arg11, gridstruct, domain, &
1260 & npx, npy, npz, flagstruct%nf_omega, bd)
1261  END IF
1262  CALL popcontrol(1,branch)
1263  IF (branch .EQ. 0) THEN
1264  DO k=npz,1,-1
1265  DO j=je,js,-1
1266  DO i=ie,is,-1
1267  CALL poprealarray(omga(i, j, k))
1268  temp_ad4 = omga_ad(i, j, k)/delz(i, j, k)
1269  delp_ad(i, j, k) = delp_ad(i, j, k) + w(i, j, k)*&
1270 & temp_ad4
1271  w_ad(i, j, k) = w_ad(i, j, k) + delp(i, j, k)*temp_ad4
1272  delz_ad(i, j, k) = delz_ad(i, j, k) - delp(i, j, k)*w(i&
1273 & , j, k)*temp_ad4/delz(i, j, k)
1274  omga_ad(i, j, k) = 0.0
1275  END DO
1276  END DO
1277  END DO
1278  END IF
1279  END IF
1280  kord_mt_pert = flagstructp%kord_mt_pert
1281  kord_wz_pert = flagstructp%kord_wz_pert
1282  CALL lagrangian_to_eulerian_bwd(last_step, consv_te, ps, ps_ad, pe&
1283 & , pe_ad, delp, delp_ad, pkz, pkz_ad, pk&
1284 & , pk_ad, mdt, bdt, npz, is, ie, js, je, &
1285 & isd, ied, jsd, jed, nq, nwat, sphum, &
1286 & q_con, u, u_ad, v, v_ad, w, w_ad, delz, &
1287 & delz_ad, pt, pt_ad, q, q_ad, phis, zvir&
1288 & , cp_air, akap, cappa, kord_mt, kord_wz&
1289 & , kord_tracer, kord_tm, peln, peln_ad, &
1290 & te_2d, te_2d_ad, ng, ua, ua_ad, va, omga&
1291 & , omga_ad, dp1, dp1_ad, ws, ws_ad, fill&
1292 & , reproduce_sum, arg10, dtdt_m, ptop, ak&
1293 & , bk, pfull, flagstruct, gridstruct, &
1294 & domain, flagstruct%do_sat_adj, &
1295 & hydrostatic, hybrid_z, do_omega, &
1296 & flagstruct%adiabatic, do_adiabatic_init&
1297 & , mfx, mfy, flagstruct%remap_option, &
1298 & kord_mt_pert, kord_wz_pert, &
1299 & kord_tracer_pert, kord_tm_pert)
1300  DO iq=nq,1,-1
1301  CALL popcontrol(1,branch)
1302  CALL popinteger(kord_tracer_pert(iq))
1303  END DO
1304  110 CALL popcontrol(2,branch)
1305  IF (branch .LT. 2) THEN
1306  IF (branch .EQ. 0) THEN
1307  CALL tracer_2d_nested_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, &
1308 & mfy, mfy_ad, cx, cx_ad, cy, cy_ad, &
1309 & gridstruct, bd, domain, npx, npy, npz, nq&
1310 & , flagstruct%hord_tr, q_split, mdt, idiag%&
1311 & id_divg, i_pack(10), flagstruct%nord_tr, &
1312 & flagstruct%trdm2, k_split, neststruct, &
1313 & parent_grid, flagstructp%hord_tr_pert, &
1314 & flagstructp%nord_tr_pert, flagstructp%&
1315 & trdm2_pert, flagstructp%split_damp_tr)
1316  ELSE
1317  CALL tracer_2d_1l_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, mfy, &
1318 & mfy_ad, cx, cx_ad, cy, cy_ad, gridstruct, bd, &
1319 & domain, npx, npy, npz, nq, flagstruct%hord_tr&
1320 & , q_split, mdt, idiag%id_divg, i_pack(10), &
1321 & flagstruct%nord_tr, flagstruct%trdm2, &
1322 & flagstructp%hord_tr_pert, flagstructp%&
1323 & nord_tr_pert, flagstructp%trdm2_pert, &
1324 & flagstructp%split_damp_tr)
1325  END IF
1326  ELSE IF (branch .EQ. 2) THEN
1327  CALL tracer_2d_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, mfy, &
1328 & mfy_ad, cx, cx_ad, cy, cy_ad, gridstruct, bd, &
1329 & domain, npx, npy, npz, nq, flagstruct%hord_tr, &
1330 & q_split, mdt, idiag%id_divg, i_pack(10), flagstruct&
1331 & %nord_tr, flagstruct%trdm2, flagstructp%&
1332 & hord_tr_pert, flagstructp%nord_tr_pert, flagstructp&
1333 & %trdm2_pert, flagstructp%split_damp_tr)
1334  ELSE
1335  mfx_ad = 0.0
1336  mfy_ad = 0.0
1337  cx_ad = 0.0
1338  cy_ad = 0.0
1339  END IF
1340  CALL popcontrol(2,branch)
1341  IF (branch .EQ. 0) THEN
1342  DO j=je+1,js-1,-1
1343  DO i=ie+1,is-1,-1
1344  CALL poprealarray(pe(i, npz+1, j))
1345  psx_ad(i, j) = psx_ad(i, j) + pe_ad(i, npz+1, j)
1346  pe_ad(i, npz+1, j) = 0.0
1347  END DO
1348  END DO
1349  CALL mpp_update_domains_adm(psx, psx_ad, domain)
1350  DO j=je,js,-1
1351  DO i=ie,is,-1
1352  dpx_ad(i, j) = dpx_ad(i, j) + psx_ad(i, j)
1353  END DO
1354  END DO
1355  END IF
1356  CALL dyn_core_bwd(npx, npy, npz, ng, sphum, nq, mdt, n_split, zvir&
1357 & , cp_air, akap, cappa, grav, hydrostatic, u, u_ad, v, &
1358 & v_ad, w, w_ad, delz, delz_ad, pt, pt_ad, q, q_ad, delp&
1359 & , delp_ad, pe, pe_ad, pk, pk_ad, phis, ws, ws_ad, omga&
1360 & , omga_ad, ptop, pfull, ua, ua_ad, va, va_ad, uc, &
1361 & uc_ad, vc, vc_ad, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad&
1362 & , cy, cy_ad, pkz, pkz_ad, peln, peln_ad, q_con, ak, bk&
1363 & , dpx, dpx_ad, ks, gridstruct, flagstruct, flagstructp&
1364 & , neststruct, idiag, bd, domain, arg10, i_pack, &
1365 & last_step, gz, gz_ad, pkc, pkc_ad, ptc, ptc_ad, crx, &
1366 & crx_ad, xfx, xfx_ad, cry, cry_ad, yfx, yfx_ad, divgd, &
1367 & divgd_ad, delpc, delpc_ad, ut, ut_ad, vt, vt_ad, zh, &
1368 & zh_ad, pk3, pk3_ad, du, du_ad, dv, dv_ad, time_total)
1369  DO k=npz,1,-1
1370  DO j=jed,jsd,-1
1371  DO i=ied,isd,-1
1372  CALL poprealarray(dp1(i, j, k))
1373  delp_ad(i, j, k) = delp_ad(i, j, k) + dp1_ad(i, j, k)
1374  dp1_ad(i, j, k) = 0.0
1375  END DO
1376  END DO
1377  END DO
1378  CALL poprealarray(u, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+2)*npz)
1379  CALL poprealarray(v, (bd%ied-bd%isd+2)*(bd%jed-bd%jsd+1)*npz)
1380  CALL start_group_halo_update_adm(i_pack(8), u, u_ad, v, v_ad, &
1381 & domain, gridtype=dgrid_ne)
1382  CALL poprealarray(pt, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1383  CALL start_group_halo_update_adm(i_pack(1), pt, pt_ad, domain, &
1384 & complete=.true.)
1385  CALL poprealarray(delp, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
1386  CALL start_group_halo_update_adm(i_pack(1), delp, delp_ad, domain&
1387 & , complete=.true.)
1388  END DO
1389  CALL popcontrol(1,branch)
1390  IF (branch .EQ. 0) THEN
1391  DO j=je,js,-1
1392  DO i=ie,is,-1
1393  pe_ad(i, npz+1, j) = pe_ad(i, npz+1, j) + psx_ad(i, j)
1394  psx_ad(i, j) = 0.0_8
1395  END DO
1396  END DO
1397  END IF
1398  CALL popcontrol(1,branch)
1399  IF (branch .EQ. 0) THEN
1400  DO k=npz,1,-1
1401  CALL popcontrol(1,branch)
1402  IF (branch .NE. 0) THEN
1403  DO j=je,js,-1
1404  DO i=ie,is,-1
1405  CALL poprealarray(q(i, j, k, theta_d))
1406  pt_ad(i, j, k) = pt_ad(i, j, k) + q_ad(i, j, k, theta_d)
1407  q_ad(i, j, k, theta_d) = 0.0
1408  END DO
1409  END DO
1410  END IF
1411  DO j=je,js,-1
1412  DO i=ie,is,-1
1413  CALL poprealarray(pt(i, j, k))
1414  temp_ad2 = pt_ad(i, j, k)/pkz(i, j, k)
1415  pkz_ad(i, j, k) = pkz_ad(i, j, k) - pt(i, j, k)*temp_ad2/pkz&
1416 & (i, j, k)
1417  pt_ad(i, j, k) = temp_ad2
1418  END DO
1419  END DO
1420  END DO
1421  ELSE
1422  DO k=npz,1,-1
1423  DO j=je,js,-1
1424  DO i=ie,is,-1
1425  CALL poprealarray(pt(i, j, k))
1426  temp7 = pkz(i, j, k)
1427  temp6 = pt(i, j, k)/temp7
1428  temp_ad3 = (dp1(i, j, k)+1.)*pt_ad(i, j, k)/temp7
1429  dp1_ad(i, j, k) = dp1_ad(i, j, k) + temp6*pt_ad(i, j, k)
1430  pkz_ad(i, j, k) = pkz_ad(i, j, k) - temp6*temp_ad3
1431  pt_ad(i, j, k) = temp_ad3
1432  END DO
1433  END DO
1434  END DO
1435  END IF
1436  CALL popcontrol(2,branch)
1437  IF (branch .EQ. 0) THEN
1438  CALL rayleigh_super_bwd(abs0, npx, npy, npz, ks, pfull, phis, &
1439 & flagstruct%tau, u, u_ad, v, v_ad, w, w_ad, pt, &
1440 & pt_ad, ua, ua_ad, va, va_ad, delz, gridstruct%&
1441 & agrid, cp_air, rdgas, ptop, hydrostatic, arg10, &
1442 & flagstruct%rf_cutoff, rf, gridstruct, domain, bd&
1443 & )
1444  ELSE IF (branch .EQ. 1) THEN
1445  CALL rayleigh_friction_bwd(abs1, npx, npy, npz, ks, pfull, &
1446 & flagstruct%tau, u, u_ad, v, v_ad, w, w_ad, pt&
1447 & , pt_ad, ua, ua_ad, va, va_ad, delz, delz_ad&
1448 & , cp_air, rdgas, ptop, hydrostatic, .true., &
1449 & flagstruct%rf_cutoff, rf, gridstruct, domain&
1450 & , bd)
1451  END IF
1452  CALL popcontrol(1,branch)
1453  IF (branch .EQ. 0) CALL compute_aam_bwd(npz, is, ie, js, je, isd, &
1454 & ied, jsd, jed, gridstruct, bd, &
1455 & ptop, ua, ua_ad, va, va_ad, u, &
1456 & u_ad, v, v_ad, delp, delp_ad, teq&
1457 & , teq_ad, ps2, ps2_ad, m_fac, &
1458 & m_fac_ad)
1459  CALL popcontrol(1,branch)
1460  IF (branch .EQ. 0) CALL compute_total_energy_bwd(is, ie, js, je, isd&
1461 & , ied, jsd, jed, npz, u, &
1462 & u_ad, v, v_ad, w, w_ad, &
1463 & delz, delz_ad, pt, pt_ad&
1464 & , delp, delp_ad, q, q_ad&
1465 & , dp1, dp1_ad, pe, pe_ad&
1466 & , peln, peln_ad, phis, &
1467 & gridstruct%rsin2, &
1468 & gridstruct%cosa_s, zvir, &
1469 & cp_air, rdgas, hlv, te_2d&
1470 & , te_2d_ad, ua, va, teq, &
1471 & teq_ad, flagstruct%&
1472 & moist_phys, nwat, sphum, &
1473 & liq_wat, rainwat, ice_wat&
1474 & , snowwat, graupel, &
1475 & hydrostatic, idiag%id_te)
1476  CALL popcontrol(1,branch)
1477  rdg = -(rdgas*agrav)
1478  CALL popcontrol(1,branch)
1479  IF (branch .EQ. 0) THEN
1480  DO k=npz,1,-1
1481  DO j=je,js,-1
1482  DO i=ie,is,-1
1483  q_ad(i, j, k, sphum) = q_ad(i, j, k, sphum) + zvir*dp1_ad(i&
1484 & , j, k)
1485  dp1_ad(i, j, k) = 0.0
1486  END DO
1487  END DO
1488  END DO
1489  ELSE
1490  DO k=npz,1,-1
1491  CALL popcontrol(1,branch)
1492  IF (branch .EQ. 0) THEN
1493  DO j=je,js,-1
1494  DO i=ie,is,-1
1495  CALL poprealarray(pkz(i, j, k))
1496  temp5 = delz(i, j, k)
1497  temp4 = delp(i, j, k)*pt(i, j, k)
1498  temp3 = temp4/temp5
1499  temp_ad1 = kappa*exp(kappa*log(rdg*temp3))*pkz_ad(i, j, k)&
1500 & /(temp3*temp5)
1501  delp_ad(i, j, k) = delp_ad(i, j, k) + pt(i, j, k)*temp_ad1
1502  pt_ad(i, j, k) = pt_ad(i, j, k) + delp(i, j, k)*temp_ad1
1503  delz_ad(i, j, k) = delz_ad(i, j, k) - temp3*temp_ad1
1504  pkz_ad(i, j, k) = 0.0
1505  dp1_ad(i, j, k) = 0.0
1506  END DO
1507  END DO
1508  ELSE
1509  DO j=je,js,-1
1510  DO i=ie,is,-1
1511  CALL poprealarray(pkz(i, j, k))
1512  temp2 = delz(i, j, k)
1513  temp = (dp1(i, j, k)+1.)/temp2
1514  temp1 = delp(i, j, k)*pt(i, j, k)
1515  temp0 = rdg*temp1*temp
1516  temp_ad = kappa*exp(kappa*log(temp0))*rdg*pkz_ad(i, j, k)/&
1517 & temp0
1518  temp_ad0 = temp1*temp_ad/temp2
1519  delp_ad(i, j, k) = delp_ad(i, j, k) + temp*pt(i, j, k)*&
1520 & temp_ad
1521  pt_ad(i, j, k) = pt_ad(i, j, k) + temp*delp(i, j, k)*&
1522 & temp_ad
1523  dp1_ad(i, j, k) = dp1_ad(i, j, k) + temp_ad0
1524  delz_ad(i, j, k) = delz_ad(i, j, k) - temp*temp_ad0
1525  pkz_ad(i, j, k) = 0.0
1526  q_ad(i, j, k, sphum) = q_ad(i, j, k, sphum) + zvir*dp1_ad(&
1527 & i, j, k)
1528  dp1_ad(i, j, k) = 0.0
1529  END DO
1530  END DO
1531  END IF
1532  END DO
1533  END IF
1534  CALL popcontrol(1,branch)
1535  CALL popcontrol(1,branch)
1536  CALL popcontrol(2,branch)
1537  IF (branch .EQ. 0) THEN
1538  CALL nested_grid_bc_apply_intt_adm(pt, pt_ad, 0, 0, npx, npy, npz&
1539 & , bd, 1., 1., neststruct%pt_bc, &
1540 & neststruct%nestbctype)
1541  ELSE IF (branch .NE. 1) THEN
1542  GOTO 120
1543  END IF
1544  CALL setup_nested_grid_bcs_adm(npx, npy, npz, zvir, ncnst, u, u_ad, &
1545 & v, v_ad, w, pt, delp, delz, q, uc, uc_ad, &
1546 & vc, vc_ad, pkz, neststruct%nested, &
1547 & flagstruct%inline_q, flagstruct%make_nh, ng&
1548 & , gridstruct, flagstruct, neststruct, &
1549 & neststruct%nest_timestep, neststruct%&
1550 & tracer_nest_timestep, domain, bd, nwat)
1551  120 CONTINUE
1552  END SUBROUTINE fv_dynamics_bwd
1553 !-----------------------------------------------------------------------
1554 ! fv_dynamics :: FV dynamical core driver
1555 !-----------------------------------------------------------------------
1556  SUBROUTINE fv_dynamics(npx, npy, npz, nq_tot, ng, bdt, consv_te, fill&
1557 & , reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, &
1558 & q_split, u, v, w, delz, hydrostatic, pt, delp, q, ps, pe, pk, peln, &
1559 & pkz, phis, q_con, omga, ua, va, uc, vc, ak, bk, mfx, mfy, cx, cy, &
1560 & ze0, hybrid_z, gridstruct, flagstruct, flagstructp, neststruct, &
1561 & idiag, bd, parent_grid, domain, time_total)
1562  IMPLICIT NONE
1563 ! Large time-step
1564  REAL, INTENT(IN) :: bdt
1565  REAL, INTENT(IN) :: consv_te
1566  REAL, INTENT(IN) :: kappa, cp_air
1567  REAL, INTENT(IN) :: zvir, ptop
1568  REAL, INTENT(IN), OPTIONAL :: time_total
1569  INTEGER, INTENT(IN) :: npx
1570  INTEGER, INTENT(IN) :: npy
1571  INTEGER, INTENT(IN) :: npz
1572 ! transported tracers
1573  INTEGER, INTENT(IN) :: nq_tot
1574  INTEGER, INTENT(IN) :: ng
1575  INTEGER, INTENT(IN) :: ks
1576  INTEGER, INTENT(IN) :: ncnst
1577 ! small-step horizontal dynamics
1578  INTEGER, INTENT(IN) :: n_split
1579 ! tracer
1580  INTEGER, INTENT(IN) :: q_split
1581  LOGICAL, INTENT(IN) :: fill
1582  LOGICAL, INTENT(IN) :: reproduce_sum
1583  LOGICAL, INTENT(IN) :: hydrostatic
1584 ! Using hybrid_z for remapping
1585  LOGICAL, INTENT(IN) :: hybrid_z
1586  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
1587 ! D grid zonal wind (m/s)
1588  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz), INTENT(INOUT) &
1589 & :: u
1590 ! D grid meridional wind (m/s)
1591  REAL, DIMENSION(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz), INTENT(INOUT) &
1592 & :: v
1593 ! W (m/s)
1594  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1595 ! temperature (K)
1596  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1597 ! pressure thickness (pascal)
1598  REAL, INTENT(INOUT) :: delp(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1599 ! specific humidity and constituents
1600  REAL, INTENT(INOUT) :: q(bd%isd:bd%ied, bd%jsd:bd%jed, npz, ncnst)
1601 ! delta-height (m); non-hydrostatic only
1602  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1603 ! height at edges (m); non-hydrostatic
1604  REAL, INTENT(INOUT) :: ze0(bd%is:bd%is, bd%js:bd%js, 1)
1605 ! ze0 no longer used
1606 !-----------------------------------------------------------------------
1607 ! Auxilliary pressure arrays:
1608 ! The 5 vars below can be re-computed from delp and ptop.
1609 !-----------------------------------------------------------------------
1610 ! dyn_aux:
1611 ! Surface pressure (pascal)
1612  REAL, INTENT(INOUT) :: ps(bd%isd:bd%ied, bd%jsd:bd%jed)
1613 ! edge pressure (pascal)
1614  REAL, INTENT(INOUT) :: pe(bd%is-1:bd%ie+1, npz+1, bd%js-1:bd%je+1)
1615 ! pe**kappa
1616  REAL, INTENT(INOUT) :: pk(bd%is:bd%ie, bd%js:bd%je, npz+1)
1617 ! ln(pe)
1618  REAL, INTENT(INOUT) :: peln(bd%is:bd%ie, npz+1, bd%js:bd%je)
1619 ! finite-volume mean pk
1620  REAL, INTENT(INOUT) :: pkz(bd%is:bd%ie, bd%js:bd%je, npz)
1621  REAL, INTENT(INOUT) :: q_con(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1622 !-----------------------------------------------------------------------
1623 ! Others:
1624 !-----------------------------------------------------------------------
1625 ! Surface geopotential (g*Z_surf)
1626  REAL, INTENT(INOUT) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
1627 ! Vertical pressure velocity (pa/s)
1628  REAL, INTENT(INOUT) :: omga(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1629 ! (uc,vc) mostly used as the C grid winds
1630  REAL, INTENT(INOUT) :: uc(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
1631  REAL, INTENT(INOUT) :: vc(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
1632  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(INOUT) ::&
1633 & ua, va
1634  REAL, DIMENSION(npz+1), INTENT(IN) :: ak, bk
1635 ! Accumulated Mass flux arrays: the "Flux Capacitor"
1636  REAL, INTENT(INOUT) :: mfx(bd%is:bd%ie+1, bd%js:bd%je, npz)
1637  REAL, INTENT(INOUT) :: mfy(bd%is:bd%ie, bd%js:bd%je+1, npz)
1638 ! Accumulated Courant number arrays
1639  REAL, INTENT(INOUT) :: cx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1640  REAL, INTENT(INOUT) :: cy(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1641  TYPE(fv_grid_type), INTENT(INOUT), TARGET :: gridstruct
1642  TYPE(fv_flags_type), INTENT(INOUT) :: flagstruct
1643  TYPE(fv_flags_pert_type), INTENT(INOUT) :: flagstructp
1644  TYPE(fv_nest_type), INTENT(INOUT) :: neststruct
1645  TYPE(domain2d), INTENT(INOUT) :: domain
1646  TYPE(fv_atmos_type), INTENT(INOUT) :: parent_grid
1647  TYPE(fv_diag_type), INTENT(IN) :: idiag
1648 ! Local Arrays
1649  REAL :: ws(bd%is:bd%ie, bd%js:bd%je)
1650  REAL :: te_2d(bd%is:bd%ie, bd%js:bd%je)
1651  REAL :: teq(bd%is:bd%ie, bd%js:bd%je)
1652  REAL :: ps2(bd%isd:bd%ied, bd%jsd:bd%jed)
1653  REAL :: m_fac(bd%is:bd%ie, bd%js:bd%je)
1654  REAL :: pfull(npz)
1655  REAL, DIMENSION(bd%is:bd%ie) :: cvm
1656  REAL :: dp1(bd%isd:bd%ied, bd%jsd:bd%jed, npz), dtdt_m(bd%is:bd%ie, &
1657 & bd%js:bd%je, npz), cappa(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1658  REAL(kind=8) :: psx(bd%isd:bd%ied, bd%jsd:bd%jed)
1659  REAL(kind=8) :: dpx(bd%is:bd%ie, bd%js:bd%je)
1660  REAL :: akap, rdg, ph1, ph2, mdt, gam, amdt, u0
1661  INTEGER :: kord_tracer(ncnst), kord_mt, kord_wz, kord_tm
1662  INTEGER :: kord_tracer_pert(ncnst), kord_mt_pert, kord_wz_pert, &
1663 & kord_tm_pert
1664  INTEGER :: i, j, k, n, iq, n_map, nq, nwat, k_split
1665 ! GFDL physics
1666  INTEGER :: sphum
1667  INTEGER, SAVE :: liq_wat=-999
1668  INTEGER, SAVE :: ice_wat=-999
1669  INTEGER, SAVE :: rainwat=-999
1670  INTEGER, SAVE :: snowwat=-999
1671  INTEGER, SAVE :: graupel=-999
1672  INTEGER, SAVE :: cld_amt=-999
1673  INTEGER, SAVE :: theta_d=-999
1674  LOGICAL :: used, last_step, do_omega
1675  INTEGER, PARAMETER :: max_packs=12
1676  TYPE(group_halo_update_type), SAVE :: i_pack(max_packs)
1677  INTEGER :: is, ie, js, je
1678  INTEGER :: isd, ied, jsd, jed
1679  REAL :: dt2
1680  REAL(kind=8) :: t1, t2
1681  INTEGER :: status
1682  REAL :: rf(npz)
1683  REAL :: gz(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1684  REAL :: pkc(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1685  REAL :: ptc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1686  REAL :: crx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1687  REAL :: xfx(bd%is:bd%ie+1, bd%jsd:bd%jed, npz)
1688  REAL :: cry(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1689  REAL :: yfx(bd%isd:bd%ied, bd%js:bd%je+1, npz)
1690  REAL :: divgd(bd%isd:bd%ied+1, bd%jsd:bd%jed+1, npz)
1691  REAL :: delpc(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1692  REAL :: ut(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1693  REAL :: vt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
1694  REAL :: zh(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1695  REAL :: pk3(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1)
1696  REAL :: du(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
1697  REAL :: dv(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
1698  INTRINSIC any
1699  INTRINSIC log
1700  INTRINSIC exp
1701  INTRINSIC abs
1702  INTRINSIC real
1703  INTRINSIC cos
1704  REAL :: abs0
1705  REAL :: abs1
1706  INTEGER :: arg1
1707  LOGICAL :: arg10
1708  REAL*8 :: arg11
1709  REAL, DIMENSION(bd%is:bd%ie, bd%js:bd%je) :: arg12
1710  REAL :: result1
1711  gz = 0.0
1712  pkc = 0.0
1713  ptc = 0.0
1714  crx = 0.0
1715  xfx = 0.0
1716  cry = 0.0
1717  yfx = 0.0
1718  divgd = 0.0
1719  delpc = 0.0
1720  ut = 0.0
1721  vt = 0.0
1722  zh = 0.0
1723  pk3 = 0.0
1724  du = 0.0
1725  dv = 0.0
1726  is = bd%is
1727  ie = bd%ie
1728  js = bd%js
1729  je = bd%je
1730  isd = bd%isd
1731  ied = bd%ied
1732  jsd = bd%jsd
1733  jed = bd%jed
1734 ! dyn_timer = 0
1735 ! comm_timer = 0
1736 ! cv_air = cp_air - rdgas
1737  agrav = 1./grav
1738  dt2 = 0.5*bdt
1739  k_split = flagstruct%k_split
1740  nwat = flagstruct%nwat
1741  nq = nq_tot - flagstruct%dnats
1742  rdg = -(rdgas*agrav)
1743 !allocate ( dp1(isd:ied, jsd:jed, 1:npz) )
1744 ! Begin Dynamics timer for GEOS history processing
1745 !t1 = MPI_Wtime(status)
1746  t1 = 0.0
1747  t2 = 0.0
1748 !allocate ( cappa(isd:isd,jsd:jsd,1) )
1749  cappa = 0.
1750 !We call this BEFORE converting pt to virtual potential temperature,
1751 !since we interpolate on (regular) temperature rather than theta.
1752  IF (gridstruct%nested .OR. any(neststruct%child_grids)) THEN
1753  CALL timing_on('NEST_BCs')
1754  CALL setup_nested_grid_bcs(npx, npy, npz, zvir, ncnst, u, v, w, pt&
1755 & , delp, delz, q, uc, vc, pkz, neststruct%&
1756 & nested, flagstruct%inline_q, flagstruct%&
1757 & make_nh, ng, gridstruct, flagstruct, &
1758 & neststruct, neststruct%nest_timestep, &
1759 & neststruct%tracer_nest_timestep, domain, bd, &
1760 & nwat)
1761  IF (gridstruct%nested) CALL nested_grid_bc_apply_intt(pt, 0, 0, &
1762 & npx, npy, npz, bd&
1763 & , 1., 1., &
1764 & neststruct%pt_bc, &
1765 & neststruct%&
1766 & nestbctype)
1767 !Correct halo values have now been set up for BCs; we can go ahead and apply them too...
1768  CALL timing_off('NEST_BCs')
1769  END IF
1770  IF (flagstruct%no_dycore) THEN
1771  IF (nwat .EQ. 2 .AND. (.NOT.hydrostatic)) sphum = get_tracer_index&
1772 & (model_atmos, 'sphum')
1773  END IF
1774 !goto 911
1775  IF (fpp%fpp_mapl_mode) THEN
1776  SELECT CASE (nwat)
1777  CASE (0)
1778  sphum = 1
1779 ! to cause trouble if (mis)used
1780  cld_amt = -1
1781  CASE (1)
1782  sphum = 1
1783 ! to cause trouble if (mis)used
1784  liq_wat = -1
1785 ! to cause trouble if (mis)used
1786  ice_wat = -1
1787 ! to cause trouble if (mis)used
1788  rainwat = -1
1789 ! to cause trouble if (mis)used
1790  snowwat = -1
1791 ! to cause trouble if (mis)used
1792  graupel = -1
1793 ! to cause trouble if (mis)used
1794  cld_amt = -1
1795 ! to cause trouble if (mis)used
1796  theta_d = -1
1797  CASE (3)
1798  sphum = 1
1799  liq_wat = 2
1800  ice_wat = 3
1801 ! to cause trouble if (mis)used
1802  rainwat = -1
1803 ! to cause trouble if (mis)used
1804  snowwat = -1
1805 ! to cause trouble if (mis)used
1806  graupel = -1
1807 ! to cause trouble if (mis)used
1808  cld_amt = -1
1809 ! to cause trouble if (mis)used
1810  theta_d = -1
1811  END SELECT
1812  ELSE
1813  IF (nwat .EQ. 0) THEN
1814  sphum = 1
1815 ! to cause trouble if (mis)used
1816  cld_amt = -1
1817  ELSE
1818  sphum = get_tracer_index(model_atmos, 'sphum')
1819  liq_wat = get_tracer_index(model_atmos, 'liq_wat')
1820  ice_wat = get_tracer_index(model_atmos, 'ice_wat')
1821  rainwat = get_tracer_index(model_atmos, 'rainwat')
1822  snowwat = get_tracer_index(model_atmos, 'snowwat')
1823  graupel = get_tracer_index(model_atmos, 'graupel')
1824  cld_amt = get_tracer_index(model_atmos, 'cld_amt')
1825  END IF
1826  theta_d = get_tracer_index(model_atmos, 'theta_d')
1827  END IF
1828  akap = kappa
1829 !$OMP parallel do default(none) shared(npz,ak,bk,flagstruct,pfull) &
1830 !$OMP private(ph1, ph2)
1831  DO k=1,npz
1832  ph1 = ak(k) + bk(k)*flagstruct%p_ref
1833  ph2 = ak(k+1) + bk(k+1)*flagstruct%p_ref
1834  pfull(k) = (ph2-ph1)/log(ph2/ph1)
1835  END DO
1836  IF (hydrostatic) THEN
1837 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,zvir,nwat,q,q_con,sphum,liq_wat, &
1838 !$OMP rainwat,ice_wat,snowwat,graupel) private(cvm)
1839  DO k=1,npz
1840  DO j=js,je
1841  DO i=is,ie
1842  dp1(i, j, k) = zvir*q(i, j, k, sphum)
1843  END DO
1844  END DO
1845  END DO
1846  ELSE
1847 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,zvir,q,q_con,sphum,liq_wat, &
1848 !$OMP rainwat,ice_wat,snowwat,graupel,pkz,flagstruct, &
1849 !$OMP cappa,kappa,rdg,delp,pt,delz,nwat) &
1850 !$OMP private(cvm)
1851  DO k=1,npz
1852  IF (flagstruct%moist_phys) THEN
1853  DO j=js,je
1854  DO i=is,ie
1855  dp1(i, j, k) = zvir*q(i, j, k, sphum)
1856  pkz(i, j, k) = exp(kappa*log(rdg*delp(i, j, k)*pt(i, j, k)&
1857 & *(1.+dp1(i, j, k))/delz(i, j, k)))
1858  END DO
1859  END DO
1860  ELSE
1861 ! Using dry pressure for the definition of the virtual potential temperature
1862 ! pkz(i,j,k) = exp( kappa*log(rdg*delp(i,j,k)*pt(i,j,k)* &
1863 ! (1.-q(i,j,k,sphum))/delz(i,j,k)) )
1864  DO j=js,je
1865  DO i=is,ie
1866  dp1(i, j, k) = 0.
1867  pkz(i, j, k) = exp(kappa*log(rdg*delp(i, j, k)*pt(i, j, k)&
1868 & /delz(i, j, k)))
1869  END DO
1870  END DO
1871  END IF
1872  END DO
1873  END IF
1874  IF (flagstruct%fv_debug) THEN
1875  CALL prt_mxm('PS', ps, is, ie, js, je, ng, 1, 0.01, gridstruct%&
1876 & area_64, domain)
1877  CALL prt_mxm('T_dyn_b', pt, is, ie, js, je, ng, npz, 1., &
1878 & gridstruct%area_64, domain)
1879  IF (.NOT.hydrostatic) CALL prt_mxm('delz', delz, is, ie, js, je, &
1880 & ng, npz, 1., gridstruct%area_64, &
1881 & domain)
1882  CALL prt_mxm('delp_b ', delp, is, ie, js, je, ng, npz, 0.01, &
1883 & gridstruct%area_64, domain)
1884  arg1 = npz + 1
1885  CALL prt_mxm('pk_b', pk, is, ie, js, je, 0, arg1, 1., gridstruct%&
1886 & area_64, domain)
1887  CALL prt_mxm('pkz_b', pkz, is, ie, js, je, 0, npz, 1., gridstruct%&
1888 & area_64, domain)
1889  END IF
1890 !---------------------
1891 ! Compute Total Energy
1892 !---------------------
1893  IF (consv_te .GT. 0. .AND. (.NOT.do_adiabatic_init)) THEN
1894  CALL compute_total_energy(is, ie, js, je, isd, ied, jsd, jed, npz&
1895 & , u, v, w, delz, pt, delp, q, dp1, pe, peln, &
1896 & phis, gridstruct%rsin2, gridstruct%cosa_s, &
1897 & zvir, cp_air, rdgas, hlv, te_2d, ua, va, teq, &
1898 & flagstruct%moist_phys, nwat, sphum, liq_wat, &
1899 & rainwat, ice_wat, snowwat, graupel, &
1900 & hydrostatic, idiag%id_te)
1901  IF (idiag%id_te .GT. 0) used = send_data(idiag%id_te, teq, fv_time&
1902 & )
1903 ! te_den=1.E-9*g_sum(teq, is, ie, js, je, ng, area, 0)/(grav*4.*pi*radius**2)
1904 ! if(is_master()) write(*,*) 'Total Energy Density (Giga J/m**2)=',te_den
1905  END IF
1906  IF ((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .AND. (.NOT.&
1907 & do_adiabatic_init)) CALL compute_aam(npz, is, ie, js, je, isd, &
1908 & ied, jsd, jed, gridstruct, bd, &
1909 & ptop, ua, va, u, v, delp, teq, &
1910 & ps2, m_fac)
1911  IF (flagstruct%tau .GT. 0.) THEN
1912  IF (gridstruct%grid_type .LT. 4) THEN
1913  IF (bdt .GE. 0.) THEN
1914  abs0 = bdt
1915  ELSE
1916  abs0 = -bdt
1917  END IF
1918  arg10 = .NOT.neststruct%nested
1919  CALL rayleigh_super(abs0, npx, npy, npz, ks, pfull, phis, &
1920 & flagstruct%tau, u, v, w, pt, ua, va, delz, &
1921 & gridstruct%agrid, cp_air, rdgas, ptop, hydrostatic&
1922 & , arg10, flagstruct%rf_cutoff, rf, gridstruct, &
1923 & domain, bd)
1924  ELSE
1925  IF (bdt .GE. 0.) THEN
1926  abs1 = bdt
1927  ELSE
1928  abs1 = -bdt
1929  END IF
1930  CALL rayleigh_friction(abs1, npx, npy, npz, ks, pfull, &
1931 & flagstruct%tau, u, v, w, pt, ua, va, delz, &
1932 & cp_air, rdgas, ptop, hydrostatic, .true., &
1933 & flagstruct%rf_cutoff, rf, gridstruct, domain, &
1934 & bd)
1935  END IF
1936  END IF
1937 ! Convert pt to virtual potential temperature on the first timestep
1938  IF (flagstruct%adiabatic) THEN
1939 !$OMP parallel do default(none) shared(theta_d,is,ie,js,je,npz,pt,pkz,q)
1940  DO k=1,npz
1941  DO j=js,je
1942  DO i=is,ie
1943  pt(i, j, k) = pt(i, j, k)/pkz(i, j, k)
1944  END DO
1945  END DO
1946  IF (theta_d .GT. 0) THEN
1947  DO j=js,je
1948  DO i=is,ie
1949  q(i, j, k, theta_d) = pt(i, j, k)
1950  END DO
1951  END DO
1952  END IF
1953  END DO
1954  ELSE
1955 !$OMP parallel do default(none) shared(is,ie,js,je,npz,pt,dp1,pkz,q_con)
1956  DO k=1,npz
1957  DO j=js,je
1958  DO i=is,ie
1959  pt(i, j, k) = pt(i, j, k)*(1.+dp1(i, j, k))/pkz(i, j, k)
1960  END DO
1961  END DO
1962  END DO
1963  END IF
1964  last_step = .false.
1965  mdt = bdt/REAL(k_split)
1966  IF (idiag%id_mdt .GT. 0 .AND. (.NOT.do_adiabatic_init)) THEN
1967 !allocate ( dtdt_m(is:ie,js:je,npz) )
1968 !$OMP parallel do default(none) shared(is,ie,js,je,npz,dtdt_m)
1969  DO k=1,npz
1970  DO j=js,je
1971  DO i=is,ie
1972  dtdt_m(i, j, k) = 0.
1973  END DO
1974  END DO
1975  END DO
1976  END IF
1977 !DryMassRoundoffControl
1978 !allocate(psx(isd:ied,jsd:jed),dpx(is:ie,js:je))
1979  IF (fpp%fpp_overload_r4) THEN
1980  DO j=js,je
1981  DO i=is,ie
1982  psx(i, j) = pe(i, npz+1, j)
1983  dpx(i, j) = 0.0
1984  END DO
1985  END DO
1986  END IF
1987  CALL timing_on('FV_DYN_LOOP')
1988 ! first level of time-split
1989  DO n_map=1,k_split
1990  CALL timing_on('COMM_TOTAL')
1991  CALL start_group_halo_update(i_pack(1), delp, domain, complete=&
1992 & .true.)
1993  CALL start_group_halo_update(i_pack(1), pt, domain, complete=&
1994 & .true.)
1995  CALL start_group_halo_update(i_pack(8), u, v, domain, gridtype=&
1996 & dgrid_ne)
1997  CALL timing_off('COMM_TOTAL')
1998 !$OMP parallel do default(none) shared(isd,ied,jsd,jed,npz,dp1,delp)
1999  DO k=1,npz
2000  DO j=jsd,jed
2001  DO i=isd,ied
2002  dp1(i, j, k) = delp(i, j, k)
2003  END DO
2004  END DO
2005  END DO
2006  IF (n_map .EQ. k_split) last_step = .true.
2007  CALL timing_on('DYN_CORE')
2008  arg10 = n_map .EQ. 1
2009  CALL dyn_core(npx, npy, npz, ng, sphum, nq, mdt, n_split, zvir, &
2010 & cp_air, akap, cappa, grav, hydrostatic, u, v, w, delz, pt&
2011 & , q, delp, pe, pk, phis, ws, omga, ptop, pfull, ua, va, uc&
2012 & , vc, mfx, mfy, cx, cy, pkz, peln, q_con, ak, bk, dpx, ks&
2013 & , gridstruct, flagstruct, flagstructp, neststruct, idiag, &
2014 & bd, domain, arg10, i_pack, last_step, gz, pkc, ptc, crx, &
2015 & xfx, cry, yfx, divgd, delpc, ut, vt, zh, pk3, du, dv, &
2016 & time_total)
2017  CALL timing_off('DYN_CORE')
2018 !DryMassRoundoffControl
2019  IF (last_step) THEN
2020  IF (fpp%fpp_overload_r4) THEN
2021  DO j=js,je
2022  DO i=is,ie
2023  psx(i, j) = psx(i, j) + dpx(i, j)
2024  END DO
2025  END DO
2026  CALL timing_on('COMM_TOTAL')
2027  CALL mpp_update_domains(psx, domain)
2028  CALL timing_off('COMM_TOTAL')
2029  DO j=js-1,je+1
2030  DO i=is-1,ie+1
2031  pe(i, npz+1, j) = psx(i, j)
2032  END DO
2033  END DO
2034  END IF
2035  END IF
2036 !deallocate(psx,dpx)
2037  IF (.NOT.flagstruct%inline_q .AND. nq .NE. 0) THEN
2038 !--------------------------------------------------------
2039 ! Perform large-time-step scalar transport using the accumulated CFL and
2040 ! mass fluxes
2041  CALL timing_on('tracer_2d')
2042 !!! CLEANUP: merge these two calls?
2043  IF (gridstruct%nested) THEN
2044  CALL tracer_2d_nested(q, dp1, mfx, mfy, cx, cy, gridstruct, bd&
2045 & , domain, npx, npy, npz, nq, flagstruct%&
2046 & hord_tr, q_split, mdt, idiag%id_divg, i_pack(&
2047 & 10), flagstruct%nord_tr, flagstruct%trdm2, &
2048 & k_split, neststruct, parent_grid, flagstructp%&
2049 & hord_tr_pert, flagstructp%nord_tr_pert, &
2050 & flagstructp%trdm2_pert, flagstructp%&
2051 & split_damp_tr)
2052  ELSE IF (flagstruct%z_tracer) THEN
2053  CALL tracer_2d_1l(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, &
2054 & domain, npx, npy, npz, nq, flagstruct%hord_tr, &
2055 & q_split, mdt, idiag%id_divg, i_pack(10), &
2056 & flagstruct%nord_tr, flagstruct%trdm2, flagstructp%&
2057 & hord_tr_pert, flagstructp%nord_tr_pert, &
2058 & flagstructp%trdm2_pert, flagstructp%split_damp_tr)
2059  ELSE
2060  CALL tracer_2d(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, &
2061 & domain, npx, npy, npz, nq, flagstruct%hord_tr, &
2062 & q_split, mdt, idiag%id_divg, i_pack(10), flagstruct%&
2063 & nord_tr, flagstruct%trdm2, flagstructp%hord_tr_pert, &
2064 & flagstructp%nord_tr_pert, flagstructp%trdm2_pert, &
2065 & flagstructp%split_damp_tr)
2066  END IF
2067  CALL timing_off('tracer_2d')
2068  IF (flagstruct%hord_tr .LT. 8 .AND. flagstruct%moist_phys) THEN
2069  CALL timing_on('Fill2D')
2070  IF (liq_wat .GT. 0) CALL fill2d(is, ie, js, je, ng, npz, q(isd&
2071 & :ied, jsd:jed, 1, liq_wat), delp, &
2072 & gridstruct%area, domain, neststruct%&
2073 & nested, npx, npy)
2074  IF (rainwat .GT. 0) CALL fill2d(is, ie, js, je, ng, npz, q(isd&
2075 & :ied, jsd:jed, 1, rainwat), delp, &
2076 & gridstruct%area, domain, neststruct%&
2077 & nested, npx, npy)
2078  IF (ice_wat .GT. 0) CALL fill2d(is, ie, js, je, ng, npz, q(isd&
2079 & :ied, jsd:jed, 1, ice_wat), delp, &
2080 & gridstruct%area, domain, neststruct%&
2081 & nested, npx, npy)
2082  IF (snowwat .GT. 0) CALL fill2d(is, ie, js, je, ng, npz, q(isd&
2083 & :ied, jsd:jed, 1, snowwat), delp, &
2084 & gridstruct%area, domain, neststruct%&
2085 & nested, npx, npy)
2086  IF (graupel .GT. 0) CALL fill2d(is, ie, js, je, ng, npz, q(isd&
2087 & :ied, jsd:jed, 1, graupel), delp, &
2088 & gridstruct%area, domain, neststruct%&
2089 & nested, npx, npy)
2090  CALL timing_off('Fill2D')
2091  END IF
2092  IF (last_step .AND. idiag%id_divg .GT. 0) THEN
2093  used = send_data(idiag%id_divg, dp1, fv_time)
2094  IF (flagstruct%fv_debug) CALL prt_mxm('divg', dp1, is, ie, js&
2095 & , je, 0, npz, 1., gridstruct%&
2096 & area_64, domain)
2097  END IF
2098  END IF
2099  IF (npz .GT. 4) THEN
2100 !------------------------------------------------------------------------
2101 ! Perform vertical remapping from Lagrangian control-volume to
2102 ! the Eulerian coordinate as specified by the routine set_eta.
2103 ! Note that this finite-volume dycore is otherwise independent of the vertical
2104 ! Eulerian coordinate.
2105 !------------------------------------------------------------------------
2106  DO iq=1,nq
2107  kord_tracer(iq) = flagstruct%kord_tr
2108 ! monotonic
2109  IF (iq .EQ. cld_amt) kord_tracer(iq) = 9
2110  kord_tracer_pert(iq) = flagstructp%kord_tr_pert
2111 ! linear
2112  IF (iq .EQ. cld_amt) kord_tracer_pert(iq) = 17
2113  END DO
2114  do_omega = hydrostatic .AND. last_step
2115  CALL timing_on('Remapping')
2116  kord_mt = flagstruct%kord_mt
2117  kord_wz = flagstruct%kord_wz
2118  kord_tm = flagstruct%kord_tm
2119  kord_mt_pert = flagstructp%kord_mt_pert
2120  kord_wz_pert = flagstructp%kord_wz_pert
2121  kord_tm_pert = flagstructp%kord_tm_pert
2122  IF (n_map .EQ. k_split) THEN
2123  kord_mt = kord_mt_pert
2124  kord_wz = kord_wz_pert
2125  kord_tm = kord_tm_pert
2126  kord_tracer = kord_tracer_pert
2127  END IF
2128  arg10 = idiag%id_mdt .GT. 0
2129  CALL lagrangian_to_eulerian(last_step, consv_te, ps, pe, delp, &
2130 & pkz, pk, mdt, bdt, npz, is, ie, js, je, &
2131 & isd, ied, jsd, jed, nq, nwat, sphum, q_con&
2132 & , u, v, w, delz, pt, q, phis, zvir, cp_air&
2133 & , akap, cappa, kord_mt, kord_wz, &
2134 & kord_tracer, kord_tm, peln, te_2d, ng, ua&
2135 & , va, omga, dp1, ws, fill, reproduce_sum, &
2136 & arg10, dtdt_m, ptop, ak, bk, pfull, &
2137 & flagstruct, gridstruct, domain, flagstruct&
2138 & %do_sat_adj, hydrostatic, hybrid_z, &
2139 & do_omega, flagstruct%adiabatic, &
2140 & do_adiabatic_init, mfx, mfy, flagstruct%&
2141 & remap_option, kord_mt_pert, kord_wz_pert, &
2142 & kord_tracer_pert, kord_tm_pert)
2143  CALL timing_off('Remapping')
2144  IF (last_step) THEN
2145  IF (.NOT.hydrostatic) THEN
2146 !$OMP parallel do default(none) shared(is,ie,js,je,npz,omga,delp,delz,w)
2147  DO k=1,npz
2148  DO j=js,je
2149  DO i=is,ie
2150  omga(i, j, k) = delp(i, j, k)/delz(i, j, k)*w(i, j, k)
2151  END DO
2152  END DO
2153  END DO
2154  END IF
2155 !--------------------------
2156 ! Filter omega for physics:
2157 !--------------------------
2158  IF (flagstruct%nf_omega .GT. 0) THEN
2159  arg11 = 0.18*gridstruct%da_min
2160  CALL del2_cubed(omga, arg11, gridstruct, domain, npx, npy, &
2161 & npz, flagstruct%nf_omega, bd)
2162  END IF
2163  END IF
2164  END IF
2165  END DO
2166 ! n_map loop
2167  CALL timing_off('FV_DYN_LOOP')
2168  IF (idiag%id_mdt .GT. 0 .AND. (.NOT.do_adiabatic_init)) THEN
2169 ! Output temperature tendency due to inline moist physics:
2170 !$OMP parallel do default(none) shared(is,ie,js,je,npz,dtdt_m,bdt)
2171  DO k=1,npz
2172  DO j=js,je
2173  DO i=is,ie
2174  dtdt_m(i, j, k) = dtdt_m(i, j, k)/bdt*86400.
2175  END DO
2176  END DO
2177  END DO
2178 ! call prt_mxm('Fast DTDT (deg/Day)', dtdt_m, is, ie, js, je, 0, npz, 1., gridstruct%area_64, domain)
2179  used = send_data(idiag%id_mdt, dtdt_m, fv_time)
2180 !deallocate ( dtdt_m )
2181  END IF
2182  IF (nwat .EQ. 6) THEN
2183  IF (cld_amt .GT. 0) THEN
2184  CALL neg_adj3(is, ie, js, je, ng, npz, flagstruct%hydrostatic, &
2185 & peln, delz, pt, delp, q(isd:ied, jsd:jed, 1, sphum), q(&
2186 & isd:ied, jsd:jed, 1, liq_wat), q(isd:ied, jsd:jed, 1, &
2187 & rainwat), q(isd:ied, jsd:jed, 1, ice_wat), q(isd:ied, &
2188 & jsd:jed, 1, snowwat), q(isd:ied, jsd:jed, 1, graupel), q&
2189 & (isd:ied, jsd:jed, 1, cld_amt), flagstruct%&
2190 & check_negative)
2191  ELSE
2192  CALL neg_adj3(is, ie, js, je, ng, npz, flagstruct%hydrostatic, &
2193 & peln, delz, pt, delp, q(isd:ied, jsd:jed, 1, sphum), q(&
2194 & isd:ied, jsd:jed, 1, liq_wat), q(isd:ied, jsd:jed, 1, &
2195 & rainwat), q(isd:ied, jsd:jed, 1, ice_wat), q(isd:ied, &
2196 & jsd:jed, 1, snowwat), q(isd:ied, jsd:jed, 1, graupel), &
2197 & check_negative=flagstruct%check_negative)
2198  END IF
2199  IF (flagstruct%fv_debug) THEN
2200  CALL prt_mxm('T_dyn_a3', pt, is, ie, js, je, ng, npz, 1., &
2201 & gridstruct%area_64, domain)
2202  CALL prt_mxm('SPHUM_dyn', q(isd:ied, jsd:jed, 1, sphum), is, ie&
2203 & , js, je, ng, npz, 1., gridstruct%area_64, domain)
2204  CALL prt_mxm('liq_wat_dyn', q(isd:ied, jsd:jed, 1, liq_wat), is&
2205 & , ie, js, je, ng, npz, 1., gridstruct%area_64, domain)
2206  CALL prt_mxm('rainwat_dyn', q(isd:ied, jsd:jed, 1, rainwat), is&
2207 & , ie, js, je, ng, npz, 1., gridstruct%area_64, domain)
2208  CALL prt_mxm('ice_wat_dyn', q(isd:ied, jsd:jed, 1, ice_wat), is&
2209 & , ie, js, je, ng, npz, 1., gridstruct%area_64, domain)
2210  CALL prt_mxm('snowwat_dyn', q(isd:ied, jsd:jed, 1, snowwat), is&
2211 & , ie, js, je, ng, npz, 1., gridstruct%area_64, domain)
2212  CALL prt_mxm('graupel_dyn', q(isd:ied, jsd:jed, 1, graupel), is&
2213 & , ie, js, je, ng, npz, 1., gridstruct%area_64, domain)
2214  END IF
2215  END IF
2216  IF (((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .OR. idiag%&
2217 & id_aam .GT. 0) .AND. (.NOT.do_adiabatic_init)) THEN
2218  CALL compute_aam(npz, is, ie, js, je, isd, ied, jsd, jed, &
2219 & gridstruct, bd, ptop, ua, va, u, v, delp, te_2d, ps, &
2220 & m_fac)
2221  IF (idiag%id_aam .GT. 0) THEN
2222  used = send_data(idiag%id_aam, te_2d, fv_time)
2223  IF (prt_minmax) gam = g_sum(domain, te_2d, is, ie, js, je, ng, &
2224 & gridstruct%area_64, 0)
2225 !if( is_master() ) write(6,*) 'Total AAM =', gam
2226  END IF
2227  END IF
2228  IF ((flagstruct%consv_am .OR. idiag%id_amdt .GT. 0) .AND. (.NOT.&
2229 & do_adiabatic_init)) THEN
2230 !$OMP parallel do default(none) shared(is,ie,js,je,te_2d,teq,dt2,ps2,ps,idiag)
2231  DO j=js,je
2232  DO i=is,ie
2233 ! Note: the mountain torque computation contains also numerical error
2234 ! The numerical error is mostly from the zonal gradient of the terrain (zxg)
2235  te_2d(i, j) = te_2d(i, j) - teq(i, j) + dt2*(ps2(i, j)+ps(i, j&
2236 & ))*idiag%zxg(i, j)
2237  END DO
2238  END DO
2239  IF (idiag%id_amdt .GT. 0) THEN
2240  arg12(:, :) = te_2d/bdt
2241  used = send_data(idiag%id_amdt, arg12(:, :), fv_time)
2242  END IF
2243  IF (flagstruct%consv_am .OR. prt_minmax) THEN
2244  amdt = g_sum(domain, te_2d, is, ie, js, je, ng, gridstruct%&
2245 & area_64, 0, .true.)
2246  result1 = g_sum(domain, m_fac, is, ie, js, je, ng, gridstruct%&
2247 & area_64, 0, .true.)
2248  u0 = -(radius*amdt/result1)
2249  IF (is_master() .AND. prt_minmax) WRITE(6, *) &
2250 & 'Dynamic AM tendency (Hadleys)='&
2251 & , amdt/(bdt*1.e18), &
2252 & 'del-u (per day)=', u0*86400./&
2253 & bdt
2254  END IF
2255 ! consv_am
2256  IF (flagstruct%consv_am) THEN
2257 !$OMP parallel do default(none) shared(is,ie,js,je,m_fac,u0,gridstruct)
2258  DO j=js,je
2259  DO i=is,ie
2260  m_fac(i, j) = u0*cos(gridstruct%agrid(i, j, 2))
2261  END DO
2262  END DO
2263 !$OMP parallel do default(none) shared(is,ie,js,je,npz,hydrostatic,pt,m_fac,ua,cp_air, &
2264 !$OMP u,u0,gridstruct,v )
2265  DO k=1,npz
2266  DO j=js,je+1
2267  DO i=is,ie
2268  u(i, j, k) = u(i, j, k) + u0*gridstruct%l2c_u(i, j)
2269  END DO
2270  END DO
2271  DO j=js,je
2272  DO i=is,ie+1
2273  v(i, j, k) = v(i, j, k) + u0*gridstruct%l2c_v(i, j)
2274  END DO
2275  END DO
2276  END DO
2277  END IF
2278  END IF
2279 !911 call cubed_to_latlon(u, v, ua, va, gridstruct, &
2280 ! npx, npy, npz, 1, gridstruct%grid_type, domain, gridstruct%nested, flagstruct%c2l_ord, bd)
2281 !deallocate(dp1)
2282 !deallocate(cappa)
2283  IF (flagstruct%fv_debug) THEN
2284  CALL prt_mxm('UA', ua, is, ie, js, je, ng, npz, 1., gridstruct%&
2285 & area_64, domain)
2286  CALL prt_mxm('VA', va, is, ie, js, je, ng, npz, 1., gridstruct%&
2287 & area_64, domain)
2288  CALL prt_mxm('TA', pt, is, ie, js, je, ng, npz, 1., gridstruct%&
2289 & area_64, domain)
2290  IF (.NOT.hydrostatic) CALL prt_mxm('W ', w, is, ie, js, je, ng, &
2291 & npz, 1., gridstruct%area_64, domain)
2292  END IF
2293  IF (flagstruct%range_warn) THEN
2294  CALL range_check('UA_dyn', ua, is, ie, js, je, ng, npz, gridstruct&
2295 & %agrid, -280., 280., bad_range)
2296  CALL range_check('VA_dyn', ua, is, ie, js, je, ng, npz, gridstruct&
2297 & %agrid, -280., 280., bad_range)
2298  CALL range_check('TA_dyn', pt, is, ie, js, je, ng, npz, gridstruct&
2299 & %agrid, 150., 335., bad_range)
2300  IF (.NOT.hydrostatic) CALL range_check('W_dyn', w, is, ie, js, je&
2301 & , ng, npz, gridstruct%agrid, -50.&
2302 & , 100., bad_range)
2303  END IF
2304 ! IF (fpp%fpp_mapl_mode) dyn_timer = dyn_timer + (t2-t1)
2305 !t2 = MPI_Wtime(status)
2306  END SUBROUTINE fv_dynamics
2307 ! Differentiation of rayleigh_super in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_ed
2308 !ge_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_c
2309 !ore_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_m
2310 !od.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayl
2311 !eigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l
2312 !_ord4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mo
2313 !d.remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap_
2314 !2d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limit
2315 !ers fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic
2316 !fv_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_su
2317 !bgrid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_ut
2318 !ils_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_util
2319 !s_mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils
2320 !_mod.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_mo
2321 !d.d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.yt
2322 !p_v_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_c
2323 !ore_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_u
2324 !tils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
2325 ! gradient of useful results: u v w ua va pt
2326 ! with respect to varying inputs: u v w ua va pt
2327  SUBROUTINE rayleigh_super_fwd(dt, npx, npy, npz, ks, pm, phis, tau, u&
2328 & , v, w, pt, ua, va, delz, agrid, cp, rg, ptop, hydrostatic, conserve&
2329 & , rf_cutoff, rf, gridstruct, domain, bd)
2330  IMPLICIT NONE
2331 !deallocate ( u2f )
2332  REAL, INTENT(IN) :: dt
2333 ! time scale (days)
2334  REAL, INTENT(IN) :: tau
2335  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
2336  INTEGER, INTENT(IN) :: npx, npy, npz, ks
2337  REAL, DIMENSION(npz), INTENT(IN) :: pm
2338  LOGICAL, INTENT(IN) :: hydrostatic
2339  LOGICAL, INTENT(IN) :: conserve
2340  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
2341 ! D grid zonal wind (m/s)
2342  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
2343 ! D grid meridional wind (m/s)
2344  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
2345 ! cell center vertical wind (m/s)
2346  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2347 ! temp
2348  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2349 !
2350  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2351 !
2352  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2353 ! delta-height (m); non-hydrostatic only
2354  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2355  REAL, INTENT(INOUT) :: rf(npz)
2356  REAL, INTENT(IN) :: agrid(bd%isd:bd%ied, bd%jsd:bd%jed, 2)
2357 ! Surface geopotential (g*Z_surf)
2358  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
2359  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
2360  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
2361 !
2362  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2363 ! scaling velocity
2364  REAL, PARAMETER :: u0=60.
2365  REAL, PARAMETER :: sday=86400.
2366  REAL :: rcv, tau0
2367  INTEGER :: i, j, k
2368  INTEGER :: is, ie, js, je
2369  INTEGER :: isd, ied, jsd, jed
2370  INTRINSIC log
2371  INTRINSIC sin
2372  LOGICAL :: res
2373  INTEGER :: ad_count
2374 
2375  u2f = 0.0
2376  rcv = 0.0
2377  tau0 = 0.0
2378  is = 0
2379  ie = 0
2380  js = 0
2381  je = 0
2382  isd = 0
2383  ied = 0
2384  jsd = 0
2385  jed = 0
2386  ad_count = 0
2387 
2388  is = bd%is
2389  ie = bd%ie
2390  js = bd%js
2391  je = bd%je
2392  rcv = 1./(cp-rg)
2393  IF (.NOT.rf_initialized) THEN
2394  tau0 = tau*sday
2395 !allocate( rf(npz) )
2396  rf(:) = 0.
2397  k = ks + 2
2398 !if( is_master() ) write(6,*) k, 0.01*pm(k)
2399  res = is_master()
2400  IF (res) WRITE(6, *) 'Rayleigh friction E-folding time (days):'
2401  ad_count = 1
2402  DO k=1,npz
2403  IF (pm(k) .LT. rf_cutoff) THEN
2404  rf(k) = dt/tau0*sin(0.5*pi*log(rf_cutoff/pm(k))/log(rf_cutoff/&
2405 & ptop))**2
2406 !if( is_master() ) write(6,*) k, 0.01*pm(k), dt/(rf(k)*sday)
2407  kmax = k
2408  ad_count = ad_count + 1
2409  ELSE
2410  GOTO 100
2411  END IF
2412  END DO
2413  CALL pushcontrol(1,0)
2414  CALL pushinteger(ad_count)
2415  CALL pushcontrol(1,0)
2416  GOTO 110
2417  100 CALL pushcontrol(1,1)
2418  CALL pushinteger(ad_count)
2419  CALL pushcontrol(1,0)
2420  110 rf_initialized = .true.
2421  ELSE
2422  CALL pushcontrol(1,1)
2423  END IF
2424  CALL c2l_ord2_fwd(u, v, ua, va, gridstruct, npz, gridstruct%&
2425 & grid_type, bd, gridstruct%nested)
2426 !allocate( u2f(isd:ied,jsd:jed,kmax) )
2427  u2f = 0.0
2428 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,pm,rf_cutoff,hydrostatic,ua,va,agrid, &
2429 !$OMP u2f,rf,w)
2430  DO k=1,kmax
2431  IF (pm(k) .LT. rf_cutoff) THEN
2432  CALL pushcontrol(1,1)
2433  u2f(:, :, k) = 1./(1.+rf(k))
2434  ELSE
2435  CALL pushcontrol(1,0)
2436  u2f(:, :, k) = 1.
2437  END IF
2438  END DO
2439  CALL mpp_update_domains(u2f, domain)
2440 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,pm,rf_cutoff,w,rf,u,v, &
2441 !$OMP conserve,hydrostatic,pt,ua,va,u2f,cp,rg,ptop,rcv)
2442  DO k=1,kmax
2443  IF (pm(k) .LT. rf_cutoff) THEN
2444 ! Add heat so as to conserve TE
2445  IF (conserve) THEN
2446  IF (hydrostatic) THEN
2447  DO j=js,je
2448  DO i=is,ie
2449  CALL pushrealarray(pt(i, j, k))
2450  pt(i, j, k) = pt(i, j, k) + 0.5*(ua(i, j, k)**2+va(i, j&
2451 & , k)**2)*(1.-u2f(i, j, k)**2)/(cp-rg*ptop/pm(k))
2452  END DO
2453  END DO
2454  CALL pushcontrol(2,2)
2455  ELSE
2456  DO j=js,je
2457  DO i=is,ie
2458  CALL pushrealarray(pt(i, j, k))
2459  pt(i, j, k) = pt(i, j, k) + 0.5*(ua(i, j, k)**2+va(i, j&
2460 & , k)**2+w(i, j, k)**2)*(1.-u2f(i, j, k)**2)*rcv
2461  END DO
2462  END DO
2463  CALL pushcontrol(2,1)
2464  END IF
2465  ELSE
2466  CALL pushcontrol(2,0)
2467  END IF
2468  DO j=js,je+1
2469  DO i=is,ie
2470  CALL pushrealarray(u(i, j, k))
2471  u(i, j, k) = 0.5*(u2f(i, j-1, k)+u2f(i, j, k))*u(i, j, k)
2472  END DO
2473  END DO
2474  DO j=js,je
2475  DO i=is,ie+1
2476  CALL pushrealarray(v(i, j, k))
2477  v(i, j, k) = 0.5*(u2f(i-1, j, k)+u2f(i, j, k))*v(i, j, k)
2478  END DO
2479  END DO
2480  IF (.NOT.hydrostatic) THEN
2481  DO j=js,je
2482  DO i=is,ie
2483  CALL pushrealarray(w(i, j, k))
2484  w(i, j, k) = u2f(i, j, k)*w(i, j, k)
2485  END DO
2486  END DO
2487  CALL pushcontrol(2,2)
2488  ELSE
2489  CALL pushcontrol(2,1)
2490  END IF
2491  ELSE
2492  CALL pushcontrol(2,0)
2493  END IF
2494  END DO
2495  CALL pushrealarray(u2f, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
2496  CALL pushinteger(je)
2497  CALL pushinteger(is)
2498  CALL pushinteger(ie)
2499  CALL pushrealarray(rcv)
2500  CALL pushinteger(js)
2501  END SUBROUTINE rayleigh_super_fwd
2502 ! Differentiation of rayleigh_super in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_e
2503 !dge_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_
2504 !core_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_
2505 !mod.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Ray
2506 !leigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2
2507 !l_ord4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_m
2508 !od.remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap
2509 !_2d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limi
2510 !ters fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic
2511 ! fv_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_s
2512 !ubgrid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_u
2513 !tils_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_uti
2514 !ls_mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_util
2515 !s_mod.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_m
2516 !od.d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.y
2517 !tp_v_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_
2518 !core_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_
2519 !utils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
2520 ! gradient of useful results: u v w ua va pt
2521 ! with respect to varying inputs: u v w ua va pt
2522  SUBROUTINE rayleigh_super_bwd(dt, npx, npy, npz, ks, pm, phis, tau, u&
2523 & , u_ad, v, v_ad, w, w_ad, pt, pt_ad, ua, ua_ad, va, va_ad, delz, &
2524 & agrid, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, &
2525 & gridstruct, domain, bd)
2526  IMPLICIT NONE
2527 !deallocate ( u2f )
2528  REAL, INTENT(IN) :: dt
2529  REAL, INTENT(IN) :: tau
2530  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
2531  INTEGER, INTENT(IN) :: npx, npy, npz, ks
2532  REAL, DIMENSION(npz), INTENT(IN) :: pm
2533  LOGICAL, INTENT(IN) :: hydrostatic
2534  LOGICAL, INTENT(IN) :: conserve
2535  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
2536  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
2537  REAL, INTENT(INOUT) :: u_ad(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
2538  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
2539  REAL, INTENT(INOUT) :: v_ad(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
2540  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2541  REAL, INTENT(INOUT) :: w_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2542  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2543  REAL, INTENT(INOUT) :: pt_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2544  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2545  REAL, INTENT(INOUT) :: ua_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2546  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2547  REAL, INTENT(INOUT) :: va_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2548  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2549  REAL, INTENT(INOUT) :: rf(npz)
2550  REAL, INTENT(IN) :: agrid(bd%isd:bd%ied, bd%jsd:bd%jed, 2)
2551  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
2552  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
2553  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
2554  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2555  REAL, PARAMETER :: u0=60.
2556  REAL, PARAMETER :: sday=86400.
2557  REAL :: rcv, tau0
2558  INTEGER :: i, j, k
2559  INTEGER :: is, ie, js, je
2560  INTEGER :: isd, ied, jsd, jed
2561  INTRINSIC log
2562  INTRINSIC sin
2563  REAL :: temp_ad
2564  REAL :: temp_ad0
2565  INTEGER :: ad_count
2566  INTEGER :: i0
2567  INTEGER :: branch
2568 
2569  u2f = 0.0
2570  rcv = 0.0
2571  tau0 = 0.0
2572  is = 0
2573  ie = 0
2574  js = 0
2575  je = 0
2576  isd = 0
2577  ied = 0
2578  jsd = 0
2579  jed = 0
2580  ad_count = 0
2581  branch = 0
2582 
2583  CALL popinteger(js)
2584  CALL poprealarray(rcv)
2585  CALL popinteger(ie)
2586  CALL popinteger(is)
2587  CALL popinteger(je)
2588  CALL poprealarray(u2f, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
2589  DO k=kmax,1,-1
2590  CALL popcontrol(2,branch)
2591  IF (branch .NE. 0) THEN
2592  IF (branch .NE. 1) THEN
2593  DO j=je,js,-1
2594  DO i=ie,is,-1
2595  CALL poprealarray(w(i, j, k))
2596  w_ad(i, j, k) = u2f(i, j, k)*w_ad(i, j, k)
2597  END DO
2598  END DO
2599  END IF
2600  DO j=je,js,-1
2601  DO i=ie+1,is,-1
2602  CALL poprealarray(v(i, j, k))
2603  v_ad(i, j, k) = (u2f(i-1, j, k)+u2f(i, j, k))*0.5*v_ad(i, j&
2604 & , k)
2605  END DO
2606  END DO
2607  DO j=je+1,js,-1
2608  DO i=ie,is,-1
2609  CALL poprealarray(u(i, j, k))
2610  u_ad(i, j, k) = (u2f(i, j-1, k)+u2f(i, j, k))*0.5*u_ad(i, j&
2611 & , k)
2612  END DO
2613  END DO
2614  CALL popcontrol(2,branch)
2615  IF (branch .NE. 0) THEN
2616  IF (branch .EQ. 1) THEN
2617  DO j=je,js,-1
2618  DO i=ie,is,-1
2619  CALL poprealarray(pt(i, j, k))
2620  temp_ad0 = (1.-u2f(i, j, k)**2)*rcv*0.5*pt_ad(i, j, k)
2621  ua_ad(i, j, k) = ua_ad(i, j, k) + 2*ua(i, j, k)*temp_ad0
2622  va_ad(i, j, k) = va_ad(i, j, k) + 2*va(i, j, k)*temp_ad0
2623  w_ad(i, j, k) = w_ad(i, j, k) + 2*w(i, j, k)*temp_ad0
2624  END DO
2625  END DO
2626  ELSE
2627  DO j=je,js,-1
2628  DO i=ie,is,-1
2629  CALL poprealarray(pt(i, j, k))
2630  temp_ad = (1.-u2f(i, j, k)**2)*0.5*pt_ad(i, j, k)/(cp-rg&
2631 & *ptop/pm(k))
2632  ua_ad(i, j, k) = ua_ad(i, j, k) + 2*ua(i, j, k)*temp_ad
2633  va_ad(i, j, k) = va_ad(i, j, k) + 2*va(i, j, k)*temp_ad
2634  END DO
2635  END DO
2636  END IF
2637  END IF
2638  END IF
2639  END DO
2640  DO k=kmax,1,-1
2641  CALL popcontrol(1,branch)
2642  END DO
2643  CALL c2l_ord2_bwd(u, u_ad, v, v_ad, ua, ua_ad, va, va_ad, gridstruct&
2644 & , npz, gridstruct%grid_type, bd, gridstruct%nested)
2645  CALL popcontrol(1,branch)
2646  IF (branch .EQ. 0) THEN
2647  CALL popinteger(ad_count)
2648  DO i0=1,ad_count
2649  IF (i0 .EQ. 1) CALL popcontrol(1,branch)
2650  END DO
2651  END IF
2652  END SUBROUTINE rayleigh_super_bwd
2653  SUBROUTINE rayleigh_super(dt, npx, npy, npz, ks, pm, phis, tau, u, v, &
2654 & w, pt, ua, va, delz, agrid, cp, rg, ptop, hydrostatic, conserve, &
2655 & rf_cutoff, rf, gridstruct, domain, bd)
2656  IMPLICIT NONE
2657 !deallocate ( u2f )
2658  REAL, INTENT(IN) :: dt
2659 ! time scale (days)
2660  REAL, INTENT(IN) :: tau
2661  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
2662  INTEGER, INTENT(IN) :: npx, npy, npz, ks
2663  REAL, DIMENSION(npz), INTENT(IN) :: pm
2664  LOGICAL, INTENT(IN) :: hydrostatic
2665  LOGICAL, INTENT(IN) :: conserve
2666  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
2667 ! D grid zonal wind (m/s)
2668  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
2669 ! D grid meridional wind (m/s)
2670  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
2671 ! cell center vertical wind (m/s)
2672  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2673 ! temp
2674  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2675 !
2676  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2677 !
2678  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2679 ! delta-height (m); non-hydrostatic only
2680  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2681  REAL, INTENT(INOUT) :: rf(npz)
2682  REAL, INTENT(IN) :: agrid(bd%isd:bd%ied, bd%jsd:bd%jed, 2)
2683 ! Surface geopotential (g*Z_surf)
2684  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
2685  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
2686  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
2687 !
2688  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2689 ! scaling velocity
2690  REAL, PARAMETER :: u0=60.
2691  REAL, PARAMETER :: sday=86400.
2692  REAL :: rcv, tau0
2693  INTEGER :: i, j, k
2694  INTEGER :: is, ie, js, je
2695  INTEGER :: isd, ied, jsd, jed
2696  INTRINSIC log
2697  INTRINSIC sin
2698  is = bd%is
2699  ie = bd%ie
2700  js = bd%js
2701  je = bd%je
2702  isd = bd%isd
2703  ied = bd%ied
2704  jsd = bd%jsd
2705  jed = bd%jed
2706  rcv = 1./(cp-rg)
2707  IF (.NOT.rf_initialized) THEN
2708  tau0 = tau*sday
2709 !allocate( rf(npz) )
2710  rf(:) = 0.
2711  k = ks + 2
2712 !if( is_master() ) write(6,*) k, 0.01*pm(k)
2713  IF (is_master()) WRITE(6, *) &
2714 & 'Rayleigh friction E-folding time (days):'
2715  DO k=1,npz
2716  IF (pm(k) .LT. rf_cutoff) THEN
2717  rf(k) = dt/tau0*sin(0.5*pi*log(rf_cutoff/pm(k))/log(rf_cutoff/&
2718 & ptop))**2
2719 !if( is_master() ) write(6,*) k, 0.01*pm(k), dt/(rf(k)*sday)
2720  kmax = k
2721  ELSE
2722  GOTO 100
2723  END IF
2724  END DO
2725  100 rf_initialized = .true.
2726  END IF
2727  CALL c2l_ord2(u, v, ua, va, gridstruct, npz, gridstruct%grid_type, &
2728 & bd, gridstruct%nested)
2729 !allocate( u2f(isd:ied,jsd:jed,kmax) )
2730  u2f = 0.0
2731 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,pm,rf_cutoff,hydrostatic,ua,va,agrid, &
2732 !$OMP u2f,rf,w)
2733  DO k=1,kmax
2734  IF (pm(k) .LT. rf_cutoff) THEN
2735  u2f(:, :, k) = 1./(1.+rf(k))
2736  ELSE
2737  u2f(:, :, k) = 1.
2738  END IF
2739  END DO
2740  CALL timing_on('COMM_TOTAL')
2741  CALL mpp_update_domains(u2f, domain)
2742  CALL timing_off('COMM_TOTAL')
2743 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,pm,rf_cutoff,w,rf,u,v, &
2744 !$OMP conserve,hydrostatic,pt,ua,va,u2f,cp,rg,ptop,rcv)
2745  DO k=1,kmax
2746  IF (pm(k) .LT. rf_cutoff) THEN
2747 ! Add heat so as to conserve TE
2748  IF (conserve) THEN
2749  IF (hydrostatic) THEN
2750  DO j=js,je
2751  DO i=is,ie
2752  pt(i, j, k) = pt(i, j, k) + 0.5*(ua(i, j, k)**2+va(i, j&
2753 & , k)**2)*(1.-u2f(i, j, k)**2)/(cp-rg*ptop/pm(k))
2754  END DO
2755  END DO
2756  ELSE
2757  DO j=js,je
2758  DO i=is,ie
2759  pt(i, j, k) = pt(i, j, k) + 0.5*(ua(i, j, k)**2+va(i, j&
2760 & , k)**2+w(i, j, k)**2)*(1.-u2f(i, j, k)**2)*rcv
2761  END DO
2762  END DO
2763  END IF
2764  END IF
2765  DO j=js,je+1
2766  DO i=is,ie
2767  u(i, j, k) = 0.5*(u2f(i, j-1, k)+u2f(i, j, k))*u(i, j, k)
2768  END DO
2769  END DO
2770  DO j=js,je
2771  DO i=is,ie+1
2772  v(i, j, k) = 0.5*(u2f(i-1, j, k)+u2f(i, j, k))*v(i, j, k)
2773  END DO
2774  END DO
2775  IF (.NOT.hydrostatic) THEN
2776  DO j=js,je
2777  DO i=is,ie
2778  w(i, j, k) = u2f(i, j, k)*w(i, j, k)
2779  END DO
2780  END DO
2781  END IF
2782  END IF
2783  END DO
2784  END SUBROUTINE rayleigh_super
2785 ! Differentiation of rayleigh_friction in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b
2786 !_edge_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dy
2787 !n_core_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_cor
2788 !e_mod.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.R
2789 !ayleigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.
2790 !c2l_ord4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz
2791 !_mod.remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.rem
2792 !ap_2d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_li
2793 !miters fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cub
2794 !ic fv_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv
2795 !_subgrid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh
2796 !_utils_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_u
2797 !tils_mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_ut
2798 !ils_mod.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core
2799 !_mod.d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod
2800 !.ytp_v sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d t
2801 !p_core_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_gri
2802 !d_utils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
2803 ! gradient of useful results: u v w ua delz va pt
2804 ! with respect to varying inputs: u v w ua delz va pt
2805  SUBROUTINE rayleigh_friction_fwd(dt, npx, npy, npz, ks, pm, tau, u, v&
2806 & , w, pt, ua, va, delz, cp, rg, ptop, hydrostatic, conserve, &
2807 & rf_cutoff, rf, gridstruct, domain, bd)
2808  IMPLICIT NONE
2809 !deallocate ( u2f )
2810  REAL, INTENT(IN) :: dt
2811 ! time scale (days)
2812  REAL, INTENT(IN) :: tau
2813  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
2814  INTEGER, INTENT(IN) :: npx, npy, npz, ks
2815  REAL, DIMENSION(npz), INTENT(IN) :: pm
2816  LOGICAL, INTENT(IN) :: hydrostatic
2817  LOGICAL, INTENT(IN) :: conserve
2818  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
2819 ! D grid zonal wind (m/s)
2820  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
2821 ! D grid meridional wind (m/s)
2822  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
2823 ! cell center vertical wind (m/s)
2824  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2825 ! temp
2826  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2827 !
2828  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2829 !
2830  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2831 ! delta-height (m); non-hydrostatic only
2832  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2833  REAL, INTENT(INOUT) :: rf(npz)
2834  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
2835  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
2836 ! local:
2837  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
2838  REAL, PARAMETER :: sday=86400.
2839 ! scaling velocity **2
2840  REAL, PARAMETER :: u000=4900.
2841  REAL :: rcv
2842  INTEGER :: i, j, k
2843  INTEGER :: is, ie, js, je
2844  INTEGER :: isd, ied, jsd, jed
2845  INTRINSIC log
2846  INTRINSIC sin
2847  INTRINSIC sqrt
2848  LOGICAL :: res
2849  INTEGER :: ad_count
2850 
2851  u2f = 0.0
2852  rcv = 0.0
2853  is = 0
2854  ie = 0
2855  js = 0
2856  je = 0
2857  isd = 0
2858  ied = 0
2859  jsd = 0
2860  jed = 0
2861  ad_count = 0
2862 
2863  is = bd%is
2864  ie = bd%ie
2865  js = bd%js
2866  je = bd%je
2867  rcv = 1./(cp-rg)
2868  IF (.NOT.rf_initialized) THEN
2869 !allocate( rf(npz) )
2870  rf = 0.0
2871  res = is_master()
2872  IF (res) WRITE(6, *) 'Rayleigh friction E-folding time (days):'
2873  ad_count = 1
2874  DO k=1,npz
2875  IF (pm(k) .LT. rf_cutoff) THEN
2876  rf(k) = dt/(tau*sday)*sin(0.5*pi*log(rf_cutoff/pm(k))/log(&
2877 & rf_cutoff/ptop))**2
2878 !if( is_master() ) write(6,*) k, 0.01*pm(k), dt/(rf(k)*sday)
2879  kmax = k
2880  ad_count = ad_count + 1
2881  ELSE
2882  GOTO 100
2883  END IF
2884  END DO
2885  CALL pushcontrol(1,0)
2886  CALL pushinteger(ad_count)
2887  CALL pushcontrol(1,0)
2888  GOTO 110
2889  100 CALL pushcontrol(1,1)
2890  CALL pushinteger(ad_count)
2891  CALL pushcontrol(1,0)
2892  110 rf_initialized = .true.
2893  ELSE
2894  CALL pushcontrol(1,1)
2895  END IF
2896 !allocate( u2f(isd:ied,jsd:jed,kmax) )
2897  CALL c2l_ord2_fwd(u, v, ua, va, gridstruct, npz, gridstruct%&
2898 & grid_type, bd, gridstruct%nested)
2899 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,u2f,hydrostatic,ua,va,w)
2900  DO k=1,kmax
2901  IF (hydrostatic) THEN
2902  DO j=js,je
2903  DO i=is,ie
2904  u2f(i, j, k) = ua(i, j, k)**2 + va(i, j, k)**2
2905  END DO
2906  END DO
2907  CALL pushcontrol(1,1)
2908  ELSE
2909  DO j=js,je
2910  DO i=is,ie
2911  u2f(i, j, k) = ua(i, j, k)**2 + va(i, j, k)**2 + w(i, j, k)&
2912 & **2
2913  END DO
2914  END DO
2915  CALL pushcontrol(1,0)
2916  END IF
2917  END DO
2918  CALL mpp_update_domains(u2f, domain)
2919 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,conserve,hydrostatic,pt,u2f,cp,rg, &
2920 !$OMP ptop,pm,rf,delz,rcv,u,v,w)
2921  DO k=1,kmax
2922  IF (conserve) THEN
2923  IF (hydrostatic) THEN
2924  DO j=js,je
2925  DO i=is,ie
2926  CALL pushrealarray(pt(i, j, k))
2927  pt(i, j, k) = pt(i, j, k) + 0.5*u2f(i, j, k)/(cp-rg*ptop/&
2928 & pm(k))*(1.-1./(1.+rf(k)*sqrt(u2f(i, j, k)/u000))**2)
2929  END DO
2930  END DO
2931  CALL pushcontrol(2,2)
2932  ELSE
2933  DO j=js,je
2934  DO i=is,ie
2935  CALL pushrealarray(delz(i, j, k))
2936  delz(i, j, k) = delz(i, j, k)/pt(i, j, k)
2937  CALL pushrealarray(pt(i, j, k))
2938  pt(i, j, k) = pt(i, j, k) + 0.5*u2f(i, j, k)*rcv*(1.-1./(&
2939 & 1.+rf(k)*sqrt(u2f(i, j, k)/u000))**2)
2940  CALL pushrealarray(delz(i, j, k))
2941  delz(i, j, k) = delz(i, j, k)*pt(i, j, k)
2942  END DO
2943  END DO
2944  CALL pushcontrol(2,1)
2945  END IF
2946  ELSE
2947  CALL pushcontrol(2,0)
2948  END IF
2949  DO j=js-1,je+1
2950  DO i=is-1,ie+1
2951  CALL pushrealarray(u2f(i, j, k))
2952  u2f(i, j, k) = rf(k)*sqrt(u2f(i, j, k)/u000)
2953  END DO
2954  END DO
2955  DO j=js,je+1
2956  DO i=is,ie
2957  CALL pushrealarray(u(i, j, k))
2958  u(i, j, k) = u(i, j, k)/(1.+0.5*(u2f(i, j-1, k)+u2f(i, j, k)))
2959  END DO
2960  END DO
2961  DO j=js,je
2962  DO i=is,ie+1
2963  CALL pushrealarray(v(i, j, k))
2964  v(i, j, k) = v(i, j, k)/(1.+0.5*(u2f(i-1, j, k)+u2f(i, j, k)))
2965  END DO
2966  END DO
2967  IF (.NOT.hydrostatic) THEN
2968  DO j=js,je
2969  DO i=is,ie
2970  CALL pushrealarray(w(i, j, k))
2971  w(i, j, k) = w(i, j, k)/(1.+u2f(i, j, k))
2972  END DO
2973  END DO
2974  CALL pushcontrol(1,1)
2975  ELSE
2976  CALL pushcontrol(1,0)
2977  END IF
2978  END DO
2979  CALL pushrealarray(u2f, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
2980  CALL pushinteger(je)
2981  CALL pushinteger(is)
2982  CALL pushinteger(ie)
2983  CALL pushrealarray(rcv)
2984  CALL pushinteger(js)
2985  END SUBROUTINE rayleigh_friction_fwd
2986 ! Differentiation of rayleigh_friction in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2
2987 !b_edge_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe d
2988 !yn_core_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_co
2989 !re_mod.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.
2990 !Rayleigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod
2991 !.c2l_ord4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_map
2992 !z_mod.remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.re
2993 !map_2d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_l
2994 !imiters fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cu
2995 !bic fv_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.f
2996 !v_subgrid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d n
2997 !h_utils_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_
2998 !utils_mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_u
2999 !tils_mod.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_cor
3000 !e_mod.d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mo
3001 !d.ytp_v sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d
3002 !tp_core_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_gr
3003 !id_utils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
3004 ! gradient of useful results: u v w ua delz va pt
3005 ! with respect to varying inputs: u v w ua delz va pt
3006  SUBROUTINE rayleigh_friction_bwd(dt, npx, npy, npz, ks, pm, tau, u, &
3007 & u_ad, v, v_ad, w, w_ad, pt, pt_ad, ua, ua_ad, va, va_ad, delz, &
3008 & delz_ad, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, &
3009 & gridstruct, domain, bd)
3010  IMPLICIT NONE
3011 !deallocate ( u2f )
3012  REAL, INTENT(IN) :: dt
3013  REAL, INTENT(IN) :: tau
3014  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
3015  INTEGER, INTENT(IN) :: npx, npy, npz, ks
3016  REAL, DIMENSION(npz), INTENT(IN) :: pm
3017  LOGICAL, INTENT(IN) :: hydrostatic
3018  LOGICAL, INTENT(IN) :: conserve
3019  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
3020  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
3021  REAL, INTENT(INOUT) :: u_ad(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
3022  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
3023  REAL, INTENT(INOUT) :: v_ad(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
3024  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3025  REAL, INTENT(INOUT) :: w_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3026  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3027  REAL, INTENT(INOUT) :: pt_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3028  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3029  REAL, INTENT(INOUT) :: ua_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3030  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3031  REAL, INTENT(INOUT) :: va_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3032  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3033  REAL, INTENT(INOUT) :: delz_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3034  REAL, INTENT(INOUT) :: rf(npz)
3035  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
3036  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
3037  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3038  REAL :: u2f_ad(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3039  REAL, PARAMETER :: sday=86400.
3040  REAL, PARAMETER :: u000=4900.
3041  REAL :: rcv
3042  INTEGER :: i, j, k
3043  INTEGER :: is, ie, js, je
3044  INTEGER :: isd, ied, jsd, jed
3045  INTRINSIC log
3046  INTRINSIC sin
3047  INTRINSIC sqrt
3048  REAL :: temp
3049  REAL :: temp0
3050  REAL :: temp1
3051  REAL :: temp2
3052  REAL :: temp3
3053  REAL :: temp4
3054  REAL :: temp5
3055  REAL :: temp6
3056  REAL :: temp7
3057  REAL :: temp_ad
3058  REAL :: temp_ad0
3059  REAL :: temp8
3060  REAL :: temp_ad1
3061  REAL :: temp9
3062  REAL :: temp_ad2
3063  REAL :: temp_ad3
3064  INTEGER :: ad_count
3065  INTEGER :: i0
3066  INTEGER :: branch
3067 
3068  u2f = 0.0
3069  rcv = 0.0
3070  is = 0
3071  ie = 0
3072  js = 0
3073  je = 0
3074  isd = 0
3075  ied = 0
3076  jsd = 0
3077  jed = 0
3078  ad_count = 0
3079  branch = 0
3080 
3081  CALL popinteger(js)
3082  CALL poprealarray(rcv)
3083  CALL popinteger(ie)
3084  CALL popinteger(is)
3085  CALL popinteger(je)
3086  CALL poprealarray(u2f, (bd%ied-bd%isd+1)*(bd%jed-bd%jsd+1)*npz)
3087  u2f_ad = 0.0
3088  DO k=kmax,1,-1
3089  CALL popcontrol(1,branch)
3090  IF (branch .NE. 0) THEN
3091  DO j=je,js,-1
3092  DO i=ie,is,-1
3093  CALL poprealarray(w(i, j, k))
3094  temp_ad3 = w_ad(i, j, k)/(u2f(i, j, k)+1.)
3095  u2f_ad(i, j, k) = u2f_ad(i, j, k) - w(i, j, k)*temp_ad3/(u2f&
3096 & (i, j, k)+1.)
3097  w_ad(i, j, k) = temp_ad3
3098  END DO
3099  END DO
3100  END IF
3101  DO j=je,js,-1
3102  DO i=ie+1,is,-1
3103  CALL poprealarray(v(i, j, k))
3104  temp9 = 0.5*(u2f(i-1, j, k)+u2f(i, j, k)) + 1.
3105  temp_ad2 = -(v(i, j, k)*0.5*v_ad(i, j, k)/temp9**2)
3106  u2f_ad(i-1, j, k) = u2f_ad(i-1, j, k) + temp_ad2
3107  u2f_ad(i, j, k) = u2f_ad(i, j, k) + temp_ad2
3108  v_ad(i, j, k) = v_ad(i, j, k)/temp9
3109  END DO
3110  END DO
3111  DO j=je+1,js,-1
3112  DO i=ie,is,-1
3113  CALL poprealarray(u(i, j, k))
3114  temp8 = 0.5*(u2f(i, j-1, k)+u2f(i, j, k)) + 1.
3115  temp_ad1 = -(u(i, j, k)*0.5*u_ad(i, j, k)/temp8**2)
3116  u2f_ad(i, j-1, k) = u2f_ad(i, j-1, k) + temp_ad1
3117  u2f_ad(i, j, k) = u2f_ad(i, j, k) + temp_ad1
3118  u_ad(i, j, k) = u_ad(i, j, k)/temp8
3119  END DO
3120  END DO
3121  DO j=je+1,js-1,-1
3122  DO i=ie+1,is-1,-1
3123  CALL poprealarray(u2f(i, j, k))
3124  IF (u2f(i, j, k)/u000 .EQ. 0.0) THEN
3125  u2f_ad(i, j, k) = 0.0
3126  ELSE
3127  u2f_ad(i, j, k) = rf(k)*u2f_ad(i, j, k)/(2.0*sqrt(u2f(i, j, &
3128 & k)/u000)*u000)
3129  END IF
3130  END DO
3131  END DO
3132  CALL popcontrol(2,branch)
3133  IF (branch .NE. 0) THEN
3134  IF (branch .EQ. 1) THEN
3135  DO j=je,js,-1
3136  DO i=ie,is,-1
3137  CALL poprealarray(delz(i, j, k))
3138  pt_ad(i, j, k) = pt_ad(i, j, k) + delz(i, j, k)*delz_ad(i&
3139 & , j, k)
3140  delz_ad(i, j, k) = pt(i, j, k)*delz_ad(i, j, k)
3141  CALL poprealarray(pt(i, j, k))
3142  temp7 = u2f(i, j, k)/u000
3143  temp6 = sqrt(temp7)
3144  temp5 = rf(k)*temp6 + 1.
3145  temp4 = temp5**2
3146  temp_ad = rcv*0.5*pt_ad(i, j, k)
3147  IF (temp7 .EQ. 0.0) THEN
3148  u2f_ad(i, j, k) = u2f_ad(i, j, k) + (1.-1.0/temp4)*&
3149 & temp_ad
3150  ELSE
3151  u2f_ad(i, j, k) = u2f_ad(i, j, k) + (rf(k)*2*temp5*u2f(i&
3152 & , j, k)/(2.0*temp6*temp4**2*u000)-1.0/temp4+1.)*&
3153 & temp_ad
3154  END IF
3155  CALL poprealarray(delz(i, j, k))
3156  temp_ad0 = delz_ad(i, j, k)/pt(i, j, k)
3157  pt_ad(i, j, k) = pt_ad(i, j, k) - delz(i, j, k)*temp_ad0/&
3158 & pt(i, j, k)
3159  delz_ad(i, j, k) = temp_ad0
3160  END DO
3161  END DO
3162  ELSE
3163  DO j=je,js,-1
3164  DO i=ie,is,-1
3165  CALL poprealarray(pt(i, j, k))
3166  temp3 = cp - rg*ptop/pm(k)
3167  temp2 = u2f(i, j, k)/u000
3168  temp1 = sqrt(temp2)
3169  temp0 = rf(k)*temp1 + 1.
3170  temp = temp0**2
3171  IF (temp2 .EQ. 0.0) THEN
3172  u2f_ad(i, j, k) = u2f_ad(i, j, k) + (1.-1.0/temp)*0.5*&
3173 & pt_ad(i, j, k)/temp3
3174  ELSE
3175  u2f_ad(i, j, k) = u2f_ad(i, j, k) + ((1.-1.0/temp)*0.5/&
3176 & temp3+rf(k)*2*temp0*u2f(i, j, k)*0.5/(2.0*temp1*temp**&
3177 & 2*temp3*u000))*pt_ad(i, j, k)
3178  END IF
3179  END DO
3180  END DO
3181  END IF
3182  END IF
3183  END DO
3184  CALL mpp_update_domains_adm(u2f, u2f_ad, domain)
3185  DO k=kmax,1,-1
3186  CALL popcontrol(1,branch)
3187  IF (branch .EQ. 0) THEN
3188  DO j=je,js,-1
3189  DO i=ie,is,-1
3190  ua_ad(i, j, k) = ua_ad(i, j, k) + 2*ua(i, j, k)*u2f_ad(i, j&
3191 & , k)
3192  va_ad(i, j, k) = va_ad(i, j, k) + 2*va(i, j, k)*u2f_ad(i, j&
3193 & , k)
3194  w_ad(i, j, k) = w_ad(i, j, k) + 2*w(i, j, k)*u2f_ad(i, j, k)
3195  u2f_ad(i, j, k) = 0.0
3196  END DO
3197  END DO
3198  ELSE
3199  DO j=je,js,-1
3200  DO i=ie,is,-1
3201  ua_ad(i, j, k) = ua_ad(i, j, k) + 2*ua(i, j, k)*u2f_ad(i, j&
3202 & , k)
3203  va_ad(i, j, k) = va_ad(i, j, k) + 2*va(i, j, k)*u2f_ad(i, j&
3204 & , k)
3205  u2f_ad(i, j, k) = 0.0
3206  END DO
3207  END DO
3208  END IF
3209  END DO
3210  CALL c2l_ord2_bwd(u, u_ad, v, v_ad, ua, ua_ad, va, va_ad, gridstruct&
3211 & , npz, gridstruct%grid_type, bd, gridstruct%nested)
3212  CALL popcontrol(1,branch)
3213  IF (branch .EQ. 0) THEN
3214  CALL popinteger(ad_count)
3215  DO i0=1,ad_count
3216  IF (i0 .EQ. 1) CALL popcontrol(1,branch)
3217  END DO
3218  END IF
3219  END SUBROUTINE rayleigh_friction_bwd
3220  SUBROUTINE rayleigh_friction(dt, npx, npy, npz, ks, pm, tau, u, v, w, &
3221 & pt, ua, va, delz, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf&
3222 & , gridstruct, domain, bd)
3223  IMPLICIT NONE
3224 !deallocate ( u2f )
3225  REAL, INTENT(IN) :: dt
3226 ! time scale (days)
3227  REAL, INTENT(IN) :: tau
3228  REAL, INTENT(IN) :: cp, rg, ptop, rf_cutoff
3229  INTEGER, INTENT(IN) :: npx, npy, npz, ks
3230  REAL, DIMENSION(npz), INTENT(IN) :: pm
3231  LOGICAL, INTENT(IN) :: hydrostatic
3232  LOGICAL, INTENT(IN) :: conserve
3233  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
3234 ! D grid zonal wind (m/s)
3235  REAL, INTENT(INOUT) :: u(bd%isd:bd%ied, bd%jsd:bd%jed+1, npz)
3236 ! D grid meridional wind (m/s)
3237  REAL, INTENT(INOUT) :: v(bd%isd:bd%ied+1, bd%jsd:bd%jed, npz)
3238 ! cell center vertical wind (m/s)
3239  REAL, INTENT(INOUT) :: w(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3240 ! temp
3241  REAL, INTENT(INOUT) :: pt(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3242 !
3243  REAL, INTENT(INOUT) :: ua(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3244 !
3245  REAL, INTENT(INOUT) :: va(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3246 ! delta-height (m); non-hydrostatic only
3247  REAL, INTENT(INOUT) :: delz(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3248  REAL, INTENT(INOUT) :: rf(npz)
3249  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
3250  TYPE(DOMAIN2D), INTENT(INOUT) :: domain
3251 ! local:
3252  REAL :: u2f(bd%isd:bd%ied, bd%jsd:bd%jed, npz)
3253  REAL, PARAMETER :: sday=86400.
3254 ! scaling velocity **2
3255  REAL, PARAMETER :: u000=4900.
3256  REAL :: rcv
3257  INTEGER :: i, j, k
3258  INTEGER :: is, ie, js, je
3259  INTEGER :: isd, ied, jsd, jed
3260  INTRINSIC log
3261  INTRINSIC sin
3262  INTRINSIC sqrt
3263  is = bd%is
3264  ie = bd%ie
3265  js = bd%js
3266  je = bd%je
3267  isd = bd%isd
3268  ied = bd%ied
3269  jsd = bd%jsd
3270  jed = bd%jed
3271  rcv = 1./(cp-rg)
3272  IF (.NOT.rf_initialized) THEN
3273 !allocate( rf(npz) )
3274  rf = 0.0
3275  IF (is_master()) WRITE(6, *) &
3276 & 'Rayleigh friction E-folding time (days):'
3277  DO k=1,npz
3278  IF (pm(k) .LT. rf_cutoff) THEN
3279  rf(k) = dt/(tau*sday)*sin(0.5*pi*log(rf_cutoff/pm(k))/log(&
3280 & rf_cutoff/ptop))**2
3281 !if( is_master() ) write(6,*) k, 0.01*pm(k), dt/(rf(k)*sday)
3282  kmax = k
3283  ELSE
3284  GOTO 100
3285  END IF
3286  END DO
3287  100 rf_initialized = .true.
3288  END IF
3289 !allocate( u2f(isd:ied,jsd:jed,kmax) )
3290  CALL c2l_ord2(u, v, ua, va, gridstruct, npz, gridstruct%grid_type, &
3291 & bd, gridstruct%nested)
3292 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,u2f,hydrostatic,ua,va,w)
3293  DO k=1,kmax
3294  IF (hydrostatic) THEN
3295  DO j=js,je
3296  DO i=is,ie
3297  u2f(i, j, k) = ua(i, j, k)**2 + va(i, j, k)**2
3298  END DO
3299  END DO
3300  ELSE
3301  DO j=js,je
3302  DO i=is,ie
3303  u2f(i, j, k) = ua(i, j, k)**2 + va(i, j, k)**2 + w(i, j, k)&
3304 & **2
3305  END DO
3306  END DO
3307  END IF
3308  END DO
3309  CALL timing_on('COMM_TOTAL')
3310  CALL mpp_update_domains(u2f, domain)
3311  CALL timing_off('COMM_TOTAL')
3312 !$OMP parallel do default(none) shared(is,ie,js,je,kmax,conserve,hydrostatic,pt,u2f,cp,rg, &
3313 !$OMP ptop,pm,rf,delz,rcv,u,v,w)
3314  DO k=1,kmax
3315  IF (conserve) THEN
3316  IF (hydrostatic) THEN
3317  DO j=js,je
3318  DO i=is,ie
3319  pt(i, j, k) = pt(i, j, k) + 0.5*u2f(i, j, k)/(cp-rg*ptop/&
3320 & pm(k))*(1.-1./(1.+rf(k)*sqrt(u2f(i, j, k)/u000))**2)
3321  END DO
3322  END DO
3323  ELSE
3324  DO j=js,je
3325  DO i=is,ie
3326  delz(i, j, k) = delz(i, j, k)/pt(i, j, k)
3327  pt(i, j, k) = pt(i, j, k) + 0.5*u2f(i, j, k)*rcv*(1.-1./(&
3328 & 1.+rf(k)*sqrt(u2f(i, j, k)/u000))**2)
3329  delz(i, j, k) = delz(i, j, k)*pt(i, j, k)
3330  END DO
3331  END DO
3332  END IF
3333  END IF
3334  DO j=js-1,je+1
3335  DO i=is-1,ie+1
3336  u2f(i, j, k) = rf(k)*sqrt(u2f(i, j, k)/u000)
3337  END DO
3338  END DO
3339  DO j=js,je+1
3340  DO i=is,ie
3341  u(i, j, k) = u(i, j, k)/(1.+0.5*(u2f(i, j-1, k)+u2f(i, j, k)))
3342  END DO
3343  END DO
3344  DO j=js,je
3345  DO i=is,ie+1
3346  v(i, j, k) = v(i, j, k)/(1.+0.5*(u2f(i-1, j, k)+u2f(i, j, k)))
3347  END DO
3348  END DO
3349  IF (.NOT.hydrostatic) THEN
3350  DO j=js,je
3351  DO i=is,ie
3352  w(i, j, k) = w(i, j, k)/(1.+u2f(i, j, k))
3353  END DO
3354  END DO
3355  END IF
3356  END DO
3357  END SUBROUTINE rayleigh_friction
3358 ! Differentiation of compute_aam in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
3359 !mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_core
3360 !_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_mod.
3361 !mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayleig
3362 !h_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_or
3363 !d4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.r
3364 !emap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap_2d
3365 !fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiters
3366 ! fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic fv_
3367 !restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subgr
3368 !id_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_utils
3369 !_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_utils_m
3370 !od.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_mo
3371 !d.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_mod.d
3372 !2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.ytp_v
3373 !_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_core
3374 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
3375 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
3376 ! gradient of useful results: u v delp ua aam va m_fac ps
3377 ! with respect to varying inputs: u v delp ua aam va m_fac ps
3378  SUBROUTINE compute_aam_fwd(npz, is, ie, js, je, isd, ied, jsd, jed, &
3379 & gridstruct, bd, ptop, ua, va, u, v, delp, aam, ps, m_fac)
3380  IMPLICIT NONE
3381 ! Compute vertically (mass) integrated Atmospheric Angular Momentum
3382  INTEGER, INTENT(IN) :: npz
3383  INTEGER, INTENT(IN) :: is, ie, js, je
3384  INTEGER, INTENT(IN) :: isd, ied, jsd, jed
3385  REAL, INTENT(IN) :: ptop
3386 ! D grid zonal wind (m/s)
3387  REAL, INTENT(INOUT) :: u(isd:ied, jsd:jed+1, npz)
3388 ! D grid meridional wind (m/s)
3389  REAL, INTENT(INOUT) :: v(isd:ied+1, jsd:jed, npz)
3390  REAL, INTENT(INOUT) :: delp(isd:ied, jsd:jed, npz)
3391  REAL, DIMENSION(isd:ied, jsd:jed, npz), INTENT(INOUT) :: ua, va
3392  REAL :: aam(is:ie, js:je)
3393  REAL :: m_fac(is:ie, js:je)
3394  REAL :: ps(isd:ied, jsd:jed)
3395  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
3396  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
3397 ! local:
3398  REAL, DIMENSION(is:ie) :: r1, r2, dm
3399  INTEGER :: i, j, k
3400  INTRINSIC cos
3401 
3402  r1 = 0.0
3403  r2 = 0.0
3404  dm = 0.0
3405 
3406  CALL c2l_ord2_fwd(u, v, ua, va, gridstruct, npz, gridstruct%&
3407 & grid_type, bd, gridstruct%nested)
3408 !$OMP parallel do default(none) shared(is,ie,js,je,npz,gridstruct,aam,m_fac,ps,ptop,delp,agrav,ua) &
3409 !$OMP private(r1, r2, dm)
3410  DO j=js,je
3411  DO i=is,ie
3412  CALL pushrealarray(r1(i))
3413  r1(i) = radius*cos(gridstruct%agrid(i, j, 2))
3414  CALL pushrealarray(r2(i))
3415  r2(i) = r1(i)*r1(i)
3416  aam(i, j) = 0.
3417  m_fac(i, j) = 0.
3418  ps(i, j) = ptop
3419  END DO
3420  DO k=1,npz
3421  DO i=is,ie
3422  CALL pushrealarray(dm(i))
3423  dm(i) = delp(i, j, k)
3424  ps(i, j) = ps(i, j) + dm(i)
3425  dm(i) = dm(i)*agrav
3426  aam(i, j) = aam(i, j) + (r2(i)*omega+r1(i)*ua(i, j, k))*dm(i)
3427  m_fac(i, j) = m_fac(i, j) + dm(i)*r2(i)
3428  END DO
3429  END DO
3430  END DO
3431  CALL pushrealarray(r2, ie - is + 1)
3432  CALL pushrealarray(r1, ie - is + 1)
3433  CALL pushrealarray(dm, ie - is + 1)
3434  END SUBROUTINE compute_aam_fwd
3435 ! Differentiation of compute_aam in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
3436 !_mod.a2b_ord2 dyn_core_mod.dyn_core dyn_core_mod.pk3_halo dyn_core_mod.pln_halo dyn_core_mod.pe_halo dyn_core_mod.adv_pe dyn_cor
3437 !e_mod.p_grad_c dyn_core_mod.nh_p_grad dyn_core_mod.split_p_grad dyn_core_mod.one_grad_p dyn_core_mod.grad1_p_update dyn_core_mod
3438 !.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Raylei
3439 !gh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_o
3440 !rd4 fv_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.
3441 !remap_z fv_mapz_mod.map_scalar fv_mapz_mod.map1_ppm fv_mapz_mod.mapn_tracer fv_mapz_mod.map1_q2 fv_mapz_mod.remap_2d
3442 ! fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiter
3443 !s fv_mapz_mod.steepz fv_mapz_mod.rst_remap fv_mapz_mod.mappm fv_mapz_mod.moist_cv fv_mapz_mod.moist_cp fv_mapz_mod.map1_cubic fv
3444 !_restart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subg
3445 !rid_z main_mod.compute_pressures main_mod.run nh_core_mod.Riem_Solver3 nh_utils_mod.update_dz_c nh_utils_mod.update_dz_d nh_util
3446 !s_mod.Riem_Solver_c nh_utils_mod.Riem_Solver3test nh_utils_mod.imp_diff_w nh_utils_mod.RIM_2D nh_utils_mod.SIM3_solver nh_utils_
3447 !mod.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_m
3448 !od.nest_halo_nh sw_core_mod.c_sw sw_core_mod.d_sw sw_core_mod.divergence_corner sw_core_mod.divergence_corner_nest sw_core_mod.
3449 !d2a2c_vect sw_core_mod.fill3_4corners sw_core_mod.fill2_4corners sw_core_mod.fill_4corners sw_core_mod.xtp_u sw_core_mod.ytp_
3450 !v_fb sw_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_cor
3451 !e_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_uti
3452 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
3453 ! gradient of useful results: u v delp ua aam va m_fac ps
3454 ! with respect to varying inputs: u v delp ua aam va m_fac ps
3455  SUBROUTINE compute_aam_bwd(npz, is, ie, js, je, isd, ied, jsd, jed, &
3456 & gridstruct, bd, ptop, ua, ua_ad, va, va_ad, u, u_ad, v, v_ad, delp, &
3457 & delp_ad, aam, aam_ad, ps, ps_ad, m_fac, m_fac_ad)
3458  IMPLICIT NONE
3459  INTEGER, INTENT(IN) :: npz
3460  INTEGER, INTENT(IN) :: is, ie, js, je
3461  INTEGER, INTENT(IN) :: isd, ied, jsd, jed
3462  REAL, INTENT(IN) :: ptop
3463  REAL, INTENT(INOUT) :: u(isd:ied, jsd:jed+1, npz)
3464  REAL, INTENT(INOUT) :: u_ad(isd:ied, jsd:jed+1, npz)
3465  REAL, INTENT(INOUT) :: v(isd:ied+1, jsd:jed, npz)
3466  REAL, INTENT(INOUT) :: v_ad(isd:ied+1, jsd:jed, npz)
3467  REAL, INTENT(INOUT) :: delp(isd:ied, jsd:jed, npz)
3468  REAL, INTENT(INOUT) :: delp_ad(isd:ied, jsd:jed, npz)
3469  REAL, DIMENSION(isd:ied, jsd:jed, npz), INTENT(INOUT) :: ua, va
3470  REAL, DIMENSION(isd:ied, jsd:jed, npz), INTENT(INOUT) :: ua_ad, &
3471 & va_ad
3472  REAL :: aam(is:ie, js:je)
3473  REAL :: aam_ad(is:ie, js:je)
3474  REAL :: m_fac(is:ie, js:je)
3475  REAL :: m_fac_ad(is:ie, js:je)
3476  REAL :: ps(isd:ied, jsd:jed)
3477  REAL :: ps_ad(isd:ied, jsd:jed)
3478  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
3479  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
3480  REAL, DIMENSION(is:ie) :: r1, r2, dm
3481  REAL, DIMENSION(is:ie) :: dm_ad
3482  INTEGER :: i, j, k
3483  INTRINSIC cos
3484 
3485  r1 = 0.0
3486  r2 = 0.0
3487  dm = 0.0
3488 
3489  CALL poprealarray(dm, ie - is + 1)
3490  CALL poprealarray(r1, ie - is + 1)
3491  CALL poprealarray(r2, ie - is + 1)
3492  dm_ad = 0.0
3493  DO j=je,js,-1
3494  DO k=npz,1,-1
3495  DO i=ie,is,-1
3496  dm_ad(i) = dm_ad(i) + (r2(i)*omega+r1(i)*ua(i, j, k))*aam_ad(i&
3497 & , j) + r2(i)*m_fac_ad(i, j)
3498  ua_ad(i, j, k) = ua_ad(i, j, k) + dm(i)*r1(i)*aam_ad(i, j)
3499  dm_ad(i) = ps_ad(i, j) + agrav*dm_ad(i)
3500  CALL poprealarray(dm(i))
3501  delp_ad(i, j, k) = delp_ad(i, j, k) + dm_ad(i)
3502  dm_ad(i) = 0.0
3503  END DO
3504  END DO
3505  DO i=ie,is,-1
3506  ps_ad(i, j) = 0.0
3507  m_fac_ad(i, j) = 0.0
3508  aam_ad(i, j) = 0.0
3509  CALL poprealarray(r2(i))
3510  CALL poprealarray(r1(i))
3511  END DO
3512  END DO
3513  CALL c2l_ord2_bwd(u, u_ad, v, v_ad, ua, ua_ad, va, va_ad, gridstruct&
3514 & , npz, gridstruct%grid_type, bd, gridstruct%nested)
3515  END SUBROUTINE compute_aam_bwd
3516  SUBROUTINE compute_aam(npz, is, ie, js, je, isd, ied, jsd, jed, &
3517 & gridstruct, bd, ptop, ua, va, u, v, delp, aam, ps, m_fac)
3518  IMPLICIT NONE
3519 ! Compute vertically (mass) integrated Atmospheric Angular Momentum
3520  INTEGER, INTENT(IN) :: npz
3521  INTEGER, INTENT(IN) :: is, ie, js, je
3522  INTEGER, INTENT(IN) :: isd, ied, jsd, jed
3523  REAL, INTENT(IN) :: ptop
3524 ! D grid zonal wind (m/s)
3525  REAL, INTENT(INOUT) :: u(isd:ied, jsd:jed+1, npz)
3526 ! D grid meridional wind (m/s)
3527  REAL, INTENT(INOUT) :: v(isd:ied+1, jsd:jed, npz)
3528  REAL, INTENT(INOUT) :: delp(isd:ied, jsd:jed, npz)
3529  REAL, DIMENSION(isd:ied, jsd:jed, npz), INTENT(INOUT) :: ua, va
3530  REAL, INTENT(OUT) :: aam(is:ie, js:je)
3531  REAL, INTENT(OUT) :: m_fac(is:ie, js:je)
3532  REAL, INTENT(OUT) :: ps(isd:ied, jsd:jed)
3533  TYPE(FV_GRID_BOUNDS_TYPE), INTENT(IN) :: bd
3534  TYPE(FV_GRID_TYPE), INTENT(IN) :: gridstruct
3535 ! local:
3536  REAL, DIMENSION(is:ie) :: r1, r2, dm
3537  INTEGER :: i, j, k
3538  INTRINSIC cos
3539  CALL c2l_ord2(u, v, ua, va, gridstruct, npz, gridstruct%grid_type, &
3540 & bd, gridstruct%nested)
3541 !$OMP parallel do default(none) shared(is,ie,js,je,npz,gridstruct,aam,m_fac,ps,ptop,delp,agrav,ua) &
3542 !$OMP private(r1, r2, dm)
3543  DO j=js,je
3544  DO i=is,ie
3545  r1(i) = radius*cos(gridstruct%agrid(i, j, 2))
3546  r2(i) = r1(i)*r1(i)
3547  aam(i, j) = 0.
3548  m_fac(i, j) = 0.
3549  ps(i, j) = ptop
3550  END DO
3551  DO k=1,npz
3552  DO i=is,ie
3553  dm(i) = delp(i, j, k)
3554  ps(i, j) = ps(i, j) + dm(i)
3555  dm(i) = dm(i)*agrav
3556  aam(i, j) = aam(i, j) + (r2(i)*omega+r1(i)*ua(i, j, k))*dm(i)
3557  m_fac(i, j) = m_fac(i, j) + dm(i)*r2(i)
3558  END DO
3559  END DO
3560  END DO
3561  END SUBROUTINE compute_aam
3562 end module fv_dynamics_adm_mod
subroutine rayleigh_super_fwd(dt, npx, npy, npz, ks, pm, phis, tau, u, v, w, pt, ua, va, delz, agrid, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
subroutine, public nested_grid_bc_apply_intt(var_nest, istag, jstag, npx, npy, npz, bd, step, split, bc, bctype)
real, parameter, public radius
Radius of the Earth [m].
Definition: constants.F90:72
integer, parameter, public model_atmos
real, parameter, public omega
Rotation rate of the Earth [1/s].
Definition: constants.F90:75
subroutine, public del2_cubed(q, cd, gridstruct, domain, npx, npy, km, nmax, bd)
subroutine, public compute_total_energy_bwd(is, ie, js, je, isd, ied, jsd, jed, km, u, u_ad, v, v_ad, w, w_ad, delz, delz_ad, pt, pt_ad, delp, delp_ad, q, q_ad, qc, qc_ad, pe, pe_ad, peln, peln_ad, hs, rsin2_l, cosa_s_l, r_vir, cp, rg, hlv, te_2d, te_2d_ad, ua, va, teq, teq_ad, moist_phys, nwat, sphum, liq_wat, rainwat, ice_wat, snowwat, graupel, hydrostatic, id_te)
real(kind=8), parameter, public pi_8
Ratio of circle circumference to diameter [N/A].
Definition: constants.F90:73
subroutine, public del2_cubed_bwd(q, q_ad, cd, gridstruct, domain, npx, npy, km, nmax, bd)
logical, save, public idealtest
real, parameter, public hlv
Latent heat of evaporation [J/kg].
Definition: constants.F90:80
subroutine, public nested_grid_bc_apply_intt_adm(var_nest, var_nest_ad, istag, jstag, npx, npy, npz, bd, step, split, bc, bctype)
subroutine, public pushcontrol(ctype, field)
subroutine, public tracer_2d_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad, cy, cy_ad, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine, public g_sum_adm(domain, p, p_ad, ifirst, ilast, jfirst, jlast, ngc, area, mode, reproduce, g_sum_ad)
subroutine compute_aam(npz, is, ie, js, je, isd, ied, jsd, jed, gridstruct, bd, ptop, ua, va, u, v, delp, aam, ps, m_fac)
real, parameter, public rdgas
Gas constant for dry air [J/kg/deg].
Definition: constants.F90:77
subroutine, public dyn_core_bwd(npx, npy, npz, ng, sphum, nq, bdt, n_split, zvir, cp, akap, cappa, grav, hydrostatic, u, u_ad, v, v_ad, w, w_ad, delz, delz_ad, pt, pt_ad, q, q_ad, delp, delp_ad, pe, pe_ad, pk, pk_ad, phis, ws, ws_ad, omga, omga_ad, ptop, pfull, ua, ua_ad, va, va_ad, uc, uc_ad, vc, vc_ad, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad, cy, cy_ad, pkz, pkz_ad, peln, peln_ad, q_con, ak, bk, dpx, dpx_ad, ks, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, domain, init_step, i_pack, end_step, gz, gz_ad, pkc, pkc_ad, ptc, ptc_ad, crx, crx_ad, xfx, xfx_ad, cry, cry_ad, yfx, yfx_ad, divgd, divgd_ad, delpc, delpc_ad, ut, ut_ad, vt, vt_ad, zh, zh_ad, pk3, pk3_ad, du, du_ad, dv, dv_ad, time_total)
real, parameter, public cp_vapor
Specific heat capacity of water vapor at constant pressure [J/kg/deg].
Definition: constants.F90:89
subroutine rayleigh_super_bwd(dt, npx, npy, npz, ks, pm, phis, tau, u, u_ad, v, v_ad, w, w_ad, pt, pt_ad, ua, ua_ad, va, va_ad, delz, agrid, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
subroutine rayleigh_friction_fwd(dt, npx, npy, npz, ks, pm, tau, u, v, w, pt, ua, va, delz, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
Definition: mpp.F90:39
subroutine, public tracer_2d_nested(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, k_split, neststruct, parent_grid, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr)
subroutine, public tracer_2d_1l(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine, public compute_total_energy(is, ie, js, je, isd, ied, jsd, jed, km, u, v, w, delz, pt, delp, q, qc, pe, peln, hs, rsin2_l, cosa_s_l, r_vir, cp, rg, hlv, te_2d, ua, va, teq, moist_phys, nwat, sphum, liq_wat, rainwat, ice_wat, snowwat, graupel, hydrostatic, id_te)
subroutine compute_aam_bwd(npz, is, ie, js, je, isd, ied, jsd, jed, gridstruct, bd, ptop, ua, ua_ad, va, va_ad, u, u_ad, v, v_ad, delp, delp_ad, aam, aam_ad, ps, ps_ad, m_fac, m_fac_ad)
subroutine, public fv_dynamics_bwd(npx, npy, npz, nq_tot, ng, bdt, consv_te, fill, reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, q_split, u, u_ad, v, v_ad, w, w_ad, delz, delz_ad, hydrostatic, pt, pt_ad, delp, delp_ad, q, q_ad, ps, ps_ad, pe, pe_ad, pk, pk_ad, peln, peln_ad, pkz, pkz_ad, phis, q_con, omga, omga_ad, ua, ua_ad, va, va_ad, uc, uc_ad, vc, vc_ad, ak, bk, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad, cy, cy_ad, ze0, hybrid_z, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, parent_grid, domain, time_total)
subroutine compute_aam_fwd(npz, is, ie, js, je, isd, ied, jsd, jed, gridstruct, bd, ptop, ua, va, u, v, delp, aam, ps, m_fac)
subroutine, public moist_cp(is, ie, isd, ied, jsd, jed, km, j, k, nwat, sphum, liq_wat, rainwat, ice_wat, snowwat, graupel, q, qd, cpm, t1)
subroutine, public setup_nested_grid_bcs_adm(npx, npy, npz, zvir, ncnst, u, u_ad, v, v_ad, w, pt, delp, delz, q, uc, uc_ad, vc, vc_ad, pkz, nested, inline_q, make_nh, ng, gridstruct, flagstruct, neststruct, nest_timestep, tracer_nest_timestep, domain, bd, nwat)
real, parameter, public rvgas
Gas constant for water vapor [J/kg/deg].
Definition: constants.F90:78
subroutine, public c2l_ord2_fwd(u, v, ua, va, gridstruct, km, grid_type, bd, do_halo)
subroutine, public lagrangian_to_eulerian_bwd(last_step, consv, ps, ps_ad, pe, pe_ad, delp, delp_ad, pkz, pkz_ad, pk, pk_ad, mdt, pdt, km, is, ie, js, je, isd, ied, jsd, jed, nq, nwat, sphum, q_con, u, u_ad, v, v_ad, w, w_ad, delz, delz_ad, pt, pt_ad, q, q_ad, hs, r_vir, cp, akap, cappa, kord_mt, kord_wz, kord_tr, kord_tm, peln, peln_ad, te0_2d, te0_2d_ad, ng, ua, ua_ad, va, omga, omga_ad, te, te_ad, ws, ws_ad, fill, reproduce_sum, out_dt, dtdt, ptop, ak, bk, pfull, flagstruct, gridstruct, domain, do_sat_adj, hydrostatic, hybrid_z, do_omega, adiabatic, do_adiabatic_init, mfx, mfy, remap_option, kord_mt_pert, kord_wz_pert, kord_tr_pert, kord_tm_pert)
subroutine timing_on(blk_name)
subroutine, public moist_cv(is, ie, isd, ied, jsd, jed, km, j, k, nwat, sphum, liq_wat, rainwat, ice_wat, snowwat, graupel, q, qd, cvm, t1)
subroutine rayleigh_super(dt, npx, npy, npz, ks, pm, phis, tau, u, v, w, pt, ua, va, delz, agrid, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
subroutine, public c2l_ord2(u, v, ua, va, gridstruct, km, grid_type, bd, do_halo)
subroutine, public tracer_2d_1l_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine, public init_ijk_mem(i1, i2, j1, j2, km, array, var)
subroutine, public setup_nested_grid_bcs(npx, npy, npz, zvir, ncnst, u, v, w, pt, delp, delz, q, uc, vc, pkz, nested, inline_q, make_nh, ng, gridstruct, flagstruct, neststruct, nest_timestep, tracer_nest_timestep, domain, bd, nwat)
integer, parameter, public r_grid
logical, public do_adiabatic_init
subroutine, public tracer_2d_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine, public tracer_2d_1l_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad, cy, cy_ad, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine rayleigh_friction(dt, npx, npy, npz, ks, pm, tau, u, v, w, pt, ua, va, delz, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
subroutine, public tracer_2d(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr, dpa)
subroutine, public lagrangian_to_eulerian_fwd(last_step, consv, ps, pe, delp, pkz, pk, mdt, pdt, km, is, ie, js, je, isd, ied, jsd, jed, nq, nwat, sphum, q_con, u, v, w, delz, pt, q, hs, r_vir, cp, akap, cappa, kord_mt, kord_wz, kord_tr, kord_tm, peln, te0_2d, ng, ua, va, omga, te, ws, fill, reproduce_sum, out_dt, dtdt, ptop, ak, bk, pfull, flagstruct, gridstruct, domain, do_sat_adj, hydrostatic, hybrid_z, do_omega, adiabatic, do_adiabatic_init, mfx, mfy, remap_option, kord_mt_pert, kord_wz_pert, kord_tr_pert, kord_tm_pert)
Definition: fv_mapz_adm.F90:99
subroutine, public complete_group_halo_update(group, groupp, domain)
Definition: fv_mp_adm.F90:436
real, dimension(:), allocatable rf
subroutine, public tracer_2d_nested_bwd(q, q_ad, dp1, dp1_ad, mfx, mfx_ad, mfy, mfy_ad, cx, cx_ad, cy, cy_ad, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, k_split, neststruct, parent_grid, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr)
real, parameter, public grav
Acceleration due to gravity [m/s^2].
Definition: constants.F90:76
subroutine, public dyn_core_fwd(npx, npy, npz, ng, sphum, nq, bdt, n_split, zvir, cp, akap, cappa, grav, hydrostatic, u, v, w, delz, pt, q, delp, pe, pk, phis, ws, omga, ptop, pfull, ua, va, uc, vc, mfx, mfy, cx, cy, pkz, peln, q_con, ak, bk, dpx, ks, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, domain, init_step, i_pack, end_step, gz, pkc, ptc, crx, xfx, cry, yfx, divgd, delpc, ut, vt, zh, pk3, du, dv, time_total)
subroutine, public neg_adj3(is, ie, js, je, ng, kbot, hydrostatic, peln, delz, pt, dp, qv, ql, qr, qi, qs, qg, qa, check_negative)
Definition: fv_sg_nlm.F90:1132
real function, public g_sum(domain, p, ifirst, ilast, jfirst, jlast, ngc, area, mode, reproduce)
subroutine, public lagrangian_to_eulerian(last_step, consv, ps, pe, delp, pkz, pk, mdt, pdt, km, is, ie, js, je, isd, ied, jsd, jed, nq, nwat, sphum, q_con, u, v, w, delz, pt, q, hs, r_vir, cp, akap, cappa, kord_mt, kord_wz, kord_tr, kord_tm, peln, te0_2d, ng, ua, va, omga, te, ws, fill, reproduce_sum, out_dt, dtdt, ptop, ak, bk, pfull, flagstruct, gridstruct, domain, do_sat_adj, hydrostatic, hybrid_z, do_omega, adiabatic, do_adiabatic_init, mfx, mfy, remap_option, kord_mt_pert, kord_wz_pert, kord_tr_pert, kord_tm_pert)
subroutine, public cubed_to_latlon(u, v, ua, va, gridstruct, npx, npy, km, mode, grid_type, domain, nested, c2l_ord, bd)
subroutine, public tracer_2d_nested_fwd(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, nq, hord, q_split, dt, id_divg, q_pack, nord_tr, trdm, k_split, neststruct, parent_grid, hord_pert, nord_tr_pert, trdm_pert, split_damp_tr)
subroutine, public prt_mxm(qname, q, is, ie, js, je, n_g, km, fac, area, domain)
subroutine rayleigh_friction_bwd(dt, npx, npy, npz, ks, pm, tau, u, u_ad, v, v_ad, w, w_ad, pt, pt_ad, ua, ua_ad, va, va_ad, delz, delz_ad, cp, rg, ptop, hydrostatic, conserve, rf_cutoff, rf, gridstruct, domain, bd)
subroutine, public c2l_ord2_bwd(u, u_ad, v, v_ad, ua, ua_ad, va, va_ad, gridstruct, km, grid_type, bd, do_halo)
subroutine, public fill2d(is, ie, js, je, ng, km, q, delp, area, domain, nested, npx, npy)
subroutine, public fv_dynamics(npx, npy, npz, nq_tot, ng, bdt, consv_te, fill, reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, q_split, u, v, w, delz, hydrostatic, pt, delp, q, ps, pe, pk, peln, pkz, phis, q_con, omga, ua, va, uc, vc, ak, bk, mfx, mfy, cx, cy, ze0, hybrid_z, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, parent_grid, domain, time_total)
subroutine, public compute_total_energy_fwd(is, ie, js, je, isd, ied, jsd, jed, km, u, v, w, delz, pt, delp, q, qc, pe, peln, hs, rsin2_l, cosa_s_l, r_vir, cp, rg, hlv, te_2d, ua, va, teq, moist_phys, nwat, sphum, liq_wat, rainwat, ice_wat, snowwat, graupel, hydrostatic, id_te)
subroutine, public del2_cubed_fwd(q, cd, gridstruct, domain, npx, npy, km, nmax, bd)
integer, parameter fvprc
subroutine, public popcontrol(ctype, field)
subroutine, public range_check(qname, q, is, ie, js, je, n_g, km, pos, q_low, q_hi, bad_range)
subroutine, public fv_dynamics_fwd(npx, npy, npz, nq_tot, ng, bdt, consv_te, fill, reproduce_sum, kappa, cp_air, zvir, ptop, ks, ncnst, n_split, q_split, u, v, w, delz, hydrostatic, pt, delp, q, ps, pe, pk, peln, pkz, phis, q_con, omga, ua, va, uc, vc, ak, bk, mfx, mfy, cx, cy, ze0, hybrid_z, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, parent_grid, domain, time_total)
type(time_type), public fv_time
Derived type containing the data.
subroutine, public dyn_core(npx, npy, npz, ng, sphum, nq, bdt, n_split, zvir, cp, akap, cappa, grav, hydrostatic, u, v, w, delz, pt, q, delp, pe, pk, phis, ws, omga, ptop, pfull, ua, va, uc, vc, mfx, mfy, cx, cy, pkz, peln, q_con, ak, bk, dpx, ks, gridstruct, flagstruct, flagstructp, neststruct, idiag, bd, domain, init_step, i_pack, end_step, gz, pkc, ptc, crx, xfx, cry, yfx, divgd, delpc, ut, vt, zh, pk3, du, dv, time_total)
real(fp), parameter, public pi
subroutine timing_off(blk_name)