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
!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 
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
    !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 
337

338
339
340
341
342
343
      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)
344
345
      !print *, "NEDGES", nEdges
      !print *, "NVERTLEVELS", nvertlevels
346
      !!$acc parallel  loop gang
347
      do iEdge = 1, nEdges
348
         !!$acc loop vector
349
350
351
352
353
354
         do k = 1, nVertLevels
            gradDensityEdge(k, iEdge) = huge(0D0)
            gradZMidEdge(k, iEdge) = huge(0D0)
            normalGMBolusVelocity(k, iEdge) = 0.0_RKIND
         end do
      end do
355
      !!$acc end parallel
356
357
358
      !$omp end do

      !$omp do schedule(runtime) private(k)
359
      !!$acc parallel  loop gang
360
      do iEdge = 1, nEdges
361
         !!$acc loop vector
362
363
364
365
366
367
368
369
         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
370
      !!$acc end parallel
371
372
373
      !$omp end do

      !$omp do schedule(runtime) private(k)
374
      !!$acc parallel  loop gang
375
      do iCell = 1, nCells + 1
376
         !!$acc loop vector
377
378
379
380
381
382
383
         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
384
      !!$acc end parallel
385
386
387
388
389
390
391
392
393
394
395
396
397
398
      !$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)
399
400
      ! verification failure
      !!$acc parallel  loop gang
401
      do iCell = 1, nCells
402
         !!$acc loop vector
403
404
405
406
407
408
409
410
411
412
413
         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
414
      !!$acc end parallel
415
416
417
418
419
420
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate dDensityDzTopOfCell to edge and layer interface
      !$omp do schedule(runtime) private(k, cell1, cell2)
421
      !!$acc parallel  loop gang
422
      do iEdge = 1, nEdges
423
         !!$acc loop vector
424
425
426
427
428
429
         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
430
      !!$acc end parallel
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
      !$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)
446
447
      ! verification failure
      !!$acc parallel  loop gang
448
449
450
451
      do iEdge = 1, nEdges
         cell1 = cellsOnEdge(1,iEdge)
         cell2 = cellsOnEdge(2,iEdge)

452
         !!$acc loop vector
453
454
455
456
457
         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
458
      !!$acc end parallel
459
460
461
462
463
464
      !$omp end do

      nEdges = nEdgesArray( 3 )

      ! Interpolate gradDensityEdge and gradZMidEdge to layer interface
      !$omp do schedule(runtime) private(k, h1, h2)
465
466
      ! verification failure
      !!$acc parallel  loop gang
467
468
469
      do iEdge = 1, nEdges
         ! The interpolation can only be carried out on non-boundary edges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
470
            !!$acc loop vector
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
            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
488
      !!$acc end parallel
489
490
491
492
493
494
495
496
497
498
499
      !$omp end do

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

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime) private(k)
500
      !!$acc parallel  loop gang
501
502
      do iEdge = 1, nEdges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
503
            !!$acc loop vector
504
505
506
507
508
509
            do k = 1, maxLevelEdgeTop(iEdge)+1
               gradDensityConstZTopOfEdge(k,iEdge) = gradDensityTopOfEdge(k,iEdge) - dDensityDzTopOfEdge(k,iEdge) &
                                                   * gradZMidTopOfEdge(k,iEdge)
            end do
         end if
      end do
510
      !!$acc end parallel
511
512
513
514
515
516
517
518
519
520
521
522
523
524
      !$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)
525
      !!$acc parallel  loop gang
526
527
528
529
      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.
530
         !!$acc loop vector
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
         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
546
      !!$acc end parallel
547
548
549
550
551
552
553
      !$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)
554
      !!$acc parallel  loop gang
555
      do iEdge = 1, nEdges
556
         !!$acc loop vector
557
558
559
560
         do k = 1, nVertLevels
            relativeSlopeTopOfEdge(k, iEdge) = max( min( relativeSlopeTopOfEdge(k, iEdge), 1.0_RKIND), -1.0_RKIND)
         end do
      end do
561
      !!$acc end parallel
562
563
564
565
566
567
568
569
      !$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)
570
      !!$acc parallel  loop gang
571
572
573
574
575
576
577
      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)
578
            !!$acc loop vector
579
580
581
582
583
584
585
            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
586
      !!$acc end parallel
587
588
589
590
591
      !$omp end do

      nCells = nCellsArray( 2 )

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

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

      nCells = nCellsArray( size(nCellsArray) )

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

      ! use relativeSlopeTaperingCell as a temporary space for smoothing of relativeSlopeTopOfCell
      relativeSlopeTaperingCell = relativeSlopeTopOfCell
618
      !!$acc parallel  loop gang
619
620
621
622
623
624
625
626
      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
627
           !!$acc loop vector
628
629
630
631
632
633
634
635
636
           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
637
      !!$acc end parallel
638
639
640
641

      nCells = nCellsArray ( 2 )
      ! first, compute tapering across full domain based on a maximum allowable slope
      !$omp do schedule(runtime) private(k)
642
      !!$acc parallel  loop gang
643
      do iCell=1,nCells
644
645
        ! compilation error
        !!$acc loop vector
646
647
648
649
        do k = 1, maxLevelCell(iCell)
          relativeSlopeTaperingCell(k,iCell) = min(1.0_RKIND, config_max_relative_slope / (relativeSlopeTopOfCell(k,iCell)+epsGM))
        end do
      end do
650
      !!$acc end parallel
651
652
653
654
655
656
657
658
      !$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)
659
         !!$acc parallel  loop gang
660
         do iCell=1,nCells
661
662
           ! compilation error
           !!$acc loop vector
663
664
665
666
667
668
669
           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
670
         !!$acc end parallel
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
         !$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)
693
      !!$acc parallel  loop gang
694
695
      do iCell=1,nCells
        k33(:, iCell) = 0.0_RKIND
696
        !!$acc loop vector
697
698
699
700
        do k = 2, maxLevelCell(iCell)
          k33(k,iCell) = ( relativeSlopeTaperingCell(k,iCell) * relativeSlopeTopOfCell(k,iCell) )**2
        end do
      end do
701
      !!$acc end parallel
702
703
704
705
706
707
      !$omp end do

      nEdges = nEdgesArray( 3 )

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

      ! allow disabling of K33 for testing
      if(config_disable_redi_k33) then
        nCells = nCellsArray( size(nCellsArray) )
        !$omp do schedule(runtime)
724
        !!$acc parallel  loop gang
725
726
727
        do iCell = 1, nCells
           k33(:, iCell) = 0.0_RKIND
        end do
728
        !!$acc end parallel
729
730
731
732
733
734
735
736
737
738
739
        !$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)
740
741
         ! compilation error
         !!$acc parallel  loop gang
742
743
744
745
746
747
748
         do iEdge = 1, nEdges
            cell1 = cellsOnEdge(1,iEdge)
            cell2 = cellsOnEdge(2,iEdge)
            sumN2 = 0.0
            ltSum = 0.0
            countN2 = 0
            
749
            !!$acc loop vector
750
751
752
753
754
755
756
757
758
759
760
761
762
763
            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
764
         !!$acc end parallel
765
766
767
768
         !$omp end do

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

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

      if (config_gm_kappa_lat_depth_variable) then

         !$omp do schedule(runtime) private(cell1, cell2, k, BruntVaisalaFreqTopEdge, maxN)
790
791
         ! compilation error
         !!$acc parallel  loop gang
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
         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
813
         !!$acc end parallel
814
815
816
817
818
819
         !$omp end do
      endif

      nEdges = nEdgesArray( 3 )

      !$omp do schedule(runtime)
820
821
      ! compilation error
      !!$acc parallel  loop gang
822
823
824
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
      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
871
      !!$acc end parallel
872
873
874
875
876
      !$omp end do

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

      nCells = nCellsArray( 1 )

      ! Interpolate gmStreamFuncTopOfEdge to cell centers for visualization
      !$omp do schedule(runtime) private(i, iEdge, areaEdge, k, rtmp)
892
893
      ! verification failure
      !!$acc parallel  loop gang
894
895
896
897
898
899
900
      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)

901
            !!$acc loop vector
902
903
904
905
906
907
            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
908
      !!$acc end parallel
909
910
911
      !$omp end do

      !$omp do schedule(runtime)
912
913
      ! execution failture
      !!$acc parallel  loop gang
914
915
916
      do iCell = 1, nCells
         gmStreamFuncTopOfCell(:, iCell) = gmStreamFuncTopOfCell(:,iCell) / areaCell(iCell)
      end do
917
      !!$acc end parallel
Youngsung Kim's avatar
Youngsung Kim committed
918

919
920
      IF (kgen_mainstage) THEN 
            
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
!          !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   
1017
1018
1019
1020
            
#ifdef _MPI 
          call mpi_barrier(mpi_comm_world, kgen_ierr) 
#endif 
1021
1022

      !$acc data copyin(nvertlevels)
1023
1024
1025
            
          CALL SYSTEM_CLOCK(kgen_start_clock, kgen_rate_clock) 
          DO kgen_intvar = 1, KGEN_MAXITER 
1026
1027
1028

      !!$acc data copyin(nvertlevels)

1029
1030
1031
1032
1033
1034
      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)
1035
      !$acc parallel  loop gang
1036
      do iEdge = 1, nEdges
1037
         !$acc loop vector
1038
1039
1040
1041
1042
1043
         do k = 1, nVertLevels
            gradDensityEdge(k, iEdge) = huge(0D0)
            gradZMidEdge(k, iEdge) = huge(0D0)
            normalGMBolusVelocity(k, iEdge) = 0.0_RKIND
         end do
      end do
1044
      !$acc end parallel
1045
1046
1047
      !$omp end do

      !$omp do schedule(runtime) private(k)
1048
      !!$acc parallel  loop gang
1049
      do iEdge = 1, nEdges
1050
         !$acc loop vector
1051
1052
1053
1054
1055
1056
1057
1058
         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
1059
      !!$acc end parallel
1060
1061
1062
      !$omp end do

      !$omp do schedule(runtime) private(k)
1063
      !!$acc parallel  loop gang
1064
      do iCell = 1, nCells + 1
1065
         !$acc loop vector
1066
1067
1068
1069
1070
1071
1072
         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
1073
      !!$acc end parallel
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
1103
1104
1105
      !$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)
1106
      !$acc parallel  loop gang
1107
      do iEdge = 1, nEdges
1108
         !$acc loop vector
1109
1110
1111
1112
1113
1114
         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
1115
      !$acc end parallel
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
1174
1175
1176
      !$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)
1177
      !$acc parallel  loop gang
1178
1179
      do iEdge = 1, nEdges
         if (maxLevelEdgeTop(iEdge) .GE. 1) then
1180
            !$acc loop vector
1181
1182
1183
1184
1185
1186
            do k = 1, maxLevelEdgeTop(iEdge)+1
               gradDensityConstZTopOfEdge(k,iEdge) = gradDensityTopOfEdge(k,iEdge) - dDensityDzTopOfEdge(k,iEdge) &
                                                   * gradZMidTopOfEdge(k,iEdge)
            end do
         end if
      end do
1187
      !$acc end parallel
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
      !$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)
1202
      !$acc parallel  loop gang
1203
1204
1205
1206
      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.
1207
         !$acc loop vector
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
         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
1223
      !$acc end parallel
1224
1225
1226
1227
1228
1229
1230
      !$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)
1231
      !$acc parallel  loop gang
1232
      do iEdge = 1, nEdges
1233
         !$acc loop vector
1234
1235
1236
1237
         do k = 1, nVertLevels
            relativeSlopeTopOfEdge(k, iEdge) = max( min( relativeSlopeTopOfEdge(k, iEdge), 1.0_RKIND), -1.0_RKIND)
         end do
      end do
1238
      !$acc end parallel
1239
1240
1241
1242
1243
1244
1245
1246
      !$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)
1247
      !$acc parallel  loop gang
1248
1249
1250
1251
1252
1253
1254
      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)
1255
            !$acc loop vector
1256
1257
1258
1259
1260
1261
1262
            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
1263
      !$acc end parallel
1264
1265
1266
1267
1268
      !$omp end do

      nCells = nCellsArray( 2 )

      !$omp do schedule(runtime) private(k)
1269
      !$acc parallel  loop gang
1270
      do iCell=1,nCells
1271
        !$acc loop vector
1272
1273
1274
1275
        do k = 1, maxLevelCell(iCell)
           relativeSlopeTopOfCell(k,iCell) = sqrt( relativeSlopeTopOfCell(k,iCell)/areaCellSum(k,iCell) )
        end do
      end do
1276
      !$acc end parallel
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
1353
1354
1355
      !$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)
1356
      !$acc parallel  loop gang
1357
1358
      do iCell=1,nCells
        k33(:, iCell) = 0.0_RKIND
1359
        !$acc loop vector
1360
1361
1362
1363
        do k = 2, maxLevelCell(iCell)
          k33(k,iCell) = ( relativeSlopeTaperingCell(k,iCell) * relativeSlopeTopOfCell(k,iCell) )**2
        end do
      end do
1364
      !$acc end parallel
1365
1366
1367
1368
1369
1370
      !$omp end do

      nEdges = nEdgesArray( 3 )

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

      ! allow disabling of K33 for testing
      if(config_disable_redi_k33) then
        nCells = nCellsArray( size(nCellsArray) )
        !$omp do schedule(runtime)
1387
        !$acc parallel  loop gang
1388
1389
1390
        do iCell = 1, nCells
           k33(:, iCell) = 0.0_RKIND
        end do
1391
        !$acc end parallel
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
1521
1522
1523
        !$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(BruntVaisalaFreqTop