pure subroutine vmix_guard_impl(nx, ny, nzp1, kv, kt, ks, bad_count)
!! Count negative or NaN diffusivities across interior interfaces.
!! A reduction over fresh scratch — uses `!$acc parallel loop
!! reduction` (the project rule for device reductions; bare
!! `count`/`sum` over device data can silently return 0).
integer, intent(in) :: nx, ny, nzp1
real(wp), intent(in) :: kv(nx, ny, nzp1)
real(wp), intent(in) :: kt(nx, ny, nzp1)
real(wp), intent(in) :: ks(nx, ny, nzp1)
integer, intent(out) :: bad_count
integer :: i, j, k
bad_count = 0
do concurrent(k=2:nzp1 - 1, j=1:ny, i=1:nx) reduce(+:bad_count)
if (kv(i, j, k) < 0.0_wp .or. kv(i, j, k) /= kv(i, j, k)) bad_count = bad_count + 1
if (kt(i, j, k) < 0.0_wp .or. kt(i, j, k) /= kt(i, j, k)) bad_count = bad_count + 1
if (ks(i, j, k) < 0.0_wp .or. ks(i, j, k) /= ks(i, j, k)) bad_count = bad_count + 1
end do
end subroutine vmix_guard_impl