Skip to content

Commit

Permalink
Merge pull request #25 from RoboticExplorationLab/bilinear
Browse files Browse the repository at this point in the history
More fixes and tweaks
  • Loading branch information
bjack205 authored Apr 9, 2022
2 parents 22020a2 + a4284ee commit 2b1ae08
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RobotDynamics"
uuid = "38ceca67-d8d3-44e8-9852-78a5596522e1"
authors = ["Brian Jackson <bjack205@gmail.com>"]
version = "0.4.5"
version = "0.4.6"

[deps]
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
Expand Down
8 changes: 6 additions & 2 deletions src/discretized_dynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ for method in (:state_dim, :control_dim, :output_dim, :errstate_dim, :statevecto
@eval $method(model::DiscretizedDynamics) = $method(model.continuous_dynamics)
end
default_diffmethod(::DiscretizedDynamics) = ForwardAD()
default_signature(model::DiscretizedDynamics) = default_signature(model.continuous_dynamics)

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

discrete_dynamics(model::ImplicitDynamicsModel, z::AbstractKnotPoint) =
integrate(integration(model), model, z)
discrete_dynamics!(model::ImplicitDynamicsModel, xn, z::AbstractKnotPoint) =
integrate!(integration(model), model, xn, z)
# discrete_dynamics!(model::ImplicitDynamicsModel, xn, z::AbstractKnotPoint) =
# integrate!(integration(model), model, xn, z)

discrete_dynamics!(model::ImplicitDynamicsModel, xn, x, u, t, dt) =
integrate!(integration(model), model, xn, x, u, t, dt)

function jacobian!(sig::FunctionSignature, ::ImplicitFunctionTheorem{D}, model::ImplicitDynamicsModel, J, xn, z) where D
jacobian!(integration(model), sig, D(), model, J, xn, z)
Expand Down
31 changes: 24 additions & 7 deletions src/integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,11 @@ function integrate(integrator::Implicit, model::ImplicitDynamicsModel,
y1 = cache.y1
z1 = z
z2 = StaticKnotPoint(z)
xn = state(z2)
ix = SVector{Nx}(1:Nx)

# Use the current state as the current guess
xn = state(z1)

diff = default_diffmethod(model.continuous_dynamics)
for iter = 1:newton_iters
# Set the guess for the next state
Expand Down Expand Up @@ -462,22 +464,35 @@ end

function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
z::AbstractKnotPoint)
integrate!(integrator, model, xn, state(z), control(z), z.t, z.dt)
end
function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
x, u, t, h)
cache = getnewtoncache(integrator)
newton_iters = cache.newton_iters
tol = cache.newton_tol

z1 = z
n,m = dims(z)
z1 = cache.z1
z2 = cache.z2
# copyto!(z1, z)
setstate!(z1, x)
setcontrol!(z1, u)
z1.t = t
z1.dt = h
copyto!(z2, z1)

n,m = dims(model)
J2 = cache.J2
J1 = cache.J1
r = cache.y2
dx = cache.y1
z2 = cache.z2
ipiv = cache.ipiv
A = cache.A
Aview = @view J2[:, 1:n]

copyto!(z2, z1)
# Use the current state as the guess
copyto!(xn, state(z1))

diff = default_diffmethod(model.continuous_dynamics)
for iter = 1:newton_iters
# Set the guess for the next state
Expand All @@ -502,7 +517,7 @@ function integrate!(integrator::Implicit, model::ImplicitDynamicsModel, xn,
# Apply the step
xn .-= dx
end
setdata!(cache.z2, getdata(z)) # copy input to cache for Jacobian check
setdata!(cache.z2, getdata(z1)) # copy input to cache for Jacobian check
end

# Use Implicit Function Theorem to calculate the dynamics Jacobians
Expand Down Expand Up @@ -569,6 +584,7 @@ mutable struct ImplicitNewtonCache
y2::Vector{Float64}
y1::Vector{Float64}
z2::StaticKnotPoint{Any,Any,Vector{Float64},Float64}
z1::KnotPoint{Any,Any,Vector{Float64},Float64}
ipiv::Vector{BlasInt}
A::Matrix{Float64}
F::LinearAlgebra.LU{Float64, Matrix{Float64}}
Expand All @@ -582,12 +598,13 @@ function ImplicitNewtonCache(n::Integer, m::Integer)
y1 = zeros(n)
v = zeros(n+m)
z2 = StaticKnotPoint{Any,Any}(n, m, v, 0.0, NaN)
z1 = KnotPoint{Any,Any}(n, m, copy(v), 0.0, NaN)
ipiv = zeros(BlasInt, n)
A = zeros(n,n)
F = lu!(A, check=false)
iters = 10 # Default number of Newton iterations
tol = 1e-12 # Default Newton tolerance
ImplicitNewtonCache(J2, J1, y2, y1, z2, ipiv, A, F, iters, tol)
ImplicitNewtonCache(J2, J1, y2, y1, z2, z1, ipiv, A, F, iters, tol)
end

"""
Expand Down
6 changes: 4 additions & 2 deletions src/knotpoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ for (name,mutable) in [(:KnotPoint, true), (:StaticKnotPoint, false)]
@inline getparams(z::$name) = (t=z.t, dt=z.dt)
@inline getdata(z::$name) = z.z
function Base.copyto!(dest::$name, src::$name)
dest.t = src.t
dest.dt = src.dt
if ismutabletype($name)
dest.t = src.t
dest.dt = src.dt
end
copyto!(dest.z, src.z)
dest
end
Expand Down
2 changes: 1 addition & 1 deletion src/trajectories.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function controls(Z::SampledTrajectory)
return [control(Z[k]) for k in eachcontrol(Z) ]
end
controls(Z::SampledTrajectory, inds::AbstractVector{<:Integer}) = [controls(Z, i) for i in inds]
controls(Z::SampledTrajectory, ind::Integer) = [control(z)[ind] for z in Z]
controls(Z::SampledTrajectory, ind::Integer) = [control(Z[k])[ind] for k in eachcontrol(Z)]

"""
gettimes(Z)
Expand Down
1 change: 1 addition & 0 deletions test/implicit_dynamics_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ x2 = newton_solve(RD.getdata(z1))
x2_inplace = copy(x1)
RD.discrete_dynamics!(dmodel, x2_inplace, z1)
@test norm(midpoint_residual(x2_inplace, z1)) < eps()
x2_inplace .= 0
RD.evaluate!(dmodel, x2_inplace, z1)
@test norm(midpoint_residual(x2_inplace, z1)) < eps()

Expand Down

2 comments on commit 2b1ae08

@bjack205
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/58242

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.6 -m "<description of version>" 2b1ae08d0574b183b45fc6accd49091ff9889ace
git push origin v0.4.6

Please sign in to comment.