@@ -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
463465function 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
506521end
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)
591608end
592609
593610"""
0 commit comments