Skip to content

Commit

Permalink
Add initialization tests using CS%initialized
Browse files Browse the repository at this point in the history
  Added a new variable, initialized, to the control structures of modules that
had been testing for an allocated control structure to verify that it had been
initialized before it was going to be used, and then duplicated the tests using
this new variable.  This was done to enable us to go ahead with MOM6 PR #5,
which eliminated many of these checks when converting the control structures
from pointers in the parent modules to elements that are always there, and then
passing them as simple types instead of as pointers.  If we decide that we do
not need these tests after all, we can easily delete them, but until this is
discussed, this commit avoids losing the messages, as it was easier to do it
this way instead of trying to recreate them after they had been removed.  All
answers and output are bitwise identical.
  • Loading branch information
Hallberg-NOAA committed Nov 22, 2021
1 parent 7b96ac1 commit be50361
Show file tree
Hide file tree
Showing 28 changed files with 345 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/core/MOM_CoriolisAdv.F90
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module MOM_CoriolisAdv

!> Control structure for mom_coriolisadv
type, public :: CoriolisAdv_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
integer :: Coriolis_Scheme !< Selects the discretization for the Coriolis terms.
!! Valid values are:
!! - SADOURNY75_ENERGY - Sadourny, 1975
Expand Down Expand Up @@ -247,6 +248,10 @@ subroutine CorAdCalc(u, v, h, uh, vh, CAu, CAv, OBC, AD, G, GV, US, CS)

if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_CoriolisAdv: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_CoriolisAdv: Module must be initialized before it is used.")

is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec
Isq = G%IscB ; Ieq = G%IecB ; Jsq = G%JscB ; Jeq = G%JecB ; nz = GV%ke
vol_neglect = GV%H_subroundoff * (1e-4 * US%m_to_L)**2
Expand Down Expand Up @@ -1131,6 +1136,8 @@ subroutine CoriolisAdv_init(Time, G, GV, US, param_file, diag, AD, CS)
endif
allocate(CS)

CS%initialized = .true.

CS%diag => diag ; CS%Time => Time

! Read all relevant parameters and write them to the model log.
Expand Down
10 changes: 10 additions & 0 deletions src/core/MOM_PressureForce_FV.F90
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module MOM_PressureForce_FV

!> Finite volume pressure gradient control structure
type, public :: PressureForce_FV_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
logical :: tides !< If true, apply tidal momentum forcing.
real :: Rho0 !< The density used in the Boussinesq
!! approximation [R ~> kg m-3].
Expand Down Expand Up @@ -165,6 +166,10 @@ subroutine PressureForce_FV_nonBouss(h, tv, PFu, PFv, G, GV, US, CS, ALE_CSp, p_

if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_PressureForce_FV_nonBouss: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_PressureForce_FV_nonBouss: Module must be initialized before it is used.")

if (CS%Stanley_T2_det_coeff>=0.) call MOM_error(FATAL, &
"MOM_PressureForce_FV_nonBouss: The Stanley parameterization is not yet"//&
"implemented in non-Boussinesq mode.")
Expand Down Expand Up @@ -502,6 +507,9 @@ subroutine PressureForce_FV_Bouss(h, tv, PFu, PFv, G, GV, US, CS, ALE_CSp, p_atm
if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_PressureForce_FV_Bouss: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_PressureForce_FV_Bouss: Module must be initialized before it is used.")

use_p_atm = associated(p_atm)
use_EOS = associated(tv%eqn_of_state)
do i=Isq,Ieq+1 ; p0(i) = 0.0 ; enddo
Expand Down Expand Up @@ -820,6 +828,8 @@ subroutine PressureForce_FV_init(Time, G, GV, US, param_file, diag, CS, tides_CS
return
else ; allocate(CS) ; endif

CS%initialized = .true.

CS%diag => diag ; CS%Time => Time
if (associated(tides_CSp)) CS%tides_CSp => tides_CSp

Expand Down
11 changes: 11 additions & 0 deletions src/core/MOM_PressureForce_Montgomery.F90
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module MOM_PressureForce_Mont

!> Control structure for the Montgomery potential form of pressure gradient
type, public :: PressureForce_Mont_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
logical :: tides !< If true, apply tidal momentum forcing.
real :: Rho0 !< The density used in the Boussinesq
!! approximation [R ~> kg m-3].
Expand Down Expand Up @@ -139,6 +140,10 @@ subroutine PressureForce_Mont_nonBouss(h, tv, PFu, PFv, G, GV, US, CS, p_atm, pb

if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_PressureForce_Mont: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_PressureForce_Mont: Module must be initialized before it is used.")

if (use_EOS) then
if (query_compressible(tv%eqn_of_state)) call MOM_error(FATAL, &
"PressureForce_Mont_nonBouss: The Montgomery form of the pressure force "//&
Expand Down Expand Up @@ -426,6 +431,10 @@ subroutine PressureForce_Mont_Bouss(h, tv, PFu, PFv, G, GV, US, CS, p_atm, pbce,

if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_PressureForce_Mont: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_PressureForce_Mont: Module must be initialized before it is used.")

if (use_EOS) then
if (query_compressible(tv%eqn_of_state)) call MOM_error(FATAL, &
"PressureForce_Mont_Bouss: The Montgomery form of the pressure force "//&
Expand Down Expand Up @@ -839,6 +848,8 @@ subroutine PressureForce_Mont_init(Time, G, GV, US, param_file, diag, CS, tides_
return
else ; allocate(CS) ; endif

CS%initialized = .true.

CS%diag => diag ; CS%Time => Time
if (associated(tides_CSp)) CS%tides_CSp => tides_CSp

Expand Down
16 changes: 16 additions & 0 deletions src/core/MOM_barotropic.F90
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ subroutine btstep(U_in, V_in, eta_in, dt, bc_accel_u, bc_accel_v, forces, pbce,

if (.not.associated(CS)) call MOM_error(FATAL, &
"btstep: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
"btstep: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%split) return
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke
Isq = G%IscB ; Ieq = G%IecB ; Jsq = G%JscB ; Jeq = G%JecB
Expand Down Expand Up @@ -2769,6 +2773,10 @@ subroutine set_dtbt(G, GV, US, CS, eta, pbce, BT_cont, gtot_est, SSH_add)

if (.not.associated(CS)) call MOM_error(FATAL, &
"set_dtbt: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
"set_dtbt: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%split) return
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke
MS%isdw = G%isd ; MS%iedw = G%ied ; MS%jsdw = G%jsd ; MS%jedw = G%jed
Expand Down Expand Up @@ -3306,6 +3314,10 @@ subroutine btcalc(h, G, GV, CS, h_u, h_v, may_use_default, OBC)
! second order accurate estimate h = 2*(h+ * h-)/(h+ + h-).
if (.not.associated(CS)) call MOM_error(FATAL, &
"btcalc: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
"btcalc: Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%split) return

use_default = .false.
Expand Down Expand Up @@ -4198,6 +4210,10 @@ subroutine bt_mass_source(h, eta, set_cor, G, GV, CS)

if (.not.associated(CS)) call MOM_error(FATAL, "bt_mass_source: "// &
"Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%module_is_initialized) call MOM_error(FATAL, "bt_mass_source: "// &
"Module MOM_barotropic must be initialized before it is used.")

if (.not.CS%split) return

is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke
Expand Down
7 changes: 7 additions & 0 deletions src/core/MOM_continuity_PPM.F90
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module MOM_continuity_PPM

!> Control structure for mom_continuity_ppm
type, public :: continuity_PPM_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
type(diag_ctrl), pointer :: diag !< Diagnostics control structure.
logical :: upwind_1st !< If true, use a first-order upwind scheme.
logical :: monotonic !< If true, use the Colella & Woodward monotonic
Expand Down Expand Up @@ -136,6 +137,10 @@ subroutine continuity_PPM(u, v, hin, h, uh, vh, dt, G, GV, US, CS, OBC, uhbt, vh

if (.not.associated(CS)) call MOM_error(FATAL, &
"MOM_continuity_PPM: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"MOM_continuity_PPM: Module must be initialized before it is used.")

x_first = (MOD(G%first_direction,2) == 0)

if (present(visc_rem_u) .neqv. present(visc_rem_v)) call MOM_error(FATAL, &
Expand Down Expand Up @@ -2210,6 +2215,8 @@ subroutine continuity_PPM_init(Time, G, GV, US, param_file, diag, CS)
endif
allocate(CS)

CS%initialized = .true.

! Read all relevant parameters and write them to the model log.
call log_version(param_file, mdl, version, "")
call get_param(param_file, mdl, "MONOTONIC_CONTINUITY", CS%monotonic, &
Expand Down
9 changes: 9 additions & 0 deletions src/diagnostics/MOM_diagnostics.F90
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module MOM_diagnostics

!> The control structure for the MOM_diagnostics module
type, public :: diagnostics_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
real :: mono_N2_column_fraction = 0. !< The lower fraction of water column over which N2 is limited as
!! monotonic for the purposes of calculating the equivalent
!! barotropic wave speed.
Expand Down Expand Up @@ -272,6 +273,9 @@ subroutine calculate_diagnostic_fields(u, v, h, uh, vh, tv, ADp, CDp, p_surf, &
if (loc(CS)==0) call MOM_error(FATAL, &
"calculate_diagnostic_fields: Module must be initialized before used.")

if (.not. CS%initialized) call MOM_error(FATAL, &
"calculate_diagnostic_fields: Module must be initialized before used.")

call calculate_derivs(dt, G, CS)

if (dt > 0.0) then
Expand Down Expand Up @@ -1253,6 +1257,9 @@ subroutine register_time_deriv(lb, f_ptr, deriv_ptr, CS)
if (.not.associated(CS)) call MOM_error(FATAL, &
"register_time_deriv: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"register_time_deriv: Module must be initialized before it is used.")

if (CS%num_time_deriv >= MAX_FIELDS_) then
call MOM_error(WARNING,"MOM_diagnostics: Attempted to register more than " // &
"MAX_FIELDS_ diagnostic time derivatives via register_time_deriv.")
Expand Down Expand Up @@ -1620,6 +1627,8 @@ subroutine MOM_diagnostics_init(MIS, ADp, CDp, Time, G, GV, US, param_file, diag
endif
allocate(CS)

CS%initialized = .true.

CS%diag => diag
use_temperature = associated(tv%T)
call get_param(param_file, mdl, "ADIABATIC", adiabatic, default=.false., &
Expand Down
7 changes: 7 additions & 0 deletions src/diagnostics/MOM_sum_output.F90
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ module MOM_sum_output

!> The control structure for the MOM_sum_output module
type, public :: sum_output_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.

type(Depth_List) :: DL !< The sorted depth list.

integer, allocatable, dimension(:) :: lH
Expand Down Expand Up @@ -160,6 +162,8 @@ subroutine MOM_sum_output_init(G, GV, US, param_file, directory, ntrnc, &
endif
allocate(CS)

CS%initialized = .true.

! Read all relevant parameters and write them to the model log.
call log_version(param_file, mdl, version, "")
call get_param(param_file, mdl, "CALCULATE_APE", CS%do_APE_calc, &
Expand Down Expand Up @@ -490,6 +494,9 @@ subroutine write_energy(u, v, h, tv, day, n, G, GV, US, CS, tracer_CSp, dt_forci
if (.not.associated(CS)) call MOM_error(FATAL, &
"write_energy: Module must be initialized before it is used.")

if (.not.CS%initialized) call MOM_error(FATAL, &
"write_energy: Module must be initialized before it is used.")

do j=js,je ; do i=is,ie
areaTm(i,j) = G%mask2dT(i,j)*G%areaT(i,j)
enddo ; enddo
Expand Down
12 changes: 12 additions & 0 deletions src/diagnostics/MOM_wave_speed.F90
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module MOM_wave_speed

!> Control structure for MOM_wave_speed
type, public :: wave_speed_CS ; private
logical :: initialized = .false. !< True if this control structure has been initialized.
logical :: use_ebt_mode = .false. !< If true, calculate the equivalent barotropic wave speed instead
!! of the first baroclinic wave speed.
!! This parameter controls the default behavior of wave_speed() which
Expand Down Expand Up @@ -149,6 +150,10 @@ subroutine wave_speed(h, tv, G, GV, US, cg1, CS, full_halos, use_ebt_mode, mono_

if (.not. associated(CS)) call MOM_error(FATAL, "MOM_wave_speed: "// &
"Module must be initialized before it is used.")

if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_speed: "// &
"Module must be initialized before it is used.")

if (present(full_halos)) then ; if (full_halos) then
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
endif ; endif
Expand Down Expand Up @@ -731,6 +736,11 @@ subroutine wave_speeds(h, tv, G, GV, US, nmodes, cn, CS, full_halos)
"Module must be initialized before it is used.")
endif

if (present(CS)) then
if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_speed: "// &
"Module must be initialized before it is used.")
endif

if (present(full_halos)) then ; if (full_halos) then
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
endif ; endif
Expand Down Expand Up @@ -1200,6 +1210,8 @@ subroutine wave_speed_init(CS, use_ebt_mode, mono_N2_column_fraction, mono_N2_de
return
else ; allocate(CS) ; endif

CS%initialized = .true.

! Write all relevant parameters to the model log.
call log_version(mdl, version)

Expand Down
6 changes: 6 additions & 0 deletions src/diagnostics/MOM_wave_structure.F90
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module MOM_wave_structure

!> The control structure for the MOM_wave_structure module
type, public :: wave_structure_CS ; !private
logical :: initialized = .false. !< True if this control structure has been initialized.
type(diag_ctrl), pointer :: diag => NULL() !< A structure that is used to
!! regulate the timing of diagnostic output.
real, allocatable, dimension(:,:,:) :: w_strct
Expand Down Expand Up @@ -198,6 +199,9 @@ subroutine wave_structure(h, tv, G, GV, US, cn, ModeNum, freq, CS, En, full_halo
"Module must be initialized before it is used.")
!endif

if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_structure: "// &
"Module must be initialized before it is used.")

if (present(full_halos)) then ; if (full_halos) then
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
endif ; endif
Expand Down Expand Up @@ -742,6 +746,8 @@ subroutine wave_structure_init(Time, G, GV, param_file, diag, CS)
return
else ; allocate(CS) ; endif

CS%initialized = .true.

call get_param(param_file, mdl, "INTERNAL_TIDE_SOURCE_X", CS%int_tide_source_x, &
"X Location of generation site for internal tide", default=1.)
call get_param(param_file, mdl, "INTERNAL_TIDE_SOURCE_Y", CS%int_tide_source_y, &
Expand Down
Loading

0 comments on commit be50361

Please sign in to comment.