-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsimplexgridbuilder.jl
More file actions
426 lines (364 loc) · 12.4 KB
/
simplexgridbuilder.jl
File metadata and controls
426 lines (364 loc) · 12.4 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
$(TYPEDEF)
Simplex grid builder: wrapper around array based mesh generator interface.
It allows to build up the input data incrementally.
"""
mutable struct SimplexGridBuilder
Generator::Union{Module, Nothing}
current_facetregion::Cint
current_cellregion::Cint
current_cellvolume::Cdouble
point_identity_tolerance::Cdouble
facetregions::Vector{Cint}
facets::Vector{Vector{Cint}}
pointlist::BinnedPointList{Cdouble}
regionpoints::ElasticArray{Cdouble, 2}
regionnumbers::Vector{Cint}
regionvolumes::Vector{Cdouble}
options::Dict{Symbol, Any}
checkexisting::Bool
_savedpoint::Cint
SimplexGridBuilder(x::Nothing) = new()
end
"""
```
SimplexGridBuilder(; Generator=nothing,
tol=1.0e-12,
checkexisting=true)
```
Create a SimplexGridBuilder.
- `Generator`: module corresponding to mesh generator package.
Valid choices are `TetGen` and `Triangulate`, corresponding to the
respective Julia packages.
- `checkexisting`: whether to check for already existing points
- `tol`: two points below this tolerance will be merged if `checkexisting` is true
"""
function SimplexGridBuilder(; Generator = nothing, tol = 1.0e-12, checkexisting = true)
builder = SimplexGridBuilder(nothing)
if isnothing(Generator)
throw(ArgumentError("Missing Generator: SimplexGridBuilder needs Generator=TetGen or Generator=Triangulate as argument"))
end
builder.Generator = Generator
if istetgen(Generator)
dim = 3
mesher = :tetgen
elseif istriangulate(Generator)
dim = 2
mesher = :triangle
else
throw(ArgumentError("Wrong Generator: SimplexGridBuilder needs Generator=TetGen or Generator=Triangulate as argument"))
end
builder.current_facetregion = 1
builder.current_cellregion = 1
builder.current_cellvolume = 1
builder.point_identity_tolerance = tol
builder.facets = []
builder.facetregions = []
builder.pointlist = BinnedPointList(dim; tol = tol)
builder.regionpoints = ElasticArray{Cdouble}(undef, dim, 0)
builder.regionvolumes = []
builder.regionnumbers = []
builder.options = default_options(mesher)
builder.checkexisting = checkexisting
builder._savedpoint = 0
return builder
end
"""
$(SIGNATURES)
Whether to check for already existing points
"""
checkexisting!(builder, b) = builder.checkexisting = b
"""
$(SIGNATURES)
Set some mesh generation options, see [`default_options`](@ref)
"""
options!(builder::SimplexGridBuilder; kwargs...) = blendoptions!(builder.options; kwargs...)
"""
$(SIGNATURES)
Space dimension of builder.
"""
ExtendableGrids.dim_space(builder::SimplexGridBuilder) = size(builder.pointlist.points, 1)
"""
```
point!(builder,x)
point!(builder,x,y)
point!(builder,x,y,z)
point!(builder,vec_or_tuple)
```
Add point or merge with already existing point. Returns its index
which can be used to set up facets with [`facet!`](@ref).
"""
function point!(builder::SimplexGridBuilder, x)
dim_space(builder) == 1 || throw(DimensionMismatch())
return insert!(builder.pointlist, [x])
end
function point!(builder::SimplexGridBuilder, x, y)
dim_space(builder) == 2 || throw(DimensionMismatch())
return insert!(builder.pointlist, [x, y])
end
function point!(builder::SimplexGridBuilder, x, y, z)
dim_space(builder) == 3 || throw(DimensionMismatch())
return insert!(builder.pointlist, [x, y, z])
end
const PointCoord = Union{AbstractVector, Tuple}
point!(builder::SimplexGridBuilder, p::PointCoord) = point!(builder, p...)
"""
```
cellregion!(builder,region)
```
Set the current cell region (acts on subsequent regionpoint() calls)
Cell regions can be used to distinguish cells of different materials etc.
In the API they are characterized by
- region number set via `cellregion!`
- maximum cell volume set via [`maxvolume!`](@ref)
- region point set via [`regionpoint!`](@ref). This is some point located
within the respective region which must be surrounded by facets in a watertight
manner.
"""
cellregion!(builder::SimplexGridBuilder, i) = builder.current_cellregion = i
"""
```
maxvolume!(builder,vol)
```
Set the current cell volume resp. area (acts on subsequent regionpoint() calls).
See [`cellregion!`](@ref).
"""
maxvolume!(builder::SimplexGridBuilder, vol) = builder.current_cellvolume = vol
"""
```
regionpoint!(builder,x)
regionpoint!(builder,x,y)
regionpoint!(builder,x,y,z)
regionpoint!(builder,vec_or_tuple)
```
Add a region point marking a region, using current cell volume an cell region
See [`cellregion!`](@ref).
"""
function regionpoint!(builder::SimplexGridBuilder, x)
dim_space(builder) == 1 || throw(DimensionMismatch())
append!(builder.regionpoints, (x))
push!(builder.regionvolumes, builder.current_cellvolume)
return push!(builder.regionnumbers, builder.current_cellregion)
end
function regionpoint!(builder::SimplexGridBuilder, x, y)
dim_space(builder) == 2 || throw(DimensionMismatch())
append!(builder.regionpoints, (x, y))
push!(builder.regionvolumes, builder.current_cellvolume)
return push!(builder.regionnumbers, builder.current_cellregion)
end
function regionpoint!(builder::SimplexGridBuilder, x, y, z)
dim_space(builder) == 3 || throw(DimensionMismatch())
append!(builder.regionpoints, (x, y, z))
push!(builder.regionvolumes, builder.current_cellvolume)
return push!(builder.regionnumbers, builder.current_cellregion)
end
regionpoint!(builder::SimplexGridBuilder, p::PointCoord) = regionpoint!(builder, p...)
"""
```
holepoint!(builder,x)
holepoint!(builder,x,y)
holepoint!(builder,x,y,z)
holepoint!(builder,vec_or_tuple)
```
Add a point marking a hole region. Hole regions need to be surrounded by facets
in a watertight manner.
"""
function holepoint!(builder::SimplexGridBuilder, x)
dim_space(builder) == 1 || throw(DimensionMismatch())
append!(builder.regionpoints, (x))
push!(builder.regionvolumes, 0)
push!(builder.regionnumbers, 0)
return nothing
end
function holepoint!(builder::SimplexGridBuilder, x, y)
dim_space(builder) == 2 || throw(DimensionMismatch())
append!(builder.regionpoints, (x, y))
push!(builder.regionvolumes, 0)
push!(builder.regionnumbers, 0)
return nothing
end
function holepoint!(builder::SimplexGridBuilder, x, y, z)
dim_space(builder) == 3 || throw(DimensionMismatch())
append!(builder.regionpoints, (x, y, z))
push!(builder.regionvolumes, 0)
push!(builder.regionnumbers, 0)
return nothing
end
holepoint!(builder::SimplexGridBuilder, p::PointCoord) = holepoint!(builder, p...)
"""
```
facetregion!(builder,region)
```
Set the current facet region. Subsequent facets will be marked with this number.
Facet regions can be used to mark different parts of the boundary, e.g. for
distinguishing boundary conditions.
"""
facetregion!(builder::SimplexGridBuilder, i) = builder.current_facetregion = i
"""
```
facet!(builder,i1)
facet!(builder,i1,i2)
facet!(builder,i1,i2,i3,i4)
facet!(builder,vector_or_tuple)
facet!(builder, (x1,y1), (x2,y2))
facet!(builder, (x1,y1,z1), (x2,y2,z2),(x3,y3,z3))
```
Add a facet via the corresponding point indices returned
by [`point!`](@ref).
Facets of two points are solely used for 2D grids. Facets
with more than two points are used for 3D grids and must be
planar.
"""
function facet!(builder::SimplexGridBuilder, i)
dim_space(builder) == 1 || throw(DimensionMismatch())
push!(builder.facets, [i])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
function facet!(builder::SimplexGridBuilder, i1, i2)
dim_space(builder) == 2 || throw(DimensionMismatch())
push!(builder.facets, [i1, i2])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
function facet!(builder::SimplexGridBuilder, i1, i2, i3)
dim_space(builder) == 3 || throw(DimensionMismatch())
push!(builder.facets, [i1, i2, i3])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
function facet!(builder::SimplexGridBuilder, i1, i2, i3, i4)
dim_space(builder) == 3 || throw(DimensionMismatch())
push!(builder.facets, [i1, i2, i3, i4])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
function facet!(builder::SimplexGridBuilder, p::Union{Vector, Tuple})
if dim_space(builder) == 1
length(p) == 1 || throw(DimensionMismatch())
end
if dim_space(builder) == 2
length(p) == 2 || throw(DimensionMismatch())
end
if dim_space(builder) == 3
length(p) >= 3 || throw(DimensionMismatch())
end
push!(builder.facets, [p...])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
facet!(builder::SimplexGridBuilder, p1::PointCoord, p2::PointCoord) = facet!(builder, point!(builder, p1), point!(builder, p2))
function facet!(builder::SimplexGridBuilder, p1::PointCoord, p2::PointCoord, p3::PointCoord)
return facet!(builder, point!(builder, p1), point!(builder, p2), point!(builder, p3))
end
"""
```
polyfacet!(builder,vector_or_tuple)
```
Add a polygonal facet via the corresponding point indices returned
by [`point!`](@ref).
Facets with more than two points are used for 3D grids and must be
planar.
"""
function polyfacet!(builder::SimplexGridBuilder, p::Union{Vector, Tuple})
push!(builder.facets, [p...])
push!(builder.facetregions, builder.current_facetregion)
return length(builder.facets)
end
"""
```
simplexgrid(builder; kwargs...)
```
Build simplex grid from the current state of the builder.
`kwargs` overwrite those set with the [`options!`](@ref) method.
See [`default_options`](@ref) for available `kwargs`.
"""
function ExtendableGrids.simplexgrid(builder::SimplexGridBuilder; kwargs...)
if dim_space(builder) == 2
facets = Array{Cint, 2}(undef, 2, length(builder.facets))
for i in 1:length(builder.facets)
facets[1, i] = builder.facets[i][1]
facets[2, i] = builder.facets[i][2]
end
make_tio = triangulateio
generator_type = TriangulateType
else
facets = builder.facets
make_tio = tetgenio
generator_type = TetGenType
end
options = blendoptions!(copy(builder.options); kwargs...)
tio = make_tio(
builder.Generator;
points = builder.pointlist.points,
bfaces = facets,
bfaceregions = builder.facetregions,
regionpoints = builder.regionpoints,
regionnumbers = builder.regionnumbers,
regionvolumes = builder.regionvolumes
)
return ExtendableGrids.simplexgrid(generator_type, builder.Generator, tio; options...)
end
"""
flags(builder)
Return mesh generator specific flag string created from builder options.
"""
function flags(builder::SimplexGridBuilder)
return if istetgen(builder.Generator)
makeflags(builder.options, :tetgen)
elseif istriangulate(builder.Generator)
makeflags(builder.options, :triangle)
else
nothing
end
end
"""
maybewatertight(this::SimplexGridBuilder; bregions=nothing)
Check if facets belonging to boundare regions in bregions are watertight.
This is based on a number of heuristics, only a negative
answer is definitive.
"""
function maybewatertight(this::SimplexGridBuilder; bregions = nothing)
maybe = true
bfaceregions = this.facetregions
if bregions == nothing
bregions = unique(bfaceregions)
end
@info "Checking for dangling facets"
points = this.pointlist.points
facets = this.facets
dim = size(points, 1)
npoints = size(points, 2)
nfacets = size(facets, 1)
ptmarkers = zeros(Int, npoints)
for ifacet in 1:nfacets
if bfaceregions[ifacet] ∈ bregions
for idim in 1:dim
ptmarkers[facets[ifacet][idim]] += 1
end
end
end
uptmarkers = unique(ptmarkers)
if 1 ∈ uptmarkers
maybe = false
end
if dim == 3 && 2 ∈ uptmarkers
maybe = false
end
if maybe
@info "Maybe description is watertight, but not sure"
else
@warn "Description is not watertight"
for ifacet in 1:nfacets
if bfaceregions[ifacet] ∈ bregions
for idim in 1:dim
pt = facets[ifacet][idim]
if ptmarkers[pt] < dim
@warn "Dangling facet $ifacet (bregion $(bfaceregions[ifacet]), point $(points[:, pt])"
end
end
end
end
end
return maybe
end