@@ -23,7 +23,7 @@ struct DAEOptimizer{T}
2323 solver:: T
2424end
2525
26- DAEMassMatrix () = DAEOptimizer (Rodas5 ( ))
26+ DAEMassMatrix () = DAEOptimizer (Rosenbrock23 (autodiff = false ))
2727DAEIndexing () = DAEOptimizer (IDA ())
2828
2929
@@ -63,32 +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-
84- function get_solver_type (opt:: DAEOptimizer )
85- if opt. solver isa Union{Rodas5, RadauIIA5, ImplicitEuler, Trapezoid}
86- return :mass_matrix
87- else
88- return :indexing
89- end
90- end
91-
9266function handle_parameters (p)
9367 if p isa SciMLBase. NullParameters
9468 return Float64[]
@@ -110,45 +84,6 @@ function setup_progress_callback(cache, solve_kwargs)
11084 return solve_kwargs
11185end
11286
113- function finite_difference_jacobian (f, x; ϵ = 1e-8 )
114- n = length (x)
115- fx = f (x)
116- if fx === nothing
117- return zeros (eltype (x), 0 , n)
118- elseif isa (fx, Number)
119- J = zeros (eltype (fx), 1 , n)
120- for j in 1 : n
121- xj = copy (x)
122- xj[j] += ϵ
123- diff = f (xj)
124- if diff === nothing
125- diffval = zero (eltype (fx))
126- else
127- diffval = diff - fx
128- end
129- J[1 , j] = diffval / ϵ
130- end
131- return J
132- else
133- m = length (fx)
134- J = zeros (eltype (fx), m, n)
135- for j in 1 : n
136- xj = copy (x)
137- xj[j] += ϵ
138- fxj = f (xj)
139- if fxj === nothing
140- @inbounds for i in 1 : m
141- J[i, j] = - fx[i] / ϵ
142- end
143- else
144- @inbounds for i in 1 : m
145- J[i, j] = (fxj[i] - fx[i]) / ϵ
146- end
147- end
148- end
149- return J
150- end
151- end
15287
15388function SciMLBase. __solve (
15489 cache:: OptimizationCache{F,RC,LB,UB,LC,UC,S,O,D,P,C}
@@ -163,8 +98,7 @@ function SciMLBase.__solve(
16398 if cache. opt isa ODEOptimizer
16499 return solve_ode (cache, dt, maxit, u0, p)
165100 else
166- solver_method = get_solver_type (cache. opt)
167- if solver_method == :mass_matrix
101+ if cache. opt. solver == Rosenbrock23 (autodiff = false )
168102 return solve_dae_mass_matrix (cache, dt, maxit, u0, p)
169103 else
170104 return solve_dae_indexing (cache, dt, maxit, u0, p, differential_vars)
@@ -240,8 +174,8 @@ function solve_dae_mass_matrix(cache, dt, maxit, u0, p)
240174 n = length (u0)
241175 m = length (cons_vals)
242176 u0_extended = vcat (u0, zeros (m))
243- M = zeros ( n + m, n + m )
244- M[ 1 : n, 1 : n] = I (n)
177+ M = Diagonal ( ones ( n + m) )
178+
245179
246180 function f_mass! (du, u, p_, t)
247181 x = @view u[1 : n]
@@ -253,31 +187,18 @@ function solve_dae_mass_matrix(cache, dt, maxit, u0, p)
253187 grad_f .= ForwardDiff. gradient (z -> cache. f. f (z, p_), x)
254188 end
255189 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
190+ cache. f. cons_j != = nothing && cache. f. cons_j (J, x)
191+
261192 @. du[1 : n] = - grad_f - (J' * λ)
262193 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
194+ @. du[n+ 1 : end ] = consv
274195 return nothing
275196 end
276197
277198 if m == 0
278- optf = ODEFunction (f_mass!, mass_matrix = I (n) )
199+ optf = ODEFunction (f_mass!)
279200 prob = ODEProblem (optf, u0, (0.0 , 1.0 ), p)
280- return solve (prob, HighOrderDescent () ; dt= dt, maxiters= maxit)
201+ return solve (prob, cache . opt . solver ; dt= dt, maxiters= maxit)
281202 end
282203
283204 ss_prob = SteadyStateProblem (ODEFunction (f_mass!, mass_matrix = M), u0_extended, p)
@@ -327,11 +248,8 @@ function solve_dae_indexing(cache, dt, maxit, u0, p, differential_vars)
327248 grad_f = similar (x)
328249 cache. f. grad (grad_f, x, p_)
329250 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
251+ cache. f. cons_j != = nothing && cache. f. cons_j (J, x)
252+
335253 @. res[1 : n] = du_x + grad_f + J' * λ
336254 consv = cache. f. cons (x, p_)
337255 @. res[n+ 1 : end ] = consv
@@ -364,4 +282,4 @@ function solve_dae_indexing(cache, dt, maxit, u0, p, differential_vars)
364282end
365283
366284
367- end
285+ end
0 commit comments