pure subroutine hvisc_avg_A_face(ah_face_x, ah_face_y, ah_t, ah_q, nx, ny, nz)
!! Average the per-face harmonic viscosity (`ah_face_x` at
!! u-faces, `ah_face_y` at v-faces) onto the T-cell centres
!! (`ah_t`) and the Bu corners (`ah_q`). The stress form needs A
!! co-located with the tension (T-cell) and shear (corner)
!! strains; the lateral-mix closure produces A at faces, so this
!! is a 4-point face→cell / face→corner reduction. On a uniform A
!! field every average returns A, preserving the Laplacian
!! reduction.
integer, intent(in) :: nx, ny, nz
real(wp), intent(in) :: ah_face_x(nx + 1, ny, nz), ah_face_y(nx, ny + 1, nz)
real(wp), intent(out) :: ah_t(nx, ny, nz), ah_q(nx + 1, ny + 1, nz)
integer :: i, j, k
! T-cell (i,j): mean of its west/east u-faces (i, i+1) and
! south/north v-faces (j, j+1).
do concurrent(k=1:nz, j=1:ny, i=1:nx)
ah_t(i, j, k) = 0.25_wp*((ah_face_x(i, j, k) + ah_face_x(i + 1, j, k)) + &
(ah_face_y(i, j, k) + ah_face_y(i, j + 1, k)))
end do
! Bu corner (i,j) = SW corner of cell (i,j): mean of the two
! u-faces sharing it (i, j) & (i, j-1) and the two v-faces
! (i, j) & (i-1, j). Interior corners only; the divergence
! stencil never reads the outermost corner ring.
do concurrent(k=1:nz, j=2:ny, i=2:nx)
ah_q(i, j, k) = 0.25_wp*((ah_face_x(i, j, k) + ah_face_x(i, j - 1, k)) + &
(ah_face_y(i, j, k) + ah_face_y(i - 1, j, k)))
end do
do concurrent(k=1:nz, j=1:ny + 1)
ah_q(1, j, k) = 0.0_wp
ah_q(nx + 1, j, k) = 0.0_wp
end do
do concurrent(k=1:nz, i=1:nx + 1)
ah_q(i, 1, k) = 0.0_wp
ah_q(i, ny + 1, k) = 0.0_wp
end do
end subroutine hvisc_avg_A_face