portedportable, not yet portedexecuted, not portableexecutable, not hit by this run
| 1 | ! This file is part of MOM6, the Modular Ocean Model version 6. | |
| 2 | ! See the LICENSE file for licensing information. | |
| 3 | ! SPDX-License-Identifier: Apache-2.0 | |
| 4 | ||
| 5 | !> Routines to calculate checksums of various array and vector types | |
| 6 | module MOM_checksums | |
| 7 | ||
| 8 | use MOM_array_transform, only : rotate_array, rotate_array_pair, rotate_vector | |
| 9 | use MOM_array_transform, only : allocate_rotated_array | |
| 10 | use MOM_coms, only : PE_here, root_PE, num_PEs, sum_across_PEs | |
| 11 | use MOM_coms, only : min_across_PEs, max_across_PEs | |
| 12 | use MOM_coms, only : reproducing_sum, field_chksum | |
| 13 | use MOM_error_handler, only : MOM_error, FATAL, is_root_pe | |
| 14 | use MOM_file_parser, only : log_version, param_file_type | |
| 15 | use MOM_hor_index, only : hor_index_type, rotate_hor_index | |
| 16 | use MOM_murmur_hash, only : murmur_hash | |
| 17 | ||
| 18 | use iso_fortran_env, only : error_unit, int32, int64 | |
| 19 | ||
| 20 | implicit none ; private | |
| 21 | ||
| 22 | public :: chksum0, zchksum, rotated_field_chksum, field_checksum | |
| 23 | public :: hchksum, Bchksum, uchksum, vchksum, qchksum, is_NaN, chksum | |
| 24 | public :: hchksum_pair, uvchksum, Bchksum_pair | |
| 25 | public :: MOM_checksums_init | |
| 26 | ||
| 27 | ! A note on unit descriptions in comments: MOM6 uses units that can be rescaled for dimensional | |
| 28 | ! consistency testing. These are noted in comments with units like Z, H, L, and T, along with | |
| 29 | ! their mks counterparts with notation like "a velocity [Z T-1 ~> m s-1]". If the units | |
| 30 | ! vary with the Boussinesq approximation, the Boussinesq variant is given first. | |
| 31 | ! The functions in this module work with variables with arbitrary units, in which case the | |
| 32 | ! arbitrary rescaled units are indicated with [A ~> a], while the unscaled units are just [a]. | |
| 33 | ||
| 34 | !> Checksums a pair of arrays (2d or 3d) staggered at tracer points | |
| 35 | interface hchksum_pair | |
| 36 | module procedure chksum_pair_h_2d, chksum_pair_h_3d | |
| 37 | end interface | |
| 38 | ||
| 39 | !> Checksums a pair velocity arrays (2d or 3d) staggered at C-grid locations | |
| 40 | interface uvchksum | |
| 41 | module procedure chksum_uv_2d, chksum_uv_3d | |
| 42 | end interface | |
| 43 | ||
| 44 | !> Checksums an array (2d or 3d) staggered at C-grid u points. | |
| 45 | interface uchksum | |
| 46 | module procedure chksum_u_2d, chksum_u_3d | |
| 47 | end interface | |
| 48 | ||
| 49 | !> Checksums an array (2d or 3d) staggered at C-grid v points. | |
| 50 | interface vchksum | |
| 51 | module procedure chksum_v_2d, chksum_v_3d | |
| 52 | end interface | |
| 53 | ||
| 54 | !> Checksums a pair of arrays (2d or 3d) staggered at corner points | |
| 55 | interface Bchksum_pair | |
| 56 | module procedure chksum_pair_B_2d, chksum_pair_B_3d | |
| 57 | end interface | |
| 58 | ||
| 59 | !> Checksums an array (2d or 3d) staggered at tracer points. | |
| 60 | interface hchksum | |
| 61 | module procedure chksum_h_2d, chksum_h_3d | |
| 62 | end interface | |
| 63 | ||
| 64 | !> Checksums an array (2d or 3d) staggered at corner points. | |
| 65 | interface Bchksum | |
| 66 | module procedure chksum_B_2d, chksum_B_3d | |
| 67 | end interface | |
| 68 | ||
| 69 | !> This is an older interface that has been renamed Bchksum | |
| 70 | interface qchksum | |
| 71 | module procedure chksum_B_2d, chksum_B_3d | |
| 72 | end interface | |
| 73 | ||
| 74 | !> This is an older interface for 1-, 2-, or 3-D checksums | |
| 75 | interface chksum | |
| 76 | module procedure chksum1d, chksum2d, chksum3d | |
| 77 | end interface | |
| 78 | ||
| 79 | !> Write a message with either checksums or numerical statistics of arrays | |
| 80 | interface chk_sum_msg | |
| 81 | module procedure chk_sum_msg1, chk_sum_msg2, chk_sum_msg3, chk_sum_msg5 | |
| 82 | end interface | |
| 83 | ||
| 84 | !> Returns .true. if any element of x is a NaN, and .false. otherwise. | |
| 85 | interface is_NaN | |
| 86 | module procedure is_NaN_0d, is_NaN_1d, is_NaN_2d, is_NaN_3d | |
| 87 | end interface | |
| 88 | ||
| 89 | !> Compute the checksum on all elements of a field that may need to be rotated or unscaled. | |
| 90 | !! This interface uses the field_chksum function that is used to verify file contents, which | |
| 91 | !! may differ from the bitcount function used for other checksums in this module. | |
| 92 | interface rotated_field_chksum | |
| 93 | module procedure field_checksum_real_0d | |
| 94 | module procedure field_checksum_real_1d | |
| 95 | module procedure field_checksum_real_2d | |
| 96 | module procedure field_checksum_real_3d | |
| 97 | module procedure field_checksum_real_4d | |
| 98 | end interface rotated_field_chksum | |
| 99 | ||
| 100 | ||
| 101 | !> Compute the checksum on all elements of a field that may need to be rotated or unscaled. | |
| 102 | !! This interface uses the field_chksum function that is used to verify file contents, which | |
| 103 | !! may differ from the bitcount function used for other checksums in this module. | |
| 104 | interface field_checksum | |
| 105 | module procedure field_checksum_real_0d | |
| 106 | module procedure field_checksum_real_1d | |
| 107 | module procedure field_checksum_real_2d | |
| 108 | module procedure field_checksum_real_3d | |
| 109 | module procedure field_checksum_real_4d | |
| 110 | end interface field_checksum | |
| 111 | ||
| 112 | integer, parameter :: bc_modulus = 1000000000 !< Modulus of checksum bitcount | |
| 113 | integer, parameter :: default_shift=0 !< The default array shift | |
| 114 | logical :: calculateStatistics=.true. !< If true, report min, max and mean. | |
| 115 | logical :: writeChksums=.true. !< If true, report the bitcount checksum | |
| 116 | logical :: checkForNaNs=.true. !< If true, checks array for NaNs and cause | |
| 117 | !! FATAL error if any are found | |
| 118 | logical :: writeHash = .false. !< If true, report the murmur hash | |
| 119 | !! NOTE: writeHash is currently disabled due to non-compliant diagnostics. | |
| 120 | ||
| 121 | contains | |
| 122 | ||
| 123 | !> Checksum a scalar field (consistent with array checksums) | |
| 124 | 0 | subroutine chksum0(scalar, mesg, scale, logunit, unscale) |
| 125 | real, intent(in) :: scalar !< The array to be checksummed in | |
| 126 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 127 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 128 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 129 | !! for checksums and output [a A-1 ~> 1] | |
| 130 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 131 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 132 | !! for checksums and output [a A-1 ~> 1]. | |
| 133 | !! Here scale and unscale are synonymous, but unscale | |
| 134 | !! takes precedence if both are present. | |
| 135 | ||
| 136 | ! Local variables | |
| 137 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 138 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 139 | ! for checksums and output | |
| 140 | real :: scaling !< Explicit rescaling factor [a A-1 ~> 1] | |
| 141 | integer :: iounit !< Log IO unit | |
| 142 | real :: rs !< Rescaled scalar [a] | |
| 143 | integer :: bc !< Scalar bitcount | |
| 144 | ||
| 145 | 0 | if (checkForNaNs .and. is_NaN(scalar)) & |
| 146 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 147 | ||
| 148 | 0 | scaling = 1.0 |
| 149 | 0 | if (present(unscale)) then ; scaling = unscale |
| 150 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 151 | ||
| 152 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 153 | ||
| 154 | 0 | if (calculateStatistics) then |
| 155 | 0 | rs = scaling * scalar |
| 156 | 0 | if (is_root_pe()) & |
| 157 | 0 | call chk_sum_msg(" scalar:", rs, rs, rs, mesg, iounit) |
| 158 | endif | |
| 159 | ||
| 160 | 0 | if (.not. writeChksums) return |
| 161 | ||
| 162 | 0 | bc = mod(bitcount(abs(scaling * scalar)), bc_modulus) |
| 163 | 0 | if (is_root_pe()) & |
| 164 | 0 | call chk_sum_msg(" scalar:", bc, mesg, iounit) |
| 165 | ||
| 166 | 0 | if (writeHash .and. is_root_pe()) & |
| 167 | write(iounit, '(" scalar: hash=", z8, 1x, a)') & | |
| 168 | 0 | murmur_hash(scaling * scalar), mesg |
| 169 | 0 | end subroutine chksum0 |
| 170 | ||
| 171 | ||
| 172 | !> Checksum a 1d array (typically a column). | |
| 173 | 0 | subroutine zchksum(array, mesg, scale, logunit, unscale) |
| 174 | real, dimension(:), intent(in) :: array !< The array to be checksummed in | |
| 175 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 176 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 177 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 178 | !! for checksums and output [a A-1 ~> 1] | |
| 179 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 180 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 181 | !! for checksums and output [a A-1 ~> 1]. | |
| 182 | !! Here scale and unscale are synonymous, but unscale | |
| 183 | !! takes precedence if both are present. | |
| 184 | ||
| 185 | ! Local variables | |
| 186 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 187 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 188 | ! for checksums and output | |
| 189 | 0 | real, allocatable, dimension(:) :: rescaled_array ! The array with scaling undone [a] |
| 190 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 191 | integer :: iounit !< Log IO unit | |
| 192 | integer :: k | |
| 193 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 194 | integer :: bc0 | |
| 195 | ||
| 196 | 0 | if (checkForNaNs) then |
| 197 | 0 | if (is_NaN(array(:))) & |
| 198 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 199 | endif | |
| 200 | ||
| 201 | 0 | scaling = 1.0 |
| 202 | 0 | if (present(unscale)) then ; scaling = unscale |
| 203 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 204 | ||
| 205 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 206 | ||
| 207 | 0 | if (calculateStatistics) then |
| 208 | 0 | if (present(unscale) .or. present(scale)) then |
| 209 | 0 | allocate(rescaled_array(LBOUND(array,1):UBOUND(array,1)), source=0.0) |
| 210 | 0 | do k=1, size(array, 1) |
| 211 | 0 | rescaled_array(k) = scaling * array(k) |
| 212 | enddo | |
| 213 | ||
| 214 | 0 | call subStats(rescaled_array, aMean, aMin, aMax) |
| 215 | 0 | deallocate(rescaled_array) |
| 216 | else | |
| 217 | 0 | call subStats(array, aMean, aMin, aMax) |
| 218 | endif | |
| 219 | ||
| 220 | 0 | if (is_root_pe()) & |
| 221 | 0 | call chk_sum_msg(" column:", aMean, aMin, aMax, mesg, iounit) |
| 222 | endif | |
| 223 | ||
| 224 | 0 | if (.not. writeChksums) return |
| 225 | ||
| 226 | 0 | bc0 = subchk(array, scaling) |
| 227 | 0 | if (is_root_pe()) call chk_sum_msg(" column:", bc0, mesg, iounit) |
| 228 | ||
| 229 | 0 | if (writeHash .and. is_root_pe()) & |
| 230 | write(iounit, '(" column: hash=", z8, 1x, a)') & | |
| 231 | 0 | murmur_hash(scaling * array), mesg |
| 232 | ||
| 233 | contains | |
| 234 | ||
| 235 | 0 | integer function subchk(array, unscale) |
| 236 | real, dimension(:), intent(in) :: array !< The array to be checksummed in | |
| 237 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 238 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 239 | !! for checksums and output [a A-1 ~> 1] | |
| 240 | integer :: k, bc | |
| 241 | 0 | subchk = 0 |
| 242 | 0 | do k=LBOUND(array, 1), UBOUND(array, 1) |
| 243 | 0 | bc = bitcount(abs(unscale * array(k))) |
| 244 | 0 | subchk = subchk + bc |
| 245 | enddo | |
| 246 | 0 | subchk=mod(subchk, bc_modulus) |
| 247 | 0 | end function subchk |
| 248 | ||
| 249 | 0 | subroutine subStats(array, aMean, aMin, aMax) |
| 250 | real, dimension(:), intent(in) :: array !< The array to be checksummed [a] | |
| 251 | real, intent(out) :: aMean !< Array mean [a] | |
| 252 | real, intent(out) :: aMin !< Array minimum [a] | |
| 253 | real, intent(out) :: aMax !< Array maximum [a] | |
| 254 | ||
| 255 | integer :: k, n | |
| 256 | ||
| 257 | 0 | aMin = array(1) |
| 258 | 0 | aMax = array(1) |
| 259 | 0 | n = 0 |
| 260 | 0 | do k=LBOUND(array,1), UBOUND(array,1) |
| 261 | 0 | aMin = min(aMin, array(k)) |
| 262 | 0 | aMax = max(aMax, array(k)) |
| 263 | 0 | n = n + 1 |
| 264 | enddo | |
| 265 | 0 | aMean = sum(array(:)) / real(n) |
| 266 | 0 | end subroutine subStats |
| 267 | end subroutine zchksum | |
| 268 | ||
| 269 | !> Checksums on a pair of 2d arrays staggered at tracer points. | |
| 270 | 0 | subroutine chksum_pair_h_2d(mesg, arrayA, arrayB, HI, haloshift, omit_corners, & |
| 271 | scale, logunit, scalar_pair, unscale) | |
| 272 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 273 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 274 | real, dimension(HI%isd:,HI%jsd:), target, intent(in) :: arrayA !< The first array to be checksummed in | |
| 275 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 276 | real, dimension(HI%isd:,HI%jsd:), target, intent(in) :: arrayB !< The second array to be checksummed in | |
| 277 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 278 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 279 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 280 | real, optional, intent(in) :: scale !< A factor to convert these arrays back to unscaled | |
| 281 | !! units for checksums and output [a A-1 ~> 1] | |
| 282 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 283 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe | |
| 284 | !! a scalar, rather than vector | |
| 285 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 286 | !! for checksums and output [a A-1 ~> 1]. | |
| 287 | !! Here scale and unscale are synonymous, but unscale | |
| 288 | !! takes precedence if both are present. | |
| 289 | logical :: vector_pair | |
| 290 | integer :: turns | |
| 291 | type(hor_index_type), pointer :: HI_in | |
| 292 | 0 | real, dimension(:,:), pointer :: arrayA_in, arrayB_in ! Rotated arrays [A ~> a] |
| 293 | ||
| 294 | 0 | vector_pair = .true. |
| 295 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 296 | ||
| 297 | 0 | turns = HI%turns |
| 298 | 0 | if (modulo(turns, 4) /= 0) then |
| 299 | ! Rotate field back to the input grid | |
| 300 | 0 | allocate(HI_in) |
| 301 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 302 | 0 | allocate(arrayA_in(HI_in%isd:HI_in%ied, HI_in%jsd:HI_in%jed)) |
| 303 | 0 | allocate(arrayB_in(HI_in%isd:HI_in%ied, HI_in%jsd:HI_in%jed)) |
| 304 | ||
| 305 | 0 | if (vector_pair) then |
| 306 | 0 | call rotate_vector(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 307 | else | |
| 308 | 0 | call rotate_array_pair(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 309 | endif | |
| 310 | else | |
| 311 | 0 | HI_in => HI |
| 312 | 0 | arrayA_in => arrayA |
| 313 | 0 | arrayB_in => arrayB |
| 314 | endif | |
| 315 | ||
| 316 | 0 | if (present(haloshift)) then |
| 317 | call chksum_h_2d(arrayA_in, 'x '//mesg, HI_in, haloshift, omit_corners, & | |
| 318 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 319 | call chksum_h_2d(arrayB_in, 'y '//mesg, HI_in, haloshift, omit_corners, & | |
| 320 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 321 | else | |
| 322 | 0 | call chksum_h_2d(arrayA_in, 'x '//mesg, HI_in, scale=scale, logunit=logunit, unscale=unscale) |
| 323 | 0 | call chksum_h_2d(arrayB_in, 'y '//mesg, HI_in, scale=scale, logunit=logunit, unscale=unscale) |
| 324 | endif | |
| 325 | 0 | end subroutine chksum_pair_h_2d |
| 326 | ||
| 327 | !> Checksums on a pair of 3d arrays staggered at tracer points. | |
| 328 | 0 | subroutine chksum_pair_h_3d(mesg, arrayA, arrayB, HI, haloshift, omit_corners, & |
| 329 | scale, logunit, scalar_pair, unscale) | |
| 330 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 331 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 332 | real, dimension(HI%isd:,HI%jsd:, :), target, intent(in) :: arrayA !< The first array to be checksummed in | |
| 333 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 334 | real, dimension(HI%isd:,HI%jsd:, :), target, intent(in) :: arrayB !< The second array to be checksummed in | |
| 335 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 336 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 337 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 338 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 339 | !! for checksums and output [a A-1 ~> 1] | |
| 340 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 341 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe | |
| 342 | !! a scalar, rather than vector | |
| 343 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 344 | !! for checksums and output [a A-1 ~> 1]. | |
| 345 | !! Here scale and unscale are synonymous, but unscale | |
| 346 | !! takes precedence if both are present. | |
| 347 | ! Local variables | |
| 348 | logical :: vector_pair | |
| 349 | integer :: turns | |
| 350 | type(hor_index_type), pointer :: HI_in | |
| 351 | 0 | real, dimension(:,:,:), pointer :: arrayA_in, arrayB_in ! Rotated arrays [A ~> a] |
| 352 | ||
| 353 | 0 | vector_pair = .true. |
| 354 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 355 | ||
| 356 | 0 | turns = HI%turns |
| 357 | 0 | if (modulo(turns, 4) /= 0) then |
| 358 | ! Rotate field back to the input grid | |
| 359 | 0 | allocate(HI_in) |
| 360 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 361 | 0 | allocate(arrayA_in(HI_in%isd:HI_in%ied, HI_in%jsd:HI_in%jed, size(arrayA, 3))) |
| 362 | 0 | allocate(arrayB_in(HI_in%isd:HI_in%ied, HI_in%jsd:HI_in%jed, size(arrayB, 3))) |
| 363 | ||
| 364 | 0 | if (vector_pair) then |
| 365 | 0 | call rotate_vector(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 366 | else | |
| 367 | 0 | call rotate_array_pair(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 368 | endif | |
| 369 | else | |
| 370 | 0 | HI_in => HI |
| 371 | 0 | arrayA_in => arrayA |
| 372 | 0 | arrayB_in => arrayB |
| 373 | endif | |
| 374 | ||
| 375 | 0 | if (present(haloshift)) then |
| 376 | call chksum_h_3d(arrayA_in, 'x '//mesg, HI_in, haloshift, omit_corners, & | |
| 377 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 378 | call chksum_h_3d(arrayB_in, 'y '//mesg, HI_in, haloshift, omit_corners, & | |
| 379 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 380 | else | |
| 381 | 0 | call chksum_h_3d(arrayA_in, 'x '//mesg, HI_in, scale=scale, logunit=logunit, unscale=unscale) |
| 382 | 0 | call chksum_h_3d(arrayB_in, 'y '//mesg, HI_in, scale=scale, logunit=logunit, unscale=unscale) |
| 383 | endif | |
| 384 | ||
| 385 | ! NOTE: automatic deallocation of array[AB]_in | |
| 386 | 0 | end subroutine chksum_pair_h_3d |
| 387 | ||
| 388 | !> Checksums a 2d array staggered at tracer points. | |
| 389 | 0 | subroutine chksum_h_2d(array_m, mesg, HI_m, haloshift, omit_corners, scale, logunit, unscale) |
| 390 | type(hor_index_type), target, intent(in) :: HI_m !< Horizontal index bounds of the model grid | |
| 391 | real, dimension(HI_m%isd:,HI_m%jsd:), target, intent(in) :: array_m !< Field array on the model grid in | |
| 392 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 393 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 394 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 395 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 396 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 397 | !! for checksums and output [a A-1 ~> 1] | |
| 398 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 399 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 400 | !! for checksums and output [a A-1 ~> 1]. | |
| 401 | !! Here scale and unscale are synonymous, but unscale | |
| 402 | !! takes precedence if both are present. | |
| 403 | ||
| 404 | ! Local variables | |
| 405 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 406 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 407 | ! for checksums and output | |
| 408 | 0 | real, pointer :: array(:,:) ! Field array on the input grid [A ~> a] |
| 409 | 0 | real, allocatable, dimension(:,:) :: rescaled_array ! The array with scaling undone [a] |
| 410 | 0 | real, allocatable :: hash_array(:,:) ! Subarray used to compute hash [a] |
| 411 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 412 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 413 | integer :: iounit !< Log IO unit | |
| 414 | integer :: i, j | |
| 415 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 416 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 417 | integer :: bcN, bcS, bcE, bcW | |
| 418 | logical :: do_corners | |
| 419 | integer :: turns ! Quarter turns from input to model grid | |
| 420 | ||
| 421 | ||
| 422 | ! Rotate array to the input grid | |
| 423 | 0 | turns = HI_m%turns |
| 424 | 0 | if (modulo(turns, 4) /= 0) then |
| 425 | 0 | allocate(HI) |
| 426 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 427 | 0 | allocate(array(HI%isd:HI%ied, HI%jsd:HI%jed)) |
| 428 | 0 | call rotate_array(array_m, -turns, array) |
| 429 | else | |
| 430 | 0 | HI => HI_m |
| 431 | 0 | array => array_m |
| 432 | endif | |
| 433 | ||
| 434 | 0 | if (checkForNaNs) then |
| 435 | 0 | if (is_NaN(array(HI%isc:HI%iec,HI%jsc:HI%jec))) & |
| 436 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 437 | ! if (is_NaN(array)) & | |
| 438 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 439 | endif | |
| 440 | ||
| 441 | 0 | scaling = 1.0 |
| 442 | 0 | if (present(unscale)) then ; scaling = unscale |
| 443 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 444 | ||
| 445 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 446 | ||
| 447 | 0 | if (calculateStatistics) then |
| 448 | 0 | if (present(unscale) .or. present(scale)) then |
| 449 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 450 | 0 | LBOUND(array,2):UBOUND(array,2)), source=0.0 ) |
| 451 | 0 | do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec |
| 452 | 0 | rescaled_array(i,j) = scaling*array(i,j) |
| 453 | enddo ; enddo | |
| 454 | 0 | call subStats(HI, rescaled_array, aMean, aMin, aMax) |
| 455 | 0 | deallocate(rescaled_array) |
| 456 | else | |
| 457 | 0 | call subStats(HI, array, aMean, aMin, aMax) |
| 458 | endif | |
| 459 | ||
| 460 | 0 | if (is_root_pe()) & |
| 461 | 0 | call chk_sum_msg("h-point:", aMean, aMin, aMax, mesg, iounit) |
| 462 | endif | |
| 463 | ||
| 464 | 0 | if (.not.writeChksums) return |
| 465 | ||
| 466 | 0 | hshift = default_shift |
| 467 | 0 | if (present(haloshift)) hshift = haloshift |
| 468 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 469 | ||
| 470 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 471 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 472 | 0 | write(0,*) 'chksum_h_2d: haloshift =',hshift |
| 473 | 0 | write(0,*) 'chksum_h_2d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 474 | 0 | write(0,*) 'chksum_h_2d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 475 | 0 | call chksum_error(FATAL,'Error in chksum_h_2d '//trim(mesg)) |
| 476 | endif | |
| 477 | ||
| 478 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 479 | ||
| 480 | 0 | if (hshift==0) then |
| 481 | 0 | if (is_root_pe()) call chk_sum_msg("h-point:", bc0, mesg, iounit) |
| 482 | else | |
| 483 | 0 | do_corners = .true. |
| 484 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 485 | ||
| 486 | 0 | if (do_corners) then |
| 487 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 488 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 489 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 490 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 491 | ||
| 492 | 0 | if (is_root_pe()) & |
| 493 | 0 | call chk_sum_msg("h-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 494 | else | |
| 495 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 496 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 497 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 498 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 499 | ||
| 500 | 0 | if (is_root_pe()) & |
| 501 | 0 | call chk_sum_msg_NSEW("h-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 502 | endif | |
| 503 | endif | |
| 504 | ||
| 505 | 0 | if (writeHash .and. is_root_pe()) then |
| 506 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec)) |
| 507 | 0 | hash_array(:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec) |
| 508 | ||
| 509 | write(iounit, '("h-point: hash=", z8, 1x, a)') & | |
| 510 | 0 | murmur_hash(hash_array), mesg |
| 511 | 0 | deallocate(hash_array) |
| 512 | endif | |
| 513 | ||
| 514 | contains | |
| 515 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 516 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 517 | real, dimension(HI%isd:,HI%jsd:), intent(in) :: array !< The array to be checksummed in | |
| 518 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 519 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 520 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 521 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 522 | !! for checksums and output [a A-1 ~> 1] | |
| 523 | integer :: i, j, bc | |
| 524 | 0 | subchk = 0 |
| 525 | 0 | do j=HI%jsc+dj,HI%jec+dj ; do i=HI%isc+di,HI%iec+di |
| 526 | 0 | bc = bitcount(abs(unscale*array(i,j))) |
| 527 | 0 | subchk = subchk + bc |
| 528 | enddo ; enddo | |
| 529 | 0 | call sum_across_PEs(subchk) |
| 530 | 0 | subchk=mod(subchk, bc_modulus) |
| 531 | 0 | end function subchk |
| 532 | ||
| 533 | 0 | subroutine subStats(HI, array, aMean, aMin, aMax) |
| 534 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 535 | real, dimension(HI%isd:,HI%jsd:), intent(in) :: array !< The array to be checksummed [a] | |
| 536 | real, intent(out) :: aMean !< Array mean [a] | |
| 537 | real, intent(out) :: aMin !< Array minimum [a] | |
| 538 | real, intent(out) :: aMax !< Array maximum [a] | |
| 539 | ||
| 540 | integer :: i, j, n | |
| 541 | ||
| 542 | 0 | aMin = array(HI%isc,HI%jsc) |
| 543 | 0 | aMax = array(HI%isc,HI%jsc) |
| 544 | 0 | n = 0 |
| 545 | 0 | do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec |
| 546 | 0 | aMin = min(aMin, array(i,j)) |
| 547 | 0 | aMax = max(aMax, array(i,j)) |
| 548 | 0 | n = n + 1 |
| 549 | enddo ; enddo | |
| 550 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec)) |
| 551 | 0 | call sum_across_PEs(n) |
| 552 | 0 | call min_across_PEs(aMin) |
| 553 | 0 | call max_across_PEs(aMax) |
| 554 | 0 | aMean = aMean / real(n) |
| 555 | 0 | end subroutine subStats |
| 556 | ||
| 557 | end subroutine chksum_h_2d | |
| 558 | ||
| 559 | !> Checksums on a pair of 2d arrays staggered at q-points. | |
| 560 | 0 | subroutine chksum_pair_B_2d(mesg, arrayA, arrayB, HI, haloshift, symmetric, & |
| 561 | omit_corners, scale, logunit, scalar_pair, unscale) | |
| 562 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 563 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 564 | real, dimension(HI%isd:,HI%jsd:), target, intent(in) :: arrayA !< The first array to be checksummed in | |
| 565 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 566 | real, dimension(HI%isd:,HI%jsd:), target, intent(in) :: arrayB !< The second array to be checksummed in | |
| 567 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 568 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 569 | !! symmetric computational domain. | |
| 570 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 571 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 572 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 573 | !! for checksums and output [a A-1 ~> 1] | |
| 574 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 575 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe | |
| 576 | !! a scalar, rather than vector | |
| 577 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 578 | !! for checksums and output [a A-1 ~> 1]. | |
| 579 | !! Here scale and unscale are synonymous, but unscale | |
| 580 | !! takes precedence if both are present. | |
| 581 | ||
| 582 | logical :: sym | |
| 583 | logical :: vector_pair | |
| 584 | integer :: turns | |
| 585 | type(hor_index_type), pointer :: HI_in | |
| 586 | 0 | real, dimension(:,:), pointer :: arrayA_in, arrayB_in ! Rotated arrays [A ~> a] |
| 587 | ||
| 588 | 0 | vector_pair = .true. |
| 589 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 590 | ||
| 591 | 0 | turns = HI%turns |
| 592 | 0 | if (modulo(turns, 4) /= 0) then |
| 593 | ! Rotate field back to the input grid | |
| 594 | 0 | allocate(HI_in) |
| 595 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 596 | 0 | allocate(arrayA_in(HI_in%IsdB:HI_in%IedB, HI_in%JsdB:HI_in%JedB)) |
| 597 | 0 | allocate(arrayB_in(HI_in%IsdB:HI_in%IedB, HI_in%JsdB:HI_in%JedB)) |
| 598 | ||
| 599 | 0 | if (vector_pair) then |
| 600 | 0 | call rotate_vector(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 601 | else | |
| 602 | 0 | call rotate_array_pair(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 603 | endif | |
| 604 | else | |
| 605 | 0 | HI_in => HI |
| 606 | 0 | arrayA_in => arrayA |
| 607 | 0 | arrayB_in => arrayB |
| 608 | endif | |
| 609 | ||
| 610 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 611 | ||
| 612 | 0 | if (present(haloshift)) then |
| 613 | call chksum_B_2d(arrayA_in, 'x '//mesg, HI_in, haloshift, symmetric=sym, & | |
| 614 | 0 | omit_corners=omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 615 | call chksum_B_2d(arrayB_in, 'y '//mesg, HI_in, haloshift, symmetric=sym, & | |
| 616 | 0 | omit_corners=omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 617 | else | |
| 618 | call chksum_B_2d(arrayA_in, 'x '//mesg, HI_in, symmetric=sym, & | |
| 619 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 620 | call chksum_B_2d(arrayB_in, 'y '//mesg, HI_in, symmetric=sym, & | |
| 621 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 622 | endif | |
| 623 | ||
| 624 | 0 | end subroutine chksum_pair_B_2d |
| 625 | ||
| 626 | !> Checksums on a pair of 3d arrays staggered at q-points. | |
| 627 | 0 | subroutine chksum_pair_B_3d(mesg, arrayA, arrayB, HI, haloshift, symmetric, & |
| 628 | omit_corners, scale, logunit, scalar_pair, unscale) | |
| 629 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 630 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 631 | real, dimension(HI%IsdB:,HI%JsdB:, :), target, intent(in) :: arrayA !< The first array to be checksummed in | |
| 632 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 633 | real, dimension(HI%IsdB:,HI%JsdB:, :), target, intent(in) :: arrayB !< The second array to be checksummed in | |
| 634 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 635 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 636 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 637 | !! symmetric computational domain. | |
| 638 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 639 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 640 | !! for checksums and output [a A-1 ~> 1] | |
| 641 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 642 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe | |
| 643 | !! a scalar, rather than vector | |
| 644 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 645 | !! for checksums and output [a A-1 ~> 1]. | |
| 646 | !! Here scale and unscale are synonymous, but unscale | |
| 647 | !! takes precedence if both are present. | |
| 648 | ! Local variables | |
| 649 | logical :: vector_pair | |
| 650 | integer :: turns | |
| 651 | type(hor_index_type), pointer :: HI_in | |
| 652 | 0 | real, dimension(:,:,:), pointer :: arrayA_in, arrayB_in ! Rotated arrays [A ~> a] |
| 653 | ||
| 654 | 0 | vector_pair = .true. |
| 655 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 656 | ||
| 657 | 0 | turns = HI%turns |
| 658 | 0 | if (modulo(turns, 4) /= 0) then |
| 659 | ! Rotate field back to the input grid | |
| 660 | 0 | allocate(HI_in) |
| 661 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 662 | 0 | allocate(arrayA_in(HI_in%IsdB:HI_in%IedB, HI_in%JsdB:HI_in%JedB, size(arrayA, 3))) |
| 663 | 0 | allocate(arrayB_in(HI_in%IsdB:HI_in%IedB, HI_in%JsdB:HI_in%JedB, size(arrayB, 3))) |
| 664 | ||
| 665 | 0 | if (vector_pair) then |
| 666 | 0 | call rotate_vector(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 667 | else | |
| 668 | 0 | call rotate_array_pair(arrayA, arrayB, -turns, arrayA_in, arrayB_in) |
| 669 | endif | |
| 670 | else | |
| 671 | 0 | HI_in => HI |
| 672 | 0 | arrayA_in => arrayA |
| 673 | 0 | arrayB_in => arrayB |
| 674 | endif | |
| 675 | ||
| 676 | 0 | if (present(haloshift)) then |
| 677 | call chksum_B_3d(arrayA_in, 'x '//mesg, HI_in, haloshift, symmetric, & | |
| 678 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 679 | call chksum_B_3d(arrayB_in, 'y '//mesg, HI_in, haloshift, symmetric, & | |
| 680 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 681 | else | |
| 682 | call chksum_B_3d(arrayA_in, 'x '//mesg, HI_in, symmetric=symmetric, & | |
| 683 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 684 | call chksum_B_3d(arrayB_in, 'y '//mesg, HI_in, symmetric=symmetric, & | |
| 685 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 686 | endif | |
| 687 | 0 | end subroutine chksum_pair_B_3d |
| 688 | ||
| 689 | !> Checksums a 2d array staggered at corner points. | |
| 690 | 0 | subroutine chksum_B_2d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 691 | scale, logunit, unscale) | |
| 692 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 693 | real, dimension(HI_m%IsdB:,HI_m%JsdB:), & | |
| 694 | target, intent(in) :: array_m !< The array to be checksummed in | |
| 695 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 696 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 697 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 698 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the | |
| 699 | !! full symmetric computational domain. | |
| 700 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 701 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 702 | !! for checksums and output [a A-1 ~> 1] | |
| 703 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 704 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 705 | !! for checksums and output [a A-1 ~> 1]. | |
| 706 | !! Here scale and unscale are synonymous, but unscale | |
| 707 | !! takes precedence if both are present. | |
| 708 | ||
| 709 | ! Local variables | |
| 710 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 711 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 712 | ! for checksums and output | |
| 713 | 0 | real, pointer :: array(:,:) ! Field array on the input grid [A ~> a] |
| 714 | 0 | real, allocatable, dimension(:,:) :: rescaled_array ! The array with scaling undone [a] |
| 715 | 0 | real, allocatable :: hash_array(:,:) ! Subarray used to compute hash [a] |
| 716 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 717 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 718 | integer :: iounit !< Log IO unit | |
| 719 | integer :: i, j, Is, Js | |
| 720 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 721 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 722 | integer :: bcN, bcS, bcE, bcW | |
| 723 | logical :: do_corners, sym, sym_stats | |
| 724 | integer :: turns ! Quarter turns from input to model grid | |
| 725 | ||
| 726 | ! Rotate array to the input grid | |
| 727 | 0 | turns = HI_m%turns |
| 728 | 0 | if (modulo(turns, 4) /= 0) then |
| 729 | 0 | allocate(HI) |
| 730 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 731 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%JsdB:HI%JedB)) |
| 732 | 0 | call rotate_array(array_m, -turns, array) |
| 733 | else | |
| 734 | 0 | HI => HI_m |
| 735 | 0 | array => array_m |
| 736 | endif | |
| 737 | ||
| 738 | 0 | if (checkForNaNs) then |
| 739 | 0 | if (is_NaN(array(HI%IscB:HI%IecB,HI%JscB:HI%JecB))) & |
| 740 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 741 | ! if (is_NaN(array)) & | |
| 742 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 743 | endif | |
| 744 | ||
| 745 | 0 | scaling = 1.0 |
| 746 | 0 | if (present(unscale)) then ; scaling = unscale |
| 747 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 748 | ||
| 749 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 750 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 751 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 752 | ||
| 753 | 0 | if (calculateStatistics) then |
| 754 | 0 | if (present(unscale) .or. present(scale)) then |
| 755 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 756 | 0 | LBOUND(array,2):UBOUND(array,2)), source=0.0 ) |
| 757 | 0 | Is = HI%isc ; if (sym_stats) Is = HI%isc-1 |
| 758 | 0 | Js = HI%jsc ; if (sym_stats) Js = HI%jsc-1 |
| 759 | 0 | do J=Js,HI%JecB ; do I=Is,HI%IecB |
| 760 | 0 | rescaled_array(I,J) = scaling*array(I,J) |
| 761 | enddo ; enddo | |
| 762 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 763 | 0 | deallocate(rescaled_array) |
| 764 | else | |
| 765 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 766 | endif | |
| 767 | 0 | if (is_root_pe()) & |
| 768 | 0 | call chk_sum_msg("B-point:", aMean, aMin, aMax, mesg, iounit) |
| 769 | endif | |
| 770 | ||
| 771 | 0 | if (.not.writeChksums) return |
| 772 | ||
| 773 | 0 | hshift = default_shift |
| 774 | 0 | if (present(haloshift)) hshift = haloshift |
| 775 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 776 | ||
| 777 | if ( HI%iscB-hshift<HI%isdB .or. HI%iecB+hshift>HI%iedB .or. & | |
| 778 | 0 | HI%jscB-hshift<HI%jsdB .or. HI%jecB+hshift>HI%jedB ) then |
| 779 | 0 | write(0,*) 'chksum_B_2d: haloshift =',hshift |
| 780 | 0 | write(0,*) 'chksum_B_2d: isd,isc,iec,ied=',HI%isdB,HI%iscB,HI%iecB,HI%iedB |
| 781 | 0 | write(0,*) 'chksum_B_2d: jsd,jsc,jec,jed=',HI%jsdB,HI%jscB,HI%jecB,HI%jedB |
| 782 | 0 | call chksum_error(FATAL,'Error in chksum_B_2d '//trim(mesg)) |
| 783 | endif | |
| 784 | ||
| 785 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 786 | ||
| 787 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 788 | ||
| 789 | 0 | if ((hshift==0) .and. .not.sym) then |
| 790 | 0 | if (is_root_pe()) call chk_sum_msg("B-point:", bc0, mesg, iounit) |
| 791 | else | |
| 792 | 0 | do_corners = .true. |
| 793 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 794 | ||
| 795 | 0 | if (do_corners) then |
| 796 | 0 | if (sym) then |
| 797 | 0 | bcSW = subchk(array, HI, -hshift-1, -hshift-1, scaling) |
| 798 | 0 | bcSE = subchk(array, HI, hshift, -hshift-1, scaling) |
| 799 | 0 | bcNW = subchk(array, HI, -hshift-1, hshift, scaling) |
| 800 | else | |
| 801 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 802 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 803 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 804 | endif | |
| 805 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 806 | ||
| 807 | 0 | if (is_root_pe()) & |
| 808 | 0 | call chk_sum_msg("B-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 809 | else | |
| 810 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 811 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 812 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 813 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 814 | ||
| 815 | 0 | if (is_root_pe()) & |
| 816 | 0 | call chk_sum_msg_NSEW("B-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 817 | endif | |
| 818 | endif | |
| 819 | ||
| 820 | 0 | if (writeHash .and. is_root_pe()) then |
| 821 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec)) |
| 822 | 0 | hash_array(:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec) |
| 823 | ||
| 824 | write(iounit, '("B-point: hash=", z8, 1x, a)') & | |
| 825 | 0 | murmur_hash(hash_array), mesg |
| 826 | 0 | deallocate(hash_array) |
| 827 | endif | |
| 828 | ||
| 829 | contains | |
| 830 | ||
| 831 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 832 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 833 | real, dimension(HI%IsdB:,HI%JsdB:), intent(in) :: array !< The array to be checksummed in | |
| 834 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 835 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 836 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 837 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 838 | !! for checksums and output [a A-1 ~> 1] | |
| 839 | integer :: i, j, bc | |
| 840 | 0 | subchk = 0 |
| 841 | ! This line deliberately uses the h-point computational domain. | |
| 842 | 0 | do J=HI%jsc+dj,HI%jec+dj ; do I=HI%isc+di,HI%iec+di |
| 843 | 0 | bc = bitcount(abs(unscale*array(I,J))) |
| 844 | 0 | subchk = subchk + bc |
| 845 | enddo ; enddo | |
| 846 | 0 | call sum_across_PEs(subchk) |
| 847 | 0 | subchk=mod(subchk, bc_modulus) |
| 848 | 0 | end function subchk |
| 849 | ||
| 850 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 851 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 852 | real, dimension(HI%IsdB:,HI%JsdB:), intent(in) :: array !< The array to be checksummed [a] | |
| 853 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 854 | !! full symmetric computational domain. | |
| 855 | real, intent(out) :: aMean !< Array mean [a] | |
| 856 | real, intent(out) :: aMin !< Array minimum [a] | |
| 857 | real, intent(out) :: aMax !< Array maximum [a] | |
| 858 | ||
| 859 | integer :: i, j, n, IsB, JsB | |
| 860 | ||
| 861 | 0 | IsB = HI%isc ; if (sym_stats) IsB = HI%isc-1 |
| 862 | 0 | JsB = HI%jsc ; if (sym_stats) JsB = HI%jsc-1 |
| 863 | ||
| 864 | 0 | aMin = array(HI%isc,HI%jsc) ; aMax = aMin |
| 865 | 0 | do J=JsB,HI%JecB ; do I=IsB,HI%IecB |
| 866 | 0 | aMin = min(aMin, array(I,J)) |
| 867 | 0 | aMax = max(aMax, array(I,J)) |
| 868 | enddo ; enddo | |
| 869 | ! This line deliberately uses the h-point computational domain. | |
| 870 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec)) |
| 871 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) |
| 872 | 0 | call sum_across_PEs(n) |
| 873 | 0 | call min_across_PEs(aMin) |
| 874 | 0 | call max_across_PEs(aMax) |
| 875 | 0 | aMean = aMean / real(n) |
| 876 | 0 | end subroutine subStats |
| 877 | ||
| 878 | end subroutine chksum_B_2d | |
| 879 | ||
| 880 | !> Checksums a pair of 2d velocity arrays staggered at C-grid locations | |
| 881 | 0 | subroutine chksum_uv_2d(mesg, arrayU, arrayV, HI, haloshift, symmetric, & |
| 882 | omit_corners, scale, logunit, scalar_pair, unscale) | |
| 883 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 884 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 885 | real, dimension(HI%IsdB:,HI%jsd:), target, intent(in) :: arrayU !< The u-component array to be checksummed in | |
| 886 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 887 | real, dimension(HI%isd:,HI%JsdB:), target, intent(in) :: arrayV !< The v-component array to be checksummed in | |
| 888 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 889 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 890 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 891 | !! symmetric computational domain. | |
| 892 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 893 | real, optional, intent(in) :: scale !< A factor to convert these arrays back to unscaled | |
| 894 | !! units for checksums and output [a A-1 ~> 1] | |
| 895 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 896 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe a | |
| 897 | !! a scalar, rather than vector | |
| 898 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 899 | !! for checksums and output [a A-1 ~> 1]. | |
| 900 | !! Here scale and unscale are synonymous, but unscale | |
| 901 | !! takes precedence if both are present. | |
| 902 | ! Local variables | |
| 903 | logical :: vector_pair | |
| 904 | integer :: turns | |
| 905 | type(hor_index_type), pointer :: HI_in | |
| 906 | 0 | real, dimension(:,:), pointer :: arrayU_in, arrayV_in ! Rotated arrays [A ~> a] |
| 907 | ||
| 908 | 0 | vector_pair = .true. |
| 909 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 910 | ||
| 911 | 0 | turns = HI%turns |
| 912 | 0 | if (modulo(turns, 4) /= 0) then |
| 913 | ! Rotate field back to the input grid | |
| 914 | 0 | allocate(HI_in) |
| 915 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 916 | 0 | allocate(arrayU_in(HI_in%IsdB:HI_in%IedB, HI_in%jsd:HI_in%jed)) |
| 917 | 0 | allocate(arrayV_in(HI_in%isd:HI_in%ied, HI_in%JsdB:HI_in%JedB)) |
| 918 | ||
| 919 | 0 | if (vector_pair) then |
| 920 | 0 | call rotate_vector(arrayU, arrayV, -turns, arrayU_in, arrayV_in) |
| 921 | else | |
| 922 | 0 | call rotate_array_pair(arrayU, arrayV, -turns, arrayU_in, arrayV_in) |
| 923 | endif | |
| 924 | else | |
| 925 | 0 | HI_in => HI |
| 926 | 0 | arrayU_in => arrayU |
| 927 | 0 | arrayV_in => arrayV |
| 928 | endif | |
| 929 | ||
| 930 | 0 | if (present(haloshift)) then |
| 931 | call chksum_u_2d(arrayU_in, 'u '//mesg, HI_in, haloshift, symmetric, & | |
| 932 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 933 | call chksum_v_2d(arrayV_in, 'v '//mesg, HI_in, haloshift, symmetric, & | |
| 934 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 935 | else | |
| 936 | call chksum_u_2d(arrayU_in, 'u '//mesg, HI_in, symmetric=symmetric, & | |
| 937 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 938 | call chksum_v_2d(arrayV_in, 'v '//mesg, HI_in, symmetric=symmetric, & | |
| 939 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 940 | endif | |
| 941 | 0 | end subroutine chksum_uv_2d |
| 942 | ||
| 943 | !> Checksums a pair of 3d velocity arrays staggered at C-grid locations | |
| 944 | 0 | subroutine chksum_uv_3d(mesg, arrayU, arrayV, HI, haloshift, symmetric, & |
| 945 | omit_corners, scale, logunit, scalar_pair, unscale) | |
| 946 | character(len=*), intent(in) :: mesg !< Identifying messages | |
| 947 | type(hor_index_type), target, intent(in) :: HI !< A horizontal index type | |
| 948 | real, dimension(HI%IsdB:,HI%jsd:,:), target, intent(in) :: arrayU !< The u-component array to be checksummed in | |
| 949 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 950 | real, dimension(HI%isd:,HI%JsdB:,:), target, intent(in) :: arrayV !< The v-component array to be checksummed in | |
| 951 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 952 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 953 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 954 | !! symmetric computational domain. | |
| 955 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 956 | real, optional, intent(in) :: scale !< A factor to convert these arrays back to unscaled | |
| 957 | !! units for checksums and output [a A-1 ~> 1] | |
| 958 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 959 | logical, optional, intent(in) :: scalar_pair !< If true, then the arrays describe a | |
| 960 | !! a scalar, rather than vector | |
| 961 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 962 | !! for checksums and output [a A-1 ~> 1]. | |
| 963 | !! Here scale and unscale are synonymous, but unscale | |
| 964 | !! takes precedence if both are present. | |
| 965 | ! Local variables | |
| 966 | logical :: vector_pair | |
| 967 | integer :: turns | |
| 968 | type(hor_index_type), pointer :: HI_in | |
| 969 | 0 | real, dimension(:,:,:), pointer :: arrayU_in, arrayV_in ! Rotated arrays [A ~> a] |
| 970 | ||
| 971 | 0 | vector_pair = .true. |
| 972 | 0 | if (present(scalar_pair)) vector_pair = .not. scalar_pair |
| 973 | ||
| 974 | 0 | turns = HI%turns |
| 975 | 0 | if (modulo(turns, 4) /= 0) then |
| 976 | ! Rotate field back to the input grid | |
| 977 | 0 | allocate(HI_in) |
| 978 | 0 | call rotate_hor_index(HI, -turns, HI_in) |
| 979 | 0 | allocate(arrayU_in(HI_in%IsdB:HI_in%IedB, HI_in%jsd:HI_in%jed, size(arrayU, 3))) |
| 980 | 0 | allocate(arrayV_in(HI_in%isd:HI_in%ied, HI_in%JsdB:HI_in%JedB, size(arrayV, 3))) |
| 981 | ||
| 982 | 0 | if (vector_pair) then |
| 983 | 0 | call rotate_vector(arrayU, arrayV, -turns, arrayU_in, arrayV_in) |
| 984 | else | |
| 985 | 0 | call rotate_array_pair(arrayU, arrayV, -turns, arrayU_in, arrayV_in) |
| 986 | endif | |
| 987 | else | |
| 988 | 0 | HI_in => HI |
| 989 | 0 | arrayU_in => arrayU |
| 990 | 0 | arrayV_in => arrayV |
| 991 | endif | |
| 992 | ||
| 993 | 0 | if (present(haloshift)) then |
| 994 | call chksum_u_3d(arrayU_in, 'u '//mesg, HI_in, haloshift, symmetric, & | |
| 995 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 996 | call chksum_v_3d(arrayV_in, 'v '//mesg, HI_in, haloshift, symmetric, & | |
| 997 | 0 | omit_corners, scale=scale, logunit=logunit, unscale=unscale) |
| 998 | else | |
| 999 | call chksum_u_3d(arrayU_in, 'u '//mesg, HI_in, symmetric=symmetric, & | |
| 1000 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 1001 | call chksum_v_3d(arrayV_in, 'v '//mesg, HI_in, symmetric=symmetric, & | |
| 1002 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 1003 | endif | |
| 1004 | 0 | end subroutine chksum_uv_3d |
| 1005 | ||
| 1006 | !> Checksums a 2d array staggered at C-grid u points. | |
| 1007 | 0 | subroutine chksum_u_2d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 1008 | scale, logunit, unscale) | |
| 1009 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1010 | real, dimension(HI_m%IsdB:,HI_m%jsd:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1011 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1012 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1013 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1014 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 1015 | !! symmetric computational domain. | |
| 1016 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1017 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1018 | !! for checksums and output [a A-1 ~> 1] | |
| 1019 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 1020 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1021 | !! for checksums and output [a A-1 ~> 1]. | |
| 1022 | !! Here scale and unscale are synonymous, but unscale | |
| 1023 | !! takes precedence if both are present. | |
| 1024 | ||
| 1025 | ! Local variables | |
| 1026 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 1027 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 1028 | ! for checksums and output | |
| 1029 | 0 | real, pointer :: array(:,:) ! Field array on the input grid [A ~> a] |
| 1030 | 0 | real, allocatable, dimension(:,:) :: rescaled_array ! The array with scaling undone [a] |
| 1031 | 0 | real, allocatable :: hash_array(:,:) ! Subarray used to compute hash [a] |
| 1032 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 1033 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 1034 | integer :: iounit !< Log IO unit | |
| 1035 | integer :: i, j, Is | |
| 1036 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 1037 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 1038 | integer :: bcN, bcS, bcE, bcW | |
| 1039 | logical :: do_corners, sym, sym_stats | |
| 1040 | integer :: turns ! Quarter turns from input to model grid | |
| 1041 | ||
| 1042 | ! Rotate array to the input grid | |
| 1043 | 0 | turns = HI_m%turns |
| 1044 | 0 | if (modulo(turns, 4) /= 0) then |
| 1045 | 0 | allocate(HI) |
| 1046 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 1047 | 0 | if (modulo(turns, 2) /= 0) then |
| 1048 | ! Arrays originating from v-points must be handled by vchksum | |
| 1049 | 0 | allocate(array(HI%isd:HI%ied, HI%JsdB:HI%JedB)) |
| 1050 | 0 | call rotate_array(array_m, -turns, array) |
| 1051 | call vchksum(array, mesg, HI, haloshift, symmetric, omit_corners, & | |
| 1052 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 1053 | 0 | return |
| 1054 | else | |
| 1055 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%jsd:HI%jed)) |
| 1056 | 0 | call rotate_array(array_m, -turns, array) |
| 1057 | endif | |
| 1058 | else | |
| 1059 | 0 | HI => HI_m |
| 1060 | 0 | array => array_m |
| 1061 | endif | |
| 1062 | ||
| 1063 | 0 | if (checkForNaNs) then |
| 1064 | 0 | if (is_NaN(array(HI%IscB:HI%IecB,HI%jsc:HI%jec))) & |
| 1065 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 1066 | ! if (is_NaN(array)) & | |
| 1067 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 1068 | endif | |
| 1069 | ||
| 1070 | 0 | scaling = 1.0 |
| 1071 | 0 | if (present(unscale)) then ; scaling = unscale |
| 1072 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 1073 | ||
| 1074 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 1075 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 1076 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 1077 | ||
| 1078 | 0 | if (calculateStatistics) then |
| 1079 | 0 | if (present(unscale) .or. present(scale)) then |
| 1080 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 1081 | 0 | LBOUND(array,2):UBOUND(array,2)), source=0.0 ) |
| 1082 | 0 | Is = HI%isc ; if (sym_stats) Is = HI%isc-1 |
| 1083 | 0 | do j=HI%jsc,HI%jec ; do I=Is,HI%IecB |
| 1084 | 0 | rescaled_array(I,j) = scaling*array(I,j) |
| 1085 | enddo ; enddo | |
| 1086 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 1087 | 0 | deallocate(rescaled_array) |
| 1088 | else | |
| 1089 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1090 | endif | |
| 1091 | ||
| 1092 | 0 | if (is_root_pe()) & |
| 1093 | 0 | call chk_sum_msg("u-point:", aMean, aMin, aMax, mesg, iounit) |
| 1094 | endif | |
| 1095 | ||
| 1096 | 0 | if (.not.writeChksums) return |
| 1097 | ||
| 1098 | 0 | hshift = default_shift |
| 1099 | 0 | if (present(haloshift)) hshift = haloshift |
| 1100 | 0 | if (hshift<0) hshift = HI%iedB-HI%iecB |
| 1101 | ||
| 1102 | if ( HI%iscB-hshift<HI%isdB .or. HI%iecB+hshift>HI%iedB .or. & | |
| 1103 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 1104 | 0 | write(0,*) 'chksum_u_2d: haloshift =',hshift |
| 1105 | 0 | write(0,*) 'chksum_u_2d: isd,isc,iec,ied=',HI%isdB,HI%iscB,HI%iecB,HI%iedB |
| 1106 | 0 | write(0,*) 'chksum_u_2d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 1107 | 0 | call chksum_error(FATAL,'Error in chksum_u_2d '//trim(mesg)) |
| 1108 | endif | |
| 1109 | ||
| 1110 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 1111 | ||
| 1112 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 1113 | ||
| 1114 | 0 | if ((hshift==0) .and. .not.sym) then |
| 1115 | 0 | if (is_root_pe()) call chk_sum_msg("u-point:", bc0, mesg, iounit) |
| 1116 | else | |
| 1117 | 0 | do_corners = .true. |
| 1118 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 1119 | ||
| 1120 | 0 | if (hshift==0) then |
| 1121 | 0 | bcW = subchk(array, HI, -hshift-1, 0, scaling) |
| 1122 | 0 | if (is_root_pe()) call chk_sum_msg_W("u-point:", bc0, bcW, mesg, iounit) |
| 1123 | 0 | elseif (do_corners) then |
| 1124 | 0 | if (sym) then |
| 1125 | 0 | bcSW = subchk(array, HI, -hshift-1, -hshift, scaling) |
| 1126 | 0 | bcNW = subchk(array, HI, -hshift-1, hshift, scaling) |
| 1127 | else | |
| 1128 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 1129 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 1130 | endif | |
| 1131 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 1132 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 1133 | ||
| 1134 | 0 | if (is_root_pe()) & |
| 1135 | 0 | call chk_sum_msg("u-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 1136 | else | |
| 1137 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 1138 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 1139 | 0 | if (sym) then |
| 1140 | 0 | bcW = subchk(array, HI, -hshift-1, 0, scaling) |
| 1141 | else | |
| 1142 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 1143 | endif | |
| 1144 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 1145 | ||
| 1146 | 0 | if (is_root_pe()) & |
| 1147 | 0 | call chk_sum_msg_NSEW("u-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 1148 | endif | |
| 1149 | endif | |
| 1150 | ||
| 1151 | 0 | if (writeHash .and. is_root_pe()) then |
| 1152 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec)) |
| 1153 | 0 | hash_array(:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec) |
| 1154 | ||
| 1155 | write(iounit, '("u-point: hash=", z8, 1x, a)') & | |
| 1156 | 0 | murmur_hash(hash_array), mesg |
| 1157 | 0 | deallocate(hash_array) |
| 1158 | endif | |
| 1159 | ||
| 1160 | contains | |
| 1161 | ||
| 1162 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 1163 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1164 | real, dimension(HI%IsdB:,HI%jsd:), intent(in) :: array !< The array to be checksummed in | |
| 1165 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1166 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 1167 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 1168 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1169 | !! for checksums and output [a A-1 ~> 1] | |
| 1170 | integer :: i, j, bc | |
| 1171 | 0 | subchk = 0 |
| 1172 | ! This line deliberately uses the h-point computational domain. | |
| 1173 | 0 | do j=HI%jsc+dj,HI%jec+dj ; do I=HI%isc+di,HI%iec+di |
| 1174 | 0 | bc = bitcount(abs(unscale*array(I,j))) |
| 1175 | 0 | subchk = subchk + bc |
| 1176 | enddo ; enddo | |
| 1177 | 0 | call sum_across_PEs(subchk) |
| 1178 | 0 | subchk=mod(subchk, bc_modulus) |
| 1179 | 0 | end function subchk |
| 1180 | ||
| 1181 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1182 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1183 | real, dimension(HI%IsdB:,HI%jsd:), intent(in) :: array !< The array to be checksummed [a] | |
| 1184 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 1185 | !! full symmetric computational domain. | |
| 1186 | real, intent(out) :: aMean !< Array mean [a] | |
| 1187 | real, intent(out) :: aMin !< Array minimum [a] | |
| 1188 | real, intent(out) :: aMax !< Array maximum [a] | |
| 1189 | ||
| 1190 | integer :: i, j, n, IsB | |
| 1191 | ||
| 1192 | 0 | IsB = HI%isc ; if (sym_stats) IsB = HI%isc-1 |
| 1193 | ||
| 1194 | 0 | aMin = array(HI%isc,HI%jsc) ; aMax = aMin |
| 1195 | 0 | do j=HI%jsc,HI%jec ; do I=IsB,HI%IecB |
| 1196 | 0 | aMin = min(aMin, array(I,j)) |
| 1197 | 0 | aMax = max(aMax, array(I,j)) |
| 1198 | enddo ; enddo | |
| 1199 | ! This line deliberately uses the h-point computational domain. | |
| 1200 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec)) |
| 1201 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) |
| 1202 | 0 | call sum_across_PEs(n) |
| 1203 | 0 | call min_across_PEs(aMin) |
| 1204 | 0 | call max_across_PEs(aMax) |
| 1205 | 0 | aMean = aMean / real(n) |
| 1206 | 0 | end subroutine subStats |
| 1207 | ||
| 1208 | end subroutine chksum_u_2d | |
| 1209 | ||
| 1210 | !> Checksums a 2d array staggered at C-grid v points. | |
| 1211 | 0 | subroutine chksum_v_2d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 1212 | scale, logunit, unscale) | |
| 1213 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1214 | real, dimension(HI_m%isd:,HI_m%JsdB:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1215 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1216 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1217 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1218 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 1219 | !! symmetric computational domain. | |
| 1220 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1221 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1222 | !! for checksums and output [a A-1 ~> 1] | |
| 1223 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 1224 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1225 | !! for checksums and output [a A-1 ~> 1]. | |
| 1226 | !! Here scale and unscale are synonymous, but unscale | |
| 1227 | !! takes precedence if both are present. | |
| 1228 | ||
| 1229 | ! Local variables | |
| 1230 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 1231 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 1232 | ! for checksums and output | |
| 1233 | 0 | real, pointer :: array(:,:) ! Field array on the input grid [A ~> a] |
| 1234 | 0 | real, allocatable, dimension(:,:) :: rescaled_array ! The array with scaling undone [a] |
| 1235 | 0 | real, allocatable :: hash_array(:,:) ! Subarray used to compute hash [a] |
| 1236 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 1237 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 1238 | integer :: iounit !< Log IO unit | |
| 1239 | integer :: i, j, Js | |
| 1240 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 1241 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 1242 | integer :: bcN, bcS, bcE, bcW | |
| 1243 | logical :: do_corners, sym, sym_stats | |
| 1244 | integer :: turns ! Quarter turns from input to model grid | |
| 1245 | ||
| 1246 | ! Rotate array to the input grid | |
| 1247 | 0 | turns = HI_m%turns |
| 1248 | 0 | if (modulo(turns, 4) /= 0) then |
| 1249 | 0 | allocate(HI) |
| 1250 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 1251 | 0 | if (modulo(turns, 2) /= 0) then |
| 1252 | ! Arrays originating from u-points must be handled by uchksum | |
| 1253 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%jsd:HI%jed)) |
| 1254 | 0 | call rotate_array(array_m, -turns, array) |
| 1255 | call uchksum(array, mesg, HI, haloshift, symmetric, omit_corners, & | |
| 1256 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 1257 | 0 | return |
| 1258 | else | |
| 1259 | 0 | allocate(array(HI%isd:HI%ied, HI%JsdB:HI%JedB)) |
| 1260 | 0 | call rotate_array(array_m, -turns, array) |
| 1261 | endif | |
| 1262 | else | |
| 1263 | 0 | HI => HI_m |
| 1264 | 0 | array => array_m |
| 1265 | endif | |
| 1266 | ||
| 1267 | 0 | if (checkForNaNs) then |
| 1268 | 0 | if (is_NaN(array(HI%isc:HI%iec,HI%JscB:HI%JecB))) & |
| 1269 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 1270 | ! if (is_NaN(array)) & | |
| 1271 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 1272 | endif | |
| 1273 | ||
| 1274 | 0 | scaling = 1.0 |
| 1275 | 0 | if (present(unscale)) then ; scaling = unscale |
| 1276 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 1277 | ||
| 1278 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 1279 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 1280 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 1281 | ||
| 1282 | 0 | if (calculateStatistics) then |
| 1283 | 0 | if (present(unscale) .or. present(scale)) then |
| 1284 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 1285 | 0 | LBOUND(array,2):UBOUND(array,2)), source=0.0 ) |
| 1286 | 0 | Js = HI%jsc ; if (sym_stats) Js = HI%jsc-1 |
| 1287 | 0 | do J=Js,HI%JecB ; do i=HI%isc,HI%iec |
| 1288 | 0 | rescaled_array(i,J) = scaling*array(i,J) |
| 1289 | enddo ; enddo | |
| 1290 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 1291 | 0 | deallocate(rescaled_array) |
| 1292 | else | |
| 1293 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1294 | endif | |
| 1295 | ||
| 1296 | 0 | if (is_root_pe()) & |
| 1297 | 0 | call chk_sum_msg("v-point:", aMean, aMin, aMax, mesg, iounit) |
| 1298 | endif | |
| 1299 | ||
| 1300 | 0 | if (.not.writeChksums) return |
| 1301 | ||
| 1302 | 0 | hshift = default_shift |
| 1303 | 0 | if (present(haloshift)) hshift = haloshift |
| 1304 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 1305 | ||
| 1306 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 1307 | 0 | HI%jscB-hshift<HI%jsdB .or. HI%jecB+hshift>HI%jedB ) then |
| 1308 | 0 | write(0,*) 'chksum_v_2d: haloshift =',hshift |
| 1309 | 0 | write(0,*) 'chksum_v_2d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 1310 | 0 | write(0,*) 'chksum_v_2d: jsd,jsc,jec,jed=',HI%jsdB,HI%jscB,HI%jecB,HI%jedB |
| 1311 | 0 | call chksum_error(FATAL,'Error in chksum_v_2d '//trim(mesg)) |
| 1312 | endif | |
| 1313 | ||
| 1314 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 1315 | ||
| 1316 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 1317 | ||
| 1318 | 0 | if ((hshift==0) .and. .not.sym) then |
| 1319 | 0 | if (is_root_pe()) call chk_sum_msg("v-point:", bc0, mesg, iounit) |
| 1320 | else | |
| 1321 | 0 | do_corners = .true. |
| 1322 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 1323 | ||
| 1324 | 0 | if (hshift==0) then |
| 1325 | 0 | bcS = subchk(array, HI, 0, -hshift-1, scaling) |
| 1326 | 0 | if (is_root_pe()) call chk_sum_msg_S("v-point:", bc0, bcS, mesg, iounit) |
| 1327 | 0 | elseif (do_corners) then |
| 1328 | 0 | if (sym) then |
| 1329 | 0 | bcSW = subchk(array, HI, -hshift, -hshift-1, scaling) |
| 1330 | 0 | bcSE = subchk(array, HI, hshift, -hshift-1, scaling) |
| 1331 | else | |
| 1332 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 1333 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 1334 | endif | |
| 1335 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 1336 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 1337 | ||
| 1338 | 0 | if (is_root_pe()) & |
| 1339 | 0 | call chk_sum_msg("v-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 1340 | else | |
| 1341 | 0 | if (sym) then |
| 1342 | 0 | bcS = subchk(array, HI, 0, -hshift-1, scaling) |
| 1343 | else | |
| 1344 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 1345 | endif | |
| 1346 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 1347 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 1348 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 1349 | ||
| 1350 | 0 | if (is_root_pe()) & |
| 1351 | 0 | call chk_sum_msg_NSEW("v-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 1352 | endif | |
| 1353 | endif | |
| 1354 | ||
| 1355 | 0 | if (writeHash .and. is_root_pe()) then |
| 1356 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec)) |
| 1357 | 0 | hash_array(:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec) |
| 1358 | ||
| 1359 | write(iounit, '("v-point: hash=", z8, 1x, a)') & | |
| 1360 | 0 | murmur_hash(hash_array), mesg |
| 1361 | 0 | deallocate(hash_array) |
| 1362 | endif | |
| 1363 | ||
| 1364 | contains | |
| 1365 | ||
| 1366 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 1367 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1368 | real, dimension(HI%isd:,HI%JsdB:), intent(in) :: array !< The array to be checksummed in | |
| 1369 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1370 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 1371 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 1372 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1373 | !! for checksums and output [a A-1 ~> 1] | |
| 1374 | integer :: i, j, bc | |
| 1375 | 0 | subchk = 0 |
| 1376 | ! This line deliberately uses the h-point computational domain. | |
| 1377 | 0 | do J=HI%jsc+dj,HI%jec+dj ; do i=HI%isc+di,HI%iec+di |
| 1378 | 0 | bc = bitcount(abs(unscale*array(i,J))) |
| 1379 | 0 | subchk = subchk + bc |
| 1380 | enddo ; enddo | |
| 1381 | 0 | call sum_across_PEs(subchk) |
| 1382 | 0 | subchk=mod(subchk, bc_modulus) |
| 1383 | 0 | end function subchk |
| 1384 | ||
| 1385 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1386 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1387 | real, dimension(HI%isd:,HI%JsdB:), intent(in) :: array !< The array to be checksummed [a] | |
| 1388 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 1389 | !! full symmetric computational domain. | |
| 1390 | real, intent(out) :: aMean !< Array mean [a] | |
| 1391 | real, intent(out) :: aMin !< Array minimum [a] | |
| 1392 | real, intent(out) :: aMax !< Array maximum [a] | |
| 1393 | ||
| 1394 | integer :: i, j, n, JsB | |
| 1395 | ||
| 1396 | 0 | JsB = HI%jsc ; if (sym_stats) JsB = HI%jsc-1 |
| 1397 | ||
| 1398 | 0 | aMin = array(HI%isc,HI%jsc) ; aMax = aMin |
| 1399 | 0 | do J=JsB,HI%JecB ; do i=HI%isc,HI%iec |
| 1400 | 0 | aMin = min(aMin, array(i,J)) |
| 1401 | 0 | aMax = max(aMax, array(i,J)) |
| 1402 | enddo ; enddo | |
| 1403 | ! This line deliberately uses the h-computational domain. | |
| 1404 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec)) |
| 1405 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) |
| 1406 | 0 | call sum_across_PEs(n) |
| 1407 | 0 | call min_across_PEs(aMin) |
| 1408 | 0 | call max_across_PEs(aMax) |
| 1409 | 0 | aMean = aMean / real(n) |
| 1410 | 0 | end subroutine subStats |
| 1411 | ||
| 1412 | end subroutine chksum_v_2d | |
| 1413 | ||
| 1414 | !> Checksums a 3d array staggered at tracer points. | |
| 1415 | 0 | subroutine chksum_h_3d(array_m, mesg, HI_m, haloshift, omit_corners, scale, logunit, unscale) |
| 1416 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1417 | real, dimension(HI_m%isd:,HI_m%jsd:,:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1418 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1419 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1420 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1421 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1422 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1423 | !! for checksums and output [a A-1 ~> 1] | |
| 1424 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 1425 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1426 | !! for checksums and output [a A-1 ~> 1]. | |
| 1427 | !! Here scale and unscale are synonymous, but unscale | |
| 1428 | !! takes precedence if both are present. | |
| 1429 | ||
| 1430 | ! Local variables | |
| 1431 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 1432 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 1433 | ! for checksums and output | |
| 1434 | 0 | real, pointer :: array(:,:,:) ! Field array on the input grid [A ~> a] |
| 1435 | 0 | real, allocatable, dimension(:,:,:) :: rescaled_array ! The array with scaling undone [a] |
| 1436 | 0 | real, allocatable :: hash_array(:,:,:) ! Subarray used to compute hash [a] |
| 1437 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 1438 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 1439 | integer :: iounit !< Log IO unit | |
| 1440 | integer :: i, j, k | |
| 1441 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 1442 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 1443 | integer :: bcN, bcS, bcE, bcW | |
| 1444 | logical :: do_corners | |
| 1445 | integer :: turns ! Quarter turns from input to model grid | |
| 1446 | ||
| 1447 | ! Rotate array to the input grid | |
| 1448 | 0 | turns = HI_m%turns |
| 1449 | 0 | if (modulo(turns, 4) /= 0) then |
| 1450 | 0 | allocate(HI) |
| 1451 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 1452 | 0 | allocate(array(HI%isd:HI%ied, HI%jsd:HI%jed, size(array_m, 3))) |
| 1453 | 0 | call rotate_array(array_m, -turns, array) |
| 1454 | else | |
| 1455 | 0 | HI => HI_m |
| 1456 | 0 | array => array_m |
| 1457 | endif | |
| 1458 | ||
| 1459 | 0 | if (checkForNaNs) then |
| 1460 | 0 | if (is_NaN(array(HI%isc:HI%iec,HI%jsc:HI%jec,:))) & |
| 1461 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 1462 | ! if (is_NaN(array)) & | |
| 1463 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 1464 | endif | |
| 1465 | ||
| 1466 | 0 | scaling = 1.0 |
| 1467 | 0 | if (present(unscale)) then ; scaling = unscale |
| 1468 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 1469 | ||
| 1470 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 1471 | ||
| 1472 | 0 | if (calculateStatistics) then |
| 1473 | 0 | if (present(unscale) .or. present(scale)) then |
| 1474 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 1475 | LBOUND(array,2):UBOUND(array,2), & | |
| 1476 | 0 | LBOUND(array,3):UBOUND(array,3)), source=0.0 ) |
| 1477 | 0 | do k=1,size(array,3) ; do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec |
| 1478 | 0 | rescaled_array(i,j,k) = scaling*array(i,j,k) |
| 1479 | enddo ; enddo ; enddo | |
| 1480 | ||
| 1481 | 0 | call subStats(HI, rescaled_array, aMean, aMin, aMax) |
| 1482 | 0 | deallocate(rescaled_array) |
| 1483 | else | |
| 1484 | 0 | call subStats(HI, array, aMean, aMin, aMax) |
| 1485 | endif | |
| 1486 | ||
| 1487 | 0 | if (is_root_pe()) & |
| 1488 | 0 | call chk_sum_msg("h-point:", aMean, aMin, aMax, mesg, iounit) |
| 1489 | endif | |
| 1490 | ||
| 1491 | 0 | if (.not.writeChksums) return |
| 1492 | ||
| 1493 | 0 | hshift = default_shift |
| 1494 | 0 | if (present(haloshift)) hshift = haloshift |
| 1495 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 1496 | ||
| 1497 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 1498 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 1499 | 0 | write(0,*) 'chksum_h_3d: haloshift =',hshift |
| 1500 | 0 | write(0,*) 'chksum_h_3d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 1501 | 0 | write(0,*) 'chksum_h_3d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 1502 | 0 | call chksum_error(FATAL,'Error in chksum_h_3d '//trim(mesg)) |
| 1503 | endif | |
| 1504 | ||
| 1505 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 1506 | ||
| 1507 | 0 | if (hshift==0) then |
| 1508 | 0 | if (is_root_pe()) call chk_sum_msg("h-point:", bc0, mesg, iounit) |
| 1509 | else | |
| 1510 | 0 | do_corners = .true. |
| 1511 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 1512 | ||
| 1513 | 0 | if (do_corners) then |
| 1514 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 1515 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 1516 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 1517 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 1518 | ||
| 1519 | 0 | if (is_root_pe()) & |
| 1520 | 0 | call chk_sum_msg("h-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 1521 | else | |
| 1522 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 1523 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 1524 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 1525 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 1526 | ||
| 1527 | 0 | if (is_root_pe()) & |
| 1528 | 0 | call chk_sum_msg_NSEW("h-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 1529 | endif | |
| 1530 | endif | |
| 1531 | ||
| 1532 | 0 | if (writeHash .and. is_root_pe()) then |
| 1533 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec, size(array, 3))) |
| 1534 | 0 | hash_array(:,:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec, :) |
| 1535 | ||
| 1536 | write(iounit, '("h-point: hash=", z8, 1x, a)') & | |
| 1537 | 0 | murmur_hash(hash_array), mesg |
| 1538 | 0 | deallocate(hash_array) |
| 1539 | endif | |
| 1540 | ||
| 1541 | contains | |
| 1542 | ||
| 1543 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 1544 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1545 | real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: array !< The array to be checksummed in | |
| 1546 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1547 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 1548 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 1549 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1550 | !! for checksums and output [a A-1 ~> 1] | |
| 1551 | integer :: i, j, k, bc | |
| 1552 | 0 | subchk = 0 |
| 1553 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do j=HI%jsc+dj,HI%jec+dj ; do i=HI%isc+di,HI%iec+di |
| 1554 | 0 | bc = bitcount(abs(unscale*array(i,j,k))) |
| 1555 | 0 | subchk = subchk + bc |
| 1556 | enddo ; enddo ; enddo | |
| 1557 | 0 | call sum_across_PEs(subchk) |
| 1558 | 0 | subchk=mod(subchk, bc_modulus) |
| 1559 | 0 | end function subchk |
| 1560 | ||
| 1561 | 0 | subroutine subStats(HI, array, aMean, aMin, aMax) |
| 1562 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1563 | real, dimension(HI%isd:,HI%jsd:,:), intent(in) :: array !< The array to be checksummed [a] | |
| 1564 | real, intent(out) :: aMean !< Array mean [a] | |
| 1565 | real, intent(out) :: aMin !< Array minimum [a] | |
| 1566 | real, intent(out) :: aMax !< Array maximum [a] | |
| 1567 | ||
| 1568 | integer :: i, j, k, n | |
| 1569 | ||
| 1570 | 0 | aMin = array(HI%isc,HI%jsc,1) |
| 1571 | 0 | aMax = array(HI%isc,HI%jsc,1) |
| 1572 | 0 | n = 0 |
| 1573 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do j=HI%jsc,HI%jec ; do i=HI%isc,HI%iec |
| 1574 | 0 | aMin = min(aMin, array(i,j,k)) |
| 1575 | 0 | aMax = max(aMax, array(i,j,k)) |
| 1576 | 0 | n = n + 1 |
| 1577 | enddo ; enddo ; enddo | |
| 1578 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec,:)) |
| 1579 | 0 | call sum_across_PEs(n) |
| 1580 | 0 | call min_across_PEs(aMin) |
| 1581 | 0 | call max_across_PEs(aMax) |
| 1582 | 0 | aMean = aMean / real(n) |
| 1583 | 0 | end subroutine subStats |
| 1584 | ||
| 1585 | end subroutine chksum_h_3d | |
| 1586 | ||
| 1587 | !> Checksums a 3d array staggered at corner points. | |
| 1588 | 0 | subroutine chksum_B_3d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 1589 | scale, logunit, unscale) | |
| 1590 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1591 | real, dimension(HI_m%IsdB:,HI_m%JsdB:,:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1592 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1593 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1594 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1595 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 1596 | !! symmetric computational domain. | |
| 1597 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1598 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1599 | !! for checksums and output [a A-1 ~> 1] | |
| 1600 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 1601 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1602 | !! for checksums and output [a A-1 ~> 1]. | |
| 1603 | !! Here scale and unscale are synonymous, but unscale | |
| 1604 | !! takes precedence if both are present. | |
| 1605 | ||
| 1606 | ! Local variables | |
| 1607 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 1608 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 1609 | ! for checksums and output | |
| 1610 | 0 | real, pointer :: array(:,:,:) ! Field array on the input grid [A ~> a] |
| 1611 | 0 | real, allocatable, dimension(:,:,:) :: rescaled_array ! The array with scaling undone [a] |
| 1612 | 0 | real, allocatable :: hash_array(:,:,:) ! Subarray used to compute hash [a] |
| 1613 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 1614 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 1615 | integer :: iounit !< Log IO unit | |
| 1616 | integer :: i, j, k, Is, Js | |
| 1617 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 1618 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 1619 | integer :: bcN, bcS, bcE, bcW | |
| 1620 | logical :: do_corners, sym, sym_stats | |
| 1621 | integer :: turns ! Quarter turns from input to model grid | |
| 1622 | ||
| 1623 | ! Rotate array to the input grid | |
| 1624 | 0 | turns = HI_m%turns |
| 1625 | 0 | if (modulo(turns, 4) /= 0) then |
| 1626 | 0 | allocate(HI) |
| 1627 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 1628 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%JsdB:HI%JedB, size(array_m, 3))) |
| 1629 | 0 | call rotate_array(array_m, -turns, array) |
| 1630 | else | |
| 1631 | 0 | HI => HI_m |
| 1632 | 0 | array => array_m |
| 1633 | endif | |
| 1634 | ||
| 1635 | 0 | if (checkForNaNs) then |
| 1636 | 0 | if (is_NaN(array(HI%IscB:HI%IecB,HI%JscB:HI%JecB,:))) & |
| 1637 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 1638 | ! if (is_NaN(array)) & | |
| 1639 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 1640 | endif | |
| 1641 | ||
| 1642 | 0 | scaling = 1.0 |
| 1643 | 0 | if (present(unscale)) then ; scaling = unscale |
| 1644 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 1645 | ||
| 1646 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 1647 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 1648 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 1649 | ||
| 1650 | 0 | if (calculateStatistics) then |
| 1651 | 0 | if (present(unscale) .or. present(scale)) then |
| 1652 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 1653 | LBOUND(array,2):UBOUND(array,2), & | |
| 1654 | 0 | LBOUND(array,3):UBOUND(array,3)), source=0.0 ) |
| 1655 | 0 | Is = HI%isc ; if (sym_stats) Is = HI%isc-1 |
| 1656 | 0 | Js = HI%jsc ; if (sym_stats) Js = HI%jsc-1 |
| 1657 | 0 | do k=1,size(array,3) ; do J=Js,HI%JecB ; do I=Is,HI%IecB |
| 1658 | 0 | rescaled_array(I,J,k) = scaling*array(I,J,k) |
| 1659 | enddo ; enddo ; enddo | |
| 1660 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 1661 | 0 | deallocate(rescaled_array) |
| 1662 | else | |
| 1663 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1664 | endif | |
| 1665 | ||
| 1666 | 0 | if (is_root_pe()) & |
| 1667 | 0 | call chk_sum_msg("B-point:", aMean, aMin, aMax, mesg, iounit) |
| 1668 | endif | |
| 1669 | ||
| 1670 | 0 | if (.not.writeChksums) return |
| 1671 | ||
| 1672 | 0 | hshift = default_shift |
| 1673 | 0 | if (present(haloshift)) hshift = haloshift |
| 1674 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 1675 | ||
| 1676 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 1677 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 1678 | 0 | write(0,*) 'chksum_B_3d: haloshift =',hshift |
| 1679 | 0 | write(0,*) 'chksum_B_3d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 1680 | 0 | write(0,*) 'chksum_B_3d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 1681 | 0 | call chksum_error(FATAL,'Error in chksum_B_3d '//trim(mesg)) |
| 1682 | endif | |
| 1683 | ||
| 1684 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 1685 | ||
| 1686 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 1687 | ||
| 1688 | 0 | if ((hshift==0) .and. .not.sym) then |
| 1689 | 0 | if (is_root_pe()) call chk_sum_msg("B-point:", bc0, mesg, iounit) |
| 1690 | else | |
| 1691 | 0 | do_corners = .true. |
| 1692 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 1693 | ||
| 1694 | 0 | if (do_corners) then |
| 1695 | 0 | if (sym) then |
| 1696 | 0 | bcSW = subchk(array, HI, -hshift-1, -hshift-1, scaling) |
| 1697 | 0 | bcSE = subchk(array, HI, hshift, -hshift-1, scaling) |
| 1698 | 0 | bcNW = subchk(array, HI, -hshift-1, hshift, scaling) |
| 1699 | else | |
| 1700 | 0 | bcSW = subchk(array, HI, -hshift-1, -hshift-1, scaling) |
| 1701 | 0 | bcSE = subchk(array, HI, hshift, -hshift-1, scaling) |
| 1702 | 0 | bcNW = subchk(array, HI, -hshift-1, hshift, scaling) |
| 1703 | endif | |
| 1704 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 1705 | ||
| 1706 | 0 | if (is_root_pe()) & |
| 1707 | 0 | call chk_sum_msg("B-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 1708 | else | |
| 1709 | 0 | if (sym) then |
| 1710 | 0 | bcS = subchk(array, HI, 0, -hshift-1, scaling) |
| 1711 | 0 | bcW = subchk(array, HI, -hshift-1, 0, scaling) |
| 1712 | else | |
| 1713 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 1714 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 1715 | endif | |
| 1716 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 1717 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 1718 | ||
| 1719 | 0 | if (is_root_pe()) & |
| 1720 | 0 | call chk_sum_msg_NSEW("B-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 1721 | endif | |
| 1722 | endif | |
| 1723 | ||
| 1724 | 0 | if (writeHash .and. is_root_pe()) then |
| 1725 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec, size(array, 3))) |
| 1726 | 0 | hash_array(:,:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec, :) |
| 1727 | ||
| 1728 | write(iounit, '("B-point: hash=", z8, 1x, a)') & | |
| 1729 | 0 | murmur_hash(hash_array), mesg |
| 1730 | 0 | deallocate(hash_array) |
| 1731 | endif | |
| 1732 | ||
| 1733 | contains | |
| 1734 | ||
| 1735 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 1736 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1737 | real, dimension(HI%IsdB:,HI%JsdB:,:), intent(in) :: array !< The array to be checksummed in | |
| 1738 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1739 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 1740 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 1741 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1742 | !! for checksums and output [a A-1 ~> 1] | |
| 1743 | integer :: i, j, k, bc | |
| 1744 | 0 | subchk = 0 |
| 1745 | ! This line deliberately uses the h-point computational domain. | |
| 1746 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do J=HI%jsc+dj,HI%jec+dj ; do I=HI%isc+di,HI%iec+di |
| 1747 | 0 | bc = bitcount(abs(unscale*array(I,J,k))) |
| 1748 | 0 | subchk = subchk + bc |
| 1749 | enddo ; enddo ; enddo | |
| 1750 | 0 | call sum_across_PEs(subchk) |
| 1751 | 0 | subchk=mod(subchk, bc_modulus) |
| 1752 | 0 | end function subchk |
| 1753 | ||
| 1754 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1755 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1756 | real, dimension(HI%IsdB:,HI%JsdB:,:), intent(in) :: array !< The array to be checksummed [a] | |
| 1757 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 1758 | !! full symmetric computational domain. | |
| 1759 | real, intent(out) :: aMean !< Array mean [a] | |
| 1760 | real, intent(out) :: aMin !< Array minimum [a] | |
| 1761 | real, intent(out) :: aMax !< Array maximum [a] | |
| 1762 | ||
| 1763 | integer :: i, j, k, n, IsB, JsB | |
| 1764 | ||
| 1765 | 0 | IsB = HI%isc ; if (sym_stats) IsB = HI%isc-1 |
| 1766 | 0 | JsB = HI%jsc ; if (sym_stats) JsB = HI%jsc-1 |
| 1767 | ||
| 1768 | 0 | aMin = array(HI%isc,HI%jsc,1) ; aMax = aMin |
| 1769 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do J=JsB,HI%JecB ; do I=IsB,HI%IecB |
| 1770 | 0 | aMin = min(aMin, array(I,J,k)) |
| 1771 | 0 | aMax = max(aMax, array(I,J,k)) |
| 1772 | enddo ; enddo ; enddo | |
| 1773 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec,:)) |
| 1774 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) * size(array,3) |
| 1775 | 0 | call sum_across_PEs(n) |
| 1776 | 0 | call min_across_PEs(aMin) |
| 1777 | 0 | call max_across_PEs(aMax) |
| 1778 | 0 | aMean = aMean / real(n) |
| 1779 | 0 | end subroutine subStats |
| 1780 | ||
| 1781 | end subroutine chksum_B_3d | |
| 1782 | ||
| 1783 | !> Checksums a 3d array staggered at C-grid u points. | |
| 1784 | 0 | subroutine chksum_u_3d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 1785 | scale, logunit, unscale) | |
| 1786 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1787 | real, dimension(HI_m%isdB:,HI_m%Jsd:,:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1788 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1789 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1790 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1791 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 1792 | !! symmetric computational domain. | |
| 1793 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1794 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1795 | !! for checksums and output [a A-1 ~> 1] | |
| 1796 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 1797 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1798 | !! for checksums and output [a A-1 ~> 1]. | |
| 1799 | !! Here scale and unscale are synonymous, but unscale | |
| 1800 | !! takes precedence if both are present. | |
| 1801 | ||
| 1802 | ! Local variables | |
| 1803 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 1804 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 1805 | ! for checksums and output | |
| 1806 | 0 | real, pointer :: array(:,:,:) ! Field array on the input grid [A ~> a] |
| 1807 | 0 | real, allocatable, dimension(:,:,:) :: rescaled_array ! The array with scaling undone [a] |
| 1808 | 0 | real, allocatable :: hash_array(:,:,:) ! Subarray used to compute hash [a] |
| 1809 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 1810 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 1811 | integer :: iounit !< Log IO unit | |
| 1812 | integer :: i, j, k, Is | |
| 1813 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 1814 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 1815 | integer :: bcN, bcS, bcE, bcW | |
| 1816 | logical :: do_corners, sym, sym_stats | |
| 1817 | integer :: turns ! Quarter turns from input to model grid | |
| 1818 | ||
| 1819 | ! Rotate array to the input grid | |
| 1820 | 0 | turns = HI_m%turns |
| 1821 | 0 | if (modulo(turns, 4) /= 0) then |
| 1822 | 0 | allocate(HI) |
| 1823 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 1824 | 0 | if (modulo(turns, 2) /= 0) then |
| 1825 | ! Arrays originating from v-points must be handled by vchksum | |
| 1826 | 0 | allocate(array(HI%isd:HI%ied, HI%JsdB:HI%JedB, size(array_m, 3))) |
| 1827 | 0 | call rotate_array(array_m, -turns, array) |
| 1828 | call vchksum(array, mesg, HI, haloshift, symmetric, omit_corners, & | |
| 1829 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 1830 | 0 | return |
| 1831 | else | |
| 1832 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%jsd:HI%jed, size(array_m, 3))) |
| 1833 | 0 | call rotate_array(array_m, -turns, array) |
| 1834 | endif | |
| 1835 | else | |
| 1836 | 0 | HI => HI_m |
| 1837 | 0 | array => array_m |
| 1838 | endif | |
| 1839 | ||
| 1840 | 0 | if (checkForNaNs) then |
| 1841 | 0 | if (is_NaN(array(HI%IscB:HI%IecB,HI%jsc:HI%jec,:))) & |
| 1842 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 1843 | ! if (is_NaN(array)) & | |
| 1844 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 1845 | endif | |
| 1846 | ||
| 1847 | 0 | scaling = 1.0 |
| 1848 | 0 | if (present(unscale)) then ; scaling = unscale |
| 1849 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 1850 | ||
| 1851 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 1852 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 1853 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 1854 | ||
| 1855 | 0 | if (calculateStatistics) then |
| 1856 | 0 | if (present(unscale) .or. present(scale)) then |
| 1857 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 1858 | LBOUND(array,2):UBOUND(array,2), & | |
| 1859 | 0 | LBOUND(array,3):UBOUND(array,3)), source=0.0 ) |
| 1860 | 0 | Is = HI%isc ; if (sym_stats) Is = HI%isc-1 |
| 1861 | 0 | do k=1,size(array,3) ; do j=HI%jsc,HI%jec ; do I=Is,HI%IecB |
| 1862 | 0 | rescaled_array(I,j,k) = scaling*array(I,j,k) |
| 1863 | enddo ; enddo ; enddo | |
| 1864 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 1865 | 0 | deallocate(rescaled_array) |
| 1866 | else | |
| 1867 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1868 | endif | |
| 1869 | 0 | if (is_root_pe()) & |
| 1870 | 0 | call chk_sum_msg("u-point:", aMean, aMin, aMax, mesg, iounit) |
| 1871 | endif | |
| 1872 | ||
| 1873 | 0 | if (.not.writeChksums) return |
| 1874 | ||
| 1875 | 0 | hshift = default_shift |
| 1876 | 0 | if (present(haloshift)) hshift = haloshift |
| 1877 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 1878 | ||
| 1879 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 1880 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 1881 | 0 | write(0,*) 'chksum_u_3d: haloshift =',hshift |
| 1882 | 0 | write(0,*) 'chksum_u_3d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 1883 | 0 | write(0,*) 'chksum_u_3d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 1884 | 0 | call chksum_error(FATAL,'Error in chksum_u_3d '//trim(mesg)) |
| 1885 | endif | |
| 1886 | ||
| 1887 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 1888 | ||
| 1889 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 1890 | ||
| 1891 | 0 | if ((hshift==0) .and. .not.sym) then |
| 1892 | 0 | if (is_root_pe()) call chk_sum_msg("u-point:", bc0, mesg, iounit) |
| 1893 | else | |
| 1894 | 0 | do_corners = .true. |
| 1895 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 1896 | ||
| 1897 | 0 | if (hshift==0) then |
| 1898 | 0 | bcW = subchk(array, HI, -hshift-1, 0, scaling) |
| 1899 | 0 | if (is_root_pe()) call chk_sum_msg_W("u-point:", bc0, bcW, mesg, iounit) |
| 1900 | 0 | elseif (do_corners) then |
| 1901 | 0 | if (sym) then |
| 1902 | 0 | bcSW = subchk(array, HI, -hshift-1, -hshift, scaling) |
| 1903 | 0 | bcNW = subchk(array, HI, -hshift-1, hshift, scaling) |
| 1904 | else | |
| 1905 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 1906 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 1907 | endif | |
| 1908 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 1909 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 1910 | ||
| 1911 | 0 | if (is_root_pe()) & |
| 1912 | 0 | call chk_sum_msg("u-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 1913 | else | |
| 1914 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 1915 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 1916 | 0 | if (sym) then |
| 1917 | 0 | bcW = subchk(array, HI, -hshift-1, 0, scaling) |
| 1918 | else | |
| 1919 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 1920 | endif | |
| 1921 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 1922 | ||
| 1923 | 0 | if (is_root_pe()) & |
| 1924 | 0 | call chk_sum_msg_NSEW("u-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 1925 | endif | |
| 1926 | endif | |
| 1927 | ||
| 1928 | 0 | if (writeHash .and. is_root_pe()) then |
| 1929 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec, size(array, 3))) |
| 1930 | 0 | hash_array(:,:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec, :) |
| 1931 | ||
| 1932 | write(iounit, '("u-point: hash=", z8, 1x, a)') & | |
| 1933 | 0 | murmur_hash(hash_array), mesg |
| 1934 | 0 | deallocate(hash_array) |
| 1935 | endif | |
| 1936 | ||
| 1937 | contains | |
| 1938 | ||
| 1939 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 1940 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1941 | real, dimension(HI%IsdB:,HI%jsd:,:), intent(in) :: array !< The array to be checksummed in | |
| 1942 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1943 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 1944 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 1945 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 1946 | !! for checksums and output [a A-1 ~> 1] | |
| 1947 | integer :: i, j, k, bc | |
| 1948 | 0 | subchk = 0 |
| 1949 | ! This line deliberately uses the h-point computational domain. | |
| 1950 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do j=HI%jsc+dj,HI%jec+dj ; do I=HI%isc+di,HI%iec+di |
| 1951 | 0 | bc = bitcount(abs(unscale*array(I,j,k))) |
| 1952 | 0 | subchk = subchk + bc |
| 1953 | enddo ; enddo ; enddo | |
| 1954 | 0 | call sum_across_PEs(subchk) |
| 1955 | 0 | subchk=mod(subchk, bc_modulus) |
| 1956 | 0 | end function subchk |
| 1957 | ||
| 1958 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 1959 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 1960 | real, dimension(HI%IsdB:,HI%jsd:,:), intent(in) :: array !< The array to be checksummed [a] | |
| 1961 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 1962 | !! full symmetric computational domain. | |
| 1963 | real, intent(out) :: aMean !< Array mean [a] | |
| 1964 | real, intent(out) :: aMin !< Array minimum [a] | |
| 1965 | real, intent(out) :: aMax !< Array maximum [a] | |
| 1966 | ||
| 1967 | integer :: i, j, k, n, IsB | |
| 1968 | ||
| 1969 | 0 | IsB = HI%isc ; if (sym_stats) IsB = HI%isc-1 |
| 1970 | ||
| 1971 | 0 | aMin = array(HI%isc,HI%jsc,1) ; aMax = aMin |
| 1972 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do j=HI%jsc,HI%jec ; do I=IsB,HI%IecB |
| 1973 | 0 | aMin = min(aMin, array(I,j,k)) |
| 1974 | 0 | aMax = max(aMax, array(I,j,k)) |
| 1975 | enddo ; enddo ; enddo | |
| 1976 | ! This line deliberately uses the h-point computational domain. | |
| 1977 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec,:)) |
| 1978 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) * size(array,3) |
| 1979 | 0 | call sum_across_PEs(n) |
| 1980 | 0 | call min_across_PEs(aMin) |
| 1981 | 0 | call max_across_PEs(aMax) |
| 1982 | 0 | aMean = aMean / real(n) |
| 1983 | 0 | end subroutine subStats |
| 1984 | ||
| 1985 | end subroutine chksum_u_3d | |
| 1986 | ||
| 1987 | !> Checksums a 3d array staggered at C-grid v points. | |
| 1988 | 0 | subroutine chksum_v_3d(array_m, mesg, HI_m, haloshift, symmetric, omit_corners, & |
| 1989 | scale, logunit, unscale) | |
| 1990 | type(hor_index_type), target, intent(in) :: HI_m !< A horizontal index type | |
| 1991 | real, dimension(HI_m%isd:,HI_m%JsdB:,:), target, intent(in) :: array_m !< The array to be checksummed in | |
| 1992 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 1993 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 1994 | integer, optional, intent(in) :: haloshift !< The width of halos to check (default 0) | |
| 1995 | logical, optional, intent(in) :: symmetric !< If true, do the checksums on the full | |
| 1996 | !! symmetric computational domain. | |
| 1997 | logical, optional, intent(in) :: omit_corners !< If true, avoid checking diagonal shifts | |
| 1998 | real, optional, intent(in) :: scale !< A factor to convert this array back to unscaled units | |
| 1999 | !! for checksums and output [a A-1 ~> 1] | |
| 2000 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 2001 | real, optional, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 2002 | !! for checksums and output [a A-1 ~> 1]. | |
| 2003 | !! Here scale and unscale are synonymous, but unscale | |
| 2004 | !! takes precedence if both are present. | |
| 2005 | ||
| 2006 | ! Local variables | |
| 2007 | ! In the following comments, [A] is used to indicate the arbitrary, possibly rescaled units | |
| 2008 | ! of the input array while [a] indicates the unscaled (e.g., mks) units that should be used | |
| 2009 | ! for checksums and output | |
| 2010 | 0 | real, pointer :: array(:,:,:) ! Field array on the input grid [A ~> a] |
| 2011 | 0 | real, allocatable, dimension(:,:,:) :: rescaled_array ! The array with scaling undone [a] |
| 2012 | 0 | real, allocatable :: hash_array(:,:,:) ! Subarray used to compute hash [a] |
| 2013 | type(hor_index_type), pointer :: HI ! Horizontal index bounds of the input grid | |
| 2014 | real :: scaling ! Explicit rescaling factor [a A-1 ~> 1] | |
| 2015 | integer :: iounit !< Log IO unit | |
| 2016 | integer :: i, j, k, Js | |
| 2017 | integer :: bc0, bcSW, bcSE, bcNW, bcNE, hshift | |
| 2018 | integer :: bcN, bcS, bcE, bcW | |
| 2019 | real :: aMean, aMin, aMax ! Array mean, global minimum and global maximum [a] | |
| 2020 | logical :: do_corners, sym, sym_stats | |
| 2021 | integer :: turns ! Quarter turns from input to model grid | |
| 2022 | ||
| 2023 | ! Rotate array to the input grid | |
| 2024 | 0 | turns = HI_m%turns |
| 2025 | 0 | if (modulo(turns, 4) /= 0) then |
| 2026 | 0 | allocate(HI) |
| 2027 | 0 | call rotate_hor_index(HI_m, -turns, HI) |
| 2028 | 0 | if (modulo(turns, 2) /= 0) then |
| 2029 | ! Arrays originating from u-points must be handled by uchksum | |
| 2030 | 0 | allocate(array(HI%IsdB:HI%IedB, HI%jsd:HI%jed, size(array_m, 3))) |
| 2031 | 0 | call rotate_array(array_m, -turns, array) |
| 2032 | call uchksum(array, mesg, HI, haloshift, symmetric, omit_corners, & | |
| 2033 | 0 | scale=scale, logunit=logunit, unscale=unscale) |
| 2034 | 0 | return |
| 2035 | else | |
| 2036 | 0 | allocate(array(HI%isd:HI%ied, HI%JsdB:HI%JedB, size(array_m, 3))) |
| 2037 | 0 | call rotate_array(array_m, -turns, array) |
| 2038 | endif | |
| 2039 | else | |
| 2040 | 0 | HI => HI_m |
| 2041 | 0 | array => array_m |
| 2042 | endif | |
| 2043 | ||
| 2044 | 0 | if (checkForNaNs) then |
| 2045 | 0 | if (is_NaN(array(HI%isc:HI%iec,HI%JscB:HI%JecB,:))) & |
| 2046 | 0 | call chksum_error(FATAL, 'NaN detected: '//trim(mesg)) |
| 2047 | ! if (is_NaN(array)) & | |
| 2048 | ! call chksum_error(FATAL, 'NaN detected in halo: '//trim(mesg)) | |
| 2049 | endif | |
| 2050 | ||
| 2051 | 0 | scaling = 1.0 |
| 2052 | 0 | if (present(unscale)) then ; scaling = unscale |
| 2053 | 0 | elseif (present(scale)) then ; scaling = scale ; endif |
| 2054 | ||
| 2055 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 2056 | 0 | sym_stats = .false. ; if (present(symmetric)) sym_stats = symmetric |
| 2057 | 0 | if (present(haloshift)) then ; if (haloshift > 0) sym_stats = .true. ; endif |
| 2058 | ||
| 2059 | 0 | if (calculateStatistics) then |
| 2060 | 0 | if (present(unscale) .or. present(scale)) then |
| 2061 | 0 | allocate( rescaled_array(LBOUND(array,1):UBOUND(array,1), & |
| 2062 | LBOUND(array,2):UBOUND(array,2), & | |
| 2063 | 0 | LBOUND(array,3):UBOUND(array,3)), source=0.0 ) |
| 2064 | 0 | Js = HI%jsc ; if (sym_stats) Js = HI%jsc-1 |
| 2065 | 0 | do k=1,size(array,3) ; do J=Js,HI%JecB ; do i=HI%isc,HI%iec |
| 2066 | 0 | rescaled_array(i,J,k) = scaling*array(i,J,k) |
| 2067 | enddo ; enddo ; enddo | |
| 2068 | 0 | call subStats(HI, rescaled_array, sym_stats, aMean, aMin, aMax) |
| 2069 | 0 | deallocate(rescaled_array) |
| 2070 | else | |
| 2071 | 0 | call subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 2072 | endif | |
| 2073 | 0 | if (is_root_pe()) & |
| 2074 | 0 | call chk_sum_msg("v-point:", aMean, aMin, aMax, mesg, iounit) |
| 2075 | endif | |
| 2076 | ||
| 2077 | 0 | if (.not.writeChksums) return |
| 2078 | ||
| 2079 | 0 | hshift = default_shift |
| 2080 | 0 | if (present(haloshift)) hshift = haloshift |
| 2081 | 0 | if (hshift<0) hshift = HI%ied-HI%iec |
| 2082 | ||
| 2083 | if ( HI%isc-hshift<HI%isd .or. HI%iec+hshift>HI%ied .or. & | |
| 2084 | 0 | HI%jsc-hshift<HI%jsd .or. HI%jec+hshift>HI%jed ) then |
| 2085 | 0 | write(0,*) 'chksum_v_3d: haloshift =',hshift |
| 2086 | 0 | write(0,*) 'chksum_v_3d: isd,isc,iec,ied=',HI%isd,HI%isc,HI%iec,HI%ied |
| 2087 | 0 | write(0,*) 'chksum_v_3d: jsd,jsc,jec,jed=',HI%jsd,HI%jsc,HI%jec,HI%jed |
| 2088 | 0 | call chksum_error(FATAL,'Error in chksum_v_3d '//trim(mesg)) |
| 2089 | endif | |
| 2090 | ||
| 2091 | 0 | bc0 = subchk(array, HI, 0, 0, scaling) |
| 2092 | ||
| 2093 | 0 | sym = .false. ; if (present(symmetric)) sym = symmetric |
| 2094 | ||
| 2095 | 0 | if ((hshift==0) .and. .not.sym) then |
| 2096 | 0 | if (is_root_pe()) call chk_sum_msg("v-point:", bc0, mesg, iounit) |
| 2097 | else | |
| 2098 | 0 | do_corners = .true. |
| 2099 | 0 | if (present(omit_corners)) do_corners = .not. omit_corners |
| 2100 | ||
| 2101 | 0 | if (hshift==0) then |
| 2102 | 0 | bcS = subchk(array, HI, 0, -hshift-1, scaling) |
| 2103 | 0 | if (is_root_pe()) call chk_sum_msg_S("v-point:", bc0, bcS, mesg, iounit) |
| 2104 | 0 | elseif (do_corners) then |
| 2105 | 0 | if (sym) then |
| 2106 | 0 | bcSW = subchk(array, HI, -hshift, -hshift-1, scaling) |
| 2107 | 0 | bcSE = subchk(array, HI, hshift, -hshift-1, scaling) |
| 2108 | else | |
| 2109 | 0 | bcSW = subchk(array, HI, -hshift, -hshift, scaling) |
| 2110 | 0 | bcSE = subchk(array, HI, hshift, -hshift, scaling) |
| 2111 | endif | |
| 2112 | 0 | bcNW = subchk(array, HI, -hshift, hshift, scaling) |
| 2113 | 0 | bcNE = subchk(array, HI, hshift, hshift, scaling) |
| 2114 | ||
| 2115 | 0 | if (is_root_pe()) & |
| 2116 | 0 | call chk_sum_msg("v-point:", bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 2117 | else | |
| 2118 | 0 | if (sym) then |
| 2119 | 0 | bcS = subchk(array, HI, 0, -hshift-1, scaling) |
| 2120 | else | |
| 2121 | 0 | bcS = subchk(array, HI, 0, -hshift, scaling) |
| 2122 | endif | |
| 2123 | 0 | bcE = subchk(array, HI, hshift, 0, scaling) |
| 2124 | 0 | bcW = subchk(array, HI, -hshift, 0, scaling) |
| 2125 | 0 | bcN = subchk(array, HI, 0, hshift, scaling) |
| 2126 | ||
| 2127 | 0 | if (is_root_pe()) & |
| 2128 | 0 | call chk_sum_msg_NSEW("v-point:", bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 2129 | endif | |
| 2130 | endif | |
| 2131 | ||
| 2132 | 0 | if (writeHash .and. is_root_pe()) then |
| 2133 | 0 | allocate(hash_array(HI%isc:HI%iec, HI%jsc:HI%jec, size(array, 3))) |
| 2134 | 0 | hash_array(:,:,:) = scaling * array(HI%isc:HI%iec, HI%jsc:HI%jec, :) |
| 2135 | ||
| 2136 | write(iounit, '("v-point: hash=", z8, 1x, a)') & | |
| 2137 | 0 | murmur_hash(hash_array), mesg |
| 2138 | 0 | deallocate(hash_array) |
| 2139 | endif | |
| 2140 | ||
| 2141 | contains | |
| 2142 | ||
| 2143 | 0 | integer function subchk(array, HI, di, dj, unscale) |
| 2144 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 2145 | real, dimension(HI%isd:,HI%JsdB:,:), intent(in) :: array !< The array to be checksummed in | |
| 2146 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 2147 | integer, intent(in) :: di !< i- direction array shift for this checksum | |
| 2148 | integer, intent(in) :: dj !< j- direction array shift for this checksum | |
| 2149 | real, intent(in) :: unscale !< A factor to convert this array back to unscaled units | |
| 2150 | !! for checksums and output [a A-1 ~> 1] | |
| 2151 | integer :: i, j, k, bc | |
| 2152 | 0 | subchk = 0 |
| 2153 | ! This line deliberately uses the h-point computational domain. | |
| 2154 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do J=HI%jsc+dj,HI%jec+dj ; do i=HI%isc+di,HI%iec+di |
| 2155 | 0 | bc = bitcount(abs(unscale*array(i,J,k))) |
| 2156 | 0 | subchk = subchk + bc |
| 2157 | enddo ; enddo ; enddo | |
| 2158 | 0 | call sum_across_PEs(subchk) |
| 2159 | 0 | subchk=mod(subchk, bc_modulus) |
| 2160 | 0 | end function subchk |
| 2161 | ||
| 2162 | !subroutine subStats(HI, array, mesg, sym_stats) | |
| 2163 | 0 | subroutine subStats(HI, array, sym_stats, aMean, aMin, aMax) |
| 2164 | type(hor_index_type), intent(in) :: HI !< A horizontal index type | |
| 2165 | real, dimension(HI%isd:,HI%JsdB:,:), intent(in) :: array !< The array to be checksummed [a] | |
| 2166 | logical, intent(in) :: sym_stats !< If true, evaluate the statistics on the | |
| 2167 | !! full symmetric computational domain. | |
| 2168 | real, intent(out) :: aMean !< Mean of array over domain [a] | |
| 2169 | real, intent(out) :: aMin !< Minimum of array over domain [a] | |
| 2170 | real, intent(out) :: aMax !< Maximum of array over domain [a] | |
| 2171 | ||
| 2172 | integer :: i, j, k, n, JsB | |
| 2173 | ||
| 2174 | 0 | JsB = HI%jsc ; if (sym_stats) JsB = HI%jsc-1 |
| 2175 | ||
| 2176 | 0 | aMin = array(HI%isc,HI%jsc,1) ; aMax = aMin |
| 2177 | 0 | do k=LBOUND(array,3),UBOUND(array,3) ; do J=JsB,HI%JecB ; do i=HI%isc,HI%iec |
| 2178 | 0 | aMin = min(aMin, array(i,J,k)) |
| 2179 | 0 | aMax = max(aMax, array(i,J,k)) |
| 2180 | enddo ; enddo ; enddo | |
| 2181 | ! This line deliberately uses the h-point computational domain. | |
| 2182 | 0 | aMean = reproducing_sum(array(HI%isc:HI%iec,HI%jsc:HI%jec,:)) |
| 2183 | 0 | n = (1 + HI%jec - HI%jsc) * (1 + HI%iec - HI%isc) * size(array,3) |
| 2184 | 0 | call sum_across_PEs(n) |
| 2185 | 0 | call min_across_PEs(aMin) |
| 2186 | 0 | call max_across_PEs(aMax) |
| 2187 | 0 | aMean = aMean / real(n) |
| 2188 | 0 | end subroutine subStats |
| 2189 | ||
| 2190 | end subroutine chksum_v_3d | |
| 2191 | ||
| 2192 | ! These are the older version of chksum that do not take the grid staggering | |
| 2193 | ! into account. | |
| 2194 | ||
| 2195 | !> chksum1d does a checksum of a 1-dimensional array. | |
| 2196 | 0 | subroutine chksum1d(array, mesg, start_i, end_i, compare_PEs, logunit) |
| 2197 | real, dimension(:), intent(in) :: array !< The array to be summed (index starts at 1) in arbitrary units [A]. | |
| 2198 | character(len=*), intent(in) :: mesg !< An identifying message. | |
| 2199 | integer, optional, intent(in) :: start_i !< The starting index for the sum (default 1) | |
| 2200 | integer, optional, intent(in) :: end_i !< The ending index for the sum (default all) | |
| 2201 | logical, optional, intent(in) :: compare_PEs !< If true, compare across PEs instead of summing | |
| 2202 | !! and list the root_PE value (default true) | |
| 2203 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 2204 | ||
| 2205 | integer :: is, ie, i, bc, sum1, sum_bc, ioUnit | |
| 2206 | real :: sum ! The global sum of the array [A] | |
| 2207 | 0 | real, allocatable :: sum_here(:) ! The sum on each PE [A] |
| 2208 | logical :: compare | |
| 2209 | integer :: pe_num ! pe number of the data | |
| 2210 | integer :: nPEs ! Total number of processsors | |
| 2211 | ||
| 2212 | 0 | is = LBOUND(array,1) ; ie = UBOUND(array,1) |
| 2213 | 0 | if (present(start_i)) is = start_i |
| 2214 | 0 | if (present(end_i)) ie = end_i |
| 2215 | 0 | compare = .true. ; if (present(compare_PEs)) compare = compare_PEs |
| 2216 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 2217 | ||
| 2218 | 0 | sum = 0.0 ; sum_bc = 0 |
| 2219 | 0 | do i=is,ie |
| 2220 | 0 | sum = sum + array(i) |
| 2221 | 0 | bc = bitcount(ABS(array(i))) |
| 2222 | 0 | sum_bc = sum_bc + bc |
| 2223 | enddo | |
| 2224 | ||
| 2225 | 0 | pe_num = pe_here() + 1 - root_pe() ; nPEs = num_pes() |
| 2226 | 0 | allocate(sum_here(nPEs), source=0.0) ; sum_here(pe_num) = sum |
| 2227 | 0 | call sum_across_PEs(sum_here,nPEs) |
| 2228 | ||
| 2229 | 0 | sum1 = sum_bc |
| 2230 | 0 | call sum_across_PEs(sum1) |
| 2231 | ||
| 2232 | 0 | if (.not.compare) then |
| 2233 | 0 | sum = 0.0 |
| 2234 | 0 | do i=1,nPEs ; sum = sum + sum_here(i) ; enddo |
| 2235 | 0 | sum_bc = sum1 |
| 2236 | 0 | elseif (is_root_pe()) then |
| 2237 | 0 | if (sum1 /= nPEs*sum_bc) & |
| 2238 | write(iounit, '(A40," bitcounts do not match across PEs: ",I12,1X,I12)') & | |
| 2239 | 0 | mesg, sum1, nPEs*sum_bc |
| 2240 | 0 | do i=1,nPEs ; if (sum /= sum_here(i)) then |
| 2241 | write(iounit, '(A40," PE ",I0," sum mismatches root_PE: ",3(ES22.13,1X))') & | |
| 2242 | 0 | mesg, i, sum_here(i), sum, sum_here(i)-sum |
| 2243 | endif ; enddo | |
| 2244 | endif | |
| 2245 | 0 | deallocate(sum_here) |
| 2246 | ||
| 2247 | 0 | if (is_root_pe()) & |
| 2248 | 0 | write(iounit,'(A50,1X,ES25.16,1X,I12)') mesg, sum, sum_bc |
| 2249 | ||
| 2250 | 0 | end subroutine chksum1d |
| 2251 | ||
| 2252 | ! These are the older version of chksum that do not take the grid staggering | |
| 2253 | ! into account. | |
| 2254 | ||
| 2255 | !> chksum2d does a checksum of all data in a 2-d array. | |
| 2256 | 0 | subroutine chksum2d(array, mesg, logunit) |
| 2257 | ||
| 2258 | real, dimension(:,:), intent(in) :: array !< The array to be checksummed in arbitrary units [A] | |
| 2259 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 2260 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 2261 | ||
| 2262 | integer :: xs, xe, ys, ye, i, j, sum1, bc, iounit | |
| 2263 | real :: sum ! The global sum of the array [A] | |
| 2264 | ||
| 2265 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 2266 | ||
| 2267 | 0 | xs = LBOUND(array,1) ; xe = UBOUND(array,1) |
| 2268 | 0 | ys = LBOUND(array,2) ; ye = UBOUND(array,2) |
| 2269 | ||
| 2270 | 0 | sum = 0.0 ; sum1 = 0 |
| 2271 | 0 | do i=xs,xe ; do j=ys,ye |
| 2272 | 0 | bc = bitcount(abs(array(i,j))) |
| 2273 | 0 | sum1 = sum1 + bc |
| 2274 | enddo ; enddo | |
| 2275 | 0 | call sum_across_PEs(sum1) |
| 2276 | ||
| 2277 | 0 | sum = reproducing_sum(array(:,:)) |
| 2278 | ||
| 2279 | 0 | if (is_root_pe()) & |
| 2280 | 0 | write(iounit,'(A50,1X,ES25.16,1X,I12)') mesg, sum, sum1 |
| 2281 | ! write(iounit,'(A40,1X,Z16.16,1X,Z16.16,1X,ES25.16,1X,I12)') & | |
| 2282 | ! mesg, sum, sum1, sum, sum1 | |
| 2283 | ||
| 2284 | 0 | end subroutine chksum2d |
| 2285 | ||
| 2286 | !> chksum3d does a checksum of all data in a 2-d array. | |
| 2287 | 0 | subroutine chksum3d(array, mesg, logunit) |
| 2288 | ||
| 2289 | real, dimension(:,:,:), intent(in) :: array !< The array to be checksummed in arbitrary units [A] | |
| 2290 | character(len=*), intent(in) :: mesg !< An identifying message | |
| 2291 | integer, optional, intent(in) :: logunit !< IO unit for checksum logging | |
| 2292 | ||
| 2293 | integer :: xs, xe, ys, ye, zs, ze, i, j, k, bc, sum1, iounit | |
| 2294 | real :: sum ! The global sum of the array [A] | |
| 2295 | ||
| 2296 | 0 | iounit = error_unit ; if (present(logunit)) iounit = logunit |
| 2297 | ||
| 2298 | 0 | xs = LBOUND(array,1) ; xe = UBOUND(array,1) |
| 2299 | 0 | ys = LBOUND(array,2) ; ye = UBOUND(array,2) |
| 2300 | 0 | zs = LBOUND(array,3) ; ze = UBOUND(array,3) |
| 2301 | ||
| 2302 | 0 | sum = 0.0 ; sum1 = 0 |
| 2303 | 0 | do i=xs,xe ; do j=ys,ye ; do k=zs,ze |
| 2304 | 0 | bc = bitcount(ABS(array(i,j,k))) |
| 2305 | 0 | sum1 = sum1 + bc |
| 2306 | enddo ; enddo ; enddo | |
| 2307 | ||
| 2308 | 0 | call sum_across_PEs(sum1) |
| 2309 | 0 | sum = reproducing_sum(array(:,:,:)) |
| 2310 | ||
| 2311 | 0 | if (is_root_pe()) & |
| 2312 | 0 | write(iounit, '(A50,1X,ES25.16,1X,I12)') mesg, sum, sum1 |
| 2313 | ! write(iounit, '(A40,1X,Z16.16,1X,Z16.16,1X,ES25.16,1X,I12)') & | |
| 2314 | ! mesg, sum, sum1, sum, sum1 | |
| 2315 | ||
| 2316 | 0 | end subroutine chksum3d |
| 2317 | ||
| 2318 | !> This function returns .true. if x is a NaN, and .false. otherwise. | |
| 2319 | 3 | function is_NaN_0d(x) |
| 2320 | real, intent(in) :: x !< The value to be checked for NaNs in arbitrary units [A] | |
| 2321 | logical :: is_NaN_0d | |
| 2322 | ||
| 2323 | !is_NaN_0d = (((x < 0.0) .and. (x >= 0.0)) .or. & | |
| 2324 | ! (.not.(x < 0.0) .and. .not.(x >= 0.0))) | |
| 2325 | 3 | if (((x < 0.0) .and. (x >= 0.0)) .or. & |
| 2326 | (.not.(x < 0.0) .and. .not.(x >= 0.0))) then | |
| 2327 | 0 | is_NaN_0d = .true. |
| 2328 | else | |
| 2329 | 3 | is_NaN_0d = .false. |
| 2330 | endif | |
| 2331 | ||
| 2332 | 3 | end function is_NaN_0d |
| 2333 | ||
| 2334 | !> Returns .true. if any element of x is a NaN, and .false. otherwise. | |
| 2335 | 0 | function is_NaN_1d(x, skip_mpp) |
| 2336 | real, dimension(:), intent(in) :: x !< The array to be checked for NaNs in arbitrary units [A] | |
| 2337 | logical, optional, intent(in) :: skip_mpp !< If true, only check this array only | |
| 2338 | !! on the local PE (default false). | |
| 2339 | logical :: is_NaN_1d | |
| 2340 | ||
| 2341 | integer :: i, n | |
| 2342 | logical :: global_check | |
| 2343 | ||
| 2344 | 0 | n = 0 |
| 2345 | 0 | do i = LBOUND(x,1), UBOUND(x,1) |
| 2346 | 0 | if (is_NaN_0d(x(i))) n = n + 1 |
| 2347 | enddo | |
| 2348 | 0 | global_check = .true. |
| 2349 | 0 | if (present(skip_mpp)) global_check = .not.skip_mpp |
| 2350 | ||
| 2351 | 0 | if (global_check) call sum_across_PEs(n) |
| 2352 | 0 | is_NaN_1d = .false. |
| 2353 | 0 | if (n>0) is_NaN_1d = .true. |
| 2354 | ||
| 2355 | 0 | end function is_NaN_1d |
| 2356 | ||
| 2357 | !> Returns .true. if any element of x is a NaN, and .false. otherwise. | |
| 2358 | 0 | function is_NaN_2d(x) |
| 2359 | real, dimension(:,:), intent(in) :: x !< The array to be checked for NaNs in arbitrary units [A] | |
| 2360 | logical :: is_NaN_2d | |
| 2361 | ||
| 2362 | integer :: i, j, n | |
| 2363 | ||
| 2364 | 0 | n = 0 |
| 2365 | 0 | do j = LBOUND(x,2), UBOUND(x,2) ; do i = LBOUND(x,1), UBOUND(x,1) |
| 2366 | 0 | if (is_NaN_0d(x(i,j))) n = n + 1 |
| 2367 | enddo ; enddo | |
| 2368 | 0 | call sum_across_PEs(n) |
| 2369 | 0 | is_NaN_2d = .false. |
| 2370 | 0 | if (n>0) is_NaN_2d = .true. |
| 2371 | ||
| 2372 | 0 | end function is_NaN_2d |
| 2373 | ||
| 2374 | !> Returns .true. if any element of x is a NaN, and .false. otherwise. | |
| 2375 | 0 | function is_NaN_3d(x) |
| 2376 | real, dimension(:,:,:), intent(in) :: x !< The array to be checked for NaNs in arbitrary units [A] | |
| 2377 | logical :: is_NaN_3d | |
| 2378 | ||
| 2379 | integer :: i, j, k, n | |
| 2380 | ||
| 2381 | 0 | n = 0 |
| 2382 | 0 | do k = LBOUND(x,3), UBOUND(x,3) |
| 2383 | 0 | do j = LBOUND(x,2), UBOUND(x,2) ; do i = LBOUND(x,1), UBOUND(x,1) |
| 2384 | 0 | if (is_NaN_0d(x(i,j,k))) n = n + 1 |
| 2385 | enddo ; enddo | |
| 2386 | enddo | |
| 2387 | 0 | call sum_across_PEs(n) |
| 2388 | 0 | is_NaN_3d = .false. |
| 2389 | 0 | if (n>0) is_NaN_3d = .true. |
| 2390 | ||
| 2391 | 0 | end function is_NaN_3d |
| 2392 | ||
| 2393 | ! The following set of routines do a checksum across all elements of a field, | |
| 2394 | ! with the potential for the unscaling and rotation of this field and masking. | |
| 2395 | ||
| 2396 | !> Compute the field checksum of a scalar that may need to be unscaled. | |
| 2397 | !! This uses the field_chksum function that is used to verify file contents, which may differ | |
| 2398 | !! from the bitcount function used for other checksums in this module. | |
| 2399 | 4 | function field_checksum_real_0d(field, pelist, mask_val, turns, unscale) & |
| 2400 | result(chksum) | |
| 2401 | real, intent(in) :: field !< Input scalar to be checksummed in arbitrary, | |
| 2402 | !! possibly rescaled units [A ~> a] | |
| 2403 | integer, optional, intent(in) :: pelist(:) !< PE list of ranks to checksum | |
| 2404 | real, optional, intent(in) :: mask_val !< FMS mask value [nondim] | |
| 2405 | integer, optional, intent(in) :: turns !< Number of quarter turns | |
| 2406 | real, optional, intent(in) :: unscale !< A factor to convert this array back to | |
| 2407 | !! unscaled units for checksums [a A-1 ~> 1] | |
| 2408 | integer(kind=int64) :: chksum !< checksum of scalar | |
| 2409 | ||
| 2410 | real :: scale_fac ! A local copy of unscale if it is present [a A-1 ~> 1] or 1 otherwise | |
| 2411 | ||
| 2412 | 4 | if (present(turns)) call MOM_error(FATAL, "Rotation not supported for 0d fields.") |
| 2413 | ||
| 2414 | 4 | scale_fac = 1.0 ; if (present(unscale)) scale_fac = unscale |
| 2415 | ||
| 2416 | 4 | chksum = field_chksum(scale_fac*field, pelist=pelist, mask_val=mask_val) |
| 2417 | 4 | end function field_checksum_real_0d |
| 2418 | ||
| 2419 | ||
| 2420 | !> Compute the field checksum of an entire 1d array that may need to be unscaled. | |
| 2421 | !! This uses the field_chksum function that is used to verify file contents, which may differ | |
| 2422 | !! from the bitcount function used for other checksums in this module. | |
| 2423 | 0 | function field_checksum_real_1d(field, pelist, mask_val, turns, unscale) & |
| 2424 | result(chksum) | |
| 2425 | real, dimension(:), intent(in) :: field !< Input array to be checksummed in arbitrary, | |
| 2426 | !! possibly rescaled units [A ~> a] | |
| 2427 | integer, optional, intent(in) :: pelist(:) !< PE list of ranks to checksum | |
| 2428 | real, optional, intent(in) :: mask_val !< FMS mask value [nondim] | |
| 2429 | integer, optional, intent(in) :: turns !< Number of quarter turns | |
| 2430 | real, optional, intent(in) :: unscale !< A factor to convert this array back to | |
| 2431 | !! unscaled units for checksums [a A-1 ~> 1] | |
| 2432 | integer(kind=int64) :: chksum !< checksum of array | |
| 2433 | ||
| 2434 | real :: scale_fac ! A local copy of unscale if it is present [a A-1 ~> 1] or 1 otherwise | |
| 2435 | ||
| 2436 | 0 | if (present(turns)) call MOM_error(FATAL, "Rotation not supported for 1d fields.") |
| 2437 | ||
| 2438 | 0 | scale_fac = 1.0 ; if (present(unscale)) scale_fac = unscale |
| 2439 | ||
| 2440 | 0 | chksum = field_chksum(scale_fac*field(:), pelist=pelist, mask_val=mask_val) |
| 2441 | 0 | end function field_checksum_real_1d |
| 2442 | ||
| 2443 | ||
| 2444 | !> Compute the field checksum of an entire 2d array that may need to be rotated or unscaled. | |
| 2445 | !! This uses the field_chksum function that is used to verify file contents, which may differ | |
| 2446 | !! from the bitcount function used for other checksums in this module. | |
| 2447 | 28 | function field_checksum_real_2d(field, pelist, mask_val, turns, unscale) & |
| 2448 | result(chksum) | |
| 2449 | real, dimension(:,:), intent(in) :: field !< Unrotated input field to be checksummed in | |
| 2450 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 2451 | integer, optional, intent(in) :: pelist(:) !< PE list of ranks to checksum | |
| 2452 | real, optional, intent(in) :: mask_val !< FMS mask value [nondim] | |
| 2453 | integer, optional, intent(in) :: turns !< Number of quarter turns | |
| 2454 | real, optional, intent(in) :: unscale !< A factor to convert this array back to | |
| 2455 | !! unscaled units for checksums [a A-1 ~> 1] | |
| 2456 | integer(kind=int64) :: chksum !< checksum of array | |
| 2457 | ||
| 2458 | ! Local variables | |
| 2459 | 28 | real, allocatable :: field_rot(:,:) ! A rotated version of field, with the same units [A ~> a] |
| 2460 | integer :: qturns ! The number of quarter turns through which to rotate field | |
| 2461 | logical :: do_unscale ! If true, unscale the variable before it is checksummed | |
| 2462 | ||
| 2463 | 28 | qturns = 0 |
| 2464 | 28 | if (present(turns)) & |
| 2465 | 28 | qturns = modulo(turns, 4) |
| 2466 | ||
| 2467 | 28 | do_unscale = .false. ; if (present(unscale)) do_unscale = (unscale /= 1.0) |
| 2468 | ||
| 2469 | 28 | if (qturns == 0) then |
| 2470 | 28 | if (do_unscale) then |
| 2471 | 0 | chksum = field_chksum(unscale*field(:,:), pelist=pelist, mask_val=mask_val) |
| 2472 | else | |
| 2473 | 28 | chksum = field_chksum(field, pelist=pelist, mask_val=mask_val) |
| 2474 | endif | |
| 2475 | else | |
| 2476 | 0 | call allocate_rotated_array(field, [1,1], qturns, field_rot) |
| 2477 | 0 | call rotate_array(field, qturns, field_rot) |
| 2478 | 0 | if (do_unscale) field_rot(:,:) = unscale*field_rot(:,:) |
| 2479 | 0 | chksum = field_chksum(field_rot, pelist=pelist, mask_val=mask_val) |
| 2480 | 0 | deallocate(field_rot) |
| 2481 | endif | |
| 2482 | 28 | end function field_checksum_real_2d |
| 2483 | ||
| 2484 | !> Compute the field checksum of an entire 3d array that may need to be rotated or unscaled. | |
| 2485 | !! This uses the field_chksum function that is used to verify file contents, which may differ | |
| 2486 | !! from the bitcount function used for other checksums in this module. | |
| 2487 | 31 | function field_checksum_real_3d(field, pelist, mask_val, turns, unscale) & |
| 2488 | result(chksum) | |
| 2489 | real, dimension(:,:,:), intent(in) :: field !< Unrotated input field to be checksummed in | |
| 2490 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 2491 | integer, optional, intent(in) :: pelist(:) !< PE list of ranks to checksum | |
| 2492 | real, optional, intent(in) :: mask_val !< FMS mask value [nondim] | |
| 2493 | integer, optional, intent(in) :: turns !< Number of quarter turns | |
| 2494 | real, optional, intent(in) :: unscale !< A factor to convert this array back to | |
| 2495 | !! unscaled units for checksums [a A-1 ~> 1] | |
| 2496 | integer(kind=int64) :: chksum !< checksum of array | |
| 2497 | ||
| 2498 | ! Local variables | |
| 2499 | 31 | real, allocatable :: field_rot(:,:,:) ! A rotated version of field, with the same units [A ~> a] |
| 2500 | integer :: qturns ! The number of quarter turns through which to rotate field | |
| 2501 | logical :: do_unscale ! If true, unscale the variable before it is checksummed | |
| 2502 | ||
| 2503 | 31 | qturns = 0 |
| 2504 | 31 | if (present(turns)) & |
| 2505 | 31 | qturns = modulo(turns, 4) |
| 2506 | ||
| 2507 | 31 | do_unscale = .false. ; if (present(unscale)) do_unscale = (unscale /= 1.0) |
| 2508 | ||
| 2509 | 31 | if (qturns == 0) then |
| 2510 | 31 | if (do_unscale) then |
| 2511 | 0 | chksum = field_chksum(unscale*field(:,:,:), pelist=pelist, mask_val=mask_val) |
| 2512 | else | |
| 2513 | 31 | chksum = field_chksum(field, pelist=pelist, mask_val=mask_val) |
| 2514 | endif | |
| 2515 | else | |
| 2516 | 0 | call allocate_rotated_array(field, [1,1,1], qturns, field_rot) |
| 2517 | 0 | call rotate_array(field, qturns, field_rot) |
| 2518 | 0 | if (do_unscale) field_rot(:,:,:) = unscale*field_rot(:,:,:) |
| 2519 | 0 | chksum = field_chksum(field_rot, pelist=pelist, mask_val=mask_val) |
| 2520 | 0 | deallocate(field_rot) |
| 2521 | endif | |
| 2522 | 31 | end function field_checksum_real_3d |
| 2523 | ||
| 2524 | !> Compute the field checksum of an entire 4d array that may need to be rotated or unscaled. | |
| 2525 | !! This uses the field_chksum function that is used to verify file contents, which may differ | |
| 2526 | !! from the bitcount function used for other checksums in this module. | |
| 2527 | 0 | function field_checksum_real_4d(field, pelist, mask_val, turns, unscale) & |
| 2528 | result(chksum) | |
| 2529 | real, dimension(:,:,:,:), intent(in) :: field !< Unrotated input field to be checksummed in | |
| 2530 | !! arbitrary, possibly rescaled units [A ~> a] | |
| 2531 | integer, optional, intent(in) :: pelist(:) !< PE list of ranks to checksum | |
| 2532 | real, optional, intent(in) :: mask_val !< FMS mask value [nondim] | |
| 2533 | integer, optional, intent(in) :: turns !< Number of quarter turns | |
| 2534 | real, optional, intent(in) :: unscale !< A factor to convert this array back to | |
| 2535 | !! unscaled units for checksums [a A-1 ~> 1] | |
| 2536 | integer(kind=int64) :: chksum !< checksum of array | |
| 2537 | ||
| 2538 | ! Local variables | |
| 2539 | 0 | real, allocatable :: field_rot(:,:,:,:) ! A rotated version of field, with the same units [A ~> a] |
| 2540 | integer :: qturns ! The number of quarter turns through which to rotate field | |
| 2541 | logical :: do_unscale ! If true, unscale the variable before it is checksummed | |
| 2542 | ||
| 2543 | 0 | qturns = 0 |
| 2544 | 0 | if (present(turns)) & |
| 2545 | 0 | qturns = modulo(turns, 4) |
| 2546 | ||
| 2547 | 0 | do_unscale = .false. ; if (present(unscale)) do_unscale = (unscale /= 1.0) |
| 2548 | ||
| 2549 | 0 | if (qturns == 0) then |
| 2550 | 0 | if (do_unscale) then |
| 2551 | 0 | chksum = field_chksum(unscale*field(:,:,:,:), pelist=pelist, mask_val=mask_val) |
| 2552 | else | |
| 2553 | 0 | chksum = field_chksum(field, pelist=pelist, mask_val=mask_val) |
| 2554 | endif | |
| 2555 | else | |
| 2556 | 0 | call allocate_rotated_array(field, [1,1,1,1], qturns, field_rot) |
| 2557 | 0 | call rotate_array(field, qturns, field_rot) |
| 2558 | 0 | if (do_unscale) field_rot(:,:,:,:) = unscale*field_rot(:,:,:,:) |
| 2559 | 0 | chksum = field_chksum(field_rot, pelist=pelist, mask_val=mask_val) |
| 2560 | 0 | deallocate(field_rot) |
| 2561 | endif | |
| 2562 | 0 | end function field_checksum_real_4d |
| 2563 | ||
| 2564 | ||
| 2565 | !> Write a message including the checksum of the non-shifted array | |
| 2566 | 0 | subroutine chk_sum_msg1(fmsg, bc0, mesg, iounit) |
| 2567 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2568 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2569 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2570 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2571 | ||
| 2572 | 0 | if (is_root_pe()) & |
| 2573 | 0 | write(iounit, '(a,1(a,i10,1x),a)') fmsg, " c=", bc0, trim(mesg) |
| 2574 | 0 | end subroutine chk_sum_msg1 |
| 2575 | ||
| 2576 | !> Write a message including checksums of non-shifted and diagonally shifted arrays | |
| 2577 | 0 | subroutine chk_sum_msg5(fmsg, bc0, bcSW, bcSE, bcNW, bcNE, mesg, iounit) |
| 2578 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2579 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2580 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2581 | integer, intent(in) :: bcSW !< The bitcount for SW shifted array | |
| 2582 | integer, intent(in) :: bcSE !< The bitcount for SE shifted array | |
| 2583 | integer, intent(in) :: bcNW !< The bitcount for NW shifted array | |
| 2584 | integer, intent(in) :: bcNE !< The bitcount for NE shifted array | |
| 2585 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2586 | ||
| 2587 | 0 | if (is_root_pe()) write(iounit, '(A,5(A,I10,1X),A)') & |
| 2588 | 0 | fmsg, " c=", bc0, "sw=", bcSW, "se=", bcSE, "nw=", bcNW, "ne=", bcNE, trim(mesg) |
| 2589 | 0 | end subroutine chk_sum_msg5 |
| 2590 | ||
| 2591 | !> Write a message including checksums of non-shifted and laterally shifted arrays | |
| 2592 | 0 | subroutine chk_sum_msg_NSEW(fmsg, bc0, bcN, bcS, bcE, bcW, mesg, iounit) |
| 2593 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2594 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2595 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2596 | integer, intent(in) :: bcN !< The bitcount for N shifted array | |
| 2597 | integer, intent(in) :: bcS !< The bitcount for S shifted array | |
| 2598 | integer, intent(in) :: bcE !< The bitcount for E shifted array | |
| 2599 | integer, intent(in) :: bcW !< The bitcount for W shifted array | |
| 2600 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2601 | ||
| 2602 | 0 | if (is_root_pe()) write(iounit, '(A,5(A,I10,1X),A)') & |
| 2603 | 0 | fmsg, " c=", bc0, "N=", bcN, "S=", bcS, "E=", bcE, "W=", bcW, trim(mesg) |
| 2604 | 0 | end subroutine chk_sum_msg_NSEW |
| 2605 | ||
| 2606 | !> Write a message including checksums of non-shifted and southward shifted arrays | |
| 2607 | 0 | subroutine chk_sum_msg_S(fmsg, bc0, bcS, mesg, iounit) |
| 2608 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2609 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2610 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2611 | integer, intent(in) :: bcS !< The bitcount of the south-shifted array | |
| 2612 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2613 | ||
| 2614 | 0 | if (is_root_pe()) write(iounit, '(A,2(A,I10,1X),A)') & |
| 2615 | 0 | fmsg, " c=", bc0, "S=", bcS, trim(mesg) |
| 2616 | 0 | end subroutine chk_sum_msg_S |
| 2617 | ||
| 2618 | !> Write a message including checksums of non-shifted and westward shifted arrays | |
| 2619 | 0 | subroutine chk_sum_msg_W(fmsg, bc0, bcW, mesg, iounit) |
| 2620 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2621 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2622 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2623 | integer, intent(in) :: bcW !< The bitcount of the west-shifted array | |
| 2624 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2625 | ||
| 2626 | 0 | if (is_root_pe()) write(iounit, '(A,2(A,I10,1X),A)') & |
| 2627 | 0 | fmsg, " c=", bc0, "W=", bcW, trim(mesg) |
| 2628 | 0 | end subroutine chk_sum_msg_W |
| 2629 | ||
| 2630 | !> Write a message including checksums of non-shifted and southwestward shifted arrays | |
| 2631 | 0 | subroutine chk_sum_msg2(fmsg, bc0, bcSW, mesg, iounit) |
| 2632 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2633 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2634 | integer, intent(in) :: bc0 !< The bitcount of the non-shifted array | |
| 2635 | integer, intent(in) :: bcSW !< The bitcount of the southwest-shifted array | |
| 2636 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2637 | ||
| 2638 | 0 | if (is_root_pe()) write(iounit, '(A,2(A,I9,1X),A)') & |
| 2639 | 0 | fmsg, " c=", bc0, "s/w=", bcSW, trim(mesg) |
| 2640 | 0 | end subroutine chk_sum_msg2 |
| 2641 | ||
| 2642 | !> Write a message including the global mean, maximum and minimum of an array | |
| 2643 | 0 | subroutine chk_sum_msg3(fmsg, aMean, aMin, aMax, mesg, iounit) |
| 2644 | character(len=*), intent(in) :: fmsg !< A checksum code-location specific preamble | |
| 2645 | character(len=*), intent(in) :: mesg !< An identifying message supplied by top-level caller | |
| 2646 | real, intent(in) :: aMean !< The mean value of the array in arbitrary units [A] | |
| 2647 | real, intent(in) :: aMin !< The minimum value of the array [A] | |
| 2648 | real, intent(in) :: aMax !< The maximum value of the array [A] | |
| 2649 | integer, intent(in) :: iounit !< Checksum logger IO unit | |
| 2650 | ||
| 2651 | ! NOTE: We add zero to aMin and aMax to remove any negative zeros. | |
| 2652 | ! This is due to inconsistencies of signed zero in local vs MPI calculations. | |
| 2653 | ||
| 2654 | 0 | if (is_root_pe()) write(iounit, '(A,3(A,ES25.16,1X),A)') & |
| 2655 | 0 | fmsg, " mean=", aMean, "min=", (0. + aMin), "max=", (0. + aMax), trim(mesg) |
| 2656 | 0 | end subroutine chk_sum_msg3 |
| 2657 | ||
| 2658 | !> MOM_checksums_init initializes the MOM_checksums module. As it happens, the | |
| 2659 | !! only thing that it does is to log the version of this module. | |
| 2660 | 1 | subroutine MOM_checksums_init(param_file) |
| 2661 | type(param_file_type), intent(in) :: param_file !< A structure to parse for run-time parameters | |
| 2662 | ! This include declares and sets the variable "version". | |
| 2663 | # include "version_variable.h" | |
| 2664 | character(len=40) :: mdl = "MOM_checksums" ! This module's name. | |
| 2665 | ||
| 2666 | 1 | call log_version(param_file, mdl, version) |
| 2667 | ||
| 2668 | 1 | end subroutine MOM_checksums_init |
| 2669 | ||
| 2670 | !> A wrapper for MOM_error used in the checksum code | |
| 2671 | 0 | subroutine chksum_error(signal, message) |
| 2672 | ! Wrapper for MOM_error to help place specific break points in debuggers | |
| 2673 | integer, intent(in) :: signal !< An error severity level, such as FATAL or WARNING | |
| 2674 | character(len=*), intent(in) :: message !< An error message | |
| 2675 | 0 | call MOM_error(signal, message) |
| 2676 | 0 | end subroutine chksum_error |
| 2677 | ||
| 2678 | !> Does a bitcount of a number by first casting to an integer and then using BTEST | |
| 2679 | !! to check bit by bit | |
| 2680 | 0 | integer function bitcount(x) |
| 2681 | real, intent(in) :: x !< Number to be bitcount in arbitrary units [A] | |
| 2682 | ||
| 2683 | integer, parameter :: xk = kind(x) !< Kind type of x | |
| 2684 | ||
| 2685 | ! NOTE: Assumes that reals and integers of kind=xk are the same size | |
| 2686 | 0 | bitcount = popcnt(transfer(x, 1_xk)) |
| 2687 | 0 | end function bitcount |
| 2688 | ||
| 2689 | end module MOM_checksums |