-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVisualization.jl
254 lines (228 loc) · 6.06 KB
/
Visualization.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
"""
"""
struct DistributedVisualizationData{A<:AbstractArray}
visdata::A
end
local_views(d::DistributedVisualizationData) = d.visdata
function Base.getproperty(x::DistributedVisualizationData, sym::Symbol)
if sym == :grid
map(i->i.grid,x.visdata)
elseif sym == :filebase
r=nothing
map(x.visdata) do visdata
r = visdata.filebase
end
r
elseif sym == :celldata
map(i->i.celldata,x.visdata)
elseif sym == :nodaldata
map(i->i.nodaldata,x.visdata)
else
getfield(x, sym)
end
end
function Base.propertynames(x::DistributedVisualizationData, private::Bool=false)
(fieldnames(typeof(x))...,:grid,:filebase,:celldata,:nodaldata)
end
# Define how each object is visualized
function Visualization.visualization_data(
model::DistributedDiscreteModel{Dc},
filebase::AbstractString;
labels=get_face_labeling(model)
) where Dc
cell_gids = get_cell_gids(model)
vd = map(local_views(model),partition(cell_gids),labels.labels) do model,gids,labels
part = part_id(gids)
vd = visualization_data(model,filebase;labels=labels)
vd_cells = vd[end]
push!(vd_cells.celldata, "gid" => local_to_global(gids))
push!(vd_cells.celldata, "part" => local_to_owner(gids))
vd
end
r = []
for i in 0:Dc
push!(r,DistributedVisualizationData(map(x->x[i+1],vd)))
end
r
end
function Visualization.visualization_data(
trian::DistributedTriangulation,
filebase::AbstractString;
order=-1,
nsubcells=-1,
celldata=nothing,
cellfields=nothing
)
trians = trian.trians
cell_gids = get_cell_gids(trian.model)
cdat = _prepare_cdata(trians,celldata)
fdat = _prepare_fdata(trians,cellfields)
vd = map(
partition(cell_gids),trians,cdat,fdat) do lindices,trian,celldata,cellfields
part = part_id(lindices)
_celldata = Dict{Any,Any}(celldata)
# we do not use "part" since it is likely to be used by the user
if haskey(_celldata,"piece")
@unreachable "piece is a reserved cell data name"
end
_celldata["piece"] = fill(part,num_cells(trian))
vd = visualization_data(
trian,filebase;
order=order,nsubcells=nsubcells,
celldata=_celldata,cellfields=cellfields)
@assert length(vd) == 1
vd[1]
end
[DistributedVisualizationData(vd)]
end
function _prepare_cdata(trians,a::Nothing)
map(trians) do t
Dict()
end
end
function _prepare_cdata(trians,a)
if length(a) == 0
return map(trians) do t
Dict()
end
end
ks = []
vs = []
for (k,v) in a
push!(ks,k)
push!(vs,v)
end
map(vs...) do vs...
b = []
for i in 1:length(vs)
push!(b,ks[i]=>vs[i])
end
b
end
end
function _prepare_fdata(trians,a::Nothing)
map(trians) do t
Dict()
end
end
function _prepare_fdata(trians,a)
_fdata(v::DistributedCellField,trians) = v.fields
_fdata(v::AbstractArray,trians) = v
_fdata(v,trians) = map(ti->v,trians)
if length(a) == 0
return map(trians) do t
Dict()
end
end
ks = []
vs = []
for (k,v) in a
push!(ks,k)
push!(vs,_fdata(v,trians))
end
map(vs...) do vs...
b = []
for i in 1:length(vs)
push!(b,ks[i]=>vs[i])
end
b
end
end
# Vtk related
function Visualization.write_vtk_file(
parts::AbstractArray,
grid::AbstractArray{<:Grid}, filebase; celldata, nodaldata,
compress=false,append=true,ascii=false,vtkversion=:default
)
pvtk = Visualization.create_vtk_file(
parts,grid,filebase;celldata=celldata,nodaldata=nodaldata,
compress=compress,append=append,ascii=ascii,vtkversion=vtkversion
)
map(pvtk) do pvtk
if !isnothing(pvtk)
vtk_save(pvtk)
end
end
end
function Visualization.create_vtk_file(
parts::AbstractArray,
grid::AbstractArray{<:Grid},
filebase;
celldata, nodaldata,
compress=false,append=true,ascii=false,vtkversion=:default
)
nparts, new_parts = filter_empty_parts(parts,grid)
map(new_parts,grid,celldata,nodaldata) do part,g,c,n
if part > 0
Visualization.create_pvtk_file(
g,filebase;
part=part,nparts=nparts,
celldata=c,nodaldata=n,
compress=compress,append=append,ascii=ascii,vtkversion=vtkversion
)
end
end
end
function filter_empty_parts(parts,grid)
notempty = map(g -> Int(num_cells(g) > 0), grid)
new_parts = scan(+,notempty,type=:inclusive,init=0)
new_parts = map(new_parts,notempty) do part, e
iszero(e) ? -1 : part
end
nparts = reduce(+,notempty)
return nparts, new_parts
end
const DistributedModelOrTriangulation = Union{DistributedDiscreteModel,DistributedTriangulation}
function Visualization.writevtk(
arg::DistributedModelOrTriangulation,args...;
compress=false,append=true,ascii=false,vtkversion=:default,kwargs...
)
parts = get_parts(arg)
map(visualization_data(arg,args...;kwargs...)) do visdata
write_vtk_file(
parts,visdata.grid,visdata.filebase,celldata=visdata.celldata,nodaldata=visdata.nodaldata,
compress=compress, append=append, ascii=ascii, vtkversion=vtkversion
)
end
end
function Visualization.createvtk(
arg::DistributedModelOrTriangulation,args...;
compress=false,append=true,ascii=false,vtkversion=:default,kwargs...
)
v = visualization_data(arg,args...;kwargs...)
parts = get_parts(arg)
@notimplementedif length(v) != 1
visdata = first(v)
Visualization.create_vtk_file(
parts,visdata.grid,visdata.filebase,celldata=visdata.celldata,nodaldata=visdata.nodaldata,
compress=compress, append=append, ascii=ascii, vtkversion=vtkversion
)
end
struct DistributedPvd{T<:AbstractArray}
pvds::T
end
function Visualization.createpvd(parts::AbstractArray,args...;kwargs...)
pvds = map_main(parts) do part
paraview_collection(args...;kwargs...)
end
DistributedPvd(pvds)
end
function Visualization.createpvd(f,parts::AbstractArray,args...;kwargs...)
pvd = createpvd(parts,args...;kwargs...)
try
f(pvd)
finally
savepvd(pvd)
end
end
function Visualization.savepvd(pvd::DistributedPvd)
map_main(pvd.pvds) do pvd
vtk_save(pvd)
end
end
function Base.setindex!(pvd::DistributedPvd,pvtk::AbstractArray,time::Real)
map(vtk_save,pvtk)
map_main(pvtk,pvd.pvds) do pvtk,pvd
pvd[time] = pvtk
end
end