Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Nov 8, 2024
1 parent 017bf8a commit 7cc796c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/system/controlsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,31 @@ struct RungeKuttaDiscretization <: MathematicalSystems.AbstractDiscretizationAlg
num_sub_steps::Int
end

# Similar to `MathematicalSystems._discretize` but we create a new one as
# it is internal
function _discretize(
algorithm::RungeKuttaDiscretization,
Δt,
f::F,
) where {F<:Function}
# We use the `let` block to avoid performance issues of Julia
# with closures. For more details, see
# https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured
return let
num_sub_steps = algorithm.num_sub_steps
Δt = Δt
(x, u) ->
runge_kutta_4(f, x, u, Δt, num_sub_steps)::typeof(x)
end
end

function MathematicalSystems.discretize(
system::MathematicalSystems.BlackBoxControlContinuousSystem,
Δt,
algorithm::RungeKuttaDiscretization,
)
f = let
num_sub_steps = algorithm.num_sub_steps
Δt = Δt
(x::SVector{N, T}, u) ->
runge_kutta_4(system.f, x, u, Δt, num_sub_steps)::SVector{N, T}
end
return MathematicalSystems.BlackBoxControlDiscreteSystem(
f,
_dicretize(algorithm, Δt, f),
system.statedim,
system.inputdim,
)
Expand All @@ -64,15 +76,9 @@ function MathematicalSystems.discretize(
Δt,
algorithm::RungeKuttaDiscretization,
)
growth_bound = let
num_sub_steps = algorithm.num_sub_steps
Δt = Δt
(r::SVector{N, T}, u) ->
runge_kutta_4(system.growth_bound, r, u, Δt, num_sub_steps)::SVector{N, T}
end
return WithGrowthBound(
MathematicalSystems.discretize(system.system, Δt, algorithm),
growth_bound,
_dicretize(algorithm, Δt, system.growth_bound),
)
end

Expand Down

0 comments on commit 7cc796c

Please sign in to comment.