Skip to content

Commit 960d8e4

Browse files
committed
Use the Jacobian cache in nonlinear systems
1 parent c232484 commit 960d8e4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,20 @@ function NonlinearSystem(eqs, states, ps;
7676
NonlinearSystem(eqs, value.(states), value.(ps), observed, jac, name, systems, defaults, nothing, connection_type)
7777
end
7878

79-
function calculate_jacobian(sys::NonlinearSystem;sparse=false,simplify=false)
79+
function calculate_jacobian(sys::NonlinearSystem; sparse=false, simplify=false)
80+
cache = get_jac(sys)[]
81+
if cache isa Tuple && cache[2] == (sparse, simplify)
82+
return cache[1]
83+
end
84+
8085
rhs = [eq.rhs for eq equations(sys)]
8186
vals = [dv for dv in states(sys)]
8287
if sparse
8388
jac = sparsejacobian(rhs, vals, simplify=simplify)
8489
else
8590
jac = jacobian(rhs, vals, simplify=simplify)
8691
end
87-
get_jac(sys)[] = jac
92+
get_jac(sys)[] = jac, (sparse, simplify)
8893
return jac
8994
end
9095

0 commit comments

Comments
 (0)