Skip to content

Commit b063e6b

Browse files
refactor: separate out resid_prototype calculation
1 parent 4d4ff85 commit b063e6b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ function hessian_sparsity(sys::NonlinearSystem)
283283
unknowns(sys)) for eq in equations(sys)]
284284
end
285285

286+
function calculate_resid_prototype(N, u0, p)
287+
u0ElType = u0 === nothing ? Float64 : eltype(u0)
288+
if SciMLStructures.isscimlstructure(p)
289+
u0ElType = promote_type(
290+
eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]),
291+
u0ElType)
292+
end
293+
return zeros(u0ElType, N)
294+
end
295+
286296
"""
287297
```julia
288298
SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys),
@@ -337,13 +347,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s
337347
if length(dvs) == length(equations(sys))
338348
resid_prototype = nothing
339349
else
340-
u0ElType = u0 === nothing ? Float64 : eltype(u0)
341-
if SciMLStructures.isscimlstructure(p)
342-
u0ElType = promote_type(
343-
eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]),
344-
u0ElType)
345-
end
346-
resid_prototype = zeros(u0ElType, length(equations(sys)))
350+
resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p)
347351
end
348352

349353
NonlinearFunction{iip}(f,

0 commit comments

Comments
 (0)