-
Notifications
You must be signed in to change notification settings - Fork 186
/
state_checker.jl
43 lines (31 loc) 路 1.11 KB
/
state_checker.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Printf
using Statistics
struct StateChecker{T, F} <: AbstractDiagnostic
schedule :: T
fields :: F
end
"""
StateChecker(; schedule, fields)
Returns a `StateChecker` that logs field information (minimum, maximum, mean)
for each field in a named tuple of `fields` when `schedule` actuates.
"""
StateChecker(model; schedule, fields=fields(model)) = StateChecker(schedule, fields)
function run_diagnostic!(sc::StateChecker, model)
pad = keys(sc.fields) .|> string .|> length |> maximum
@info "State check @ $(summary(model.clock))"
for (name, field) in pairs(sc.fields)
state_check(field, name, pad)
end
return nothing
end
function state_check(field, name, pad)
min_val = minimum(field)
max_val = maximum(field)
mean_val = mean(field)
name = lpad(name, pad)
@info @sprintf("%s | min = %+.15e | max = %+.15e | mean = %+.15e", name, min_val, max_val, mean_val)
return nothing
end
(sc::StateChecker)(model) = run_diagnostic!(sc, model)
Base.show(io::IO, sc::StateChecker) =
print(io, "StateChecker checking $(length(sc.fields)) fields: $(keys(sc.fields))")