This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
interface.jl
392 lines (326 loc) · 9.7 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#### Balance Law Interface
"""
abstract type BalanceLaw end
An abstract type representing a PDE balance law of the form:
```math
\\frac{dq}{dt} = \\nabla \\cdot F_1(q, a, t) + \\nabla \\cdot F_2(q, \\nabla g, h, a, t) + S(q, \\nabla g, a, t)
```
where:
- ``q`` is the prognostic state,
- ``a`` is the auxiliary state,
- ``g = G(q, a, t)`` is the gradient state (variables of which we compute the
gradient),
- ``h`` is the hyperdiffusive state.
Subtypes of `BalanceLaw` should define the following interfaces:
- [`vars_state`](@ref) to define the prognostic, auxiliary and intermediate
variables.
- [`flux_first_order!`](@ref) to compute ``F_1``
- [`flux_second_order!`](@ref) to compute ``F_2``
- [`source!`](@ref) to compute ``S``
If `vars(bl, ::GradientFlux, FT)` is non-empty, then the following should be
defined:
- [`compute_gradient_argument!`](@ref) to compute ``G``
- [`compute_gradient_flux!`](@ref) is a linear transformation of ``\\nabla g``
If `vars(bl, ::Hyperdiffusive, FT)` is non-empty, then the following should be
defined:
- [`transform_post_gradient_laplacian!`](@ref)
Additional functions:
- [`wavespeed`](@ref) if using the Rusanov numerical flux.
- [`boundary_state!`](@ref) if using non-periodic boundary conditions.
"""
abstract type BalanceLaw end
"""
BalanceLaws.vars_state(::BL, ::AbstractStateType, FT)
Defines the state variables of a [`BalanceLaw`](@ref) subtype `BL` with floating
point type `FT`.
For each [`AbstractStateType`](@ref), this should return a `NamedTuple` type,
with element type either `FT`, an `SArray` with element type `FT` or another
`NamedTuple` satisfying the same property.
For convenience, we recommend using the [`VariableTemplates.@vars`](@ref) macro.
# Example
```julia
struct MyBalanceLaw <: BalanceLaw end
BalanceLaws.vars_state(::MyBalanceLaw, ::Prognostic, FT) =
@vars(x::FT, y::SVector{3, FT})
BalanceLaws.vars_state(::MyBalanceLaw, ::Auxiliary, FT) =
@vars(components::@vars(a::FT, b::FT))
```
"""
function vars_state end
# Fallback: no variables
vars_state(::BalanceLaw, ::AbstractStateType, FT) = @vars()
"""
init_state_prognostic!(
::BL,
state_prognostic::Vars,
state_auxiliary::Vars,
coords,
args...,
)
Sets the initial state of the prognostic variables `state_prognostic` at each
node for a [`BalanceLaw`](@ref) subtype `BL`.
"""
function init_state_prognostic! end
# TODO: make these functions consistent with init_state_prognostic!
"""
nodal_init_state_auxiliary!(::BL, state_auxiliary, state_temporary, geom)
Sets the initial state of the auxiliary variables `state_auxiliary` at each
node for a [`BalanceLaw`](@ref) subtype `BL`.
See also [`init_state_auxiliary!`](@ref).
"""
function nodal_init_state_auxiliary!(m::BalanceLaw, aux, tmp, geom)
end
"""
init_state_auxiliary!(
::BL,
statearray_auxiliary,
geom::LocalGeometry,
)
Sets the initial state of the auxiliary variables `state_auxiliary` at each node
for a [`BalanceLaw`](@ref) subtype `BL`. By default this calls
[`nodal_init_state_auxiliary!`](@ref).
"""
function init_state_auxiliary!(
balance_law::BalanceLaw,
statearray_auxiliary,
grid,
direction,
)
init_state_auxiliary!(
balance_law,
nodal_init_state_auxiliary!,
statearray_auxiliary,
grid,
direction,
)
end
"""
flux_first_order!(
::BL,
flux::Grad,
state_prognostic::Vars,
state_auxiliary::Vars,
t::Real,
direction
)
Sets the first-order (hyperbolic) `flux` terms for a [`BalanceLaw`](@ref) subtype `BL`.
"""
function flux_first_order! end
"""
flux_second_order!(
::BL,
flux::Grad,
state_prognostic::Vars,
state_gradient_flux::Vars,
hyperdiffusive::Vars,
state_auxiliary::Vars,
t::Real
)
Sets second-order (parabolic) `flux` terms for a [`BalanceLaw`](@ref) subtype `BL`.
"""
function flux_second_order! end
"""
source!(
::BL,
source::Vars,
state_prognostic::Vars,
diffusive::Vars,
state_auxiliary::Vars,
t::Real
)
Compute non-conservative source terms for a [`BalanceLaw`](@ref) subtype `BL`.
"""
function source! end
"""
compute_gradient_argument!(
::BL,
transformstate::Vars,
state_prognostic::Vars,
state_auxiliary::Vars,
t::Real
)
Transformation of state variables `state_prognostic` to variables
`transformstate` of which gradients are computed for a [`BalanceLaw`](@ref)
subtype `BL`.
"""
function compute_gradient_argument! end
"""
compute_gradient_flux!(
::BL,
state_gradient_flux::Vars,
∇transformstate::Grad,
state_auxiliary::Vars,
t::Real
)
Transformation of gradients to the diffusive variables for a
[`BalanceLaw`](@ref) subtype `BL`. This should be a linear function of
`∇transformstate`
"""
function compute_gradient_flux! end
"""
transform_post_gradient_laplacian!(
::BL,
Qhypervisc_div::Vars,
∇Δtransformstate::Grad,
state_auxiliary::Vars,
t::Real
)
Transformation of Laplacian gradients to the hyperdiffusive variables for a
[`BalanceLaw`](@ref) subtype `BL`.
"""
function transform_post_gradient_laplacian! end
"""
wavespeed(
::BL,
n⁻,
state_prognostic::Vars,
state_auxiliary::Vars,
t::Real,
direction
)
Wavespeed in the direction `n⁻` for a [`BalanceLaw`](@ref) subtype `BL`. This is
required to be defined if using a `RusanovNumericalFlux` numerical flux.
"""
function wavespeed end
"""
boundary_state!(
::NumericalFluxGradient,
::L,
state_prognostic⁺::Vars,
state_auxiliary⁺::Vars,
normal⁻,
state_prognostic⁻::Vars,
state_auxiliary⁻::Vars,
bctype,
t
)
boundary_state!(
::NumericalFluxFirstOrder,
::L,
state_prognostic⁺::Vars,
state_auxiliary⁺::Vars,
normal⁻,
state_prognostic⁻::Vars,
state_auxiliary⁻::Vars,
bctype,
t
)
boundary_state!(
::NumericalFluxSecondOrder,
::L,
state_prognostic⁺::Vars,
state_gradient_flux⁺::Vars,
state_auxiliary⁺:
Vars, normal⁻,
state_prognostic⁻::Vars,
state_gradient_flux⁻::Vars,
state_auxiliary⁻::Vars,
bctype,
t
)
Apply boundary conditions for
- `NumericalFluxGradient` numerical flux (internal method)
- `NumericalFluxFirstOrder` first-order unknowns
- `NumericalFluxSecondOrder` second-order unknowns
"""
function boundary_state! end
"""
update_auxiliary_state!(
dg::DGModel,
m::BalanceLaw,
statearray_aux,
t::Real,
elems::UnitRange,
[diffusive=false]
)
Hook to update the auxiliary state variables before calling any other functions.
By default, this calls [`nodal_update_auxiliary_state!`](@ref) at each node.
If `diffusive=true`, then `state_gradflux` is also passed to
`nodal_update_auxiliary_state!`.
"""
function update_auxiliary_state!(
dg,
balance_law::BalanceLaw,
state_prognostic,
t,
elems,
diffusive = false,
)
update_auxiliary_state!(
nodal_update_auxiliary_state!,
dg,
balance_law,
state_prognostic,
t,
elems;
diffusive = diffusive,
)
end
"""
nodal_update_auxiliary_state!(::BL, state_prognostic, state_auxiliary, [state_gradflux,] t)
Update the auxiliary state variables at each node for a [`BalanceLaw`](@ref)
subtype `BL`. By default it does nothing.
Called by [`update_auxiliary_state!`](@ref).
"""
function nodal_update_auxiliary_state!(args...)
nothing
end
"""
update_auxiliary_state_gradient!(
dg::DGModel,
m::BalanceLaw,
statearray_aux,
t::Real,
elems::UnitRange,
[diffusive=false]
)
Hook to update the auxiliary state variables after the gradient computation.
By default, this calls nothing.
If `diffusive=true`, then `state_gradflux` is also passed to
`nodal_update_auxiliary_state!`.
"""
function update_auxiliary_state_gradient! end
# upward integrals
"""
integral_load_auxiliary_state!(::BL, integrand, state_prognostic, state_aux)
Specify variables `integrand` which will have their upward integrals computed.
See also [`UpwardIntegrals`](@ref)
"""
function integral_load_auxiliary_state! end
"""
integral_set_auxiliary_state!(::BL, state_aux, integral)
Update auxiliary variables based on the upward integral `integral` defined in
[`integral_load_auxiliary_state!`](@ref).
"""
function integral_set_auxiliary_state! end
"""
indefinite_stack_integral!
Compute indefinite integral along stack.
"""
function indefinite_stack_integral! end
# downward integrals
"""
reverse_integral_load_auxiliary_state!(::BL, integrand, state_prognostic, state_aux)
Specify variables `integrand` which will have their downward integrals computed.
See also [`DownwardIntegrals`](@ref)
"""
function reverse_integral_load_auxiliary_state! end
"""
reverse_integral_set_auxiliary_state!(::BL, state_aux, integral)
Update auxiliary variables based on the downward integral `integral` defined in
[`reverse_integral_load_auxiliary_state!`](@ref).
"""
function reverse_integral_set_auxiliary_state! end
"""
reverse_indefinite_stack_integral!
Compute reverse indefinite integral along stack.
"""
function reverse_indefinite_stack_integral! end
# Internal methods
number_states(m::BalanceLaw, st::AbstractStateType, FT = Int) =
varsize(vars_state(m, st, FT))
### split explicit functions
function initialize_states! end
function tendency_from_slow_to_fast! end
function cummulate_fast_solution! end
function reconcile_from_fast_to_slow! end