subroutine ocean_sponge_apply_maps(grid, sp, ms, dt)
!! Map-driven sponge dispatch (`&ocean_sponge_nml enable=.true.`).
!! Relaxes momentum toward `u_ref`/`v_ref` (when `relax_uv`) and every
!! registered tracer toward `ref_tracer` (when `relax_tracers`),
!! mirroring the S/T tracer relaxation into `ms%salt_budget_sponge` /
!! `ms%heat_budget_sponge` so the console salt/heat budget can close
!! with the sponge on (`rdb_ocean_console_stats::ocean_heat_src_sum` /
!! `ocean_salt_src_sum`). No-op when `.not. sp%is_init .or. .not.
!! sp%enable`. Run from the same slot as the legacy path — see
!! `rdb_ocean_dyn::run_stage_split`'s dispatch (exactly one of the
!! legacy band / map-driven path runs).
type(hgrid_t), intent(in) :: grid
type(ocean_sponge_t), intent(in) :: sp
type(multilayer_state_t), intent(inout) :: ms
real(wp), intent(in) :: dt
integer :: nx, ny, nz, it
if (.not. sp%is_init .or. .not. sp%enable) return
nx = grid%nx_total
ny = grid%ny_total
nz = ms%nz_ml
if (sp%relax_uv) then
call relax_map_u_impl(ms%u_face_x_layer, sp%u_ref, sp%idamp_u, nx + 1, ny, nz, dt)
call relax_map_v_impl(ms%v_face_y_layer, sp%v_ref, sp%idamp_v, nx, ny + 1, nz, dt)
end if
if (sp%relax_tracers .and. allocated(ms%tracers)) then
! Outer-shim: the per-tracer `it` loop stays outside the `do
! concurrent` kernels (§6.4 of the plan) — `ms%tracers(it)%hTr` is
! dereferenced here on the host, never inside the device loop.
do it = 1, min(size(ms%tracers), sp%n_tracers)
if (.not. allocated(ms%tracers(it)%hTr)) cycle
if (it == ms%idx_salinity) then
call relax_map_tracer_budget_impl(ms%tracers(it)%hTr, ms%h_layer, &
sp%ref_tracer, it, sp%n_tracers, &
sp%idamp_h, ms%salt_budget_sponge, &
nx, ny, nz, dt)
else if (it == ms%idx_temperature) then
call relax_map_tracer_budget_impl(ms%tracers(it)%hTr, ms%h_layer, &
sp%ref_tracer, it, sp%n_tracers, &
sp%idamp_h, ms%heat_budget_sponge, &
nx, ny, nz, dt)
else
call relax_map_tracer_impl(ms%tracers(it)%hTr, ms%h_layer, &
sp%ref_tracer, it, sp%n_tracers, &
sp%idamp_h, nx, ny, nz, dt)
end if
end do
end if
end subroutine ocean_sponge_apply_maps