Skip to content

Commit d6d741b

Browse files
author
Oscar Smith
authored
refactor: simplify __wrapprecs (#465)
* 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. * fix * fix: typo * remove __init_ones * remove imports * Update src/NonlinearSolve.jl
1 parent 736b4a3 commit d6d741b

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

src/NonlinearSolve.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ using LinearAlgebra: LinearAlgebra, ColumnNorm, Diagonal, I, LowerTriangular, Sy
3232
UpperTriangular, axpy!, cond, diag, diagind, dot, issuccess, istril,
3333
istriu, lu, mul!, norm, pinv, tril!, triu!
3434
using LineSearches: LineSearches
35-
using LinearSolve: LinearSolve, LUFactorization, QRFactorization, ComposePreconditioner,
36-
InvPreconditioner, needs_concrete_A, AbstractFactorization,
35+
using LinearSolve: LinearSolve, LUFactorization, QRFactorization,
36+
needs_concrete_A, AbstractFactorization,
3737
DefaultAlgorithmChoice, DefaultLinearSolver
3838
using MaybeInplace: @bb
3939
using Printf: @printf
4040
using Preferences: Preferences, @load_preference, @set_preferences!
41-
using RecursiveArrayTools: recursivecopy!, recursivefill!
41+
using RecursiveArrayTools: recursivecopy!
4242
using SciMLBase: AbstractNonlinearAlgorithm, JacobianWrapper, AbstractNonlinearProblem,
4343
AbstractSciMLOperator, _unwrap_val, isinplace, NLStats
4444
using SciMLJacobianOperators: AbstractJacobianOperator, JacobianOperator, VecJacOperator,

src/internal/linear_solve.jl

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ function LinearSolverCache(alg, linsolve, A, b, u; stats, kwargs...)
8080
@bb u_ = copy(u_fixed)
8181
linprob = LinearProblem(A, b; u0 = u_, kwargs...)
8282

83-
weight = __init_ones(u_fixed)
8483
if __hasfield(alg, Val(:precs))
8584
precs = alg.precs
8685
Pl_, Pr_ = precs(A, nothing, u, ntuple(Returns(nothing), 6)...)
8786
else
8887
precs, Pl_, Pr_ = nothing, nothing, nothing
8988
end
90-
Pl, Pr = __wrapprecs(Pl_, Pr_, weight)
89+
Pl, Pr = __wrapprecs(Pl_, Pr_, u)
9190

9291
# Unalias here, we will later use these as caches
9392
lincache = init(linprob, linsolve; alias_A = false, alias_b = false, Pl, Pr)
@@ -128,10 +127,8 @@ function (cache::LinearSolverCache)(;
128127
b !== nothing && (cache.lincache.b = b)
129128
linu !== nothing && __set_lincache_u!(cache, linu)
130129

131-
Plprev = cache.lincache.Pl isa ComposePreconditioner ? cache.lincache.Pl.outer :
132-
cache.lincache.Pl
133-
Prprev = cache.lincache.Pr isa ComposePreconditioner ? cache.lincache.Pr.outer :
134-
cache.lincache.Pr
130+
Plprev = cache.lincache.Pl
131+
Prprev = cache.lincache.Pr
135132

136133
if cache.precs === nothing
137134
_Pl, _Pr = nothing, nothing
@@ -141,10 +138,7 @@ function (cache::LinearSolverCache)(;
141138
end
142139

143140
if (_Pl !== nothing || _Pr !== nothing)
144-
_weight = weight === nothing ?
145-
(cache.lincache.Pr isa Diagonal ? cache.lincache.Pr.diag :
146-
cache.lincache.Pr.inner.diag) : weight
147-
Pl, Pr = __wrapprecs(_Pl, _Pr, _weight)
141+
Pl, Pr = __wrapprecs(_Pl, _Pr, linu)
148142
cache.lincache.Pl = Pl
149143
cache.lincache.Pr = Pr
150144
end
@@ -242,14 +236,9 @@ function __set_lincache_A(lincache, new_A)
242236
end
243237
end
244238

245-
@inline function __wrapprecs(_Pl, _Pr, weight)
246-
Pl = _Pl !== nothing ?
247-
ComposePreconditioner(InvPreconditioner(Diagonal(_vec(weight))), _Pl) :
248-
InvPreconditioner(Diagonal(_vec(weight)))
249-
250-
Pr = _Pr !== nothing ? ComposePreconditioner(Diagonal(_vec(weight)), _Pr) :
251-
Diagonal(_vec(weight))
252-
239+
function __wrapprecs(_Pl, _Pr, u)
240+
Pl = _Pl !== nothing ? _Pl : IdentityOperator(length(u))
241+
Pr = _Pr !== nothing ? _Pr : IdentityOperator(length(u))
253242
return Pl, Pr
254243
end
255244

src/utils.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ end
3434
@inline _restructure(y, x) = restructure(y, x)
3535
@inline _restructure(y::Number, x::Number) = x
3636

37-
@inline function __init_ones(x)
38-
w = similar(x)
39-
recursivefill!(w, true)
40-
return w
41-
end
42-
@inline __init_ones(x::StaticArray) = ones(typeof(x))
43-
4437
@inline __maybe_unaliased(x::Union{Number, SArray}, ::Bool) = x
4538
@inline function __maybe_unaliased(x::AbstractArray, alias::Bool)
4639
# Spend time coping iff we will mutate the array

0 commit comments

Comments
 (0)