Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Mar 26, 2024
1 parent 15b4a1d commit 37395d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function save_vertical_flux(u, t, integrator)
Δt = get_Δt(integrator, graph)
vertical_flux_mean = copy(vertical_flux_integrated)
vertical_flux_mean ./= Δt
fill!(vertical_flux_mean, 0.0)
fill!(vertical_flux_integrated, 0.0)

return vertical_flux_mean
end
Expand Down
42 changes: 42 additions & 0 deletions core/test/time_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@
# some difference is expected since the modeled flow is for the period up to t
@test isapprox(flow_1_to_2.flow_rate, flow_expected, rtol = 0.005)
end

@test "vertical_flux_means" begin
using DataFrames: DataFrame

toml_path =
normpath(@__DIR__, "../../generated_testmodels/basic_transient/ribasim.toml")
@test ispath(toml_path)
model = Ribasim.run(toml_path)
basin = model.integrator.p.basin
basin_table = DataFrame(Ribasim.basin_table(model))
filter!(:node_id => id -> id == 1, basin_table)
basin_table[!, "time"] .=
Ribasim.seconds_since.(basin_table.time, model.config.starttime)

# No vertical flux data for last saveat
t_end = last(basin_table).time
data_end = filter(:time => t -> t == t_end, basin_table)
@test all(data_end.precipitation .== 0)
@test all(data_end.evaporation .== 0)
@test all(data_end.drainage .== 0)
@test all(data_end.infiltration .== 0)

time_table = DataFrame(basin.time)
filter!(:node_id => id -> id == 1, time_table)
time_table[!, "time"] .= Ribasim.seconds_since.(time_table.time, model.config.starttime)
fixed_area = basin.area[1][end]
area = [
Ribasim.get_area_and_level(basin, 1, storage)[1] for storage in basin_table.storage
]
area = (area[2:end] .+ area[1:(end - 1)]) ./ 2
@test all(
basin_table.precipitation[1:(end - 1)] .≈
fixed_area .* time_table.precipitation[1:(end - 1)],
)
@test all(
isapprox(
basin_table.evaporation[1:(end - 1)],
area .* time_table.potential_evaporation[1:(end - 1)];
rtol = 1e-4,
),
)
end

0 comments on commit 37395d9

Please sign in to comment.