Skip to content

Commit 2b1ae08

Browse files
authored
Merge pull request #25 from RoboticExplorationLab/bilinear
More fixes and tweaks
2 parents 22020a2 + a4284ee commit 2b1ae08

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RobotDynamics"
22
uuid = "38ceca67-d8d3-44e8-9852-78a5596522e1"
33
authors = ["Brian Jackson <[email protected]>"]
4-
version = "0.4.5"
4+
version = "0.4.6"
55

66
[deps]
77
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"

src/discretized_dynamics.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ for method in (:state_dim, :control_dim, :output_dim, :errstate_dim, :statevecto
188188
@eval $method(model::DiscretizedDynamics) = $method(model.continuous_dynamics)
189189
end
190190
default_diffmethod(::DiscretizedDynamics) = ForwardAD()
191+
default_signature(model::DiscretizedDynamics) = default_signature(model.continuous_dynamics)
191192

192193
# Shallow copy
193194
function Base.copy(model::DiscretizedDynamics)
@@ -229,8 +230,11 @@ const ImplicitDynamicsModel{L,Q} = DiscretizedDynamics{L,Q} where {L,Q<:Implicit
229230

230231
discrete_dynamics(model::ImplicitDynamicsModel, z::AbstractKnotPoint) =
231232
integrate(integration(model), model, z)
232-
discrete_dynamics!(model::ImplicitDynamicsModel, xn, z::AbstractKnotPoint) =
233-
integrate!(integration(model), model, xn, z)
233+
# discrete_dynamics!(model::ImplicitDynamicsModel, xn, z::AbstractKnotPoint) =
234+
# integrate!(integration(model), model, xn, z)
235+
236+
discrete_dynamics!(model::ImplicitDynamicsModel, xn, x, u, t, dt) =
237+
integrate!(integration(model), model, xn, x, u, t, dt)
234238

235239
function jacobian!(sig::FunctionSignature, ::ImplicitFunctionTheorem{D}, model::ImplicitDynamicsModel, J, xn, z) where D
236240
jacobian!(integration(model), sig, D(), model, J, xn, z)

src/integration.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,11 @@ function integrate(integrator::Implicit, model::ImplicitDynamicsModel,
431431
y1 = cache.y1
432432
z1 = z
433433
z2 = StaticKnotPoint(z)
434-
xn = state(z2)
435434
ix = SVector{Nx}(1:Nx)
436435

436+
# Use the current state as the current guess
437+
xn = state(z1)
438+
437439
diff = default_diffmethod(model.continuous_dynamics)
438440
for iter = 1:newton_iters
439441
# Set the guess for the next state
@@ -462,22 +464,35 @@ end
462464

463465
function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
464466
z::AbstractKnotPoint)
467+
integrate!(integrator, model, xn, state(z), control(z), z.t, z.dt)
468+
end
469+
function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
470+
x, u, t, h)
465471
cache = getnewtoncache(integrator)
466472
newton_iters = cache.newton_iters
467473
tol = cache.newton_tol
468474

469-
z1 = z
470-
n,m = dims(z)
475+
z1 = cache.z1
476+
z2 = cache.z2
477+
# copyto!(z1, z)
478+
setstate!(z1, x)
479+
setcontrol!(z1, u)
480+
z1.t = t
481+
z1.dt = h
482+
copyto!(z2, z1)
483+
484+
n,m = dims(model)
471485
J2 = cache.J2
472486
J1 = cache.J1
473487
r = cache.y2
474488
dx = cache.y1
475-
z2 = cache.z2
476489
ipiv = cache.ipiv
477490
A = cache.A
478491
Aview = @view J2[:, 1:n]
479492

480-
copyto!(z2, z1)
493+
# Use the current state as the guess
494+
copyto!(xn, state(z1))
495+
481496
diff = default_diffmethod(model.continuous_dynamics)
482497
for iter = 1:newton_iters
483498
# Set the guess for the next state
@@ -502,7 +517,7 @@ function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
502517
# Apply the step
503518
xn .-= dx
504519
end
505-
setdata!(cache.z2, getdata(z)) # copy input to cache for Jacobian check
520+
setdata!(cache.z2, getdata(z1)) # copy input to cache for Jacobian check
506521
end
507522

508523
# Use Implicit Function Theorem to calculate the dynamics Jacobians
@@ -569,6 +584,7 @@ mutable struct ImplicitNewtonCache
569584
y2::Vector{Float64}
570585
y1::Vector{Float64}
571586
z2::StaticKnotPoint{Any,Any,Vector{Float64},Float64}
587+
z1::KnotPoint{Any,Any,Vector{Float64},Float64}
572588
ipiv::Vector{BlasInt}
573589
A::Matrix{Float64}
574590
F::LinearAlgebra.LU{Float64, Matrix{Float64}}
@@ -582,12 +598,13 @@ function ImplicitNewtonCache(n::Integer, m::Integer)
582598
y1 = zeros(n)
583599
v = zeros(n+m)
584600
z2 = StaticKnotPoint{Any,Any}(n, m, v, 0.0, NaN)
601+
z1 = KnotPoint{Any,Any}(n, m, copy(v), 0.0, NaN)
585602
ipiv = zeros(BlasInt, n)
586603
A = zeros(n,n)
587604
F = lu!(A, check=false)
588605
iters = 10 # Default number of Newton iterations
589606
tol = 1e-12 # Default Newton tolerance
590-
ImplicitNewtonCache(J2, J1, y2, y1, z2, ipiv, A, F, iters, tol)
607+
ImplicitNewtonCache(J2, J1, y2, y1, z2, z1, ipiv, A, F, iters, tol)
591608
end
592609

593610
"""

src/knotpoint.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ for (name,mutable) in [(:KnotPoint, true), (:StaticKnotPoint, false)]
195195
@inline getparams(z::$name) = (t=z.t, dt=z.dt)
196196
@inline getdata(z::$name) = z.z
197197
function Base.copyto!(dest::$name, src::$name)
198-
dest.t = src.t
199-
dest.dt = src.dt
198+
if ismutabletype($name)
199+
dest.t = src.t
200+
dest.dt = src.dt
201+
end
200202
copyto!(dest.z, src.z)
201203
dest
202204
end

src/trajectories.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function controls(Z::SampledTrajectory)
185185
return [control(Z[k]) for k in eachcontrol(Z) ]
186186
end
187187
controls(Z::SampledTrajectory, inds::AbstractVector{<:Integer}) = [controls(Z, i) for i in inds]
188-
controls(Z::SampledTrajectory, ind::Integer) = [control(z)[ind] for z in Z]
188+
controls(Z::SampledTrajectory, ind::Integer) = [control(Z[k])[ind] for k in eachcontrol(Z)]
189189

190190
"""
191191
gettimes(Z)

test/implicit_dynamics_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ x2 = newton_solve(RD.getdata(z1))
7272
x2_inplace = copy(x1)
7373
RD.discrete_dynamics!(dmodel, x2_inplace, z1)
7474
@test norm(midpoint_residual(x2_inplace, z1)) < eps()
75+
x2_inplace .= 0
7576
RD.evaluate!(dmodel, x2_inplace, z1)
7677
@test norm(midpoint_residual(x2_inplace, z1)) < eps()
7778

0 commit comments

Comments
 (0)