| 1 |
# Copyright(C) 1999-2020, 2024 National Technology & Engineering Solutions |
| 2 |
# of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
# NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions are |
| 7 |
# met: |
| 8 |
# |
| 9 |
# * Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following |
| 14 |
# disclaimer in the documentation and/or other materials provided |
| 15 |
# with the distribution. |
| 16 |
# |
| 17 |
# * Neither the name of NTESS nor the names of its |
| 18 |
# contributors may be used to endorse or promote products derived |
| 19 |
# from this software without specific prior written permission. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
from phactori import * |
| 34 |
from paraview.simple import * |
| 35 |
import sys |
| 36 |
from .PhactoriParaviewMultiBlockRecursion import * |
| 37 |
from .PhactoriVectorLibrary import * |
| 38 |
from .PhactoriVtkCellOperations import * |
| 39 |
|
| 40 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 41 |
|
| 42 |
class CellEdgeAngleMetricParameters(): |
| 43 |
def __init__(self): |
| 44 |
self.numPointsPerBlock = None |
| 45 |
self.numCellsPerBlock = None |
| 46 |
self.UseSmallestAngle = True |
| 47 |
self.OffsetIndex = 0 |
| 48 |
self.AngleCellVariableName = "minedgenormalangle" |
| 49 |
self.HeightCellVariableName = "cellheight" |
| 50 |
|
| 51 |
def Initialize(self, inUseSmallestAngle, inOffsetIndex, |
| 52 |
inAngleCellVariableName, inHeightCellVariableName): |
| 53 |
self.numPointsPerBlock = [] |
| 54 |
self.numCellsPerBlock = [] |
| 55 |
self.UseSmallestAngle = inUseSmallestAngle |
| 56 |
self.OffsetIndex = inOffsetIndex |
| 57 |
self.AngleCellVariableName = inAngleCellVariableName |
| 58 |
self.HeightCellVariableName = inHeightCellVariableName |
| 59 |
|
| 60 |
class PhactoriCellEdgeAngleMetrics(PhactoriOperationSpecifics): |
| 61 |
"""experimental filter to find the lengths of the edges of cells in the mesh""" |
| 62 |
def __init__(self): |
| 63 |
PhactoriOperationSpecifics.__init__(self) |
| 64 |
self.inPvFilter = None |
| 65 |
self.pfcelProgrammableFilter = None |
| 66 |
self.mProgFilterString = None |
| 67 |
self.OutputAngleCellVariableName = "minedgenormalangle" |
| 68 |
self.OutputHeightCellVariableName = "cellheight" |
| 69 |
self.UseSmallestAngle = True |
| 70 |
self.OffsetIndex = 0 |
| 71 |
self.DoOperationInCreateParaViewMethod = True |
| 72 |
self.CreateProgrammableFilter = False |
| 73 |
|
| 74 |
def ParseParametersFromJson(self, inJson): |
| 75 |
|
| 76 |
keyval3 = "output nth smallest angle" |
| 77 |
keyval4 = "output nth largest angle" |
| 78 |
if keyval3 in inJson: |
| 79 |
self.UseSmallestAngle = True |
| 80 |
self.OffsetIndex = inJson[keyval3] |
| 81 |
elif keyval4 in inJson: |
| 82 |
self.UseSmallestAngle = False |
| 83 |
self.OffsetIndex = inJson[keyval3] |
| 84 |
else: |
| 85 |
self.UseSmallestAngle = True |
| 86 |
self.OffsetIndex = 0 |
| 87 |
|
| 88 |
keyval10 = "output angle cell variable name" |
| 89 |
if keyval10 in inJson: |
| 90 |
self.OutputAngleCellVariableName = inJson[keyval10] |
| 91 |
keyval10 = "output height cell variable name" |
| 92 |
if keyval10 in inJson: |
| 93 |
self.OutputHeightCellVariableName = inJson[keyval10] |
| 94 |
|
| 95 |
keyval11 = "do operation in createparaview method" |
| 96 |
if keyval11 in inJson: |
| 97 |
self.DoOperationInCreateParaViewMethod = inJson[keyval11] |
| 98 |
|
| 99 |
keyval12 = "create programmable filter" |
| 100 |
if keyval12 in inJson: |
| 101 |
self.CreateProgrammableFilter = inJson[keyval12] |
| 102 |
|
| 103 |
def CreateParaViewFilter(self, inInputFilter): |
| 104 |
"""create the MergeBlocks filter for ParaView""" |
| 105 |
if PhactoriDbg(100): |
| 106 |
myDebugPrint3("PhactoriCellEdgeAngleMetrics.CreateParaViewFilter " |
| 107 |
"entered\n", 100) |
| 108 |
self.inPvFilter = inInputFilter |
| 109 |
|
| 110 |
savedActiveSource = GetActiveSource() |
| 111 |
|
| 112 |
UpdatePipelineWithCurrentTimeArgument(self.inPvFilter) |
| 113 |
|
| 114 |
if self.DoOperationInCreateParaViewMethod: |
| 115 |
if PhactoriDbg(100): |
| 116 |
myDebugPrint3("point and cell array info before calculating metrics:") |
| 117 |
RecursivelyPrintPointAndCellArrayInformation(self.inPvFilter) |
| 118 |
|
| 119 |
recursionObj = PhactoriParaviewMultiBlockRecursionControl() |
| 120 |
recursionObj.mParameters = CellEdgeAngleMetricParameters() |
| 121 |
recursionObj.mParameters.Initialize(self.UseSmallestAngle, |
| 122 |
self.OffsetIndex, |
| 123 |
self.OutputAngleCellVariableName, |
| 124 |
self.OutputHeightCellVariableName) |
| 125 |
recursionObj.mOperationToDoPerBlock = self.FindCellEdgeAngleMetricsInBlock |
| 126 |
PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.inPvFilter) |
| 127 |
|
| 128 |
UpdatePipelineWithCurrentTimeArgument(self.inPvFilter) |
| 129 |
|
| 130 |
if PhactoriDbg(100): |
| 131 |
myDebugPrint3("point and cell array info after calculating metrics:") |
| 132 |
RecursivelyPrintPointAndCellArrayInformation(self.inPvFilter) |
| 133 |
|
| 134 |
#SetActiveSource(newParaViewFilter) |
| 135 |
#UpdatePipelineWithCurrentTimeArgument(newParaViewFilter) |
| 136 |
|
| 137 |
if self.CreateProgrammableFilter: |
| 138 |
self.pfcelProgrammableFilter = ProgrammableFilter(Input = self.inPvFilter) |
| 139 |
self.pfcelProgrammableFilter.CopyArrays = 1 |
| 140 |
self.CreateProgrammableFilterString() |
| 141 |
self.pfcelProgrammableFilter.Script = self.mProgFilterString |
| 142 |
self.pfcelProgrammableFilter.UpdatePipeline() |
| 143 |
|
| 144 |
SetActiveSource(savedActiveSource) |
| 145 |
|
| 146 |
if self.CreateProgrammableFilter: |
| 147 |
if PhactoriDbg(100): |
| 148 |
myDebugPrint3("PhactoriCellEdgeAngleMetrics.CreateParaViewFilter " |
| 149 |
"returning programmable filter\n", 100) |
| 150 |
return self.pfcelProgrammableFilter |
| 151 |
else: |
| 152 |
if PhactoriDbg(100): |
| 153 |
myDebugPrint3("PhactoriCellEdgeAngleMetrics.CreateParaViewFilter " |
| 154 |
"returning passthrough filter\n", 100) |
| 155 |
return self.inPvFilter |
| 156 |
|
| 157 |
@staticmethod |
| 158 |
def FindCellEdgeAngleMetricsInBlock(recursionObject, inInputCsData, inParameters): |
| 159 |
if PhactoriDbg(100): |
| 160 |
myDebugPrint3("FindCellEdgeAngleMetricsInBlock entered\n") |
| 161 |
|
| 162 |
numCells = inInputCsData.GetNumberOfCells() |
| 163 |
numPoints = inInputCsData.GetNumberOfPoints() |
| 164 |
if PhactoriDbg(100): |
| 165 |
myDebugPrint3("numCells: " + str(numCells) + \ |
| 166 |
" numPoints: " + str(numPoints) + "\n") |
| 167 |
inParameters.numCellsPerBlock.append(numCells) |
| 168 |
inParameters.numPointsPerBlock.append(numPoints) |
| 169 |
|
| 170 |
ncda = vtk.vtkDoubleArray() |
| 171 |
ncda.SetNumberOfTuples(numCells) |
| 172 |
ncda2 = vtk.vtkDoubleArray() |
| 173 |
ncda2.SetNumberOfTuples(numCells) |
| 174 |
|
| 175 |
outputAngles = [] |
| 176 |
outputHeights = [] |
| 177 |
for ii in range(0, numCells): |
| 178 |
#if PhactoriDbg(100): |
| 179 |
# myDebugPrint3("cell index " + str(ii) + " of " + str(numCells) + "\n") |
| 180 |
edgeAngle, cellHeight = PhactoriFindCellEdgeAngleMetricsForOneCell(inInputCsData, ii, |
| 181 |
inParameters.UseSmallestAngle, inParameters.OffsetIndex) |
| 182 |
outputAngles.append(edgeAngle) |
| 183 |
outputHeights.append(cellHeight) |
| 184 |
ncda.SetValue(ii, edgeAngle) |
| 185 |
ncda2.SetValue(ii, cellHeight) |
| 186 |
|
| 187 |
celldata = inInputCsData.GetCellData() |
| 188 |
ncda.SetName(inParameters.AngleCellVariableName) |
| 189 |
celldata.AddArray(ncda) |
| 190 |
ncda2.SetName(inParameters.HeightCellVariableName) |
| 191 |
celldata.AddArray(ncda2) |
| 192 |
if PhactoriDbg(100): |
| 193 |
myDebugPrint3("numCells: " + str(numCells) + "\n" |
| 194 |
"inParameters.AngleCellVariableName: " + inParameters.AngleCellVariableName + "\n" |
| 195 |
"inParameters.HeightCellVariableName: " + inParameters.HeightCellVariableName + "\n") |
| 196 |
if numCells > 0: |
| 197 |
myDebugPrint3("edgeAngle 0: " + str(ncda.GetValue(0)) + "\n") |
| 198 |
|
| 199 |
if PhactoriDbg(100): |
| 200 |
minOutputAngle = 91.0 |
| 201 |
minOutputAngleIndex = -1 |
| 202 |
maxOutputAngle = -1.0 |
| 203 |
maxOutputAngleIndex = -1 |
| 204 |
minHeight = 1e15 |
| 205 |
minHeightIndex = -1 |
| 206 |
maxHeight = -1.0 |
| 207 |
maxHeightIndex = -1 |
| 208 |
for ii in range(0, numCells): |
| 209 |
edgeAngle = outputAngles[ii] |
| 210 |
cellHeight = outputAngles[ii] |
| 211 |
if edgeAngle < minOutputAngle: |
| 212 |
minOutputAngle = edgeAngle |
| 213 |
minOutputAngleIndex = ii |
| 214 |
if edgeAngle > maxOutputAngle: |
| 215 |
maxOutputAngle = edgeAngle |
| 216 |
maxOutputAngleIndex = ii |
| 217 |
if cellHeight < minHeight: |
| 218 |
minHeight = cellHeight |
| 219 |
minHeightIndex = ii |
| 220 |
if cellHeight > maxHeight: |
| 221 |
maxHeight = cellHeight |
| 222 |
maxHeightIndex = ii |
| 223 |
myDebugPrint3( |
| 224 |
"minOutputAngle, index: " + str(minOutputAngle) + ", " + str(minOutputAngleIndex) + "\n" + \ |
| 225 |
"maxOutputAngle, index: " + str(maxOutputAngle) + ", " + str(maxOutputAngleIndex) + "\n" + \ |
| 226 |
"minHeight, index: " + str(minHeight) + ", " + str(minHeightIndex) + "\n" + \ |
| 227 |
"maxHeight, index: " + str(maxHeight) + ", " + str(maxHeightIndex) + "\n" + \ |
| 228 |
"FindCellEdgeAngleMetricsInBlock returning\n") |
| 229 |
return outputAngles, outputHeights |
| 230 |
|
| 231 |
def CreateProgrammableFilterString(self): |
| 232 |
|
| 233 |
if self.mProgFilterString != None: |
| 234 |
return |
| 235 |
|
| 236 |
scriptLines = [] |
| 237 |
|
| 238 |
scriptLines.append("import math\n") |
| 239 |
GetPhactoriVectorLibraryProgrammableFilterLines(scriptLines) |
| 240 |
GetPhactoriVtkCellOperationsProgrammableFilterLines(scriptLines) |
| 241 |
|
| 242 |
scriptLines.append("localLeafVisitCount = 0\n") |
| 243 |
scriptLines.append("def flatten(input, output):\n") |
| 244 |
scriptLines.append(" # Copy the cells etc.\n") |
| 245 |
scriptLines.append(" output.ShallowCopy(input)\n") |
| 246 |
scriptLines.append(" numPoints = input.GetNumberOfPoints()\n") |
| 247 |
scriptLines.append(" celldata = output.GetCellData()\n") |
| 248 |
scriptLines.append(" numCells = input.GetNumberOfCells()\n") |
| 249 |
scriptLines.append(" #print(str(numCells))\n") |
| 250 |
scriptLines.append(" ncda = vtk.vtkDoubleArray()\n") |
| 251 |
scriptLines.append(" ncda.SetNumberOfTuples(numCells)\n") |
| 252 |
scriptLines.append(" ncda2 = vtk.vtkDoubleArray()\n") |
| 253 |
scriptLines.append(" ncda2.SetNumberOfTuples(numCells)\n") |
| 254 |
scriptLines.append(" numTuples = ncda.GetNumberOfTuples()\n") |
| 255 |
scriptLines.append(" for ii in range(0, numTuples):\n") |
| 256 |
scriptLines.append(" ncda.SetValue(ii, -1.0)\n") |
| 257 |
scriptLines.append(" ncda2.SetValue(ii, -1.0)\n") |
| 258 |
scriptLines.append(" for ii in range(0, numCells):\n") |
| 259 |
scriptLines.append(" cellAngle, cellHeight = PhactoriFindCellEdgeAngleMetricsForOneCell(input, ii, " + \ |
| 260 |
str(self.UseSmallestAngle) + ", " + str(self.OffsetIndex) + ")\n") |
| 261 |
scriptLines.append(" ncda.SetValue(ii, cellAngle)\n") |
| 262 |
scriptLines.append(" ncda2.SetValue(ii, cellHeight)\n") |
| 263 |
scriptLines.append(" ncda.SetName('" + self.OutputAngleCellVariableName + "')\n") |
| 264 |
scriptLines.append(" celldata.AddArray(ncda)\n") |
| 265 |
scriptLines.append(" ncda2.SetName('" + self.OutputHeightCellVariableName + "')\n") |
| 266 |
scriptLines.append(" celldata.AddArray(ncda2)\n") |
| 267 |
|
| 268 |
scriptLines.append("input = self.GetInputDataObject(0, 0)\n") |
| 269 |
scriptLines.append("output = self.GetOutputDataObject(0)\n") |
| 270 |
|
| 271 |
scriptLines.append("if input.IsA('vtkMultiBlockDataSet'):\n") |
| 272 |
scriptLines.append(" output.CopyStructure(input)\n") |
| 273 |
scriptLines.append(" iter = input.NewIterator()\n") |
| 274 |
scriptLines.append(" iter.UnRegister(None)\n") |
| 275 |
scriptLines.append(" iter.InitTraversal()\n") |
| 276 |
scriptLines.append(" while not iter.IsDoneWithTraversal():\n") |
| 277 |
scriptLines.append(" localLeafVisitCount += 1\n") |
| 278 |
scriptLines.append(" curInput = iter.GetCurrentDataObject()\n") |
| 279 |
scriptLines.append(" curOutput = curInput.NewInstance()\n") |
| 280 |
scriptLines.append(" curOutput.UnRegister(None)\n") |
| 281 |
scriptLines.append(" output.SetDataSet(iter, curOutput)\n") |
| 282 |
scriptLines.append(" flatten(curInput, curOutput)\n") |
| 283 |
scriptLines.append(" iter.GoToNextItem();\n") |
| 284 |
scriptLines.append("else:\n") |
| 285 |
scriptLines.append(" flatten(input, output)\n") |
| 286 |
|
| 287 |
self.mProgFilterString = "".join(scriptLines) |
| 288 |
|
| 289 |
if PhactoriDbg(100): |
| 290 |
myDebugPrint3("PhactoriCellEdgeAngleMetrics constructed script:\n" + \ |
| 291 |
self.mProgFilterString) |
| 292 |
|
| 293 |
#phactori_combine_to_single_python_file_subpiece_end_1 |