Export

Tesserae uses VTK files to write simulation states that can be opened in ParaView. Grid fields and particle fields can be exported separately, combined into a multiblock dataset, or collected as a time series.

The export functions are built on WriteVTK.jl.

VTK file

To export a mesh or particles, use openvtk and closevtk. First, prepare a grid and particles:

GridProp = @NamedTuple begin
    x  :: Vec{2, Float64}
    m  :: Float64
    mv :: Vec{2, Float64}
    f  :: Vec{2, Float64}
end
ParticleProp = @NamedTuple begin
    x :: Vec{2, Float64}
    m :: Float64
    v :: Vec{2, Float64}
end

grid = generate_grid(GridProp, CartesianMesh(1, (0, 3), (0, 4)))
particles = generate_particles(ParticleProp, grid.x)

The grid properties can be exported as

vtk_grid = openvtk("grid", grid.x)
vtk_grid["Momentum"] = grid.mv
vtk_grid["Force"] = grid.f
closevtk(vtk_grid)
1-element Vector{String}:
 "grid.vts"

This can also be written using a do block, which automatically closes the file:

openvtk("grid", grid.x) do vtk
    vtk["Momentum"] = grid.mv
    vtk["Force"] = grid.f
end
1-element Vector{String}:
 "grid.vts"

Particles can be exported in the same way:

# without do block
vtk_particles = openvtk("particles", particles.x)
vtk_particles["Velocity"] = particles.v
closevtk(vtk_particles)

# with do block
openvtk("particles", particles.x) do vtk
    vtk["Velocity"] = particles.v
end
1-element Vector{String}:
 "particles.vtu"

Multiblock data set

Sometimes, we want to export both the grid and particles into a single dataset. This can be done using a vtm file with the openvtm and closevtm functions.

openvtm("grid_and_particles") do vtm
    openvtk(vtm, grid.x) do vtk
        vtk["Momentum"] = grid.mv
        vtk["Force"] = grid.f
    end
    openvtk(vtm, particles.x) do vtk
        vtk["Velocity"] = particles.v
    end
end
3-element Vector{String}:
 "grid_and_particles.vtm"
 "grid_and_particles_1.vts"
 "grid_and_particles_2.vtu"

ParaView collection file

A ParaView collection file (pvd) represents a time series of VTK files.

filename = "Simulation"
closepvd(openpvd(filename)) # Just create a file by `closepvd`

for (step, t) in enumerate(range(0, 10, step=0.5))

    # Simulation...

    # Reopening and closing the file at each time step allows us
    # to visualize intermediate results without having to wait
    # for the simulation to finish.
    openpvd(filename; append=true) do pvd
        openvtm(string(filename, step)) do vtm
            openvtk(vtm, grid.x) do vtk
                vtk["Momentum"] = grid.mv
                vtk["Force"] = grid.f
            end
            openvtk(vtm, particles.x) do vtk
                vtk["Velocity"] = particles.v
            end
            pvd[t] = vtm # save to pvd!
        end
    end
end