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

Convert autodiff Boolean to ADTypes #2076

Merged
merged 2 commits into from
Feb 18, 2025
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
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.11.3"
manifest_format = "2.0"
project_hash = "921c2f0c042509feb4a1950ec24084c1ad561dd0"
project_hash = "728a3147f36fe80499c0ee10fefc8d0d614823e8"

[[deps.ADTypes]]
git-tree-sha1 = "fb97701c117c8162e84dfcf80215caa904aef44f"
Expand Down Expand Up @@ -2137,7 +2137,7 @@ weakdeps = ["Distributed"]
DistributedExt = "Distributed"

[[deps.Ribasim]]
deps = ["Accessors", "Arrow", "BasicModelInterface", "CodecZstd", "ComponentArrays", "Configurations", "DBInterface", "DataInterpolations", "DataStructures", "Dates", "DiffEqBase", "DiffEqCallbacks", "EnumX", "FiniteDiff", "Graphs", "HiGHS", "IterTools", "JuMP", "Legolas", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoggingExtras", "MetaGraphsNext", "OrdinaryDiffEqBDF", "OrdinaryDiffEqCore", "OrdinaryDiffEqLowOrderRK", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqRosenbrock", "OrdinaryDiffEqSDIRK", "OrdinaryDiffEqTsit5", "PreallocationTools", "SQLite", "SciMLBase", "SparseArrays", "SparseConnectivityTracer", "StructArrays", "Tables", "TerminalLoggers", "TranscodingStreams"]
deps = ["ADTypes", "Accessors", "Arrow", "BasicModelInterface", "CodecZstd", "ComponentArrays", "Configurations", "DBInterface", "DataInterpolations", "DataStructures", "Dates", "DiffEqBase", "DiffEqCallbacks", "EnumX", "FiniteDiff", "Graphs", "HiGHS", "IterTools", "JuMP", "Legolas", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoggingExtras", "MetaGraphsNext", "OrdinaryDiffEqBDF", "OrdinaryDiffEqCore", "OrdinaryDiffEqLowOrderRK", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqRosenbrock", "OrdinaryDiffEqSDIRK", "OrdinaryDiffEqTsit5", "PreallocationTools", "SQLite", "SciMLBase", "SparseArrays", "SparseConnectivityTracer", "StructArrays", "Tables", "TerminalLoggers", "TranscodingStreams"]
path = "core"
uuid = "aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"
version = "2025.1.0"
Expand Down
2 changes: 2 additions & 0 deletions core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ manifest = "../Manifest.toml"
version = "2025.1.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
BasicModelInterface = "59605e27-edc0-445a-b93d-c09a3a50b330"
Expand Down Expand Up @@ -55,6 +56,7 @@ Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
RibasimMakieExt = ["Makie", "DataFrames"]

[compat]
ADTypes = "1.13"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabeticly ordered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upper case is sorted before lowercase -_-
This is where Pkg wants it.

Accessors = "0.1"
Aqua = "0.8"
Arrow = "2.3"
Expand Down
6 changes: 3 additions & 3 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@
basin.concentration_data.concentration_external[listen_node_id.idx][variable](t)
elseif startswith(variable, "concentration.")
substance = Symbol(last(split(variable, ".")))
var_idx = findfirst(==(substance), basin.concentration_data.substances)
var_idx = find_index(substance, basin.concentration_data.substances)

Check warning on line 617 in core/src/callback.jl

View check run for this annotation

Codecov / codecov/patch

core/src/callback.jl#L617

Added line #L617 was not covered by tests
value = basin.concentration_data.concentration_state[listen_node_id.idx, var_idx]
else
error("Unsupported condition variable $variable.")
Expand Down Expand Up @@ -753,7 +753,7 @@

for row in timeblock
i = searchsortedfirst(node_id, NodeID(NodeType.Basin, row.node_id, 0))
j = findfirst(==(Symbol(row.substance)), substances)
j = find_index(Symbol(row.substance), substances)
ismissing(row.drainage) || (concentration[1, i, j] = row.drainage)
ismissing(row.precipitation) || (concentration[2, i, j] = row.precipitation)
end
Expand All @@ -774,7 +774,7 @@

for row in timeblock
i = searchsortedfirst(node_id, NodeID(nodetype, row.node_id, 0))
j = findfirst(==(Symbol(row.substance)), substances)
j = find_index(Symbol(row.substance), substances)
ismissing(row.concentration) || (concentration[i, j] = row.concentration)
end
return nothing
Expand Down
3 changes: 2 additions & 1 deletion core/src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using DataStructures: DefaultDict
using Dates: DateTime
using Logging: LogLevel, Debug, Info, Warn, Error
using ..Ribasim: Ribasim, isnode, nodetype
using ADTypes: AutoForwardDiff, AutoFiniteDiff
using OrdinaryDiffEqCore: OrdinaryDiffEqAlgorithm, OrdinaryDiffEqNewtonAdaptiveAlgorithm
using OrdinaryDiffEqNonlinearSolve: NLNewton
using OrdinaryDiffEqLowOrderRK: Euler, RK4
Expand Down Expand Up @@ -305,7 +306,7 @@ function algorithm(solver::Solver; u0 = [])::OrdinaryDiffEqAlgorithm
end

if function_accepts_kwarg(algotype, :autodiff)
kwargs[:autodiff] = solver.autodiff
kwargs[:autodiff] = solver.autodiff ? AutoForwardDiff() : AutoFiniteDiff()
end

algotype(; kwargs...)
Expand Down
2 changes: 1 addition & 1 deletion core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ function set_concentrations!(
)
for substance in unique(concentration_data.substance)
data_sub = filter(row -> row.substance == substance, concentration_data)
sub_idx = findfirst(==(Symbol(substance)), substances)
sub_idx = find_index(Symbol(substance), substances)
for group in IterTools.groupby(row -> row.node_id, data_sub)
first_row = first(group)
value = getproperty(first_row, concentration_column)
Expand Down
13 changes: 13 additions & 0 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1138,3 +1138,16 @@ function unsafe_array(
)::Vector{Float64}
GC.@preserve A unsafe_wrap(Array, pointer(A), length(A))
end

"""
Find the index of a symbol in an ordered set using iteration.

This replaces `findfirst(==(x), s)` which triggered this depwarn:
> indexing is deprecated for OrderedSet, please rewrite your code to use iteration
"""
function find_index(x::Symbol, s::OrderedSet{Symbol})
for (i, s) in enumerate(s)
s === x && return i
end
error(lazy"$x not found in $s.")
end
9 changes: 9 additions & 0 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,12 @@ end
x.b[2] = 10.0
@test y[2] === 10.0
end

@testitem "find_index" begin
using Ribasim: find_index
using DataStructures: OrderedSet
s = OrderedSet([:a, :b, :c])
@test find_index(:a, s) === 1
@test find_index(:c, s) === 3
@test_throws "not found" find_index(:d, s)
end