Skip to content

Commit be50361

Browse files
committed
Add initialization tests using CS%initialized
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.
1 parent 7b96ac1 commit be50361

28 files changed

Lines changed: 345 additions & 0 deletions

src/core/MOM_CoriolisAdv.F90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module MOM_CoriolisAdv
2525

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

248249
if (.not.associated(CS)) call MOM_error(FATAL, &
249250
"MOM_CoriolisAdv: Module must be initialized before it is used.")
251+
252+
if (.not.CS%initialized) call MOM_error(FATAL, &
253+
"MOM_CoriolisAdv: Module must be initialized before it is used.")
254+
250255
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec
251256
Isq = G%IscB ; Ieq = G%IecB ; Jsq = G%JscB ; Jeq = G%JecB ; nz = GV%ke
252257
vol_neglect = GV%H_subroundoff * (1e-4 * US%m_to_L)**2
@@ -1131,6 +1136,8 @@ subroutine CoriolisAdv_init(Time, G, GV, US, param_file, diag, AD, CS)
11311136
endif
11321137
allocate(CS)
11331138

1139+
CS%initialized = .true.
1140+
11341141
CS%diag => diag ; CS%Time => Time
11351142

11361143
! Read all relevant parameters and write them to the model log.

src/core/MOM_PressureForce_FV.F90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module MOM_PressureForce_FV
3434

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

166167
if (.not.associated(CS)) call MOM_error(FATAL, &
167168
"MOM_PressureForce_FV_nonBouss: Module must be initialized before it is used.")
169+
170+
if (.not.CS%initialized) call MOM_error(FATAL, &
171+
"MOM_PressureForce_FV_nonBouss: Module must be initialized before it is used.")
172+
168173
if (CS%Stanley_T2_det_coeff>=0.) call MOM_error(FATAL, &
169174
"MOM_PressureForce_FV_nonBouss: The Stanley parameterization is not yet"//&
170175
"implemented in non-Boussinesq mode.")
@@ -502,6 +507,9 @@ subroutine PressureForce_FV_Bouss(h, tv, PFu, PFv, G, GV, US, CS, ALE_CSp, p_atm
502507
if (.not.associated(CS)) call MOM_error(FATAL, &
503508
"MOM_PressureForce_FV_Bouss: Module must be initialized before it is used.")
504509

510+
if (.not.CS%initialized) call MOM_error(FATAL, &
511+
"MOM_PressureForce_FV_Bouss: Module must be initialized before it is used.")
512+
505513
use_p_atm = associated(p_atm)
506514
use_EOS = associated(tv%eqn_of_state)
507515
do i=Isq,Ieq+1 ; p0(i) = 0.0 ; enddo
@@ -820,6 +828,8 @@ subroutine PressureForce_FV_init(Time, G, GV, US, param_file, diag, CS, tides_CS
820828
return
821829
else ; allocate(CS) ; endif
822830

831+
CS%initialized = .true.
832+
823833
CS%diag => diag ; CS%Time => Time
824834
if (associated(tides_CSp)) CS%tides_CSp => tides_CSp
825835

src/core/MOM_PressureForce_Montgomery.F90

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module MOM_PressureForce_Mont
3030

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

140141
if (.not.associated(CS)) call MOM_error(FATAL, &
141142
"MOM_PressureForce_Mont: Module must be initialized before it is used.")
143+
144+
if (.not.CS%initialized) call MOM_error(FATAL, &
145+
"MOM_PressureForce_Mont: Module must be initialized before it is used.")
146+
142147
if (use_EOS) then
143148
if (query_compressible(tv%eqn_of_state)) call MOM_error(FATAL, &
144149
"PressureForce_Mont_nonBouss: The Montgomery form of the pressure force "//&
@@ -426,6 +431,10 @@ subroutine PressureForce_Mont_Bouss(h, tv, PFu, PFv, G, GV, US, CS, p_atm, pbce,
426431

427432
if (.not.associated(CS)) call MOM_error(FATAL, &
428433
"MOM_PressureForce_Mont: Module must be initialized before it is used.")
434+
435+
if (.not.CS%initialized) call MOM_error(FATAL, &
436+
"MOM_PressureForce_Mont: Module must be initialized before it is used.")
437+
429438
if (use_EOS) then
430439
if (query_compressible(tv%eqn_of_state)) call MOM_error(FATAL, &
431440
"PressureForce_Mont_Bouss: The Montgomery form of the pressure force "//&
@@ -839,6 +848,8 @@ subroutine PressureForce_Mont_init(Time, G, GV, US, param_file, diag, CS, tides_
839848
return
840849
else ; allocate(CS) ; endif
841850

851+
CS%initialized = .true.
852+
842853
CS%diag => diag ; CS%Time => Time
843854
if (associated(tides_CSp)) CS%tides_CSp => tides_CSp
844855

src/core/MOM_barotropic.F90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ subroutine btstep(U_in, V_in, eta_in, dt, bc_accel_u, bc_accel_v, forces, pbce,
697697

698698
if (.not.associated(CS)) call MOM_error(FATAL, &
699699
"btstep: Module MOM_barotropic must be initialized before it is used.")
700+
701+
if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
702+
"btstep: Module MOM_barotropic must be initialized before it is used.")
703+
700704
if (.not.CS%split) return
701705
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke
702706
Isq = G%IscB ; Ieq = G%IecB ; Jsq = G%JscB ; Jeq = G%JecB
@@ -2769,6 +2773,10 @@ subroutine set_dtbt(G, GV, US, CS, eta, pbce, BT_cont, gtot_est, SSH_add)
27692773

27702774
if (.not.associated(CS)) call MOM_error(FATAL, &
27712775
"set_dtbt: Module MOM_barotropic must be initialized before it is used.")
2776+
2777+
if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
2778+
"set_dtbt: Module MOM_barotropic must be initialized before it is used.")
2779+
27722780
if (.not.CS%split) return
27732781
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke
27742782
MS%isdw = G%isd ; MS%iedw = G%ied ; MS%jsdw = G%jsd ; MS%jedw = G%jed
@@ -3306,6 +3314,10 @@ subroutine btcalc(h, G, GV, CS, h_u, h_v, may_use_default, OBC)
33063314
! second order accurate estimate h = 2*(h+ * h-)/(h+ + h-).
33073315
if (.not.associated(CS)) call MOM_error(FATAL, &
33083316
"btcalc: Module MOM_barotropic must be initialized before it is used.")
3317+
3318+
if (.not.CS%module_is_initialized) call MOM_error(FATAL, &
3319+
"btcalc: Module MOM_barotropic must be initialized before it is used.")
3320+
33093321
if (.not.CS%split) return
33103322

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

41994211
if (.not.associated(CS)) call MOM_error(FATAL, "bt_mass_source: "// &
42004212
"Module MOM_barotropic must be initialized before it is used.")
4213+
4214+
if (.not.CS%module_is_initialized) call MOM_error(FATAL, "bt_mass_source: "// &
4215+
"Module MOM_barotropic must be initialized before it is used.")
4216+
42014217
if (.not.CS%split) return
42024218

42034219
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; nz = GV%ke

src/core/MOM_continuity_PPM.F90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module MOM_continuity_PPM
2626

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

137138
if (.not.associated(CS)) call MOM_error(FATAL, &
138139
"MOM_continuity_PPM: Module must be initialized before it is used.")
140+
141+
if (.not.CS%initialized) call MOM_error(FATAL, &
142+
"MOM_continuity_PPM: Module must be initialized before it is used.")
143+
139144
x_first = (MOD(G%first_direction,2) == 0)
140145

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

2218+
CS%initialized = .true.
2219+
22132220
! Read all relevant parameters and write them to the model log.
22142221
call log_version(param_file, mdl, version, "")
22152222
call get_param(param_file, mdl, "MONOTONIC_CONTINUITY", CS%monotonic, &

src/diagnostics/MOM_diagnostics.F90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module MOM_diagnostics
4949

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

276+
if (.not. CS%initialized) call MOM_error(FATAL, &
277+
"calculate_diagnostic_fields: Module must be initialized before used.")
278+
275279
call calculate_derivs(dt, G, CS)
276280

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

1260+
if (.not.CS%initialized) call MOM_error(FATAL, &
1261+
"register_time_deriv: Module must be initialized before it is used.")
1262+
12561263
if (CS%num_time_deriv >= MAX_FIELDS_) then
12571264
call MOM_error(WARNING,"MOM_diagnostics: Attempted to register more than " // &
12581265
"MAX_FIELDS_ diagnostic time derivatives via register_time_deriv.")
@@ -1620,6 +1627,8 @@ subroutine MOM_diagnostics_init(MIS, ADp, CDp, Time, G, GV, US, param_file, diag
16201627
endif
16211628
allocate(CS)
16221629

1630+
CS%initialized = .true.
1631+
16231632
CS%diag => diag
16241633
use_temperature = associated(tv%T)
16251634
call get_param(param_file, mdl, "ADIABATIC", adiabatic, default=.false., &

src/diagnostics/MOM_sum_output.F90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ module MOM_sum_output
5959

6060
!> The control structure for the MOM_sum_output module
6161
type, public :: sum_output_CS ; private
62+
logical :: initialized = .false. !< True if this control structure has been initialized.
63+
6264
type(Depth_List) :: DL !< The sorted depth list.
6365

6466
integer, allocatable, dimension(:) :: lH
@@ -160,6 +162,8 @@ subroutine MOM_sum_output_init(G, GV, US, param_file, directory, ntrnc, &
160162
endif
161163
allocate(CS)
162164

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

497+
if (.not.CS%initialized) call MOM_error(FATAL, &
498+
"write_energy: Module must be initialized before it is used.")
499+
493500
do j=js,je ; do i=is,ie
494501
areaTm(i,j) = G%mask2dT(i,j)*G%areaT(i,j)
495502
enddo ; enddo

src/diagnostics/MOM_wave_speed.F90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module MOM_wave_speed
2626

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

150151
if (.not. associated(CS)) call MOM_error(FATAL, "MOM_wave_speed: "// &
151152
"Module must be initialized before it is used.")
153+
154+
if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_speed: "// &
155+
"Module must be initialized before it is used.")
156+
152157
if (present(full_halos)) then ; if (full_halos) then
153158
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
154159
endif ; endif
@@ -731,6 +736,11 @@ subroutine wave_speeds(h, tv, G, GV, US, nmodes, cn, CS, full_halos)
731736
"Module must be initialized before it is used.")
732737
endif
733738

739+
if (present(CS)) then
740+
if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_speed: "// &
741+
"Module must be initialized before it is used.")
742+
endif
743+
734744
if (present(full_halos)) then ; if (full_halos) then
735745
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
736746
endif ; endif
@@ -1200,6 +1210,8 @@ subroutine wave_speed_init(CS, use_ebt_mode, mono_N2_column_fraction, mono_N2_de
12001210
return
12011211
else ; allocate(CS) ; endif
12021212

1213+
CS%initialized = .true.
1214+
12031215
! Write all relevant parameters to the model log.
12041216
call log_version(mdl, version)
12051217

src/diagnostics/MOM_wave_structure.F90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module MOM_wave_structure
3636

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

202+
if (.not. CS%initialized) call MOM_error(FATAL, "MOM_wave_structure: "// &
203+
"Module must be initialized before it is used.")
204+
201205
if (present(full_halos)) then ; if (full_halos) then
202206
is = G%isd ; ie = G%ied ; js = G%jsd ; je = G%jed
203207
endif ; endif
@@ -742,6 +746,8 @@ subroutine wave_structure_init(Time, G, GV, param_file, diag, CS)
742746
return
743747
else ; allocate(CS) ; endif
744748

749+
CS%initialized = .true.
750+
745751
call get_param(param_file, mdl, "INTERNAL_TIDE_SOURCE_X", CS%int_tide_source_x, &
746752
"X Location of generation site for internal tide", default=1.)
747753
call get_param(param_file, mdl, "INTERNAL_TIDE_SOURCE_Y", CS%int_tide_source_y, &

0 commit comments

Comments
 (0)