Skip to content

Commit 7e7d994

Browse files
committed
+Refactor homogenize_field and revise its interface
Refactored the homogenize_field routine in MOM_horizontal_regridding to make use of the unscale argument to reproducing_sum(), and revised its interface to make it more nearly consistent with the interface to homogenize_field_t() in MOM_forcing_type. The interface changes include revising the order of the arguments, making the weight argument options, replacing the scale argument with an optional tmp_scale argument that is the inverse of the previous scale, and making the default for the use of reproducing sums to be true when the answer_date argument is absent. The two homogenize_field routines now give equivalent behavior when none of the optional arguments to homogenize_field() are absent. The homogenize_field calls in MOM_temp_salt_initialize_from_Z() and the horiz_interp_and_extrap_tracer() routines have been modified in accordance with the interface changes. All answers are bitwise identical, but the interface to a publicly visible routine has been substantially changed to the point where any calls using the previous interface will not compile.
1 parent 5ccb388 commit 7e7d994

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/framework/MOM_horizontal_regridding.F90

+32-24
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ subroutine horiz_interp_and_extrap_tracer_record(filename, varnam, recnum, G, tr
591591

592592
! Horizontally homogenize data to produce perfectly "flat" initial conditions
593593
if (PRESENT(homogenize)) then ; if (homogenize) then
594-
call homogenize_field(tr_out, mask_out, G, scale, answer_date)
594+
call homogenize_field(tr_out, G, tmp_scale=I_scale, weights=mask_out, answer_date=answer_date)
595595
endif ; endif
596596

597597
! tr_out contains input z-space data on the model grid with missing values
@@ -908,7 +908,7 @@ subroutine horiz_interp_and_extrap_tracer_fms_id(field, Time, G, tr_z, mask_z, &
908908

909909
! Horizontally homogenize data to produce perfectly "flat" initial conditions
910910
if (PRESENT(homogenize)) then ; if (homogenize) then
911-
call homogenize_field(tr_out, mask_out, G, scale, answer_date)
911+
call homogenize_field(tr_out, G, tmp_scale=I_scale, weights=mask_out, answer_date=answer_date)
912912
endif ; endif
913913

914914
! tr_out contains input z-space data on the model grid with missing values
@@ -950,14 +950,15 @@ subroutine horiz_interp_and_extrap_tracer_fms_id(field, Time, G, tr_z, mask_z, &
950950
end subroutine horiz_interp_and_extrap_tracer_fms_id
951951

952952
!> Replace all values of a 2-d field with the weighted average over the valid points.
953-
subroutine homogenize_field(field, weight, G, scale, answer_date, wt_unscale)
953+
subroutine homogenize_field(field, G, tmp_scale, weights, answer_date, wt_unscale)
954954
type(ocean_grid_type), intent(inout) :: G !< Ocean grid type
955955
real, dimension(SZI_(G),SZJ_(G)), intent(inout) :: field !< The tracer on the model grid in arbitrary units [A ~> a]
956-
real, dimension(SZI_(G),SZJ_(G)), intent(in) :: weight !< The weights for the tracer in arbitrary units that
956+
real, optional, intent(in) :: tmp_scale !< A temporary rescaling factor for the
957+
!! variable that is reversed in the
958+
!! return value [a A-1 ~> 1]
959+
real, dimension(SZI_(G),SZJ_(G)), &
960+
optional, intent(in) :: weights !< The weights for the tracer in arbitrary units that
957961
!! typically differ from those used by field [B ~> b]
958-
real, intent(in) :: scale !< A rescaling factor that has been used for the
959-
!! variable and has to be undone before the
960-
!! reproducing sums [A a-1 ~> 1]
961962
integer, optional, intent(in) :: answer_date !< The vintage of the expressions in the code.
962963
!! Dates before 20230101 use non-reproducing sums
963964
!! in their averages, while later versions use
@@ -971,12 +972,11 @@ subroutine homogenize_field(field, weight, G, scale, answer_date, wt_unscale)
971972
! In the following comments, [A] and [B] are used to indicate the arbitrary, possibly rescaled
972973
! units of the input field and the weighting array, while [a] and [b] indicate the corresponding
973974
! unscaled (e.g., mks) units that can be used with the reproducing sums
974-
real, dimension(SZI_(G),SZJ_(G)) :: field_for_Sums ! The field times the weights with the scaling undone [a b]
975-
real, dimension(SZI_(G),SZJ_(G)) :: wts_for_Sums ! A copy of the wieghts with the scaling undone [b]
975+
real, dimension(G%isc:G%iec, G%jsc:G%jec) :: field_for_Sums ! The field times the weights [A B ~> a b]
976+
real, dimension(G%isc:G%iec, G%jsc:G%jec) :: weight ! A copy of weights, if it is present, or the
977+
! tracer-point grid mask if it weights is absent [B ~> b]
976978
real :: var_unscale ! The reciprocal of the scaling factor for the field and weights [a b A-1 B-1 ~> 1]
977-
real :: wt_descale ! A factor that undoes any dimensional scaling of the weights so that they
978-
! can be used with reproducing sums [b B-1 ~> 1]
979-
real :: wt_sum ! The sum of the weights, in [b] (reproducing) or [B ~> b] (non-reproducing)
979+
real :: wt_sum ! The sum of the weights, in [B ~> b]
980980
real :: varsum ! The weighted sum of field being averaged [A B ~> a b]
981981
real :: varAvg ! The average of the field [A ~> a]
982982
logical :: use_repro_sums ! If true, use reproducing sums.
@@ -988,23 +988,27 @@ subroutine homogenize_field(field, weight, G, scale, answer_date, wt_unscale)
988988

989989
use_repro_sums = .false. ; if (present(answer_date)) use_repro_sums = (answer_date >= 20230101)
990990

991-
if (scale == 0.0) then
992-
! This seems like an unlikely case to ever be used, but dealing with it is better than having NaNs arise?
993-
varAvg = 0.0
994-
elseif (use_repro_sums) then
995-
wt_descale = 1.0 ; if (present(wt_unscale)) wt_descale = wt_unscale
996-
var_unscale = wt_descale / scale
991+
if (present(weights)) then
992+
do j=js,je ; do i=is,ie
993+
weight(i,j) = weights(i,j)
994+
enddo ; enddo
995+
else
996+
do j=js,je ; do i=is,ie
997+
weight(i,j) = G%mask2dT(i,j)
998+
enddo ; enddo
999+
endif
1000+
1001+
if (use_repro_sums) then
1002+
var_unscale = 1.0 ; if (present(tmp_scale)) var_unscale = tmp_scale
1003+
if (present(wt_unscale)) var_unscale = wt_unscale * var_unscale
9971004

998-
field_for_Sums(:,:) = 0.0
999-
wts_for_Sums(:,:) = 0.0
10001005
do j=js,je ; do i=is,ie
1001-
wts_for_Sums(i,j) = wt_descale * weight(i,j)
1002-
field_for_Sums(i,j) = var_unscale * (field(i,j) * weight(i,j))
1006+
field_for_Sums(i,j) = field(i,j) * weight(i,j)
10031007
enddo ; enddo
10041008

1005-
wt_sum = reproducing_sum(wts_for_Sums)
1009+
wt_sum = reproducing_sum(weight, unscale=wt_unscale)
10061010
if (abs(wt_sum) > 0.0) &
1007-
varAvg = reproducing_sum(field_for_Sums) * (scale / wt_sum)
1011+
varAvg = reproducing_sum(field_for_Sums, unscale=var_unscale) * (1.0 / wt_sum)
10081012

10091013
else ! Do the averages with order-dependent sums to reproduce older answers.
10101014
wt_sum = 0 ; varsum = 0.
@@ -1021,8 +1025,12 @@ subroutine homogenize_field(field, weight, G, scale, answer_date, wt_unscale)
10211025
call sum_across_PEs(varsum)
10221026
varAvg = varsum / wt_sum
10231027
endif
1028+
10241029
endif
10251030

1031+
! This seems like an unlikely case to ever be used, but it is needed to recreate previous behavior.
1032+
if (present(tmp_scale)) then ; if (tmp_scale == 0.0) varAvg = 0.0 ; endif
1033+
10261034
field(:,:) = varAvg
10271035

10281036
end subroutine homogenize_field

src/initialization/MOM_state_initialization.F90

+2-2
Original file line numberDiff line numberDiff line change
@@ -2928,8 +2928,8 @@ subroutine MOM_temp_salt_initialize_from_Z(h, tv, depth_tot, G, GV, US, PF, just
29282928
if (homogenize) then
29292929
! Horizontally homogenize data to produce perfectly "flat" initial conditions
29302930
do k=1,nz
2931-
call homogenize_field(tv%T(:,:,k), G%mask2dT, G, scale=US%degC_to_C, answer_date=hor_regrid_answer_date)
2932-
call homogenize_field(tv%S(:,:,k), G%mask2dT, G, scale=US%ppt_to_S, answer_date=hor_regrid_answer_date)
2931+
call homogenize_field(tv%T(:,:,k), G, tmp_scale=US%C_to_degC, answer_date=hor_regrid_answer_date)
2932+
call homogenize_field(tv%S(:,:,k), G, tmp_scale=US%S_to_ppt, answer_date=hor_regrid_answer_date)
29332933
enddo
29342934
endif
29352935

0 commit comments

Comments
 (0)