Skip to content

Commit 77b8fbd

Browse files
Construct explicit second-order AD for optimization BVPs
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent 219b502 commit 77b8fbd

4 files changed

Lines changed: 29 additions & 32 deletions

File tree

lib/BoundaryValueDiffEqCore/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BoundaryValueDiffEqCore"
22
uuid = "56b672f2-a5fe-4263-ab2d-da677488eb3a"
3-
version = "2.7.4"
3+
version = "2.7.5"
44
authors = ["Qingyu Qu <erikqqy123@gmail.com>"]
55

66
[deps]
@@ -9,6 +9,7 @@ Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
99
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
1010
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
1111
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
12+
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
1213
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1314
Integrals = "de52edbc-65ea-441a-8357-d3a637375a31"
1415
LineSearch = "87fe0de2-c867-4266-b59a-2f0a94fc965b"
@@ -35,6 +36,7 @@ Adapt = "4.1.1"
3536
ArrayInterface = "7.18"
3637
ConcreteStructs = "0.2.3"
3738
DiffEqBase = "6.213, 7"
39+
DifferentiationInterface = "0.7.13"
3840
ForwardDiff = "0.10.38, 1"
3941
Integrals = "4.7.1, 5"
4042
LineSearch = "0.1.4"

lib/BoundaryValueDiffEqCore/src/BoundaryValueDiffEqCore.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ module BoundaryValueDiffEqCore
22

33
using Adapt: adapt
44
using ADTypes: ADTypes, AbstractADType, AutoSparse, AutoForwardDiff, AutoFiniteDiff,
5-
AutoPolyesterForwardDiff
5+
AutoPolyesterForwardDiff, AutoSymbolics, AutoZygote
66
using ArrayInterface: parameterless_type
77
using ConcreteStructs: @concrete
88
using DiffEqBase: DiffEqBase, solve
9+
using DifferentiationInterface: SecondOrder
910
using ForwardDiff: ForwardDiff, pickchunksize
1011
using Integrals: Integrals, IntegralProblem
1112
using LinearAlgebra: LinearAlgebra, mul!

lib/BoundaryValueDiffEqCore/src/internal_problems.jl

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
@inline __default_cost(::Nothing) = (x, p) -> 0.0
22
@inline __default_cost(f) = f
33

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+
417
"""
518
__build_cost(fun, cache, mesh, M; tune_parameters = false, p = nothing)
619
@@ -119,10 +132,7 @@ function __construct_internal_problem(
119132
else
120133
optf = OptimizationFunction{true}(
121134
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),
126136
cons = loss,
127137
cons_j = jac,
128138
cons_jac_prototype = sparse(jac_prototype)
@@ -151,10 +161,7 @@ function __construct_internal_problem(
151161
else
152162
optf = OptimizationFunction{true}(
153163
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),
158165
cons = loss,
159166
cons_j = jac,
160167
cons_jac_prototype = sparse(jac_prototype)
@@ -184,10 +191,7 @@ function __construct_internal_problem(
184191
else
185192
optf = OptimizationFunction{iip}(
186193
__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),
191195
cons = loss,
192196
cons_j = jac,
193197
cons_jac_prototype = sparse(jac_prototype)
@@ -227,10 +231,7 @@ function __construct_internal_problem(
227231
else
228232
optf = OptimizationFunction{true}(
229233
__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),
234235
cons = loss,
235236
cons_j = jac,
236237
cons_jac_prototype = sparse(jac_prototype)
@@ -258,10 +259,7 @@ function __construct_internal_problem(
258259
else
259260
optf = OptimizationFunction{true}(
260261
__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),
265263
cons = loss,
266264
cons_j = jac,
267265
cons_jac_prototype = sparse(jac_prototype)
@@ -291,10 +289,7 @@ function __construct_internal_problem(
291289
else
292290
optf = OptimizationFunction{iip}(
293291
__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),
298293
cons = loss,
299294
cons_j = jac,
300295
cons_jac_prototype = sparse(jac_prototype)
@@ -322,10 +317,7 @@ function __construct_internal_problem(
322317
else
323318
optf = OptimizationFunction{true}(
324319
__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),
329321
cons = loss,
330322
cons_j = jac,
331323
cons_jac_prototype = sparse(jac_prototype)

lib/BoundaryValueDiffEqMIRK/test/Core/mirk_basic_tests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ end
643643
simplependulum!, bc!, [pi / 2, pi / 2], tspan,
644644
lcons = [-10.0, -10.0], ucons = [10.0, 10.0]
645645
)
646-
@test_nowarn sol = solve(prob, MIRK4(; optimize = IpoptOptimizer()), dt = 0.05)
646+
sol = @test_nowarn solve(prob, MIRK4(; optimize = IpoptOptimizer()), dt = 0.05)
647+
@test SciMLBase.successful_retcode(sol)
647648
end
648649

649650
# https://github.com/SciML/BoundaryValueDiffEq.jl/pull/473
@@ -668,7 +669,8 @@ end
668669
prob = BVProblem(
669670
simplependulum!, bc!, [pi / 2, pi / 2], tspan
670671
)
671-
@test_nowarn solve(prob, MIRK4(; optimize = IpoptOptimizer()), dt = 0.05)
672+
sol = @test_nowarn solve(prob, MIRK4(; optimize = IpoptOptimizer()), dt = 0.05)
673+
@test SciMLBase.successful_retcode(sol)
672674
end
673675

674676
@testset "Test initial guess" begin

0 commit comments

Comments
 (0)