-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.jl
81 lines (66 loc) · 2.44 KB
/
mesh.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
export Mesh
"""
Mesh(args)
Construct a periodic mesh of `N` collocation points regularly spaced between
`xmin` (included) and `xmax` (excluded), and associated Fourier modes.
# Arguments
Can be either
- a `NamedTuple` containing `N` and `xmin` and `xmax`; or
- a `NamedTuple` containing `N` and `L` (in which case `xmin=-L` and `xmax=L`); or
- a vector of regularly spaced collocation points.
# Return values
`m=Mesh(args)` is of parametric type and offers
with
- `m.N `: number of collocation points and Fourier modes;
- `m.xmin`: minimum of the mesh (included in the vector of collocation points);
- `m.xmax`: maximum of the mesh (excluded in the vector of collocation points);
- `m.dx`: distance between two collocation points;
- `m.x`: the vector of collocation points;
- `m.k`: the vector of wavenumbers;
- `m.kmin`: minimum of wavenumbers (included in the vector of wavenumbers);
- `m.kmax`: maximum of wavenumbers (included in the vector of wavenumbers);
- `m.dk`: distance between two Fourier modes.
"""
struct Mesh
N :: Int64
xmin :: Float64
xmax :: Float64
dx :: Float64
x :: Vector{Float64}
kmin :: Float64
kmax :: Float64
dk :: Float64
k :: Vector{Float64}
function Mesh(param :: NamedTuple)
if :xmin in keys(param) && :xmax in keys(param)
xmin = param.xmin
xmax = param.xmax
elseif :L in keys(param)
xmin = - param.L
xmax = param.L
else
@error("the NamedTuple must contain the field `L`, or `xmin` and `xmax`, when defining a mesh.")
end
N = param.N
x = zeros(Float64, N)
x .= range(xmin, stop=xmax, length=N+1)[1:end-1]
dx=x[2]-x[1]
dk = 2π/(N*dx)
kmin = -N÷2*dk
kmax = (N-1)÷2*dk
k = zeros(Float64, N)
k .= dk .* vcat(0:(N-1)÷2, -N÷2:-1)
new( N, xmin, xmax, dx, x, kmin, kmax, dk, k)
end
function Mesh( x )
if !(x[2:end].-x[2]≈x[1:end-1].-x[1])
@error("Collocation points must be equally spaced.")
else
Mesh( (xmin=x[1], xmax=x[end]+(x[2]-x[1]), N=length(x)) )
end
end
end
show(io::IO, m::Mesh) =
print(io,"One-dimensional grid of $(m.N) collocation points on [$(m.xmin), $(m.xmax)].\n\
├─Grid spacing dx=$(m.dx).\n└─Maximal wavenumber k=$(-m.kmin).")
Base.:(==)(a::Mesh, b::Mesh) = ( a.N == b.N && a.xmin == a.xmin && a.xmax == b.xmax )