From 63e3f4e373c9712dc0b20a398b58712e147d5b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Sun, 9 Jun 2024 00:19:24 +0200 Subject: [PATCH 1/4] Add :cellcoloring keyword for handling partitioned grids --- src/common.jl | 43 +++++++++++++++++++++++++++++++++++++++---- src/dispatch.jl | 2 ++ src/makie.jl | 9 +++++---- src/meshcat.jl | 3 ++- src/plots.jl | 8 ++++---- src/plutovista.jl | 13 ++++++------- src/pyplot.jl | 13 ++++++------- src/vtkview.jl | 2 +- 8 files changed, 65 insertions(+), 28 deletions(-) diff --git a/src/common.jl b/src/common.jl index 95226a0..18699ef 100644 --- a/src/common.jl +++ b/src/common.jl @@ -9,14 +9,15 @@ Return corresponding points and facets for each region for drawing as mesh (Maki or trisurf (pyplot) """ function GridVisualizeTools.extract_visible_cells3D(grid::ExtendableGrid, xyzcut; + cellcoloring = :cellregions, gridscale = 1.0, primepoints = zeros(0, 0), Tp = SVector{3, Float32}, Tf = SVector{3, Int32}) coord = grid[Coordinates] * gridscale cellnodes = grid[CellNodes] - cellregions = grid[CellRegions] - nregions = grid[NumCellRegions] + cellregions = cellcolors(grid, cellcoloring) + nregions = num_cellcolors(grid, cellcoloring) extract_visible_cells3D(coord, cellnodes, cellregions, nregions, [xyzcut...] * gridscale; primepoints = primepoints, Tp = Tp, Tf = Tf) @@ -86,10 +87,10 @@ end ############################################## # Create meshes from grid data -function regionmesh(grid, gridscale, iregion) +function regionmesh(grid, gridscale, iregion; cellcoloring = :cellregions) coord = grid[Coordinates] cn = grid[CellNodes] - cr = grid[CellRegions] + cr = cellcolors(grid, cellcoloring) @views points = [Point2f(coord[:, i] * gridscale) for i = 1:size(coord, 2)] faces = Vector{GLTriangleFace}(undef, 0) for i = 1:length(cr) @@ -425,3 +426,37 @@ function bary!(λ, invA, L2G, x) end ExtendableGrids.postprocess_xreftest!(λ, Triangle2D) end + +function cellcolors(grid, coloring) + xr = grid[CellRegions] + if coloring == :partitions + xr = similar(xr) + for icol in pcolors(grid) + for ipart in pcolor_partitions(grid, icol) + for icell in partition_cells(grid, ipart) + xr[icell] = ipart + end + end + end + elseif coloring == :pcolors + xr = similar(xr) + for icol in pcolors(grid) + for ipart in pcolor_partitions(grid, icol) + for icell in partition_cells(grid, ipart) + xr[icell] = icol + end + end + end + end + xr +end + +function num_cellcolors(grid, coloring) + if coloring == :partitions + num_partitions(grid) + elseif coloring == :pcolors + num_pcolors(grid) + else + num_cellregions(grid) + end +end diff --git a/src/dispatch.jl b/src/dispatch.jl index b5f56e6..3ab3727 100644 --- a/src/dispatch.jl +++ b/src/dispatch.jl @@ -336,6 +336,8 @@ function default_plot_kwargs() :zplanes => Pair([prevfloat(Inf)], "3D z plane positions or number thereof"), :zoom => Pair(1.0, "Zoom level"), :gridscale => Pair(1, "Grid scale factor. Will be applied also to planes, spacing"), + :cellcoloring => Pair(:cellregions, + "Coloring of cells: one of [:cellregions, :pcolors, :partitions]"), :azim => Pair(-60, "3D azimuth angle (in degrees)"), :elev => Pair(30, "3D elevation angle (in degrees)"), :perspectiveness => Pair(0.25, diff --git a/src/makie.jl b/src/makie.jl index fe8df07..c1e6dc2 100644 --- a/src/makie.jl +++ b/src/makie.jl @@ -203,11 +203,11 @@ function basemesh1d(grid, gridscale) end # Point list for intervals -function regionmesh1d(grid, gridscale, iregion) +function regionmesh1d(grid, gridscale, iregion; cellcoloring = :cellregions) coord = vec(grid[Coordinates]) points = Vector{Point2f}(undef, 0) cn = grid[CellNodes] - cr = grid[CellRegions] + cr = cellcolors(grid, cellcoloring) ncells = length(cr) for i = 1:ncells if cr[i] == iregion @@ -275,7 +275,7 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid) # Colored cell regions for i = 1:nregions XMakie.linesegments!(ctx[:scene], - map(g -> regionmesh1d(g, gridscale, i), ctx[:grid]); + map(g -> regionmesh1d(g, gridscale, i; cellcoloring = ctx[:cellcoloring]), ctx[:grid]); color = cmap[i], linewidth = 4, label = "c $(i)",) @@ -562,7 +562,7 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid) ctx[:cmap] = cmap for i = 1:nregions XMakie.poly!(ctx[:scene], - map(g -> regionmesh(g, ctx[:gridscale], i), ctx[:grid]); + map(g -> regionmesh(g, ctx[:gridscale], i; cellcoloring = ctx[:cellcoloring]), ctx[:grid]); color = cmap[i], strokecolor = :black, strokewidth = ctx[:linewidth],) @@ -958,6 +958,7 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid) if ctx[:interior] ctx[:celldata] = map(d -> extract_visible_cells3D(d.g, [d.x, d.y, d.z] / ctx[:gridscale]; + cellcoloring = ctx[:cellcoloring], gridscale = ctx[:gridscale], primepoints = hcat(xyzmin, xyzmax), Tp = Point3f, diff --git a/src/meshcat.jl b/src/meshcat.jl index f0c5793..a920aaa 100644 --- a/src/meshcat.jl +++ b/src/meshcat.jl @@ -38,7 +38,7 @@ function gridplot!(ctx, TP::Type{MeshCatType}, ::Type{Val{2}}, grid) cmap = region_cmap(nregions) bcmap = bregion_cmap(nbregions) for i = 1:nregions - mesh = regionmesh(grid, i) + mesh = regionmesh(grid, i; cellcoloring = ctx[:cellcoloring]) MeshCat.setobject!(vis["interior"]["r$(i)"], mesh, MeshCat.MeshLambertMaterial(; color = RGBA{Float32}(cmap[i], 1.0))) @@ -86,6 +86,7 @@ function gridplot!(ctx, TP::Type{MeshCatType}, ::Type{Val{3}}, grid) if ctx[:interior] pts, fcs = extract_visible_cells3D(grid, xyzcut; + cellcoloring = ctx[:cellcoloring], primepoints = hcat(xyzmin, xyzmax), Tp = Point3f, Tf = GLTriangleFace,) diff --git a/src/plots.jl b/src/plots.jl index 57990ec..052c100 100644 --- a/src/plots.jl +++ b/src/plots.jl @@ -84,10 +84,10 @@ function gridplot!(ctx, TP::Type{PlotsType}, ::Type{Val{1}}, grid) end p = ctx[:ax] - cellregions = grid[CellRegions] + cellregions = cellcolors(grid, ctx[:cellcoloring]) + ncellregions = num_cellcolors(grid, ctx[:cellcoloring]) cellnodes = grid[CellNodes] coord = grid[Coordinates] - ncellregions = grid[NumCellRegions] bfacenodes = grid[BFaceNodes] bfaceregions = grid[BFaceRegions] nbfaceregions = grid[NumBFaceRegions] @@ -132,10 +132,10 @@ function gridplot!(ctx, TP::Type{PlotsType}, ::Type{Val{2}}, grid) ctx[:ax] = Plots.plot(; title = ctx[:title]) end p = ctx[:ax] - cellregions = grid[CellRegions] + cellregions = cellcolors(grid, ctx[:cellcoloring]) + ncellregions = num_cellcolors(grid, ctx[:cellcoloring]) cellnodes = grid[CellNodes] coord = grid[Coordinates] * ctx[:gridscale] - ncellregions = grid[NumCellRegions] bfacenodes = grid[BFaceNodes] bfaceregions = grid[BFaceRegions] nbfaceregions = grid[NumBFaceRegions] diff --git a/src/plutovista.jl b/src/plutovista.jl index 661036f..bc6062f 100644 --- a/src/plutovista.jl +++ b/src/plutovista.jl @@ -96,14 +96,13 @@ function gridplot!(ctx, TP::Type{PlutoVistaType}, ::Type{Val{1}}, grid) PlutoVista.backend!(ctx[:figure]; backend = ctx[:backend], datadim = 1) coord = grid[Coordinates] - cellregions = grid[CellRegions] + cellregions = cellcolors(grid, ctx[:cellcoloring]) + ncellregions = num_cellcolors(grid, ctx[:cellcoloring]) cellnodes = grid[CellNodes] coord = grid[Coordinates] * ctx[:gridscale] - ncellregions = grid[NumCellRegions] bfacenodes = grid[BFaceNodes] bfaceregions = grid[BFaceRegions] nbfaceregions = grid[NumBFaceRegions] - ncellregions = grid[NumCellRegions] crflag = ones(Bool, ncellregions) brflag = ones(Bool, nbfaceregions) @@ -224,14 +223,14 @@ function scalarplot!(ctx, end function gridplot!(ctx, TP::Type{PlutoVistaType}, ::Type{Val{2}}, grid) - nregions = num_cellregions(grid) + nregions = num_cellcolors(grid, ctx[:cellcoloring]) nbregions = num_bfaceregions(grid) cmap = region_cmap(nregions) bcmap = bregion_cmap(nbregions) PlutoVista = ctx[:Plotter] pts = grid[Coordinates] tris = grid[CellNodes] - markers = grid[CellRegions] + markers = cellcolors(grid, ctx[:cellcoloring]) edges = grid[BFaceNodes] edgemarkers = grid[BFaceRegions] @@ -316,8 +315,8 @@ end function streamplot!(ctx, TP::Type{PlutoVistaType}, ::Type{Val{2}}, grid, func) end function gridplot!(ctx, TP::Type{PlutoVistaType}, ::Type{Val{3}}, grid) - nregions = num_cellregions(grid) nbregions = num_bfaceregions(grid) + nregions = num_cellcolors(grid, ctx[:cellcoloring]) cmap = region_cmap(nregions) bcmap = bregion_cmap(nbregions) @@ -325,7 +324,7 @@ function gridplot!(ctx, TP::Type{PlutoVistaType}, ::Type{Val{3}}, grid) pts = grid[Coordinates] tris = grid[CellNodes] faces = grid[BFaceNodes] - markers = grid[CellRegions] + markers = cellcolors(grid, ctx[:cellcoloring]) facemarkers = grid[BFaceRegions] PlutoVista.backend!(ctx[:figure]; backend = ctx[:backend], datadim = 3) diff --git a/src/pyplot.jl b/src/pyplot.jl index 6d77bff..4fc41f3 100644 --- a/src/pyplot.jl +++ b/src/pyplot.jl @@ -134,14 +134,13 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{1}}, grid) ax = ctx[:ax] fig = ctx[:figure] - cellregions = grid[CellRegions] + cellregions = cellcolors(grid, ctx[:cellcoloring]) + ncellregions = num_cellcolors(grid, ctx[:cellcoloring]) cellnodes = grid[CellNodes] coord = grid[Coordinates] - ncellregions = grid[NumCellRegions] bfacenodes = grid[BFaceNodes] bfaceregions = grid[BFaceRegions] nbfaceregions = grid[NumBFaceRegions] - ncellregions = grid[NumCellRegions] crflag = ones(Bool, ncellregions) brflag = ones(Bool, nbfaceregions) @@ -213,11 +212,10 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}}, grid) end ax = ctx[:ax] fig = ctx[:figure] - cellregions = grid[CellRegions] + cellregions = cellcolors(grid, ctx[:cellcoloring]) + ncellregions = num_cellcolors(grid, ctx[:cellcoloring]) cellnodes = grid[CellNodes] - ncellregions = grid[NumCellRegions] nbfaceregions = grid[NumBFaceRegions] - ncellregions = grid[NumCellRegions] if nbfaceregions > 0 bfacenodes = grid[BFaceNodes] bfaceregions = grid[BFaceRegions] @@ -229,7 +227,7 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}}, grid) tridat = tridata(grid, ctx[:gridscale]) cmap = region_cmap(ncellregions) cdata = ax.tripcolor(tridat...; - facecolors = grid[CellRegions], + facecolors = cellcolors(grid, ctx[:cellcoloring]), cmap = PyPlot.ColorMap(cmap, length(cmap)), vmin = 1.0, vmax = length(cmap),) @@ -310,6 +308,7 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{3}}, grid) if ctx[:interior] regpoints0, regfacets0 = extract_visible_cells3D(grid, xyzcut; gridscale = ctx[:gridscale], + cellcoloring = ctx[:cellcoloring], primepoints = hcat(xyzmin, xyzmax)) regfacets = [reshape(reinterpret(Int32, regfacets0[i]), (3, length(regfacets0[i]))) for i = 1:nregions] diff --git a/src/vtkview.jl b/src/vtkview.jl index 7f1cc39..56e2bc0 100644 --- a/src/vtkview.jl +++ b/src/vtkview.jl @@ -56,7 +56,7 @@ function gridplot!(ctx, TP::Type{VTKViewType}, grid) VTKView.simplexgrid!(ctx[:dataset], grid[Coordinates], grid[CellNodes]) VTKView.boundarygrid!(ctx[:dataset], grid[BFaceNodes]) VTKView.boundarymarker!(ctx[:dataset], grid[BFaceRegions]) - VTKView.cellmarker!(ctx[:dataset], grid[CellRegions]) + VTKView.cellmarker!(ctx[:dataset], cellcolors(grid, ctx[:cellcoloring])) if !haskey(ctx, :gridview) ctx[:gridview] = VTKView.GridView() VTKView.data!(ctx[:gridview], ctx[:dataset]) From 70e0847c17977ea23768df3bce87ad7bbd7de027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Sun, 9 Jun 2024 21:19:54 +0200 Subject: [PATCH 2/4] upgrade project.toml, dependencies --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index ddb59c8..b7f255d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GridVisualize" uuid = "5eed8a63-0fb0-45eb-886d-8d5a387d12b8" authors = ["Juergen Fuhrmann "] -version = "1.6" +version = "1.7" [deps] ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" @@ -26,7 +26,7 @@ ColorSchemes = "3" Colors = "0.12,1" DocStringExtensions = "0.8,0.9" ElasticArrays = "1" -ExtendableGrids = "0.9,1" +ExtendableGrids = "1.7" GLMakie = "0.9, 0.10" GeometryBasics = "0.4.1" GridVisualizeTools = "1.1" From 1361f4ea78bc3622c202a80363f99d06b8d4414c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Tue, 18 Jun 2024 14:38:00 +0200 Subject: [PATCH 3/4] fix cellcolor numbering --- src/makie.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/makie.jl b/src/makie.jl index c1e6dc2..9b673f1 100644 --- a/src/makie.jl +++ b/src/makie.jl @@ -528,7 +528,7 @@ end function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid) XMakie = ctx[:Plotter] - nregions = num_cellregions(grid) + nregions = num_cellcolors(grid, ctx[:cellcoloring]) nbregions = num_bfaceregions(grid) From db0209fa7fe4937ec2cc698aafae7c5660acfdd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Tue, 18 Jun 2024 15:29:03 +0200 Subject: [PATCH 4/4] remove nightly from ci due to pluto problem --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04fcf11..9050b7d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: version: - '1.9' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'. - '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia. - - 'nightly' +# - 'nightly' os: - ubuntu-latest - macos-latest