Skip to content

Commit

Permalink
Bounding box function and mirrorGrid changes (#59)
Browse files Browse the repository at this point in the history
* re-doing the changes from: https://github.com/mdolab/cgnsutilities/pull/58/files

* format

* more format

* fix test, gitignore

* removed surface specific mirror grid function and added optional argument to the default mirror grid function
  • Loading branch information
anilyil authored Sep 28, 2022
1 parent 1040111 commit aaa5b66
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
src/libcgns_utils-f2pywrappers2.f90
src/libcgns_utilsmodule.c
examples/717_wl_L2_overwriteBCs.cgns
examples/717_wl_L2_overwriteBCs_array.cgns
examples/717_wl_L2_overwriteFamilies.cgns
examples/717_wl_L2_overwriteBCFamilyWithBC.cgns

config.mk
3 changes: 2 additions & 1 deletion cgnsutilities/cgns_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def get_parser():
p_mirror.add_argument("axis", help="Mirror about plane defined by axis: 'x', 'y', 'z'")
p_mirror.add_argument("tol", nargs="?", default=1e-12, help="Tolerance for node merge")
p_mirror.add_argument("useOldNames", nargs="?", default=False, help="Whether to use old block names")
p_mirror.add_argument("surface", nargs="?", default=False, help="Whether this is a surface or volume grid")
p_mirror.add_argument("outFile", nargs="?", default=None, help="Optional output file")

# ------------- Options for 'split' mode --------------------
Expand Down Expand Up @@ -838,7 +839,7 @@ def main():
curGrid.scale(args.scale)

elif args.mode == "mirror":
curGrid = mirrorGrid(curGrid, args.axis, args.tol, useOldNames=args.useOldNames)
curGrid = mirrorGrid(curGrid, args.axis, args.tol, useOldNames=args.useOldNames, surface=args.surface)

elif args.mode == "coarsen":
curGrid.coarsen()
Expand Down
88 changes: 62 additions & 26 deletions cgnsutilities/cgnsutilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,20 +641,26 @@ def double2D(self):
for blk in self.blocks:
blk.double2D()

def simpleCart(self, dh, hExtra, nExtra, sym, mgcycle, outFile):
"""Generates a Cartesian mesh around the provided grid"""
def getBoundingBox(self):

# Get the bounds of each grid.
xMin = 1e20 * np.ones(3)
xMax = -1.0 * np.ones(3)

for blk in self.blocks:
tmp1 = np.min(blk.coords, axis=(0, 1, 2))
tmp2 = np.max(blk.coords, axis=(0, 1, 2))
for iDim in range(3):
xMin[iDim] = min(xMin[iDim], tmp1[iDim])
xMax[iDim] = max(xMax[iDim], tmp2[iDim])

return xMin, xMax

def simpleCart(self, dh, hExtra, nExtra, sym, mgcycle, outFile):
"""Generates a Cartesian mesh around the provided grid"""

# Get the bounds of the grid.
xMin, xMax = self.getBoundingBox()

# Call the generic routine
return simpleCart(xMin, xMax, dh, hExtra, nExtra, sym, mgcycle, outFile)

Expand Down Expand Up @@ -2942,39 +2948,69 @@ def convertPlot3d(plot3dFile, cgnsFile):
libcgns_utils.utils.convertplot3d(plot3dFile, cgnsFile)


def mirrorGrid(grid, axis, tol, useOldNames=False):
"""Method that takes a grid and mirrors about the axis. Boundary
condition information is retained if possible"""
def mirrorGrid(grid, axis, tol, useOldNames=True, surface=False):
"""
Method that takes a grid and mirrors about the axis. Boundary
condition information is retained if possible
# First make sure the grid is face matched:
grid.split([])
Parameters
----------
# Now copy original blocks
grid : Grid object
The grid object to mirror
axis : str
Direction about which to mirror. ('x', 'y', or 'z')
tol : float
Tolerance used for the block to block connections.
useOldNames : bool, optional
Flag to determine if we want to maintain the old names in the original grid
object. The default behavior adds the string ``_mirror`` to the block id
string thats ahead of the block number at the end. Setting this to ``False``
will rename all blocks in this grid with the grid name and increasing block
indices starting from 1.
surface : bool, optional
Flag to disable volume mesh specific operations here to get this routine
working with surface meshes.
"""

if not surface:
# First make sure the grid is face matched:
grid.split([])

# create the new grid object
newGrid = Grid()

for blk in grid.blocks:
blk.removeSymBCs()
blk.B2Bs = []
newGrid.addBlock(blk)
# rename the new grid if asked for
if useOldNames:
newGrid.name = grid.name

if useOldNames:
# Add the current block name to the new grid
blk.name = blk.name.split(".")[0]
# Now copy original blocks
for blk in grid.blocks:
new_blk = copy.deepcopy(blk)
if not surface:
new_blk.removeSymBCs()
new_blk.B2Bs = []
newGrid.addBlock(new_blk)

mirrorBlk = copy.deepcopy(blk)
mirrorBlk = copy.deepcopy(new_blk)
mirrorBlk.flip(axis)
newGrid.addBlock(mirrorBlk)

if useOldNames:
# Add the new mirrored block name to the new grid
newGrid.blocks[-1].name = blk.name.split(".")[0] + "_mirror"

print(f"Mirroring block: {blk.name} to {newGrid.blocks[-1].name}")
# overwrite the name of the mirror block
mirrorBlk.name = blk.name.split(".")[0] + "_mirror." + blk.name.split(".")[-1]
newGrid.addBlock(mirrorBlk)

# Now rename the blocks and redo-connectivity
newGrid.renameBlocks(useOldNames=useOldNames)
newGrid.renameBCs()
newGrid.connect(tol)
if not useOldNames:
newGrid.renameBlocks()

if not surface:
newGrid.renameBCs()
newGrid.connect(tol)

return newGrid

Expand Down
10 changes: 5 additions & 5 deletions tests/test_cgnsutilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ def test_mirror(self):
# Test mirroring function with useOldNames=True option
newNameList = [
"domain.00001",
"domain_mirror.00001",
"domain.00002",
"domain_mirror.00002",
"domain.00003",
"domain_mirror.00003",
"domain.00004",
"domain_mirror.00004",
"domain.00005",
"domain_mirror.00006",
"domain.00007",
"domain_mirror.00008",
"domain.00009",
"domain_mirror.00010",
"domain_mirror.00005",
]
newMirrorGrid = mirrorGrid(self.grid1, "z", 1e-12, useOldNames=True)
newNames = [blk.name for blk in newMirrorGrid.blocks]
Expand Down

0 comments on commit aaa5b66

Please sign in to comment.