-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.jl
More file actions
141 lines (112 loc) · 2.9 KB
/
state.jl
File metadata and controls
141 lines (112 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
$(TYPEDSIGNATURES)
Define the state dimension and possibly the names of each component.
!!! note
You must use state! only once to set the state dimension.
# Examples
```@example
julia> state!(ocp, 1)
julia> state_dimension(ocp)
1
julia> state_components(ocp)
["x"]
julia> state!(ocp, 1, "y")
julia> state_dimension(ocp)
1
julia> state_components(ocp)
["y"]
julia> state!(ocp, 2)
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["x₁", "x₂"]
julia> state!(ocp, 2, :y)
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["y₁", "y₂"]
julia> state!(ocp, 2, "y")
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["y₁", "y₂"]
julia> state!(ocp, 2, "y", ["u", "v"])
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["u", "v"]
julia> state!(ocp, 2, "y", [:u, :v])
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["u", "v"]
```
"""
function state!(
ocp::PreModel,
n::Dimension,
name::T1=__state_name(),
components_names::Vector{T2}=__state_components(n, string(name)),
)::Nothing where {T1<:Union{String,Symbol},T2<:Union{String,Symbol}}
# checks
@ensure !__is_state_set(ocp) CTBase.UnauthorizedCall("the state has already been set.")
@ensure n > 0 CTBase.IncorrectArgument("the state dimension must be greater than 0")
@ensure size(components_names, 1) == n CTBase.IncorrectArgument(
"the number of state names must be equal to the state dimension"
)
# set the state
ocp.state = StateModel(string(name), string.(components_names))
return nothing
end
# ------------------------------------------------------------------------------ #
# GETTERS
# ------------------------------------------------------------------------------ #
"""
$(TYPEDSIGNATURES)
Get the name of the state from the state model.
"""
function name(model::StateModel)::String
return model.name
end
"""
$(TYPEDSIGNATURES)
Get the components names of the state from the state model.
"""
function components(model::StateModel)::Vector{String}
return model.components
end
"""
$(TYPEDSIGNATURES)
Get the dimension of the state from the state model.
"""
function dimension(model::StateModel)::Dimension
return length(components(model))
end
"""
$(TYPEDSIGNATURES)
Get the name of the state from the state model solution.
"""
function name(model::StateModelSolution)::String
return model.name
end
"""
$(TYPEDSIGNATURES)
Get the components names of the state from the state model solution.
"""
function components(model::StateModelSolution)::Vector{String}
return model.components
end
"""
$(TYPEDSIGNATURES)
Get the dimension of the state from the state model solution.
"""
function dimension(model::StateModelSolution)::Dimension
return length(components(model))
end
"""
$(TYPEDSIGNATURES)
Get the state function from the state model solution.
"""
function value(model::StateModelSolution{TS})::TS where {TS<:Function}
return model.value
end