FV3 Bundle
nh_utils_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 !***********************************************************************
21 ! Developer: S.-J. Lin, NOAA/GFDL
22 ! To do list:
23 ! include moisture effect in pt
24 !------------------------------
25  use constants_mod, only: rdgas, cp_air, grav
26  use tp_core_adm_mod, only: fv_tp_2d
31 
34 
35  implicit none
36  private
37 
40  public sim3p0_solver, rim_2d
41  public riem_solver_c
45  public riem_solver_c_fwd
49  public riem_solver_c_bwd
50 
51  real, parameter:: dz_min = 2.
52  real, parameter:: r3 = 1./3.
53 
54 CONTAINS
55 ! Differentiation of update_dz_c in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
56 !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
57 !_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.
58 !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
59 !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
60 !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
61 !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
62 !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
63 ! 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_
64 !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
65 !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
66 !_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
67 !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
68 !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
69 !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
70 !_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
71 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
72 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
73 ! gradient of useful results: ws gz ut vt
74 ! with respect to varying inputs: ws gz ut vt
75  SUBROUTINE update_dz_c_fwd(is, ie, js, je, km, ng, dt, dp0, zs, area, &
76 & ut, vt, gz, ws, npx, npy, sw_corner, se_corner, ne_corner, nw_corner&
77 & , bd, grid_type)
78  IMPLICIT NONE
79 ! !INPUT PARAMETERS:
80  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
81  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy, grid_type
82  LOGICAL, INTENT(IN) :: sw_corner, se_corner, ne_corner, nw_corner
83  REAL, INTENT(IN) :: dt
84  REAL, INTENT(IN) :: dp0(km)
85  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: ut, vt
86  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng), INTENT(IN) :: area
87  REAL, INTENT(INOUT) :: gz(is-ng:ie+ng, js-ng:je+ng, km+1)
88  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
89  REAL :: ws(is-ng:ie+ng, js-ng:je+ng)
90 ! Local Work array:
91  REAL :: gz2(is-ng:ie+ng, js-ng:je+ng)
92  REAL, DIMENSION(is-1:ie+2, js-1:je+1) :: xfx, fx
93  REAL, DIMENSION(is-1:ie+1, js-1:je+2) :: yfx, fy
94  REAL, PARAMETER :: r14=1./14.
95  INTEGER :: i, j, k
96  INTEGER :: is1, ie1, js1, je1
97  INTEGER :: ie2, je2
98  REAL :: rdt, top_ratio, bot_ratio, int_ratio
99  INTRINSIC max
100 
101  gz2 = 0.0
102  xfx = 0.0
103  fx = 0.0
104  yfx = 0.0
105  fy = 0.0
106  is1 = 0
107  ie1 = 0
108  js1 = 0
109  je1 = 0
110  ie2 = 0
111  je2 = 0
112  rdt = 0.0
113  top_ratio = 0.0
114  bot_ratio = 0.0
115  int_ratio = 0.0
116 
117 !--------------------------------------------------------------------
118  rdt = 1./dt
119  top_ratio = dp0(1)/(dp0(1)+dp0(2))
120  bot_ratio = dp0(km)/(dp0(km-1)+dp0(km))
121  is1 = is - 1
122  js1 = js - 1
123  ie1 = ie + 1
124  je1 = je + 1
125  ie2 = ie + 2
126  je2 = je + 2
127 !$OMP parallel do default(none) shared(js1,je1,is1,ie2,km,je2,ie1,ut,top_ratio,vt, &
128 !$OMP bot_ratio,dp0,js,je,ng,is,ie,gz,grid_type, &
129 !$OMP bd,npx,npy,sw_corner,se_corner,ne_corner, &
130 !$OMP nw_corner,area) &
131 !$OMP private(gz2, xfx, yfx, fx, fy, int_ratio)
132  DO k=1,km+1
133  IF (k .EQ. 1) THEN
134  DO j=js1,je1
135  DO i=is1,ie2
136  CALL pushrealarray(xfx(i, j))
137  xfx(i, j) = ut(i, j, 1) + (ut(i, j, 1)-ut(i, j, 2))*&
138 & top_ratio
139  END DO
140  END DO
141  DO j=js1,je2
142  DO i=is1,ie1
143  CALL pushrealarray(yfx(i, j))
144  yfx(i, j) = vt(i, j, 1) + (vt(i, j, 1)-vt(i, j, 2))*&
145 & top_ratio
146  END DO
147  END DO
148  CALL pushcontrol(2,2)
149  ELSE IF (k .EQ. km + 1) THEN
150 ! Bottom extrapolation
151  DO j=js1,je1
152  DO i=is1,ie2
153  CALL pushrealarray(xfx(i, j))
154  xfx(i, j) = ut(i, j, km) + (ut(i, j, km)-ut(i, j, km-1))*&
155 & bot_ratio
156  END DO
157  END DO
158 ! xfx(i,j) = r14*(3.*ut(i,j,km-2)-13.*ut(i,j,km-1)+24.*ut(i,j,km))
159 ! if ( xfx(i,j)*ut(i,j,km)<0. ) xfx(i,j) = 0.
160  DO j=js1,je2
161  DO i=is1,ie1
162  CALL pushrealarray(yfx(i, j))
163  yfx(i, j) = vt(i, j, km) + (vt(i, j, km)-vt(i, j, km-1))*&
164 & bot_ratio
165  END DO
166  END DO
167  CALL pushcontrol(2,1)
168  ELSE
169 ! yfx(i,j) = r14*(3.*vt(i,j,km-2)-13.*vt(i,j,km-1)+24.*vt(i,j,km))
170 ! if ( yfx(i,j)*vt(i,j,km)<0. ) yfx(i,j) = 0.
171  CALL pushrealarray(int_ratio)
172  int_ratio = 1./(dp0(k-1)+dp0(k))
173  DO j=js1,je1
174  DO i=is1,ie2
175  CALL pushrealarray(xfx(i, j))
176  xfx(i, j) = (dp0(k)*ut(i, j, k-1)+dp0(k-1)*ut(i, j, k))*&
177 & int_ratio
178  END DO
179  END DO
180  DO j=js1,je2
181  DO i=is1,ie1
182  CALL pushrealarray(yfx(i, j))
183  yfx(i, j) = (dp0(k)*vt(i, j, k-1)+dp0(k-1)*vt(i, j, k))*&
184 & int_ratio
185  END DO
186  END DO
187  CALL pushcontrol(2,0)
188  END IF
189  DO j=js-ng,je+ng
190  DO i=is-ng,ie+ng
191  CALL pushrealarray(gz2(i, j))
192  gz2(i, j) = gz(i, j, k)
193  END DO
194  END DO
195  IF (grid_type .LT. 3) THEN
196  CALL fill_4corners_fwd(gz2, 1, bd, npx, npy, sw_corner, &
197 & se_corner, ne_corner, nw_corner)
198  CALL pushcontrol(1,1)
199  ELSE
200  CALL pushcontrol(1,0)
201  END IF
202  DO j=js1,je1
203  DO i=is1,ie2
204  IF (xfx(i, j) .GT. 0.) THEN
205  CALL pushrealarray(fx(i, j))
206  fx(i, j) = gz2(i-1, j)
207  CALL pushcontrol(1,0)
208  ELSE
209  CALL pushrealarray(fx(i, j))
210  fx(i, j) = gz2(i, j)
211  CALL pushcontrol(1,1)
212  END IF
213  CALL pushrealarray(fx(i, j))
214  fx(i, j) = xfx(i, j)*fx(i, j)
215  END DO
216  END DO
217  IF (grid_type .LT. 3) THEN
218  CALL fill_4corners_fwd(gz2, 2, bd, npx, npy, sw_corner, &
219 & se_corner, ne_corner, nw_corner)
220  CALL pushcontrol(1,1)
221  ELSE
222  CALL pushcontrol(1,0)
223  END IF
224  DO j=js1,je2
225  DO i=is1,ie1
226  IF (yfx(i, j) .GT. 0.) THEN
227  CALL pushrealarray(fy(i, j))
228  fy(i, j) = gz2(i, j-1)
229  CALL pushcontrol(1,0)
230  ELSE
231  CALL pushrealarray(fy(i, j))
232  fy(i, j) = gz2(i, j)
233  CALL pushcontrol(1,1)
234  END IF
235  CALL pushrealarray(fy(i, j))
236  fy(i, j) = yfx(i, j)*fy(i, j)
237  END DO
238  END DO
239  DO j=js1,je1
240  DO i=is1,ie1
241  CALL pushrealarray(gz(i, j, k))
242  gz(i, j, k) = (gz2(i, j)*area(i, j)+(fx(i, j)-fx(i+1, j))+(fy(&
243 & i, j)-fy(i, j+1)))/(area(i, j)+(xfx(i, j)-xfx(i+1, j))+(yfx(&
244 & i, j)-yfx(i, j+1)))
245  END DO
246  END DO
247  END DO
248 ! Enforce monotonicity of height to prevent blowup
249 !$OMP parallel do default(none) shared(is1,ie1,js1,je1,ws,zs,gz,rdt,km)
250  DO j=js1,je1
251  DO i=is1,ie1
252  CALL pushrealarray(ws(i, j))
253  ws(i, j) = (zs(i, j)-gz(i, j, km+1))*rdt
254  END DO
255  DO k=km,1,-1
256  DO i=is1,ie1
257  IF (gz(i, j, k) .LT. gz(i, j, k+1) + dz_min) THEN
258  CALL pushrealarray(gz(i, j, k))
259  gz(i, j, k) = gz(i, j, k+1) + dz_min
260  CALL pushcontrol(1,0)
261  ELSE
262  CALL pushrealarray(gz(i, j, k))
263  gz(i, j, k) = gz(i, j, k)
264  CALL pushcontrol(1,1)
265  END IF
266  END DO
267  END DO
268  END DO
269  CALL pushinteger(ie2)
270  CALL pushinteger(ie1)
271  CALL pushrealarray(fy, (ie-is+3)*(je-js+4))
272  CALL pushrealarray(fx, (ie-is+4)*(je-js+3))
273  CALL pushinteger(js1)
274  CALL pushrealarray(rdt)
275  CALL pushrealarray(yfx, (ie-is+3)*(je-js+4))
276  CALL pushinteger(je2)
277  CALL pushinteger(je1)
278  CALL pushrealarray(int_ratio)
279  CALL pushrealarray(top_ratio)
280  CALL pushrealarray(bot_ratio)
281  CALL pushinteger(is1)
282  CALL pushrealarray(gz2, (ie+2*ng-is+1)*(je+2*ng-js+1))
283  CALL pushrealarray(xfx, (ie-is+4)*(je-js+3))
284  END SUBROUTINE update_dz_c_fwd
285 ! Differentiation of update_dz_c in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
286 !_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
287 !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
288 !.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
289 !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
290 !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.
291 !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
292 ! 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
293 !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
294 !_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
295 !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
296 !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_
297 !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
298 !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.
299 !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_
300 !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
301 !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
302 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
303 ! gradient of useful results: ws gz ut vt
304 ! with respect to varying inputs: ws gz ut vt
305  SUBROUTINE update_dz_c_bwd(is, ie, js, je, km, ng, dt, dp0, zs, area, &
306 & ut, ut_ad, vt, vt_ad, gz, gz_ad, ws, ws_ad, npx, npy, sw_corner, &
307 & se_corner, ne_corner, nw_corner, bd, grid_type)
308  IMPLICIT NONE
309  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
310  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy, grid_type
311  LOGICAL, INTENT(IN) :: sw_corner, se_corner, ne_corner, nw_corner
312  REAL, INTENT(IN) :: dt
313  REAL, INTENT(IN) :: dp0(km)
314  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: ut, vt
315  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km) :: ut_ad, vt_ad
316  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng), INTENT(IN) :: area
317  REAL, INTENT(INOUT) :: gz(is-ng:ie+ng, js-ng:je+ng, km+1)
318  REAL, INTENT(INOUT) :: gz_ad(is-ng:ie+ng, js-ng:je+ng, km+1)
319  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
320  REAL :: ws(is-ng:ie+ng, js-ng:je+ng)
321  REAL :: ws_ad(is-ng:ie+ng, js-ng:je+ng)
322  REAL :: gz2(is-ng:ie+ng, js-ng:je+ng)
323  REAL :: gz2_ad(is-ng:ie+ng, js-ng:je+ng)
324  REAL, DIMENSION(is-1:ie+2, js-1:je+1) :: xfx, fx
325  REAL, DIMENSION(is-1:ie+2, js-1:je+1) :: xfx_ad, fx_ad
326  REAL, DIMENSION(is-1:ie+1, js-1:je+2) :: yfx, fy
327  REAL, DIMENSION(is-1:ie+1, js-1:je+2) :: yfx_ad, fy_ad
328  REAL, PARAMETER :: r14=1./14.
329  INTEGER :: i, j, k
330  INTEGER :: is1, ie1, js1, je1
331  INTEGER :: ie2, je2
332  REAL :: rdt, top_ratio, bot_ratio, int_ratio
333  INTRINSIC max
334  REAL :: temp
335  REAL :: temp_ad
336  REAL :: temp_ad0
337  INTEGER :: branch
338 
339  gz2 = 0.0
340  xfx = 0.0
341  fx = 0.0
342  yfx = 0.0
343  fy = 0.0
344  is1 = 0
345  ie1 = 0
346  js1 = 0
347  je1 = 0
348  ie2 = 0
349  je2 = 0
350  rdt = 0.0
351  top_ratio = 0.0
352  bot_ratio = 0.0
353  int_ratio = 0.0
354  branch = 0
355 
356  CALL poprealarray(xfx, (ie-is+4)*(je-js+3))
357  CALL poprealarray(gz2, (ie+2*ng-is+1)*(je+2*ng-js+1))
358  CALL popinteger(is1)
359  CALL poprealarray(bot_ratio)
360  CALL poprealarray(top_ratio)
361  CALL poprealarray(int_ratio)
362  CALL popinteger(je1)
363  CALL popinteger(je2)
364  CALL poprealarray(yfx, (ie-is+3)*(je-js+4))
365  CALL poprealarray(rdt)
366  CALL popinteger(js1)
367  CALL poprealarray(fx, (ie-is+4)*(je-js+3))
368  CALL poprealarray(fy, (ie-is+3)*(je-js+4))
369  CALL popinteger(ie1)
370  CALL popinteger(ie2)
371  DO j=je1,js1,-1
372  DO k=1,km,1
373  DO i=ie1,is1,-1
374  CALL popcontrol(1,branch)
375  IF (branch .EQ. 0) THEN
376  CALL poprealarray(gz(i, j, k))
377  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
378  gz_ad(i, j, k) = 0.0
379  ELSE
380  CALL poprealarray(gz(i, j, k))
381  END IF
382  END DO
383  END DO
384  DO i=ie1,is1,-1
385  CALL poprealarray(ws(i, j))
386  gz_ad(i, j, km+1) = gz_ad(i, j, km+1) - rdt*ws_ad(i, j)
387  ws_ad(i, j) = 0.0
388  END DO
389  END DO
390  xfx_ad = 0.0
391  gz2_ad = 0.0
392  yfx_ad = 0.0
393  fx_ad = 0.0
394  fy_ad = 0.0
395  DO k=km+1,1,-1
396  DO j=je1,js1,-1
397  DO i=ie1,is1,-1
398  CALL poprealarray(gz(i, j, k))
399  temp = area(i, j) + xfx(i, j) - xfx(i+1, j) + yfx(i, j) - yfx(&
400 & i, j+1)
401  temp_ad = gz_ad(i, j, k)/temp
402  temp_ad0 = -((area(i, j)*gz2(i, j)+fx(i, j)-fx(i+1, j)+fy(i, j&
403 & )-fy(i, j+1))*temp_ad/temp)
404  gz2_ad(i, j) = gz2_ad(i, j) + area(i, j)*temp_ad
405  fx_ad(i, j) = fx_ad(i, j) + temp_ad
406  fx_ad(i+1, j) = fx_ad(i+1, j) - temp_ad
407  fy_ad(i, j) = fy_ad(i, j) + temp_ad
408  fy_ad(i, j+1) = fy_ad(i, j+1) - temp_ad
409  xfx_ad(i, j) = xfx_ad(i, j) + temp_ad0
410  xfx_ad(i+1, j) = xfx_ad(i+1, j) - temp_ad0
411  yfx_ad(i, j) = yfx_ad(i, j) + temp_ad0
412  yfx_ad(i, j+1) = yfx_ad(i, j+1) - temp_ad0
413  gz_ad(i, j, k) = 0.0
414  END DO
415  END DO
416  DO j=je2,js1,-1
417  DO i=ie1,is1,-1
418  CALL poprealarray(fy(i, j))
419  yfx_ad(i, j) = yfx_ad(i, j) + fy(i, j)*fy_ad(i, j)
420  fy_ad(i, j) = yfx(i, j)*fy_ad(i, j)
421  CALL popcontrol(1,branch)
422  IF (branch .EQ. 0) THEN
423  CALL poprealarray(fy(i, j))
424  gz2_ad(i, j-1) = gz2_ad(i, j-1) + fy_ad(i, j)
425  fy_ad(i, j) = 0.0
426  ELSE
427  CALL poprealarray(fy(i, j))
428  gz2_ad(i, j) = gz2_ad(i, j) + fy_ad(i, j)
429  fy_ad(i, j) = 0.0
430  END IF
431  END DO
432  END DO
433  CALL popcontrol(1,branch)
434  IF (branch .NE. 0) CALL fill_4corners_bwd(gz2, gz2_ad, 2, bd, npx&
435 & , npy, sw_corner, se_corner, &
436 & ne_corner, nw_corner)
437  DO j=je1,js1,-1
438  DO i=ie2,is1,-1
439  CALL poprealarray(fx(i, j))
440  xfx_ad(i, j) = xfx_ad(i, j) + fx(i, j)*fx_ad(i, j)
441  fx_ad(i, j) = xfx(i, j)*fx_ad(i, j)
442  CALL popcontrol(1,branch)
443  IF (branch .EQ. 0) THEN
444  CALL poprealarray(fx(i, j))
445  gz2_ad(i-1, j) = gz2_ad(i-1, j) + fx_ad(i, j)
446  fx_ad(i, j) = 0.0
447  ELSE
448  CALL poprealarray(fx(i, j))
449  gz2_ad(i, j) = gz2_ad(i, j) + fx_ad(i, j)
450  fx_ad(i, j) = 0.0
451  END IF
452  END DO
453  END DO
454  CALL popcontrol(1,branch)
455  IF (branch .NE. 0) CALL fill_4corners_bwd(gz2, gz2_ad, 1, bd, npx&
456 & , npy, sw_corner, se_corner, &
457 & ne_corner, nw_corner)
458  DO j=je+ng,js-ng,-1
459  DO i=ie+ng,is-ng,-1
460  CALL poprealarray(gz2(i, j))
461  gz_ad(i, j, k) = gz_ad(i, j, k) + gz2_ad(i, j)
462  gz2_ad(i, j) = 0.0
463  END DO
464  END DO
465  CALL popcontrol(2,branch)
466  IF (branch .EQ. 0) THEN
467  DO j=je2,js1,-1
468  DO i=ie1,is1,-1
469  CALL poprealarray(yfx(i, j))
470  vt_ad(i, j, k-1) = vt_ad(i, j, k-1) + int_ratio*dp0(k)*&
471 & yfx_ad(i, j)
472  vt_ad(i, j, k) = vt_ad(i, j, k) + int_ratio*dp0(k-1)*yfx_ad(&
473 & i, j)
474  yfx_ad(i, j) = 0.0
475  END DO
476  END DO
477  DO j=je1,js1,-1
478  DO i=ie2,is1,-1
479  CALL poprealarray(xfx(i, j))
480  ut_ad(i, j, k-1) = ut_ad(i, j, k-1) + int_ratio*dp0(k)*&
481 & xfx_ad(i, j)
482  ut_ad(i, j, k) = ut_ad(i, j, k) + int_ratio*dp0(k-1)*xfx_ad(&
483 & i, j)
484  xfx_ad(i, j) = 0.0
485  END DO
486  END DO
487  CALL poprealarray(int_ratio)
488  ELSE IF (branch .EQ. 1) THEN
489  DO j=je2,js1,-1
490  DO i=ie1,is1,-1
491  CALL poprealarray(yfx(i, j))
492  vt_ad(i, j, km) = vt_ad(i, j, km) + (bot_ratio+1.0)*yfx_ad(i&
493 & , j)
494  vt_ad(i, j, km-1) = vt_ad(i, j, km-1) - bot_ratio*yfx_ad(i, &
495 & j)
496  yfx_ad(i, j) = 0.0
497  END DO
498  END DO
499  DO j=je1,js1,-1
500  DO i=ie2,is1,-1
501  CALL poprealarray(xfx(i, j))
502  ut_ad(i, j, km) = ut_ad(i, j, km) + (bot_ratio+1.0)*xfx_ad(i&
503 & , j)
504  ut_ad(i, j, km-1) = ut_ad(i, j, km-1) - bot_ratio*xfx_ad(i, &
505 & j)
506  xfx_ad(i, j) = 0.0
507  END DO
508  END DO
509  ELSE
510  DO j=je2,js1,-1
511  DO i=ie1,is1,-1
512  CALL poprealarray(yfx(i, j))
513  vt_ad(i, j, 1) = vt_ad(i, j, 1) + (top_ratio+1.0)*yfx_ad(i, &
514 & j)
515  vt_ad(i, j, 2) = vt_ad(i, j, 2) - top_ratio*yfx_ad(i, j)
516  yfx_ad(i, j) = 0.0
517  END DO
518  END DO
519  DO j=je1,js1,-1
520  DO i=ie2,is1,-1
521  CALL poprealarray(xfx(i, j))
522  ut_ad(i, j, 1) = ut_ad(i, j, 1) + (top_ratio+1.0)*xfx_ad(i, &
523 & j)
524  ut_ad(i, j, 2) = ut_ad(i, j, 2) - top_ratio*xfx_ad(i, j)
525  xfx_ad(i, j) = 0.0
526  END DO
527  END DO
528  END IF
529  END DO
530  END SUBROUTINE update_dz_c_bwd
531  SUBROUTINE update_dz_c(is, ie, js, je, km, ng, dt, dp0, zs, area, ut, &
532 & vt, gz, ws, npx, npy, sw_corner, se_corner, ne_corner, nw_corner, bd&
533 & , grid_type)
534  IMPLICIT NONE
535 ! !INPUT PARAMETERS:
536  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
537  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy, grid_type
538  LOGICAL, INTENT(IN) :: sw_corner, se_corner, ne_corner, nw_corner
539  REAL, INTENT(IN) :: dt
540  REAL, INTENT(IN) :: dp0(km)
541  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: ut, vt
542  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng), INTENT(IN) :: area
543  REAL, INTENT(INOUT) :: gz(is-ng:ie+ng, js-ng:je+ng, km+1)
544  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
545  REAL, INTENT(OUT) :: ws(is-ng:ie+ng, js-ng:je+ng)
546 ! Local Work array:
547  REAL :: gz2(is-ng:ie+ng, js-ng:je+ng)
548  REAL, DIMENSION(is-1:ie+2, js-1:je+1) :: xfx, fx
549  REAL, DIMENSION(is-1:ie+1, js-1:je+2) :: yfx, fy
550  REAL, PARAMETER :: r14=1./14.
551  INTEGER :: i, j, k
552  INTEGER :: is1, ie1, js1, je1
553  INTEGER :: ie2, je2
554  REAL :: rdt, top_ratio, bot_ratio, int_ratio
555  INTRINSIC max
556 !--------------------------------------------------------------------
557  rdt = 1./dt
558  top_ratio = dp0(1)/(dp0(1)+dp0(2))
559  bot_ratio = dp0(km)/(dp0(km-1)+dp0(km))
560  is1 = is - 1
561  js1 = js - 1
562  ie1 = ie + 1
563  je1 = je + 1
564  ie2 = ie + 2
565  je2 = je + 2
566 !$OMP parallel do default(none) shared(js1,je1,is1,ie2,km,je2,ie1,ut,top_ratio,vt, &
567 !$OMP bot_ratio,dp0,js,je,ng,is,ie,gz,grid_type, &
568 !$OMP bd,npx,npy,sw_corner,se_corner,ne_corner, &
569 !$OMP nw_corner,area) &
570 !$OMP private(gz2, xfx, yfx, fx, fy, int_ratio)
571  DO k=1,km+1
572  IF (k .EQ. 1) THEN
573  DO j=js1,je1
574  DO i=is1,ie2
575  xfx(i, j) = ut(i, j, 1) + (ut(i, j, 1)-ut(i, j, 2))*&
576 & top_ratio
577  END DO
578  END DO
579  DO j=js1,je2
580  DO i=is1,ie1
581  yfx(i, j) = vt(i, j, 1) + (vt(i, j, 1)-vt(i, j, 2))*&
582 & top_ratio
583  END DO
584  END DO
585  ELSE IF (k .EQ. km + 1) THEN
586 ! Bottom extrapolation
587  DO j=js1,je1
588  DO i=is1,ie2
589  xfx(i, j) = ut(i, j, km) + (ut(i, j, km)-ut(i, j, km-1))*&
590 & bot_ratio
591  END DO
592  END DO
593 ! xfx(i,j) = r14*(3.*ut(i,j,km-2)-13.*ut(i,j,km-1)+24.*ut(i,j,km))
594 ! if ( xfx(i,j)*ut(i,j,km)<0. ) xfx(i,j) = 0.
595  DO j=js1,je2
596  DO i=is1,ie1
597  yfx(i, j) = vt(i, j, km) + (vt(i, j, km)-vt(i, j, km-1))*&
598 & bot_ratio
599  END DO
600  END DO
601  ELSE
602 ! yfx(i,j) = r14*(3.*vt(i,j,km-2)-13.*vt(i,j,km-1)+24.*vt(i,j,km))
603 ! if ( yfx(i,j)*vt(i,j,km)<0. ) yfx(i,j) = 0.
604  int_ratio = 1./(dp0(k-1)+dp0(k))
605  DO j=js1,je1
606  DO i=is1,ie2
607  xfx(i, j) = (dp0(k)*ut(i, j, k-1)+dp0(k-1)*ut(i, j, k))*&
608 & int_ratio
609  END DO
610  END DO
611  DO j=js1,je2
612  DO i=is1,ie1
613  yfx(i, j) = (dp0(k)*vt(i, j, k-1)+dp0(k-1)*vt(i, j, k))*&
614 & int_ratio
615  END DO
616  END DO
617  END IF
618  DO j=js-ng,je+ng
619  DO i=is-ng,ie+ng
620  gz2(i, j) = gz(i, j, k)
621  END DO
622  END DO
623  IF (grid_type .LT. 3) CALL fill_4corners(gz2, 1, bd, npx, npy, &
624 & sw_corner, se_corner, ne_corner&
625 & , nw_corner)
626  DO j=js1,je1
627  DO i=is1,ie2
628  IF (xfx(i, j) .GT. 0.) THEN
629  fx(i, j) = gz2(i-1, j)
630  ELSE
631  fx(i, j) = gz2(i, j)
632  END IF
633  fx(i, j) = xfx(i, j)*fx(i, j)
634  END DO
635  END DO
636  IF (grid_type .LT. 3) CALL fill_4corners(gz2, 2, bd, npx, npy, &
637 & sw_corner, se_corner, ne_corner&
638 & , nw_corner)
639  DO j=js1,je2
640  DO i=is1,ie1
641  IF (yfx(i, j) .GT. 0.) THEN
642  fy(i, j) = gz2(i, j-1)
643  ELSE
644  fy(i, j) = gz2(i, j)
645  END IF
646  fy(i, j) = yfx(i, j)*fy(i, j)
647  END DO
648  END DO
649  DO j=js1,je1
650  DO i=is1,ie1
651  gz(i, j, k) = (gz2(i, j)*area(i, j)+(fx(i, j)-fx(i+1, j))+(fy(&
652 & i, j)-fy(i, j+1)))/(area(i, j)+(xfx(i, j)-xfx(i+1, j))+(yfx(&
653 & i, j)-yfx(i, j+1)))
654  END DO
655  END DO
656  END DO
657 ! Enforce monotonicity of height to prevent blowup
658 !$OMP parallel do default(none) shared(is1,ie1,js1,je1,ws,zs,gz,rdt,km)
659  DO j=js1,je1
660  DO i=is1,ie1
661  ws(i, j) = (zs(i, j)-gz(i, j, km+1))*rdt
662  END DO
663  DO k=km,1,-1
664  DO i=is1,ie1
665  IF (gz(i, j, k) .LT. gz(i, j, k+1) + dz_min) THEN
666  gz(i, j, k) = gz(i, j, k+1) + dz_min
667  ELSE
668  gz(i, j, k) = gz(i, j, k)
669  END IF
670  END DO
671  END DO
672  END DO
673  END SUBROUTINE update_dz_c
674 ! Differentiation of update_dz_d in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
675 !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
676 !_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.
677 !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
678 !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
679 !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
680 !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
681 !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
682 ! 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_
683 !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
684 !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
685 !_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
686 !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
687 !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
688 !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
689 !_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
690 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
691 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
692 ! gradient of useful results: xfx ws yfx zh crx cry
693 ! with respect to varying inputs: xfx ws yfx zh crx cry
694  SUBROUTINE update_dz_d_fwd(ndif, damp, hord, is, ie, js, je, km, ng, &
695 & npx, npy, area, rarea, dp0, zs, zh, crx, cry, xfx, yfx, delz, ws, &
696 & rdt, gridstruct, bd, hord_pert)
697  IMPLICIT NONE
698  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
699  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy
700  INTEGER, INTENT(IN) :: hord, hord_pert
701  REAL, INTENT(IN) :: rdt
702  REAL, INTENT(IN) :: dp0(km)
703  REAL, INTENT(IN) :: area(is-ng:ie+ng, js-ng:je+ng)
704  REAL, INTENT(IN) :: rarea(is-ng:ie+ng, js-ng:je+ng)
705  REAL, INTENT(INOUT) :: damp(km+1)
706  INTEGER, INTENT(INOUT) :: ndif(km+1)
707  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
708  REAL, INTENT(INOUT) :: zh(is-ng:ie+ng, js-ng:je+ng, km+1)
709  REAL, INTENT(OUT) :: delz(is-ng:ie+ng, js-ng:je+ng, km)
710  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km), INTENT(INOUT) :: crx, xfx
711  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km), INTENT(INOUT) :: cry, yfx
712  REAL :: ws(is:ie, js:je)
713  TYPE(fv_grid_type), INTENT(IN), TARGET :: gridstruct
714 !-----------------------------------------------------
715 ! Local array:
716  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km+1) :: crx_adv, xfx_adv
717  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km+1) :: cry_adv, yfx_adv
718  REAL, DIMENSION(is:ie+1, js:je) :: fx
719  REAL, DIMENSION(is:ie, js:je+1) :: fy
720  REAL, DIMENSION(is-ng:ie+ng+1, js-ng:je+ng) :: fx2
721  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng+1) :: fy2
722  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng) :: wk2, z2
723  REAL :: ra_x(is:ie, js-ng:je+ng)
724  REAL :: ra_y(is-ng:ie+ng, js:je)
725 !--------------------------------------------------------------------
726  INTEGER :: i, j, k, isd, ied, jsd, jed
727  LOGICAL :: uniform_grid
728  INTRINSIC max
729  INTEGER :: arg1
730 
731  crx_adv = 0.0
732  xfx_adv = 0.0
733  cry_adv = 0.0
734  yfx_adv = 0.0
735  fx = 0.0
736  fy = 0.0
737  fx2 = 0.0
738  fy2 = 0.0
739  wk2 = 0.0
740  z2 = 0.0
741  ra_x = 0.0
742  ra_y = 0.0
743  isd = 0
744  ied = 0
745  jsd = 0
746  jed = 0
747  arg1 = 0
748 
749  uniform_grid = .false.
750  CALL pushrealarray(damp(km+1))
751  damp(km+1) = damp(km)
752  CALL pushinteger(ndif(km+1))
753  ndif(km+1) = ndif(km)
754  isd = is - ng
755  ied = ie + ng
756  jsd = js - ng
757  jed = je + ng
758 !$OMP parallel do default(none) shared(jsd,jed,crx,xfx,crx_adv,xfx_adv,is,ie,isd,ied, &
759 !$OMP km,dp0,uniform_grid,js,je,cry,yfx,cry_adv,yfx_adv)
760  DO j=jsd,jed
761  arg1 = ie + 1
762  CALL edge_profile_fwd(crx, xfx, crx_adv, xfx_adv, is, arg1, jsd, &
763 & jed, j, km, dp0, uniform_grid, 0)
764  IF (j .LE. je + 1 .AND. j .GE. js) THEN
765  arg1 = je + 1
766  CALL edge_profile_fwd(cry, yfx, cry_adv, yfx_adv, isd, ied, js, &
767 & arg1, j, km, dp0, uniform_grid, 0)
768  CALL pushcontrol(1,1)
769  ELSE
770  CALL pushcontrol(1,0)
771  END IF
772  END DO
773 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,km,area,xfx_adv,yfx_adv, &
774 !$OMP damp,zh,crx_adv,cry_adv,npx,npy,hord,gridstruct,bd, &
775 !$OMP ndif,rarea) &
776 !$OMP private(z2, fx2, fy2, ra_x, ra_y, fx, fy,wk2)
777  DO k=1,km+1
778  DO j=jsd,jed
779  DO i=is,ie
780  CALL pushrealarray(ra_x(i, j))
781  ra_x(i, j) = area(i, j) + (xfx_adv(i, j, k)-xfx_adv(i+1, j, k)&
782 & )
783  END DO
784  END DO
785  DO j=js,je
786  DO i=isd,ied
787  CALL pushrealarray(ra_y(i, j))
788  ra_y(i, j) = area(i, j) + (yfx_adv(i, j, k)-yfx_adv(i, j+1, k)&
789 & )
790  END DO
791  END DO
792  IF (damp(k) .GT. 1.e-5) THEN
793  DO j=jsd,jed
794  DO i=isd,ied
795  CALL pushrealarray(z2(i, j))
796  z2(i, j) = zh(i, j, k)
797  END DO
798  END DO
799  IF (hord .EQ. hord_pert) THEN
800  CALL fv_tp_2d_fwd(z2, crx_adv(is:ie+1, jsd:jed, k), cry_adv&
801 & (isd:ied, js:je+1, k), npx, npy, hord, fx, fy, &
802 & xfx_adv(is:ie+1, jsd:jed, k), yfx_adv(isd:ied, &
803 & js:je+1, k), gridstruct, bd, ra_x, ra_y)
804  CALL pushcontrol(1,0)
805  ELSE
806  CALL pushrealarray(fy, (ie-is+1)*(je-js+2))
807  CALL pushrealarray(fx, (ie-is+2)*(je-js+1))
808  CALL pushrealarray(z2, (ie+2*ng-is+1)*(je+2*ng-js+1))
809  CALL fv_tp_2d(z2, crx_adv(is:ie+1, jsd:jed, k), cry_adv(isd:&
810 & ied, js:je+1, k), npx, npy, hord_pert, fx, fy, xfx_adv&
811 & (is:ie+1, jsd:jed, k), yfx_adv(isd:ied, js:je+1, k), &
812 & gridstruct, bd, ra_x, ra_y)
813  CALL pushcontrol(1,1)
814  END IF
815  CALL del6_vt_flux(ndif(k), npx, npy, damp(k), z2, wk2, fx2, fy2&
816 & , gridstruct, bd)
817  DO j=js,je
818  DO i=is,ie
819  zh(i, j, k) = (z2(i, j)*area(i, j)+(fx(i, j)-fx(i+1, j))+(fy&
820 & (i, j)-fy(i, j+1)))/(ra_x(i, j)+ra_y(i, j)-area(i, j)) + (&
821 & fx2(i, j)-fx2(i+1, j)+(fy2(i, j)-fy2(i, j+1)))*rarea(i, j)
822  END DO
823  END DO
824  CALL pushcontrol(1,1)
825  ELSE
826  IF (hord .EQ. hord_pert) THEN
827  CALL fv_tp_2d_fwd(zh(isd:ied, jsd:jed, k), crx_adv(is:ie+1&
828 & , jsd:jed, k), cry_adv(isd:ied, js:je+1, k), &
829 & npx, npy, hord, fx, fy, xfx_adv(is:ie+1, jsd:&
830 & jed, k), yfx_adv(isd:ied, js:je+1, k), &
831 & gridstruct, bd, ra_x, ra_y)
832  CALL pushcontrol(1,1)
833  ELSE
834  CALL pushrealarray(fy, (ie-is+1)*(je-js+2))
835  CALL pushrealarray(fx, (ie-is+2)*(je-js+1))
836  CALL pushrealarray(zh(isd:ied, jsd:jed, k), (ied-isd+1)*(jed-&
837 & jsd+1))
838  CALL fv_tp_2d(zh(isd:ied, jsd:jed, k), crx_adv(is:ie+1, jsd:&
839 & jed, k), cry_adv(isd:ied, js:je+1, k), npx, npy, &
840 & hord_pert, fx, fy, xfx_adv(is:ie+1, jsd:jed, k), &
841 & yfx_adv(isd:ied, js:je+1, k), gridstruct, bd, ra_x, &
842 & ra_y)
843  CALL pushcontrol(1,0)
844  END IF
845  DO j=js,je
846  DO i=is,ie
847  CALL pushrealarray(zh(i, j, k))
848  zh(i, j, k) = (zh(i, j, k)*area(i, j)+(fx(i, j)-fx(i+1, j))+&
849 & (fy(i, j)-fy(i, j+1)))/(ra_x(i, j)+ra_y(i, j)-area(i, j))
850  END DO
851  END DO
852  CALL pushcontrol(1,0)
853  END IF
854  END DO
855 ! zh(i,j,k) = rarea(i,j)*(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1)) &
856 ! + zh(i,j,k)*(3.-rarea(i,j)*(ra_x(i,j) + ra_y(i,j)))
857 !$OMP parallel do default(none) shared(is,ie,js,je,km,ws,zs,zh,rdt)
858  DO j=js,je
859  DO i=is,ie
860  CALL pushrealarray(ws(i, j))
861  ws(i, j) = (zs(i, j)-zh(i, j, km+1))*rdt
862  END DO
863  DO k=km,1,-1
864  DO i=is,ie
865  IF (zh(i, j, k) .LT. zh(i, j, k+1) + dz_min) THEN
866  CALL pushrealarray(zh(i, j, k))
867  zh(i, j, k) = zh(i, j, k+1) + dz_min
868  CALL pushcontrol(1,0)
869  ELSE
870  CALL pushrealarray(zh(i, j, k))
871  zh(i, j, k) = zh(i, j, k)
872  CALL pushcontrol(1,1)
873  END IF
874  END DO
875  END DO
876  END DO
877  CALL pushinteger(jed)
878  CALL pushrealarray(fy, (ie-is+1)*(je-js+2))
879  CALL pushrealarray(fx, (ie-is+2)*(je-js+1))
880  CALL pushinteger(isd)
881  CALL pushrealarray(ra_y, (ie+2*ng-is+1)*(je-js+1))
882  CALL pushrealarray(ra_x, (ie-is+1)*(je+2*ng-js+1))
883  CALL pushrealarray(cry_adv, (ie+2*ng-is+1)*(je-js+2)*(km+1))
884  CALL pushinteger(ied)
885  CALL pushrealarray(z2, (ie+2*ng-is+1)*(je+2*ng-js+1))
886  CALL pushinteger(jsd)
887  CALL pushrealarray(xfx_adv, (ie-is+2)*(je+2*ng-js+1)*(km+1))
888  CALL pushrealarray(crx_adv, (ie-is+2)*(je+2*ng-js+1)*(km+1))
889  CALL pushrealarray(yfx_adv, (ie+2*ng-is+1)*(je-js+2)*(km+1))
890  END SUBROUTINE update_dz_d_fwd
891 ! Differentiation of update_dz_d in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
892 !_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
893 !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
894 !.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
895 !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
896 !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.
897 !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
898 ! 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
899 !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
900 !_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
901 !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
902 !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_
903 !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
904 !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.
905 !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_
906 !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
907 !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
908 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
909 ! gradient of useful results: xfx ws yfx zh crx cry
910 ! with respect to varying inputs: xfx ws yfx zh crx cry
911  SUBROUTINE update_dz_d_bwd(ndif, damp, hord, is, ie, js, je, km, ng, &
912 & npx, npy, area, rarea, dp0, zs, zh, zh_ad, crx, crx_ad, cry, cry_ad&
913 & , xfx, xfx_ad, yfx, yfx_ad, delz, ws, ws_ad, rdt, gridstruct, bd, &
914 & hord_pert)
915  IMPLICIT NONE
916  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
917  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy
918  INTEGER, INTENT(IN) :: hord, hord_pert
919  REAL, INTENT(IN) :: rdt
920  REAL, INTENT(IN) :: dp0(km)
921  REAL, INTENT(IN) :: area(is-ng:ie+ng, js-ng:je+ng)
922  REAL, INTENT(IN) :: rarea(is-ng:ie+ng, js-ng:je+ng)
923  REAL, INTENT(INOUT) :: damp(km+1)
924  INTEGER, INTENT(INOUT) :: ndif(km+1)
925  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
926  REAL, INTENT(INOUT) :: zh(is-ng:ie+ng, js-ng:je+ng, km+1)
927  REAL, INTENT(INOUT) :: zh_ad(is-ng:ie+ng, js-ng:je+ng, km+1)
928  REAL, INTENT(OUT) :: delz(is-ng:ie+ng, js-ng:je+ng, km)
929  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km), INTENT(INOUT) :: crx, xfx
930  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km), INTENT(INOUT) :: crx_ad, &
931 & xfx_ad
932  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km), INTENT(INOUT) :: cry, yfx
933  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km), INTENT(INOUT) :: cry_ad, &
934 & yfx_ad
935  REAL :: ws(is:ie, js:je)
936  REAL :: ws_ad(is:ie, js:je)
937  TYPE(fv_grid_type), INTENT(IN), TARGET :: gridstruct
938  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km+1) :: crx_adv, xfx_adv
939  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km+1) :: crx_adv_ad, &
940 & xfx_adv_ad
941  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km+1) :: cry_adv, yfx_adv
942  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km+1) :: cry_adv_ad, &
943 & yfx_adv_ad
944  REAL, DIMENSION(is:ie+1, js:je) :: fx
945  REAL, DIMENSION(is:ie+1, js:je) :: fx_ad
946  REAL, DIMENSION(is:ie, js:je+1) :: fy
947  REAL, DIMENSION(is:ie, js:je+1) :: fy_ad
948  REAL, DIMENSION(is-ng:ie+ng+1, js-ng:je+ng) :: fx2
949  REAL, DIMENSION(is-ng:ie+ng+1, js-ng:je+ng) :: fx2_ad
950  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng+1) :: fy2
951  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng+1) :: fy2_ad
952  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng) :: wk2, z2
953  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng) :: wk2_ad, z2_ad
954  REAL :: ra_x(is:ie, js-ng:je+ng)
955  REAL :: ra_x_ad(is:ie, js-ng:je+ng)
956  REAL :: ra_y(is-ng:ie+ng, js:je)
957  REAL :: ra_y_ad(is-ng:ie+ng, js:je)
958  INTEGER :: i, j, k, isd, ied, jsd, jed
959  LOGICAL :: uniform_grid
960  INTRINSIC max
961  INTEGER :: arg1
962  REAL :: temp
963  REAL :: temp_ad
964  REAL :: temp_ad0
965  REAL :: temp_ad1
966  REAL :: temp0
967  REAL :: temp_ad2
968  REAL :: temp_ad3
969  INTEGER :: branch
970 
971  crx_adv = 0.0
972  xfx_adv = 0.0
973  cry_adv = 0.0
974  yfx_adv = 0.0
975  fx = 0.0
976  fy = 0.0
977  fx2 = 0.0
978  fy2 = 0.0
979  wk2 = 0.0
980  z2 = 0.0
981  ra_x = 0.0
982  ra_y = 0.0
983  isd = 0
984  ied = 0
985  jsd = 0
986  jed = 0
987  arg1 = 0
988  branch = 0
989 
990  CALL poprealarray(yfx_adv, (ie+2*ng-is+1)*(je-js+2)*(km+1))
991  CALL poprealarray(crx_adv, (ie-is+2)*(je+2*ng-js+1)*(km+1))
992  CALL poprealarray(xfx_adv, (ie-is+2)*(je+2*ng-js+1)*(km+1))
993  CALL popinteger(jsd)
994  CALL poprealarray(z2, (ie+2*ng-is+1)*(je+2*ng-js+1))
995  CALL popinteger(ied)
996  CALL poprealarray(cry_adv, (ie+2*ng-is+1)*(je-js+2)*(km+1))
997  CALL poprealarray(ra_x, (ie-is+1)*(je+2*ng-js+1))
998  CALL poprealarray(ra_y, (ie+2*ng-is+1)*(je-js+1))
999  CALL popinteger(isd)
1000  CALL poprealarray(fx, (ie-is+2)*(je-js+1))
1001  CALL poprealarray(fy, (ie-is+1)*(je-js+2))
1002  CALL popinteger(jed)
1003  DO j=je,js,-1
1004  DO k=1,km,1
1005  DO i=ie,is,-1
1006  CALL popcontrol(1,branch)
1007  IF (branch .EQ. 0) THEN
1008  CALL poprealarray(zh(i, j, k))
1009  zh_ad(i, j, k+1) = zh_ad(i, j, k+1) + zh_ad(i, j, k)
1010  zh_ad(i, j, k) = 0.0
1011  ELSE
1012  CALL poprealarray(zh(i, j, k))
1013  END IF
1014  END DO
1015  END DO
1016  DO i=ie,is,-1
1017  CALL poprealarray(ws(i, j))
1018  zh_ad(i, j, km+1) = zh_ad(i, j, km+1) - rdt*ws_ad(i, j)
1019  ws_ad(i, j) = 0.0
1020  END DO
1021  END DO
1022  yfx_adv_ad = 0.0
1023  crx_adv_ad = 0.0
1024  fy2_ad = 0.0
1025  wk2_ad = 0.0
1026  xfx_adv_ad = 0.0
1027  z2_ad = 0.0
1028  cry_adv_ad = 0.0
1029  ra_x_ad = 0.0
1030  ra_y_ad = 0.0
1031  fx_ad = 0.0
1032  fy_ad = 0.0
1033  fx2_ad = 0.0
1034  DO k=km+1,1,-1
1035  CALL popcontrol(1,branch)
1036  IF (branch .EQ. 0) THEN
1037  DO j=je,js,-1
1038  DO i=ie,is,-1
1039  CALL poprealarray(zh(i, j, k))
1040  temp0 = ra_x(i, j) - area(i, j) + ra_y(i, j)
1041  temp_ad2 = zh_ad(i, j, k)/temp0
1042  temp_ad3 = -((area(i, j)*zh(i, j, k)+fx(i, j)-fx(i+1, j)+fy(&
1043 & i, j)-fy(i, j+1))*temp_ad2/temp0)
1044  fx_ad(i, j) = fx_ad(i, j) + temp_ad2
1045  fx_ad(i+1, j) = fx_ad(i+1, j) - temp_ad2
1046  fy_ad(i, j) = fy_ad(i, j) + temp_ad2
1047  fy_ad(i, j+1) = fy_ad(i, j+1) - temp_ad2
1048  ra_x_ad(i, j) = ra_x_ad(i, j) + temp_ad3
1049  ra_y_ad(i, j) = ra_y_ad(i, j) + temp_ad3
1050  zh_ad(i, j, k) = area(i, j)*temp_ad2
1051  END DO
1052  END DO
1053  CALL popcontrol(1,branch)
1054  IF (branch .EQ. 0) THEN
1055  CALL poprealarray(zh(isd:ied, jsd:jed, k), (ied-isd+1)*(jed-&
1056 & jsd+1))
1057  CALL poprealarray(fx, (ie-is+2)*(je-js+1))
1058  CALL poprealarray(fy, (ie-is+1)*(je-js+2))
1059  CALL fv_tp_2d_adm(zh(isd:ied, jsd:jed, k), zh_ad(isd:ied, jsd:&
1060 & jed, k), crx_adv(is:ie+1, jsd:jed, k), crx_adv_ad(&
1061 & is:ie+1, jsd:jed, k), cry_adv(isd:ied, js:je+1, k)&
1062 & , cry_adv_ad(isd:ied, js:je+1, k), npx, npy, &
1063 & hord_pert, fx, fx_ad, fy, fy_ad, xfx_adv(is:ie+1, &
1064 & jsd:jed, k), xfx_adv_ad(is:ie+1, jsd:jed, k), &
1065 & yfx_adv(isd:ied, js:je+1, k), yfx_adv_ad(isd:ied, &
1066 & js:je+1, k), gridstruct, bd, ra_x, ra_x_ad, ra_y, &
1067 & ra_y_ad)
1068  ELSE
1069  CALL fv_tp_2d_bwd(zh(isd:ied, jsd:jed, k), zh_ad(isd:ied, &
1070 & jsd:jed, k), crx_adv(is:ie+1, jsd:jed, k), &
1071 & crx_adv_ad(is:ie+1, jsd:jed, k), cry_adv(isd:&
1072 & ied, js:je+1, k), cry_adv_ad(isd:ied, js:je+1, &
1073 & k), npx, npy, hord, fx, fx_ad, fy, fy_ad, &
1074 & xfx_adv(is:ie+1, jsd:jed, k), xfx_adv_ad(is:ie+&
1075 & 1, jsd:jed, k), yfx_adv(isd:ied, js:je+1, k), &
1076 & yfx_adv_ad(isd:ied, js:je+1, k), gridstruct, bd&
1077 & , ra_x, ra_x_ad, ra_y, ra_y_ad)
1078  END IF
1079  ELSE
1080  DO j=je,js,-1
1081  DO i=ie,is,-1
1082  temp = ra_x(i, j) - area(i, j) + ra_y(i, j)
1083  temp_ad = zh_ad(i, j, k)/temp
1084  temp_ad0 = -((area(i, j)*z2(i, j)+fx(i, j)-fx(i+1, j)+fy(i, &
1085 & j)-fy(i, j+1))*temp_ad/temp)
1086  temp_ad1 = rarea(i, j)*zh_ad(i, j, k)
1087  z2_ad(i, j) = z2_ad(i, j) + area(i, j)*temp_ad
1088  fx_ad(i, j) = fx_ad(i, j) + temp_ad
1089  fx_ad(i+1, j) = fx_ad(i+1, j) - temp_ad
1090  fy_ad(i, j) = fy_ad(i, j) + temp_ad
1091  fy_ad(i, j+1) = fy_ad(i, j+1) - temp_ad
1092  ra_x_ad(i, j) = ra_x_ad(i, j) + temp_ad0
1093  ra_y_ad(i, j) = ra_y_ad(i, j) + temp_ad0
1094  fx2_ad(i, j) = fx2_ad(i, j) + temp_ad1
1095  fx2_ad(i+1, j) = fx2_ad(i+1, j) - temp_ad1
1096  fy2_ad(i, j) = fy2_ad(i, j) + temp_ad1
1097  fy2_ad(i, j+1) = fy2_ad(i, j+1) - temp_ad1
1098  zh_ad(i, j, k) = 0.0
1099  END DO
1100  END DO
1101  CALL del6_vt_flux_adm(ndif(k), npx, npy, damp(k), z2, z2_ad, wk2&
1102 & , wk2_ad, fx2, fx2_ad, fy2, fy2_ad, gridstruct, &
1103 & bd)
1104  CALL popcontrol(1,branch)
1105  IF (branch .EQ. 0) THEN
1106  CALL fv_tp_2d_bwd(z2, z2_ad, crx_adv(is:ie+1, jsd:jed, k), &
1107 & crx_adv_ad(is:ie+1, jsd:jed, k), cry_adv(isd:&
1108 & ied, js:je+1, k), cry_adv_ad(isd:ied, js:je+1, &
1109 & k), npx, npy, hord, fx, fx_ad, fy, fy_ad, &
1110 & xfx_adv(is:ie+1, jsd:jed, k), xfx_adv_ad(is:ie+&
1111 & 1, jsd:jed, k), yfx_adv(isd:ied, js:je+1, k), &
1112 & yfx_adv_ad(isd:ied, js:je+1, k), gridstruct, bd&
1113 & , ra_x, ra_x_ad, ra_y, ra_y_ad)
1114  ELSE
1115  CALL poprealarray(z2, (ie+2*ng-is+1)*(je+2*ng-js+1))
1116  CALL poprealarray(fx, (ie-is+2)*(je-js+1))
1117  CALL poprealarray(fy, (ie-is+1)*(je-js+2))
1118  CALL fv_tp_2d_adm(z2, z2_ad, crx_adv(is:ie+1, jsd:jed, k), &
1119 & crx_adv_ad(is:ie+1, jsd:jed, k), cry_adv(isd:ied, &
1120 & js:je+1, k), cry_adv_ad(isd:ied, js:je+1, k), npx&
1121 & , npy, hord_pert, fx, fx_ad, fy, fy_ad, xfx_adv(is&
1122 & :ie+1, jsd:jed, k), xfx_adv_ad(is:ie+1, jsd:jed, k&
1123 & ), yfx_adv(isd:ied, js:je+1, k), yfx_adv_ad(isd:&
1124 & ied, js:je+1, k), gridstruct, bd, ra_x, ra_x_ad, &
1125 & ra_y, ra_y_ad)
1126  END IF
1127  DO j=jed,jsd,-1
1128  DO i=ied,isd,-1
1129  CALL poprealarray(z2(i, j))
1130  zh_ad(i, j, k) = zh_ad(i, j, k) + z2_ad(i, j)
1131  z2_ad(i, j) = 0.0
1132  END DO
1133  END DO
1134  END IF
1135  DO j=je,js,-1
1136  DO i=ied,isd,-1
1137  CALL poprealarray(ra_y(i, j))
1138  yfx_adv_ad(i, j, k) = yfx_adv_ad(i, j, k) + ra_y_ad(i, j)
1139  yfx_adv_ad(i, j+1, k) = yfx_adv_ad(i, j+1, k) - ra_y_ad(i, j)
1140  ra_y_ad(i, j) = 0.0
1141  END DO
1142  END DO
1143  DO j=jed,jsd,-1
1144  DO i=ie,is,-1
1145  CALL poprealarray(ra_x(i, j))
1146  xfx_adv_ad(i, j, k) = xfx_adv_ad(i, j, k) + ra_x_ad(i, j)
1147  xfx_adv_ad(i+1, j, k) = xfx_adv_ad(i+1, j, k) - ra_x_ad(i, j)
1148  ra_x_ad(i, j) = 0.0
1149  END DO
1150  END DO
1151  END DO
1152  DO j=jed,jsd,-1
1153  CALL popcontrol(1,branch)
1154  IF (branch .NE. 0) CALL edge_profile_bwd(cry, cry_ad, yfx, yfx_ad&
1155 & , cry_adv, cry_adv_ad, yfx_adv&
1156 & , yfx_adv_ad, isd, ied, js, &
1157 & arg1, j, km, dp0, uniform_grid&
1158 & , 0)
1159  arg1 = ie + 1
1160  CALL edge_profile_bwd(crx, crx_ad, xfx, xfx_ad, crx_adv, &
1161 & crx_adv_ad, xfx_adv, xfx_adv_ad, is, arg1, jsd, &
1162 & jed, j, km, dp0, uniform_grid, 0)
1163  END DO
1164  CALL popinteger(ndif(km+1))
1165  CALL poprealarray(damp(km+1))
1166  END SUBROUTINE update_dz_d_bwd
1167  SUBROUTINE update_dz_d(ndif, damp, hord, is, ie, js, je, km, ng, npx, &
1168 & npy, area, rarea, dp0, zs, zh, crx, cry, xfx, yfx, delz, ws, rdt, &
1169 & gridstruct, bd, hord_pert)
1170  IMPLICIT NONE
1171  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
1172  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km, npx, npy
1173  INTEGER, INTENT(IN) :: hord, hord_pert
1174  REAL, INTENT(IN) :: rdt
1175  REAL, INTENT(IN) :: dp0(km)
1176  REAL, INTENT(IN) :: area(is-ng:ie+ng, js-ng:je+ng)
1177  REAL, INTENT(IN) :: rarea(is-ng:ie+ng, js-ng:je+ng)
1178  REAL, INTENT(INOUT) :: damp(km+1)
1179  INTEGER, INTENT(INOUT) :: ndif(km+1)
1180  REAL, INTENT(IN) :: zs(is-ng:ie+ng, js-ng:je+ng)
1181  REAL, INTENT(INOUT) :: zh(is-ng:ie+ng, js-ng:je+ng, km+1)
1182  REAL, INTENT(OUT) :: delz(is-ng:ie+ng, js-ng:je+ng, km)
1183  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km), INTENT(INOUT) :: crx, xfx
1184  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km), INTENT(INOUT) :: cry, yfx
1185  REAL, INTENT(OUT) :: ws(is:ie, js:je)
1186  TYPE(fv_grid_type), INTENT(IN), TARGET :: gridstruct
1187 !-----------------------------------------------------
1188 ! Local array:
1189  REAL, DIMENSION(is:ie+1, js-ng:je+ng, km+1) :: crx_adv, xfx_adv
1190  REAL, DIMENSION(is-ng:ie+ng, js:je+1, km+1) :: cry_adv, yfx_adv
1191  REAL, DIMENSION(is:ie+1, js:je) :: fx
1192  REAL, DIMENSION(is:ie, js:je+1) :: fy
1193  REAL, DIMENSION(is-ng:ie+ng+1, js-ng:je+ng) :: fx2
1194  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng+1) :: fy2
1195  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng) :: wk2, z2
1196  REAL :: ra_x(is:ie, js-ng:je+ng)
1197  REAL :: ra_y(is-ng:ie+ng, js:je)
1198 !--------------------------------------------------------------------
1199  INTEGER :: i, j, k, isd, ied, jsd, jed
1200  LOGICAL :: uniform_grid
1201  INTRINSIC max
1202  INTEGER :: arg1
1203  uniform_grid = .false.
1204  damp(km+1) = damp(km)
1205  ndif(km+1) = ndif(km)
1206  isd = is - ng
1207  ied = ie + ng
1208  jsd = js - ng
1209  jed = je + ng
1210 !$OMP parallel do default(none) shared(jsd,jed,crx,xfx,crx_adv,xfx_adv,is,ie,isd,ied, &
1211 !$OMP km,dp0,uniform_grid,js,je,cry,yfx,cry_adv,yfx_adv)
1212  DO j=jsd,jed
1213  arg1 = ie + 1
1214  CALL edge_profile(crx, xfx, crx_adv, xfx_adv, is, arg1, jsd, jed, &
1215 & j, km, dp0, uniform_grid, 0)
1216  IF (j .LE. je + 1 .AND. j .GE. js) THEN
1217  arg1 = je + 1
1218  CALL edge_profile(cry, yfx, cry_adv, yfx_adv, isd, ied, js, arg1&
1219 & , j, km, dp0, uniform_grid, 0)
1220  END IF
1221  END DO
1222 !$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,km,area,xfx_adv,yfx_adv, &
1223 !$OMP damp,zh,crx_adv,cry_adv,npx,npy,hord,gridstruct,bd, &
1224 !$OMP ndif,rarea) &
1225 !$OMP private(z2, fx2, fy2, ra_x, ra_y, fx, fy,wk2)
1226  DO k=1,km+1
1227  DO j=jsd,jed
1228  DO i=is,ie
1229  ra_x(i, j) = area(i, j) + (xfx_adv(i, j, k)-xfx_adv(i+1, j, k)&
1230 & )
1231  END DO
1232  END DO
1233  DO j=js,je
1234  DO i=isd,ied
1235  ra_y(i, j) = area(i, j) + (yfx_adv(i, j, k)-yfx_adv(i, j+1, k)&
1236 & )
1237  END DO
1238  END DO
1239  IF (damp(k) .GT. 1.e-5) THEN
1240  DO j=jsd,jed
1241  DO i=isd,ied
1242  z2(i, j) = zh(i, j, k)
1243  END DO
1244  END DO
1245  IF (hord .EQ. hord_pert) THEN
1246  CALL fv_tp_2d(z2, crx_adv(is:ie+1, jsd:jed, k), cry_adv(isd&
1247 & :ied, js:je+1, k), npx, npy, hord, fx, fy, xfx_adv(&
1248 & is:ie+1, jsd:jed, k), yfx_adv(isd:ied, js:je+1, k)&
1249 & , gridstruct, bd, ra_x, ra_y)
1250  ELSE
1251  CALL fv_tp_2d(z2, crx_adv(is:ie+1, jsd:jed, k), cry_adv(isd:&
1252 & ied, js:je+1, k), npx, npy, hord_pert, fx, fy, xfx_adv&
1253 & (is:ie+1, jsd:jed, k), yfx_adv(isd:ied, js:je+1, k), &
1254 & gridstruct, bd, ra_x, ra_y)
1255  END IF
1256  CALL del6_vt_flux(ndif(k), npx, npy, damp(k), z2, wk2, fx2, fy2&
1257 & , gridstruct, bd)
1258  DO j=js,je
1259  DO i=is,ie
1260  zh(i, j, k) = (z2(i, j)*area(i, j)+(fx(i, j)-fx(i+1, j))+(fy&
1261 & (i, j)-fy(i, j+1)))/(ra_x(i, j)+ra_y(i, j)-area(i, j)) + (&
1262 & fx2(i, j)-fx2(i+1, j)+(fy2(i, j)-fy2(i, j+1)))*rarea(i, j)
1263  END DO
1264  END DO
1265  ELSE
1266  IF (hord .EQ. hord_pert) THEN
1267  CALL fv_tp_2d(zh(isd:ied, jsd:jed, k), crx_adv(is:ie+1, jsd&
1268 & :jed, k), cry_adv(isd:ied, js:je+1, k), npx, npy, &
1269 & hord, fx, fy, xfx_adv(is:ie+1, jsd:jed, k), yfx_adv&
1270 & (isd:ied, js:je+1, k), gridstruct, bd, ra_x, ra_y)
1271  ELSE
1272  CALL fv_tp_2d(zh(isd:ied, jsd:jed, k), crx_adv(is:ie+1, jsd:&
1273 & jed, k), cry_adv(isd:ied, js:je+1, k), npx, npy, &
1274 & hord_pert, fx, fy, xfx_adv(is:ie+1, jsd:jed, k), &
1275 & yfx_adv(isd:ied, js:je+1, k), gridstruct, bd, ra_x, &
1276 & ra_y)
1277  END IF
1278  DO j=js,je
1279  DO i=is,ie
1280  zh(i, j, k) = (zh(i, j, k)*area(i, j)+(fx(i, j)-fx(i+1, j))+&
1281 & (fy(i, j)-fy(i, j+1)))/(ra_x(i, j)+ra_y(i, j)-area(i, j))
1282  END DO
1283  END DO
1284  END IF
1285  END DO
1286 ! zh(i,j,k) = rarea(i,j)*(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1)) &
1287 ! + zh(i,j,k)*(3.-rarea(i,j)*(ra_x(i,j) + ra_y(i,j)))
1288 !$OMP parallel do default(none) shared(is,ie,js,je,km,ws,zs,zh,rdt)
1289  DO j=js,je
1290  DO i=is,ie
1291  ws(i, j) = (zs(i, j)-zh(i, j, km+1))*rdt
1292  END DO
1293  DO k=km,1,-1
1294  DO i=is,ie
1295  IF (zh(i, j, k) .LT. zh(i, j, k+1) + dz_min) THEN
1296  zh(i, j, k) = zh(i, j, k+1) + dz_min
1297  ELSE
1298  zh(i, j, k) = zh(i, j, k)
1299  END IF
1300  END DO
1301  END DO
1302  END DO
1303  END SUBROUTINE update_dz_d
1304 ! Differentiation of riem_solver_c in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edg
1305 !e_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_co
1306 !re_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_mo
1307 !d.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayle
1308 !igh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_
1309 !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_mod
1310 !.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_2
1311 !d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limite
1312 !rs 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 f
1313 !v_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_sub
1314 !grid_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_uti
1315 !ls_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
1316 !_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_
1317 !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_mod
1318 !.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
1319 !_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_co
1320 !re_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_ut
1321 !ils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
1322 ! gradient of useful results: ws gz delp w3 pef pt
1323 ! with respect to varying inputs: ws gz delp w3 pef pt
1324  SUBROUTINE riem_solver_c_fwd(ms, dt, is, ie, js, je, km, ng, akap, &
1325 & cappa, cp, ptop, hs, w3, pt, q_con, delp, gz, pef, ws, p_fac, a_imp&
1326 & , scale_m)
1327  IMPLICIT NONE
1328  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km
1329  INTEGER, INTENT(IN) :: ms
1330  REAL, INTENT(IN) :: dt, akap, cp, ptop, p_fac, a_imp, scale_m
1331  REAL, INTENT(IN) :: ws(is-ng:ie+ng, js-ng:je+ng)
1332  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: pt, &
1333 & delp
1334  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: q_con, &
1335 & cappa
1336  REAL, INTENT(IN) :: hs(is-ng:ie+ng, js-ng:je+ng)
1337  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: w3
1338 ! OUTPUT PARAMETERS
1339  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1), INTENT(INOUT) :: gz
1340  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1) :: pef
1341 ! Local:
1342  REAL, DIMENSION(is-1:ie+1, km) :: dm, dz2, w2, pm2, gm2, cp2
1343  REAL, DIMENSION(is-1:ie+1, km+1) :: pem, pe2, peg
1344  REAL :: gama, rgrav
1345  INTEGER :: i, j, k
1346  INTEGER :: is1, ie1
1347  INTRINSIC log
1348 
1349  dm = 0.0
1350  dz2 = 0.0
1351  w2 = 0.0
1352  pm2 = 0.0
1353  gm2 = 0.0
1354  cp2 = 0.0
1355  pem = 0.0
1356  pe2 = 0.0
1357  peg = 0.0
1358  gama = 0.0
1359  rgrav = 0.0
1360  is1 = 0
1361  ie1 = 0
1362 
1363  gama = 1./(1.-akap)
1364  rgrav = 1./grav
1365  is1 = is - 1
1366  ie1 = ie + 1
1367 !$OMP parallel do default(none) shared(js,je,is1,ie1,km,delp,pef,ptop,gz,rgrav,w3,pt, &
1368 !$OMP a_imp,dt,gama,akap,ws,p_fac,scale_m,ms,hs,q_con,cappa) &
1369 !$OMP private(cp2,gm2, dm, dz2, w2, pm2, pe2, pem, peg)
1370  DO j=js-1,je+1
1371  DO k=1,km
1372  DO i=is1,ie1
1373  CALL pushrealarray(dm(i, k))
1374  dm(i, k) = delp(i, j, k)
1375  END DO
1376  END DO
1377  DO i=is1,ie1
1378 ! full pressure at top
1379  CALL pushrealarray(pef(i, j, 1))
1380  pef(i, j, 1) = ptop
1381  CALL pushrealarray(pem(i, 1))
1382  pem(i, 1) = ptop
1383  END DO
1384  DO k=2,km+1
1385  DO i=is1,ie1
1386  CALL pushrealarray(pem(i, k))
1387  pem(i, k) = pem(i, k-1) + dm(i, k-1)
1388  END DO
1389  END DO
1390  DO k=1,km
1391  DO i=is1,ie1
1392  CALL pushrealarray(dz2(i, k))
1393  dz2(i, k) = gz(i, j, k+1) - gz(i, j, k)
1394  CALL pushrealarray(pm2(i, k))
1395  pm2(i, k) = dm(i, k)/log(pem(i, k+1)/pem(i, k))
1396  CALL pushrealarray(dm(i, k))
1397  dm(i, k) = dm(i, k)*rgrav
1398  CALL pushrealarray(w2(i, k))
1399  w2(i, k) = w3(i, j, k)
1400  END DO
1401  END DO
1402  IF (a_imp .LT. -0.01) THEN
1403  CALL sim3p0_solver_fwd(dt, is1, ie1, km, rdgas, gama, akap, pe2&
1404 & , dm, pem, w2, dz2, pt(is1:ie1, j, 1:km), ws(&
1405 & is1:ie1, j), p_fac, scale_m)
1406  CALL pushcontrol(2,2)
1407  ELSE IF (a_imp .LE. 0.5) THEN
1408  CALL rim_2d_fwd(ms, dt, is1, ie1, km, rdgas, gama, gm2, pe2, dm&
1409 & , pm2, w2, dz2, pt(is1:ie1, j, 1:km), ws(is1:ie1, j), &
1410 & .true.)
1411  CALL pushcontrol(2,1)
1412  ELSE
1413  CALL sim1_solver_fwd(dt, is1, ie1, km, rdgas, gama, gm2, cp2, &
1414 & akap, pe2, dm, pm2, pem, w2, dz2, pt(is1:ie1, j, &
1415 & 1:km), ws(is1:ie1, j), p_fac)
1416  CALL pushcontrol(2,0)
1417  END IF
1418  DO k=2,km+1
1419  DO i=is1,ie1
1420 ! add hydrostatic full-component
1421  CALL pushrealarray(pef(i, j, k))
1422  pef(i, j, k) = pe2(i, k) + pem(i, k)
1423  END DO
1424  END DO
1425 ! Compute Height * grav (for p-gradient computation)
1426  DO i=is1,ie1
1427  CALL pushrealarray(gz(i, j, km+1))
1428  gz(i, j, km+1) = hs(i, j)
1429  END DO
1430  DO k=km,1,-1
1431  DO i=is1,ie1
1432  CALL pushrealarray(gz(i, j, k))
1433  gz(i, j, k) = gz(i, j, k+1) - dz2(i, k)*grav
1434  END DO
1435  END DO
1436  END DO
1437  CALL pushinteger(ie1)
1438  CALL pushrealarray(pem, (ie-is+3)*(km+1))
1439  CALL pushrealarray(gama)
1440  CALL pushrealarray(pm2, (ie-is+3)*km)
1441  CALL pushrealarray(rgrav)
1442  CALL pushrealarray(w2, (ie-is+3)*km)
1443  CALL pushrealarray(dz2, (ie-is+3)*km)
1444  CALL pushinteger(is1)
1445  CALL pushrealarray(pe2, (ie-is+3)*(km+1))
1446  CALL pushrealarray(dm, (ie-is+3)*km)
1447  END SUBROUTINE riem_solver_c_fwd
1448 ! Differentiation of riem_solver_c in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_ed
1449 !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
1450 !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
1451 !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
1452 !eigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l
1453 !_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
1454 !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_
1455 !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
1456 !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
1457 !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
1458 !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
1459 !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
1460 !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
1461 !_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
1462 !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
1463 !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
1464 !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
1465 !tils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
1466 ! gradient of useful results: ws gz delp w3 pef pt
1467 ! with respect to varying inputs: ws gz delp w3 pef pt
1468  SUBROUTINE riem_solver_c_bwd(ms, dt, is, ie, js, je, km, ng, akap, &
1469 & cappa, cp, ptop, hs, w3, w3_ad, pt, pt_ad, q_con, delp, delp_ad, gz&
1470 & , gz_ad, pef, pef_ad, ws, ws_ad, p_fac, a_imp, scale_m)
1471  IMPLICIT NONE
1472  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km
1473  INTEGER, INTENT(IN) :: ms
1474  REAL, INTENT(IN) :: dt, akap, cp, ptop, p_fac, a_imp, scale_m
1475  REAL, INTENT(IN) :: ws(is-ng:ie+ng, js-ng:je+ng)
1476  REAL :: ws_ad(is-ng:ie+ng, js-ng:je+ng)
1477  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: pt, &
1478 & delp
1479  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km) :: pt_ad, delp_ad
1480  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: q_con, &
1481 & cappa
1482  REAL, INTENT(IN) :: hs(is-ng:ie+ng, js-ng:je+ng)
1483  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: w3
1484  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km) :: w3_ad
1485  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1), INTENT(INOUT) :: gz
1486  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1), INTENT(INOUT) :: &
1487 & gz_ad
1488  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1) :: pef
1489  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1) :: pef_ad
1490  REAL, DIMENSION(is-1:ie+1, km) :: dm, dz2, w2, pm2, gm2, cp2
1491  REAL, DIMENSION(is-1:ie+1, km) :: dm_ad, dz2_ad, w2_ad, pm2_ad
1492  REAL, DIMENSION(is-1:ie+1, km+1) :: pem, pe2, peg
1493  REAL, DIMENSION(is-1:ie+1, km+1) :: pem_ad, pe2_ad
1494  REAL :: gama, rgrav
1495  INTEGER :: i, j, k
1496  INTEGER :: is1, ie1
1497  INTRINSIC log
1498  REAL :: temp
1499  REAL :: temp0
1500  REAL :: temp1
1501  REAL :: temp_ad
1502  INTEGER :: branch
1503 
1504  dm = 0.0
1505  dz2 = 0.0
1506  w2 = 0.0
1507  pm2 = 0.0
1508  gm2 = 0.0
1509  cp2 = 0.0
1510  pem = 0.0
1511  pe2 = 0.0
1512  peg = 0.0
1513  gama = 0.0
1514  rgrav = 0.0
1515  is1 = 0
1516  ie1 = 0
1517  branch = 0
1518 
1519  CALL poprealarray(dm, (ie-is+3)*km)
1520  CALL poprealarray(pe2, (ie-is+3)*(km+1))
1521  CALL popinteger(is1)
1522  CALL poprealarray(dz2, (ie-is+3)*km)
1523  CALL poprealarray(w2, (ie-is+3)*km)
1524  CALL poprealarray(rgrav)
1525  CALL poprealarray(pm2, (ie-is+3)*km)
1526  CALL poprealarray(gama)
1527  CALL poprealarray(pem, (ie-is+3)*(km+1))
1528  CALL popinteger(ie1)
1529  dm_ad = 0.0
1530  pe2_ad = 0.0
1531  dz2_ad = 0.0
1532  w2_ad = 0.0
1533  pm2_ad = 0.0
1534  pem_ad = 0.0
1535  DO j=je+1,js-1,-1
1536  DO k=1,km,1
1537  DO i=ie1,is1,-1
1538  CALL poprealarray(gz(i, j, k))
1539  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
1540  dz2_ad(i, k) = dz2_ad(i, k) - grav*gz_ad(i, j, k)
1541  gz_ad(i, j, k) = 0.0
1542  END DO
1543  END DO
1544  DO i=ie1,is1,-1
1545  CALL poprealarray(gz(i, j, km+1))
1546  gz_ad(i, j, km+1) = 0.0
1547  END DO
1548  DO k=km+1,2,-1
1549  DO i=ie1,is1,-1
1550  CALL poprealarray(pef(i, j, k))
1551  pe2_ad(i, k) = pe2_ad(i, k) + pef_ad(i, j, k)
1552  pem_ad(i, k) = pem_ad(i, k) + pef_ad(i, j, k)
1553  pef_ad(i, j, k) = 0.0
1554  END DO
1555  END DO
1556  CALL popcontrol(2,branch)
1557  IF (branch .EQ. 0) THEN
1558  CALL sim1_solver_bwd(dt, is1, ie1, km, rdgas, gama, gm2, cp2, &
1559 & akap, pe2, pe2_ad, dm, dm_ad, pm2, pm2_ad, pem, &
1560 & pem_ad, w2, w2_ad, dz2, dz2_ad, pt(is1:ie1, j, 1:&
1561 & km), pt_ad(is1:ie1, j, 1:km), ws(is1:ie1, j), &
1562 & ws_ad(is1:ie1, j), p_fac)
1563  ELSE IF (branch .EQ. 1) THEN
1564  CALL rim_2d_bwd(ms, dt, is1, ie1, km, rdgas, gama, gm2, pe2, &
1565 & pe2_ad, dm, dm_ad, pm2, pm2_ad, w2, w2_ad, dz2, dz2_ad&
1566 & , pt(is1:ie1, j, 1:km), pt_ad(is1:ie1, j, 1:km), ws(&
1567 & is1:ie1, j), ws_ad(is1:ie1, j), .true.)
1568  ELSE
1569  CALL sim3p0_solver_bwd(dt, is1, ie1, km, rdgas, gama, akap, pe2&
1570 & , pe2_ad, dm, dm_ad, pem, pem_ad, w2, w2_ad, &
1571 & dz2, dz2_ad, pt(is1:ie1, j, 1:km), pt_ad(is1:&
1572 & ie1, j, 1:km), ws(is1:ie1, j), ws_ad(is1:ie1, j&
1573 & ), p_fac, scale_m)
1574  END IF
1575  DO k=km,1,-1
1576  DO i=ie1,is1,-1
1577  temp1 = pem(i, k)
1578  temp = pem(i, k+1)/temp1
1579  temp0 = log(temp)
1580  CALL poprealarray(w2(i, k))
1581  w3_ad(i, j, k) = w3_ad(i, j, k) + w2_ad(i, k)
1582  w2_ad(i, k) = 0.0
1583  CALL poprealarray(dm(i, k))
1584  dm_ad(i, k) = pm2_ad(i, k)/temp0 + rgrav*dm_ad(i, k)
1585  CALL poprealarray(pm2(i, k))
1586  temp_ad = -(dm(i, k)*pm2_ad(i, k)/(temp*temp0**2*temp1))
1587  pem_ad(i, k+1) = pem_ad(i, k+1) + temp_ad
1588  pem_ad(i, k) = pem_ad(i, k) - temp*temp_ad
1589  pm2_ad(i, k) = 0.0
1590  CALL poprealarray(dz2(i, k))
1591  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + dz2_ad(i, k)
1592  gz_ad(i, j, k) = gz_ad(i, j, k) - dz2_ad(i, k)
1593  dz2_ad(i, k) = 0.0
1594  END DO
1595  END DO
1596  DO k=km+1,2,-1
1597  DO i=ie1,is1,-1
1598  CALL poprealarray(pem(i, k))
1599  pem_ad(i, k-1) = pem_ad(i, k-1) + pem_ad(i, k)
1600  dm_ad(i, k-1) = dm_ad(i, k-1) + pem_ad(i, k)
1601  pem_ad(i, k) = 0.0
1602  END DO
1603  END DO
1604  DO i=ie1,is1,-1
1605  CALL poprealarray(pem(i, 1))
1606  pem_ad(i, 1) = 0.0
1607  CALL poprealarray(pef(i, j, 1))
1608  pef_ad(i, j, 1) = 0.0
1609  END DO
1610  DO k=km,1,-1
1611  DO i=ie1,is1,-1
1612  CALL poprealarray(dm(i, k))
1613  delp_ad(i, j, k) = delp_ad(i, j, k) + dm_ad(i, k)
1614  dm_ad(i, k) = 0.0
1615  END DO
1616  END DO
1617  END DO
1618  END SUBROUTINE riem_solver_c_bwd
1619  SUBROUTINE riem_solver_c(ms, dt, is, ie, js, je, km, ng, akap, cappa, &
1620 & cp, ptop, hs, w3, pt, q_con, delp, gz, pef, ws, p_fac, a_imp, &
1621 & scale_m)
1622  IMPLICIT NONE
1623  INTEGER, INTENT(IN) :: is, ie, js, je, ng, km
1624  INTEGER, INTENT(IN) :: ms
1625  REAL, INTENT(IN) :: dt, akap, cp, ptop, p_fac, a_imp, scale_m
1626  REAL, INTENT(IN) :: ws(is-ng:ie+ng, js-ng:je+ng)
1627  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: pt, &
1628 & delp
1629  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: q_con, &
1630 & cappa
1631  REAL, INTENT(IN) :: hs(is-ng:ie+ng, js-ng:je+ng)
1632  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km), INTENT(IN) :: w3
1633 ! OUTPUT PARAMETERS
1634  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1), INTENT(INOUT) :: gz
1635  REAL, DIMENSION(is-ng:ie+ng, js-ng:je+ng, km+1), INTENT(OUT) :: pef
1636 ! Local:
1637  REAL, DIMENSION(is-1:ie+1, km) :: dm, dz2, w2, pm2, gm2, cp2
1638  REAL, DIMENSION(is-1:ie+1, km+1) :: pem, pe2, peg
1639  REAL :: gama, rgrav
1640  INTEGER :: i, j, k
1641  INTEGER :: is1, ie1
1642  INTRINSIC log
1643  gama = 1./(1.-akap)
1644  rgrav = 1./grav
1645  is1 = is - 1
1646  ie1 = ie + 1
1647 !$OMP parallel do default(none) shared(js,je,is1,ie1,km,delp,pef,ptop,gz,rgrav,w3,pt, &
1648 !$OMP a_imp,dt,gama,akap,ws,p_fac,scale_m,ms,hs,q_con,cappa) &
1649 !$OMP private(cp2,gm2, dm, dz2, w2, pm2, pe2, pem, peg)
1650  DO j=js-1,je+1
1651  DO k=1,km
1652  DO i=is1,ie1
1653  dm(i, k) = delp(i, j, k)
1654  END DO
1655  END DO
1656  DO i=is1,ie1
1657 ! full pressure at top
1658  pef(i, j, 1) = ptop
1659  pem(i, 1) = ptop
1660  END DO
1661  DO k=2,km+1
1662  DO i=is1,ie1
1663  pem(i, k) = pem(i, k-1) + dm(i, k-1)
1664  END DO
1665  END DO
1666  DO k=1,km
1667  DO i=is1,ie1
1668  dz2(i, k) = gz(i, j, k+1) - gz(i, j, k)
1669  pm2(i, k) = dm(i, k)/log(pem(i, k+1)/pem(i, k))
1670  dm(i, k) = dm(i, k)*rgrav
1671  w2(i, k) = w3(i, j, k)
1672  END DO
1673  END DO
1674  IF (a_imp .LT. -0.01) THEN
1675  CALL sim3p0_solver(dt, is1, ie1, km, rdgas, gama, akap, pe2, dm&
1676 & , pem, w2, dz2, pt(is1:ie1, j, 1:km), ws(is1:ie1, j&
1677 & ), p_fac, scale_m)
1678  ELSE IF (a_imp .LE. 0.5) THEN
1679  CALL rim_2d(ms, dt, is1, ie1, km, rdgas, gama, gm2, pe2, dm, pm2&
1680 & , w2, dz2, pt(is1:ie1, j, 1:km), ws(is1:ie1, j), .true.)
1681  ELSE
1682  CALL sim1_solver(dt, is1, ie1, km, rdgas, gama, gm2, cp2, akap, &
1683 & pe2, dm, pm2, pem, w2, dz2, pt(is1:ie1, j, 1:km), ws(&
1684 & is1:ie1, j), p_fac)
1685  END IF
1686  DO k=2,km+1
1687  DO i=is1,ie1
1688 ! add hydrostatic full-component
1689  pef(i, j, k) = pe2(i, k) + pem(i, k)
1690  END DO
1691  END DO
1692 ! Compute Height * grav (for p-gradient computation)
1693  DO i=is1,ie1
1694  gz(i, j, km+1) = hs(i, j)
1695  END DO
1696  DO k=km,1,-1
1697  DO i=is1,ie1
1698  gz(i, j, k) = gz(i, j, k+1) - dz2(i, k)*grav
1699  END DO
1700  END DO
1701  END DO
1702  END SUBROUTINE riem_solver_c
1703 !GFDL - This routine will not give absoulte reproducibility when compiled with -fast-transcendentals.
1704 !GFDL - It is now inside of nh_core.F90 and being compiled without -fast-transcendentals.
1705  SUBROUTINE riem_solver3test(ms, dt, is, ie, js, je, km, ng, isd, ied, &
1706 & jsd, jed, akap, cappa, cp, ptop, zs, q_con, w, delz, pt, delp, zh, &
1707 & pe, ppe, pk3, pk, peln, ws, scale_m, p_fac, a_imp, use_logp, &
1708 & last_call, fp_out)
1709  IMPLICIT NONE
1710 !--------------------------------------------
1711 ! !OUTPUT PARAMETERS
1712 ! Ouput: gz: grav*height at edges
1713 ! pe: full hydrostatic pressure
1714 ! ppe: non-hydrostatic pressure perturbation
1715 !--------------------------------------------
1716  INTEGER, INTENT(IN) :: ms, is, ie, js, je, km, ng
1717  INTEGER, INTENT(IN) :: isd, ied, jsd, jed
1718 ! the BIG horizontal Lagrangian time step
1719  REAL, INTENT(IN) :: dt
1720  REAL, INTENT(IN) :: akap, cp, ptop, p_fac, a_imp, scale_m
1721  REAL, INTENT(IN) :: zs(isd:ied, jsd:jed)
1722  LOGICAL, INTENT(IN) :: last_call, use_logp, fp_out
1723  REAL, INTENT(IN) :: ws(is:ie, js:je)
1724  REAL, DIMENSION(isd:ied, jsd:jed, km), INTENT(IN) :: q_con, cappa
1725  REAL, DIMENSION(isd:ied, jsd:jed, km), INTENT(IN) :: delp, pt
1726  REAL, DIMENSION(isd:ied, jsd:jed, km+1), INTENT(INOUT) :: zh
1727  REAL, DIMENSION(isd:ied, jsd:jed, km), INTENT(INOUT) :: w
1728  REAL, INTENT(INOUT) :: pe(is-1:ie+1, km+1, js-1:je+1)
1729 ! ln(pe)
1730  REAL, INTENT(OUT) :: peln(is:ie, km+1, js:je)
1731  REAL, DIMENSION(isd:ied, jsd:jed, km+1), INTENT(OUT) :: ppe
1732  REAL, INTENT(OUT) :: delz(is-ng:ie+ng, js-ng:je+ng, km)
1733  REAL, INTENT(OUT) :: pk(is:ie, js:je, km+1)
1734  REAL, INTENT(OUT) :: pk3(isd:ied, jsd:jed, km+1)
1735 ! Local:
1736  REAL, DIMENSION(is:ie, km) :: dm, dz2, pm2, w2, gm2, cp2
1737  REAL, DIMENSION(is:ie, km+1) :: pem, pe2, peln2, peg, pelng
1738  REAL :: gama, rgrav, ptk, peln1
1739  INTEGER :: i, j, k
1740  INTRINSIC log
1741  INTRINSIC exp
1742  INTRINSIC abs
1743  REAL :: abs0
1744  gama = 1./(1.-akap)
1745  rgrav = 1./grav
1746  peln1 = log(ptop)
1747  ptk = exp(akap*peln1)
1748 !$OMP parallel do default(none) shared(is,ie,js,je,km,delp,ptop,peln1,pk3,ptk,akap,rgrav,zh,pt, &
1749 !$OMP w,a_imp,dt,gama,ws,p_fac,scale_m,ms,delz,last_call, &
1750 !$OMP peln,pk,fp_out,ppe,use_logp,zs,pe,cappa,q_con ) &
1751 !$OMP private(cp2, gm2, dm, dz2, pm2, pem, peg, pelng, pe2, peln2, w2)
1752  DO j=js,je
1753  DO k=1,km
1754  DO i=is,ie
1755  dm(i, k) = delp(i, j, k)
1756  END DO
1757  END DO
1758  DO i=is,ie
1759  pem(i, 1) = ptop
1760  peln2(i, 1) = peln1
1761  pk3(i, j, 1) = ptk
1762  END DO
1763  DO k=2,km+1
1764  DO i=is,ie
1765  pem(i, k) = pem(i, k-1) + dm(i, k-1)
1766  peln2(i, k) = log(pem(i, k))
1767  pk3(i, j, k) = exp(akap*peln2(i, k))
1768  END DO
1769  END DO
1770  DO k=1,km
1771  DO i=is,ie
1772  pm2(i, k) = dm(i, k)/(peln2(i, k+1)-peln2(i, k))
1773  dm(i, k) = dm(i, k)*rgrav
1774  dz2(i, k) = zh(i, j, k+1) - zh(i, j, k)
1775  w2(i, k) = w(i, j, k)
1776  END DO
1777  END DO
1778  IF (a_imp .LT. -0.999) THEN
1779  CALL sim3p0_solver(dt, is, ie, km, rdgas, gama, akap, pe2, dm, &
1780 & pem, w2, dz2, pt(is:ie, j, 1:km), ws(is:ie, j), &
1781 & p_fac, scale_m)
1782  ELSE IF (a_imp .LT. -0.5) THEN
1783  IF (a_imp .GE. 0.) THEN
1784  abs0 = a_imp
1785  ELSE
1786  abs0 = -a_imp
1787  END IF
1788  CALL sim3_solver(dt, is, ie, km, rdgas, gama, akap, pe2, dm, pem&
1789 & , w2, dz2, pt(is:ie, j, 1:km), ws(is:ie, j), abs0, &
1790 & p_fac, scale_m)
1791  ELSE IF (a_imp .LE. 0.5) THEN
1792  CALL rim_2d(ms, dt, is, ie, km, rdgas, gama, gm2, pe2, dm, pm2, &
1793 & w2, dz2, pt(is:ie, j, 1:km), ws(is:ie, j), .false.)
1794  ELSE IF (a_imp .GT. 0.999) THEN
1795  CALL sim1_solver(dt, is, ie, km, rdgas, gama, gm2, cp2, akap, &
1796 & pe2, dm, pm2, pem, w2, dz2, pt(is:ie, j, 1:km), ws(is&
1797 & :ie, j), p_fac)
1798  ELSE
1799  CALL sim_solver(dt, is, ie, km, rdgas, gama, gm2, cp2, akap, pe2&
1800 & , dm, pm2, pem, w2, dz2, pt(is:ie, j, 1:km), ws(is:ie&
1801 & , j), a_imp, p_fac, scale_m)
1802  END IF
1803  DO k=1,km
1804  DO i=is,ie
1805  w(i, j, k) = w2(i, k)
1806  delz(i, j, k) = dz2(i, k)
1807  END DO
1808  END DO
1809  IF (last_call) THEN
1810  DO k=1,km+1
1811  DO i=is,ie
1812  peln(i, k, j) = peln2(i, k)
1813  pk(i, j, k) = pk3(i, j, k)
1814  pe(i, k, j) = pem(i, k)
1815  END DO
1816  END DO
1817  END IF
1818  IF (fp_out) THEN
1819  DO k=1,km+1
1820  DO i=is,ie
1821  ppe(i, j, k) = pe2(i, k) + pem(i, k)
1822  END DO
1823  END DO
1824  ELSE
1825  DO k=1,km+1
1826  DO i=is,ie
1827  ppe(i, j, k) = pe2(i, k)
1828  END DO
1829  END DO
1830  END IF
1831  IF (use_logp) THEN
1832  DO k=2,km+1
1833  DO i=is,ie
1834  pk3(i, j, k) = peln2(i, k)
1835  END DO
1836  END DO
1837  END IF
1838  DO i=is,ie
1839  zh(i, j, km+1) = zs(i, j)
1840  END DO
1841  DO k=km,1,-1
1842  DO i=is,ie
1843  zh(i, j, k) = zh(i, j, k+1) - dz2(i, k)
1844  END DO
1845  END DO
1846  END DO
1847  END SUBROUTINE riem_solver3test
1848  SUBROUTINE imp_diff_w(j, is, ie, js, je, ng, km, cd, delz, ws, w, w3)
1849  IMPLICIT NONE
1850  INTEGER, INTENT(IN) :: j, is, ie, js, je, km, ng
1851  REAL, INTENT(IN) :: cd
1852 ! delta-height (m)
1853  REAL, INTENT(IN) :: delz(is-ng:ie+ng, km)
1854 ! vertical vel. (m/s)
1855  REAL, INTENT(IN) :: w(is:ie, km)
1856  REAL, INTENT(IN) :: ws(is:ie)
1857  REAL, INTENT(OUT) :: w3(is-ng:ie+ng, js-ng:je+ng, km)
1858 ! Local:
1859  REAL, DIMENSION(is:ie, km) :: c, gam, dz, wt
1860  REAL :: bet(is:ie)
1861  REAL :: a
1862  INTEGER :: i, k
1863  DO k=2,km
1864  DO i=is,ie
1865  dz(i, k) = 0.5*(delz(i, k-1)+delz(i, k))
1866  END DO
1867  END DO
1868  DO k=1,km-1
1869  DO i=is,ie
1870  c(i, k) = -(cd/(dz(i, k+1)*delz(i, k)))
1871  END DO
1872  END DO
1873 ! model top:
1874  DO i=is,ie
1875 ! bet(i) = b
1876  bet(i) = 1. - c(i, 1)
1877  wt(i, 1) = w(i, 1)/bet(i)
1878  END DO
1879 ! Interior:
1880  DO k=2,km-1
1881  DO i=is,ie
1882  gam(i, k) = c(i, k-1)/bet(i)
1883  a = cd/(dz(i, k)*delz(i, k))
1884  bet(i) = 1. + a - c(i, k) + a*gam(i, k)
1885  wt(i, k) = (w(i, k)+a*wt(i, k-1))/bet(i)
1886  END DO
1887  END DO
1888 ! Bottom:
1889  DO i=is,ie
1890  gam(i, km) = c(i, km-1)/bet(i)
1891  a = cd/(dz(i, km)*delz(i, km))
1892  wt(i, km) = (w(i, km)+2.*ws(i)*cd/delz(i, km)**2+a*wt(i, km-1))/(&
1893 & 1.+a+(cd+cd)/delz(i, km)**2+a*gam(i, km))
1894  END DO
1895  DO k=km-1,1,-1
1896  DO i=is,ie
1897  wt(i, k) = wt(i, k) - gam(i, k+1)*wt(i, k+1)
1898  END DO
1899  END DO
1900  DO k=1,km
1901  DO i=is,ie
1902  w3(i, j, k) = wt(i, k)
1903  END DO
1904  END DO
1905  END SUBROUTINE imp_diff_w
1906 ! Differentiation of rim_2d in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_mod.a
1907 !2b_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_mod.
1908 !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.mix_d
1909 !p dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayleigh_Sup
1910 !er fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_ord4 fv
1911 !_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.remap_
1912 !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 fv_ma
1913 !pz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiters fv_m
1914 !apz_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_resta
1915 !rt_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subgrid_z
1916 !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_mod.
1917 !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_mod.SI
1918 !M3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_mod.nes
1919 !t_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.d2a2c_
1920 !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 s
1921 !w_core_mod.compute_divergence_damping sw_core_mod.smag_corner tp_core_mod.mp_ghost_ew tp_core_mod.fv_tp_2d tp_core_mod.
1922 !copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_utils_mod
1923 !.great_circle_dist sw_core_mod.edge_interpolate4)):
1924 ! gradient of useful results: ws pe2 dm2 dz2 w2 pm2 pt2
1925 ! with respect to varying inputs: ws pe2 dm2 dz2 w2 pm2 pt2
1926  SUBROUTINE rim_2d_fwd(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, dm2, &
1927 & pm2, w2, dz2, pt2, ws, c_core)
1928  IMPLICIT NONE
1929  INTEGER, INTENT(IN) :: ms, is, ie, km
1930  REAL, INTENT(IN) :: bdt, gama, rgas
1931  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pm2, gm2
1932  LOGICAL, INTENT(IN) :: c_core
1933  REAL, INTENT(IN) :: pt2(is:ie, km)
1934  REAL, INTENT(IN) :: ws(is:ie)
1935 ! IN/OUT:
1936  REAL, INTENT(INOUT) :: dz2(is:ie, km)
1937  REAL, INTENT(INOUT) :: w2(is:ie, km)
1938  REAL :: pe2(is:ie, km+1)
1939 ! Local:
1940  REAL :: ws2(is:ie)
1941  REAL, DIMENSION(km+1) :: m_bot, m_top, r_bot, r_top, pe1, pbar, wbar
1942  REAL, DIMENSION(km) :: r_hi, r_lo, dz, wm, dm, dts
1943  REAL, DIMENSION(km) :: pf1, wc, cm, pp, pt1
1944  REAL :: dt, rdt, grg, z_frac, ptmp1, rden, pf, time_left
1945  REAL :: m_surf
1946  INTEGER :: i, k, n, ke, kt1, ktop
1947  INTEGER :: ks0, ks1
1948  INTRINSIC real
1949  INTRINSIC log
1950  INTRINSIC exp
1951  INTRINSIC sqrt
1952  INTRINSIC max
1953  INTEGER :: ad_count
1954  INTEGER :: ad_from
1955  INTEGER :: ad_count0
1956  INTEGER :: ad_from0
1957  INTEGER :: ad_from1
1958  INTEGER :: ad_count1
1959  INTEGER :: ad_from2
1960  INTEGER :: ad_count2
1961  INTEGER :: ad_count3
1962  INTEGER :: ad_from3
1963  INTEGER :: ad_from4
1964  INTEGER :: ad_from5
1965  INTEGER :: ad_from6
1966  INTEGER :: ad_from7
1967  INTEGER :: ad_from8
1968 
1969  ws2 = 0.0
1970  m_bot = 0.0
1971  m_top = 0.0
1972  r_bot = 0.0
1973  r_top = 0.0
1974  pe1 = 0.0
1975  pbar = 0.0
1976  wbar = 0.0
1977  r_hi = 0.0
1978  r_lo = 0.0
1979  dz = 0.0
1980  wm = 0.0
1981  dm = 0.0
1982  dts = 0.0
1983  pf1 = 0.0
1984  wc = 0.0
1985  cm = 0.0
1986  pp = 0.0
1987  pt1 = 0.0
1988  dt = 0.0
1989  rdt = 0.0
1990  grg = 0.0
1991  z_frac = 0.0
1992  ptmp1 = 0.0
1993  rden = 0.0
1994  pf = 0.0
1995  time_left = 0.0
1996  m_surf = 0.0
1997  n = 0.0
1998  ke = 0.0
1999  kt1 = 0.0
2000  ktop = 0.0
2001  ks0 = 0.0
2002  ks1 = 0.0
2003  ad_count = 0.0
2004  ad_from = 0.0
2005  ad_count0 = 0.0
2006  ad_from0 = 0.0
2007  ad_from1 = 0.0
2008  ad_count1 = 0.0
2009  ad_from2 = 0.0
2010  ad_count2 = 0.0
2011  ad_count3 = 0.0
2012  ad_from3 = 0.0
2013  ad_from4 = 0.0
2014  ad_from5 = 0.0
2015  ad_from6 = 0.0
2016  ad_from7 = 0.0
2017  ad_from8 = 0.0
2018 
2019  grg = gama*rgas
2020  rdt = 1./bdt
2021  dt = bdt/REAL(ms)
2022  pbar(:) = 0.
2023  wbar(:) = 0.
2024  DO i=is,ie
2025  ws2(i) = 2.*ws(i)
2026  END DO
2027 ! end i-loop
2028  DO 170 i=is,ie
2029  DO k=1,km
2030  CALL pushrealarray(dz(k))
2031  dz(k) = dz2(i, k)
2032  CALL pushrealarray(dm(k))
2033  dm(k) = dm2(i, k)
2034  CALL pushrealarray(wm(k))
2035  wm(k) = w2(i, k)*dm(k)
2036  CALL pushrealarray(pt1(k))
2037  pt1(k) = pt2(i, k)
2038  END DO
2039  pe1(:) = 0.
2040  CALL pushrealarray(wbar(km+1))
2041  wbar(km+1) = ws(i)
2042  CALL pushinteger(ks0)
2043  ks0 = 1
2044  IF (ms .GT. 1 .AND. ms .LT. 8) THEN
2045  CALL pushinteger(k)
2046  ad_count = 1
2047 ! Continuity of (pbar, wbar) is maintained
2048  DO k=1,km
2049  rden = -(rgas*dm(k)/dz(k))
2050  CALL pushrealarray(pf1(k))
2051  pf1(k) = exp(gama*log(rden*pt1(k)))
2052  CALL pushrealarray(dts(k))
2053  dts(k) = -(dz(k)/sqrt(grg*pf1(k)/rden))
2054  IF (bdt .GT. dts(k)) THEN
2055  GOTO 100
2056  ELSE
2057  CALL pushinteger(k)
2058  ad_count = ad_count + 1
2059  END IF
2060  END DO
2061  CALL pushcontrol(1,0)
2062  CALL pushinteger(ad_count)
2063  ks0 = km
2064  GOTO 222
2065  100 CALL pushcontrol(1,1)
2066  CALL pushinteger(ad_count)
2067  ks0 = k - 1
2068  222 IF (ks0 .EQ. 1) THEN
2069  CALL pushcontrol(2,0)
2070  ELSE
2071  CALL pushinteger(k)
2072  DO k=1,ks0
2073  CALL pushrealarray(cm(k))
2074  cm(k) = dm(k)/dts(k)
2075  CALL pushrealarray(wc(k))
2076  wc(k) = wm(k)/dts(k)
2077  CALL pushrealarray(pp(k))
2078  pp(k) = pf1(k) - pm2(i, k)
2079  END DO
2080  CALL pushinteger(k - 1)
2081  CALL pushrealarray(wbar(1))
2082  wbar(1) = (wc(1)+pp(1))/cm(1)
2083  DO k=2,ks0
2084  CALL pushrealarray(wbar(k))
2085  wbar(k) = (wc(k-1)+wc(k)+pp(k)-pp(k-1))/(cm(k-1)+cm(k))
2086  CALL pushrealarray(pbar(k))
2087  pbar(k) = bdt*(cm(k-1)*wbar(k)-wc(k-1)+pp(k-1))
2088  pe1(k) = pbar(k)
2089  END DO
2090  CALL pushinteger(k - 1)
2091  IF (ks0 .EQ. km) THEN
2092  CALL pushrealarray(pbar(km+1))
2093  pbar(km+1) = bdt*(cm(km)*wbar(km+1)-wc(km)+pp(km))
2094  IF (c_core) THEN
2095  DO k=1,km
2096  CALL pushrealarray(dz2(i, k))
2097  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
2098  END DO
2099  CALL pushcontrol(1,0)
2100  ELSE
2101  DO k=1,km
2102  CALL pushrealarray(dz2(i, k))
2103  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
2104  CALL pushrealarray(w2(i, k))
2105  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
2106  END DO
2107  CALL pushcontrol(1,1)
2108  END IF
2109  CALL pushrealarray(pe2(i, 1))
2110  pe2(i, 1) = 0.
2111  DO k=2,km+1
2112  CALL pushrealarray(pe2(i, k))
2113  pe2(i, k) = pbar(k)*rdt
2114  END DO
2115  CALL pushcontrol(1,0)
2116  GOTO 170
2117  ELSE
2118 ! next i
2119  IF (c_core) THEN
2120  DO k=1,ks0-1
2121  CALL pushrealarray(dz2(i, k))
2122  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
2123  END DO
2124  CALL pushinteger(k - 1)
2125  CALL pushcontrol(1,0)
2126  ELSE
2127  DO k=1,ks0-1
2128  CALL pushrealarray(dz2(i, k))
2129  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
2130  CALL pushrealarray(w2(i, k))
2131  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
2132  END DO
2133  CALL pushinteger(k - 1)
2134  CALL pushcontrol(1,1)
2135  END IF
2136  CALL pushrealarray(pbar(ks0))
2137  pbar(ks0) = pbar(ks0)/REAL(ms)
2138  CALL pushcontrol(2,1)
2139  END IF
2140  END IF
2141  ELSE
2142  CALL pushcontrol(2,2)
2143  END IF
2144  ks1 = ks0
2145  DO n=1,ms
2146  ad_from = ks1
2147  CALL pushinteger(k)
2148  DO k=ad_from,km
2149  rden = -(rgas*dm(k)/dz(k))
2150  CALL pushrealarray(pf)
2151  pf = exp(gama*log(rden*pt1(k)))
2152  CALL pushrealarray(dts(k))
2153  dts(k) = -(dz(k)/sqrt(grg*pf/rden))
2154  ptmp1 = dts(k)*(pf-pm2(i, k))
2155  CALL pushrealarray(r_lo(k))
2156  r_lo(k) = wm(k) + ptmp1
2157  CALL pushrealarray(r_hi(k))
2158  r_hi(k) = wm(k) - ptmp1
2159  END DO
2160  CALL pushinteger(ad_from)
2161  ad_count0 = 1
2162  DO k=ks1,km
2163  IF (dt .GT. dts(k)) THEN
2164  GOTO 110
2165  ELSE
2166  ad_count0 = ad_count0 + 1
2167  END IF
2168  END DO
2169  CALL pushcontrol(1,0)
2170  CALL pushinteger(ad_count0)
2171  ktop = km
2172  GOTO 333
2173  110 CALL pushcontrol(1,1)
2174  CALL pushinteger(ad_count0)
2175  ktop = k - 1
2176  333 IF (ktop .GE. ks1) THEN
2177  ad_from0 = ks1
2178  DO k=ad_from0,ktop
2179  z_frac = dt/dts(k)
2180  CALL pushrealarray(r_bot(k))
2181  r_bot(k) = z_frac*r_lo(k)
2182  CALL pushrealarray(r_top(k+1))
2183  r_top(k+1) = z_frac*r_hi(k)
2184  CALL pushrealarray(m_bot(k))
2185  m_bot(k) = z_frac*dm(k)
2186  CALL pushrealarray(m_top(k+1))
2187  m_top(k+1) = m_bot(k)
2188  END DO
2189  CALL pushinteger(k - 1)
2190  CALL pushinteger(ad_from0)
2191  IF (ktop .EQ. km) THEN
2192  CALL pushcontrol(1,0)
2193  GOTO 666
2194  ELSE
2195  CALL pushcontrol(1,1)
2196  END IF
2197  ELSE
2198  CALL pushcontrol(1,0)
2199  END IF
2200  ad_from1 = ktop + 2
2201  DO k=ad_from1,km+1
2202  CALL pushrealarray(m_top(k))
2203  m_top(k) = 0.
2204  CALL pushrealarray(r_top(k))
2205  r_top(k) = 0.
2206  END DO
2207  CALL pushinteger(ad_from1)
2208  IF (1 .LT. ktop) THEN
2209  kt1 = ktop
2210  ELSE
2211  kt1 = 1
2212  END IF
2213  DO 130 ke=km+1,ktop+2,-1
2214  CALL pushrealarray(time_left)
2215  time_left = dt
2216  CALL pushinteger(k)
2217  ad_count1 = 1
2218  DO k=ke-1,kt1,-1
2219  IF (time_left .GT. dts(k)) THEN
2220  time_left = time_left - dts(k)
2221  CALL pushrealarray(m_top(ke))
2222  m_top(ke) = m_top(ke) + dm(k)
2223  CALL pushrealarray(r_top(ke))
2224  r_top(ke) = r_top(ke) + r_hi(k)
2225  CALL pushinteger(k)
2226  ad_count1 = ad_count1 + 1
2227  ELSE
2228  GOTO 120
2229  END IF
2230  END DO
2231  CALL pushcontrol(1,0)
2232  CALL pushinteger(ad_count1)
2233  CALL pushcontrol(1,1)
2234  GOTO 130
2235  120 CALL pushcontrol(1,1)
2236  CALL pushinteger(ad_count1)
2237  z_frac = time_left/dts(k)
2238  CALL pushrealarray(m_top(ke))
2239  m_top(ke) = m_top(ke) + z_frac*dm(k)
2240  CALL pushrealarray(r_top(ke))
2241  r_top(ke) = r_top(ke) + z_frac*r_hi(k)
2242  CALL pushcontrol(1,0)
2243  130 CONTINUE
2244  CALL pushinteger(ke + 1)
2245  ad_from2 = ktop + 1
2246  CALL pushinteger(k)
2247 ! next level
2248  DO k=ad_from2,km
2249  CALL pushrealarray(m_bot(k))
2250  m_bot(k) = 0.
2251  CALL pushrealarray(r_bot(k))
2252  r_bot(k) = 0.
2253  END DO
2254  CALL pushinteger(ad_from2)
2255  ad_from3 = ktop + 1
2256  DO 160 ke=ad_from3,km
2257  CALL pushrealarray(time_left)
2258  time_left = dt
2259  CALL pushinteger(k)
2260  ad_count2 = 1
2261  DO k=ke,km
2262  IF (time_left .GT. dts(k)) THEN
2263  time_left = time_left - dts(k)
2264  CALL pushrealarray(m_bot(ke))
2265  m_bot(ke) = m_bot(ke) + dm(k)
2266  CALL pushrealarray(r_bot(ke))
2267  r_bot(ke) = r_bot(ke) + r_lo(k)
2268  CALL pushinteger(k)
2269  ad_count2 = ad_count2 + 1
2270  ELSE
2271  GOTO 150
2272  END IF
2273  END DO
2274  CALL pushcontrol(1,0)
2275  CALL pushinteger(ad_count2)
2276 ! next interface
2277  CALL pushrealarray(m_surf)
2278  m_surf = m_bot(ke)
2279  CALL pushinteger(k)
2280  ad_count3 = 1
2281  DO k=km,kt1,-1
2282  IF (time_left .GT. dts(k)) THEN
2283  time_left = time_left - dts(k)
2284  CALL pushrealarray(m_bot(ke))
2285  m_bot(ke) = m_bot(ke) + dm(k)
2286  CALL pushrealarray(r_bot(ke))
2287  r_bot(ke) = r_bot(ke) - r_hi(k)
2288  CALL pushinteger(k)
2289  ad_count3 = ad_count3 + 1
2290  ELSE
2291  GOTO 140
2292  END IF
2293  END DO
2294  CALL pushcontrol(1,0)
2295  CALL pushinteger(ad_count3)
2296  CALL pushcontrol(2,2)
2297  GOTO 160
2298  140 CALL pushcontrol(1,1)
2299  CALL pushinteger(ad_count3)
2300  z_frac = time_left/dts(k)
2301  CALL pushrealarray(m_bot(ke))
2302  m_bot(ke) = m_bot(ke) + z_frac*dm(k)
2303  CALL pushrealarray(r_bot(ke))
2304  r_bot(ke) = r_bot(ke) - z_frac*r_hi(k) + (m_bot(ke)-m_surf)*&
2305 & ws2(i)
2306  CALL pushcontrol(2,1)
2307  GOTO 160
2308  150 CALL pushcontrol(1,1)
2309  CALL pushinteger(ad_count2)
2310  z_frac = time_left/dts(k)
2311  CALL pushrealarray(m_bot(ke))
2312  m_bot(ke) = m_bot(ke) + z_frac*dm(k)
2313  CALL pushrealarray(r_bot(ke))
2314  r_bot(ke) = r_bot(ke) + z_frac*r_lo(k)
2315  CALL pushcontrol(2,0)
2316  160 CONTINUE
2317  CALL pushinteger(ad_from3)
2318  CALL pushcontrol(1,1)
2319 ! next interface
2320  666 IF (ks1 .EQ. 1) THEN
2321  CALL pushrealarray(wbar(1))
2322  wbar(1) = r_bot(1)/m_bot(1)
2323  CALL pushcontrol(1,1)
2324  ELSE
2325  CALL pushcontrol(1,0)
2326  END IF
2327  ad_from4 = ks1 + 1
2328  CALL pushinteger(k)
2329  DO k=ad_from4,km
2330  CALL pushrealarray(wbar(k))
2331  wbar(k) = (r_bot(k)+r_top(k))/(m_top(k)+m_bot(k))
2332  END DO
2333  CALL pushinteger(ad_from4)
2334  ad_from5 = ks1 + 1
2335 ! pbar here is actually dt*pbar
2336  DO k=ad_from5,km+1
2337  CALL pushrealarray(pbar(k))
2338  pbar(k) = m_top(k)*wbar(k) - r_top(k)
2339  pe1(k) = pe1(k) + pbar(k)
2340  END DO
2341  CALL pushinteger(ad_from5)
2342  IF (n .EQ. ms) THEN
2343  IF (c_core) THEN
2344  ad_from6 = ks1
2345  DO k=ad_from6,km
2346  CALL pushrealarray(dz2(i, k))
2347  dz2(i, k) = dz(k) + dt*(wbar(k+1)-wbar(k))
2348  END DO
2349  CALL pushinteger(ad_from6)
2350  CALL pushcontrol(2,0)
2351  ELSE
2352  ad_from7 = ks1
2353  DO k=ad_from7,km
2354  CALL pushrealarray(dz2(i, k))
2355  dz2(i, k) = dz(k) + dt*(wbar(k+1)-wbar(k))
2356  CALL pushrealarray(w2(i, k))
2357  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
2358  END DO
2359  CALL pushinteger(ad_from7)
2360  CALL pushcontrol(2,1)
2361  END IF
2362  ELSE
2363  ad_from8 = ks1
2364  DO k=ad_from8,km
2365  CALL pushrealarray(dz(k))
2366  dz(k) = dz(k) + dt*(wbar(k+1)-wbar(k))
2367  CALL pushrealarray(wm(k))
2368  wm(k) = wm(k) + pbar(k+1) - pbar(k)
2369  END DO
2370  CALL pushinteger(ad_from8)
2371  CALL pushcontrol(2,2)
2372  END IF
2373  END DO
2374  CALL pushrealarray(pe2(i, 1))
2375  pe2(i, 1) = 0.
2376  CALL pushinteger(k)
2377  DO k=2,km+1
2378  CALL pushrealarray(pe2(i, k))
2379  pe2(i, k) = pe1(k)*rdt
2380  END DO
2381  CALL pushcontrol(1,1)
2382  170 CONTINUE
2383  CALL pushrealarray(r_hi, km)
2384  CALL pushrealarray(wm, km)
2385  CALL pushrealarray(m_surf)
2386  CALL pushrealarray(pbar, km + 1)
2387  CALL pushrealarray(wc, km)
2388  CALL pushrealarray(pp, km)
2389  CALL pushrealarray(cm, km)
2390  CALL pushrealarray(pt1, km)
2391  CALL pushrealarray(time_left)
2392  CALL pushrealarray(ws2, ie - is + 1)
2393  CALL pushrealarray(r_bot, km + 1)
2394  CALL pushrealarray(pf)
2395  CALL pushrealarray(rdt)
2396  CALL pushrealarray(m_bot, km + 1)
2397  CALL pushrealarray(r_top, km + 1)
2398  CALL pushrealarray(m_top, km + 1)
2399  CALL pushrealarray(pf1, km)
2400  CALL pushinteger(ks0)
2401  CALL pushrealarray(wbar, km + 1)
2402  CALL pushrealarray(r_lo, km)
2403  CALL pushrealarray(dz, km)
2404  CALL pushrealarray(grg)
2405  CALL pushrealarray(dt)
2406  CALL pushrealarray(dts, km)
2407  CALL pushrealarray(dm, km)
2408  END SUBROUTINE rim_2d_fwd
2409 ! Differentiation of rim_2d in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_mod.
2410 !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_mod
2411 !.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.mix_
2412 !dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayleigh_Su
2413 !per fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_ord4 f
2414 !v_grid_utils_mod.c2l_ord2 fv_mapz_mod.Lagrangian_to_Eulerian fv_mapz_mod.compute_total_energy fv_mapz_mod.pkez fv_mapz_mod.remap
2415 !_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 fv_m
2416 !apz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiters fv_
2417 !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_rest
2418 !art_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subgrid_z
2419 ! 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_mod
2420 !.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_mod.S
2421 !IM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_mod.ne
2422 !st_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.d2a2c
2423 !_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
2424 !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_mod
2425 !.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_utils_mo
2426 !d.great_circle_dist sw_core_mod.edge_interpolate4)):
2427 ! gradient of useful results: ws pe2 dm2 dz2 w2 pm2 pt2
2428 ! with respect to varying inputs: ws pe2 dm2 dz2 w2 pm2 pt2
2429  SUBROUTINE rim_2d_bwd(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, &
2430 & pe2_ad, dm2, dm2_ad, pm2, pm2_ad, w2, w2_ad, dz2, dz2_ad, pt2, &
2431 & pt2_ad, ws, ws_ad, c_core)
2432  IMPLICIT NONE
2433  INTEGER, INTENT(IN) :: ms, is, ie, km
2434  REAL, INTENT(IN) :: bdt, gama, rgas
2435  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pm2, gm2
2436  REAL, DIMENSION(is:ie, km) :: dm2_ad, pm2_ad
2437  LOGICAL, INTENT(IN) :: c_core
2438  REAL, INTENT(IN) :: pt2(is:ie, km)
2439  REAL :: pt2_ad(is:ie, km)
2440  REAL, INTENT(IN) :: ws(is:ie)
2441  REAL :: ws_ad(is:ie)
2442  REAL, INTENT(INOUT) :: dz2(is:ie, km)
2443  REAL, INTENT(INOUT) :: dz2_ad(is:ie, km)
2444  REAL, INTENT(INOUT) :: w2(is:ie, km)
2445  REAL, INTENT(INOUT) :: w2_ad(is:ie, km)
2446  REAL :: pe2(is:ie, km+1)
2447  REAL :: pe2_ad(is:ie, km+1)
2448  REAL :: ws2(is:ie)
2449  REAL :: ws2_ad(is:ie)
2450  REAL, DIMENSION(km+1) :: m_bot, m_top, r_bot, r_top, pe1, pbar, wbar
2451  REAL, DIMENSION(km+1) :: m_bot_ad, m_top_ad, r_bot_ad, r_top_ad, &
2452 & pe1_ad, pbar_ad, wbar_ad
2453  REAL, DIMENSION(km) :: r_hi, r_lo, dz, wm, dm, dts
2454  REAL, DIMENSION(km) :: r_hi_ad, r_lo_ad, dz_ad, wm_ad, dm_ad, dts_ad
2455  REAL, DIMENSION(km) :: pf1, wc, cm, pp, pt1
2456  REAL, DIMENSION(km) :: pf1_ad, wc_ad, cm_ad, pp_ad, pt1_ad
2457  REAL :: dt, rdt, grg, z_frac, ptmp1, rden, pf, time_left
2458  REAL :: z_frac_ad, ptmp1_ad, rden_ad, pf_ad, time_left_ad
2459  REAL :: m_surf
2460  REAL :: m_surf_ad
2461  INTEGER :: i, k, n, ke, kt1, ktop
2462  INTEGER :: ks0, ks1
2463  INTRINSIC real
2464  INTRINSIC log
2465  INTRINSIC exp
2466  INTRINSIC sqrt
2467  INTRINSIC max
2468  REAL :: temp
2469  REAL :: temp0
2470  REAL :: temp_ad
2471  REAL :: temp_ad0
2472  REAL :: temp_ad1
2473  REAL :: temp_ad2
2474  REAL :: temp_ad3
2475  REAL :: temp_ad4
2476  REAL :: temp_ad5
2477  REAL :: temp_ad6
2478  REAL :: temp_ad7
2479  REAL :: temp_ad8
2480  REAL :: temp_ad9
2481  REAL :: temp_ad10
2482  REAL :: temp1
2483  REAL :: temp2
2484  REAL :: temp_ad11
2485  REAL :: temp_ad12
2486  REAL :: temp_ad13
2487  REAL :: temp_ad14
2488  REAL :: temp_ad15
2489  REAL :: temp_ad16
2490  REAL :: temp_ad17
2491  REAL :: temp_ad18
2492  REAL :: temp_ad19
2493  REAL :: temp_ad20
2494  INTEGER :: ad_count
2495  INTEGER :: i0
2496  INTEGER :: branch
2497  INTEGER :: ad_to
2498  INTEGER :: ad_to0
2499  INTEGER :: ad_to1
2500  INTEGER :: ad_to2
2501  INTEGER :: ad_from
2502  INTEGER :: ad_count0
2503  INTEGER :: i1
2504  INTEGER :: ad_from0
2505  INTEGER :: ad_to3
2506  INTEGER :: ad_from1
2507  INTEGER :: ad_count1
2508  INTEGER :: i2
2509  INTEGER :: ad_to4
2510  INTEGER :: ad_from2
2511  INTEGER :: ad_count2
2512  INTEGER :: i3
2513  INTEGER :: ad_count3
2514  INTEGER :: i4
2515  INTEGER :: ad_from3
2516  INTEGER :: ad_from4
2517  INTEGER :: ad_from5
2518  INTEGER :: ad_from6
2519  INTEGER :: ad_from7
2520  INTEGER :: ad_from8
2521 
2522  ws2 = 0.0
2523  m_bot = 0.0
2524  m_top = 0.0
2525  r_bot = 0.0
2526  r_top = 0.0
2527  pe1 = 0.0
2528  pbar = 0.0
2529  wbar = 0.0
2530  r_hi = 0.0
2531  r_lo = 0.0
2532  dz = 0.0
2533  wm = 0.0
2534  dm = 0.0
2535  dts = 0.0
2536  pf1 = 0.0
2537  wc = 0.0
2538  cm = 0.0
2539  pp = 0.0
2540  pt1 = 0.0
2541  dt = 0.0
2542  rdt = 0.0
2543  grg = 0.0
2544  z_frac = 0.0
2545  ptmp1 = 0.0
2546  rden = 0.0
2547  pf = 0.0
2548  time_left = 0.0
2549  m_surf = 0.0
2550  n = 0.0
2551  ke = 0.0
2552  kt1 = 0.0
2553  ktop = 0.0
2554  ks0 = 0.0
2555  ks1 = 0.0
2556  ad_count = 0.0
2557  ad_from = 0.0
2558  ad_count0 = 0.0
2559  ad_from0 = 0.0
2560  ad_from1 = 0.0
2561  ad_count1 = 0.0
2562  ad_from2 = 0.0
2563  ad_count2 = 0.0
2564  ad_count3 = 0.0
2565  ad_from3 = 0.0
2566  ad_from4 = 0.0
2567  ad_from5 = 0.0
2568  ad_from6 = 0.0
2569  ad_from7 = 0.0
2570  ad_from8 = 0.0
2571  ad_to = 0
2572  ad_to0 = 0
2573  ad_to1 = 0
2574  ad_to2 = 0
2575  ad_to3 = 0
2576  ad_to4 = 0
2577  branch = 0
2578 
2579  CALL poprealarray(dm, km)
2580  CALL poprealarray(dts, km)
2581  CALL poprealarray(dt)
2582  CALL poprealarray(grg)
2583  CALL poprealarray(dz, km)
2584  CALL poprealarray(r_lo, km)
2585  CALL poprealarray(wbar, km + 1)
2586  CALL popinteger(ks0)
2587  CALL poprealarray(pf1, km)
2588  CALL poprealarray(m_top, km + 1)
2589  CALL poprealarray(r_top, km + 1)
2590  CALL poprealarray(m_bot, km + 1)
2591  CALL poprealarray(rdt)
2592  CALL poprealarray(pf)
2593  CALL poprealarray(r_bot, km + 1)
2594  CALL poprealarray(ws2, ie - is + 1)
2595  CALL poprealarray(time_left)
2596  CALL poprealarray(pt1, km)
2597  CALL poprealarray(cm, km)
2598  CALL poprealarray(pp, km)
2599  CALL poprealarray(wc, km)
2600  CALL poprealarray(pbar, km + 1)
2601  CALL poprealarray(m_surf)
2602  CALL poprealarray(wm, km)
2603  CALL poprealarray(r_hi, km)
2604  dm_ad = 0.0
2605  dts_ad = 0.0
2606  dz_ad = 0.0
2607  r_lo_ad = 0.0
2608  wbar_ad = 0.0
2609  pf1_ad = 0.0
2610  m_top_ad = 0.0
2611  r_top_ad = 0.0
2612  m_bot_ad = 0.0
2613  r_bot_ad = 0.0
2614  ws2_ad = 0.0
2615  pt1_ad = 0.0
2616  cm_ad = 0.0
2617  pp_ad = 0.0
2618  wc_ad = 0.0
2619  pbar_ad = 0.0
2620  wm_ad = 0.0
2621  r_hi_ad = 0.0
2622  DO i=ie,is,-1
2623  CALL popcontrol(1,branch)
2624  IF (branch .EQ. 0) THEN
2625  DO k=km+1,2,-1
2626  CALL poprealarray(pe2(i, k))
2627  pbar_ad(k) = pbar_ad(k) + rdt*pe2_ad(i, k)
2628  pe2_ad(i, k) = 0.0
2629  END DO
2630  CALL poprealarray(pe2(i, 1))
2631  pe2_ad(i, 1) = 0.0
2632  CALL popcontrol(1,branch)
2633  IF (branch .EQ. 0) THEN
2634  DO k=km,1,-1
2635  CALL poprealarray(dz2(i, k))
2636  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2637  wbar_ad(k+1) = wbar_ad(k+1) + bdt*dz2_ad(i, k)
2638  wbar_ad(k) = wbar_ad(k) - bdt*dz2_ad(i, k)
2639  dz2_ad(i, k) = 0.0
2640  END DO
2641  ELSE
2642  DO k=km,1,-1
2643  CALL poprealarray(w2(i, k))
2644  temp_ad9 = w2_ad(i, k)/dm(k)
2645  wm_ad(k) = wm_ad(k) + temp_ad9
2646  pbar_ad(k+1) = pbar_ad(k+1) + temp_ad9
2647  pbar_ad(k) = pbar_ad(k) - temp_ad9
2648  dm_ad(k) = dm_ad(k) - (wm(k)+pbar(k+1)-pbar(k))*temp_ad9/dm(&
2649 & k)
2650  w2_ad(i, k) = 0.0
2651  CALL poprealarray(dz2(i, k))
2652  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2653  wbar_ad(k+1) = wbar_ad(k+1) + bdt*dz2_ad(i, k)
2654  wbar_ad(k) = wbar_ad(k) - bdt*dz2_ad(i, k)
2655  dz2_ad(i, k) = 0.0
2656  END DO
2657  END IF
2658  CALL poprealarray(pbar(km+1))
2659  temp_ad8 = bdt*pbar_ad(km+1)
2660  cm_ad(km) = cm_ad(km) + wbar(km+1)*temp_ad8
2661  wbar_ad(km+1) = wbar_ad(km+1) + cm(km)*temp_ad8
2662  wc_ad(km) = wc_ad(km) - temp_ad8
2663  pp_ad(km) = pp_ad(km) + temp_ad8
2664  pbar_ad(km+1) = 0.0
2665  pe1_ad = 0.0
2666  ELSE
2667  pe1_ad = 0.0
2668  DO k=km+1,2,-1
2669  CALL poprealarray(pe2(i, k))
2670  pe1_ad(k) = pe1_ad(k) + rdt*pe2_ad(i, k)
2671  pe2_ad(i, k) = 0.0
2672  END DO
2673  CALL popinteger(k)
2674  CALL poprealarray(pe2(i, 1))
2675  pe2_ad(i, 1) = 0.0
2676  DO n=ms,1,-1
2677  CALL popcontrol(2,branch)
2678  IF (branch .EQ. 0) THEN
2679  CALL popinteger(ad_from6)
2680  DO k=km,ad_from6,-1
2681  CALL poprealarray(dz2(i, k))
2682  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2683  wbar_ad(k+1) = wbar_ad(k+1) + dt*dz2_ad(i, k)
2684  wbar_ad(k) = wbar_ad(k) - dt*dz2_ad(i, k)
2685  dz2_ad(i, k) = 0.0
2686  END DO
2687  ELSE IF (branch .EQ. 1) THEN
2688  CALL popinteger(ad_from7)
2689  DO k=km,ad_from7,-1
2690  CALL poprealarray(w2(i, k))
2691  temp_ad20 = w2_ad(i, k)/dm(k)
2692  wm_ad(k) = wm_ad(k) + temp_ad20
2693  pbar_ad(k+1) = pbar_ad(k+1) + temp_ad20
2694  pbar_ad(k) = pbar_ad(k) - temp_ad20
2695  dm_ad(k) = dm_ad(k) - (wm(k)+pbar(k+1)-pbar(k))*temp_ad20/&
2696 & dm(k)
2697  w2_ad(i, k) = 0.0
2698  CALL poprealarray(dz2(i, k))
2699  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2700  wbar_ad(k+1) = wbar_ad(k+1) + dt*dz2_ad(i, k)
2701  wbar_ad(k) = wbar_ad(k) - dt*dz2_ad(i, k)
2702  dz2_ad(i, k) = 0.0
2703  END DO
2704  ELSE
2705  CALL popinteger(ad_from8)
2706  DO k=km,ad_from8,-1
2707  CALL poprealarray(wm(k))
2708  pbar_ad(k+1) = pbar_ad(k+1) + wm_ad(k)
2709  pbar_ad(k) = pbar_ad(k) - wm_ad(k)
2710  CALL poprealarray(dz(k))
2711  wbar_ad(k+1) = wbar_ad(k+1) + dt*dz_ad(k)
2712  wbar_ad(k) = wbar_ad(k) - dt*dz_ad(k)
2713  END DO
2714  END IF
2715  CALL popinteger(ad_from5)
2716  DO k=km+1,ad_from5,-1
2717  pbar_ad(k) = pbar_ad(k) + pe1_ad(k)
2718  CALL poprealarray(pbar(k))
2719  m_top_ad(k) = m_top_ad(k) + wbar(k)*pbar_ad(k)
2720  wbar_ad(k) = wbar_ad(k) + m_top(k)*pbar_ad(k)
2721  r_top_ad(k) = r_top_ad(k) - pbar_ad(k)
2722  pbar_ad(k) = 0.0
2723  END DO
2724  CALL popinteger(ad_from4)
2725  DO k=km,ad_from4,-1
2726  CALL poprealarray(wbar(k))
2727  temp_ad18 = wbar_ad(k)/(m_top(k)+m_bot(k))
2728  temp_ad19 = -((r_bot(k)+r_top(k))*temp_ad18/(m_top(k)+m_bot(&
2729 & k)))
2730  r_bot_ad(k) = r_bot_ad(k) + temp_ad18
2731  r_top_ad(k) = r_top_ad(k) + temp_ad18
2732  m_top_ad(k) = m_top_ad(k) + temp_ad19
2733  m_bot_ad(k) = m_bot_ad(k) + temp_ad19
2734  wbar_ad(k) = 0.0
2735  END DO
2736  CALL popinteger(k)
2737  CALL popcontrol(1,branch)
2738  IF (branch .NE. 0) THEN
2739  CALL poprealarray(wbar(1))
2740  temp_ad17 = wbar_ad(1)/m_bot(1)
2741  r_bot_ad(1) = r_bot_ad(1) + temp_ad17
2742  m_bot_ad(1) = m_bot_ad(1) - r_bot(1)*temp_ad17/m_bot(1)
2743  wbar_ad(1) = 0.0
2744  END IF
2745  CALL popcontrol(1,branch)
2746  IF (branch .NE. 0) THEN
2747  CALL popinteger(ad_from3)
2748  DO ke=km,ad_from3,-1
2749  CALL popcontrol(2,branch)
2750  IF (branch .EQ. 0) THEN
2751  z_frac = time_left/dts(k)
2752  CALL poprealarray(r_bot(ke))
2753  z_frac_ad = dm(k)*m_bot_ad(ke) + r_lo(k)*r_bot_ad(ke)
2754  r_lo_ad(k) = r_lo_ad(k) + z_frac*r_bot_ad(ke)
2755  CALL poprealarray(m_bot(ke))
2756  dm_ad(k) = dm_ad(k) + z_frac*m_bot_ad(ke)
2757  temp_ad16 = z_frac_ad/dts(k)
2758  time_left_ad = temp_ad16
2759  dts_ad(k) = dts_ad(k) - time_left*temp_ad16/dts(k)
2760  ELSE
2761  IF (branch .EQ. 1) THEN
2762  m_bot_ad(ke) = m_bot_ad(ke) + ws2(i)*r_bot_ad(ke)
2763  z_frac = time_left/dts(k)
2764  CALL poprealarray(r_bot(ke))
2765  z_frac_ad = dm(k)*m_bot_ad(ke) - r_hi(k)*r_bot_ad(ke)
2766  r_hi_ad(k) = r_hi_ad(k) - z_frac*r_bot_ad(ke)
2767  m_surf_ad = -(ws2(i)*r_bot_ad(ke))
2768  ws2_ad(i) = ws2_ad(i) + (m_bot(ke)-m_surf)*r_bot_ad(ke&
2769 & )
2770  CALL poprealarray(m_bot(ke))
2771  dm_ad(k) = dm_ad(k) + z_frac*m_bot_ad(ke)
2772  temp_ad15 = z_frac_ad/dts(k)
2773  time_left_ad = temp_ad15
2774  dts_ad(k) = dts_ad(k) - time_left*temp_ad15/dts(k)
2775  ELSE
2776  time_left_ad = 0.0
2777  m_surf_ad = 0.0
2778  END IF
2779  CALL popinteger(ad_count3)
2780  DO i4=1,ad_count3
2781  IF (i4 .EQ. 1) THEN
2782  CALL popcontrol(1,branch)
2783  IF (branch .EQ. 0) THEN
2784  time_left_ad = 0.0
2785  m_surf_ad = 0.0
2786  END IF
2787  ELSE
2788  CALL poprealarray(r_bot(ke))
2789  r_hi_ad(k) = r_hi_ad(k) - r_bot_ad(ke)
2790  CALL poprealarray(m_bot(ke))
2791  dm_ad(k) = dm_ad(k) + m_bot_ad(ke)
2792  dts_ad(k) = dts_ad(k) - time_left_ad
2793  END IF
2794  CALL popinteger(k)
2795  END DO
2796  CALL poprealarray(m_surf)
2797  m_bot_ad(ke) = m_bot_ad(ke) + m_surf_ad
2798  END IF
2799  CALL popinteger(ad_count2)
2800  DO i3=1,ad_count2
2801  IF (i3 .EQ. 1) THEN
2802  CALL popcontrol(1,branch)
2803  ELSE
2804  CALL poprealarray(r_bot(ke))
2805  r_lo_ad(k) = r_lo_ad(k) + r_bot_ad(ke)
2806  CALL poprealarray(m_bot(ke))
2807  dm_ad(k) = dm_ad(k) + m_bot_ad(ke)
2808  dts_ad(k) = dts_ad(k) - time_left_ad
2809  END IF
2810  CALL popinteger(k)
2811  END DO
2812  CALL poprealarray(time_left)
2813  END DO
2814  CALL popinteger(ad_from2)
2815  DO k=km,ad_from2,-1
2816  CALL poprealarray(r_bot(k))
2817  r_bot_ad(k) = 0.0
2818  CALL poprealarray(m_bot(k))
2819  m_bot_ad(k) = 0.0
2820  END DO
2821  CALL popinteger(k)
2822  CALL popinteger(ad_to4)
2823  DO ke=ad_to4,km+1,1
2824  CALL popcontrol(1,branch)
2825  IF (branch .EQ. 0) THEN
2826  z_frac = time_left/dts(k)
2827  CALL poprealarray(r_top(ke))
2828  z_frac_ad = dm(k)*m_top_ad(ke) + r_hi(k)*r_top_ad(ke)
2829  r_hi_ad(k) = r_hi_ad(k) + z_frac*r_top_ad(ke)
2830  CALL poprealarray(m_top(ke))
2831  dm_ad(k) = dm_ad(k) + z_frac*m_top_ad(ke)
2832  temp_ad14 = z_frac_ad/dts(k)
2833  time_left_ad = temp_ad14
2834  dts_ad(k) = dts_ad(k) - time_left*temp_ad14/dts(k)
2835  ELSE
2836  time_left_ad = 0.0
2837  END IF
2838  CALL popinteger(ad_count1)
2839  DO i2=1,ad_count1
2840  IF (i2 .EQ. 1) THEN
2841  CALL popcontrol(1,branch)
2842  IF (branch .EQ. 0) time_left_ad = 0.0
2843  ELSE
2844  CALL poprealarray(r_top(ke))
2845  r_hi_ad(k) = r_hi_ad(k) + r_top_ad(ke)
2846  CALL poprealarray(m_top(ke))
2847  dm_ad(k) = dm_ad(k) + m_top_ad(ke)
2848  dts_ad(k) = dts_ad(k) - time_left_ad
2849  END IF
2850  CALL popinteger(k)
2851  END DO
2852  CALL poprealarray(time_left)
2853  END DO
2854  CALL popinteger(ad_from1)
2855  DO k=km+1,ad_from1,-1
2856  CALL poprealarray(r_top(k))
2857  r_top_ad(k) = 0.0
2858  CALL poprealarray(m_top(k))
2859  m_top_ad(k) = 0.0
2860  END DO
2861  CALL popcontrol(1,branch)
2862  IF (branch .EQ. 0) GOTO 100
2863  END IF
2864  CALL popinteger(ad_from0)
2865  CALL popinteger(ad_to3)
2866  DO k=ad_to3,ad_from0,-1
2867  CALL poprealarray(m_top(k+1))
2868  m_bot_ad(k) = m_bot_ad(k) + m_top_ad(k+1)
2869  m_top_ad(k+1) = 0.0
2870  z_frac = dt/dts(k)
2871  CALL poprealarray(m_bot(k))
2872  z_frac_ad = r_hi(k)*r_top_ad(k+1) + r_lo(k)*r_bot_ad(k) + dm&
2873 & (k)*m_bot_ad(k)
2874  dm_ad(k) = dm_ad(k) + z_frac*m_bot_ad(k)
2875  m_bot_ad(k) = 0.0
2876  CALL poprealarray(r_top(k+1))
2877  r_hi_ad(k) = r_hi_ad(k) + z_frac*r_top_ad(k+1)
2878  r_top_ad(k+1) = 0.0
2879  CALL poprealarray(r_bot(k))
2880  r_lo_ad(k) = r_lo_ad(k) + z_frac*r_bot_ad(k)
2881  r_bot_ad(k) = 0.0
2882  dts_ad(k) = dts_ad(k) - dt*z_frac_ad/dts(k)**2
2883  END DO
2884  100 CALL popinteger(ad_count0)
2885  DO i1=1,ad_count0
2886  IF (i1 .EQ. 1) CALL popcontrol(1,branch)
2887  END DO
2888  CALL popinteger(ad_from)
2889  DO k=km,ad_from,-1
2890  CALL poprealarray(r_hi(k))
2891  wm_ad(k) = wm_ad(k) + r_lo_ad(k) + r_hi_ad(k)
2892  ptmp1_ad = r_lo_ad(k) - r_hi_ad(k)
2893  r_hi_ad(k) = 0.0
2894  CALL poprealarray(r_lo(k))
2895  r_lo_ad(k) = 0.0
2896  dts_ad(k) = dts_ad(k) + (pf-pm2(i, k))*ptmp1_ad
2897  pm2_ad(i, k) = pm2_ad(i, k) - dts(k)*ptmp1_ad
2898  rden = -(rgas*dm(k)/dz(k))
2899  temp1 = pf/rden
2900  temp2 = sqrt(grg*temp1)
2901  IF (grg*temp1 .EQ. 0.0) THEN
2902  temp_ad11 = 0.0
2903  ELSE
2904  temp_ad11 = grg*dz(k)*dts_ad(k)/(2.0*temp2**3*rden)
2905  END IF
2906  pf_ad = temp_ad11 + dts(k)*ptmp1_ad
2907  CALL poprealarray(dts(k))
2908  CALL poprealarray(pf)
2909  temp_ad13 = gama*exp(gama*log(rden*pt1(k)))*pf_ad/(rden*pt1(&
2910 & k))
2911  rden_ad = pt1(k)*temp_ad13 - temp1*temp_ad11
2912  pt1_ad(k) = pt1_ad(k) + rden*temp_ad13
2913  temp_ad12 = -(rgas*rden_ad/dz(k))
2914  dz_ad(k) = dz_ad(k) - dm(k)*temp_ad12/dz(k) - dts_ad(k)/&
2915 & temp2
2916  dts_ad(k) = 0.0
2917  dm_ad(k) = dm_ad(k) + temp_ad12
2918  END DO
2919  CALL popinteger(k)
2920  END DO
2921  CALL popcontrol(2,branch)
2922  IF (branch .EQ. 0) THEN
2923  GOTO 110
2924  ELSE IF (branch .EQ. 1) THEN
2925  CALL poprealarray(pbar(ks0))
2926  pbar_ad(ks0) = pbar_ad(ks0)/REAL(ms)
2927  CALL popcontrol(1,branch)
2928  IF (branch .EQ. 0) THEN
2929  CALL popinteger(ad_to1)
2930  DO k=ad_to1,1,-1
2931  CALL poprealarray(dz2(i, k))
2932  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2933  wbar_ad(k+1) = wbar_ad(k+1) + bdt*dz2_ad(i, k)
2934  wbar_ad(k) = wbar_ad(k) - bdt*dz2_ad(i, k)
2935  dz2_ad(i, k) = 0.0
2936  END DO
2937  ELSE
2938  CALL popinteger(ad_to2)
2939  DO k=ad_to2,1,-1
2940  CALL poprealarray(w2(i, k))
2941  temp_ad10 = w2_ad(i, k)/dm(k)
2942  wm_ad(k) = wm_ad(k) + temp_ad10
2943  pbar_ad(k+1) = pbar_ad(k+1) + temp_ad10
2944  pbar_ad(k) = pbar_ad(k) - temp_ad10
2945  dm_ad(k) = dm_ad(k) - (wm(k)+pbar(k+1)-pbar(k))*temp_ad10/&
2946 & dm(k)
2947  w2_ad(i, k) = 0.0
2948  CALL poprealarray(dz2(i, k))
2949  dz_ad(k) = dz_ad(k) + dz2_ad(i, k)
2950  wbar_ad(k+1) = wbar_ad(k+1) + bdt*dz2_ad(i, k)
2951  wbar_ad(k) = wbar_ad(k) - bdt*dz2_ad(i, k)
2952  dz2_ad(i, k) = 0.0
2953  END DO
2954  END IF
2955  ELSE
2956  GOTO 130
2957  END IF
2958  END IF
2959  CALL popinteger(ad_to0)
2960  DO k=ad_to0,2,-1
2961  pbar_ad(k) = pbar_ad(k) + pe1_ad(k)
2962  pe1_ad(k) = 0.0
2963  CALL poprealarray(pbar(k))
2964  temp_ad5 = bdt*pbar_ad(k)
2965  wbar_ad(k) = wbar_ad(k) + cm(k-1)*temp_ad5
2966  pp_ad(k-1) = pp_ad(k-1) + temp_ad5
2967  pbar_ad(k) = 0.0
2968  temp_ad7 = wbar_ad(k)/(cm(k-1)+cm(k))
2969  wc_ad(k-1) = wc_ad(k-1) + temp_ad7 - temp_ad5
2970  temp_ad6 = -((wc(k-1)+wc(k)+pp(k)-pp(k-1))*temp_ad7/(cm(k-1)+cm(&
2971 & k)))
2972  cm_ad(k-1) = cm_ad(k-1) + temp_ad6 + wbar(k)*temp_ad5
2973  CALL poprealarray(wbar(k))
2974  wc_ad(k) = wc_ad(k) + temp_ad7
2975  pp_ad(k) = pp_ad(k) + temp_ad7
2976  pp_ad(k-1) = pp_ad(k-1) - temp_ad7
2977  cm_ad(k) = cm_ad(k) + temp_ad6
2978  wbar_ad(k) = 0.0
2979  END DO
2980  CALL poprealarray(wbar(1))
2981  temp_ad4 = wbar_ad(1)/cm(1)
2982  wc_ad(1) = wc_ad(1) + temp_ad4
2983  pp_ad(1) = pp_ad(1) + temp_ad4
2984  cm_ad(1) = cm_ad(1) - (wc(1)+pp(1))*temp_ad4/cm(1)
2985  wbar_ad(1) = 0.0
2986  CALL popinteger(ad_to)
2987  DO k=ad_to,1,-1
2988  temp_ad3 = cm_ad(k)/dts(k)
2989  CALL poprealarray(pp(k))
2990  pf1_ad(k) = pf1_ad(k) + pp_ad(k)
2991  pm2_ad(i, k) = pm2_ad(i, k) - pp_ad(k)
2992  pp_ad(k) = 0.0
2993  CALL poprealarray(wc(k))
2994  temp_ad2 = wc_ad(k)/dts(k)
2995  wm_ad(k) = wm_ad(k) + temp_ad2
2996  dts_ad(k) = dts_ad(k) - dm(k)*temp_ad3/dts(k) - wm(k)*temp_ad2/&
2997 & dts(k)
2998  wc_ad(k) = 0.0
2999  CALL poprealarray(cm(k))
3000  dm_ad(k) = dm_ad(k) + temp_ad3
3001  cm_ad(k) = 0.0
3002  END DO
3003  CALL popinteger(k)
3004  110 CALL popinteger(ad_count)
3005  DO i0=1,ad_count
3006  IF (i0 .EQ. 1) THEN
3007  CALL popcontrol(1,branch)
3008  IF (branch .EQ. 0) GOTO 120
3009  END IF
3010  rden = -(rgas*dm(k)/dz(k))
3011  CALL poprealarray(dts(k))
3012  temp = pf1(k)/rden
3013  temp0 = sqrt(grg*temp)
3014  IF (grg*temp .EQ. 0.0) THEN
3015  temp_ad = 0.0
3016  ELSE
3017  temp_ad = grg*dz(k)*dts_ad(k)/(2.0*temp0**3*rden)
3018  END IF
3019  pf1_ad(k) = pf1_ad(k) + temp_ad
3020  CALL poprealarray(pf1(k))
3021  temp_ad1 = gama*exp(gama*log(rden*pt1(k)))*pf1_ad(k)/(rden*pt1(k&
3022 & ))
3023  rden_ad = pt1(k)*temp_ad1 - temp*temp_ad
3024  pt1_ad(k) = pt1_ad(k) + rden*temp_ad1
3025  pf1_ad(k) = 0.0
3026  temp_ad0 = -(rgas*rden_ad/dz(k))
3027  dz_ad(k) = dz_ad(k) - dm(k)*temp_ad0/dz(k) - dts_ad(k)/temp0
3028  dts_ad(k) = 0.0
3029  dm_ad(k) = dm_ad(k) + temp_ad0
3030  120 CALL popinteger(k)
3031  END DO
3032  130 CALL popinteger(ks0)
3033  CALL poprealarray(wbar(km+1))
3034  ws_ad(i) = ws_ad(i) + wbar_ad(km+1)
3035  wbar_ad(km+1) = 0.0
3036  DO k=km,1,-1
3037  CALL poprealarray(pt1(k))
3038  pt2_ad(i, k) = pt2_ad(i, k) + pt1_ad(k)
3039  pt1_ad(k) = 0.0
3040  CALL poprealarray(wm(k))
3041  w2_ad(i, k) = w2_ad(i, k) + dm(k)*wm_ad(k)
3042  dm_ad(k) = dm_ad(k) + w2(i, k)*wm_ad(k)
3043  wm_ad(k) = 0.0
3044  CALL poprealarray(dm(k))
3045  dm2_ad(i, k) = dm2_ad(i, k) + dm_ad(k)
3046  dm_ad(k) = 0.0
3047  CALL poprealarray(dz(k))
3048  dz2_ad(i, k) = dz2_ad(i, k) + dz_ad(k)
3049  dz_ad(k) = 0.0
3050  END DO
3051  END DO
3052  DO i=ie,is,-1
3053  ws_ad(i) = ws_ad(i) + 2.*ws2_ad(i)
3054  ws2_ad(i) = 0.0
3055  END DO
3056  END SUBROUTINE rim_2d_bwd
3057  SUBROUTINE rim_2d(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, dm2, pm2&
3058 & , w2, dz2, pt2, ws, c_core)
3059  IMPLICIT NONE
3060  INTEGER, INTENT(IN) :: ms, is, ie, km
3061  REAL, INTENT(IN) :: bdt, gama, rgas
3062  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pm2, gm2
3063  LOGICAL, INTENT(IN) :: c_core
3064  REAL, INTENT(IN) :: pt2(is:ie, km)
3065  REAL, INTENT(IN) :: ws(is:ie)
3066 ! IN/OUT:
3067  REAL, INTENT(INOUT) :: dz2(is:ie, km)
3068  REAL, INTENT(INOUT) :: w2(is:ie, km)
3069  REAL, INTENT(OUT) :: pe2(is:ie, km+1)
3070 ! Local:
3071  REAL :: ws2(is:ie)
3072  REAL, DIMENSION(km+1) :: m_bot, m_top, r_bot, r_top, pe1, pbar, wbar
3073  REAL, DIMENSION(km) :: r_hi, r_lo, dz, wm, dm, dts
3074  REAL, DIMENSION(km) :: pf1, wc, cm, pp, pt1
3075  REAL :: dt, rdt, grg, z_frac, ptmp1, rden, pf, time_left
3076  REAL :: m_surf
3077  INTEGER :: i, k, n, ke, kt1, ktop
3078  INTEGER :: ks0, ks1
3079  INTRINSIC real
3080  INTRINSIC log
3081  INTRINSIC exp
3082  INTRINSIC sqrt
3083  INTRINSIC max
3084  grg = gama*rgas
3085  rdt = 1./bdt
3086  dt = bdt/REAL(ms)
3087  pbar(:) = 0.
3088  wbar(:) = 0.
3089  DO i=is,ie
3090  ws2(i) = 2.*ws(i)
3091  END DO
3092 ! end i-loop
3093  DO i=is,ie
3094  DO k=1,km
3095  dz(k) = dz2(i, k)
3096  dm(k) = dm2(i, k)
3097  wm(k) = w2(i, k)*dm(k)
3098  pt1(k) = pt2(i, k)
3099  END DO
3100  pe1(:) = 0.
3101  wbar(km+1) = ws(i)
3102  ks0 = 1
3103  IF (ms .GT. 1 .AND. ms .LT. 8) THEN
3104 ! Continuity of (pbar, wbar) is maintained
3105  DO k=1,km
3106  rden = -(rgas*dm(k)/dz(k))
3107  pf1(k) = exp(gama*log(rden*pt1(k)))
3108  dts(k) = -(dz(k)/sqrt(grg*pf1(k)/rden))
3109  IF (bdt .GT. dts(k)) THEN
3110  ks0 = k - 1
3111  GOTO 222
3112  END IF
3113  END DO
3114  ks0 = km
3115  222 IF (ks0 .NE. 1) THEN
3116  DO k=1,ks0
3117  cm(k) = dm(k)/dts(k)
3118  wc(k) = wm(k)/dts(k)
3119  pp(k) = pf1(k) - pm2(i, k)
3120  END DO
3121  wbar(1) = (wc(1)+pp(1))/cm(1)
3122  DO k=2,ks0
3123  wbar(k) = (wc(k-1)+wc(k)+pp(k)-pp(k-1))/(cm(k-1)+cm(k))
3124  pbar(k) = bdt*(cm(k-1)*wbar(k)-wc(k-1)+pp(k-1))
3125  pe1(k) = pbar(k)
3126  END DO
3127  IF (ks0 .EQ. km) THEN
3128  pbar(km+1) = bdt*(cm(km)*wbar(km+1)-wc(km)+pp(km))
3129  IF (c_core) THEN
3130  DO k=1,km
3131  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
3132  END DO
3133  ELSE
3134  DO k=1,km
3135  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
3136  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
3137  END DO
3138  END IF
3139  pe2(i, 1) = 0.
3140  DO k=2,km+1
3141  pe2(i, k) = pbar(k)*rdt
3142  END DO
3143  GOTO 6000
3144  ELSE
3145 ! next i
3146  IF (c_core) THEN
3147  DO k=1,ks0-1
3148  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
3149  END DO
3150  ELSE
3151  DO k=1,ks0-1
3152  dz2(i, k) = dz(k) + bdt*(wbar(k+1)-wbar(k))
3153  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
3154  END DO
3155  END IF
3156  pbar(ks0) = pbar(ks0)/REAL(ms)
3157  END IF
3158  END IF
3159  END IF
3160  ks1 = ks0
3161  DO n=1,ms
3162  DO k=ks1,km
3163  rden = -(rgas*dm(k)/dz(k))
3164  pf = exp(gama*log(rden*pt1(k)))
3165  dts(k) = -(dz(k)/sqrt(grg*pf/rden))
3166  ptmp1 = dts(k)*(pf-pm2(i, k))
3167  r_lo(k) = wm(k) + ptmp1
3168  r_hi(k) = wm(k) - ptmp1
3169  END DO
3170  ktop = ks1
3171  DO k=ks1,km
3172  IF (dt .GT. dts(k)) THEN
3173  ktop = k - 1
3174  GOTO 333
3175  END IF
3176  END DO
3177  ktop = km
3178  333 IF (ktop .GE. ks1) THEN
3179  DO k=ks1,ktop
3180  z_frac = dt/dts(k)
3181  r_bot(k) = z_frac*r_lo(k)
3182  r_top(k+1) = z_frac*r_hi(k)
3183  m_bot(k) = z_frac*dm(k)
3184  m_top(k+1) = m_bot(k)
3185  END DO
3186  IF (ktop .EQ. km) GOTO 666
3187  END IF
3188  DO k=ktop+2,km+1
3189  m_top(k) = 0.
3190  r_top(k) = 0.
3191  END DO
3192  IF (1 .LT. ktop) THEN
3193  kt1 = ktop
3194  ELSE
3195  kt1 = 1
3196  END IF
3197  DO ke=km+1,ktop+2,-1
3198  time_left = dt
3199  DO k=ke-1,kt1,-1
3200  IF (time_left .GT. dts(k)) THEN
3201  time_left = time_left - dts(k)
3202  m_top(ke) = m_top(ke) + dm(k)
3203  r_top(ke) = r_top(ke) + r_hi(k)
3204  ELSE
3205  z_frac = time_left/dts(k)
3206  m_top(ke) = m_top(ke) + z_frac*dm(k)
3207  r_top(ke) = r_top(ke) + z_frac*r_hi(k)
3208  GOTO 444
3209  END IF
3210  END DO
3211  444 CONTINUE
3212  END DO
3213 ! next level
3214  DO k=ktop+1,km
3215  m_bot(k) = 0.
3216  r_bot(k) = 0.
3217  END DO
3218  DO ke=ktop+1,km
3219  time_left = dt
3220  DO k=ke,km
3221  IF (time_left .GT. dts(k)) THEN
3222  time_left = time_left - dts(k)
3223  m_bot(ke) = m_bot(ke) + dm(k)
3224  r_bot(ke) = r_bot(ke) + r_lo(k)
3225  ELSE
3226  z_frac = time_left/dts(k)
3227  m_bot(ke) = m_bot(ke) + z_frac*dm(k)
3228  r_bot(ke) = r_bot(ke) + z_frac*r_lo(k)
3229  GOTO 4000
3230  END IF
3231  END DO
3232 ! next interface
3233  m_surf = m_bot(ke)
3234  DO k=km,kt1,-1
3235  IF (time_left .GT. dts(k)) THEN
3236  time_left = time_left - dts(k)
3237  m_bot(ke) = m_bot(ke) + dm(k)
3238  r_bot(ke) = r_bot(ke) - r_hi(k)
3239  ELSE
3240  z_frac = time_left/dts(k)
3241  m_bot(ke) = m_bot(ke) + z_frac*dm(k)
3242  r_bot(ke) = r_bot(ke) - z_frac*r_hi(k) + (m_bot(ke)-m_surf&
3243 & )*ws2(i)
3244  GOTO 4000
3245  END IF
3246  END DO
3247  4000 CONTINUE
3248  END DO
3249 ! next interface
3250  666 IF (ks1 .EQ. 1) wbar(1) = r_bot(1)/m_bot(1)
3251  DO k=ks1+1,km
3252  wbar(k) = (r_bot(k)+r_top(k))/(m_top(k)+m_bot(k))
3253  END DO
3254 ! pbar here is actually dt*pbar
3255  DO k=ks1+1,km+1
3256  pbar(k) = m_top(k)*wbar(k) - r_top(k)
3257  pe1(k) = pe1(k) + pbar(k)
3258  END DO
3259  IF (n .EQ. ms) THEN
3260  IF (c_core) THEN
3261  DO k=ks1,km
3262  dz2(i, k) = dz(k) + dt*(wbar(k+1)-wbar(k))
3263  END DO
3264  ELSE
3265  DO k=ks1,km
3266  dz2(i, k) = dz(k) + dt*(wbar(k+1)-wbar(k))
3267  w2(i, k) = (wm(k)+pbar(k+1)-pbar(k))/dm(k)
3268  END DO
3269  END IF
3270  ELSE
3271  DO k=ks1,km
3272  dz(k) = dz(k) + dt*(wbar(k+1)-wbar(k))
3273  wm(k) = wm(k) + pbar(k+1) - pbar(k)
3274  END DO
3275  END IF
3276  END DO
3277  pe2(i, 1) = 0.
3278  DO k=2,km+1
3279  pe2(i, k) = pe1(k)*rdt
3280  END DO
3281  6000 CONTINUE
3282  END DO
3283  END SUBROUTINE rim_2d
3284 ! Differentiation of sim3_solver in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
3285 !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
3286 !_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.
3287 !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
3288 !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
3289 !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
3290 !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
3291 !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
3292 ! 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_
3293 !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
3294 !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
3295 !_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
3296 !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
3297 !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
3298 !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
3299 !_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
3300 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
3301 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
3302 ! gradient of useful results: ws dm pe2 dz2 w2 pem pt2
3303 ! with respect to varying inputs: ws dm pe2 dz2 w2 pem pt2
3304  SUBROUTINE sim3_solver_fwd(dt, is, ie, km, rgas, gama, kappa, pe2, dm&
3305 & , pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
3306  IMPLICIT NONE
3307  INTEGER, INTENT(IN) :: is, ie, km
3308  REAL, INTENT(IN) :: dt, rgas, gama, kappa, alpha, p_fac, scale_m
3309  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
3310  REAL, INTENT(IN) :: ws(is:ie)
3311  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
3312  REAL :: pe2(is:ie, km+1)
3313  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
3314 ! Local
3315  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
3316  REAL, DIMENSION(is:ie, km+1) :: pp
3317  REAL, DIMENSION(is:ie) :: p1, wk1, bet
3318  REAL :: beta, t2, t1g, rdt, ra, capa1, r2g, r6g
3319  INTEGER :: i, k
3320  INTRINSIC log
3321  INTRINSIC exp
3322  INTRINSIC max
3323 
3324  aa = 0.0
3325  bb = 0.0
3326  dd = 0.0
3327  w1 = 0.0
3328  wk = 0.0
3329  g_rat = 0.0
3330  gam = 0.0
3331  pp = 0.0
3332  p1 = 0.0
3333  wk1 = 0.0
3334  bet = 0.0
3335  beta = 0.0
3336  t2 = 0.0
3337  t1g = 0.0
3338  rdt = 0.0
3339  ra = 0.0
3340  capa1 = 0.0
3341  r2g = 0.0
3342  r6g = 0.0
3343 
3344  beta = 1. - alpha
3345  ra = 1./alpha
3346  t2 = beta/alpha
3347  t1g = gama*2.*(alpha*dt)**2
3348  rdt = 1./dt
3349  capa1 = kappa - 1.
3350  r2g = grav/2.
3351  r6g = grav/6.
3352  DO k=1,km
3353  DO i=is,ie
3354  w1(i, k) = w2(i, k)
3355 ! Full pressure at center
3356  aa(i, k) = exp(gama*log(-(dm(i, k)/dz2(i, k)*rgas*pt2(i, k))))
3357  END DO
3358  END DO
3359  DO k=1,km-1
3360  DO i=is,ie
3361 ! for profile reconstruction
3362  g_rat(i, k) = dm(i, k)/dm(i, k+1)
3363  bb(i, k) = 2.*(1.+g_rat(i, k))
3364  dd(i, k) = 3.*(aa(i, k)+g_rat(i, k)*aa(i, k+1))
3365  END DO
3366  END DO
3367 ! pe2 is full p at edges
3368  DO i=is,ie
3369 ! Top:
3370  bet(i) = bb(i, 1)
3371  CALL pushrealarray(pe2(i, 1))
3372  pe2(i, 1) = pem(i, 1)
3373  CALL pushrealarray(pe2(i, 2))
3374  pe2(i, 2) = (dd(i, 1)-pem(i, 1))/bet(i)
3375 ! Bottom:
3376  bb(i, km) = 2.
3377  CALL pushrealarray(dd(i, km))
3378  dd(i, km) = 3.*aa(i, km) + r2g*dm(i, km)
3379  END DO
3380  DO k=2,km
3381  DO i=is,ie
3382  gam(i, k) = g_rat(i, k-1)/bet(i)
3383  CALL pushrealarray(bet(i))
3384  bet(i) = bb(i, k) - gam(i, k)
3385  CALL pushrealarray(pe2(i, k+1))
3386  pe2(i, k+1) = (dd(i, k)-pe2(i, k))/bet(i)
3387  END DO
3388  END DO
3389  DO k=km,2,-1
3390  DO i=is,ie
3391  CALL pushrealarray(pe2(i, k))
3392  pe2(i, k) = pe2(i, k) - gam(i, k)*pe2(i, k+1)
3393  END DO
3394  END DO
3395 ! done reconstruction of full:
3396 ! pp is pert. p at edges
3397  DO k=1,km+1
3398  DO i=is,ie
3399  pp(i, k) = pe2(i, k) - pem(i, k)
3400  END DO
3401  END DO
3402  DO k=2,km
3403  DO i=is,ie
3404  CALL pushrealarray(aa(i, k))
3405  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k)
3406  wk(i, k) = t2*aa(i, k)*(w1(i, k-1)-w1(i, k))
3407  CALL pushrealarray(aa(i, k))
3408  aa(i, k) = aa(i, k) - scale_m*dm(i, 1)
3409  END DO
3410  END DO
3411  DO i=is,ie
3412  CALL pushrealarray(bet(i))
3413  bet(i) = dm(i, 1) - aa(i, 2)
3414  CALL pushrealarray(w2(i, 1))
3415  w2(i, 1) = (dm(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))/bet(i)
3416  END DO
3417  DO k=2,km-1
3418  DO i=is,ie
3419  CALL pushrealarray(gam(i, k))
3420  gam(i, k) = aa(i, k)/bet(i)
3421  CALL pushrealarray(bet(i))
3422  bet(i) = dm(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
3423  CALL pushrealarray(w2(i, k))
3424  w2(i, k) = (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))+wk(i, k+1&
3425 & )-wk(i, k)-aa(i, k)*w2(i, k-1))/bet(i)
3426  END DO
3427  END DO
3428  DO i=is,ie
3429  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
3430  CALL pushrealarray(gam(i, km))
3431  gam(i, km) = aa(i, km)/bet(i)
3432  CALL pushrealarray(bet(i))
3433  bet(i) = dm(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
3434  CALL pushrealarray(w2(i, km))
3435  w2(i, km) = (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk(i, &
3436 & km)+wk1(i)*(t2*w1(i, km)-ra*ws(i))-aa(i, km)*w2(i, km-1))/bet(i)
3437  END DO
3438  DO k=km-1,1,-1
3439  DO i=is,ie
3440  CALL pushrealarray(w2(i, k))
3441  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
3442  END DO
3443  END DO
3444 ! pe2 is updated perturbation p at edges
3445  DO i=is,ie
3446  CALL pushrealarray(pe2(i, 1))
3447  pe2(i, 1) = 0.
3448  END DO
3449  DO k=1,km
3450  DO i=is,ie
3451  CALL pushrealarray(pe2(i, k+1))
3452  pe2(i, k+1) = pe2(i, k) + (dm(i, k)*(w2(i, k)-w1(i, k))*rdt-beta&
3453 & *(pp(i, k+1)-pp(i, k)))*ra
3454  END DO
3455  END DO
3456 ! Full non-hydro pressure at edges:
3457  DO i=is,ie
3458  CALL pushrealarray(pe2(i, 1))
3459  pe2(i, 1) = pem(i, 1)
3460  END DO
3461  DO k=2,km+1
3462  DO i=is,ie
3463  IF (p_fac*pem(i, k) .LT. pe2(i, k) + pem(i, k)) THEN
3464  CALL pushrealarray(pe2(i, k))
3465  pe2(i, k) = pe2(i, k) + pem(i, k)
3466  CALL pushcontrol(1,0)
3467  ELSE
3468  CALL pushrealarray(pe2(i, k))
3469  pe2(i, k) = p_fac*pem(i, k)
3470  CALL pushcontrol(1,1)
3471  END IF
3472  END DO
3473  END DO
3474  DO i=is,ie
3475 ! Recover cell-averaged pressure
3476  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3 - r6g*dm(i, km)
3477  CALL pushrealarray(dz2(i, km))
3478  dz2(i, km) = -(dm(i, km)*rgas*pt2(i, km)*exp(capa1*log(p1(i))))
3479  END DO
3480  DO k=km-1,1,-1
3481  DO i=is,ie
3482  CALL pushrealarray(p1(i))
3483  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
3484 & *r3 - g_rat(i, k)*p1(i)
3485  CALL pushrealarray(dz2(i, k))
3486  dz2(i, k) = -(dm(i, k)*rgas*pt2(i, k)*exp(capa1*log(p1(i))))
3487  END DO
3488  END DO
3489  DO k=1,km+1
3490  DO i=is,ie
3491  CALL pushrealarray(pe2(i, k))
3492  pe2(i, k) = pe2(i, k) - pem(i, k)
3493  pe2(i, k) = pe2(i, k) + beta*(pp(i, k)-pe2(i, k))
3494  END DO
3495  END DO
3496  CALL pushrealarray(gam, (ie-is+1)*km)
3497  CALL pushrealarray(t1g)
3498  CALL pushrealarray(wk, (ie-is+1)*km)
3499  CALL pushrealarray(capa1)
3500  CALL pushrealarray(g_rat, (ie-is+1)*km)
3501  CALL pushrealarray(pp, (ie-is+1)*(km+1))
3502  CALL pushrealarray(beta)
3503  CALL pushrealarray(rdt)
3504  CALL pushrealarray(t2)
3505  CALL pushrealarray(w1, (ie-is+1)*km)
3506  CALL pushrealarray(bb, (ie-is+1)*km)
3507  CALL pushrealarray(ra)
3508  CALL pushrealarray(r6g)
3509  CALL pushrealarray(wk1, ie - is + 1)
3510  CALL pushrealarray(bet, ie - is + 1)
3511  CALL pushrealarray(p1, ie - is + 1)
3512  CALL pushrealarray(r2g)
3513  CALL pushrealarray(aa, (ie-is+1)*km)
3514  CALL pushrealarray(dd, (ie-is+1)*km)
3515  END SUBROUTINE sim3_solver_fwd
3516 ! Differentiation of sim3_solver in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
3517 !_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
3518 !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
3519 !.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
3520 !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
3521 !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.
3522 !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
3523 ! 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
3524 !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
3525 !_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
3526 !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
3527 !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_
3528 !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
3529 !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.
3530 !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_
3531 !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
3532 !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
3533 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
3534 ! gradient of useful results: ws dm pe2 dz2 w2 pem pt2
3535 ! with respect to varying inputs: ws dm pe2 dz2 w2 pem pt2
3536  SUBROUTINE sim3_solver_bwd(dt, is, ie, km, rgas, gama, kappa, pe2, &
3537 & pe2_ad, dm, dm_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad&
3538 & , ws, ws_ad, alpha, p_fac, scale_m)
3539  IMPLICIT NONE
3540  INTEGER, INTENT(IN) :: is, ie, km
3541  REAL, INTENT(IN) :: dt, rgas, gama, kappa, alpha, p_fac, scale_m
3542  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
3543  REAL, DIMENSION(is:ie, km) :: dm_ad, pt2_ad
3544  REAL, INTENT(IN) :: ws(is:ie)
3545  REAL :: ws_ad(is:ie)
3546  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
3547  REAL, DIMENSION(is:ie, km+1) :: pem_ad
3548  REAL :: pe2(is:ie, km+1)
3549  REAL :: pe2_ad(is:ie, km+1)
3550  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
3551  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2_ad, w2_ad
3552  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
3553  REAL, DIMENSION(is:ie, km) :: aa_ad, bb_ad, dd_ad, w1_ad, wk_ad, &
3554 & g_rat_ad, gam_ad
3555  REAL, DIMENSION(is:ie, km+1) :: pp
3556  REAL, DIMENSION(is:ie, km+1) :: pp_ad
3557  REAL, DIMENSION(is:ie) :: p1, wk1, bet
3558  REAL, DIMENSION(is:ie) :: p1_ad, wk1_ad, bet_ad
3559  REAL :: beta, t2, t1g, rdt, ra, capa1, r2g, r6g
3560  INTEGER :: i, k
3561  INTRINSIC log
3562  INTRINSIC exp
3563  INTRINSIC max
3564  REAL :: temp
3565  REAL :: temp0
3566  REAL :: temp1
3567  REAL :: temp_ad
3568  REAL :: temp_ad0
3569  REAL :: temp_ad1
3570  REAL :: temp_ad2
3571  REAL :: temp_ad3
3572  REAL :: temp_ad4
3573  REAL :: temp2
3574  REAL :: temp_ad5
3575  REAL :: temp_ad6
3576  REAL :: temp_ad7
3577  REAL :: temp_ad8
3578  REAL :: temp_ad9
3579  REAL :: temp_ad10
3580  REAL :: temp3
3581  REAL :: temp_ad11
3582  REAL :: temp_ad12
3583  REAL :: temp_ad13
3584  REAL :: temp_ad14
3585  REAL :: temp_ad15
3586  REAL :: temp4
3587  REAL :: temp_ad16
3588  REAL :: temp5
3589  REAL :: temp_ad17
3590  REAL :: temp_ad18
3591  INTEGER :: branch
3592 
3593  aa = 0.0
3594  bb = 0.0
3595  dd = 0.0
3596  w1 = 0.0
3597  wk = 0.0
3598  g_rat = 0.0
3599  gam = 0.0
3600  pp = 0.0
3601  p1 = 0.0
3602  wk1 = 0.0
3603  bet = 0.0
3604  beta = 0.0
3605  t2 = 0.0
3606  t1g = 0.0
3607  rdt = 0.0
3608  ra = 0.0
3609  capa1 = 0.0
3610  r2g = 0.0
3611  r6g = 0.0
3612  branch = 0
3613 
3614  CALL poprealarray(dd, (ie-is+1)*km)
3615  CALL poprealarray(aa, (ie-is+1)*km)
3616  CALL poprealarray(r2g)
3617  CALL poprealarray(p1, ie - is + 1)
3618  CALL poprealarray(bet, ie - is + 1)
3619  CALL poprealarray(wk1, ie - is + 1)
3620  CALL poprealarray(r6g)
3621  CALL poprealarray(ra)
3622  CALL poprealarray(bb, (ie-is+1)*km)
3623  CALL poprealarray(w1, (ie-is+1)*km)
3624  CALL poprealarray(t2)
3625  CALL poprealarray(rdt)
3626  CALL poprealarray(beta)
3627  CALL poprealarray(pp, (ie-is+1)*(km+1))
3628  CALL poprealarray(g_rat, (ie-is+1)*km)
3629  CALL poprealarray(capa1)
3630  CALL poprealarray(wk, (ie-is+1)*km)
3631  CALL poprealarray(t1g)
3632  CALL poprealarray(gam, (ie-is+1)*km)
3633  pp_ad = 0.0
3634  DO k=km+1,1,-1
3635  DO i=ie,is,-1
3636  pp_ad(i, k) = pp_ad(i, k) + beta*pe2_ad(i, k)
3637  pe2_ad(i, k) = (1.0-beta)*pe2_ad(i, k)
3638  CALL poprealarray(pe2(i, k))
3639  pem_ad(i, k) = pem_ad(i, k) - pe2_ad(i, k)
3640  END DO
3641  END DO
3642  p1_ad = 0.0
3643  bb_ad = 0.0
3644  g_rat_ad = 0.0
3645  DO k=1,km-1,1
3646  DO i=ie,is,-1
3647  CALL poprealarray(dz2(i, k))
3648  temp5 = capa1*log(p1(i))
3649  temp_ad17 = -(rgas*exp(temp5)*dz2_ad(i, k))
3650  dm_ad(i, k) = dm_ad(i, k) + pt2(i, k)*temp_ad17
3651  pt2_ad(i, k) = pt2_ad(i, k) + dm(i, k)*temp_ad17
3652  p1_ad(i) = p1_ad(i) - capa1*exp(temp5)*dm(i, k)*pt2(i, k)*rgas*&
3653 & dz2_ad(i, k)/p1(i)
3654  dz2_ad(i, k) = 0.0
3655  CALL poprealarray(p1(i))
3656  temp_ad18 = r3*p1_ad(i)
3657  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad18
3658  bb_ad(i, k) = bb_ad(i, k) + pe2(i, k+1)*temp_ad18
3659  pe2_ad(i, k+1) = pe2_ad(i, k+1) + bb(i, k)*temp_ad18
3660  g_rat_ad(i, k) = g_rat_ad(i, k) + pe2(i, k+2)*temp_ad18 - p1(i)*&
3661 & p1_ad(i)
3662  pe2_ad(i, k+2) = pe2_ad(i, k+2) + g_rat(i, k)*temp_ad18
3663  p1_ad(i) = -(g_rat(i, k)*p1_ad(i))
3664  END DO
3665  END DO
3666  DO i=ie,is,-1
3667  CALL poprealarray(dz2(i, km))
3668  temp4 = capa1*log(p1(i))
3669  temp_ad16 = -(rgas*exp(temp4)*dz2_ad(i, km))
3670  pt2_ad(i, km) = pt2_ad(i, km) + dm(i, km)*temp_ad16
3671  p1_ad(i) = p1_ad(i) - capa1*exp(temp4)*dm(i, km)*pt2(i, km)*rgas*&
3672 & dz2_ad(i, km)/p1(i)
3673  dm_ad(i, km) = dm_ad(i, km) + pt2(i, km)*temp_ad16 - r6g*p1_ad(i)
3674  dz2_ad(i, km) = 0.0
3675  pe2_ad(i, km) = pe2_ad(i, km) + r3*p1_ad(i)
3676  pe2_ad(i, km+1) = pe2_ad(i, km+1) + r3*2.*p1_ad(i)
3677  p1_ad(i) = 0.0
3678  END DO
3679  DO k=km+1,2,-1
3680  DO i=ie,is,-1
3681  CALL popcontrol(1,branch)
3682  IF (branch .EQ. 0) THEN
3683  CALL poprealarray(pe2(i, k))
3684  pem_ad(i, k) = pem_ad(i, k) + pe2_ad(i, k)
3685  ELSE
3686  CALL poprealarray(pe2(i, k))
3687  pem_ad(i, k) = pem_ad(i, k) + p_fac*pe2_ad(i, k)
3688  pe2_ad(i, k) = 0.0
3689  END IF
3690  END DO
3691  END DO
3692  DO i=ie,is,-1
3693  CALL poprealarray(pe2(i, 1))
3694  pem_ad(i, 1) = pem_ad(i, 1) + pe2_ad(i, 1)
3695  pe2_ad(i, 1) = 0.0
3696  END DO
3697  w1_ad = 0.0
3698  DO k=km,1,-1
3699  DO i=ie,is,-1
3700  CALL poprealarray(pe2(i, k+1))
3701  temp_ad14 = ra*pe2_ad(i, k+1)
3702  temp_ad15 = rdt*dm(i, k)*temp_ad14
3703  pe2_ad(i, k) = pe2_ad(i, k) + pe2_ad(i, k+1)
3704  dm_ad(i, k) = dm_ad(i, k) + rdt*(w2(i, k)-w1(i, k))*temp_ad14
3705  w2_ad(i, k) = w2_ad(i, k) + temp_ad15
3706  w1_ad(i, k) = w1_ad(i, k) - temp_ad15
3707  pp_ad(i, k+1) = pp_ad(i, k+1) - beta*temp_ad14
3708  pp_ad(i, k) = pp_ad(i, k) + beta*temp_ad14
3709  pe2_ad(i, k+1) = 0.0
3710  END DO
3711  END DO
3712  DO i=ie,is,-1
3713  CALL poprealarray(pe2(i, 1))
3714  pe2_ad(i, 1) = 0.0
3715  END DO
3716  gam_ad = 0.0
3717  DO k=1,km-1,1
3718  DO i=ie,is,-1
3719  CALL poprealarray(w2(i, k))
3720  gam_ad(i, k+1) = gam_ad(i, k+1) - w2(i, k+1)*w2_ad(i, k)
3721  w2_ad(i, k+1) = w2_ad(i, k+1) - gam(i, k+1)*w2_ad(i, k)
3722  END DO
3723  END DO
3724  aa_ad = 0.0
3725  bet_ad = 0.0
3726  wk1_ad = 0.0
3727  wk_ad = 0.0
3728  DO i=ie,is,-1
3729  CALL poprealarray(w2(i, km))
3730  temp_ad11 = w2_ad(i, km)/bet(i)
3731  temp3 = t2*w1(i, km) - ra*ws(i)
3732  w1_ad(i, km) = w1_ad(i, km) + (wk1(i)*t2+dm(i, km))*temp_ad11
3733  pp_ad(i, km+1) = pp_ad(i, km+1) + dt*temp_ad11
3734  pp_ad(i, km) = pp_ad(i, km) - dt*temp_ad11
3735  wk_ad(i, km) = wk_ad(i, km) - temp_ad11
3736  ws_ad(i) = ws_ad(i) - wk1(i)*ra*temp_ad11
3737  w2_ad(i, km-1) = w2_ad(i, km-1) - aa(i, km)*temp_ad11
3738  bet_ad(i) = bet_ad(i) - (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i&
3739 & , km))-wk(i, km)+wk1(i)*temp3-aa(i, km)*w2(i, km-1))*temp_ad11/&
3740 & bet(i)
3741  dm_ad(i, km) = dm_ad(i, km) + bet_ad(i) + w1(i, km)*temp_ad11
3742  wk1_ad(i) = wk1_ad(i) + temp3*temp_ad11 - bet_ad(i)
3743  w2_ad(i, km) = 0.0
3744  CALL poprealarray(bet(i))
3745  gam_ad(i, km) = gam_ad(i, km) - aa(i, km)*bet_ad(i)
3746  temp_ad12 = gam_ad(i, km)/bet(i)
3747  aa_ad(i, km) = aa_ad(i, km) + ((-1.0)-gam(i, km))*bet_ad(i) + &
3748 & temp_ad12 - w2(i, km-1)*temp_ad11
3749  bet_ad(i) = -(aa(i, km)*temp_ad12/bet(i))
3750  CALL poprealarray(gam(i, km))
3751  gam_ad(i, km) = 0.0
3752  temp_ad13 = t1g*wk1_ad(i)/dz2(i, km)
3753  pe2_ad(i, km+1) = pe2_ad(i, km+1) + temp_ad13
3754  dz2_ad(i, km) = dz2_ad(i, km) - pe2(i, km+1)*temp_ad13/dz2(i, km)
3755  wk1_ad(i) = 0.0
3756  END DO
3757  DO k=km-1,2,-1
3758  DO i=ie,is,-1
3759  CALL poprealarray(w2(i, k))
3760  temp_ad9 = w2_ad(i, k)/bet(i)
3761  w1_ad(i, k) = w1_ad(i, k) + dm(i, k)*temp_ad9
3762  pp_ad(i, k+1) = pp_ad(i, k+1) + dt*temp_ad9
3763  pp_ad(i, k) = pp_ad(i, k) - dt*temp_ad9
3764  wk_ad(i, k+1) = wk_ad(i, k+1) + temp_ad9
3765  wk_ad(i, k) = wk_ad(i, k) - temp_ad9
3766  w2_ad(i, k-1) = w2_ad(i, k-1) - aa(i, k)*temp_ad9
3767  bet_ad(i) = bet_ad(i) - (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, &
3768 & k))+wk(i, k+1)-wk(i, k)-aa(i, k)*w2(i, k-1))*temp_ad9/bet(i)
3769  dm_ad(i, k) = dm_ad(i, k) + bet_ad(i) + w1(i, k)*temp_ad9
3770  aa_ad(i, k) = aa_ad(i, k) + ((-1.0)-gam(i, k))*bet_ad(i) - w2(i&
3771 & , k-1)*temp_ad9
3772  w2_ad(i, k) = 0.0
3773  CALL poprealarray(bet(i))
3774  aa_ad(i, k+1) = aa_ad(i, k+1) - bet_ad(i)
3775  gam_ad(i, k) = gam_ad(i, k) - aa(i, k)*bet_ad(i)
3776  CALL poprealarray(gam(i, k))
3777  temp_ad10 = gam_ad(i, k)/bet(i)
3778  bet_ad(i) = -(aa(i, k)*temp_ad10/bet(i))
3779  aa_ad(i, k) = aa_ad(i, k) + temp_ad10
3780  gam_ad(i, k) = 0.0
3781  END DO
3782  END DO
3783  DO i=ie,is,-1
3784  CALL poprealarray(w2(i, 1))
3785  temp_ad8 = w2_ad(i, 1)/bet(i)
3786  w1_ad(i, 1) = w1_ad(i, 1) + dm(i, 1)*temp_ad8
3787  pp_ad(i, 2) = pp_ad(i, 2) + dt*temp_ad8
3788  wk_ad(i, 2) = wk_ad(i, 2) + temp_ad8
3789  bet_ad(i) = bet_ad(i) - (dm(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))*&
3790 & temp_ad8/bet(i)
3791  dm_ad(i, 1) = dm_ad(i, 1) + bet_ad(i) + w1(i, 1)*temp_ad8
3792  w2_ad(i, 1) = 0.0
3793  CALL poprealarray(bet(i))
3794  aa_ad(i, 2) = aa_ad(i, 2) - bet_ad(i)
3795  bet_ad(i) = 0.0
3796  END DO
3797  DO k=km,2,-1
3798  DO i=ie,is,-1
3799  CALL poprealarray(aa(i, k))
3800  dm_ad(i, 1) = dm_ad(i, 1) - scale_m*aa_ad(i, k)
3801  temp_ad5 = t2*aa(i, k)*wk_ad(i, k)
3802  aa_ad(i, k) = aa_ad(i, k) + t2*(w1(i, k-1)-w1(i, k))*wk_ad(i, k)
3803  w1_ad(i, k-1) = w1_ad(i, k-1) + temp_ad5
3804  w1_ad(i, k) = w1_ad(i, k) - temp_ad5
3805  wk_ad(i, k) = 0.0
3806  CALL poprealarray(aa(i, k))
3807  temp2 = dz2(i, k-1) + dz2(i, k)
3808  temp_ad6 = t1g*aa_ad(i, k)/temp2
3809  temp_ad7 = -(pe2(i, k)*temp_ad6/temp2)
3810  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad6
3811  dz2_ad(i, k-1) = dz2_ad(i, k-1) + temp_ad7
3812  dz2_ad(i, k) = dz2_ad(i, k) + temp_ad7
3813  aa_ad(i, k) = 0.0
3814  END DO
3815  END DO
3816  DO k=km+1,1,-1
3817  DO i=ie,is,-1
3818  pe2_ad(i, k) = pe2_ad(i, k) + pp_ad(i, k)
3819  pem_ad(i, k) = pem_ad(i, k) - pp_ad(i, k)
3820  pp_ad(i, k) = 0.0
3821  END DO
3822  END DO
3823  DO k=2,km,1
3824  DO i=ie,is,-1
3825  CALL poprealarray(pe2(i, k))
3826  gam_ad(i, k) = gam_ad(i, k) - pe2(i, k+1)*pe2_ad(i, k)
3827  pe2_ad(i, k+1) = pe2_ad(i, k+1) - gam(i, k)*pe2_ad(i, k)
3828  END DO
3829  END DO
3830  dd_ad = 0.0
3831  DO k=km,2,-1
3832  DO i=ie,is,-1
3833  CALL poprealarray(pe2(i, k+1))
3834  temp_ad3 = pe2_ad(i, k+1)/bet(i)
3835  dd_ad(i, k) = dd_ad(i, k) + temp_ad3
3836  pe2_ad(i, k) = pe2_ad(i, k) - temp_ad3
3837  bet_ad(i) = bet_ad(i) - (dd(i, k)-pe2(i, k))*temp_ad3/bet(i)
3838  pe2_ad(i, k+1) = 0.0
3839  CALL poprealarray(bet(i))
3840  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
3841  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
3842  temp_ad4 = gam_ad(i, k)/bet(i)
3843  bet_ad(i) = -(g_rat(i, k-1)*temp_ad4/bet(i))
3844  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad4
3845  gam_ad(i, k) = 0.0
3846  END DO
3847  END DO
3848  DO i=ie,is,-1
3849  CALL poprealarray(dd(i, km))
3850  aa_ad(i, km) = aa_ad(i, km) + 3.*dd_ad(i, km)
3851  dm_ad(i, km) = dm_ad(i, km) + r2g*dd_ad(i, km)
3852  dd_ad(i, km) = 0.0
3853  bb_ad(i, km) = 0.0
3854  CALL poprealarray(pe2(i, 2))
3855  temp_ad2 = pe2_ad(i, 2)/bet(i)
3856  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad2
3857  bet_ad(i) = bet_ad(i) - (dd(i, 1)-pem(i, 1))*temp_ad2/bet(i)
3858  pe2_ad(i, 2) = 0.0
3859  pem_ad(i, 1) = pem_ad(i, 1) + pe2_ad(i, 1) - temp_ad2
3860  CALL poprealarray(pe2(i, 1))
3861  pe2_ad(i, 1) = 0.0
3862  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
3863  bet_ad(i) = 0.0
3864  END DO
3865  DO k=km-1,1,-1
3866  DO i=ie,is,-1
3867  temp_ad0 = 3.*dd_ad(i, k)
3868  aa_ad(i, k) = aa_ad(i, k) + temp_ad0
3869  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + aa(i, k+1)*&
3870 & temp_ad0
3871  aa_ad(i, k+1) = aa_ad(i, k+1) + g_rat(i, k)*temp_ad0
3872  dd_ad(i, k) = 0.0
3873  bb_ad(i, k) = 0.0
3874  temp_ad1 = g_rat_ad(i, k)/dm(i, k+1)
3875  dm_ad(i, k) = dm_ad(i, k) + temp_ad1
3876  dm_ad(i, k+1) = dm_ad(i, k+1) - dm(i, k)*temp_ad1/dm(i, k+1)
3877  g_rat_ad(i, k) = 0.0
3878  END DO
3879  END DO
3880  DO k=km,1,-1
3881  DO i=ie,is,-1
3882  temp1 = dz2(i, k)
3883  temp0 = dm(i, k)*pt2(i, k)
3884  temp = temp0/temp1
3885  temp_ad = gama*exp(gama*log(-(rgas*temp)))*aa_ad(i, k)/(temp*&
3886 & temp1)
3887  dm_ad(i, k) = dm_ad(i, k) + pt2(i, k)*temp_ad
3888  pt2_ad(i, k) = pt2_ad(i, k) + dm(i, k)*temp_ad
3889  dz2_ad(i, k) = dz2_ad(i, k) - temp*temp_ad
3890  aa_ad(i, k) = 0.0
3891  w2_ad(i, k) = w2_ad(i, k) + w1_ad(i, k)
3892  w1_ad(i, k) = 0.0
3893  END DO
3894  END DO
3895  END SUBROUTINE sim3_solver_bwd
3896  SUBROUTINE sim3_solver(dt, is, ie, km, rgas, gama, kappa, pe2, dm, pem&
3897 & , w2, dz2, pt2, ws, alpha, p_fac, scale_m)
3898  IMPLICIT NONE
3899  INTEGER, INTENT(IN) :: is, ie, km
3900  REAL, INTENT(IN) :: dt, rgas, gama, kappa, alpha, p_fac, scale_m
3901  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
3902  REAL, INTENT(IN) :: ws(is:ie)
3903  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
3904  REAL, INTENT(OUT) :: pe2(is:ie, km+1)
3905  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
3906 ! Local
3907  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
3908  REAL, DIMENSION(is:ie, km+1) :: pp
3909  REAL, DIMENSION(is:ie) :: p1, wk1, bet
3910  REAL :: beta, t2, t1g, rdt, ra, capa1, r2g, r6g
3911  INTEGER :: i, k
3912  INTRINSIC log
3913  INTRINSIC exp
3914  INTRINSIC max
3915  beta = 1. - alpha
3916  ra = 1./alpha
3917  t2 = beta/alpha
3918  t1g = gama*2.*(alpha*dt)**2
3919  rdt = 1./dt
3920  capa1 = kappa - 1.
3921  r2g = grav/2.
3922  r6g = grav/6.
3923  DO k=1,km
3924  DO i=is,ie
3925  w1(i, k) = w2(i, k)
3926 ! Full pressure at center
3927  aa(i, k) = exp(gama*log(-(dm(i, k)/dz2(i, k)*rgas*pt2(i, k))))
3928  END DO
3929  END DO
3930  DO k=1,km-1
3931  DO i=is,ie
3932 ! for profile reconstruction
3933  g_rat(i, k) = dm(i, k)/dm(i, k+1)
3934  bb(i, k) = 2.*(1.+g_rat(i, k))
3935  dd(i, k) = 3.*(aa(i, k)+g_rat(i, k)*aa(i, k+1))
3936  END DO
3937  END DO
3938 ! pe2 is full p at edges
3939  DO i=is,ie
3940 ! Top:
3941  bet(i) = bb(i, 1)
3942  pe2(i, 1) = pem(i, 1)
3943  pe2(i, 2) = (dd(i, 1)-pem(i, 1))/bet(i)
3944 ! Bottom:
3945  bb(i, km) = 2.
3946  dd(i, km) = 3.*aa(i, km) + r2g*dm(i, km)
3947  END DO
3948  DO k=2,km
3949  DO i=is,ie
3950  gam(i, k) = g_rat(i, k-1)/bet(i)
3951  bet(i) = bb(i, k) - gam(i, k)
3952  pe2(i, k+1) = (dd(i, k)-pe2(i, k))/bet(i)
3953  END DO
3954  END DO
3955  DO k=km,2,-1
3956  DO i=is,ie
3957  pe2(i, k) = pe2(i, k) - gam(i, k)*pe2(i, k+1)
3958  END DO
3959  END DO
3960 ! done reconstruction of full:
3961 ! pp is pert. p at edges
3962  DO k=1,km+1
3963  DO i=is,ie
3964  pp(i, k) = pe2(i, k) - pem(i, k)
3965  END DO
3966  END DO
3967  DO k=2,km
3968  DO i=is,ie
3969  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k)
3970  wk(i, k) = t2*aa(i, k)*(w1(i, k-1)-w1(i, k))
3971  aa(i, k) = aa(i, k) - scale_m*dm(i, 1)
3972  END DO
3973  END DO
3974  DO i=is,ie
3975  bet(i) = dm(i, 1) - aa(i, 2)
3976  w2(i, 1) = (dm(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))/bet(i)
3977  END DO
3978  DO k=2,km-1
3979  DO i=is,ie
3980  gam(i, k) = aa(i, k)/bet(i)
3981  bet(i) = dm(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
3982  w2(i, k) = (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))+wk(i, k+1&
3983 & )-wk(i, k)-aa(i, k)*w2(i, k-1))/bet(i)
3984  END DO
3985  END DO
3986  DO i=is,ie
3987  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
3988  gam(i, km) = aa(i, km)/bet(i)
3989  bet(i) = dm(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
3990  w2(i, km) = (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk(i, &
3991 & km)+wk1(i)*(t2*w1(i, km)-ra*ws(i))-aa(i, km)*w2(i, km-1))/bet(i)
3992  END DO
3993  DO k=km-1,1,-1
3994  DO i=is,ie
3995  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
3996  END DO
3997  END DO
3998 ! pe2 is updated perturbation p at edges
3999  DO i=is,ie
4000  pe2(i, 1) = 0.
4001  END DO
4002  DO k=1,km
4003  DO i=is,ie
4004  pe2(i, k+1) = pe2(i, k) + (dm(i, k)*(w2(i, k)-w1(i, k))*rdt-beta&
4005 & *(pp(i, k+1)-pp(i, k)))*ra
4006  END DO
4007  END DO
4008 ! Full non-hydro pressure at edges:
4009  DO i=is,ie
4010  pe2(i, 1) = pem(i, 1)
4011  END DO
4012  DO k=2,km+1
4013  DO i=is,ie
4014  IF (p_fac*pem(i, k) .LT. pe2(i, k) + pem(i, k)) THEN
4015  pe2(i, k) = pe2(i, k) + pem(i, k)
4016  ELSE
4017  pe2(i, k) = p_fac*pem(i, k)
4018  END IF
4019  END DO
4020  END DO
4021  DO i=is,ie
4022 ! Recover cell-averaged pressure
4023  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3 - r6g*dm(i, km)
4024  dz2(i, km) = -(dm(i, km)*rgas*pt2(i, km)*exp(capa1*log(p1(i))))
4025  END DO
4026  DO k=km-1,1,-1
4027  DO i=is,ie
4028  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
4029 & *r3 - g_rat(i, k)*p1(i)
4030  dz2(i, k) = -(dm(i, k)*rgas*pt2(i, k)*exp(capa1*log(p1(i))))
4031  END DO
4032  END DO
4033  DO k=1,km+1
4034  DO i=is,ie
4035  pe2(i, k) = pe2(i, k) - pem(i, k)
4036  pe2(i, k) = pe2(i, k) + beta*(pp(i, k)-pe2(i, k))
4037  END DO
4038  END DO
4039  END SUBROUTINE sim3_solver
4040 ! Differentiation of sim3p0_solver in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edg
4041 !e_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_co
4042 !re_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_mo
4043 !d.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayle
4044 !igh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_
4045 !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_mod
4046 !.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_2
4047 !d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limite
4048 !rs 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 f
4049 !v_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_sub
4050 !grid_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_uti
4051 !ls_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
4052 !_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_
4053 !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_mod
4054 !.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
4055 !_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_co
4056 !re_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_ut
4057 !ils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
4058 ! gradient of useful results: ws dm pe2 dz2 w2 pem pt2
4059 ! with respect to varying inputs: ws dm pe2 dz2 w2 pem pt2
4060  SUBROUTINE sim3p0_solver_fwd(dt, is, ie, km, rgas, gama, kappa, pe2, &
4061 & dm, pem, w2, dz2, pt2, ws, p_fac, scale_m)
4062  IMPLICIT NONE
4063 ! Sa SIM3, but for beta==0
4064  INTEGER, INTENT(IN) :: is, ie, km
4065  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, scale_m
4066  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
4067  REAL, INTENT(IN) :: ws(is:ie)
4068  REAL, INTENT(IN) :: pem(is:ie, km+1)
4069  REAL :: pe2(is:ie, km+1)
4070  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
4071 ! Local
4072  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
4073  REAL, DIMENSION(is:ie, km+1) :: pp
4074  REAL, DIMENSION(is:ie) :: p1, wk1, bet
4075  REAL :: t1g, rdt, capa1, r2g, r6g
4076  INTEGER :: i, k
4077  INTRINSIC log
4078  INTRINSIC exp
4079  INTRINSIC max
4080 
4081  aa = 0.0
4082  bb = 0.0
4083  dd = 0.0
4084  w1 = 0.0
4085  g_rat = 0.0
4086  gam = 0.0
4087  pp = 0.0
4088  p1 = 0.0
4089  wk1 = 0.0
4090  bet = 0.0
4091  t1g = 0.0
4092  rdt = 0.0
4093  capa1 = 0.0
4094  r2g = 0.0
4095  r6g = 0.0
4096 
4097  t1g = 2.*gama*dt**2
4098  rdt = 1./dt
4099  capa1 = kappa - 1.
4100  r2g = grav/2.
4101  r6g = grav/6.
4102  DO k=1,km
4103  DO i=is,ie
4104  w1(i, k) = w2(i, k)
4105 ! Full pressure at center
4106  aa(i, k) = exp(gama*log(-(dm(i, k)/dz2(i, k)*rgas*pt2(i, k))))
4107  END DO
4108  END DO
4109  DO k=1,km-1
4110  DO i=is,ie
4111 ! for profile reconstruction
4112  g_rat(i, k) = dm(i, k)/dm(i, k+1)
4113  bb(i, k) = 2.*(1.+g_rat(i, k))
4114  dd(i, k) = 3.*(aa(i, k)+g_rat(i, k)*aa(i, k+1))
4115  END DO
4116  END DO
4117 ! pe2 is full p at edges
4118  DO i=is,ie
4119 ! Top:
4120  bet(i) = bb(i, 1)
4121  CALL pushrealarray(pe2(i, 1))
4122  pe2(i, 1) = pem(i, 1)
4123  CALL pushrealarray(pe2(i, 2))
4124  pe2(i, 2) = (dd(i, 1)-pem(i, 1))/bet(i)
4125 ! Bottom:
4126  bb(i, km) = 2.
4127  CALL pushrealarray(dd(i, km))
4128  dd(i, km) = 3.*aa(i, km) + r2g*dm(i, km)
4129  END DO
4130  DO k=2,km
4131  DO i=is,ie
4132  gam(i, k) = g_rat(i, k-1)/bet(i)
4133  CALL pushrealarray(bet(i))
4134  bet(i) = bb(i, k) - gam(i, k)
4135  CALL pushrealarray(pe2(i, k+1))
4136  pe2(i, k+1) = (dd(i, k)-pe2(i, k))/bet(i)
4137  END DO
4138  END DO
4139  DO k=km,2,-1
4140  DO i=is,ie
4141  CALL pushrealarray(pe2(i, k))
4142  pe2(i, k) = pe2(i, k) - gam(i, k)*pe2(i, k+1)
4143  END DO
4144  END DO
4145 ! done reconstruction of full:
4146 ! pp is pert. p at edges
4147  DO k=1,km+1
4148  DO i=is,ie
4149  pp(i, k) = pe2(i, k) - pem(i, k)
4150  END DO
4151  END DO
4152  DO k=2,km
4153  DO i=is,ie
4154  CALL pushrealarray(aa(i, k))
4155  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k) - scale_m*dm(i&
4156 & , 1)
4157  END DO
4158  END DO
4159  DO i=is,ie
4160  CALL pushrealarray(bet(i))
4161  bet(i) = dm(i, 1) - aa(i, 2)
4162  CALL pushrealarray(w2(i, 1))
4163  w2(i, 1) = (dm(i, 1)*w1(i, 1)+dt*pp(i, 2))/bet(i)
4164  END DO
4165  DO k=2,km-1
4166  DO i=is,ie
4167  CALL pushrealarray(gam(i, k))
4168  gam(i, k) = aa(i, k)/bet(i)
4169  CALL pushrealarray(bet(i))
4170  bet(i) = dm(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
4171  CALL pushrealarray(w2(i, k))
4172  w2(i, k) = (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))-aa(i, k)*&
4173 & w2(i, k-1))/bet(i)
4174  END DO
4175  END DO
4176  DO i=is,ie
4177  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
4178  CALL pushrealarray(gam(i, km))
4179  gam(i, km) = aa(i, km)/bet(i)
4180  CALL pushrealarray(bet(i))
4181  bet(i) = dm(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
4182  CALL pushrealarray(w2(i, km))
4183  w2(i, km) = (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk1(i)&
4184 & *ws(i)-aa(i, km)*w2(i, km-1))/bet(i)
4185  END DO
4186  DO k=km-1,1,-1
4187  DO i=is,ie
4188  CALL pushrealarray(w2(i, k))
4189  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
4190  END DO
4191  END DO
4192 ! pe2 is updated perturbation p at edges
4193  DO i=is,ie
4194  CALL pushrealarray(pe2(i, 1))
4195  pe2(i, 1) = 0.
4196  END DO
4197  DO k=1,km
4198  DO i=is,ie
4199  CALL pushrealarray(pe2(i, k+1))
4200  pe2(i, k+1) = pe2(i, k) + dm(i, k)*(w2(i, k)-w1(i, k))*rdt
4201  END DO
4202  END DO
4203 ! Full non-hydro pressure at edges:
4204  DO i=is,ie
4205  CALL pushrealarray(pe2(i, 1))
4206  pe2(i, 1) = pem(i, 1)
4207  END DO
4208  DO k=2,km+1
4209  DO i=is,ie
4210  IF (p_fac*pem(i, k) .LT. pe2(i, k) + pem(i, k)) THEN
4211  CALL pushrealarray(pe2(i, k))
4212  pe2(i, k) = pe2(i, k) + pem(i, k)
4213  CALL pushcontrol(1,0)
4214  ELSE
4215  CALL pushrealarray(pe2(i, k))
4216  pe2(i, k) = p_fac*pem(i, k)
4217  CALL pushcontrol(1,1)
4218  END IF
4219  END DO
4220  END DO
4221  DO i=is,ie
4222 ! Recover cell-averaged pressure
4223  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3 - r6g*dm(i, km)
4224  CALL pushrealarray(dz2(i, km))
4225  dz2(i, km) = -(dm(i, km)*rgas*pt2(i, km)*exp(capa1*log(p1(i))))
4226  END DO
4227  DO k=km-1,1,-1
4228  DO i=is,ie
4229  CALL pushrealarray(p1(i))
4230  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
4231 & *r3 - g_rat(i, k)*p1(i)
4232  CALL pushrealarray(dz2(i, k))
4233  dz2(i, k) = -(dm(i, k)*rgas*pt2(i, k)*exp(capa1*log(p1(i))))
4234  END DO
4235  END DO
4236  DO k=1,km+1
4237  DO i=is,ie
4238  CALL pushrealarray(pe2(i, k))
4239  pe2(i, k) = pe2(i, k) - pem(i, k)
4240  END DO
4241  END DO
4242  CALL pushrealarray(gam, (ie-is+1)*km)
4243  CALL pushrealarray(t1g)
4244  CALL pushrealarray(capa1)
4245  CALL pushrealarray(g_rat, (ie-is+1)*km)
4246  CALL pushrealarray(pp, (ie-is+1)*(km+1))
4247  CALL pushrealarray(rdt)
4248  CALL pushrealarray(w1, (ie-is+1)*km)
4249  CALL pushrealarray(bb, (ie-is+1)*km)
4250  CALL pushrealarray(r6g)
4251  CALL pushrealarray(wk1, ie - is + 1)
4252  CALL pushrealarray(bet, ie - is + 1)
4253  CALL pushrealarray(p1, ie - is + 1)
4254  CALL pushrealarray(r2g)
4255  CALL pushrealarray(aa, (ie-is+1)*km)
4256  CALL pushrealarray(dd, (ie-is+1)*km)
4257  END SUBROUTINE sim3p0_solver_fwd
4258 ! Differentiation of sim3p0_solver in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_ed
4259 !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
4260 !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
4261 !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
4262 !eigh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l
4263 !_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
4264 !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_
4265 !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
4266 !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
4267 !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
4268 !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
4269 !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
4270 !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
4271 !_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
4272 !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
4273 !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
4274 !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
4275 !tils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
4276 ! gradient of useful results: ws dm pe2 dz2 w2 pem pt2
4277 ! with respect to varying inputs: ws dm pe2 dz2 w2 pem pt2
4278  SUBROUTINE sim3p0_solver_bwd(dt, is, ie, km, rgas, gama, kappa, pe2, &
4279 & pe2_ad, dm, dm_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad&
4280 & , ws, ws_ad, p_fac, scale_m)
4281  IMPLICIT NONE
4282  INTEGER, INTENT(IN) :: is, ie, km
4283  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, scale_m
4284  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
4285  REAL, DIMENSION(is:ie, km) :: dm_ad, pt2_ad
4286  REAL, INTENT(IN) :: ws(is:ie)
4287  REAL :: ws_ad(is:ie)
4288  REAL, INTENT(IN) :: pem(is:ie, km+1)
4289  REAL :: pem_ad(is:ie, km+1)
4290  REAL :: pe2(is:ie, km+1)
4291  REAL :: pe2_ad(is:ie, km+1)
4292  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
4293  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2_ad, w2_ad
4294  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
4295  REAL, DIMENSION(is:ie, km) :: aa_ad, bb_ad, dd_ad, w1_ad, g_rat_ad, &
4296 & gam_ad
4297  REAL, DIMENSION(is:ie, km+1) :: pp
4298  REAL, DIMENSION(is:ie, km+1) :: pp_ad
4299  REAL, DIMENSION(is:ie) :: p1, wk1, bet
4300  REAL, DIMENSION(is:ie) :: p1_ad, wk1_ad, bet_ad
4301  REAL :: t1g, rdt, capa1, r2g, r6g
4302  INTEGER :: i, k
4303  INTRINSIC log
4304  INTRINSIC exp
4305  INTRINSIC max
4306  REAL :: temp
4307  REAL :: temp0
4308  REAL :: temp1
4309  REAL :: temp_ad
4310  REAL :: temp_ad0
4311  REAL :: temp_ad1
4312  REAL :: temp_ad2
4313  REAL :: temp_ad3
4314  REAL :: temp_ad4
4315  REAL :: temp2
4316  REAL :: temp_ad5
4317  REAL :: temp_ad6
4318  REAL :: temp_ad7
4319  REAL :: temp_ad8
4320  REAL :: temp_ad9
4321  REAL :: temp_ad10
4322  REAL :: temp_ad11
4323  REAL :: temp_ad12
4324  REAL :: temp_ad13
4325  REAL :: temp3
4326  REAL :: temp_ad14
4327  REAL :: temp4
4328  REAL :: temp_ad15
4329  REAL :: temp_ad16
4330  INTEGER :: branch
4331 
4332  aa = 0.0
4333  bb = 0.0
4334  dd = 0.0
4335  w1 = 0.0
4336  g_rat = 0.0
4337  gam = 0.0
4338  pp = 0.0
4339  p1 = 0.0
4340  wk1 = 0.0
4341  bet = 0.0
4342  t1g = 0.0
4343  rdt = 0.0
4344  capa1 = 0.0
4345  r2g = 0.0
4346  r6g = 0.0
4347  branch = 0
4348 
4349  CALL poprealarray(dd, (ie-is+1)*km)
4350  CALL poprealarray(aa, (ie-is+1)*km)
4351  CALL poprealarray(r2g)
4352  CALL poprealarray(p1, ie - is + 1)
4353  CALL poprealarray(bet, ie - is + 1)
4354  CALL poprealarray(wk1, ie - is + 1)
4355  CALL poprealarray(r6g)
4356  CALL poprealarray(bb, (ie-is+1)*km)
4357  CALL poprealarray(w1, (ie-is+1)*km)
4358  CALL poprealarray(rdt)
4359  CALL poprealarray(pp, (ie-is+1)*(km+1))
4360  CALL poprealarray(g_rat, (ie-is+1)*km)
4361  CALL poprealarray(capa1)
4362  CALL poprealarray(t1g)
4363  CALL poprealarray(gam, (ie-is+1)*km)
4364  DO k=km+1,1,-1
4365  DO i=ie,is,-1
4366  CALL poprealarray(pe2(i, k))
4367  pem_ad(i, k) = pem_ad(i, k) - pe2_ad(i, k)
4368  END DO
4369  END DO
4370  p1_ad = 0.0
4371  bb_ad = 0.0
4372  g_rat_ad = 0.0
4373  DO k=1,km-1,1
4374  DO i=ie,is,-1
4375  CALL poprealarray(dz2(i, k))
4376  temp4 = capa1*log(p1(i))
4377  temp_ad15 = -(rgas*exp(temp4)*dz2_ad(i, k))
4378  dm_ad(i, k) = dm_ad(i, k) + pt2(i, k)*temp_ad15
4379  pt2_ad(i, k) = pt2_ad(i, k) + dm(i, k)*temp_ad15
4380  p1_ad(i) = p1_ad(i) - capa1*exp(temp4)*dm(i, k)*pt2(i, k)*rgas*&
4381 & dz2_ad(i, k)/p1(i)
4382  dz2_ad(i, k) = 0.0
4383  CALL poprealarray(p1(i))
4384  temp_ad16 = r3*p1_ad(i)
4385  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad16
4386  bb_ad(i, k) = bb_ad(i, k) + pe2(i, k+1)*temp_ad16
4387  pe2_ad(i, k+1) = pe2_ad(i, k+1) + bb(i, k)*temp_ad16
4388  g_rat_ad(i, k) = g_rat_ad(i, k) + pe2(i, k+2)*temp_ad16 - p1(i)*&
4389 & p1_ad(i)
4390  pe2_ad(i, k+2) = pe2_ad(i, k+2) + g_rat(i, k)*temp_ad16
4391  p1_ad(i) = -(g_rat(i, k)*p1_ad(i))
4392  END DO
4393  END DO
4394  DO i=ie,is,-1
4395  CALL poprealarray(dz2(i, km))
4396  temp3 = capa1*log(p1(i))
4397  temp_ad14 = -(rgas*exp(temp3)*dz2_ad(i, km))
4398  pt2_ad(i, km) = pt2_ad(i, km) + dm(i, km)*temp_ad14
4399  p1_ad(i) = p1_ad(i) - capa1*exp(temp3)*dm(i, km)*pt2(i, km)*rgas*&
4400 & dz2_ad(i, km)/p1(i)
4401  dm_ad(i, km) = dm_ad(i, km) + pt2(i, km)*temp_ad14 - r6g*p1_ad(i)
4402  dz2_ad(i, km) = 0.0
4403  pe2_ad(i, km) = pe2_ad(i, km) + r3*p1_ad(i)
4404  pe2_ad(i, km+1) = pe2_ad(i, km+1) + r3*2.*p1_ad(i)
4405  p1_ad(i) = 0.0
4406  END DO
4407  DO k=km+1,2,-1
4408  DO i=ie,is,-1
4409  CALL popcontrol(1,branch)
4410  IF (branch .EQ. 0) THEN
4411  CALL poprealarray(pe2(i, k))
4412  pem_ad(i, k) = pem_ad(i, k) + pe2_ad(i, k)
4413  ELSE
4414  CALL poprealarray(pe2(i, k))
4415  pem_ad(i, k) = pem_ad(i, k) + p_fac*pe2_ad(i, k)
4416  pe2_ad(i, k) = 0.0
4417  END IF
4418  END DO
4419  END DO
4420  DO i=ie,is,-1
4421  CALL poprealarray(pe2(i, 1))
4422  pem_ad(i, 1) = pem_ad(i, 1) + pe2_ad(i, 1)
4423  pe2_ad(i, 1) = 0.0
4424  END DO
4425  w1_ad = 0.0
4426  DO k=km,1,-1
4427  DO i=ie,is,-1
4428  CALL poprealarray(pe2(i, k+1))
4429  temp_ad13 = rdt*dm(i, k)*pe2_ad(i, k+1)
4430  pe2_ad(i, k) = pe2_ad(i, k) + pe2_ad(i, k+1)
4431  dm_ad(i, k) = dm_ad(i, k) + rdt*(w2(i, k)-w1(i, k))*pe2_ad(i, k+&
4432 & 1)
4433  w2_ad(i, k) = w2_ad(i, k) + temp_ad13
4434  w1_ad(i, k) = w1_ad(i, k) - temp_ad13
4435  pe2_ad(i, k+1) = 0.0
4436  END DO
4437  END DO
4438  DO i=ie,is,-1
4439  CALL poprealarray(pe2(i, 1))
4440  pe2_ad(i, 1) = 0.0
4441  END DO
4442  gam_ad = 0.0
4443  DO k=1,km-1,1
4444  DO i=ie,is,-1
4445  CALL poprealarray(w2(i, k))
4446  gam_ad(i, k+1) = gam_ad(i, k+1) - w2(i, k+1)*w2_ad(i, k)
4447  w2_ad(i, k+1) = w2_ad(i, k+1) - gam(i, k+1)*w2_ad(i, k)
4448  END DO
4449  END DO
4450  aa_ad = 0.0
4451  bet_ad = 0.0
4452  wk1_ad = 0.0
4453  pp_ad = 0.0
4454  DO i=ie,is,-1
4455  CALL poprealarray(w2(i, km))
4456  temp_ad10 = w2_ad(i, km)/bet(i)
4457  w1_ad(i, km) = w1_ad(i, km) + dm(i, km)*temp_ad10
4458  pp_ad(i, km+1) = pp_ad(i, km+1) + dt*temp_ad10
4459  pp_ad(i, km) = pp_ad(i, km) - dt*temp_ad10
4460  ws_ad(i) = ws_ad(i) - wk1(i)*temp_ad10
4461  w2_ad(i, km-1) = w2_ad(i, km-1) - aa(i, km)*temp_ad10
4462  bet_ad(i) = bet_ad(i) - (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i&
4463 & , km))-wk1(i)*ws(i)-aa(i, km)*w2(i, km-1))*temp_ad10/bet(i)
4464  dm_ad(i, km) = dm_ad(i, km) + bet_ad(i) + w1(i, km)*temp_ad10
4465  wk1_ad(i) = wk1_ad(i) - bet_ad(i) - ws(i)*temp_ad10
4466  w2_ad(i, km) = 0.0
4467  CALL poprealarray(bet(i))
4468  gam_ad(i, km) = gam_ad(i, km) - aa(i, km)*bet_ad(i)
4469  temp_ad11 = gam_ad(i, km)/bet(i)
4470  aa_ad(i, km) = aa_ad(i, km) + ((-1.0)-gam(i, km))*bet_ad(i) + &
4471 & temp_ad11 - w2(i, km-1)*temp_ad10
4472  bet_ad(i) = -(aa(i, km)*temp_ad11/bet(i))
4473  CALL poprealarray(gam(i, km))
4474  gam_ad(i, km) = 0.0
4475  temp_ad12 = t1g*wk1_ad(i)/dz2(i, km)
4476  pe2_ad(i, km+1) = pe2_ad(i, km+1) + temp_ad12
4477  dz2_ad(i, km) = dz2_ad(i, km) - pe2(i, km+1)*temp_ad12/dz2(i, km)
4478  wk1_ad(i) = 0.0
4479  END DO
4480  DO k=km-1,2,-1
4481  DO i=ie,is,-1
4482  CALL poprealarray(w2(i, k))
4483  temp_ad8 = w2_ad(i, k)/bet(i)
4484  w1_ad(i, k) = w1_ad(i, k) + dm(i, k)*temp_ad8
4485  pp_ad(i, k+1) = pp_ad(i, k+1) + dt*temp_ad8
4486  pp_ad(i, k) = pp_ad(i, k) - dt*temp_ad8
4487  w2_ad(i, k-1) = w2_ad(i, k-1) - aa(i, k)*temp_ad8
4488  bet_ad(i) = bet_ad(i) - (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, &
4489 & k))-aa(i, k)*w2(i, k-1))*temp_ad8/bet(i)
4490  dm_ad(i, k) = dm_ad(i, k) + bet_ad(i) + w1(i, k)*temp_ad8
4491  aa_ad(i, k) = aa_ad(i, k) + ((-1.0)-gam(i, k))*bet_ad(i) - w2(i&
4492 & , k-1)*temp_ad8
4493  w2_ad(i, k) = 0.0
4494  CALL poprealarray(bet(i))
4495  aa_ad(i, k+1) = aa_ad(i, k+1) - bet_ad(i)
4496  gam_ad(i, k) = gam_ad(i, k) - aa(i, k)*bet_ad(i)
4497  CALL poprealarray(gam(i, k))
4498  temp_ad9 = gam_ad(i, k)/bet(i)
4499  bet_ad(i) = -(aa(i, k)*temp_ad9/bet(i))
4500  aa_ad(i, k) = aa_ad(i, k) + temp_ad9
4501  gam_ad(i, k) = 0.0
4502  END DO
4503  END DO
4504  DO i=ie,is,-1
4505  CALL poprealarray(w2(i, 1))
4506  temp_ad7 = w2_ad(i, 1)/bet(i)
4507  w1_ad(i, 1) = w1_ad(i, 1) + dm(i, 1)*temp_ad7
4508  pp_ad(i, 2) = pp_ad(i, 2) + dt*temp_ad7
4509  bet_ad(i) = bet_ad(i) - (dm(i, 1)*w1(i, 1)+dt*pp(i, 2))*temp_ad7/&
4510 & bet(i)
4511  dm_ad(i, 1) = dm_ad(i, 1) + bet_ad(i) + w1(i, 1)*temp_ad7
4512  w2_ad(i, 1) = 0.0
4513  CALL poprealarray(bet(i))
4514  aa_ad(i, 2) = aa_ad(i, 2) - bet_ad(i)
4515  bet_ad(i) = 0.0
4516  END DO
4517  DO k=km,2,-1
4518  DO i=ie,is,-1
4519  CALL poprealarray(aa(i, k))
4520  temp2 = dz2(i, k-1) + dz2(i, k)
4521  temp_ad5 = t1g*aa_ad(i, k)/temp2
4522  temp_ad6 = -(pe2(i, k)*temp_ad5/temp2)
4523  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad5
4524  dz2_ad(i, k-1) = dz2_ad(i, k-1) + temp_ad6
4525  dz2_ad(i, k) = dz2_ad(i, k) + temp_ad6
4526  dm_ad(i, 1) = dm_ad(i, 1) - scale_m*aa_ad(i, k)
4527  aa_ad(i, k) = 0.0
4528  END DO
4529  END DO
4530  DO k=km+1,1,-1
4531  DO i=ie,is,-1
4532  pe2_ad(i, k) = pe2_ad(i, k) + pp_ad(i, k)
4533  pem_ad(i, k) = pem_ad(i, k) - pp_ad(i, k)
4534  pp_ad(i, k) = 0.0
4535  END DO
4536  END DO
4537  DO k=2,km,1
4538  DO i=ie,is,-1
4539  CALL poprealarray(pe2(i, k))
4540  gam_ad(i, k) = gam_ad(i, k) - pe2(i, k+1)*pe2_ad(i, k)
4541  pe2_ad(i, k+1) = pe2_ad(i, k+1) - gam(i, k)*pe2_ad(i, k)
4542  END DO
4543  END DO
4544  dd_ad = 0.0
4545  DO k=km,2,-1
4546  DO i=ie,is,-1
4547  CALL poprealarray(pe2(i, k+1))
4548  temp_ad3 = pe2_ad(i, k+1)/bet(i)
4549  dd_ad(i, k) = dd_ad(i, k) + temp_ad3
4550  pe2_ad(i, k) = pe2_ad(i, k) - temp_ad3
4551  bet_ad(i) = bet_ad(i) - (dd(i, k)-pe2(i, k))*temp_ad3/bet(i)
4552  pe2_ad(i, k+1) = 0.0
4553  CALL poprealarray(bet(i))
4554  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
4555  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
4556  temp_ad4 = gam_ad(i, k)/bet(i)
4557  bet_ad(i) = -(g_rat(i, k-1)*temp_ad4/bet(i))
4558  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad4
4559  gam_ad(i, k) = 0.0
4560  END DO
4561  END DO
4562  DO i=ie,is,-1
4563  CALL poprealarray(dd(i, km))
4564  aa_ad(i, km) = aa_ad(i, km) + 3.*dd_ad(i, km)
4565  dm_ad(i, km) = dm_ad(i, km) + r2g*dd_ad(i, km)
4566  dd_ad(i, km) = 0.0
4567  bb_ad(i, km) = 0.0
4568  CALL poprealarray(pe2(i, 2))
4569  temp_ad2 = pe2_ad(i, 2)/bet(i)
4570  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad2
4571  bet_ad(i) = bet_ad(i) - (dd(i, 1)-pem(i, 1))*temp_ad2/bet(i)
4572  pe2_ad(i, 2) = 0.0
4573  pem_ad(i, 1) = pem_ad(i, 1) + pe2_ad(i, 1) - temp_ad2
4574  CALL poprealarray(pe2(i, 1))
4575  pe2_ad(i, 1) = 0.0
4576  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
4577  bet_ad(i) = 0.0
4578  END DO
4579  DO k=km-1,1,-1
4580  DO i=ie,is,-1
4581  temp_ad0 = 3.*dd_ad(i, k)
4582  aa_ad(i, k) = aa_ad(i, k) + temp_ad0
4583  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + aa(i, k+1)*&
4584 & temp_ad0
4585  aa_ad(i, k+1) = aa_ad(i, k+1) + g_rat(i, k)*temp_ad0
4586  dd_ad(i, k) = 0.0
4587  bb_ad(i, k) = 0.0
4588  temp_ad1 = g_rat_ad(i, k)/dm(i, k+1)
4589  dm_ad(i, k) = dm_ad(i, k) + temp_ad1
4590  dm_ad(i, k+1) = dm_ad(i, k+1) - dm(i, k)*temp_ad1/dm(i, k+1)
4591  g_rat_ad(i, k) = 0.0
4592  END DO
4593  END DO
4594  DO k=km,1,-1
4595  DO i=ie,is,-1
4596  temp1 = dz2(i, k)
4597  temp0 = dm(i, k)*pt2(i, k)
4598  temp = temp0/temp1
4599  temp_ad = gama*exp(gama*log(-(rgas*temp)))*aa_ad(i, k)/(temp*&
4600 & temp1)
4601  dm_ad(i, k) = dm_ad(i, k) + pt2(i, k)*temp_ad
4602  pt2_ad(i, k) = pt2_ad(i, k) + dm(i, k)*temp_ad
4603  dz2_ad(i, k) = dz2_ad(i, k) - temp*temp_ad
4604  aa_ad(i, k) = 0.0
4605  w2_ad(i, k) = w2_ad(i, k) + w1_ad(i, k)
4606  w1_ad(i, k) = 0.0
4607  END DO
4608  END DO
4609  END SUBROUTINE sim3p0_solver_bwd
4610  SUBROUTINE sim3p0_solver(dt, is, ie, km, rgas, gama, kappa, pe2, dm, &
4611 & pem, w2, dz2, pt2, ws, p_fac, scale_m)
4612  IMPLICIT NONE
4613 ! Sa SIM3, but for beta==0
4614  INTEGER, INTENT(IN) :: is, ie, km
4615  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, scale_m
4616  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm, pt2
4617  REAL, INTENT(IN) :: ws(is:ie)
4618  REAL, INTENT(IN) :: pem(is:ie, km+1)
4619  REAL, INTENT(OUT) :: pe2(is:ie, km+1)
4620  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
4621 ! Local
4622  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
4623  REAL, DIMENSION(is:ie, km+1) :: pp
4624  REAL, DIMENSION(is:ie) :: p1, wk1, bet
4625  REAL :: t1g, rdt, capa1, r2g, r6g
4626  INTEGER :: i, k
4627  INTRINSIC log
4628  INTRINSIC exp
4629  INTRINSIC max
4630  t1g = 2.*gama*dt**2
4631  rdt = 1./dt
4632  capa1 = kappa - 1.
4633  r2g = grav/2.
4634  r6g = grav/6.
4635  DO k=1,km
4636  DO i=is,ie
4637  w1(i, k) = w2(i, k)
4638 ! Full pressure at center
4639  aa(i, k) = exp(gama*log(-(dm(i, k)/dz2(i, k)*rgas*pt2(i, k))))
4640  END DO
4641  END DO
4642  DO k=1,km-1
4643  DO i=is,ie
4644 ! for profile reconstruction
4645  g_rat(i, k) = dm(i, k)/dm(i, k+1)
4646  bb(i, k) = 2.*(1.+g_rat(i, k))
4647  dd(i, k) = 3.*(aa(i, k)+g_rat(i, k)*aa(i, k+1))
4648  END DO
4649  END DO
4650 ! pe2 is full p at edges
4651  DO i=is,ie
4652 ! Top:
4653  bet(i) = bb(i, 1)
4654  pe2(i, 1) = pem(i, 1)
4655  pe2(i, 2) = (dd(i, 1)-pem(i, 1))/bet(i)
4656 ! Bottom:
4657  bb(i, km) = 2.
4658  dd(i, km) = 3.*aa(i, km) + r2g*dm(i, km)
4659  END DO
4660  DO k=2,km
4661  DO i=is,ie
4662  gam(i, k) = g_rat(i, k-1)/bet(i)
4663  bet(i) = bb(i, k) - gam(i, k)
4664  pe2(i, k+1) = (dd(i, k)-pe2(i, k))/bet(i)
4665  END DO
4666  END DO
4667  DO k=km,2,-1
4668  DO i=is,ie
4669  pe2(i, k) = pe2(i, k) - gam(i, k)*pe2(i, k+1)
4670  END DO
4671  END DO
4672 ! done reconstruction of full:
4673 ! pp is pert. p at edges
4674  DO k=1,km+1
4675  DO i=is,ie
4676  pp(i, k) = pe2(i, k) - pem(i, k)
4677  END DO
4678  END DO
4679  DO k=2,km
4680  DO i=is,ie
4681  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k) - scale_m*dm(i&
4682 & , 1)
4683  END DO
4684  END DO
4685  DO i=is,ie
4686  bet(i) = dm(i, 1) - aa(i, 2)
4687  w2(i, 1) = (dm(i, 1)*w1(i, 1)+dt*pp(i, 2))/bet(i)
4688  END DO
4689  DO k=2,km-1
4690  DO i=is,ie
4691  gam(i, k) = aa(i, k)/bet(i)
4692  bet(i) = dm(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
4693  w2(i, k) = (dm(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))-aa(i, k)*&
4694 & w2(i, k-1))/bet(i)
4695  END DO
4696  END DO
4697  DO i=is,ie
4698  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
4699  gam(i, km) = aa(i, km)/bet(i)
4700  bet(i) = dm(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
4701  w2(i, km) = (dm(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk1(i)&
4702 & *ws(i)-aa(i, km)*w2(i, km-1))/bet(i)
4703  END DO
4704  DO k=km-1,1,-1
4705  DO i=is,ie
4706  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
4707  END DO
4708  END DO
4709 ! pe2 is updated perturbation p at edges
4710  DO i=is,ie
4711  pe2(i, 1) = 0.
4712  END DO
4713  DO k=1,km
4714  DO i=is,ie
4715  pe2(i, k+1) = pe2(i, k) + dm(i, k)*(w2(i, k)-w1(i, k))*rdt
4716  END DO
4717  END DO
4718 ! Full non-hydro pressure at edges:
4719  DO i=is,ie
4720  pe2(i, 1) = pem(i, 1)
4721  END DO
4722  DO k=2,km+1
4723  DO i=is,ie
4724  IF (p_fac*pem(i, k) .LT. pe2(i, k) + pem(i, k)) THEN
4725  pe2(i, k) = pe2(i, k) + pem(i, k)
4726  ELSE
4727  pe2(i, k) = p_fac*pem(i, k)
4728  END IF
4729  END DO
4730  END DO
4731  DO i=is,ie
4732 ! Recover cell-averaged pressure
4733  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3 - r6g*dm(i, km)
4734  dz2(i, km) = -(dm(i, km)*rgas*pt2(i, km)*exp(capa1*log(p1(i))))
4735  END DO
4736  DO k=km-1,1,-1
4737  DO i=is,ie
4738  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
4739 & *r3 - g_rat(i, k)*p1(i)
4740  dz2(i, k) = -(dm(i, k)*rgas*pt2(i, k)*exp(capa1*log(p1(i))))
4741  END DO
4742  END DO
4743  DO k=1,km+1
4744  DO i=is,ie
4745  pe2(i, k) = pe2(i, k) - pem(i, k)
4746  END DO
4747  END DO
4748  END SUBROUTINE sim3p0_solver
4749 ! Differentiation of sim1_solver in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
4750 !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
4751 !_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.
4752 !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
4753 !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
4754 !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
4755 !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
4756 !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
4757 ! 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_
4758 !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
4759 !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
4760 !_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
4761 !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
4762 !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
4763 !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
4764 !_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
4765 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
4766 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
4767 ! gradient of useful results: ws dm2 dz2 w2 pm2 pem pe pt2
4768 ! with respect to varying inputs: ws dm2 dz2 w2 pm2 pem pe pt2
4769  SUBROUTINE sim1_solver_fwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa&
4770 & , pe, dm2, pm2, pem, w2, dz2, pt2, ws, p_fac)
4771  IMPLICIT NONE
4772  INTEGER, INTENT(IN) :: is, ie, km
4773  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac
4774  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
4775  REAL, INTENT(IN) :: ws(is:ie)
4776  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
4777  REAL :: pe(is:ie, km+1)
4778  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
4779 ! Local
4780  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
4781  REAL, DIMENSION(is:ie, km+1) :: pp
4782  REAL, DIMENSION(is:ie) :: p1, bet
4783  REAL :: t1g, rdt, capa1
4784  INTEGER :: i, k
4785  INTRINSIC log
4786  INTRINSIC exp
4787  INTRINSIC max
4788  REAL :: max1
4789  REAL :: max2
4790 
4791  aa = 0.0
4792  bb = 0.0
4793  dd = 0.0
4794  w1 = 0.0
4795  g_rat = 0.0
4796  gam = 0.0
4797  pp = 0.0
4798  p1 = 0.0
4799  bet = 0.0
4800  t1g = 0.0
4801  rdt = 0.0
4802  capa1 = 0.0
4803  max1 = 0.0
4804  max2 = 0.0
4805 
4806  t1g = gama*2.*dt*dt
4807  rdt = 1./dt
4808  capa1 = kappa - 1.
4809  DO k=1,km
4810  DO i=is,ie
4811  w1(i, k) = w2(i, k)
4812  CALL pushrealarray(pe(i, k))
4813  pe(i, k) = exp(gama*log(-(dm2(i, k)/dz2(i, k)*rgas*pt2(i, k)))) &
4814 & - pm2(i, k)
4815  END DO
4816  END DO
4817  DO k=1,km-1
4818  DO i=is,ie
4819  g_rat(i, k) = dm2(i, k)/dm2(i, k+1)
4820  bb(i, k) = 2.*(1.+g_rat(i, k))
4821  dd(i, k) = 3.*(pe(i, k)+g_rat(i, k)*pe(i, k+1))
4822  END DO
4823  END DO
4824  DO i=is,ie
4825  bet(i) = bb(i, 1)
4826  pp(i, 1) = 0.
4827  pp(i, 2) = dd(i, 1)/bet(i)
4828  bb(i, km) = 2.
4829  CALL pushrealarray(dd(i, km))
4830  dd(i, km) = 3.*pe(i, km)
4831  END DO
4832  DO k=2,km
4833  DO i=is,ie
4834  gam(i, k) = g_rat(i, k-1)/bet(i)
4835  CALL pushrealarray(bet(i))
4836  bet(i) = bb(i, k) - gam(i, k)
4837  CALL pushrealarray(pp(i, k+1))
4838  pp(i, k+1) = (dd(i, k)-pp(i, k))/bet(i)
4839  END DO
4840  END DO
4841  DO k=km,2,-1
4842  DO i=is,ie
4843  CALL pushrealarray(pp(i, k))
4844  pp(i, k) = pp(i, k) - gam(i, k)*pp(i, k+1)
4845  END DO
4846  END DO
4847 ! Start the w-solver
4848  DO k=2,km
4849  DO i=is,ie
4850  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*(pem(i, k)+pp(i, k))
4851  END DO
4852  END DO
4853  DO i=is,ie
4854  CALL pushrealarray(bet(i))
4855  bet(i) = dm2(i, 1) - aa(i, 2)
4856  CALL pushrealarray(w2(i, 1))
4857  w2(i, 1) = (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2))/bet(i)
4858  END DO
4859  DO k=2,km-1
4860  DO i=is,ie
4861  CALL pushrealarray(gam(i, k))
4862  gam(i, k) = aa(i, k)/bet(i)
4863  CALL pushrealarray(bet(i))
4864  bet(i) = dm2(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
4865  CALL pushrealarray(w2(i, k))
4866  w2(i, k) = (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))-aa(i, k)&
4867 & *w2(i, k-1))/bet(i)
4868  END DO
4869  END DO
4870  DO i=is,ie
4871  p1(i) = t1g/dz2(i, km)*(pem(i, km+1)+pp(i, km+1))
4872  CALL pushrealarray(gam(i, km))
4873  gam(i, km) = aa(i, km)/bet(i)
4874  CALL pushrealarray(bet(i))
4875  bet(i) = dm2(i, km) - (aa(i, km)+p1(i)+aa(i, km)*gam(i, km))
4876  CALL pushrealarray(w2(i, km))
4877  w2(i, km) = (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-p1(i)&
4878 & *ws(i)-aa(i, km)*w2(i, km-1))/bet(i)
4879  END DO
4880  DO k=km-1,1,-1
4881  DO i=is,ie
4882  CALL pushrealarray(w2(i, k))
4883  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
4884  END DO
4885  END DO
4886  DO i=is,ie
4887  CALL pushrealarray(pe(i, 1))
4888  pe(i, 1) = 0.
4889  END DO
4890  DO k=1,km
4891  DO i=is,ie
4892  CALL pushrealarray(pe(i, k+1))
4893  pe(i, k+1) = pe(i, k) + dm2(i, k)*(w2(i, k)-w1(i, k))*rdt
4894  END DO
4895  END DO
4896  DO i=is,ie
4897  CALL pushrealarray(p1(i))
4898  p1(i) = (pe(i, km)+2.*pe(i, km+1))*r3
4899  IF (p_fac*pm2(i, km) .LT. p1(i) + pm2(i, km)) THEN
4900  CALL pushrealarray(max1)
4901  max1 = p1(i) + pm2(i, km)
4902  CALL pushcontrol(1,0)
4903  ELSE
4904  CALL pushrealarray(max1)
4905  max1 = p_fac*pm2(i, km)
4906  CALL pushcontrol(1,1)
4907  END IF
4908  CALL pushrealarray(dz2(i, km))
4909  dz2(i, km) = -(dm2(i, km)*rgas*pt2(i, km)*exp(capa1*log(max1)))
4910  END DO
4911  DO k=km-1,1,-1
4912  DO i=is,ie
4913  CALL pushrealarray(p1(i))
4914  p1(i) = (pe(i, k)+bb(i, k)*pe(i, k+1)+g_rat(i, k)*pe(i, k+2))*r3&
4915 & - g_rat(i, k)*p1(i)
4916  IF (p_fac*pm2(i, k) .LT. p1(i) + pm2(i, k)) THEN
4917  CALL pushrealarray(max2)
4918  max2 = p1(i) + pm2(i, k)
4919  CALL pushcontrol(1,0)
4920  ELSE
4921  CALL pushrealarray(max2)
4922  max2 = p_fac*pm2(i, k)
4923  CALL pushcontrol(1,1)
4924  END IF
4925  CALL pushrealarray(dz2(i, k))
4926  dz2(i, k) = -(dm2(i, k)*rgas*pt2(i, k)*exp(capa1*log(max2)))
4927  END DO
4928  END DO
4929  CALL pushrealarray(gam, (ie-is+1)*km)
4930  CALL pushrealarray(t1g)
4931  CALL pushrealarray(capa1)
4932  CALL pushrealarray(g_rat, (ie-is+1)*km)
4933  CALL pushrealarray(pp, (ie-is+1)*(km+1))
4934  CALL pushrealarray(rdt)
4935  CALL pushrealarray(w1, (ie-is+1)*km)
4936  CALL pushrealarray(bb, (ie-is+1)*km)
4937  CALL pushrealarray(bet, ie - is + 1)
4938  CALL pushrealarray(p1, ie - is + 1)
4939  CALL pushrealarray(max2)
4940  CALL pushrealarray(max1)
4941  CALL pushrealarray(aa, (ie-is+1)*km)
4942  CALL pushrealarray(dd, (ie-is+1)*km)
4943  END SUBROUTINE sim1_solver_fwd
4944 ! Differentiation of sim1_solver in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
4945 !_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
4946 !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
4947 !.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
4948 !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
4949 !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.
4950 !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
4951 ! 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
4952 !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
4953 !_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
4954 !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
4955 !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_
4956 !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
4957 !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.
4958 !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_
4959 !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
4960 !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
4961 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
4962 ! gradient of useful results: ws dm2 dz2 w2 pm2 pem pe pt2
4963 ! with respect to varying inputs: ws dm2 dz2 w2 pm2 pem pe pt2
4964  SUBROUTINE sim1_solver_bwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa&
4965 & , pe, pe_ad, dm2, dm2_ad, pm2, pm2_ad, pem, pem_ad, w2, w2_ad, dz2, &
4966 & dz2_ad, pt2, pt2_ad, ws, ws_ad, p_fac)
4967  IMPLICIT NONE
4968  INTEGER, INTENT(IN) :: is, ie, km
4969  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac
4970  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
4971  REAL, DIMENSION(is:ie, km) :: dm2_ad, pt2_ad, pm2_ad
4972  REAL, INTENT(IN) :: ws(is:ie)
4973  REAL :: ws_ad(is:ie)
4974  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
4975  REAL, DIMENSION(is:ie, km+1) :: pem_ad
4976  REAL :: pe(is:ie, km+1)
4977  REAL :: pe_ad(is:ie, km+1)
4978  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
4979  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2_ad, w2_ad
4980  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
4981  REAL, DIMENSION(is:ie, km) :: aa_ad, bb_ad, dd_ad, w1_ad, g_rat_ad, &
4982 & gam_ad
4983  REAL, DIMENSION(is:ie, km+1) :: pp
4984  REAL, DIMENSION(is:ie, km+1) :: pp_ad
4985  REAL, DIMENSION(is:ie) :: p1, bet
4986  REAL, DIMENSION(is:ie) :: p1_ad, bet_ad
4987  REAL :: t1g, rdt, capa1
4988  INTEGER :: i, k
4989  INTRINSIC log
4990  INTRINSIC exp
4991  INTRINSIC max
4992  REAL :: max1
4993  REAL :: max1_ad
4994  REAL :: max2
4995  REAL :: max2_ad
4996  REAL :: temp
4997  REAL :: temp0
4998  REAL :: temp1
4999  REAL :: temp_ad
5000  REAL :: temp_ad0
5001  REAL :: temp_ad1
5002  REAL :: temp_ad2
5003  REAL :: temp_ad3
5004  REAL :: temp_ad4
5005  REAL :: temp2
5006  REAL :: temp_ad5
5007  REAL :: temp_ad6
5008  REAL :: temp_ad7
5009  REAL :: temp_ad8
5010  REAL :: temp_ad9
5011  REAL :: temp_ad10
5012  REAL :: temp_ad11
5013  REAL :: temp_ad12
5014  REAL :: temp_ad13
5015  REAL :: temp3
5016  REAL :: temp_ad14
5017  REAL :: temp_ad15
5018  REAL :: temp4
5019  REAL :: temp_ad16
5020  INTEGER :: branch
5021 
5022  aa = 0.0
5023  bb = 0.0
5024  dd = 0.0
5025  w1 = 0.0
5026  g_rat = 0.0
5027  gam = 0.0
5028  pp = 0.0
5029  p1 = 0.0
5030  bet = 0.0
5031  t1g = 0.0
5032  rdt = 0.0
5033  capa1 = 0.0
5034  branch = 0
5035 
5036  CALL poprealarray(dd, (ie-is+1)*km)
5037  CALL poprealarray(aa, (ie-is+1)*km)
5038  CALL poprealarray(max1)
5039  CALL poprealarray(max2)
5040  CALL poprealarray(p1, ie - is + 1)
5041  CALL poprealarray(bet, ie - is + 1)
5042  CALL poprealarray(bb, (ie-is+1)*km)
5043  CALL poprealarray(w1, (ie-is+1)*km)
5044  CALL poprealarray(rdt)
5045  CALL poprealarray(pp, (ie-is+1)*(km+1))
5046  CALL poprealarray(g_rat, (ie-is+1)*km)
5047  CALL poprealarray(capa1)
5048  CALL poprealarray(t1g)
5049  CALL poprealarray(gam, (ie-is+1)*km)
5050  p1_ad = 0.0
5051  bb_ad = 0.0
5052  g_rat_ad = 0.0
5053  DO k=1,km-1,1
5054  DO i=ie,is,-1
5055  CALL poprealarray(dz2(i, k))
5056  temp4 = capa1*log(max2)
5057  temp_ad16 = -(rgas*exp(temp4)*dz2_ad(i, k))
5058  dm2_ad(i, k) = dm2_ad(i, k) + pt2(i, k)*temp_ad16
5059  pt2_ad(i, k) = pt2_ad(i, k) + dm2(i, k)*temp_ad16
5060  max2_ad = -(capa1*exp(temp4)*dm2(i, k)*pt2(i, k)*rgas*dz2_ad(i, &
5061 & k)/max2)
5062  dz2_ad(i, k) = 0.0
5063  CALL popcontrol(1,branch)
5064  IF (branch .EQ. 0) THEN
5065  CALL poprealarray(max2)
5066  p1_ad(i) = p1_ad(i) + max2_ad
5067  pm2_ad(i, k) = pm2_ad(i, k) + max2_ad
5068  ELSE
5069  CALL poprealarray(max2)
5070  pm2_ad(i, k) = pm2_ad(i, k) + p_fac*max2_ad
5071  END IF
5072  CALL poprealarray(p1(i))
5073  temp_ad15 = r3*p1_ad(i)
5074  pe_ad(i, k) = pe_ad(i, k) + temp_ad15
5075  bb_ad(i, k) = bb_ad(i, k) + pe(i, k+1)*temp_ad15
5076  pe_ad(i, k+1) = pe_ad(i, k+1) + bb(i, k)*temp_ad15
5077  g_rat_ad(i, k) = g_rat_ad(i, k) + pe(i, k+2)*temp_ad15 - p1(i)*&
5078 & p1_ad(i)
5079  pe_ad(i, k+2) = pe_ad(i, k+2) + g_rat(i, k)*temp_ad15
5080  p1_ad(i) = -(g_rat(i, k)*p1_ad(i))
5081  END DO
5082  END DO
5083  DO i=ie,is,-1
5084  CALL poprealarray(dz2(i, km))
5085  temp3 = capa1*log(max1)
5086  temp_ad14 = -(rgas*exp(temp3)*dz2_ad(i, km))
5087  dm2_ad(i, km) = dm2_ad(i, km) + pt2(i, km)*temp_ad14
5088  pt2_ad(i, km) = pt2_ad(i, km) + dm2(i, km)*temp_ad14
5089  max1_ad = -(capa1*exp(temp3)*dm2(i, km)*pt2(i, km)*rgas*dz2_ad(i, &
5090 & km)/max1)
5091  dz2_ad(i, km) = 0.0
5092  CALL popcontrol(1,branch)
5093  IF (branch .EQ. 0) THEN
5094  CALL poprealarray(max1)
5095  p1_ad(i) = p1_ad(i) + max1_ad
5096  pm2_ad(i, km) = pm2_ad(i, km) + max1_ad
5097  ELSE
5098  CALL poprealarray(max1)
5099  pm2_ad(i, km) = pm2_ad(i, km) + p_fac*max1_ad
5100  END IF
5101  CALL poprealarray(p1(i))
5102  pe_ad(i, km) = pe_ad(i, km) + r3*p1_ad(i)
5103  pe_ad(i, km+1) = pe_ad(i, km+1) + r3*2.*p1_ad(i)
5104  p1_ad(i) = 0.0
5105  END DO
5106  w1_ad = 0.0
5107  DO k=km,1,-1
5108  DO i=ie,is,-1
5109  CALL poprealarray(pe(i, k+1))
5110  temp_ad13 = rdt*dm2(i, k)*pe_ad(i, k+1)
5111  pe_ad(i, k) = pe_ad(i, k) + pe_ad(i, k+1)
5112  dm2_ad(i, k) = dm2_ad(i, k) + rdt*(w2(i, k)-w1(i, k))*pe_ad(i, k&
5113 & +1)
5114  w2_ad(i, k) = w2_ad(i, k) + temp_ad13
5115  w1_ad(i, k) = w1_ad(i, k) - temp_ad13
5116  pe_ad(i, k+1) = 0.0
5117  END DO
5118  END DO
5119  DO i=ie,is,-1
5120  CALL poprealarray(pe(i, 1))
5121  pe_ad(i, 1) = 0.0
5122  END DO
5123  gam_ad = 0.0
5124  DO k=1,km-1,1
5125  DO i=ie,is,-1
5126  CALL poprealarray(w2(i, k))
5127  gam_ad(i, k+1) = gam_ad(i, k+1) - w2(i, k+1)*w2_ad(i, k)
5128  w2_ad(i, k+1) = w2_ad(i, k+1) - gam(i, k+1)*w2_ad(i, k)
5129  END DO
5130  END DO
5131  aa_ad = 0.0
5132  bet_ad = 0.0
5133  pp_ad = 0.0
5134  DO i=ie,is,-1
5135  CALL poprealarray(w2(i, km))
5136  temp_ad10 = w2_ad(i, km)/bet(i)
5137  w1_ad(i, km) = w1_ad(i, km) + dm2(i, km)*temp_ad10
5138  pp_ad(i, km+1) = pp_ad(i, km+1) + dt*temp_ad10
5139  pp_ad(i, km) = pp_ad(i, km) - dt*temp_ad10
5140  ws_ad(i) = ws_ad(i) - p1(i)*temp_ad10
5141  w2_ad(i, km-1) = w2_ad(i, km-1) - aa(i, km)*temp_ad10
5142  bet_ad(i) = bet_ad(i) - (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i&
5143 & , km))-p1(i)*ws(i)-aa(i, km)*w2(i, km-1))*temp_ad10/bet(i)
5144  dm2_ad(i, km) = dm2_ad(i, km) + bet_ad(i) + w1(i, km)*temp_ad10
5145  p1_ad(i) = p1_ad(i) - bet_ad(i) - ws(i)*temp_ad10
5146  w2_ad(i, km) = 0.0
5147  CALL poprealarray(bet(i))
5148  gam_ad(i, km) = gam_ad(i, km) - aa(i, km)*bet_ad(i)
5149  temp_ad11 = gam_ad(i, km)/bet(i)
5150  aa_ad(i, km) = aa_ad(i, km) + ((-1.0)-gam(i, km))*bet_ad(i) + &
5151 & temp_ad11 - w2(i, km-1)*temp_ad10
5152  bet_ad(i) = -(aa(i, km)*temp_ad11/bet(i))
5153  CALL poprealarray(gam(i, km))
5154  gam_ad(i, km) = 0.0
5155  temp_ad12 = t1g*p1_ad(i)/dz2(i, km)
5156  pem_ad(i, km+1) = pem_ad(i, km+1) + temp_ad12
5157  pp_ad(i, km+1) = pp_ad(i, km+1) + temp_ad12
5158  dz2_ad(i, km) = dz2_ad(i, km) - (pem(i, km+1)+pp(i, km+1))*&
5159 & temp_ad12/dz2(i, km)
5160  p1_ad(i) = 0.0
5161  END DO
5162  DO k=km-1,2,-1
5163  DO i=ie,is,-1
5164  CALL poprealarray(w2(i, k))
5165  temp_ad8 = w2_ad(i, k)/bet(i)
5166  w1_ad(i, k) = w1_ad(i, k) + dm2(i, k)*temp_ad8
5167  pp_ad(i, k+1) = pp_ad(i, k+1) + dt*temp_ad8
5168  pp_ad(i, k) = pp_ad(i, k) - dt*temp_ad8
5169  w2_ad(i, k-1) = w2_ad(i, k-1) - aa(i, k)*temp_ad8
5170  bet_ad(i) = bet_ad(i) - (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i&
5171 & , k))-aa(i, k)*w2(i, k-1))*temp_ad8/bet(i)
5172  dm2_ad(i, k) = dm2_ad(i, k) + bet_ad(i) + w1(i, k)*temp_ad8
5173  aa_ad(i, k) = aa_ad(i, k) + ((-1.0)-gam(i, k))*bet_ad(i) - w2(i&
5174 & , k-1)*temp_ad8
5175  w2_ad(i, k) = 0.0
5176  CALL poprealarray(bet(i))
5177  aa_ad(i, k+1) = aa_ad(i, k+1) - bet_ad(i)
5178  gam_ad(i, k) = gam_ad(i, k) - aa(i, k)*bet_ad(i)
5179  CALL poprealarray(gam(i, k))
5180  temp_ad9 = gam_ad(i, k)/bet(i)
5181  bet_ad(i) = -(aa(i, k)*temp_ad9/bet(i))
5182  aa_ad(i, k) = aa_ad(i, k) + temp_ad9
5183  gam_ad(i, k) = 0.0
5184  END DO
5185  END DO
5186  DO i=ie,is,-1
5187  CALL poprealarray(w2(i, 1))
5188  temp_ad7 = w2_ad(i, 1)/bet(i)
5189  w1_ad(i, 1) = w1_ad(i, 1) + dm2(i, 1)*temp_ad7
5190  pp_ad(i, 2) = pp_ad(i, 2) + dt*temp_ad7
5191  bet_ad(i) = bet_ad(i) - (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2))*temp_ad7/&
5192 & bet(i)
5193  dm2_ad(i, 1) = dm2_ad(i, 1) + bet_ad(i) + w1(i, 1)*temp_ad7
5194  w2_ad(i, 1) = 0.0
5195  CALL poprealarray(bet(i))
5196  aa_ad(i, 2) = aa_ad(i, 2) - bet_ad(i)
5197  bet_ad(i) = 0.0
5198  END DO
5199  DO k=km,2,-1
5200  DO i=ie,is,-1
5201  temp2 = dz2(i, k-1) + dz2(i, k)
5202  temp_ad5 = t1g*aa_ad(i, k)/temp2
5203  temp_ad6 = -((pem(i, k)+pp(i, k))*temp_ad5/temp2)
5204  pem_ad(i, k) = pem_ad(i, k) + temp_ad5
5205  pp_ad(i, k) = pp_ad(i, k) + temp_ad5
5206  dz2_ad(i, k-1) = dz2_ad(i, k-1) + temp_ad6
5207  dz2_ad(i, k) = dz2_ad(i, k) + temp_ad6
5208  aa_ad(i, k) = 0.0
5209  END DO
5210  END DO
5211  DO k=2,km,1
5212  DO i=ie,is,-1
5213  CALL poprealarray(pp(i, k))
5214  gam_ad(i, k) = gam_ad(i, k) - pp(i, k+1)*pp_ad(i, k)
5215  pp_ad(i, k+1) = pp_ad(i, k+1) - gam(i, k)*pp_ad(i, k)
5216  END DO
5217  END DO
5218  dd_ad = 0.0
5219  DO k=km,2,-1
5220  DO i=ie,is,-1
5221  CALL poprealarray(pp(i, k+1))
5222  temp_ad3 = pp_ad(i, k+1)/bet(i)
5223  dd_ad(i, k) = dd_ad(i, k) + temp_ad3
5224  pp_ad(i, k) = pp_ad(i, k) - temp_ad3
5225  bet_ad(i) = bet_ad(i) - (dd(i, k)-pp(i, k))*temp_ad3/bet(i)
5226  pp_ad(i, k+1) = 0.0
5227  CALL poprealarray(bet(i))
5228  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
5229  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
5230  temp_ad4 = gam_ad(i, k)/bet(i)
5231  bet_ad(i) = -(g_rat(i, k-1)*temp_ad4/bet(i))
5232  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad4
5233  gam_ad(i, k) = 0.0
5234  END DO
5235  END DO
5236  DO i=ie,is,-1
5237  CALL poprealarray(dd(i, km))
5238  pe_ad(i, km) = pe_ad(i, km) + 3.*dd_ad(i, km)
5239  dd_ad(i, km) = 0.0
5240  bb_ad(i, km) = 0.0
5241  temp_ad2 = pp_ad(i, 2)/bet(i)
5242  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad2
5243  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad2/bet(i)
5244  pp_ad(i, 2) = 0.0
5245  pp_ad(i, 1) = 0.0
5246  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
5247  bet_ad(i) = 0.0
5248  END DO
5249  DO k=km-1,1,-1
5250  DO i=ie,is,-1
5251  temp_ad0 = 3.*dd_ad(i, k)
5252  pe_ad(i, k) = pe_ad(i, k) + temp_ad0
5253  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pe(i, k+1)*&
5254 & temp_ad0
5255  pe_ad(i, k+1) = pe_ad(i, k+1) + g_rat(i, k)*temp_ad0
5256  dd_ad(i, k) = 0.0
5257  bb_ad(i, k) = 0.0
5258  temp_ad1 = g_rat_ad(i, k)/dm2(i, k+1)
5259  dm2_ad(i, k) = dm2_ad(i, k) + temp_ad1
5260  dm2_ad(i, k+1) = dm2_ad(i, k+1) - dm2(i, k)*temp_ad1/dm2(i, k+1)
5261  g_rat_ad(i, k) = 0.0
5262  END DO
5263  END DO
5264  DO k=km,1,-1
5265  DO i=ie,is,-1
5266  CALL poprealarray(pe(i, k))
5267  temp1 = dz2(i, k)
5268  temp0 = dm2(i, k)*pt2(i, k)
5269  temp = temp0/temp1
5270  temp_ad = gama*exp(gama*log(-(rgas*temp)))*pe_ad(i, k)/(temp*&
5271 & temp1)
5272  dm2_ad(i, k) = dm2_ad(i, k) + pt2(i, k)*temp_ad
5273  pt2_ad(i, k) = pt2_ad(i, k) + dm2(i, k)*temp_ad
5274  dz2_ad(i, k) = dz2_ad(i, k) - temp*temp_ad
5275  pm2_ad(i, k) = pm2_ad(i, k) - pe_ad(i, k)
5276  pe_ad(i, k) = 0.0
5277  w2_ad(i, k) = w2_ad(i, k) + w1_ad(i, k)
5278  w1_ad(i, k) = 0.0
5279  END DO
5280  END DO
5281  END SUBROUTINE sim1_solver_bwd
5282  SUBROUTINE sim1_solver(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe&
5283 & , dm2, pm2, pem, w2, dz2, pt2, ws, p_fac)
5284  IMPLICIT NONE
5285  INTEGER, INTENT(IN) :: is, ie, km
5286  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac
5287  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
5288  REAL, INTENT(IN) :: ws(is:ie)
5289  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
5290  REAL, INTENT(OUT) :: pe(is:ie, km+1)
5291  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
5292 ! Local
5293  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, g_rat, gam
5294  REAL, DIMENSION(is:ie, km+1) :: pp
5295  REAL, DIMENSION(is:ie) :: p1, bet
5296  REAL :: t1g, rdt, capa1
5297  INTEGER :: i, k
5298  INTRINSIC log
5299  INTRINSIC exp
5300  INTRINSIC max
5301  REAL :: max1
5302  REAL :: max2
5303  t1g = gama*2.*dt*dt
5304  rdt = 1./dt
5305  capa1 = kappa - 1.
5306  DO k=1,km
5307  DO i=is,ie
5308  w1(i, k) = w2(i, k)
5309  pe(i, k) = exp(gama*log(-(dm2(i, k)/dz2(i, k)*rgas*pt2(i, k)))) &
5310 & - pm2(i, k)
5311  END DO
5312  END DO
5313  DO k=1,km-1
5314  DO i=is,ie
5315  g_rat(i, k) = dm2(i, k)/dm2(i, k+1)
5316  bb(i, k) = 2.*(1.+g_rat(i, k))
5317  dd(i, k) = 3.*(pe(i, k)+g_rat(i, k)*pe(i, k+1))
5318  END DO
5319  END DO
5320  DO i=is,ie
5321  bet(i) = bb(i, 1)
5322  pp(i, 1) = 0.
5323  pp(i, 2) = dd(i, 1)/bet(i)
5324  bb(i, km) = 2.
5325  dd(i, km) = 3.*pe(i, km)
5326  END DO
5327  DO k=2,km
5328  DO i=is,ie
5329  gam(i, k) = g_rat(i, k-1)/bet(i)
5330  bet(i) = bb(i, k) - gam(i, k)
5331  pp(i, k+1) = (dd(i, k)-pp(i, k))/bet(i)
5332  END DO
5333  END DO
5334  DO k=km,2,-1
5335  DO i=is,ie
5336  pp(i, k) = pp(i, k) - gam(i, k)*pp(i, k+1)
5337  END DO
5338  END DO
5339 ! Start the w-solver
5340  DO k=2,km
5341  DO i=is,ie
5342  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*(pem(i, k)+pp(i, k))
5343  END DO
5344  END DO
5345  DO i=is,ie
5346  bet(i) = dm2(i, 1) - aa(i, 2)
5347  w2(i, 1) = (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2))/bet(i)
5348  END DO
5349  DO k=2,km-1
5350  DO i=is,ie
5351  gam(i, k) = aa(i, k)/bet(i)
5352  bet(i) = dm2(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
5353  w2(i, k) = (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))-aa(i, k)&
5354 & *w2(i, k-1))/bet(i)
5355  END DO
5356  END DO
5357  DO i=is,ie
5358  p1(i) = t1g/dz2(i, km)*(pem(i, km+1)+pp(i, km+1))
5359  gam(i, km) = aa(i, km)/bet(i)
5360  bet(i) = dm2(i, km) - (aa(i, km)+p1(i)+aa(i, km)*gam(i, km))
5361  w2(i, km) = (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-p1(i)&
5362 & *ws(i)-aa(i, km)*w2(i, km-1))/bet(i)
5363  END DO
5364  DO k=km-1,1,-1
5365  DO i=is,ie
5366  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
5367  END DO
5368  END DO
5369  DO i=is,ie
5370  pe(i, 1) = 0.
5371  END DO
5372  DO k=1,km
5373  DO i=is,ie
5374  pe(i, k+1) = pe(i, k) + dm2(i, k)*(w2(i, k)-w1(i, k))*rdt
5375  END DO
5376  END DO
5377  DO i=is,ie
5378  p1(i) = (pe(i, km)+2.*pe(i, km+1))*r3
5379  IF (p_fac*pm2(i, km) .LT. p1(i) + pm2(i, km)) THEN
5380  max1 = p1(i) + pm2(i, km)
5381  ELSE
5382  max1 = p_fac*pm2(i, km)
5383  END IF
5384  dz2(i, km) = -(dm2(i, km)*rgas*pt2(i, km)*exp(capa1*log(max1)))
5385  END DO
5386  DO k=km-1,1,-1
5387  DO i=is,ie
5388  p1(i) = (pe(i, k)+bb(i, k)*pe(i, k+1)+g_rat(i, k)*pe(i, k+2))*r3&
5389 & - g_rat(i, k)*p1(i)
5390  IF (p_fac*pm2(i, k) .LT. p1(i) + pm2(i, k)) THEN
5391  max2 = p1(i) + pm2(i, k)
5392  ELSE
5393  max2 = p_fac*pm2(i, k)
5394  END IF
5395  dz2(i, k) = -(dm2(i, k)*rgas*pt2(i, k)*exp(capa1*log(max2)))
5396  END DO
5397  END DO
5398  END SUBROUTINE sim1_solver
5399 ! Differentiation of sim_solver in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_m
5400 !od.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_
5401 !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.m
5402 !ix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayleigh
5403 !_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_ord
5404 !4 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.re
5405 !map_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 f
5406 !v_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limiters
5407 !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_r
5408 !estart_mod.d2c_setup fv_tracer2d_mod.tracer_2d_1L fv_tracer2d_mod.tracer_2d fv_tracer2d_mod.tracer_2d_nested fv_sg_mod.fv_subgri
5409 !d_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_
5410 !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_mo
5411 !d.SIM3p0_solver nh_utils_mod.SIM1_solver nh_utils_mod.SIM_solver nh_utils_mod.edge_scalar nh_utils_mod.edge_profile nh_utils_mod
5412 !.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.d2
5413 !a2c_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_
5414 !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_
5415 !mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_utils
5416 !_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
5417 ! gradient of useful results: ws pe2 dm2 dz2 w2 pm2 pem pt2
5418 ! with respect to varying inputs: ws pe2 dm2 dz2 w2 pm2 pem pt2
5419  SUBROUTINE sim_solver_fwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa&
5420 & , pe2, dm2, pm2, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
5421  IMPLICIT NONE
5422  INTEGER, INTENT(IN) :: is, ie, km
5423  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, alpha, scale_m
5424  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
5425  REAL, INTENT(IN) :: ws(is:ie)
5426  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
5427  REAL :: pe2(is:ie, km+1)
5428  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
5429 ! Local
5430  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
5431  REAL, DIMENSION(is:ie, km+1) :: pp
5432  REAL, DIMENSION(is:ie) :: p1, wk1, bet
5433  REAL :: beta, t2, t1g, rdt, ra, capa1
5434  INTEGER :: i, k
5435  INTRINSIC log
5436  INTRINSIC exp
5437  INTRINSIC max
5438  REAL :: max1
5439  REAL :: max2
5440 
5441  aa = 0.0
5442  bb = 0.0
5443  dd = 0.0
5444  w1 = 0.0
5445  wk = 0.0
5446  g_rat = 0.0
5447  gam = 0.0
5448  pp = 0.0
5449  p1 = 0.0
5450  wk1 = 0.0
5451  bet = 0.0
5452  t1g = 0.0
5453  rdt = 0.0
5454  capa1 = 0.0
5455  max1 = 0.0
5456  max2 = 0.0
5457 
5458  beta = 1. - alpha
5459  ra = 1./alpha
5460  t2 = beta/alpha
5461  t1g = 2.*gama*(alpha*dt)**2
5462  rdt = 1./dt
5463  capa1 = kappa - 1.
5464  DO k=1,km
5465  DO i=is,ie
5466  w1(i, k) = w2(i, k)
5467 ! P_g perturbation
5468  CALL pushrealarray(pe2(i, k))
5469  pe2(i, k) = exp(gama*log(-(dm2(i, k)/dz2(i, k)*rgas*pt2(i, k))))&
5470 & - pm2(i, k)
5471  END DO
5472  END DO
5473  DO k=1,km-1
5474  DO i=is,ie
5475  g_rat(i, k) = dm2(i, k)/dm2(i, k+1)
5476  bb(i, k) = 2.*(1.+g_rat(i, k))
5477  dd(i, k) = 3.*(pe2(i, k)+g_rat(i, k)*pe2(i, k+1))
5478  END DO
5479  END DO
5480  DO i=is,ie
5481  bet(i) = bb(i, 1)
5482  pp(i, 1) = 0.
5483  pp(i, 2) = dd(i, 1)/bet(i)
5484  bb(i, km) = 2.
5485  CALL pushrealarray(dd(i, km))
5486  dd(i, km) = 3.*pe2(i, km)
5487  END DO
5488  DO k=2,km
5489  DO i=is,ie
5490  gam(i, k) = g_rat(i, k-1)/bet(i)
5491  CALL pushrealarray(bet(i))
5492  bet(i) = bb(i, k) - gam(i, k)
5493  CALL pushrealarray(pp(i, k+1))
5494  pp(i, k+1) = (dd(i, k)-pp(i, k))/bet(i)
5495  END DO
5496  END DO
5497  DO k=km,2,-1
5498  DO i=is,ie
5499  CALL pushrealarray(pp(i, k))
5500  pp(i, k) = pp(i, k) - gam(i, k)*pp(i, k+1)
5501  END DO
5502  END DO
5503  DO k=1,km+1
5504  DO i=is,ie
5505 ! pe2 is Full p
5506  CALL pushrealarray(pe2(i, k))
5507  pe2(i, k) = pem(i, k) + pp(i, k)
5508  END DO
5509  END DO
5510  DO k=2,km
5511  DO i=is,ie
5512  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k)
5513  wk(i, k) = t2*aa(i, k)*(w1(i, k-1)-w1(i, k))
5514  CALL pushrealarray(aa(i, k))
5515  aa(i, k) = aa(i, k) - scale_m*dm2(i, 1)
5516  END DO
5517  END DO
5518 ! Top:
5519  DO i=is,ie
5520  CALL pushrealarray(bet(i))
5521  bet(i) = dm2(i, 1) - aa(i, 2)
5522  CALL pushrealarray(w2(i, 1))
5523  w2(i, 1) = (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))/bet(i)
5524  END DO
5525 ! Interior:
5526  DO k=2,km-1
5527  DO i=is,ie
5528  CALL pushrealarray(gam(i, k))
5529  gam(i, k) = aa(i, k)/bet(i)
5530  CALL pushrealarray(bet(i))
5531  bet(i) = dm2(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
5532  CALL pushrealarray(w2(i, k))
5533  w2(i, k) = (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))+wk(i, k+&
5534 & 1)-wk(i, k)-aa(i, k)*w2(i, k-1))/bet(i)
5535  END DO
5536  END DO
5537 ! Bottom: k=km
5538  DO i=is,ie
5539  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
5540  CALL pushrealarray(gam(i, km))
5541  gam(i, km) = aa(i, km)/bet(i)
5542  CALL pushrealarray(bet(i))
5543  bet(i) = dm2(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
5544  CALL pushrealarray(w2(i, km))
5545  w2(i, km) = (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk(i&
5546 & , km)+wk1(i)*(t2*w1(i, km)-ra*ws(i))-aa(i, km)*w2(i, km-1))/bet(&
5547 & i)
5548  END DO
5549  DO k=km-1,1,-1
5550  DO i=is,ie
5551  CALL pushrealarray(w2(i, k))
5552  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
5553  END DO
5554  END DO
5555  DO i=is,ie
5556  CALL pushrealarray(pe2(i, 1))
5557  pe2(i, 1) = 0.
5558  END DO
5559  DO k=1,km
5560  DO i=is,ie
5561  CALL pushrealarray(pe2(i, k+1))
5562  pe2(i, k+1) = pe2(i, k) + (dm2(i, k)*(w2(i, k)-w1(i, k))*rdt-&
5563 & beta*(pp(i, k+1)-pp(i, k)))*ra
5564  END DO
5565  END DO
5566  DO i=is,ie
5567  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3
5568  IF (p_fac*pm2(i, km) .LT. p1(i) + pm2(i, km)) THEN
5569  CALL pushrealarray(max1)
5570  max1 = p1(i) + pm2(i, km)
5571  CALL pushcontrol(1,0)
5572  ELSE
5573  CALL pushrealarray(max1)
5574  max1 = p_fac*pm2(i, km)
5575  CALL pushcontrol(1,1)
5576  END IF
5577  CALL pushrealarray(dz2(i, km))
5578  dz2(i, km) = -(dm2(i, km)*rgas*pt2(i, km)*exp(capa1*log(max1)))
5579  END DO
5580  DO k=km-1,1,-1
5581  DO i=is,ie
5582  CALL pushrealarray(p1(i))
5583  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
5584 & *r3 - g_rat(i, k)*p1(i)
5585  IF (p_fac*pm2(i, k) .LT. p1(i) + pm2(i, k)) THEN
5586  CALL pushrealarray(max2)
5587  max2 = p1(i) + pm2(i, k)
5588  CALL pushcontrol(1,0)
5589  ELSE
5590  CALL pushrealarray(max2)
5591  max2 = p_fac*pm2(i, k)
5592  CALL pushcontrol(1,1)
5593  END IF
5594 ! delz = -dm*R*T_m / p_gas
5595  CALL pushrealarray(dz2(i, k))
5596  dz2(i, k) = -(dm2(i, k)*rgas*pt2(i, k)*exp(capa1*log(max2)))
5597  END DO
5598  END DO
5599  DO k=1,km+1
5600  DO i=is,ie
5601  CALL pushrealarray(pe2(i, k))
5602  pe2(i, k) = pe2(i, k) + beta*(pp(i, k)-pe2(i, k))
5603  END DO
5604  END DO
5605  CALL pushrealarray(gam, (ie-is+1)*km)
5606  CALL pushrealarray(t1g)
5607  CALL pushrealarray(wk, (ie-is+1)*km)
5608  CALL pushrealarray(capa1)
5609  CALL pushrealarray(g_rat, (ie-is+1)*km)
5610  CALL pushrealarray(pp, (ie-is+1)*(km+1))
5611  CALL pushrealarray(beta)
5612  CALL pushrealarray(rdt)
5613  CALL pushrealarray(t2)
5614  CALL pushrealarray(w1, (ie-is+1)*km)
5615  CALL pushrealarray(bb, (ie-is+1)*km)
5616  CALL pushrealarray(ra)
5617  CALL pushrealarray(wk1, ie - is + 1)
5618  CALL pushrealarray(bet, ie - is + 1)
5619  CALL pushrealarray(max2)
5620  CALL pushrealarray(max1)
5621  CALL pushrealarray(aa, (ie-is+1)*km)
5622  CALL pushrealarray(dd, (ie-is+1)*km)
5623  END SUBROUTINE sim_solver_fwd
5624 ! Differentiation of sim_solver in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge_
5625 !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
5626 !_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.
5627 !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
5628 !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
5629 !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
5630 !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
5631 !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
5632 ! 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_
5633 !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
5634 !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
5635 !_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
5636 !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
5637 !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
5638 !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
5639 !_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
5640 !_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_util
5641 !s_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
5642 ! gradient of useful results: ws pe2 dm2 dz2 w2 pm2 pem pt2
5643 ! with respect to varying inputs: ws pe2 dm2 dz2 w2 pm2 pem pt2
5644  SUBROUTINE sim_solver_bwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa&
5645 & , pe2, pe2_ad, dm2, dm2_ad, pm2, pm2_ad, pem, pem_ad, w2, w2_ad, dz2&
5646 & , dz2_ad, pt2, pt2_ad, ws, ws_ad, alpha, p_fac, scale_m)
5647  IMPLICIT NONE
5648  INTEGER, INTENT(IN) :: is, ie, km
5649  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, alpha, scale_m
5650  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
5651  REAL, DIMENSION(is:ie, km) :: dm2_ad, pt2_ad, pm2_ad
5652  REAL, INTENT(IN) :: ws(is:ie)
5653  REAL :: ws_ad(is:ie)
5654  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
5655  REAL, DIMENSION(is:ie, km+1) :: pem_ad
5656  REAL :: pe2(is:ie, km+1)
5657  REAL :: pe2_ad(is:ie, km+1)
5658  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
5659  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2_ad, w2_ad
5660  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
5661  REAL, DIMENSION(is:ie, km) :: aa_ad, bb_ad, dd_ad, w1_ad, wk_ad, &
5662 & g_rat_ad, gam_ad
5663  REAL, DIMENSION(is:ie, km+1) :: pp
5664  REAL, DIMENSION(is:ie, km+1) :: pp_ad
5665  REAL, DIMENSION(is:ie) :: p1, wk1, bet
5666  REAL, DIMENSION(is:ie) :: p1_ad, wk1_ad, bet_ad
5667  REAL :: beta, t2, t1g, rdt, ra, capa1
5668  INTEGER :: i, k
5669  INTRINSIC log
5670  INTRINSIC exp
5671  INTRINSIC max
5672  REAL :: max1
5673  REAL :: max1_ad
5674  REAL :: max2
5675  REAL :: max2_ad
5676  REAL :: temp
5677  REAL :: temp0
5678  REAL :: temp1
5679  REAL :: temp_ad
5680  REAL :: temp_ad0
5681  REAL :: temp_ad1
5682  REAL :: temp_ad2
5683  REAL :: temp_ad3
5684  REAL :: temp_ad4
5685  REAL :: temp2
5686  REAL :: temp_ad5
5687  REAL :: temp_ad6
5688  REAL :: temp_ad7
5689  REAL :: temp_ad8
5690  REAL :: temp_ad9
5691  REAL :: temp_ad10
5692  REAL :: temp3
5693  REAL :: temp_ad11
5694  REAL :: temp_ad12
5695  REAL :: temp_ad13
5696  REAL :: temp_ad14
5697  REAL :: temp_ad15
5698  REAL :: temp4
5699  REAL :: temp_ad16
5700  REAL :: temp_ad17
5701  REAL :: temp5
5702  REAL :: temp_ad18
5703  INTEGER :: branch
5704 
5705  aa = 0.0
5706  bb = 0.0
5707  dd = 0.0
5708  w1 = 0.0
5709  wk = 0.0
5710  g_rat = 0.0
5711  gam = 0.0
5712  pp = 0.0
5713  p1 = 0.0
5714  wk1 = 0.0
5715  bet = 0.0
5716  t1g = 0.0
5717  rdt = 0.0
5718  capa1 = 0.0
5719  max1 = 0.0
5720  max2 = 0.0
5721  branch = 0
5722 
5723  CALL poprealarray(dd, (ie-is+1)*km)
5724  CALL poprealarray(aa, (ie-is+1)*km)
5725  CALL poprealarray(max1)
5726  CALL poprealarray(max2)
5727  CALL poprealarray(bet, ie - is + 1)
5728  CALL poprealarray(wk1, ie - is + 1)
5729  CALL poprealarray(ra)
5730  CALL poprealarray(bb, (ie-is+1)*km)
5731  CALL poprealarray(w1, (ie-is+1)*km)
5732  CALL poprealarray(t2)
5733  CALL poprealarray(rdt)
5734  CALL poprealarray(beta)
5735  CALL poprealarray(pp, (ie-is+1)*(km+1))
5736  CALL poprealarray(g_rat, (ie-is+1)*km)
5737  CALL poprealarray(capa1)
5738  CALL poprealarray(wk, (ie-is+1)*km)
5739  CALL poprealarray(t1g)
5740  CALL poprealarray(gam, (ie-is+1)*km)
5741  pp_ad = 0.0
5742  DO k=km+1,1,-1
5743  DO i=ie,is,-1
5744  CALL poprealarray(pe2(i, k))
5745  pp_ad(i, k) = pp_ad(i, k) + beta*pe2_ad(i, k)
5746  pe2_ad(i, k) = (1.0-beta)*pe2_ad(i, k)
5747  END DO
5748  END DO
5749  p1_ad = 0.0
5750  bb_ad = 0.0
5751  g_rat_ad = 0.0
5752  DO k=1,km-1,1
5753  DO i=ie,is,-1
5754  CALL poprealarray(dz2(i, k))
5755  temp5 = capa1*log(max2)
5756  temp_ad18 = -(rgas*exp(temp5)*dz2_ad(i, k))
5757  dm2_ad(i, k) = dm2_ad(i, k) + pt2(i, k)*temp_ad18
5758  pt2_ad(i, k) = pt2_ad(i, k) + dm2(i, k)*temp_ad18
5759  max2_ad = -(capa1*exp(temp5)*dm2(i, k)*pt2(i, k)*rgas*dz2_ad(i, &
5760 & k)/max2)
5761  dz2_ad(i, k) = 0.0
5762  CALL popcontrol(1,branch)
5763  IF (branch .EQ. 0) THEN
5764  CALL poprealarray(max2)
5765  p1_ad(i) = p1_ad(i) + max2_ad
5766  pm2_ad(i, k) = pm2_ad(i, k) + max2_ad
5767  ELSE
5768  CALL poprealarray(max2)
5769  pm2_ad(i, k) = pm2_ad(i, k) + p_fac*max2_ad
5770  END IF
5771  CALL poprealarray(p1(i))
5772  temp_ad17 = r3*p1_ad(i)
5773  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad17
5774  bb_ad(i, k) = bb_ad(i, k) + pe2(i, k+1)*temp_ad17
5775  pe2_ad(i, k+1) = pe2_ad(i, k+1) + bb(i, k)*temp_ad17
5776  g_rat_ad(i, k) = g_rat_ad(i, k) + pe2(i, k+2)*temp_ad17 - p1(i)*&
5777 & p1_ad(i)
5778  pe2_ad(i, k+2) = pe2_ad(i, k+2) + g_rat(i, k)*temp_ad17
5779  p1_ad(i) = -(g_rat(i, k)*p1_ad(i))
5780  END DO
5781  END DO
5782  DO i=ie,is,-1
5783  CALL poprealarray(dz2(i, km))
5784  temp4 = capa1*log(max1)
5785  temp_ad16 = -(rgas*exp(temp4)*dz2_ad(i, km))
5786  dm2_ad(i, km) = dm2_ad(i, km) + pt2(i, km)*temp_ad16
5787  pt2_ad(i, km) = pt2_ad(i, km) + dm2(i, km)*temp_ad16
5788  max1_ad = -(capa1*exp(temp4)*dm2(i, km)*pt2(i, km)*rgas*dz2_ad(i, &
5789 & km)/max1)
5790  dz2_ad(i, km) = 0.0
5791  CALL popcontrol(1,branch)
5792  IF (branch .EQ. 0) THEN
5793  CALL poprealarray(max1)
5794  p1_ad(i) = p1_ad(i) + max1_ad
5795  pm2_ad(i, km) = pm2_ad(i, km) + max1_ad
5796  ELSE
5797  CALL poprealarray(max1)
5798  pm2_ad(i, km) = pm2_ad(i, km) + p_fac*max1_ad
5799  END IF
5800  pe2_ad(i, km) = pe2_ad(i, km) + r3*p1_ad(i)
5801  pe2_ad(i, km+1) = pe2_ad(i, km+1) + r3*2.*p1_ad(i)
5802  p1_ad(i) = 0.0
5803  END DO
5804  w1_ad = 0.0
5805  DO k=km,1,-1
5806  DO i=ie,is,-1
5807  CALL poprealarray(pe2(i, k+1))
5808  temp_ad14 = ra*pe2_ad(i, k+1)
5809  temp_ad15 = rdt*dm2(i, k)*temp_ad14
5810  pe2_ad(i, k) = pe2_ad(i, k) + pe2_ad(i, k+1)
5811  dm2_ad(i, k) = dm2_ad(i, k) + rdt*(w2(i, k)-w1(i, k))*temp_ad14
5812  w2_ad(i, k) = w2_ad(i, k) + temp_ad15
5813  w1_ad(i, k) = w1_ad(i, k) - temp_ad15
5814  pp_ad(i, k+1) = pp_ad(i, k+1) - beta*temp_ad14
5815  pp_ad(i, k) = pp_ad(i, k) + beta*temp_ad14
5816  pe2_ad(i, k+1) = 0.0
5817  END DO
5818  END DO
5819  DO i=ie,is,-1
5820  CALL poprealarray(pe2(i, 1))
5821  pe2_ad(i, 1) = 0.0
5822  END DO
5823  gam_ad = 0.0
5824  DO k=1,km-1,1
5825  DO i=ie,is,-1
5826  CALL poprealarray(w2(i, k))
5827  gam_ad(i, k+1) = gam_ad(i, k+1) - w2(i, k+1)*w2_ad(i, k)
5828  w2_ad(i, k+1) = w2_ad(i, k+1) - gam(i, k+1)*w2_ad(i, k)
5829  END DO
5830  END DO
5831  aa_ad = 0.0
5832  bet_ad = 0.0
5833  wk1_ad = 0.0
5834  wk_ad = 0.0
5835  DO i=ie,is,-1
5836  CALL poprealarray(w2(i, km))
5837  temp_ad11 = w2_ad(i, km)/bet(i)
5838  temp3 = t2*w1(i, km) - ra*ws(i)
5839  w1_ad(i, km) = w1_ad(i, km) + (wk1(i)*t2+dm2(i, km))*temp_ad11
5840  pp_ad(i, km+1) = pp_ad(i, km+1) + dt*temp_ad11
5841  pp_ad(i, km) = pp_ad(i, km) - dt*temp_ad11
5842  wk_ad(i, km) = wk_ad(i, km) - temp_ad11
5843  ws_ad(i) = ws_ad(i) - wk1(i)*ra*temp_ad11
5844  w2_ad(i, km-1) = w2_ad(i, km-1) - aa(i, km)*temp_ad11
5845  bet_ad(i) = bet_ad(i) - (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i&
5846 & , km))-wk(i, km)+wk1(i)*temp3-aa(i, km)*w2(i, km-1))*temp_ad11/&
5847 & bet(i)
5848  dm2_ad(i, km) = dm2_ad(i, km) + bet_ad(i) + w1(i, km)*temp_ad11
5849  wk1_ad(i) = wk1_ad(i) + temp3*temp_ad11 - bet_ad(i)
5850  w2_ad(i, km) = 0.0
5851  CALL poprealarray(bet(i))
5852  gam_ad(i, km) = gam_ad(i, km) - aa(i, km)*bet_ad(i)
5853  temp_ad12 = gam_ad(i, km)/bet(i)
5854  aa_ad(i, km) = aa_ad(i, km) + ((-1.0)-gam(i, km))*bet_ad(i) + &
5855 & temp_ad12 - w2(i, km-1)*temp_ad11
5856  bet_ad(i) = -(aa(i, km)*temp_ad12/bet(i))
5857  CALL poprealarray(gam(i, km))
5858  gam_ad(i, km) = 0.0
5859  temp_ad13 = t1g*wk1_ad(i)/dz2(i, km)
5860  pe2_ad(i, km+1) = pe2_ad(i, km+1) + temp_ad13
5861  dz2_ad(i, km) = dz2_ad(i, km) - pe2(i, km+1)*temp_ad13/dz2(i, km)
5862  wk1_ad(i) = 0.0
5863  END DO
5864  DO k=km-1,2,-1
5865  DO i=ie,is,-1
5866  CALL poprealarray(w2(i, k))
5867  temp_ad9 = w2_ad(i, k)/bet(i)
5868  w1_ad(i, k) = w1_ad(i, k) + dm2(i, k)*temp_ad9
5869  pp_ad(i, k+1) = pp_ad(i, k+1) + dt*temp_ad9
5870  pp_ad(i, k) = pp_ad(i, k) - dt*temp_ad9
5871  wk_ad(i, k+1) = wk_ad(i, k+1) + temp_ad9
5872  wk_ad(i, k) = wk_ad(i, k) - temp_ad9
5873  w2_ad(i, k-1) = w2_ad(i, k-1) - aa(i, k)*temp_ad9
5874  bet_ad(i) = bet_ad(i) - (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i&
5875 & , k))+wk(i, k+1)-wk(i, k)-aa(i, k)*w2(i, k-1))*temp_ad9/bet(i)
5876  dm2_ad(i, k) = dm2_ad(i, k) + bet_ad(i) + w1(i, k)*temp_ad9
5877  aa_ad(i, k) = aa_ad(i, k) + ((-1.0)-gam(i, k))*bet_ad(i) - w2(i&
5878 & , k-1)*temp_ad9
5879  w2_ad(i, k) = 0.0
5880  CALL poprealarray(bet(i))
5881  aa_ad(i, k+1) = aa_ad(i, k+1) - bet_ad(i)
5882  gam_ad(i, k) = gam_ad(i, k) - aa(i, k)*bet_ad(i)
5883  CALL poprealarray(gam(i, k))
5884  temp_ad10 = gam_ad(i, k)/bet(i)
5885  bet_ad(i) = -(aa(i, k)*temp_ad10/bet(i))
5886  aa_ad(i, k) = aa_ad(i, k) + temp_ad10
5887  gam_ad(i, k) = 0.0
5888  END DO
5889  END DO
5890  DO i=ie,is,-1
5891  CALL poprealarray(w2(i, 1))
5892  temp_ad8 = w2_ad(i, 1)/bet(i)
5893  w1_ad(i, 1) = w1_ad(i, 1) + dm2(i, 1)*temp_ad8
5894  pp_ad(i, 2) = pp_ad(i, 2) + dt*temp_ad8
5895  wk_ad(i, 2) = wk_ad(i, 2) + temp_ad8
5896  bet_ad(i) = bet_ad(i) - (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))*&
5897 & temp_ad8/bet(i)
5898  dm2_ad(i, 1) = dm2_ad(i, 1) + bet_ad(i) + w1(i, 1)*temp_ad8
5899  w2_ad(i, 1) = 0.0
5900  CALL poprealarray(bet(i))
5901  aa_ad(i, 2) = aa_ad(i, 2) - bet_ad(i)
5902  bet_ad(i) = 0.0
5903  END DO
5904  DO k=km,2,-1
5905  DO i=ie,is,-1
5906  CALL poprealarray(aa(i, k))
5907  dm2_ad(i, 1) = dm2_ad(i, 1) - scale_m*aa_ad(i, k)
5908  temp_ad5 = t2*aa(i, k)*wk_ad(i, k)
5909  aa_ad(i, k) = aa_ad(i, k) + t2*(w1(i, k-1)-w1(i, k))*wk_ad(i, k)
5910  w1_ad(i, k-1) = w1_ad(i, k-1) + temp_ad5
5911  w1_ad(i, k) = w1_ad(i, k) - temp_ad5
5912  wk_ad(i, k) = 0.0
5913  temp2 = dz2(i, k-1) + dz2(i, k)
5914  temp_ad6 = t1g*aa_ad(i, k)/temp2
5915  temp_ad7 = -(pe2(i, k)*temp_ad6/temp2)
5916  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad6
5917  dz2_ad(i, k-1) = dz2_ad(i, k-1) + temp_ad7
5918  dz2_ad(i, k) = dz2_ad(i, k) + temp_ad7
5919  aa_ad(i, k) = 0.0
5920  END DO
5921  END DO
5922  DO k=km+1,1,-1
5923  DO i=ie,is,-1
5924  CALL poprealarray(pe2(i, k))
5925  pem_ad(i, k) = pem_ad(i, k) + pe2_ad(i, k)
5926  pp_ad(i, k) = pp_ad(i, k) + pe2_ad(i, k)
5927  pe2_ad(i, k) = 0.0
5928  END DO
5929  END DO
5930  DO k=2,km,1
5931  DO i=ie,is,-1
5932  CALL poprealarray(pp(i, k))
5933  gam_ad(i, k) = gam_ad(i, k) - pp(i, k+1)*pp_ad(i, k)
5934  pp_ad(i, k+1) = pp_ad(i, k+1) - gam(i, k)*pp_ad(i, k)
5935  END DO
5936  END DO
5937  dd_ad = 0.0
5938  DO k=km,2,-1
5939  DO i=ie,is,-1
5940  CALL poprealarray(pp(i, k+1))
5941  temp_ad3 = pp_ad(i, k+1)/bet(i)
5942  dd_ad(i, k) = dd_ad(i, k) + temp_ad3
5943  pp_ad(i, k) = pp_ad(i, k) - temp_ad3
5944  bet_ad(i) = bet_ad(i) - (dd(i, k)-pp(i, k))*temp_ad3/bet(i)
5945  pp_ad(i, k+1) = 0.0
5946  CALL poprealarray(bet(i))
5947  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
5948  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
5949  temp_ad4 = gam_ad(i, k)/bet(i)
5950  bet_ad(i) = -(g_rat(i, k-1)*temp_ad4/bet(i))
5951  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad4
5952  gam_ad(i, k) = 0.0
5953  END DO
5954  END DO
5955  DO i=ie,is,-1
5956  CALL poprealarray(dd(i, km))
5957  pe2_ad(i, km) = pe2_ad(i, km) + 3.*dd_ad(i, km)
5958  dd_ad(i, km) = 0.0
5959  bb_ad(i, km) = 0.0
5960  temp_ad2 = pp_ad(i, 2)/bet(i)
5961  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad2
5962  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad2/bet(i)
5963  pp_ad(i, 2) = 0.0
5964  pp_ad(i, 1) = 0.0
5965  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
5966  bet_ad(i) = 0.0
5967  END DO
5968  DO k=km-1,1,-1
5969  DO i=ie,is,-1
5970  temp_ad0 = 3.*dd_ad(i, k)
5971  pe2_ad(i, k) = pe2_ad(i, k) + temp_ad0
5972  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pe2(i, k+1)*&
5973 & temp_ad0
5974  pe2_ad(i, k+1) = pe2_ad(i, k+1) + g_rat(i, k)*temp_ad0
5975  dd_ad(i, k) = 0.0
5976  bb_ad(i, k) = 0.0
5977  temp_ad1 = g_rat_ad(i, k)/dm2(i, k+1)
5978  dm2_ad(i, k) = dm2_ad(i, k) + temp_ad1
5979  dm2_ad(i, k+1) = dm2_ad(i, k+1) - dm2(i, k)*temp_ad1/dm2(i, k+1)
5980  g_rat_ad(i, k) = 0.0
5981  END DO
5982  END DO
5983  DO k=km,1,-1
5984  DO i=ie,is,-1
5985  CALL poprealarray(pe2(i, k))
5986  temp1 = dz2(i, k)
5987  temp0 = dm2(i, k)*pt2(i, k)
5988  temp = temp0/temp1
5989  temp_ad = gama*exp(gama*log(-(rgas*temp)))*pe2_ad(i, k)/(temp*&
5990 & temp1)
5991  dm2_ad(i, k) = dm2_ad(i, k) + pt2(i, k)*temp_ad
5992  pt2_ad(i, k) = pt2_ad(i, k) + dm2(i, k)*temp_ad
5993  dz2_ad(i, k) = dz2_ad(i, k) - temp*temp_ad
5994  pm2_ad(i, k) = pm2_ad(i, k) - pe2_ad(i, k)
5995  pe2_ad(i, k) = 0.0
5996  w2_ad(i, k) = w2_ad(i, k) + w1_ad(i, k)
5997  w1_ad(i, k) = 0.0
5998  END DO
5999  END DO
6000  END SUBROUTINE sim_solver_bwd
6001  SUBROUTINE sim_solver(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe2&
6002 & , dm2, pm2, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
6003  IMPLICIT NONE
6004  INTEGER, INTENT(IN) :: is, ie, km
6005  REAL, INTENT(IN) :: dt, rgas, gama, kappa, p_fac, alpha, scale_m
6006  REAL, DIMENSION(is:ie, km), INTENT(IN) :: dm2, pt2, pm2, gm2, cp2
6007  REAL, INTENT(IN) :: ws(is:ie)
6008  REAL, DIMENSION(is:ie, km+1), INTENT(IN) :: pem
6009  REAL, INTENT(OUT) :: pe2(is:ie, km+1)
6010  REAL, DIMENSION(is:ie, km), INTENT(INOUT) :: dz2, w2
6011 ! Local
6012  REAL, DIMENSION(is:ie, km) :: aa, bb, dd, w1, wk, g_rat, gam
6013  REAL, DIMENSION(is:ie, km+1) :: pp
6014  REAL, DIMENSION(is:ie) :: p1, wk1, bet
6015  REAL :: beta, t2, t1g, rdt, ra, capa1
6016  INTEGER :: i, k
6017  INTRINSIC log
6018  INTRINSIC exp
6019  INTRINSIC max
6020  REAL :: max1
6021  REAL :: max2
6022  beta = 1. - alpha
6023  ra = 1./alpha
6024  t2 = beta/alpha
6025  t1g = 2.*gama*(alpha*dt)**2
6026  rdt = 1./dt
6027  capa1 = kappa - 1.
6028  DO k=1,km
6029  DO i=is,ie
6030  w1(i, k) = w2(i, k)
6031 ! P_g perturbation
6032  pe2(i, k) = exp(gama*log(-(dm2(i, k)/dz2(i, k)*rgas*pt2(i, k))))&
6033 & - pm2(i, k)
6034  END DO
6035  END DO
6036  DO k=1,km-1
6037  DO i=is,ie
6038  g_rat(i, k) = dm2(i, k)/dm2(i, k+1)
6039  bb(i, k) = 2.*(1.+g_rat(i, k))
6040  dd(i, k) = 3.*(pe2(i, k)+g_rat(i, k)*pe2(i, k+1))
6041  END DO
6042  END DO
6043  DO i=is,ie
6044  bet(i) = bb(i, 1)
6045  pp(i, 1) = 0.
6046  pp(i, 2) = dd(i, 1)/bet(i)
6047  bb(i, km) = 2.
6048  dd(i, km) = 3.*pe2(i, km)
6049  END DO
6050  DO k=2,km
6051  DO i=is,ie
6052  gam(i, k) = g_rat(i, k-1)/bet(i)
6053  bet(i) = bb(i, k) - gam(i, k)
6054  pp(i, k+1) = (dd(i, k)-pp(i, k))/bet(i)
6055  END DO
6056  END DO
6057  DO k=km,2,-1
6058  DO i=is,ie
6059  pp(i, k) = pp(i, k) - gam(i, k)*pp(i, k+1)
6060  END DO
6061  END DO
6062  DO k=1,km+1
6063  DO i=is,ie
6064 ! pe2 is Full p
6065  pe2(i, k) = pem(i, k) + pp(i, k)
6066  END DO
6067  END DO
6068  DO k=2,km
6069  DO i=is,ie
6070  aa(i, k) = t1g/(dz2(i, k-1)+dz2(i, k))*pe2(i, k)
6071  wk(i, k) = t2*aa(i, k)*(w1(i, k-1)-w1(i, k))
6072  aa(i, k) = aa(i, k) - scale_m*dm2(i, 1)
6073  END DO
6074  END DO
6075 ! Top:
6076  DO i=is,ie
6077  bet(i) = dm2(i, 1) - aa(i, 2)
6078  w2(i, 1) = (dm2(i, 1)*w1(i, 1)+dt*pp(i, 2)+wk(i, 2))/bet(i)
6079  END DO
6080 ! Interior:
6081  DO k=2,km-1
6082  DO i=is,ie
6083  gam(i, k) = aa(i, k)/bet(i)
6084  bet(i) = dm2(i, k) - (aa(i, k)+aa(i, k+1)+aa(i, k)*gam(i, k))
6085  w2(i, k) = (dm2(i, k)*w1(i, k)+dt*(pp(i, k+1)-pp(i, k))+wk(i, k+&
6086 & 1)-wk(i, k)-aa(i, k)*w2(i, k-1))/bet(i)
6087  END DO
6088  END DO
6089 ! Bottom: k=km
6090  DO i=is,ie
6091  wk1(i) = t1g/dz2(i, km)*pe2(i, km+1)
6092  gam(i, km) = aa(i, km)/bet(i)
6093  bet(i) = dm2(i, km) - (aa(i, km)+wk1(i)+aa(i, km)*gam(i, km))
6094  w2(i, km) = (dm2(i, km)*w1(i, km)+dt*(pp(i, km+1)-pp(i, km))-wk(i&
6095 & , km)+wk1(i)*(t2*w1(i, km)-ra*ws(i))-aa(i, km)*w2(i, km-1))/bet(&
6096 & i)
6097  END DO
6098  DO k=km-1,1,-1
6099  DO i=is,ie
6100  w2(i, k) = w2(i, k) - gam(i, k+1)*w2(i, k+1)
6101  END DO
6102  END DO
6103  DO i=is,ie
6104  pe2(i, 1) = 0.
6105  END DO
6106  DO k=1,km
6107  DO i=is,ie
6108  pe2(i, k+1) = pe2(i, k) + (dm2(i, k)*(w2(i, k)-w1(i, k))*rdt-&
6109 & beta*(pp(i, k+1)-pp(i, k)))*ra
6110  END DO
6111  END DO
6112  DO i=is,ie
6113  p1(i) = (pe2(i, km)+2.*pe2(i, km+1))*r3
6114  IF (p_fac*pm2(i, km) .LT. p1(i) + pm2(i, km)) THEN
6115  max1 = p1(i) + pm2(i, km)
6116  ELSE
6117  max1 = p_fac*pm2(i, km)
6118  END IF
6119  dz2(i, km) = -(dm2(i, km)*rgas*pt2(i, km)*exp(capa1*log(max1)))
6120  END DO
6121  DO k=km-1,1,-1
6122  DO i=is,ie
6123  p1(i) = (pe2(i, k)+bb(i, k)*pe2(i, k+1)+g_rat(i, k)*pe2(i, k+2))&
6124 & *r3 - g_rat(i, k)*p1(i)
6125  IF (p_fac*pm2(i, k) .LT. p1(i) + pm2(i, k)) THEN
6126  max2 = p1(i) + pm2(i, k)
6127  ELSE
6128  max2 = p_fac*pm2(i, k)
6129  END IF
6130 ! delz = -dm*R*T_m / p_gas
6131  dz2(i, k) = -(dm2(i, k)*rgas*pt2(i, k)*exp(capa1*log(max2)))
6132  END DO
6133  END DO
6134  DO k=1,km+1
6135  DO i=is,ie
6136  pe2(i, k) = pe2(i, k) + beta*(pp(i, k)-pe2(i, k))
6137  END DO
6138  END DO
6139  END SUBROUTINE sim_solver
6140  SUBROUTINE edge_scalar(q1, qe, i1, i2, km, id)
6141  IMPLICIT NONE
6142 ! Optimized for wind profile reconstruction:
6143  INTEGER, INTENT(IN) :: i1, i2, km
6144 ! 0: pp 1: wind
6145  INTEGER, INTENT(IN) :: id
6146  REAL, DIMENSION(i1:i2, km), INTENT(IN) :: q1
6147  REAL, DIMENSION(i1:i2, km+1), INTENT(OUT) :: qe
6148 !-----------------------------------------------------------------------
6149  REAL, PARAMETER :: r2o3=2./3.
6150  REAL, PARAMETER :: r4o3=4./3.
6151  REAL :: gak(km)
6152  REAL :: bet
6153  INTEGER :: i, k
6154 !------------------------------------------------
6155 ! Optimized coding for uniform grid: SJL Apr 2007
6156 !------------------------------------------------
6157  IF (id .EQ. 1) THEN
6158  DO i=i1,i2
6159  qe(i, 1) = r4o3*q1(i, 1) + r2o3*q1(i, 2)
6160  END DO
6161  ELSE
6162  DO i=i1,i2
6163  qe(i, 1) = 1.e30
6164  END DO
6165  END IF
6166  gak(1) = 7./3.
6167  DO k=2,km
6168  gak(k) = 1./(4.-gak(k-1))
6169  DO i=i1,i2
6170  qe(i, k) = (3.*(q1(i, k-1)+q1(i, k))-qe(i, k-1))*gak(k)
6171  END DO
6172  END DO
6173  bet = 1./(1.5-3.5*gak(km))
6174  DO i=i1,i2
6175  qe(i, km+1) = (4.*q1(i, km)+q1(i, km-1)-3.5*qe(i, km))*bet
6176  END DO
6177  DO k=km,1,-1
6178  DO i=i1,i2
6179  qe(i, k) = qe(i, k) - gak(k)*qe(i, k+1)
6180  END DO
6181  END DO
6182  END SUBROUTINE edge_scalar
6183 ! Differentiation of edge_profile in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
6184 !_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
6185 !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
6186 !.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
6187 !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
6188 !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.
6189 !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
6190 ! 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
6191 !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
6192 !_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
6193 !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
6194 !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_
6195 !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
6196 !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.
6197 !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_
6198 !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
6199 !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
6200 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
6201 ! gradient of useful results: q1e q2e q1 q2
6202 ! with respect to varying inputs: q1e q2e q1 q2
6203  SUBROUTINE edge_profile_fwd(q1, q2, q1e, q2e, i1, i2, j1, j2, j, km, &
6204 & dp0, uniform_grid, limiter)
6205  IMPLICIT NONE
6206 ! Optimized for wind profile reconstruction:
6207  INTEGER, INTENT(IN) :: i1, i2, j1, j2
6208  INTEGER, INTENT(IN) :: j, km
6209  INTEGER, INTENT(IN) :: limiter
6210  LOGICAL, INTENT(IN) :: uniform_grid
6211  REAL, INTENT(IN) :: dp0(km)
6212  REAL, DIMENSION(i1:i2, j1:j2, km), INTENT(IN) :: q1, q2
6213  REAL, DIMENSION(i1:i2, j1:j2, km+1) :: q1e, q2e
6214 !-----------------------------------------------------------------------
6215 ! edge values
6216  REAL, DIMENSION(i1:i2, km+1) :: qe1, qe2, gam
6217  REAL :: gak(km)
6218  REAL :: bet, r2o3, r4o3
6219  REAL :: g0, gk, xt1, xt2, a_bot
6220  INTEGER :: i, k
6221 
6222  qe1 = 0.0
6223  qe2 = 0.0
6224  gam = 0.0
6225  gak = 0.0
6226  bet = 0.0
6227  r2o3 = 0.0
6228  r4o3 = 0.0
6229  g0 = 0.0
6230  gk = 0.0
6231  xt1 = 0.0
6232  xt2 = 0.0
6233  a_bot = 0.0
6234 
6235  IF (uniform_grid) THEN
6236 !------------------------------------------------
6237 ! Optimized coding for uniform grid: SJL Apr 2007
6238 !------------------------------------------------
6239  r2o3 = 2./3.
6240  r4o3 = 4./3.
6241  DO i=i1,i2
6242  qe1(i, 1) = r4o3*q1(i, j, 1) + r2o3*q1(i, j, 2)
6243  qe2(i, 1) = r4o3*q2(i, j, 1) + r2o3*q2(i, j, 2)
6244  END DO
6245  gak(1) = 7./3.
6246  DO k=2,km
6247  CALL pushrealarray(gak(k))
6248  gak(k) = 1./(4.-gak(k-1))
6249  DO i=i1,i2
6250  qe1(i, k) = (3.*(q1(i, j, k-1)+q1(i, j, k))-qe1(i, k-1))*gak(k&
6251 & )
6252  qe2(i, k) = (3.*(q2(i, j, k-1)+q2(i, j, k))-qe2(i, k-1))*gak(k&
6253 & )
6254  END DO
6255  END DO
6256  bet = 1./(1.5-3.5*gak(km))
6257  DO i=i1,i2
6258  qe1(i, km+1) = (4.*q1(i, j, km)+q1(i, j, km-1)-3.5*qe1(i, km))*&
6259 & bet
6260  qe2(i, km+1) = (4.*q2(i, j, km)+q2(i, j, km-1)-3.5*qe2(i, km))*&
6261 & bet
6262  END DO
6263  DO k=km,1,-1
6264  DO i=i1,i2
6265  qe1(i, k) = qe1(i, k) - gak(k)*qe1(i, k+1)
6266  qe2(i, k) = qe2(i, k) - gak(k)*qe2(i, k+1)
6267  END DO
6268  END DO
6269  CALL pushcontrol(1,0)
6270  ELSE
6271 ! Assuming grid varying in vertical only
6272  g0 = dp0(2)/dp0(1)
6273  xt1 = 2.*g0*(g0+1.)
6274  bet = g0*(g0+0.5)
6275  DO i=i1,i2
6276  qe1(i, 1) = (xt1*q1(i, j, 1)+q1(i, j, 2))/bet
6277  qe2(i, 1) = (xt1*q2(i, j, 1)+q2(i, j, 2))/bet
6278  gam(i, 1) = (1.+g0*(g0+1.5))/bet
6279  END DO
6280  DO k=2,km
6281  CALL pushrealarray(gk)
6282  gk = dp0(k-1)/dp0(k)
6283  DO i=i1,i2
6284  CALL pushrealarray(bet)
6285  bet = 2. + 2.*gk - gam(i, k-1)
6286  qe1(i, k) = (3.*(q1(i, j, k-1)+gk*q1(i, j, k))-qe1(i, k-1))/&
6287 & bet
6288  qe2(i, k) = (3.*(q2(i, j, k-1)+gk*q2(i, j, k))-qe2(i, k-1))/&
6289 & bet
6290  gam(i, k) = gk/bet
6291  END DO
6292  END DO
6293  a_bot = 1. + gk*(gk+1.5)
6294  CALL pushrealarray(xt1)
6295  xt1 = 2.*gk*(gk+1.)
6296  DO i=i1,i2
6297  CALL pushrealarray(xt2)
6298  xt2 = gk*(gk+0.5) - a_bot*gam(i, km)
6299  qe1(i, km+1) = (xt1*q1(i, j, km)+q1(i, j, km-1)-a_bot*qe1(i, km)&
6300 & )/xt2
6301  qe2(i, km+1) = (xt1*q2(i, j, km)+q2(i, j, km-1)-a_bot*qe2(i, km)&
6302 & )/xt2
6303  END DO
6304  DO k=km,1,-1
6305  DO i=i1,i2
6306  qe1(i, k) = qe1(i, k) - gam(i, k)*qe1(i, k+1)
6307  qe2(i, k) = qe2(i, k) - gam(i, k)*qe2(i, k+1)
6308  END DO
6309  END DO
6310  CALL pushcontrol(1,1)
6311  END IF
6312 !------------------
6313 ! Apply constraints
6314 !------------------
6315  IF (limiter .NE. 0) THEN
6316 ! limit the top & bottom winds
6317  DO i=i1,i2
6318 ! Top
6319  IF (q1(i, j, 1)*qe1(i, 1) .LT. 0.) THEN
6320  qe1(i, 1) = 0.
6321  CALL pushcontrol(1,0)
6322  ELSE
6323  CALL pushcontrol(1,1)
6324  END IF
6325  IF (q2(i, j, 1)*qe2(i, 1) .LT. 0.) THEN
6326  qe2(i, 1) = 0.
6327  CALL pushcontrol(1,0)
6328  ELSE
6329  CALL pushcontrol(1,1)
6330  END IF
6331 ! Surface:
6332  IF (q1(i, j, km)*qe1(i, km+1) .LT. 0.) THEN
6333  qe1(i, km+1) = 0.
6334  CALL pushcontrol(1,0)
6335  ELSE
6336  CALL pushcontrol(1,1)
6337  END IF
6338  IF (q2(i, j, km)*qe2(i, km+1) .LT. 0.) THEN
6339  qe2(i, km+1) = 0.
6340  CALL pushcontrol(1,1)
6341  ELSE
6342  CALL pushcontrol(1,0)
6343  END IF
6344  END DO
6345  CALL pushcontrol(1,1)
6346  ELSE
6347  CALL pushcontrol(1,0)
6348  END IF
6349  DO k=1,km+1
6350  DO i=i1,i2
6351  q1e(i, j, k) = qe1(i, k)
6352  q2e(i, j, k) = qe2(i, k)
6353  END DO
6354  END DO
6355  CALL pushrealarray(gam, (i2-i1+1)*(km+1))
6356  CALL pushrealarray(gak, km)
6357  CALL pushrealarray(xt2)
6358  CALL pushrealarray(xt1)
6359  CALL pushrealarray(bet)
6360  CALL pushrealarray(a_bot)
6361  CALL pushrealarray(gk)
6362  END SUBROUTINE edge_profile_fwd
6363 ! Differentiation of edge_profile in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edg
6364 !e_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_co
6365 !re_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_mo
6366 !d.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayle
6367 !igh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_
6368 !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_mod
6369 !.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_2
6370 !d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limite
6371 !rs 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 f
6372 !v_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_sub
6373 !grid_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_uti
6374 !ls_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
6375 !_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_
6376 !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_mod
6377 !.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
6378 !_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_co
6379 !re_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_ut
6380 !ils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
6381 ! gradient of useful results: q1e q2e q1 q2
6382 ! with respect to varying inputs: q1e q2e q1 q2
6383  SUBROUTINE edge_profile_bwd(q1, q1_ad, q2, q2_ad, q1e, q1e_ad, q2e, &
6384 & q2e_ad, i1, i2, j1, j2, j, km, dp0, uniform_grid, limiter)
6385  IMPLICIT NONE
6386  INTEGER, INTENT(IN) :: i1, i2, j1, j2
6387  INTEGER, INTENT(IN) :: j, km
6388  INTEGER, INTENT(IN) :: limiter
6389  LOGICAL, INTENT(IN) :: uniform_grid
6390  REAL, INTENT(IN) :: dp0(km)
6391  REAL, DIMENSION(i1:i2, j1:j2, km), INTENT(IN) :: q1, q2
6392  REAL, DIMENSION(i1:i2, j1:j2, km) :: q1_ad, q2_ad
6393  REAL, DIMENSION(i1:i2, j1:j2, km+1) :: q1e, q2e
6394  REAL, DIMENSION(i1:i2, j1:j2, km+1) :: q1e_ad, q2e_ad
6395  REAL, DIMENSION(i1:i2, km+1) :: qe1, qe2, gam
6396  REAL, DIMENSION(i1:i2, km+1) :: qe1_ad, qe2_ad
6397  REAL :: gak(km)
6398  REAL :: bet, r2o3, r4o3
6399  REAL :: g0, gk, xt1, xt2, a_bot
6400  INTEGER :: i, k
6401  REAL :: temp_ad
6402  REAL :: temp_ad0
6403  REAL :: temp_ad1
6404  REAL :: temp_ad2
6405  REAL :: temp_ad3
6406  REAL :: temp_ad4
6407  REAL :: temp_ad5
6408  REAL :: temp_ad6
6409  INTEGER :: branch
6410 
6411  qe1 = 0.0
6412  qe2 = 0.0
6413  gam = 0.0
6414  gak = 0.0
6415  bet = 0.0
6416  r2o3 = 0.0
6417  r4o3 = 0.0
6418  g0 = 0.0
6419  gk = 0.0
6420  xt1 = 0.0
6421  xt2 = 0.0
6422  a_bot = 0.0
6423  branch = 0
6424 
6425  CALL poprealarray(gk)
6426  CALL poprealarray(a_bot)
6427  CALL poprealarray(bet)
6428  CALL poprealarray(xt1)
6429  CALL poprealarray(xt2)
6430  CALL poprealarray(gak, km)
6431  CALL poprealarray(gam, (i2-i1+1)*(km+1))
6432  qe1_ad = 0.0
6433  qe2_ad = 0.0
6434  DO k=km+1,1,-1
6435  DO i=i2,i1,-1
6436  qe2_ad(i, k) = qe2_ad(i, k) + q2e_ad(i, j, k)
6437  q2e_ad(i, j, k) = 0.0
6438  qe1_ad(i, k) = qe1_ad(i, k) + q1e_ad(i, j, k)
6439  q1e_ad(i, j, k) = 0.0
6440  END DO
6441  END DO
6442  CALL popcontrol(1,branch)
6443  IF (branch .NE. 0) THEN
6444  DO i=i2,i1,-1
6445  CALL popcontrol(1,branch)
6446  IF (branch .NE. 0) qe2_ad(i, km+1) = 0.0
6447  CALL popcontrol(1,branch)
6448  IF (branch .EQ. 0) qe1_ad(i, km+1) = 0.0
6449  CALL popcontrol(1,branch)
6450  IF (branch .EQ. 0) qe2_ad(i, 1) = 0.0
6451  CALL popcontrol(1,branch)
6452  IF (branch .EQ. 0) qe1_ad(i, 1) = 0.0
6453  END DO
6454  END IF
6455  CALL popcontrol(1,branch)
6456  IF (branch .EQ. 0) THEN
6457  DO k=1,km,1
6458  DO i=i2,i1,-1
6459  qe2_ad(i, k+1) = qe2_ad(i, k+1) - gak(k)*qe2_ad(i, k)
6460  qe1_ad(i, k+1) = qe1_ad(i, k+1) - gak(k)*qe1_ad(i, k)
6461  END DO
6462  END DO
6463  DO i=i2,i1,-1
6464  temp_ad1 = bet*qe2_ad(i, km+1)
6465  q2_ad(i, j, km) = q2_ad(i, j, km) + 4.*temp_ad1
6466  q2_ad(i, j, km-1) = q2_ad(i, j, km-1) + temp_ad1
6467  qe2_ad(i, km) = qe2_ad(i, km) - 3.5*temp_ad1
6468  qe2_ad(i, km+1) = 0.0
6469  temp_ad2 = bet*qe1_ad(i, km+1)
6470  q1_ad(i, j, km) = q1_ad(i, j, km) + 4.*temp_ad2
6471  q1_ad(i, j, km-1) = q1_ad(i, j, km-1) + temp_ad2
6472  qe1_ad(i, km) = qe1_ad(i, km) - 3.5*temp_ad2
6473  qe1_ad(i, km+1) = 0.0
6474  END DO
6475  DO k=km,2,-1
6476  DO i=i2,i1,-1
6477  temp_ad = gak(k)*qe2_ad(i, k)
6478  q2_ad(i, j, k-1) = q2_ad(i, j, k-1) + 3.*temp_ad
6479  q2_ad(i, j, k) = q2_ad(i, j, k) + 3.*temp_ad
6480  qe2_ad(i, k-1) = qe2_ad(i, k-1) - temp_ad
6481  qe2_ad(i, k) = 0.0
6482  temp_ad0 = gak(k)*qe1_ad(i, k)
6483  q1_ad(i, j, k-1) = q1_ad(i, j, k-1) + 3.*temp_ad0
6484  q1_ad(i, j, k) = q1_ad(i, j, k) + 3.*temp_ad0
6485  qe1_ad(i, k-1) = qe1_ad(i, k-1) - temp_ad0
6486  qe1_ad(i, k) = 0.0
6487  END DO
6488  CALL poprealarray(gak(k))
6489  END DO
6490  r2o3 = 2./3.
6491  r4o3 = 4./3.
6492  DO i=i2,i1,-1
6493  q2_ad(i, j, 1) = q2_ad(i, j, 1) + r4o3*qe2_ad(i, 1)
6494  q2_ad(i, j, 2) = q2_ad(i, j, 2) + r2o3*qe2_ad(i, 1)
6495  qe2_ad(i, 1) = 0.0
6496  q1_ad(i, j, 1) = q1_ad(i, j, 1) + r4o3*qe1_ad(i, 1)
6497  q1_ad(i, j, 2) = q1_ad(i, j, 2) + r2o3*qe1_ad(i, 1)
6498  qe1_ad(i, 1) = 0.0
6499  END DO
6500  ELSE
6501  DO k=1,km,1
6502  DO i=i2,i1,-1
6503  qe2_ad(i, k+1) = qe2_ad(i, k+1) - gam(i, k)*qe2_ad(i, k)
6504  qe1_ad(i, k+1) = qe1_ad(i, k+1) - gam(i, k)*qe1_ad(i, k)
6505  END DO
6506  END DO
6507  DO i=i2,i1,-1
6508  temp_ad5 = qe2_ad(i, km+1)/xt2
6509  q2_ad(i, j, km) = q2_ad(i, j, km) + xt1*temp_ad5
6510  q2_ad(i, j, km-1) = q2_ad(i, j, km-1) + temp_ad5
6511  qe2_ad(i, km) = qe2_ad(i, km) - a_bot*temp_ad5
6512  qe2_ad(i, km+1) = 0.0
6513  temp_ad6 = qe1_ad(i, km+1)/xt2
6514  q1_ad(i, j, km) = q1_ad(i, j, km) + xt1*temp_ad6
6515  q1_ad(i, j, km-1) = q1_ad(i, j, km-1) + temp_ad6
6516  qe1_ad(i, km) = qe1_ad(i, km) - a_bot*temp_ad6
6517  qe1_ad(i, km+1) = 0.0
6518  CALL poprealarray(xt2)
6519  END DO
6520  CALL poprealarray(xt1)
6521  DO k=km,2,-1
6522  DO i=i2,i1,-1
6523  temp_ad3 = qe2_ad(i, k)/bet
6524  q2_ad(i, j, k-1) = q2_ad(i, j, k-1) + 3.*temp_ad3
6525  q2_ad(i, j, k) = q2_ad(i, j, k) + 3.*gk*temp_ad3
6526  qe2_ad(i, k-1) = qe2_ad(i, k-1) - temp_ad3
6527  qe2_ad(i, k) = 0.0
6528  temp_ad4 = qe1_ad(i, k)/bet
6529  q1_ad(i, j, k-1) = q1_ad(i, j, k-1) + 3.*temp_ad4
6530  q1_ad(i, j, k) = q1_ad(i, j, k) + 3.*gk*temp_ad4
6531  qe1_ad(i, k-1) = qe1_ad(i, k-1) - temp_ad4
6532  qe1_ad(i, k) = 0.0
6533  CALL poprealarray(bet)
6534  END DO
6535  CALL poprealarray(gk)
6536  END DO
6537  DO i=i2,i1,-1
6538  q2_ad(i, j, 1) = q2_ad(i, j, 1) + xt1*qe2_ad(i, 1)/bet
6539  q2_ad(i, j, 2) = q2_ad(i, j, 2) + qe2_ad(i, 1)/bet
6540  qe2_ad(i, 1) = 0.0
6541  q1_ad(i, j, 1) = q1_ad(i, j, 1) + xt1*qe1_ad(i, 1)/bet
6542  q1_ad(i, j, 2) = q1_ad(i, j, 2) + qe1_ad(i, 1)/bet
6543  qe1_ad(i, 1) = 0.0
6544  END DO
6545  END IF
6546  END SUBROUTINE edge_profile_bwd
6547  SUBROUTINE edge_profile(q1, q2, q1e, q2e, i1, i2, j1, j2, j, km, dp0, &
6548 & uniform_grid, limiter)
6549  IMPLICIT NONE
6550 ! Optimized for wind profile reconstruction:
6551  INTEGER, INTENT(IN) :: i1, i2, j1, j2
6552  INTEGER, INTENT(IN) :: j, km
6553  INTEGER, INTENT(IN) :: limiter
6554  LOGICAL, INTENT(IN) :: uniform_grid
6555  REAL, INTENT(IN) :: dp0(km)
6556  REAL, DIMENSION(i1:i2, j1:j2, km), INTENT(IN) :: q1, q2
6557  REAL, DIMENSION(i1:i2, j1:j2, km+1), INTENT(OUT) :: q1e, q2e
6558 !-----------------------------------------------------------------------
6559 ! edge values
6560  REAL, DIMENSION(i1:i2, km+1) :: qe1, qe2, gam
6561  REAL :: gak(km)
6562  REAL :: bet, r2o3, r4o3
6563  REAL :: g0, gk, xt1, xt2, a_bot
6564  INTEGER :: i, k
6565  IF (uniform_grid) THEN
6566 !------------------------------------------------
6567 ! Optimized coding for uniform grid: SJL Apr 2007
6568 !------------------------------------------------
6569  r2o3 = 2./3.
6570  r4o3 = 4./3.
6571  DO i=i1,i2
6572  qe1(i, 1) = r4o3*q1(i, j, 1) + r2o3*q1(i, j, 2)
6573  qe2(i, 1) = r4o3*q2(i, j, 1) + r2o3*q2(i, j, 2)
6574  END DO
6575  gak(1) = 7./3.
6576  DO k=2,km
6577  gak(k) = 1./(4.-gak(k-1))
6578  DO i=i1,i2
6579  qe1(i, k) = (3.*(q1(i, j, k-1)+q1(i, j, k))-qe1(i, k-1))*gak(k&
6580 & )
6581  qe2(i, k) = (3.*(q2(i, j, k-1)+q2(i, j, k))-qe2(i, k-1))*gak(k&
6582 & )
6583  END DO
6584  END DO
6585  bet = 1./(1.5-3.5*gak(km))
6586  DO i=i1,i2
6587  qe1(i, km+1) = (4.*q1(i, j, km)+q1(i, j, km-1)-3.5*qe1(i, km))*&
6588 & bet
6589  qe2(i, km+1) = (4.*q2(i, j, km)+q2(i, j, km-1)-3.5*qe2(i, km))*&
6590 & bet
6591  END DO
6592  DO k=km,1,-1
6593  DO i=i1,i2
6594  qe1(i, k) = qe1(i, k) - gak(k)*qe1(i, k+1)
6595  qe2(i, k) = qe2(i, k) - gak(k)*qe2(i, k+1)
6596  END DO
6597  END DO
6598  ELSE
6599 ! Assuming grid varying in vertical only
6600  g0 = dp0(2)/dp0(1)
6601  xt1 = 2.*g0*(g0+1.)
6602  bet = g0*(g0+0.5)
6603  DO i=i1,i2
6604  qe1(i, 1) = (xt1*q1(i, j, 1)+q1(i, j, 2))/bet
6605  qe2(i, 1) = (xt1*q2(i, j, 1)+q2(i, j, 2))/bet
6606  gam(i, 1) = (1.+g0*(g0+1.5))/bet
6607  END DO
6608  DO k=2,km
6609  gk = dp0(k-1)/dp0(k)
6610  DO i=i1,i2
6611  bet = 2. + 2.*gk - gam(i, k-1)
6612  qe1(i, k) = (3.*(q1(i, j, k-1)+gk*q1(i, j, k))-qe1(i, k-1))/&
6613 & bet
6614  qe2(i, k) = (3.*(q2(i, j, k-1)+gk*q2(i, j, k))-qe2(i, k-1))/&
6615 & bet
6616  gam(i, k) = gk/bet
6617  END DO
6618  END DO
6619  a_bot = 1. + gk*(gk+1.5)
6620  xt1 = 2.*gk*(gk+1.)
6621  DO i=i1,i2
6622  xt2 = gk*(gk+0.5) - a_bot*gam(i, km)
6623  qe1(i, km+1) = (xt1*q1(i, j, km)+q1(i, j, km-1)-a_bot*qe1(i, km)&
6624 & )/xt2
6625  qe2(i, km+1) = (xt1*q2(i, j, km)+q2(i, j, km-1)-a_bot*qe2(i, km)&
6626 & )/xt2
6627  END DO
6628  DO k=km,1,-1
6629  DO i=i1,i2
6630  qe1(i, k) = qe1(i, k) - gam(i, k)*qe1(i, k+1)
6631  qe2(i, k) = qe2(i, k) - gam(i, k)*qe2(i, k+1)
6632  END DO
6633  END DO
6634  END IF
6635 !------------------
6636 ! Apply constraints
6637 !------------------
6638  IF (limiter .NE. 0) THEN
6639 ! limit the top & bottom winds
6640  DO i=i1,i2
6641 ! Top
6642  IF (q1(i, j, 1)*qe1(i, 1) .LT. 0.) qe1(i, 1) = 0.
6643  IF (q2(i, j, 1)*qe2(i, 1) .LT. 0.) qe2(i, 1) = 0.
6644 ! Surface:
6645  IF (q1(i, j, km)*qe1(i, km+1) .LT. 0.) qe1(i, km+1) = 0.
6646  IF (q2(i, j, km)*qe2(i, km+1) .LT. 0.) qe2(i, km+1) = 0.
6647  END DO
6648  END IF
6649  DO k=1,km+1
6650  DO i=i1,i2
6651  q1e(i, j, k) = qe1(i, k)
6652  q2e(i, j, k) = qe2(i, k)
6653  END DO
6654  END DO
6655  END SUBROUTINE edge_profile
6656 ! Differentiation of nest_halo_nh in reverse (adjoint) mode, forward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edge
6657 !_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
6658 !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
6659 !.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
6660 !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
6661 !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.
6662 !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
6663 ! 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
6664 !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
6665 !_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
6666 !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
6667 !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_
6668 !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
6669 !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.
6670 !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_
6671 !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
6672 !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
6673 !ls_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
6674 ! gradient of useful results: pk3 gz delp delz pkc pt
6675 ! with respect to varying inputs: pk3 gz delp delz pkc pt
6676  SUBROUTINE nest_halo_nh_fwd(ptop, grav, kappa, cp, delp, delz, pt, &
6677 & phis, pkc, gz, pk3, npx, npy, npz, nested, pkc_pertn, computepk3, &
6678 & fullhalo, bd)
6679  IMPLICIT NONE
6680 !INPUT: delp, delz, pt
6681 !OUTPUT: gz, pkc, pk3 (optional)
6682  INTEGER, INTENT(IN) :: npx, npy, npz
6683  LOGICAL, INTENT(IN) :: pkc_pertn, computepk3, fullhalo, nested
6684  REAL, INTENT(IN) :: ptop, kappa, cp, grav
6685  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
6686  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
6687  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(IN) :: pt&
6688 & , delp, delz
6689  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1), INTENT(INOUT) &
6690 & :: gz, pkc, pk3
6691  INTEGER :: i, j, k
6692 !'gamma'
6693  REAL :: gama
6694  REAL :: ptk, rgrav, rkap, peln1, rdg
6695  REAL, DIMENSION(bd%isd:bd%ied, npz+1, bd%jsd:bd%jed) :: pe, peln
6696  REAL, DIMENSION(bd%isd:bd%ied, npz) :: gam, bb, dd, pkz
6697  REAL, DIMENSION(bd%isd:bd%ied, npz-1) :: g_rat
6698  REAL, DIMENSION(bd%isd:bd%ied) :: bet
6699  REAL :: pm
6700  INTEGER :: ifirst, ilast, jfirst, jlast
6701  INTEGER :: is, ie, js, je
6702  INTEGER :: isd, ied, jsd, jed
6703  INTRINSIC log
6704  INTRINSIC exp
6705 
6706  gama = 0.0
6707  ptk = 0.0
6708  rgrav = 0.0
6709  rkap = 0.0
6710  peln1 = 0.0
6711  rdg = 0.0
6712  pe = 0.0
6713  peln = 0.0
6714  gam = 0.0
6715  bb = 0.0
6716  dd = 0.0
6717  pkz = 0.0
6718  g_rat = 0.0
6719  bet = 0.0
6720  pm = 0.0
6721  ifirst = 0
6722  ilast = 0
6723  jfirst = 0
6724  jlast = 0
6725  is = 0
6726  js = 0
6727  je = 0
6728  isd = 0
6729  ied = 0
6730  jsd = 0
6731  jed = 0
6732 
6733  is = bd%is
6734  ie = bd%ie
6735  js = bd%js
6736  je = bd%je
6737  isd = bd%isd
6738  ied = bd%ied
6739  jsd = bd%jsd
6740  jed = bd%jed
6741  IF (.NOT.nested) THEN
6742  CALL pushcontrol(2,0)
6743  ELSE
6744  ifirst = isd
6745  jfirst = jsd
6746  ilast = ied
6747  jlast = jed
6748 !Remember we want to compute these in the HALO. Note also this routine
6749 !requires an appropriate
6750  rgrav = 1./grav
6751  gama = 1./(1.-kappa)
6752  ptk = ptop**kappa
6753  peln1 = log(ptop)
6754 !NOTE: Compiler does NOT like this sort of nested-grid BC code. Is it trying to do some ugly optimization?
6755  IF (is .EQ. 1) THEN
6756  DO j=jfirst,jlast
6757 !GZ
6758  DO i=ifirst,0
6759  CALL pushrealarray(gz(i, j, npz+1))
6760  gz(i, j, npz+1) = phis(i, j)
6761  END DO
6762  DO k=npz,1,-1
6763  DO i=ifirst,0
6764  CALL pushrealarray(gz(i, j, k))
6765  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
6766  END DO
6767  END DO
6768 !Hydrostatic interface pressure
6769  DO i=ifirst,0
6770  pe(i, 1, j) = ptop
6771  peln(i, 1, j) = peln1
6772  END DO
6773  DO k=2,npz+1
6774  DO i=ifirst,0
6775  CALL pushrealarray(pe(i, k, j))
6776  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
6777  peln(i, k, j) = log(pe(i, k, j))
6778  END DO
6779  END DO
6780 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
6781  DO k=1,npz
6782  DO i=ifirst,0
6783 !Full p
6784  CALL pushrealarray(pkz(i, k))
6785  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
6786 & k)*rdgas*pt(i, j, k))))
6787 !hydro
6788  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
6789 !Remove hydro cell-mean pressure
6790  pkz(i, k) = pkz(i, k) - pm
6791  END DO
6792  END DO
6793 !pressure solver
6794  DO k=1,npz-1
6795  DO i=ifirst,0
6796  CALL pushrealarray(g_rat(i, k))
6797  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
6798  bb(i, k) = 2.*(1.+g_rat(i, k))
6799  CALL pushrealarray(dd(i, k))
6800  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
6801  END DO
6802  END DO
6803  DO i=ifirst,0
6804  CALL pushrealarray(bet(i))
6805  bet(i) = bb(i, 1)
6806  CALL pushrealarray(pkc(i, j, 1))
6807  pkc(i, j, 1) = 0.
6808  CALL pushrealarray(pkc(i, j, 2))
6809  pkc(i, j, 2) = dd(i, 1)/bet(i)
6810  bb(i, npz) = 2.
6811  CALL pushrealarray(dd(i, npz))
6812  dd(i, npz) = 3.*pkz(i, npz)
6813  END DO
6814  DO k=2,npz
6815  DO i=ifirst,0
6816  CALL pushrealarray(gam(i, k))
6817  gam(i, k) = g_rat(i, k-1)/bet(i)
6818  CALL pushrealarray(bet(i))
6819  bet(i) = bb(i, k) - gam(i, k)
6820  CALL pushrealarray(pkc(i, j, k+1))
6821  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
6822  END DO
6823  END DO
6824  DO k=npz,2,-1
6825  DO i=ifirst,0
6826  CALL pushrealarray(pkc(i, j, k))
6827  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
6828  END DO
6829  END DO
6830  END DO
6831  DO j=jfirst,jlast
6832  IF (.NOT.pkc_pertn) THEN
6833  DO k=npz+1,1,-1
6834  DO i=ifirst,0
6835  CALL pushrealarray(pkc(i, j, k))
6836  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
6837  END DO
6838  END DO
6839  CALL pushcontrol(1,0)
6840  ELSE
6841  CALL pushcontrol(1,1)
6842  END IF
6843 !pk3 if necessary; doesn't require condenstate loading calculation
6844  IF (computepk3) THEN
6845  DO i=ifirst,0
6846  CALL pushrealarray(pk3(i, j, 1))
6847  pk3(i, j, 1) = ptk
6848  END DO
6849  DO k=2,npz+1
6850  DO i=ifirst,0
6851  CALL pushrealarray(pk3(i, j, k))
6852  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
6853  END DO
6854  END DO
6855  CALL pushcontrol(1,1)
6856  ELSE
6857  CALL pushcontrol(1,0)
6858  END IF
6859  END DO
6860  CALL pushcontrol(1,0)
6861  ELSE
6862  CALL pushcontrol(1,1)
6863  END IF
6864  IF (ie .EQ. npx - 1) THEN
6865  DO j=jfirst,jlast
6866 !GZ
6867  DO i=npx,ilast
6868  CALL pushrealarray(gz(i, j, npz+1))
6869  gz(i, j, npz+1) = phis(i, j)
6870  END DO
6871  DO k=npz,1,-1
6872  DO i=npx,ilast
6873  CALL pushrealarray(gz(i, j, k))
6874  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
6875  END DO
6876  END DO
6877 !Hydrostatic interface pressure
6878  DO i=npx,ilast
6879  CALL pushrealarray(pe(i, 1, j))
6880  pe(i, 1, j) = ptop
6881  CALL pushrealarray(peln(i, 1, j))
6882  peln(i, 1, j) = peln1
6883  END DO
6884  DO k=2,npz+1
6885  DO i=npx,ilast
6886  CALL pushrealarray(pe(i, k, j))
6887  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
6888  CALL pushrealarray(peln(i, k, j))
6889  peln(i, k, j) = log(pe(i, k, j))
6890  END DO
6891  END DO
6892 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
6893  DO k=1,npz
6894  DO i=npx,ilast
6895 !Full p
6896  CALL pushrealarray(pkz(i, k))
6897  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
6898 & k)*rdgas*pt(i, j, k))))
6899 !hydro
6900  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
6901 !Remove hydro cell-mean pressure
6902  pkz(i, k) = pkz(i, k) - pm
6903  END DO
6904  END DO
6905 !pressure solver
6906  DO k=1,npz-1
6907  DO i=npx,ilast
6908  CALL pushrealarray(g_rat(i, k))
6909  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
6910  bb(i, k) = 2.*(1.+g_rat(i, k))
6911  CALL pushrealarray(dd(i, k))
6912  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
6913  END DO
6914  END DO
6915  DO i=npx,ilast
6916  CALL pushrealarray(bet(i))
6917  bet(i) = bb(i, 1)
6918  CALL pushrealarray(pkc(i, j, 1))
6919  pkc(i, j, 1) = 0.
6920  CALL pushrealarray(pkc(i, j, 2))
6921  pkc(i, j, 2) = dd(i, 1)/bet(i)
6922  bb(i, npz) = 2.
6923  CALL pushrealarray(dd(i, npz))
6924  dd(i, npz) = 3.*pkz(i, npz)
6925  END DO
6926  DO k=2,npz
6927  DO i=npx,ilast
6928  CALL pushrealarray(gam(i, k))
6929  gam(i, k) = g_rat(i, k-1)/bet(i)
6930  CALL pushrealarray(bet(i))
6931  bet(i) = bb(i, k) - gam(i, k)
6932  CALL pushrealarray(pkc(i, j, k+1))
6933  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
6934  END DO
6935  END DO
6936  DO k=npz,2,-1
6937  DO i=npx,ilast
6938  CALL pushrealarray(pkc(i, j, k))
6939  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
6940  END DO
6941  END DO
6942  END DO
6943  DO j=jfirst,jlast
6944  IF (.NOT.pkc_pertn) THEN
6945  DO k=npz+1,1,-1
6946  DO i=npx,ilast
6947  CALL pushrealarray(pkc(i, j, k))
6948  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
6949  END DO
6950  END DO
6951  CALL pushcontrol(1,0)
6952  ELSE
6953  CALL pushcontrol(1,1)
6954  END IF
6955 !pk3 if necessary
6956  IF (computepk3) THEN
6957  DO i=npx,ilast
6958  CALL pushrealarray(pk3(i, j, 1))
6959  pk3(i, j, 1) = ptk
6960  END DO
6961  DO k=2,npz+1
6962  DO i=npx,ilast
6963  CALL pushrealarray(pk3(i, j, k))
6964  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
6965  END DO
6966  END DO
6967  CALL pushcontrol(1,1)
6968  ELSE
6969  CALL pushcontrol(1,0)
6970  END IF
6971  END DO
6972  CALL pushcontrol(1,0)
6973  ELSE
6974  CALL pushcontrol(1,1)
6975  END IF
6976  IF (js .EQ. 1) THEN
6977  DO j=jfirst,0
6978 !GZ
6979  DO i=ifirst,ilast
6980  CALL pushrealarray(gz(i, j, npz+1))
6981  gz(i, j, npz+1) = phis(i, j)
6982  END DO
6983  DO k=npz,1,-1
6984  DO i=ifirst,ilast
6985  CALL pushrealarray(gz(i, j, k))
6986  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
6987  END DO
6988  END DO
6989 !Hydrostatic interface pressure
6990  DO i=ifirst,ilast
6991  CALL pushrealarray(pe(i, 1, j))
6992  pe(i, 1, j) = ptop
6993  CALL pushrealarray(peln(i, 1, j))
6994  peln(i, 1, j) = peln1
6995  END DO
6996  DO k=2,npz+1
6997  DO i=ifirst,ilast
6998  CALL pushrealarray(pe(i, k, j))
6999  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
7000  CALL pushrealarray(peln(i, k, j))
7001  peln(i, k, j) = log(pe(i, k, j))
7002  END DO
7003  END DO
7004 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
7005  DO k=1,npz
7006  DO i=ifirst,ilast
7007 !Full p
7008  CALL pushrealarray(pkz(i, k))
7009  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
7010 & k)*rdgas*pt(i, j, k))))
7011 !hydro
7012 !hydro
7013  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
7014 !Remove hydro cell-mean pressure
7015  pkz(i, k) = pkz(i, k) - pm
7016  END DO
7017  END DO
7018 !pressure solver
7019  DO k=1,npz-1
7020  DO i=ifirst,ilast
7021  CALL pushrealarray(g_rat(i, k))
7022  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
7023  bb(i, k) = 2.*(1.+g_rat(i, k))
7024  CALL pushrealarray(dd(i, k))
7025  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
7026  END DO
7027  END DO
7028  DO i=ifirst,ilast
7029  CALL pushrealarray(bet(i))
7030  bet(i) = bb(i, 1)
7031  CALL pushrealarray(pkc(i, j, 1))
7032  pkc(i, j, 1) = 0.
7033  CALL pushrealarray(pkc(i, j, 2))
7034  pkc(i, j, 2) = dd(i, 1)/bet(i)
7035  bb(i, npz) = 2.
7036  CALL pushrealarray(dd(i, npz))
7037  dd(i, npz) = 3.*pkz(i, npz)
7038  END DO
7039  DO k=2,npz
7040  DO i=ifirst,ilast
7041  CALL pushrealarray(gam(i, k))
7042  gam(i, k) = g_rat(i, k-1)/bet(i)
7043  CALL pushrealarray(bet(i))
7044  bet(i) = bb(i, k) - gam(i, k)
7045  CALL pushrealarray(pkc(i, j, k+1))
7046  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
7047  END DO
7048  END DO
7049  DO k=npz,2,-1
7050  DO i=ifirst,ilast
7051  CALL pushrealarray(pkc(i, j, k))
7052  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
7053  END DO
7054  END DO
7055  END DO
7056  DO j=jfirst,0
7057  IF (.NOT.pkc_pertn) THEN
7058  DO k=npz+1,1,-1
7059  DO i=ifirst,ilast
7060  CALL pushrealarray(pkc(i, j, k))
7061  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
7062  END DO
7063  END DO
7064  CALL pushcontrol(1,0)
7065  ELSE
7066  CALL pushcontrol(1,1)
7067  END IF
7068 !pk3 if necessary
7069  IF (computepk3) THEN
7070  DO i=ifirst,ilast
7071  CALL pushrealarray(pk3(i, j, 1))
7072  pk3(i, j, 1) = ptk
7073  END DO
7074  DO k=2,npz+1
7075  DO i=ifirst,ilast
7076  CALL pushrealarray(pk3(i, j, k))
7077  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
7078  END DO
7079  END DO
7080  CALL pushcontrol(1,1)
7081  ELSE
7082  CALL pushcontrol(1,0)
7083  END IF
7084  END DO
7085  CALL pushcontrol(1,0)
7086  ELSE
7087  CALL pushcontrol(1,1)
7088  END IF
7089  IF (je .EQ. npy - 1) THEN
7090  DO j=npy,jlast
7091 !GZ
7092  DO i=ifirst,ilast
7093  CALL pushrealarray(gz(i, j, npz+1))
7094  gz(i, j, npz+1) = phis(i, j)
7095  END DO
7096  DO k=npz,1,-1
7097  DO i=ifirst,ilast
7098  CALL pushrealarray(gz(i, j, k))
7099  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
7100  END DO
7101  END DO
7102 !Hydrostatic interface pressure
7103  DO i=ifirst,ilast
7104  CALL pushrealarray(pe(i, 1, j))
7105  pe(i, 1, j) = ptop
7106  CALL pushrealarray(peln(i, 1, j))
7107  peln(i, 1, j) = peln1
7108  END DO
7109  DO k=2,npz+1
7110  DO i=ifirst,ilast
7111  CALL pushrealarray(pe(i, k, j))
7112  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
7113  CALL pushrealarray(peln(i, k, j))
7114  peln(i, k, j) = log(pe(i, k, j))
7115  END DO
7116  END DO
7117 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
7118  DO k=1,npz
7119  DO i=ifirst,ilast
7120 !Full p
7121  CALL pushrealarray(pkz(i, k))
7122  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
7123 & k)*rdgas*pt(i, j, k))))
7124 !hydro
7125 !hydro
7126  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
7127 !Remove hydro cell-mean pressure
7128  pkz(i, k) = pkz(i, k) - pm
7129  END DO
7130  END DO
7131 !Reversible interpolation on layer NH pressure perturbation
7132 ! to recover lastge NH pressure perturbation
7133  DO k=1,npz-1
7134  DO i=ifirst,ilast
7135  CALL pushrealarray(g_rat(i, k))
7136  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
7137  bb(i, k) = 2.*(1.+g_rat(i, k))
7138  CALL pushrealarray(dd(i, k))
7139  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
7140  END DO
7141  END DO
7142  DO i=ifirst,ilast
7143  CALL pushrealarray(bet(i))
7144  bet(i) = bb(i, 1)
7145  CALL pushrealarray(pkc(i, j, 1))
7146  pkc(i, j, 1) = 0.
7147  CALL pushrealarray(pkc(i, j, 2))
7148  pkc(i, j, 2) = dd(i, 1)/bet(i)
7149  bb(i, npz) = 2.
7150  CALL pushrealarray(dd(i, npz))
7151  dd(i, npz) = 3.*pkz(i, npz)
7152  END DO
7153  DO k=2,npz
7154  DO i=ifirst,ilast
7155  CALL pushrealarray(gam(i, k))
7156  gam(i, k) = g_rat(i, k-1)/bet(i)
7157  CALL pushrealarray(bet(i))
7158  bet(i) = bb(i, k) - gam(i, k)
7159  CALL pushrealarray(pkc(i, j, k+1))
7160  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
7161  END DO
7162  END DO
7163  DO k=npz,2,-1
7164  DO i=ifirst,ilast
7165  CALL pushrealarray(pkc(i, j, k))
7166  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
7167  END DO
7168  END DO
7169  END DO
7170  DO j=npy,jlast
7171  IF (.NOT.pkc_pertn) THEN
7172  DO k=npz+1,1,-1
7173  DO i=ifirst,ilast
7174  CALL pushrealarray(pkc(i, j, k))
7175  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
7176  END DO
7177  END DO
7178  CALL pushcontrol(1,0)
7179  ELSE
7180  CALL pushcontrol(1,1)
7181  END IF
7182 !pk3 if necessary
7183  IF (computepk3) THEN
7184  DO i=ifirst,ilast
7185  CALL pushrealarray(pk3(i, j, 1))
7186  pk3(i, j, 1) = ptk
7187  END DO
7188  DO k=2,npz+1
7189  DO i=ifirst,ilast
7190  CALL pushrealarray(pk3(i, j, k))
7191  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
7192  END DO
7193  END DO
7194  CALL pushcontrol(1,1)
7195  ELSE
7196  CALL pushcontrol(1,0)
7197  END IF
7198  END DO
7199  CALL pushrealarray(gam, (bd%ied-bd%isd+1)*npz)
7200  CALL pushinteger(ifirst)
7201  CALL pushrealarray(g_rat, (bd%ied-bd%isd+1)*(npz-1))
7202  CALL pushrealarray(pe, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%jsd&
7203 & +1))
7204  CALL pushrealarray(pkz, (bd%ied-bd%isd+1)*npz)
7205  CALL pushrealarray(gama)
7206  CALL pushinteger(jlast)
7207  CALL pushrealarray(rgrav)
7208  CALL pushinteger(ilast)
7209  CALL pushrealarray(bet, bd%ied - bd%isd + 1)
7210  CALL pushrealarray(peln, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%&
7211 & jsd+1))
7212  CALL pushrealarray(dd, (bd%ied-bd%isd+1)*npz)
7213  CALL pushcontrol(2,2)
7214  ELSE
7215  CALL pushrealarray(gam, (bd%ied-bd%isd+1)*npz)
7216  CALL pushinteger(ifirst)
7217  CALL pushrealarray(g_rat, (bd%ied-bd%isd+1)*(npz-1))
7218  CALL pushrealarray(pe, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%jsd&
7219 & +1))
7220  CALL pushrealarray(pkz, (bd%ied-bd%isd+1)*npz)
7221  CALL pushrealarray(gama)
7222  CALL pushinteger(jlast)
7223  CALL pushrealarray(rgrav)
7224  CALL pushinteger(ilast)
7225  CALL pushrealarray(bet, bd%ied - bd%isd + 1)
7226  CALL pushrealarray(peln, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%&
7227 & jsd+1))
7228  CALL pushrealarray(dd, (bd%ied-bd%isd+1)*npz)
7229  CALL pushcontrol(2,1)
7230  END IF
7231  END IF
7232  END SUBROUTINE nest_halo_nh_fwd
7233 ! Differentiation of nest_halo_nh in reverse (adjoint) mode, backward sweep (with options split(a2b_edge_mod.a2b_ord4 a2b_edg
7234 !e_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_co
7235 !re_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_mo
7236 !d.mix_dp dyn_core_mod.geopk dyn_core_mod.del2_cubed dyn_core_mod.Rayleigh_fast fv_dynamics_mod.fv_dynamics fv_dynamics_mod.Rayle
7237 !igh_Super fv_dynamics_mod.Rayleigh_Friction fv_dynamics_mod.compute_aam fv_grid_utils_mod.cubed_to_latlon fv_grid_utils_mod.c2l_
7238 !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_mod
7239 !.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_2
7240 !d fv_mapz_mod.scalar_profile fv_mapz_mod.cs_profile fv_mapz_mod.cs_limiters fv_mapz_mod.ppm_profile fv_mapz_mod.ppm_limite
7241 !rs 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 f
7242 !v_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_sub
7243 !grid_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_uti
7244 !ls_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
7245 !_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_
7246 !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_mod
7247 !.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
7248 !_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_co
7249 !re_mod.copy_corners tp_core_mod.xppm tp_core_mod.yppm tp_core_mod.deln_flux a2b_edge_mod.extrap_corner fv_grid_ut
7250 !ils_mod.great_circle_dist sw_core_mod.edge_interpolate4)):
7251 ! gradient of useful results: pk3 gz delp delz pkc pt
7252 ! with respect to varying inputs: pk3 gz delp delz pkc pt
7253  SUBROUTINE nest_halo_nh_bwd(ptop, grav, kappa, cp, delp, delp_ad, delz&
7254 & , delz_ad, pt, pt_ad, phis, pkc, pkc_ad, gz, gz_ad, pk3, pk3_ad, npx&
7255 & , npy, npz, nested, pkc_pertn, computepk3, fullhalo, bd)
7256  IMPLICIT NONE
7257  INTEGER, INTENT(IN) :: npx, npy, npz
7258  LOGICAL, INTENT(IN) :: pkc_pertn, computepk3, fullhalo, nested
7259  REAL, INTENT(IN) :: ptop, kappa, cp, grav
7260  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
7261  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
7262  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(IN) :: pt&
7263 & , delp, delz
7264  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz) :: pt_ad, delp_ad&
7265 & , delz_ad
7266  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1), INTENT(INOUT) &
7267 & :: gz, pkc, pk3
7268  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1), INTENT(INOUT) &
7269 & :: gz_ad, pkc_ad, pk3_ad
7270  INTEGER :: i, j, k
7271  REAL :: gama
7272  REAL :: ptk, rgrav, rkap, peln1, rdg
7273  REAL, DIMENSION(bd%isd:bd%ied, npz+1, bd%jsd:bd%jed) :: pe, peln
7274  REAL, DIMENSION(bd%isd:bd%ied, npz+1, bd%jsd:bd%jed) :: pe_ad, &
7275 & peln_ad
7276  REAL, DIMENSION(bd%isd:bd%ied, npz) :: gam, bb, dd, pkz
7277  REAL, DIMENSION(bd%isd:bd%ied, npz) :: gam_ad, bb_ad, dd_ad, pkz_ad
7278  REAL, DIMENSION(bd%isd:bd%ied, npz-1) :: g_rat
7279  REAL, DIMENSION(bd%isd:bd%ied, npz-1) :: g_rat_ad
7280  REAL, DIMENSION(bd%isd:bd%ied) :: bet
7281  REAL, DIMENSION(bd%isd:bd%ied) :: bet_ad
7282  REAL :: pm
7283  REAL :: pm_ad
7284  INTEGER :: ifirst, ilast, jfirst, jlast
7285  INTEGER :: is, ie, js, je
7286  INTEGER :: isd, ied, jsd, jed
7287  INTRINSIC log
7288  INTRINSIC exp
7289  REAL :: temp
7290  REAL :: temp0
7291  REAL :: temp1
7292  REAL :: temp2
7293  REAL :: temp3
7294  REAL :: temp_ad
7295  REAL :: temp_ad0
7296  REAL :: temp_ad1
7297  REAL :: temp_ad2
7298  REAL :: temp_ad3
7299  REAL :: temp_ad4
7300  REAL :: temp_ad5
7301  REAL :: temp4
7302  REAL :: temp5
7303  REAL :: temp6
7304  REAL :: temp7
7305  REAL :: temp8
7306  REAL :: temp_ad6
7307  REAL :: temp_ad7
7308  REAL :: temp_ad8
7309  REAL :: temp_ad9
7310  REAL :: temp_ad10
7311  REAL :: temp_ad11
7312  REAL :: temp_ad12
7313  REAL :: temp9
7314  REAL :: temp10
7315  REAL :: temp11
7316  REAL :: temp12
7317  REAL :: temp13
7318  REAL :: temp_ad13
7319  REAL :: temp_ad14
7320  REAL :: temp_ad15
7321  REAL :: temp_ad16
7322  REAL :: temp_ad17
7323  REAL :: temp_ad18
7324  REAL :: temp_ad19
7325  REAL :: temp14
7326  REAL :: temp15
7327  REAL :: temp16
7328  REAL :: temp17
7329  REAL :: temp18
7330  REAL :: temp_ad20
7331  REAL :: temp_ad21
7332  REAL :: temp_ad22
7333  REAL :: temp_ad23
7334  REAL :: temp_ad24
7335  REAL :: temp_ad25
7336  REAL :: temp_ad26
7337  INTEGER :: branch
7338 
7339  gama = 0.0
7340  ptk = 0.0
7341  rgrav = 0.0
7342  rkap = 0.0
7343  peln1 = 0.0
7344  rdg = 0.0
7345  pe = 0.0
7346  peln = 0.0
7347  gam = 0.0
7348  bb = 0.0
7349  dd = 0.0
7350  pkz = 0.0
7351  g_rat = 0.0
7352  bet = 0.0
7353  pm = 0.0
7354  ifirst = 0
7355  ilast = 0
7356  jfirst = 0
7357  jlast = 0
7358  is = 0
7359  js = 0
7360  je = 0
7361  isd = 0
7362  ied = 0
7363  jsd = 0
7364  jed = 0
7365  branch = 0
7366 
7367  CALL popcontrol(2,branch)
7368  IF (branch .NE. 0) THEN
7369  IF (branch .EQ. 1) THEN
7370  CALL poprealarray(dd, (bd%ied-bd%isd+1)*npz)
7371  CALL poprealarray(peln, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%&
7372 & jsd+1))
7373  CALL poprealarray(bet, bd%ied - bd%isd + 1)
7374  CALL popinteger(ilast)
7375  CALL poprealarray(rgrav)
7376  CALL popinteger(jlast)
7377  CALL poprealarray(gama)
7378  CALL poprealarray(pkz, (bd%ied-bd%isd+1)*npz)
7379  CALL poprealarray(pe, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%jsd+&
7380 & 1))
7381  CALL poprealarray(g_rat, (bd%ied-bd%isd+1)*(npz-1))
7382  CALL popinteger(ifirst)
7383  CALL poprealarray(gam, (bd%ied-bd%isd+1)*npz)
7384  dd_ad = 0.0
7385  peln_ad = 0.0
7386  bet_ad = 0.0
7387  bb_ad = 0.0
7388  pkz_ad = 0.0
7389  pe_ad = 0.0
7390  g_rat_ad = 0.0
7391  gam_ad = 0.0
7392  ELSE
7393  CALL poprealarray(dd, (bd%ied-bd%isd+1)*npz)
7394  CALL poprealarray(peln, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%&
7395 & jsd+1))
7396  CALL poprealarray(bet, bd%ied - bd%isd + 1)
7397  CALL popinteger(ilast)
7398  CALL poprealarray(rgrav)
7399  CALL popinteger(jlast)
7400  CALL poprealarray(gama)
7401  CALL poprealarray(pkz, (bd%ied-bd%isd+1)*npz)
7402  CALL poprealarray(pe, (bd%ied-bd%isd+1)*(npz+1)*(bd%jed-bd%jsd+&
7403 & 1))
7404  CALL poprealarray(g_rat, (bd%ied-bd%isd+1)*(npz-1))
7405  CALL popinteger(ifirst)
7406  CALL poprealarray(gam, (bd%ied-bd%isd+1)*npz)
7407  pe_ad = 0.0
7408  DO j=jlast,npy,-1
7409  CALL popcontrol(1,branch)
7410  IF (branch .NE. 0) THEN
7411  DO k=npz+1,2,-1
7412  DO i=ilast,ifirst,-1
7413  CALL poprealarray(pk3(i, j, k))
7414  pe_ad(i, k, j) = pe_ad(i, k, j) + kappa*exp(kappa*log(pe&
7415 & (i, k, j)))*pk3_ad(i, j, k)/pe(i, k, j)
7416  pk3_ad(i, j, k) = 0.0
7417  END DO
7418  END DO
7419  DO i=ilast,ifirst,-1
7420  CALL poprealarray(pk3(i, j, 1))
7421  pk3_ad(i, j, 1) = 0.0
7422  END DO
7423  END IF
7424  CALL popcontrol(1,branch)
7425  IF (branch .EQ. 0) THEN
7426  DO k=1,npz+1,1
7427  DO i=ilast,ifirst,-1
7428  CALL poprealarray(pkc(i, j, k))
7429  pe_ad(i, k, j) = pe_ad(i, k, j) + pkc_ad(i, j, k)
7430  END DO
7431  END DO
7432  END IF
7433  END DO
7434  dd_ad = 0.0
7435  peln_ad = 0.0
7436  bet_ad = 0.0
7437  bb_ad = 0.0
7438  pkz_ad = 0.0
7439  g_rat_ad = 0.0
7440  gam_ad = 0.0
7441  DO j=jlast,npy,-1
7442  DO k=2,npz,1
7443  DO i=ilast,ifirst,-1
7444  CALL poprealarray(pkc(i, j, k))
7445  gam_ad(i, k) = gam_ad(i, k) - pkc(i, j, k+1)*pkc_ad(i, j, &
7446 & k)
7447  pkc_ad(i, j, k+1) = pkc_ad(i, j, k+1) - gam(i, k)*pkc_ad(i&
7448 & , j, k)
7449  END DO
7450  END DO
7451  DO k=npz,2,-1
7452  DO i=ilast,ifirst,-1
7453  CALL poprealarray(pkc(i, j, k+1))
7454  temp_ad25 = pkc_ad(i, j, k+1)/bet(i)
7455  dd_ad(i, k) = dd_ad(i, k) + temp_ad25
7456  pkc_ad(i, j, k) = pkc_ad(i, j, k) - temp_ad25
7457  bet_ad(i) = bet_ad(i) - (dd(i, k)-pkc(i, j, k))*temp_ad25/&
7458 & bet(i)
7459  pkc_ad(i, j, k+1) = 0.0
7460  CALL poprealarray(bet(i))
7461  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
7462  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
7463  CALL poprealarray(gam(i, k))
7464  temp_ad26 = gam_ad(i, k)/bet(i)
7465  bet_ad(i) = -(g_rat(i, k-1)*temp_ad26/bet(i))
7466  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad26
7467  gam_ad(i, k) = 0.0
7468  END DO
7469  END DO
7470  DO i=ilast,ifirst,-1
7471  CALL poprealarray(dd(i, npz))
7472  pkz_ad(i, npz) = pkz_ad(i, npz) + 3.*dd_ad(i, npz)
7473  dd_ad(i, npz) = 0.0
7474  bb_ad(i, npz) = 0.0
7475  CALL poprealarray(pkc(i, j, 2))
7476  temp_ad24 = pkc_ad(i, j, 2)/bet(i)
7477  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad24
7478  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad24/bet(i)
7479  pkc_ad(i, j, 2) = 0.0
7480  CALL poprealarray(pkc(i, j, 1))
7481  pkc_ad(i, j, 1) = 0.0
7482  CALL poprealarray(bet(i))
7483  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
7484  bet_ad(i) = 0.0
7485  END DO
7486  DO k=npz-1,1,-1
7487  DO i=ilast,ifirst,-1
7488  CALL poprealarray(dd(i, k))
7489  temp_ad22 = 3.*dd_ad(i, k)
7490  pkz_ad(i, k) = pkz_ad(i, k) + temp_ad22
7491  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pkz(i, &
7492 & k+1)*temp_ad22
7493  pkz_ad(i, k+1) = pkz_ad(i, k+1) + g_rat(i, k)*temp_ad22
7494  dd_ad(i, k) = 0.0
7495  bb_ad(i, k) = 0.0
7496  CALL poprealarray(g_rat(i, k))
7497  temp_ad23 = g_rat_ad(i, k)/delp(i, j, k+1)
7498  delp_ad(i, j, k) = delp_ad(i, j, k) + temp_ad23
7499  delp_ad(i, j, k+1) = delp_ad(i, j, k+1) - delp(i, j, k)*&
7500 & temp_ad23/delp(i, j, k+1)
7501  g_rat_ad(i, k) = 0.0
7502  END DO
7503  END DO
7504  DO k=npz,1,-1
7505  DO i=ilast,ifirst,-1
7506  temp17 = delz(i, j, k)
7507  temp16 = delp(i, j, k)*pt(i, j, k)
7508  temp14 = temp16/temp17
7509  temp15 = -(rgrav*rdgas*temp14)
7510  temp_ad21 = -(rgrav*rdgas*gama*exp(gama*log(temp15))*&
7511 & pkz_ad(i, k)/(temp15*temp17))
7512  pm_ad = -pkz_ad(i, k)
7513  temp18 = peln(i, k+1, j) - peln(i, k, j)
7514  temp_ad20 = -(delp(i, j, k)*pm_ad/temp18**2)
7515  delp_ad(i, j, k) = delp_ad(i, j, k) + pt(i, j, k)*&
7516 & temp_ad21 + pm_ad/temp18
7517  peln_ad(i, k+1, j) = peln_ad(i, k+1, j) + temp_ad20
7518  peln_ad(i, k, j) = peln_ad(i, k, j) - temp_ad20
7519  CALL poprealarray(pkz(i, k))
7520  pt_ad(i, j, k) = pt_ad(i, j, k) + delp(i, j, k)*temp_ad21
7521  delz_ad(i, j, k) = delz_ad(i, j, k) - temp14*temp_ad21
7522  pkz_ad(i, k) = 0.0
7523  END DO
7524  END DO
7525  DO k=npz+1,2,-1
7526  DO i=ilast,ifirst,-1
7527  CALL poprealarray(peln(i, k, j))
7528  pe_ad(i, k, j) = pe_ad(i, k, j) + peln_ad(i, k, j)/pe(i, k&
7529 & , j)
7530  peln_ad(i, k, j) = 0.0
7531  CALL poprealarray(pe(i, k, j))
7532  pe_ad(i, k-1, j) = pe_ad(i, k-1, j) + pe_ad(i, k, j)
7533  delp_ad(i, j, k-1) = delp_ad(i, j, k-1) + pe_ad(i, k, j)
7534  pe_ad(i, k, j) = 0.0
7535  END DO
7536  END DO
7537  DO i=ilast,ifirst,-1
7538  CALL poprealarray(peln(i, 1, j))
7539  peln_ad(i, 1, j) = 0.0
7540  CALL poprealarray(pe(i, 1, j))
7541  pe_ad(i, 1, j) = 0.0
7542  END DO
7543  DO k=1,npz,1
7544  DO i=ilast,ifirst,-1
7545  CALL poprealarray(gz(i, j, k))
7546  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
7547  delz_ad(i, j, k) = delz_ad(i, j, k) - grav*gz_ad(i, j, k)
7548  gz_ad(i, j, k) = 0.0
7549  END DO
7550  END DO
7551  DO i=ilast,ifirst,-1
7552  CALL poprealarray(gz(i, j, npz+1))
7553  gz_ad(i, j, npz+1) = 0.0
7554  END DO
7555  END DO
7556  END IF
7557  jsd = bd%jsd
7558  jfirst = jsd
7559  CALL popcontrol(1,branch)
7560  IF (branch .EQ. 0) THEN
7561  DO j=0,jfirst,-1
7562  CALL popcontrol(1,branch)
7563  IF (branch .NE. 0) THEN
7564  DO k=npz+1,2,-1
7565  DO i=ilast,ifirst,-1
7566  CALL poprealarray(pk3(i, j, k))
7567  pe_ad(i, k, j) = pe_ad(i, k, j) + kappa*exp(kappa*log(pe&
7568 & (i, k, j)))*pk3_ad(i, j, k)/pe(i, k, j)
7569  pk3_ad(i, j, k) = 0.0
7570  END DO
7571  END DO
7572  DO i=ilast,ifirst,-1
7573  CALL poprealarray(pk3(i, j, 1))
7574  pk3_ad(i, j, 1) = 0.0
7575  END DO
7576  END IF
7577  CALL popcontrol(1,branch)
7578  IF (branch .EQ. 0) THEN
7579  DO k=1,npz+1,1
7580  DO i=ilast,ifirst,-1
7581  CALL poprealarray(pkc(i, j, k))
7582  pe_ad(i, k, j) = pe_ad(i, k, j) + pkc_ad(i, j, k)
7583  END DO
7584  END DO
7585  END IF
7586  END DO
7587  DO j=0,jfirst,-1
7588  DO k=2,npz,1
7589  DO i=ilast,ifirst,-1
7590  CALL poprealarray(pkc(i, j, k))
7591  gam_ad(i, k) = gam_ad(i, k) - pkc(i, j, k+1)*pkc_ad(i, j, &
7592 & k)
7593  pkc_ad(i, j, k+1) = pkc_ad(i, j, k+1) - gam(i, k)*pkc_ad(i&
7594 & , j, k)
7595  END DO
7596  END DO
7597  DO k=npz,2,-1
7598  DO i=ilast,ifirst,-1
7599  CALL poprealarray(pkc(i, j, k+1))
7600  temp_ad18 = pkc_ad(i, j, k+1)/bet(i)
7601  dd_ad(i, k) = dd_ad(i, k) + temp_ad18
7602  pkc_ad(i, j, k) = pkc_ad(i, j, k) - temp_ad18
7603  bet_ad(i) = bet_ad(i) - (dd(i, k)-pkc(i, j, k))*temp_ad18/&
7604 & bet(i)
7605  pkc_ad(i, j, k+1) = 0.0
7606  CALL poprealarray(bet(i))
7607  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
7608  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
7609  CALL poprealarray(gam(i, k))
7610  temp_ad19 = gam_ad(i, k)/bet(i)
7611  bet_ad(i) = -(g_rat(i, k-1)*temp_ad19/bet(i))
7612  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad19
7613  gam_ad(i, k) = 0.0
7614  END DO
7615  END DO
7616  DO i=ilast,ifirst,-1
7617  CALL poprealarray(dd(i, npz))
7618  pkz_ad(i, npz) = pkz_ad(i, npz) + 3.*dd_ad(i, npz)
7619  dd_ad(i, npz) = 0.0
7620  bb_ad(i, npz) = 0.0
7621  CALL poprealarray(pkc(i, j, 2))
7622  temp_ad17 = pkc_ad(i, j, 2)/bet(i)
7623  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad17
7624  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad17/bet(i)
7625  pkc_ad(i, j, 2) = 0.0
7626  CALL poprealarray(pkc(i, j, 1))
7627  pkc_ad(i, j, 1) = 0.0
7628  CALL poprealarray(bet(i))
7629  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
7630  bet_ad(i) = 0.0
7631  END DO
7632  DO k=npz-1,1,-1
7633  DO i=ilast,ifirst,-1
7634  CALL poprealarray(dd(i, k))
7635  temp_ad15 = 3.*dd_ad(i, k)
7636  pkz_ad(i, k) = pkz_ad(i, k) + temp_ad15
7637  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pkz(i, &
7638 & k+1)*temp_ad15
7639  pkz_ad(i, k+1) = pkz_ad(i, k+1) + g_rat(i, k)*temp_ad15
7640  dd_ad(i, k) = 0.0
7641  bb_ad(i, k) = 0.0
7642  CALL poprealarray(g_rat(i, k))
7643  temp_ad16 = g_rat_ad(i, k)/delp(i, j, k+1)
7644  delp_ad(i, j, k) = delp_ad(i, j, k) + temp_ad16
7645  delp_ad(i, j, k+1) = delp_ad(i, j, k+1) - delp(i, j, k)*&
7646 & temp_ad16/delp(i, j, k+1)
7647  g_rat_ad(i, k) = 0.0
7648  END DO
7649  END DO
7650  DO k=npz,1,-1
7651  DO i=ilast,ifirst,-1
7652  temp12 = delz(i, j, k)
7653  temp11 = delp(i, j, k)*pt(i, j, k)
7654  temp9 = temp11/temp12
7655  temp10 = -(rgrav*rdgas*temp9)
7656  temp_ad14 = -(rgrav*rdgas*gama*exp(gama*log(temp10))*&
7657 & pkz_ad(i, k)/(temp10*temp12))
7658  pm_ad = -pkz_ad(i, k)
7659  temp13 = peln(i, k+1, j) - peln(i, k, j)
7660  temp_ad13 = -(delp(i, j, k)*pm_ad/temp13**2)
7661  delp_ad(i, j, k) = delp_ad(i, j, k) + pt(i, j, k)*&
7662 & temp_ad14 + pm_ad/temp13
7663  peln_ad(i, k+1, j) = peln_ad(i, k+1, j) + temp_ad13
7664  peln_ad(i, k, j) = peln_ad(i, k, j) - temp_ad13
7665  CALL poprealarray(pkz(i, k))
7666  pt_ad(i, j, k) = pt_ad(i, j, k) + delp(i, j, k)*temp_ad14
7667  delz_ad(i, j, k) = delz_ad(i, j, k) - temp9*temp_ad14
7668  pkz_ad(i, k) = 0.0
7669  END DO
7670  END DO
7671  DO k=npz+1,2,-1
7672  DO i=ilast,ifirst,-1
7673  CALL poprealarray(peln(i, k, j))
7674  pe_ad(i, k, j) = pe_ad(i, k, j) + peln_ad(i, k, j)/pe(i, k&
7675 & , j)
7676  peln_ad(i, k, j) = 0.0
7677  CALL poprealarray(pe(i, k, j))
7678  pe_ad(i, k-1, j) = pe_ad(i, k-1, j) + pe_ad(i, k, j)
7679  delp_ad(i, j, k-1) = delp_ad(i, j, k-1) + pe_ad(i, k, j)
7680  pe_ad(i, k, j) = 0.0
7681  END DO
7682  END DO
7683  DO i=ilast,ifirst,-1
7684  CALL poprealarray(peln(i, 1, j))
7685  peln_ad(i, 1, j) = 0.0
7686  CALL poprealarray(pe(i, 1, j))
7687  pe_ad(i, 1, j) = 0.0
7688  END DO
7689  DO k=1,npz,1
7690  DO i=ilast,ifirst,-1
7691  CALL poprealarray(gz(i, j, k))
7692  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
7693  delz_ad(i, j, k) = delz_ad(i, j, k) - grav*gz_ad(i, j, k)
7694  gz_ad(i, j, k) = 0.0
7695  END DO
7696  END DO
7697  DO i=ilast,ifirst,-1
7698  CALL poprealarray(gz(i, j, npz+1))
7699  gz_ad(i, j, npz+1) = 0.0
7700  END DO
7701  END DO
7702  END IF
7703  CALL popcontrol(1,branch)
7704  IF (branch .EQ. 0) THEN
7705  DO j=jlast,jfirst,-1
7706  CALL popcontrol(1,branch)
7707  IF (branch .NE. 0) THEN
7708  DO k=npz+1,2,-1
7709  DO i=ilast,npx,-1
7710  CALL poprealarray(pk3(i, j, k))
7711  pe_ad(i, k, j) = pe_ad(i, k, j) + kappa*exp(kappa*log(pe&
7712 & (i, k, j)))*pk3_ad(i, j, k)/pe(i, k, j)
7713  pk3_ad(i, j, k) = 0.0
7714  END DO
7715  END DO
7716  DO i=ilast,npx,-1
7717  CALL poprealarray(pk3(i, j, 1))
7718  pk3_ad(i, j, 1) = 0.0
7719  END DO
7720  END IF
7721  CALL popcontrol(1,branch)
7722  IF (branch .EQ. 0) THEN
7723  DO k=1,npz+1,1
7724  DO i=ilast,npx,-1
7725  CALL poprealarray(pkc(i, j, k))
7726  pe_ad(i, k, j) = pe_ad(i, k, j) + pkc_ad(i, j, k)
7727  END DO
7728  END DO
7729  END IF
7730  END DO
7731  DO j=jlast,jfirst,-1
7732  DO k=2,npz,1
7733  DO i=ilast,npx,-1
7734  CALL poprealarray(pkc(i, j, k))
7735  gam_ad(i, k) = gam_ad(i, k) - pkc(i, j, k+1)*pkc_ad(i, j, &
7736 & k)
7737  pkc_ad(i, j, k+1) = pkc_ad(i, j, k+1) - gam(i, k)*pkc_ad(i&
7738 & , j, k)
7739  END DO
7740  END DO
7741  DO k=npz,2,-1
7742  DO i=ilast,npx,-1
7743  CALL poprealarray(pkc(i, j, k+1))
7744  temp_ad11 = pkc_ad(i, j, k+1)/bet(i)
7745  dd_ad(i, k) = dd_ad(i, k) + temp_ad11
7746  pkc_ad(i, j, k) = pkc_ad(i, j, k) - temp_ad11
7747  bet_ad(i) = bet_ad(i) - (dd(i, k)-pkc(i, j, k))*temp_ad11/&
7748 & bet(i)
7749  pkc_ad(i, j, k+1) = 0.0
7750  CALL poprealarray(bet(i))
7751  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
7752  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
7753  CALL poprealarray(gam(i, k))
7754  temp_ad12 = gam_ad(i, k)/bet(i)
7755  bet_ad(i) = -(g_rat(i, k-1)*temp_ad12/bet(i))
7756  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad12
7757  gam_ad(i, k) = 0.0
7758  END DO
7759  END DO
7760  DO i=ilast,npx,-1
7761  CALL poprealarray(dd(i, npz))
7762  pkz_ad(i, npz) = pkz_ad(i, npz) + 3.*dd_ad(i, npz)
7763  dd_ad(i, npz) = 0.0
7764  bb_ad(i, npz) = 0.0
7765  CALL poprealarray(pkc(i, j, 2))
7766  temp_ad10 = pkc_ad(i, j, 2)/bet(i)
7767  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad10
7768  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad10/bet(i)
7769  pkc_ad(i, j, 2) = 0.0
7770  CALL poprealarray(pkc(i, j, 1))
7771  pkc_ad(i, j, 1) = 0.0
7772  CALL poprealarray(bet(i))
7773  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
7774  bet_ad(i) = 0.0
7775  END DO
7776  DO k=npz-1,1,-1
7777  DO i=ilast,npx,-1
7778  CALL poprealarray(dd(i, k))
7779  temp_ad8 = 3.*dd_ad(i, k)
7780  pkz_ad(i, k) = pkz_ad(i, k) + temp_ad8
7781  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pkz(i, &
7782 & k+1)*temp_ad8
7783  pkz_ad(i, k+1) = pkz_ad(i, k+1) + g_rat(i, k)*temp_ad8
7784  dd_ad(i, k) = 0.0
7785  bb_ad(i, k) = 0.0
7786  CALL poprealarray(g_rat(i, k))
7787  temp_ad9 = g_rat_ad(i, k)/delp(i, j, k+1)
7788  delp_ad(i, j, k) = delp_ad(i, j, k) + temp_ad9
7789  delp_ad(i, j, k+1) = delp_ad(i, j, k+1) - delp(i, j, k)*&
7790 & temp_ad9/delp(i, j, k+1)
7791  g_rat_ad(i, k) = 0.0
7792  END DO
7793  END DO
7794  DO k=npz,1,-1
7795  DO i=ilast,npx,-1
7796  temp7 = delz(i, j, k)
7797  temp6 = delp(i, j, k)*pt(i, j, k)
7798  temp4 = temp6/temp7
7799  temp5 = -(rgrav*rdgas*temp4)
7800  temp_ad7 = -(rgrav*rdgas*gama*exp(gama*log(temp5))*pkz_ad(&
7801 & i, k)/(temp5*temp7))
7802  pm_ad = -pkz_ad(i, k)
7803  temp8 = peln(i, k+1, j) - peln(i, k, j)
7804  temp_ad6 = -(delp(i, j, k)*pm_ad/temp8**2)
7805  delp_ad(i, j, k) = delp_ad(i, j, k) + pt(i, j, k)*temp_ad7&
7806 & + pm_ad/temp8
7807  peln_ad(i, k+1, j) = peln_ad(i, k+1, j) + temp_ad6
7808  peln_ad(i, k, j) = peln_ad(i, k, j) - temp_ad6
7809  CALL poprealarray(pkz(i, k))
7810  pt_ad(i, j, k) = pt_ad(i, j, k) + delp(i, j, k)*temp_ad7
7811  delz_ad(i, j, k) = delz_ad(i, j, k) - temp4*temp_ad7
7812  pkz_ad(i, k) = 0.0
7813  END DO
7814  END DO
7815  DO k=npz+1,2,-1
7816  DO i=ilast,npx,-1
7817  CALL poprealarray(peln(i, k, j))
7818  pe_ad(i, k, j) = pe_ad(i, k, j) + peln_ad(i, k, j)/pe(i, k&
7819 & , j)
7820  peln_ad(i, k, j) = 0.0
7821  CALL poprealarray(pe(i, k, j))
7822  pe_ad(i, k-1, j) = pe_ad(i, k-1, j) + pe_ad(i, k, j)
7823  delp_ad(i, j, k-1) = delp_ad(i, j, k-1) + pe_ad(i, k, j)
7824  pe_ad(i, k, j) = 0.0
7825  END DO
7826  END DO
7827  DO i=ilast,npx,-1
7828  CALL poprealarray(peln(i, 1, j))
7829  peln_ad(i, 1, j) = 0.0
7830  CALL poprealarray(pe(i, 1, j))
7831  pe_ad(i, 1, j) = 0.0
7832  END DO
7833  DO k=1,npz,1
7834  DO i=ilast,npx,-1
7835  CALL poprealarray(gz(i, j, k))
7836  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
7837  delz_ad(i, j, k) = delz_ad(i, j, k) - grav*gz_ad(i, j, k)
7838  gz_ad(i, j, k) = 0.0
7839  END DO
7840  END DO
7841  DO i=ilast,npx,-1
7842  CALL poprealarray(gz(i, j, npz+1))
7843  gz_ad(i, j, npz+1) = 0.0
7844  END DO
7845  END DO
7846  END IF
7847  CALL popcontrol(1,branch)
7848  IF (branch .EQ. 0) THEN
7849  DO j=jlast,jfirst,-1
7850  CALL popcontrol(1,branch)
7851  IF (branch .NE. 0) THEN
7852  DO k=npz+1,2,-1
7853  DO i=0,ifirst,-1
7854  CALL poprealarray(pk3(i, j, k))
7855  pe_ad(i, k, j) = pe_ad(i, k, j) + kappa*exp(kappa*log(pe&
7856 & (i, k, j)))*pk3_ad(i, j, k)/pe(i, k, j)
7857  pk3_ad(i, j, k) = 0.0
7858  END DO
7859  END DO
7860  DO i=0,ifirst,-1
7861  CALL poprealarray(pk3(i, j, 1))
7862  pk3_ad(i, j, 1) = 0.0
7863  END DO
7864  END IF
7865  CALL popcontrol(1,branch)
7866  IF (branch .EQ. 0) THEN
7867  DO k=1,npz+1,1
7868  DO i=0,ifirst,-1
7869  CALL poprealarray(pkc(i, j, k))
7870  pe_ad(i, k, j) = pe_ad(i, k, j) + pkc_ad(i, j, k)
7871  END DO
7872  END DO
7873  END IF
7874  END DO
7875  DO j=jlast,jfirst,-1
7876  DO k=2,npz,1
7877  DO i=0,ifirst,-1
7878  CALL poprealarray(pkc(i, j, k))
7879  gam_ad(i, k) = gam_ad(i, k) - pkc(i, j, k+1)*pkc_ad(i, j, &
7880 & k)
7881  pkc_ad(i, j, k+1) = pkc_ad(i, j, k+1) - gam(i, k)*pkc_ad(i&
7882 & , j, k)
7883  END DO
7884  END DO
7885  DO k=npz,2,-1
7886  DO i=0,ifirst,-1
7887  CALL poprealarray(pkc(i, j, k+1))
7888  temp_ad4 = pkc_ad(i, j, k+1)/bet(i)
7889  dd_ad(i, k) = dd_ad(i, k) + temp_ad4
7890  pkc_ad(i, j, k) = pkc_ad(i, j, k) - temp_ad4
7891  bet_ad(i) = bet_ad(i) - (dd(i, k)-pkc(i, j, k))*temp_ad4/&
7892 & bet(i)
7893  pkc_ad(i, j, k+1) = 0.0
7894  CALL poprealarray(bet(i))
7895  bb_ad(i, k) = bb_ad(i, k) + bet_ad(i)
7896  gam_ad(i, k) = gam_ad(i, k) - bet_ad(i)
7897  CALL poprealarray(gam(i, k))
7898  temp_ad5 = gam_ad(i, k)/bet(i)
7899  bet_ad(i) = -(g_rat(i, k-1)*temp_ad5/bet(i))
7900  g_rat_ad(i, k-1) = g_rat_ad(i, k-1) + temp_ad5
7901  gam_ad(i, k) = 0.0
7902  END DO
7903  END DO
7904  DO i=0,ifirst,-1
7905  CALL poprealarray(dd(i, npz))
7906  pkz_ad(i, npz) = pkz_ad(i, npz) + 3.*dd_ad(i, npz)
7907  dd_ad(i, npz) = 0.0
7908  bb_ad(i, npz) = 0.0
7909  CALL poprealarray(pkc(i, j, 2))
7910  temp_ad3 = pkc_ad(i, j, 2)/bet(i)
7911  dd_ad(i, 1) = dd_ad(i, 1) + temp_ad3
7912  bet_ad(i) = bet_ad(i) - dd(i, 1)*temp_ad3/bet(i)
7913  pkc_ad(i, j, 2) = 0.0
7914  CALL poprealarray(pkc(i, j, 1))
7915  pkc_ad(i, j, 1) = 0.0
7916  CALL poprealarray(bet(i))
7917  bb_ad(i, 1) = bb_ad(i, 1) + bet_ad(i)
7918  bet_ad(i) = 0.0
7919  END DO
7920  DO k=npz-1,1,-1
7921  DO i=0,ifirst,-1
7922  CALL poprealarray(dd(i, k))
7923  temp_ad1 = 3.*dd_ad(i, k)
7924  pkz_ad(i, k) = pkz_ad(i, k) + temp_ad1
7925  g_rat_ad(i, k) = g_rat_ad(i, k) + 2.*bb_ad(i, k) + pkz(i, &
7926 & k+1)*temp_ad1
7927  pkz_ad(i, k+1) = pkz_ad(i, k+1) + g_rat(i, k)*temp_ad1
7928  dd_ad(i, k) = 0.0
7929  bb_ad(i, k) = 0.0
7930  CALL poprealarray(g_rat(i, k))
7931  temp_ad2 = g_rat_ad(i, k)/delp(i, j, k+1)
7932  delp_ad(i, j, k) = delp_ad(i, j, k) + temp_ad2
7933  delp_ad(i, j, k+1) = delp_ad(i, j, k+1) - delp(i, j, k)*&
7934 & temp_ad2/delp(i, j, k+1)
7935  g_rat_ad(i, k) = 0.0
7936  END DO
7937  END DO
7938  DO k=npz,1,-1
7939  DO i=0,ifirst,-1
7940  temp2 = delz(i, j, k)
7941  temp1 = delp(i, j, k)*pt(i, j, k)
7942  temp = temp1/temp2
7943  temp0 = -(rgrav*rdgas*temp)
7944  temp_ad0 = -(rgrav*rdgas*gama*exp(gama*log(temp0))*pkz_ad(&
7945 & i, k)/(temp0*temp2))
7946  pm_ad = -pkz_ad(i, k)
7947  temp3 = peln(i, k+1, j) - peln(i, k, j)
7948  temp_ad = -(delp(i, j, k)*pm_ad/temp3**2)
7949  delp_ad(i, j, k) = delp_ad(i, j, k) + pt(i, j, k)*temp_ad0&
7950 & + pm_ad/temp3
7951  peln_ad(i, k+1, j) = peln_ad(i, k+1, j) + temp_ad
7952  peln_ad(i, k, j) = peln_ad(i, k, j) - temp_ad
7953  CALL poprealarray(pkz(i, k))
7954  pt_ad(i, j, k) = pt_ad(i, j, k) + delp(i, j, k)*temp_ad0
7955  delz_ad(i, j, k) = delz_ad(i, j, k) - temp*temp_ad0
7956  pkz_ad(i, k) = 0.0
7957  END DO
7958  END DO
7959  DO k=npz+1,2,-1
7960  DO i=0,ifirst,-1
7961  pe_ad(i, k, j) = pe_ad(i, k, j) + peln_ad(i, k, j)/pe(i, k&
7962 & , j)
7963  peln_ad(i, k, j) = 0.0
7964  CALL poprealarray(pe(i, k, j))
7965  pe_ad(i, k-1, j) = pe_ad(i, k-1, j) + pe_ad(i, k, j)
7966  delp_ad(i, j, k-1) = delp_ad(i, j, k-1) + pe_ad(i, k, j)
7967  pe_ad(i, k, j) = 0.0
7968  END DO
7969  END DO
7970  DO i=0,ifirst,-1
7971  peln_ad(i, 1, j) = 0.0
7972  pe_ad(i, 1, j) = 0.0
7973  END DO
7974  DO k=1,npz,1
7975  DO i=0,ifirst,-1
7976  CALL poprealarray(gz(i, j, k))
7977  gz_ad(i, j, k+1) = gz_ad(i, j, k+1) + gz_ad(i, j, k)
7978  delz_ad(i, j, k) = delz_ad(i, j, k) - grav*gz_ad(i, j, k)
7979  gz_ad(i, j, k) = 0.0
7980  END DO
7981  END DO
7982  DO i=0,ifirst,-1
7983  CALL poprealarray(gz(i, j, npz+1))
7984  gz_ad(i, j, npz+1) = 0.0
7985  END DO
7986  END DO
7987  END IF
7988  END IF
7989  END SUBROUTINE nest_halo_nh_bwd
7990  SUBROUTINE nest_halo_nh(ptop, grav, kappa, cp, delp, delz, pt, phis, &
7991 & pkc, gz, pk3, npx, npy, npz, nested, pkc_pertn, computepk3, fullhalo&
7992 & , bd)
7993  IMPLICIT NONE
7994 !INPUT: delp, delz, pt
7995 !OUTPUT: gz, pkc, pk3 (optional)
7996  INTEGER, INTENT(IN) :: npx, npy, npz
7997  LOGICAL, INTENT(IN) :: pkc_pertn, computepk3, fullhalo, nested
7998  REAL, INTENT(IN) :: ptop, kappa, cp, grav
7999  TYPE(fv_grid_bounds_type), INTENT(IN) :: bd
8000  REAL, INTENT(IN) :: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
8001  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz), INTENT(IN) :: pt&
8002 & , delp, delz
8003  REAL, DIMENSION(bd%isd:bd%ied, bd%jsd:bd%jed, npz+1), INTENT(INOUT) &
8004 & :: gz, pkc, pk3
8005  INTEGER :: i, j, k
8006 !'gamma'
8007  REAL :: gama
8008  REAL :: ptk, rgrav, rkap, peln1, rdg
8009  REAL, DIMENSION(bd%isd:bd%ied, npz+1, bd%jsd:bd%jed) :: pe, peln
8010  REAL, DIMENSION(bd%isd:bd%ied, npz) :: gam, bb, dd, pkz
8011  REAL, DIMENSION(bd%isd:bd%ied, npz-1) :: g_rat
8012  REAL, DIMENSION(bd%isd:bd%ied) :: bet
8013  REAL :: pm
8014  INTEGER :: ifirst, ilast, jfirst, jlast
8015  INTEGER :: is, ie, js, je
8016  INTEGER :: isd, ied, jsd, jed
8017  INTRINSIC log
8018  INTRINSIC exp
8019  is = bd%is
8020  ie = bd%ie
8021  js = bd%js
8022  je = bd%je
8023  isd = bd%isd
8024  ied = bd%ied
8025  jsd = bd%jsd
8026  jed = bd%jed
8027  IF (.NOT.nested) THEN
8028  RETURN
8029  ELSE
8030  ifirst = isd
8031  jfirst = jsd
8032  ilast = ied
8033  jlast = jed
8034 !Remember we want to compute these in the HALO. Note also this routine
8035 !requires an appropriate
8036  rgrav = 1./grav
8037  gama = 1./(1.-kappa)
8038  ptk = ptop**kappa
8039  rkap = 1./kappa
8040  peln1 = log(ptop)
8041  rdg = -(rdgas*rgrav)
8042 !NOTE: Compiler does NOT like this sort of nested-grid BC code. Is it trying to do some ugly optimization?
8043  IF (is .EQ. 1) THEN
8044  DO j=jfirst,jlast
8045 !GZ
8046  DO i=ifirst,0
8047  gz(i, j, npz+1) = phis(i, j)
8048  END DO
8049  DO k=npz,1,-1
8050  DO i=ifirst,0
8051  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
8052  END DO
8053  END DO
8054 !Hydrostatic interface pressure
8055  DO i=ifirst,0
8056  pe(i, 1, j) = ptop
8057  peln(i, 1, j) = peln1
8058  END DO
8059  DO k=2,npz+1
8060  DO i=ifirst,0
8061  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
8062  peln(i, k, j) = log(pe(i, k, j))
8063  END DO
8064  END DO
8065 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
8066  DO k=1,npz
8067  DO i=ifirst,0
8068 !Full p
8069  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
8070 & k)*rdgas*pt(i, j, k))))
8071 !hydro
8072  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8073 !Remove hydro cell-mean pressure
8074  pkz(i, k) = pkz(i, k) - pm
8075  END DO
8076  END DO
8077 !pressure solver
8078  DO k=1,npz-1
8079  DO i=ifirst,0
8080  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
8081  bb(i, k) = 2.*(1.+g_rat(i, k))
8082  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
8083  END DO
8084  END DO
8085  DO i=ifirst,0
8086  bet(i) = bb(i, 1)
8087  pkc(i, j, 1) = 0.
8088  pkc(i, j, 2) = dd(i, 1)/bet(i)
8089  bb(i, npz) = 2.
8090  dd(i, npz) = 3.*pkz(i, npz)
8091  END DO
8092  DO k=2,npz
8093  DO i=ifirst,0
8094  gam(i, k) = g_rat(i, k-1)/bet(i)
8095  bet(i) = bb(i, k) - gam(i, k)
8096  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
8097  END DO
8098  END DO
8099  DO k=npz,2,-1
8100  DO i=ifirst,0
8101  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
8102  END DO
8103  END DO
8104  END DO
8105  DO j=jfirst,jlast
8106  IF (.NOT.pkc_pertn) THEN
8107  DO k=npz+1,1,-1
8108  DO i=ifirst,0
8109  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
8110  END DO
8111  END DO
8112  END IF
8113 !pk3 if necessary; doesn't require condenstate loading calculation
8114  IF (computepk3) THEN
8115  DO i=ifirst,0
8116  pk3(i, j, 1) = ptk
8117  END DO
8118  DO k=2,npz+1
8119  DO i=ifirst,0
8120  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
8121  END DO
8122  END DO
8123  END IF
8124  END DO
8125  END IF
8126  IF (ie .EQ. npx - 1) THEN
8127  DO j=jfirst,jlast
8128 !GZ
8129  DO i=npx,ilast
8130  gz(i, j, npz+1) = phis(i, j)
8131  END DO
8132  DO k=npz,1,-1
8133  DO i=npx,ilast
8134  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
8135  END DO
8136  END DO
8137 !Hydrostatic interface pressure
8138  DO i=npx,ilast
8139  pe(i, 1, j) = ptop
8140  peln(i, 1, j) = peln1
8141  END DO
8142  DO k=2,npz+1
8143  DO i=npx,ilast
8144  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
8145  peln(i, k, j) = log(pe(i, k, j))
8146  END DO
8147  END DO
8148 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
8149  DO k=1,npz
8150  DO i=npx,ilast
8151 !Full p
8152  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
8153 & k)*rdgas*pt(i, j, k))))
8154 !hydro
8155  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8156 !Remove hydro cell-mean pressure
8157  pkz(i, k) = pkz(i, k) - pm
8158  END DO
8159  END DO
8160 !pressure solver
8161  DO k=1,npz-1
8162  DO i=npx,ilast
8163  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
8164  bb(i, k) = 2.*(1.+g_rat(i, k))
8165  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
8166  END DO
8167  END DO
8168  DO i=npx,ilast
8169  bet(i) = bb(i, 1)
8170  pkc(i, j, 1) = 0.
8171  pkc(i, j, 2) = dd(i, 1)/bet(i)
8172  bb(i, npz) = 2.
8173  dd(i, npz) = 3.*pkz(i, npz)
8174  END DO
8175  DO k=2,npz
8176  DO i=npx,ilast
8177  gam(i, k) = g_rat(i, k-1)/bet(i)
8178  bet(i) = bb(i, k) - gam(i, k)
8179  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
8180  END DO
8181  END DO
8182  DO k=npz,2,-1
8183  DO i=npx,ilast
8184  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
8185  END DO
8186  END DO
8187  END DO
8188  DO j=jfirst,jlast
8189  IF (.NOT.pkc_pertn) THEN
8190  DO k=npz+1,1,-1
8191  DO i=npx,ilast
8192  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
8193  END DO
8194  END DO
8195  END IF
8196 !pk3 if necessary
8197  IF (computepk3) THEN
8198  DO i=npx,ilast
8199  pk3(i, j, 1) = ptk
8200  END DO
8201  DO k=2,npz+1
8202  DO i=npx,ilast
8203  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
8204  END DO
8205  END DO
8206  END IF
8207  END DO
8208  END IF
8209  IF (js .EQ. 1) THEN
8210  DO j=jfirst,0
8211 !GZ
8212  DO i=ifirst,ilast
8213  gz(i, j, npz+1) = phis(i, j)
8214  END DO
8215  DO k=npz,1,-1
8216  DO i=ifirst,ilast
8217  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
8218  END DO
8219  END DO
8220 !Hydrostatic interface pressure
8221  DO i=ifirst,ilast
8222  pe(i, 1, j) = ptop
8223  peln(i, 1, j) = peln1
8224  END DO
8225  DO k=2,npz+1
8226  DO i=ifirst,ilast
8227  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
8228  peln(i, k, j) = log(pe(i, k, j))
8229  END DO
8230  END DO
8231 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
8232  DO k=1,npz
8233  DO i=ifirst,ilast
8234 !Full p
8235  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
8236 & k)*rdgas*pt(i, j, k))))
8237 !hydro
8238  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8239 !hydro
8240  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8241 !Remove hydro cell-mean pressure
8242  pkz(i, k) = pkz(i, k) - pm
8243  END DO
8244  END DO
8245 !pressure solver
8246  DO k=1,npz-1
8247  DO i=ifirst,ilast
8248  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
8249  bb(i, k) = 2.*(1.+g_rat(i, k))
8250  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
8251  END DO
8252  END DO
8253  DO i=ifirst,ilast
8254  bet(i) = bb(i, 1)
8255  pkc(i, j, 1) = 0.
8256  pkc(i, j, 2) = dd(i, 1)/bet(i)
8257  bb(i, npz) = 2.
8258  dd(i, npz) = 3.*pkz(i, npz)
8259  END DO
8260  DO k=2,npz
8261  DO i=ifirst,ilast
8262  gam(i, k) = g_rat(i, k-1)/bet(i)
8263  bet(i) = bb(i, k) - gam(i, k)
8264  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
8265  END DO
8266  END DO
8267  DO k=npz,2,-1
8268  DO i=ifirst,ilast
8269  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
8270  END DO
8271  END DO
8272  END DO
8273  DO j=jfirst,0
8274  IF (.NOT.pkc_pertn) THEN
8275  DO k=npz+1,1,-1
8276  DO i=ifirst,ilast
8277  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
8278  END DO
8279  END DO
8280  END IF
8281 !pk3 if necessary
8282  IF (computepk3) THEN
8283  DO i=ifirst,ilast
8284  pk3(i, j, 1) = ptk
8285  END DO
8286  DO k=2,npz+1
8287  DO i=ifirst,ilast
8288  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
8289  END DO
8290  END DO
8291  END IF
8292  END DO
8293  END IF
8294  IF (je .EQ. npy - 1) THEN
8295  DO j=npy,jlast
8296 !GZ
8297  DO i=ifirst,ilast
8298  gz(i, j, npz+1) = phis(i, j)
8299  END DO
8300  DO k=npz,1,-1
8301  DO i=ifirst,ilast
8302  gz(i, j, k) = gz(i, j, k+1) - delz(i, j, k)*grav
8303  END DO
8304  END DO
8305 !Hydrostatic interface pressure
8306  DO i=ifirst,ilast
8307  pe(i, 1, j) = ptop
8308  peln(i, 1, j) = peln1
8309  END DO
8310  DO k=2,npz+1
8311  DO i=ifirst,ilast
8312  pe(i, k, j) = pe(i, k-1, j) + delp(i, j, k-1)
8313  peln(i, k, j) = log(pe(i, k, j))
8314  END DO
8315  END DO
8316 !Perturbation nonhydro layer-mean pressure (NOT to the kappa)
8317  DO k=1,npz
8318  DO i=ifirst,ilast
8319 !Full p
8320  pkz(i, k) = exp(gama*log(-(delp(i, j, k)*rgrav/delz(i, j, &
8321 & k)*rdgas*pt(i, j, k))))
8322 !hydro
8323  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8324 !hydro
8325  pm = delp(i, j, k)/(peln(i, k+1, j)-peln(i, k, j))
8326 !Remove hydro cell-mean pressure
8327  pkz(i, k) = pkz(i, k) - pm
8328  END DO
8329  END DO
8330 !Reversible interpolation on layer NH pressure perturbation
8331 ! to recover lastge NH pressure perturbation
8332  DO k=1,npz-1
8333  DO i=ifirst,ilast
8334  g_rat(i, k) = delp(i, j, k)/delp(i, j, k+1)
8335  bb(i, k) = 2.*(1.+g_rat(i, k))
8336  dd(i, k) = 3.*(pkz(i, k)+g_rat(i, k)*pkz(i, k+1))
8337  END DO
8338  END DO
8339  DO i=ifirst,ilast
8340  bet(i) = bb(i, 1)
8341  pkc(i, j, 1) = 0.
8342  pkc(i, j, 2) = dd(i, 1)/bet(i)
8343  bb(i, npz) = 2.
8344  dd(i, npz) = 3.*pkz(i, npz)
8345  END DO
8346  DO k=2,npz
8347  DO i=ifirst,ilast
8348  gam(i, k) = g_rat(i, k-1)/bet(i)
8349  bet(i) = bb(i, k) - gam(i, k)
8350  pkc(i, j, k+1) = (dd(i, k)-pkc(i, j, k))/bet(i)
8351  END DO
8352  END DO
8353  DO k=npz,2,-1
8354  DO i=ifirst,ilast
8355  pkc(i, j, k) = pkc(i, j, k) - gam(i, k)*pkc(i, j, k+1)
8356  END DO
8357  END DO
8358  END DO
8359  DO j=npy,jlast
8360  IF (.NOT.pkc_pertn) THEN
8361  DO k=npz+1,1,-1
8362  DO i=ifirst,ilast
8363  pkc(i, j, k) = pkc(i, j, k) + pe(i, k, j)
8364  END DO
8365  END DO
8366  END IF
8367 !pk3 if necessary
8368  IF (computepk3) THEN
8369  DO i=ifirst,ilast
8370  pk3(i, j, 1) = ptk
8371  END DO
8372  DO k=2,npz+1
8373  DO i=ifirst,ilast
8374  pk3(i, j, k) = exp(kappa*log(pe(i, k, j)))
8375  END DO
8376  END DO
8377  END IF
8378  END DO
8379  END IF
8380  END IF
8381  END SUBROUTINE nest_halo_nh
8382 end module nh_utils_adm_mod
subroutine, public sim_solver(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe2, dm2, pm2, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
subroutine, public sim1_solver_fwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe, dm2, pm2, pem, w2, dz2, pt2, ws, p_fac)
subroutine, public fill_4corners_fwd(q, dir, bd, npx, npy, sw_corner, se_corner, ne_corner, nw_corner)
subroutine, public update_dz_d_fwd(ndif, damp, hord, is, ie, js, je, km, ng, npx, npy, area, rarea, dp0, zs, zh, crx, cry, xfx, yfx, delz, ws, rdt, gridstruct, bd, hord_pert)
subroutine, public nest_halo_nh(ptop, grav, kappa, cp, delp, delz, pt, phis, pkc, gz, pk3, npx, npy, npz, nested, pkc_pertn, computepk3, fullhalo, bd)
subroutine, public rim_2d(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, dm2, pm2, w2, dz2, pt2, ws, c_core)
real, parameter dz_min
subroutine edge_profile_bwd(q1, q1_ad, q2, q2_ad, q1e, q1e_ad, q2e, q2e_ad, i1, i2, j1, j2, j, km, dp0, uniform_grid, limiter)
subroutine, public pushcontrol(ctype, field)
subroutine, public fv_tp_2d_adm(q, q_ad, crx, crx_ad, cry, cry_ad, npx, npy, hord, fx, fx_ad, fy, fy_ad, xfx, xfx_ad, yfx, yfx_ad, gridstruct, bd, ra_x, ra_x_ad, ra_y, ra_y_ad, mfx, mfx_ad, mfy, mfy_ad, mass, mass_ad, nord, damp_c)
subroutine imp_diff_w(j, is, ie, js, je, ng, km, cd, delz, ws, w, w3)
subroutine edge_profile(q1, q2, q1e, q2e, i1, i2, j1, j2, j, km, dp0, uniform_grid, limiter)
subroutine, public rim_2d_fwd(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, dm2, pm2, w2, dz2, pt2, ws, c_core)
real, parameter, public rdgas
Gas constant for dry air [J/kg/deg].
Definition: constants.F90:77
subroutine, public update_dz_d_bwd(ndif, damp, hord, is, ie, js, je, km, ng, npx, npy, area, rarea, dp0, zs, zh, zh_ad, crx, crx_ad, cry, cry_ad, xfx, xfx_ad, yfx, yfx_ad, delz, ws, ws_ad, rdt, gridstruct, bd, hord_pert)
subroutine, public fv_tp_2d_fwd(q, crx, cry, npx, npy, hord, fx, fy, xfx, yfx, gridstruct, bd, ra_x, ra_y, mfx, mfy, mass, nord, damp_c)
subroutine, public sim_solver_fwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe2, dm2, pm2, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
subroutine, public sim1_solver_bwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe, pe_ad, dm2, dm2_ad, pm2, pm2_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad, ws, ws_ad, p_fac)
subroutine edge_profile_fwd(q1, q2, q1e, q2e, i1, i2, j1, j2, j, km, dp0, uniform_grid, limiter)
subroutine, public sim3p0_solver_bwd(dt, is, ie, km, rgas, gama, kappa, pe2, pe2_ad, dm, dm_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad, ws, ws_ad, p_fac, scale_m)
subroutine, public update_dz_d(ndif, damp, hord, is, ie, js, je, km, ng, npx, npy, area, rarea, dp0, zs, zh, crx, cry, xfx, yfx, delz, ws, rdt, gridstruct, bd, hord_pert)
subroutine, public sim3p0_solver(dt, is, ie, km, rgas, gama, kappa, pe2, dm, pem, w2, dz2, pt2, ws, p_fac, scale_m)
subroutine, public fv_tp_2d(q, crx, cry, npx, npy, hord, fx, fy, xfx, yfx, gridstruct, bd, ra_x, ra_y, mfx, mfy, mass, nord, damp_c)
subroutine, public update_dz_c_fwd(is, ie, js, je, km, ng, dt, dp0, zs, area, ut, vt, gz, ws, npx, npy, sw_corner, se_corner, ne_corner, nw_corner, bd, grid_type)
subroutine, public sim3p0_solver_fwd(dt, is, ie, km, rgas, gama, kappa, pe2, dm, pem, w2, dz2, pt2, ws, p_fac, scale_m)
subroutine, public update_dz_c(is, ie, js, je, km, ng, dt, dp0, zs, area, ut, vt, gz, ws, npx, npy, sw_corner, se_corner, ne_corner, nw_corner, bd, grid_type)
subroutine, public del6_vt_flux(nord, npx, npy, damp, q, d2, fx2, fy2, gridstruct, bd)
subroutine, public sim3_solver_bwd(dt, is, ie, km, rgas, gama, kappa, pe2, pe2_ad, dm, dm_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad, ws, ws_ad, alpha, p_fac, scale_m)
subroutine, public fill_4corners_bwd(q, q_ad, dir, bd, npx, npy, sw_corner, se_corner, ne_corner, nw_corner)
real, parameter, public cp_air
Specific heat capacity of dry air at constant pressure [J/kg/deg].
Definition: constants.F90:83
subroutine, public sim_solver_bwd(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe2, pe2_ad, dm2, dm2_ad, pm2, pm2_ad, pem, pem_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad, ws, ws_ad, alpha, p_fac, scale_m)
subroutine, public sim1_solver(dt, is, ie, km, rgas, gama, gm2, cp2, kappa, pe, dm2, pm2, pem, w2, dz2, pt2, ws, p_fac)
subroutine, public fv_tp_2d_bwd(q, q_ad, crx, crx_ad, cry, cry_ad, npx, npy, hord, fx, fx_ad, fy, fy_ad, xfx, xfx_ad, yfx, yfx_ad, gridstruct, bd, ra_x, ra_x_ad, ra_y, ra_y_ad, mfx, mfx_ad, mfy, mfy_ad, mass, mass_ad, nord, damp_c)
subroutine, public nest_halo_nh_bwd(ptop, grav, kappa, cp, delp, delp_ad, delz, delz_ad, pt, pt_ad, phis, pkc, pkc_ad, gz, gz_ad, pk3, pk3_ad, npx, npy, npz, nested, pkc_pertn, computepk3, fullhalo, bd)
subroutine, public riem_solver_c_fwd(ms, dt, is, ie, js, je, km, ng, akap, cappa, cp, ptop, hs, w3, pt, q_con, delp, gz, pef, ws, p_fac, a_imp, scale_m)
subroutine, public sim3_solver(dt, is, ie, km, rgas, gama, kappa, pe2, dm, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
subroutine, public riem_solver_c_bwd(ms, dt, is, ie, js, je, km, ng, akap, cappa, cp, ptop, hs, w3, w3_ad, pt, pt_ad, q_con, delp, delp_ad, gz, gz_ad, pef, pef_ad, ws, ws_ad, p_fac, a_imp, scale_m)
real, parameter, public grav
Acceleration due to gravity [m/s^2].
Definition: constants.F90:76
subroutine, public rim_2d_bwd(ms, bdt, is, ie, km, rgas, gama, gm2, pe2, pe2_ad, dm2, dm2_ad, pm2, pm2_ad, w2, w2_ad, dz2, dz2_ad, pt2, pt2_ad, ws, ws_ad, c_core)
subroutine edge_scalar(q1, qe, i1, i2, km, id)
#define max(a, b)
Definition: mosaic_util.h:33
real, parameter r3
subroutine, public riem_solver_c(ms, dt, is, ie, js, je, km, ng, akap, cappa, cp, ptop, hs, w3, pt, q_con, delp, gz, pef, ws, p_fac, a_imp, scale_m)
subroutine, public sim3_solver_fwd(dt, is, ie, km, rgas, gama, kappa, pe2, dm, pem, w2, dz2, pt2, ws, alpha, p_fac, scale_m)
subroutine, public nest_halo_nh_fwd(ptop, grav, kappa, cp, delp, delz, pt, phis, pkc, gz, pk3, npx, npy, npz, nested, pkc_pertn, computepk3, fullhalo, bd)
subroutine, public update_dz_c_bwd(is, ie, js, je, km, ng, dt, dp0, zs, area, ut, ut_ad, vt, vt_ad, gz, gz_ad, ws, ws_ad, npx, npy, sw_corner, se_corner, ne_corner, nw_corner, bd, grid_type)
subroutine riem_solver3test(ms, dt, is, ie, js, je, km, ng, isd, ied, jsd, jed, akap, cappa, cp, ptop, zs, q_con, w, delz, pt, delp, zh, pe, ppe, pk3, pk, peln, ws, scale_m, p_fac, a_imp, use_logp, last_call, fp_out)
subroutine, public fill_4corners(q, dir, bd, npx, npy, sw_corner, se_corner, ne_corner, nw_corner)
subroutine, public popcontrol(ctype, field)
Derived type containing the data.
subroutine, public del6_vt_flux_adm(nord, npx, npy, damp, q, q_ad, d2, d2_ad, fx2, fx2_ad, fy2, fy2_ad, gridstruct, bd)