-
Notifications
You must be signed in to change notification settings - Fork 98
/
Tableaus.jl
336 lines (273 loc) · 7.4 KB
/
Tableaus.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
###############
# TableauType #
###############
"""
abstract type TableauType <: GridapType end
Trait that indicates whether a tableau is explicit, implicit or
implicit-explicit.
"""
abstract type TableauType <: GridapType end
"""
struct ExplicitTableau <: TableauType end
Tableau whose matrix is strictly lower triangular.
"""
struct ExplicitTableau <: TableauType end
"""
abstract type ImplicitTableau <: TableauType end
Tableau whose matrix has at least one nonzero coefficient outside its strict
lower triangular part.
"""
abstract type ImplicitTableau <: TableauType end
"""
struct DiagonallyImplicitTableau <: ImplicitTableau end
Tableau whose matrix is lower triangular, with at least one nonzero diagonal
coefficient.
"""
struct DiagonallyImplicitTableau <: ImplicitTableau end
"""
struct FullyImplicitTableau <: ImplicitTableau end
Tableau whose matrix has at least one nonzero coefficient in its strict upper
triangular part.
"""
struct FullyImplicitTableau <: ImplicitTableau end
"""
struct ImplicitExplicitTableau <: ImplicitTableau end
Pair of implicit and explicit tableaus that form a valid implicit-explicit
scheme.
"""
struct ImplicitExplicitTableau <: TableauType end
###################
# AbstractTableau #
###################
"""
abstract type AbstractTableau{T} <: GridapType end
Type that stores the Butcher tableau corresponding to a Runge-Kutta scheme.
"""
abstract type AbstractTableau{T<:TableauType} <: GridapType end
"""
TableauType(::AbstractTableau) -> TableauType
Return the `TableauType` of the tableau.
"""
TableauType(::AbstractTableau{T}) where {T} = T
"""
get_matrix(tableau::AbstractTableau) -> AbstractMatrix
Return the matrix of the tableau.
"""
function Algebra.get_matrix(tableau::AbstractTableau)
@abstractmethod
end
"""
get_weights(tableau::AbstractTableau) -> AbstractVector
Return the weights of the tableau.
"""
function ReferenceFEs.get_weights(tableau::AbstractTableau)
@abstractmethod
end
"""
get_nodes(tableau::AbstractTableau) -> AbstractVector
Return the nodes of the tableau.
"""
function ReferenceFEs.get_nodes(tableau::AbstractTableau)
@abstractmethod
end
"""
get_order(tableau::AbstractTableau) -> Integer
Return the order of the scheme corresponding to the tableau.
"""
function Polynomials.get_order(tableau::AbstractTableau)
@abstractmethod
end
##################
# GenericTableau #
##################
"""
struct GenericTableau <: AbstractTableau end
Generic type that stores any type of Butcher tableau.
"""
struct GenericTableau{T<:TableauType} <: AbstractTableau{T}
matrix::Matrix
weights::Vector
nodes::Vector
order::Integer
function GenericTableau(matrix, weights, order)
nodes = reshape(sum(matrix, dims=2), size(matrix, 1))
T = _tableau_type(matrix)
new{T}(matrix, weights, nodes, order)
end
end
function Algebra.get_matrix(tableau::GenericTableau)
tableau.matrix
end
function ReferenceFEs.get_weights(tableau::GenericTableau)
tableau.weights
end
function ReferenceFEs.get_nodes(tableau::GenericTableau)
tableau.nodes
end
function Polynomials.get_order(tableau::GenericTableau)
tableau.order
end
function _tableau_type(matrix::Matrix)
T = ExplicitTableau
n = size(matrix, 1)
for i in 1:n
if any(j -> !iszero(matrix[i, j]), i+1:n)
T = FullyImplicitTableau
break
elseif !iszero(matrix[i, i])
T = DiagonallyImplicitTableau
end
end
T
end
###################
# EmbeddedTableau #
###################
"""
struct EmbeddedTableau <: AbstractTableau end
Generic type that stores any type of embedded Butcher tableau.
"""
struct EmbeddedTableau{T} <: AbstractTableau{T}
tableau::AbstractTableau{T}
emb_weights::Vector
emb_order::Integer
end
function Algebra.get_matrix(tableau::EmbeddedTableau)
get_matrix(tableau.tableau)
end
function ReferenceFEs.get_weights(tableau::EmbeddedTableau)
get_weights(tableau.tableau)
end
function ReferenceFEs.get_nodes(tableau::EmbeddedTableau)
get_nodes(tableau.tableau)
end
function Polynomials.get_order(tableau::EmbeddedTableau)
get_order(tableau.tableau)
end
"""
get_embedded_weights(tableau::EmbeddedTableau) -> AbstractVector
Return the embedded weight of the tableau.
"""
function get_embedded_weights(tableau::EmbeddedTableau)
tableau.emb_weights
end
"""
get_embedded_order(tableau::EmbeddedTableau) -> Integer
Return the embedded order of the tableau.
"""
function get_embedded_order(tableau::EmbeddedTableau)
tableau.emb_order
end
###############
# IMEXTableau #
###############
"""
struct IMEXTableau <: AbstractTableau end
Generic type that stores any type of implicit-explicit pair of Butcher tableaus,
that form a valid IMEX scheme.
"""
struct IMEXTableau <: AbstractTableau{ImplicitExplicitTableau}
im_tableau::AbstractTableau{<:ImplicitTableau}
ex_tableau::AbstractTableau{ExplicitTableau}
imex_order::Integer
is_padded::Bool
function IMEXTableau(im_tableau, ex_tableau, imex_order)
Tim = TableauType(im_tableau)
Tex = TableauType(ex_tableau)
msg = """Invalid IMEX tableau:
the first tableau must be implicit and the second must be explicit."""
@check (Tim <: ImplicitTableau && Tex == ExplicitTableau) msg
msg = """Invalid IMEX tableau:
the nodes of the implicit and explicit tableaus must coincide."""
@check isapprox(get_nodes(im_tableau), get_nodes(ex_tableau)) msg
is_padded = _is_padded(im_tableau)
new(im_tableau, ex_tableau, imex_order, is_padded)
end
end
function Polynomials.get_order(tableau::IMEXTableau)
tableau.imex_order
end
function get_imex_tableaus(tableau::IMEXTableau)
(tableau.im_tableau, tableau.ex_tableau)
end
function is_padded(tableau::IMEXTableau)
tableau.is_padded
end
function _is_padded(tableau::AbstractTableau)
A = get_matrix(tableau)
b = get_matrix(tableau)
iszero(b[1]) && all(i -> iszero(A[i, 1]), axes(A, 1))
end
############################
# Concrete implementations #
############################
"""
abstract type TableauName <: GridapType end
Name of a Butcher tableau.
"""
abstract type TableauName <: GridapType end
"""
ButcherTableau(name::TableauName, type::Type) -> AbtractTableau
Builds the Butcher tableau corresponding to a `TableauName`.
"""
function ButcherTableau(name::TableauName, type::Type=Float64)
@abstractmethod
end
function ButcherTableau(name::Symbol, type::Type=Float64)
eval(:(ButcherTableau($name(), $type)))
end
##################
# Import schemes #
##################
include("TableausEX.jl")
include("TableausDIM.jl")
include("TableausIMEX.jl")
const available_tableaus = [
:EXRK_Euler_1_1,
:EXRK_Midpoint_2_2,
:EXRK_SSP_2_2,
:EXRK_Heun_2_2,
:EXRK_Ralston_2_2,
:EXRK_Kutta_3_3,
:EXRK_Heun_3_3,
:EXRK_Wray_3_3,
:EXRK_VanDerHouwen_3_3,
:EXRK_Ralston_3_3,
:EXRK_SSP_3_3,
:EXRK_SSP_3_2,
:EXRK_Fehlberg_3_2,
:EXRK_RungeKutta_4_4,
:EXRK_Simpson_4_4,
:EXRK_Ralston_4_4,
:EXRK_SSP_4_3,
:EXRK_BogackiShampine_4_3,
:SDIRK_Euler_1_1,
:SDIRK_Midpoint_1_2,
:DIRK_CrankNicolson_2_2,
:SDIRK_QinZhang_2_2,
:DIRK_LobattoIIIA_2_2,
:DIRK_RadauI_2_3,
:DIRK_RadauII_2_3,
:SDIRK_LobattoIIIC_2_2,
:SDIRK_2_2,
:SDIRK_SSP_2_3,
:SDIRK_Crouzeix_2_3,
:SDIRK_3_2,
:DIRK_TRBDF_3_2,
:DIRK_TRX_3_2,
:SDIRK_3_3,
:SDIRK_Crouzeix_3_4,
:SDIRK_Norsett_3_4,
:DIRK_LobattoIIIC_3_4,
:SDIRK_4_3,
]
const available_imex_tableaus = [
:IMEXRK_1_1_1,
:IMEXRK_1_2_1,
:IMEXRK_1_2_2,
:IMEXRK_2_2_2,
:IMEXRK_2_3_2,
:IMEXRK_2_3_3,
:IMEXRK_3_4_3,
:IMEXRK_4_4_3,
]