-
Notifications
You must be signed in to change notification settings - Fork 191
/
test_jld2_output_writer.jl
243 lines (177 loc) · 8.14 KB
/
test_jld2_output_writer.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
include("dependencies_for_runtests.jl")
#####
##### JLD2OutputWriter tests
#####
function jld2_sliced_field_output(model, outputs=model.velocities)
model.clock.iteration = 0
model.clock.time = 0.0
set!(model, u = (x, y, z) -> rand(),
v = (x, y, z) -> rand(),
w = (x, y, z) -> rand())
simulation = Simulation(model, Δt=1.0, stop_iteration=1)
simulation.output_writers[:velocities] =
JLD2OutputWriter(model, outputs,
schedule = TimeInterval(1),
indices = (1:2, 1:4, :),
with_halos = false,
dir = ".",
filename = "test.jld2",
overwrite_existing = true)
run!(simulation)
file = jldopen("test.jld2")
u₁ = file["timeseries/u/0"]
v₁ = file["timeseries/v/0"]
w₁ = file["timeseries/w/0"]
close(file)
rm("test.jld2")
return size(u₁) == (2, 2, 4) && size(v₁) == (2, 2, 4) && size(w₁) == (2, 2, 5)
end
function test_jld2_file_splitting(arch)
grid = RectilinearGrid(arch, size=(16, 16, 16), extent=(1, 1, 1), halo=(1, 1, 1))
model = NonhydrostaticModel(; grid, buoyancy=SeawaterBuoyancy(), tracers=(:T, :S))
simulation = Simulation(model, Δt=1, stop_iteration=10)
function fake_bc_init(file, model)
file["boundary_conditions/fake"] = π
end
ow = JLD2OutputWriter(model, (; u=model.velocities.u);
dir = ".",
filename = "test.jld2",
schedule = IterationInterval(1),
init = fake_bc_init,
including = [:grid],
array_type = Array{Float64},
with_halos = true,
max_filesize = 200KiB,
overwrite_existing = true)
push!(simulation.output_writers, ow)
# 531 KiB of output will be written which should get split into 3 files.
run!(simulation)
# Test that files has been split according to size as expected.
@test filesize("test_part1.jld2") > 200KiB
@test filesize("test_part2.jld2") > 200KiB
@test filesize("test_part3.jld2") < 200KiB
for n in string.(1:3)
filename = "test_part$n.jld2"
jldopen(filename, "r") do file
# Test to make sure all files contain structs from `including`.
@test file["grid/Nx"] == 16
# Test to make sure all files contain info from `init` function.
@test file["boundary_conditions/fake"] == π
end
# Leave test directory clean.
rm(filename)
end
return nothing
end
function test_jld2_time_averaging_of_horizontal_averages(model)
model.clock.iteration = 0
model.clock.time = 0.0
u, v, w = model.velocities
T = model.tracers.T
u .= 1
v .= 2
w .= 0
T .= 4
Δt = 0.1
simulation = Simulation(model, Δt=Δt, stop_iteration=5)
average_fluxes = (wu = Field(Average(w * u, dims=(1, 2))),
uv = Field(Average(u * v, dims=(1, 2))),
wT = Field(Average(w * T, dims=(1, 2))))
simulation.output_writers[:fluxes] = JLD2OutputWriter(model, average_fluxes,
schedule = AveragedTimeInterval(4Δt, window=2Δt),
dir = ".",
filename = "jld2_time_averaging_test.jld2",
overwrite_existing = true)
run!(simulation)
test_file_name = "jld2_time_averaging_test.jld2"
file = jldopen(test_file_name)
# Data is saved without halos by default
wu = file["timeseries/wu/4"][1, 1, 3]
uv = file["timeseries/uv/4"][1, 1, 3]
wT = file["timeseries/wT/4"][1, 1, 3]
close(file)
rm(test_file_name)
FT = eltype(model.grid)
@test wu == zero(FT)
@test wT == zero(FT)
@test uv == FT(2)
return nothing
end
for arch in archs
# Some tests can reuse this same grid and model.
topo =(Periodic, Periodic, Bounded)
grid = RectilinearGrid(arch, topology=topo, size=(4, 4, 4), extent=(1, 1, 1))
model = NonhydrostaticModel(grid=grid, buoyancy=SeawaterBuoyancy(), tracers=(:T, :S))
@testset "JLD2 output writer [$(typeof(arch))]" begin
@info " Testing JLD2 output writer [$(typeof(arch))]..."
set!(model, u = (x, y, z) -> rand(),
v = (x, y, z) -> rand(),
w = (x, y, z) -> rand())
simulation = Simulation(model, Δt=1.0, stop_iteration=1)
simulation.output_writers[:velocities] = JLD2OutputWriter(model, model.velocities,
schedule = TimeInterval(1),
dir = ".",
filename = "vanilla_jld2_test.jld2",
indices = (:, :, :),
with_halos = false,
overwrite_existing = true)
simulation.output_writers[:sliced] = JLD2OutputWriter(model, model.velocities,
schedule = TimeInterval(1),
indices = (1:2, 1:4, :),
with_halos = false,
dir = ".",
filename = "sliced_jld2_test.jld2",
overwrite_existing = true)
u, v, w = model.velocities
func_outputs = (u = model -> u, v = model -> v, w = model -> w)
simulation.output_writers[:sliced_funcs] = JLD2OutputWriter(model, func_outputs,
schedule = TimeInterval(1),
indices = (1:2, 1:4, :),
with_halos = false,
dir = ".",
filename = "sliced_funcs_jld2_test.jld2",
overwrite_existing = true)
u₀ = CUDA.@allowscalar model.velocities.u[3, 3, 3]
v₀ = CUDA.@allowscalar model.velocities.v[3, 3, 3]
w₀ = CUDA.@allowscalar model.velocities.w[3, 3, 3]
run!(simulation)
#####
##### Stuff was outputted
#####
file = jldopen("vanilla_jld2_test.jld2")
# Data is saved without halos by default
u₁ = file["timeseries/u/0"][3, 3, 3]
v₁ = file["timeseries/v/0"][3, 3, 3]
w₁ = file["timeseries/w/0"][3, 3, 3]
close(file)
rm("vanilla_jld2_test.jld2")
FT = typeof(u₁)
@test FT(u₀) == u₁
@test FT(v₀) == v₁
@test FT(w₀) == w₁
#####
##### Field slicing
#####
function test_field_slicing(test_file_name, sizes...)
file = jldopen(test_file_name)
u₁ = file["timeseries/u/0"]
v₁ = file["timeseries/v/0"]
w₁ = file["timeseries/w/0"]
close(file)
rm(test_file_name)
@test size(u₁) == sizes[1]
@test size(v₁) == sizes[2]
@test size(w₁) == sizes[3]
end
test_field_slicing("sliced_jld2_test.jld2", (2, 4, 4), (2, 4, 4), (2, 4, 5))
test_field_slicing("sliced_funcs_jld2_test.jld2", (4, 4, 4), (4, 4, 4), (4, 4, 5))
#####
##### File splitting
#####
test_jld2_file_splitting(arch)
#####
##### Time-averaging
#####
test_jld2_time_averaging_of_horizontal_averages(model)
end
end