@@ -1019,28 +1019,35 @@ function linearization_function(sys::AbstractSystem, inputs,
1019
1019
kwargs... )
1020
1020
sys, diff_idxs, alge_idxs, input_idxs = io_preprocessing (sys, inputs, outputs; simplify,
1021
1021
kwargs... )
1022
- sts = states (sys)
1023
- fun = ODEFunction (sys)
1024
- lin_fun = let fun = fun,
1025
- h = ModelingToolkit. build_explicit_observed_function (sys, outputs)
1022
+ lin_fun = let diff_idxs = diff_idxs,
1023
+ alge_idxs = alge_idxs,
1024
+ input_idxs = input_idxs,
1025
+ sts = states (sys),
1026
+ fun = ODEFunction (sys),
1027
+ h = ModelingToolkit. build_explicit_observed_function (sys, outputs),
1028
+ chunk = ForwardDiff. Chunk (input_idxs)
1026
1029
1027
1030
function (u, p, t)
1028
1031
if u != = nothing # Handle systems without states
1029
1032
length (sts) == length (u) ||
1030
1033
error (" Number of state variables ($(length (sts)) ) does not match the number of input states ($(length (u)) )" )
1031
1034
uf = SciMLBase. UJacobianWrapper (fun, t, p)
1032
1035
fg_xz = ForwardDiff. jacobian (uf, u)
1033
- h_xz = ForwardDiff. jacobian (xz -> h (xz, p, t), u)
1036
+ h_xz = ForwardDiff. jacobian (let p = p, t = t
1037
+ xz -> h (xz, p, t)
1038
+ end , u)
1034
1039
pf = SciMLBase. ParamJacobianWrapper (fun, t, u)
1035
- # TODO : this is very inefficient, p contains all parameters of the system
1036
- fg_u = ForwardDiff. jacobian (pf, p)[:, input_idxs]
1040
+ fg_u = jacobian_wrt_vars (pf, p, input_idxs, chunk)
1037
1041
else
1038
1042
length (sts) == 0 ||
1039
1043
error (" Number of state variables (0) does not match the number of input states ($(length (u)) )" )
1040
1044
fg_xz = zeros (0 , 0 )
1041
1045
h_xz = fg_u = zeros (0 , length (inputs))
1042
1046
end
1043
- h_u = ForwardDiff. jacobian (p -> h (u, p, t), p)[:, input_idxs]
1047
+ hp = let u = u, t = t
1048
+ p -> h (u, p, t)
1049
+ end
1050
+ h_u = jacobian_wrt_vars (hp, p, input_idxs, chunk)
1044
1051
(f_x = fg_xz[diff_idxs, diff_idxs],
1045
1052
f_z = fg_xz[diff_idxs, alge_idxs],
1046
1053
g_x = fg_xz[alge_idxs, diff_idxs],
0 commit comments