-
Notifications
You must be signed in to change notification settings - Fork 79
/
mesh.jl
231 lines (172 loc) · 5.61 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
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
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
Mesh{Dim,T,TP}
A mesh embedded in a `Dim`-dimensional space with coordinates of type `T`
and topology of type `TP`.
"""
abstract type Mesh{Dim,T,TP<:Topology} <: Domain{Dim,T} end
"""
vertex(mesh, ind)
Return the vertex of a `mesh` at index `ind`.
"""
function vertex end
"""
vertices(mesh)
Return the vertices of the `mesh`.
"""
vertices(m::Mesh) = [vertex(m, ind) for ind in 1:nvertices(m)]
"""
nvertices(mesh)
Return the number of vertices of the `mesh`.
"""
nvertices(m::Mesh) = nvertices(topology(m))
"""
faces(mesh, rank)
Return an iterator with `rank`-faces of the `mesh`.
## Examples
Consider a mesh of tetrahedrons embedded in a 3D space. We can loop over
all 3-faces (a.k.a. elements) or over all 2-faces to handle the interfaces
(i.e. triangles) between adjacent elements:
```julia
tetrahedrons = faces(mesh, 3)
triangles = faces(mesh, 2)
segments = faces(mesh, 1)
```
"""
faces(m::Mesh, rank) = (materialize(f, vertices(m)) for f in faces(topology(m), rank))
"""
nfaces(mesh, rank)
Return the number of `rank`-faces of the `mesh`.
"""
nfaces(m::Mesh, rank) = nfaces(topology(m), rank)
"""
elements(mesh)
Return the top-faces (a.k.a. elements) of the `mesh`.
## Examples
The elements of a volume embedded in 3D space can be tetrahedrons, hexahedrons,
or any 3-face. The elements of a surface embedded in 3D space can be triangles,
quadrangles or any 2-face.
"""
elements(m::Mesh) = (element(m, ind) for ind in 1:nelements(m))
"""
element(mesh, ind)
Return the element of the `mesh` at index `ind`.
"""
element(m::Mesh, ind) = materialize(element(topology(m), ind), vertices(m))
"""
nelements(mesh)
Return the number of elements of the `mesh`.
"""
nelements(m::Mesh) = nelements(topology(m))
"""
facets(mesh)
Return the (top-1)-faces (a.k.a. facets) of the `mesh`.
"""
facets(m::Mesh) = (facet(m, ind) for ind in 1:nfacets(m))
"""
facet(mesh, ind)
Return the facet of the `mesh` at index `ind`.
"""
facet(m::Mesh, ind) = materialize(facet(topology(m), ind), vertices(m))
"""
nfacets(mesh)
Return the number of facets of the `mesh`.
"""
nfacets(m::Mesh) = nfacets(topology(m))
"""
topoconvert(T, mesh)
Convert underlying topology of the `mesh` to topology of type `T`.
## Examples
Convert underlying topology to [`HalfEdgeTopology`](@ref) for
efficient topological relations.
```julia
newmesh = topoconvert(HalfEdgeTopology, mesh)
```
"""
topoconvert(TP::Type{<:Topology}, m::Mesh) = SimpleMesh(vertices(m), convert(TP, topology(m)))
==(m₁::Mesh, m₂::Mesh) = vertices(m₁) == vertices(m₂) && topology(m₁) == topology(m₂)
function Base.show(io::IO, ::MIME"text/plain", m::Mesh{Dim,T}) where {Dim,T}
t = topology(m)
verts = vertices(m)
elems = elements(t)
nvert = nvertices(m)
nelms = nelements(m)
summary(io, m)
println(io)
println(io, " $nvert vertices")
printelms(io, verts, " ")
println(io)
println(io, " $nelms elements")
printitr(io, elems, " ")
end
"""
Grid{Dim,T}
A grid embedded in a `Dim`-dimensional space with coordinates of type `T`.
"""
const Grid{Dim,T} = Mesh{Dim,T,GridTopology{Dim}}
"""
SubGrid{Dim,T}
A view of a grid in a `Dim`-dimensinoal space with coordinates of type `T`.
"""
const SubGrid{Dim,T} = SubDomain{Dim,T,<:Grid{Dim,T}}
"""
vertex(grid, ijk)
Convert Cartesian index `ijk` to vertex on `grid`.
"""
vertex(g::Grid{Dim}, ijk::CartesianIndex{Dim}) where {Dim} = vertex(g, ijk.I)
"""
xyz(grid)
Returns the coordinate vectors of each dimension of the `grid`, e.g `(x, y, z, ...)`.
The vertex `i,j,k,...` is constructed with `Point(x[i], y[j], z[k], ...)`.
"""
function xyz end
"""
XYZ(grid)
Returns the coordinate arrays of each dimension of the `grid`, e.g `(X, Y, Z, ...)`.
The vertex `i,j,k,...` is constructed with `Point(X[i,j,k,...], Y[i,j,k,...], Z[i,j,k,...], ...)`.
"""
function XYZ end
# ----------
# FALLBACKS
# ----------
Base.size(g::Grid) = size(topology(g))
vertex(g::Grid, ind::Int) = vertex(g, CartesianIndices(size(g) .+ 1)[ind])
vertex(g::Grid{Dim}, ijk::Dims{Dim}) where {Dim} = vertex(g, LinearIndices(size(g) .+ 1)[ijk...])
Base.minimum(g::Grid{Dim}) where {Dim} = vertex(g, ntuple(i -> 1, Dim))
Base.maximum(g::Grid{Dim}) where {Dim} = vertex(g, size(g) .+ 1)
Base.extrema(g::Grid{Dim}) where {Dim} = minimum(g), maximum(g)
function element(g::Grid, ind::Int)
elem = element(topology(g), ind)
type = pltype(elem)
einds = indices(elem)
cinds = CartesianIndices(size(g) .+ 1)
verts = ntuple(i -> vertex(g, cinds[einds[i]]), nvertices(type))
type(verts)
end
Base.eltype(g::Grid) = typeof(first(g))
Base.getindex(g::Grid{Dim}, ijk::Vararg{Int,Dim}) where {Dim} = element(g, LinearIndices(size(g))[ijk...])
@propagate_inbounds function Base.getindex(g::Grid{Dim}, ijk::Vararg{Union{UnitRange{Int},Colon,Int},Dim}) where {Dim}
dims = size(g)
ranges = ntuple(i -> _asrange(dims[i], ijk[i]), Dim)
getindex(g, CartesianIndices(ranges))
end
_asrange(::Int, r::UnitRange{Int}) = r
_asrange(d::Int, ::Colon) = 1:d
_asrange(::Int, i::Int) = i:i
function _checkbounds(g::Grid{Dim}, I::CartesianIndices{Dim}) where {Dim}
dims = size(g)
ranges = I.indices
if !all(first(r) ≥ 1 && last(r) ≤ d for (d, r) in zip(dims, ranges))
throw(BoundsError(g, ranges))
end
end
# ----------------
# IMPLEMENTATIONS
# ----------------
include("mesh/cartesiangrid.jl")
include("mesh/rectilineargrid.jl")
include("mesh/structuredgrid.jl")
include("mesh/simplemesh.jl")
include("mesh/transformedmesh.jl")