Skip to content

Commit

Permalink
Merge pull request #27 from j-fu/handle-partitioning
Browse files Browse the repository at this point in the history
Handle partitioning
  • Loading branch information
j-fu authored Jun 18, 2024
2 parents 9fa2d0c + db0209f commit 6299b6f
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GridVisualize"
uuid = "5eed8a63-0fb0-45eb-886d-8d5a387d12b8"
authors = ["Juergen Fuhrmann <[email protected]>"]
version = "1.6"
version = "1.7"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand All @@ -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"
Expand Down
43 changes: 39 additions & 4 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)",)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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],)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/meshcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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,)
Expand Down
8 changes: 4 additions & 4 deletions src/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 6 additions & 7 deletions src/plutovista.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -316,16 +315,16 @@ 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)

PlutoVista = ctx[:Plotter]
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)
Expand Down
13 changes: 6 additions & 7 deletions src/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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),)
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/vtkview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

2 comments on commit 6299b6f

@j-fu
Copy link
Member Author

@j-fu j-fu commented on 6299b6f Jun 18, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/109260

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.7.0 -m "<description of version>" 6299b6f7686f5631c7e2a3c73a606335b1f50cb8
git push origin v1.7.0

Please sign in to comment.