@@ -113,6 +113,7 @@ function TangentDynamicalSystem(ds::CoreDynamicalSystem{IIP};
113113 u0_correct = correct_state (Val {IIP} (), u0)
114114 Q0_correct = correct_matrix_type (Val {IIP} (), Q0)
115115 newstate = hcat (u0_correct, Q0_correct)
116+ J = isnothing (J) ? jacobian (ds) : J
116117 newrule = tangent_rule (f, J, J0, Val {IIP} (), Val {k} (), u0_correct)
117118
118119 # Pass everything to analytic system constructors
@@ -137,7 +138,6 @@ correct_matrix_type(::Val{true}, Q::AbstractMatrix) = ismutable(Q) ? Q : Array(Q
137138# ##########################################################################################
138139# Creation of tangent rule
139140# ##########################################################################################
140- import ForwardDiff
141141# IIP Tangent space dynamics
142142function tangent_rule (f:: F , J:: JAC , J0, :: Val{true} , :: Val{k} , u0) where {F, JAC, k}
143143 tangentf = (du, u, p, t) -> begin
@@ -149,37 +149,12 @@ function tangent_rule(f::F, J::JAC, J0, ::Val{true}, ::Val{k}, u0) where {F, JAC
149149 end
150150 return tangentf
151151end
152- # for the case of autodiffed systems, a specialized version is created
153- # so that f! is not called twice in ForwardDiff
154- function tangent_rule (f:: F , :: Nothing , J0, :: Val{true} , :: Val{k} , u0) where {F, k}
155- let
156- cfg = ForwardDiff. JacobianConfig (
157- (du, u) -> f (du, u, p, p), deepcopy (u0), deepcopy (u0)
158- )
159- tangentf = (du, u, p, t) -> begin
160- uv = @view u[:, 1 ]
161- ForwardDiff. jacobian! (
162- J0, (du, u) -> f (du, u, p, t), view (du, :, 1 ), uv, cfg, Val {false} ()
163- )
164- mul! ((@view du[:, 2 : k+ 1 ]), J0, (@view u[:, 2 : k+ 1 ]))
165- nothing
166- end
167- return tangentf
168- end
169- end
170152
171153# OOP Tangent space dynamics
172154function tangent_rule (f:: F , J:: JAC , J0, :: Val{false} , :: Val{k} , u0) where {F, JAC, k}
173- # out of place
174- if JAC == Nothing
175- # There is no config needed here
176- Jf = (u, p, t) -> ForwardDiff. jacobian ((x) -> f (x, p, t), u)
177- else
178- Jf = J
179- end
180155 # Initial matrix `J0` is ignored
181156 ws_index = SVector {k, Int} (2 : (k+ 1 ). .. )
182- tangentf = TangentOOP (f, Jf , ws_index)
157+ tangentf = TangentOOP (f, J , ws_index)
183158 return tangentf
184159end
185160struct TangentOOP{F, JAC, k} <: Function
0 commit comments