-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathCartesianGrids.jl
219 lines (178 loc) · 5.3 KB
/
CartesianGrids.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
# Descriptor of a cartesian grid
"""
struct CartesianDescriptor{D,T,F<:Function}
origin::Point{D,T}
sizes::Point{D,T}
partition::Point{D,Int}
map::F
end
Struct that stores the data defining a Cartesian grid.
"""
struct CartesianDescriptor{D,T,F<:Function} <: GridapType
origin::Point{D,T}
sizes::Point{D,T}
partition::Point{D,Int}
map::F
@doc """
CartesianDescriptor(origin,sizes,partition,map::Function=identity)
"""
function CartesianDescriptor(origin,sizes,partition,map::Function=identity)
D = length(partition)
T = eltype(sizes)
F = typeof(map)
new{D,T,F}(origin,sizes,partition,map)
end
end
"""
CartesianDescriptor(domain,partition,map::Function=identity)
"""
function CartesianDescriptor(domain,partition,map::Function=identity)
D = length(partition)
limits = [(domain[2*d-1],domain[2*d]) for d in 1:D]
sizes = [(limits[d][2]-limits[d][1])/partition[d] for d in 1:D]
origin = [ limits[d][1] for d in 1:D]
CartesianDescriptor(origin,sizes,partition,map)
end
"""
CartesianDescriptor(
pmin::Point{D},pmax::Point{D},partition,map::Function=identity) where D
"""
function CartesianDescriptor(
pmin::Point{D},pmax::Point{D},partition,map::Function=identity) where D
T = eltype(pmin)
domain = zeros(T,2*D)
for d in 1:D
domain[2*(d-1)+1] = pmin[d]
domain[2*(d-1)+2] = pmax[d]
end
CartesianDescriptor(domain,partition,map)
end
# Coordinates
struct CartesianCoordinates{D,T,F} <: AbstractArray{Point{D,T},D}
data::CartesianDescriptor{D,T,F}
end
Base.size(a::CartesianCoordinates) = Tuple(a.data.partition) .+ 1
Base.IndexStyle(::Type{<:CartesianCoordinates}) = IndexCartesian()
function Base.getindex(a::CartesianCoordinates{D,T}, I::Vararg{Int,D}) where {D,T}
p = zero(mutable(Point{D,T}))
x0 = a.data.origin
dx = a.data.sizes
@inbounds for d in 1:D
p[d] = x0[d] + (I[d]-1)*dx[d]
end
a.data.map(Point(p))
end
# Cell nodes
struct CartesianCellNodes{D} <: AbstractArray{Vector{Int},D}
partition::Point{D,Int}
function CartesianCellNodes(partition)
D = length(partition)
new{D}(partition)
end
end
Base.size(a::CartesianCellNodes) = Tuple(a.partition)
Base.IndexStyle(::Type{<:CartesianCellNodes}) = IndexCartesian()
function array_cache(a::CartesianCellNodes{D}) where D
zeros(Int,2^D)
end
@inline function getindex!(cache,a::CartesianCellNodes,i::Integer)
cis = CartesianIndices(size(a))
ci = cis[i]
getindex!(cache,a,Tuple(ci)...)
end
@inline function getindex!(v,a::CartesianCellNodes{1},i::Integer)
v[1] = i
v[2] = i+1
v
end
@inline function getindex!(v,a::CartesianCellNodes{D},i::Integer...) where D
nodes = LinearIndices(size(a).+1)
lnodes = CartesianIndices(tfill(2,Val{D}()))
j = i .- 1
for (i,lnode) in enumerate(lnodes)
k = Tuple(lnode) .+ j
v[i] = nodes[ k... ]
end
v
end
function Base.getindex(a::CartesianCellNodes,i::Integer...)
cache = array_cache(a)
getindex!(cache,a,i...)
end
# Cartesian Grid
"""
struct CartesianGrid{D,T,F} <: Grid{D,D}
# private fields
end
"""
struct CartesianGrid{D,T,F} <: Grid{D,D}
node_coords::CartesianCoordinates{D,T,F}
cell_nodes::CartesianCellNodes{D}
cell_type::Fill{Int8,1,Tuple{Base.OneTo{Int}}}
@doc """
CartesianGrid(desc::CartesianDescriptor)
"""
function CartesianGrid(desc::CartesianDescriptor{D,T,F}) where {D,T,F}
node_coords = CartesianCoordinates(desc)
cell_nodes = CartesianCellNodes(desc.partition)
cell_type = Fill(Int8(1),length(cell_nodes))
new{D,T,F}(node_coords,cell_nodes,cell_type)
end
end
OrientationStyle(::Type{<:CartesianGrid}) = Val{true}()
"""
get_cartesian_descriptor(grid::CartesianGrid)
Get the descriptor of the Cartesian grid
"""
function get_cartesian_descriptor(a::CartesianGrid)
a.node_coords.data
end
get_node_coordinates(g::CartesianGrid) = g.node_coords
get_cell_type(g::CartesianGrid) = g.cell_type
get_cell_nodes(g::CartesianGrid) = g.cell_nodes
function get_reffes(g::CartesianGrid{D}) where D
p = Polytope(tfill(HEX_AXIS,Val{D}()))
order = 1
reffe = LagrangianRefFE(Float64,p,order)
[reffe,]
end
get_reffes(g::CartesianGrid{1}) = [SEG2,]
get_reffes(g::CartesianGrid{2}) = [QUAD4,]
get_reffes(g::CartesianGrid{3}) = [HEX8,]
"""
CartesianGrid(domain,partition,map::Function=identity)
"""
function CartesianGrid(domain,partition,map::Function=identity)
desc = CartesianDescriptor(domain,partition,map)
CartesianGrid(desc)
end
# Cell map
struct CartesianMap{D,T,L} <: AbstractArray{AffineMap{D,T,L},D}
data::CartesianDescriptor{D,T,typeof(identity)}
function CartesianMap(des::CartesianDescriptor{D,T}) where {D,T}
L = D*D
new{D,T,L}(des)
end
end
Base.size(a::CartesianMap) = Tuple(a.data.partition)
Base.IndexStyle(::Type{<:CartesianMap}) = IndexCartesian()
function Base.getindex(a::CartesianMap{D,T},I::Vararg{Int,D}) where {D,T}
p = zero(mutable(Point{D,T}))
x0 = a.data.origin
dx = a.data.sizes
@inbounds for d in 1:D
p[d] = x0[d] + (I[d]-1)*dx[d]
end
origin = Point(p)
jacobian = diagonal_tensor(dx)
AffineMap(jacobian,origin)
end
function field_array_gradient(a::CartesianMap)
dx = a.data.sizes
jacobian = diagonal_tensor(dx)
j = AffineMapGrad(jacobian)
Fill(j,length(a))
end
function get_cell_map(grid::CartesianGrid{D,T,typeof(identity)} where {D,T})
CartesianMap(grid.node_coords.data)
end