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

Show convergence bottleneck nodes to users #1440

Merged
merged 14 commits into from
May 8, 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
57 changes: 31 additions & 26 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.3"
manifest_format = "2.0"
project_hash = "dc6d536cc58558be9be8b5e2e4d511d0451ed14f"
project_hash = "bd4c7b1f5d210b9878847b2e4ee3ae8220c5f1c7"

[[deps.ADTypes]]
git-tree-sha1 = "016833eb52ba2d6bea9fcb50ca295980e728ee24"
Expand Down Expand Up @@ -100,9 +100,9 @@ weakdeps = ["SparseArrays"]

[[deps.Arrow]]
deps = ["ArrowTypes", "BitIntegers", "CodecLz4", "CodecZstd", "ConcurrentUtilities", "DataAPI", "Dates", "EnumX", "LoggingExtras", "Mmap", "PooledArrays", "SentinelArrays", "Tables", "TimeZones", "TranscodingStreams", "UUIDs"]
git-tree-sha1 = "29faa9835f77dee04b2833b2c9ee415223b3ebbd"
git-tree-sha1 = "f8d411d1b45459368567dc51f683ed78a919d795"
uuid = "69666777-d1a9-59fb-9406-91d4454c9d45"
version = "2.7.1"
version = "2.7.2"

[[deps.ArrowTypes]]
deps = ["Sockets", "UUIDs"]
Expand Down Expand Up @@ -210,9 +210,9 @@ version = "0.3.0"

[[deps.Compat]]
deps = ["TOML", "UUIDs"]
git-tree-sha1 = "c955881e3c981181362ae4088b35995446298b80"
git-tree-sha1 = "b1c55339b7c6c350ee89f2c1604299660525b248"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.14.0"
version = "4.15.0"
weakdeps = ["Dates", "LinearAlgebra"]

[deps.Compat.extensions]
Expand Down Expand Up @@ -327,20 +327,19 @@ uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "1.6.1"

[[deps.DataInterpolations]]
deps = ["FindFirstFunctions", "LinearAlgebra", "PrettyTables", "RecipesBase", "Reexport"]
git-tree-sha1 = "6af80a2e4d79f1f1ed9f0cee69ba5ac4ba3358a1"
deps = ["FindFirstFunctions", "ForwardDiff", "LinearAlgebra", "PrettyTables", "RecipesBase", "Reexport"]
git-tree-sha1 = "b24ab19ead284c563c9e494899d2464a7e95face"
uuid = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
version = "4.7.2"
version = "4.8.0"

[deps.DataInterpolations.extensions]
DataInterpolationsChainRulesCoreExt = "ChainRulesCore"
DataInterpolationsOptimExt = ["ForwardDiff", "Optim"]
DataInterpolationsOptimExt = "Optim"
DataInterpolationsRegularizationToolsExt = "RegularizationTools"
DataInterpolationsSymbolicsExt = "Symbolics"

[deps.DataInterpolations.weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
RegularizationTools = "29dad682-9a27-4bc3-9c72-016788665182"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Expand Down Expand Up @@ -368,9 +367,9 @@ version = "0.4.2"

[[deps.DiffEqBase]]
deps = ["ArrayInterface", "ConcreteStructs", "DataStructures", "DocStringExtensions", "EnumX", "EnzymeCore", "FastBroadcast", "FastClosures", "ForwardDiff", "FunctionWrappers", "FunctionWrappersWrappers", "LinearAlgebra", "Logging", "Markdown", "MuladdMacro", "Parameters", "PreallocationTools", "PrecompileTools", "Printf", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Static", "StaticArraysCore", "Statistics", "Tricks", "TruncatedStacktraces"]
git-tree-sha1 = "531c53fd0405716712a8b4960216c3b7b5ec89b9"
git-tree-sha1 = "c8b0bdee28a1addddb7ab939365fe6543d7d2d0d"
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
version = "6.149.1"
version = "6.149.2"

[deps.DiffEqBase.extensions]
DiffEqBaseChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -496,9 +495,9 @@ uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[deps.FillArrays]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "57f08d5665e76397e96b168f9acc12ab17c84a68"
git-tree-sha1 = "0653c0a2396a6da5bc4766c43041ef5fd3efbe57"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.10.2"
version = "1.11.0"

[deps.FillArrays.extensions]
FillArraysPDMatsExt = "PDMats"
Expand Down Expand Up @@ -581,9 +580,9 @@ version = "1.3.1"

[[deps.Graphs]]
deps = ["ArnoldiMethod", "Compat", "DataStructures", "Distributed", "Inflate", "LinearAlgebra", "Random", "SharedArrays", "SimpleTraits", "SparseArrays", "Statistics"]
git-tree-sha1 = "3863330da5466410782f2bffc64f3d505a6a8334"
git-tree-sha1 = "4f2b57488ac7ee16124396de4f2bbdd51b2602ad"
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
version = "1.10.0"
version = "1.11.0"

[[deps.HiGHS]]
deps = ["HiGHS_jll", "MathOptInterface", "PrecompileTools", "SparseArrays"]
Expand All @@ -609,6 +608,12 @@ git-tree-sha1 = "47ac8cc196b81001a711f4b2c12c97372338f00c"
uuid = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
version = "1.24.2"

[[deps.IOCapture]]
deps = ["Logging", "Random"]
git-tree-sha1 = "8b72179abc660bfab5e28472e019392b97d0985c"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.2.4"

[[deps.IfElse]]
git-tree-sha1 = "debdd00ffef04665ccbb3e150747a77560e8fad1"
uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down Expand Up @@ -1065,9 +1070,9 @@ version = "1.6.3"

[[deps.OrdinaryDiffEq]]
deps = ["ADTypes", "Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DocStringExtensions", "ExponentialUtilities", "FastBroadcast", "FastClosures", "FillArrays", "FiniteDiff", "ForwardDiff", "FunctionWrappersWrappers", "IfElse", "InteractiveUtils", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "MacroTools", "MuladdMacro", "NonlinearSolve", "Polyester", "PreallocationTools", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLOperators", "SimpleNonlinearSolve", "SimpleUnPack", "SparseArrays", "SparseDiffTools", "StaticArrayInterface", "StaticArrays", "TruncatedStacktraces"]
git-tree-sha1 = "91079af18db922354197eeae2a17b177079e24c1"
git-tree-sha1 = "cd8c4fb1cc88e65e27f92c7e714afc430cd1debc"
uuid = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
version = "6.74.1"
version = "6.75.0"

[[deps.OteraEngine]]
deps = ["Markdown", "Pkg", "TOML"]
Expand Down Expand Up @@ -1188,9 +1193,9 @@ version = "1.3.4"

[[deps.RecursiveArrayTools]]
deps = ["Adapt", "ArrayInterface", "DocStringExtensions", "GPUArraysCore", "IteratorInterfaceExtensions", "LinearAlgebra", "RecipesBase", "SparseArrays", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables"]
git-tree-sha1 = "6305b6fe0391eeb4f280a4a403ac0c350b82c718"
git-tree-sha1 = "f599a896fb28043dd63a4d372231dfcbdd117394"
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
version = "3.15.0"
version = "3.16.0"

[deps.RecursiveArrayTools.extensions]
RecursiveArrayToolsFastBroadcastExt = "FastBroadcast"
Expand Down Expand Up @@ -1341,9 +1346,9 @@ uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[deps.SimpleNonlinearSolve]]
deps = ["ADTypes", "ArrayInterface", "ConcreteStructs", "DiffEqBase", "DiffResults", "FastClosures", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "MaybeInplace", "PrecompileTools", "Reexport", "SciMLBase", "StaticArraysCore"]
git-tree-sha1 = "d4c17fc60bf5f8f2be02777c4836878f27ac7b9b"
git-tree-sha1 = "c020028bb22a2f23cbd88cb92cf47cbb8c98513f"
uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7"
version = "1.7.0"
version = "1.8.0"

[deps.SimpleNonlinearSolve.extensions]
SimpleNonlinearSolveChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -1420,9 +1425,9 @@ version = "0.3.9"

[[deps.SpecialFunctions]]
deps = ["IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"]
git-tree-sha1 = "e2cfc4012a19088254b3950b85c3c1d8882d864d"
git-tree-sha1 = "2f5d4697f21388cbe1ff299430dd169ef97d7e14"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "2.3.1"
version = "2.4.0"
weakdeps = ["ChainRulesCore"]

[deps.SpecialFunctions.extensions]
Expand Down Expand Up @@ -1579,9 +1584,9 @@ uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
version = "0.5.23"

[[deps.TranscodingStreams]]
git-tree-sha1 = "71509f04d045ec714c4748c785a59045c3736349"
git-tree-sha1 = "5d54d076465da49d6746c647022f3b3674e64156"
uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
version = "0.10.7"
version = "0.10.8"
weakdeps = ["Random", "Test"]

[deps.TranscodingStreams.extensions]
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
Expand Down
25 changes: 25 additions & 0 deletions core/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@
t = datetime_since(model.integrator.t, starttime)
retcode = model.integrator.sol.retcode
@error "The model exited at model time $t with return code $retcode.\nSee https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes"

(; cache, p, u) = model.integrator
(; basin) = p
# Indicate convergence bottlenecks if possible with the current algorithm
if hasproperty(cache, :nlsolver)
storage_error = @. abs(cache.nlsolver.cache.atmp.storage / u.storage)
@show storage_error
perm = sortperm(storage_error; rev = true)
println(
"The following basins were identified as convergence bottlenecks (in descending order of severity):",
)
for i in perm
node_id = basin.node_id.values[i]
error = storage_error[i]
if error < config.solver.reltol
break
end
println("$node_id, error = $error")
end
else
algorithm = model.config.solver_algorithm
println(

Check warning on line 70 in core/src/main.jl

View check run for this annotation

Codecov / codecov/patch

core/src/main.jl#L69-L70

Added lines #L69 - L70 were not covered by tests
"The current algorithm ($algorithm) does not support indicating convergence bottlenecks, for that try a different one.",
)
end
return 1
end
end
Expand Down
18 changes: 18 additions & 0 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,21 @@ end
dt,
)
end

@testitem "Convergence bottleneck" begin
using IOCapture: capture
toml_path =
normpath(@__DIR__, "../../generated_testmodels/invalid_unstable/ribasim.toml")
@test ispath(toml_path)
(; output) = capture() do
Ribasim.main(toml_path)
end
output = split(output, "\n")[(end - 4):end]
@test startswith(
output[1],
"The following basins were identified as convergence bottlenecks",
)
@test startswith(output[2], "Basin #11")
@test startswith(output[3], "Basin #31")
@test startswith(output[4], "Basin #51")
end
2 changes: 2 additions & 0 deletions python/ribasim_testmodels/ribasim_testmodels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
invalid_edge_types_model,
invalid_fractional_flow_model,
invalid_qh_model,
invalid_unstable_model,
)
from ribasim_testmodels.pid_control import (
discrete_control_of_pid_control_model,
Expand All @@ -74,6 +75,7 @@
"invalid_edge_types_model",
"invalid_fractional_flow_model",
"invalid_qh_model",
"invalid_unstable_model",
"leaky_bucket_model",
"level_boundary_condition_model",
"level_demand_model",
Expand Down
28 changes: 27 additions & 1 deletion python/ribasim_testmodels/ribasim_testmodels/invalid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any

import pandas as pd
from ribasim.config import Node
from ribasim.config import Node, Solver
from ribasim.input_base import TableModel
from ribasim.model import Model
from ribasim.nodes import (
Expand Down Expand Up @@ -223,3 +223,29 @@ def invalid_edge_types_model() -> Model:
model.edge.df["edge_type"] = ["foo", "bar"]

return model


def invalid_unstable_model() -> Model:
"""Model with several extremely quickly emptying basins."""

model = Model(
starttime="2020-01-01",
endtime="2021-01-01",
crs="EPSG:28992",
solver=Solver(dtmin=60.0),
)
id_shift = 10
for i in range(6):
model.basin.add(
Node(1 + id_shift * i, Point(i, 0)),
[basin.Profile(area=1000.0, level=[0.0, 1.0]), basin.State(level=[1.0])],
)
flow_rate = 1.0 if (i % 2 == 0) else 1e10
model.pump.add(
Node(2 + id_shift * i, Point(i, 1)), [pump.Static(flow_rate=[flow_rate])]
)
model.terminal.add(Node(3 + id_shift * i, Point(i, 2)))

model.edge.add(model.basin[1 + id_shift * i], model.pump[2 + id_shift * i])
model.edge.add(model.pump[2 + id_shift * i], model.terminal[3 + id_shift * i])
return model