Index: /palm/trunk/SOURCE/turbulence_closure_mod.f90
===================================================================
--- /palm/trunk/SOURCE/turbulence_closure_mod.f90	(revision 3358)
+++ /palm/trunk/SOURCE/turbulence_closure_mod.f90	(revision 3359)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Restructured loops and ifs in production_e to ease vectorization on GPUs
+! 
+! 3300 2018-10-02 14:16:54Z gronemeier
 ! - removed global array wall_flags_0_global, hence reduced accuracy of l_wall
 !   calculation
@@ -2570,4 +2573,5 @@
     INTEGER(iwp) ::  surf_e  !< end index of surface elements at given i-j position
     INTEGER(iwp) ::  surf_s  !< start index of surface elements at given i-j position
+    INTEGER(iwp) ::  flag_nr !< number of masking flag
 
     REAL(wp)     ::  def         !< 
@@ -2584,56 +2588,59 @@
     REAL(wp)     ::  wsvs        !< momentum flux w"v"
 
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dudx  !< Gradient of u-component in x-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dudy  !< Gradient of u-component in y-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dudz  !< Gradient of u-component in z-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dvdx  !< Gradient of v-component in x-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dvdy  !< Gradient of v-component in y-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dvdz  !< Gradient of v-component in z-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dwdx  !< Gradient of w-component in x-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dwdy  !< Gradient of w-component in y-direction
-    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn) ::  dwdz  !< Gradient of w-component in z-direction
-
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dudx  !< Gradient of u-component in x-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dudy  !< Gradient of u-component in y-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dudz  !< Gradient of u-component in z-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dvdx  !< Gradient of v-component in x-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dvdy  !< Gradient of v-component in y-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dvdz  !< Gradient of v-component in z-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dwdx  !< Gradient of w-component in x-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dwdy  !< Gradient of w-component in y-direction
+    REAL(wp), DIMENSION(nzb+1:nzt) ::  dwdz  !< Gradient of w-component in z-direction
+
+
+
+!
+!-- Calculate TKE production by shear. Calculate gradients at all grid
+!-- points first, gradients at surface-bounded grid points will be
+!-- overwritten further below.
     DO  i = nxl, nxr
-
-       IF ( constant_flux_layer )  THEN
-
-!
-!--       Calculate TKE production by shear. Calculate gradients at all grid 
-!--       points first, gradients at surface-bounded grid points will be 
-!--       overwritten further below. 
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-
-                dudx(k,j) =           ( u(k,j,i+1) - u(k,j,i)     ) * ddx
-                dudy(k,j) = 0.25_wp * ( u(k,j+1,i) + u(k,j+1,i+1) -            &
-                                        u(k,j-1,i) - u(k,j-1,i+1) ) * ddy
-                dudz(k,j) = 0.5_wp  * ( u(k+1,j,i) + u(k+1,j,i+1) -            &
-                                        u(k-1,j,i) - u(k-1,j,i+1) ) *          &
-                                                         dd2zu(k)
-   
-                dvdx(k,j) = 0.25_wp * ( v(k,j,i+1) + v(k,j+1,i+1) -            &
-                                        v(k,j,i-1) - v(k,j+1,i-1) ) * ddx
-                dvdy(k,j) =           ( v(k,j+1,i) - v(k,j,i)     ) * ddy
-                dvdz(k,j) = 0.5_wp  * ( v(k+1,j,i) + v(k+1,j+1,i) -            &
-                                        v(k-1,j,i) - v(k-1,j+1,i) ) *          &
-                                                         dd2zu(k)
-
-                dwdx(k,j) = 0.25_wp * ( w(k,j,i+1) + w(k-1,j,i+1) -            &
-                                        w(k,j,i-1) - w(k-1,j,i-1) ) * ddx
-                dwdy(k,j) = 0.25_wp * ( w(k,j+1,i) + w(k-1,j+1,i) -            &
-                                        w(k,j-1,i) - w(k-1,j-1,i) ) * ddy
-                dwdz(k,j) =           ( w(k,j,i)   - w(k-1,j,i)   ) * ddzw(k)
-
-             ENDDO
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+
+             dudx(k) =           ( u(k,j,i+1) - u(k,j,i)     ) * ddx
+             dudy(k) = 0.25_wp * ( u(k,j+1,i) + u(k,j+1,i+1) -                 &
+                                   u(k,j-1,i) - u(k,j-1,i+1) ) * ddy
+             dudz(k) = 0.5_wp  * ( u(k+1,j,i) + u(k+1,j,i+1) -                 &
+                                   u(k-1,j,i) - u(k-1,j,i+1) ) * dd2zu(k)
+
+             dvdx(k) = 0.25_wp * ( v(k,j,i+1) + v(k,j+1,i+1) -                 &
+                                   v(k,j,i-1) - v(k,j+1,i-1) ) * ddx
+             dvdy(k) =           ( v(k,j+1,i) - v(k,j,i)     ) * ddy
+             dvdz(k) = 0.5_wp  * ( v(k+1,j,i) + v(k+1,j+1,i) -                 &
+                                     v(k-1,j,i) - v(k-1,j+1,i) ) * dd2zu(k)
+
+             dwdx(k) = 0.25_wp * ( w(k,j,i+1) + w(k-1,j,i+1) -                 &
+                                   w(k,j,i-1) - w(k-1,j,i-1) ) * ddx
+             dwdy(k) = 0.25_wp * ( w(k,j+1,i) + w(k-1,j+1,i) -                 &
+                                   w(k,j-1,i) - w(k-1,j-1,i) ) * ddy
+             dwdz(k) =           ( w(k,j,i)   - w(k-1,j,i)   ) * ddzw(k)
+
           ENDDO
 
-!
-!--       Position beneath wall
-!--       (2) - Will allways be executed.
-!--       'bottom and wall: use u_0,v_0 and wall functions'
-          DO  j = nys, nyn
+
+          flag_nr = 29
+
+
+          IF ( constant_flux_layer )  THEN
+!
+
+             flag_nr = 0
+
+!--          Position beneath wall
+!--          (2) - Will allways be executed.
+!--          'bottom and wall: use u_0,v_0 and wall functions'
 !
 !--          Compute gradients at north- and south-facing surfaces.
-!--          First, for default surfaces, then for urban surfaces. 
+!--          First, for default surfaces, then for urban surfaces.
 !--          Note, so far no natural vertical surfaces implemented
              DO  l = 0, 1
@@ -2644,5 +2651,5 @@
                    usvs        = surf_def_v(l)%mom_flux_tke(0,m)
                    wsvs        = surf_def_v(l)%mom_flux_tke(1,m)
-   
+
                    km_neutral = kappa * ( usvs**2 + wsvs**2 )**0.25_wp         &
                                    * 0.5_wp * dy
@@ -2650,7 +2657,7 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j-1,i), 0 ) ) 
-                   dudy(k,j) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
-                   dwdy(k,j) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
+                                     BTEST( wall_flags_0(k,j-1,i), flag_nr ) )
+                   dudy(k) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
+                   dwdy(k) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
                 ENDDO
 !
@@ -2662,5 +2669,5 @@
                    usvs        = surf_lsm_v(l)%mom_flux_tke(0,m)
                    wsvs        = surf_lsm_v(l)%mom_flux_tke(1,m)
-   
+
                    km_neutral = kappa * ( usvs**2 + wsvs**2 )**0.25_wp         &
                                    * 0.5_wp * dy
@@ -2668,8 +2675,8 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j-1,i), 0 ) ) 
-                   dudy(k,j) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
-                   dwdy(k,j) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
-                ENDDO 
+                                     BTEST( wall_flags_0(k,j-1,i), flag_nr ) )
+                   dudy(k) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
+                   dwdy(k) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
+                ENDDO
 !
 !--             Urban surfaces
@@ -2680,5 +2687,5 @@
                    usvs        = surf_usm_v(l)%mom_flux_tke(0,m)
                    wsvs        = surf_usm_v(l)%mom_flux_tke(1,m)
-   
+
                    km_neutral = kappa * ( usvs**2 + wsvs**2 )**0.25_wp         &
                                    * 0.5_wp * dy
@@ -2686,8 +2693,8 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j-1,i), 0 ) ) 
-                   dudy(k,j) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
-                   dwdy(k,j) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
-                ENDDO  
+                                     BTEST( wall_flags_0(k,j-1,i), flag_nr ) )
+                   dudy(k) = sign_dir * usvs / ( km_neutral + 1E-10_wp )
+                   dwdy(k) = sign_dir * wsvs / ( km_neutral + 1E-10_wp )
+                ENDDO
              ENDDO
 !
@@ -2706,10 +2713,10 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j,i-1), 0 ) ) 
-                   dvdx(k,j) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
-                   dwdx(k,j) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
-                ENDDO 
-!
-!--             Natural surfaces                   
+                                     BTEST( wall_flags_0(k,j,i-1), flag_nr ) )
+                   dvdx(k) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
+                   dwdx(k) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
+                ENDDO
+!
+!--             Natural surfaces
                 surf_s = surf_lsm_v(l)%start_index(j,i)
                 surf_e = surf_lsm_v(l)%end_index(j,i)
@@ -2724,10 +2731,10 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j,i-1), 0 ) ) 
-                   dvdx(k,j) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
-                   dwdx(k,j) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
-                ENDDO   
-!
-!--             Urban surfaces                   
+                                     BTEST( wall_flags_0(k,j,i-1), flag_nr ) )
+                   dvdx(k) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
+                   dwdx(k) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
+                ENDDO
+!
+!--             Urban surfaces
                 surf_s = surf_usm_v(l)%start_index(j,i)
                 surf_e = surf_usm_v(l)%end_index(j,i)
@@ -2742,8 +2749,8 @@
 !--                -1.0 for right-facing wall, 1.0 for left-facing wall
                    sign_dir = MERGE( 1.0_wp, -1.0_wp,                          &
-                                     BTEST( wall_flags_0(k,j,i-1), 0 ) ) 
-                   dvdx(k,j) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
-                   dwdx(k,j) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
-                ENDDO 
+                                     BTEST( wall_flags_0(k,j,i-1), flag_nr ) )
+                   dvdx(k) = sign_dir * vsus / ( km_neutral + 1E-10_wp )
+                   dwdx(k) = sign_dir * wsus / ( km_neutral + 1E-10_wp )
+                ENDDO
              ENDDO
 !
@@ -2754,12 +2761,12 @@
                 k = surf_def_h(0)%k(m)
 !
-!--             Please note, actually, an interpolation of u_0 and v_0 
-!--             onto the grid center would be required. However, this 
+!--             Please note, actually, an interpolation of u_0 and v_0
+!--             onto the grid center would be required. However, this
 !--             would require several data transfers between 2D-grid and
-!--             wall type. The effect of this missing interpolation is 
+!--             wall type. The effect of this missing interpolation is
 !--             negligible. (See also production_e_init).
-                dudz(k,j) = ( u(k+1,j,i) - surf_def_h(0)%u_0(m) ) * dd2zu(k)   
-                dvdz(k,j) = ( v(k+1,j,i) - surf_def_h(0)%v_0(m) ) * dd2zu(k)
-         
+                dudz(k) = ( u(k+1,j,i) - surf_def_h(0)%u_0(m) ) * dd2zu(k)
+                dvdz(k) = ( v(k+1,j,i) - surf_def_h(0)%v_0(m) ) * dd2zu(k)
+
              ENDDO
 !
@@ -2770,7 +2777,7 @@
                 k = surf_lsm_h%k(m)
 
-                dudz(k,j) = ( u(k+1,j,i) - surf_lsm_h%u_0(m) ) * dd2zu(k)   
-                dvdz(k,j) = ( v(k+1,j,i) - surf_lsm_h%v_0(m) ) * dd2zu(k)
-         
+                dudz(k) = ( u(k+1,j,i) - surf_lsm_h%u_0(m) ) * dd2zu(k)
+                dvdz(k) = ( v(k+1,j,i) - surf_lsm_h%v_0(m) ) * dd2zu(k)
+
              ENDDO
 !
@@ -2781,10 +2788,10 @@
                 k = surf_usm_h%k(m)
 
-                dudz(k,j) = ( u(k+1,j,i) - surf_usm_h%u_0(m) ) * dd2zu(k)   
-                dvdz(k,j) = ( v(k+1,j,i) - surf_usm_h%v_0(m) ) * dd2zu(k)
-         
+                dudz(k) = ( u(k+1,j,i) - surf_usm_h%u_0(m) ) * dd2zu(k)
+                dvdz(k) = ( v(k+1,j,i) - surf_usm_h%v_0(m) ) * dd2zu(k)
+
              ENDDO
 !
-!--          Compute gradients at downward-facing walls, only for 
+!--          Compute gradients at downward-facing walls, only for
 !--          non-natural default surfaces
              surf_s = surf_def_h(1)%start_index(j,i)
@@ -2793,89 +2800,44 @@
                 k = surf_def_h(1)%k(m)
 
-                dudz(k,j) = ( surf_def_h(1)%u_0(m) - u(k-1,j,i) ) * dd2zu(k)   
-                dvdz(k,j) = ( surf_def_h(1)%v_0(m) - v(k-1,j,i) ) * dd2zu(k)
+                dudz(k) = ( surf_def_h(1)%u_0(m) - u(k-1,j,i) ) * dd2zu(k)
+                dvdz(k) = ( surf_def_h(1)%v_0(m) - v(k-1,j,i) ) * dd2zu(k)
 
              ENDDO
+
+
+          ENDIF
+
+
+          DO  k = nzb+1, nzt
+
+             def = 2.0_wp * ( dudx(k)**2 + dvdy(k)**2 + dwdz(k)**2 ) +         &
+                              dudy(k)**2 + dvdx(k)**2 + dwdx(k)**2 +           &
+                              dwdy(k)**2 + dudz(k)**2 + dvdz(k)**2 +           &
+                   2.0_wp * ( dvdx(k)*dudy(k) + dwdx(k)*dudz(k) +              &
+                              dwdy(k)*dvdz(k) )
+
+             IF ( def < 0.0_wp )  def = 0.0_wp
+
+             flag = MERGE( 1.0_wp, 0.0_wp, BTEST(wall_flags_0(k,j,i),flag_nr) )
+
+             tend(k,j,i) = tend(k,j,i) + km(k,j,i) * def * flag
+
           ENDDO
 
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-
-                def = 2.0_wp * ( dudx(k,j)**2 + dvdy(k,j)**2 + dwdz(k,j)**2 ) + &
-                                 dudy(k,j)**2 + dvdx(k,j)**2 + dwdx(k,j)**2 +   &
-                                 dwdy(k,j)**2 + dudz(k,j)**2 + dvdz(k,j)**2 +   &
-                      2.0_wp * ( dvdx(k,j)*dudy(k,j) + dwdx(k,j)*dudz(k,j)  +   &
-                                 dwdy(k,j)*dvdz(k,j) )
-
-                IF ( def < 0.0_wp )  def = 0.0_wp
-
-                flag  = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0 ) ) 
-
-                tend(k,j,i) = tend(k,j,i) + km(k,j,i) * def * flag
-
-             ENDDO
-          ENDDO
-
-       ELSE
-
-          DO  j = nys, nyn
-!
-!--          Calculate TKE production by shear. Here, no additional
-!--          wall-bounded code is considered. 
-!--          Why?
-             DO  k = nzb+1, nzt
-
-                dudx(k,j)  =           ( u(k,j,i+1) - u(k,j,i)     ) * ddx
-                dudy(k,j)  = 0.25_wp * ( u(k,j+1,i) + u(k,j+1,i+1) -           &
-                                         u(k,j-1,i) - u(k,j-1,i+1) ) * ddy
-                dudz(k,j)  = 0.5_wp  * ( u(k+1,j,i) + u(k+1,j,i+1) -           &
-                                         u(k-1,j,i) - u(k-1,j,i+1) ) *         &
-                                                                     dd2zu(k)
-
-                dvdx(k,j)  = 0.25_wp * ( v(k,j,i+1) + v(k,j+1,i+1) -           &
-                                         v(k,j,i-1) - v(k,j+1,i-1) ) * ddx
-                dvdy(k,j)  =           ( v(k,j+1,i) - v(k,j,i)     ) * ddy
-                dvdz(k,j)  = 0.5_wp  * ( v(k+1,j,i) + v(k+1,j+1,i) -           &
-                                         v(k-1,j,i) - v(k-1,j+1,i) ) *         &
-                                                                     dd2zu(k)
-
-                dwdx(k,j)  = 0.25_wp * ( w(k,j,i+1) + w(k-1,j,i+1) -           &
-                                         w(k,j,i-1) - w(k-1,j,i-1) ) * ddx
-                dwdy(k,j)  = 0.25_wp * ( w(k,j+1,i) + w(k-1,j+1,i) -           &
-                                         w(k,j-1,i) - w(k-1,j-1,i) ) * ddy
-                dwdz(k,j)  =           ( w(k,j,i)   - w(k-1,j,i)   ) *         &
-                                                                     ddzw(k)
-   
-                def = 2.0_wp * (                                               &
-                             dudx(k,j)**2 + dvdy(k,j)**2 + dwdz(k,j)**2        &
-                               ) +                                             &
-                             dudy(k,j)**2 + dvdx(k,j)**2 + dwdx(k,j)**2 +      &
-                             dwdy(k,j)**2 + dudz(k,j)**2 + dvdz(k,j)**2 +      &
-                      2.0_wp * (                                               &
-                             dvdx(k,j)*dudy(k,j) + dwdx(k,j)*dudz(k,j)  +      &
-                             dwdy(k,j)*dvdz(k,j)                               &
-                               )
-
-                IF ( def < 0.0_wp )  def = 0.0_wp
-
-                flag  = MERGE( 1.0_wp, 0.0_wp,                                 &
-                               BTEST( wall_flags_0(k,j,i), 29 ) ) 
-                tend(k,j,i) = tend(k,j,i) + km(k,j,i) * def * flag
-   
-             ENDDO
-          ENDDO
-
-       ENDIF
-
-!
-!--    If required, calculate TKE production by buoyancy
-       IF ( .NOT. neutral )  THEN
-
-          IF ( .NOT. humidity )  THEN
-
-             IF ( ocean_mode )  THEN
-!
-!--             So far in the ocean no special treatment of density flux
-!--             in the bottom and top surface layer
+
+       ENDDO
+    ENDDO
+
+!
+!-- If required, calculate TKE production by buoyancy
+    IF ( .NOT. neutral )  THEN
+
+       IF ( .NOT. humidity )  THEN
+
+          IF ( ocean_mode )  THEN
+!
+!--          So far in the ocean no special treatment of density flux
+!--          in the bottom and top surface layer
+             DO  i = nxl, nxr
                 DO  j = nys, nyn
                    DO  k = nzb+1, nzt
@@ -2891,5 +2853,5 @@
                                 MERGE( 1.0_wp, 0.0_wp,                         &
                                        BTEST( wall_flags_0(k,j,i), 9 )         &
-                                     )  
+                                     )
                    ENDDO
 !
@@ -2921,13 +2883,16 @@
                                              use_single_reference_value ) *    &
                                       drho_air_zw(k) *                         &
-                                      surf_def_h(2)%shf(m) 
+                                      surf_def_h(2)%shf(m)
                       ENDDO
                    ENDIF
 
                 ENDDO
-
-             ELSE
-
+             ENDDO
+
+          ELSE ! or IF ( .NOT. ocean_mode )  THEN
+
+             DO  i = nxl, nxr
                 DO  j = nys, nyn
+
                    DO  k = nzb+1, nzt
 !
@@ -2945,5 +2910,5 @@
                                 MERGE( 1.0_wp, 0.0_wp,                         &
                                        BTEST( wall_flags_0(k,j,i), 9 )         &
-                                     )  
+                                     )
                    ENDDO
 
@@ -2960,6 +2925,6 @@
                                         use_single_reference_value )           &
                                                    * drho_air_zw(k-1)          &
-                                                   * surf_def_h(l)%shf(m)       
-                         ENDDO      
+                                                   * surf_def_h(l)%shf(m)
+                         ENDDO
                       ENDDO
 !
@@ -2973,5 +2938,5 @@
                                         use_single_reference_value )           &
                                                    * drho_air_zw(k-1)          &
-                                                   * surf_lsm_h%shf(m)   
+                                                   * surf_lsm_h%shf(m)
                       ENDDO
 !
@@ -2985,6 +2950,6 @@
                                         use_single_reference_value )           &
                                                    * drho_air_zw(k-1)          &
-                                                   * surf_usm_h%shf(m)   
-                      ENDDO                          
+                                                   * surf_usm_h%shf(m)
+                      ENDDO
                    ENDIF
 
@@ -2998,13 +2963,16 @@
                                         use_single_reference_value )           &
                                                    * drho_air_zw(k)            &
-                                                   * surf_def_h(2)%shf(m) 
+                                                   * surf_def_h(2)%shf(m)
                       ENDDO
                    ENDIF
+
                 ENDDO
-
-             ENDIF
-
-          ELSE
-
+             ENDDO
+
+          ENDIF ! from IF ( .NOT. ocean_mode )
+
+       ELSE ! or IF ( humidity )  THEN
+
+          DO  i = nxl, nxr
              DO  j = nys, nyn
 
@@ -3059,5 +3027,5 @@
                       k1 = 1.0_wp + 0.61_wp * q(k,j,i) - ql(k,j,i)
                       k2 = 0.61_wp * pt(k,j,i)
-                      tend(k,j,i) = tend(k,j,i) -                              & 
+                      tend(k,j,i) = tend(k,j,i) -                              &
                                     kh(k,j,i) * g /                            &
                                  MERGE( vpt_reference, vpt(k,j,i),             &
@@ -3077,9 +3045,5 @@
                 ENDDO
 
-             ENDDO
-
-             IF ( use_surface_fluxes )  THEN
-
-                DO  j = nys, nyn
+                IF ( use_surface_fluxes )  THEN
 !
 !--                Treat horizontal default surfaces
@@ -3161,5 +3125,5 @@
                    surf_e = surf_usm_h%end_index(j,i)
                    DO  m = surf_s, surf_e
-                      k = surf_usm_h%k(m)
+                      k = surf_lsm_h%k(m)
 
                       IF ( .NOT. bulk_cloud_model .AND. .NOT. cloud_droplets ) THEN
@@ -3193,11 +3157,7 @@
                    ENDDO
 
-                ENDDO
-
-             ENDIF
-
-             IF ( use_top_fluxes )  THEN
-
-                DO  j = nys, nyn
+                ENDIF ! from IF ( use_surface_fluxes )  THEN
+
+                IF ( use_top_fluxes )  THEN
 
                    surf_s = surf_def_h(2)%start_index(j,i)
@@ -3237,13 +3197,12 @@
                    ENDDO
 
-                ENDDO
-
-             ENDIF
-
-          ENDIF
+                ENDIF ! from IF ( use_top_fluxes )  THEN
+
+             ENDDO
+          ENDDO
 
        ENDIF
 
-    ENDDO
+    ENDIF
 
  END SUBROUTINE production_e
