From f258d70db91966560a1ca66d85af7bc7740bf157 Mon Sep 17 00:00:00 2001 From: Ben Dudson Date: Fri, 20 Jun 2025 19:51:23 -0700 Subject: [PATCH 1/2] Plotting: Hide regions of mesh in penalty regions Makes clearer the parts of the mesh that are inside the wall, by not showing parts that are masked by the penalty region. --- hypnotoad/core/mesh.py | 83 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/hypnotoad/core/mesh.py b/hypnotoad/core/mesh.py index d303a350..490f6a5c 100644 --- a/hypnotoad/core/mesh.py +++ b/hypnotoad/core/mesh.py @@ -3072,9 +3072,11 @@ def smoothnl(self, varname): if change < 1.0e-3: break - def plotGridCellEdges(self, ax=None, **kwargs): + def plotGridCellEdges(self, ax=None, exclude_penalty=True, **kwargs): """ Plot lines between cell corners + + exclude_penalty Exclude regions where penalty mask > 0.99? """ from matplotlib import pyplot from cycler import cycle @@ -3087,20 +3089,60 @@ def plotGridCellEdges(self, ax=None, **kwargs): for region in self.regions.values(): c = next(colors) label = region.myID + jmin = 0 + jmax = region.Rxy.corners.shape[1] - 1 + + if exclude_penalty: + # Calculate penalty mask at the lower corners first by padding all boundaries + # and then averaging + penalty_corners = numpy.ndarray((region.nx + 2, region.ny + 2)) + penalty_corners[1:-1, 1:-1] = region.penalty_mask + # Pad edges + penalty_corners[0, 1:-1] = region.penalty_mask[0, :] + penalty_corners[-1, 1:-1] = region.penalty_mask[-1, :] + penalty_corners[1:-1, 0] = region.penalty_mask[:, 0] + penalty_corners[1:-1, -1] = region.penalty_mask[:, -1] + # corners + penalty_corners[0, 0] = region.penalty_mask[0, 0] + penalty_corners[0, -1] = region.penalty_mask[0, -1] + penalty_corners[-1, 0] = region.penalty_mask[-1, 0] + penalty_corners[-1, -1] = region.penalty_mask[-1, -1] + # Average 4 cells to get corner value + penalty_corners = 0.25 * ( + penalty_corners[1:, 1:] + + penalty_corners[:-1, 1:] + + penalty_corners[1:, :-1] + + penalty_corners[:-1, :-1] + ) + for i in range(region.nx + 1): + if exclude_penalty: + jwhere = numpy.argwhere(penalty_corners[i, :] < 0.99) + if len(jwhere) == 0: + continue + jmin = jwhere[0][0] + jmax = jwhere[-1][0] ax.plot( - region.Rxy.corners[i, :], - region.Zxy.corners[i, :], + region.Rxy.corners[i, jmin : (jmax + 1)], + region.Zxy.corners[i, jmin : (jmax + 1)], c=c, label=label, **kwargs, ) label = None label = region.myID + imin = 0 + imax = region.Zxy.corners.shape[0] - 1 for j in range(region.ny + 1): + if exclude_penalty: + iwhere = numpy.argwhere(penalty_corners[:, j] < 0.99) + if len(iwhere) == 0: + continue + imin = iwhere[0][0] + imax = iwhere[-1][0] ax.plot( - region.Rxy.corners[:, j], - region.Zxy.corners[:, j], + region.Rxy.corners[imin : (imax + 1), j], + region.Zxy.corners[imin : (imax + 1), j], c=c, label=None, **kwargs, @@ -3136,7 +3178,12 @@ def plotPenaltyMask(self, ax=None, **kwargs): alpha=penalty, ) - def plotGridLines(self, ax=None, **kwargs): + def plotGridLines(self, ax=None, exclude_penalty=True, **kwargs): + """ + Plot lines through cell centers + + exclude_penalty Exclude regions where penalty mask > 0.99? + """ from matplotlib import pyplot from cycler import cycle @@ -3150,20 +3197,36 @@ def plotGridLines(self, ax=None, **kwargs): for region in self.regions.values(): c = next(colors) label = region.myID + jmin = 0 + jmax = region.ny - 1 for i in range(region.nx): + if exclude_penalty: + jwhere = numpy.argwhere(region.penalty_mask[i, :] < 0.5) + if len(jwhere) == 0: + continue + jmin = jwhere[0][0] + jmax = jwhere[-1][0] ax.plot( - region.Rxy.centre[i, :], - region.Zxy.centre[i, :], + region.Rxy.centre[i, jmin : (jmax + 1)], + region.Zxy.centre[i, jmin : (jmax + 1)], c=c, label=label, **kwargs, ) label = None label = region.myID + imin = 0 + imax = region.nx - 1 for j in range(region.ny): + if exclude_penalty: + iwhere = numpy.argwhere(region.penalty_mask[:, j] < 0.5) + if len(iwhere) == 0: + continue + imin = iwhere[0][0] + imax = iwhere[-1][0] ax.plot( - region.Rxy.centre[:, j], - region.Zxy.centre[:, j], + region.Rxy.centre[imin : (imax + 1), j], + region.Zxy.centre[imin : (imax + 1), j], c=c, label=None, **kwargs, From 5c312584138b91ad9b8c21d31ec872ab40df44e5 Mon Sep 17 00:00:00 2001 From: Ben Dudson Date: Sat, 21 Jun 2025 14:58:22 -0700 Subject: [PATCH 2/2] Plotting: Simplify plotting of cell edges with penalty mask Plot each cell separately, depending on the penalty mask. --- hypnotoad/core/mesh.py | 84 +++++++++++++----------------------------- 1 file changed, 26 insertions(+), 58 deletions(-) diff --git a/hypnotoad/core/mesh.py b/hypnotoad/core/mesh.py index 490f6a5c..b9c1b057 100644 --- a/hypnotoad/core/mesh.py +++ b/hypnotoad/core/mesh.py @@ -3089,65 +3089,31 @@ def plotGridCellEdges(self, ax=None, exclude_penalty=True, **kwargs): for region in self.regions.values(): c = next(colors) label = region.myID - jmin = 0 - jmax = region.Rxy.corners.shape[1] - 1 - - if exclude_penalty: - # Calculate penalty mask at the lower corners first by padding all boundaries - # and then averaging - penalty_corners = numpy.ndarray((region.nx + 2, region.ny + 2)) - penalty_corners[1:-1, 1:-1] = region.penalty_mask - # Pad edges - penalty_corners[0, 1:-1] = region.penalty_mask[0, :] - penalty_corners[-1, 1:-1] = region.penalty_mask[-1, :] - penalty_corners[1:-1, 0] = region.penalty_mask[:, 0] - penalty_corners[1:-1, -1] = region.penalty_mask[:, -1] - # corners - penalty_corners[0, 0] = region.penalty_mask[0, 0] - penalty_corners[0, -1] = region.penalty_mask[0, -1] - penalty_corners[-1, 0] = region.penalty_mask[-1, 0] - penalty_corners[-1, -1] = region.penalty_mask[-1, -1] - # Average 4 cells to get corner value - penalty_corners = 0.25 * ( - penalty_corners[1:, 1:] - + penalty_corners[:-1, 1:] - + penalty_corners[1:, :-1] - + penalty_corners[:-1, :-1] - ) - - for i in range(region.nx + 1): - if exclude_penalty: - jwhere = numpy.argwhere(penalty_corners[i, :] < 0.99) - if len(jwhere) == 0: - continue - jmin = jwhere[0][0] - jmax = jwhere[-1][0] - ax.plot( - region.Rxy.corners[i, jmin : (jmax + 1)], - region.Zxy.corners[i, jmin : (jmax + 1)], - c=c, - label=label, - **kwargs, - ) - label = None - label = region.myID - imin = 0 - imax = region.Zxy.corners.shape[0] - 1 - for j in range(region.ny + 1): - if exclude_penalty: - iwhere = numpy.argwhere(penalty_corners[:, j] < 0.99) - if len(iwhere) == 0: + + for i in range(region.nx): + for j in range(region.ny): + if exclude_penalty and region.penalty_mask[i, j] > 0.99: continue - imin = iwhere[0][0] - imax = iwhere[-1][0] - ax.plot( - region.Rxy.corners[imin : (imax + 1), j], - region.Zxy.corners[imin : (imax + 1), j], - c=c, - label=None, - **kwargs, - ) - label = None + # Plot cell edges around (i,j) + ax.plot( + [ + region.Rxy.corners[i, j], + region.Rxy.corners[i, j + 1], + region.Rxy.corners[i + 1, j + 1], + region.Rxy.corners[i + 1, j], + ], + [ + region.Zxy.corners[i, j], + region.Zxy.corners[i, j + 1], + region.Zxy.corners[i + 1, j + 1], + region.Zxy.corners[i + 1, j], + ], + c=c, + label=label, + **kwargs, + ) + label = None + return ax def plotPenaltyMask(self, ax=None, **kwargs): from matplotlib import pyplot @@ -3177,6 +3143,7 @@ def plotPenaltyMask(self, ax=None, **kwargs): "k", alpha=penalty, ) + return ax def plotGridLines(self, ax=None, exclude_penalty=True, **kwargs): """ @@ -3234,6 +3201,7 @@ def plotGridLines(self, ax=None, exclude_penalty=True, **kwargs): label = None l = fig.legend() l.set_draggable(True) + return ax def plotPoints( self,