Skip to content

Iterator api #23

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.15.0"
authors = ["Juergen Fuhrmann <[email protected]>", "Patrick Jaap <[email protected]>"]

[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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/GridVisualize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 5 additions & 19 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Comment on lines +87 to +92
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here func (a user-provided function) is passed directly as the values vector to LinearSimplices. You likely need to evaluate func at each node coordinate to build a numeric vector before constructing the iterator.

Copilot uses AI. Check for mistakes.

Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, funcs[i] is passed in place of a values vector. You should apply each funcs[i] over the corresponding grid nodes to produce the values vector for LinearSimplices.

Suggested change
all_ls = [LinearSimplices(grids[i], funcs[i]; gridscale) for i in 1:length(grids)]
all_ls = [LinearSimplices(grids[i], funcs[i](grids[i][Coordinates]); gridscale) for i in 1:length(grids)]

Copilot uses AI. Check for mistakes.

all_lines = vcat([marching_triangles(ls, levels) for ls in all_ls]...)
return [vcat(all_lines...)]
end

##############################################
Expand Down
33 changes: 33 additions & 0 deletions src/griditerator.jl
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions test/griditerators.jl
Original file line number Diff line number Diff line change
@@ -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
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The magic number 256 is unexplained; consider defining a named constant (e.g. MAX_ALLOC_BYTES) or adding a comment on how this threshold was chosen.

Suggested change
@test nalloc < 256 # allow for some allocations
@test nalloc < MAX_ALLOC_BYTES # Threshold for memory allocation to ensure efficiency

Copilot uses AI. Check for mistakes.

return @test sum_f == (dim + 1) * num_cells(g)
end

@testset "iterator testloops" begin
testloops(1)
testloops(2)
testloops(3)
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import CairoMakie, PyPlot, PlutoVista

CairoMakie.activate!(; type = "svg", visible = false)


include("griditerators.jl")
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Use an explicit path with @__DIR__ to ensure the file is found regardless of working directory, e.g., include(joinpath(@__DIR__, "griditerators.jl")).

Suggested change
include("griditerators.jl")
include(joinpath(@__DIR__, "griditerators.jl"))

Copilot uses AI. Check for mistakes.


plotting = joinpath(@__DIR__, "..", "examples", "plotting.jl")
include(plotting)

Expand Down
Loading