-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.jl
270 lines (219 loc) · 7.06 KB
/
interface.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
AbstractSolver
Abstract supertype for all direct integration methods.
"""
abstract type AbstractSolver end
"""
step_size(alg::AbstractSolver)
Return the step size of the given algorithm.
### Input
- `alg` -- algorithm
### Output
The step size of the algorithm, or `nothing` if the step-size is not fixed.
"""
step_size(alg::AbstractSolver) = alg.Δt
"""
AbstractSolution
Abstract supertype that holds the solution of a numerical integration.
"""
abstract type AbstractSolution end
"""
Solution{T<:AbstractSolver, UT, VT, AT} <: AbstractSolution
### Fields
- `alg` -- algorithm used in the integration
- `U` -- displacements
- `U′` -- velocities
- `U′′` -- accelerations
- `t` -- vector of time values
"""
struct Solution{T<:AbstractSolver,UT,VT,AT,ST} <: AbstractSolution
alg::T
U::UT
U′::VT
U′′::AT
t::ST
end
# constructor with missing fields
Solution(alg, U, t) = Solution(alg, U, nothing, nothing, t)
"""
dim(sol::Solution)
Return the ambient dimension of the state space of the solution.
"""
dim(sol::Solution) = length(first(sol.U))
"""
displacements(sol::Solution)
Return the vector of displacements of the given solution.
"""
displacements(sol::Solution) = sol.U
"""
displacements(sol::Solution, i::Int)
Return the vector of displacements of the given solution along coordinate `i`.
"""
function displacements(sol::Solution, i::Int)
1 ≤ i ≤ dim(sol) ||
throw(ArgumentError("expected the coordinate to be between 1 and $(dim(sol)), got $i"))
U = displacements(sol)
return [u[i] for u in U]
end
"""
velocities(sol::Solution)
Return the vector of velocities of the given solution.
"""
velocities(sol::Solution) = sol.U′
"""
velocities(sol::Solution, i::Int)
Return the vector of velocities of the given solution along coordinate `i`.
"""
function velocities(sol::Solution, i::Int)
1 ≤ i ≤ dim(sol) ||
throw(ArgumentError("expected the coordinate to be between 1 and $(dim(sol)), got $i"))
U′ = velocities(sol)
return [u′[i] for u′ in U′]
end
"""
accelerations(sol::Solution)
Return the vector of accelerations of the given solution.
"""
accelerations(sol::Solution) = sol.U′′
"""
accelerations(sol::Solution, i::Int)
Return the vector of accelerations of the given solution along coordinate `i`.
"""
function accelerations(sol::Solution, i::Int)
1 ≤ i ≤ dim(sol) ||
throw(ArgumentError("expected the coordinate to be between 1 and $(dim(sol)), got $i"))
U′′ = accelerations(sol)
return [u′′[i] for u′′ in U′′]
end
"""
times(sol::Solution)
Return the vector of times of the given solution.
"""
times(sol::Solution) = sol.t
struct SolutionExtrema{T<:AbstractSolver,VT,ST,DT,RT} <: AbstractSolution
alg::T
U_min::VT
U_max::VT
U′_min::VT
U′_max::VT
idx::Int
hist::DT
times::RT
t::ST
end
# ===============
# Problem types
# ===============
abstract type AbstractProblem end
struct StructuralDynamicsProblem{T,PT} <: AbstractProblem
alg::T
ivp::PT
NSTEPS::Int
end
"""
solve(ivp::InitialValueProblem, alg::AbstractSolver, args..; kwargs...)
Solve an initial-value problem.
### Input
- `ivp` -- initial-value problem
- `alg` -- algorithm
### Output
A solution structure (`Solution`) that holds the result and the algorithm used
to obtain it.
"""
function solve(ivp::IVP{<:AbstractContinuousSystem},
alg::AbstractSolver,
args...;
kwargs...,)
sdprob = init(ivp, alg, args...; kwargs...)
return _solve(sdprob.alg, sdprob.ivp, sdprob.NSTEPS; kwargs...)
end
const SOACS = SecondOrderConstrainedLinearControlContinuousSystem
const SOCLCCS = SecondOrderConstrainedLinearControlContinuousSystem
function init(ivp::InitialValueProblem{ST,XT},
alg::AbstractSolver;
kwargs...,) where {VT,
ST, # FIXME restrict to SOACS and SOCLCCS
XT<:Tuple{VT,VT}}
if haskey(kwargs, :NSTEPS)
NSTEPS = kwargs[:NSTEPS]
elseif haskey(kwargs, :T) || haskey(kwargs, :finalTime)
Δt = step_size(alg)
if haskey(kwargs, :T)
NSTEPS = ceil(Int, kwargs[:T] / Δt)
else
haskey(kwargs, :finalTime)
NSTEPS = ceil(Int, kwargs[:finalTime] / Δt)
end
elseif haskey(kwargs, :tspan)
Δt = step_size(alg)
tsp = kwargs[:tspan]
@assert iszero(tsp[1]) "expected that the initial time is zero, got $(tsp[1])"
T = tsp[2]
NSTEPS = ceil(Int, T / Δt)
else
throw(ArgumentError("please define `NSTEPS`, `T`, `finalTime` or `tspan`"))
end
return StructuralDynamicsProblem(alg, ivp, NSTEPS)
end
# lazily extend the vector to the required number of steps
function _init_input(R::AbstractVector{N}, IMAX) where {N<:Number}
return Fill(R, IMAX)
end
# no-op, checking that the number of forcing terms has the correct length
function _init_input(R::AbstractVector{VT}, IMAX) where {N,VT<:AbstractVector{N}}
@assert length(R) == IMAX "expected the forcing term to be an array of length $IMAX, got $(length(R))"
return R
end
# TODO dispatch on B (eg. IdentityMultiple)
function _init_input(R::AbstractVector{VT},
B::AbstractMatrix,
IMAX) where {N,VT<:AbstractVector{N}}
@assert length(R) == IMAX "expected the forcing term to be an array of length $IMAX, got $(length(R))"
return [B * Ri for Ri in R]
end
# unwrap a second order system into its each component
function _unwrap(sys::SecondOrderAffineContinuousSystem, IMAX)
M = mass_matrix(sys)
C = viscosity_matrix(sys)
K = stiffness_matrix(sys)
R = affine_term(sys)
R = _init_input(R, IMAX)
return M, C, K, R
end
function _unwrap(sys::SecondOrderConstrainedLinearControlContinuousSystem, IMAX)
M = mass_matrix(sys)
C = viscosity_matrix(sys)
K = stiffness_matrix(sys)
R = inputset(sys)
B = input_matrix(sys)
R = _init_input(R, B, IMAX)
return M, C, K, R
end
# ================
# Plot recipes
# ================
function _check_vars(vars)
if vars == nothing
throw(ArgumentError("default ploting variables not implemented yet; you need " *
"to pass the `vars=(...)` option, e.g. `vars=(0, 1)` to plot variable with " *
"index 1 vs. time, or `vars=(1, 2)` to plot variable with index 2 vs. variable with index 1`"))
end
D = length(vars)
@assert (D == 1) || (D == 2) "can only plot in one or two dimensions, " *
"but received $D variable indices where `vars = ` $vars"
end
# plot displacements of the solution for the given vars tuple, eg. vars=(0, 1) for x1(t) vs t
@recipe function plot_solution(sol::Solution; vars=nothing, func=displacements)
seriestype --> :path # :scatter
markershape --> :circle
_check_vars(vars)
if vars[1] == 0 && vars[2] != 0
x = times(sol)
y = func(sol, vars[2])
x, y
else
x = func(sol, vars[1])
y = func(sol, vars[2])
end
return x, y
end