@@ -4,7 +4,8 @@ $(TYPEDSIGNATURES)
44Generate `NonlinearSystem`, dependent variables, and parameters from an `NonlinearProblem`.
55"""
66function modelingtoolkitize (
7- prob:: NonlinearProblem ; u_names = nothing , p_names = nothing , kwargs... )
7+ prob:: Union{NonlinearProblem, NonlinearLeastSquaresProblem} ;
8+ u_names = nothing , p_names = nothing , kwargs... )
89 p = prob. p
910 has_p = ! (p isa Union{DiffEqBase. NullParameters, Nothing})
1011
@@ -37,13 +38,18 @@ function modelingtoolkitize(
3738 end
3839
3940 if DiffEqBase. isinplace (prob)
40- rhs = ArrayInterface. restructure (prob. u0, similar (vars, Num))
41+ if prob isa NonlinearLeastSquaresProblem
42+ rhs = ArrayInterface. restructure (prob. f. resid_prototype, similar (prob. f. resid_prototype, Num))
43+ else
44+ rhs = ArrayInterface. restructure (prob. u0, similar (vars, Num))
45+ end
4146 prob. f (rhs, vars, params)
47+ eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (prob. f. resid_prototype)]. .. )
4248 else
4349 rhs = prob. f (vars, params)
50+ out_def = prob. f (prob. u0, prob. p)
51+ eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (out_def)]. .. )
4452 end
45- out_def = prob. f (prob. u0, prob. p)
46- eqs = vcat ([0.0 ~ rhs[i] for i in 1 : length (out_def)]. .. )
4753
4854 sts = vec (collect (vars))
4955 _params = params
0 commit comments