|
1 | 1 | @inline __default_cost(::Nothing) = (x, p) -> 0.0 |
2 | 2 | @inline __default_cost(f) = f |
3 | 3 |
|
| 4 | +@inline __optimization_second_order_ad(ad) = SecondOrder(ad, ad) |
| 5 | +@inline __optimization_second_order_ad(ad::SecondOrder) = ad |
| 6 | +@inline __optimization_second_order_ad(ad::AutoZygote) = SecondOrder(AutoForwardDiff(), ad) |
| 7 | +@inline __optimization_second_order_ad(ad::AutoSymbolics) = ad |
| 8 | +@inline __optimization_second_order_ad(ad::SciMLBase.NoAD) = ad |
| 9 | + |
| 10 | +@inline function __optimization_ad(diffmode, detector_diffmode = diffmode) |
| 11 | + return AutoSparse( |
| 12 | + __optimization_second_order_ad(get_dense_ad(diffmode)), |
| 13 | + sparsity_detector = __default_sparsity_detector(detector_diffmode) |
| 14 | + ) |
| 15 | +end |
| 16 | + |
4 | 17 | """ |
5 | 18 | __build_cost(fun, cache, mesh, M; tune_parameters = false, p = nothing) |
6 | 19 |
|
@@ -119,10 +132,7 @@ function __construct_internal_problem( |
119 | 132 | else |
120 | 133 | optf = OptimizationFunction{true}( |
121 | 134 | cost_fun, |
122 | | - AutoSparse( |
123 | | - get_dense_ad(alg.jac_alg.nonbc_diffmode), |
124 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.diffmode) |
125 | | - ), |
| 135 | + __optimization_ad(alg.jac_alg.nonbc_diffmode, alg.jac_alg.diffmode), |
126 | 136 | cons = loss, |
127 | 137 | cons_j = jac, |
128 | 138 | cons_jac_prototype = sparse(jac_prototype) |
@@ -151,10 +161,7 @@ function __construct_internal_problem( |
151 | 161 | else |
152 | 162 | optf = OptimizationFunction{true}( |
153 | 163 | cost_fun, |
154 | | - AutoSparse( |
155 | | - get_dense_ad(alg.jac_alg.diffmode), |
156 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.diffmode) |
157 | | - ), |
| 164 | + __optimization_ad(alg.jac_alg.diffmode), |
158 | 165 | cons = loss, |
159 | 166 | cons_j = jac, |
160 | 167 | cons_jac_prototype = sparse(jac_prototype) |
@@ -184,10 +191,7 @@ function __construct_internal_problem( |
184 | 191 | else |
185 | 192 | optf = OptimizationFunction{iip}( |
186 | 193 | __default_cost(prob.f.cost), |
187 | | - AutoSparse( |
188 | | - get_dense_ad(alg.jac_alg.diffmode), |
189 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.diffmode) |
190 | | - ), |
| 194 | + __optimization_ad(alg.jac_alg.diffmode), |
191 | 195 | cons = loss, |
192 | 196 | cons_j = jac, |
193 | 197 | cons_jac_prototype = sparse(jac_prototype) |
@@ -227,10 +231,7 @@ function __construct_internal_problem( |
227 | 231 | else |
228 | 232 | optf = OptimizationFunction{true}( |
229 | 233 | __default_cost(prob.f.cost), |
230 | | - AutoSparse( |
231 | | - get_dense_ad(alg.jac_alg.nonbc_diffmode), |
232 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.nonbc_diffmode) |
233 | | - ), |
| 234 | + __optimization_ad(alg.jac_alg.nonbc_diffmode), |
234 | 235 | cons = loss, |
235 | 236 | cons_j = jac, |
236 | 237 | cons_jac_prototype = sparse(jac_prototype) |
@@ -258,10 +259,7 @@ function __construct_internal_problem( |
258 | 259 | else |
259 | 260 | optf = OptimizationFunction{true}( |
260 | 261 | __default_cost(prob.f.cost), |
261 | | - AutoSparse( |
262 | | - get_dense_ad(alg.jac_alg.diffmode), |
263 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.nonbc_diffmode) |
264 | | - ), |
| 262 | + __optimization_ad(alg.jac_alg.diffmode, alg.jac_alg.nonbc_diffmode), |
265 | 263 | cons = loss, |
266 | 264 | cons_j = jac, |
267 | 265 | cons_jac_prototype = sparse(jac_prototype) |
@@ -291,10 +289,7 @@ function __construct_internal_problem( |
291 | 289 | else |
292 | 290 | optf = OptimizationFunction{iip}( |
293 | 291 | __default_cost(prob.f.cost), |
294 | | - AutoSparse( |
295 | | - get_dense_ad(alg.jac_alg.nonbc_diffmode), |
296 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.nonbc_diffmode) |
297 | | - ), |
| 292 | + __optimization_ad(alg.jac_alg.nonbc_diffmode), |
298 | 293 | cons = loss, |
299 | 294 | cons_j = jac, |
300 | 295 | cons_jac_prototype = sparse(jac_prototype) |
@@ -322,10 +317,7 @@ function __construct_internal_problem( |
322 | 317 | else |
323 | 318 | optf = OptimizationFunction{true}( |
324 | 319 | __default_cost(prob.f), |
325 | | - AutoSparse( |
326 | | - get_dense_ad(alg.jac_alg.diffmode), |
327 | | - sparsity_detector = __default_sparsity_detector(alg.jac_alg.nonbc_diffmode) |
328 | | - ), |
| 320 | + __optimization_ad(alg.jac_alg.diffmode, alg.jac_alg.nonbc_diffmode), |
329 | 321 | cons = loss, |
330 | 322 | cons_j = jac, |
331 | 323 | cons_jac_prototype = sparse(jac_prototype) |
|
0 commit comments