Skip to content

Commit 15d9b37

Browse files
committed
+Refactor totalTandS to work with scaled variables
Refactored totalTandS and totalStuff to optionally work with scaled variables, by adding an optional unit_scale_type argument and a optional argument specifying the unscaling of thickness to totalTandS and an optional unscale argument to totalStuff. The comments describing the units of 19 variables were modified to reflect the various units that might be used. All solutions are bitwise identical, and output is unchanged when dimensional rescaling is not being used, but the debugging output can now be unaltered by the use of dimensional rescaling. There are new optional arguments to two publicly visible routines. This change has been tested via calls to totalTandS added to step_MOM_thermo, but because totalTandS is only intended for debugging, these testing calls are commented out. I am uncertain whether to ultimately retain these comments to illustrate the use of totalTandS or whether to delete them before this PR is merged in, but retaining them for now seems like they may help the PR review process.
1 parent 084a5db commit 15d9b37

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

src/core/MOM.F90

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module MOM
55

66
! Infrastructure modules
77
use MOM_array_transform, only : rotate_array, rotate_vector
8-
use MOM_debugging, only : MOM_debugging_init, hchksum, uvchksum
8+
use MOM_debugging, only : MOM_debugging_init, hchksum, uvchksum, totalTandS
99
use MOM_debugging, only : check_redundant, query_debugging_checks
1010
use MOM_checksum_packages, only : MOM_thermo_chksum, MOM_state_chksum
1111
use MOM_checksum_packages, only : MOM_accel_chksum, MOM_surface_chksum
@@ -1720,6 +1720,13 @@ subroutine step_MOM_thermo(CS, G, GV, US, u, v, h, tv, fluxes, dtdia, &
17201720

17211721
call disable_averaging(CS%diag)
17221722

1723+
! This works in general:
1724+
! if (associated(tv%T)) &
1725+
! call totalTandS(G%HI, h, G%areaT, tv%T, tv%S, "End of step_MOM", US, GV%H_to_mks)
1726+
! This works only if there is no rescaling being used:
1727+
! if (associated(tv%T)) &
1728+
! call totalTandS(G%HI, h, G%areaT, tv%T, tv%S, "End of step_MOM")
1729+
17231730
if (showCallTree) call callTree_leave("step_MOM_thermo(), MOM.F90")
17241731

17251732
end subroutine step_MOM_thermo

src/diagnostics/MOM_debugging.F90

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ module MOM_debugging
88

99
! This file is part of MOM6. See LICENSE.md for the license.
1010

11-
use MOM_checksums, only : hchksum, Bchksum, qchksum, uvchksum, hchksum_pair
12-
use MOM_checksums, only : is_NaN, chksum, MOM_checksums_init
13-
use MOM_coms, only : PE_here, root_PE, num_PEs
14-
use MOM_coms, only : min_across_PEs, max_across_PEs, reproducing_sum
15-
use MOM_domains, only : pass_vector, pass_var, pe_here
16-
use MOM_domains, only : BGRID_NE, AGRID, To_All, Scalar_Pair
11+
use MOM_checksums, only : hchksum, Bchksum, qchksum, uvchksum, hchksum_pair
12+
use MOM_checksums, only : is_NaN, chksum, MOM_checksums_init
13+
use MOM_coms, only : PE_here, root_PE, num_PEs
14+
use MOM_coms, only : min_across_PEs, max_across_PEs, reproducing_sum
15+
use MOM_domains, only : pass_vector, pass_var, pe_here
16+
use MOM_domains, only : BGRID_NE, AGRID, To_All, Scalar_Pair
1717
use MOM_error_handler, only : MOM_error, FATAL, WARNING, is_root_pe
18-
use MOM_file_parser, only : log_version, param_file_type, get_param
19-
use MOM_grid, only : ocean_grid_type
20-
use MOM_hor_index, only : hor_index_type
18+
use MOM_file_parser, only : log_version, param_file_type, get_param
19+
use MOM_grid, only : ocean_grid_type
20+
use MOM_hor_index, only : hor_index_type
21+
use MOM_io, only : stdout
22+
use MOM_unit_scaling, only : unit_scale_type
2123

2224
implicit none ; private
2325

@@ -837,67 +839,101 @@ end subroutine chksum_vec_A2d
837839

838840
!> This function returns the sum over computational domain of all
839841
!! processors of hThick*stuff, where stuff is a 3-d array at tracer points.
840-
function totalStuff(HI, hThick, areaT, stuff)
842+
function totalStuff(HI, hThick, areaT, stuff, unscale)
841843
type(hor_index_type), intent(in) :: HI !< A horizontal index type
842-
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: hThick !< The array of thicknesses to use as weights [m]
843-
real, dimension(HI%isd:,HI%jsd:), intent(in) :: areaT !< The array of cell areas [m2]
844-
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: stuff !< The array of stuff to be summed in arbitrary units [a]
845-
real :: totalStuff !< the globally integrated amount of stuff [a m3]
844+
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: hThick !< The array of thicknesses to use as weights
845+
!! [H ~> m or kg m-2] or [m] or [kg m-2]
846+
real, dimension(HI%isd:,HI%jsd:), intent(in) :: areaT !< The array of cell areas [L2 ~> m2] or [m2]
847+
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: stuff !< The array of stuff to be summed in arbitrary
848+
!! units [A ~> a] or [a]
849+
real, optional, intent(in) :: unscale !< A factor that is used to undo scaling of the array
850+
!! and the cell mass or volume before it is summed in
851+
!! [a m3 A-1 H-1 L-2 ~> 1] or [a kg A-1 H-1 L-2 ~> 1]
852+
real :: totalStuff !< the globally integrated amount of stuff
853+
!! [A H L2 ~> a m3 or a kg] or [a m3]
846854
! Local variables
847-
real, dimension(HI%isc:HI%iec, HI%jsc:HI%jec) :: tmp_for_sum ! The column integrated amount of stuff in a cell [a m3]
855+
real :: tmp_for_sum(HI%isc:HI%iec, HI%jsc:HI%jec) ! The column integrated amount of stuff in a
856+
! cell [A H L2 ~> a m3 or a kg] or [a m3]
848857
integer :: i, j, k, nz
849858

850859
nz = size(hThick,3)
851860
tmp_for_sum(:,:) = 0.0
852861
do k=1,nz ; do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec
853862
tmp_for_sum(i,j) = tmp_for_sum(i,j) + hThick(i,j,k) * stuff(i,j,k) * areaT(i,j)
854863
enddo ; enddo ; enddo
855-
totalStuff = reproducing_sum(tmp_for_sum)
864+
totalStuff = reproducing_sum(tmp_for_sum, unscale=unscale)
856865

857866
end function totalStuff
858867

859868
!> This subroutine display the total thickness, temperature and salinity
860869
!! as well as the change since the last call.
861-
subroutine totalTandS(HI, hThick, areaT, temperature, salinity, mesg)
870+
subroutine totalTandS(HI, hThick, areaT, temperature, salinity, mesg, US, H_to_mks)
862871
type(hor_index_type), intent(in) :: HI !< A horizontal index type
863-
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: hThick !< The array of thicknesses to use as weights [m]
864-
real, dimension(HI%isd:,HI%jsd:), intent(in) :: areaT !< The array of cell areas [m2]
865-
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: temperature !< The temperature field to sum [degC]
866-
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: salinity !< The salinity field to sum [ppt]
872+
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: hThick !< The array of thicknesses to use as weights
873+
!! [H ~> m or kg m-2] or [m] or [kg m-2]
874+
real, dimension(HI%isd:,HI%jsd:), intent(in) :: areaT !< The array of cell areas [L2 ~> m2] or [m2]
875+
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: temperature !< The temperature field to sum [C ~> degC] or [degC]
876+
real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: salinity !< The salinity field to sum [S ~> ppt] or [ppt]
867877
character(len=*), intent(in) :: mesg !< An identifying message
878+
type(unit_scale_type), optional, intent(in) :: US !< A dimensional unit scaling type
879+
real, optional, intent(in) :: H_to_MKS !< A constant that translates thickness units to its
880+
!! MKS units (m or kg m-2) based on whether the model is
881+
!! Boussinesq [m H-1 ~> 1] or not [kg m-2 H-1 ~> 1]
868882
! NOTE: This subroutine uses "save" data which is not thread safe and is purely for
869883
! extreme debugging without a proper debugger.
870-
real, save :: totalH = 0. ! The total ocean volume, saved for the next call [m3]
871-
real, save :: totalT = 0. ! The total volume integrated ocean temperature, saved for the next call [degC m3]
872-
real, save :: totalS = 0. ! The total volume integrated ocean salinity, saved for the next call [ppt m3]
884+
real, save :: totalH = 0. ! The total ocean volume or mass, saved for the next
885+
! call [H L2 ~> m3 or kg] or [m3] or [kg]
886+
real, save :: totalT = 0. ! The total volume integrated ocean temperature, saved for the next
887+
! call [C H L2 ~> degC m3 or degC kg] or [degC m3] or [degC kg]
888+
real, save :: totalS = 0. ! The total volume integrated ocean salinity, saved for the next
889+
! call [S H L2 ~> ppt m3 or ppt kg] or [ppt m3] or [ppt kg]
873890
! Local variables
874891
logical, save :: firstCall = .true.
875-
real, dimension(HI%isc:HI%iec, HI%jsc:HI%jec) :: tmp_for_sum ! The volume of each column [m3]
876-
real :: thisH, delH ! The total ocean volume and the change from the last call [m3]
877-
real :: thisT, delT ! The current total volume integrated temperature and the change from the last call [degC m3]
878-
real :: thisS, delS ! The current total volume integrated salinity and the change from the last call [ppt m3]
892+
real :: tmp_for_sum(HI%isc:HI%iec, HI%jsc:HI%jec) ! The volume of each column [H L2 ~> m3 or kg] or [m3] or [kg]
893+
real :: thisH, delH ! The total ocean volume and the change from the last call [H L2 ~> m3 or kg] or [m3] or [kg]
894+
real :: thisT, delT ! The current total volume integrated temperature and the change from the last
895+
! call [C H L2 ~> degC m3 or degC kg] or [degC m3] or [degC kg]
896+
real :: thisS, delS ! The current total volume integrated salinity and the change from the last
897+
! call [S H L2 ~> ppt m3 or ppt kg] or [ppt m3] or [ppt kg]
898+
real :: H_unscale ! A constant that translates thickness units to its MKS units (m or kg m-2) based on
899+
! whether the model is Boussinesq [m H-1 ~> 1] or non-Boussinesq [kg m-2 H-1 ~> 1]
900+
real :: HL2_unscale ! An overall unscaling factor for cell mass or volume [m3 H-1 L-2 ~> 1] or [kg H-1 L-2 ~> 1]
901+
real :: T_unscale ! An overall unscaling factor for cell-integrated temperature [degC m3 C-1 H-1 L-2 ~> 1] or
902+
! [degC kg C-1 H-1 L-2 ~> 1]
903+
real :: S_unscale ! An overall unscaling factor for cell-integrated salinity [ppt m3 S-1 H-1 L-2 ~> 1] or
904+
! [ppt kg S-1 H-1 L-2 ~> 1]
879905
integer :: i, j, k, nz
880906

907+
H_unscale = 1.0 ; if (present(H_to_mks)) H_unscale = H_to_mks
908+
if (present(US)) then
909+
HL2_unscale = US%L_to_m**2 * H_unscale
910+
T_unscale = US%C_to_degC * HL2_unscale ; S_unscale = US%S_to_ppt * HL2_unscale
911+
else
912+
HL2_unscale = H_unscale
913+
T_unscale = HL2_unscale ; S_unscale = HL2_unscale
914+
endif
915+
881916
nz = size(hThick,3)
882917
tmp_for_sum(:,:) = 0.0
883918
do k=1,nz ; do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec
884919
tmp_for_sum(i,j) = tmp_for_sum(i,j) + hThick(i,j,k) * areaT(i,j)
885920
enddo ; enddo ; enddo
886-
thisH = reproducing_sum(tmp_for_sum)
887-
thisT = totalStuff(HI, hThick, areaT, temperature)
888-
thisS = totalStuff(HI, hThick, areaT, salinity)
921+
thisH = reproducing_sum(tmp_for_sum, unscale=HL2_unscale)
922+
thisT = totalStuff(HI, hThick, areaT, temperature, unscale=T_unscale)
923+
thisS = totalStuff(HI, hThick, areaT, salinity, unscale=S_unscale)
889924

890925
if (is_root_pe()) then
891926
if (firstCall) then
892927
totalH = thisH ; totalT = thisT ; totalS = thisS
893-
write(0,*) 'Totals H,T,S:',thisH,thisT,thisS,' ',mesg
928+
write(stdout,*) 'Totals H,T,S:', thisH*HL2_unscale, thisT*T_unscale, thisS*S_unscale, ' ', mesg
894929
firstCall = .false.
895930
else
896931
delH = thisH - totalH
897932
delT = thisT - totalT
898933
delS = thisS - totalS
899934
totalH = thisH ; totalT = thisT ; totalS = thisS
900-
write(0,*) 'Tot/del H,T,S:',thisH,thisT,thisS,delH,delT,delS,' ',mesg
935+
write(0,*) 'Tot/del H,T,S:', thisH*HL2_unscale, thisT*T_unscale, thisS*S_unscale, &
936+
delH*HL2_unscale, delT*T_unscale, delS*S_unscale, ' ', mesg
901937
endif
902938
endif
903939

0 commit comments

Comments
 (0)