Skip to content

Commit 073e15d

Browse files
committed
Add explode(grid) function
1 parent c1bd211 commit 073e15d

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
3+
## [1.15.0]
4+
- add and export `explode` function that produces a grid with all adjacencies removed
5+
26
## [1.14.2] - 2025-11-25
37
- less allocations in gFindLocal
48

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ExtendableGrids"
22
uuid = "cfc395e8-590f-11e8-1f13-43a2532b2fa8"
33
authors = ["Juergen Fuhrmann <[email protected]>", "Christian Merdon <[email protected]>", "Johannes Taraz <[email protected]>", "Patrick Jaap <[email protected]>"]
4-
version = "1.14.2"
4+
version = "1.15.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

docs/src/more.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55
Modules = [ExtendableGrids]
66
Pages = ["derived.jl","more.jl"]
77
```
8+
9+
## Additional Functions
10+
11+
```@docs
12+
explode
13+
```

src/ExtendableGrids.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export num_cellregions, num_bfaceregions, num_bedgeregions
7878
export gridcomponents
7979
export isconsistent, dangling_nodes
8080
export seemingly_equal, numbers_match
81-
export trim!, trim
81+
export trim!, trim, explode
8282

8383
include("partitioning.jl")
8484
export PColorPartitions, PartitionCells, PartitionBFaces, PartitionNodes, NodePermutation, PartitionEdges

src/extendablegrid.jl

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,118 @@ function trim(grid::ExtendableGrid; keep = [])
823823

824824
return grid_copy
825825
end
826+
827+
828+
"""
829+
$(TYPEDSIGNATURES)
830+
831+
Remove all adjacency information from a grid, creating independent cells by duplicating coordinates
832+
and assigning new node indices while keeping the original coordinates.
833+
834+
This function creates a new grid where each cell has its own independent set of nodes by duplicating
835+
the coordinate information and creating new node indices. The original coordinates are preserved,
836+
but adjacency information is removed.
837+
838+
# Arguments
839+
- `grid::ExtendableGrid`: The input grid to be processed
840+
841+
# Returns
842+
- `::ExtendableGrid`: A new grid with duplicated coordinates and independent cells
843+
"""
844+
function explode(grid::ExtendableGrid)
845+
# create a new grid with the same type parameters
846+
Tc = coord_type(grid)
847+
Ti = index_type(grid)
848+
new_grid = ExtendableGrid{Tc, Ti}()
849+
850+
# get original coordinates and cell-node adjacency
851+
coords = grid[Coordinates]
852+
cellnodes = grid[CellNodes]
853+
854+
# create new coordinates array by duplicating nodes for each cell
855+
n_cells = num_cells(grid)
856+
nodes_per_cell = size(cellnodes, 1)
857+
new_coords = similar(coords, size(coords, 1), n_cells * nodes_per_cell)
858+
859+
# create new cell-node adjacency where each cell gets its own nodes
860+
new_cellnodes = similar(cellnodes)
861+
862+
# fill new coordinates and cell-node adjacency
863+
for icell in 1:n_cells
864+
for inode in 1:nodes_per_cell
865+
original_node = cellnodes[inode, icell]
866+
new_node = (icell - 1) * nodes_per_cell + inode
867+
new_coords[:, new_node] = coords[:, original_node]
868+
new_cellnodes[inode, icell] = new_node
869+
end
870+
end
871+
872+
# assign the new coordinates and cell-node adjacency
873+
new_grid[Coordinates] = new_coords
874+
new_grid[CellNodes] = new_cellnodes
875+
876+
# reference original grid as parent
877+
new_grid[ParentGrid] = grid
878+
new_grid[CellParents] = collect(Ti, 1:n_cells)
879+
880+
# copy other components
881+
if haskey(grid, CellGeometries)
882+
new_grid[CellGeometries] = grid[CellGeometries]
883+
end
884+
if haskey(grid, CellRegions)
885+
new_grid[CellRegions] = grid[CellRegions]
886+
end
887+
888+
# handle boundary face nodes and regions
889+
if haskey(grid, BFaceNodes)
890+
# create new boundary face nodes by duplicating for each cell
891+
bfacenodes = grid[BFaceNodes]
892+
bfacecells = grid[BFaceCells]
893+
n_bfaces = num_bfaces(grid)
894+
nodes_per_bface = size(bfacenodes, 1)
895+
new_bfacenodes = similar(bfacenodes)
896+
897+
# map boundary face nodes to new node indices using cell information
898+
for ibface in 1:n_bfaces
899+
# get the cell this boundary face belongs to
900+
cell_id = bfacecells[1, ibface] # first entry is the cell ID
901+
for inode in 1:nodes_per_bface
902+
original_node = bfacenodes[inode, ibface]
903+
# find the local node index within the cell
904+
local_node_id = -1
905+
for icell_node in 1:nodes_per_cell
906+
if cellnodes[icell_node, cell_id] == original_node
907+
local_node_id = icell_node
908+
break
909+
end
910+
end
911+
if local_node_id > 0
912+
# calculate new node index based on cell and local node position
913+
new_node = (cell_id - 1) * nodes_per_cell + local_node_id
914+
new_bfacenodes[inode, ibface] = new_node
915+
else
916+
error("Could not find node $original_node in cell $cell_id")
917+
end
918+
end
919+
end
920+
921+
new_grid[BFaceNodes] = new_bfacenodes
922+
end
923+
924+
# transfer other components
925+
for Component in [
926+
BFaceRegions,
927+
CellGeometries,
928+
BFaceGeometries,
929+
CoordinateSystem,
930+
]
931+
932+
if haskey(grid, Component)
933+
new_grid[Component] = grid[Component]
934+
end
935+
936+
end
937+
938+
939+
return new_grid
940+
end

test/test_explode.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using ExtendableGrids
2+
using Test
3+
4+
@testset "explode function tests" begin
5+
# create a grid
6+
grid = uniform_refine(grid_unitcube(Tetrahedron3D), 3)
7+
8+
# Test explode function
9+
exploded_grid = explode(grid)
10+
11+
# Verify results
12+
@test num_cells(exploded_grid) == num_cells(grid)
13+
@test num_nodes(exploded_grid) == 4 * num_cells(grid) # 4 tetrahedron corners for each cell
14+
15+
# Verify coordinates are preserved
16+
original_coords = Set([Tuple(grid[Coordinates][:, i]) for i in 1:size(grid[Coordinates], 2)])
17+
exploded_coords = Set([Tuple(exploded_grid[Coordinates][:, i]) for i in 1:size(exploded_grid[Coordinates], 2)])
18+
@test original_coords == exploded_coords
19+
20+
# Verify regions are preserved
21+
@test exploded_grid[CellRegions] == grid[CellRegions]
22+
23+
# Verify parent relation
24+
@test exploded_grid[ParentGrid] === grid
25+
end

0 commit comments

Comments
 (0)