Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding box function and mirrorGrid changes #59

Merged
merged 5 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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