Skip to content

Commit 5f4db4f

Browse files
authored
Merge pull request #85 from control-toolbox/51-dev-tests
improve covering
2 parents eae2b56 + 15eb4f3 commit 5f4db4f

19 files changed

Lines changed: 595 additions & 216 deletions

ext/CTModelsJLD.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ $(TYPEDSIGNATURES)
1010
Export OCP solution in JLD format
1111
"""
1212
function CTModels.export_ocp_solution(
13-
::CTModels.JLD2Tag, sol::CTModels.Solution; filename_prefix="solution"
13+
::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String="solution"
1414
)
15-
save_object(filename_prefix * ".jld2", sol)
15+
save_object(filename * ".jld2", sol)
1616
return nothing
1717
end
1818

@@ -22,9 +22,9 @@ $(TYPEDSIGNATURES)
2222
Import OCP solution in JLD format
2323
"""
2424
function CTModels.import_ocp_solution(
25-
::CTModels.JLD2Tag, ocp::CTModels.Model; filename_prefix="solution"
25+
::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String="solution"
2626
)
27-
return load_object(filename_prefix * ".jld2")
27+
return load_object(filename * ".jld2")
2828
end
2929

3030
end

ext/CTModelsJSON.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ $(TYPEDSIGNATURES)
1111
Export OCP solution in JSON format
1212
"""
1313
function CTModels.export_ocp_solution(
14-
::CTModels.JSON3Tag, sol::CTModels.Solution; filename_prefix="solution"
14+
::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String="solution"
1515
)
1616
T = CTModels.time_grid(sol)
1717

1818
blob = Dict(
1919
"time_grid" => CTModels.time_grid(sol),
20-
"state" => CTModels.state_discretized(sol),
21-
"control" => CTModels.control_discretized(sol),
20+
"state" => CTModels.discretize(CTModels.state(sol), T),
21+
"control" => CTModels.discretize(CTModels.control(sol), T),
2222
"variable" => CTModels.variable(sol),
23-
"costate" => CTModels.costate_discretized(sol)[1:(end - 1), :],
23+
"costate" => CTModels.discretize(CTModels.costate(sol), T),
2424
"objective" => CTModels.objective(sol),
2525
"iterations" => CTModels.iterations(sol),
2626
"constraints_violation" => CTModels.constraints_violation(sol),
@@ -42,7 +42,7 @@ function CTModels.export_ocp_solution(
4242
"variable_constraints_ub_dual" => CTModels.variable_constraints_ub_dual(sol), # ctVector or Nothing
4343
)
4444

45-
open(filename_prefix * ".json", "w") do io
45+
open(filename * ".json", "w") do io
4646
JSON3.pretty(io, blob)
4747
end
4848

@@ -55,9 +55,9 @@ $(TYPEDSIGNATURES)
5555
Import OCP solution in JSON format
5656
"""
5757
function CTModels.import_ocp_solution(
58-
::CTModels.JSON3Tag, ocp::CTModels.Model; filename_prefix="solution"
58+
::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String="solution"
5959
)
60-
json_string = read(filename_prefix * ".json", String)
60+
json_string = read(filename * ".json", String)
6161
blob = JSON3.read(json_string)
6262

6363
# get state

ext/default.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ $(TYPEDSIGNATURES)
6161
6262
Default style for the plot. Must be an empty tuple.
6363
"""
64-
__plot_style() = ()
64+
__plot_style() = NamedTuple()
6565

6666
"""
6767
$(TYPEDSIGNATURES)

ext/plot.jl

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,27 @@ function __plot_time!(
103103
return p
104104
end
105105

106-
"""
107-
$(TYPEDSIGNATURES)
108-
109-
Plot the i-th component of a vectorial function of time `f(t) ∈ Rᵈ` where `f` is given by the symbol `s`.
110-
- The argument `s` can be `:state`, `:control` or `:costate`.
111-
- `time` can be `:default` or `:normalize`.
112-
"""
113-
function __plot_time(
114-
sol::CTModels.Solution,
115-
model::Union{CTModels.Model,Nothing},
116-
s::Symbol,
117-
i::Int,
118-
time::Symbol;
119-
t_label::String,
120-
label::String,
121-
kwargs...,
122-
)
123-
return __plot_time!(
124-
Plots.plot(), sol, model, s, i, time; t_label=t_label, label=label, kwargs...
125-
)
126-
end
106+
# """
107+
# $(TYPEDSIGNATURES)
108+
109+
# Plot the i-th component of a vectorial function of time `f(t) ∈ Rᵈ` where `f` is given by the symbol `s`.
110+
# - The argument `s` can be `:state`, `:control` or `:costate`.
111+
# - `time` can be `:default` or `:normalize`.
112+
# """
113+
# function __plot_time(
114+
# sol::CTModels.Solution,
115+
# model::Union{CTModels.Model,Nothing},
116+
# s::Symbol,
117+
# i::Int,
118+
# time::Symbol;
119+
# t_label::String,
120+
# label::String,
121+
# kwargs...,
122+
# )
123+
# return __plot_time!(
124+
# Plots.plot(), sol, model, s, i, time; t_label=t_label, label=label, kwargs...
125+
# )
126+
# end
127127

128128
"""
129129
$(TYPEDSIGNATURES)
@@ -153,36 +153,36 @@ function __plot_time!(
153153
return p
154154
end
155155

156-
"""
157-
$(TYPEDSIGNATURES)
158-
159-
Plot a vectorial function of time `f(t) ∈ Rᵈ` where `f` is given by the symbol `s`.
160-
The argument `s` can be `:state`, `:control` or `:costate`.
161-
"""
162-
function __plot_time(
163-
sol::CTModels.Solution,
164-
model::Union{CTModels.Model,Nothing},
165-
d::CTModels.Dimension,
166-
s::Symbol,
167-
time::Symbol;
168-
t_label::String,
169-
labels::Vector{String},
170-
title::String,
171-
kwargs...,
172-
)
173-
return __plot_time!(
174-
Plots.plot(),
175-
sol,
176-
model,
177-
d,
178-
s,
179-
time;
180-
t_label=t_label,
181-
labels=labels,
182-
title=title,
183-
kwargs...,
184-
)
185-
end
156+
# """
157+
# $(TYPEDSIGNATURES)
158+
159+
# Plot a vectorial function of time `f(t) ∈ Rᵈ` where `f` is given by the symbol `s`.
160+
# The argument `s` can be `:state`, `:control` or `:costate`.
161+
# """
162+
# function __plot_time(
163+
# sol::CTModels.Solution,
164+
# model::Union{CTModels.Model,Nothing},
165+
# d::CTModels.Dimension,
166+
# s::Symbol,
167+
# time::Symbol;
168+
# t_label::String,
169+
# labels::Vector{String},
170+
# title::String,
171+
# kwargs...,
172+
# )
173+
# return __plot_time!(
174+
# Plots.plot(),
175+
# sol,
176+
# model,
177+
# d,
178+
# s,
179+
# time;
180+
# t_label=t_label,
181+
# labels=labels,
182+
# title=title,
183+
# kwargs...,
184+
# )
185+
# end
186186

187187
# --------------------------------------------------------------------------------------------------
188188
#
@@ -406,15 +406,15 @@ function __plot!(
406406
time::Symbol,
407407
control::Symbol,
408408
layout::Symbol,
409-
time_style::Tuple,
410-
state_style::Tuple,
411-
state_bounds_style::Tuple,
412-
control_style::Tuple,
413-
control_bounds_style::Tuple,
414-
costate_style::Tuple,
415-
path_style::Tuple,
416-
path_bounds_style::Tuple,
417-
dual_path_style::Tuple,
409+
time_style::NamedTuple,
410+
state_style::NamedTuple,
411+
state_bounds_style::NamedTuple,
412+
control_style::NamedTuple,
413+
control_bounds_style::NamedTuple,
414+
costate_style::NamedTuple,
415+
path_style::NamedTuple,
416+
path_bounds_style::NamedTuple,
417+
dual_path_style::NamedTuple,
418418
kwargs...,
419419
)
420420
if solution_label != ""
@@ -816,15 +816,15 @@ function __plot(
816816
time::Symbol,
817817
control::Symbol,
818818
layout::Symbol,
819-
time_style::Tuple,
820-
state_style::Tuple,
821-
state_bounds_style::Tuple,
822-
control_style::Tuple,
823-
control_bounds_style::Tuple,
824-
costate_style::Tuple,
825-
path_style::Tuple,
826-
path_bounds_style::Tuple,
827-
dual_path_style::Tuple,
819+
time_style::NamedTuple,
820+
state_style::NamedTuple,
821+
state_bounds_style::NamedTuple,
822+
control_style::NamedTuple,
823+
control_bounds_style::NamedTuple,
824+
costate_style::NamedTuple,
825+
path_style::NamedTuple,
826+
path_bounds_style::NamedTuple,
827+
dual_path_style::NamedTuple,
828828
size=__size_plot(sol, model, control, layout),
829829
kwargs...,
830830
)
@@ -890,12 +890,12 @@ function Plots.plot!(
890890
control_style=control_style,
891891
costate_style=costate_style,
892892
model=nothing,
893-
state_bounds_style=(),
894-
control_bounds_style=(),
895-
time_style=(),
896-
path_style=(),
897-
path_bounds_style=(),
898-
dual_path_style=(),
893+
state_bounds_style=__plot_style(),
894+
control_bounds_style=__plot_style(),
895+
time_style=__plot_style(),
896+
path_style=__plot_style(),
897+
path_bounds_style=__plot_style(),
898+
dual_path_style=__plot_style(),
899899
kwargs...,
900900
)
901901
end
@@ -932,12 +932,12 @@ function Plots.plot(
932932
control_style=control_style,
933933
costate_style=costate_style,
934934
model=nothing,
935-
state_bounds_style=(),
936-
control_bounds_style=(),
937-
time_style=(),
938-
path_style=(),
939-
path_bounds_style=(),
940-
dual_path_style=(),
935+
state_bounds_style=__plot_style(),
936+
control_bounds_style=__plot_style(),
937+
time_style=__plot_style(),
938+
path_style=__plot_style(),
939+
path_bounds_style=__plot_style(),
940+
dual_path_style=__plot_style(),
941941
size=size,
942942
kwargs...,
943943
)
@@ -966,15 +966,15 @@ function Plots.plot!(
966966
control::Symbol=__control_layout(),
967967
time::Symbol=__time_normalization(),
968968
solution_label::String=__plot_label_suffix(),
969-
state_style::Tuple=__plot_style(),
970-
state_bounds_style::Tuple=__plot_style(),
971-
control_style::Tuple=__plot_style(),
972-
control_bounds_style::Tuple=__plot_style(),
973-
costate_style::Tuple=__plot_style(),
974-
time_style::Tuple=__plot_style(),
975-
path_style::Tuple=__plot_style(),
976-
path_bounds_style::Tuple=__plot_style(),
977-
dual_path_style::Tuple=__plot_style(),
969+
state_style::NamedTuple=__plot_style(),
970+
state_bounds_style::NamedTuple=__plot_style(),
971+
control_style::NamedTuple=__plot_style(),
972+
control_bounds_style::NamedTuple=__plot_style(),
973+
costate_style::NamedTuple=__plot_style(),
974+
time_style::NamedTuple=__plot_style(),
975+
path_style::NamedTuple=__plot_style(),
976+
path_bounds_style::NamedTuple=__plot_style(),
977+
dual_path_style::NamedTuple=__plot_style(),
978978
kwargs...,
979979
)
980980

@@ -1018,15 +1018,15 @@ function Plots.plot(
10181018
control::Symbol=__control_layout(),
10191019
time::Symbol=__time_normalization(),
10201020
solution_label::String=__plot_label_suffix(),
1021-
state_style::Tuple=__plot_style(),
1022-
state_bounds_style::Tuple=__plot_style(),
1023-
control_style::Tuple=__plot_style(),
1024-
control_bounds_style::Tuple=__plot_style(),
1025-
costate_style::Tuple=__plot_style(),
1026-
time_style::Tuple=__plot_style(),
1027-
path_style::Tuple=__plot_style(),
1028-
path_bounds_style::Tuple=__plot_style(),
1029-
dual_path_style::Tuple=__plot_style(),
1021+
state_style::NamedTuple=__plot_style(),
1022+
state_bounds_style::NamedTuple=__plot_style(),
1023+
control_style::NamedTuple=__plot_style(),
1024+
control_bounds_style::NamedTuple=__plot_style(),
1025+
costate_style::NamedTuple=__plot_style(),
1026+
time_style::NamedTuple=__plot_style(),
1027+
path_style::NamedTuple=__plot_style(),
1028+
path_bounds_style::NamedTuple=__plot_style(),
1029+
dual_path_style::NamedTuple=__plot_style(),
10301030
size=__size_plot(sol, model, control, layout),
10311031
kwargs...,
10321032
)

src/CTModels.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,19 @@ Export a solution in JLD or JSON formats.
163163
# Examples
164164
165165
```julia-repl
166-
julia> CTModels.export_ocp_solution(sol; filename_prefix="solution", format=:JSON)
167-
julia> CTModels.export_ocp_solution(sol; filename_prefix="solution", format=:JLD)
166+
julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JSON)
167+
julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JLD)
168168
```
169169
"""
170-
function export_ocp_solution(args...; format=__format(), kwargs...)
170+
function export_ocp_solution(args...; format::Symbol=__format(), kwargs...)
171171
if format == :JLD
172172
return export_ocp_solution(JLD2Tag(), args...; kwargs...)
173173
elseif format == :JSON
174174
return export_ocp_solution(JSON3Tag(), args...; kwargs...)
175175
else
176176
throw(
177177
CTBase.IncorrectArgument(
178-
"Export_ocp_solution: unknown format (should be :JLD or :JSON): ", format
178+
"unknown format (should be :JLD or :JSON): " * string(format)
179179
),
180180
)
181181
end
@@ -189,19 +189,19 @@ Import a solution from a JLD or JSON file.
189189
# Examples
190190
191191
```julia-repl
192-
julia> sol = CTModels.import_ocp_solution(ocp; filename_prefix="solution", format=:JSON)
193-
julia> sol = CTModels.import_ocp_solution(ocp; filename_prefix="solution", format=:JLD)
192+
julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JSON)
193+
julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JLD)
194194
```
195195
"""
196-
function import_ocp_solution(args...; format=__format(), kwargs...)
196+
function import_ocp_solution(args...; format::Symbol=__format(), kwargs...)
197197
if format == :JLD
198198
return import_ocp_solution(JLD2Tag(), args...; kwargs...)
199199
elseif format == :JSON
200200
return import_ocp_solution(JSON3Tag(), args...; kwargs...)
201201
else
202202
throw(
203203
CTBase.IncorrectArgument(
204-
"Import_ocp_solution: unknown format (should be :JLD or :JSON): ", format
204+
"unknown format (should be :JLD or :JSON): " * string(format)
205205
),
206206
)
207207
end
@@ -221,14 +221,14 @@ function RecipesBase.plot(sol::AbstractSolution; kwargs...)
221221
throw(CTBase.ExtensionError(:Plots))
222222
end
223223

224-
"""
225-
$(TYPEDSIGNATURES)
224+
# """
225+
# $(TYPEDSIGNATURES)
226226

227-
Plot a solution on an existing plot.
228-
"""
229-
function RecipesBase.plot!(p::RecipesBase.AbstractPlot, sol::AbstractSolution; kwargs...)
230-
throw(CTBase.ExtensionError(:Plots))
231-
end
227+
# Plot a solution on an existing plot.
228+
# """
229+
# function RecipesBase.plot!(p::RecipesBase.AbstractPlot, sol::AbstractSolution; kwargs...)
230+
# throw(CTBase.ExtensionError(:Plots))
231+
# end
232232

233233
#
234234
include("init.jl")

0 commit comments

Comments
 (0)