/
weno_reconstruction.jl
177 lines (146 loc) · 7.23 KB
/
weno_reconstruction.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
#####
##### Weighted Essentially Non-Oscillatory (WENO) advection scheme
#####
struct WENO{N, FT, XT, YT, ZT, WF, PP, CA, SI} <: AbstractUpwindBiasedAdvectionScheme{N, FT}
"Coefficient for ENO reconstruction on x-faces"
coeff_xᶠᵃᵃ::XT
"Coefficient for ENO reconstruction on x-centers"
coeff_xᶜᵃᵃ::XT
"Coefficient for ENO reconstruction on y-faces"
coeff_yᵃᶠᵃ::YT
"Coefficient for ENO reconstruction on y-centers"
coeff_yᵃᶜᵃ::YT
"Coefficient for ENO reconstruction on z-faces"
coeff_zᵃᵃᶠ::ZT
"Coefficient for ENO reconstruction on z-centers"
coeff_zᵃᵃᶜ::ZT
"Bounds for maximum-principle-satisfying WENO scheme"
bounds :: PP
"Advection scheme used near boundaries"
buffer_scheme :: CA
"Reconstruction scheme used for symmetric interpolation"
advecting_velocity_scheme :: SI
function WENO{N, FT, WF}(coeff_xᶠᵃᵃ::XT, coeff_xᶜᵃᵃ::XT,
coeff_yᵃᶠᵃ::YT, coeff_yᵃᶜᵃ::YT,
coeff_zᵃᵃᶠ::ZT, coeff_zᵃᵃᶜ::ZT,
bounds::PP, buffer_scheme::CA,
advecting_velocity_scheme :: SI) where {N, FT, XT, YT, ZT, WF, PP, CA, SI}
return new{N, FT, XT, YT, ZT, WF, PP, CA, SI}(coeff_xᶠᵃᵃ, coeff_xᶜᵃᵃ,
coeff_yᵃᶠᵃ, coeff_yᵃᶜᵃ,
coeff_zᵃᵃᶠ, coeff_zᵃᵃᶜ,
bounds, buffer_scheme, advecting_velocity_scheme)
end
end
"""
WENO([FT=Float64;]
order = 5,
grid = nothing,
zweno = true,
bounds = nothing)
Construct a weighted essentially non-oscillatory advection scheme of order `order`.
Keyword arguments
=================
- `order`: The order of the WENO advection scheme. Default: 5
- `grid`: (defaults to `nothing`)
- `zweno`: When `true` implement a Z-WENO formulation for the WENO weights calculation.
(defaults to `true`)
Examples
========
```jldoctest
julia> using Oceananigans
julia> WENO()
WENO reconstruction order 5
Smoothness formulation:
└── Z-weno
Boundary scheme:
└── WENO reconstruction order 3
Symmetric scheme:
└── Centered reconstruction order 4
Directions:
├── X regular
├── Y regular
└── Z regular
```
```jldoctest
julia> using Oceananigans
julia> Nx, Nz = 16, 10;
julia> Lx, Lz = 1e4, 1e3;
julia> chebychev_spaced_z_faces(k) = - Lz/2 - Lz/2 * cos(π * (k - 1) / Nz);
julia> grid = RectilinearGrid(size = (Nx, Nz), halo = (4, 4), topology=(Periodic, Flat, Bounded),
x = (0, Lx), z = chebychev_spaced_z_faces);
julia> WENO(grid; order=7)
WENO reconstruction order 7
Smoothness formulation:
└── Z-weno
Boundary scheme:
└── WENO reconstruction order 5
Symmetric scheme:
└── Centered reconstruction order 6
Directions:
├── X regular
├── Y regular
└── Z stretched
```
"""
function WENO(FT::DataType=Float64;
order = 5,
grid = nothing,
zweno = true,
bounds = nothing)
if !(grid isa Nothing)
FT = eltype(grid)
end
mod(order, 2) == 0 && throw(ArgumentError("WENO reconstruction scheme is defined only for odd orders"))
if order < 3
# WENO(order = 1) is equivalent to UpwindBiased(order = 1)
return UpwindBiased(FT; order = 1)
else
N = Int((order + 1) ÷ 2)
weno_coefficients = compute_reconstruction_coefficients(grid, FT, :WENO; order = N)
buffer_scheme = WENO(FT; grid, order = order - 2, zweno, bounds)
advecting_velocity_scheme = Centered(FT; grid, order = order - 1)
end
return WENO{N, FT, zweno}(weno_coefficients..., bounds, buffer_scheme, advecting_velocity_scheme)
end
WENO(grid, FT::DataType=Float64; kwargs...) = WENO(FT; grid, kwargs...)
# Some usefull aliases
WENOThirdOrder(grid=nothing, FT::DataType=Float64; kwargs...) = WENO(grid, FT; order=3, kwargs...)
WENOFifthOrder(grid=nothing, FT::DataType=Float64; kwargs...) = WENO(grid, FT; order=5, kwargs...)
# Flavours of WENO
const ZWENO = WENO{<:Any, <:Any, <:Any, <:Any, <:Any, true}
const PositiveWENO = WENO{<:Any, <:Any, <:Any, <:Any, <:Any, <:Any, <:Tuple}
Base.summary(a::WENO{N}) where N = string("WENO reconstruction order ", N*2-1)
Base.show(io::IO, a::WENO{N, FT, RX, RY, RZ, WF, PP}) where {N, FT, RX, RY, RZ, WF, PP} =
print(io, summary(a), " \n",
" Smoothness formulation: ", "\n",
" └── $(WF ? "Z-weno" : "JS-weno") \n",
a.bounds isa Nothing ? "" : " Bounds : \n └── $(a.bounds) \n",
" Boundary scheme: ", "\n",
" └── ", summary(a.buffer_scheme) , "\n",
" Symmetric scheme: ", "\n",
" └── ", summary(a.advecting_velocity_scheme) , "\n",
" Directions:", "\n",
" ├── X $(RX == Nothing ? "regular" : "stretched") \n",
" ├── Y $(RY == Nothing ? "regular" : "stretched") \n",
" └── Z $(RZ == Nothing ? "regular" : "stretched")" )
Adapt.adapt_structure(to, scheme::WENO{N, FT, XT, YT, ZT, WF, PP}) where {N, FT, XT, YT, ZT, WF, PP} =
WENO{N, FT, WF}(Adapt.adapt(to, scheme.coeff_xᶠᵃᵃ), Adapt.adapt(to, scheme.coeff_xᶜᵃᵃ),
Adapt.adapt(to, scheme.coeff_yᵃᶠᵃ), Adapt.adapt(to, scheme.coeff_yᵃᶜᵃ),
Adapt.adapt(to, scheme.coeff_zᵃᵃᶠ), Adapt.adapt(to, scheme.coeff_zᵃᵃᶜ),
Adapt.adapt(to, scheme.bounds),
Adapt.adapt(to, scheme.buffer_scheme),
Adapt.adapt(to, scheme.advecting_velocity_scheme))
on_architecture(to, scheme::WENO{N, FT, XT, YT, ZT, WF, PP}) where {N, FT, XT, YT, ZT, WF, PP} =
WENO{N, FT, WF}(on_architecture(to, scheme.coeff_xᶠᵃᵃ), on_architecture(to, scheme.coeff_xᶜᵃᵃ),
on_architecture(to, scheme.coeff_yᵃᶠᵃ), on_architecture(to, scheme.coeff_yᵃᶜᵃ),
on_architecture(to, scheme.coeff_zᵃᵃᶠ), on_architecture(to, scheme.coeff_zᵃᵃᶜ),
on_architecture(to, scheme.bounds),
on_architecture(to, scheme.buffer_scheme),
on_architecture(to, scheme.advecting_velocity_scheme))
# Retrieve precomputed coefficients (+2 for julia's 1 based indices)
@inline retrieve_coeff(scheme::WENO, r, ::Val{1}, i, ::Type{Face}) = @inbounds scheme.coeff_xᶠᵃᵃ[r+2][i]
@inline retrieve_coeff(scheme::WENO, r, ::Val{1}, i, ::Type{Center}) = @inbounds scheme.coeff_xᶜᵃᵃ[r+2][i]
@inline retrieve_coeff(scheme::WENO, r, ::Val{2}, i, ::Type{Face}) = @inbounds scheme.coeff_yᵃᶠᵃ[r+2][i]
@inline retrieve_coeff(scheme::WENO, r, ::Val{2}, i, ::Type{Center}) = @inbounds scheme.coeff_yᵃᶜᵃ[r+2][i]
@inline retrieve_coeff(scheme::WENO, r, ::Val{3}, i, ::Type{Face}) = @inbounds scheme.coeff_zᵃᵃᶠ[r+2][i]
@inline retrieve_coeff(scheme::WENO, r, ::Val{3}, i, ::Type{Center}) = @inbounds scheme.coeff_zᵃᵃᶜ[r+2][i]