Skip to content

Commit

Permalink
Merge pull request #70 from control-toolbox/auto-juliaformatter-pr
Browse files Browse the repository at this point in the history
[AUTO] JuliaFormatter.jl run
  • Loading branch information
ocots authored Sep 7, 2024
2 parents d43b64f + 0339093 commit 42e147f
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 247 deletions.
14 changes: 7 additions & 7 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ using Documenter
using DocumenterMermaid
using CTFlows

makedocs(
sitename = "CTFlows.jl",
format = Documenter.HTML(
prettyurls = false,
assets = [
makedocs(;
sitename="CTFlows.jl",
format=Documenter.HTML(;
prettyurls=false,
assets=[
asset("https://control-toolbox.org/assets/css/documentation.css"),
asset("https://control-toolbox.org/assets/js/documentation.js"),
],
),
pages = ["Introduction" => "index.md", "API" => "api.md", "Developers" => "dev.md"],
pages=["Introduction" => "index.md", "API" => "api.md", "Developers" => "dev.md"],
)

deploydocs(repo = "github.com/control-toolbox/CTFlows.jl.git", devbranch = "main")
deploydocs(; repo="github.com/control-toolbox/CTFlows.jl.git", devbranch="main")
2 changes: 1 addition & 1 deletion ext/CTFlowsODE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DCoTangent = ctVector
const ctgradient = CTBase.__ctgradient

# types
abstract type AbstractFlow{D, U} end
abstract type AbstractFlow{D,U} end

# from CTFlows
const __create_hamiltonian = CTFlows.__create_hamiltonian
Expand Down
31 changes: 18 additions & 13 deletions ext/concatenation.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function __concat_rhs(F::AbstractFlow{D, U}, G::AbstractFlow{D, U}, t_switch::Time) where {D, U}
function __concat_rhs(
F::AbstractFlow{D,U}, G::AbstractFlow{D,U}, t_switch::Time
) where {D,U}
function rhs!(du::D, u::U, p::Variable, t::Time)
t < t_switch ? F.rhs!(du, u, p, t) : G.rhs!(du, u, p, t)
return t < t_switch ? F.rhs!(du, u, p, t) : G.rhs!(du, u, p, t)
end
return rhs!
end

function __concat_rhs(F::VectorFieldFlow, G::VectorFieldFlow, t_switch::Time)
return (x::State, v::Variable, t::Time) -> (t < t_switch ? F.rhs(x, v, t) : G.rhs(x, v, t))
return (x::State, v::Variable, t::Time) ->
(t < t_switch ? F.rhs(x, v, t) : G.rhs(x, v, t))
end

function __concat_rhs(F::ODEFlow, G::ODEFlow, t_switch::Time)
Expand All @@ -23,16 +26,18 @@ end

function __concat_feedback_control(F::AbstractFlow, G::AbstractFlow, t_switch::Time)
function _feedback_control(t, x, u, v)
t < t_switch ? F.feedback_control(t, x, u, v) : G.feedback_control(t, x, u, v)
return if t < t_switch
F.feedback_control(t, x, u, v)
else
G.feedback_control(t, x, u, v)
end
end
feedback_control = ControlLaw(_feedback_control, NonAutonomous, NonFixed)
return feedback_control
end

function __concat_jumps(
F::AbstractFlow,
G::AbstractFlow,
jump::Union{Nothing, Tuple{Time, Any}} = nothing,
F::AbstractFlow, G::AbstractFlow, jump::Union{Nothing,Tuple{Time,Any}}=nothing
)
jumps = F.jumps
append!(jumps, G.jumps)
Expand All @@ -42,15 +47,15 @@ end

# --------------------------------------------------------------------------------------------
# concatenate two flows with a prescribed switching time
function concatenate(F::TF, g::Tuple{ctNumber, TF})::TF where {TF <: AbstractFlow}
function concatenate(F::TF, g::Tuple{ctNumber,TF})::TF where {TF<:AbstractFlow}
t_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
jumps = __concat_jumps(F, G) # jumps
return TF(F.f, rhs!, tstops, jumps) # we choose default values and options of F
end

function concatenate(F::TF, g::Tuple{ctNumber, Any, TF})::TF where {TF <: AbstractFlow}
function concatenate(F::TF, g::Tuple{ctNumber,Any,TF})::TF where {TF<:AbstractFlow}
t_switch, η_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
Expand All @@ -60,7 +65,7 @@ end

# --------------------------------------------------------------------------------------------
# concatenate two flows with a prescribed switching time
function concatenate(F::TF, g::Tuple{ctNumber, TF})::TF where {TF <: OptimalControlFlow}
function concatenate(F::TF, g::Tuple{ctNumber,TF})::TF where {TF<:OptimalControlFlow}
t_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
Expand All @@ -69,7 +74,7 @@ function concatenate(F::TF, g::Tuple{ctNumber, TF})::TF where {TF <: OptimalCont
return OptimalControlFlow(F.f, rhs!, feedback_u, F.ocp, F.kwargs_Flow, tstops, jumps) # we choose default values and options of F
end

function concatenate(F::TF, g::Tuple{ctNumber, Any, TF})::TF where {TF <: OptimalControlFlow}
function concatenate(F::TF, g::Tuple{ctNumber,Any,TF})::TF where {TF<:OptimalControlFlow}
t_switch, η_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
Expand All @@ -79,5 +84,5 @@ function concatenate(F::TF, g::Tuple{ctNumber, Any, TF})::TF where {TF <: Optima
end

# --------------------------------------------------------------------------------------------
*(F::TF, g::Tuple{ctNumber, TF}) where {TF <: AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber, Any, TF}) where {TF <: AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber,TF}) where {TF<:AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber,Any,TF}) where {TF<:AbstractFlow} = concatenate(F, g)
42 changes: 22 additions & 20 deletions ext/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,44 @@ function ode_usage(alg, abstol, reltol, saveat; kwargs_Flow...)

# kwargs has priority wrt kwargs_flow
function f(
tspan::Tuple{Time, Time},
tspan::Tuple{Time,Time},
x0,
v = nothing;
v=nothing;
jumps,
_t_stops_interne,
DiffEqRHS,
tstops = __tstops(),
callback = __callback(),
tstops=__tstops(),
callback=__callback(),
kwargs...,
)

# ode
ode =
isnothing(v) ? OrdinaryDiffEq.ODEProblem(DiffEqRHS, x0, tspan) :
ode = if isnothing(v)
OrdinaryDiffEq.ODEProblem(DiffEqRHS, x0, tspan)
else
OrdinaryDiffEq.ODEProblem(DiffEqRHS, x0, tspan, v)
end

# jumps and callbacks
cb, t_stops_all = __callbacks(callback, jumps, nothing, _t_stops_interne, tstops)

# solve
sol = OrdinaryDiffEq.solve(
ode,
alg = alg,
abstol = abstol,
reltol = reltol,
saveat = saveat,
tstops = t_stops_all,
callback = cb;
ode;
alg=alg,
abstol=abstol,
reltol=reltol,
saveat=saveat,
tstops=t_stops_all,
callback=cb,
kwargs_Flow...,
kwargs...,
)

return sol
end

function f(t0::Time, x0, tf::Time, v = nothing; kwargs...)
function f(t0::Time, x0, tf::Time, v=nothing; kwargs...)
sol = f((t0, tf), x0, v; kwargs...)
return sol.u[end]
end
Expand All @@ -53,12 +55,12 @@ end
# --------------------------------------------------------------------------------------------
function CTFlows.Flow(
dyn::Function;
autonomous = true,
variable = false,
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
autonomous=true,
variable=false,
alg=__alg(),
abstol=__abstol(),
reltol=__reltol(),
saveat=__saveat(),
kwargs_Flow...,
)
#
Expand Down
48 changes: 25 additions & 23 deletions ext/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Returns a function that solves ODE problem associated to Hamiltonian vector fiel
"""
function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)
function f(
tspan::Tuple{Time, Time},
tspan::Tuple{Time,Time},
x0::State,
p0::Costate,
v::Variable = __variable(x0, p0);
v::Variable=__variable(x0, p0);
jumps,
_t_stops_interne,
DiffEqRHS,
tstops = __tstops(),
callback = __callback(),
tstops=__tstops(),
callback=__callback(),
kwargs...,
)

Expand All @@ -23,17 +23,19 @@ function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)

# jumps and callbacks
n = size(x0, 1)
cb, t_stops_all = __callbacks(callback, jumps, rg(n + 1, 2n), _t_stops_interne, tstops)
cb, t_stops_all = __callbacks(
callback, jumps, rg(n + 1, 2n), _t_stops_interne, tstops
)

# solve
sol = OrdinaryDiffEq.solve(
ode,
alg = alg,
abstol = abstol,
reltol = reltol,
saveat = saveat,
tstops = t_stops_all,
callback = cb;
ode;
alg=alg,
abstol=abstol,
reltol=reltol,
saveat=saveat,
tstops=t_stops_all,
callback=cb,
kwargs_Flow...,
kwargs...,
)
Expand All @@ -46,7 +48,7 @@ function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)
x0::State,
p0::Costate,
tf::Time,
v::Variable = __variable(x0, p0);
v::Variable=__variable(x0, p0);
kwargs...,
)
sol = f((t0, tf), x0, p0, v; kwargs...)
Expand All @@ -68,7 +70,7 @@ function rhs(h::AbstractHamiltonian)
foo(z) = h(t, z[rg(1, n)], z[rg(n + 1, 2n)], v)
dh = ctgradient(foo, z)
dz[1:n] = dh[(n + 1):(2n)]
dz[(n + 1):(2n)] = -dh[1:n]
return dz[(n + 1):(2n)] = -dh[1:n]
end
return rhs!
end
Expand All @@ -77,10 +79,10 @@ end
# Flow from a Hamiltonian
function CTFlows.Flow(
h::AbstractHamiltonian;
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
alg=__alg(),
abstol=__abstol(),
reltol=__reltol(),
saveat=__saveat(),
kwargs_Flow...,
)
#
Expand All @@ -93,17 +95,17 @@ end
# Flow from a Hamiltonian Vector Field
function CTFlows.Flow(
hv::HamiltonianVectorField;
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
alg=__alg(),
abstol=__abstol(),
reltol=__reltol(),
saveat=__saveat(),
kwargs_Flow...,
)
#
f = hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)
function rhs!(dz::DCoTangent, z::CoTangent, v::Variable, t::Time)
n = size(z, 1) ÷ 2
dz[rg(1, n)], dz[rg(n + 1, 2n)] = hv(t, z[rg(1, n)], z[rg(n + 1, 2n)], v)
return dz[rg(1, n)], dz[rg(n + 1, 2n)] = hv(t, z[rg(1, n)], z[rg(n + 1, 2n)], v)
end
return HamiltonianFlow(f, rhs!)
end
38 changes: 19 additions & 19 deletions ext/optimal_control_problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ julia> f = Flow(ocp, (x, p) -> p)
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
"""
function CTFlows.Flow(
ocp::OptimalControlModel{T, V},
u_::Union{Function, ControlLaw{T, V}};
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
ocp::OptimalControlModel{T,V},
u_::Union{Function,ControlLaw{T,V}};
alg=__alg(),
abstol=__abstol(),
reltol=__reltol(),
saveat=__saveat(),
kwargs_Flow...,
) where {T, V}
) where {T,V}
h, u = __create_hamiltonian(ocp, u_) # construction of the Hamiltonian
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
end
Expand All @@ -46,33 +46,33 @@ julia> f = Flow(ocp, (t, x, p) -> p[1], (t, x, u) -> x[1] - 1, (t, x, p) -> x[1]
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
"""
function CTFlows.Flow(
ocp::OptimalControlModel{T, V},
u_::Union{Function, ControlLaw{T, V}, FeedbackControl{T, V}},
g_::Union{Function, MixedConstraint{T, V}, StateConstraint{T, V}},
μ_::Union{Function, Multiplier{T, V}};
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
ocp::OptimalControlModel{T,V},
u_::Union{Function,ControlLaw{T,V},FeedbackControl{T,V}},
g_::Union{Function,MixedConstraint{T,V},StateConstraint{T,V}},
μ_::Union{Function,Multiplier{T,V}};
alg=__alg(),
abstol=__abstol(),
reltol=__reltol(),
saveat=__saveat(),
kwargs_Flow...,
) where {T, V}
) where {T,V}
h, u = __create_hamiltonian(ocp, u_, g_, μ_) # construction of the Hamiltonian
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
end

# ---------------------------------------------------------------------------------------------------
function __ocp_Flow(
ocp::OptimalControlModel{T, V},
ocp::OptimalControlModel{T,V},
h::Hamiltonian,
u::ControlLaw,
alg,
abstol,
reltol,
saveat;
kwargs_Flow...,
) where {T, V}
) where {T,V}
rhs! = rhs(h) # right and side: same as for a flow from a Hamiltonian
f = hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...) # flow function
kwargs_Flow = (kwargs_Flow..., alg = alg, abstol = abstol, reltol = reltol, saveat = saveat)
kwargs_Flow = (kwargs_Flow..., alg=alg, abstol=abstol, reltol=reltol, saveat=saveat)
return OptimalControlFlow(f, rhs!, u, ocp, kwargs_Flow)
end
Loading

0 comments on commit 42e147f

Please sign in to comment.