diff --git a/Project.toml b/Project.toml index abf95c3..daab199 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ version = "1.15.0" authors = ["Juergen Fuhrmann ", "Patrick Jaap "] [deps] +ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e" ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" @@ -19,6 +20,8 @@ Observables = "510215fc-4207-5dde-b226-833fc4488ee2" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + [weakdeps] Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" @@ -37,6 +40,7 @@ GridVisualizePlotsExt = "Plots" GridVisualizePlutoVistaExt = "PlutoVista" [compat] +ChunkSplitters = "2.1" ColorSchemes = "3" Colors = "0.12,0.13,1" DocStringExtensions = "0.8,0.9" diff --git a/src/GridVisualize.jl b/src/GridVisualize.jl index ba5663e..ab7c9f0 100644 --- a/src/GridVisualize.jl +++ b/src/GridVisualize.jl @@ -16,8 +16,12 @@ using Colors using ColorSchemes using GeometryBasics using GridVisualizeTools +using ChunkSplitters using ExtendableGrids +include("griditerator.jl") +export LinearSimplices + include("dispatch.jl") include("common.jl") include("slice_plots.jl") diff --git a/src/common.jl b/src/common.jl index 8cf7051..db42a32 100644 --- a/src/common.jl +++ b/src/common.jl @@ -82,30 +82,16 @@ end """ $(SIGNATURES) -Deprecated """ function GridVisualizeTools.marching_triangles(grid::ExtendableGrid, func, levels; gridscale = 1.0) - coord::Matrix{Float64} = grid[Coordinates] * gridscale - cellnodes::Matrix{Int32} = grid[CellNodes] - points, _, _ = marching_triangles(coord, cellnodes, func, [], levels) - return points -end - -""" - $(SIGNATURES) - -Collect isoline snippets and/or intersection points with lines and values ready for linesegments! -""" -function GridVisualizeTools.marching_triangles(grid::ExtendableGrid, func, lines, levels; gridscale = 1.0) - coord::Matrix{Float64} = grid[Coordinates] * gridscale - cellnodes::Matrix{Int32} = grid[CellNodes] - return marching_triangles(coord, cellnodes, func, lines, levels) + ls = LinearSimplices(grid, func; gridscale) + return vcat(marching_triangles(ls, levels)...) end function GridVisualizeTools.marching_triangles(grids::Vector{ExtendableGrid{Tv, Ti}}, funcs, lines, levels; gridscale = 1.0) where {Tv, Ti} - coords = [grid[Coordinates] * gridscale for grid in grids] - cellnodes = [grid[CellNodes] for grid in grids] - return marching_triangles(coords, cellnodes, funcs, lines, levels) + all_ls = [LinearSimplices(grids[i], funcs[i]; gridscale) for i in 1:length(grids)] + all_lines = vcat([marching_triangles(ls, levels) for ls in all_ls]...) + return [vcat(all_lines...)] end ############################################## diff --git a/src/griditerator.jl b/src/griditerator.jl new file mode 100644 index 0000000..9954089 --- /dev/null +++ b/src/griditerator.jl @@ -0,0 +1,33 @@ +struct LinearSimplices{D, Tc, Ti, Tf} <: LinearSimplexIterator{D} + coord::Matrix{Tc} + cellnodes::Matrix{Ti} + values::Vector{Tf} + gridscale::Tc + range::StepRange{Int, Int} +end + +function LinearSimplices(coord::Matrix{Tc}, cn::Matrix{Ti}, f::Vector{Tf}; gridscale = 1.0, nthreads = Threads.nthreads()) where {Tc, Ti, Tf} + ncells = size(cn, 2) + dim = size(coord, 1) + return map(enumerate(chunks(1:ncells; n = nthreads))) do c + LinearSimplices{dim, Tc, Ti, Tf}(coord, cn, f, gridscale, last(c)) + end +end + +function LinearSimplices(g::ExtendableGrid, f::Vector; nthreads = Threads.nthreads(), gridscale = 1.0) + return LinearSimplices(g[Coordinates], g[CellNodes], f; nthreads, gridscale) +end + +function Base.iterate(linear_simplices::LinearSimplices{D}, args...) where {D} + (; coord, cellnodes, values, gridscale, range) = linear_simplices + iter = iterate(range, args...) + isnothing(iter) && return nothing + (icell, state) = iter + @views s = LinearSimplex( + Val{D}, + coord[:, cellnodes[:, icell]], + values[cellnodes[:, icell]], + gridscale + ) + return (s, state) +end diff --git a/test/griditerators.jl b/test/griditerators.jl new file mode 100644 index 0000000..a5ef5c4 --- /dev/null +++ b/test/griditerators.jl @@ -0,0 +1,30 @@ +using ExtendableGrids +using GridVisualizeTools +using GridVisualize +using Test + + +function testloops(dim) + X = 0:0.1:10 + if dim == 1 + g = simplexgrid(X) + elseif dim == 2 + g = simplexgrid(X, X) + else + g = simplexgrid(X, X, X) + end + f = ones(num_nodes(g)) + ls = LinearSimplices(g, f; nthreads = 3) + testloop(ls) # for compilation + nalloc = @allocated sum_f = testloop(ls) + + + @test nalloc < 256 # allow for some allocations + return @test sum_f == (dim + 1) * num_cells(g) +end + +@testset "iterator testloops" begin + testloops(1) + testloops(2) + testloops(3) +end diff --git a/test/runtests.jl b/test/runtests.jl index a548257..e653b7b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,9 @@ import CairoMakie, PyPlot, PlutoVista CairoMakie.activate!(; type = "svg", visible = false) + +include("griditerators.jl") + plotting = joinpath(@__DIR__, "..", "examples", "plotting.jl") include(plotting)