From dfc693dc3d7d951731852911319568115f8b1b58 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 25 Sep 2024 15:14:32 -0400 Subject: [PATCH 1/6] simplify `__wrapprecs` previously the preconditioner being set was a very complicated `IdentityOperator`. Using a regular `IdentityOperator` means that solvers that don't support one of the preconditioners won't throw warnings since they mostly know that `IdentityOperator`s aren't real. --- src/internal/linear_solve.jl | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/internal/linear_solve.jl b/src/internal/linear_solve.jl index 27e6a780f..0ad81299e 100644 --- a/src/internal/linear_solve.jl +++ b/src/internal/linear_solve.jl @@ -242,14 +242,9 @@ function __set_lincache_A(lincache, new_A) end end -@inline function __wrapprecs(_Pl, _Pr, weight) - Pl = _Pl !== nothing ? - ComposePreconditioner(InvPreconditioner(Diagonal(_vec(weight))), _Pl) : - InvPreconditioner(Diagonal(_vec(weight))) - - Pr = _Pr !== nothing ? ComposePreconditioner(Diagonal(_vec(weight)), _Pr) : - Diagonal(_vec(weight)) - +function __wrapprecs(_Pl, _Pr, weight) + Pl = _Pl !== nothing ?= _Pl : IdentityOperator(length(weight)) + Pr = _Pr !== nothing ? _Pr : IdentityOperator(length(weight)) return Pl, Pr end From 23f61c9e76e6de7098bb85d9ea7df5c8203c84c3 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 25 Sep 2024 15:18:43 -0400 Subject: [PATCH 2/6] fix --- src/internal/linear_solve.jl | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/internal/linear_solve.jl b/src/internal/linear_solve.jl index 0ad81299e..3e7a191ae 100644 --- a/src/internal/linear_solve.jl +++ b/src/internal/linear_solve.jl @@ -80,14 +80,13 @@ function LinearSolverCache(alg, linsolve, A, b, u; stats, kwargs...) @bb u_ = copy(u_fixed) linprob = LinearProblem(A, b; u0 = u_, kwargs...) - weight = __init_ones(u_fixed) if __hasfield(alg, Val(:precs)) precs = alg.precs Pl_, Pr_ = precs(A, nothing, u, ntuple(Returns(nothing), 6)...) else precs, Pl_, Pr_ = nothing, nothing, nothing end - Pl, Pr = __wrapprecs(Pl_, Pr_, weight) + Pl, Pr = __wrapprecs(Pl_, Pr_, u) # Unalias here, we will later use these as caches lincache = init(linprob, linsolve; alias_A = false, alias_b = false, Pl, Pr) @@ -128,10 +127,8 @@ function (cache::LinearSolverCache)(; b !== nothing && (cache.lincache.b = b) linu !== nothing && __set_lincache_u!(cache, linu) - Plprev = cache.lincache.Pl isa ComposePreconditioner ? cache.lincache.Pl.outer : - cache.lincache.Pl - Prprev = cache.lincache.Pr isa ComposePreconditioner ? cache.lincache.Pr.outer : - cache.lincache.Pr + Plprev = cache.lincache.Pl + Prprev = cache.lincache.Pr if cache.precs === nothing _Pl, _Pr = nothing, nothing @@ -141,10 +138,7 @@ function (cache::LinearSolverCache)(; end if (_Pl !== nothing || _Pr !== nothing) - _weight = weight === nothing ? - (cache.lincache.Pr isa Diagonal ? cache.lincache.Pr.diag : - cache.lincache.Pr.inner.diag) : weight - Pl, Pr = __wrapprecs(_Pl, _Pr, _weight) + Pl, Pr = __wrapprecs(_Pl, _Pr, linu) cache.lincache.Pl = Pl cache.lincache.Pr = Pr end @@ -242,9 +236,9 @@ function __set_lincache_A(lincache, new_A) end end -function __wrapprecs(_Pl, _Pr, weight) - Pl = _Pl !== nothing ?= _Pl : IdentityOperator(length(weight)) - Pr = _Pr !== nothing ? _Pr : IdentityOperator(length(weight)) +function __wrapprecs(_Pl, _Pr, u) + Pl = _Pl !== nothing ?= _Pl : IdentityOperator(length(u)) + Pr = _Pr !== nothing ? _Pr : IdentityOperator(length(u)) return Pl, Pr end From c466c241ce6fef0c7ee938224e78fcc62b3bc171 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 27 Sep 2024 14:33:22 -0400 Subject: [PATCH 3/6] fix: typo --- src/internal/linear_solve.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/linear_solve.jl b/src/internal/linear_solve.jl index 3e7a191ae..a0c1ba664 100644 --- a/src/internal/linear_solve.jl +++ b/src/internal/linear_solve.jl @@ -237,7 +237,7 @@ function __set_lincache_A(lincache, new_A) end function __wrapprecs(_Pl, _Pr, u) - Pl = _Pl !== nothing ?= _Pl : IdentityOperator(length(u)) + Pl = _Pl !== nothing ? _Pl : IdentityOperator(length(u)) Pr = _Pr !== nothing ? _Pr : IdentityOperator(length(u)) return Pl, Pr end From f4219af451236c5251030e598d128a551bb5a53d Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Fri, 27 Sep 2024 14:53:08 -0400 Subject: [PATCH 4/6] remove __init_ones --- src/utils.jl | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 1a4ffd2c8..e13f9ca60 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -34,13 +34,6 @@ end @inline _restructure(y, x) = restructure(y, x) @inline _restructure(y::Number, x::Number) = x -@inline function __init_ones(x) - w = similar(x) - recursivefill!(w, true) - return w -end -@inline __init_ones(x::StaticArray) = ones(typeof(x)) - @inline __maybe_unaliased(x::Union{Number, SArray}, ::Bool) = x @inline function __maybe_unaliased(x::AbstractArray, alias::Bool) # Spend time coping iff we will mutate the array From 947c771fbbbcf783438265af52ef6bf55fb7ee7e Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Fri, 27 Sep 2024 20:57:08 -0400 Subject: [PATCH 5/6] remove imports --- src/NonlinearSolve.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NonlinearSolve.jl b/src/NonlinearSolve.jl index e68fa5472..34c53cf18 100644 --- a/src/NonlinearSolve.jl +++ b/src/NonlinearSolve.jl @@ -32,13 +32,13 @@ using LinearAlgebra: LinearAlgebra, ColumnNorm, Diagonal, I, LowerTriangular, Sy UpperTriangular, axpy!, cond, diag, diagind, dot, issuccess, istril, istriu, lu, mul!, norm, pinv, tril!, triu! using LineSearches: LineSearches -using LinearSolve: LinearSolve, LUFactorization, QRFactorization, ComposePreconditioner, - InvPreconditioner, needs_concrete_A, AbstractFactorization, +using LinearSolve: LinearSolve, LUFactorization, QRFactorization, + needs_concrete_A, AbstractFactorization, DefaultAlgorithmChoice, DefaultLinearSolver using MaybeInplace: @bb using Printf: @printf using Preferences: Preferences, @load_preference, @set_preferences! -using RecursiveArrayTools: recursivecopy!, recursivefill! +using RecursiveArrayTools: recursivecopy!, using SciMLBase: AbstractNonlinearAlgorithm, JacobianWrapper, AbstractNonlinearProblem, AbstractSciMLOperator, _unwrap_val, isinplace, NLStats using SciMLJacobianOperators: AbstractJacobianOperator, JacobianOperator, VecJacOperator, From 474d3f3e00bd0efabb66eae236cff60de966d433 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 27 Sep 2024 21:17:05 -0400 Subject: [PATCH 6/6] Update src/NonlinearSolve.jl --- src/NonlinearSolve.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NonlinearSolve.jl b/src/NonlinearSolve.jl index 34c53cf18..9f8048cfb 100644 --- a/src/NonlinearSolve.jl +++ b/src/NonlinearSolve.jl @@ -38,7 +38,7 @@ using LinearSolve: LinearSolve, LUFactorization, QRFactorization, using MaybeInplace: @bb using Printf: @printf using Preferences: Preferences, @load_preference, @set_preferences! -using RecursiveArrayTools: recursivecopy!, +using RecursiveArrayTools: recursivecopy! using SciMLBase: AbstractNonlinearAlgorithm, JacobianWrapper, AbstractNonlinearProblem, AbstractSciMLOperator, _unwrap_val, isinplace, NLStats using SciMLJacobianOperators: AbstractJacobianOperator, JacobianOperator, VecJacOperator,