Skip to content

Commit f625b5d

Browse files
authored
Allow setting component names in PVTK files (#154)
1 parent 79ed09b commit f625b5d

4 files changed

Lines changed: 56 additions & 30 deletions

File tree

src/gridtypes/pvtk_grid.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,21 @@ end
137137

138138
# Add point and cell data as usual
139139
function Base.setindex!(
140-
pvtk::PVTKFile, data, name::AbstractString, loc::AbstractFieldData,
140+
pvtk::PVTKFile, data, name::AbstractString, loc::AbstractFieldData;
141+
component_names::Union{Nothing, ListOfStrings} = nothing
141142
)
142-
pvtk.vtk[name, loc] = data
143+
setindex!(pvtk.vtk, data, name, loc; component_names = component_names)
143144
end
144145

145146
# In the case of field data, also add it to the pvtk file.
146147
# Otherwise field data (typically the time / "TimeValue") is not seen by ParaView.
147148
function Base.setindex!(
148-
pvtk::PVTKFile, data, name::AbstractString, loc::VTKFieldData,
149+
pvtk::PVTKFile, data, name::AbstractString, loc::VTKFieldData;
150+
component_names::Union{Nothing, ListOfStrings} = nothing,
149151
)
150-
pvtk.vtk[name, loc] = data
152+
setindex!(pvtk.vtk, data, name, loc; component_names = component_names)
151153
if pvtk.pvtkargs.ismain
152-
add_field_data(pvtk, data, name, loc)
154+
add_field_data(pvtk, data, name, loc; component_names = component_names)
153155
end
154156
data
155157
end
@@ -164,10 +166,11 @@ end
164166

165167
# If `loc` was not passed, try to guess which kind of data was passed.
166168
function Base.setindex!(
167-
pvtk::PVTKFile, data, name::AbstractString,
169+
pvtk::PVTKFile, data, name::AbstractString;
170+
component_names::Union{Nothing, ListOfStrings} = nothing
168171
)
169-
loc = guess_data_location(data, pvtk.vtk) :: AbstractFieldData
170-
pvtk[name, loc] = data
172+
loc = guess_data_location(data, pvtk.vtk)::AbstractFieldData
173+
setindex!(pvtk, data, name, loc; component_names = component_names)
171174
end
172175

173176
# Write XML attribute.
@@ -332,10 +335,18 @@ function _update_pvtk!(pvtk::PVTKFile)
332335
t = attribute(vtk_data_array, "type")
333336
name = attribute(vtk_data_array, "Name")
334337
Nc = attribute(vtk_data_array, "NumberOfComponents")
338+
Nc_val = parse(Int, Nc)
335339
pvtk_pdata_array = new_child(pvtk_ppoint_data, "PDataArray")
336340
set_attribute(pvtk_pdata_array, "type", t)
337341
set_attribute(pvtk_pdata_array, "Name", name)
338342
set_attribute(pvtk_pdata_array, "NumberOfComponents", Nc)
343+
for n in 1:Nc_val
344+
attr = "ComponentName$(n - 1)"
345+
component_name = attribute(vtk_data_array, attr)
346+
if component_name !== nothing
347+
set_attribute(pvtk_pdata_array, attr, component_name)
348+
end
349+
end
339350
end
340351
end
341352

src/write_data.jl

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ xml_data_array_name(::Union{String,ListOfStrings}) = "Array"
152152
data_to_xml(
153153
vtk::DatasetFile, xParent::XMLElement, data,
154154
name::AbstractString, Nc::Union{Int,AbstractFieldData} = 1;
155-
component_names::Union{AbstractVector, Nothing} = nothing
155+
component_names = nothing
156156
)
157157
158158
Add numerical data to VTK XML file.
@@ -164,8 +164,8 @@ In the latter case, the number of components will be deduced from the data
164164
dimensions and the type of field data.
165165
"""
166166
function data_to_xml(vtk, xParent, data, name,
167-
Nc::Union{Int,AbstractFieldData}=1;
168-
component_names::Union{AbstractVector,Nothing}=nothing)
167+
Nc::Union{Int,AbstractFieldData} = 1;
168+
component_names = nothing)
169169
xDA = new_child(xParent, xml_data_array_name(data))
170170
set_attribute(xDA, "type", datatype_str(data))
171171
set_attribute(xDA, "Name", name)
@@ -322,14 +322,16 @@ function data_to_xml_ascii(vtk::VTKFile, xDA::XMLElement, data)
322322
end
323323

324324
"""
325-
add_field_data(vtk::DatasetFile, data,
326-
name::AbstractString, loc::AbstractFieldData)
325+
add_field_data(
326+
vtk::DatasetFile, data, name::AbstractString, loc::AbstractFieldData;
327+
component_names = nothing,
328+
)
327329
328330
Add either point or cell data to VTK file.
329331
"""
330332
function add_field_data(vtk::VTKFile, data, name::AbstractString,
331333
loc::AbstractFieldData;
332-
component_names::Union{AbstractVector, Nothing}=nothing)
334+
component_names = nothing)
333335
xbase = find_base_xml_node_to_add_field(vtk, loc)
334336

335337
# Find or create "nodetype" (PointData, CellData or FieldData) node.
@@ -360,19 +362,23 @@ vtk_cell_data(args...; kwargs...) = add_field_data(args..., VTKCellData(); kwarg
360362
vtk_field_data(args...; kwargs...) = add_field_data(args..., VTKFieldData(); kwargs...)
361363

362364
"""
363-
setindex!(vtk::DatasetFile, data, name::AbstractString, [field_type])
365+
setindex!(vtk::DatasetFile, data, name::AbstractString, [field_type]; [component_names])
364366
365367
Add a new dataset to VTK file.
366368
367369
The number of components of the dataset (e.g. for scalar or vector fields) is
368370
determined automatically from the input data dimensions.
369371
370-
The optional argument `field_type` should be an instance of `VTKPointData`,
372+
The optional `field_type` argument should be an instance of `VTKPointData`,
371373
`VTKCellData` or `VTKFieldData`.
372374
It determines whether the data should be associated to grid points, cells or
373375
none.
374376
If not given, this is guessed from the input data size and the grid dimensions.
375377
378+
The optional `component_names` keyword argument allows to override the default component
379+
names when writing vector or tensor fields. It should be a tuple or a vector of strings (see
380+
below for an example).
381+
376382
# Example
377383
378384
Add "velocity" dataset and time scalar to VTK file.
@@ -382,20 +388,27 @@ vel = rand(3, 12, 14, 42) # vector field
382388
time = 42.0
383389
384390
vtk = vtk_grid(...)
385-
vtk["velocity", VTKPointData()] = vel
391+
vtk["velocity", VTKPointData(), component_names = ("Ux", "Uy", "Uz")] = vel
386392
vtk["time", VTKFieldData()] = time
387393
388394
# This also works, and will generally give the same result:
389-
vtk["velocity"] = vel
395+
vtk["velocity", component_names = ("Ux", "Uy", "Uz")] = vel
390396
vtk["time"] = time
391397
```
392398
"""
393-
Base.setindex!(vtk::DatasetFile, data, name::AbstractString,
394-
loc::AbstractFieldData; kwargs...) = add_field_data(vtk, data, name, loc; kwargs...)
399+
function Base.setindex!(
400+
vtk::DatasetFile, data, name::AbstractString, loc::AbstractFieldData;
401+
component_names::Union{Nothing, ListOfStrings} = nothing,
402+
)
403+
add_field_data(vtk, data, name, loc; component_names = component_names)
404+
end
395405

396-
function Base.setindex!(vtk::DatasetFile, data, name::AbstractString; kwargs...)
397-
loc = guess_data_location(data, vtk) :: AbstractFieldData
398-
setindex!(vtk, data, name, loc; kwargs...)
406+
function Base.setindex!(
407+
vtk::DatasetFile, data, name::AbstractString;
408+
component_names::Union{Nothing, ListOfStrings} = nothing
409+
)
410+
loc = guess_data_location(data, vtk)::AbstractFieldData
411+
setindex!(vtk, data, name, loc; component_names = component_names)
399412
end
400413

401414
"""

test/checksums.sha1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ a898a771d8e0962628118d73a216a3e4660a5295 pimage/pimage_2.vti
6868
4badae7ec804773110e662e105531e5e3d4fd288 pimage/pimage_4.vti
6969
99843bc0f87c436d9ed385b40707ee1e0327f709 pimage/pimage_5.vti
7070
4e37ea3120610664425712254b88d88f78d4e45d pimage/pimage_6.vti
71-
9c0d3e7ee95c6e7bfc2f0554972d31fb9d832172 prectilinear.pvtr
72-
e7ff55d7c8bb2c1739a2dc73c0c1fdeff2f7d536 prectilinear/prectilinear_1.vtr
73-
6b0b2f8e5d381ec4f9644905677be8f29f432af2 prectilinear/prectilinear_2.vtr
74-
68f065b66a0ff9ba80bc18776912f248ebc83390 prectilinear/prectilinear_3.vtr
75-
b8b456b99f45f45e2e0fcc0de56963185b3f24ec prectilinear/prectilinear_4.vtr
76-
a6fd8a0367efc31e7616dd7046f048c424aa8ae0 prectilinear/prectilinear_5.vtr
77-
b1ef0b3a9e3c25781c31e0d2ceb552c78dab23f8 prectilinear/prectilinear_6.vtr
71+
c70c5786fe01d09417c7a1772441910e0eb7ceda prectilinear.pvtr
72+
8f1d50d1da766a64a9d702752209f8c2c84b30b0 prectilinear/prectilinear_1.vtr
73+
548d706be5ac47cb64727f8de23bc39fbb4bf1b9 prectilinear/prectilinear_2.vtr
74+
ca43f918aa4807323f39029fc31dd4e80428f324 prectilinear/prectilinear_3.vtr
75+
ee05657f2065b5134f4763ac17e22c2ed0d0af2a prectilinear/prectilinear_4.vtr
76+
d507180d6ada6da6831f4375dc47db1d4320d4c7 prectilinear/prectilinear_5.vtr
77+
54dea6c985f95580167d768527dbd791ae60af90 prectilinear/prectilinear_6.vtr
7878
d9c0b2367bf7ba42534a134b3ef4dace753560a9 pvtk_structured_output/pstructured.pvts
7979
92ff18a7c1c631800de3adc3b0bd4631e16be845 pvtk_structured_output/pstructured/pstructured_1.vts
8080
9bb94bd6b2374156e60df995340e3b395f2ae6b9 pvtk_structured_output/pstructured/pstructured_2.vts

test/pvtk_grid.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ function pvtk_rectilinear()
9191
@spawn begin
9292
xs = map(is -> [sqrt(i) for i is], extent) # local grid
9393
point_data = map(sum, Iterators.product(xs...))
94+
vecdata = (1, 2, 3) .* Ref(point_data)
9495
processid = fill(n, length.(xs) .- 1) # cell data
9596
filenames[n] = pvtk_grid(
9697
"prectilinear", xs...;
9798
part = n, extents = extents,
9899
append = false, compress = false,
99100
) do vtk
100101
vtk["point_data"] = point_data
102+
vtk["vector", component_names = ("Ux", "Uy", "Uz")] = vecdata
101103
vtk["process_id"] = processid
102104
vtk["TimeValue"] = 3.4
103105
end

0 commit comments

Comments
 (0)