Skip to content

Commit 42ff61d

Browse files
committed
add write_paraview for FEData
1 parent 6025e26 commit 42ff61d

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

src/Paraview_output.jl

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ end
196196

197197
"""
198198
write_paraview(DataSet::Q1Data, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
199-
Writes a `Q1Data` dataset to disk, whicgh has cell and vertex field
199+
Writes a `Q1Data` dataset to disk, which has cell and vertex field
200200
"""
201201
function write_paraview(DataSet::Q1Data, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
202202

@@ -262,6 +262,98 @@ function write_paraview(DataSet::Q1Data, filename="test"; directory=nothing, pvd
262262
end
263263

264264

265+
outfiles = vtk_save(vtkfile);
266+
if verbose
267+
println("Saved file: $(outfiles[1])")
268+
end
269+
if !isnothing(pvd)
270+
# Write movie
271+
pvd[time] = vtkfile
272+
end
273+
274+
return pvd
275+
end
276+
277+
278+
279+
280+
"""
281+
write_paraview(DataSet::FEData, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
282+
Writes a `FEData` dataset (general finite element) to disk, which has cell and vertex field
283+
"""
284+
function write_paraview(DataSet::FEData, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
285+
286+
# Create directory if required
287+
if !isnothing(directory)
288+
mkpath(directory)
289+
filename = joinpath(directory, filename); # add directory name to pathname
290+
end
291+
292+
connectivity = DataSet.connectivity
293+
if size(DataSet.connectivity,1) == 4
294+
celltype = VTKCellTypes.VTK_TETRA
295+
296+
elseif size(DataSet.connectivity,1) == 8
297+
celltype = VTKCellTypes.VTK_HEXAHEDRON
298+
299+
# we need to reorder this as pTatin uses a different ordering than VTK
300+
id_reorder = [1,2,4,3,5,6,8,7]
301+
connectivity = connectivity[id_reorder,:]
302+
else
303+
error("This element is not yet implemented")
304+
end
305+
306+
# Create VTU file
307+
points = DataSet.vertices
308+
cells = MeshCell[];
309+
for i = 1: size(connectivity,2)
310+
push!(cells, MeshCell(celltype, connectivity[:,i]))
311+
end
312+
313+
vtkfile = vtk_grid(filename, points, cells)
314+
315+
# Add vertex data fields to VT* file
316+
names = String.(collect(keys(DataSet.fields))); # this is how to retrieve the names of the data fields
317+
for (index, name) in enumerate(names)
318+
319+
if typeof(DataSet.fields[index])<: Tuple
320+
# if we do a tuple of velocities, it appears difficult to deal with units
321+
# This will require some more work
322+
unit_name = ""
323+
Data = DataSet.fields[index]
324+
if unit(Data[1][1])!=NoUnits
325+
error("potential error as vector data fields have units; please save them with no units!")
326+
end
327+
else
328+
unit_name = unit(DataSet.fields[index][1])
329+
Data = ustrip.(DataSet.fields[index])
330+
end
331+
332+
name_with_units = join([name," [$(unit_name)]"]); # add units to the name of the field
333+
vtkfile[name_with_units] = Data;
334+
end
335+
336+
# Add cell data fields to VT* file
337+
names = String.(collect(keys(DataSet.cellfields))); # this is how to retrieve the names of the data fields
338+
for (index, name) in enumerate(names)
339+
340+
if typeof(DataSet.cellfields[index])<: Tuple
341+
# if we do a tuple of velocities, it appears difficult to deal with units
342+
# This will require some more work
343+
unit_name = ""
344+
Data = DataSet.cellfields[index]
345+
if unit(Data[1][1])!=NoUnits
346+
error("potential error as vector data fields have units; please save them with no units!")
347+
end
348+
else
349+
unit_name = unit(DataSet.cellfields[index][1])
350+
Data = ustrip.(DataSet.cellfields[index])
351+
end
352+
353+
name_with_units = join([name," [$(unit_name)]"]); # add units to the name of the field
354+
vtkfile[name_with_units, VTKCellData()] = Data[:];
355+
end
356+
265357
outfiles = vtk_save(vtkfile);
266358
if verbose
267359
println("Saved file: $(outfiles[1])")

0 commit comments

Comments
 (0)