Skip to content

Commit a2053b1

Browse files
ocotsgithub-actions[bot]
authored andcommitted
🤖 Format .jl files
1 parent 2af415d commit a2053b1

21 files changed

Lines changed: 343 additions & 201 deletions

ext/CTModelsJLD.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ julia> export_ocp_solution(JLD2Tag(), sol; filename="mysolution")
2525
# → creates "mysolution.jld2"
2626
```
2727
"""
28-
function CTModels.export_ocp_solution(::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String)
28+
function CTModels.export_ocp_solution(
29+
::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String
30+
)
2931
save_object(filename * ".jld2", sol)
3032
return nothing
3133
end
@@ -52,7 +54,9 @@ This function loads a previously saved `CTModels.Solution` from disk.
5254
julia> sol = import_ocp_solution(JLD2Tag(), model; filename="mysolution")
5355
```
5456
"""
55-
function CTModels.import_ocp_solution(::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String)
57+
function CTModels.import_ocp_solution(
58+
::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String
59+
)
5660
return load_object(filename * ".jld2")
5761
end
5862

ext/CTModelsJSON.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ julia> export_ocp_solution(JSON3Tag(), sol; filename="mysolution")
2929
# → creates "mysolution.json"
3030
```
3131
"""
32-
function CTModels.export_ocp_solution(::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String)
32+
function CTModels.export_ocp_solution(
33+
::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String
34+
)
3335
T = CTModels.time_grid(sol)
3436

3537
blob = Dict(
@@ -92,7 +94,9 @@ Handles both vector and matrix encodings of signals. If dual fields are missing
9294
julia> sol = import_ocp_solution(JSON3Tag(), model; filename="mysolution")
9395
```
9496
"""
95-
function CTModels.import_ocp_solution(::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String)
97+
function CTModels.import_ocp_solution(
98+
::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String
99+
)
96100
json_string = read(filename * ".json", String)
97101
blob = JSON3.read(json_string)
98102

src/CTModels.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,36 @@ $(TYPEDSIGNATURES)
136136
137137
Export a solution in JLD format.
138138
"""
139-
export_ocp_solution(::JLD2Tag, ::AbstractSolution; filename::String) = throw(CTBase.ExtensionError(:JLD2))
139+
function export_ocp_solution(::JLD2Tag, ::AbstractSolution; filename::String)
140+
throw(CTBase.ExtensionError(:JLD2))
141+
end
140142

141143
"""
142144
$(TYPEDSIGNATURES)
143145
144146
Import a solution from a JLD file.
145147
"""
146-
import_ocp_solution(::JLD2Tag, ::AbstractModel; filename::String) = throw(CTBase.ExtensionError(:JLD2))
148+
function import_ocp_solution(::JLD2Tag, ::AbstractModel; filename::String)
149+
throw(CTBase.ExtensionError(:JLD2))
150+
end
147151

148152
"""
149153
$(TYPEDSIGNATURES)
150154
151155
Export a solution in JSON format.
152156
"""
153-
export_ocp_solution(::JSON3Tag, ::AbstractSolution; filename::String) = throw(CTBase.ExtensionError(:JSON3))
157+
function export_ocp_solution(::JSON3Tag, ::AbstractSolution; filename::String)
158+
throw(CTBase.ExtensionError(:JSON3))
159+
end
154160

155161
"""
156162
$(TYPEDSIGNATURES)
157163
158164
Import a solution from a JLD file.
159165
"""
160-
import_ocp_solution(::JSON3Tag, ::AbstractModel; filename::String) = throw(CTBase.ExtensionError(:JSON3))
166+
function import_ocp_solution(::JSON3Tag, ::AbstractModel; filename::String)
167+
throw(CTBase.ExtensionError(:JSON3))
168+
end
161169

162170
"""
163171
$(TYPEDSIGNATURES)
@@ -171,7 +179,11 @@ julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JSON)
171179
julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JLD)
172180
```
173181
"""
174-
function export_ocp_solution(sol::AbstractSolution; format::Symbol=__format(), filename::String=__filename_export_import())
182+
function export_ocp_solution(
183+
sol::AbstractSolution;
184+
format::Symbol=__format(),
185+
filename::String=__filename_export_import(),
186+
)
175187
if format == :JLD
176188
return export_ocp_solution(JLD2Tag(), sol; filename=filename)
177189
elseif format == :JSON
@@ -197,7 +209,11 @@ julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JSON
197209
julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JLD)
198210
```
199211
"""
200-
function import_ocp_solution(ocp::AbstractModel; format::Symbol=__format(), filename::String=__filename_export_import())
212+
function import_ocp_solution(
213+
ocp::AbstractModel;
214+
format::Symbol=__format(),
215+
filename::String=__filename_export_import(),
216+
)
201217
if format == :JLD
202218
return import_ocp_solution(JLD2Tag(), ocp; filename=filename)
203219
elseif format == :JSON

src/constraints.jl

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ function __constraint!(
6262
)
6363

6464
# checkings: the constraint must not be set before
65-
@ensure(!(label keys(ocp_constraints)),
65+
@ensure(
66+
!(label keys(ocp_constraints)),
6667
CTBase.UnauthorizedCall(
6768
"the constraint named " * String(label) * " already exists."
6869
),
6970
)
7071

7172
# checkings: lb and ub cannot be both nothing
72-
@ensure(!(isnothing(lb) && isnothing(ub)),
73+
@ensure(
74+
!(isnothing(lb) && isnothing(ub)),
7375
CTBase.UnauthorizedCall(
7476
"The lower bound `lb` and the upper bound `ub` cannot be both nothing."
7577
),
@@ -80,7 +82,8 @@ function __constraint!(
8082
isnothing(ub) && (ub = Inf * ones(eltype(lb), length(lb)))
8183

8284
# lb and ub must have the same length
83-
@ensure(length(lb) == length(ub),
85+
@ensure(
86+
length(lb) == length(ub),
8487
CTBase.IncorrectArgument(
8588
"the lower bound `lb` and the upper bound `ub` must have the same length."
8689
),
@@ -116,21 +119,24 @@ function __constraint!(
116119
@ensure(length(rg) == length(lb), CTBase.IncorrectArgument(txt))
117120
# check if the range is valid
118121
if type == :state
119-
@ensure(all(1 .≤ rg .≤ n),
122+
@ensure(
123+
all(1 .≤ rg .≤ n),
120124
CTBase.IncorrectArgument(
121-
"the range of the state constraint must be contained in 1:$n",
125+
"the range of the state constraint must be contained in 1:$n"
122126
),
123127
)
124128
elseif type == :control
125-
@ensure(all(1 .≤ rg .≤ m),
129+
@ensure(
130+
all(1 .≤ rg .≤ m),
126131
CTBase.IncorrectArgument(
127-
"the range of the control constraint must be contained in 1:$m",
132+
"the range of the control constraint must be contained in 1:$m"
128133
),
129134
)
130135
elseif type == :variable
131-
@ensure(all(1 .≤ rg .≤ q),
136+
@ensure(
137+
all(1 .≤ rg .≤ q),
132138
CTBase.IncorrectArgument(
133-
"the range of the variable constraint must be contained in 1:$q",
139+
"the range of the variable constraint must be contained in 1:$q"
134140
),
135141
)
136142
else
@@ -192,21 +198,27 @@ julia> constraint!(ocp, :control, rg=1:2, lb=[0.0], ub=[1.0], label=:control_con
192198
function constraint!(
193199
ocp::PreModel,
194200
type::Symbol;
195-
rg::Union{Int, OrdinalRange{Int}, Nothing} = nothing,
196-
f::Union{Function, Nothing} = nothing,
197-
lb::Union{ctNumber, ctVector, Nothing} = nothing,
198-
ub::Union{ctNumber, ctVector, Nothing} = nothing,
199-
label::Symbol = __constraint_label(),
201+
rg::Union{Int,OrdinalRange{Int},Nothing}=nothing,
202+
f::Union{Function,Nothing}=nothing,
203+
lb::Union{ctNumber,ctVector,Nothing}=nothing,
204+
ub::Union{ctNumber,ctVector,Nothing}=nothing,
205+
label::Symbol=__constraint_label(),
200206
)
201207

202208
# checkings: times, state and control must be set before adding constraints
203-
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall("the state must be set before adding constraints.")
204-
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall("the control must be set before adding constraints.")
205-
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall("the times must be set before adding constraints.")
209+
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall(
210+
"the state must be set before adding constraints."
211+
)
212+
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall(
213+
"the control must be set before adding constraints."
214+
)
215+
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall(
216+
"the times must be set before adding constraints."
217+
)
206218

207219
# checkings: variable must be set if using type=:variable
208220
@ensure (type != :variable || __is_variable_set(ocp)) CTBase.UnauthorizedCall(
209-
"the ocp has no variable, you cannot use constraint! function with type=:variable. If it is a mistake, please set the variable first."
221+
"the ocp has no variable, you cannot use constraint! function with type=:variable. If it is a mistake, please set the variable first.",
210222
)
211223

212224
# dimensions
@@ -221,15 +233,14 @@ function constraint!(
221233
n,
222234
m,
223235
q;
224-
rg = as_range(rg),
225-
f = f,
226-
lb = as_vector(lb),
227-
ub = as_vector(ub),
228-
label = label,
236+
rg=as_range(rg),
237+
f=f,
238+
lb=as_vector(lb),
239+
ub=as_vector(ub),
240+
label=label,
229241
)
230242
end
231243

232-
233244
as_vector(::Nothing) = nothing
234245
(as_vector(x::T)::Vector{T}) where {T<:ctNumber} = [x]
235246
as_vector(x::Vector{T}) where {T<:ctNumber} = x

src/control.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ julia> control_components(ocp)
4242
function control!(
4343
ocp::PreModel,
4444
m::Dimension,
45-
name::T1 = __control_name(),
46-
components_names::Vector{T2} = __control_components(m, string(name)),
47-
)::Nothing where {T1<:Union{String,Symbol}, T2<:Union{String,Symbol}}
45+
name::T1=__control_name(),
46+
components_names::Vector{T2}=__control_components(m, string(name)),
47+
)::Nothing where {T1<:Union{String,Symbol},T2<:Union{String,Symbol}}
4848

4949
# checkings using @ensure
50-
@ensure !__is_control_set(ocp) CTBase.UnauthorizedCall("the control has already been set.")
50+
@ensure !__is_control_set(ocp) CTBase.UnauthorizedCall(
51+
"the control has already been set."
52+
)
5153
@ensure m > 0 CTBase.IncorrectArgument("the control dimension must be greater than 0")
5254
@ensure size(components_names, 1) == m CTBase.IncorrectArgument(
5355
"the number of control names must be equal to the control dimension"
@@ -59,7 +61,6 @@ function control!(
5961
return nothing
6062
end
6163

62-
6364
# ------------------------------------------------------------------------------ #
6465
# GETTERS
6566
# ------------------------------------------------------------------------------ #

src/default.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ __matrix_dimension_storage() = 1
107107
$(TYPEDSIGNATURES)
108108
109109
"""
110-
__filename_export_import() = "solution"
110+
__filename_export_import() = "solution"

src/dual_model.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ function dual(sol::Solution, model::Model, label::Symbol)
109109

110110
# throw an exception if the label is not found
111111
throw(CTBase.IncorrectArgument("Label $label not found in the model."))
112-
113112
end
114113

115114
"""

src/dynamics.jl

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,25 @@ dynamics have already been set.
2020
Throws `CTBase.UnauthorizedCall` if called out of order or in an invalid state.
2121
"""
2222
function dynamics!(ocp::PreModel, f::Function)::Nothing
23-
24-
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall("the state must be set before the dynamics.")
25-
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall("the control must be set before the dynamics.")
26-
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall("the times must be set before the dynamics.")
27-
@ensure !__is_dynamics_set(ocp) CTBase.UnauthorizedCall("the dynamics has already been set.")
23+
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall(
24+
"the state must be set before the dynamics."
25+
)
26+
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall(
27+
"the control must be set before the dynamics."
28+
)
29+
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall(
30+
"the times must be set before the dynamics."
31+
)
32+
@ensure !__is_dynamics_set(ocp) CTBase.UnauthorizedCall(
33+
"the dynamics has already been set."
34+
)
2835

2936
# set the dynamics
3037
ocp.dynamics = f
3138

3239
return nothing
3340
end
3441

35-
3642
"""
3743
$(TYPEDSIGNATURES)
3844
@@ -67,31 +73,50 @@ julia> dynamics!(ocp, 3:3, (out, t, x, u, v) -> out .= x[3] * v[1])
6773
```
6874
"""
6975
function dynamics!(ocp::PreModel, rg::AbstractUnitRange{<:Integer}, f::Function)::Nothing
70-
71-
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall("the state must be set before the dynamics.")
72-
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall("the control must be set before the dynamics.")
73-
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall("the times must be set before the dynamics.")
74-
@ensure !__is_dynamics_complete(ocp) CTBase.UnauthorizedCall("the dynamics has already been set.")
76+
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall(
77+
"the state must be set before the dynamics."
78+
)
79+
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall(
80+
"the control must be set before the dynamics."
81+
)
82+
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall(
83+
"the times must be set before the dynamics."
84+
)
85+
@ensure !__is_dynamics_complete(ocp) CTBase.UnauthorizedCall(
86+
"the dynamics has already been set."
87+
)
7588

7689
# Check indices in rg are within valid state index bounds
7790
for i in rg
7891
if i < 1 || i > state_dimension(ocp)
79-
throw(CTBase.IncorrectArgument("index $i in the range is out of valid bounds [1, $(state_dimension(ocp))]."))
92+
throw(
93+
CTBase.IncorrectArgument(
94+
"index $i in the range is out of valid bounds [1, $(state_dimension(ocp))].",
95+
),
96+
)
8097
end
8198
end
8299

83100
# initialize dynamics container if needed
84101
if isnothing(ocp.dynamics)
85-
ocp.dynamics = Vector{Tuple{UnitRange{Int}, Function}}()
102+
ocp.dynamics = Vector{Tuple{UnitRange{Int},Function}}()
86103
elseif ocp.dynamics isa Function
87-
throw(CTBase.UnauthorizedCall("cannot add partial dynamics: dynamics already defined as a single function."))
104+
throw(
105+
CTBase.UnauthorizedCall(
106+
"cannot add partial dynamics: dynamics already defined as a single function.",
107+
),
108+
)
88109
end
89110

90111
# check that indices in rg are not already covered
91112
for (existing_range, _) in ocp.dynamics
92113
for i in rg
93114
if i in existing_range
94-
throw(CTBase.UnauthorizedCall("index $i in the range already has assigned dynamics."))
115+
throw(
116+
CTBase.UnauthorizedCall(
117+
"index $i in the range already has assigned dynamics."
118+
),
119+
)
95120
end
96121
end
97122
end
@@ -102,7 +127,6 @@ function dynamics!(ocp::PreModel, rg::AbstractUnitRange{<:Integer}, f::Function)
102127
return nothing
103128
end
104129

105-
106130
"""
107131
$(TYPEDSIGNATURES)
108132
@@ -171,7 +195,9 @@ julia> dyn!(val, 0.0, [1.0, 2.0, 3.0], [0.5, 0.5], 2.0)
171195
julia> println(val) # prints [1.5, 2.5, 6.0]
172196
```
173197
"""
174-
function __build_dynamics_from_parts(parts::Vector{<:Tuple{<:AbstractUnitRange{<:Integer}, <:Function}})::Function
198+
function __build_dynamics_from_parts(
199+
parts::Vector{<:Tuple{<:AbstractUnitRange{<:Integer},<:Function}}
200+
)::Function
175201
function dyn!(val, t, x, u, v)
176202
for (rg, f!) in parts
177203
f!(@view(val[rg]), t, x, u, v)
@@ -180,4 +206,3 @@ function __build_dynamics_from_parts(parts::Vector{<:Tuple{<:AbstractUnitRange{<
180206
end
181207
return dyn!
182208
end
183-

0 commit comments

Comments
 (0)