• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JoshuaLampert / DispersiveShallowWater.jl / 10374046094

13 Aug 2024 05:03PM UTC coverage: 96.185% (-0.7%) from 96.883%
10374046094

Pull #127

github

web-flow
Merge 012303f29 into 6577a58b2
Pull Request #127: implement Serre-Green-Naghdi equations for flat bathymetry

179 of 194 new or added lines in 3 files covered. (92.27%)

4 existing lines in 2 files now uncovered.

1311 of 1363 relevant lines covered (96.18%)

2936747.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.97
/src/equations/equations.jl
1
"""
2
    AbstractEquations{NDIMS, NVARS}
3

4
An abstract supertype of specific equations such as the BBM-BBM equations.
5
The type parameters encode the number of spatial dimensions (`NDIMS`) and the
6
number of primary variables (`NVARS`) of the physics model.
7

8
See also [`AbstractShallowWaterEquations`](@ref).
9
"""
10
abstract type AbstractEquations{NDIMS, NVARS} end
11

12
# Retrieve number of variables from equation instance
13
@inline nvariables(::AbstractEquations{NDIMS, NVARS}) where {NDIMS, NVARS} = NVARS
153,968,466✔
14

15
"""
16
    eachvariable(equations::AbstractEquations)
17

18
Return an iterator over the indices that specify the location in relevant data structures
19
for the variables in `equations`. In particular, not the variables themselves are returned.
20
"""
21
@inline eachvariable(equations::AbstractEquations) = Base.OneTo(nvariables(equations))
71,950,191✔
22

23
"""
24
    get_name(equations::AbstractEquations)
25

26
Return the canonical, human-readable name for the given system of equations.
27
# Examples
28
```jldoctest
29
julia> DispersiveShallowWater.get_name(BBMBBMEquations1D(gravity_constant=1.0))
30
"BBMBBMEquations1D"
31
```
32
"""
33
get_name(equations::AbstractEquations) = equations |> typeof |> nameof |> string
1,131✔
34

35
"""
36
    varnames(conversion_function, equations)
37

38
Return the list of variable names when applying `conversion_function` to the
39
conserved variables associated to `equations`.
40
Common choices of the `conversion_function` are [`prim2prim`](@ref) and
41
[`prim2cons`](@ref).
42
"""
43
function varnames end
44

45
"""
46
    AbstractShallowWaterEquations{NDIMS, NVARS}
47

48
An abstract supertype of all equation system that contain the classical
49
shallow water equations as a subsystem, e.g., the
50
[`BBMBBMEquations1D`](@ref), the [`SvaerdKalischEquations1D`](@ref),
51
and the [`SerreGreenNaghdiEquations1D`](@ref).
52
In 1D, the shallow water equations with flat bathymetry are given by
53
```math
54
\begin{aligned}
55
  h_t + (h v)_x &= 0,\\
56
  h v_t + \frac{1}{2} g (h^2)_x + \frac{1}{2} h (v^2)_x &= 0,
57
\end{aligned}
58
```
59
where ``h`` is the [`waterheight`](@ref),
60
``v`` the [`velocity`](@ref), and
61
``g`` the [`gravity_constant`](@ref).
62
"""
63
abstract type AbstractShallowWaterEquations{NDIMS, NVARS} <: AbstractEquations{NDIMS, NVARS} end
64

65
"""
66
    prim2prim(q, equations)
67

68
Return the primitive variables `q`. While this function is as trivial as `identity`,
69
it is also as useful.
70
"""
71
@inline prim2prim(q, ::AbstractEquations) = q
4,929✔
72

73
"""
74
    prim2cons(q, equations)
75

76
Convert the primitive variables `q` to the conserved variables for a given set of
77
`equations`. `q` is a vector type of the correct length `nvariables(equations)`.
78
Notice the function doesn't include any error checks for the purpose of efficiency,
79
so please make sure your input is correct.
80
The inverse conversion is performed by [`cons2prim`](@ref).
81
"""
82
function prim2cons end
83

84
"""
85
    cons2prim(u, equations)
86

87
Convert the conserved variables `u` to the primitive variables for a given set of
88
`equations`. `u` is a vector type of the correct length `nvariables(equations)`.
89
Notice the function doesn't include any error checks for the purpose of efficiency,
90
so please make sure your input is correct.
91
The inverse conversion is performed by [`prim2cons`](@ref).
92
"""
93
function cons2prim end
94

95
"""
96
    waterheight_total(q, equations)
97

98
Return the total waterheight of the primitive variables `q` for a given set of
99
`equations`, i.e., the [`waterheight`](@ref) plus the
100
[`bathymetry`](@ref).
101

102
`q` is a vector of the primitive variables at a single node, i.e., a vector
103
of the correct length `nvariables(equations)`.
104
"""
105
function waterheight_total end
106

107
varnames(::typeof(waterheight_total), equations) = ("η",)
12✔
108

109
"""
110
    waterheight(q, equations)
111

112
Return the waterheight of the primitive variables `q` for a given set of
113
`equations`, i.e., the waterheight above the bathymetry.
114

115
`q` is a vector of the primitive variables at a single node, i.e., a vector
116
of the correct length `nvariables(equations)`.
117
"""
118
function waterheight end
119

120
varnames(::typeof(waterheight), equations) = ("h",)
12✔
121

122
"""
123
    velocity(q, equations)
124

125
Return the velocity of the primitive variables `q` for a given set of
126
`equations`.
127

128
`q` is a vector of the primitive variables at a single node, i.e., a vector
129
of the correct length `nvariables(equations)`.
130
"""
131
function velocity end
132

133
varnames(::typeof(velocity), equations) = ("v",)
12✔
134

135
"""
136
    momentum(q, equations)
137

138
Return the momentum/discharge of the primitive variables `q` for a given set of
139
`equations`, i.e., the [`waterheight`](@ref) times the [`velocity`](@ref).
140

141
`q` is a vector of the primitive variables at a single node, i.e., a vector
142
of the correct length `nvariables(equations)`.
143
"""
144
@inline function momentum(q, equations::AbstractEquations)
31,224✔
145
    return waterheight(q, equations) * velocity(q, equations)
31,224✔
146
end
147

148
varnames(::typeof(momentum), equations) = ("P",)
12✔
149

150
"""
151
    discharge(q, equations)
152

153
See [`momentum`](@ref).
154
"""
155
@inline discharge(q, equations::AbstractEquations) = momentum(q, equations)
12✔
156

157
varnames(::typeof(discharge), equations) = ("P",)
12✔
158

159
"""
160
    still_water_surface(q, equations::AbstractShallowWaterEquations)
161

162
Return the still water surface ``\\eta_0`` (lake at rest)
163
for a given set of `equations`.
164
"""
NEW
165
@inline function still_water_surface(q, equations::AbstractShallowWaterEquations)
×
NEW
166
    return equations.eta0
×
167
end
168

169
@inline function still_waterdepth(q, equations::AbstractEquations)
50,166✔
170
    b = bathymetry(q, equations)
50,166✔
171
    D = equations.eta0 - b
50,166✔
172
    return D
50,166✔
173
end
174

175
"""
176
    entropy(q, equations)
177

178
Return the entropy of the primitive variables `q` for a given set of
179
`equations`. For all [`AbstractShallowWaterEquations`](@ref), the `entropy`
180
is just the [`energy_total`](@ref).
181

182
`q` is a vector of the primitive variables at a single node, i.e., a vector
183
of the correct length `nvariables(equations)`.
184
"""
185
function entropy end
186

NEW
UNCOV
187
function entropy(q, equations::AbstractShallowWaterEquations)
×
NEW
UNCOV
188
    return energy_total(q, equations)
×
189
end
190

191
varnames(::typeof(entropy), equations) = ("U",)
12✔
192

193
"""
194
    gravity_constant(equations::AbstractShallowWaterEquations)
195

196
Return the gravity constant ``g`` for a given set of `equations`.
197
See also [`AbstractShallowWaterEquations`](@ref).
198
"""
199
@inline function gravity_constant(equations::AbstractShallowWaterEquations)
8,984,553✔
200
    return equations.gravity
8,984,553✔
201
end
202

203
"""
204
    energy_total(q, equations)
205

206
Return the total energy of the primitive variables `q` for a given set of
207
`equations`. For all [`AbstractShallowWaterEquations`](@ref), the total
208
energy is given by the sum of the kinetic and potential energy of the
209
shallow water subsystem, i.e.,
210
```math
211
\\frac{1}{2} h v^2 + \\frac{1}{2} g \\eta^2
212
```
213
in 1D, where ``h`` is the [`waterheight`](@ref),
214
``\\eta = h + b`` the [`waterheight_total`](@ref),
215
``v`` the [`velocity`](@ref), and ``g`` the [`gravity_constant`](@ref).
216

217
`q` is a vector of the primitive variables at a single node, i.e., a vector
218
of the correct length `nvariables(equations)`.
219
"""
220
@inline function energy_total(q, equations::AbstractShallowWaterEquations)
8,984,553✔
221
    h = waterheight(q, equations)
8,984,553✔
222
    eta = waterheight_total(q, equations)
8,984,553✔
223
    v = velocity(q, equations)
8,984,553✔
224
    return 0.5f0 * h * v^2 + 0.5f0 * gravity_constant(equations) * eta^2
8,984,553✔
225
end
226

227
varnames(::typeof(energy_total), equations) = ("e_total",)
12✔
228

229
# The modified entropy/total energy takes the whole `q_global` for every point in space
230
"""
231
    energy_total_modified(q_global, equations::AbstractShallowWaterEquations, cache)
232

233
Return the modified total energy of the primitive variables `q_global` for the
234
`equations`. This modified total energy is a conserved quantity and can
235
contain additional terms compared to the usual [`energy_total`](@ref).
236
For example, for the [`SvaerdKalischEquations1D`](@ref) and the
237
[`SerreGreenNaghdiEquations1D`](@ref), it contains additional terms
238
depending on the derivative of the velocity ``v_x`` modeling non-hydrostatic
239
contributions.
240

241
`q_global` is a vector of the primitive variables at ALL nodes.
242
`cache` needs to hold the SBP operators used by the `solver` if non-hydrostatic
243
terms are present.
244
"""
NEW
245
function energy_total_modified(q_global, equations::AbstractShallowWaterEquations, cache)
×
246
    # `q_global` is an `ArrayPartition` of the primitive variables at all nodes
NEW
247
    @assert nvariables(equations) == length(q_global.x)
×
248

NEW
249
    e = similar(q_global.x[begin])
×
NEW
250
    for i in eachindex(q_global.x[begin])
×
NEW
251
        e[i] = energy_total(get_node_vars(q, equations, i))
×
NEW
252
    end
×
253

NEW
254
    return e
×
255
end
256

257
varnames(::typeof(energy_total_modified), equations) = ("e_modified",)
3✔
258

259
"""
260
    entropy_modified(q, equations::AbstractShallowWaterEquations, cache)
261

262
Alias for [`energy_total_modified`](@ref).
263
"""
264
@inline function entropy_modified(q, equations::AbstractShallowWaterEquations, cache)
658✔
265
    energy_total_modified(q, equations, cache)
273,532✔
266
end
267

268
varnames(::typeof(entropy_modified), equations) = ("U_modified",)
3✔
269

270
# Add methods to show some information on systems of equations.
271
function Base.show(io::IO, equations::AbstractEquations)
15✔
272
    # Since this is not performance-critical, we can use `@nospecialize` to reduce latency.
273
    @nospecialize equations # reduce precompilation time
15✔
274

275
    print(io, get_name(equations), " with ")
15✔
276
    if nvariables(equations) == 1
15✔
277
        println(io, "one variable")
×
278
    else
279
        println(io, nvariables(equations), " variables")
15✔
280
    end
281
end
282

283
function Base.show(io::IO, ::MIME"text/plain", equations::AbstractEquations)
12✔
284
    # Since this is not performance-critical, we can use `@nospecialize` to reduce latency.
285
    @nospecialize equations # reduce precompilation time
12✔
286

287
    if get(io, :compact, false)
24✔
UNCOV
288
        show(io, equations)
×
289
    else
290
        println(io, get_name(equations))
12✔
291
        println(io, "#variables: ", nvariables(equations))
12✔
292
        for variable in eachvariable(equations)
12✔
293
            println("    variable " * string(variable), ": ",
30✔
294
                    varnames(prim2prim, equations)[variable])
295
        end
30✔
296
    end
297
end
298

299
@inline Base.ndims(::AbstractEquations{NDIMS}) where {NDIMS} = NDIMS
123✔
300

301
"""
302
    default_analysis_errors(equations)
303

304
Default analysis errors used by the [`AnalysisCallback`](@ref).
305
"""
306
default_analysis_errors(::AbstractEquations) = (:l2_error, :linf_error)
111✔
307

308
"""
309
    default_analysis_integrals(equations)
310

311
Default analysis integrals used by the [`AnalysisCallback`](@ref).
312
"""
313
default_analysis_integrals(::AbstractEquations) = Symbol[]
111✔
314

315
abstract type AbstractBathymetry end
316
struct BathymetryFlat <: AbstractBathymetry end
3✔
317
"""
318
    bathymetry_flat = DispersiveShallowWater.BathymetryFlat()
319

320
A singleton struct indicating a flat bathymetry ``b = 0``.
321
"""
322
const bathymetry_flat = BathymetryFlat()
323

324
# BBM-BBM equations
325
abstract type AbstractBBMBBMEquations{NDIMS, NVARS} <:
326
              AbstractShallowWaterEquations{NDIMS, NVARS} end
327
include("bbm_bbm_1d.jl")
328
include("bbm_bbm_variable_bathymetry_1d.jl")
329

330
# Svärd-Kalisch equations
331
abstract type AbstractSvaerdKalischEquations{NDIMS, NVARS} <:
332
              AbstractShallowWaterEquations{NDIMS, NVARS} end
333
include("svaerd_kalisch_1d.jl")
334

335
# Serre-Green-Naghdi equations
336
abstract type AbstractSerreGreenNaghdiEquations{NDIMS, NVARS} <:
337
              AbstractShallowWaterEquations{NDIMS, NVARS} end
338
include("serre_green_naghdi_flat_1d.jl")
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc