@@ -63,24 +63,6 @@ function SciMLBase.__init(prob::OptimizationProblem, opt::DAEOptimizer;
6363end
6464
6565
66- function solve_constrained_root (cache, u0, p)
67- n = length (u0)
68- cons_vals = cache. f. cons (u0, p)
69- m = length (cons_vals)
70- function resid! (res, u)
71- temp = similar (u)
72- f_mass! (temp, u, p, 0.0 )
73- res .= temp
74- end
75- u0_ext = vcat (u0, zeros (m))
76- prob_nl = NonlinearProblem (resid!, u0_ext, p)
77- sol_nl = solve (prob_nl, Newton (); tol = 1e-8 , maxiters = 100000 ,
78- callback = cache. callback, progress = get (cache. solver_args, :progress , false ))
79- u_ext = sol_nl. u
80- return u_ext[1 : n], sol_nl. retcode
81- end
82-
83-
8466function get_solver_type (opt:: DAEOptimizer )
8567 if opt. solver isa Union{Rodas5, RadauIIA5, ImplicitEuler, Trapezoid}
8668 return :mass_matrix
@@ -89,6 +71,7 @@ function get_solver_type(opt::DAEOptimizer)
8971 end
9072end
9173
74+
9275function handle_parameters (p)
9376 if p isa SciMLBase. NullParameters
9477 return Float64[]
@@ -240,8 +223,8 @@ function solve_dae_mass_matrix(cache, dt, maxit, u0, p)
240223 n = length (u0)
241224 m = length (cons_vals)
242225 u0_extended = vcat (u0, zeros (m))
243- M = zeros ( n + m, n + m )
244- M[ 1 : n, 1 : n] = I (n)
226+ M = Diagonal ( ones ( n + m) )
227+
245228
246229 function f_mass! (du, u, p_, t)
247230 x = @view u[1 : n]
@@ -253,24 +236,11 @@ function solve_dae_mass_matrix(cache, dt, maxit, u0, p)
253236 grad_f .= ForwardDiff. gradient (z -> cache. f. f (z, p_), x)
254237 end
255238 J = Matrix {eltype(x)} (undef, m, n)
256- if cache. f. cons_j != = nothing
257- cache. f. cons_j (J, x)
258- else
259- J .= finite_difference_jacobian (z -> cache. f. cons (z, p_), x)
260- end
239+ cache. f. cons_j != = nothing && cache. f. cons_j (J, x)
240+
261241 @. du[1 : n] = - grad_f - (J' * λ)
262242 consv = cache. f. cons (x, p_)
263- if consv === nothing
264- fill! (du[n+ 1 : end ], zero (eltype (x)))
265- else
266- if isa (consv, Number)
267- @assert m == 1
268- du[n+ 1 ] = consv
269- else
270- @assert length (consv) == m
271- @. du[n+ 1 : end ] = consv
272- end
273- end
243+ @. du[n+ 1 : end ] = consv
274244 return nothing
275245 end
276246
@@ -327,11 +297,8 @@ function solve_dae_indexing(cache, dt, maxit, u0, p, differential_vars)
327297 grad_f = similar (x)
328298 cache. f. grad (grad_f, x, p_)
329299 J = zeros (m, n)
330- if cache. f. cons_j != = nothing
331- cache. f. cons_j (J, x)
332- else
333- J .= finite_difference_jacobian (z -> cache. f. cons (z,p_), x)
334- end
300+ cache. f. cons_j != = nothing && cache. f. cons_j (J, x)
301+
335302 @. res[1 : n] = du_x + grad_f + J' * λ
336303 consv = cache. f. cons (x, p_)
337304 @. res[n+ 1 : end ] = consv
@@ -364,4 +331,4 @@ function solve_dae_indexing(cache, dt, maxit, u0, p, differential_vars)
364331end
365332
366333
367- end
334+ end
0 commit comments