Skip to content

Add VTK Support #16

@daniellivingston

Description

@daniellivingston

In plain text, an example in pure Fortran:

module VTUWriter

    implicit none
    private

    type, public :: VTUFile
        integer :: file_unit
        char(len=500) :: filename
    end type

    public :: VTUOpenFile
end module

function VTUOpenFile(filename)

    implicit none
    type(VTUOpenFile), pointer :: vtu
    character(len=500) :: filename
    integer :: unit = 42

    allocate(f)

    vtu%unit = unit
    vtu%filename = filename

    open(unit = unit, file = filename, status = 'new')

    VTUOpenFile => vtu

end function

function VTUCloseFile(vtu)
    VTUOpenFile :: vtu
    close(vtu%unit)
end function

function VTUWriteHeader(vtu)
    implicit none

    type(VTUFile), pointer :: vtu
    write(vtu%unit, '(A)') '<VTKFile type="UnstructuredGrid" version="1.0" header_type="UInt64">'
    write(vtu%unit, '(A)') '<UnstructuredGrid>'
    write(vtu%unit, '(A)') '<Piece NumberOfPoints="', n_vertices, '" NumberOfCells="', n_elements, '">'

end function

function VTUWritePoints()
    write(vtu%unit, '(A)') '<Points>'
    write(vtu%unit, '(A)') '<DataArray type="Float32" NumberOfComponents="3" format="ascii">'

    do (i=1,n_points)
        write(vtu%unit, '(A)') points(i, 1), points(i, 2), points(i, 3)
    end do

    write(vtu%unit, '(A)') '</DataArray>'
    write(vtu%unit, '(A)') '</Points>'
end function

function VTUWriteCells()
    write(vtu%unit, '(A)') '<Cells>'
    write(vtu%unit, '(A)') '<DataArray type="Int64" Name="connectivity" format="ascii">'

    do i=1,n_cells
        do j=1,size(cells(i))
            write(vtu%unit, '(A)') cells(i, j)
        end do
    end do

    write(vtu%unit, '(A)') '</DataArray>'
    write(vtu%unit, '(A)') '</Cells>'
    ! https://github.com/polyfem/polyfem/blob/3f58d84cd0ad930b71e8c6db917fe46e8dd4e100/src/mesh/VTUWriter.cpp#L170
end function

function VTUWriteFooter()
    write(vtu%unit, '(A)') '</Piece>'
    write(vtu%unit, '(A)') '</UnstructuredGrid>'
    write(vtu%unit, '(A)') '</VTKFile>'
end function

function VTUWriteFile()
! https://github.com/polyfem/polyfem/blob/3f58d84cd0ad930b71e8c6db917fe46e8dd4e100/src/mesh/VTUWriter.cpp#L82:21
    vtu = VTUOpenFile(filename)

    VTUWriteHeader(vtu)
    VTUWritePoints(vtu, points)
    VTUWriteFooter(vtu)

    VTUCloseFile(vtu)
end function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions