FV3 Bundle
gw_drag_b.F90
Go to the documentation of this file.
1 ! Generated by TAPENADE (INRIA, Tropics team)
2 ! Tapenade 3.2 (r3024) - 06/17/2009 13:03
3 !
4 ! $Id: gw_drag_b.F90,v 1.2 2013/09/10 19:30:14 jgkim Exp $
5 MODULE gw_drag_b
8  USE gw_drag_init
9 
10 
11  IMPLICIT NONE
12 !---------------------------------------------------------------------------------
13 ! Purpose:
14 !
15 ! Module to compute the forcing due to parameterized gravity waves. Both an
16 ! orographic and an internal source spectrum are considered.
17 !
18 ! Author: Byron Boville
19 ! In-Sun Song
20 !
21 !---------------------------------------------------------------------------------
22 !save
23 ! Make default type private to the module_src
24  PRIVATE
25 !
26 ! PUBLIC: interfaces
27 !
28 ! interface to actual parameterization
29 ! PUBLIC gw_intr, gw_sub_intr, gw_drag_prof, gw_bgnd, gw_oro, gw_prof
30  PUBLIC gw_main_b
31 !, gw_drag_prof_b, gw_bgnd_b, gw_drag_prof_bgnd_b
32 
33 !, gw_intr_fwd, gw_intr_bwd, gw_drag_prof_fwd, gw_drag_prof_bwd, &
34 !& gw_oro_fwd, gw_oro_bwd, gw_prof_fwd, gw_prof_bwd
35 ! PUBLIC gw_main_b, gw_intr_fwd, gw_intr_bwd, gw_drag_prof_fwd, gw_drag_prof_bwd, &
36 !& gw_bgnd_fwd, gw_bgnd_bwd, gw_oro_fwd, gw_oro_bwd
37 !
38 ! PRIVATE: Rest of the data and interfaces are private to this module
39 !
40 ! effective horizontal wave number for background
41  REAL, PARAMETER, PUBLIC :: kwvb=6.28e-5
42 ! effective horizontal wave number for background
43  REAL, PARAMETER, PUBLIC :: kwvbeq=6.28e-5/7.
44 ! effective horizontal wave number for orographic
45  REAL, PARAMETER, PUBLIC :: kwvo=6.28e-5
46 ! fraction of stress deposited in low level region
47  REAL, PARAMETER, PUBLIC :: fracldv=0.0
48 ! max asymmetry between tau(c) and tau(-c)
49  REAL, PARAMETER, PUBLIC :: mxasym=0.1
50 ! max range of tau for all c
51  REAL, PARAMETER, PUBLIC :: mxrange=0.001
52 ! min value of bouyancy frequency
53  REAL, PARAMETER, PUBLIC :: n2min=1.e-8
54 ! critical froude number
55  REAL, PARAMETER, PUBLIC :: fcrit2=0.5
56 ! min surface displacment height for orographic waves
57  REAL, PARAMETER, PUBLIC :: orohmin=10.
58 ! min wind speed for orographic waves
59  REAL, PARAMETER, PUBLIC :: orovmin=2.0
60 ! background source strength (/TAUSCAL)
61  REAL, PARAMETER, PUBLIC :: taubgnd=6.4
62 ! minimum (nonzero) stress
63  REAL, PARAMETER, PUBLIC :: taumin=1.e-10
64 ! scale factor for background stress source
65  REAL, PARAMETER, PUBLIC :: tauscal=0.001
66 ! maximum wind tendency
67  REAL, PARAMETER, PUBLIC :: tndmax=500./86400.
68 ! factor to limit tendency to prevent reversing u-c
69  REAL, PARAMETER, PUBLIC :: umcfac=0.5
70 ! min (u-c)**2
71  REAL, PARAMETER, PUBLIC :: ubmc2mn=0.1
72 ! constant for determining zldv from tau0
73  REAL, PARAMETER, PUBLIC :: zldvcon=10.
74  REAL, PARAMETER, PUBLIC :: rog=mapl_rgas/mapl_grav
75 ! 1/2 * horizontal wavenumber
76  REAL, PARAMETER, PUBLIC :: oroko2=0.5*kwvo
77 ! This is *not* MAPL_PI
78  REAL, PARAMETER, PUBLIC :: pi_gwd=4.0*atan(1.0)
79 ! -----------------------------------------------
80 
81 CONTAINS
82 
83 SUBROUTINE gw_main_b(pcols, pver, dt, pgwv, effgworo_dev, &
84 & effgwbkg_dev, pint_dev, t_dev, u_dev, u_devb, v_dev, v_devb, &
85 & sgh_dev, pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, &
86 & qvt_dev, rog, mapl_vireps_, rlat_dev)
87  IMPLICIT NONE
88 !-----------------------------------------------------------------------
89 ! Interface for multiple gravity wave drag parameterization.
90 !-----------------------------------------------------------------------
91 !------------------------------Arguments--------------------------------
92 ! - number of columns
93  INTEGER :: pcols
94 ! - number of vertical layers
95  INTEGER :: pver
96 ! - time step
97  REAL :: dt
98 ! - number of waves allowed (Default = 4, 0 nullifies)
99  INTEGER :: pgwv
100 ! - tendency efficiency for background gwd (Default = 0.125)
101  REAL :: effgwbkg_dev(pcols)
102 ! - tendency efficiency for orographic gwd (Default = 0.125)
103  REAL :: effgworo_dev(pcols)
104 ! - pressure at the layer edges
105  REAL :: pint_dev(pcols, pver+1)
106 ! - temperature at layers
107  REAL :: t_dev(pcols, pver)
108 ! - zonal wind at layers
109  REAL :: u_dev(pcols, pver)
110  REAL :: u_devb(pcols, pver)
111 ! - meridional wind at layers
112  REAL :: v_dev(pcols, pver)
113  REAL :: v_devb(pcols, pver)
114 ! e standard deviation of orography
115  REAL :: sgh_dev(pcols)
116 ! e reference pressure at the layeredges
117  REAL :: pref_dev(pver+1)
118 ! c pressure at the layers
119  REAL :: pmid_dev(pcols, pver)
120 ! c pressure thickness at the layers
121  REAL :: pdel_dev(pcols, pver)
122 ! c 1.0 / pdel
123  REAL :: rpdel_dev(pcols, pver)
124 ! c log(pint)
125  REAL :: lnpint_dev(pcols, pver+1)
126 ! c height above surface at layers
127  REAL :: zm_dev(pcols, pver)
128 ! c height above surface at layers
129  REAL :: zi_dev(pcols, pver+1)
130 ! c pressure at the layers
131  REAL :: qvt_dev(pcols, pver)
132 !mg latitude in radian
133  REAL :: rlat_dev(pcols)
134 ! zonal wind tendency at layer
135  REAL :: dudt_gwd_dev(pcols, pver)
136  REAL :: dudt_gwd_devb(pcols, pver)
137 ! meridional wind tendency at layer
138  REAL :: dvdt_gwd_dev(pcols, pver)
139  REAL :: dvdt_gwd_devb(pcols, pver)
140 ! temperature tendency at layer
141  REAL :: dtdt_gwd_dev(pcols, pver)
142  REAL :: dtdt_gwd_devb(pcols, pver)
143 ! zonal wind tendency at layer due to orography GWD
144  REAL :: dudt_org_dev(pcols, pver)
145 ! meridional wind tendency at layer due to orography GWD
146  REAL :: dvdt_org_dev(pcols, pver)
147 ! temperature tendency at layer due to orography GWD
148  REAL :: dtdt_org_dev(pcols, pver)
149 ! zonal gravity wave surface stress
150  REAL :: taugwdx_dev(pcols)
151 ! meridional gravity wave surface stress
152  REAL :: taugwdy_dev(pcols)
153 ! zonal orographic gravity wave stress
154  REAL :: tauox_dev(pcols, pver+1)
155 ! meridional orographic gravity wave stress
156  REAL :: tauoy_dev(pcols, pver+1)
157 ! energy flux of orographic gravity waves
158  REAL :: feo_dev(pcols, pver+1)
159 ! pseudoenergy flux of orographic gravity waves
160  REAL :: fepo_dev(pcols, pver+1)
161 ! zonal gravity wave background stress
162  REAL :: taubkgx_dev(pcols)
163 ! meridional gravity wave background stress
164  REAL :: taubkgy_dev(pcols)
165 ! zonal background gravity wave stress
166  REAL :: taubx_dev(pcols, pver+1)
167 ! meridional background gravity wave stress
168  REAL :: tauby_dev(pcols, pver+1)
169 ! energy flux of background gravity waves
170  REAL :: feb_dev(pcols, pver+1)
171 ! pseudoenergy flux of background gravity waves
172  REAL :: fepb_dev(pcols, pver+1)
173 ! dU/dt below background launch level
174  REAL :: utbsrc_dev(pcols, pver)
175 ! dV/dt below background launch level
176  REAL :: vtbsrc_dev(pcols, pver)
177 ! dT/dt below background launch level
178  REAL :: ttbsrc_dev(pcols, pver)
179 !---------------------------Local storage-------------------------------
180 ! loop indexes
181  INTEGER :: i, k, kc
182 ! launch-level index for orographic
183  INTEGER :: kbotoro
184 ! launch-level index for background
185  INTEGER :: kbotbg
186 ! top interface of gwd region
187  INTEGER :: ktopbg, ktoporo
188 ! top interface of low level stress divergence region
189  INTEGER :: kldv
190 ! min value of kldv
191  INTEGER :: kldvmn
192 ! index of top interface of source region
193  INTEGER :: ksrc
194 ! min value of ksrc
195  INTEGER :: ksrcmn
196 ! temperature tendency
197  REAL :: ttgw(pver)
198 ! zonal wind tendency
199  REAL :: utgw(pver)
200 ! meridional wind tendency
201  REAL :: vtgw(pver)
202 ! interface Brunt-Vaisalla frequency
203  REAL :: ni(0:pver)
204 ! midpoint Brunt-Vaisalla frequency
205  REAL :: nm(pver)
206 ! 1/dp across low level divergence region
207  REAL :: rdpldv
208 ! interface density
209  REAL :: rhoi(0:pver)
210 ! c=0 sfc. stress (zonal)
211  REAL :: tau0x
212 ! c=0 sfc. stress (meridional)
213  REAL :: tau0y
214 ! interface temperature
215  REAL :: ti(0:pver)
216 ! projection of wind at interfaces
217  REAL :: ubi(0:pver)
218 ! projection of wind at midpoints
219  REAL :: ubm(pver)
220 ! unit vectors of source wind (x)
221  REAL :: xv
222 ! unit vectors of source wind (y)
223  REAL :: yv
224  REAL :: utosrc(pver)
225  REAL :: vtosrc(pver)
226  REAL :: ttosrc(pver)
227 ! newtonian cooling coefficients
228  REAL :: alpha(0:pver)
229 ! newtonian cooling coefficients
230  REAL :: dback(0:pver)
231 ! wave phase speeds
232  REAL :: c(-pgwv:pgwv)
233 ! - temperature at layers
234  REAL :: att_dev(pcols, pver)
235  REAL :: att_devb(pcols, pver)
236  REAL :: pwr1
237  REAL :: pwx2
238  REAL :: pwy2
239  REAL :: pwr2
240  REAL :: hkl, hkk, tvfac, tv, rog
241  REAL :: mapl_vireps_
242 
244 
245  DO i=1,pcols
246  DO k=1,pver
247  att_dev(i, k) = t_dev(i, k)*mapl_p00**mapl_kappa*(mapl_p00/&
248 & pint_dev(i, k))**(-mapl_kappa)
249  END DO
250  END DO
251  DO i=1,pcols
252  zi_dev(i, pver+1) = 0.
253  DO k=pver,1,-1
254  hkl = lnpint_dev(i, k+1) - lnpint_dev(i, k)
255  hkk = 1. - pint_dev(i, k)*hkl*rpdel_dev(i, k)
256  tvfac = 1. + mapl_vireps_*qvt_dev(i, k)
257  tv = att_dev(i, k)*tvfac
258  zm_dev(i, k) = zi_dev(i, k+1) + rog*tv*hkk
259  zi_dev(i, k) = zi_dev(i, k+1) + rog*tv*hkl
260  END DO
261  END DO
262 
263  DO i=1,pcols
264  If (.not.bypass_bgnd) then
265  do_bgnd=.true.
266  do_oro=.false.
267 
268  CALL gw_intr_fwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
269 & effgwbkg_dev, pint_dev, att_dev, u_dev, v_dev, sgh_dev, &
270 & pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, &
271 & zm_dev, rlat_dev, dudt_gwd_dev, dvdt_gwd_dev, &
272 & dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, &
273 & taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, &
274 & taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, &
275 & fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
276  Endif
277 
278  If (.not.bypass_oro) then
279  do_bgnd=.false.
280  do_oro=.true.
281 
282  CALL gw_intr_fwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
283 & effgwbkg_dev, pint_dev, att_dev, u_dev, v_dev, sgh_dev, &
284 & pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, &
285 & zm_dev, rlat_dev, dudt_gwd_dev, dvdt_gwd_dev, &
286 & dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, &
287 & taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, &
288 & taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, &
289 & fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
290  Endif
291 
292  If (.not.bypass_oro) then
293  dvdt_gwd_devb = 0.0
294  dudt_gwd_devb = 0.0
295 
296  do_bgnd=.false.
297  do_oro=.true.
298 
299  CALL gw_intr_bwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
300 & effgwbkg_dev, pint_dev, att_dev, u_dev, u_devb&
301 & , v_dev, v_devb, sgh_dev, pref_dev, pmid_dev, pdel_dev, &
302 & rpdel_dev, lnpint_dev, zm_dev, rlat_dev, dudt_gwd_dev, &
303 & dudt_gwd_devb, dvdt_gwd_dev, dvdt_gwd_devb, dtdt_gwd_dev&
304 & , dudt_org_dev, dvdt_org_dev, dtdt_org_dev&
305 & , taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev&
306 & , taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev&
307 & , fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
308  Endif
309 
310  If (.not.bypass_bgnd) then
311  dvdt_gwd_devb = 0.0
312  dudt_gwd_devb = 0.0
313 
314  do_bgnd=.true.
315  do_oro=.false.
316 
317  CALL gw_intr_bwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
318 & effgwbkg_dev, pint_dev, att_dev, u_dev, u_devb&
319 & , v_dev, v_devb, sgh_dev, pref_dev, pmid_dev, pdel_dev, &
320 & rpdel_dev, lnpint_dev, zm_dev, rlat_dev, dudt_gwd_dev, &
321 & dudt_gwd_devb, dvdt_gwd_dev, dvdt_gwd_devb, dtdt_gwd_dev&
322 & , dudt_org_dev, dvdt_org_dev, dtdt_org_dev&
323 & , taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev&
324 & , taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev&
325 & , fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
326  Endif
327  END DO
328 
329 END SUBROUTINE gw_main_b
330 
331  SUBROUTINE gw_intr_fwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
332 & effgwbkg_dev, pint_dev, t_dev, u_dev, v_dev, sgh_dev, pref_dev, &
333 & pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, rlat_dev, &
334 & dudt_gwd_dev, dvdt_gwd_dev, dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev&
335 & , dtdt_org_dev, taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, &
336 & feo_dev, taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, &
337 & fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
338  IMPLICIT NONE
339 !-----------------------------------------------------------------------
340 ! Interface for multiple gravity wave drag parameterization.
341 !-----------------------------------------------------------------------
342 !------------------------------Arguments--------------------------------
343 ! - number of columns
344  INTEGER, INTENT(IN) :: pcols
345 ! - number of vertical layers
346  INTEGER, INTENT(IN) :: pver
347 ! - time step
348  REAL, INTENT(IN) :: dt
349 ! - number of waves allowed (Default = 4, 0 nullifies)
350  INTEGER, INTENT(IN) :: pgwv
351 ! - tendency efficiency for background gwd (Default = 0.125)
352  REAL :: effgwbkg_dev(pcols)
353 ! - tendency efficiency for orographic gwd (Default = 0.125)
354  REAL :: effgworo_dev(pcols)
355 ! - pressure at the layer edges
356  REAL :: pint_dev(pcols, pver+1)
357 ! - temperature at layers
358  REAL :: t_dev(pcols, pver)
359 ! - zonal wind at layers
360  REAL :: u_dev(pcols, pver)
361 ! - meridional wind at layers
362  REAL :: v_dev(pcols, pver)
363 ! e standard deviation of orography
364  REAL :: sgh_dev(pcols)
365 ! e reference pressure at the layeredges
366  REAL :: pref_dev(pver+1)
367 ! c pressure at the layers
368  REAL :: pmid_dev(pcols, pver)
369 ! c pressure thickness at the layers
370  REAL :: pdel_dev(pcols, pver)
371 ! c 1.0 / pdel
372  REAL :: rpdel_dev(pcols, pver)
373 ! c log(pint)
374  REAL :: lnpint_dev(pcols, pver+1)
375 ! c height above surface at layers
376  REAL :: zm_dev(pcols, pver)
377 !mg latitude in radian
378  REAL :: rlat_dev(pcols)
379 ! zonal wind tendency at layer
380  REAL :: dudt_gwd_dev(pcols, pver)
381 ! meridional wind tendency at layer
382  REAL :: dvdt_gwd_dev(pcols, pver)
383 ! temperature tendency at layer
384  REAL :: dtdt_gwd_dev(pcols, pver)
385 ! zonal wind tendency at layer due to orography GWD
386  REAL :: dudt_org_dev(pcols, pver)
387 ! meridional wind tendency at layer due to orography GWD
388  REAL :: dvdt_org_dev(pcols, pver)
389 ! temperature tendency at layer due to orography GWD
390  REAL :: dtdt_org_dev(pcols, pver)
391 ! zonal gravity wave surface stress
392  REAL :: taugwdx_dev(pcols)
393 ! meridional gravity wave surface stress
394  REAL :: taugwdy_dev(pcols)
395 ! zonal orographic gravity wave stress
396  REAL :: tauox_dev(pcols, pver+1)
397 ! meridional orographic gravity wave stress
398  REAL :: tauoy_dev(pcols, pver+1)
399 ! energy flux of orographic gravity waves
400  REAL :: feo_dev(pcols, pver+1)
401 ! pseudoenergy flux of orographic gravity waves
402  REAL :: fepo_dev(pcols, pver+1)
403 ! zonal gravity wave background stress
404  REAL :: taubkgx_dev(pcols)
405 ! meridional gravity wave background stress
406  REAL :: taubkgy_dev(pcols)
407 ! zonal background gravity wave stress
408  REAL :: taubx_dev(pcols, pver+1)
409 ! meridional background gravity wave stress
410  REAL :: tauby_dev(pcols, pver+1)
411 ! energy flux of background gravity waves
412  REAL :: feb_dev(pcols, pver+1)
413 ! pseudoenergy flux of background gravity waves
414  REAL :: fepb_dev(pcols, pver+1)
415 ! dU/dt below background launch level
416  REAL :: utbsrc_dev(pcols, pver)
417 ! dV/dt below background launch level
418  REAL :: vtbsrc_dev(pcols, pver)
419 ! dT/dt below background launch level
420  REAL :: ttbsrc_dev(pcols, pver)
421 !---------------------------Local storage-------------------------------
422 ! loop indexes
423  INTEGER :: i, ii, k, kc
424 ! launch-level index for orographic
425  INTEGER :: kbotoro
426 ! launch-level index for background
427  INTEGER :: kbotbg
428 ! top interface of gwd region
429  INTEGER :: ktopbg, ktoporo
430 ! top interface of low level stress divergence region
431  INTEGER :: kldv
432 ! min value of kldv
433  INTEGER :: kldvmn
434 ! index of top interface of source region
435  INTEGER :: ksrc
436 ! min value of ksrc
437  INTEGER :: ksrcmn
438 ! temperature tendency
439  REAL :: ttgw(pver)
440 ! zonal wind tendency
441  REAL :: utgw(pver)
442 ! meridional wind tendency
443  REAL :: vtgw(pver)
444 ! interface Brunt-Vaisalla frequency
445  REAL :: ni(0:pver)
446 ! midpoint Brunt-Vaisalla frequency
447  REAL :: nm(pver)
448 ! 1/dp across low level divergence region
449  REAL :: rdpldv
450 ! interface density
451  REAL :: rhoi(0:pver)
452 ! c=0 sfc. stress (zonal)
453  REAL :: tau0x
454 ! c=0 sfc. stress (meridional)
455  REAL :: tau0y
456 ! interface temperature
457  REAL :: ti(0:pver)
458 ! projection of wind at interfaces
459  REAL :: ubi(0:pver)
460 ! projection of wind at midpoints
461  REAL :: ubm(pver)
462 ! unit vectors of source wind (x)
463  REAL :: xv
464 ! unit vectors of source wind (y)
465  REAL :: yv
466  REAL :: utosrc(pver)
467  REAL :: vtosrc(pver)
468  REAL :: ttosrc(pver)
469 ! newtonian cooling coefficients
470  REAL :: alpha(0:pver)
471 ! newtonian cooling coefficients
472  REAL :: dback(0:pver)
473 ! wave phase speeds
474  REAL :: c(-pgwv:pgwv)
475 ! wave Reynolds stress
476  REAL :: tau(-pgwv:pgwv, 0:pver)
477 ! Assign wave phase speeds
478 ! ------------------------
479  DO kc=-pgwv,pgwv
480  c(kc) = 10.0*kc
481  END DO
482 ! Determine the bounds of the background and orographic stress regions
483  ktoporo = 0
484  ktopbg = 0
485  kbotoro = pver
486  DO k=0,pver
487  IF (pref_dev(k+1) .LT. p_src) kbotbg = k
488 ! spectrum source at 400 mb
489  END DO
490  DO k=0,pver
491 ! Profiles of background state variables
492  CALL gw_prof(i, k, pcols, pver, u_dev, v_dev, t_dev, pmid_dev, &
493 & pint_dev, rhoi, ni, ti, nm)
494  END DO
495 !-----------------------------------------------------------------------------
496 ! Non-orographic backgound gravity wave spectrum
497 !-----------------------------------------------------------------------------
498  IF (pgwv .GT. 0 .AND. do_bgnd) THEN
499 ! Determine the wave source for a background spectrum at ~400 mb
500  CALL gw_bgnd_fwd(i, pcols, pver, c, u_dev, v_dev, t_dev, pmid_dev&
501 & , pint_dev, pdel_dev, rpdel_dev, lnpint_dev, rlat_dev, &
502 & kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, &
503 & yv, pgwv, kbotbg)
504  CALL pushreal4array(vtosrc, pver)
505  CALL pushreal4array(utosrc, pver)
506  CALL pushreal4array(vtgw, pver)
507  CALL pushreal4array(utgw, pver)
508  CALL pushreal4array(tau, (2*pgwv+1)*(pver+1))
509 ! Solve for the drag profile
510  CALL gw_drag_prof_bgnd(i, pcols, pver, pgwv, pgwv, kbotbg, ktopbg&
511 & , c, u_dev, v_dev, t_dev, pint_dev, pdel_dev, &
512 & rpdel_dev, lnpint_dev, rlat_dev, rhoi, ni, ti, nm&
513 & , dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, &
514 & rdpldv, tau, ubi, ubm, xv, yv, utgw, vtgw, ttgw, &
515 & taubx_dev, tauby_dev, feb_dev, fepb_dev, utosrc, &
516 & vtosrc, ttosrc, tau0x, tau0y, effgwbkg_dev)
517 ! Add the momentum tendencies to the output tendency arrays
518  DO k=1,pver
519  dudt_gwd_dev(i, k) = utgw(k) + utosrc(k)
520  dvdt_gwd_dev(i, k) = vtgw(k) + vtosrc(k)
521  END DO
522  CALL pushinteger4(0)
523  ELSE
524 ! dudt_gwd_dev(i,k) = utgw(k) + utosrc(k)
525 ! dvdt_gwd_dev(i,k) = vtgw(k) + vtosrc(k)
526 ! dtdt_gwd_dev(i,k) = ttgw(k) + ttosrc(k)
527 ! zero net tendencies if no spectrum computed
528  DO k=1,pver
529  dudt_gwd_dev(i, k) = 0.
530  dvdt_gwd_dev(i, k) = 0.
531  END DO
532  CALL pushinteger4(1)
533  END IF
534 ! dtdt_gwd_dev(i,k) = 0.
535 !-----------------------------------------------------------------------------
536 ! Orographic stationary gravity wave
537 !-----------------------------------------------------------------------------
538 ! Determine the orographic wave source
539  IF (do_oro) THEN
540  CALL gw_oro_fwd(i, pcols, pver, pgwv, u_dev, v_dev, t_dev, sgh_dev&
541 & , pmid_dev, pint_dev, pdel_dev, zm_dev, nm, kldv, kldvmn&
542 & , ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, kbotoro, &
543 & rlat_dev)
544 ! Solve for the drag profile
545  CALL gw_drag_prof_fwd(i, pcols, pver, pgwv, 0, kbotoro, ktoporo, c&
546 & , u_dev, v_dev, t_dev, pint_dev, pdel_dev, &
547 & rpdel_dev, lnpint_dev, rlat_dev, rhoi, ni, ti, nm&
548 & , dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, &
549 & rdpldv, tau, ubi, ubm, xv, yv, utgw, vtgw, ttgw, &
550 & tauox_dev, tauoy_dev, feo_dev, fepo_dev, utosrc, &
551 & vtosrc, ttosrc, tau0x, tau0y, effgworo_dev)
552 ! Add the orographic tendencies to the spectrum tendencies
553 ! Compute the temperature tendency from energy conservation (includes spectrum).
554  DO k=1,pver
555  dudt_gwd_dev(i, k) = dudt_gwd_dev(i, k) + utgw(k)
556  dvdt_gwd_dev(i, k) = dvdt_gwd_dev(i, k) + vtgw(k)
557  END DO
558  CALL pushinteger4(1)
559  ELSE
560  CALL pushinteger4(0)
561  END IF
562 ! dtdt_gwd_dev(i,k) = dtdt_gwd_dev(i,k) + ttgw(k)
563  DO k=1,pver
564  CALL pushreal4(u_dev(i, k))
565  u_dev(i, k) = u_dev(i, k) + dudt_gwd_dev(i, k)*dt
566  CALL pushreal4(v_dev(i, k))
567  v_dev(i, k) = v_dev(i, k) + dvdt_gwd_dev(i, k)*dt
568  END DO
569  CALL pushinteger4(ktoporo)
570  CALL pushreal4array(ni, pver + 1)
571  CALL pushreal4array(vtgw, pver)
572  CALL pushreal4array(utgw, pver)
573  CALL pushinteger4(kbotbg)
574  CALL pushreal4(xv)
575  CALL pushinteger4(kbotoro)
576  CALL pushreal4array(rhoi, pver + 1)
577  CALL pushreal4(yv)
578  CALL pushinteger4(ktopbg)
579  CALL pushreal4array(ubi, pver + 1)
580  CALL pushreal4array(c, 2*pgwv + 1)
581  END SUBROUTINE gw_intr_fwd
582 ! Differentiation of gw_intr in reverse (adjoint) mode (backward sweep):
583 ! gradient, with respect to input variables: dvdt_gwd_dev u_dev
584 ! dudt_gwd_dev v_dev
585 ! of linear combination of output variables: dvdt_gwd_dev u_dev
586 ! dudt_gwd_dev v_dev
587  SUBROUTINE gw_intr_bwd(i, pcols, pver, dt, pgwv, effgworo_dev, &
588 & effgwbkg_dev, pint_dev, t_dev, u_dev, u_devb, v_dev, v_devb, sgh_dev&
589 & , pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, &
590 & rlat_dev, dudt_gwd_dev, dudt_gwd_devb, dvdt_gwd_dev, dvdt_gwd_devb, &
591 & dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, taugwdx_dev&
592 & , taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, taubkgx_dev, &
593 & taubkgy_dev, taubx_dev, tauby_dev, feb_dev, fepo_dev, fepb_dev, &
594 & utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
595  IMPLICIT NONE
596  INTEGER, INTENT(IN) :: pcols
597  INTEGER, INTENT(IN) :: pver
598  REAL, INTENT(IN) :: dt
599  INTEGER, INTENT(IN) :: pgwv
600  REAL :: effgwbkg_dev(pcols)
601  REAL :: effgworo_dev(pcols)
602  REAL :: pint_dev(pcols, pver+1)
603  REAL :: t_dev(pcols, pver)
604  REAL :: u_dev(pcols, pver)
605  REAL :: u_devb(pcols, pver)
606  REAL :: v_dev(pcols, pver)
607  REAL :: v_devb(pcols, pver)
608  REAL :: sgh_dev(pcols)
609  REAL :: pref_dev(pver+1)
610  REAL :: pmid_dev(pcols, pver)
611  REAL :: pdel_dev(pcols, pver)
612  REAL :: rpdel_dev(pcols, pver)
613  REAL :: lnpint_dev(pcols, pver+1)
614  REAL :: zm_dev(pcols, pver)
615  REAL :: rlat_dev(pcols)
616  REAL :: dudt_gwd_dev(pcols, pver)
617  REAL :: dudt_gwd_devb(pcols, pver)
618  REAL :: dvdt_gwd_dev(pcols, pver)
619  REAL :: dvdt_gwd_devb(pcols, pver)
620  REAL :: dtdt_gwd_dev(pcols, pver)
621  REAL :: dudt_org_dev(pcols, pver)
622  REAL :: dvdt_org_dev(pcols, pver)
623  REAL :: dtdt_org_dev(pcols, pver)
624  REAL :: taugwdx_dev(pcols)
625  REAL :: taugwdy_dev(pcols)
626  REAL :: tauox_dev(pcols, pver+1)
627  REAL :: tauoy_dev(pcols, pver+1)
628  REAL :: feo_dev(pcols, pver+1)
629  REAL :: fepo_dev(pcols, pver+1)
630  REAL :: taubkgx_dev(pcols)
631  REAL :: taubkgy_dev(pcols)
632  REAL :: taubx_dev(pcols, pver+1)
633  REAL :: tauby_dev(pcols, pver+1)
634  REAL :: feb_dev(pcols, pver+1)
635  REAL :: fepb_dev(pcols, pver+1)
636  REAL :: utbsrc_dev(pcols, pver)
637  REAL :: vtbsrc_dev(pcols, pver)
638  REAL :: ttbsrc_dev(pcols, pver)
639  INTEGER :: i, ii, k, kc
640  INTEGER :: kbotoro
641  INTEGER :: kbotbg
642  INTEGER :: ktopbg, ktoporo
643  INTEGER :: kldv
644  INTEGER :: kldvmn
645  INTEGER :: ksrc
646  INTEGER :: ksrcmn
647  REAL :: ttgw(pver)
648  REAL :: utgw(pver)
649  REAL :: utgwb(pver)
650  REAL :: vtgw(pver)
651  REAL :: vtgwb(pver)
652  REAL :: ni(0:pver)
653  REAL :: nm(pver)
654  REAL :: rdpldv
655  REAL :: rhoi(0:pver)
656  REAL :: tau0x
657  REAL :: tau0y
658  REAL :: ti(0:pver)
659  REAL :: ubi(0:pver)
660  REAL :: ubib(0:pver)
661  REAL :: ubm(pver)
662  REAL :: ubmb(pver)
663  REAL :: xv
664  REAL :: xvb
665  REAL :: yv
666  REAL :: yvb
667  REAL :: utosrc(pver)
668  REAL :: utosrcb(pver)
669  REAL :: vtosrc(pver)
670  REAL :: vtosrcb(pver)
671  REAL :: ttosrc(pver)
672  REAL :: alpha(0:pver)
673  REAL :: dback(0:pver)
674  REAL :: c(-pgwv:pgwv)
675  REAL :: tau(-pgwv:pgwv, 0:pver)
676  REAL :: taub(-pgwv:pgwv, 0:pver)
677  INTEGER :: branch
678  CALL popreal4array(c, 2*pgwv + 1)
679  CALL popreal4array(ubi, pver + 1)
680  CALL popinteger4(ktopbg)
681  CALL popreal4(yv)
682  CALL popreal4array(rhoi, pver + 1)
683  CALL popinteger4(kbotoro)
684  CALL popreal4(xv)
685  CALL popinteger4(kbotbg)
686  CALL popreal4array(utgw, pver)
687  CALL popreal4array(vtgw, pver)
688  CALL popreal4array(ni, pver + 1)
689  CALL popinteger4(ktoporo)
690  DO k=pver,1,-1
691  CALL popreal4(v_dev(i, k))
692  dvdt_gwd_devb(i, k) = dvdt_gwd_devb(i, k) + dt*v_devb(i, k)
693  CALL popreal4(u_dev(i, k))
694  dudt_gwd_devb(i, k) = dudt_gwd_devb(i, k) + dt*u_devb(i, k)
695  END DO
696  CALL popinteger4(branch)
697  IF (branch .LT. 1) THEN
698  taub(:, :) = 0.0_8
699  vtgwb(:) = 0.0_8
700  utgwb(:) = 0.0_8
701  ubib(:) = 0.0_8
702  ubmb(:) = 0.0_8
703  ELSE
704  vtgwb(:) = 0.0_8
705  utgwb(:) = 0.0_8
706  DO k=pver,1,-1
707  vtgwb(k) = vtgwb(k) + dvdt_gwd_devb(i, k)
708  utgwb(k) = utgwb(k) + dudt_gwd_devb(i, k)
709  END DO
710  CALL gw_drag_prof_bwd(i, pcols, pver, pgwv, 0, kbotoro, ktoporo, c&
711 & , u_dev, v_dev, t_dev, pint_dev, pdel_dev, &
712 & rpdel_dev, lnpint_dev, rlat_dev, rhoi, ni, ti, nm&
713 & , dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, &
714 & rdpldv, tau, taub, ubi, ubib, ubm, xv, xvb, yv, &
715 & yvb, utgw, utgwb, vtgw, vtgwb, ttgw, tauox_dev, &
716 & tauoy_dev, feo_dev, fepo_dev, utosrc, vtosrc, &
717 & ttosrc, tau0x, tau0y, effgworo_dev)
718  CALL gw_oro_bwd(i, pcols, pver, pgwv, u_dev, u_devb, v_dev, v_devb&
719 & , t_dev, sgh_dev, pmid_dev, pint_dev, pdel_dev, zm_dev, &
720 & nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, &
721 & ubib, ubm, ubmb, xv, xvb, yv, yvb, kbotoro, rlat_dev)
722  END IF
723  CALL popinteger4(branch)
724  IF (branch .LT. 1) THEN
725  vtosrcb(:) = 0.0_8
726  utosrcb(:) = 0.0_8
727  DO k=pver,1,-1
728  vtgwb(k) = vtgwb(k) + dvdt_gwd_devb(i, k)
729  vtosrcb(k) = vtosrcb(k) + dvdt_gwd_devb(i, k)
730  dvdt_gwd_devb(i, k) = 0.0_8
731  utgwb(k) = utgwb(k) + dudt_gwd_devb(i, k)
732  utosrcb(k) = utosrcb(k) + dudt_gwd_devb(i, k)
733  dudt_gwd_devb(i, k) = 0.0_8
734  END DO
735  CALL popreal4array(tau, (2*pgwv+1)*(pver+1))
736  CALL popreal4array(utgw, pver)
737  CALL popreal4array(vtgw, pver)
738  CALL popreal4array(utosrc, pver)
739  CALL popreal4array(vtosrc, pver)
740  CALL gw_drag_prof_bgnd_b(i, pcols, pver, pgwv, pgwv, kbotbg, &
741 & ktopbg, c, u_dev, v_dev, t_dev, pint_dev, &
742 & pdel_dev, rpdel_dev, lnpint_dev, rlat_dev, rhoi&
743 & , ni, ti, nm, dt, alpha, dback, kldv, kldvmn, &
744 & ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, ubm&
745 & , xv, xvb, yv, yvb, utgw, utgwb, vtgw, vtgwb, &
746 & ttgw, taubx_dev, tauby_dev, feb_dev, fepb_dev, &
747 & utosrc, utosrcb, vtosrc, vtosrcb, ttosrc, tau0x&
748 & , tau0y, effgwbkg_dev)
749  CALL gw_bgnd_bwd(i, pcols, pver, c, u_dev, u_devb, v_dev, v_devb, &
750 & t_dev, pmid_dev, pint_dev, pdel_dev, rpdel_dev, &
751 & lnpint_dev, rlat_dev, kldv, kldvmn, ksrc, ksrcmn, &
752 & rdpldv, tau, ubi, ubib, ubm, ubmb, xv, xvb, yv, yvb, &
753 & pgwv, kbotbg)
754  ELSE
755  DO k=pver,1,-1
756  dvdt_gwd_devb(i, k) = 0.0_8
757  dudt_gwd_devb(i, k) = 0.0_8
758  END DO
759  END IF
760  END SUBROUTINE gw_intr_bwd
761  SUBROUTINE gw_intr(i, pcols, pver, dt, pgwv, effgworo_dev, &
762 & effgwbkg_dev, pint_dev, t_dev, u_dev, v_dev, sgh_dev, pref_dev, &
763 & pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, rlat_dev, &
764 & dudt_gwd_dev, dvdt_gwd_dev, dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev&
765 & , dtdt_org_dev, taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, &
766 & feo_dev, taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, &
767 & fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
768  IMPLICIT NONE
769 !-----------------------------------------------------------------------
770 ! Interface for multiple gravity wave drag parameterization.
771 !-----------------------------------------------------------------------
772 !------------------------------Arguments--------------------------------
773 ! - number of columns
774  INTEGER, INTENT(IN) :: pcols
775 ! - number of vertical layers
776  INTEGER, INTENT(IN) :: pver
777 ! - time step
778  REAL, INTENT(IN) :: dt
779 ! - number of waves allowed (Default = 4, 0 nullifies)
780  INTEGER, INTENT(IN) :: pgwv
781 ! - tendency efficiency for background gwd (Default = 0.125)
782  REAL :: effgwbkg_dev(pcols)
783 ! - tendency efficiency for orographic gwd (Default = 0.125)
784  REAL :: effgworo_dev(pcols)
785 ! - pressure at the layer edges
786  REAL :: pint_dev(pcols, pver+1)
787 ! - temperature at layers
788  REAL :: t_dev(pcols, pver)
789 ! - zonal wind at layers
790  REAL :: u_dev(pcols, pver)
791 ! - meridional wind at layers
792  REAL :: v_dev(pcols, pver)
793 ! e standard deviation of orography
794  REAL :: sgh_dev(pcols)
795 ! e reference pressure at the layeredges
796  REAL :: pref_dev(pver+1)
797 ! c pressure at the layers
798  REAL :: pmid_dev(pcols, pver)
799 ! c pressure thickness at the layers
800  REAL :: pdel_dev(pcols, pver)
801 ! c 1.0 / pdel
802  REAL :: rpdel_dev(pcols, pver)
803 ! c log(pint)
804  REAL :: lnpint_dev(pcols, pver+1)
805 ! c height above surface at layers
806  REAL :: zm_dev(pcols, pver)
807 !mg latitude in radian
808  REAL :: rlat_dev(pcols)
809 ! zonal wind tendency at layer
810  REAL :: dudt_gwd_dev(pcols, pver)
811 ! meridional wind tendency at layer
812  REAL :: dvdt_gwd_dev(pcols, pver)
813 ! temperature tendency at layer
814  REAL :: dtdt_gwd_dev(pcols, pver)
815 ! zonal wind tendency at layer due to orography GWD
816  REAL :: dudt_org_dev(pcols, pver)
817 ! meridional wind tendency at layer due to orography GWD
818  REAL :: dvdt_org_dev(pcols, pver)
819 ! temperature tendency at layer due to orography GWD
820  REAL :: dtdt_org_dev(pcols, pver)
821 ! zonal gravity wave surface stress
822  REAL :: taugwdx_dev(pcols)
823 ! meridional gravity wave surface stress
824  REAL :: taugwdy_dev(pcols)
825 ! zonal orographic gravity wave stress
826  REAL :: tauox_dev(pcols, pver+1)
827 ! meridional orographic gravity wave stress
828  REAL :: tauoy_dev(pcols, pver+1)
829 ! energy flux of orographic gravity waves
830  REAL :: feo_dev(pcols, pver+1)
831 ! pseudoenergy flux of orographic gravity waves
832  REAL :: fepo_dev(pcols, pver+1)
833 ! zonal gravity wave background stress
834  REAL :: taubkgx_dev(pcols)
835 ! meridional gravity wave background stress
836  REAL :: taubkgy_dev(pcols)
837 ! zonal background gravity wave stress
838  REAL :: taubx_dev(pcols, pver+1)
839 ! meridional background gravity wave stress
840  REAL :: tauby_dev(pcols, pver+1)
841 ! energy flux of background gravity waves
842  REAL :: feb_dev(pcols, pver+1)
843 ! pseudoenergy flux of background gravity waves
844  REAL :: fepb_dev(pcols, pver+1)
845 ! dU/dt below background launch level
846  REAL :: utbsrc_dev(pcols, pver)
847 ! dV/dt below background launch level
848  REAL :: vtbsrc_dev(pcols, pver)
849 ! dT/dt below background launch level
850  REAL :: ttbsrc_dev(pcols, pver)
851 !---------------------------Local storage-------------------------------
852 ! loop indexes
853  INTEGER :: i, ii, k, kc
854 ! launch-level index for orographic
855  INTEGER :: kbotoro
856 ! launch-level index for background
857  INTEGER :: kbotbg
858 ! top interface of gwd region
859  INTEGER :: ktopbg, ktoporo
860 ! top interface of low level stress divergence region
861  INTEGER :: kldv
862 ! min value of kldv
863  INTEGER :: kldvmn
864 ! index of top interface of source region
865  INTEGER :: ksrc
866 ! min value of ksrc
867  INTEGER :: ksrcmn
868 ! temperature tendency
869  REAL :: ttgw(pver)
870 ! zonal wind tendency
871  REAL :: utgw(pver)
872 ! meridional wind tendency
873  REAL :: vtgw(pver)
874 ! interface Brunt-Vaisalla frequency
875  REAL :: ni(0:pver)
876 ! midpoint Brunt-Vaisalla frequency
877  REAL :: nm(pver)
878 ! 1/dp across low level divergence region
879  REAL :: rdpldv
880 ! interface density
881  REAL :: rhoi(0:pver)
882 ! c=0 sfc. stress (zonal)
883  REAL :: tau0x
884 ! c=0 sfc. stress (meridional)
885  REAL :: tau0y
886 ! interface temperature
887  REAL :: ti(0:pver)
888 ! projection of wind at interfaces
889  REAL :: ubi(0:pver)
890 ! projection of wind at midpoints
891  REAL :: ubm(pver)
892 ! unit vectors of source wind (x)
893  REAL :: xv
894 ! unit vectors of source wind (y)
895  REAL :: yv
896  REAL :: utosrc(pver)
897  REAL :: vtosrc(pver)
898  REAL :: ttosrc(pver)
899 ! newtonian cooling coefficients
900  REAL :: alpha(0:pver)
901 ! newtonian cooling coefficients
902  REAL :: dback(0:pver)
903 ! wave phase speeds
904  REAL :: c(-pgwv:pgwv)
905 ! wave Reynolds stress
906  REAL :: tau(-pgwv:pgwv, 0:pver)
907 !-----------------------------------------------------------------------------
908 ! Assign newtonian cooling coefficients
909 ! -------------------------------------
910  DO k=0,pver
911  alpha(k) = 0.0
912  dback(k) = 0.0
913  END DO
914 ! Assign wave phase speeds
915 ! ------------------------
916  DO kc=-pgwv,pgwv
917  c(kc) = 10.0*kc
918  END DO
919 ! Determine the bounds of the background and orographic stress regions
920  ktoporo = 0
921  ktopbg = 0
922  kbotoro = pver
923  DO k=0,pver
924  IF (pref_dev(k+1) .LT. p_src) kbotbg = k
925 ! spectrum source at 400 mb
926  END DO
927  DO k=0,pver
928 ! Profiles of background state variables
929  CALL gw_prof(i, k, pcols, pver, u_dev, v_dev, t_dev, pmid_dev, &
930 & pint_dev, rhoi, ni, ti, nm)
931  END DO
932 !-----------------------------------------------------------------------------
933 ! Non-orographic backgound gravity wave spectrum
934 !-----------------------------------------------------------------------------
935  IF (pgwv .GT. 0 .AND. do_bgnd) THEN
936 ! Determine the wave source for a background spectrum at ~400 mb
937  CALL gw_bgnd(i, pcols, pver, c, u_dev, v_dev, t_dev, pmid_dev, &
938 & pint_dev, pdel_dev, rpdel_dev, lnpint_dev, rlat_dev, kldv, &
939 & kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, pgwv, &
940 & kbotbg)
941 ! Solve for the drag profile
942  CALL gw_drag_prof_bgnd(i, pcols, pver, pgwv, pgwv, kbotbg, ktopbg&
943 & , c, u_dev, v_dev, t_dev, pint_dev, pdel_dev, &
944 & rpdel_dev, lnpint_dev, rlat_dev, rhoi, ni, ti, nm&
945 & , dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, &
946 & rdpldv, tau, ubi, ubm, xv, yv, utgw, vtgw, ttgw, &
947 & taubx_dev, tauby_dev, feb_dev, fepb_dev, utosrc, &
948 & vtosrc, ttosrc, tau0x, tau0y, effgwbkg_dev)
949 ! Add the momentum tendencies to the output tendency arrays
950  DO k=1,pver
951  dudt_gwd_dev(i, k) = utgw(k) + utosrc(k)
952  dvdt_gwd_dev(i, k) = vtgw(k) + vtosrc(k)
953  END DO
954  ELSE
955 ! dudt_gwd_dev(i,k) = utgw(k) + utosrc(k)
956 ! dvdt_gwd_dev(i,k) = vtgw(k) + vtosrc(k)
957 ! dtdt_gwd_dev(i,k) = ttgw(k) + ttosrc(k)
958 ! zero net tendencies if no spectrum computed
959  DO k=1,pver
960  dudt_gwd_dev(i, k) = 0.
961  dvdt_gwd_dev(i, k) = 0.
962  END DO
963  END IF
964 ! dtdt_gwd_dev(i,k) = 0.
965 !-----------------------------------------------------------------------------
966 ! Orographic stationary gravity wave
967 !-----------------------------------------------------------------------------
968 ! Determine the orographic wave source
969  IF (do_oro) THEN
970  CALL gw_oro(i, pcols, pver, pgwv, u_dev, v_dev, t_dev, sgh_dev, &
971 & pmid_dev, pint_dev, pdel_dev, zm_dev, nm, kldv, kldvmn, ksrc&
972 & , ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, kbotoro, rlat_dev)
973 ! Solve for the drag profile
974  CALL gw_drag_prof(i, pcols, pver, pgwv, 0, kbotoro, ktoporo, c, &
975 & u_dev, v_dev, t_dev, pint_dev, pdel_dev, rpdel_dev, &
976 & lnpint_dev, rlat_dev, rhoi, ni, ti, nm, dt, alpha, &
977 & dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, &
978 & ubm, xv, yv, utgw, vtgw, ttgw, tauox_dev, tauoy_dev, &
979 & feo_dev, fepo_dev, utosrc, vtosrc, ttosrc, tau0x, &
980 & tau0y, effgworo_dev)
981 ! Add the orographic tendencies to the spectrum tendencies
982 ! Compute the temperature tendency from energy conservation (includes spectrum).
983  DO k=1,pver
984  dudt_gwd_dev(i, k) = dudt_gwd_dev(i, k) + utgw(k)
985  dvdt_gwd_dev(i, k) = dvdt_gwd_dev(i, k) + vtgw(k)
986  END DO
987  END IF
988 ! dtdt_gwd_dev(i,k) = dtdt_gwd_dev(i,k) + ttgw(k)
989  DO k=1,pver
990  u_dev(i, k) = u_dev(i, k) + dudt_gwd_dev(i, k)*dt
991  v_dev(i, k) = v_dev(i, k) + dvdt_gwd_dev(i, k)*dt
992  END DO
993 ! t_dev(i,k) = t_dev(i,k) + dtdt_gwd_dev(i,k)*dt
994  RETURN
995  END SUBROUTINE gw_intr
996 !================================================================================
997  SUBROUTINE gw_prof(i, k, pcols, pver, u, v, t, pm, pi, rhoi, ni, ti, &
998 & nm)
999  IMPLICIT NONE
1000 !-----------------------------------------------------------------------
1001 ! Compute profiles of background state quantities for the multiple
1002 ! gravity wave drag parameterization.
1003 !
1004 ! The parameterization is assumed to operate only where water vapor
1005 ! concentrations are negligible in determining the density.
1006 !-----------------------------------------------------------------------
1007 !------------------------------Arguments--------------------------------
1008 ! current atmospheric column
1009  INTEGER, INTENT(IN) :: i
1010 ! current atmospheric layer
1011  INTEGER, INTENT(IN) :: k
1012 ! number of atmospheric columns
1013  INTEGER, INTENT(IN) :: pcols
1014 ! number of vertical layers
1015  INTEGER, INTENT(IN) :: pver
1016 ! midpoint zonal wind
1017  REAL :: u(pcols, pver)
1018 ! midpoint meridional wind
1019  REAL :: v(pcols, pver)
1020 ! midpoint temperatures
1021  REAL :: t(pcols, pver)
1022 ! midpoint pressures
1023  REAL :: pm(pcols, pver)
1024 ! interface pressures
1025  REAL :: pi(pcols, 0:pver)
1026 ! interface density
1027  REAL :: rhoi(0:pver)
1028 ! interface Brunt-Vaisalla frequency
1029  REAL :: ni(0:pver)
1030 ! interface temperature
1031  REAL :: ti(0:pver)
1032 ! midpoint Brunt-Vaisalla frequency
1033  REAL :: nm(pver)
1034 !---------------------------Local storage-------------------------------
1035  REAL :: dtdp
1036 ! Brunt-Vaisalla frequency squared
1037  REAL :: n2
1038 ! Brunt-Vaisalla frequency squared
1039  REAL :: t_new, t_new_
1040  INTRINSIC max
1041  INTRINSIC sqrt
1042  REAL :: max1
1043 !-----------------------------------------------------------------------------
1044 ! Determine the interface densities and Brunt-Vaisala frequencies.
1045 !-----------------------------------------------------------------------------
1046 ! The top interface values are calculated assuming an isothermal atmosphere
1047 ! above the top level.
1048  IF (k .EQ. 0) THEN
1049  ti(k) = t(i, k+1)
1050  CALL get_ti(t_new, ti(k))
1051  rhoi(k) = pi(i, k)/(mapl_rgas*t_new)
1052  ni(k) = sqrt(mapl_grav*mapl_grav/(mapl_cp*t_new))
1053 ! Interior points use centered differences
1054  ELSE IF (k .GT. 0 .AND. k .LT. pver) THEN
1055  ti(k) = 0.5*(t(i, k)+t(i, k+1))
1056  CALL get_ti(t_new, ti(k))
1057  rhoi(k) = pi(i, k)/(mapl_rgas*t_new)
1058  dtdp = (t(i, k+1)-t(i, k))/(pm(i, k+1)-pm(i, k))
1059  CALL get_ti(t_new_, dtdp)
1060  n2 = mapl_grav*mapl_grav/t_new*(1./mapl_cp-rhoi(k)*t_new_)
1061  IF (n2min .LT. n2) THEN
1062  max1 = n2
1063  ELSE
1064  max1 = n2min
1065  END IF
1066  ni(k) = sqrt(max1)
1067 ! Bottom interface uses bottom level temperature, density; next interface
1068 ! B-V frequency.
1069  ELSE IF (k .EQ. pver) THEN
1070  ti(k) = t(i, k)
1071  CALL get_ti(t_new, ti(k))
1072  rhoi(k) = pi(i, k)/(mapl_rgas*t_new)
1073  ni(k) = ni(k-1)
1074  END IF
1075 !-----------------------------------------------------------------------------
1076 ! Determine the midpoint Brunt-Vaisala frequencies.
1077 !-----------------------------------------------------------------------------
1078  IF (k .GT. 0) nm(k) = 0.5*(ni(k-1)+ni(k))
1079  RETURN
1080  END SUBROUTINE gw_prof
1081 ! Differentiation of gw_oro in reverse (adjoint) mode (forward sweep):
1082 ! gradient, with respect to input variables: tau u v ubi ubm
1083 ! of linear combination of output variables: tau u v xv yv ubi
1084 !================================================================================
1085  SUBROUTINE gw_oro_fwd(i, pcols, pver, pgwv, u, v, t, sgh, pm, pi, dpm&
1086 & , zm, nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv&
1087 & , kbot, rlat)
1088  IMPLICIT NONE
1089 !-----------------------------------------------------------------------
1090 ! Orographic source for multiple gravity wave drag parameterization.
1091 !
1092 ! The stress is returned for a single wave with c=0, over orography.
1093 ! For points where the orographic variance is small (including ocean),
1094 ! the returned stress is zero.
1095 !------------------------------Arguments--------------------------------
1096 ! number of current column
1097  INTEGER, INTENT(IN) :: i
1098 ! number of atmospheric columns
1099  INTEGER, INTENT(IN) :: pcols
1100 ! number of atmospheric columns
1101  INTEGER, INTENT(IN) :: pver
1102 ! number of waves allowed
1103  INTEGER, INTENT(IN) :: pgwv
1104 ! midpoint zonal wind
1105  REAL :: u(pcols, pver)
1106 ! midpoint meridional wind
1107  REAL :: v(pcols, pver)
1108 ! midpoint temperatures
1109  REAL :: t(pcols, pver)
1110 ! standard deviation of orography
1111  REAL :: sgh(pcols)
1112 ! midpoint pressures
1113  REAL :: pm(pcols, pver)
1114 ! interface pressures
1115  REAL :: pi(pcols, 0:pver)
1116 ! midpoint delta p (pi(k)-pi(k-1))
1117  REAL :: dpm(pcols, pver)
1118 ! midpoint heights
1119  REAL :: zm(pcols, pver)
1120 ! midpoint Brunt-Vaisalla frequency
1121  REAL :: nm(pver)
1122 ! top interface of low level stress div region
1123  INTEGER :: kldv
1124 ! min value of kldv
1125  INTEGER :: kldvmn
1126 ! index of top interface of source region
1127  INTEGER :: ksrc
1128 ! min value of ksrc
1129  INTEGER :: ksrcmn
1130 ! 1/dp across low level divergence region
1131  REAL :: rdpldv
1132 ! wave Reynolds stress
1133  REAL :: tau(-pgwv:pgwv, 0:pver)
1134 ! projection of wind at interfaces
1135  REAL :: ubi(0:pver)
1136 ! projection of wind at midpoints
1137  REAL :: ubm(pver)
1138 ! unit vectors of source wind (x)
1139  REAL :: xv
1140 ! unit vectors of source wind (y)
1141  REAL :: yv
1142  INTEGER :: kbot
1143  REAL :: rlat(pcols)
1144 !---------------------------Local storage-------------------------------
1145 ! loop indexes
1146  INTEGER :: k
1147 ! Source-layer basic-state wind
1148  REAL :: ubsrc
1149 ! surface streamline displacment height (2*sgh)
1150  REAL :: hdsp
1151 ! max orographic sdv to use
1152  REAL :: sghmax
1153 ! c=0 stress from orography
1154  REAL :: tauoro
1155 ! real :: zldv ! top of the low level stress divergence region
1156 ! b-f frequency averaged over source region
1157  REAL :: nsrc
1158 ! interface pressure at top of source region
1159  REAL :: psrc
1160 ! density averaged over source region
1161  REAL :: rsrc
1162 ! u wind averaged over source region
1163  REAL :: usrc
1164 ! v wind averaged over source region
1165  REAL :: vsrc
1166  REAL :: u_new, v_new, t_new
1167  REAL :: min1
1168  INTRINSIC min
1169  INTRINSIC sqrt
1170 ! Begins
1171 !---------------------------------------------------------------------------
1172 ! Average the basic state variables for the wave source over the depth of
1173 ! the orographic standard deviation. Here we assume that the apropiate
1174 ! values of wind, stability, etc. for determining the wave source are
1175 ! averages over the depth of the atmosphere pentrated by the typical mountain.
1176 ! Reduces to the bottom midpoint values when sgh=0, such as over ocean.
1177 !
1178 ! Also determine the depth of the low level stress divergence region, as
1179 ! the max of the boundary layer depth and the source region depth. This
1180 ! can be done here if the stress magnitude does not determine the depth,
1181 ! otherwise it must be done below.
1182 !---------------------------------------------------------------------------
1183  ksrc = pver - 1
1184  psrc = pi(i, pver-1)
1185  CALL get_ti(t_new, t(i, pver))
1186  rsrc = pm(i, pver)/(mapl_rgas*t_new)*dpm(i, pver)
1187  CALL get_uv(u_new, u(i, pver))
1188  CALL get_uv(v_new, v(i, pver))
1189  usrc = u_new*dpm(i, pver)
1190  vsrc = v_new*dpm(i, pver)
1191  nsrc = nm(pver)*dpm(i, pver)
1192  hdsp = 2.0*sgh(i)
1193  DO k=pver-1,pver/2,-1
1194  IF (hdsp .GT. sqrt(zm(i, k)*zm(i, k+1))) THEN
1195  ksrc = k - 1
1196  psrc = pi(i, k-1)
1197  CALL get_ti(t_new, t(i, k))
1198  rsrc = rsrc + pm(i, k)/(mapl_rgas*t_new)*dpm(i, k)
1199  CALL get_uv(u_new, u(i, k))
1200  CALL get_uv(v_new, v(i, k))
1201  usrc = usrc + u_new*dpm(i, k)
1202  vsrc = vsrc + v_new*dpm(i, k)
1203  nsrc = nsrc + nm(k)*dpm(i, k)
1204  CALL pushinteger4(2)
1205  ELSE
1206  CALL pushinteger4(1)
1207  END IF
1208  END DO
1209  rsrc = rsrc/(pi(i, pver)-psrc)
1210  usrc = usrc/(pi(i, pver)-psrc)
1211  vsrc = vsrc/(pi(i, pver)-psrc)
1212  nsrc = nsrc/(pi(i, pver)-psrc)
1213  IF (usrc .EQ. 0. .AND. vsrc .EQ. 0.) THEN
1214  ubsrc = sqrt(ubmc2mn)
1215  CALL pushreal4(xv)
1216  xv = 1.
1217  CALL pushreal4(yv)
1218  yv = 0.
1219  CALL pushinteger4(0)
1220  ELSE
1221  ubsrc = sqrt(usrc**2 + vsrc**2)
1222  CALL pushreal4(xv)
1223  xv = usrc/ubsrc
1224  CALL pushreal4(yv)
1225  yv = vsrc/ubsrc
1226  CALL pushinteger4(1)
1227  END IF
1228 ! Project the local wind at midpoints onto the source wind.
1229  DO k=1,pver
1230  ubm(k) = u(i, k)*xv + v(i, k)*yv
1231  END DO
1232  CALL pushreal4(ubi(0))
1233 ! Compute the interface wind projection by averaging the midpoint winds.
1234 ! Use the top level wind at the top interface.
1235  ubi(0) = ubm(1)
1236  DO k=1,pver
1237  CALL pushreal4(ubi(k))
1238  ubi(k) = ubm(k)
1239  END DO
1240 ! Determine the orographic c=0 source term following McFarlane (1987).
1241 ! Set the source top interface index to pver, if the orographic term is zero.
1242  IF (ubsrc .GT. orovmin .AND. hdsp .GT. orohmin) THEN
1243  sghmax = fcrit2*(ubsrc/nsrc)**2
1244  IF (hdsp**2 .GT. sghmax) THEN
1245  min1 = sghmax
1246  CALL pushinteger4(1)
1247  ELSE
1248  min1 = hdsp**2
1249  CALL pushinteger4(0)
1250  END IF
1251  tauoro = oroko2*min1*rsrc*nsrc*ubsrc
1252  CALL pushinteger4(0)
1253  ELSE
1254  tauoro = 0.
1255  ksrc = pver
1256  CALL pushinteger4(1)
1257  END IF
1258 ! tauoro is nontrivial when ubsrc is positive. However, if ubi(ksrc) is negative
1259 ! [note that the sign of ubsrc is irrelevant to the sign of ubi(ksrc)], orographic
1260 ! GWs can propagative upward passing through the negative basic-state wind.
1261 ! The following is to prevent this physically unjustified simulation.
1262 ! In addition, even if ubi(ksrc) > 0, if ubm(ksrc) < 0 .and. ubi(ksrc-1) < 0,
1263 ! negative wave stress leads to the acceleration of the negative ubm(ksrc).
1264 ! This result is physically inconsistent. However, if ubm(ksrc) > 0 .and.
1265 ! ubi(ksrc-1) < 0, GWs are filtered in physically consistent way, and
1266 ! decelerate the positive ubm(ksrc). Therefore, GWs are also assumed not to be
1267 ! launched when ubm(ksrc) < 0.
1268  IF (ubi(kbot) .LT. 0. .OR. ubm(kbot) .LT. 0.) THEN
1269  tauoro = 0.
1270  ksrc = pver
1271  CALL pushinteger4(1)
1272  ELSE
1273  CALL pushinteger4(0)
1274  END IF
1275 ! Sets kbot equal to ksrc
1276  kbot = ksrc
1277 ! Set the phase speeds and wave numbers in the direction of the source wind.
1278 ! Set the source stress magnitude (positive only, note that the sign of the
1279 ! stress is the same as (c-u).
1280  tau(0, kbot) = tauoro
1281 ! Determine the min value of kldv and ksrc for limiting later loops
1282 ! and the pressure at the top interface of the low level stress divergence
1283 ! region.
1284  CALL pushreal4(rsrc)
1285  CALL pushreal4(ubsrc)
1286  CALL pushreal4(psrc)
1287  CALL pushreal4(nsrc)
1288  CALL pushreal4(min1)
1289  CALL pushreal4(vsrc)
1290  CALL pushreal4(usrc)
1291  END SUBROUTINE gw_oro_fwd
1292 ! Differentiation of gw_oro in reverse (adjoint) mode (backward sweep):
1293 ! gradient, with respect to input variables: tau u v ubi ubm
1294 ! of linear combination of output variables: tau u v xv yv ubi
1295 !================================================================================
1296  SUBROUTINE gw_oro_bwd(i, pcols, pver, pgwv, u, ub, v, vb, t, sgh, pm, &
1297 & pi, dpm, zm, nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi&
1298 & , ubib, ubm, ubmb, xv, xvb, yv, yvb, kbot, rlat)
1299  IMPLICIT NONE
1300  INTEGER, INTENT(IN) :: i
1301  INTEGER, INTENT(IN) :: pcols
1302  INTEGER, INTENT(IN) :: pver
1303  INTEGER, INTENT(IN) :: pgwv
1304  REAL :: u(pcols, pver)
1305  REAL :: ub(pcols, pver)
1306  REAL :: v(pcols, pver)
1307  REAL :: vb(pcols, pver)
1308  REAL :: t(pcols, pver)
1309  REAL :: sgh(pcols)
1310  REAL :: pm(pcols, pver)
1311  REAL :: pi(pcols, 0:pver)
1312  REAL :: dpm(pcols, pver)
1313  REAL :: zm(pcols, pver)
1314  REAL :: nm(pver)
1315  INTEGER :: kldv
1316  INTEGER :: kldvmn
1317  INTEGER :: ksrc
1318  INTEGER :: ksrcmn
1319  REAL :: rdpldv
1320  REAL :: tau(-pgwv:pgwv, 0:pver)
1321  REAL :: taub(-pgwv:pgwv, 0:pver)
1322  REAL :: ubi(0:pver)
1323  REAL :: ubib(0:pver)
1324  REAL :: ubm(pver)
1325  REAL :: ubmb(pver)
1326  REAL :: xv
1327  REAL :: xvb
1328  REAL :: yv
1329  REAL :: yvb
1330  INTEGER :: kbot
1331  REAL :: rlat(pcols)
1332  INTEGER :: k
1333  REAL :: ubsrc
1334  REAL :: ubsrcb
1335  REAL :: hdsp
1336  REAL :: sghmax
1337  REAL :: sghmaxb
1338  REAL :: tauoro
1339  REAL :: tauorob
1340  REAL :: nsrc
1341  REAL :: psrc
1342  REAL :: rsrc
1343  REAL :: usrc
1344  REAL :: usrcb
1345  REAL :: vsrc
1346  REAL :: vsrcb
1347  REAL :: u_new, v_new, t_new
1348  REAL :: u_newb, v_newb
1349  INTEGER :: branch
1350  REAL :: min1
1351  REAL :: tempb0
1352  REAL :: min1b
1353  REAL :: tempb
1354  INTRINSIC min
1355  INTRINSIC sqrt
1356  CALL popreal4(usrc)
1357  CALL popreal4(vsrc)
1358  CALL popreal4(min1)
1359  CALL popreal4(nsrc)
1360  CALL popreal4(psrc)
1361  CALL popreal4(ubsrc)
1362  CALL popreal4(rsrc)
1363  tauorob = taub(0, kbot)
1364  taub(0, kbot) = 0.0_8
1365  CALL popinteger4(branch)
1366  IF (.NOT.branch .LT. 1) tauorob = 0.0_8
1367  CALL popinteger4(branch)
1368  IF (branch .LT. 1) THEN
1369  tempb0 = oroko2*rsrc*nsrc*tauorob
1370  min1b = ubsrc*tempb0
1371  ubsrcb = min1*tempb0
1372  CALL popinteger4(branch)
1373  IF (branch .LT. 1) THEN
1374  sghmaxb = 0.0_8
1375  ELSE
1376  sghmaxb = min1b
1377  END IF
1378  ubsrcb = ubsrcb + fcrit2*2*ubsrc*sghmaxb/nsrc**2
1379  ELSE
1380  ubsrcb = 0.0_8
1381  END IF
1382  ubmb(:) = 0.0_8
1383  DO k=pver,1,-1
1384  CALL popreal4(ubi(k))
1385  ubmb(k) = ubmb(k) + ubib(k)
1386  ubib(k) = 0.0_8
1387  END DO
1388  CALL popreal4(ubi(0))
1389  ubmb(1) = ubmb(1) + ubib(0)
1390  ubib(0) = 0.0_8
1391  DO k=pver,1,-1
1392  ub(i, k) = ub(i, k) + xv*ubmb(k)
1393  xvb = xvb + u(i, k)*ubmb(k)
1394  vb(i, k) = vb(i, k) + yv*ubmb(k)
1395  yvb = yvb + v(i, k)*ubmb(k)
1396  ubmb(k) = 0.0_8
1397  END DO
1398  CALL popinteger4(branch)
1399  IF (branch .LT. 1) THEN
1400  CALL popreal4(yv)
1401  CALL popreal4(xv)
1402  vsrcb = 0.0_8
1403  usrcb = 0.0_8
1404  ELSE
1405  CALL popreal4(yv)
1406  ubsrcb = ubsrcb - usrc*xvb/ubsrc**2 - vsrc*yvb/ubsrc**2
1407  IF (usrc**2 + vsrc**2 .EQ. 0.0) THEN
1408  tempb = 0.0
1409  ELSE
1410  tempb = ubsrcb/(2.0*sqrt(usrc**2+vsrc**2))
1411  END IF
1412  vsrcb = 2*vsrc*tempb + yvb/ubsrc
1413  CALL popreal4(xv)
1414  usrcb = 2*usrc*tempb + xvb/ubsrc
1415  END IF
1416  vsrcb = vsrcb/(pi(i, pver)-psrc)
1417  usrcb = usrcb/(pi(i, pver)-psrc)
1418  DO k=pver/2,pver-1,1
1419  CALL popinteger4(branch)
1420  IF (.NOT.branch .LT. 2) THEN
1421  v_newb = dpm(i, k)*vsrcb
1422  u_newb = dpm(i, k)*usrcb
1423  CALL get_uv_b(v_new, v_newb, v(i, k), vb(i, k))
1424  CALL get_uv_b(u_new, u_newb, u(i, k), ub(i, k))
1425  END IF
1426  END DO
1427  v_newb = dpm(i, pver)*vsrcb
1428  u_newb = dpm(i, pver)*usrcb
1429  CALL get_uv_b(v_new, v_newb, v(i, pver), vb(i, pver))
1430  CALL get_uv_b(u_new, u_newb, u(i, pver), ub(i, pver))
1431  END SUBROUTINE gw_oro_bwd
1432 !================================================================================
1433  SUBROUTINE gw_oro(i, pcols, pver, pgwv, u, v, t, sgh, pm, pi, dpm, zm&
1434 & , nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, &
1435 & kbot, rlat)
1436  IMPLICIT NONE
1437 !-----------------------------------------------------------------------
1438 ! Orographic source for multiple gravity wave drag parameterization.
1439 !
1440 ! The stress is returned for a single wave with c=0, over orography.
1441 ! For points where the orographic variance is small (including ocean),
1442 ! the returned stress is zero.
1443 !------------------------------Arguments--------------------------------
1444 ! number of current column
1445  INTEGER, INTENT(IN) :: i
1446 ! number of atmospheric columns
1447  INTEGER, INTENT(IN) :: pcols
1448 ! number of atmospheric columns
1449  INTEGER, INTENT(IN) :: pver
1450 ! number of waves allowed
1451  INTEGER, INTENT(IN) :: pgwv
1452 ! midpoint zonal wind
1453  REAL :: u(pcols, pver)
1454 ! midpoint meridional wind
1455  REAL :: v(pcols, pver)
1456 ! midpoint temperatures
1457  REAL :: t(pcols, pver)
1458 ! standard deviation of orography
1459  REAL :: sgh(pcols)
1460 ! midpoint pressures
1461  REAL :: pm(pcols, pver)
1462 ! interface pressures
1463  REAL :: pi(pcols, 0:pver)
1464 ! midpoint delta p (pi(k)-pi(k-1))
1465  REAL :: dpm(pcols, pver)
1466 ! midpoint heights
1467  REAL :: zm(pcols, pver)
1468 ! midpoint Brunt-Vaisalla frequency
1469  REAL :: nm(pver)
1470 ! top interface of low level stress div region
1471  INTEGER :: kldv
1472 ! min value of kldv
1473  INTEGER :: kldvmn
1474 ! index of top interface of source region
1475  INTEGER :: ksrc
1476 ! min value of ksrc
1477  INTEGER :: ksrcmn
1478 ! 1/dp across low level divergence region
1479  REAL :: rdpldv
1480 ! wave Reynolds stress
1481  REAL :: tau(-pgwv:pgwv, 0:pver)
1482 ! projection of wind at interfaces
1483  REAL :: ubi(0:pver)
1484 ! projection of wind at midpoints
1485  REAL :: ubm(pver)
1486 ! unit vectors of source wind (x)
1487  REAL :: xv
1488 ! unit vectors of source wind (y)
1489  REAL :: yv
1490  INTEGER :: kbot
1491  REAL :: rlat(pcols)
1492 !---------------------------Local storage-------------------------------
1493 ! loop indexes
1494  INTEGER :: k
1495 ! Source-layer basic-state wind
1496  REAL :: ubsrc
1497 ! surface streamline displacment height (2*sgh)
1498  REAL :: hdsp
1499 ! max orographic sdv to use
1500  REAL :: sghmax
1501 ! c=0 stress from orography
1502  REAL :: tauoro
1503 ! real :: zldv ! top of the low level stress divergence region
1504 ! b-f frequency averaged over source region
1505  REAL :: nsrc
1506 ! interface pressure at top of source region
1507  REAL :: psrc
1508 ! density averaged over source region
1509  REAL :: rsrc
1510 ! u wind averaged over source region
1511  REAL :: usrc
1512 ! v wind averaged over source region
1513  REAL :: vsrc
1514  REAL :: u_new, v_new, t_new
1515  REAL :: min1
1516  INTRINSIC min
1517  INTRINSIC sqrt
1518 ! Begins
1519 !---------------------------------------------------------------------------
1520 ! Average the basic state variables for the wave source over the depth of
1521 ! the orographic standard deviation. Here we assume that the apropiate
1522 ! values of wind, stability, etc. for determining the wave source are
1523 ! averages over the depth of the atmosphere pentrated by the typical mountain.
1524 ! Reduces to the bottom midpoint values when sgh=0, such as over ocean.
1525 !
1526 ! Also determine the depth of the low level stress divergence region, as
1527 ! the max of the boundary layer depth and the source region depth. This
1528 ! can be done here if the stress magnitude does not determine the depth,
1529 ! otherwise it must be done below.
1530 !---------------------------------------------------------------------------
1531  ksrc = pver - 1
1532  kldv = pver - 1
1533  psrc = pi(i, pver-1)
1534  CALL get_ti(t_new, t(i, pver))
1535  rsrc = pm(i, pver)/(mapl_rgas*t_new)*dpm(i, pver)
1536  CALL get_uv(u_new, u(i, pver))
1537  CALL get_uv(v_new, v(i, pver))
1538  usrc = u_new*dpm(i, pver)
1539  vsrc = v_new*dpm(i, pver)
1540  nsrc = nm(pver)*dpm(i, pver)
1541  hdsp = 2.0*sgh(i)
1542  DO k=pver-1,pver/2,-1
1543  IF (hdsp .GT. sqrt(zm(i, k)*zm(i, k+1))) THEN
1544  ksrc = k - 1
1545  kldv = k - 1
1546  psrc = pi(i, k-1)
1547  CALL get_ti(t_new, t(i, k))
1548  rsrc = rsrc + pm(i, k)/(mapl_rgas*t_new)*dpm(i, k)
1549  CALL get_uv(u_new, u(i, k))
1550  CALL get_uv(v_new, v(i, k))
1551  usrc = usrc + u_new*dpm(i, k)
1552  vsrc = vsrc + v_new*dpm(i, k)
1553  nsrc = nsrc + nm(k)*dpm(i, k)
1554  END IF
1555  END DO
1556  rsrc = rsrc/(pi(i, pver)-psrc)
1557  usrc = usrc/(pi(i, pver)-psrc)
1558  vsrc = vsrc/(pi(i, pver)-psrc)
1559  nsrc = nsrc/(pi(i, pver)-psrc)
1560  IF (usrc .EQ. 0. .AND. vsrc .EQ. 0.) THEN
1561  ubsrc = sqrt(ubmc2mn)
1562  xv = 1.
1563  yv = 0.
1564  ELSE
1565  ubsrc = sqrt(usrc**2 + vsrc**2)
1566  xv = usrc/ubsrc
1567  yv = vsrc/ubsrc
1568  END IF
1569 ! Project the local wind at midpoints onto the source wind.
1570  DO k=1,pver
1571  ubm(k) = u(i, k)*xv + v(i, k)*yv
1572  END DO
1573 ! Compute the interface wind projection by averaging the midpoint winds.
1574 ! Use the top level wind at the top interface.
1575  ubi(0) = ubm(1)
1576  DO k=1,pver
1577  ubi(k) = ubm(k)
1578  END DO
1579 ! Determine the orographic c=0 source term following McFarlane (1987).
1580 ! Set the source top interface index to pver, if the orographic term is zero.
1581  IF (ubsrc .GT. orovmin .AND. hdsp .GT. orohmin) THEN
1582  sghmax = fcrit2*(ubsrc/nsrc)**2
1583  IF (hdsp**2 .GT. sghmax) THEN
1584  min1 = sghmax
1585  ELSE
1586  min1 = hdsp**2
1587  END IF
1588  tauoro = oroko2*min1*rsrc*nsrc*ubsrc
1589  ELSE
1590  tauoro = 0.
1591  ksrc = pver
1592  kldv = pver
1593  END IF
1594 ! tauoro is nontrivial when ubsrc is positive. However, if ubi(ksrc) is negative
1595 ! [note that the sign of ubsrc is irrelevant to the sign of ubi(ksrc)], orographic
1596 ! GWs can propagative upward passing through the negative basic-state wind.
1597 ! The following is to prevent this physically unjustified simulation.
1598 ! In addition, even if ubi(ksrc) > 0, if ubm(ksrc) < 0 .and. ubi(ksrc-1) < 0,
1599 ! negative wave stress leads to the acceleration of the negative ubm(ksrc).
1600 ! This result is physically inconsistent. However, if ubm(ksrc) > 0 .and.
1601 ! ubi(ksrc-1) < 0, GWs are filtered in physically consistent way, and
1602 ! decelerate the positive ubm(ksrc). Therefore, GWs are also assumed not to be
1603 ! launched when ubm(ksrc) < 0.
1604  IF (ubi(kbot) .LT. 0. .OR. ubm(kbot) .LT. 0.) THEN
1605  tauoro = 0.
1606  ksrc = pver
1607  kldv = pver
1608  END IF
1609 ! Sets kbot equal to ksrc
1610  kbot = ksrc
1611 ! Set the phase speeds and wave numbers in the direction of the source wind.
1612 ! Set the source stress magnitude (positive only, note that the sign of the
1613 ! stress is the same as (c-u).
1614  tau(0, kbot) = tauoro
1615 ! Determine the min value of kldv and ksrc for limiting later loops
1616 ! and the pressure at the top interface of the low level stress divergence
1617 ! region.
1618  ksrcmn = pver
1619  kldvmn = pver
1620  IF (ksrcmn .GT. ksrc) THEN
1621  ksrcmn = ksrc
1622  ELSE
1623  ksrcmn = ksrcmn
1624  END IF
1625  IF (kldvmn .GT. kldv) THEN
1626  kldvmn = kldv
1627  ELSE
1628  kldvmn = kldvmn
1629  END IF
1630  IF (kldv .NE. pver) rdpldv = 1./(pi(i, kldv)-pi(i, pver))
1631 ! kldvmn is always pver because FRACLDV == 0.
1632  IF (fracldv .LE. 0.) kldvmn = pver
1633  RETURN
1634  END SUBROUTINE gw_oro
1635 ! Differentiation of gw_bgnd in reverse (adjoint) mode (forward sweep):
1636 ! gradient, with respect to input variables: u v
1637 ! of linear combination of output variables: u v xv yv ubi ubm
1638 !===============================================================================
1639  SUBROUTINE gw_bgnd_fwd(i, pcols, pver, c, u, v, t, pm, pi, dpm, rdpm, &
1640 & piln, rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, &
1641 & yv, ngwv, kbot)
1642  IMPLICIT NONE
1643 !-----------------------------------------------------------------------
1644 ! Driver for multiple gravity wave drag parameterization.
1645 !
1646 ! The parameterization is assumed to operate only where water vapor
1647 ! concentrations are negligible in determining the density.
1648 !-----------------------------------------------------------------------
1649 !------------------------------Arguments--------------------------------
1650 ! number of current column
1651  INTEGER :: i
1652 ! number of atmospheric columns
1653  INTEGER :: pcols
1654 ! number of atmospheric columns
1655  INTEGER :: pver
1656 ! index of bottom (source) interface
1657  INTEGER :: kbot
1658 ! number of gravity waves to use
1659  INTEGER :: ngwv
1660 ! wave phase speeds
1661  REAL :: c(-ngwv:ngwv)
1662 ! midpoint zonal wind
1663  REAL :: u(pcols, pver)
1664 ! midpoint meridional wind
1665  REAL :: v(pcols, pver)
1666 ! midpoint temperatures
1667  REAL :: t(pcols, pver)
1668 ! midpoint pressures
1669  REAL :: pm(pcols, pver)
1670 ! interface pressures
1671  REAL :: pi(pcols, 0:pver)
1672 ! midpoint delta p (pi(k)-pi(k-1))
1673  REAL :: dpm(pcols, pver)
1674 ! 1. / (pi(k)-pi(k-1))
1675  REAL :: rdpm(pcols, pver)
1676 ! ln(interface pressures)
1677  REAL :: piln(pcols, 0:pver)
1678 ! latitude in radians for columns
1679  REAL :: rlat(pcols)
1680 ! top interface of low level stress divergence region
1681  INTEGER :: kldv
1682 ! min value of kldv
1683  INTEGER :: kldvmn
1684 ! index of top interface of source region
1685  INTEGER :: ksrc
1686 ! min value of ksrc
1687  INTEGER :: ksrcmn
1688 ! 1/dp across low level divergence region
1689  REAL :: rdpldv
1690 ! wave Reynolds stress
1691  REAL :: tau(-ngwv:ngwv, 0:pver)
1692 ! projection of wind at interfaces
1693  REAL :: ubi(0:pver)
1694 ! projection of wind at midpoints
1695  REAL :: ubm(pver)
1696 ! unit vectors of source wind (x)
1697  REAL :: xv
1698 ! unit vectors of source wind (y)
1699  REAL :: yv
1700 !---------------------------Local storage-------------------------------
1701 ! loop indexes
1702  INTEGER :: k, l
1703 ! background stress at c=0
1704  REAL :: tauback
1705 ! u wind averaged over source region
1706  REAL :: usrc
1707 ! v wind averaged over source region
1708  REAL :: vsrc
1709  REAL :: ubsrc
1710 ! Used in lat dependence of GW spec.
1711  REAL :: al0
1712 ! Used in lat dependence of GW spec.
1713  REAL :: dlat0
1714  REAL :: latdeg
1715 ! The actual lat dependence of GW spec.
1716  REAL :: flat_gw
1717  REAL :: u_new, v_new, tau_new
1718  REAL :: tmp
1719  INTRINSIC exp
1720  INTRINSIC max
1721  INTRINSIC abs
1722  REAL :: x1
1723  INTRINSIC dble
1724  REAL :: abs6
1725  REAL :: abs5
1726  REAL :: abs4
1727  REAL :: abs3
1728  REAL :: abs2
1729  REAL :: abs1
1730  INTRINSIC sqrt
1731  REAL :: y1
1732 !---------------------------------------------------------------------------
1733 ! Determine the source layer wind and unit vectors, then project winds.
1734 !---------------------------------------------------------------------------
1735 ! Just use the source level interface values for the source
1736 ! wind speed and direction (unit vector).
1737  ubm = 0.
1738  ubi = 0.
1739  tau = 0.
1740  usrc = 0.5*(u(i, kbot+1)+u(i, kbot))
1741  vsrc = 0.5*(v(i, kbot+1)+v(i, kbot))
1742  x1 = sqrt(usrc**2 + vsrc**2)
1743  y1 = sqrt(ubmc2mn)
1744  IF (x1 .LT. y1) THEN
1745  ubsrc = y1
1746  CALL pushinteger4(1)
1747  ELSE
1748  ubsrc = x1
1749  CALL pushinteger4(0)
1750  END IF
1751  IF (usrc .EQ. 0. .AND. vsrc .EQ. 0.) THEN
1752  xv = 1.0
1753  yv = 0.0
1754  CALL pushinteger4(1)
1755  ELSE
1756  xv = usrc/ubsrc
1757  yv = vsrc/ubsrc
1758  CALL pushinteger4(0)
1759  END IF
1760 ! Project the local wind at midpoints onto the source wind.
1761  DO k=1,pver
1762  ubm(k) = u(i, k)*xv + v(i, k)*yv
1763  END DO
1764 ! Compute the bottom interface wind projection using the midpoint winds.
1765  ubi(0) = ubm(1)
1766  DO k=1,pver
1767  ubi(k) = ubm(k)
1768  END DO
1769  CALL pushreal4(ubsrc)
1770  CALL pushreal4(vsrc)
1771  CALL pushreal4(usrc)
1772 !-----------------------------------------------------------------------
1773 ! Gravity wave sources
1774 !-----------------------------------------------------------------------
1775 ! Determine the background stress at c=0
1776  tauback = taubgnd*tauscal
1777 ! Include dependence on latitude:
1778  latdeg = rlat(i)*180./pi_gwd
1779 !
1780  IF (-15.3 .LT. latdeg .AND. latdeg .LT. 15.3) THEN
1781  IF (latdeg .GE. 0.) THEN
1782  abs1 = latdeg
1783  ELSE
1784  abs1 = -latdeg
1785  END IF
1786  flat_gw = 1.2*exp(-(dble((abs1-3.)/8.0)**2))
1787  IF (latdeg .GE. 0.) THEN
1788  abs2 = latdeg
1789  ELSE
1790  abs2 = -latdeg
1791  END IF
1792  IF (flat_gw .LT. 1.2 .AND. abs2 .LE. 3.) flat_gw = 1.2
1793  ELSE IF (latdeg .GT. -31. .AND. latdeg .LE. -15.3) THEN
1794  flat_gw = 0.10
1795  ELSE IF (latdeg .LT. 31. .AND. latdeg .GE. 15.3) THEN
1796  flat_gw = 0.10
1797  ELSE IF (latdeg .GT. -60. .AND. latdeg .LE. -31.) THEN
1798  IF (latdeg .GE. 0.) THEN
1799  abs3 = latdeg
1800  ELSE
1801  abs3 = -latdeg
1802  END IF
1803  flat_gw = 0.50*exp(-(dble((abs3-60.)/23.)**2))
1804  ELSE IF (latdeg .LT. 60. .AND. latdeg .GE. 31.) THEN
1805  IF (latdeg .GE. 0.) THEN
1806  abs4 = latdeg
1807  ELSE
1808  abs4 = -latdeg
1809  END IF
1810  flat_gw = 0.50*exp(-(dble((abs4-60.)/23.)**2))
1811  ELSE IF (latdeg .LE. -60.) THEN
1812  IF (latdeg .GE. 0.) THEN
1813  abs5 = latdeg
1814  ELSE
1815  abs5 = -latdeg
1816  END IF
1817  flat_gw = 0.50*exp(-(dble((abs5-60.)/70.)**2))
1818  ELSE IF (latdeg .GE. 60.) THEN
1819  IF (latdeg .GE. 0.) THEN
1820  abs6 = latdeg
1821  ELSE
1822  abs6 = -latdeg
1823  END IF
1824  flat_gw = 0.50*exp(-(dble((abs6-60.)/70.)**2))
1825  END IF
1826  tauback = tauback*flat_gw
1827 ! Set the phase speeds and wave numbers in the direction of the source wind.
1828 ! Set the source stress magnitude (positive only, note that the sign of the
1829 ! stress is the same as (c-u).
1830  DO l=1,ngwv
1831  tau(l, kbot) = tauback*exp(-((c(l)/30.)**2))
1832  tmp = tau(l, kbot)
1833  tau(-l, kbot) = tmp
1834  END DO
1835  tau(0, kbot) = tauback
1836 ! Determine the min value of kldv and ksrc for limiting later loops
1837 ! and the pressure at the top interface of the low level stress divergence
1838 ! region.
1839  END SUBROUTINE gw_bgnd_fwd
1840 ! Differentiation of gw_bgnd in reverse (adjoint) mode (backward sweep):
1841 ! gradient, with respect to input variables: u v
1842 ! of linear combination of output variables: u v xv yv ubi ubm
1843 !===============================================================================
1844  SUBROUTINE gw_bgnd_bwd(i, pcols, pver, c, u, ub, v, vb, t, pm, pi, dpm&
1845 & , rdpm, piln, rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, &
1846 & ubib, ubm, ubmb, xv, xvb, yv, yvb, ngwv, kbot)
1847  IMPLICIT NONE
1848  INTEGER :: i
1849  INTEGER :: pcols
1850  INTEGER :: pver
1851  INTEGER :: kbot
1852  INTEGER :: ngwv
1853  REAL :: c(-ngwv:ngwv)
1854  REAL :: u(pcols, pver)
1855  REAL :: ub(pcols, pver)
1856  REAL :: v(pcols, pver)
1857  REAL :: vb(pcols, pver)
1858  REAL :: t(pcols, pver)
1859  REAL :: pm(pcols, pver)
1860  REAL :: pi(pcols, 0:pver)
1861  REAL :: dpm(pcols, pver)
1862  REAL :: rdpm(pcols, pver)
1863  REAL :: piln(pcols, 0:pver)
1864  REAL :: rlat(pcols)
1865  INTEGER :: kldv
1866  INTEGER :: kldvmn
1867  INTEGER :: ksrc
1868  INTEGER :: ksrcmn
1869  REAL :: rdpldv
1870  REAL :: tau(-ngwv:ngwv, 0:pver)
1871  REAL :: ubi(0:pver)
1872  REAL :: ubib(0:pver)
1873  REAL :: ubm(pver)
1874  REAL :: ubmb(pver)
1875  REAL :: xv
1876  REAL :: xvb
1877  REAL :: yv
1878  REAL :: yvb
1879  INTEGER :: k, l
1880  REAL :: tauback
1881  REAL :: usrc
1882  REAL :: usrcb
1883  REAL :: vsrc
1884  REAL :: vsrcb
1885  REAL :: ubsrc
1886  REAL :: ubsrcb
1887  REAL :: al0
1888  REAL :: dlat0
1889  REAL :: latdeg
1890  REAL :: flat_gw
1891  REAL :: u_new, v_new, tau_new
1892  INTEGER :: branch
1893  INTRINSIC exp
1894  INTRINSIC max
1895  INTRINSIC abs
1896  REAL :: x1
1897  REAL :: tempb
1898  REAL :: x1b
1899  INTRINSIC dble
1900  REAL :: abs6
1901  REAL :: abs5
1902  REAL :: abs4
1903  REAL :: abs3
1904  REAL :: abs2
1905  REAL :: abs1
1906  INTRINSIC sqrt
1907  REAL :: y1
1908  CALL popreal4(usrc)
1909  CALL popreal4(vsrc)
1910  CALL popreal4(ubsrc)
1911  DO k=pver,1,-1
1912  ubmb(k) = ubmb(k) + ubib(k)
1913  ubib(k) = 0.0_8
1914  END DO
1915  ubmb(1) = ubmb(1) + ubib(0)
1916  DO k=pver,1,-1
1917  ub(i, k) = ub(i, k) + xv*ubmb(k)
1918  xvb = xvb + u(i, k)*ubmb(k)
1919  vb(i, k) = vb(i, k) + yv*ubmb(k)
1920  yvb = yvb + v(i, k)*ubmb(k)
1921  ubmb(k) = 0.0_8
1922  END DO
1923  CALL popinteger4(branch)
1924  IF (branch .LT. 1) THEN
1925  vsrcb = yvb/ubsrc
1926  ubsrcb = -(usrc*xvb/ubsrc**2) - vsrc*yvb/ubsrc**2
1927  usrcb = xvb/ubsrc
1928  ELSE
1929  ubsrcb = 0.0_8
1930  vsrcb = 0.0_8
1931  usrcb = 0.0_8
1932  END IF
1933  CALL popinteger4(branch)
1934  IF (branch .LT. 1) THEN
1935  x1b = ubsrcb
1936  ELSE
1937  x1b = 0.0_8
1938  END IF
1939  IF (usrc**2 + vsrc**2 .EQ. 0.0) THEN
1940  tempb = 0.0
1941  ELSE
1942  tempb = x1b/(2.0*sqrt(usrc**2+vsrc**2))
1943  END IF
1944  usrcb = usrcb + 2*usrc*tempb
1945  vsrcb = vsrcb + 2*vsrc*tempb
1946  vb(i, kbot+1) = vb(i, kbot+1) + 0.5*vsrcb
1947  vb(i, kbot) = vb(i, kbot) + 0.5*vsrcb
1948  ub(i, kbot+1) = ub(i, kbot+1) + 0.5*usrcb
1949  ub(i, kbot) = ub(i, kbot) + 0.5*usrcb
1950  END SUBROUTINE gw_bgnd_bwd
1951 !===============================================================================
1952  SUBROUTINE gw_bgnd(i, pcols, pver, c, u, v, t, pm, pi, dpm, rdpm, piln&
1953 & , rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, &
1954 & ngwv, kbot)
1955  IMPLICIT NONE
1956 !-----------------------------------------------------------------------
1957 ! Driver for multiple gravity wave drag parameterization.
1958 !
1959 ! The parameterization is assumed to operate only where water vapor
1960 ! concentrations are negligible in determining the density.
1961 !-----------------------------------------------------------------------
1962 !------------------------------Arguments--------------------------------
1963 ! number of current column
1964  INTEGER :: i
1965 ! number of atmospheric columns
1966  INTEGER :: pcols
1967 ! number of atmospheric columns
1968  INTEGER :: pver
1969 ! index of bottom (source) interface
1970  INTEGER :: kbot
1971 ! number of gravity waves to use
1972  INTEGER :: ngwv
1973 ! wave phase speeds
1974  REAL :: c(-ngwv:ngwv)
1975 ! midpoint zonal wind
1976  REAL :: u(pcols, pver)
1977 ! midpoint meridional wind
1978  REAL :: v(pcols, pver)
1979 ! midpoint temperatures
1980  REAL :: t(pcols, pver)
1981 ! midpoint pressures
1982  REAL :: pm(pcols, pver)
1983 ! interface pressures
1984  REAL :: pi(pcols, 0:pver)
1985 ! midpoint delta p (pi(k)-pi(k-1))
1986  REAL :: dpm(pcols, pver)
1987 ! 1. / (pi(k)-pi(k-1))
1988  REAL :: rdpm(pcols, pver)
1989 ! ln(interface pressures)
1990  REAL :: piln(pcols, 0:pver)
1991 ! latitude in radians for columns
1992  REAL :: rlat(pcols)
1993 ! top interface of low level stress divergence region
1994  INTEGER :: kldv
1995 ! min value of kldv
1996  INTEGER :: kldvmn
1997 ! index of top interface of source region
1998  INTEGER :: ksrc
1999 ! min value of ksrc
2000  INTEGER :: ksrcmn
2001 ! 1/dp across low level divergence region
2002  REAL :: rdpldv
2003 ! wave Reynolds stress
2004  REAL :: tau(-ngwv:ngwv, 0:pver)
2005 ! projection of wind at interfaces
2006  REAL :: ubi(0:pver)
2007 ! projection of wind at midpoints
2008  REAL :: ubm(pver)
2009 ! unit vectors of source wind (x)
2010  REAL :: xv
2011 ! unit vectors of source wind (y)
2012  REAL :: yv
2013 !---------------------------Local storage-------------------------------
2014 ! loop indexes
2015  INTEGER :: k, l
2016 ! background stress at c=0
2017  REAL :: tauback
2018 ! u wind averaged over source region
2019  REAL :: usrc
2020 ! v wind averaged over source region
2021  REAL :: vsrc
2022  REAL :: ubsrc
2023 ! Used in lat dependence of GW spec.
2024  REAL :: al0
2025 ! Used in lat dependence of GW spec.
2026  REAL :: dlat0
2027  REAL :: latdeg
2028 ! The actual lat dependence of GW spec.
2029  REAL :: flat_gw
2030  REAL :: u_new, v_new, tau_new
2031  INTRINSIC exp
2032  INTRINSIC max
2033  INTRINSIC abs
2034  REAL :: x1
2035  INTRINSIC dble
2036  REAL :: abs6
2037  REAL :: abs5
2038  REAL :: abs4
2039  REAL :: abs3
2040  REAL :: abs2
2041  REAL :: abs1
2042  INTRINSIC sqrt
2043  REAL :: y1
2044 !---------------------------------------------------------------------------
2045 ! Determine the source layer wind and unit vectors, then project winds.
2046 !---------------------------------------------------------------------------
2047 ! Just use the source level interface values for the source
2048 ! wind speed and direction (unit vector).
2049  ubm = 0.
2050  ubi = 0.
2051  xv = 0.
2052  yv = 0.
2053  tau = 0.
2054  ksrc = kbot
2055  kldv = kbot
2056  usrc = 0.5*(u(i, kbot+1)+u(i, kbot))
2057  vsrc = 0.5*(v(i, kbot+1)+v(i, kbot))
2058  x1 = sqrt(usrc**2 + vsrc**2)
2059  y1 = sqrt(ubmc2mn)
2060  IF (x1 .LT. y1) THEN
2061  ubsrc = y1
2062  ELSE
2063  ubsrc = x1
2064  END IF
2065  IF (usrc .EQ. 0. .AND. vsrc .EQ. 0.) THEN
2066  xv = 1.0
2067  yv = 0.0
2068  ELSE
2069  xv = usrc/ubsrc
2070  yv = vsrc/ubsrc
2071  END IF
2072 ! Project the local wind at midpoints onto the source wind.
2073  DO k=1,pver
2074  ubm(k) = u(i, k)*xv + v(i, k)*yv
2075  END DO
2076 ! Compute the bottom interface wind projection using the midpoint winds.
2077  ubi(0) = ubm(1)
2078  DO k=1,pver
2079  ubi(k) = ubm(k)
2080  END DO
2081 !-----------------------------------------------------------------------
2082 ! Gravity wave sources
2083 !-----------------------------------------------------------------------
2084 ! Determine the background stress at c=0
2085  tauback = taubgnd*tauscal
2086 ! Include dependence on latitude:
2087  latdeg = rlat(i)*180./pi_gwd
2088 !
2089  IF (-15.3 .LT. latdeg .AND. latdeg .LT. 15.3) THEN
2090  IF (latdeg .GE. 0.) THEN
2091  abs1 = latdeg
2092  ELSE
2093  abs1 = -latdeg
2094  END IF
2095  flat_gw = 1.2*exp(-(dble((abs1-3.)/8.0)**2))
2096  IF (latdeg .GE. 0.) THEN
2097  abs2 = latdeg
2098  ELSE
2099  abs2 = -latdeg
2100  END IF
2101  IF (flat_gw .LT. 1.2 .AND. abs2 .LE. 3.) flat_gw = 1.2
2102  ELSE IF (latdeg .GT. -31. .AND. latdeg .LE. -15.3) THEN
2103  flat_gw = 0.10
2104  ELSE IF (latdeg .LT. 31. .AND. latdeg .GE. 15.3) THEN
2105  flat_gw = 0.10
2106  ELSE IF (latdeg .GT. -60. .AND. latdeg .LE. -31.) THEN
2107  IF (latdeg .GE. 0.) THEN
2108  abs3 = latdeg
2109  ELSE
2110  abs3 = -latdeg
2111  END IF
2112  flat_gw = 0.50*exp(-(dble((abs3-60.)/23.)**2))
2113  ELSE IF (latdeg .LT. 60. .AND. latdeg .GE. 31.) THEN
2114  IF (latdeg .GE. 0.) THEN
2115  abs4 = latdeg
2116  ELSE
2117  abs4 = -latdeg
2118  END IF
2119  flat_gw = 0.50*exp(-(dble((abs4-60.)/23.)**2))
2120  ELSE IF (latdeg .LE. -60.) THEN
2121  IF (latdeg .GE. 0.) THEN
2122  abs5 = latdeg
2123  ELSE
2124  abs5 = -latdeg
2125  END IF
2126  flat_gw = 0.50*exp(-(dble((abs5-60.)/70.)**2))
2127  ELSE IF (latdeg .GE. 60.) THEN
2128  IF (latdeg .GE. 0.) THEN
2129  abs6 = latdeg
2130  ELSE
2131  abs6 = -latdeg
2132  END IF
2133  flat_gw = 0.50*exp(-(dble((abs6-60.)/70.)**2))
2134  END IF
2135  tauback = tauback*flat_gw
2136 ! Set the phase speeds and wave numbers in the direction of the source wind.
2137 ! Set the source stress magnitude (positive only, note that the sign of the
2138 ! stress is the same as (c-u).
2139  DO l=1,ngwv
2140  tau(l, kbot) = tauback*exp(-((c(l)/30.)**2))
2141  tau(-l, kbot) = tau(l, kbot)
2142  END DO
2143  tau(0, kbot) = tauback
2144 ! Determine the min value of kldv and ksrc for limiting later loops
2145 ! and the pressure at the top interface of the low level stress divergence
2146 ! region.
2147  ksrcmn = pver
2148  kldvmn = pver
2149  RETURN
2150  END SUBROUTINE gw_bgnd
2151 ! Differentiation of gw_drag_prof in reverse (adjoint) mode (forward sweep):
2152 ! gradient, with respect to input variables: tau xv ut yv vt
2153 ! ubi
2154 ! of linear combination of output variables: ut vt
2155 !===============================================================================
2156  SUBROUTINE gw_drag_prof_fwd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c&
2157 & , u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, &
2158 & dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut&
2159 & , vt, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, &
2160 & tau0y, effgw)
2161  IMPLICIT NONE
2162 !-----------------------------------------------------------------------
2163 ! Solve for the drag profile from the multiple gravity wave drag
2164 ! parameterization.
2165 ! 1. scan up from the wave source to determine the stress profile
2166 ! 2. scan down the stress profile to determine the tendencies
2167 ! => apply bounds to the tendency
2168 ! a. from wkb solution
2169 ! b. from computational stability constraints
2170 ! => adjust stress on interface below to reflect actual bounded tendency
2171 !-----------------------------------------------------------------------
2172 !------------------------------Arguments--------------------------------
2173 ! number of current column
2174  INTEGER, INTENT(IN) :: i
2175 ! number of atmospheric columns
2176  INTEGER, INTENT(IN) :: pcols
2177 ! number of atmospheric columns
2178  INTEGER, INTENT(IN) :: pver
2179 ! index of bottom (source) interface
2180  INTEGER, INTENT(IN) :: kbot
2181 ! index of top interface of gwd region
2182  INTEGER, INTENT(IN) :: ktop
2183 ! number of gravity waves possible
2184  INTEGER, INTENT(IN) :: pgwv
2185 ! number of gravity waves to use
2186  INTEGER, INTENT(IN) :: ngwv
2187 ! top interface of low level stress divergence region
2188  INTEGER, INTENT(IN) :: kldv
2189 ! min value of kldv
2190  INTEGER, INTENT(IN) :: kldvmn
2191 ! index of top interface of source region
2192  INTEGER, INTENT(IN) :: ksrc
2193 ! min value of ksrc
2194  INTEGER, INTENT(IN) :: ksrcmn
2195 ! wave phase speeds
2196  REAL :: c(-pgwv:pgwv)
2197 ! midpoint zonal wind
2198  REAL :: u(pcols, pver)
2199 ! midpoint meridional wind
2200  REAL :: v(pcols, pver)
2201 ! midpoint temperatures
2202  REAL :: t(pcols, pver)
2203 ! interface pressures
2204  REAL :: pi(pcols, 0:pver)
2205 ! midpoint delta p (pi(k)-pi(k-1))
2206  REAL :: dpm(pcols, pver)
2207 ! 1. / (pi(k)-pi(k-1))
2208  REAL :: rdpm(pcols, pver)
2209 ! ln(interface pressures)
2210  REAL :: piln(pcols, 0:pver)
2211  REAL :: rlat(pcols)
2212 ! interface density
2213  REAL :: rhoi(0:pver)
2214 ! interface Brunt-Vaisalla frequency
2215  REAL :: ni(0:pver)
2216 ! interface temperature
2217  REAL :: ti(0:pver)
2218 ! midpoint Brunt-Vaisalla frequency
2219  REAL :: nm(pver)
2220 ! time step
2221  REAL :: dt
2222 ! newtonian cooling coefficients
2223  REAL :: alpha(0:pver)
2224 ! newtonian cooling coefficients
2225  REAL :: dback(0:pver)
2226 ! 1/dp across low level divergence region
2227  REAL :: rdpldv
2228 ! projection of wind at interfaces
2229  REAL :: ubi(0:pver)
2230 ! projection of wind at midpoints
2231  REAL :: ubm(pver)
2232 ! unit vectors of source wind (x)
2233  REAL :: xv
2234 ! unit vectors of source wind (y)
2235  REAL :: yv
2236 ! tendency efficiency for gwd
2237  REAL :: effgw(pcols)
2238 ! wave Reynolds stress
2239  REAL :: tau(-pgwv:pgwv, 0:pver)
2240 ! zonal wind tendency
2241  REAL :: ut(pver)
2242 ! meridional wind tendency
2243  REAL :: vt(pver)
2244 ! temperature tendency
2245  REAL :: tt(pver)
2246 ! Total zonal GW momentum flux
2247  REAL :: taugwx(pcols, 0:pver)
2248 ! Total meridional GW momentum flux
2249  REAL :: taugwy(pcols, 0:pver)
2250 ! Total GW energy flux
2251  REAL :: fegw(pcols, 0:pver)
2252 ! Total GW pseudo energy flux
2253  REAL :: fepgw(pcols, 0:pver)
2254 ! Total U tendency below launch level
2255  REAL :: dusrc(pver)
2256 ! Total V tendency below launch level
2257  REAL :: dvsrc(pver)
2258 ! Total V tendency below launch level
2259  REAL :: dtsrc(pver)
2260 ! c=0 sfc. stress (zonal)
2261  REAL :: tau0x
2262 ! c=0 sfc. stress (meridional)
2263  REAL :: tau0y
2264 !---------------------------Local storage-------------------------------
2265 ! loop indexes
2266  INTEGER :: k, l
2267 ! fraction of dsat to use
2268  REAL :: dscal
2269 ! imaginary part of vertical wavenumber
2270  REAL :: mi
2271 ! stress after damping
2272  REAL :: taudmp
2273 !real :: taumax(pcols) ! max(tau) for any l
2274 ! saturation stress
2275  REAL :: tausat
2276 ! (ub-c)
2277  REAL :: ubmc
2278 ! (ub-c)**2
2279  REAL :: ubmc2
2280 ! ubar tendency
2281  REAL :: ubt
2282 ! tbar tendency
2283  REAL :: tbt
2284 ! ubar tendency from wave l
2285  REAL :: ubtl
2286 ! saturation tendency
2287  REAL :: ubtlsat
2288 ! layer pressure
2289  REAL :: pm
2290 ! layer density
2291  REAL :: rhom
2292 ! launch level height
2293  REAL :: zlb
2294 ! c-u
2295  REAL :: cmu
2296  REAL :: dzm, hscal, tautmp
2297  REAL :: utl
2298  REAL :: ttl
2299 ! zonal pseudomomentum flux spectrum
2300  REAL :: fpmx
2301 ! meridional pseudomomentum flux spectrum
2302  REAL :: fpmy
2303 ! energy flux (p'w') spectrum
2304  REAL :: fe
2305 ! pseudoenergy flux (p'w'+U rho u'w') spectrum
2306  REAL :: fpe
2307  REAL :: fpml
2308  REAL :: fpmt
2309  REAL :: fpel
2310  REAL :: fpet
2311  REAL :: dusrcl
2312  REAL :: dvsrcl
2313  REAL :: dtsrcl
2314  REAL :: zi
2315  REAL :: effkwvmap
2316  REAL :: zfac
2317  REAL :: uhtmax
2318  REAL :: utfac
2319  REAL :: tau_min, cmu_, t_new
2320  INTRINSIC max
2321  INTRINSIC sign
2322  INTRINSIC abs
2323  REAL :: x2
2324  REAL :: x1
2325  INTRINSIC sqrt
2326 ! Initialize gravity wave drag tendencies to zero
2327  DO l=-ngwv,ngwv
2328  DO k=pver-1,ktop,-1
2329  IF (k .LE. kbot - 1) THEN
2330  tau(l, k) = 0.
2331  CALL pushinteger4(2)
2332  ELSE
2333  CALL pushinteger4(1)
2334  END IF
2335  END DO
2336  END DO
2337  DO k=1,pver
2338  ut(k) = 0.
2339  vt(k) = 0.
2340  END DO
2341 ! Initialize total momentum and energy fluxes to zero
2342  DO k=0,pver
2343  taugwx(i, k) = 0.
2344  taugwy(i, k) = 0.
2345  fegw(i, k) = 0.
2346  fepgw(i, k) = 0.
2347  END DO
2348 !---------------------------------------------------------------------------
2349 ! Compute the stress profiles and diffusivities
2350 !---------------------------------------------------------------------------
2351 ! Determine the absolute value of the saturation stress and the diffusivity
2352 ! for each wave.
2353 ! Define critical levels where the sign of (u-c) changes between interfaces.
2354 ! Loop from bottom to top to get stress profiles
2355  DO l=-ngwv,ngwv
2356  DO k=pver-1,ktop,-1
2357  IF (k .LE. kbot - 1) THEN
2358  CALL pushreal4(ubmc)
2359 !!!!!!!!jk d = dback(k)
2360  ubmc = ubi(k) - c(l)
2361  IF (ngwv .GT. 0) THEN
2362  CALL pushreal4(effkwvmap)
2363  CALL get_effkwvmap_1(effkwvmap, rlat(i))
2364  IF (-15.0 .LT. rlat(i)*180./pi_gwd .AND. rlat(i)*180./pi_gwd&
2365 & .LT. 15.0) THEN
2366  CALL pushinteger4(0)
2367  effkwvmap = fcrit2*kwvbeq
2368  ELSE
2369  CALL pushinteger4(0)
2370  END IF
2371  ELSE
2372  IF (pi(i, k) .LT. 1000.0) THEN
2373  zfac = (pi(i, k)/1000.0)**3
2374  ELSE
2375  zfac = 1.0
2376  END IF
2377  IF (rlat(i)*180./pi_gwd .LT. -20.0) THEN
2378  CALL pushreal4(effkwvmap)
2379  CALL get_effkwvmap_2(effkwvmap, rlat(i), zfac)
2380  CALL pushinteger4(1)
2381  ELSE
2382  CALL pushreal4(effkwvmap)
2383  CALL get_effkwvmap_3(effkwvmap, rlat(i), zfac)
2384  CALL pushinteger4(2)
2385  END IF
2386  END IF
2387  x1 = effkwvmap*rhoi(k)*ubmc**3/(2.*ni(k))
2388  IF (x1 .GE. 0.) THEN
2389  tausat = x1
2390  CALL pushinteger4(1)
2391  ELSE
2392  tausat = -x1
2393  CALL pushinteger4(0)
2394  END IF
2395  IF (tausat .LE. taumin) THEN
2396  tausat = 0.0
2397  CALL pushinteger4(1)
2398  ELSE
2399  CALL pushinteger4(0)
2400  END IF
2401  IF (ubmc*(ubi(k+1)-c(l)) .LE. 0.0) THEN
2402  tausat = 0.0
2403  CALL pushinteger4(1)
2404  ELSE
2405  CALL pushinteger4(0)
2406  END IF
2407  IF (k .EQ. ktop) THEN
2408  tausat = 0.
2409  CALL pushinteger4(1)
2410  ELSE
2411  CALL pushinteger4(0)
2412  END IF
2413 !
2414  IF (k .EQ. ktop + 1) THEN
2415  tausat = tausat*0.02
2416  CALL pushinteger4(1)
2417  ELSE
2418  CALL pushinteger4(0)
2419  END IF
2420  IF (k .EQ. ktop + 2) THEN
2421  tausat = tausat*0.05
2422  CALL pushinteger4(1)
2423  ELSE
2424  CALL pushinteger4(0)
2425  END IF
2426  IF (k .EQ. ktop + 3) THEN
2427  tausat = tausat*0.10
2428  CALL pushinteger4(1)
2429  ELSE
2430  CALL pushinteger4(0)
2431  END IF
2432  IF (k .EQ. ktop + 4) THEN
2433  tausat = tausat*0.20
2434  CALL pushinteger4(1)
2435  ELSE
2436  CALL pushinteger4(0)
2437  END IF
2438  IF (k .EQ. ktop + 5) THEN
2439  tausat = tausat*0.50
2440  CALL pushinteger4(1)
2441  ELSE
2442  CALL pushinteger4(0)
2443  END IF
2444 !
2445  tau_min = tausat
2446  IF (tau_min .GT. tau(l, k+1)) THEN
2447  tau_min = tau(l, k+1)
2448  CALL pushinteger4(1)
2449  ELSE
2450  CALL pushinteger4(0)
2451  END IF
2452  tau(l, k) = tau_min
2453  CALL pushinteger4(2)
2454  ELSE
2455  CALL pushinteger4(1)
2456  END IF
2457  END DO
2458  END DO
2459 !---------------------------------------------------------------------------
2460 ! Compute the tendencies from the stress divergence.
2461 !---------------------------------------------------------------------------
2462 ! Accumulate the mean wind tendency over wavenumber.
2463 ! Loop over levels from top to bottom
2464  DO k=ktop+1,pver
2465  CALL pushreal4(ubt)
2466  ubt = 0.0
2467  DO l=-ngwv,ngwv
2468  IF (k .LE. kbot) THEN
2469  CALL pushreal4(ubtl)
2470 ! Determine the wind tendency including excess stress carried down from above.
2471  ubtl = mapl_grav*(tau(l, k)-tau(l, k-1))*rdpm(i, k)
2472 ! Calculate the sign of wind tendency
2473  utl = sign(ubtl, c(l) - ubi(k))
2474 ! Accumulate the mean wind tendency over wavenumber.
2475  ubt = ubt + utl
2476 ! Calculate irreversible temperature tendency associated with gravity wave breaking.
2477 ! Adding frictional heating associated with the GW momentum forcing
2478  CALL pushinteger4(2)
2479  ELSE
2480  CALL pushinteger4(1)
2481  END IF
2482  END DO
2483 ! Project the mean wind tendency onto the components and scale by "efficiency".
2484  IF (k .LE. kbot) THEN
2485  ut(k) = ubt*xv*effgw(i)
2486  vt(k) = ubt*yv*effgw(i)
2487  CALL pushinteger4(2)
2488  ELSE
2489  CALL pushinteger4(1)
2490  END IF
2491  END DO
2492 !-----------------------------------------------------------------------
2493 ! Calculates energy and momentum flux profiles
2494 !-----------------------------------------------------------------------
2495  DO l=-ngwv,ngwv
2496  DO k=ktop,pver
2497  IF (k .LE. kbot) THEN
2498  cmu = c(l) - ubi(k)
2499  CALL get_cmu(cmu_, cmu)
2500  fpmx = cmu_*tau(l, k)*xv*effgw(i)
2501  fpmy = cmu_*tau(l, k)*yv*effgw(i)
2502  fe = cmu*cmu_*tau(l, k)*effgw(i)
2503  fpe = c(l)*cmu_*tau(l, k)*effgw(i)
2504 ! Record outputs for GW fluxes
2505  taugwx(i, k) = taugwx(i, k) + fpmx
2506  taugwy(i, k) = taugwy(i, k) + fpmy
2507  fegw(i, k) = fegw(i, k) + fe
2508  fepgw(i, k) = fepgw(i, k) + fpe
2509  END IF
2510  END DO
2511  END DO
2512 !-----------------------------------------------------------------------
2513 ! Adjust efficiency factor to prevent unrealistically strong forcing
2514 !-----------------------------------------------------------------------
2515  uhtmax = 0.0
2516  utfac = 1.0
2517  DO k=1,pver
2518  x2 = sqrt(ut(k)**2 + vt(k)**2)
2519  IF (x2 .LT. uhtmax) THEN
2520  uhtmax = uhtmax
2521  CALL pushinteger4(2)
2522  ELSE
2523  uhtmax = x2
2524  CALL pushinteger4(1)
2525  END IF
2526  END DO
2527  IF (uhtmax .GT. tndmax) THEN
2528  utfac = tndmax/uhtmax
2529  CALL pushinteger4(1)
2530  ELSE
2531  CALL pushinteger4(0)
2532  END IF
2533 !jkim effgw(i) = effgw(i)*utfac
2534  DO k=1,pver
2535  CALL pushreal4(ut(k))
2536  ut(k) = ut(k)*utfac
2537  CALL pushreal4(vt(k))
2538  vt(k) = vt(k)*utfac
2539  END DO
2540  CALL pushreal4(ubmc)
2541  CALL pushreal4(effkwvmap)
2542  CALL pushreal4(ubtl)
2543  CALL pushreal4(utfac)
2544  CALL pushreal4(uhtmax)
2545  CALL pushreal4(ubt)
2546  END SUBROUTINE gw_drag_prof_fwd
2547 ! Differentiation of gw_drag_prof in reverse (adjoint) mode (backward sweep):
2548 ! gradient, with respect to input variables: tau xv ut yv vt
2549 ! ubi
2550 ! of linear combination of output variables: ut vt
2551 !===============================================================================
2552  SUBROUTINE gw_drag_prof_bwd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c&
2553 & , u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, &
2554 & dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, ubm&
2555 & , xv, xvb, yv, yvb, ut, utb, vt, vtb, tt, taugwx, taugwy, fegw, &
2556 & fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y, effgw)
2557  IMPLICIT NONE
2558  INTEGER, INTENT(IN) :: i
2559  INTEGER, INTENT(IN) :: pcols
2560  INTEGER, INTENT(IN) :: pver
2561  INTEGER, INTENT(IN) :: kbot
2562  INTEGER, INTENT(IN) :: ktop
2563  INTEGER, INTENT(IN) :: pgwv
2564  INTEGER, INTENT(IN) :: ngwv
2565  INTEGER, INTENT(IN) :: kldv
2566  INTEGER, INTENT(IN) :: kldvmn
2567  INTEGER, INTENT(IN) :: ksrc
2568  INTEGER, INTENT(IN) :: ksrcmn
2569  REAL :: c(-pgwv:pgwv)
2570  REAL :: u(pcols, pver)
2571  REAL :: v(pcols, pver)
2572  REAL :: t(pcols, pver)
2573  REAL :: pi(pcols, 0:pver)
2574  REAL :: dpm(pcols, pver)
2575  REAL :: rdpm(pcols, pver)
2576  REAL :: piln(pcols, 0:pver)
2577  REAL :: rlat(pcols)
2578  REAL :: rhoi(0:pver)
2579  REAL :: ni(0:pver)
2580  REAL :: ti(0:pver)
2581  REAL :: nm(pver)
2582  REAL :: dt
2583  REAL :: alpha(0:pver)
2584  REAL :: dback(0:pver)
2585  REAL :: rdpldv
2586  REAL :: ubi(0:pver)
2587  REAL :: ubib(0:pver)
2588  REAL :: ubm(pver)
2589  REAL :: xv
2590  REAL :: xvb
2591  REAL :: yv
2592  REAL :: yvb
2593  REAL :: effgw(pcols)
2594  REAL :: tau(-pgwv:pgwv, 0:pver)
2595  REAL :: taub(-pgwv:pgwv, 0:pver)
2596  REAL :: ut(pver)
2597  REAL :: utb(pver)
2598  REAL :: vt(pver)
2599  REAL :: vtb(pver)
2600  REAL :: tt(pver)
2601  REAL :: taugwx(pcols, 0:pver)
2602  REAL :: taugwy(pcols, 0:pver)
2603  REAL :: fegw(pcols, 0:pver)
2604  REAL :: fepgw(pcols, 0:pver)
2605  REAL :: dusrc(pver)
2606  REAL :: dvsrc(pver)
2607  REAL :: dtsrc(pver)
2608  REAL :: tau0x
2609  REAL :: tau0y
2610  INTEGER :: k, l
2611  REAL :: dscal
2612  REAL :: mi
2613  REAL :: taudmp
2614  REAL :: tausat
2615  REAL :: tausatb
2616  REAL :: ubmc
2617  REAL :: ubmcb
2618  REAL :: ubmc2
2619  REAL :: ubt
2620  REAL :: ubtb
2621  REAL :: tbt
2622  REAL :: ubtl
2623  REAL :: ubtlb
2624  REAL :: ubtlsat
2625  REAL :: pm
2626  REAL :: rhom
2627  REAL :: zlb
2628  REAL :: cmu
2629  REAL :: dzm, hscal, tautmp
2630  REAL :: utl
2631  REAL :: utlb
2632  REAL :: ttl
2633  REAL :: fpmx
2634  REAL :: fpmy
2635  REAL :: fe
2636  REAL :: fpe
2637  REAL :: fpml
2638  REAL :: fpmt
2639  REAL :: fpel
2640  REAL :: fpet
2641  REAL :: dusrcl
2642  REAL :: dvsrcl
2643  REAL :: dtsrcl
2644  REAL :: zi
2645  REAL :: effkwvmap
2646  REAL :: zfac
2647  REAL :: uhtmax
2648  REAL :: uhtmaxb
2649  REAL :: utfac
2650  REAL :: utfacb
2651  REAL :: tau_min, cmu_, t_new
2652  REAL :: tau_minb
2653  INTEGER :: branch
2654  REAL :: tempb0
2655  INTRINSIC max
2656  INTRINSIC sign
2657  INTRINSIC abs
2658  REAL :: x2
2659  REAL :: x1
2660  REAL :: x2b
2661  REAL :: tempb
2662  REAL :: x1b
2663  INTRINSIC sqrt
2664  CALL popreal4(ubt)
2665  CALL popreal4(uhtmax)
2666  CALL popreal4(utfac)
2667  CALL popreal4(ubtl)
2668  CALL popreal4(effkwvmap)
2669  CALL popreal4(ubmc)
2670  utfacb = 0.0_8
2671  DO k=pver,1,-1
2672  CALL popreal4(vt(k))
2673  CALL popreal4(ut(k))
2674  utfacb = utfacb + ut(k)*utb(k) + vt(k)*vtb(k)
2675  vtb(k) = utfac*vtb(k)
2676  utb(k) = utfac*utb(k)
2677  END DO
2678  CALL popinteger4(branch)
2679  IF (branch .LT. 1) THEN
2680  uhtmaxb = 0.0_8
2681  ELSE
2682  uhtmaxb = -(tndmax*utfacb/uhtmax**2)
2683  END IF
2684  DO k=pver,1,-1
2685  CALL popinteger4(branch)
2686  IF (branch .LT. 2) THEN
2687  x2b = uhtmaxb
2688  uhtmaxb = 0.0_8
2689  ELSE
2690  x2b = 0.0_8
2691  END IF
2692  IF (ut(k)**2 + vt(k)**2 .EQ. 0.0) THEN
2693  tempb0 = 0.0
2694  ELSE
2695  tempb0 = x2b/(2.0*sqrt(ut(k)**2+vt(k)**2))
2696  END IF
2697  utb(k) = utb(k) + 2*ut(k)*tempb0
2698  vtb(k) = vtb(k) + 2*vt(k)*tempb0
2699  END DO
2700  taub(:, :) = 0.0_8
2701  xvb = 0.0_8
2702  yvb = 0.0_8
2703  DO k=pver,ktop+1,-1
2704  CALL popinteger4(branch)
2705  IF (branch .LT. 2) THEN
2706  ubtb = 0.0_8
2707  ELSE
2708  ubtb = effgw(i)*xv*utb(k) + effgw(i)*yv*vtb(k)
2709  yvb = yvb + effgw(i)*ubt*vtb(k)
2710  vtb(k) = 0.0_8
2711  xvb = xvb + effgw(i)*ubt*utb(k)
2712  utb(k) = 0.0_8
2713  END IF
2714  DO l=ngwv,-ngwv,-1
2715  CALL popinteger4(branch)
2716  IF (.NOT.branch .LT. 2) THEN
2717  utlb = ubtb
2718  ubtlb = sign(1.d0, ubtl*(c(l)-ubi(k)))*utlb
2719  CALL popreal4(ubtl)
2720  tempb = mapl_grav*rdpm(i, k)*ubtlb
2721  taub(l, k) = taub(l, k) + tempb
2722  taub(l, k-1) = taub(l, k-1) - tempb
2723  END IF
2724  END DO
2725  CALL popreal4(ubt)
2726  END DO
2727  ubib(:) = 0.0_8
2728  DO l=ngwv,-ngwv,-1
2729  DO k=ktop,pver-1,1
2730  CALL popinteger4(branch)
2731  IF (.NOT.branch .LT. 2) THEN
2732  tau_minb = taub(l, k)
2733  taub(l, k) = 0.0_8
2734  CALL popinteger4(branch)
2735  IF (.NOT.branch .LT. 1) THEN
2736  taub(l, k+1) = taub(l, k+1) + tau_minb
2737  tau_minb = 0.0_8
2738  END IF
2739  tausatb = tau_minb
2740  CALL popinteger4(branch)
2741  IF (.NOT.branch .LT. 1) tausatb = 0.50*tausatb
2742  CALL popinteger4(branch)
2743  IF (.NOT.branch .LT. 1) tausatb = 0.20*tausatb
2744  CALL popinteger4(branch)
2745  IF (.NOT.branch .LT. 1) tausatb = 0.10*tausatb
2746  CALL popinteger4(branch)
2747  IF (.NOT.branch .LT. 1) tausatb = 0.05*tausatb
2748  CALL popinteger4(branch)
2749  IF (.NOT.branch .LT. 1) tausatb = 0.02*tausatb
2750  CALL popinteger4(branch)
2751  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
2752  CALL popinteger4(branch)
2753  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
2754  CALL popinteger4(branch)
2755  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
2756  CALL popinteger4(branch)
2757  IF (branch .LT. 1) THEN
2758  x1b = -tausatb
2759  ELSE
2760  x1b = tausatb
2761  END IF
2762  ubmcb = effkwvmap*rhoi(k)*3*ubmc**2*x1b/(2.*ni(k))
2763  CALL popinteger4(branch)
2764  IF (branch .LT. 2) THEN
2765  IF (branch .LT. 1) THEN
2766  CALL popreal4(effkwvmap)
2767  ELSE
2768  CALL popreal4(effkwvmap)
2769  END IF
2770  ELSE
2771  CALL popreal4(effkwvmap)
2772  END IF
2773  CALL popreal4(ubmc)
2774  ubib(k) = ubib(k) + ubmcb
2775  END IF
2776  END DO
2777  END DO
2778  DO k=pver,1,-1
2779  vtb(k) = 0.0_8
2780  utb(k) = 0.0_8
2781  END DO
2782  DO l=ngwv,-ngwv,-1
2783  DO k=ktop,pver-1,1
2784  CALL popinteger4(branch)
2785  IF (.NOT.branch .LT. 2) taub(l, k) = 0.0_8
2786  END DO
2787  END DO
2788  END SUBROUTINE gw_drag_prof_bwd
2789 !===============================================================================
2790  SUBROUTINE gw_drag_prof(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, &
2791 & v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback&
2792 & , kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut, vt&
2793 & , tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y&
2794 & , effgw)
2795  IMPLICIT NONE
2796 !-----------------------------------------------------------------------
2797 ! Solve for the drag profile from the multiple gravity wave drag
2798 ! parameterization.
2799 ! 1. scan up from the wave source to determine the stress profile
2800 ! 2. scan down the stress profile to determine the tendencies
2801 ! => apply bounds to the tendency
2802 ! a. from wkb solution
2803 ! b. from computational stability constraints
2804 ! => adjust stress on interface below to reflect actual bounded tendency
2805 !-----------------------------------------------------------------------
2806 !------------------------------Arguments--------------------------------
2807 ! number of current column
2808  INTEGER, INTENT(IN) :: i
2809 ! number of atmospheric columns
2810  INTEGER, INTENT(IN) :: pcols
2811 ! number of atmospheric columns
2812  INTEGER, INTENT(IN) :: pver
2813 ! index of bottom (source) interface
2814  INTEGER, INTENT(IN) :: kbot
2815 ! index of top interface of gwd region
2816  INTEGER, INTENT(IN) :: ktop
2817 ! number of gravity waves possible
2818  INTEGER, INTENT(IN) :: pgwv
2819 ! number of gravity waves to use
2820  INTEGER, INTENT(IN) :: ngwv
2821 ! top interface of low level stress divergence region
2822  INTEGER, INTENT(IN) :: kldv
2823 ! min value of kldv
2824  INTEGER, INTENT(IN) :: kldvmn
2825 ! index of top interface of source region
2826  INTEGER, INTENT(IN) :: ksrc
2827 ! min value of ksrc
2828  INTEGER, INTENT(IN) :: ksrcmn
2829 ! wave phase speeds
2830  REAL :: c(-pgwv:pgwv)
2831 ! midpoint zonal wind
2832  REAL :: u(pcols, pver)
2833 ! midpoint meridional wind
2834  REAL :: v(pcols, pver)
2835 ! midpoint temperatures
2836  REAL :: t(pcols, pver)
2837 ! interface pressures
2838  REAL :: pi(pcols, 0:pver)
2839 ! midpoint delta p (pi(k)-pi(k-1))
2840  REAL :: dpm(pcols, pver)
2841 ! 1. / (pi(k)-pi(k-1))
2842  REAL :: rdpm(pcols, pver)
2843 ! ln(interface pressures)
2844  REAL :: piln(pcols, 0:pver)
2845  REAL :: rlat(pcols)
2846 ! interface density
2847  REAL :: rhoi(0:pver)
2848 ! interface Brunt-Vaisalla frequency
2849  REAL :: ni(0:pver)
2850 ! interface temperature
2851  REAL :: ti(0:pver)
2852 ! midpoint Brunt-Vaisalla frequency
2853  REAL :: nm(pver)
2854 ! time step
2855  REAL :: dt
2856 ! newtonian cooling coefficients
2857  REAL :: alpha(0:pver)
2858 ! newtonian cooling coefficients
2859  REAL :: dback(0:pver)
2860 ! 1/dp across low level divergence region
2861  REAL :: rdpldv
2862 ! projection of wind at interfaces
2863  REAL :: ubi(0:pver)
2864 ! projection of wind at midpoints
2865  REAL :: ubm(pver)
2866 ! unit vectors of source wind (x)
2867  REAL :: xv
2868 ! unit vectors of source wind (y)
2869  REAL :: yv
2870 ! tendency efficiency for gwd
2871  REAL :: effgw(pcols)
2872 ! wave Reynolds stress
2873  REAL :: tau(-pgwv:pgwv, 0:pver)
2874 ! zonal wind tendency
2875  REAL :: ut(pver)
2876 ! meridional wind tendency
2877  REAL :: vt(pver)
2878 ! temperature tendency
2879  REAL :: tt(pver)
2880 ! Total zonal GW momentum flux
2881  REAL :: taugwx(pcols, 0:pver)
2882 ! Total meridional GW momentum flux
2883  REAL :: taugwy(pcols, 0:pver)
2884 ! Total GW energy flux
2885  REAL :: fegw(pcols, 0:pver)
2886 ! Total GW pseudo energy flux
2887  REAL :: fepgw(pcols, 0:pver)
2888 ! Total U tendency below launch level
2889  REAL :: dusrc(pver)
2890 ! Total V tendency below launch level
2891  REAL :: dvsrc(pver)
2892 ! Total V tendency below launch level
2893  REAL :: dtsrc(pver)
2894 ! c=0 sfc. stress (zonal)
2895  REAL :: tau0x
2896 ! c=0 sfc. stress (meridional)
2897  REAL :: tau0y
2898 !---------------------------Local storage-------------------------------
2899 ! loop indexes
2900  INTEGER :: k, l
2901 ! fraction of dsat to use
2902  REAL :: dscal
2903 ! imaginary part of vertical wavenumber
2904  REAL :: mi
2905 ! stress after damping
2906  REAL :: taudmp
2907 !real :: taumax(pcols) ! max(tau) for any l
2908 ! saturation stress
2909  REAL :: tausat
2910 ! (ub-c)
2911  REAL :: ubmc
2912 ! (ub-c)**2
2913  REAL :: ubmc2
2914 ! ubar tendency
2915  REAL :: ubt
2916 ! tbar tendency
2917  REAL :: tbt
2918 ! ubar tendency from wave l
2919  REAL :: ubtl
2920 ! saturation tendency
2921  REAL :: ubtlsat
2922 ! layer pressure
2923  REAL :: pm
2924 ! layer density
2925  REAL :: rhom
2926 ! launch level height
2927  REAL :: zlb
2928 ! c-u
2929  REAL :: cmu
2930  REAL :: dzm, hscal, tautmp
2931  REAL :: utl
2932  REAL :: ttl
2933 ! zonal pseudomomentum flux spectrum
2934  REAL :: fpmx
2935 ! meridional pseudomomentum flux spectrum
2936  REAL :: fpmy
2937 ! energy flux (p'w') spectrum
2938  REAL :: fe
2939 ! pseudoenergy flux (p'w'+U rho u'w') spectrum
2940  REAL :: fpe
2941  REAL :: fpml
2942  REAL :: fpmt
2943  REAL :: fpel
2944  REAL :: fpet
2945  REAL :: dusrcl
2946  REAL :: dvsrcl
2947  REAL :: dtsrcl
2948  REAL :: zi
2949  REAL :: effkwvmap
2950  REAL :: zfac
2951  REAL :: uhtmax
2952  REAL :: utfac
2953  REAL :: tau_min, cmu_, t_new
2954  INTRINSIC max
2955  INTRINSIC sign
2956  INTRINSIC abs
2957  REAL :: x2
2958  REAL :: x1
2959  INTRINSIC sqrt
2960 ! Initialize gravity wave drag tendencies to zero
2961  DO l=-ngwv,ngwv
2962  DO k=pver-1,ktop,-1
2963  IF (k .LE. kbot - 1) tau(l, k) = 0.
2964  END DO
2965  END DO
2966  DO k=1,pver
2967  ut(k) = 0.
2968  vt(k) = 0.
2969  tt(k) = 0.
2970  dusrc(k) = 0.
2971  dvsrc(k) = 0.
2972  dtsrc(k) = 0.
2973  END DO
2974 ! Initialize total momentum and energy fluxes to zero
2975  DO k=0,pver
2976  taugwx(i, k) = 0.
2977  taugwy(i, k) = 0.
2978  fegw(i, k) = 0.
2979  fepgw(i, k) = 0.
2980  END DO
2981 ! Initialize surface wave stress at c = 0 to zero
2982  tau0x = 0.
2983  tau0y = 0.
2984 !---------------------------------------------------------------------------
2985 ! Compute the stress profiles and diffusivities
2986 !---------------------------------------------------------------------------
2987 ! Determine the absolute value of the saturation stress and the diffusivity
2988 ! for each wave.
2989 ! Define critical levels where the sign of (u-c) changes between interfaces.
2990 ! Loop from bottom to top to get stress profiles
2991  DO l=-ngwv,ngwv
2992  DO k=pver-1,ktop,-1
2993  IF (k .LE. kbot - 1) THEN
2994 !!!!!!!!jk d = dback(k)
2995  ubmc = ubi(k) - c(l)
2996  IF (ngwv .GT. 0) THEN
2997  CALL get_effkwvmap_1(effkwvmap, rlat(i))
2998  IF (-15.0 .LT. rlat(i)*180./pi_gwd .AND. rlat(i)*180./pi_gwd&
2999 & .LT. 15.0) effkwvmap = fcrit2*kwvbeq
3000  ELSE
3001  IF (pi(i, k) .LT. 1000.0) THEN
3002  zfac = (pi(i, k)/1000.0)**3
3003  ELSE
3004  zfac = 1.0
3005  END IF
3006  IF (rlat(i)*180./pi_gwd .LT. -20.0) THEN
3007  CALL get_effkwvmap_2(effkwvmap, rlat(i), zfac)
3008  ELSE
3009  CALL get_effkwvmap_3(effkwvmap, rlat(i), zfac)
3010  END IF
3011  END IF
3012  x1 = effkwvmap*rhoi(k)*ubmc**3/(2.*ni(k))
3013  IF (x1 .GE. 0.) THEN
3014  tausat = x1
3015  ELSE
3016  tausat = -x1
3017  END IF
3018  IF (tausat .LE. taumin) tausat = 0.0
3019  IF (ubmc*(ubi(k+1)-c(l)) .LE. 0.0) tausat = 0.0
3020  IF (k .EQ. ktop) tausat = 0.
3021 !
3022  IF (k .EQ. ktop + 1) tausat = tausat*0.02
3023  IF (k .EQ. ktop + 2) tausat = tausat*0.05
3024  IF (k .EQ. ktop + 3) tausat = tausat*0.10
3025  IF (k .EQ. ktop + 4) tausat = tausat*0.20
3026  IF (k .EQ. ktop + 5) tausat = tausat*0.50
3027 !
3028  tau_min = tausat
3029  IF (tau_min .GT. tau(l, k+1)) tau_min = tau(l, k+1)
3030  tau(l, k) = tau_min
3031  END IF
3032  END DO
3033  END DO
3034 !---------------------------------------------------------------------------
3035 ! Compute the tendencies from the stress divergence.
3036 !---------------------------------------------------------------------------
3037 ! Accumulate the mean wind tendency over wavenumber.
3038 ! Loop over levels from top to bottom
3039  DO k=ktop+1,pver
3040  ubt = 0.0
3041  tbt = 0.0
3042  DO l=-ngwv,ngwv
3043  IF (k .LE. kbot) THEN
3044 ! Determine the wind tendency including excess stress carried down from above.
3045  ubtl = mapl_grav*(tau(l, k)-tau(l, k-1))*rdpm(i, k)
3046 ! Calculate the sign of wind tendency
3047  utl = sign(ubtl, c(l) - ubi(k))
3048 ! Accumulate the mean wind tendency over wavenumber.
3049  ubt = ubt + utl
3050 ! Calculate irreversible temperature tendency associated with gravity wave breaking.
3051 ! Adding frictional heating associated with the GW momentum forcing
3052  ttl = (c(l)-ubm(k))*utl/mapl_cp
3053 ! Adding frictional heating associated with the GW momentum forcing
3054  tbt = tbt + ttl
3055  END IF
3056  END DO
3057 ! Project the mean wind tendency onto the components and scale by "efficiency".
3058  IF (k .LE. kbot) THEN
3059  ut(k) = ubt*xv*effgw(i)
3060  vt(k) = ubt*yv*effgw(i)
3061  tt(k) = tbt*effgw(i)
3062  END IF
3063  END DO
3064 !-----------------------------------------------------------------------
3065 ! Calculates wind and temperature tendencies below launch level for
3066 ! energy and momentum conservation (does nothing for orographic GWs).
3067 !-----------------------------------------------------------------------
3068 !----kimmmmm here
3069 ! Calculate launch level height
3070  zlb = 0.
3071  DO k=ktop+1,pver
3072  IF (k .GE. kbot + 1) THEN
3073 ! Define layer pressure and density
3074  pm = (pi(i, k-1)+pi(i, k))*0.5
3075  CALL get_ti(t_new, t(i, k))
3076  rhom = pm/(mapl_rgas*t_new)
3077  zlb = zlb + dpm(i, k)/mapl_grav/rhom
3078  END IF
3079  END DO
3080 !-----------------------------------------------------------------------
3081 ! Calculates energy and momentum flux profiles
3082 !-----------------------------------------------------------------------
3083  DO l=-ngwv,ngwv
3084  DO k=ktop,pver
3085  IF (k .LE. kbot) THEN
3086  cmu = c(l) - ubi(k)
3087  CALL get_cmu(cmu_, cmu)
3088  fpmx = cmu_*tau(l, k)*xv*effgw(i)
3089  fpmy = cmu_*tau(l, k)*yv*effgw(i)
3090  fe = cmu*cmu_*tau(l, k)*effgw(i)
3091  fpe = c(l)*cmu_*tau(l, k)*effgw(i)
3092  IF (k .EQ. kbot) THEN
3093  fpml = fpmx*xv + fpmy*yv
3094  fpel = fpe
3095  END IF
3096  IF (k .EQ. ktop) THEN
3097  fpmt = fpmx*xv + fpmy*yv
3098  fpet = fpe
3099  END IF
3100 ! Record outputs for GW fluxes
3101  taugwx(i, k) = taugwx(i, k) + fpmx
3102  taugwy(i, k) = taugwy(i, k) + fpmy
3103  fegw(i, k) = fegw(i, k) + fe
3104  fepgw(i, k) = fepgw(i, k) + fpe
3105  END IF
3106  END DO
3107  DO k=ktop+1,pver
3108  IF (k .GE. kbot + 1) THEN
3109 ! Define layer pressure and density
3110 ! Add sub-source wind and temperature tendencies
3111  pm = (pi(i, k-1)+pi(i, k))*0.5
3112  CALL get_ti(t_new, t(i, k))
3113  rhom = pm/(mapl_rgas*t_new)
3114  dusrcl = -((fpml-fpmt)/(rhom*zlb)*xv)
3115  dvsrcl = -((fpml-fpmt)/(rhom*zlb)*yv)
3116  dtsrcl = -((fpel-fpet-ubm(k)*(fpml-fpmt))/(rhom*zlb*mapl_cp))
3117 ! Add sub-source wind and temperature tendencies
3118  dusrc(k) = dusrc(k) + dusrcl
3119  dvsrc(k) = dvsrc(k) + dvsrcl
3120  dtsrc(k) = dtsrc(k) + dtsrcl
3121  END IF
3122  END DO
3123  END DO
3124 ! For orographic waves, sub-source tendencies are set equal to zero.
3125  DO k=1,pver
3126  IF (ngwv .EQ. 0) THEN
3127  dusrc(k) = 0.0
3128  dvsrc(k) = 0.0
3129  dtsrc(k) = 0.0
3130  END IF
3131  END DO
3132 !-----------------------------------------------------------------------
3133 ! Adjust efficiency factor to prevent unrealistically strong forcing
3134 !-----------------------------------------------------------------------
3135  uhtmax = 0.0
3136  utfac = 1.0
3137  DO k=1,pver
3138  x2 = sqrt(ut(k)**2 + vt(k)**2)
3139  IF (x2 .LT. uhtmax) THEN
3140  uhtmax = uhtmax
3141  ELSE
3142  uhtmax = x2
3143  END IF
3144  END DO
3145  IF (uhtmax .GT. tndmax) utfac = tndmax/uhtmax
3146 !jkim effgw(i) = effgw(i)*utfac
3147  DO k=1,pver
3148  ut(k) = ut(k)*utfac
3149  vt(k) = vt(k)*utfac
3150  tt(k) = tt(k)*utfac
3151  dusrc(k) = dusrc(k)*utfac
3152  dvsrc(k) = dvsrc(k)*utfac
3153  dtsrc(k) = dtsrc(k)*utfac
3154  END DO
3155 !-----------------------------------------------------------------------
3156 ! Project the c=0 stress (scaled) in the direction of the source wind
3157 ! for recording on the output file.
3158 !-----------------------------------------------------------------------
3159  tau0x = tau(0, kbot)*xv*effgw(i)*utfac
3160  tau0y = tau(0, kbot)*yv*effgw(i)*utfac
3161  RETURN
3162  END SUBROUTINE gw_drag_prof
3163 ! Differentiation of gw_drag_prof_bgnd in reverse (adjoint) mode:
3164 ! gradient, with respect to input variables: xv yv ubi
3165 ! of linear combination of output variables: tau ut vt ubi dvsrc
3166 ! dusrc
3167  SUBROUTINE gw_drag_prof_bgnd_b(i, pcols, pver, pgwv, ngwv, kbot, ktop&
3168 & , c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha&
3169 & , dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, &
3170 & ubm, xv, xvb, yv, yvb, ut, utb, vt, vtb, tt, taugwx, taugwy, fegw, &
3171 & fepgw, dusrc, dusrcb, dvsrc, dvsrcb, dtsrc, tau0x, tau0y, effgw)
3172  IMPLICIT NONE
3173 !-----------------------------------------------------------------------
3174 ! Solve for the drag profile from the multiple gravity wave drag
3175 ! parameterization.
3176 ! 1. scan up from the wave source to determine the stress profile
3177 ! 2. scan down the stress profile to determine the tendencies
3178 ! => apply bounds to the tendency
3179 ! a. from wkb solution
3180 ! b. from computational stability constraints
3181 ! => adjust stress on interface below to reflect actual bounded tendency
3182 !-----------------------------------------------------------------------
3183 !------------------------------Arguments--------------------------------
3184 ! number of current column
3185  INTEGER, INTENT(IN) :: i
3186 ! number of atmospheric columns
3187  INTEGER, INTENT(IN) :: pcols
3188 ! number of atmospheric columns
3189  INTEGER, INTENT(IN) :: pver
3190 ! index of bottom (source) interface
3191  INTEGER, INTENT(IN) :: kbot
3192 ! index of top interface of gwd region
3193  INTEGER, INTENT(IN) :: ktop
3194 ! number of gravity waves possible
3195  INTEGER, INTENT(IN) :: pgwv
3196 ! number of gravity waves to use
3197  INTEGER, INTENT(IN) :: ngwv
3198 ! top interface of low level stress divergence region
3199  INTEGER, INTENT(IN) :: kldv
3200 ! min value of kldv
3201  INTEGER, INTENT(IN) :: kldvmn
3202 ! index of top interface of source region
3203  INTEGER, INTENT(IN) :: ksrc
3204 ! min value of ksrc
3205  INTEGER, INTENT(IN) :: ksrcmn
3206 ! wave phase speeds
3207  REAL :: c(-pgwv:pgwv)
3208 ! midpoint zonal wind
3209  REAL :: u(pcols, pver)
3210 ! midpoint meridional wind
3211  REAL :: v(pcols, pver)
3212 ! midpoint temperatures
3213  REAL :: t(pcols, pver)
3214 ! interface pressures
3215  REAL :: pi(pcols, 0:pver)
3216 ! midpoint delta p (pi(k)-pi(k-1))
3217  REAL :: dpm(pcols, pver)
3218 ! 1. / (pi(k)-pi(k-1))
3219  REAL :: rdpm(pcols, pver)
3220 ! ln(interface pressures)
3221  REAL :: piln(pcols, 0:pver)
3222  REAL :: rlat(pcols)
3223 ! interface density
3224  REAL :: rhoi(0:pver)
3225 ! interface Brunt-Vaisalla frequency
3226  REAL :: ni(0:pver)
3227 ! interface temperature
3228  REAL :: ti(0:pver)
3229 ! midpoint Brunt-Vaisalla frequency
3230  REAL :: nm(pver)
3231 ! time step
3232  REAL :: dt
3233 ! newtonian cooling coefficients
3234  REAL :: alpha(0:pver)
3235 ! newtonian cooling coefficients
3236  REAL :: dback(0:pver)
3237 ! 1/dp across low level divergence region
3238  REAL :: rdpldv
3239 ! projection of wind at interfaces
3240  REAL :: ubi(0:pver)
3241  REAL :: ubib(0:pver)
3242 ! projection of wind at midpoints
3243  REAL :: ubm(pver)
3244 ! unit vectors of source wind (x)
3245  REAL :: xv
3246  REAL :: xvb
3247 ! unit vectors of source wind (y)
3248  REAL :: yv
3249  REAL :: yvb
3250 ! tendency efficiency for gwd
3251  REAL :: effgw(pcols)
3252 ! wave Reynolds stress
3253  REAL :: tau(-pgwv:pgwv, 0:pver)
3254  REAL :: taub(-pgwv:pgwv, 0:pver)
3255 ! zonal wind tendency
3256  REAL :: ut(pver)
3257  REAL :: utb(pver)
3258 ! meridional wind tendency
3259  REAL :: vt(pver)
3260  REAL :: vtb(pver)
3261 ! temperature tendency
3262  REAL :: tt(pver)
3263 ! Total zonal GW momentum flux
3264  REAL :: taugwx(pcols, 0:pver)
3265 ! Total meridional GW momentum flux
3266  REAL :: taugwy(pcols, 0:pver)
3267 ! Total GW energy flux
3268  REAL :: fegw(pcols, 0:pver)
3269 ! Total GW pseudo energy flux
3270  REAL :: fepgw(pcols, 0:pver)
3271 ! Total U tendency below launch level
3272  REAL :: dusrc(pver)
3273  REAL :: dusrcb(pver)
3274 ! Total V tendency below launch level
3275  REAL :: dvsrc(pver)
3276  REAL :: dvsrcb(pver)
3277 ! Total V tendency below launch level
3278  REAL :: dtsrc(pver)
3279 ! c=0 sfc. stress (zonal)
3280  REAL :: tau0x
3281 ! c=0 sfc. stress (meridional)
3282  REAL :: tau0y
3283 !---------------------------Local storage-------------------------------
3284 ! loop indexes
3285  INTEGER :: k, l
3286 ! fraction of dsat to use
3287  REAL :: dscal
3288 ! imaginary part of vertical wavenumber
3289  REAL :: mi
3290 ! stress after damping
3291  REAL :: taudmp
3292 !real :: taumax(pcols) ! max(tau) for any l
3293 ! saturation stress
3294  REAL :: tausat
3295  REAL :: tausatb
3296 ! (ub-c)
3297  REAL :: ubmc
3298  REAL :: ubmcb
3299 ! (ub-c)**2
3300  REAL :: ubmc2
3301 ! ubar tendency
3302  REAL :: ubt
3303  REAL :: ubtb
3304 ! tbar tendency
3305  REAL :: tbt
3306 ! ubar tendency from wave l
3307  REAL :: ubtl
3308  REAL :: ubtlb
3309 ! saturation tendency
3310  REAL :: ubtlsat
3311 ! layer pressure
3312  REAL :: pm
3313 ! layer density
3314  REAL :: rhom
3315 ! launch level height
3316  REAL :: zlb
3317 ! c-u
3318  REAL :: cmu
3319  REAL :: dzm, hscal, tautmp
3320  REAL :: utl
3321  REAL :: utlb
3322  REAL :: ttl
3323 ! zonal pseudomomentum flux spectrum
3324  REAL :: fpmx
3325  REAL :: fpmxb
3326 ! meridional pseudomomentum flux spectrum
3327  REAL :: fpmy
3328  REAL :: fpmyb
3329 ! energy flux (p'w') spectrum
3330  REAL :: fe
3331 ! pseudoenergy flux (p'w'+U rho u'w') spectrum
3332  REAL :: fpe
3333  REAL :: fpml
3334  REAL :: fpmlb
3335  REAL :: fpmt
3336  REAL :: fpmtb
3337  REAL :: fpel
3338  REAL :: fpet
3339  REAL :: dusrcl
3340  REAL :: dusrclb
3341  REAL :: dvsrcl
3342  REAL :: dvsrclb
3343  REAL :: dtsrcl
3344  REAL :: zi
3345  REAL :: effkwvmap
3346  REAL :: zfac
3347  REAL :: uhtmax
3348  REAL :: uhtmaxb
3349  REAL :: utfac
3350  REAL :: utfacb
3351  REAL :: tau_min, cmu_, t_new
3352  REAL :: tau_minb
3353  INTEGER :: branch
3354  REAL :: tempb4
3355  REAL :: tempb3
3356  REAL :: tempb2
3357  REAL :: tempb1
3358  REAL :: tempb0
3359  INTRINSIC max
3360  INTRINSIC sign
3361  INTRINSIC abs
3362  REAL :: x2
3363  REAL :: x1
3364  REAL :: x2b
3365  REAL :: tempb
3366  REAL :: x1b
3367  INTRINSIC sqrt
3368 ! Initialize gravity wave drag tendencies to zero
3369  DO l=-ngwv,ngwv
3370  DO k=pver-1,ktop,-1
3371  IF (k .LE. kbot - 1) tau(l, k) = 0.
3372  END DO
3373  END DO
3374  DO k=1,pver
3375  ut(k) = 0.
3376  vt(k) = 0.
3377  dusrc(k) = 0.
3378  dvsrc(k) = 0.
3379  END DO
3380 !---------------------------------------------------------------------------
3381 ! Compute the stress profiles and diffusivities
3382 !---------------------------------------------------------------------------
3383 ! Determine the absolute value of the saturation stress and the diffusivity
3384 ! for each wave.
3385 ! Define critical levels where the sign of (u-c) changes between interfaces.
3386 ! Loop from bottom to top to get stress profiles
3387  DO l=-ngwv,ngwv
3388  DO k=pver-1,ktop,-1
3389  IF (k .LE. kbot - 1) THEN
3390  CALL pushreal4(ubmc)
3391 !!!!!!!!jk d = dback(k)
3392  ubmc = ubi(k) - c(l)
3393  IF (ngwv .GT. 0) THEN
3394  CALL pushreal4(effkwvmap)
3395  CALL get_effkwvmap_1(effkwvmap, rlat(i))
3396  IF (-15.0 .LT. rlat(i)*180./pi_gwd .AND. rlat(i)*180./pi_gwd&
3397 & .LT. 15.0) THEN
3398  CALL pushinteger4(0)
3399  effkwvmap = fcrit2*kwvbeq
3400  ELSE
3401  CALL pushinteger4(0)
3402  END IF
3403  ELSE
3404  IF (pi(i, k) .LT. 1000.0) THEN
3405  zfac = (pi(i, k)/1000.0)**3
3406  ELSE
3407  zfac = 1.0
3408  END IF
3409  IF (rlat(i)*180./pi_gwd .LT. -20.0) THEN
3410  CALL pushreal4(effkwvmap)
3411  CALL get_effkwvmap_2(effkwvmap, rlat(i), zfac)
3412  CALL pushinteger4(1)
3413  ELSE
3414  CALL pushreal4(effkwvmap)
3415  CALL get_effkwvmap_3(effkwvmap, rlat(i), zfac)
3416  CALL pushinteger4(2)
3417  END IF
3418  END IF
3419  x1 = effkwvmap*rhoi(k)*ubmc**3/(2.*ni(k))
3420  IF (x1 .GE. 0.) THEN
3421  tausat = x1
3422  CALL pushinteger4(1)
3423  ELSE
3424  tausat = -x1
3425  CALL pushinteger4(0)
3426  END IF
3427  IF (tausat .LE. taumin) THEN
3428  tausat = 0.0
3429  CALL pushinteger4(1)
3430  ELSE
3431  CALL pushinteger4(0)
3432  END IF
3433  IF (ubmc*(ubi(k+1)-c(l)) .LE. 0.0) THEN
3434  tausat = 0.0
3435  CALL pushinteger4(1)
3436  ELSE
3437  CALL pushinteger4(0)
3438  END IF
3439  IF (k .EQ. ktop) THEN
3440  tausat = 0.
3441  CALL pushinteger4(1)
3442  ELSE
3443  CALL pushinteger4(0)
3444  END IF
3445 !
3446  IF (k .EQ. ktop + 1) THEN
3447  tausat = tausat*0.02
3448  CALL pushinteger4(1)
3449  ELSE
3450  CALL pushinteger4(0)
3451  END IF
3452  IF (k .EQ. ktop + 2) THEN
3453  tausat = tausat*0.05
3454  CALL pushinteger4(1)
3455  ELSE
3456  CALL pushinteger4(0)
3457  END IF
3458  IF (k .EQ. ktop + 3) THEN
3459  tausat = tausat*0.10
3460  CALL pushinteger4(1)
3461  ELSE
3462  CALL pushinteger4(0)
3463  END IF
3464  IF (k .EQ. ktop + 4) THEN
3465  tausat = tausat*0.20
3466  CALL pushinteger4(1)
3467  ELSE
3468  CALL pushinteger4(0)
3469  END IF
3470  IF (k .EQ. ktop + 5) THEN
3471  tausat = tausat*0.50
3472  CALL pushinteger4(1)
3473  ELSE
3474  CALL pushinteger4(0)
3475  END IF
3476 !
3477  tau_min = tausat
3478  IF (tau_min .GT. tau(l, k+1)) THEN
3479  tau_min = tau(l, k+1)
3480  CALL pushinteger4(1)
3481  ELSE
3482  CALL pushinteger4(0)
3483  END IF
3484  tau(l, k) = tau_min
3485  CALL pushinteger4(2)
3486  ELSE
3487  CALL pushinteger4(1)
3488  END IF
3489  END DO
3490  END DO
3491 !---------------------------------------------------------------------------
3492 ! Compute the tendencies from the stress divergence.
3493 !---------------------------------------------------------------------------
3494 ! Accumulate the mean wind tendency over wavenumber.
3495 ! Loop over levels from top to bottom
3496  DO k=ktop+1,pver
3497  CALL pushreal4(ubt)
3498  ubt = 0.0
3499  DO l=-ngwv,ngwv
3500  IF (k .LE. kbot) THEN
3501  CALL pushreal4(ubtl)
3502 ! Determine the wind tendency including excess stress carried down from above.
3503  ubtl = mapl_grav*(tau(l, k)-tau(l, k-1))*rdpm(i, k)
3504 ! Calculate the sign of wind tendency
3505  utl = sign(ubtl, c(l) - ubi(k))
3506 ! Accumulate the mean wind tendency over wavenumber.
3507  ubt = ubt + utl
3508 ! Calculate irreversible temperature tendency associated with gravity wave breaking.
3509 ! Adding frictional heating associated with the GW momentum forcing
3510  CALL pushinteger4(2)
3511  ELSE
3512  CALL pushinteger4(1)
3513  END IF
3514  END DO
3515 ! Project the mean wind tendency onto the components and scale by "efficiency".
3516  IF (k .LE. kbot) THEN
3517  ut(k) = ubt*xv*effgw(i)
3518  vt(k) = ubt*yv*effgw(i)
3519  CALL pushinteger4(2)
3520  ELSE
3521  CALL pushinteger4(1)
3522  END IF
3523  END DO
3524 !-----------------------------------------------------------------------
3525 ! Calculates wind and temperature tendencies below launch level for
3526 ! energy and momentum conservation (does nothing for orographic GWs).
3527 !-----------------------------------------------------------------------
3528 !----kimmmmm here
3529 ! Calculate launch level height
3530  zlb = 0.
3531  DO k=ktop+1,pver
3532  IF (k .GE. kbot + 1) THEN
3533 ! Define layer pressure and density
3534  pm = (pi(i, k-1)+pi(i, k))*0.5
3535  CALL get_ti(t_new, t(i, k))
3536  rhom = pm/(mapl_rgas*t_new)
3537  zlb = zlb + dpm(i, k)/mapl_grav/rhom
3538  END IF
3539  END DO
3540 !-----------------------------------------------------------------------
3541 ! Calculates energy and momentum flux profiles
3542 !-----------------------------------------------------------------------
3543  DO l=-ngwv,ngwv
3544  DO k=ktop,pver
3545  IF (k .LE. kbot) THEN
3546  cmu = c(l) - ubi(k)
3547  CALL pushreal4(cmu_)
3548  CALL get_cmu(cmu_, cmu)
3549  CALL pushreal4(fpmx)
3550  fpmx = cmu_*tau(l, k)*xv*effgw(i)
3551  CALL pushreal4(fpmy)
3552  fpmy = cmu_*tau(l, k)*yv*effgw(i)
3553  IF (k .EQ. kbot) THEN
3554  CALL pushreal4(fpml)
3555  fpml = fpmx*xv + fpmy*yv
3556  CALL pushinteger4(1)
3557  ELSE
3558  CALL pushinteger4(0)
3559  END IF
3560  IF (k .EQ. ktop) THEN
3561  CALL pushreal4(fpmt)
3562  fpmt = fpmx*xv + fpmy*yv
3563  CALL pushinteger4(3)
3564  ELSE
3565  CALL pushinteger4(2)
3566  END IF
3567  ELSE
3568  CALL pushinteger4(1)
3569  END IF
3570  END DO
3571  DO k=ktop+1,pver
3572  IF (k .GE. kbot + 1) THEN
3573 ! Define layer pressure and density
3574  pm = (pi(i, k-1)+pi(i, k))*0.5
3575  CALL get_ti(t_new, t(i, k))
3576  CALL pushreal4(rhom)
3577  rhom = pm/(mapl_rgas*t_new)
3578  dusrcl = -((fpml-fpmt)/(rhom*zlb)*xv)
3579  dvsrcl = -((fpml-fpmt)/(rhom*zlb)*yv)
3580 ! Add sub-source wind and temperature tendencies
3581  dusrc(k) = dusrc(k) + dusrcl
3582  dvsrc(k) = dvsrc(k) + dvsrcl
3583  CALL pushinteger4(2)
3584  ELSE
3585  CALL pushinteger4(1)
3586  END IF
3587  END DO
3588  END DO
3589 ! For orographic waves, sub-source tendencies are set equal to zero.
3590  DO k=1,pver
3591  IF (ngwv .EQ. 0) THEN
3592  dusrc(k) = 0.0
3593  dvsrc(k) = 0.0
3594  CALL pushinteger4(2)
3595  ELSE
3596  CALL pushinteger4(1)
3597  END IF
3598  END DO
3599 !-----------------------------------------------------------------------
3600 ! Adjust efficiency factor to prevent unrealistically strong forcing
3601 !-----------------------------------------------------------------------
3602  uhtmax = 0.0
3603  utfac = 1.0
3604  DO k=1,pver
3605  x2 = sqrt(ut(k)**2 + vt(k)**2)
3606  IF (x2 .LT. uhtmax) THEN
3607  uhtmax = uhtmax
3608  CALL pushinteger4(2)
3609  ELSE
3610  uhtmax = x2
3611  CALL pushinteger4(1)
3612  END IF
3613  END DO
3614  IF (uhtmax .GT. tndmax) THEN
3615  utfac = tndmax/uhtmax
3616  CALL pushinteger4(1)
3617  ELSE
3618  CALL pushinteger4(0)
3619  END IF
3620  utfacb = 0.0_8
3621  DO k=pver,1,-1
3622  utfacb = utfacb + dusrc(k)*dusrcb(k) + ut(k)*utb(k) + vt(k)*vtb(k)&
3623 & + dvsrc(k)*dvsrcb(k)
3624  dvsrcb(k) = utfac*dvsrcb(k)
3625  dusrcb(k) = utfac*dusrcb(k)
3626  vtb(k) = utfac*vtb(k)
3627  utb(k) = utfac*utb(k)
3628  END DO
3629  CALL popinteger4(branch)
3630  IF (branch .LT. 1) THEN
3631  uhtmaxb = 0.0_8
3632  ELSE
3633  uhtmaxb = -(tndmax*utfacb/uhtmax**2)
3634  END IF
3635  DO k=pver,1,-1
3636  CALL popinteger4(branch)
3637  IF (branch .LT. 2) THEN
3638  x2b = uhtmaxb
3639  uhtmaxb = 0.0_8
3640  ELSE
3641  x2b = 0.0_8
3642  END IF
3643  IF (ut(k)**2 + vt(k)**2 .EQ. 0.0) THEN
3644  tempb4 = 0.0
3645  ELSE
3646  tempb4 = x2b/(2.0*sqrt(ut(k)**2+vt(k)**2))
3647  END IF
3648  utb(k) = utb(k) + 2*ut(k)*tempb4
3649  vtb(k) = vtb(k) + 2*vt(k)*tempb4
3650  END DO
3651  DO k=pver,1,-1
3652  CALL popinteger4(branch)
3653  IF (.NOT.branch .LT. 2) THEN
3654  dvsrcb(k) = 0.0_8
3655  dusrcb(k) = 0.0_8
3656  END IF
3657  END DO
3658  xvb = 0.0_8
3659  yvb = 0.0_8
3660  fpmlb = 0.0_8
3661  fpmtb = 0.0_8
3662  DO l=ngwv,-ngwv,-1
3663  DO k=pver,ktop+1,-1
3664  CALL popinteger4(branch)
3665  IF (.NOT.branch .LT. 2) THEN
3666  dvsrclb = dvsrcb(k)
3667  dusrclb = dusrcb(k)
3668  tempb2 = -(dvsrclb/(rhom*zlb))
3669  tempb3 = -(dusrclb/(rhom*zlb))
3670  fpmlb = fpmlb + xv*tempb3 + yv*tempb2
3671  fpmtb = fpmtb - xv*tempb3 - yv*tempb2
3672  yvb = yvb + (fpml-fpmt)*tempb2
3673  xvb = xvb + (fpml-fpmt)*tempb3
3674  CALL popreal4(rhom)
3675  END IF
3676  END DO
3677  DO k=pver,ktop,-1
3678  CALL popinteger4(branch)
3679  IF (branch .LT. 3) THEN
3680  IF (branch .LT. 2) THEN
3681  GOTO 100
3682  ELSE
3683  fpmxb = 0.0_8
3684  fpmyb = 0.0_8
3685  END IF
3686  ELSE
3687  CALL popreal4(fpmt)
3688  fpmxb = xv*fpmtb
3689  xvb = xvb + fpmx*fpmtb
3690  fpmyb = yv*fpmtb
3691  yvb = yvb + fpmy*fpmtb
3692  fpmtb = 0.0_8
3693  END IF
3694  CALL popinteger4(branch)
3695  IF (.NOT.branch .LT. 1) THEN
3696  CALL popreal4(fpml)
3697  fpmxb = fpmxb + xv*fpmlb
3698  xvb = xvb + fpmx*fpmlb
3699  fpmyb = fpmyb + yv*fpmlb
3700  yvb = yvb + fpmy*fpmlb
3701  fpmlb = 0.0_8
3702  END IF
3703  CALL popreal4(fpmy)
3704  tempb0 = cmu_*effgw(i)*fpmyb
3705  tempb1 = cmu_*effgw(i)*fpmxb
3706  taub(l, k) = taub(l, k) + xv*tempb1 + yv*tempb0
3707  yvb = yvb + tau(l, k)*tempb0
3708  CALL popreal4(fpmx)
3709  xvb = xvb + tau(l, k)*tempb1
3710  CALL popreal4(cmu_)
3711  100 CONTINUE
3712  END DO
3713  END DO
3714  DO k=pver,ktop+1,-1
3715  CALL popinteger4(branch)
3716  IF (branch .LT. 2) THEN
3717  ubtb = 0.0_8
3718  ELSE
3719  ubtb = effgw(i)*xv*utb(k) + effgw(i)*yv*vtb(k)
3720  yvb = yvb + effgw(i)*ubt*vtb(k)
3721  vtb(k) = 0.0_8
3722  xvb = xvb + effgw(i)*ubt*utb(k)
3723  utb(k) = 0.0_8
3724  END IF
3725  DO l=ngwv,-ngwv,-1
3726  CALL popinteger4(branch)
3727  IF (.NOT.branch .LT. 2) THEN
3728  utlb = ubtb
3729  ubtlb = sign(1.d0, ubtl*(c(l)-ubi(k)))*utlb
3730  CALL popreal4(ubtl)
3731  tempb = mapl_grav*rdpm(i, k)*ubtlb
3732  taub(l, k) = taub(l, k) + tempb
3733  taub(l, k-1) = taub(l, k-1) - tempb
3734  END IF
3735  END DO
3736  CALL popreal4(ubt)
3737  END DO
3738  DO l=ngwv,-ngwv,-1
3739  DO k=ktop,pver-1,1
3740  CALL popinteger4(branch)
3741  IF (.NOT.branch .LT. 2) THEN
3742  tau_minb = taub(l, k)
3743  taub(l, k) = 0.0_8
3744  CALL popinteger4(branch)
3745  IF (.NOT.branch .LT. 1) THEN
3746  taub(l, k+1) = taub(l, k+1) + tau_minb
3747  tau_minb = 0.0_8
3748  END IF
3749  tausatb = tau_minb
3750  CALL popinteger4(branch)
3751  IF (.NOT.branch .LT. 1) tausatb = 0.50*tausatb
3752  CALL popinteger4(branch)
3753  IF (.NOT.branch .LT. 1) tausatb = 0.20*tausatb
3754  CALL popinteger4(branch)
3755  IF (.NOT.branch .LT. 1) tausatb = 0.10*tausatb
3756  CALL popinteger4(branch)
3757  IF (.NOT.branch .LT. 1) tausatb = 0.05*tausatb
3758  CALL popinteger4(branch)
3759  IF (.NOT.branch .LT. 1) tausatb = 0.02*tausatb
3760  CALL popinteger4(branch)
3761  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
3762  CALL popinteger4(branch)
3763  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
3764  CALL popinteger4(branch)
3765  IF (.NOT.branch .LT. 1) tausatb = 0.0_8
3766  CALL popinteger4(branch)
3767  IF (branch .LT. 1) THEN
3768  x1b = -tausatb
3769  ELSE
3770  x1b = tausatb
3771  END IF
3772  ubmcb = effkwvmap*rhoi(k)*3*ubmc**2*x1b/(2.*ni(k))
3773  CALL popinteger4(branch)
3774  IF (branch .LT. 2) THEN
3775  IF (branch .LT. 1) THEN
3776  CALL popreal4(effkwvmap)
3777  ELSE
3778  CALL popreal4(effkwvmap)
3779  END IF
3780  ELSE
3781  CALL popreal4(effkwvmap)
3782  END IF
3783  CALL popreal4(ubmc)
3784  ubib(k) = ubib(k) + ubmcb
3785  END IF
3786  END DO
3787  END DO
3788  END SUBROUTINE gw_drag_prof_bgnd_b
3789  SUBROUTINE gw_drag_prof_bgnd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c&
3790 & , u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, &
3791 & dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut&
3792 & , vt, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, &
3793 & tau0y, effgw)
3794  IMPLICIT NONE
3795 !-----------------------------------------------------------------------
3796 ! Solve for the drag profile from the multiple gravity wave drag
3797 ! parameterization.
3798 ! 1. scan up from the wave source to determine the stress profile
3799 ! 2. scan down the stress profile to determine the tendencies
3800 ! => apply bounds to the tendency
3801 ! a. from wkb solution
3802 ! b. from computational stability constraints
3803 ! => adjust stress on interface below to reflect actual bounded tendency
3804 !-----------------------------------------------------------------------
3805 !------------------------------Arguments--------------------------------
3806 ! number of current column
3807  INTEGER, INTENT(IN) :: i
3808 ! number of atmospheric columns
3809  INTEGER, INTENT(IN) :: pcols
3810 ! number of atmospheric columns
3811  INTEGER, INTENT(IN) :: pver
3812 ! index of bottom (source) interface
3813  INTEGER, INTENT(IN) :: kbot
3814 ! index of top interface of gwd region
3815  INTEGER, INTENT(IN) :: ktop
3816 ! number of gravity waves possible
3817  INTEGER, INTENT(IN) :: pgwv
3818 ! number of gravity waves to use
3819  INTEGER, INTENT(IN) :: ngwv
3820 ! top interface of low level stress divergence region
3821  INTEGER, INTENT(IN) :: kldv
3822 ! min value of kldv
3823  INTEGER, INTENT(IN) :: kldvmn
3824 ! index of top interface of source region
3825  INTEGER, INTENT(IN) :: ksrc
3826 ! min value of ksrc
3827  INTEGER, INTENT(IN) :: ksrcmn
3828 ! wave phase speeds
3829  REAL :: c(-pgwv:pgwv)
3830 ! midpoint zonal wind
3831  REAL :: u(pcols, pver)
3832 ! midpoint meridional wind
3833  REAL :: v(pcols, pver)
3834 ! midpoint temperatures
3835  REAL :: t(pcols, pver)
3836 ! interface pressures
3837  REAL :: pi(pcols, 0:pver)
3838 ! midpoint delta p (pi(k)-pi(k-1))
3839  REAL :: dpm(pcols, pver)
3840 ! 1. / (pi(k)-pi(k-1))
3841  REAL :: rdpm(pcols, pver)
3842 ! ln(interface pressures)
3843  REAL :: piln(pcols, 0:pver)
3844  REAL :: rlat(pcols)
3845 ! interface density
3846  REAL :: rhoi(0:pver)
3847 ! interface Brunt-Vaisalla frequency
3848  REAL :: ni(0:pver)
3849 ! interface temperature
3850  REAL :: ti(0:pver)
3851 ! midpoint Brunt-Vaisalla frequency
3852  REAL :: nm(pver)
3853 ! time step
3854  REAL :: dt
3855 ! newtonian cooling coefficients
3856  REAL :: alpha(0:pver)
3857 ! newtonian cooling coefficients
3858  REAL :: dback(0:pver)
3859 ! 1/dp across low level divergence region
3860  REAL :: rdpldv
3861 ! projection of wind at interfaces
3862  REAL :: ubi(0:pver)
3863 ! projection of wind at midpoints
3864  REAL :: ubm(pver)
3865 ! unit vectors of source wind (x)
3866  REAL :: xv
3867 ! unit vectors of source wind (y)
3868  REAL :: yv
3869 ! tendency efficiency for gwd
3870  REAL :: effgw(pcols)
3871 ! wave Reynolds stress
3872  REAL :: tau(-pgwv:pgwv, 0:pver)
3873 ! zonal wind tendency
3874  REAL :: ut(pver)
3875 ! meridional wind tendency
3876  REAL :: vt(pver)
3877 ! temperature tendency
3878  REAL :: tt(pver)
3879 ! Total zonal GW momentum flux
3880  REAL :: taugwx(pcols, 0:pver)
3881 ! Total meridional GW momentum flux
3882  REAL :: taugwy(pcols, 0:pver)
3883 ! Total GW energy flux
3884  REAL :: fegw(pcols, 0:pver)
3885 ! Total GW pseudo energy flux
3886  REAL :: fepgw(pcols, 0:pver)
3887 ! Total U tendency below launch level
3888  REAL :: dusrc(pver)
3889 ! Total V tendency below launch level
3890  REAL :: dvsrc(pver)
3891 ! Total V tendency below launch level
3892  REAL :: dtsrc(pver)
3893 ! c=0 sfc. stress (zonal)
3894  REAL :: tau0x
3895 ! c=0 sfc. stress (meridional)
3896  REAL :: tau0y
3897 !---------------------------Local storage-------------------------------
3898 ! loop indexes
3899  INTEGER :: k, l
3900 ! fraction of dsat to use
3901  REAL :: dscal
3902 ! imaginary part of vertical wavenumber
3903  REAL :: mi
3904 ! stress after damping
3905  REAL :: taudmp
3906 !real :: taumax(pcols) ! max(tau) for any l
3907 ! saturation stress
3908  REAL :: tausat
3909 ! (ub-c)
3910  REAL :: ubmc
3911 ! (ub-c)**2
3912  REAL :: ubmc2
3913 ! ubar tendency
3914  REAL :: ubt
3915 ! tbar tendency
3916  REAL :: tbt
3917 ! ubar tendency from wave l
3918  REAL :: ubtl
3919 ! saturation tendency
3920  REAL :: ubtlsat
3921 ! layer pressure
3922  REAL :: pm
3923 ! layer density
3924  REAL :: rhom
3925 ! launch level height
3926  REAL :: zlb
3927 ! c-u
3928  REAL :: cmu
3929  REAL :: dzm, hscal, tautmp
3930  REAL :: utl
3931  REAL :: ttl
3932 ! zonal pseudomomentum flux spectrum
3933  REAL :: fpmx
3934 ! meridional pseudomomentum flux spectrum
3935  REAL :: fpmy
3936 ! energy flux (p'w') spectrum
3937  REAL :: fe
3938 ! pseudoenergy flux (p'w'+U rho u'w') spectrum
3939  REAL :: fpe
3940  REAL :: fpml
3941  REAL :: fpmt
3942  REAL :: fpel
3943  REAL :: fpet
3944  REAL :: dusrcl
3945  REAL :: dvsrcl
3946  REAL :: dtsrcl
3947  REAL :: zi
3948  REAL :: effkwvmap
3949  REAL :: zfac
3950  REAL :: uhtmax
3951  REAL :: utfac
3952  REAL :: tau_min, cmu_, t_new
3953  INTRINSIC max
3954  INTRINSIC sign
3955  INTRINSIC abs
3956  REAL :: x2
3957  REAL :: x1
3958  INTRINSIC sqrt
3959 ! Initialize gravity wave drag tendencies to zero
3960  DO l=-ngwv,ngwv
3961  DO k=pver-1,ktop,-1
3962  IF (k .LE. kbot - 1) tau(l, k) = 0.
3963  END DO
3964  END DO
3965  DO k=1,pver
3966  ut(k) = 0.
3967  vt(k) = 0.
3968  tt(k) = 0.
3969  dusrc(k) = 0.
3970  dvsrc(k) = 0.
3971  dtsrc(k) = 0.
3972  END DO
3973 ! Initialize total momentum and energy fluxes to zero
3974  DO k=0,pver
3975  taugwx(i, k) = 0.
3976  taugwy(i, k) = 0.
3977  fegw(i, k) = 0.
3978  fepgw(i, k) = 0.
3979  END DO
3980 ! Initialize surface wave stress at c = 0 to zero
3981  tau0x = 0.
3982  tau0y = 0.
3983 !---------------------------------------------------------------------------
3984 ! Compute the stress profiles and diffusivities
3985 !---------------------------------------------------------------------------
3986 ! Determine the absolute value of the saturation stress and the diffusivity
3987 ! for each wave.
3988 ! Define critical levels where the sign of (u-c) changes between interfaces.
3989 ! Loop from bottom to top to get stress profiles
3990  DO l=-ngwv,ngwv
3991  DO k=pver-1,ktop,-1
3992  IF (k .LE. kbot - 1) THEN
3993 !!!!!!!!jk d = dback(k)
3994  ubmc = ubi(k) - c(l)
3995  IF (ngwv .GT. 0) THEN
3996  CALL get_effkwvmap_1(effkwvmap, rlat(i))
3997  IF (-15.0 .LT. rlat(i)*180./pi_gwd .AND. rlat(i)*180./pi_gwd&
3998 & .LT. 15.0) effkwvmap = fcrit2*kwvbeq
3999  ELSE
4000  IF (pi(i, k) .LT. 1000.0) THEN
4001  zfac = (pi(i, k)/1000.0)**3
4002  ELSE
4003  zfac = 1.0
4004  END IF
4005  IF (rlat(i)*180./pi_gwd .LT. -20.0) THEN
4006  CALL get_effkwvmap_2(effkwvmap, rlat(i), zfac)
4007  ELSE
4008  CALL get_effkwvmap_3(effkwvmap, rlat(i), zfac)
4009  END IF
4010  END IF
4011  x1 = effkwvmap*rhoi(k)*ubmc**3/(2.*ni(k))
4012  IF (x1 .GE. 0.) THEN
4013  tausat = x1
4014  ELSE
4015  tausat = -x1
4016  END IF
4017  IF (tausat .LE. taumin) tausat = 0.0
4018  IF (ubmc*(ubi(k+1)-c(l)) .LE. 0.0) tausat = 0.0
4019  IF (k .EQ. ktop) tausat = 0.
4020 !
4021  IF (k .EQ. ktop + 1) tausat = tausat*0.02
4022  IF (k .EQ. ktop + 2) tausat = tausat*0.05
4023  IF (k .EQ. ktop + 3) tausat = tausat*0.10
4024  IF (k .EQ. ktop + 4) tausat = tausat*0.20
4025  IF (k .EQ. ktop + 5) tausat = tausat*0.50
4026 !
4027  tau_min = tausat
4028  IF (tau_min .GT. tau(l, k+1)) tau_min = tau(l, k+1)
4029  tau(l, k) = tau_min
4030  END IF
4031  END DO
4032  END DO
4033 !---------------------------------------------------------------------------
4034 ! Compute the tendencies from the stress divergence.
4035 !---------------------------------------------------------------------------
4036 ! Accumulate the mean wind tendency over wavenumber.
4037 ! Loop over levels from top to bottom
4038  DO k=ktop+1,pver
4039  ubt = 0.0
4040  tbt = 0.0
4041  DO l=-ngwv,ngwv
4042  IF (k .LE. kbot) THEN
4043 ! Determine the wind tendency including excess stress carried down from above.
4044  ubtl = mapl_grav*(tau(l, k)-tau(l, k-1))*rdpm(i, k)
4045 ! Calculate the sign of wind tendency
4046  utl = sign(ubtl, c(l) - ubi(k))
4047 ! Accumulate the mean wind tendency over wavenumber.
4048  ubt = ubt + utl
4049 ! Calculate irreversible temperature tendency associated with gravity wave breaking.
4050 ! Adding frictional heating associated with the GW momentum forcing
4051  ttl = (c(l)-ubm(k))*utl/mapl_cp
4052 ! Adding frictional heating associated with the GW momentum forcing
4053  tbt = tbt + ttl
4054  END IF
4055  END DO
4056 ! Project the mean wind tendency onto the components and scale by "efficiency".
4057  IF (k .LE. kbot) THEN
4058  ut(k) = ubt*xv*effgw(i)
4059  vt(k) = ubt*yv*effgw(i)
4060  tt(k) = tbt*effgw(i)
4061  END IF
4062  END DO
4063 !-----------------------------------------------------------------------
4064 ! Calculates wind and temperature tendencies below launch level for
4065 ! energy and momentum conservation (does nothing for orographic GWs).
4066 !-----------------------------------------------------------------------
4067 !----kimmmmm here
4068 ! Calculate launch level height
4069  zlb = 0.
4070  DO k=ktop+1,pver
4071  IF (k .GE. kbot + 1) THEN
4072 ! Define layer pressure and density
4073  pm = (pi(i, k-1)+pi(i, k))*0.5
4074  CALL get_ti(t_new, t(i, k))
4075  rhom = pm/(mapl_rgas*t_new)
4076  zlb = zlb + dpm(i, k)/mapl_grav/rhom
4077  END IF
4078  END DO
4079 !-----------------------------------------------------------------------
4080 ! Calculates energy and momentum flux profiles
4081 !-----------------------------------------------------------------------
4082  DO l=-ngwv,ngwv
4083  DO k=ktop,pver
4084  IF (k .LE. kbot) THEN
4085  cmu = c(l) - ubi(k)
4086  CALL get_cmu(cmu_, cmu)
4087  fpmx = cmu_*tau(l, k)*xv*effgw(i)
4088  fpmy = cmu_*tau(l, k)*yv*effgw(i)
4089  fe = cmu*cmu_*tau(l, k)*effgw(i)
4090  fpe = c(l)*cmu_*tau(l, k)*effgw(i)
4091  IF (k .EQ. kbot) THEN
4092  fpml = fpmx*xv + fpmy*yv
4093  fpel = fpe
4094  END IF
4095  IF (k .EQ. ktop) THEN
4096  fpmt = fpmx*xv + fpmy*yv
4097  fpet = fpe
4098  END IF
4099 ! Record outputs for GW fluxes
4100  taugwx(i, k) = taugwx(i, k) + fpmx
4101  taugwy(i, k) = taugwy(i, k) + fpmy
4102  fegw(i, k) = fegw(i, k) + fe
4103  fepgw(i, k) = fepgw(i, k) + fpe
4104  END IF
4105  END DO
4106  DO k=ktop+1,pver
4107  IF (k .GE. kbot + 1) THEN
4108 ! Define layer pressure and density
4109  pm = (pi(i, k-1)+pi(i, k))*0.5
4110  CALL get_ti(t_new, t(i, k))
4111  rhom = pm/(mapl_rgas*t_new)
4112  dusrcl = -((fpml-fpmt)/(rhom*zlb)*xv)
4113  dvsrcl = -((fpml-fpmt)/(rhom*zlb)*yv)
4114  dtsrcl = -((fpel-fpet-ubm(k)*(fpml-fpmt))/(rhom*zlb*mapl_cp))
4115 ! Add sub-source wind and temperature tendencies
4116  dusrc(k) = dusrc(k) + dusrcl
4117  dvsrc(k) = dvsrc(k) + dvsrcl
4118  dtsrc(k) = dtsrc(k) + dtsrcl
4119  END IF
4120  END DO
4121  END DO
4122 ! For orographic waves, sub-source tendencies are set equal to zero.
4123  DO k=1,pver
4124  IF (ngwv .EQ. 0) THEN
4125  dusrc(k) = 0.0
4126  dvsrc(k) = 0.0
4127  dtsrc(k) = 0.0
4128  END IF
4129  END DO
4130 !-----------------------------------------------------------------------
4131 ! Adjust efficiency factor to prevent unrealistically strong forcing
4132 !-----------------------------------------------------------------------
4133  uhtmax = 0.0
4134  utfac = 1.0
4135  DO k=1,pver
4136  x2 = sqrt(ut(k)**2 + vt(k)**2)
4137  IF (x2 .LT. uhtmax) THEN
4138  uhtmax = uhtmax
4139  ELSE
4140  uhtmax = x2
4141  END IF
4142  END DO
4143  IF (uhtmax .GT. tndmax) utfac = tndmax/uhtmax
4144 !jkim effgw(i) = effgw(i)*utfac
4145  DO k=1,pver
4146  ut(k) = ut(k)*utfac
4147  vt(k) = vt(k)*utfac
4148  tt(k) = tt(k)*utfac
4149  dusrc(k) = dusrc(k)*utfac
4150  dvsrc(k) = dvsrc(k)*utfac
4151  dtsrc(k) = dtsrc(k)*utfac
4152  END DO
4153 !-----------------------------------------------------------------------
4154 ! Project the c=0 stress (scaled) in the direction of the source wind
4155 ! for recording on the output file.
4156 !-----------------------------------------------------------------------
4157  tau0x = tau(0, kbot)*xv*effgw(i)*utfac
4158  tau0y = tau(0, kbot)*yv*effgw(i)*utfac
4159  RETURN
4160  END SUBROUTINE gw_drag_prof_bgnd
4161 ! Differentiation of get_uv in reverse (adjoint) mode:
4162 ! gradient, with respect to input variables: uv_in
4163 ! of linear combination of output variables: uv_out uv_in
4164  SUBROUTINE get_uv_b(uv_out, uv_outb, uv_in, uv_inb)
4165  IMPLICIT NONE
4166  REAL :: uv_out, uv_in
4167  REAL :: uv_outb, uv_inb
4168  LOGICAL :: src_bypass
4169  IF (src_bypass) THEN
4170  uv_inb = uv_inb + uv_outb
4171  ELSE
4172  uv_inb = uv_inb + uv_outb
4173  END IF
4174  END SUBROUTINE get_uv_b
4175  SUBROUTINE get_uv(uv_out, uv_in)
4176  IMPLICIT NONE
4177  REAL :: uv_out, uv_in
4178  LOGICAL :: src_bypass
4179  IF (src_bypass) THEN
4180  uv_out = uv_in
4181  ELSE
4182  uv_out = uv_in
4183  END IF
4184  END SUBROUTINE get_uv
4185  SUBROUTINE get_effkwvmap_1(effkwvmap_, rlat_)
4186  IMPLICIT NONE
4187  REAL :: tanh1, tanh2, effkwvmap_, rlat_
4188  INTRINSIC tanh
4189  tanh1 = (rlat_*180./pi_gwd-20.0)/6.0
4190  tanh2 = -((rlat_*180./pi_gwd+20.0)/6.0)
4191  tanh1 = tanh(tanh1)
4192  tanh2 = tanh(tanh2)
4193  effkwvmap_ = fcrit2*kwvb*(0.5*(1.0+tanh1)+0.5*(1.0+tanh2))
4194  END SUBROUTINE get_effkwvmap_1
4195  SUBROUTINE get_effkwvmap_2(effkwvmap_, rlat_, zfac)
4196  IMPLICIT NONE
4197  REAL :: effkwvmap_, rlat_, zfac
4198  INTRINSIC exp
4199  INTRINSIC abs
4200  REAL :: abs1
4201  IF (rlat_*180./pi_gwd + 20. .GE. 0.) THEN
4202  abs1 = rlat_*180./pi_gwd + 20.
4203  ELSE
4204  abs1 = -(rlat_*180./pi_gwd+20.)
4205  END IF
4206  effkwvmap_ = fcrit2*kwvo*zfac*(2.0-0.65*exp(-(abs1/5.)))
4207  END SUBROUTINE get_effkwvmap_2
4208  SUBROUTINE get_effkwvmap_3(effkwvmap_, rlat_, zfac)
4209  IMPLICIT NONE
4210  REAL :: effkwvmap_, rlat_, zfac
4211  INTRINSIC exp
4212  INTRINSIC abs
4213  REAL :: abs1
4214  IF (rlat_*180./pi_gwd + 20. .GE. 0.) THEN
4215  abs1 = rlat_*180./pi_gwd + 20.
4216  ELSE
4217  abs1 = -(rlat_*180./pi_gwd+20.)
4218  END IF
4219  effkwvmap_ = fcrit2*kwvo*zfac*(0.7+0.65*exp(-(abs1/5.)))
4220  END SUBROUTINE get_effkwvmap_3
4221  SUBROUTINE get_cmu(cmu_, cmu)
4222  IMPLICIT NONE
4223  REAL :: cmu_, cmu
4224  INTRINSIC sign
4225  cmu_ = sign(1.0, cmu)
4226  END SUBROUTINE get_cmu
4227  SUBROUTINE get_ti(t_out, t_in)
4228  IMPLICIT NONE
4229  REAL :: t_out, t_in
4230  LOGICAL :: src_bypass
4231  t_out = t_in
4232  END SUBROUTINE get_ti
4233 END MODULE gw_drag_b
subroutine gw_drag_prof_bwd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, ubm, xv, xvb, yv, yvb, ut, utb, vt, vtb, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y, effgw)
Definition: gw_drag_b.F90:2557
integer, public isize_s1_tau
subroutine popinteger4(x)
Definition: adBuffer.f:541
logical, public do_oro
subroutine get_cmu(cmu_, cmu)
Definition: gw_drag_b.F90:4222
real, parameter, public oroko2
Definition: gw_drag_b.F90:76
subroutine gw_drag_prof_bgnd_b(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, ubm, xv, xvb, yv, yvb, ut, utb, vt, vtb, tt, taugwx, taugwy, fegw, fepgw, dusrc, dusrcb, dvsrc, dvsrcb, dtsrc, tau0x, tau0y, effgw)
Definition: gw_drag_b.F90:3172
subroutine get_effkwvmap_2(effkwvmap_, rlat_, zfac)
Definition: gw_drag_b.F90:4196
real *8, parameter, public p_src
real, parameter, public umcfac
Definition: gw_drag_b.F90:69
subroutine gw_bgnd_fwd(i, pcols, pver, c, u, v, t, pm, pi, dpm, rdpm, piln, rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ngwv, kbot)
Definition: gw_drag_b.F90:1642
real, parameter, public fcrit2
Definition: gw_drag_b.F90:55
real, parameter, public orohmin
Definition: gw_drag_b.F90:57
subroutine gw_oro_bwd(i, pcols, pver, pgwv, u, ub, v, vb, t, sgh, pm, pi, dpm, zm, nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, taub, ubi, ubib, ubm, ubmb, xv, xvb, yv, yvb, kbot, rlat)
Definition: gw_drag_b.F90:1299
subroutine gw_oro_fwd(i, pcols, pver, pgwv, u, v, t, sgh, pm, pi, dpm, zm, nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, kbot, rlat)
Definition: gw_drag_b.F90:1088
subroutine gw_bgnd(i, pcols, pver, c, u, v, t, pm, pi, dpm, rdpm, piln, rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ngwv, kbot)
Definition: gw_drag_b.F90:1955
real, parameter, public mapl_cp
real, parameter, public mapl_vireps
real, parameter, public rog
Definition: gw_drag_b.F90:74
real, parameter, public kwvo
Definition: gw_drag_b.F90:45
real, parameter, public taubgnd
Definition: gw_drag_b.F90:61
subroutine gw_drag_prof_bgnd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut, vt, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y, effgw)
Definition: gw_drag_b.F90:3794
integer, public isize_e1_tau
subroutine get_uv_b(uv_out, uv_outb, uv_in, uv_inb)
Definition: gw_drag_b.F90:4165
real, parameter, public fracldv
Definition: gw_drag_b.F90:47
subroutine, public gw_main_b(pcols, pver, dt, pgwv, effgworo_dev, effgwbkg_dev, pint_dev, t_dev, u_dev, u_devb, v_dev, v_devb, sgh_dev, pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, qvt_dev, rog, mapl_vireps_, rlat_dev)
Definition: gw_drag_b.F90:87
subroutine gw_prof(i, k, pcols, pver, u, v, t, pm, pi, rhoi, ni, ti, nm)
Definition: gw_drag_b.F90:999
subroutine get_uv(uv_out, uv_in)
Definition: gw_drag_b.F90:4176
real, parameter, public mapl_kappa
void pushreal4array(float *x, int n)
Definition: adStack.c:362
real, parameter, public taumin
Definition: gw_drag_b.F90:63
real, parameter, public mapl_p00
integer, public isize_s2_tau
real, parameter, public tauscal
Definition: gw_drag_b.F90:65
subroutine get_ti(t_out, t_in)
Definition: gw_drag_b.F90:4228
real, parameter, public mxrange
Definition: gw_drag_b.F90:51
subroutine get_effkwvmap_3(effkwvmap_, rlat_, zfac)
Definition: gw_drag_b.F90:4209
subroutine gw_drag_prof_fwd(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut, vt, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y, effgw)
Definition: gw_drag_b.F90:2161
subroutine gw_oro(i, pcols, pver, pgwv, u, v, t, sgh, pm, pi, dpm, zm, nm, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, kbot, rlat)
Definition: gw_drag_b.F90:1436
logical, public do_bgnd
subroutine pushreal4(x)
Definition: adBuffer.f:670
real, parameter, public orovmin
Definition: gw_drag_b.F90:59
real, parameter, public kwvbeq
Definition: gw_drag_b.F90:43
subroutine gw_intr_fwd(i, pcols, pver, dt, pgwv, effgworo_dev, effgwbkg_dev, pint_dev, t_dev, u_dev, v_dev, sgh_dev, pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, rlat_dev, dudt_gwd_dev, dvdt_gwd_dev, dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
Definition: gw_drag_b.F90:338
real, parameter, public n2min
Definition: gw_drag_b.F90:53
integer, public isize_e2_tau
#define max(a, b)
Definition: mosaic_util.h:33
subroutine gw_intr(i, pcols, pver, dt, pgwv, effgworo_dev, effgwbkg_dev, pint_dev, t_dev, u_dev, v_dev, sgh_dev, pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, rlat_dev, dudt_gwd_dev, dvdt_gwd_dev, dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
Definition: gw_drag_b.F90:768
real, parameter, public mapl_rgas
real, parameter, public ubmc2mn
Definition: gw_drag_b.F90:71
logical, parameter, public bypass_bgnd
real, parameter, public mapl_grav
subroutine gw_drag_prof(i, pcols, pver, pgwv, ngwv, kbot, ktop, c, u, v, t, pi, dpm, rdpm, piln, rlat, rhoi, ni, ti, nm, dt, alpha, dback, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubm, xv, yv, ut, vt, tt, taugwx, taugwy, fegw, fepgw, dusrc, dvsrc, dtsrc, tau0x, tau0y, effgw)
Definition: gw_drag_b.F90:2795
real, parameter, public mxasym
Definition: gw_drag_b.F90:49
subroutine gw_bgnd_bwd(i, pcols, pver, c, u, ub, v, vb, t, pm, pi, dpm, rdpm, piln, rlat, kldv, kldvmn, ksrc, ksrcmn, rdpldv, tau, ubi, ubib, ubm, ubmb, xv, xvb, yv, yvb, ngwv, kbot)
Definition: gw_drag_b.F90:1847
real, parameter, public tndmax
Definition: gw_drag_b.F90:67
#define min(a, b)
Definition: mosaic_util.h:32
logical, parameter, public bypass_oro
real, parameter, public zldvcon
Definition: gw_drag_b.F90:73
subroutine gw_intr_bwd(i, pcols, pver, dt, pgwv, effgworo_dev, effgwbkg_dev, pint_dev, t_dev, u_dev, u_devb, v_dev, v_devb, sgh_dev, pref_dev, pmid_dev, pdel_dev, rpdel_dev, lnpint_dev, zm_dev, rlat_dev, dudt_gwd_dev, dudt_gwd_devb, dvdt_gwd_dev, dvdt_gwd_devb, dtdt_gwd_dev, dudt_org_dev, dvdt_org_dev, dtdt_org_dev, taugwdx_dev, taugwdy_dev, tauox_dev, tauoy_dev, feo_dev, taubkgx_dev, taubkgy_dev, taubx_dev, tauby_dev, feb_dev, fepo_dev, fepb_dev, utbsrc_dev, vtbsrc_dev, ttbsrc_dev)
Definition: gw_drag_b.F90:595
real, parameter, public kwvb
Definition: gw_drag_b.F90:41
void popreal4array(float *x, int n)
Definition: adStack.c:365
real, parameter, public pi_gwd
Definition: gw_drag_b.F90:78
subroutine pushinteger4(x)
Definition: adBuffer.f:484
subroutine get_effkwvmap_1(effkwvmap_, rlat_)
Definition: gw_drag_b.F90:4186
subroutine popreal4(x)
Definition: adBuffer.f:727