subroutine visc_rem_halo_refresh(grid, bt_work, bc)
!! Exchange `bt_work%visc_rem_u/v` face halos right after
!! production — MOM6's `pass_visc_rem` group pass, run after
!! every one of the three `vertvisc_remnant` calls. MPI
!! halo first, then the periodic wrap, then the tripolar fold —
!! the same ordering contract every other seam fill in this module
!! follows (`ocean_halo_exchange_ml_state` then
!! `ocean_periodic_wrap_state` then `ocean_fold_wrap_state`).
!! `visc_rem` is a POSITIVE SCALAR on a face (the viscous-remnant
!! fraction), not a true-vector flux component, so the fold uses
!! `ocean_fold_wrap_visc_rem` (copy across the seam), NOT
!! `ocean_fold_wrap_stress`'s negate-on-fold vector contract.
!! `bc` absent (e.g. a direct unit-test call with no boundary
!! state) ⇒ periodic wrap + fold are skipped; the halo exchange
!! itself is unconditional (no-op on 1 rank, D0).
type(hgrid_t), intent(in) :: grid
type(barotropic_workstate_t), intent(inout) :: bt_work
type(ocean_bc_state_t), intent(in), optional :: bc
integer :: nz
nz = size(bt_work%visc_rem_u, 3)
! GATED on an actually-decomposed axis (mirrors the `u_av_layer`/
! `v_av_layer` seam fill in `run_stage_split`): the halo specifics
! take EXPLICIT-SHAPE dummies sized from the comm module's own
! `oh_nx_total`/`oh_ny_total`, which a direct unit-test call that
! never runs `ocean_halo_init` leaves at 0 — a mis-shaped device
! dummy, not a benign no-op, on the GPU build.
if (ocean_halo_is_decomposed_x() .or. ocean_halo_is_decomposed_y()) then
call ocean_halo_face_x(bt_work%visc_rem_u, nz)
call ocean_halo_face_y(bt_work%visc_rem_v, nz)
end if
if (.not. present(bc)) return
if (bc%periodic_x .or. bc%periodic_y) then
call ocean_periodic_wrap_face_x_3d( &
bt_work%visc_rem_u, size(bt_work%visc_rem_u, 1), size(bt_work%visc_rem_u, 2), &
nz, grid%nx_phys, grid%ny_phys, grid%nghost, &
bc%periodic_x .and. .not. ocean_halo_is_decomposed_x(), &
bc%periodic_y .and. .not. ocean_halo_is_decomposed_y())
call ocean_periodic_wrap_face_y_3d( &
bt_work%visc_rem_v, size(bt_work%visc_rem_v, 1), size(bt_work%visc_rem_v, 2), &
nz, grid%nx_phys, grid%ny_phys, grid%nghost, &
bc%periodic_x .and. .not. ocean_halo_is_decomposed_x(), &
bc%periodic_y .and. .not. ocean_halo_is_decomposed_y())
end if
! Tripolar north-fold seam — periodic-FIRST-fold-SECOND, as above.
if (bc%north_fold) call ocean_fold_wrap_visc_rem(grid, bc, bt_work%visc_rem_u, bt_work%visc_rem_v)
end subroutine visc_rem_halo_refresh