|
| 1 | +using LightXML |
| 2 | +using WriteVTK, Printf |
| 3 | + |
| 4 | +export make_paraview_collection |
| 5 | + |
| 6 | +""" |
| 7 | + make_paraview_collection(; dir=pwd(), pvd_name=nothing, files=nothing, file_extension = ".vts", time = nothing) |
| 8 | +
|
| 9 | +In case one has a list of `*.vtk` files, this routine creates a `*.pvd` file that can be opened in Paraview. |
| 10 | +This is useful if you previously saved vtk files but didnt save it as a collection in the code itself. |
| 11 | +
|
| 12 | +Optional options |
| 13 | +=== |
| 14 | +- `dir`: directory where the `*.vtk` are stored. |
| 15 | +- `pvd_name`: filename of the resulting `*.pvd` file without extension; if not specified, `full_simulation` is used. |
| 16 | +- `files`: Vector of the `*.vtk` files without extension; if not specified, all `*.vtk` files in the directory are used. |
| 17 | +- `file_extension`: file extension of the vtk files. Default is `.vts` but all `vt*` work. |
| 18 | +- `time`: Vector of the timesteps; if not specified, pseudo time steps are assigned. |
| 19 | +""" |
| 20 | +function make_paraview_collection(; dir=pwd(), pvd_name=nothing, files=nothing, file_extension = ".vts", time = nothing) |
| 21 | + |
| 22 | + # if no files are given, use all vtm files in the directory |
| 23 | + curdir = pwd() |
| 24 | + cd(dir) |
| 25 | + if isnothing(files) |
| 26 | + files = filter(endswith(file_extension), readdir()) |
| 27 | + files = joinpath.(dir, files) |
| 28 | + end |
| 29 | + # if no time is given, use pseudo time steps |
| 30 | + if isnothing(time) |
| 31 | + time = [string(i) for i in 1:length(files)] |
| 32 | + end |
| 33 | + # Check that the arrays have the same length |
| 34 | + @assert length(time) == length(files) |
| 35 | + @assert length(files) > 0 |
| 36 | + |
| 37 | + # Create a new paraview collection |
| 38 | + if isnothing(pvd_name) |
| 39 | + pvd_name = "full_simulation" |
| 40 | + end |
| 41 | + |
| 42 | + pvd_name = joinpath(curdir, pvd_name) |
| 43 | + |
| 44 | + make_paraview_collection(pvd_name, files, time) |
| 45 | + cd(curdir) # return to original directory |
| 46 | + return pvd_name |
| 47 | +end |
| 48 | + |
| 49 | +""" |
| 50 | + make_paraview_collection(pvd_name::String, files::Vector{String}, time::Vector{String}) |
| 51 | +This function makes a `*.pvd` file from a provided list of `*.vtk` files and time steps. |
| 52 | +
|
| 53 | +Inputs are: |
| 54 | +- `pvd_name`: filename of the resulting `*.pvd` file without extension. |
| 55 | +- `files`: Vector of desired files to be included in the `*.pvd` file. |
| 56 | +- `time`: Vector of time steps corresponding to the `files` vector. |
| 57 | +""" |
| 58 | + |
| 59 | +function make_paraview_collection(pvd_name::String, files::Vector{String}, time::Vector{String}) |
| 60 | + # Check that the arrays have the same length |
| 61 | + @assert length(time) == length(files) |
| 62 | + |
| 63 | + # Create a new paraview collection |
| 64 | + pvd = paraview_collection(pvd_name) |
| 65 | + # save the collection |
| 66 | + vtk_save(pvd) |
| 67 | + |
| 68 | + # Parse the XML file |
| 69 | + xdoc = parse_file("$pvd_name.pvd") |
| 70 | + |
| 71 | + # Find the <Collection> element |
| 72 | + collection = find_element(root(xdoc), "Collection") |
| 73 | + |
| 74 | + # Loop over the time and data values |
| 75 | + for i in 1:length(time) |
| 76 | + # Create a new <DataSet> element |
| 77 | + new_dataset = new_child(collection, "DataSet") |
| 78 | + # Set the attributes of the new <DataSet> element |
| 79 | + set_attribute(new_dataset, "timestep", time[i]) |
| 80 | + set_attribute(new_dataset, "part", "0") |
| 81 | + set_attribute(new_dataset, "file", files[i]) |
| 82 | + end |
| 83 | + |
| 84 | + # Write the modified XML to a new file of your choice (or overwrite the original) |
| 85 | + open("$pvd_name.pvd", "w") do f |
| 86 | + print(f, xdoc) |
| 87 | + end |
| 88 | + # pretty print the xml file |
| 89 | + str = read("$pvd_name.pvd", String) |
| 90 | + str_out = replace(str, "/>" => "/>\n") |
| 91 | + open("$pvd_name.pvd", "w") do f |
| 92 | + print(f, str_out) |
| 93 | + end |
| 94 | + return pvd_name |
| 95 | +end |
0 commit comments