-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvector_field.jl
243 lines (190 loc) · 6.8 KB
/
vector_field.jl
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# =============================
# Vector Field for continuous system
# =============================
"""
vector_field(system::ContinuousIdentitySystem, x::AbstractVector)
Return the vector field state of a `ContinuousIdentitySystem`.
### Input
- `system` -- `ContinuousIdentitySystem`
- `x` -- state (it should be any vector type)
### Output
A zeros vector of dimension `statedim`.
"""
function vector_field(system::ContinuousIdentitySystem, x::AbstractVector)
!_is_conformable_state(system, x) && _argument_error(:x)
return zeros(statedim(system))
end
"""
vector_field(system::ConstrainedContinuousIdentitySystem, x::AbstractVector;
[check_constraints]=true)
Return the vector field state of a `ConstrainedContinuousIdentitySystem`.
### Input
- `system` -- `ConstrainedContinuousIdentitySystem`
- `x` -- state (it should be any vector type)
- `check_constraints` -- (optional, default: `true`) check if the state belongs to
the state set
### Output
A zeros vector of dimension `statedim`.
"""
function vector_field(system::ConstrainedContinuousIdentitySystem, x::AbstractVector;
check_constraints::Bool=true)
!_is_conformable_state(system, x) && _argument_error(:x)
if check_constraints
!_in_stateset(system, x) && _argument_error(:x,:X)
end
return zeros(statedim(system))
end
"""
vector_field(system::AbstractContinuousSystem, x::AbstractVector;
[check_constraints]=true)
Return the vector field state of an `AbstractContinuousSystem`.
### Input
- `system` -- `AbstractContinuousSystem`
- `x` -- state (it should be any vector type)
- `check_constraints` -- (optional, default: `true`) check if the state belongs to
the state set
### Output
The vector field of the system at state `x`.
"""
vector_field(system::AbstractContinuousSystem, x::AbstractVector; kwargs...) =
_instantiate(system, x; kwargs...)
"""
vector_field(system::AbstractContinuousSystem, x::AbstractVector, u::AbstractVector;
[check_constraints]=true)
Return the vector field state of an `AbstractContinuousSystem`.
### Input
- `system` -- `AbstractContinuousSystem`
- `x` -- state (it should be any vector type)
- `u` -- input (it should be any vector type) or noise, if `system`
is not controlled
- `check_constraints` -- (optional, default: `true`) check if the state belongs to
the state set
### Output
The vector field of the system at state `x` and applying input `u`.
### Notes
If the system is not controlled but noisy, the input `u` is interpreted as noise.
"""
vector_field(system::AbstractContinuousSystem, x::AbstractVector, u::AbstractVector; kwargs...) =
_instantiate(system, x, u; kwargs...)
"""
vector_field(system::AbstractContinuousSystem,
x::AbstractVector, u::AbstractVector, w::AbstractVector; [check_constraints]=true)
Return the vector field state of an `AbstractContinuousSystem`.
### Input
- `system` -- `AbstractContinuousSystem`
- `x` -- state (it should be any vector type)
- `u` -- input (it should be any vector type)
- `w` -- noise (it should be any vector type)
- `check_constraints` -- (optional, default: `true`) check if the state belongs to
the state set
### Output
The vector field of the system at state `x` and applying input `u` and noise `w`.
"""
vector_field(system::AbstractContinuousSystem, x::AbstractVector, u::AbstractVector, w::AbstractVector; kwargs...) =
_instantiate(system, x, u, w; kwargs...)
# =============================
# VectorField type for continuous system
# =============================
"""
VectorField{T<:Function}
Type that computes the vector field of an `AbstractContinuousSystem`.
### Fields
- `field` -- function for calculating the vector field
"""
struct VectorField{T<:Function}
field::T
end
# function-like evaluation
@inline function (V::VectorField)(args...)
evaluate(V, args...)
end
function evaluate(V::VectorField, args...)
return V.field(args...)
end
"""
VectorField(sys::AbstractContinuousSystem)
Constructor for creating a `VectorField` from an `AbstractContinuousSystem`.
### Input
- `sys` -- `AbstractContinuousSystem`
### Ouptut
The `VectorField` for the continuous system `sys`.
"""
function VectorField(sys::AbstractContinuousSystem)
if inputdim(sys) == 0 && noisedim(sys) == 0
field = (x) -> vector_field(sys, x)
elseif inputdim(sys) == 0 || noisedim(sys) == 0
field = (x, u) -> vector_field(sys, x, u)
else
field = (x, u, w) -> vector_field(sys, x, u, w)
end
return VectorField(field)
end
function _position_value_list(V::VectorField, grid, dimx, dimy)
X, Y = grid
x1 = X[1]
y1 = Y[1]
N_POS = typeof(x1)
N_VAL = typeof(V([x1, y1])[dimx])
m = length(X) * length(Y)
x = Vector{N_POS}(undef, m) # x coordinates of each
y = similar(x)
vx = Vector{N_VAL}(undef, m)
vy = similar(vx)
k = 1
max_entry = N_VAL(-Inf)
for xi in X
for yj in Y
x[k] = xi
y[k] = yj
val = V([xi, yj])
vx_k = val[dimx]
vy_k = val[dimy]
max_entry = max(max_entry, abs(vx_k), abs(vy_k))
vx[k] = vx_k
vy[k] = vy_k
k += 1
end
end
# normalize
if max_entry > zero(N_VAL)
step_x = (maximum(X) - minimum(X)) / length(X)
step_y = (maximum(Y) - minimum(Y)) / length(Y)
max_entry_x = max_entry / step_x
max_entry_y = max_entry / step_y
max_entry = max(max_entry_x, max_entry_y)
for k in 1:m
vx[k] /= max_entry
vy[k] /= max_entry
end
end
# filter out (0, 0) entries
for k in m:-1:1
if vx[k] == vy[k] == zero(N_VAL)
deleteat!(x, k)
deleteat!(y, k)
deleteat!(vx, k)
deleteat!(vy, k)
end
end
# center arrows in the grid points
for k in 1:length(x)
x[k] -= vx[k] / 2
y[k] -= vy[k] / 2
end
return x, y, vx, vy
end
### plot recipe
# arguments:
# - grid_points: pair containing the x and y coordinates of the grid points
# example: the pair `([-1, 0, 1], [2, 3])` represents the grid of points
# `[-1, 2], [0, 2], [1, 2], [-1, 3], [0, 3], [1, 3]`
# - dims: the two dimensions to plot
@recipe function plot(V::VectorField;
grid_points=[range(-3, stop=3, length=21),
range(-3, stop=3, length=21)],
dims=[1, 2])
seriestype := :quiver
x, y, vx, vy = _position_value_list(V, grid_points, dims[1], dims[2])
quiver := (vx, vy)
(x, y)
end