@@ -823,3 +823,118 @@ function trim(grid::ExtendableGrid; keep = [])
823823
824824 return grid_copy
825825end
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
0 commit comments