-
Notifications
You must be signed in to change notification settings - Fork 94
/
CartesianGeometry.jl
331 lines (280 loc) · 8.97 KB
/
CartesianGeometry.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
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
module CartesianGeometry
# Dependencies of this module
using StaticArrays: SVector, MVector, @SVector
using Gridap
using Gridap.Helpers
# Functionality provided
export CartesianGrid
export CartesianDiscreteModel
import Base: size, getindex, IndexStyle
import Gridap: points, cells, celltypes, cellorders, gridgraph
import Gridap: Grid, GridGraph, FaceLabels
import Gridap: UnstructuredGrid
import Gridap: FlexibleUnstructuredGrid
import Gridap: FullGridGraph
struct CartesianGrid{D} <: Grid{D,D}
dim_to_limits::NTuple{D,NTuple{2,Float64}}
dim_to_ncells::NTuple{D,Int}
extrusion::NTuple{D,Int}
order:: Int
end
function CartesianGrid(;partition::NTuple{D,Int},domain=nothing,order::Int=1) where D
_cartesiangrid(partition,domain,order)
end
function points(self::CartesianGrid)
dim_to_npoint = tuple([ i+1 for i in self.dim_to_ncells ]...)
CartesianGridPoints(self.dim_to_limits,dim_to_npoint)
end
cells(self::CartesianGrid) = CartesianGridCells(self.dim_to_ncells)
celltypes(self::CartesianGrid) = ConstantCellValue(self.extrusion,prod(self.dim_to_ncells))
cellorders(self::CartesianGrid) = ConstantCellValue(self.order,prod(self.dim_to_ncells))
#function gridgraph(self::CartesianGrid)
# #fverdugo this is a temporary implementation
# nparts = [i for i in self.dim_to_ncells]
# mesh = StructHexMesh(nparts)
# GridGraphFromData(mesh.cellvefs,mesh.vefcells)
#end
"""
Construct an `UnstructuredGrid` from a `CartesianGrid`
"""
function UnstructuredGrid(grid::CartesianGrid{D}) where D
ps = _compute_points(grid)
ts = celltypes(grid)
os = cellorders(grid)
data, ptrs = _compute_cells(grid,UnstructuredGrid{D,D})
UnstructuredGrid(ps,data,ptrs,ts,os)
end
"""
Construct an `FlexibleUnstructuredGrid` from a `CartesianGrid`
"""
function FlexibleUnstructuredGrid(grid::CartesianGrid{D}) where D
ps = _compute_points(grid)
ts = celltypes(grid)
os = cellorders(grid)
cs = _compute_cells(grid,FlexibleUnstructuredGrid{D,D})
FlexibleUnstructuredGrid(ps,cs,ts,os)
end
"""
DiscreteModel associated with a CartesianGrid
"""
struct CartesianDiscreteModel{D} <: DiscreteModel{D}
cgrid::CartesianGrid{D}
ugrid::UnstructuredGrid
gridgraph::FullGridGraph
end
function CartesianDiscreteModel(; args...)
cgrid = CartesianGrid(; args...)
ugrid = UnstructuredGrid(cgrid)
gridgraph = FullGridGraph(ugrid)
CartesianDiscreteModel(cgrid, ugrid, gridgraph)
end
Grid(model::CartesianDiscreteModel{D},::Val{D}) where D = model.cgrid
function Grid(model::CartesianDiscreteModel{D},::Val{Z}) where {D,Z}
ugrid = model.ugrid
gridgraph = model.gridgraph
face_to_vertices = connections(gridgraph,Z,0)
nfaces = length(face_to_vertices)
fcode = tuple([HEX_AXIS for i in 1:Z]...)
order = 1
@notimplementedif order != (cellorders(model.cgrid)).value
_points = points(ugrid)
_cells_data = face_to_vertices.data
_cells_ptrs = face_to_vertices.ptrs
_ctypes = ConstantCellValue(fcode,nfaces)
_corders = ConstantCellValue(order,nfaces)
UnstructuredGrid(
_points, _cells_data, _cells_ptrs, _ctypes, _corders)
end
FullGridGraph(model::CartesianDiscreteModel) = model.gridgraph
#@fverdugo precompute this result
function FaceLabels(model::CartesianDiscreteModel{D}) where D
dim_to_face_to_geolabel = Vector{Vector{Int}}(undef,D+1)
dim_to_offset = _generate_dim_to_offset(D)
interior_id = dim_to_offset[end]+1
boundary_id = -1
for d in 0:(D-1)
face_to_cells = connections(model.gridgraph,d,D)
cell_to_faces = connections(model.gridgraph,D,d)
offset = dim_to_offset[d+1]
face_to_geolabel = _generate_pre_geolabel(
face_to_cells,
cell_to_faces,
offset,
interior_id,boundary_id,d,D)
dim_to_face_to_geolabel[d+1] = face_to_geolabel
end
for d in 0:(D-2)
for j in (d+1):(D-1)
dface_to_jfaces = connections(model.gridgraph,d,j)
dface_to_geolabel = dim_to_face_to_geolabel[d+1]
jface_to_geolabel = dim_to_face_to_geolabel[j+1]
_fix_dface_geolabels!(
dface_to_geolabel,
jface_to_geolabel,
dface_to_jfaces,
interior_id,boundary_id)
end
end
_ncells = ncells(model.cgrid)
dim_to_face_to_geolabel[end] = ConstantCellValue(interior_id,_ncells)
phys_labels = [ [i] for i in 1:interior_id ]
push!(phys_labels,[i for i in 1:(interior_id-1)])
tag_to_name = ["physical_tag_$i" for i in 1:interior_id-1]
push!(tag_to_name,"interior")
push!(tag_to_name,"boundary")
FaceLabels(dim_to_face_to_geolabel, phys_labels, tag_to_name)
end
# Helpers
function _generate_dim_to_offset(D)
code = tuple(fill(HEX_AXIS,D)...)
polytope = Polytope(code)
dim_to_offset = zeros(Int,D+1)
for d in 1:D
dim_to_offset[d+1] = dim_to_offset[d] + num_nfaces(polytope,d-1)
end
dim_to_offset
end
function _generate_pre_geolabel(
face_to_cells,
cell_to_faces,
offset,
interior_id,
boundary_id,d,D)
nfaces = length(face_to_cells)
face_to_geolabel = fill(interior_id,nfaces)
max_ncells_around = 2^(D-d)
_generate_pre_geolabel_kernel!(
face_to_geolabel,
face_to_cells.data,
face_to_cells.ptrs,
cell_to_faces.data,
cell_to_faces.ptrs,
offset,boundary_id,max_ncells_around)
face_to_geolabel
end
function _generate_pre_geolabel_kernel!(
face_to_geolabel,
face_to_cells_data,
face_to_cells_ptrs,
cell_to_faces_data,
cell_to_faces_ptrs,
offset,boundary_id,max_ncells_around)
nfaces = length(face_to_geolabel)
for face in 1:nfaces
a = face_to_cells_ptrs[face]-1
ncells_around = face_to_cells_ptrs[face+1] - (a+1)
if ncells_around == 1
icell_around = 1
cell = face_to_cells_data[a+icell_around]
b = cell_to_faces_ptrs[cell]-1
nlfaces = cell_to_faces_ptrs[cell+1] - (b+1)
for lface in 1:nlfaces
face2 = cell_to_faces_data[b+lface]
if face == face2
face_to_geolabel[face] = lface + offset
break
end
end
elseif ncells_around != max_ncells_around
face_to_geolabel[face] = boundary_id
end
end
end
function _fix_dface_geolabels!(
dface_to_geolabel,
jface_to_geolabel,
dface_to_jfaces,
interior_id,boundary_id)
for (dface, jfaces) in enumerate(dface_to_jfaces)
if dface_to_geolabel[dface] != boundary_id
continue
end
for jface in jfaces
geolabel = jface_to_geolabel[jface]
if geolabel != interior_id && geolabel != boundary_id
dface_to_geolabel[dface] = geolabel
break
end
end
end
end
function _cartesiangrid(partition::NTuple{D,Int},domain,order) where D
if domain === nothing
_domain = [ i*(-1)^j for i in ones(D) for j in 1:2 ]
else
_domain = domain
end
dim_to_limits = tuple([(_domain[2*i-1],_domain[2*i]) for i in 1:D ]...)
extrusion = tuple(fill(HEX_AXIS,D)...)
dim_to_ncells = partition
@notimplementedif order != 1
CartesianGrid{D}(dim_to_limits,dim_to_ncells,extrusion,order)
end
struct CartesianGridPoints{D} <: IndexCellValue{Point{D,Float64},D}
dim_to_limits::NTuple{D,NTuple{2,Float64}}
dim_to_npoint::NTuple{D,Int}
end
size(self::CartesianGridPoints) = self.dim_to_npoint
IndexStyle(::Type{CartesianGridPoints{D}} where D) = IndexCartesian()
function getindex(self::CartesianGridPoints{D}, I::Vararg{Int, D}) where D
p = zero(MVector{D,Float64})
@inbounds for d in 1:D
xa = self.dim_to_limits[d][1]
xb = self.dim_to_limits[d][2]
p[d] = xa + (I[d]-1)*(xb-xa)/(self.dim_to_npoint[d]-1)
end
Point(p)
end
struct CartesianGridCells{D,L} <: IndexCellArray{Int,1,SVector{L,Int},D}
dim_to_ncell::SVector{D,Int}
end
function CartesianGridCells(dim_to_ncell::NTuple{D,Int}) where D
CartesianGridCells{D,2^D}(dim_to_ncell)
end
size(self::CartesianGridCells) = self.dim_to_ncell.data
IndexStyle(::Type{CartesianGridCells{D,L}} where {D,L}) = IndexCartesian()
function getindex(self::CartesianGridCells{D,L}, I::Vararg{Int, D}) where {D,L}
dim_to_ngpoint = 1 .+ self.dim_to_ncell
dim_to_nlpoint = @SVector fill(2,D)
offset = @SVector fill(1,D)
pointgids = LinearIndices(dim_to_ngpoint.data)
cellpointlids = CartesianIndices(dim_to_nlpoint.data)
cellgid = CartesianIndex(I...) - CartesianIndex(offset.data)
cellpointgids = cellpointlids .+ cellgid
ids = zero(MVector{L,Int})
@inbounds for (l,pgid) in enumerate(cellpointgids)
ids[l] = pointgids[pgid]
end
SVector{L,Int}(ids)
end
function _compute_points(grid::CartesianGrid{D}) where D
ps = Array{Point{D,Float64},1}(undef,(length(points(grid)),))
for (i,xi) in enumerate(points(grid))
ps[i] = xi
end
ps
end
function _compute_cells(grid::CartesianGrid{D},::Type{FlexibleUnstructuredGrid{D,D}}) where D
cs = [ Array{Int,1}(undef,(2^D,)) for i in 1:length(cells(grid)) ]
for (i,ci) in enumerate(cells(grid))
cs[i] .= ci
end
cs
end
function _compute_cells(grid::CartesianGrid{D},::Type{UnstructuredGrid{D,D}}) where D
ptrs = fill(2^D,(length(cells(grid))+1,))
length_to_ptrs!(ptrs)
data = zeros(Int,ptrs[end]-1)
_fill_cell_data!(data,cells(grid))
(data, ptrs)
end
function _fill_cell_data!(data,cells)
k = 1
for v in cells
for vi in v
@inbounds data[k] = vi
k +=1
end
end
end
end # module