Skip to content

Commit a739091

Browse files
committed
Fix static arrays
1 parent 8f9a432 commit a739091

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NonlinearSolve"
22
uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
33
authors = ["SciML"]
4-
version = "3.5.4"
4+
version = "3.5.5"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

src/internal/linear_solve.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,32 @@ function reinit_cache!(cache::LinearSolverCache, args...; kwargs...)
4949
cache.nfactors = 0
5050
end
5151

52+
@inline __fix_strange_type_combination(A, b, u) = u
53+
@inline function __fix_strange_type_combination(A, b, u::SArray)
54+
A isa SArray && b isa SArray && return u
55+
@warn "Solving Linear System A::$(typeof(A)) x::$(typeof(u)) = b::$(typeof(u)) is not \
56+
properly supported. Converting `x` to a mutable array. Check the return type \
57+
of the nonlinear function provided for optimal performance." maxlog=1
58+
return MArray(u)
59+
end
60+
61+
@inline __set_lincache_u!(cache, u) = (cache.lincache.u = u)
62+
@inline function __set_lincache_u!(cache, u::SArray)
63+
cache.lincache.u isa MArray && return __set_lincache_u!(cache, MArray(u))
64+
cache.lincache.u = u
65+
end
66+
5267
function LinearSolverCache(alg, linsolve, A, b, u; kwargs...)
68+
u_fixed = __fix_strange_type_combination(A, b, u)
69+
5370
if (A isa Number && b isa Number) || (linsolve === nothing && A isa SMatrix) ||
5471
(A isa Diagonal) || (linsolve isa typeof(\))
5572
return LinearSolverCache(nothing, nothing, A, b, nothing, 0, 0)
5673
end
57-
@bb u_ = copy(u)
74+
@bb u_ = copy(u_fixed)
5875
linprob = LinearProblem(A, b; u0 = u_, kwargs...)
5976

60-
weight = __init_ones(u)
77+
weight = __init_ones(u_fixed)
6178
if __hasfield(alg, Val(:precs))
6279
precs = alg.precs
6380
Pl_, Pr_ = precs(A, nothing, u, nothing, nothing, nothing, nothing, nothing,
@@ -97,7 +114,7 @@ function (cache::LinearSolverCache)(; A = nothing, b = nothing, linu = nothing,
97114

98115
__update_A!(cache, A, reuse_A_if_factorization)
99116
b !== nothing && (cache.lincache.b = b)
100-
linu !== nothing && (cache.lincache.u = linu)
117+
linu !== nothing && __set_lincache_u!(cache, linu)
101118

102119
Plprev = cache.lincache.Pl isa ComposePreconditioner ? cache.lincache.Pl.outer :
103120
cache.lincache.Pl

test/misc/polyalg_tests.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ end
178178
end
179179

180180
@testitem "[OOP] Infeasible" setup=[InfeasibleFunction] begin
181-
using StaticArrays
181+
using LinearAlgebra, StaticArrays
182182

183183
u0 = [0.0, 0.0, 0.0]
184184
prob = NonlinearProblem(f1_infeasible, u0)
@@ -189,8 +189,12 @@ end
189189

190190
u0 = @SVector [0.0, 0.0, 0.0]
191191
prob = NonlinearProblem(f1_infeasible, u0)
192-
sol = solve(prob)
193192

194-
@test all(!isnan, sol.u)
195-
@test !SciMLBase.successful_retcode(sol.retcode)
193+
try
194+
sol = solve(prob)
195+
@test all(!isnan, sol.u)
196+
@test !SciMLBase.successful_retcode(sol.retcode)
197+
catch err
198+
@test err isa LinearAlgebra.SingularException
199+
end
196200
end

0 commit comments

Comments
 (0)