Skip to content

Commit

Permalink
Paths in parallel VTK files are now relative to that file (#141)
Browse files Browse the repository at this point in the history
* Use relative paths in paralel VTK files

* Modify test
  • Loading branch information
jipolanco authored Apr 4, 2024
1 parent 7eb6761 commit bdb309e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
32 changes: 26 additions & 6 deletions src/gridtypes/pvtk_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ function pvtk_grid(
nparts = _pvtk_nparts(is_structured; kwargs...)
extents = _pvtk_extents(is_structured; kwargs...)

mkpath(filename)
bname = basename(filename)
prefix = joinpath(filename, bname)
fn = _serial_filename(part, nparts, prefix, "")
dir_serial = filename * "_dir" # directory where serial files will be written
mkpath(dir_serial)
prefix = _pvtk_vtk_filename_prefix(filename; relative_to_pvtk = false, create_dirs = true)
filename_serial = _serial_filename(part, nparts, prefix, "")

vtk = let kws_vtk = _remove_parallel_kwargs(; kwargs...)
kws = if extents === nothing
kws_vtk
else
(; kws_vtk..., extent = extents[part])
end
vtk_grid(fn, args...; kws...)
vtk_grid(filename_serial, args...; kws...)
end

pvtkargs = PVTKArgs(part, nparts, ismain, ghost_level)
Expand Down Expand Up @@ -200,6 +200,26 @@ function _serial_filename(part, nparts, prefix, extension)
prefix * "_$p" * extension
end

# Determine base filename (or "prefix") for serial VTK files included in a parallel VTK
# file.
# Here `path` is the basename of the main pvtk file (without the extension).
# Note that it can be in a subdirectory of `.`, and it can either be an absolute or relative
# path.
# If it is an absolute value, then always return an absolute value.
function _pvtk_vtk_filename_prefix(path; relative_to_pvtk, create_dirs = false)
dir_serial = path # directory where serial files will be written
if create_dirs
mkpath(dir_serial)
end
bname = basename(path)
if isabspath(path) || !relative_to_pvtk
joinpath(dir_serial, bname)
else
_, reldir = splitdir(dir_serial)
joinpath(reldir, bname)
end
end

function _init_pvtk!(pvtk::PVTKFile, extents)
# Recover some data
vtk = pvtk.vtk
Expand All @@ -208,7 +228,7 @@ function _init_pvtk!(pvtk::PVTKFile, extents)
npieces = pvtkargs.nparts
pref, _ = splitext(pvtk.path)
_, ext = splitext(vtk.path)
prefix = joinpath(pref, basename(pref))
prefix = _pvtk_vtk_filename_prefix(pref; relative_to_pvtk = true)

# VTKFile (root) node
pvtk_root = create_root(pvtk.xdoc, "VTKFile")
Expand Down
14 changes: 7 additions & 7 deletions test/checksums.sha1
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ e7ff55d7c8bb2c1739a2dc73c0c1fdeff2f7d536 prectilinear/prectilinear_1.vtr
b8b456b99f45f45e2e0fcc0de56963185b3f24ec prectilinear/prectilinear_4.vtr
a6fd8a0367efc31e7616dd7046f048c424aa8ae0 prectilinear/prectilinear_5.vtr
b1ef0b3a9e3c25781c31e0d2ceb552c78dab23f8 prectilinear/prectilinear_6.vtr
d9c0b2367bf7ba42534a134b3ef4dace753560a9 pstructured.pvts
92ff18a7c1c631800de3adc3b0bd4631e16be845 pstructured/pstructured_1.vts
9bb94bd6b2374156e60df995340e3b395f2ae6b9 pstructured/pstructured_2.vts
ad4b841fdf277c6138bbd763f4db2a359c46e400 pstructured/pstructured_3.vts
651905ba5698b503f58d40fc1051267fefa4b49e pstructured/pstructured_4.vts
bba1504744a03b3f9594fcfe63435b7311e030fe pstructured/pstructured_5.vts
2e8acea4c25a7ef64c25c05ff3f65b3890a52e72 pstructured/pstructured_6.vts
d9c0b2367bf7ba42534a134b3ef4dace753560a9 pvtk_structured_output/pstructured.pvts
92ff18a7c1c631800de3adc3b0bd4631e16be845 pvtk_structured_output/pstructured/pstructured_1.vts
9bb94bd6b2374156e60df995340e3b395f2ae6b9 pvtk_structured_output/pstructured/pstructured_2.vts
ad4b841fdf277c6138bbd763f4db2a359c46e400 pvtk_structured_output/pstructured/pstructured_3.vts
651905ba5698b503f58d40fc1051267fefa4b49e pvtk_structured_output/pstructured/pstructured_4.vts
bba1504744a03b3f9594fcfe63435b7311e030fe pvtk_structured_output/pstructured/pstructured_5.vts
2e8acea4c25a7ef64c25c05ff3f65b3890a52e72 pvtk_structured_output/pstructured/pstructured_6.vts
6 changes: 5 additions & 1 deletion test/pvtk_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ function pvtk_rectilinear()
collect(Iterators.flatten(filenames))
end

# In this test we write to a subdirectory using relative paths.
function pvtk_structured()
Ns, extents = make_structured_partition()
nparts = length(extents) # number of "processes"
filenames = Vector{Vector{String}}(undef, nparts)
outdir = "pvtk_structured_output"
mkpath(outdir)
outname = joinpath(outdir, "pstructured")
@sync for (n, extent) enumerate(extents)
@spawn begin
points = [
Expand All @@ -121,7 +125,7 @@ function pvtk_structured()
point_data = map(sum, points)
processid = fill(n, length.(extent) .- 1) # cell data
filenames[n] = pvtk_grid(
"pstructured", points;
outname, points;
part = n, extents = extents,
append = false, compress = false,
) do vtk
Expand Down

0 comments on commit bdb309e

Please sign in to comment.