mpas_ocn_gm.f90 104 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
!KGEN-generated Fortran source file 
  
!Generated at : 2020-04-03 14:07:28 
!KGEN version : 0.9.0 
  
! Copyright (c) 2013,  Los Alamos National Security, LLC (LANS)
! and the University Corporation for Atmospheric Research (UCAR).
! Unless noted otherwise source code is licensed under the BSD license.
! Additional copyright and license information can be found in the LICENSE file
! distributed with this code, or at http://mpas-dev.github.com/license.html


!
!

module ocn_gm

    USE mpas_constants 

    USE ocn_constants 
    USE kgen_utils_mod, ONLY: kgen_dp, kgen_array_sumcheck 
    USE tprof_mod, ONLY: tstart, tstop, tnull, tprnt 

    IMPLICIT NONE 
    PRIVATE 
    SAVE 
   !--------------------------------------------------------------------
   ! Public parameters
   !--------------------------------------------------------------------
   !--------------------------------------------------------------------
   ! Public member functions
   !--------------------------------------------------------------------

   !
   !

   !
   !

    PUBLIC ocn_gm_compute_bolus_velocity 
   !--------------------------------------------------------------------
   ! Private module variables
   !--------------------------------------------------------------------

   !
   !
    PRIVATE tridiagonal_solve 
   ! Config options

   real (kind=RKIND), pointer :: config_gravWaveSpeed_trunc
   real (kind=RKIND), pointer :: config_max_relative_slope
   logical, pointer :: config_disable_redi_k33
   logical, pointer :: config_use_Redi_surface_layer_tapering
   logical, pointer :: config_use_Redi_bottom_layer_tapering
   real (kind=RKIND), pointer :: config_Redi_surface_layer_tapering_extent
   real (kind=RKIND), pointer :: config_Redi_bottom_layer_tapering_depth
   logical, pointer :: config_gm_lat_variable_c2
   logical, pointer :: config_gm_kappa_lat_depth_variable
   real (kind=RKIND), pointer :: config_gm_min_stratification_ratio
   real (kind=RKIND), pointer :: config_gm_min_phase_speed
   real (kind=RKIND), parameter :: epsGM = 1.0e-12_RKIND
!***********************************************************************
#ifdef _MPI 
   include "mpif.h" 
#endif 
     
   PUBLIC kr_externs_in_ocn_gm 
   PUBLIC kr_externs_out_ocn_gm 


contains
!***********************************************************************
!  routine ocn_gm_compute_Bolus_velocity
!> \brief   Computes GM Bolus velocity
!> \author  Qingshan Chen, Mark Petersen, Todd Ringler
!> \date    January 2013
!> \details
!>  This routine is the main driver for the Gent-McWilliams (GM) parameterization.
!>  It computes horizontal and vertical density gradients, the slope
!>  of isopycnal surfaces, and solves a boundary value problem in each column
!>  for the stream function, which is used to compute the Bolus velocity.
!-----------------------------------------------------------------------

!
!
!

SUBROUTINE ocn_gm_compute_bolus_velocity(kgen_unit, kgen_measure, kgen_isverified, kgen_filepath) 
      !-----------------------------------------------------------------
      ! input variables
      !-----------------------------------------------------------------
    USE kgen_utils_mod, ONLY: kgen_dp, kgen_array_sumcheck 
    USE kgen_utils_mod, ONLY: kgen_perturb_real 
    USE kgen_utils_mod, ONLY: check_t, kgen_init_check, kgen_init_verify, kgen_tolerance, kgen_minvalue, kgen_verboselevel, &
    &CHECK_IDENTICAL, CHECK_IN_TOL, CHECK_OUT_TOL 

      !
      !

      !-----------------------------------------------------------------
      ! input/output variables
      !-----------------------------------------------------------------

      !
      !

      !-----------------------------------------------------------------
      ! local variables
      !-----------------------------------------------------------------

      !
      !

    REAL(KIND=rkind), dimension(:,:), pointer :: density, displaceddensity, zmid, normalgmbolusvelocity, layerthicknessedge, &
    &graddensityedge, graddensitytopofedge, graddensityconstztopofedge, gradzmidedge, gradzmidtopofedge, relativeslopetopofedge, &
    &relativeslopetopofcell, k33, gmstreamfunctopofedge, bruntvaisalafreqtop, gmstreamfunctopofcell, ddensitydztopofedge, &
    &ddensitydztopofcell, relativeslopetapering, relativeslopetaperingcell, areacellsum, kappagm3d 

    REAL(KIND=rkind), dimension(:), pointer :: boundarylayerdepth, gmboluskappa, cgmphasespeed 
    REAL(KIND=rkind), dimension(:), pointer :: areacell, dcedge, dvedge, tridiaga, tridiagb, tridiagc, righthandside 
    INTEGER, dimension(:), pointer :: maxleveledgetop, maxlevelcell, nedgesoncell 
    INTEGER, dimension(:,:), pointer :: cellsonedge, edgesoncell 
    INTEGER :: i, k, iedge, cell1, cell2, icell, n, iter 
    REAL(KIND=rkind) :: h1, h2, areaedge, bruntvaisalafreqtopedge, rtmp, stmp 
    REAL(KIND=rkind) :: sumn2, countn2, maxn, ltsum 
      ! Dimensions
      
    INTEGER :: ncells, nedges 
    INTEGER, pointer :: nvertlevels 
    INTEGER, dimension(:), pointer :: ncellsarray, nedgesarray 

    INTEGER, INTENT(IN) :: kgen_unit 
    REAL(KIND=kgen_dp), INTENT(OUT) :: kgen_measure 
    LOGICAL, INTENT(OUT) :: kgen_isverified 
    CHARACTER(LEN=*), INTENT(IN) :: kgen_filepath 
    LOGICAL :: kgen_istrue 
    REAL(KIND=8) :: kgen_array_sum 
    INTEGER :: kgen_intvar, kgen_ierr 
    INTEGER :: kgen_mpirank, kgen_openmptid, kgen_kernelinvoke 
    LOGICAL :: kgen_evalstage, kgen_warmupstage, kgen_mainstage 
    COMMON / state / kgen_mpirank, kgen_openmptid, kgen_kernelinvoke, kgen_evalstage, kgen_warmupstage, kgen_mainstage 
    INTEGER, PARAMETER :: KGEN_MAXITER = NUM_REPEAT
      
    TYPE(check_t) :: check_status 
    INTEGER*8 :: kgen_start_clock, kgen_stop_clock, kgen_rate_clock 
    REAL(KIND=kgen_dp) :: gkgen_measure 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_graddensityedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_gradzmidedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_normalgmbolusvelocity 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_graddensitytopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_ddensitydztopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_gradzmidtopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_relativeslopetopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_relativeslopetapering 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_ddensitydztopofcell 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_k33 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_relativeslopetopofcell 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_relativeslopetaperingcell 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_graddensityconstztopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_areacellsum 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_kappagm3d 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_gmstreamfunctopofedge 
    REAL(KIND=rkind), pointer, dimension(:,:) :: kgenref_gmstreamfunctopofcell 
    REAL(KIND=rkind), pointer, dimension(:) :: kgenref_cgmphasespeed 
    REAL(KIND=rkind), pointer, dimension(:) :: kgenref_tridiagb 
    REAL(KIND=rkind), pointer, dimension(:) :: kgenref_tridiagc 
    REAL(KIND=rkind), pointer, dimension(:) :: kgenref_righthandside 
    REAL(KIND=rkind), pointer, dimension(:) :: kgenref_tridiaga 
    INTEGER, pointer, dimension(:) :: kgenref_maxleveledgetop 
    INTEGER :: kgenref_iedge 
    INTEGER :: kgenref_k 
    INTEGER :: kgenref_icell 
    INTEGER :: kgenref_cell1 
    INTEGER :: kgenref_cell2 
    INTEGER :: kgenref_i 
    INTEGER :: kgenref_iter 
    INTEGER :: kgenref_n 
    REAL(KIND=rkind) :: kgenref_rtmp 
    REAL(KIND=rkind) :: kgenref_h1 
    REAL(KIND=rkind) :: kgenref_h2 
    REAL(KIND=rkind) :: kgenref_areaedge 
    REAL(KIND=rkind) :: kgenref_stmp 
    REAL(KIND=rkind) :: kgenref_bruntvaisalafreqtopedge 
    REAL(KIND=rkind) :: kgenref_sumn2 
    REAL(KIND=rkind) :: kgenref_ltsum 
    REAL(KIND=rkind) :: kgenref_countn2 
    REAL(KIND=rkind) :: kgenref_maxn 
    INTEGER :: kgenref_ncells 
    INTEGER :: kgenref_nedges 
      
    !parent block preprocessing 
      
#ifdef _MPI 
    call mpi_comm_rank(mpi_comm_world, kgen_mpirank, kgen_ierr) 
#else 
    kgen_mpirank = 0 
#endif 
      
      
    !local input variables 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(displaceddensity, kgen_unit, "displaceddensity", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(density, kgen_unit, "density", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(zmid, kgen_unit, "zmid", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(ddensitydztopofcell, kgen_unit, "ddensitydztopofcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(layerthicknessedge, kgen_unit, "layerthicknessedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(graddensityedge, kgen_unit, "graddensityedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(gradzmidedge, kgen_unit, "gradzmidedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(graddensitytopofedge, kgen_unit, "graddensitytopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(ddensitydztopofedge, kgen_unit, "ddensitydztopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(gradzmidtopofedge, kgen_unit, "gradzmidtopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(relativeslopetopofedge, kgen_unit, "relativeslopetopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(relativeslopetopofcell, kgen_unit, "relativeslopetopofcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(areacellsum, kgen_unit, "areacellsum", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(relativeslopetaperingcell, kgen_unit, "relativeslopetaperingcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(bruntvaisalafreqtop, kgen_unit, "bruntvaisalafreqtop", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kappagm3d, kgen_unit, "kappagm3d", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(graddensityconstztopofedge, kgen_unit, "graddensityconstztopofedge", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(gmstreamfunctopofedge, kgen_unit, "gmstreamfunctopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(gmstreamfunctopofcell, kgen_unit, "gmstreamfunctopofcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(normalgmbolusvelocity, kgen_unit, "normalgmbolusvelocity", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(relativeslopetapering, kgen_unit, "relativeslopetapering", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(k33, kgen_unit, "k33", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(boundarylayerdepth, kgen_unit, "boundarylayerdepth", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(gmboluskappa, kgen_unit, "gmboluskappa", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(cgmphasespeed, kgen_unit, "cgmphasespeed", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(dcedge, kgen_unit, "dcedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(dvedge, kgen_unit, "dvedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(tridiaga, kgen_unit, "tridiaga", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(tridiagb, kgen_unit, "tridiagb", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(tridiagc, kgen_unit, "tridiagc", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(righthandside, kgen_unit, "righthandside", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(areacell, kgen_unit, "areacell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(maxlevelcell, kgen_unit, "maxlevelcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(maxleveledgetop, kgen_unit, "maxleveledgetop", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(nedgesoncell, kgen_unit, "nedgesoncell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp3(cellsonedge, kgen_unit, "cellsonedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp3(edgesoncell, kgen_unit, "edgesoncell", .FALSE.) 
    READ (UNIT = kgen_unit) k 
    READ (UNIT = kgen_unit) iedge 
    READ (UNIT = kgen_unit) icell 
    READ (UNIT = kgen_unit) cell1 
    READ (UNIT = kgen_unit) cell2 
    READ (UNIT = kgen_unit) i 
    READ (UNIT = kgen_unit) n 
    READ (UNIT = kgen_unit) iter 
    READ (UNIT = kgen_unit) rtmp 
    READ (UNIT = kgen_unit) h2 
    READ (UNIT = kgen_unit) h1 
    READ (UNIT = kgen_unit) areaedge 
    READ (UNIT = kgen_unit) stmp 
    READ (UNIT = kgen_unit) bruntvaisalafreqtopedge 
    READ (UNIT = kgen_unit) sumn2 
    READ (UNIT = kgen_unit) ltsum 
    READ (UNIT = kgen_unit) countn2 
    READ (UNIT = kgen_unit) maxn 
    READ (UNIT = kgen_unit) nedges 
    READ (UNIT = kgen_unit) ncells 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp5(nvertlevels, kgen_unit, "nvertlevels", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(ncellsarray, kgen_unit, "ncellsarray", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(nedgesarray, kgen_unit, "nedgesarray", .FALSE.) 
      
    !extern output variables 
    CALL kr_externs_out_ocn_gm(kgen_unit) 
      
    !local output variables 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_graddensityedge, kgen_unit, "kgenref_graddensityedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_gradzmidedge, kgen_unit, "kgenref_gradzmidedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_normalgmbolusvelocity, kgen_unit, "kgenref_normalgmbolusvelocity", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_graddensitytopofedge, kgen_unit, "kgenref_graddensitytopofedge", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_ddensitydztopofedge, kgen_unit, "kgenref_ddensitydztopofedge", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_gradzmidtopofedge, kgen_unit, "kgenref_gradzmidtopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_relativeslopetopofedge, kgen_unit, "kgenref_relativeslopetopofedge", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_relativeslopetapering, kgen_unit, "kgenref_relativeslopetapering", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_ddensitydztopofcell, kgen_unit, "kgenref_ddensitydztopofcell", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_k33, kgen_unit, "kgenref_k33", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_relativeslopetopofcell, kgen_unit, "kgenref_relativeslopetopofcell", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_relativeslopetaperingcell, kgen_unit, &
    &"kgenref_relativeslopetaperingcell", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_graddensityconstztopofedge, kgen_unit, &
    &"kgenref_graddensityconstztopofedge", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_areacellsum, kgen_unit, "kgenref_areacellsum", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_kappagm3d, kgen_unit, "kgenref_kappagm3d", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_gmstreamfunctopofedge, kgen_unit, "kgenref_gmstreamfunctopofedge", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp0(kgenref_gmstreamfunctopofcell, kgen_unit, "kgenref_gmstreamfunctopofcell", &
    &.FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(kgenref_cgmphasespeed, kgen_unit, "kgenref_cgmphasespeed", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(kgenref_tridiagb, kgen_unit, "kgenref_tridiagb", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(kgenref_tridiagc, kgen_unit, "kgenref_tridiagc", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(kgenref_righthandside, kgen_unit, "kgenref_righthandside", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp1(kgenref_tridiaga, kgen_unit, "kgenref_tridiaga", .FALSE.) 
    CALL kr_kgen_ocn_gm_compute_bolus_velocity_subp2(kgenref_maxleveledgetop, kgen_unit, "kgenref_maxleveledgetop", .FALSE.) 
    READ (UNIT = kgen_unit) kgenref_iedge 
    READ (UNIT = kgen_unit) kgenref_k 
    READ (UNIT = kgen_unit) kgenref_icell 
    READ (UNIT = kgen_unit) kgenref_cell1 
    READ (UNIT = kgen_unit) kgenref_cell2 
    READ (UNIT = kgen_unit) kgenref_i 
    READ (UNIT = kgen_unit) kgenref_iter 
    READ (UNIT = kgen_unit) kgenref_n 
    READ (UNIT = kgen_unit) kgenref_rtmp 
    READ (UNIT = kgen_unit) kgenref_h1 
    READ (UNIT = kgen_unit) kgenref_h2 
    READ (UNIT = kgen_unit) kgenref_areaedge 
    READ (UNIT = kgen_unit) kgenref_stmp 
    READ (UNIT = kgen_unit) kgenref_bruntvaisalafreqtopedge 
    READ (UNIT = kgen_unit) kgenref_sumn2 
    READ (UNIT = kgen_unit) kgenref_ltsum 
    READ (UNIT = kgen_unit) kgenref_countn2 
    READ (UNIT = kgen_unit) kgenref_maxn 
    READ (UNIT = kgen_unit) kgenref_ncells 
    READ (UNIT = kgen_unit) kgenref_nedges 


!$kgen begin_callsite ocn_gm_compute_Bolus_velocity


    IF (kgen_evalstage) THEN 
    END IF   
    IF (kgen_warmupstage) THEN 
    END IF   
    IF (kgen_mainstage) THEN 
    END IF   
      
    !Uncomment following call statement to turn on perturbation experiment. 
    !Adjust perturbation value and/or kind parameter if required. 
    !CALL kgen_perturb_real( your_variable, 1.0D-15_8 ) 
      
      
    !call to kgen kernel 
339
340
      !$acc data copyin(nvertlevels)

341
342
343
344
345
346
      nCells = nCellsArray( size(nCellsArray) )
      nEdges = nEdgesArray( size(nEdgesArray) )

      ! Assign a huge value to the scratch variables which may manifest itself when
      ! there is a bug.
      !$omp do schedule(runtime) private(k)
347
348
349
      !print *, "NEDGES", nEdges
      !print *, "NVERTLEVELS", nvertlevels
      !$acc parallel  loop gang
350
      do iEdge = 1, nEdges
351
         !$acc loop vector
352
353
354
355
356
357
         do k = 1, nVertLevels
            gradDensityEdge(k, iEdge) = huge(0D0)
            gradZMidEdge(k, iEdge) = huge(0D0)
            normalGMBolusVelocity(k, iEdge) = 0.0_RKIND
         end do
      end do
358
      !$acc end parallel
359
360
361
      !$omp end do

      !$omp do schedule(runtime) private(k)
362
      !$acc parallel  loop gang
363
      do iEdge = 1, nEdges
364
         !$acc loop vector
365
366
367
368
369
370
371
372
         do k = 1, nVertLevels + 1
            gradDensityTopOfEdge(k, iEdge) = huge(0D0)
            dDensityDzTopOfEdge(k, iEdge) = huge(0D0)
            gradZMidTopOfEdge(k, iEdge) = huge(0D0)
            relativeSlopeTopOfEdge(k, iEdge) = 0.0_RKIND
            relativeSlopeTapering(k, iEdge) = 0.0_RKIND
         end do
      end do
373
      !$acc end parallel
374
375
376
      !$omp end do

      !$omp do schedule(runtime) private(k)
377
      !$acc parallel  loop gang
378
      do iCell = 1, nCells + 1
379
         !$acc loop vector
380
381
382
383
384
385
386
         do k = 1, nVertLevels
            dDensityDzTopOfCell(k,  iCell) = huge(0D0)
            k33(k, iCell) = 0.0_RKIND
            relativeSlopeTopOfCell(k, iCell) = 0.0_RKIND
            relativeSlopeTaperingCell(k, iCell) = 0.0_RKIND
         end do
      end do
387
      !$acc end parallel
388
389
390
391
392
393
394
395
396
397
398
399
400
401
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute vertical derivative of density at top of cell, interpolate to top of edge
      ! This is required for Redi and Bolus parts.
      !
      !--------------------------------------------------------------------

      nCells = nCellsArray( 3 )
      ! Compute vertical derivative of density (dDensityDzTopOfCell) at cell center and layer interface
      ! Note that displacedDensity is used from the upper cell, so that the EOS reference level for
      ! pressure is the same for both displacedDensity(k-1,iCell) and density(k,iCell).
      !$omp do schedule(runtime) private(k, rtmp)
402
403
      ! verification failure
      !!$acc parallel  loop gang
404
      do iCell = 1, nCells
405
         !!$acc loop vector
406
407
408
409
410
411
412
413
414
415
416
         do k = 2, maxLevelCell(iCell)
            rtmp = (displacedDensity(k-1,iCell) - density(k,iCell)) / (zMid(k-1,iCell) - zMid(k,iCell))
            dDensityDzTopOfCell(k,iCell) = min(rtmp, -epsGM)
         end do

         ! Approximation of dDensityDzTopOfCell on the top and bottom interfaces through the idea of having
         ! ghost cells above the top and below the bottom layers of the same depths and density.
         ! Essentially, this enforces the boundary condition (d density)/dz = 0 at the top and bottom.
         dDensityDzTopOfCell(1,iCell) = 0.0_RKIND
         dDensityDzTopOfCell(maxLevelCell(iCell)+1,iCell) = 0.0_RKIND
      end do
417
      !!$acc end parallel
418
419
420
421
422
423
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate dDensityDzTopOfCell to edge and layer interface
      !$omp do schedule(runtime) private(k, cell1, cell2)
424
      !$acc parallel  loop gang
425
      do iEdge = 1, nEdges
426
         !$acc loop vector
427
428
429
430
431
432
         do k = 1, maxLevelEdgeTop(iEdge)+1
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)
            dDensityDzTopOfEdge(k,iEdge) = 0.5_RKIND * (dDensityDzTopOfCell(k,cell1) + dDensityDzTopOfCell(k,cell2))
         end do
      end do
433
      !$acc end parallel
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute horizontal gradient and mid-layer of edge, interpolate to top of edge
      ! This is required for Redi and Bolus parts.
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      ! Compute density gradient (gradDensityEdge) and gradient of zMid (gradZMidEdge)
      ! along the constant coordinate surface.
      ! The computed variables lives at edge and mid-layer depth
      !$omp do schedule(runtime) private(cell1, cell2, k)
449
450
      ! verification failure
      !!$acc parallel  loop gang
451
452
453
454
      do iEdge = 1, nEdges
         cell1 = cellsOnEdge(1,iEdge)
         cell2 = cellsOnEdge(2,iEdge)

455
         !!$acc loop vector
456
457
458
459
460
         do k=1,maxLevelEdgeTop(iEdge)
            gradDensityEdge(k,iEdge) = (density(k,cell2) - density(k,cell1)) / dcEdge(iEdge)
            gradZMidEdge(k,iEdge) = (zMid(k,cell2) - zMid(k,cell1)) / dcEdge(iEdge)
         end do
      end do
461
      !!$acc end parallel
462
463
464
465
466
467
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate gradDensityEdge and gradZMidEdge to layer interface
      !$omp do schedule(runtime) private(k, h1, h2)
468
469
      ! verification failure
      !!$acc parallel  loop gang
470
471
472
      do iEdge = 1, nEdges
         ! The interpolation can only be carried out on non-boundary edges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
473
            !!$acc loop vector
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
            do k = 2, maxLevelEdgeTop(iEdge)
               h1 = layerThicknessEdge(k-1,iEdge)
               h2 = layerThicknessEdge(k,iEdge)
               ! Using second-order interpolation below
               gradDensityTopOfEdge(k,iEdge) = (h2 * gradDensityEdge(k-1,iEdge) + h1 * gradDensityEdge(k,iEdge)) / (h1 + h2)
               gradZMidTopOfEdge(k,iEdge) = (h2 * gradZMidEdge(k-1,iEdge) + h1 * gradZMidEdge(k,iEdge)) / (h1 + h2)

            end do

            ! Approximation of values on the top and bottom interfaces through the idea of having ghost cells above
            ! the top and below the bottom layers of the same depths and density.
            gradDensityTopOfEdge(1,iEdge) = gradDensityEdge(1,iEdge)
            gradDensityTopOfEdge(maxLevelEdgeTop(iEdge)+1,iEdge) = gradDensityEdge(maxLevelEdgeTop(iEdge),iEdge)
            gradZMidTopOfEdge(1,iEdge) = gradZMidEdge(1,iEdge)
            gradZMidTopOfEdge(maxLevelEdgeTop(iEdge)+1,iEdge) = gradZMidEdge(maxLevelEdgeTop(iEdge),iEdge)
         end if
      end do
491
      !!$acc end parallel
492
493
494
495
496
497
498
499
500
501
502
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute horizontal gradient required for Bolus part (along constant z)
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime) private(k)
503
      !$acc parallel  loop gang
504
505
      do iEdge = 1, nEdges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
506
            !$acc loop vector
507
508
509
510
511
512
            do k = 1, maxLevelEdgeTop(iEdge)+1
               gradDensityConstZTopOfEdge(k,iEdge) = gradDensityTopOfEdge(k,iEdge) - dDensityDzTopOfEdge(k,iEdge) &
                                                   * gradZMidTopOfEdge(k,iEdge)
            end do
         end if
      end do
513
      !$acc end parallel
514
515
516
517
518
519
520
521
522
523
524
525
526
527
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute relative slope and k33 for Redi part of GM.
      ! These variables are used in del2 velocity tendency routines.
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      ! Compute relativeSlopeTopOfEdge at edge and layer interface
      ! set relativeSlopeTopOfEdge to zero for horizontal land/water edges.
      !$omp do schedule(runtime) private(k)
528
      !$acc parallel  loop gang
529
530
531
532
      do iEdge = 1, nEdges
        relativeSlopeTopOfEdge(:, iEdge) = 0.0_RKIND

         ! Beside a full land cell (e.g. missing cell) maxLevelEdgeTop=0, so relativeSlopeTopOfEdge at that edge will remain zero.
533
         !$acc loop vector
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
         do k = 2, maxLevelEdgeTop(iEdge)
            relativeSlopeTopOfEdge(k,iEdge) = - gradDensityTopOfEdge(k,iEdge) / min(dDensityDzTopOfEdge(k,iEdge),-epsGM)
         end do

         ! Since dDensityDzTopOfEdge is guaranteed to be zero on the top surface, relativeSlopeTopOfEdge on the top
         ! surface is identified with its value on the second interface.
         relativeSlopeTopOfEdge(1,iEdge) = relativeSlopeTopOfEdge(2,iEdge)

         ! dDensityDzTopOfEdge may or may not equal zero on the bottom surface, depending on whether
         ! maxLevelEdgeTop(iEdge) = maxLevelEdgeBottom(iEdge). But here we
         ! take a simplistic approach and identify relativeSlopeTopOfEdge on the bottom surface with its value on
         ! the interface just above.
         relativeSlopeTopOfEdge( maxLevelEdgeTop(iEdge)+1, iEdge ) = relativeSlopeTopOfEdge( max(1,maxLevelEdgeTop(iEdge)), iEdge )

      end do
549
      !$acc end parallel
550
551
552
553
554
555
556
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! slope can be unbounded in regions of neutral stability, reset to the large, but bounded, value
      ! values is hardwrite to 1.0, this is equivalent to a slope of 45 degrees
      !$omp do schedule(runtime) private(k)
557
      !$acc parallel  loop gang
558
      do iEdge = 1, nEdges
559
         !$acc loop vector
560
561
562
563
         do k = 1, nVertLevels
            relativeSlopeTopOfEdge(k, iEdge) = max( min( relativeSlopeTopOfEdge(k, iEdge), 1.0_RKIND), -1.0_RKIND)
         end do
      end do
564
      !$acc end parallel
565
566
567
568
569
570
571
572
      !$omp end do

      ! average relative slope to cell centers
      ! do this by computing (relative slope)^2, then taking sqrt

      nCells = nCellsArray( 2 )

      !$omp do schedule(runtime) private(i, iEdge, areaEdge, rtmp, k)
573
      !$acc parallel  loop gang
574
575
576
577
578
579
580
      do iCell = 1, nCells
         areaCellSum(:, iCell) = 1.0e-34_RKIND
         do i = 1, nEdgesOnCell(iCell)
            iEdge = edgesOnCell(i, iCell)

            !contribution of cell area from this edge * 2.0
            areaEdge = 0.5_RKIND * dcEdge(iEdge) * dvEdge(iEdge)
581
            !$acc loop vector
582
583
584
585
586
587
588
            do k = 1, maxLevelEdgeTop(iEdge)
               rtmp = areaEdge * relativeSlopeTopOfEdge(k, iEdge)**2
               relativeSlopeTopOfCell(k, iCell) = relativeSlopeTopOfCell(k, iCell) + rtmp
               areaCellSum(k, iCell) = areaCellSum(k, iCell) + areaEdge
            end do
         end do
      end do
589
      !$acc end parallel
590
591
592
593
594
      !$omp end do

      nCells = nCellsArray( 2 )

      !$omp do schedule(runtime) private(k)
595
      !$acc parallel  loop gang
596
      do iCell=1,nCells
597
        !$acc loop vector
598
599
600
601
        do k = 1, maxLevelCell(iCell)
           relativeSlopeTopOfCell(k,iCell) = sqrt( relativeSlopeTopOfCell(k,iCell)/areaCellSum(k,iCell) )
        end do
      end do
602
      !$acc end parallel
603
604
605
606
607
608
609
610
      !$omp end do

      ! Compute tapering function
      ! Compute k33 at cell center and layer interface

      nCells = nCellsArray( size(nCellsArray) )

      !$omp do schedule(runtime)
611
612
      ! verification failure
      !!$acc parallel  loop gang
613
614
615
      do iCell = 1, nCells
         k33(:, iCell) = 0.0_RKIND
      end do
616
      !!$acc end parallel
617
618
619
620
      !$omp end do

      ! use relativeSlopeTaperingCell as a temporary space for smoothing of relativeSlopeTopOfCell
      relativeSlopeTaperingCell = relativeSlopeTopOfCell
621
      !!$acc parallel  loop gang
622
623
624
625
626
627
628
629
      do iter = 1, 5

         nCells = nCellsArray( 2 )

         !$omp do schedule(runtime)
         do iCell=1,nCells
           relativeSlopeTaperingCell(1, iCell) = 0.0_RKIND
           relativeSlopeTaperingCell(maxLevelCell(iCell):nVertLevels, iCell) = 0.0_RKIND
630
           !!$acc loop vector
631
632
633
634
635
636
637
638
639
           do k = 2, maxLevelCell(iCell)-1
             rtmp = relativeSlopeTopOfCell(k-1,iCell) + relativeSlopeTopOfCell(k+1,iCell)
             stmp = 2.0_RKIND*relativeSlopeTopOfCell(k,iCell)
             relativeSlopeTaperingCell(k,iCell) = (rtmp+stmp)/4.0_RKIND
           end do
           relativeSlopeTopOfCell(:, iCell) = relativeSlopeTaperingCell(:, iCell)
         end do
         !$omp end do
      end do  ! iter
640
      !!$acc end parallel
641
642
643
644

      nCells = nCellsArray ( 2 )
      ! first, compute tapering across full domain based on a maximum allowable slope
      !$omp do schedule(runtime) private(k)
645
      !!$acc parallel  loop gang
646
      do iCell=1,nCells
647
648
        ! compilation error
        !!$acc loop vector
649
650
651
652
        do k = 1, maxLevelCell(iCell)
          relativeSlopeTaperingCell(k,iCell) = min(1.0_RKIND, config_max_relative_slope / (relativeSlopeTopOfCell(k,iCell)+epsGM))
        end do
      end do
653
      !!$acc end parallel
654
655
656
657
658
659
660
661
      !$omp end do

      ! now further taper in the boundary layer
      ! vertical (k33) tapering starts at 2*OBL, increases linearly to OBL and is held uniform across OBL
      ! rtmp = 1 @ zMid = -2.0*OBL, rtmp = 0 @ zMid = -OBL
      if(config_use_Redi_surface_layer_tapering) then
         nCells = nCellsArray ( 2 )
         !$omp do schedule(runtime) private(k, rtmp)
662
         !!$acc parallel  loop gang
663
         do iCell=1,nCells
664
665
           ! compilation error
           !!$acc loop vector
666
667
668
669
670
671
672
           do k = 1, maxLevelCell(iCell)
             rtmp = -zMid(k,iCell)/max(config_Redi_surface_layer_tapering_extent,boundaryLayerDepth(iCell)+epsGM)
             rtmp = max(0.0_RKIND,rtmp)
             rtmp = min(1.0_RKIND,rtmp)
             relativeSlopeTaperingCell(k,iCell) = rtmp*relativeSlopeTaperingCell(k,iCell)
           end do
         end do
673
         !!$acc end parallel
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
         !$omp end do
      endif ! config_use_Redi_surface_layer_tapering

      ! now further taper in the boundary layer
      ! vertical (k33) tapering starts at 2*OBL, increases linearly to OBL and is held uniform across OBL
      ! rtmp = 1 @ zMid = zMid(maxLevelCell) + config_Redi_bottom_layer_tapering_depth, rtmp = 0 @ zMid = zMid(maxLevelCell)
      if(config_use_Redi_bottom_layer_tapering) then
         nCells = nCellsArray ( 2 )
         !$omp do schedule(runtime) private(k, rtmp)
         do iCell=1,nCells
           do k = 1, maxLevelCell(iCell)
             rtmp = (zMid(k,iCell)-zMid(maxLevelCell(iCell),iCell))/(config_Redi_bottom_layer_tapering_depth+epsGM)
             rtmp = max(0.0_RKIND,rtmp)
             rtmp = min(1.0_RKIND,rtmp)
             relativeSlopeTaperingCell(k,iCell) = rtmp*relativeSlopeTaperingCell(k,iCell)
           end do
         end do
         !$omp end do
      endif ! config_use_Redi_bottom_layer_tapering

      nCells = nCellsArray( 2 )
      !$omp do schedule(runtime) private(k)
696
      !$acc parallel  loop gang
697
698
      do iCell=1,nCells
        k33(:, iCell) = 0.0_RKIND
699
        !$acc loop vector
700
701
702
703
        do k = 2, maxLevelCell(iCell)
          k33(k,iCell) = ( relativeSlopeTaperingCell(k,iCell) * relativeSlopeTopOfCell(k,iCell) )**2
        end do
      end do
704
      !$acc end parallel
705
706
707
708
709
710
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! average tapering function to layer edges
      !$omp do schedule(runtime) private(cell1, cell2, k)
711
      !$acc parallel  loop gang
712
713
714
      do iEdge = 1, nEdges
        cell1 = cellsOnEdge(1,iEdge)
        cell2 = cellsOnEdge(2,iEdge)
715
        !$acc loop vector
716
717
718
719
        do k = 1, maxLevelEdgeTop(iEdge)
          relativeSlopeTapering(k,iEdge) = 0.5_RKIND * (relativeSlopeTaperingCell(k,cell1) + relativeSlopeTaperingCell(k,cell2))
        enddo
      enddo
720
      !$acc end parallel
721
722
723
724
725
726
      !$omp end do

      ! allow disabling of K33 for testing
      if(config_disable_redi_k33) then
        nCells = nCellsArray( size(nCellsArray) )
        !$omp do schedule(runtime)
727
        !$acc parallel  loop gang
728
729
730
        do iCell = 1, nCells
           k33(:, iCell) = 0.0_RKIND
        end do
731
        !$acc end parallel
732
733
734
735
736
737
738
739
740
741
742
        !$omp end do
      end if

      !--------------------------------------------------------------------
      !
      ! Compute stream function and Bolus velocity for Bolus part of GM
      !
      !--------------------------------------------------------------------

      if (config_gm_lat_variable_c2) then
         !$omp do schedule(runtime) private(cell1, cell2, sumN2, ltSum, countN2, BruntVaisalaFreqTopEdge)
743
744
         ! compilation error
         !!$acc parallel  loop gang
745
746
747
748
749
750
751
         do iEdge = 1, nEdges
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)
            sumN2 = 0.0
            ltSum = 0.0
            countN2 = 0
            
752
            !!$acc loop vector
753
754
755
756
757
758
759
760
761
762
763
764
765
766
            do k=2,maxLevelEdgeTop(iEdge)

               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
                
               sumN2 = sumN2 + BruntVaisalaFreqTopEdge*layerThicknessEdge(k,iEdge)
               ltSum = ltSum + layerThicknessEdge(k,iEdge)
               countN2 = countN2 +1

            enddo

            if(countN2 > 0) cGMphaseSpeed(iEdge) = max(config_gm_min_phase_speed ,sqrt(sumN2/ltSum)*ltSum / 3.141592_RKIND)

         enddo
767
         !!$acc end parallel
768
769
770
771
         !$omp end do

      else
         !$omp do schedule(runtime)
772
773
         ! compilation error
         !!$acc parallel  loop gang
774
775
776
         do iEdge = 1, nEdges
            cGMphaseSpeed(iEdge) = config_gravWaveSpeed_trunc
         enddo
777
         !!$acc end parallel
778
779
780
781
         !$omp end do
      endif

      !$omp do schedule(runtime)
782
783
      ! runtime failure
      !!$acc parallel  loop gang
784
785
786
      do iEdge=1,nEdges
         kappaGM3D(:,iEdge) = gmBolusKappa(iEdge)
      enddo 
787
      !!$acc end parallel
788
789
790
791
792
      !$omp end do

      if (config_gm_kappa_lat_depth_variable) then

         !$omp do schedule(runtime) private(cell1, cell2, k, BruntVaisalaFreqTopEdge, maxN)
793
794
         ! compilation error
         !!$acc parallel  loop gang
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
         do iEdge = 1,nEdges
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)

            maxN = -1.0_RKIND
            do k=2,maxLevelEdgeTop(iEdge)
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)

               maxN = max(maxN,BruntVaisalaFreqTopEdge)

            enddo

            do k=2,maxLevelEdgeTop(iEdge)
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)

               kappaGM3D(k,iEdge) = gmBolusKappa(iEdge)*max(config_gm_min_stratification_ratio, &
                       BruntVaisalaFreqTopEdge / (maxN + 1.0E-10_RKIND))
            enddo
         enddo
816
         !!$acc end parallel
817
818
819
820
821
822
         !$omp end do
      endif

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime)
823
824
      ! compilation error
      !!$acc parallel  loop gang
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
      do iEdge = 1, nEdges
         cell1 = cellsOnEdge(1,iEdge)
         cell2 = cellsOnEdge(2,iEdge)

         gmStreamFuncTopOfEdge(:, iEdge) = 0.0_RKIND

         ! Construct the tridiagonal matrix
         if (maxLevelEdgeTop(iEdge) .GE. 3) then
            ! First row
            k = 2
            BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
            BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
            tridiagB(k-1) = - 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / (layerThicknessEdge(k-1,iEdge) &
                          * layerThicknessEdge(k,iEdge)) - BruntVaisalaFreqTopEdge
            tridiagC(k-1) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k, iEdge) &
                          / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
            rightHandSide(k-1) = kappaGM3D(k-1,iEdge) * gravity / rho_sw * gradDensityConstZTopOfEdge(k,iEdge)

            ! Second to next to the last rows
            do k = 3, maxLevelEdgeTop(iEdge)-1
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
               tridiagA(k-2) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k-1, iEdge) &
                             / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
               tridiagB(k-1) = - 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / (layerThicknessEdge(k-1, iEdge) &
                             * layerThicknessEdge(k, iEdge) ) - BruntVaisalaFreqTopEdge
               tridiagC(k-1) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k, iEdge) &
                             / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
               rightHandSide(k-1) = kappaGM3D(k-1,iEdge) * gravity / rho_sw * gradDensityConstZTopOfEdge(k,iEdge)
            end do

            ! Last row
            k = maxLevelEdgeTop(iEdge)
            BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
            BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
            tridiagA(k-2) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k-1,iEdge) &
                          / (layerThicknessEdge(k-1,iEdge) + layerThicknessEdge(k,iEdge))
            tridiagB(k-1) = - 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / (layerThicknessEdge(k-1, iEdge) &
                          * layerThicknessEdge(k, iEdge)) - BruntVaisalaFreqTopEdge
            rightHandSide(k-1) = kappaGM3D(k-1,iEdge) * gravity / rho_sw * gradDensityConstZTopOfEdge(k,iEdge)

            ! Total number of rows
            N = maxLevelEdgeTop(iEdge) - 1

            ! Call the tridiagonal solver
            call tridiagonal_solve(tridiagA, tridiagB, tridiagC, rightHandSide, &
                                   gmStreamFuncTopOfEdge(2:maxLevelEdgeTop(iEdge), iEdge), N)
         end if
      end do
874
      !!$acc end parallel
875
876
877
878
879
      !$omp end do

      nEdges = nEdgesArray( 3 )
      ! Compute normalGMBolusVelocity from the stream function
      !$omp do schedule(runtime) private(k)
880
      !$acc parallel  loop gang
881
      do iEdge = 1, nEdges
882
         !$acc loop vector
883
884
885
886
887
         do k = 1, maxLevelEdgeTop(iEdge)
            normalGMBolusVelocity(k,iEdge) = (gmStreamFuncTopOfEdge(k,iEdge) - gmStreamFuncTopOfEdge(k+1,iEdge)) &
                                           / layerThicknessEdge(k,iEdge)
         end do
      end do
888
      !$acc end parallel
889
890
891
892
893
894
      !$omp end do

      nCells = nCellsArray( 1 )

      ! Interpolate gmStreamFuncTopOfEdge to cell centers for visualization
      !$omp do schedule(runtime) private(i, iEdge, areaEdge, k, rtmp)
895
896
      ! verification failure
      !!$acc parallel  loop gang
897
898
899
900
901
902
903
      do iCell = 1, nCells
         gmStreamFuncTopOfCell(:, iCell) = 0.0_RKIND
         do i = 1, nEdgesOnCell(iCell)
            iEdge = edgesOnCell(i, iCell)

            areaEdge = 0.25_RKIND * dcEdge(iEdge) * dvEdge(iEdge)

904
            !!$acc loop vector
905
906
907
908
909
910
            do k = 1, maxLevelEdgeTop(iEdge)
               rtmp = 0.5_RKIND * ( gmStreamFuncTopOfEdge(k, iEdge) + gmStreamFuncTopOfEdge(k+1, iEdge) ) * areaEdge
               gmStreamFuncTopOfCell(k, iCell) = gmStreamFuncTopOfCell(k, iCell) + rtmp
            end do
         end do
      end do
911
      !!$acc end parallel
912
913
914
      !$omp end do

      !$omp do schedule(runtime)
915
916
      ! execution failture
      !!$acc parallel  loop gang
917
918
919
      do iCell = 1, nCells
         gmStreamFuncTopOfCell(:, iCell) = gmStreamFuncTopOfCell(:,iCell) / areaCell(iCell)
      end do
920
      !!$acc end parallel
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
      IF (kgen_mainstage) THEN 
            
          !verify init 
          CALL kgen_init_verify(tolerance=MAX_TOL, minvalue=1.D-14, verboseLevel=VERBOSITY) 
          CALL kgen_init_check(check_status, rank=kgen_mpirank) 
            
          !extern verify variables 
            
          !local verify variables 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("graddensityedge", check_status, graddensityedge, &
          &kgenref_graddensityedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("relativeslopetopofedge", check_status, relativeslopetopofedge, &
          &kgenref_relativeslopetopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("relativeslopetaperingcell", check_status, relativeslopetaperingcell, &
          &kgenref_relativeslopetaperingcell) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("k33", check_status, k33, kgenref_k33) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("graddensitytopofedge", check_status, graddensitytopofedge, &
          &kgenref_graddensitytopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("normalgmbolusvelocity", check_status, normalgmbolusvelocity, &
          &kgenref_normalgmbolusvelocity) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("graddensityconstztopofedge", check_status, &
          &graddensityconstztopofedge, kgenref_graddensityconstztopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("gmstreamfunctopofedge", check_status, gmstreamfunctopofedge, &
          &kgenref_gmstreamfunctopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("kappagm3d", check_status, kappagm3d, kgenref_kappagm3d) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("gradzmidtopofedge", check_status, gradzmidtopofedge, &
          &kgenref_gradzmidtopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("ddensitydztopofedge", check_status, ddensitydztopofedge, &
          &kgenref_ddensitydztopofedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("ddensitydztopofcell", check_status, ddensitydztopofcell, &
          &kgenref_ddensitydztopofcell) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("gmstreamfunctopofcell", check_status, gmstreamfunctopofcell, &
          &kgenref_gmstreamfunctopofcell) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("relativeslopetapering", check_status, relativeslopetapering, &
          &kgenref_relativeslopetapering) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("relativeslopetopofcell", check_status, relativeslopetopofcell, &
          &kgenref_relativeslopetopofcell) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("areacellsum", check_status, areacellsum, kgenref_areacellsum) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp0("gradzmidedge", check_status, gradzmidedge, kgenref_gradzmidedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp1("cgmphasespeed", check_status, cgmphasespeed, kgenref_cgmphasespeed) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp1("tridiagb", check_status, tridiagb, kgenref_tridiagb) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp1("tridiagc", check_status, tridiagc, kgenref_tridiagc) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp1("tridiaga", check_status, tridiaga, kgenref_tridiaga) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp1("righthandside", check_status, righthandside, kgenref_righthandside) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp2("maxleveledgetop", check_status, maxleveledgetop, &
          &kgenref_maxleveledgetop) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("i", check_status, i, kgenref_i) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("icell", check_status, icell, kgenref_icell) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("k", check_status, k, kgenref_k) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("cell2", check_status, cell2, kgenref_cell2) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("iter", check_status, iter, kgenref_iter) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("n", check_status, n, kgenref_n) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("cell1", check_status, cell1, kgenref_cell1) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("iedge", check_status, iedge, kgenref_iedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("h2", check_status, h2, kgenref_h2) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("stmp", check_status, stmp, kgenref_stmp) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("h1", check_status, h1, kgenref_h1) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("bruntvaisalafreqtopedge", check_status, bruntvaisalafreqtopedge, &
          &kgenref_bruntvaisalafreqtopedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("rtmp", check_status, rtmp, kgenref_rtmp) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("areaedge", check_status, areaedge, kgenref_areaedge) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("sumn2", check_status, sumn2, kgenref_sumn2) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("maxn", check_status, maxn, kgenref_maxn) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("ltsum", check_status, ltsum, kgenref_ltsum) 
          CALL kv_kgen_ocn_gm_compute_bolus_velocity_subp3("countn2", check_status, countn2, kgenref_countn2) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("nedges", check_status, nedges, kgenref_nedges) 
          CALL kv_ocn_gm_compute_bolus_velocity_integer__("ncells", check_status, ncells, kgenref_ncells) 
          IF (check_status%rank == 0) THEN 
              WRITE (*, *) "" 
          END IF   
          IF (kgen_verboseLevel > 0) THEN 
              IF (check_status%rank == 0) THEN 
                  WRITE (*, *) "Number of output variables: ", check_status%numTotal 
                  WRITE (*, *) "Number of identical variables: ", check_status%numIdentical 
                  WRITE (*, *) "Number of non-identical variables within tolerance: ", check_status%numInTol 
                  WRITE (*, *) "Number of non-identical variables out of tolerance: ", check_status%numOutTol 
                  WRITE (*, *) "Tolerance: ", kgen_tolerance 
              END IF   
          END IF   
          IF (check_status%rank == 0) THEN 
              WRITE (*, *) "" 
          END IF   
          IF (check_status%numOutTol > 0) THEN 
              IF (check_status%rank == 0) THEN 
                  WRITE (*, *) "Verification FAILED with" // TRIM(ADJUSTL(kgen_filepath)) 
              END IF   
              check_status%Passed = .FALSE. 
              kgen_isverified = .FALSE. 
          ELSE 
              IF (check_status%rank == 0) THEN 
                  WRITE (*, *) "Verification PASSED with " // TRIM(ADJUSTL(kgen_filepath)) 
              END IF   
              check_status%Passed = .TRUE. 
              kgen_isverified = .TRUE. 
          END IF   
          IF (check_status%rank == 0) THEN 
              WRITE (*, *) "" 
          END IF   
            
#ifdef _MPI 
          call mpi_barrier(mpi_comm_world, kgen_ierr) 
#endif 
            
          CALL SYSTEM_CLOCK(kgen_start_clock, kgen_rate_clock) 
          DO kgen_intvar = 1, KGEN_MAXITER 
      nCells = nCellsArray( size(nCellsArray) )
      nEdges = nEdgesArray( size(nEdgesArray) )

      ! Assign a huge value to the scratch variables which may manifest itself when
      ! there is a bug.
      !$omp do schedule(runtime) private(k)
1032
      !$acc parallel  loop gang
1033
      do iEdge = 1, nEdges
1034
         !$acc loop vector
1035
1036
1037
1038
1039
1040
         do k = 1, nVertLevels
            gradDensityEdge(k, iEdge) = huge(0D0)
            gradZMidEdge(k, iEdge) = huge(0D0)
            normalGMBolusVelocity(k, iEdge) = 0.0_RKIND
         end do
      end do
1041
      !$acc end parallel
1042
1043
1044
      !$omp end do

      !$omp do schedule(runtime) private(k)
1045
      !$acc parallel  loop gang
1046
      do iEdge = 1, nEdges
1047
         !$acc loop vector
1048
1049
1050
1051
1052
1053
1054
1055
         do k = 1, nVertLevels + 1
            gradDensityTopOfEdge(k, iEdge) = huge(0D0)
            dDensityDzTopOfEdge(k, iEdge) = huge(0D0)
            gradZMidTopOfEdge(k, iEdge) = huge(0D0)
            relativeSlopeTopOfEdge(k, iEdge) = 0.0_RKIND
            relativeSlopeTapering(k, iEdge) = 0.0_RKIND
         end do
      end do
1056
      !$acc end parallel
1057
1058
1059
      !$omp end do

      !$omp do schedule(runtime) private(k)
1060
      !$acc parallel  loop gang
1061
      do iCell = 1, nCells + 1
1062
         !$acc loop vector
1063
1064
1065
1066
1067
1068
1069
         do k = 1, nVertLevels
            dDensityDzTopOfCell(k,  iCell) = huge(0D0)
            k33(k, iCell) = 0.0_RKIND
            relativeSlopeTopOfCell(k, iCell) = 0.0_RKIND
            relativeSlopeTaperingCell(k, iCell) = 0.0_RKIND
         end do
      end do
1070
      !$acc end parallel
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute vertical derivative of density at top of cell, interpolate to top of edge
      ! This is required for Redi and Bolus parts.
      !
      !--------------------------------------------------------------------

      nCells = nCellsArray( 3 )
      ! Compute vertical derivative of density (dDensityDzTopOfCell) at cell center and layer interface
      ! Note that displacedDensity is used from the upper cell, so that the EOS reference level for
      ! pressure is the same for both displacedDensity(k-1,iCell) and density(k,iCell).
      !$omp do schedule(runtime) private(k, rtmp)
      do iCell = 1, nCells
         do k = 2, maxLevelCell(iCell)
            rtmp = (displacedDensity(k-1,iCell) - density(k,iCell)) / (zMid(k-1,iCell) - zMid(k,iCell))
            dDensityDzTopOfCell(k,iCell) = min(rtmp, -epsGM)
         end do

         ! Approximation of dDensityDzTopOfCell on the top and bottom interfaces through the idea of having
         ! ghost cells above the top and below the bottom layers of the same depths and density.
         ! Essentially, this enforces the boundary condition (d density)/dz = 0 at the top and bottom.
         dDensityDzTopOfCell(1,iCell) = 0.0_RKIND
         dDensityDzTopOfCell(maxLevelCell(iCell)+1,iCell) = 0.0_RKIND
      end do
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate dDensityDzTopOfCell to edge and layer interface
      !$omp do schedule(runtime) private(k, cell1, cell2)
1103
      !$acc parallel  loop gang
1104
      do iEdge = 1, nEdges
1105
         !$acc loop vector
1106
1107
1108
1109
1110
1111
         do k = 1, maxLevelEdgeTop(iEdge)+1
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)
            dDensityDzTopOfEdge(k,iEdge) = 0.5_RKIND * (dDensityDzTopOfCell(k,cell1) + dDensityDzTopOfCell(k,cell2))
         end do
      end do
1112
      !$acc end parallel
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute horizontal gradient and mid-layer of edge, interpolate to top of edge
      ! This is required for Redi and Bolus parts.
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      ! Compute density gradient (gradDensityEdge) and gradient of zMid (gradZMidEdge)
      ! along the constant coordinate surface.
      ! The computed variables lives at edge and mid-layer depth
      !$omp do schedule(runtime) private(cell1, cell2, k)
      do iEdge = 1, nEdges
         cell1 = cellsOnEdge(1,iEdge)
         cell2 = cellsOnEdge(2,iEdge)

         do k=1,maxLevelEdgeTop(iEdge)
            gradDensityEdge(k,iEdge) = (density(k,cell2) - density(k,cell1)) / dcEdge(iEdge)
            gradZMidEdge(k,iEdge) = (zMid(k,cell2) - zMid(k,cell1)) / dcEdge(iEdge)
         end do
      end do
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate gradDensityEdge and gradZMidEdge to layer interface
      !$omp do schedule(runtime) private(k, h1, h2)
      do iEdge = 1, nEdges
         ! The interpolation can only be carried out on non-boundary edges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
            do k = 2, maxLevelEdgeTop(iEdge)
               h1 = layerThicknessEdge(k-1,iEdge)
               h2 = layerThicknessEdge(k,iEdge)
               ! Using second-order interpolation below
               gradDensityTopOfEdge(k,iEdge) = (h2 * gradDensityEdge(k-1,iEdge) + h1 * gradDensityEdge(k,iEdge)) / (h1 + h2)
               gradZMidTopOfEdge(k,iEdge) = (h2 * gradZMidEdge(k-1,iEdge) + h1 * gradZMidEdge(k,iEdge)) / (h1 + h2)

            end do

            ! Approximation of values on the top and bottom interfaces through the idea of having ghost cells above
            ! the top and below the bottom layers of the same depths and density.
            gradDensityTopOfEdge(1,iEdge) = gradDensityEdge(1,iEdge)
            gradDensityTopOfEdge(maxLevelEdgeTop(iEdge)+1,iEdge) = gradDensityEdge(maxLevelEdgeTop(iEdge),iEdge)
            gradZMidTopOfEdge(1,iEdge) = gradZMidEdge(1,iEdge)
            gradZMidTopOfEdge(maxLevelEdgeTop(iEdge)+1,iEdge) = gradZMidEdge(maxLevelEdgeTop(iEdge),iEdge)
         end if
      end do
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute horizontal gradient required for Bolus part (along constant z)
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime) private(k)
1174
      !$acc parallel  loop gang
1175
1176
      do iEdge = 1, nEdges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
1177
            !$acc loop vector
1178
1179
1180
1181
1182
1183
            do k = 1, maxLevelEdgeTop(iEdge)+1
               gradDensityConstZTopOfEdge(k,iEdge) = gradDensityTopOfEdge(k,iEdge) - dDensityDzTopOfEdge(k,iEdge) &
                                                   * gradZMidTopOfEdge(k,iEdge)
            end do
         end if
      end do
1184
      !$acc end parallel
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
      !$omp end do

      !--------------------------------------------------------------------
      !
      ! Compute relative slope and k33 for Redi part of GM.
      ! These variables are used in del2 velocity tendency routines.
      !
      !--------------------------------------------------------------------

      nEdges = nEdgesArray( 3 )

      ! Compute relativeSlopeTopOfEdge at edge and layer interface
      ! set relativeSlopeTopOfEdge to zero for horizontal land/water edges.
      !$omp do schedule(runtime) private(k)
1199
      !$acc parallel  loop gang
1200
1201
1202
1203
      do iEdge = 1, nEdges
        relativeSlopeTopOfEdge(:, iEdge) = 0.0_RKIND

         ! Beside a full land cell (e.g. missing cell) maxLevelEdgeTop=0, so relativeSlopeTopOfEdge at that edge will remain zero.
1204
         !$acc loop vector
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
         do k = 2, maxLevelEdgeTop(iEdge)
            relativeSlopeTopOfEdge(k,iEdge) = - gradDensityTopOfEdge(k,iEdge) / min(dDensityDzTopOfEdge(k,iEdge),-epsGM)
         end do

         ! Since dDensityDzTopOfEdge is guaranteed to be zero on the top surface, relativeSlopeTopOfEdge on the top
         ! surface is identified with its value on the second interface.
         relativeSlopeTopOfEdge(1,iEdge) = relativeSlopeTopOfEdge(2,iEdge)

         ! dDensityDzTopOfEdge may or may not equal zero on the bottom surface, depending on whether
         ! maxLevelEdgeTop(iEdge) = maxLevelEdgeBottom(iEdge). But here we
         ! take a simplistic approach and identify relativeSlopeTopOfEdge on the bottom surface with its value on
         ! the interface just above.
         relativeSlopeTopOfEdge( maxLevelEdgeTop(iEdge)+1, iEdge ) = relativeSlopeTopOfEdge( max(1,maxLevelEdgeTop(iEdge)), iEdge )

      end do
1220
      !$acc end parallel
1221
1222
1223
1224
1225
1226
1227
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! slope can be unbounded in regions of neutral stability, reset to the large, but bounded, value
      ! values is hardwrite to 1.0, this is equivalent to a slope of 45 degrees
      !$omp do schedule(runtime) private(k)
1228
      !$acc parallel  loop gang
1229
      do iEdge = 1, nEdges
1230
         !$acc loop vector
1231
1232
1233
1234
         do k = 1, nVertLevels
            relativeSlopeTopOfEdge(k, iEdge) = max( min( relativeSlopeTopOfEdge(k, iEdge), 1.0_RKIND), -1.0_RKIND)
         end do
      end do
1235
      !$acc end parallel
1236
1237
1238
1239
1240
1241
1242
1243
      !$omp end do

      ! average relative slope to cell centers
      ! do this by computing (relative slope)^2, then taking sqrt

      nCells = nCellsArray( 2 )

      !$omp do schedule(runtime) private(i, iEdge, areaEdge, rtmp, k)
1244
      !$acc parallel  loop gang
1245
1246
1247
1248
1249
1250
1251
      do iCell = 1, nCells
         areaCellSum(:, iCell) = 1.0e-34_RKIND
         do i = 1, nEdgesOnCell(iCell)
            iEdge = edgesOnCell(i, iCell)

            !contribution of cell area from this edge * 2.0
            areaEdge = 0.5_RKIND * dcEdge(iEdge) * dvEdge(iEdge)
1252
            !$acc loop vector
1253
1254
1255
1256
1257
1258
1259
            do k = 1, maxLevelEdgeTop(iEdge)
               rtmp = areaEdge * relativeSlopeTopOfEdge(k, iEdge)**2
               relativeSlopeTopOfCell(k, iCell) = relativeSlopeTopOfCell(k, iCell) + rtmp
               areaCellSum(k, iCell) = areaCellSum(k, iCell) + areaEdge
            end do
         end do
      end do
1260
      !$acc end parallel
1261
1262
1263
1264
1265
      !$omp end do

      nCells = nCellsArray( 2 )

      !$omp do schedule(runtime) private(k)
1266
      !$acc parallel  loop gang
1267
      do iCell=1,nCells
1268
        !$acc loop vector
1269
1270
1271
1272
        do k = 1, maxLevelCell(iCell)
           relativeSlopeTopOfCell(k,iCell) = sqrt( relativeSlopeTopOfCell(k,iCell)/areaCellSum(k,iCell) )
        end do
      end do
1273
      !$acc end parallel
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
      !$omp end do

      ! Compute tapering function
      ! Compute k33 at cell center and layer interface

      nCells = nCellsArray( size(nCellsArray) )

      !$omp do schedule(runtime)
      do iCell = 1, nCells
         k33(:, iCell) = 0.0_RKIND
      end do
      !$omp end do

      ! use relativeSlopeTaperingCell as a temporary space for smoothing of relativeSlopeTopOfCell
      relativeSlopeTaperingCell = relativeSlopeTopOfCell
      do iter = 1, 5

         nCells = nCellsArray( 2 )

         !$omp do schedule(runtime)
         do iCell=1,nCells
           relativeSlopeTaperingCell(1, iCell) = 0.0_RKIND
           relativeSlopeTaperingCell(maxLevelCell(iCell):nVertLevels, iCell) = 0.0_RKIND
           do k = 2, maxLevelCell(iCell)-1
             rtmp = relativeSlopeTopOfCell(k-1,iCell) + relativeSlopeTopOfCell(k+1,iCell)
             stmp = 2.0_RKIND*relativeSlopeTopOfCell(k,iCell)
             relativeSlopeTaperingCell(k,iCell) = (rtmp+stmp)/4.0_RKIND
           end do
           relativeSlopeTopOfCell(:, iCell) = relativeSlopeTaperingCell(:, iCell)
         end do
         !$omp end do
      end do  ! iter

      nCells = nCellsArray ( 2 )
      ! first, compute tapering across full domain based on a maximum allowable slope
      !$omp do schedule(runtime) private(k)
      do iCell=1,nCells
        do k = 1, maxLevelCell(iCell)
          relativeSlopeTaperingCell(k,iCell) = min(1.0_RKIND, config_max_relative_slope / (relativeSlopeTopOfCell(k,iCell)+epsGM))
        end do
      end do
      !$omp end do

      ! now further taper in the boundary layer
      ! vertical (k33) tapering starts at 2*OBL, increases linearly to OBL and is held uniform across OBL
      ! rtmp = 1 @ zMid = -2.0*OBL, rtmp = 0 @ zMid = -OBL
      if(config_use_Redi_surface_layer_tapering) then
         nCells = nCellsArray ( 2 )
         !$omp do schedule(runtime) private(k, rtmp)
         do iCell=1,nCells
           do k = 1, maxLevelCell(iCell)
             rtmp = -zMid(k,iCell)/max(config_Redi_surface_layer_tapering_extent,boundaryLayerDepth(iCell)+epsGM)
             rtmp = max(0.0_RKIND,rtmp)
             rtmp = min(1.0_RKIND,rtmp)
             relativeSlopeTaperingCell(k,iCell) = rtmp*relativeSlopeTaperingCell(k,iCell)
           end do
         end do
         !$omp end do
      endif ! config_use_Redi_surface_layer_tapering

      ! now further taper in the boundary layer
      ! vertical (k33) tapering starts at 2*OBL, increases linearly to OBL and is held uniform across OBL
      ! rtmp = 1 @ zMid = zMid(maxLevelCell) + config_Redi_bottom_layer_tapering_depth, rtmp = 0 @ zMid = zMid(maxLevelCell)
      if(config_use_Redi_bottom_layer_tapering) then
         nCells = nCellsArray ( 2 )
         !$omp do schedule(runtime) private(k, rtmp)
         do iCell=1,nCells
           do k = 1, maxLevelCell(iCell)
             rtmp = (zMid(k,iCell)-zMid(maxLevelCell(iCell),iCell))/(config_Redi_bottom_layer_tapering_depth+epsGM)
             rtmp = max(0.0_RKIND,rtmp)
             rtmp = min(1.0_RKIND,rtmp)
             relativeSlopeTaperingCell(k,iCell) = rtmp*relativeSlopeTaperingCell(k,iCell)
           end do
         end do
         !$omp end do
      endif ! config_use_Redi_bottom_layer_tapering

      nCells = nCellsArray( 2 )
      !$omp do schedule(runtime) private(k)
1353
      !$acc parallel  loop gang
1354
1355
      do iCell=1,nCells
        k33(:, iCell) = 0.0_RKIND
1356
        !$acc loop vector
1357
1358
1359
1360
        do k = 2, maxLevelCell(iCell)
          k33(k,iCell) = ( relativeSlopeTaperingCell(k,iCell) * relativeSlopeTopOfCell(k,iCell) )**2
        end do
      end do
1361
      !$acc end parallel
1362
1363
1364
1365
1366
1367
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! average tapering function to layer edges
      !$omp do schedule(runtime) private(cell1, cell2, k)
1368
      !$acc parallel  loop gang
1369
1370
1371
      do iEdge = 1, nEdges
        cell1 = cellsOnEdge(1,iEdge)
        cell2 = cellsOnEdge(2,iEdge)
1372
        !$acc loop vector
1373
1374
1375
1376
        do k = 1, maxLevelEdgeTop(iEdge)
          relativeSlopeTapering(k,iEdge) = 0.5_RKIND * (relativeSlopeTaperingCell(k,cell1) + relativeSlopeTaperingCell(k,cell2))
        enddo
      enddo
1377
      !$acc end parallel
1378
1379
1380
1381
1382
1383
      !$omp end do

      ! allow disabling of K33 for testing
      if(config_disable_redi_k33) then
        nCells = nCellsArray( size(nCellsArray) )
        !$omp do schedule(runtime)
1384
        !$acc parallel  loop gang
1385
1386
1387
        do iCell = 1, nCells
           k33(:, iCell) = 0.0_RKIND
        end do
1388
        !$acc end parallel
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
        !$omp end do
      end if

      !--------------------------------------------------------------------
      !
      ! Compute stream function and Bolus velocity for Bolus part of GM
      !
      !--------------------------------------------------------------------

      if (config_gm_lat_variable_c2) then
         !$omp do schedule(runtime) private(cell1, cell2, sumN2, ltSum, countN2, BruntVaisalaFreqTopEdge)
         do iEdge = 1, nEdges
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)
            sumN2 = 0.0
            ltSum = 0.0
            countN2 = 0
            
            do k=2,maxLevelEdgeTop(iEdge)

               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
                
               sumN2 = sumN2 + BruntVaisalaFreqTopEdge*layerThicknessEdge(k,iEdge)
               ltSum = ltSum + layerThicknessEdge(k,iEdge)
               countN2 = countN2 +1

            enddo

            if(countN2 > 0) cGMphaseSpeed(iEdge) = max(config_gm_min_phase_speed ,sqrt(sumN2/ltSum)*ltSum / 3.141592_RKIND)

         enddo
         !$omp end do

      else
         !$omp do schedule(runtime)
         do iEdge = 1, nEdges
            cGMphaseSpeed(iEdge) = config_gravWaveSpeed_trunc
         enddo
         !$omp end do
      endif

      !$omp do schedule(runtime)
      do iEdge=1,nEdges
         kappaGM3D(:,iEdge) = gmBolusKappa(iEdge)
      enddo 
      !$omp end do

      if (config_gm_kappa_lat_depth_variable) then

         !$omp do schedule(runtime) private(cell1, cell2, k, BruntVaisalaFreqTopEdge, maxN)
         do iEdge = 1,nEdges
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)

            maxN = -1.0_RKIND
            do k=2,maxLevelEdgeTop(iEdge)
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)

               maxN = max(maxN,BruntVaisalaFreqTopEdge)

            enddo

            do k=2,maxLevelEdgeTop(iEdge)
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)

               kappaGM3D(k,iEdge) = gmBolusKappa(iEdge)*max(config_gm_min_stratification_ratio, &
                       BruntVaisalaFreqTopEdge / (maxN + 1.0E-10_RKIND))
            enddo
         enddo
         !$omp end do
      endif

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime)
      do iEdge = 1, nEdges
         cell1 = cellsOnEdge(1,iEdge)
         cell2 = cellsOnEdge(2,iEdge)

         gmStreamFuncTopOfEdge(:, iEdge) = 0.0_RKIND

         ! Construct the tridiagonal matrix
         if (maxLevelEdgeTop(iEdge) .GE. 3) then
            ! First row
            k = 2
            BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
            BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
            tridiagB(k-1) = - 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / (layerThicknessEdge(k-1,iEdge) &
                          * layerThicknessEdge(k,iEdge)) - BruntVaisalaFreqTopEdge
            tridiagC(k-1) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k, iEdge) &
                          / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
            rightHandSide(k-1) = kappaGM3D(k-1,iEdge) * gravity / rho_sw * gradDensityConstZTopOfEdge(k,iEdge)

            ! Second to next to the last rows
            do k = 3, maxLevelEdgeTop(iEdge)-1
               BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) + BruntVaisalaFreqTop(k,cell2))
               BruntVaisalaFreqTopEdge = max(BruntVaisalaFreqTopEdge, 0.0_RKIND)
               tridiagA(k-2) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k-1, iEdge) &
                             / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
               tridiagB(k-1) = - 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / (layerThicknessEdge(k-1, iEdge) &
                             * layerThicknessEdge(k, iEdge) ) - BruntVaisalaFreqTopEdge
               tridiagC(k-1) = 2.0_RKIND * cGMphaseSpeed(iEdge)**2 / layerThicknessEdge(k, iEdge) &
                             / (layerThicknessEdge(k-1, iEdge) + layerThicknessEdge(k, iEdge))
               rightHandSide(k-1) = kappaGM3D(k-1,iEdge) * gravity / rho_sw * gradDensityConstZTopOfEdge(k,iEdge)
            end do

            ! Last row
            k = maxLevelEdgeTop(iEdge)
            BruntVaisalaFreqTopEdge = 0.5_RKIND * (BruntVaisalaFreqTop(k,cell1) +