Skip to content

Commit 0bcb5b6

Browse files
committed
switch to SArrays
1 parent e9541d4 commit 0bcb5b6

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

src/TrixiShallowWater.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using Trixi
88
# Import additional symbols that are not exported by Trixi.jl
99
using Trixi: get_node_vars, set_node_vars!
1010
using MuladdMacro: @muladd
11-
using StaticArrays: SVector, MVector, MArray, SMatrix, @SMatrix
11+
using StaticArrays: SVector, MVector, MArray, SMatrix, @SMatrix, SArray
1212
using Static: True, False
1313
using LinearAlgebra: norm
1414
using Roots: Order2, solve, ZeroProblem

src/equations/moment_matrices.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,19 @@ function compute_B_tensor(n::Int, RealT = Float64)
4949
B[i, j, k] = 0.5f0 * (2 * i + 1) * res
5050
end # i, j, k
5151

52-
return Array{RealT, 3}(B)
52+
return SArray{Tuple{n, n, n}, RealT, 3, n * n * n}(B)
5353
end
5454

5555
# Directly compute the A tensor from B using the relation
5656
# A_ijk / (2i + 1) = - B_ijk / (2i + 1) - B_kji / (2k + 1)
57-
function compute_A_tensor(B::Array{RealT, 3}) where {RealT}
58-
N = size(B, 1) # Get number of moments
57+
function compute_A_tensor(B::SArray{Tuple{N, N, N}, RealT, 3}) where {RealT <: Real, N}
5958
A = Array{RealT, 3}(zeros(RealT, N, N, N)) # initialize the A tensor
6059

6160
for i in 1:N, j in 1:N, k in 1:N
6261
A[i, j, k] = -(2 * i + 1) * (B[i, j, k] / (2 * i + 1) + B[k, j, i] / (2 * k + 1))
6362
end # i, j, k
6463

65-
return A
64+
return SArray{Tuple{N, N, N}, RealT, 3, N * N * N}(A)
6665
end
6766

6867
# Helper function to precompute and store every instance of the interior integral
@@ -152,7 +151,7 @@ function compute_C_matrix(n::Int, RealT = Float64)
152151
C[i, j] = 0.5f0 * (2 * i + 1) * res
153152
end # i, j
154153

155-
return C
154+
return SArray{Tuple{n, n}, RealT, 2, n * n}(C)
156155
end
157156

158157
# The tensor B and matrix C are built from the shifted and unnormalized Legendre polynomials.

src/equations/shallow_water_moments_1d.jl

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,44 @@ For details see the paper:
4141
Entropy analysis and entropy stable DG methods for the shallow water moment equations
4242
[DOI: 10.48550/arXiv.2602.06513](https://doi.org/10.48550/arXiv.2602.06513)
4343
"""
44-
struct ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT <: Real} <:
44+
struct ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT <: Real,
45+
Array3 <: AbstractArray{RealT, 3},
46+
Array2 <: AbstractArray{RealT, 2}} <:
4547
AbstractMomentEquations{1, NVARS, NMOMENTS}
4648
gravity::RealT # gravitational acceleration
4749
H0::RealT # constant "lake-at-rest" total water height
4850
n_moments::Integer # number of moments
4951
# Moment matrices
50-
A::Array{RealT, 3}
51-
B::Array{RealT, 3}
52-
C::Array{RealT, 2}
52+
A::Array3
53+
B::Array3
54+
C::Array2
5355
# Friction related quantities
5456
nu::RealT # kinematic viscosity
5557
lambda::RealT # slip length
5658
rho::RealT # fluid density (relevant for Manning friction)
5759
nman::RealT # Manning roughness coefficient
5860

59-
function ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT}(gravity::RealT,
60-
H0::RealT,
61-
n_moments::Integer,
62-
A::Array{RealT, 3},
63-
B::Array{RealT, 3},
64-
C::Array{RealT, 2},
65-
nu::RealT,
66-
lambda::RealT,
67-
rho::RealT,
68-
nman::RealT) where {
69-
NVARS,
70-
NMOMENTS,
71-
RealT <:
72-
Real
73-
}
61+
function ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT, Array3, Array2}(gravity::RealT,
62+
H0::RealT,
63+
n_moments::Integer,
64+
A::Array3,
65+
B::Array3,
66+
C::Array2,
67+
nu::RealT,
68+
lambda::RealT,
69+
rho::RealT,
70+
nman::RealT) where {
71+
NVARS,
72+
NMOMENTS,
73+
RealT <:
74+
Real,
75+
Array3 <:
76+
AbstractArray{RealT,
77+
3},
78+
Array2 <:
79+
AbstractArray{RealT,
80+
2}
81+
}
7482
new(gravity, H0, n_moments, A, B, C, nu, lambda, rho, nman)
7583
end
7684
end
@@ -93,24 +101,33 @@ function ShallowWaterMomentEquations1D(; gravity, H0 = zero(gravity), n_moments,
93101
A = compute_A_tensor(B)
94102
C = compute_C_matrix(n_moments)
95103

96-
return ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT}(gravity,
97-
H0,
98-
n_moments,
99-
A,
100-
B,
101-
C,
102-
nu,
103-
lambda,
104-
rho,
105-
nman)
104+
Array3 = promote_type(typeof(A), typeof(B))
105+
Array2 = typeof(C)
106+
return ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT, Array3, Array2}(gravity,
107+
H0,
108+
n_moments,
109+
A,
110+
B,
111+
C,
112+
nu,
113+
lambda,
114+
rho,
115+
nman)
106116
end
107117

108-
@inline function Base.real(::ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT}) where {
109-
NVARS,
110-
NMOMENTS,
111-
RealT <:
112-
Real
113-
}
118+
@inline function Base.real(::ShallowWaterMomentEquations1D{NVARS, NMOMENTS, RealT,
119+
Array3, Array2}) where {
120+
NVARS,
121+
NMOMENTS,
122+
RealT <:
123+
Real,
124+
Array3 <:
125+
AbstractArray{RealT,
126+
3},
127+
Array2 <:
128+
AbstractArray{RealT,
129+
2}
130+
}
114131
RealT
115132
end
116133

0 commit comments

Comments
 (0)