Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve connectivity integer type in output files #107

Merged
merged 3 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/gridtypes/unstructured/polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ end
Base.eltype(::Type{<:VTKPolyhedron}) = VTKCellTypes.VTKCellType
cell_type(::VTKPolyhedron) = VTKCellTypes.VTK_POLYHEDRON

_connectivity_type(::Type{<:VTKPolyhedron{V}}) where {V} =
_connectivity_type(V)

faces(cell::VTKPolyhedron) = cell.faces

function process_faces!(data, cell::VTKPolyhedron, offset)
Expand Down
24 changes: 21 additions & 3 deletions src/gridtypes/unstructured/unstructured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,31 @@ end

cell_type(cell::MeshCell) = cell.ctype

# Obtain common integer type for holding connectivity indices.
# If all elements of `cells` have the same type, then the return type is equal
# to the element type of the `connectivity` field of each cell.
# Otherwise, fall back to Int.
function connectivity_type(cells)
Cell = eltype(cells) :: Type{<:AbstractMeshCell}
if isconcretetype(Cell)
_connectivity_type(Cell) :: Type{<:Integer}
else
Int
end
end

_connectivity_type(::Type{MeshCell{T,V}}) where {T,V} = _connectivity_type(V)
_connectivity_type(::Type{V}) where {V <: Connectivity} = eltype(V)

function add_cells!(vtk, xml_piece, number_attr, xml_name, cells;
with_types::Val=Val(true))
Ncls = length(cells)
write_types = with_types === Val(true)

IntType = connectivity_type(cells) :: Type{<:Integer}

# Create data arrays.
offsets = Array{Int}(undef, Ncls)
offsets = Array{IntType}(undef, Ncls)

# Write `types` array? This must be true for unstructured grids,
# and false for polydata.
Expand All @@ -93,11 +111,11 @@ function add_cells!(vtk, xml_piece, number_attr, xml_name, cells;
end

# Create connectivity array.
conn = Array{Int}(undef, Nconn)
conn = Array{IntType}(undef, Nconn)
n = 1
for c in cells, i in c.connectivity
# We transform to zero-based indexing, required by VTK.
conn[n] = i - 1
conn[n] = i - one(i)
n += 1
end

Expand Down
12 changes: 6 additions & 6 deletions test/checksums.sha1
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ d67bc56bd3aa8f24d368d27643bfa8b8dfdb3cf2 structured_2D_VectorOfSVector.vts
4121e5a6c966a4dba4a55eb958c60fe482562e60 structured_3D_MultipleVectors.vts
4121e5a6c966a4dba4a55eb958c60fe482562e60 structured_3D_SingleArray.vts
4121e5a6c966a4dba4a55eb958c60fe482562e60 structured_3D_VectorOfSVector.vts
4c216331f3754c7e946ff3bd73ec8e0f9875cb90 unstructured_1D.vtu
4c216331f3754c7e946ff3bd73ec8e0f9875cb90 unstructured_1D_tuple.vtu
1ea73796ca7240c4b83a55a39eb5ed467c6b577d unstructured_1D_SVector.vtu
2a39f5566559a0f7ce2ef24356a7bf7b1c90be2f unstructured_2D.vtu
2a39f5566559a0f7ce2ef24356a7bf7b1c90be2f unstructured_2D_tuple.vtu
ce49751a8b2cc14806b32e7dee0f7a6a5f2fc4ba unstructured_2D_SVector.vtu
469abd27dea107592adb0abcaefbb5ea039ba8a7 unstructured_1D.vtu
469abd27dea107592adb0abcaefbb5ea039ba8a7 unstructured_1D_tuple.vtu
1308ed306780dc0f47878ace582a093e27baa696 unstructured_1D_SVector.vtu
dc1fbc6ff5e7d52a8901944f2b96473e74315129 unstructured_2D.vtu
dc1fbc6ff5e7d52a8901944f2b96473e74315129 unstructured_2D_tuple.vtu
6cf08e49a10860a59a02b0ba6e06d0dee6b6b929 unstructured_2D_SVector.vtu
a96c57cd06b71ea2793883224ec08e69c401240c unstructured_3D.vtu
a96c57cd06b71ea2793883224ec08e69c401240c unstructured_3D_tuple.vtu
084079bc4289bce1005ec85fc0b6bcb079070d81 unstructured_3D_SVector.vtu
Expand Down
54 changes: 27 additions & 27 deletions test/multiblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ function third_block_data()

# Create cells (all hexahedrons in this case) and cell data.
celltype = VTKCellTypes.VTK_HEXAHEDRON
cells = MeshCell[]
cells = MeshCell{VTKCellType, SVector{8, Int}}[]
cdata = FloatType[]

for k = 2:Nk, j = 2:Nj, i = 2:Ni
# Define connectivity of cell.
inds = SVector{8, Int32}(
inds = SVector{8, Int}(
indices[i-1, j-1, k-1],
indices[i , j-1, k-1],
indices[i , j , k-1],
Expand Down Expand Up @@ -116,12 +116,12 @@ function fourth_block_data()

# Create cells (all tetrahedra in this case) and cell data.
celltype = VTKCellTypes.VTK_TETRA
cells_vol = MeshCell[]
cells_vol = MeshCell{VTKCellType, SVector{4, Int}}[]
cdata_vol = FloatType[]

for k = 1:kmax-1, j = 1:jmax-1, i = 1:imax-1
# 1, 3, 4, 5
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i, j, k],
indices[i+1, j, k+1],
indices[i, j, k+1],
Expand All @@ -134,7 +134,7 @@ function fourth_block_data()
push!(cdata_vol, i*j*k)

# 8, 5, 4, 3
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i, j+1, k+1],
indices[i, j+1, k],
indices[i, j, k+1],
Expand All @@ -147,7 +147,7 @@ function fourth_block_data()
push!(cdata_vol, i*j*k)

# 8, 7, 5, 3
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i, j+1, k+1],
indices[i+1, j+1, k+1],
indices[i, j+1, k],
Expand All @@ -160,7 +160,7 @@ function fourth_block_data()
push!(cdata_vol, i*j*k)

# 7, 6, 5, 3
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i+1, j+1, k+1],
indices[i+1, j+1, k],
indices[i, j+1, k],
Expand All @@ -173,7 +173,7 @@ function fourth_block_data()
push!(cdata_vol, i*j*k)

# 6, 2, 5, 3
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i+1, j+1, k],
indices[i+1, j, k],
indices[i, j+1, k],
Expand All @@ -186,7 +186,7 @@ function fourth_block_data()
push!(cdata_vol, i*j*k)

# 5, 2, 1, 3
inds = SVector{4, Int32}(
inds = SVector{4, Int}(
indices[i, j+1, k],
indices[i+1, j, k],
indices[i, j, k],
Expand Down Expand Up @@ -215,12 +215,12 @@ function fourth_block_data()
@. pdata_imin = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_imin = MeshCell[]
cells_imin = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_imin = FloatType[]

for k = 1:kmax-1, j = 1:jmax-1, i = 1:1
# 1, 3, 4, 5
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j, k],
indices[i, j, k+1],
indices[i, j+1, k])
Expand All @@ -232,7 +232,7 @@ function fourth_block_data()
push!(cdata_imin, i*j*k)

# 8, 5, 4, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j+1, k+1],
indices[i, j+1, k],
indices[i, j, k+1])
Expand Down Expand Up @@ -262,12 +262,12 @@ function fourth_block_data()
@. pdata_imax = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_imax = MeshCell[]
cells_imax = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_imax = FloatType[]

for k = 1:kmax-1, j = 1:jmax-1, i = imax-1:imax-1
# 7, 6, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i+1, j+1, k+1],
indices[i+1, j+1, k],
indices[i+1, j, k+1])
Expand All @@ -279,7 +279,7 @@ function fourth_block_data()
push!(cdata_imax, i*j*k)

# 6, 2, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i+1, j+1, k],
indices[i+1, j, k],
indices[i+1, j, k+1])
Expand Down Expand Up @@ -307,12 +307,12 @@ function fourth_block_data()
@. pdata_jmin = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_jmin = MeshCell[]
cells_jmin = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_jmin = FloatType[]

for k = 1:kmax-1, j = 1:1, i = 1:imax-1
# 1, 3, 4, 5
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j, k],
indices[i+1, j, k+1],
indices[i, j, k+1])
Expand All @@ -324,7 +324,7 @@ function fourth_block_data()
push!(cdata_jmin, i*j*k)

# 5, 2, 1, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i+1, j, k],
indices[i, j, k],
indices[i+1, j, k+1])
Expand Down Expand Up @@ -354,12 +354,12 @@ function fourth_block_data()
@. pdata_jmax = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_jmax = MeshCell[]
cells_jmax = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_jmax = FloatType[]

for k = 1:kmax-1, j = jmax-1:jmax-1, i = 1:imax-1
# 8, 7, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j+1, k+1],
indices[i+1, j+1, k+1],
indices[i, j+1, k])
Expand All @@ -371,7 +371,7 @@ function fourth_block_data()
push!(cdata_jmax, i*j*k)

# 7, 6, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i+1, j+1, k+1],
indices[i+1, j+1, k],
indices[i, j+1, k])
Expand Down Expand Up @@ -399,12 +399,12 @@ function fourth_block_data()
@. pdata_kmin = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_kmin = MeshCell[]
cells_kmin = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_kmin = FloatType[]

for k = 1:1, j = 1:jmax-1, i = 1:imax-1
# 6, 2, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i+1, j+1, k],
indices[i+1, j, k],
indices[i, j+1, k])
Expand All @@ -416,7 +416,7 @@ function fourth_block_data()
push!(cdata_kmin, i*j*k)

# 5, 2, 1, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j+1, k],
indices[i+1, j, k],
indices[i, j, k])
Expand Down Expand Up @@ -446,12 +446,12 @@ function fourth_block_data()
@. pdata_kmax = sin(2*pi*x)*sin(4*pi*y)*sin(6*pi*z)

celltype = VTKCellTypes.VTK_TRIANGLE
cells_kmax = MeshCell[]
cells_kmax = MeshCell{VTKCellType, SVector{3, Int}}[]
cdata_kmax = FloatType[]

for k = kmax-1:kmax-1, j = 1:jmax-1, i = 1:imax-1
# 8, 5, 4, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j+1, k+1],
indices[i, j, k+1],
indices[i+1, j, k+1])
Expand All @@ -463,7 +463,7 @@ function fourth_block_data()
push!(cdata_kmax, i*j*k)

# 8, 7, 5, 3
inds = SVector{3, Int32}(
inds = SVector{3, Int}(
indices[i, j+1, k+1],
indices[i+1, j+1, k+1],
indices[i+1, j, k+1])
Expand Down
3 changes: 2 additions & 1 deletion test/polydata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ end
function empty_cells()
points = [0 0 0; 1. 1. 1.]
vel = [0 .5 .5; 1 1 0]
verts = MeshCell{PolyData.Verts}[]
verts = MeshCell{PolyData.Verts, Vector{Int}}[]
@test isconcretetype(eltype(verts))
@time filenames = vtk_grid("empty_cells.vtp", points, verts,
compress=false, append=false, ascii=true) do vtk
vtk["vel"] = vel
Expand Down
8 changes: 4 additions & 4 deletions test/unstructured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function mesh_data(::Val{3})

# Create cells (all hexahedrons in this case) and cell data.
celltype = VTKCellTypes.VTK_HEXAHEDRON
cells = MeshCell[]
cells = MeshCell{VTKCellType, SVector{8, Int}}[]
cdata = FloatType[]

@test VTKCellType(celltype.vtk_id) === celltype
Expand All @@ -40,7 +40,7 @@ function mesh_data(::Val{3})

for k = 2:Nk, j = 2:Nj, i = 2:Ni
# Define connectivity of cell.
inds = MVector{8, Int32}(undef)
inds = MVector{8, Int}(undef)
inds[1] = indices[i-1, j-1, k-1]
inds[2] = indices[i , j-1, k-1]
inds[3] = indices[i , j , k-1]
Expand Down Expand Up @@ -83,7 +83,7 @@ function mesh_data(::Val{2})

# Create cells (all quads in this case) and cell data.
celltype = VTKCellTypes.VTK_QUAD
cells = MeshCell[]
cells = MeshCell{VTKCellType, Vector{Int32}}[]
cdata = FloatType[]

@test VTKCellType(celltype.vtk_id) === celltype
Expand Down Expand Up @@ -125,7 +125,7 @@ function mesh_data(::Val{1})

# Create cells (all lines in this case) and cell data.
celltype = VTKCellTypes.VTK_LINE
cells = MeshCell[]
cells = MeshCell{VTKCellType, Vector{Int32}}[]
cdata = FloatType[]

@test VTKCellType(celltype.vtk_id) === celltype
Expand Down