pure subroutine hvisc_ke_diss_impl(nx, ny, nz, u_face, v_face, du_visc, dv_visc, &
rho_layer, h_layer, ke_diss)
!! C-grid KE budget: each face's `u·du_visc` rate is split half to each
!! adjacent T-cell and depth-integrated with `ρ_k h_k`. Race-free —
!! every (i,j) writes only its own `ke_diss(i,j)`.
integer, intent(in) :: nx, ny, nz
real(wp), intent(in) :: u_face(nx + 1, ny, nz), v_face(nx, ny + 1, nz)
real(wp), intent(in) :: du_visc(nx + 1, ny, nz), dv_visc(nx, ny + 1, nz)
real(wp), intent(in) :: rho_layer(nx, ny, nz), h_layer(nx, ny, nz)
real(wp), intent(inout) :: ke_diss(nx, ny)
integer :: i, j, k
real(wp) :: acc, ke
do concurrent(j=1:ny, i=1:nx) local(acc, ke, k)
acc = 0.0_wp
do k = 1, nz
ke = 0.5_wp*(u_face(i, j, k)*du_visc(i, j, k) &
+ u_face(i + 1, j, k)*du_visc(i + 1, j, k)) &
+ 0.5_wp*(v_face(i, j, k)*dv_visc(i, j, k) &
+ v_face(i, j + 1, k)*dv_visc(i, j + 1, k))
acc = acc + rho_layer(i, j, k)*h_layer(i, j, k)*ke
end do
ke_diss(i, j) = acc
end do
end subroutine hvisc_ke_diss_impl