Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SmoothInterpolation as a dependency #1649

Merged
merged 7 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 73 additions & 74 deletions Manifest.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ OteraEngine = "b2d7f28f-acd6-4007-8b26-bc27716e5513"
PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d"
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
ReadOnlyArrays = "988b38a3-91fc-5605-94a2-ee2116b3bd83"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Ribasim = "aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"
SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SmoothInterpolation = "4412c578-8aba-4b04-8c8c-6ae573ccb792"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Expand Down
6 changes: 3 additions & 3 deletions core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
ReadOnlyArrays = "988b38a3-91fc-5605-94a2-ee2116b3bd83"
SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SmoothInterpolation = "4412c578-8aba-4b04-8c8c-6ae573ccb792"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Expand All @@ -51,7 +51,7 @@ ComponentArrays = "0.13, 0.14, 0.15"
Configurations = "0.17"
DBInterface = "2.4"
DataFrames = "1.4"
DataInterpolations = "=5.2.0"
DataInterpolations = "=5.3.1"
DataStructures = "0.18"
Dates = "<0.0.1, 1"
DiffEqCallbacks = "3.6"
Expand All @@ -71,9 +71,9 @@ MetaGraphsNext = "0.6, 0.7"
OrdinaryDiffEq = "6.7"
PreallocationTools = "0.4"
ReTestItems = "1.20"
ReadOnlyArrays = "0.2"
SQLite = "1.5.1"
SciMLBase = "2.36"
SmoothInterpolation = "0.1.0"
SparseArrays = "<0.0.1, 1"
StructArrays = "0.6.13"
TOML = "<0.0.1, 1"
Expand Down
5 changes: 3 additions & 2 deletions core/src/Ribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ using Accessors: @set
using Arrow: Arrow, Table
using CodecZstd: ZstdCompressor
using ComponentArrays: ComponentVector
using DataInterpolations: LinearInterpolation, derivative, integral
using DataInterpolations:
LinearInterpolation, LinearInterpolationIntInv, invert_integral, derivative, integral
using Dates: Dates, DateTime, Millisecond, @dateformat_str
using DBInterface: execute
using DiffEqCallbacks:
Expand All @@ -50,6 +51,7 @@ using MetaGraphsNext:
inneighbor_labels
using OrdinaryDiffEq: OrdinaryDiffEq, OrdinaryDiffEqRosenbrockAdaptiveAlgorithm, get_du
using PreallocationTools: DiffCache, get_tmp
using ReadOnlyArrays: ReadOnlyVector
using SciMLBase:
init,
solve!,
Expand All @@ -64,7 +66,6 @@ using SciMLBase:
ODESolution,
VectorContinuousCallback,
get_proposed_dt
using SmoothInterpolation
using SparseArrays: SparseMatrixCSC, spzeros
using SQLite: SQLite, DB, Query, esc_id
using StructArrays: StructVector
Expand Down
15 changes: 13 additions & 2 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ end

Base.to_index(id::NodeID) = Int(id.value)

const ScalarInterpolation = LinearInterpolation{Vector{Float64}, Vector{Float64}, Float64}
const ScalarInterpolation = LinearInterpolation{
ReadOnlyVector{Float64, Vector{Float64}},
ReadOnlyVector{Float64, Vector{Float64}},
Vector{Float64},
Vector{Float64},
Float64,
}

"""
Store information for a subnetwork used for allocation.
Expand Down Expand Up @@ -261,7 +267,12 @@ end
current_area::T = zeros(length(node_id))
# Discrete values for interpolation
storage_to_level::Vector{
LinearInterpolationIntInv{Vector{Float64}, Vector{Float64}, Float64},
LinearInterpolationIntInv{
ReadOnlyVector{Float64, Vector{Float64}},
Vector{Float64},
ScalarInterpolation,
Float64,
},
}
level_to_area::Vector{ScalarInterpolation}
# Demands for allocation if applicable
Expand Down
14 changes: 10 additions & 4 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,7 @@ function Basin(db::DB, config::Config, graph::MetaGraph, chunk_sizes::Vector{Int
error("Invalid Basin / profile table.")
end

level_to_area = SmoothedLinearInterpolation.(area, level; extrapolate = true, λ = 0.01)
level_to_area = LinearInterpolation.(level_to_area)
level_to_area = LinearInterpolation.(area, level; extrapolate = true)
storage_to_level = invert_integral.(level_to_area)

return Basin(;
Expand Down Expand Up @@ -901,7 +900,12 @@ function user_demand_static!(
for row in group
priority_idx = findsorted(priorities, row.priority)
demand_row = coalesce(row.demand, 0.0)
demand_itp[user_demand_idx][priority_idx].u .= demand_row
demand_itp_old = demand_itp[user_demand_idx][priority_idx]
demand_itp[user_demand_idx][priority_idx] = LinearInterpolation(
fill(demand_row, 2),
demand_itp_old.t;
extrapolate = true,
)
demand[user_demand_idx, priority_idx] = demand_row
end
end
Expand Down Expand Up @@ -974,7 +978,9 @@ function UserDemand(db::DB, config::Config, graph::MetaGraph)::UserDemand
demand_reduced = zeros(n_user, n_priority)
trivial_timespan = [0.0, prevfloat(Inf)]
demand_itp = [
[LinearInterpolation(zeros(2), trivial_timespan) for i in eachindex(priorities)] for j in eachindex(node_ids)
ScalarInterpolation[
LinearInterpolation(zeros(2), trivial_timespan) for i in eachindex(priorities)
] for j in eachindex(node_ids)
]
demand_from_timeseries = fill(false, n_user)
allocated = fill(Inf, n_user, n_priority)
Expand Down
6 changes: 2 additions & 4 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ function get_scalar_interpolation(
push!(parameter, parameter[end])
end

itp = SmoothedLinearInterpolation(parameter, times; extrapolate = true, λ = 0.1)
return LinearInterpolation(itp), allunique(times)
return LinearInterpolation(parameter, times; extrapolate = true), allunique(times)
end

"""
Expand All @@ -121,8 +120,7 @@ function qh_interpolation(node_id::NodeID, table::StructVector)::ScalarInterpola
pushfirst!(level, first(level) - 1)
pushfirst!(flow_rate, first(flow_rate))

itp = SmoothedLinearInterpolation(flow_rate, level; extrapolate = true)
return LinearInterpolation(itp)
return LinearInterpolation(flow_rate, level; extrapolate = true)
end

"""
Expand Down
4 changes: 2 additions & 2 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ end
(; allocation_models) = p.allocation
(; basin, level_demand, graph) = p

fill!(level_demand.max_level[1].u, Inf)
fill!(level_demand.max_level[2].u, Inf)
fill!(level_demand.max_level[1].u.parent, Inf)
fill!(level_demand.max_level[2].u.parent, Inf)

# Given a max_level of Inf, the basin capacity is 0.0 because it is not possible for the basin level to be > Inf
@test Ribasim.get_basin_capacity(allocation_models[1], u, p, t, basin.node_id[1]) == 0.0
Expand Down
7 changes: 3 additions & 4 deletions core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ end

@testitem "Profile" begin
import Tables
using DataInterpolations: LinearInterpolation, integral
using SmoothInterpolation: invert_integral
using DataInterpolations: LinearInterpolation, integral, invert_integral

"Shorthand for Ribasim.get_area_and_level"
function lookup(profile, S)
Expand All @@ -313,7 +312,7 @@ end
storage = range(0.0, 1000.0, n_interpolations)

# Covers interpolation for constant and non-constant area, extrapolation for constant area
A = [0.0, 100.0, 100.0]
A = [1e-9, 100.0, 100.0]
h = [0.0, 10.0, 15.0]
S = integral.(Ref(LinearInterpolation(A, h)), h)
profile = (; S, A, h)
Expand Down Expand Up @@ -341,7 +340,7 @@ end
end

# Covers extrapolation for non-constant area
A = [0.0, 100.0]
A = [1e-9, 100.0]
h = [0.0, 10.0]
S = integral.(Ref(LinearInterpolation(A, h)), h)

Expand Down
6 changes: 2 additions & 4 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ end
@testitem "bottom" begin
using StructArrays: StructVector
using Ribasim: NodeID
using DataInterpolations: LinearInterpolation, integral
using SmoothInterpolation: invert_integral
using DataInterpolations: LinearInterpolation, integral, invert_integral

# create two basins with different bottoms/levels
area = [[0.01, 1.0], [0.01, 1.0]]
Expand Down Expand Up @@ -46,8 +45,7 @@ end
using StructArrays: StructVector
using Logging
using Ribasim: NodeID
using DataInterpolations: LinearInterpolation
using SmoothInterpolation: invert_integral
using DataInterpolations: LinearInterpolation, invert_integral

level = [
0.0,
Expand Down
7 changes: 5 additions & 2 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ end
parameters = model.integrator.p

(; graph, tabulated_rating_curve, basin) = parameters
tabulated_rating_curve.table[1].t[1] = invalid_level
tabulated_rating_curve.table[1].t.parent[1] = invalid_level

logger = TestLogger()
with_logger(logger) do
Expand Down Expand Up @@ -438,6 +438,9 @@ end
Ribasim.main(toml_path)
end

@test occursin("Warning: Convergence bottlenecks in descending order of severity:", output)
@test occursin(
"Warning: Convergence bottlenecks in descending order of severity:",
output,
)
@test occursin("Basin #11 = ", output)
end
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install = { depends_on = [
# Julia
update-registry-julia = "julia --eval='using Pkg; Registry.update()'"
update-manifest-julia = "julia --project --eval='using Pkg; Pkg.update()'"
instantiate-julia = "julia --project --eval='using Pkg; Pkg.instantiate()'"
instantiate-julia = { cmd = "julia --project --eval='using Pkg; Pkg.instantiate()'", env = { JULIA_SSL_CA_ROOTS_PATH = "" } }
initialize-julia = { depends_on = [
"update-registry-julia",
"instantiate-julia",
Expand Down