Debian Codegraph @

packages/seacas/libraries/ioss/src/visualization/catalyst/phactori/Operation/PhactoriOperationBlock.py

raw
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 .PhactoriVectorLibrary import *
35 from paraview.simple import *
36 from .PhactoriSegment import *
37 from .PhactoriMpiUtilities import *
38 import json
39
40 #phactori_combine_to_single_python_file_subpiece_begin_1
41
42 global FlagToTestOutgoingPvGeometryFilter
43 FlagToTestOutgoingPvGeometryFilter = False
44
45 global WriteEachDeadCellElementToFiles
46 WriteEachDeadCellElementToFiles = False
47
48 class BlockRecursionControlItem:
49 """see DoMethodPerBlock(); mOperationPerBlock should be set to a method
50 which takes 1 parameter, mParameters should be set to the parameter
51 instance which will be passes to the mOperationPerBlock call"""
52 def __init__(self):
53 self.mOperationPerBlock = None
54 self.mParameters = None
55
56 class PhactoriOperationBlock:
57 """manages one stage of the data pipeline, analagous to ParaView Filter
58
59 An instance of this class represents and manages one stage of the data
60 pipeline which has been set up for management by phatori. It creates and
61 holds a reference to a ParaView/Catalyst/vtk Filter which is doing the
62 real work--this class might be thought of as an adapter or interface from
63 phactori to ParaView. As currently implemented, an operation is assumed to
64 have only one input and one output. Multiple operations can have the same
65 input, so a tree structure is allowed rather than just a linear pipe.
66 Operations with multiple inputs and outputs are conceiveable, and may be
67 added pending user requirements.
68 The instance is presumed to contain a name unique amound the operation
69 blocks and keeps a reference to the input operation (by name), the
70 ParaView/Catalyst filter which is built, and some flags determining where
71 we are in the construction process.
72 """
73 def __init__(self):
74 self.mName = ""
75 self.mInputOperationName = None
76 self.mType = ""
77 self.mHasBeenConstructed = None
78 self.mParaViewFilter = None
79 #for keeping track of data bounds for this operation and only updating
80 #it when necessary
81 self.mDataBoundsIsCurrent = False
82 self.mDataBounds = [0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
83 self.mOperationSpecifics = None
84
85 #If this is a purely internal pipeline filter, this will remain None.
86 #If somebody uses the filter for rendering, it will be created and
87 #handed off for rendering--only needs creation once.
88 self.mOutgoingGeometryFilter = None
89
90 #stores annotation time source, if one gets created associated with this
91 #operation
92 self.mParaViewTimeAnnotationSource = None
93
94 self.mRecursionParameterStore = {}
95
96
97 def GetPvFilter(self):
98 return self.mParaViewFilter
99
100 def CreateOutgoingPvGeometryFilter(self):
101 savedActiveSource = GetActiveSource()
102 self.mOutgoingGeometryFilter = ExtractSurface(self.GetPvFilter())
103 if PhactoriDbg():
104 myDebugPrint3("PhactoriOperationBlock::CreateOutgoingPvGeometryFilter\n"
105 "created self.mOutgoingGeometryFilter: " + \
106 str(self.mOutgoingGeometryFilter) + "\n")
107
108 SetActiveSource(savedActiveSource)
109
110 def GetOutgoingPvGeometryFilter(self):
111 global FlagToTestOutgoingPvGeometryFilter
112 if FlagToTestOutgoingPvGeometryFilter == False:
113 if PhactoriDbg():
114 myDebugPrint3("PhactoriOperationBlock::GetOutgoingPvGeometryFilter\n"
115 "not creating or using external geometry filter:\n")
116 return self.GetPvFilter()
117 if self.mOutgoingGeometryFilter == None:
118 self.CreateOutgoingPvGeometryFilter()
119 return self.mOutgoingGeometryFilter
120
121 def GetListOfInputOperationNames(self):
122 retList = self.mOperationSpecifics.GetListOfInputOperationNamesForThisOperationType()
123 if self.mInputOperationName != None:
124 retList.append(self.mInputOperationName)
125 return retList
126
127 def DoUpdateDueToChangeInData(self, ioPipeAndViewsState):
128 outputPvFilter = self.mParaViewFilter
129 if self.mInputOperationName == None:
130 inputOperation = ioPipeAndViewsState.mIncomingDefaultOperation
131 else:
132 inputOperation = ioPipeAndViewsState.mOperationBlocks[
133 self.mInputOperationName]
134 if inputOperation == None:
135 myDebugPrint3AndException(
136 "PhactoriOperationBlock::DoUpdateDueToChangeInData:\n"
137 "couldn't find input operation with name: " + \
138 str(self.mInputOperationName) + "\n")
139
140 self.mOperationSpecifics.DoUpdateDueToChangeInData(
141 inputOperation.mParaViewFilter, outputPvFilter)
142
143 def DoMethodPerBlockFromParaViewFilter(self, inRecursionControlItem, inPvFilter):
144 pvClientSideData = inPvFilter.GetClientSideObject().GetOutputDataObject(0)
145 if pvClientSideData == None:
146 if PhactoriDbg(100):
147 myDebugPrint3(
148 'DoMethodPerBlock: pvClientSideData is None, returning',100)
149
150 self.DoMethodPerBlockRecurse1(pvClientSideData, inRecursionControlItem)
151
152 def DoMethodPerBlock(self, inRecursionControlItem):
153 """DoMethodPerBlock is a generic method for doing recursion through the
154 multiblock dataset and doing something (a callback) on a per leaf block
155 basis. Gets the clientside data and calls DoMethodPerBlockRecurse1
156 which calls itself on internal nodes and calls
157 inRecursionControlItem.mOperationToDoPerBlock on leaf block nodes"""
158 pvFilter = self.GetPvFilter()
159 self.DoMethodPerBlockFromParaViewFilter(inRecursionControlItem, pvFilter)
160
161 def DoMethodPerBlockRecurse1(self, inInputCsData, inRecursionControlItem):
162 """DoMethodPerBlockRecurse1 is a generic method for doing recursion through
163 the multiblock dataset and doing something (a callback) on a per leaf block
164 basis. Called by DoMethodPerBlock which got clientside data and calls
165 itself on internal nodes and calls
166 inRecursionControlItem.mOperationToDoPerBlock on leaf block nodes"""
167 #if PhactoriDbg(100):
168 # myDebugPrint3('DoMethodPerBlockRecurse1 entered\n', 100)
169
170 icsdClassname = inInputCsData.GetClassName()
171 if icsdClassname == "vtkMultiBlockDataSet" or \
172 icsdClassname == "vtkExodusIIMultiBlockDataSet":
173 #myDebugPrint3('recursing: ' + icsdClassname + '\n')
174 numBlocks = inInputCsData.GetNumberOfBlocks()
175 for ii in range(0, numBlocks):
176 oneBlock = inInputCsData.GetBlock(ii)
177 if(oneBlock != None):
178 self.DoMethodPerBlockRecurse1(oneBlock, inRecursionControlItem)
179 else:
180 inRecursionControlItem.mOperationToDoPerBlock(inInputCsData,
181 inRecursionControlItem.mParameters)
182
183 #if PhactoriDbg(100):
184 # myDebugPrint3('DoMethodPerBlockRecurse1 returning\n', 100)
185
186 def OutputElementListFromOneBlockToFile(self, inInputCsData, inParameters):
187 if PhactoriDbg(100):
188 myDebugPrint3("OutputElementListFromOneBlockToFile entered\n")
189 cellData = inInputCsData.GetCellData()
190 numTuplesX = cellData.GetNumberOfTuples()
191 numArrays = cellData.GetNumberOfArrays()
192
193 global WriteEachDeadCellElementToFiles
194 #if inParameters.mBlockCount == 0:
195 if (inParameters.mFlag1 == 0) and (numTuplesX > 0):
196 inParameters.mFlag1 = 1
197 if WriteEachDeadCellElementToFiles:
198 inParameters.mOutFileff.write("element index")
199 for arrayIndex in range(0, numArrays):
200 inParameters.mOutFileff.write(
201 ", " + str(cellData.GetArray(arrayIndex).GetName()))
202 inParameters.mOutFileff.write("\n")
203
204 if WriteEachDeadCellElementToFiles:
205 for cellIndex in range(0, numTuplesX):
206 inParameters.mOutFileff.write(
207 str(cellIndex + inParameters.mElementCount))
208 for arrayIndex in range(0, numArrays):
209 inParameters.mOutFileff.write(
210 ", " + str(cellData.GetArray(arrayIndex).GetTuple1(cellIndex)))
211 inParameters.mOutFileff.write("\n")
212
213 killed_criteria_array = cellData.GetArray("KILLED")
214 for cellIndex in range(0, numTuplesX):
215 kval = killed_criteria_array.GetTuple1(cellIndex)
216 kvalindex = int(kval)
217 if (kvalindex >= 0) and (kvalindex < 10):
218 inParameters.mKilledByCriteriaCount[kvalindex] += 1
219
220
221 inParameters.mElementCount += inInputCsData.GetNumberOfCells()
222 inParameters.mNodeCount += inInputCsData.GetNumberOfPoints()
223 inParameters.mBlockCount += 1
224
225 if PhactoriDbg(100):
226 myDebugPrint3("OutputElementListFromOneBlockToFile returning\n")
227
228 class FindClosestNPointsToListParams:
229 """recursion structure for FindClosestNPointsToList(). Also servers to
230 store/track data for passing back answer"""
231 def __init__(self, inParentOperation, inNumToFind,
232 inTargetGlobalNodeIdList, inTargetPointXyzList):
233 self.mParentOperation = inParentOperation
234 self.mNumToFind = inNumToFind
235
236 #list of points we need to find closest local points to
237 #(global node ids and geometry xyzs)
238 self.mTargetIds = inTargetGlobalNodeIdList
239 self.mTargetXyzs = inTargetPointXyzList
240
241 self.mKdtree = None
242
243 #closest N local points (local id and id from list and geometry xyzs
244 #from local and from list and distance squared
245 self.mThisProcIds = vtk.vtkIntArray()
246 self.mTargetMatchIds = vtk.vtkIntArray()
247 self.mThisProcXyzs = vtk.vtkDoubleArray()
248 self.mTargetMatchXyzs = vtk.vtkDoubleArray()
249 self.mDistSqrds = vtk.vtkDoubleArray()
250
251 self.mThisProcIds.SetNumberOfValues(inNumToFind)
252 self.mTargetMatchIds.SetNumberOfValues(inNumToFind)
253 self.mThisProcXyzs.SetNumberOfValues(inNumToFind*3)
254 self.mTargetMatchXyzs.SetNumberOfValues(inNumToFind*3)
255 self.mDistSqrds.SetNumberOfValues(inNumToFind)
256
257 #set default values indicating nothing found in those entries
258 for ii in range(0,inNumToFind):
259 self.mThisProcIds.SetValue(ii, -1)
260 self.mTargetMatchIds.SetValue(ii, -1)
261 self.mDistSqrds.SetValue(ii, sys.float_info.max)
262 self.mMinDistSqrd = sys.float_info.max
263 #index of the item that currently has the biggest distance
264 self.mcfndx = 0
265
266 def SetUpKdtree(self):
267 """take the points in this instance (presumably the target object) and
268 put them in a kdtree (using scipy) so we can find the closest point
269 in log(n) time"""
270 from scipy import spatial
271 kdtreepts = []
272 numpts = self.mTargetIds.GetNumberOfValues()
273 for ii in range(0,numpts):
274 pndx = ii*3
275 kdtreepts.append([
276 self.mTargetXyzs.GetValue(pndx),
277 self.mTargetXyzs.GetValue(pndx+1),
278 self.mTargetXyzs.GetValue(pndx+2)])
279 self.mKdtree = None
280 self.mKdtree = spatial.KDTree(kdtreepts)
281
282 def TestPointWithKdtree(self, inSrcId, inSrcXyz):
283 nrstdist, nrstndx = self.mKdtree.query(inSrcXyz)
284 #print "nrstdist: ", str(nrstdist)
285 #print "nrstndx: ", str(nrstndx)
286 tgtId = self.mTargetIds.GetValue(nrstndx)
287 tgtXyz = self.mKdtree.data[nrstndx]
288 #self.TestPointSub1(inSrcId, inSrcXyz[0], inSrcXyz[1], inSrcXyz[2],
289 # tgtId, tgtXyz[0], tgtXyz[1], tgtXyz[2])
290 self.TestPointSub2(inSrcId, inSrcXyz, tgtId, tgtXyz, nrstdist)
291
292 def TestPoint(self, inId, inXyz):
293 """given an xyz point in the local processor (and its global node id)
294 see if it is closer to any of the target points than the current
295 set of nearest points and, if so, put it in the set, dropping
296 others out if necessary"""
297 if PhactoriDbg():
298 myDebugPrint3("TestPoint id " + str(inId))
299 tgtxyzs = self.mTargetXyzs
300 numtgtpts = self.mTargetIds.GetNumberOfValues()
301 for pp in range(0, numtgtpts):
302 tgtid = self.mTargetIds.GetValue(pp)
303 ndx = pp*3
304 self.TestPointSub1(inId, inXyz[0], inXyz[1], inXyz[2],
305 tgtid, tgtxyzs.GetValue(ndx), tgtxyzs.GetValue(ndx+1),
306 tgtxyzs.GetValue(ndx+2))
307
308 def TestPointSub2(self, inSrcId, inSrcXyz, inTgtId, inTgtXyz, inNrstdist):
309 dstsqd = inNrstdist*inNrstdist
310 if dstsqd >= self.mMinDistSqrd:
311 #we already have mNumToFind closer
312 #if PhactoriDbg():
313 # myDebugPrint3("inSrcId inTgtId " + str(inSrcId) + " " + \
314 # str(inTgtId) + \
315 # " too far: " + str(dstsqd) + " >= " + \
316 # str(self.mMinDistSqrd) + "\n")
317 return
318
319 #replace the previous point that was furthest
320 self.mDistSqrds.SetValue(self.mcfndx, dstsqd)
321 self.mThisProcIds.SetValue(self.mcfndx, inSrcId)
322 self.mTargetMatchIds.SetValue(self.mcfndx, inTgtId)
323 gndx = self.mcfndx * 3
324 self.mThisProcXyzs.SetValue(gndx, inSrcXyz[0])
325 self.mThisProcXyzs.SetValue(gndx+1, inSrcXyz[1])
326 self.mThisProcXyzs.SetValue(gndx+2, inSrcXyz[2])
327 self.mTargetMatchXyzs.SetValue(gndx, inTgtXyz[0])
328 self.mTargetMatchXyzs.SetValue(gndx+1, inTgtXyz[1])
329 self.mTargetMatchXyzs.SetValue(gndx+2, inTgtXyz[2])
330 #if PhactoriDbg():
331 # myDebugPrint3("closer point found put in index " + \
332 # str(self.mcfndx) + \
333 # " dstsqrd: " + str(self.mDistSqrds.GetValue(self.mcfndx)) + "\n" + \
334 # "\nsource id xyz: " + \
335 # str(self.mThisProcIds.GetValue(self.mcfndx)) + " " + \
336 # str(self.mThisProcXyzs.GetValue(gndx)) + ", " + \
337 # str(self.mThisProcXyzs.GetValue(gndx+1)) + ", " + \
338 # str(self.mThisProcXyzs.GetValue(gndx+2)) + ", " + \
339 # "\ntarget id xyz: " + \
340 # str(self.mTargetMatchIds.GetValue(self.mcfndx)) + " " + \
341 # str(self.mTargetMatchXyzs.GetValue(gndx)) + ", " + \
342 # str(self.mTargetMatchXyzs.GetValue(gndx+1)) + ", " + \
343 # str(self.mTargetMatchXyzs.GetValue(gndx+2)) + "\n")
344
345 #now find which in the current list has the biggest distance, as it is
346 #next in line for replacement (we do this to avoid having to shift
347 #elements every time
348 self.mcfndx = 0
349 self.mMinDistSqrd = self.mDistSqrds.GetValue(0)
350 #if PhactoriDbg():
351 # myDebugPrint3("find next index to be replaced try 0 \n" + \
352 # str(self.mMinDistSqrd) + "\n")
353 for kk in range(1, self.mNumToFind):
354 #if PhactoriDbg():
355 # myDebugPrint3("try " + str(kk) + " " + \
356 # str(self.mDistSqrds.GetValue(kk)) + " >? " + \
357 # str(self.mMinDistSqrd)+ "\n")
358 if self.mDistSqrds.GetValue(kk) > self.mMinDistSqrd:
359 self.mcfndx = kk
360 self.mMinDistSqrd = self.mDistSqrds.GetValue(kk)
361 #if PhactoriDbg():
362 # myDebugPrint3("yes, now " + str(self.mcfndx) + " " + \
363 # str(self.mMinDistSqrd) + "\n")
364 #if PhactoriDbg():
365 # myDebugPrint3("next to be replaced ndx: " + str(self.mcfndx) + \
366 # " sid: " + \
367 # str(self.mThisProcIds.GetValue(self.mcfndx)) + \
368 # " tid: " + \
369 # str(self.mTargetMatchIds.GetValue(self.mcfndx)) + \
370 # " dsqrd: " + \
371 # str(self.mDistSqrds.GetValue(self.mcfndx)) + "\n")
372
373 def TestPointSub1(self, inId, inX, inY, inZ, tId, tX, tY, tZ):
374 """given an xyz point in the local processor (and its global node id)
375 see if it is closer to one of the target points than the current
376 set of nearest points and, if so, put it in the set, dropping
377 others out if necessary"""
378 ddx = inX - tX
379 ddy = inY - tY
380 ddz = inZ - tZ
381 dstsqd = ddx*ddx + ddy*ddy + ddz*ddz
382 if dstsqd >= self.mMinDistSqrd:
383 #we already have mNumToFind closer
384 #if PhactoriDbg():
385 # myDebugPrint3("inId tId " + str(inId) + " " + str(tId) + \
386 # " too far: " + str(dstsqd) + " >= " + \
387 # str(self.mMinDistSqrd) + "\n")
388 return
389
390 #replace the previous point that was furthest
391 self.mDistSqrds.SetValue(self.mcfndx, dstsqd)
392 self.mThisProcIds.SetValue(self.mcfndx, inId)
393 self.mTargetMatchIds.SetValue(self.mcfndx, tId)
394 gndx = self.mcfndx * 3
395 self.mThisProcXyzs.SetValue(gndx, inX)
396 self.mThisProcXyzs.SetValue(gndx+1, inY)
397 self.mThisProcXyzs.SetValue(gndx+2, inZ)
398 self.mTargetMatchXyzs.SetValue(gndx, tX)
399 self.mTargetMatchXyzs.SetValue(gndx+1, tY)
400 self.mTargetMatchXyzs.SetValue(gndx+2, tZ)
401 if PhactoriDbg():
402 myDebugPrint3("closer point found put in index " + \
403 str(self.mcfndx) + \
404 " dstsqrd: " + str(self.mDistSqrds.GetValue(self.mcfndx)) + "\n" + \
405 "\nsource id xyz: " + \
406 str(self.mThisProcIds.GetValue(self.mcfndx)) + " " + \
407 str(self.mThisProcXyzs.GetValue(gndx)) + ", " + \
408 str(self.mThisProcXyzs.GetValue(gndx+1)) + ", " + \
409 str(self.mThisProcXyzs.GetValue(gndx+2)) + ", " + \
410 "\ntarget id xyz: " + \
411 str(self.mTargetMatchIds.GetValue(self.mcfndx)) + " " + \
412 str(self.mTargetMatchXyzs.GetValue(gndx)) + ", " + \
413 str(self.mTargetMatchXyzs.GetValue(gndx+1)) + ", " + \
414 str(self.mTargetMatchXyzs.GetValue(gndx+2)) + "\n")
415
416 #now find which in the current list has the biggest distance, as it is
417 #next in line for replacement (we do this to avoid having to shift
418 #elements every time
419 self.mcfndx = 0
420 self.mMinDistSqrd = self.mDistSqrds.GetValue(0)
421 if PhactoriDbg():
422 myDebugPrint3("find next index to be replaced try 0 \n" + \
423 str(self.mMinDistSqrd) + "\n")
424 for kk in range(1, self.mNumToFind):
425 if PhactoriDbg():
426 myDebugPrint3("try " + str(kk) + " " + \
427 str(self.mDistSqrds.GetValue(kk)) + " >? " + \
428 str(self.mMinDistSqrd)+ "\n")
429 if self.mDistSqrds.GetValue(kk) > self.mMinDistSqrd:
430 self.mcfndx = kk
431 self.mMinDistSqrd = self.mDistSqrds.GetValue(kk)
432 if PhactoriDbg():
433 myDebugPrint3("yes, now " + str(self.mcfndx) + " " + \
434 str(self.mMinDistSqrd) + "\n")
435 if PhactoriDbg():
436 myDebugPrint3("next to be replaced ndx: " + str(self.mcfndx) + \
437 " sid: " + \
438 str(self.mThisProcIds.GetValue(self.mcfndx)) + \
439 " tid: " + \
440 str(self.mTargetMatchIds.GetValue(self.mcfndx)) + \
441 " dsqrd: " + \
442 str(self.mDistSqrds.GetValue(self.mcfndx)) + "\n")
443
444 def ToStr(self):
445 retStr = "closest " + str(self.mNumToFind) + " points:\n" + \
446 "index: source id, target id, dist sqrd: source xyz: target xyz\n"
447 for ii in range(0, self.mNumToFind):
448 pp = ii*3
449 adst = str(ii) + ": " + \
450 str(self.mThisProcIds.GetValue(ii)) + ", " + \
451 str(self.mTargetMatchIds.GetValue(ii)) + ", " + \
452 str(self.mDistSqrds.GetValue(ii)) + ": " + \
453 str(self.mThisProcXyzs.GetValue(pp)) + ", " + \
454 str(self.mThisProcXyzs.GetValue(pp+1)) + ", " + \
455 str(self.mThisProcXyzs.GetValue(pp+2)) + ": " + \
456 str(self.mTargetMatchXyzs.GetValue(pp)) + ", " + \
457 str(self.mTargetMatchXyzs.GetValue(pp+1)) + ", " + \
458 str(self.mTargetMatchXyzs.GetValue(pp+2)) + "\n"
459 retStr += adst
460 return retStr
461
462
463 def FindClosestNPointsToList(self, inGlobalNodeIdList, inPointXyzList,
464 inNumToFind):
465 """given a list of node ids and xyz points, recursively find the nearest
466 (geometrically) inNumToFind points in the local processor to the xyz
467 points in inPointXyzList. Returns a list of inNumToFind global node
468 ids and inNumToFind xyz points
469 inGlobalNodeIds is vtkIntArray
470 inPointXyzs is vtkDoubleArray
471 returns a FindClosestNPointsToListParams instance which has list of
472 length inNumToFind which contain the node id of the closest local
473 process point, the node id of the corresponding point from the
474 inGlobalNodeIdList, the xyzs of each of those, and this distance
475 between (squared to save computation)"""
476 recursionItem = BlockRecursionControlItem()
477 recursionItem.mParameters = \
478 PhactoriOperationBlock.FindClosestNPointsToListParams(
479 self, inNumToFind, inGlobalNodeIdList, inPointXyzList)
480 recursionItem.mOperationToDoPerBlock = \
481 self.FindClosestNPointsToListInBlock
482 self.DoMethodPerBlock(recursionItem)
483 return recursionItem.mParameters
484
485 def FindClosestNPointsToListInBlock(self, inInputCsData, inParameters):
486 if PhactoriDbg(100):
487 myDebugPrint3("FindClosestNPointsToListInBlock entered\n")
488 pointsArray = inInputCsData.GetPoints()
489 if pointsArray == None:
490 #no points here
491 return
492 pointsData = inInputCsData.GetPointData()
493 globalNodeIdArray = pointsData.GetArray('GlobalNodeId')
494 numPoints = pointsArray.GetNumberOfPoints()
495 ptXyz = [0.0, 0.0, 0.0]
496
497 #get target to set up Kdtree for quickly finding nearest point
498 inParameters.SetUpKdtree()
499
500 for ndx in range(0,numPoints):
501 #thePoint = pointsArray.GetPoint(ndx, ptXyz)
502 if ndx % 100 == 0:
503 if PhactoriDbg():
504 myDebugPrint3("test " + str(ndx) + " of " + str(numPoints) + "\n")
505 pointsArray.GetPoint(ndx, ptXyz)
506 if(globalNodeIdArray == None):
507 theGlobalNodeId = ndx + 1
508 else:
509 theGlobalNodeId = globalNodeIdArray.GetValue(ndx)
510
511 inParameters.TestPointWithKdtree(theGlobalNodeId, ptXyz)
512 #inParameters.TestPoint(theGlobalNodeId, ptXyz)
513 if PhactoriDbg(100):
514 myDebugPrint3("FindClosestNPointsToListInBlock returning\n")
515
516 class MakeListOfAllPoints1Params:
517 def __init__(self):
518 self.mGlobalNodeIdList = vtk.vtkIntArray()
519 self.mPointXYZList = vtk.vtkDoubleArray()
520
521 def MakeListOfAllPoints1(self):
522 """recursively going through multiblock setup, make a list of all the
523 points in this operation output in this process. We get a list
524 of global node ids and a list of xyz geometry points"""
525 recursionItem = BlockRecursionControlItem()
526 recursionItem.mParameters = \
527 PhactoriOperationBlock.MakeListOfAllPoints1Params()
528 recursionItem.mOperationToDoPerBlock = \
529 self.MakeListOfAllPointsInBlock1
530 self.DoMethodPerBlock(recursionItem)
531 return recursionItem.mParameters.mGlobalNodeIdList, \
532 recursionItem.mParameters.mPointXYZList
533
534 def MakeListOfAllPointsInBlock1(self, inInputCsData, inParameters):
535 #if PhactoriDbg(100):
536 # myDebugPrint3("MakeListOfAllPointsInBlock1 entered\n")
537 pointsArray = inInputCsData.GetPoints()
538 if pointsArray == None:
539 #no points here
540 return
541 pointsData = inInputCsData.GetPointData()
542 globalNodeIdArray = pointsData.GetArray('GlobalNodeId')
543 numPoints = pointsArray.GetNumberOfPoints()
544 ptXyz = [0.0, 0.0, 0.0]
545 for ndx in range(0,pointsArray.GetNumberOfPoints()):
546 #thePoint = pointsArray.GetPoint(ndx, ptXyz)
547 pointsArray.GetPoint(ndx, ptXyz)
548 if globalNodeIdArray == None:
549 inParameters.mGlobalNodeIdList.InsertNextValue(ndx+1)
550 else:
551 inParameters.mGlobalNodeIdList.InsertNextValue(globalNodeIdArray.GetValue(ndx))
552 inParameters.mPointXYZList.InsertNextValue(ptXyz[0])
553 inParameters.mPointXYZList.InsertNextValue(ptXyz[1])
554 inParameters.mPointXYZList.InsertNextValue(ptXyz[2])
555 #if PhactoriDbg(100):
556 # myDebugPrint3("MakeListOfAllPointsInBlock1 returning\n")
557
558 class MakeListOfAllPointsAndNodeIdsInBlock2Params:
559 def __init__(self):
560 self.mPointXYZAndNodeIdList = []
561
562 def MakeListOfAllPointsAndNodeIdsInBlock2(self, inInputCsData, inParameters):
563 if PhactoriDbg(100):
564 myDebugPrint3("MakeListOfAllPointsAndNodeIdsInBlock2 entered\n")
565 pointsArray = inInputCsData.GetPoints()
566 if pointsArray == None:
567 #no points here
568 return
569 pointsData = inInputCsData.GetPointData()
570 globalNodeIdArray = pointsData.GetArray('GlobalNodeId')
571 numPoints = pointsArray.GetNumberOfPoints()
572 ptXyz = [0.0, 0.0, 0.0]
573 for ndx in range(0,pointsArray.GetNumberOfPoints()):
574 #thePoint = pointsArray.GetPoint(ndx, ptXyz)
575 pointsArray.GetPoint(ndx, ptXyz)
576 if globalNodeIdArray == None:
577 thisNodeId = -1
578 else:
579 thisNodeId = int(globalNodeIdArray.GetValue(ndx))
580 inParameters.mPointXYZAndNodeIdList.append([ptXyz[0], ptXyz[1], ptXyz[2], thisNodeId])
581 if PhactoriDbg(100):
582 numPtsThisBlock = len(inParameters.mPointXYZAndNodeIdList)
583 myDebugPrint3("num points after this block: " + str(numPtsThisBlock) + "\n")
584 if numPtsThisBlock > 0:
585 myDebugPrint3("last point: " + str(inParameters.mPointXYZAndNodeIdList[-1]) + "\n")
586 if PhactoriDbg(100):
587 myDebugPrint3("MakeListOfAllPointsAndNodeIdsInBlock2 returning\n")
588
589 def MakeListOfAllPointsAndNodeIdsOnThisProcessFromParaViewFilter(self, inInputFilter):
590 if PhactoriDbg(100):
591 myDebugPrint3("MakeListOfAllPointsAndNodeIdsOnThisProcessFromParaViewFilter entered\n")
592 recursionItem = BlockRecursionControlItem()
593 recursionItem.mParameters = PhactoriOperationBlock.MakeListOfAllPointsAndNodeIdsInBlock2Params()
594 recursionItem.mOperationToDoPerBlock = self.MakeListOfAllPointsAndNodeIdsInBlock2
595 self.DoMethodPerBlockFromParaViewFilter(recursionItem, inInputFilter)
596 if PhactoriDbg(100):
597 numPtsThisProcess = len(recursionItem.mParameters.mPointXYZAndNodeIdList)
598 myDebugPrint3("num points in this process: " + str(numPtsThisProcess) + "\n")
599 if numPtsThisProcess > 0:
600 myDebugPrint3("last point: " + str(recursionItem.mParameters.mPointXYZAndNodeIdList[-1]) + "\n")
601 if PhactoriDbg(100):
602 myDebugPrint3("MakeListOfAllPointsAndNodeIdsOnThisProcessFromParaViewFilter returning\n")
603 return recursionItem.mParameters.mPointXYZAndNodeIdList
604
605 class GetQuadClosestToPointsInBlock2Params:
606 def __init__(self):
607 self.testPointList = []
608 self.distSqrdList = []
609 self.closestList = []
610
611 def InitializeWithPointList(self, inTestPointList):
612 self.testPointList = inTestPointList
613 numTestPoints = len(inTestPointList)
614 for ii in range(0, numTestPoints):
615 self.distSqrdList.append(sys.float_info.max)
616 self.closestList.append(None)
617 if PhactoriDbg(100):
618 myDebugPrint3("InitializeWithPointList: " + str(numTestPoints) + " " +\
619 str(len(self.distSqrdList)) + " " + str(len(self.closestList)) +\
620 "\n" + str(self.testPointList) + "\n" +\
621 "\n" + str(self.distSqrdList) + "\n" +\
622 "\n" + str(self.closestList) + "\n")
623
624 def GetQuadsClosestToPointsInBlock2(self, inInputCsData, inParameters):
625 if PhactoriDbg(100):
626 myDebugPrint3("GetCloseQuadsInBlock2 entered\n")
627 numQuads = inInputCsData.GetNumberOfCells()
628 if numQuads == 0:
629 #no quads here
630 return
631
632 if PhactoriDbg(100):
633 myDebugPrint3(str(inParameters.testPointList) + "\n")
634 myDebugPrint3(str(inParameters.distSqrdList) + "\n")
635
636 cellsData = inInputCsData.GetCellData()
637 globalCellIdArray = cellsData.GetArray('GlobalCellId')
638 for quadIndex in range(0,numQuads):
639 thisQuad = QuadInfoGQC()
640 thisQuad.PopulateFromVtkData(inInputCsData, globalCellIdArray, quadIndex)
641 for ptndx, oneTestPt in enumerate(inParameters.testPointList):
642 testDist = thisQuad.CalculateDistanceSquaredTo(oneTestPt)
643 if testDist < inParameters.distSqrdList[ptndx]:
644 inParameters.closestList[ptndx] = thisQuad
645 inParameters.distSqrdList[ptndx] = testDist
646
647 if PhactoriDbg(100):
648 myDebugPrint3(str(inParameters.testPointList) + "\n")
649 myDebugPrint3(str(inParameters.distSqrdList) + "\n")
650
651 if PhactoriDbg(100):
652 myDebugPrint3("after this block:\n")
653 for ii, oneQuad in enumerate(inParameters.closestList):
654 myDebugPrint3(str(ii) + ": " + \
655 str(inParameters.distSqrdList[ii]) + "\n" + \
656 str(inParameters.testPointList[ii]) + "\n" + oneQuad.ToString())
657 myDebugPrint3("\n")
658
659 if PhactoriDbg(100):
660 myDebugPrint3("GetCloseQuadsInBlock2 returning\n")
661
662 def GetQuadsClosestToPointsOnThisProcessFromParaViewFilter(self, inInputFilter, inTestPointList):
663 if PhactoriDbg(100):
664 myDebugPrint3("GetQuadsClosestToPointsOnThisProcessFromParaViewFilter entered\n")
665 recursionItem = BlockRecursionControlItem()
666 recursionItem.mParameters = PhactoriOperationBlock.GetQuadClosestToPointsInBlock2Params()
667 recursionItem.mParameters.InitializeWithPointList(inTestPointList)
668 recursionItem.mOperationToDoPerBlock = self.GetQuadsClosestToPointsInBlock2
669 self.DoMethodPerBlockFromParaViewFilter(recursionItem, inInputFilter)
670 if PhactoriDbg(100):
671 myDebugPrint3("GetQuadsClosestToPointsOnThisProcessFromParaViewFilter returning\n")
672 return recursionItem.mParameters.closestList, recursionItem.mParameters.distSqrdList
673
674 class OutputElementListToFileParams:
675 def __init__(self):
676 self.mOutFileff = None
677 self.mBlockCount = 0
678 self.mElementCount = 0
679 self.mNodeCount = 0
680 self.mFlag1 = 0
681 self.mKilledByCriteriaCount = [0,0,0,0,0,0,0,0,0,0]
682
683 def OutputElementListToFile(self, inFileNameToWrite):
684 recursionItem = BlockRecursionControlItem()
685
686 recursionItem.mParameters = \
687 PhactoriOperationBlock.OutputElementListToFileParams()
688
689 if WriteEachDeadCellElementToFiles:
690 recursionItem.mParameters.mOutFileff = open(inFileNameToWrite, "w+b")
691 recursionItem.mOperationToDoPerBlock = \
692 self.OutputElementListFromOneBlockToFile
693 self.DoMethodPerBlock(recursionItem)
694 if WriteEachDeadCellElementToFiles:
695 recursionItem.mParameters.mOutFileff.close()
696 return recursionItem.mParameters
697
698 def OutputSingleElementFromBlockToTimeHistoryFile(
699 self, inInputCsData, inParameters):
700 if PhactoriDbg(100):
701 myDebugPrint3("OutputSingleElementFromBlockToTimeHistoryFile entered\n")
702 cellData = inInputCsData.GetCellData()
703 numCellTuplesX = cellData.GetNumberOfTuples()
704 numCellArrays = cellData.GetNumberOfArrays()
705 pointData = inInputCsData.GetPointData()
706 numPointTuplesX = cellData.GetNumberOfTuples()
707 numPointArrays = pointData.GetNumberOfArrays()
708
709 #if inParameters.mBlockCount == 0:
710 if (inParameters.mFlag1 == 0) and (numCellTuplesX > 0):
711 inParameters.mFlag1 = 1
712 #as constructed, we should only open once, so we can do 'w' not 'a+'
713 inParameters.mOutElementFileff = open(inParameters.mOutElementFileName, "w")
714 inParameters.mOutElementFileff.write("step, simtime")
715 #inParameters.mOutElementFileff.write(", element index")
716 for arrayIndex in range(0, numCellArrays):
717 inParameters.mOutElementFileff.write(
718 ", " + str(cellData.GetArray(arrayIndex).GetName()))
719 inParameters.mOutElementFileff.write("\n")
720
721 inParameters.mOutNodeFileff = open(inParameters.mOutNodeFileName, "w")
722 inParameters.mOutNodeFileff.write("step, simtime")
723 for arrayIndex in range(0, numPointArrays):
724 oneArray = pointData.GetArray(arrayIndex)
725 numComp = oneArray.GetNumberOfComponents()
726 oneArrayName = str(oneArray.GetName())
727 if numComp == 1:
728 inParameters.mOutNodeFileff.write(", " + oneArrayName)
729 elif(numComp == 3):
730 inParameters.mOutNodeFileff.write(
731 ", " + oneArrayName + "x" + \
732 ", " + oneArrayName + "y" + \
733 ", " + oneArrayName + "z")
734 else:
735 myDebugPrint3AndException(
736 "OutputSingleElementFromBlockToTimeHistoryFile:\n" \
737 "expecting 1 or 3 components (A)\n")
738 inParameters.mOutNodeFileff.write("\n")
739
740 #for now, only dump 1 element
741 if numCellTuplesX > 0:
742 numCellsToDo = 1
743 else:
744 numCellsToDo = 0
745 for cellIndex in range(0, numCellsToDo):
746 inParameters.mOutElementFileff.write(str(inParameters.mTimeStep) + ", " + \
747 str(inParameters.mSimulationTime))
748 #inParameters.mOutElementFileff.write(", " + \
749 # str(cellIndex + inParameters.mElementCount))
750 for arrayIndex in range(0, numCellArrays):
751 inParameters.mOutElementFileff.write(
752 ", " + str(cellData.GetArray(arrayIndex).GetTuple1(cellIndex)))
753 inParameters.mOutElementFileff.write("\n")
754
755 #for now, only dump 1 node
756 if numPointTuplesX > 0:
757 numPointsToDo = 1
758 else:
759 numPointsToDo = 0
760 for pointIndex in range(0, numPointsToDo):
761 inParameters.mOutNodeFileff.write(str(inParameters.mTimeStep) + ", " + \
762 str(inParameters.mSimulationTime))
763 #inParameters.mOutNodeFileff.write(", " + \
764 # str(pointIndex + inParameters.mCellCount))
765 for arrayIndex in range(0, numPointArrays):
766 #inParameters.mOutNodeFileff.write(
767 # ", " + str(pointData.GetArray(arrayIndex).GetTuple1(0)))
768 oneArray = pointData.GetArray(arrayIndex)
769 numComp = oneArray.GetNumberOfComponents()
770 if(numComp == 1):
771 inParameters.mOutNodeFileff.write(
772 ", " + str(pointData.GetArray(arrayIndex).GetTuple1(pointIndex)))
773 elif(numComp == 3):
774 theTuple = pointData.GetArray(arrayIndex).GetTuple3(0)
775 inParameters.mOutNodeFileff.write( \
776 ", " + str(theTuple[0]) + \
777 ", " + str(theTuple[1]) + \
778 ", " + str(theTuple[2]))
779 else:
780 myDebugPrint3AndException(
781 "OutputSingleElementFromBlockToTimeHistoryFile:\n" \
782 "expecting 1 or 3 components (B)\n")
783 inParameters.mOutNodeFileff.write("\n")
784
785 inParameters.mNodeCount += inInputCsData.GetNumberOfPoints()
786 inParameters.mElementCount += inInputCsData.GetNumberOfCells()
787 inParameters.mBlockCount += 1
788
789 if PhactoriDbg(100):
790 numCells = inInputCsData.GetNumberOfCells()
791 if numCells > 0:
792 numPoints = inInputCsData.GetNumberOfPoints()
793 myDebugPrint3(
794 "numCells: " + str(numCells) + "\n" \
795 "numCellTuplesX: " + str(numCellTuplesX) + "\n" \
796 "numPoints: " + str(numPoints) + "\n" \
797 "numPointTuplesX: " + str(numPointTuplesX) + "\n")
798
799 if(inParameters.mOutElementFileff != None):
800 inParameters.mOutElementFileff.flush()
801 inParameters.mOutNodeFileff.flush()
802
803 #dump 1 node
804
805 if PhactoriDbg(100):
806 myDebugPrint3("OutputSingleElementFromBlockToTimeHistoryFile returning\n")
807
808 class OutputSingleElementToTimeHistoryFileParams:
809 def __init__(self):
810 self.mOutElementFileff = None
811 self.mOutElementFileName = None
812 self.mOutNodeFileff = None
813 self.mOutNodeFileName = None
814 self.mTimeStep = 0
815 self.mSimulationTime = 0.0
816 self.mBlockCount = 0
817 self.mElementCount = 0
818 self.mNodeCount = 0
819 self.mFlag1 = 0
820
821 def ResetBlockNodeElementCount(self):
822 self.mBlockCount = 0
823 self.mElementCount = 0
824 self.mNodeCount = 0
825
826 def OutputSingleElementToTimeHistoryFile(self,
827 inElementFileNameToWrite, inNodeFileNameToWrite,
828 inTimeStep, inSimulationTime):
829 recursionItem = BlockRecursionControlItem()
830
831 if inElementFileNameToWrite in self.mRecursionParameterStore:
832 recursionItem.mParameters = \
833 self.mRecursionParameterStore[inElementFileNameToWrite]
834 recursionItem.mParameters.ResetBlockNodeElementCount()
835 else:
836 recursionItem.mParameters = \
837 PhactoriOperationBlock.OutputSingleElementToTimeHistoryFileParams()
838 self.mRecursionParameterStore[inElementFileNameToWrite] = \
839 recursionItem.mParameters
840
841 #recursionItem.mParameters.mOutFileff = open(inFileNameToWrite, "w+b")
842 recursionItem.mParameters.mOutElementFileName = inElementFileNameToWrite
843 recursionItem.mParameters.mOutNodeFileName = inNodeFileNameToWrite
844 recursionItem.mParameters.mTimeStep = inTimeStep
845 recursionItem.mParameters.mSimulationTime = inSimulationTime
846 recursionItem.mOperationToDoPerBlock = \
847 self.OutputSingleElementFromBlockToTimeHistoryFile
848 self.DoMethodPerBlock(recursionItem)
849 #recursionItem.mParameters.mOutFileff.close()
850
851 def ExportOperationData(self, datadescription):
852 """this will be called once per callback (before WriteImages) to allow the
853 operation to export any desired data which is not an image. We call
854 the operation specifics version of this method."""
855 self.mOperationSpecifics.ExportOperationData(datadescription)
856
857 #phactori_combine_to_single_python_file_subpiece_end_1