Skip to content

Commit f863bb0

Browse files
authored
Merge pull request #123 from control-toolbox/122-doc-update-docstrings
Update docstrings
2 parents 3ca8e09 + af81f5e commit f863bb0

18 files changed

Lines changed: 906 additions & 131 deletions

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
34

45
[compat]
56
Documenter = "1"

docs/make.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
using Documenter
22
using CTModels
3+
using Plots
4+
5+
# to add docstrings from external packages
6+
Modules = [Plots]
7+
for Module in Modules
8+
isnothing(DocMeta.getdocmeta(Module, :DocTestSetup)) &&
9+
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive=true)
10+
end
311

412
repo_url = "github.com/control-toolbox/CTModels.jl"
513

docs/src/dev.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ Modules = [CTModels]
2828
Order = [:constant, :type, :function, :macro]
2929
```
3030

31+
```@docs
32+
plot(::CTModels.Solution, ::Symbol...)
33+
```

ext/CTModelsJLD.jl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@ using JLD2
66

77
"""
88
$(TYPEDSIGNATURES)
9-
10-
Export OCP solution in JLD format
9+
10+
Export an optimal control solution to a `.jld2` file using the JLD2 format.
11+
12+
This function serializes and saves a `CTModels.Solution` object to disk,
13+
allowing it to be reloaded later.
14+
15+
# Arguments
16+
- `::CTModels.JLD2Tag`: A tag used to dispatch the export method for JLD2.
17+
- `sol::CTModels.Solution`: The optimal control solution to be saved.
18+
19+
# Keyword Arguments
20+
- `filename::String = "solution"`: Base name of the file. The `.jld2` extension is automatically appended.
21+
22+
# Example
23+
```julia-repl
24+
julia> export_ocp_solution(JLD2Tag(), sol; filename="mysolution")
25+
# → creates "mysolution.jld2"
26+
```
1127
"""
1228
function CTModels.export_ocp_solution(
1329
::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String="solution"
@@ -18,8 +34,25 @@ end
1834

1935
"""
2036
$(TYPEDSIGNATURES)
21-
22-
Import OCP solution in JLD format
37+
38+
Import an optimal control solution from a `.jld2` file.
39+
40+
This function loads a previously saved `CTModels.Solution` from disk.
41+
42+
# Arguments
43+
- `::CTModels.JLD2Tag`: A tag used to dispatch the import method for JLD2.
44+
- `ocp::CTModels.Model`: The associated model (used for dispatch consistency; not used internally).
45+
46+
# Keyword Arguments
47+
- `filename::String = "solution"`: Base name of the file. The `.jld2` extension is automatically appended.
48+
49+
# Returns
50+
- `CTModels.Solution`: The loaded solution object.
51+
52+
# Example
53+
```julia-repl
54+
julia> sol = import_ocp_solution(JLD2Tag(), model; filename="mysolution")
55+
```
2356
"""
2457
function CTModels.import_ocp_solution(
2558
::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String="solution"

ext/CTModelsJSON.jl

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,27 @@ using JSON3
77

88
"""
99
$(TYPEDSIGNATURES)
10-
11-
Export OCP solution in JSON format
10+
11+
Export an optimal control solution to a `.json` file using the JSON3 format.
12+
13+
This function serializes a `CTModels.Solution` into a structured JSON dictionary,
14+
including all primal and dual information, which can be read by external tools.
15+
16+
# Arguments
17+
- `::CTModels.JSON3Tag`: A tag used to dispatch the export method for JSON3.
18+
- `sol::CTModels.Solution`: The solution to be saved.
19+
20+
# Keyword Arguments
21+
- `filename::String = "solution"`: Base filename. The `.json` extension is automatically appended.
22+
23+
# Notes
24+
The exported JSON includes the time grid, state, control, costate, objective, solver info, and all constraint duals (if available).
25+
26+
# Example
27+
```julia-repl
28+
julia> export_ocp_solution(JSON3Tag(), sol; filename="mysolution")
29+
# → creates "mysolution.json"
30+
```
1231
"""
1332
function CTModels.export_ocp_solution(
1433
::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String="solution"
@@ -51,8 +70,29 @@ end
5170

5271
"""
5372
$(TYPEDSIGNATURES)
54-
55-
Import OCP solution in JSON format
73+
74+
Import an optimal control solution from a `.json` file exported with `export_ocp_solution`.
75+
76+
This function reads the JSON contents and reconstructs a `CTModels.Solution` object,
77+
including the discretized primal and dual trajectories.
78+
79+
# Arguments
80+
- `::CTModels.JSON3Tag`: A tag used to dispatch the import method for JSON3.
81+
- `ocp::CTModels.Model`: The model associated with the optimal control problem. Used to rebuild the full solution.
82+
83+
# Keyword Arguments
84+
- `filename::String = "solution"`: Base filename. The `.json` extension is automatically appended.
85+
86+
# Returns
87+
- `CTModels.Solution`: A reconstructed solution instance.
88+
89+
# Notes
90+
Handles both vector and matrix encodings of signals. If dual fields are missing or `null`, the corresponding attributes are set to `nothing`.
91+
92+
# Example
93+
```julia-repl
94+
julia> sol = import_ocp_solution(JSON3Tag(), model; filename="mysolution")
95+
```
5696
"""
5797
function CTModels.import_ocp_solution(
5898
::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String="solution"

ext/default.jl

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,59 @@
11
"""
22
$(TYPEDSIGNATURES)
33
4-
Used to set the default value of the layout of the plots.
5-
Either :split or :group.
4+
Default layout for the full plot.
5+
6+
Returns `:split`, which arranges each component (e.g. state, control) in separate subplots.
7+
8+
Possible values:
9+
- `:split`: One subplot per component (default).
10+
- `:group`: Combine components into shared subplots.
11+
12+
Used as the default for `layout` in `plot(sol; layout=...)`.
613
"""
714
__plot_layout() = :split
815

916
"""
1017
$(TYPEDSIGNATURES)
1118
12-
Used to set the default value of the layout of the control plots.
13-
Either :components or :norm or :all.
19+
Default layout for control input visualization.
20+
21+
Returns `:components`, which plots each control component individually.
22+
23+
Possible values:
24+
- `:components`: One plot per control component (default).
25+
- `:norm`: Single plot showing the control norm ‖u(t)‖.
26+
- `:all`: Show both components and norm.
27+
28+
Used as the default for `control` in `plot(sol; control=...)`.
1429
"""
1530
__control_layout() = :components
1631

1732
"""
1833
$(TYPEDSIGNATURES)
1934
20-
Used to set the default value of the time grid normalization.
21-
Either :default or :normalize or :normalise.
35+
Default time axis normalization.
36+
37+
Returns `:default`, which plots against real time.
38+
39+
Possible values:
40+
- `:default`: Plot time in original units (default).
41+
- `:normalize`: Normalize time to [0, 1].
42+
- `:normalise`: Same as `:normalize`, British spelling.
43+
44+
Used as the default for `time` in `plot(sol; time=...)`.
2245
"""
2346
__time_normalization() = :default
2447

2548
"""
2649
$(TYPEDSIGNATURES)
2750
51+
Return the default list of description symbols to be plotted if the user does not specify any.
52+
53+
Includes aliases for backward compatibility.
54+
55+
Returns a tuple of symbols, such as:
56+
- `:state`, `:costate`, `:control`, `:path`, `:dual`, ...
2857
"""
2958
function __description()
3059
(
@@ -46,7 +75,20 @@ end
4675
"""
4776
$(TYPEDSIGNATURES)
4877
49-
Used to set the default value of the plot size.
78+
Compute a default size `(width, height)` for the plot figure.
79+
80+
This depends on the number of subplots, which is inferred from:
81+
- The layout (`:group` or `:split`)
82+
- The presence of state, control, costate, path constraint, or dual variable plots
83+
- The number of state and control variables
84+
- The control layout choice (`:components`, `:norm`, `:all`)
85+
86+
Used internally in the `plot` function to automatically size the output plot.
87+
88+
# Example
89+
```julia-repl
90+
julia> size = __size_plot(sol, model, :components, :split; ...)
91+
```
5092
"""
5193
function __size_plot(
5294
sol::CTModels.Solution,
@@ -124,13 +166,21 @@ end
124166
"""
125167
$(TYPEDSIGNATURES)
126168
127-
Default style for the plot. Must be an empty tuple.
169+
Default plot style.
170+
171+
Returns an empty `NamedTuple()`, which means no style override is applied.
172+
173+
Used when no user-defined style is passed for plotting states, controls, etc.
128174
"""
129175
__plot_style() = NamedTuple()
130176

131177
"""
132178
$(TYPEDSIGNATURES)
133179
134-
Default suffix label for the plot. Must be an empty string.
180+
Default suffix used for the solution label in plots.
181+
182+
Returns an empty string `""`.
183+
184+
This label can be used to distinguish multiple solutions in comparative plots.
135185
"""
136186
__plot_label_suffix() = ""

0 commit comments

Comments
 (0)