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

Throw informative error if cell vector is type unstable #156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/gridtypes/unstructured/unstructured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@
vtk
end

# If the type of `cells` is not concrete, throw an error describing the proper (type stable)
# way of initialising a vector of cells to be passed to vtk_grid.
function check_cells(cells::AbstractVector)
isconcretetype(eltype(cells)) && return nothing
s = if isempty(cells)
"" # we can't guess the cell type

Check warning on line 151 in src/gridtypes/unstructured/unstructured.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/unstructured/unstructured.jl#L150-L151

Added lines #L150 - L151 were not covered by tests
else
CellType = typeof(first(cells)) # try guessing cell type from first cell
"""

Check warning on line 154 in src/gridtypes/unstructured/unstructured.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/unstructured/unstructured.jl#L153-L154

Added lines #L153 - L154 were not covered by tests
Try initialising it as:

cells = $(CellType)[]

before appending cells to it.
"""
end
s = replace(s, "VTKBase." => "") # e.g. VTKBase.PolyData.Polys => PolyData.Polys
throw(ArgumentError("The concrete cell type cannot be inferred from the given `cells` vector.$s"))

Check warning on line 163 in src/gridtypes/unstructured/unstructured.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/unstructured/unstructured.jl#L162-L163

Added lines #L162 - L163 were not covered by tests
end

# Variant of vtk_grid with 2-D array "points".
# size(points) = (dim, num_points), with dim ∈ {1, 2, 3}
"""
Expand All @@ -159,6 +179,7 @@
function vtk_grid(filename::AbstractString, points::AbstractArray{T,2},
cells::CellVector, args...; kwargs...) where T
dim, Npts = size(points)
check_cells(cells)
gtype = grid_type(eltype(cells))

if dim == 3
Expand Down Expand Up @@ -205,6 +226,7 @@
throw(DimensionMismatch("length of x, y and z arrays must be the same."))
end
points = (x, y, z)
check_cells(cells)
gtype = grid_type(eltype(cells))
vtk_grid(gtype, filename, points, cells, args...; kwargs...)
end
Expand Down Expand Up @@ -237,6 +259,7 @@
"""
function vtk_grid(filename::AbstractString, xs::AbstractVector,
cells::CellVector, args...; kwargs...)
check_cells(cells)
gtype = grid_type(eltype(cells))
vtk_grid(gtype, filename, xs, cells, args...; kwargs...)
end
Expand All @@ -247,6 +270,7 @@
cells::CellVector, args...; kwargs...) where T
dim, Ni, Nj, Nk = size(points)
points_r = reshape(points, dim, Ni * Nj * Nk)
check_cells(cells)
gtype = grid_type(eltype(cells))
vtk_grid(gtype, filename, points_r, cells, args...; kwargs...)
end
Loading