| 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 |
|
| 39 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 40 |
|
| 41 |
class CellEdgeLengthCalculationParameters(): |
| 42 |
def __init__(self): |
| 43 |
self.numPointsPerBlock = None |
| 44 |
self.numCellsPerBlock = None |
| 45 |
self.OutputingFromShortest = True |
| 46 |
self.OutputShortestLongestIndex = 0 |
| 47 |
|
| 48 |
def Initialize(self, inOutputingFromShortest, inOutputShortestLongestIndex): |
| 49 |
self.numPointsPerBlock = [] |
| 50 |
self.numCellsPerBlock = [] |
| 51 |
self.OutputingFromShortest = inOutputingFromShortest |
| 52 |
self.OutputShortestLongestIndex = inOutputShortestLongestIndex |
| 53 |
|
| 54 |
class PhactoriFindCellEdgeLengths(PhactoriOperationSpecifics): |
| 55 |
"""experimental filter to find the lengths of the edges of cells in the mesh""" |
| 56 |
def __init__(self): |
| 57 |
PhactoriOperationSpecifics.__init__(self) |
| 58 |
self.inPvFilter = None |
| 59 |
self.pfcelProgrammableFilter = None |
| 60 |
self.mProgFilterString = None |
| 61 |
self.ProgrammableFilterOutputCellVariableName = "mincelledgelength" |
| 62 |
self.OutputingFromShortest = True |
| 63 |
self.OutputShortestLongestIndex = 0 |
| 64 |
|
| 65 |
def ParseParametersFromJson(self, inJson): |
| 66 |
|
| 67 |
keyval3 = "output nth shortest edge" |
| 68 |
keyval4 = "output nth longest edge" |
| 69 |
if keyval3 in inJson: |
| 70 |
self.OutputingFromShortest = True |
| 71 |
self.OutputShortestLongestIndex = inJson[keyval3] |
| 72 |
elif keyval4 in inJson: |
| 73 |
self.OutputingFromShortest = False |
| 74 |
self.OutputShortestLongestIndex = inJson[keyval3] |
| 75 |
else: |
| 76 |
self.OutputingFromShortest = True |
| 77 |
self.OutputShortestLongestIndex = 0 |
| 78 |
|
| 79 |
keyval10 = "output cell variable name" |
| 80 |
if keyval10 in inJson: |
| 81 |
self.ProgrammableFilterOutputCellVariableName = inJson[keyval10] |
| 82 |
|
| 83 |
def CreateParaViewFilter(self, inInputFilter): |
| 84 |
"""create the MergeBlocks filter for ParaView""" |
| 85 |
if PhactoriDbg(100): |
| 86 |
myDebugPrint3("PhactoriFindCellEdgeLengths.CreateParaViewFilter " |
| 87 |
"entered\n", 100) |
| 88 |
self.inPvFilter = inInputFilter |
| 89 |
|
| 90 |
savedActiveSource = GetActiveSource() |
| 91 |
|
| 92 |
UpdatePipelineWithCurrentTimeArgument(inInputFilter) |
| 93 |
|
| 94 |
#SetActiveSource(newParaViewFilter) |
| 95 |
#UpdatePipelineWithCurrentTimeArgument(newParaViewFilter) |
| 96 |
#SetActiveSource(savedActiveSource) |
| 97 |
|
| 98 |
self.pfcelProgrammableFilter = ProgrammableFilter(Input = self.inPvFilter) |
| 99 |
self.pfcelProgrammableFilter.CopyArrays = 1 |
| 100 |
self.CreateProgrammableFilterString() |
| 101 |
self.pfcelProgrammableFilter.Script = self.mProgFilterString |
| 102 |
self.pfcelProgrammableFilter.UpdatePipeline() |
| 103 |
|
| 104 |
SetActiveSource(self.pfcelProgrammableFilter) |
| 105 |
SetActiveSource(savedActiveSource) |
| 106 |
|
| 107 |
if PhactoriDbg(100): |
| 108 |
myDebugPrint3("PhactoriFindCellEdgeLengths.CreateParaViewFilter " |
| 109 |
"returning\n", 100) |
| 110 |
|
| 111 |
return self.pfcelProgrammableFilter |
| 112 |
|
| 113 |
@staticmethod |
| 114 |
def FindCellEdgeLengthsForOneCell(inInputCsData, inParameters, inCellIndex, |
| 115 |
paramOutputingFromShortest, paramOutputShortestLongestIndex): |
| 116 |
oneCell = inInputCsData.GetCell(inCellIndex) |
| 117 |
#if PhactoriDbg(100): |
| 118 |
# myDebugPrint3("oneCell: " + str(oneCell) + "\n") |
| 119 |
numEdges = oneCell.GetNumberOfEdges() |
| 120 |
ptA = [0.0, 0.0, 0.0] |
| 121 |
ptB = [0.0, 0.0, 0.0] |
| 122 |
edgeLengthList = [] |
| 123 |
for ii in range(0, numEdges): |
| 124 |
oneEdge = oneCell.GetEdge(ii) |
| 125 |
edgePoints = oneEdge.GetPointIds() |
| 126 |
ptAId = edgePoints.GetId(0) |
| 127 |
ptBId = edgePoints.GetId(1) |
| 128 |
inInputCsData.GetPoint(ptAId, ptA) |
| 129 |
inInputCsData.GetPoint(ptBId, ptB) |
| 130 |
edgeLength = vecDistance(ptA, ptB) |
| 131 |
edgeLengthList.append(edgeLength) |
| 132 |
sortedEdgeLengths = sorted(edgeLengthList) |
| 133 |
|
| 134 |
if paramOutputingFromShortest: |
| 135 |
outputIndex = min(paramOutputShortestLongestIndex, len(sortedEdgeLengths) - 1) |
| 136 |
else: |
| 137 |
outputIndex = max(0, len(sortedEdgeLengths) - 1 - paramOutputShortestLongestIndex) |
| 138 |
|
| 139 |
if PhactoriDbg(100): |
| 140 |
myDebugPrint3( |
| 141 |
"shortest edge length: " + str(sortedEdgeLengths[0]) + "\n" |
| 142 |
" longest edge length: " + str(sortedEdgeLengths[-1]) + "\n" |
| 143 |
" output edge length: " + str(sortedEdgeLengths[outputIndex]) + "\n" |
| 144 |
" shortest/longest flag: " + str(paramOutputingFromShortest) + "\n" |
| 145 |
"shortest/longest index: " + str(paramOutputShortestLongestIndex) + "\n" |
| 146 |
" outputIndex: " + str(outputIndex) + "\n") |
| 147 |
|
| 148 |
@staticmethod |
| 149 |
def FindCellEdgeLengthsForCellsInBlock(recursionObject, inInputCsData, inParameters): |
| 150 |
if PhactoriDbg(100): |
| 151 |
myDebugPrint3("FindCellEdgeLengthsForCellsInBlock entered\n") |
| 152 |
|
| 153 |
numCells = inInputCsData.GetNumberOfCells() |
| 154 |
numPoints = inInputCsData.GetNumberOfPoints() |
| 155 |
if PhactoriDbg(100): |
| 156 |
myDebugPrint3("numCells: " + str(numCells) + \ |
| 157 |
" numPoints: " + str(numPoints) + "\n") |
| 158 |
inParameters.numCellsPerBlock.append(numCells) |
| 159 |
inParameters.numPointsPerBlock.append(numPoints) |
| 160 |
|
| 161 |
for ii in range(0, numCells): |
| 162 |
if PhactoriDbg(100): |
| 163 |
myDebugPrint3("cell index " + str(ii) + " of " + str(numCells) + "\n") |
| 164 |
PhactoriFindCellEdgeLengths.FindCellEdgeLengthsForOneCell( |
| 165 |
inInputCsData, inParameters, ii, |
| 166 |
inParameters.OutputingFromShortest, |
| 167 |
inParameters.OutputShortestLongestIndex) |
| 168 |
|
| 169 |
if PhactoriDbg(100): |
| 170 |
myDebugPrint3("FindCellEdgeLengthsForCellsInBlock returning\n") |
| 171 |
|
| 172 |
def ExportOperationData(self, datadescription): |
| 173 |
if PhactoriDbg(100): |
| 174 |
myDebugPrint3("PhactoriFindCellEdgeLengths.ExportOperationData entered\n", |
| 175 |
100) |
| 176 |
|
| 177 |
UpdatePipelineWithCurrentTimeArgument(self.inPvFilter) |
| 178 |
|
| 179 |
#recursionObj = PhactoriParaviewMultiBlockRecursionControl() |
| 180 |
#recursionObj.mParameters = CellEdgeLengthCalculationParameters() |
| 181 |
#recursionObj.mParameters.Initialize(self.OutputingFromShortest, |
| 182 |
# self.OutputShortestLongestIndex) |
| 183 |
#recursionObj.mOperationToDoPerBlock = self.FindCellEdgeLengthsForCellsInBlock |
| 184 |
#PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.inPvFilter) |
| 185 |
|
| 186 |
if PhactoriDbg(100): |
| 187 |
myDebugPrint3("PhactoriFindCellEdgeLengths.ExportOperationData returning\n", |
| 188 |
100) |
| 189 |
|
| 190 |
def CreateProgrammableFilterString(self): |
| 191 |
|
| 192 |
if self.mProgFilterString != None: |
| 193 |
return |
| 194 |
|
| 195 |
scriptLines = [] |
| 196 |
|
| 197 |
scriptLines.append("#print('test2')\n") |
| 198 |
|
| 199 |
scriptLines.append("localLeafVisitCount = 0\n") |
| 200 |
|
| 201 |
scriptLines.append("import math\n") |
| 202 |
scriptLines.append("def FindEdgeLengthsForOneCell(inInputCsData, inCellIndex):\n") |
| 203 |
scriptLines.append(" oneCell = inInputCsData.GetCell(inCellIndex)\n") |
| 204 |
scriptLines.append(" numEdges = oneCell.GetNumberOfEdges()\n") |
| 205 |
scriptLines.append(" ptA = [0.0, 0.0, 0.0]\n") |
| 206 |
scriptLines.append(" ptB = [0.0, 0.0, 0.0]\n") |
| 207 |
scriptLines.append(" edgeLengthList = []\n") |
| 208 |
scriptLines.append(" for ii in range(0, numEdges):\n") |
| 209 |
scriptLines.append(" oneEdge = oneCell.GetEdge(ii)\n") |
| 210 |
scriptLines.append(" edgePoints = oneEdge.GetPointIds()\n") |
| 211 |
scriptLines.append(" ptAId = edgePoints.GetId(0)\n") |
| 212 |
scriptLines.append(" ptBId = edgePoints.GetId(1)\n") |
| 213 |
scriptLines.append(" inInputCsData.GetPoint(ptAId, ptA)\n") |
| 214 |
scriptLines.append(" inInputCsData.GetPoint(ptBId, ptB)\n") |
| 215 |
scriptLines.append(" dx = ptB[0] - ptA[0]\n") |
| 216 |
scriptLines.append(" dy = ptB[1] - ptA[1]\n") |
| 217 |
scriptLines.append(" dz = ptB[2] - ptA[2]\n") |
| 218 |
scriptLines.append(" edgeLength = dx*dx + dy*dy + dz*dz\n") |
| 219 |
scriptLines.append(" edgeLengthList.append(edgeLength)\n") |
| 220 |
scriptLines.append(" sortedEdgeLengths = sorted(edgeLengthList)\n") |
| 221 |
pickIndexStr = str(self.OutputShortestLongestIndex) |
| 222 |
if self.OutputingFromShortest: |
| 223 |
scriptLines.append(" outputIndex = min("+pickIndexStr+", len(sortedEdgeLengths) - 1)\n") |
| 224 |
else: |
| 225 |
scriptLines.append(" outputIndex = max(0, len(sortedEdgeLengths) - 1 - "+pickIndexStr+")\n") |
| 226 |
|
| 227 |
scriptLines.append(" returnEdgeLength = math.sqrt(sortedEdgeLengths[outputIndex])\n") |
| 228 |
scriptLines.append(" return returnEdgeLength\n") |
| 229 |
|
| 230 |
scriptLines.append("def flatten(input, output):\n") |
| 231 |
scriptLines.append(" # Copy the cells etc.\n") |
| 232 |
scriptLines.append(" output.ShallowCopy(input)\n") |
| 233 |
scriptLines.append(" numPoints = input.GetNumberOfPoints()\n") |
| 234 |
scriptLines.append(" celldata = output.GetCellData()\n") |
| 235 |
scriptLines.append(" numCells = input.GetNumberOfCells()\n") |
| 236 |
scriptLines.append(" #print(str(numCells))\n") |
| 237 |
scriptLines.append(" ncda = vtk.vtkDoubleArray()\n") |
| 238 |
scriptLines.append(" ncda.SetNumberOfTuples(numCells)\n") |
| 239 |
scriptLines.append(" numTuples = ncda.GetNumberOfTuples()\n") |
| 240 |
scriptLines.append(" for ii in range(0, numTuples):\n") |
| 241 |
scriptLines.append(" ncda.SetValue(ii, -1.0)\n") |
| 242 |
scriptLines.append(" for ii in range(0, numCells):\n") |
| 243 |
scriptLines.append(" minEdgeLengthForCell = FindEdgeLengthsForOneCell(input, ii)\n") |
| 244 |
scriptLines.append(" ncda.SetValue(ii, minEdgeLengthForCell)\n") |
| 245 |
scriptLines.append(" ncda.SetName('" + self.ProgrammableFilterOutputCellVariableName + "')\n") |
| 246 |
scriptLines.append(" #print(ncda.GetName())\n") |
| 247 |
scriptLines.append(" celldata.AddArray(ncda)\n") |
| 248 |
|
| 249 |
scriptLines.append("input = self.GetInputDataObject(0, 0)\n") |
| 250 |
scriptLines.append("output = self.GetOutputDataObject(0)\n") |
| 251 |
|
| 252 |
scriptLines.append("if input.IsA('vtkMultiBlockDataSet'):\n") |
| 253 |
scriptLines.append(" output.CopyStructure(input)\n") |
| 254 |
scriptLines.append(" iter = input.NewIterator()\n") |
| 255 |
scriptLines.append(" iter.UnRegister(None)\n") |
| 256 |
scriptLines.append(" iter.InitTraversal()\n") |
| 257 |
scriptLines.append(" while not iter.IsDoneWithTraversal():\n") |
| 258 |
scriptLines.append(" localLeafVisitCount += 1\n") |
| 259 |
scriptLines.append(" curInput = iter.GetCurrentDataObject()\n") |
| 260 |
scriptLines.append(" curOutput = curInput.NewInstance()\n") |
| 261 |
scriptLines.append(" curOutput.UnRegister(None)\n") |
| 262 |
scriptLines.append(" output.SetDataSet(iter, curOutput)\n") |
| 263 |
scriptLines.append(" flatten(curInput, curOutput)\n") |
| 264 |
scriptLines.append(" iter.GoToNextItem();\n") |
| 265 |
scriptLines.append("else:\n") |
| 266 |
scriptLines.append(" flatten(input, output)\n") |
| 267 |
|
| 268 |
self.mProgFilterString = "".join(scriptLines) |
| 269 |
|
| 270 |
if PhactoriDbg(100): |
| 271 |
myDebugPrint3("PhactoriFindCellEdgeLengths constructed script:\n" + \ |
| 272 |
self.mProgFilterString) |
| 273 |
|
| 274 |
#phactori_combine_to_single_python_file_subpiece_end_1 |