|
36 | 36 | return cost_fun |
37 | 37 | end |
38 | 38 |
|
39 | | -@inline function __extract_lcons_ucons( |
40 | | - prob::AbstractBVProblem, ::Type{T}, M, N, bcresid_prototype, f_prototype |
41 | | - ) where {T} |
42 | | - L_f_prototype = length(f_prototype) |
43 | | - L_bcresid_prototype = length(bcresid_prototype) |
44 | | - lcons = if isnothing(prob.lcons) |
45 | | - zeros(T, L_bcresid_prototype + (N - 1) * L_f_prototype) |
46 | | - else |
47 | | - lcons_length = length(prob.lcons) |
48 | | - vcat(prob.lcons, zeros(T, N * M - lcons_length)) |
49 | | - end |
50 | | - ucons = if isnothing(prob.ucons) |
51 | | - zeros(T, L_bcresid_prototype + (N - 1) * L_f_prototype) |
52 | | - else |
53 | | - ucons_length = length(prob.ucons) |
54 | | - vcat(prob.ucons, zeros(T, N * M - ucons_length)) |
55 | | - end |
56 | | - return lcons, ucons |
57 | | -end |
| 39 | +""" |
| 40 | + __extract_lcons_ucons(prob, T, constraint_length) |
58 | 41 |
|
| 42 | +Build the `lcons` / `ucons` vectors for the equality-constrained optimization |
| 43 | +problem constructed from a BVP. Length must match the actual constraint vector |
| 44 | +(`resid_prototype`) passed to the solver — not a reconstruction from |
| 45 | +`(M, N, bcresid_prototype, f_prototype)`, which has a different formula per |
| 46 | +discretization (MIRK: `L_bc + M*(N-1)`; FIRK Expanded: `L_bc + M*(N-1)*(stage+1)`; |
| 47 | +MIRKN: `L_bc + M*2*(N-1)`; shooting: `L_bc`). Callers pass `length(resid_prototype)` |
| 48 | +directly. |
| 49 | +""" |
59 | 50 | @inline function __extract_lcons_ucons( |
60 | | - prob::AbstractBVProblem, ::Type{T}, M, N, bcresid_prototype, ::Nothing |
| 51 | + prob::AbstractBVProblem, ::Type{T}, constraint_length::Int |
61 | 52 | ) where {T} |
62 | | - lcons = zeros(T, N * M) |
63 | | - ucons = zeros(T, N * M) |
64 | | - return lcons, ucons |
65 | | -end |
66 | | - |
67 | | -@inline function __extract_lcons_ucons(prob::AbstractBVProblem, ::Type{T}, M, N) where {T} |
68 | 53 | lcons = if isnothing(prob.lcons) |
69 | | - zeros(T, N * M) |
| 54 | + zeros(T, constraint_length) |
70 | 55 | else |
71 | 56 | lcons_length = length(prob.lcons) |
72 | | - vcat(prob.lcons, zeros(T, N * M - lcons_length)) |
| 57 | + vcat(prob.lcons, zeros(T, constraint_length - lcons_length)) |
73 | 58 | end |
74 | 59 | ucons = if isnothing(prob.ucons) |
75 | | - zeros(T, N * M) |
| 60 | + zeros(T, constraint_length) |
76 | 61 | else |
77 | 62 | ucons_length = length(prob.ucons) |
78 | | - vcat(prob.ucons, zeros(T, N * M - ucons_length)) |
| 63 | + vcat(prob.ucons, zeros(T, constraint_length - ucons_length)) |
79 | 64 | end |
80 | 65 | return lcons, ucons |
81 | 66 | end |
@@ -129,7 +114,7 @@ function __construct_internal_problem( |
129 | 114 | cons_j = jac, |
130 | 115 | cons_jac_prototype = sparse(jac_prototype) |
131 | 116 | ) |
132 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N, bcresid_prototype, f_prototype) |
| 117 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
133 | 118 | lb, ub = __extract_lb_ub(prob, T, M, N) |
134 | 119 |
|
135 | 120 | return __internal_optimization_problem( |
@@ -161,7 +146,7 @@ function __construct_internal_problem( |
161 | 146 | cons_j = jac, |
162 | 147 | cons_jac_prototype = sparse(jac_prototype) |
163 | 148 | ) |
164 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N, bcresid_prototype, f_prototype) |
| 149 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
165 | 150 | lb, ub = __extract_lb_ub(prob, T, M, N) |
166 | 151 |
|
167 | 152 | return __internal_optimization_problem( |
@@ -194,7 +179,7 @@ function __construct_internal_problem( |
194 | 179 | cons_j = jac, |
195 | 180 | cons_jac_prototype = sparse(jac_prototype) |
196 | 181 | ) |
197 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N) |
| 182 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
198 | 183 | lb, ub = __extract_lb_ub(prob, T, M, N) |
199 | 184 |
|
200 | 185 | return __internal_optimization_problem( |
@@ -237,7 +222,7 @@ function __construct_internal_problem( |
237 | 222 | cons_j = jac, |
238 | 223 | cons_jac_prototype = sparse(jac_prototype) |
239 | 224 | ) |
240 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N, bcresid_prototype, f_prototype) |
| 225 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
241 | 226 | lb, ub = __extract_lb_ub(prob, T, M, N) |
242 | 227 |
|
243 | 228 | return __internal_optimization_problem( |
@@ -268,7 +253,7 @@ function __construct_internal_problem( |
268 | 253 | cons_j = jac, |
269 | 254 | cons_jac_prototype = sparse(jac_prototype) |
270 | 255 | ) |
271 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N, bcresid_prototype, f_prototype) |
| 256 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
272 | 257 | lb, ub = __extract_lb_ub(prob, T, M, N) |
273 | 258 |
|
274 | 259 | return __internal_optimization_problem( |
@@ -301,7 +286,7 @@ function __construct_internal_problem( |
301 | 286 | cons_j = jac, |
302 | 287 | cons_jac_prototype = sparse(jac_prototype) |
303 | 288 | ) |
304 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N) |
| 289 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
305 | 290 | lb, ub = __extract_lb_ub(prob, T, M, N) |
306 | 291 |
|
307 | 292 | return __internal_optimization_problem( |
@@ -332,7 +317,7 @@ function __construct_internal_problem( |
332 | 317 | cons_j = jac, |
333 | 318 | cons_jac_prototype = sparse(jac_prototype) |
334 | 319 | ) |
335 | | - lcons, ucons = __extract_lcons_ucons(prob, T, M, N) |
| 320 | + lcons, ucons = __extract_lcons_ucons(prob, T, length(resid_prototype)) |
336 | 321 | lb, ub = __extract_lb_ub(prob, T, M, N) |
337 | 322 |
|
338 | 323 | return __internal_optimization_problem( |
|
0 commit comments