Skip to content

Commit dcec968

Browse files
committed
add optional mask
1 parent 8d6245e commit dcec968

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/shared_utilities/utils.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,28 +494,34 @@ function isdivisible(
494494
end
495495

496496
"""
497-
count_nans_state(sol)
497+
count_nans_state(state, mask::ClimaCore.Fields.Field = nothing)
498498
499-
Count the number of NaNs in the state variables. This function is useful for
499+
Count the number of NaNs in the state, e.g. the FieldVector given by
500+
`sol.u[end]` after calling `solve`. This function is useful for
500501
debugging simulations to determine quantitatively if a simulation is stable.
501502
502503
If this function is called on a FieldVector, it will recursively call itself
503504
on each Field in the FieldVector. If it is called on a Field, it will count
504505
the number of NaNs in the Field and produce a warning if any are found.
505506
506-
Input: `state` - e.g. the FieldVector given by `sol.u[end]` after calling `solve`
507+
If a ClimaCore Field is provided as `mask`, the function will only count NaNs
508+
in the state variables where the mask is 1. This is intended to be used with
509+
the land/sea mask, to avoid counting NaNs over the ocean. Note this assumes
510+
the mask is 1 over land and 0 over ocean.
507511
"""
508-
function count_nans_state(state::ClimaCore.Fields.FieldVector)
512+
function count_nans_state(state::ClimaCore.Fields.FieldVector, mask = nothing)
509513
for pn in propertynames(state)
510514
state_new = getproperty(state, pn)
511515
@info "Checking NaNs in $pn"
512-
count_nans_state(state_new)
516+
count_nans_state(state_new, mask)
513517
end
514518
return nothing
515519
end
516520

517-
function count_nans_state(state::ClimaCore.Fields.Field)
518-
num_nans = count(isnan.(Array(parent(state))))
521+
function count_nans_state(state::ClimaCore.Fields.Field, mask = nothing)
522+
num_nans =
523+
isnothing(mask) ? count(==(1), isnan.(parent(state))) :
524+
count(==(1), isnan.(parent(state)) .* parent(mask))
519525
if num_nans > 0
520526
@warn "$num_nans NaNs found"
521527
else

test/shared_utilities/utilities.jl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ using Test
22
import ClimaComms
33
ClimaComms.@import_required_backends
44
using ClimaCore: Spaces, Geometry, Fields
5-
import ClimaComms
65
using ClimaLand
76
using ClimaLand: Domains, condition, SavingAffect, saving_initialize
87

@@ -285,10 +284,14 @@ end
285284
"Checking NaNs in var3",
286285
) (:info, "No NaNs found") ClimaLand.count_nans_state(Y)
287286

288-
# Add some NaNs to the fields
289-
Array(parent(var1))[1] = NaN
290-
Array(parent(var2))[1] = NaN
291-
Array(parent(var2))[2] = NaN
287+
# Add some NaNs to the fields without scalar indexing
288+
var1_copy = Array(parent(var1))
289+
var1_copy[1] = NaN
290+
parent(var1) .= var1_copy
291+
var2_copy = Array(parent(var1))
292+
var2_copy[1] = NaN
293+
var2_copy[2] = NaN
294+
parent(var2) .= var2_copy
292295

293296
# Count and log the number of NaNs in the state
294297
@test_logs (:info, "Checking NaNs in var1") (:warn, "1 NaNs found") (
@@ -299,4 +302,22 @@ end
299302
"Checking NaNs in var3",
300303
) (:info, "No NaNs found") ClimaLand.count_nans_state(Y)
301304

305+
# Test with a mask
306+
mask_zeros = Fields.zeros(space)
307+
@test_logs (:info, "Checking NaNs in var1") (:info, "No NaNs found") (
308+
:info,
309+
"Checking NaNs in fieldvec",
310+
) (:info, "Checking NaNs in var2") (:info, "No NaNs found") (
311+
:info,
312+
"Checking NaNs in var3",
313+
) (:info, "No NaNs found") ClimaLand.count_nans_state(Y, mask_zeros)
314+
315+
mask_ones = Fields.ones(space)
316+
@test_logs (:info, "Checking NaNs in var1") (:warn, "1 NaNs found") (
317+
:info,
318+
"Checking NaNs in fieldvec",
319+
) (:info, "Checking NaNs in var2") (:warn, "2 NaNs found") (
320+
:info,
321+
"Checking NaNs in var3",
322+
) (:info, "No NaNs found") ClimaLand.count_nans_state(Y, mask_ones)
302323
end

0 commit comments

Comments
 (0)