Skip to content

Commit 57564bf

Browse files
Add hessian_sparsity method for NonlinearSystem (#1602)
1 parent 7163f97 commit 57564bf

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ DocStringExtensions = "0.7, 0.8, 0.9"
5858
DomainSets = "0.5"
5959
Graphs = "1.5.2"
6060
IfElse = "0.1"
61-
JuliaFormatter = "0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 1"
61+
JuliaFormatter = "1"
6262
LabelledArrays = "1.3"
6363
Latexify = "0.11, 0.12, 0.13, 0.14, 0.15"
6464
MacroTools = "0.5"

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ function jacobian_sparsity(sys::NonlinearSystem)
172172
states(sys))
173173
end
174174

175+
function hessian_sparsity(sys::NonlinearSystem)
176+
[hessian_sparsity(eq.rhs,
177+
states(sys)) for eq in equations(sys)]
178+
end
179+
175180
function DiffEqBase.NonlinearFunction(sys::NonlinearSystem, args...; kwargs...)
176181
NonlinearFunction{true}(sys, args...; kwargs...)
177182
end

src/systems/optimization/optimizationsystem.jl

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,10 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
184184
_hess = nothing
185185
end
186186

187-
_f = DiffEqBase.OptimizationFunction{iip, SciMLBase.NoAD, typeof(f), typeof(_grad),
188-
typeof(_hess), Nothing, Nothing, Nothing, Nothing}(f,
189-
SciMLBase.NoAD(),
190-
_grad,
191-
_hess,
192-
nothing,
193-
nothing,
194-
nothing,
195-
nothing)
187+
_f = DiffEqBase.OptimizationFunction{iip}(f,
188+
SciMLBase.NoAD();
189+
grad = _grad,
190+
hess = _hess)
196191

197192
defs = defaults(sys)
198193
defs = mergedefaults(defs, parammap, ps)
@@ -272,13 +267,9 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
272267
hess = $_hess
273268
lb = $lb
274269
ub = $ub
275-
_f = OptimizationFunction{$iip, typeof(f), typeof(grad), typeof(hess),
276-
SciMLBase.NoAD, Nothing, Nothing, Nothing}(f, grad, hess,
277-
nothing,
278-
SciMLBase.NoAD(),
279-
nothing,
280-
nothing,
281-
nothing, 0)
270+
_f = OptimizationFunction{iip}(f, SciMLBase.NoAD();
271+
grad = grad,
272+
hess = hess)
282273
OptimizationProblem{$iip}(_f, u0, p; lb = lb, ub = ub, kwargs...)
283274
end
284275
end

test/nonlinearsystem.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ jac = calculate_jacobian(ns)
7272

7373
jac = generate_jacobian(ns)
7474

75+
sH = calculate_hessian(ns)
76+
@test getfield.(ModelingToolkit.hessian_sparsity(ns), :colptr) ==
77+
getfield.(sparse.(sH), :colptr)
78+
@test getfield.(ModelingToolkit.hessian_sparsity(ns), :rowval) ==
79+
getfield.(sparse.(sH), :rowval)
80+
7581
prob = NonlinearProblem(ns, ones(3), ones(3))
7682
sol = solve(prob, NewtonRaphson())
7783
@test sol.u[1] sol.u[2]

0 commit comments

Comments
 (0)