From 5776667dec8d086ce36602185ad1d804d093ff07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Wed, 24 Jan 2024 21:33:12 +0100 Subject: [PATCH 1/5] Start iterator based API --- Project.toml | 16 +++++++++------- src/GridVisualize.jl | 4 ++++ src/griditerator.jl | 30 ++++++++++++++++++++++++++++++ test/griditerators.jl | 30 ++++++++++++++++++++++++++++++ test/runtests.jl | 3 +++ 5 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 src/griditerator.jl create mode 100644 test/griditerators.jl diff --git a/Project.toml b/Project.toml index 7ad59b5c..0e5e77dc 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Juergen Fuhrmann "] version = "1.5.0" [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,14 @@ 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] +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" +GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +PlutoVista = "646e1f28-b900-46d7-9d87-d554eb38a413" +PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" [compat] CairoMakie = "0.11.3" @@ -49,10 +58,3 @@ GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" PlutoVista = "646e1f28-b900-46d7-9d87-d554eb38a413" PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" - -[weakdeps] -CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" -GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -PlutoVista = "646e1f28-b900-46d7-9d87-d554eb38a413" -PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" diff --git a/src/GridVisualize.jl b/src/GridVisualize.jl index bb4ce241..0c565d9f 100644 --- a/src/GridVisualize.jl +++ b/src/GridVisualize.jl @@ -14,8 +14,12 @@ using Interpolations: linear_interpolation using IntervalSets using GridVisualizeTools +using ChunkSplitters using ExtendableGrids +include("griditerator.jl") +export LinearSimplices + include("dispatch.jl") include("common.jl") export quiverdata, vectorsample diff --git a/src/griditerator.jl b/src/griditerator.jl new file mode 100644 index 00000000..aa0797f6 --- /dev/null +++ b/src/griditerator.jl @@ -0,0 +1,30 @@ +struct LinearSimplices{D,Tc,Ti,Tf} <: LinearSimplexIterator{D} + coord::Matrix{Tc} + cellnodes::Matrix{Ti} + values::Vector{Tf} + range::StepRange{Int,Int} + ichunk::Int +end + +function LinearSimplices(coord::Matrix{Tc},cn::Matrix{Ti},f::Vector{Tf};nthreads=Threads.nthreads()) where {Tc,Ti,Tf} + ncells=size(cn,2) + dim=size(coord,1) + map(chunks(1:ncells,nthreads)) do c + LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,c...) + end +end + +function LinearSimplices(g::ExtendableGrid,f::Vector;nthreads=1) + LinearSimplices(g[Coordinates],g[CellNodes],f;nthreads) +end + +function Base.iterate(linear_simplices::LinearSimplices{D}, args...) where D + (;coord,cellnodes,values,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]]) + (s,state) +end diff --git a/test/griditerators.jl b/test/griditerators.jl new file mode 100644 index 00000000..adf04aa9 --- /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 + @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 65e9680f..9a129859 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,9 @@ using Test, ExtendableGrids, GridVisualize, Pkg import CairoMakie CairoMakie.activate!(; type = "svg", visible = false) + +include("griditerators.jl") + plotting = joinpath(@__DIR__, "..", "examples", "plotting.jl") include(plotting) include("../docs/makeplots.jl") From 048224278ed738aee0672147c1ee53a6f462ef7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Wed, 24 Jan 2024 23:19:52 +0100 Subject: [PATCH 2/5] first working iterator based plot --- src/common.jl | 11 +++++------ src/griditerator.jl | 14 ++++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/common.jl b/src/common.jl index 95226a00..dccdfe45 100644 --- a/src/common.jl +++ b/src/common.jl @@ -73,15 +73,14 @@ end Collect isoline snippets on triangles ready for linesegments! """ function GridVisualizeTools.marching_triangles(grid::ExtendableGrid, func, levels; gridscale = 1.0) - coord::Matrix{Float64} = grid[Coordinates] * gridscale - cellnodes::Matrix{Int32} = grid[CellNodes] - marching_triangles(coord, cellnodes, func, levels) + ls=LinearSimplices(grid,func;gridscale) + vcat(marching_triangles(ls,levels)...) end function GridVisualizeTools.marching_triangles(grids::Vector{ExtendableGrid{Tv, Ti}}, funcs, levels; gridscale = 1.0) where {Tv, Ti} - coords = [grid[Coordinates] * gridscale for grid in grids] - cellnodes = [grid[CellNodes] for grid in grids] - marching_triangles(coords, cellnodes, funcs, levels) + all_ls = [LinearSimplices(grids[i],funcs[i];gridscale) for i=1:length(grids)] + all_lines=vcat([marching_triangles(ls,levels) for ls in all_ls]...) + vcat(all_lines...) end ############################################## diff --git a/src/griditerator.jl b/src/griditerator.jl index aa0797f6..e6600869 100644 --- a/src/griditerator.jl +++ b/src/griditerator.jl @@ -2,29 +2,31 @@ struct LinearSimplices{D,Tc,Ti,Tf} <: LinearSimplexIterator{D} coord::Matrix{Tc} cellnodes::Matrix{Ti} values::Vector{Tf} + gridscale::Tc range::StepRange{Int,Int} ichunk::Int end -function LinearSimplices(coord::Matrix{Tc},cn::Matrix{Ti},f::Vector{Tf};nthreads=Threads.nthreads()) where {Tc,Ti,Tf} +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) map(chunks(1:ncells,nthreads)) do c - LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,c...) + LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,gridscale,c...) end end -function LinearSimplices(g::ExtendableGrid,f::Vector;nthreads=1) - LinearSimplices(g[Coordinates],g[CellNodes],f;nthreads) +function LinearSimplices(g::ExtendableGrid,f::Vector;nthreads=Threads.nthreads(), gridscale=1.0) + LinearSimplices(g[Coordinates],g[CellNodes],f;nthreads,gridscale) end function Base.iterate(linear_simplices::LinearSimplices{D}, args...) where D - (;coord,cellnodes,values,range)=linear_simplices + (;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]]) + values[cellnodes[:,icell]], + gridscale) (s,state) end From 036598b240108cda0f738da81fb9d204e35d64ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Thu, 25 Jan 2024 15:14:03 +0100 Subject: [PATCH 3/5] Adopt to new ChunkSplitters API See https://discourse.julialang.org/t/chunksplitters-jl-simple-parallel-chunk-splitter/87945/8?u=j-fu --- Project.toml | 1 + src/griditerator.jl | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 0e5e77dc..23eea471 100644 --- a/Project.toml +++ b/Project.toml @@ -31,6 +31,7 @@ PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" [compat] CairoMakie = "0.11.3" +ChunkSplitters = "2.1" ColorSchemes = "3" Colors = "0.12,1" DocStringExtensions = "0.8,0.9" diff --git a/src/griditerator.jl b/src/griditerator.jl index e6600869..a81d1959 100644 --- a/src/griditerator.jl +++ b/src/griditerator.jl @@ -4,14 +4,13 @@ struct LinearSimplices{D,Tc,Ti,Tf} <: LinearSimplexIterator{D} values::Vector{Tf} gridscale::Tc range::StepRange{Int,Int} - ichunk::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) - map(chunks(1:ncells,nthreads)) do c - LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,gridscale,c...) + map(enumerate(chunks(1:ncells;n=nthreads))) do c + LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,gridscale,last(c)) end end From b6d57cda1daf57b19027ae6a459f0027b2c8bc02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Tue, 17 Jun 2025 14:17:39 +0200 Subject: [PATCH 4/5] make iterator working again --- Project.toml | 6 ------ src/common.jl | 5 ++--- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index 4d208756..daab199d 100644 --- a/Project.toml +++ b/Project.toml @@ -22,12 +22,6 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -[weakdeps] -CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" -GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -PlutoVista = "646e1f28-b900-46d7-9d87-d554eb38a413" -PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" [weakdeps] Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" diff --git a/src/common.jl b/src/common.jl index 166b1cbd..9eac779b 100644 --- a/src/common.jl +++ b/src/common.jl @@ -82,17 +82,16 @@ end """ $(SIGNATURES) -Deprecated """ function GridVisualizeTools.marching_triangles(grid::ExtendableGrid, func, levels; gridscale = 1.0) ls=LinearSimplices(grid,func;gridscale) vcat(marching_triangles(ls,levels)...) end -function GridVisualizeTools.marching_triangles(grids::Vector{ExtendableGrid{Tv, Ti}}, funcs, levels; gridscale = 1.0) where {Tv, Ti} +function GridVisualizeTools.marching_triangles(grids::Vector{ExtendableGrid{Tv, Ti}}, funcs, lines, levels; gridscale = 1.0) where {Tv, Ti} all_ls = [LinearSimplices(grids[i],funcs[i];gridscale) for i=1:length(grids)] all_lines=vcat([marching_triangles(ls,levels) for ls in all_ls]...) - vcat(all_lines...) + [vcat(all_lines...)] end ############################################## From 620662329b8a04f4813d6f1659d660aac84a78d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Tue, 17 Jun 2025 14:17:51 +0200 Subject: [PATCH 5/5] runic --- src/common.jl | 10 +++++----- src/griditerator.jl | 42 ++++++++++++++++++++++-------------------- test/griditerators.jl | 26 +++++++++++++------------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/common.jl b/src/common.jl index 9eac779b..db42a325 100644 --- a/src/common.jl +++ b/src/common.jl @@ -84,14 +84,14 @@ end """ function GridVisualizeTools.marching_triangles(grid::ExtendableGrid, func, levels; gridscale = 1.0) - ls=LinearSimplices(grid,func;gridscale) - vcat(marching_triangles(ls,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} - all_ls = [LinearSimplices(grids[i],funcs[i];gridscale) for i=1:length(grids)] - all_lines=vcat([marching_triangles(ls,levels) for ls in all_ls]...) - [vcat(all_lines...)] + 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 index a81d1959..99540894 100644 --- a/src/griditerator.jl +++ b/src/griditerator.jl @@ -1,31 +1,33 @@ -struct LinearSimplices{D,Tc,Ti,Tf} <: LinearSimplexIterator{D} +struct LinearSimplices{D, Tc, Ti, Tf} <: LinearSimplexIterator{D} coord::Matrix{Tc} cellnodes::Matrix{Ti} values::Vector{Tf} gridscale::Tc - range::StepRange{Int,Int} + 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) - map(enumerate(chunks(1:ncells;n=nthreads))) do c - LinearSimplices{dim,Tc,Ti,Tf}(coord,cn,f,gridscale,last(c)) +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 +end -function LinearSimplices(g::ExtendableGrid,f::Vector;nthreads=Threads.nthreads(), gridscale=1.0) - LinearSimplices(g[Coordinates],g[CellNodes],f;nthreads,gridscale) +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) - (s,state) +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 index adf04aa9..a5ef5c43 100644 --- a/test/griditerators.jl +++ b/test/griditerators.jl @@ -5,26 +5,26 @@ using Test function testloops(dim) - X=0:0.1:10 - if dim==1 - g=simplexgrid(X) - elseif dim==2 - g=simplexgrid(X,X) + X = 0:0.1:10 + if dim == 1 + g = simplexgrid(X) + elseif dim == 2 + g = simplexgrid(X, X) else - g=simplexgrid(X,X,X) + g = simplexgrid(X, X, X) end - f=ones(num_nodes(g)) - ls=LinearSimplices(g,f;nthreads=3) + f = ones(num_nodes(g)) + ls = LinearSimplices(g, f; nthreads = 3) testloop(ls) # for compilation - nalloc=@allocated sum_f=testloop(ls) + nalloc = @allocated sum_f = testloop(ls) - - @test nalloc<256 # allow for some allocations - @test sum_f==(dim+1)*num_cells(g) + + @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 +end