-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiagnostics_tests.jl
151 lines (140 loc) · 5.67 KB
/
diagnostics_tests.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#=
Unit tests for ClimaCoupler Diagnostics module
=#
import Test: @test, @testset
import CUDA
import Dates
import ClimaComms
import ClimaCore as CC
import ClimaCoupler: ConservationChecker, Diagnostics, Interfacer, TestHelper, TimeManager
Diagnostics.get_var(cs::Interfacer.CoupledSimulation, ::Val{:x}) = 1
for FT in (Float32, Float64)
@testset "init_diagnostics for FT=$FT" begin
names = (:x, :y)
space = TestHelper.create_space(FT)
dg = Diagnostics.init_diagnostics(names, space)
@test typeof(dg) == Diagnostics.DiagnosticsGroup{TimeManager.EveryTimestep, NamedTuple{(), Tuple{}}}
end
@testset "accumulate_diagnostics!, collect_diags, iterate_operations, operation{accumulation{TimeMean, Nothing}}, get_var for FT=$FT" begin
cases = (nothing, Diagnostics.TimeMean([Int(0)]))
expected_results = (FT(2), FT(3))
for (c_i, case) in enumerate(cases)
names = (:x,)
space = TestHelper.create_space(FT)
dg_2d = Diagnostics.init_diagnostics(
names,
space,
save = TimeManager.EveryTimestep(),
operations = (; accumulate = case),
)
dg_2d.field_vector .= FT(2)
cs = Interfacer.CoupledSimulation{FT}(
nothing, # comms_ctx
nothing, # dates
nothing, # boundary_space
nothing, # fields
nothing, # parsed_args
nothing, # conservation_checks
(Int(0), Int(1000)), # tspan
Int(100), # t
Int(100), # Δt_cpl
(;), # surface_masks
(;), # model_sims
(;), # mode
(dg_2d,),
(;), # callbacks
(;), # dirs
nothing, # turbulent_fluxes
nothing, # thermo_params
)
Diagnostics.accumulate_diagnostics!(cs)
CUDA.@allowscalar begin
@test cs.diagnostics[1].field_vector[1] == expected_results[c_i]
end
@test isnothing(Diagnostics.get_var(cs, Val(:z)))
end
end
if !Sys.iswindows() # Windows has NetCDF / HDF5 support limitations
@testset "save_diagnostics" begin
test_dir = "diag_test_dir"
names = (:x,)
space = TestHelper.create_space(FT)
dg_2d = Diagnostics.init_diagnostics(
names,
space,
save = TimeManager.EveryTimestep(),
operations = (; accumulate = Diagnostics.TimeMean([Int(0)])),
output_dir = test_dir,
) # or use accumulate = nothing for snapshop save
cs = Interfacer.CoupledSimulation{FT}(
ClimaComms.SingletonCommsContext(), # comms_ctx
(date = [Dates.DateTime(0, 2)], date1 = [Dates.DateTime(0, 1)]), # dates
nothing, # boundary_space
nothing, # fields
nothing, # parsed_args
nothing, # conservation_checks
(Int(0), Int(1000)),# tspan
Int(100), # t
Int(100), # Δt_cpl
(;), # surface_masks
(;), # model_sims
(;), # mode
(dg_2d,), # diagnostics
(;), # callbacks
(;), # dirs
nothing, # turbulent_fluxes
nothing, # thermo_params
)
Diagnostics.save_diagnostics(cs, cs.diagnostics[1])
file = filter(x -> endswith(x, ".hdf5"), readdir(test_dir))
@test !isempty(file)
rm(test_dir; recursive = true, force = true)
end
end
@testset "save_time_format for FT=$FT" begin
date = Dates.DateTime(1970, 2, 1, 0, 1)
unix = Diagnostics.save_time_format(date, TimeManager.Monthly())
@test unix == 0
end
@testset "pre_save{TimeMean, Nothing}, post_save for FT=$FT" begin
cases = (nothing, Diagnostics.TimeMean([Int(0)]))
expected_results = ((FT(3), FT(1), FT(1)), (FT(4), FT(2.5), FT(0)))
for (c_i, case) in enumerate(cases)
names = (:x,)
space = TestHelper.create_space(FT)
dg_2d = Diagnostics.init_diagnostics(
names,
space,
save = TimeManager.EveryTimestep(),
operations = (; accumulate = case),
)
dg_2d.field_vector .= FT(3)
cs = Interfacer.CoupledSimulation{FT}(
nothing, # comms_ctx
nothing, # dates
nothing, # boundary_space
nothing, # fields
nothing, # parsed_args
nothing, # conservation_checks
(Int(0), Int(1000)), # tspan
Int(100), # t
Int(100), # Δt_cpl
(;), # surface_masks
(;), # model_sims
(;), # mode
(dg_2d,),
(;), # callbacks
(;), # dirs
nothing, # turbulent_fluxes
nothing, # thermo_params
)
Diagnostics.accumulate_diagnostics!(cs)
@test cs.diagnostics[1].field_vector[1] == expected_results[c_i][1]
Diagnostics.accumulate_diagnostics!(cs)
Diagnostics.pre_save(cs.diagnostics[1].operations.accumulate, cs, cs.diagnostics[1])
@test cs.diagnostics[1].field_vector[1] == expected_results[c_i][2]
Diagnostics.post_save(cs.diagnostics[1].operations.accumulate, cs, cs.diagnostics[1])
@test cs.diagnostics[1].field_vector[1] == expected_results[c_i][3]
end
end
end