diff --git a/Manifest.toml b/Manifest.toml index ae5a50b7a..d00a2764c 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -2,7 +2,7 @@ julia_version = "1.11.1" manifest_format = "2.0" -project_hash = "0257f2772e4bfed0b6316b6871c4495978c173b4" +project_hash = "19c93fc8f0d17f2e1562ac67644d7881a73709f5" [[deps.ADTypes]] git-tree-sha1 = "eea5d80188827b35333801ef97a40c2ed653b081" @@ -1174,13 +1174,13 @@ version = "1.6.3" [[deps.OrdinaryDiffEqBDF]] deps = ["ArrayInterface", "DiffEqBase", "FastBroadcast", "LinearAlgebra", "MacroTools", "MuladdMacro", "OrdinaryDiffEqCore", "OrdinaryDiffEqDifferentiation", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqSDIRK", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "StaticArrays", "TruncatedStacktraces"] -git-tree-sha1 = "b4498d40bf35da0b6d22652ff2e9d8820590b3c6" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqBDF" uuid = "6ad6398a-0878-4a85-9266-38940aa047c8" version = "1.1.2" [[deps.OrdinaryDiffEqCore]] deps = ["ADTypes", "Accessors", "Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DocStringExtensions", "EnumX", "FastBroadcast", "FastClosures", "FastPower", "FillArrays", "FunctionWrappersWrappers", "InteractiveUtils", "LinearAlgebra", "Logging", "MacroTools", "MuladdMacro", "Polyester", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLOperators", "SciMLStructures", "SimpleUnPack", "Static", "StaticArrayInterface", "StaticArraysCore", "TruncatedStacktraces"] -git-tree-sha1 = "1175717a62ab21736a8f5d0d2531d2a6ad3b9e74" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqCore" uuid = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" version = "1.9.0" weakdeps = ["EnzymeCore"] @@ -1196,31 +1196,31 @@ version = "1.1.0" [[deps.OrdinaryDiffEqLowOrderRK]] deps = ["DiffEqBase", "FastBroadcast", "LinearAlgebra", "MuladdMacro", "OrdinaryDiffEqCore", "RecursiveArrayTools", "Reexport", "SciMLBase", "Static"] -git-tree-sha1 = "d4bb32e09d6b68ce2eb45fb81001eab46f60717a" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqLowOrderRK" uuid = "1344f307-1e59-4825-a18e-ace9aa3fa4c6" version = "1.2.0" [[deps.OrdinaryDiffEqNonlinearSolve]] deps = ["ADTypes", "ArrayInterface", "DiffEqBase", "FastBroadcast", "FastClosures", "ForwardDiff", "LinearAlgebra", "LinearSolve", "MuladdMacro", "NonlinearSolve", "OrdinaryDiffEqCore", "OrdinaryDiffEqDifferentiation", "PreallocationTools", "RecursiveArrayTools", "SciMLBase", "SciMLOperators", "SciMLStructures", "SimpleNonlinearSolve", "StaticArrays"] -git-tree-sha1 = "a2a4119f3e35f7982f78e17beea7b12485d179e9" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqNonlinearSolve" uuid = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" version = "1.2.1" [[deps.OrdinaryDiffEqRosenbrock]] deps = ["ADTypes", "DiffEqBase", "FastBroadcast", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "LinearSolve", "MacroTools", "MuladdMacro", "OrdinaryDiffEqCore", "OrdinaryDiffEqDifferentiation", "Polyester", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "Static"] -git-tree-sha1 = "96b47cdd12cb4ce8f70d701b49f855271a462bd4" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqRosenbrock" uuid = "43230ef6-c299-4910-a778-202eb28ce4ce" version = "1.2.0" [[deps.OrdinaryDiffEqSDIRK]] deps = ["DiffEqBase", "FastBroadcast", "LinearAlgebra", "MacroTools", "MuladdMacro", "OrdinaryDiffEqCore", "OrdinaryDiffEqDifferentiation", "OrdinaryDiffEqNonlinearSolve", "RecursiveArrayTools", "Reexport", "SciMLBase", "TruncatedStacktraces"] -git-tree-sha1 = "f6683803a58de600ab7a26d2f49411c9923e9721" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqSDIRK" uuid = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" version = "1.1.0" [[deps.OrdinaryDiffEqTsit5]] deps = ["DiffEqBase", "FastBroadcast", "LinearAlgebra", "MuladdMacro", "OrdinaryDiffEqCore", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "Static", "TruncatedStacktraces"] -git-tree-sha1 = "96552f7d4619fabab4038a29ed37dd55e9eb513a" +path = "C:\\Users\\konin_bt\\SciML\\OrdinaryDiffEq.jl\\lib\\OrdinaryDiffEqTsit5" uuid = "b1df2697-797e-41e3-8120-5422d3b24e4a" version = "1.1.0" @@ -1398,7 +1398,7 @@ uuid = "295af30f-e4ad-537b-8983-00126c2a3abe" version = "3.6.2" [[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", "Statistics", "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", "LineSearch", "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 = "2024.11.0" diff --git a/Project.toml b/Project.toml index 4e594a1cf..a6ce75c11 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,7 @@ authors = ["Deltares and contributors "] description = "Meta-project used to share the Manifest of Ribasim and its dependents" [deps] +ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45" Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" @@ -32,7 +33,7 @@ JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" Legolas = "741b9549-f6ed-4911-9fbf-4a1c0c97f0cd" LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433" -LineSearches = "d3d80556-e9d4-5f37-9878-2ab0fcc64255" +LineSearch = "87fe0de2-c867-4266-b59a-2f0a94fc965b" LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" @@ -43,7 +44,6 @@ OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" OrdinaryDiffEqLowOrderRK = "1344f307-1e59-4825-a18e-ace9aa3fa4c6" OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" -OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce" OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" OteraEngine = "b2d7f28f-acd6-4007-8b26-bc27716e5513" diff --git a/core/Project.toml b/core/Project.toml index 9e761b65d..89ffbf116 100644 --- a/core/Project.toml +++ b/core/Project.toml @@ -5,6 +5,7 @@ manifest = "../Manifest.toml" version = "2024.11.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" @@ -24,7 +25,7 @@ HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" Legolas = "741b9549-f6ed-4911-9fbf-4a1c0c97f0cd" -LineSearches = "d3d80556-e9d4-5f37-9878-2ab0fcc64255" +LineSearch = "87fe0de2-c867-4266-b59a-2f0a94fc965b" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -78,7 +79,7 @@ IOCapture = "0.2" IterTools = "1.4" JuMP = "1.15" Legolas = "0.5" -LineSearches = "7" +LineSearch = "0.1.4" LinearAlgebra = "1" LinearSolve = "2.24" Logging = "1" diff --git a/core/src/Ribasim.jl b/core/src/Ribasim.jl index b3656f4c1..83a0ab56b 100644 --- a/core/src/Ribasim.jl +++ b/core/src/Ribasim.jl @@ -22,8 +22,6 @@ using OrdinaryDiffEqCore: AbstractNLSolver, calculate_residuals! using DiffEqBase: DiffEqBase -using OrdinaryDiffEqNonlinearSolve: OrdinaryDiffEqNonlinearSolve, relax!, _compute_rhs! -using LineSearches: BackTracking # Interface for defining and solving the ODE problem of the physical layer. using SciMLBase: diff --git a/core/src/config.jl b/core/src/config.jl index 1fc0012e2..a49ad3ae7 100644 --- a/core/src/config.jl +++ b/core/src/config.jl @@ -8,18 +8,20 @@ Ribasim.config is a submodule mainly to avoid name clashes between the configura """ module config +using ADTypes: AutoFiniteDiff, AutoForwardDiff using Configurations: Configurations, @option, from_toml, @type_alias using DataStructures: DefaultDict using Dates: DateTime +using LineSearch: BackTracking using Logging: LogLevel, Debug, Info, Warn, Error using ..Ribasim: Ribasim, isnode, nodetype using OrdinaryDiffEqCore: OrdinaryDiffEqAlgorithm, OrdinaryDiffEqNewtonAdaptiveAlgorithm -using OrdinaryDiffEqNonlinearSolve: NLNewton +using OrdinaryDiffEqNonlinearSolve: NonlinearSolveAlg, NewtonRaphson using OrdinaryDiffEqLowOrderRK: Euler, RK4 using OrdinaryDiffEqTsit5: Tsit5 using OrdinaryDiffEqSDIRK: ImplicitEuler, KenCarp4, TRBDF2 using OrdinaryDiffEqBDF: FBDF, QNDF -using OrdinaryDiffEqRosenbrock: Rosenbrock23, Rodas4P, Rodas5P +# using OrdinaryDiffEqRosenbrock: Rosenbrock23, Rodas4P, Rodas5P export Config, Solver, Results, Logging, Toml export algorithm, @@ -253,10 +255,10 @@ Supported algorithms: const algorithms = Dict{String, Type}( "QNDF" => QNDF, "FBDF" => FBDF, - "Rosenbrock23" => Rosenbrock23, + # "Rosenbrock23" => Rosenbrock23, "TRBDF2" => TRBDF2, - "Rodas4P" => Rodas4P, - "Rodas5P" => Rodas5P, + # "Rodas4P" => Rodas4P, + # "Rodas5P" => Rodas5P, "KenCarp4" => KenCarp4, "Tsit5" => Tsit5, "RK4" => RK4, @@ -287,8 +289,8 @@ function algorithm(solver::Solver; u0 = [])::OrdinaryDiffEqAlgorithm kwargs = Dict{Symbol, Any}() if algotype <: OrdinaryDiffEqNewtonAdaptiveAlgorithm - kwargs[:nlsolve] = NLNewton(; - relax = Ribasim.MonitoredBackTracking(; z_tmp = copy(u0), dz_tmp = copy(u0)), + kwargs[:nlsolve] = NonlinearSolveAlg( + NewtonRaphson(; linesearch = BackTracking(), autodiff = AutoFiniteDiff()), ) end diff --git a/core/src/util.jl b/core/src/util.jl index d12498c9b..27051bd85 100644 --- a/core/src/util.jl +++ b/core/src/util.jl @@ -859,71 +859,6 @@ relaxed_root(x::GradientTracer, threshold::Real) = x get_level_from_storage(basin::Basin, state_idx::Int, storage::GradientTracer) = storage stop_declining_negative_storage!(du, u::ComponentVector{<:GradientTracer}) = nothing -@kwdef struct MonitoredBackTracking{B, V} - linesearch::B = BackTracking() - dz_tmp::V = [] - z_tmp::V = [] -end - -""" -Compute the residual of the non-linear solver, i.e. a measure of the -error in the solution to the implicit equation defined by the solver algorithm -""" -function residual(z, integrator, nlsolver, f) - (; uprev, t, p, dt, opts, isdae) = integrator - (; tmp, ztmp, γ, α, cache, method) = nlsolver - (; ustep, atmp, tstep, k, invγdt, tstep, k, invγdt) = cache - if isdae - _uprev = get_dae_uprev(integrator, uprev) - b, ustep2 = - _compute_rhs!(tmp, ztmp, ustep, α, tstep, k, invγdt, p, _uprev, f::TF, z) - else - b, ustep2 = - _compute_rhs!(tmp, ztmp, ustep, γ, α, tstep, k, invγdt, method, p, dt, f, z) - end - calculate_residuals!( - atmp, - b, - uprev, - ustep2, - opts.abstol, - opts.reltol, - opts.internalnorm, - t, - ) - ndz = opts.internalnorm(atmp, t) - return ndz -end - -""" -MonitoredBackTracing is a thin wrapper of BackTracking, making sure that -the BackTracking relaxation is rejected if it results in a residual increase -""" -function OrdinaryDiffEqNonlinearSolve.relax!( - dz, - nlsolver::AbstractNLSolver, - integrator::DEIntegrator, - f, - linesearch::MonitoredBackTracking, -) - (; linesearch, dz_tmp, z_tmp) = linesearch - - # Store step before relaxation - @. dz_tmp = dz - - # Apply relaxation and measure the residual change - @. z_tmp = nlsolver.z + dz - resid_before = residual(z_tmp, integrator, nlsolver, f) - relax!(dz, nlsolver, integrator, f, linesearch) - @. z_tmp = nlsolver.z + dz - resid_after = residual(z_tmp, integrator, nlsolver, f) - - # If the residual increased due to the relaxation, reject it - if resid_after > resid_before - @. dz = dz_tmp - end -end - function build_state_vector(p::Parameters) # It is assumed that the horizontal flow states come first in # p.state_inflow_edge and p.state_outflow_edge diff --git a/core/test/config_test.jl b/core/test/config_test.jl index 8f9cb3e8f..c3a1422b7 100644 --- a/core/test/config_test.jl +++ b/core/test/config_test.jl @@ -37,7 +37,8 @@ end @testitem "Solver" begin - using OrdinaryDiffEqCore: alg_autodiff, AutoFiniteDiff, AutoForwardDiff + using OrdinaryDiffEqCore: alg_autodiff + using ADTypes: AutoFiniteDiff, AutoForwardDiff using Ribasim: convert_saveat, convert_dt, Solver, algorithm solver = Solver()