From 1249784175c45432c8d17896b1bc27e271109449 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 23 Sep 2024 11:00:10 +0200 Subject: [PATCH 1/4] Default to finite difference calculation of the Jacobian Fixes #1832 --- core/src/config.jl | 2 +- core/test/config_test.jl | 4 ++-- core/test/docs.toml | 2 +- python/ribasim/ribasim/config.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/config.jl b/core/src/config.jl index 95e4d8c52..1aa849892 100644 --- a/core/src/config.jl +++ b/core/src/config.jl @@ -106,7 +106,7 @@ const nodetypes = collect(keys(nodekinds)) water_balance_reltol::Float64 = 1e-2 maxiters::Int = 1e9 sparse::Bool = true - autodiff::Bool = true + autodiff::Bool = false end # Separate struct, as basin clashes with nodetype diff --git a/core/test/config_test.jl b/core/test/config_test.jl index 07021234e..8f9cb3e8f 100644 --- a/core/test/config_test.jl +++ b/core/test/config_test.jl @@ -32,7 +32,7 @@ @testset "docs" begin config = Ribasim.Config(normpath(@__DIR__, "docs.toml")) @test config isa Ribasim.Config - @test config.solver.autodiff + @test !config.solver.autodiff end end @@ -60,7 +60,7 @@ end AutoForwardDiff() @test alg_autodiff(algorithm(Solver(; algorithm = "QNDF", autodiff = false))) == AutoFiniteDiff() - @test alg_autodiff(algorithm(Solver(; algorithm = "QNDF"))) == AutoForwardDiff() + @test alg_autodiff(algorithm(Solver(; algorithm = "QNDF"))) == AutoFiniteDiff() # autodiff is not a kwargs for explicit algorithms, but we use try-catch to bypass algorithm(Solver(; algorithm = "Euler", autodiff = true)) diff --git a/core/test/docs.toml b/core/test/docs.toml index 1ab1721d3..a327f01f5 100644 --- a/core/test/docs.toml +++ b/core/test/docs.toml @@ -37,7 +37,7 @@ water_balance_abstol = 1e-6 # optional, default 1e-6 water_balance_reltol = 1e-6 # optional, default 1e-6 maxiters = 1e9 # optional, default 1e9 sparse = true # optional, default true -autodiff = true # optional, default true +autodiff = false # optional, default false [logging] # defines the logging level of Ribasim diff --git a/python/ribasim/ribasim/config.py b/python/ribasim/ribasim/config.py index 9e9a7d18d..bc3d7ad5b 100644 --- a/python/ribasim/ribasim/config.py +++ b/python/ribasim/ribasim/config.py @@ -121,7 +121,7 @@ class Solver(ChildModel): reltol: float = 1e-05 maxiters: int = 1000000000 sparse: bool = True - autodiff: bool = True + autodiff: bool = False class Verbosity(str, Enum): From 7bf92a5d1326ffc579f7712d441bec1ec6b7c7d4 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 23 Sep 2024 11:37:51 +0200 Subject: [PATCH 2/4] Increase Manning test tolerance --- core/test/run_models_test.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/test/run_models_test.jl b/core/test/run_models_test.jl index 349546f50..c107cf8e3 100644 --- a/core/test/run_models_test.jl +++ b/core/test/run_models_test.jl @@ -506,7 +506,7 @@ end # numerical choices to make in terms of what the representative friction # slope is. See e.g.: # https://www.hec.usace.army.mil/confluence/rasdocs/ras1dtechref/latest/theoretical-basis-for-one-dimensional-and-two-dimensional-hydrodynamic-calculations/1d-steady-flow-water-surface-profiles/friction-loss-evaluation - @test all(isapprox.(h_expected, h_actual; atol = 0.02)) + @test all(isapprox.(h_expected, h_actual; atol = 0.04)) # Test for conservation of mass, flow at the beginning == flow at the end @test Ribasim.get_flow(du, p, t, (NodeID(:FlowBoundary, 1, p), NodeID(:Basin, 2, p))) ≈ 5.0 atol = 0.001 skip = Sys.isapple() @@ -515,7 +515,7 @@ end p, t, (NodeID(:ManningResistance, 101, p), NodeID(:Basin, 102, p)), - ) ≈ 5.0 atol = 0.001 skip = Sys.isapple() + ) ≈ 5.0 atol = 0.002 skip = Sys.isapple() end @testitem "mean_flow" begin From 83699272d30625c4c98ab2fa8f57d1e233644b86 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 23 Sep 2024 13:16:41 +0200 Subject: [PATCH 3/4] log --- core/test/run_models_test.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/test/run_models_test.jl b/core/test/run_models_test.jl index 4a71ed219..54a672961 100644 --- a/core/test/run_models_test.jl +++ b/core/test/run_models_test.jl @@ -506,6 +506,11 @@ end # numerical choices to make in terms of what the representative friction # slope is. See e.g.: # https://www.hec.usace.army.mil/confluence/rasdocs/ras1dtechref/latest/theoretical-basis-for-one-dimensional-and-two-dimensional-hydrodynamic-calculations/1d-steady-flow-water-surface-profiles/friction-loss-evaluation + for (i, (a, b)) in enumerate(zip(h_expected, h_actual)) + @testset "approx $i" begin + @test isapprox(a, b; atol = 0.04) + end + end @test all(isapprox.(h_expected, h_actual; atol = 0.04)) # Test for conservation of mass, flow at the beginning == flow at the end @test Ribasim.get_flow(du, p, t, (NodeID(:FlowBoundary, 1, p), NodeID(:Basin, 2, p))) ≈ From 167b7e618e8f1c4555d1b2550d0465c9f9609027 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 23 Sep 2024 14:17:35 +0200 Subject: [PATCH 4/4] Use autodiff in Backwater model --- core/test/run_models_test.jl | 9 ++------- .../ribasim_testmodels/ribasim_testmodels/backwater.py | 1 + 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/core/test/run_models_test.jl b/core/test/run_models_test.jl index 54a672961..8e60ef289 100644 --- a/core/test/run_models_test.jl +++ b/core/test/run_models_test.jl @@ -506,12 +506,7 @@ end # numerical choices to make in terms of what the representative friction # slope is. See e.g.: # https://www.hec.usace.army.mil/confluence/rasdocs/ras1dtechref/latest/theoretical-basis-for-one-dimensional-and-two-dimensional-hydrodynamic-calculations/1d-steady-flow-water-surface-profiles/friction-loss-evaluation - for (i, (a, b)) in enumerate(zip(h_expected, h_actual)) - @testset "approx $i" begin - @test isapprox(a, b; atol = 0.04) - end - end - @test all(isapprox.(h_expected, h_actual; atol = 0.04)) + @test all(isapprox.(h_expected, h_actual; atol = 0.02)) # Test for conservation of mass, flow at the beginning == flow at the end @test Ribasim.get_flow(du, p, t, (NodeID(:FlowBoundary, 1, p), NodeID(:Basin, 2, p))) ≈ 5.0 atol = 0.001 skip = Sys.isapple() @@ -520,7 +515,7 @@ end p, t, (NodeID(:ManningResistance, 101, p), NodeID(:Basin, 102, p)), - ) ≈ 5.0 atol = 0.002 skip = Sys.isapple() + ) ≈ 5.0 atol = 0.001 skip = Sys.isapple() end @testitem "mean_flow" begin diff --git a/python/ribasim_testmodels/ribasim_testmodels/backwater.py b/python/ribasim_testmodels/ribasim_testmodels/backwater.py index b40b72083..81f6fa2a0 100644 --- a/python/ribasim_testmodels/ribasim_testmodels/backwater.py +++ b/python/ribasim_testmodels/ribasim_testmodels/backwater.py @@ -23,6 +23,7 @@ def backwater_model(): starttime="2020-01-01", endtime="2021-01-01", crs="EPSG:28992", + solver=ribasim.Solver(autodiff=True), ) model.flow_boundary.add(