pure subroutine ocean_munk_worst_case(cfg, metrics, grid, nu_h, ratio_min, beta_at, dx_at, j_at)
!! Worst-case (smallest) `delta_M/dx` ratio over the physical
!! domain, honouring a latitude-varying `beta` under
!! `coriolis_scheme='planetary'` on a non-Cartesian grid (`beta =
!! 2*omega*cos(lat)/R`, maximal — hence `delta_M` MINIMAL, the
!! worst case — at the most equatorward row) and a constant `beta`
!! (`&ocean_topo_nml coriolis_beta`) everywhere else. `ratio_min` is
!! `huge(1.0_wp)` (no constraint) when `beta<=0` everywhere (an
!! f-plane run has no Munk boundary layer to resolve).
type(config_t), intent(in) :: cfg
type(ocean_metrics_t), intent(in) :: metrics
type(hgrid_t), intent(in) :: grid
real(wp), intent(in) :: nu_h
real(wp), intent(out) :: ratio_min, beta_at, dx_at
integer, intent(out) :: j_at
real(wp), parameter :: DEG2RAD = PI/180.0_wp
logical :: planetary
integer :: i, j, ng, i0, i1, j0, j1
real(wp) :: beta_ij, dx_ij, delta_ij, ratio_ij
planetary = (parse_grid_config(cfg%ocean%grid%grid_config) /= GRID_CONFIG_CARTESIAN &
.and. parse_coriolis_scheme(cfg%ocean%grid%coriolis_scheme) == CORIOLIS_SCHEME_PLANETARY)
ratio_min = huge(1.0_wp)
beta_at = 0.0_wp
dx_at = 0.0_wp
j_at = 0
if (nu_h <= 0.0_wp) return
ng = grid%nghost
i0 = ng + 1
i1 = ng + grid%nx_phys
j0 = ng + 1
j1 = ng + grid%ny_phys
if (.not. planetary) then
! Uniform beta (beta_plane, or the f-plane beta<=0 no-op).
beta_ij = cfg%ocean%topo%coriolis_beta
if (beta_ij <= 0.0_wp) return
do j = j0, j1
do i = i0, i1
dx_ij = min(metrics%dxT(i, j), metrics%dyT(i, j))
delta_ij = ocean_munk_delta_m(nu_h, beta_ij)
ratio_ij = delta_ij/dx_ij
if (ratio_ij < ratio_min) then
ratio_min = ratio_ij
beta_at = beta_ij
dx_at = dx_ij
j_at = j
end if
end do
end do
else
do j = j0, j1
do i = i0, i1
beta_ij = 2.0_wp*cfg%ocean%grid%omega*cos(metrics%geolatT(i, j)*DEG2RAD)/ &
cfg%ocean%grid%rad_earth
if (beta_ij <= 0.0_wp) cycle
dx_ij = min(metrics%dxT(i, j), metrics%dyT(i, j))
delta_ij = ocean_munk_delta_m(nu_h, beta_ij)
ratio_ij = delta_ij/dx_ij
if (ratio_ij < ratio_min) then
ratio_min = ratio_ij
beta_at = beta_ij
dx_at = dx_ij
j_at = j
end if
end do
end do
end if
end subroutine ocean_munk_worst_case