Debian Codegraph @

packages/seacas/libraries/ioss/src/visualization/catalyst/phactori/Operation/PhactoriSegmentCellSampler3.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 .PhactoriOperationBlock import *
35 from .PhactoriVectorLibrary import *
36 from paraview.simple import *
37 from .PhactoriSegment import *
38 from .PhactoriMpiUtilities import *
39 from .PhactoriParaviewMultiBlockRecursion import *
40 from .PhactoriSampledCellInfo import *
41 import vtk
42 import json
43
44 #phactori_combine_to_single_python_file_subpiece_begin_1
45
46 class GetCellsClosestToPointsInBlockRecursionParams:
47 def __init__(self):
48 self.testPointList = []
49 self.distSqrdList = []
50 self.closestList = []
51
52 def InitializeWithPointList(self, inTestPointList):
53 self.testPointList = inTestPointList
54 numTestPoints = len(inTestPointList)
55 for ii in range(0, numTestPoints):
56 self.distSqrdList.append(sys.float_info.max)
57 self.closestList.append(None)
58
59 class SetMaskValueForCellsNearSegmentsRecursionParams:
60 def __init__(self):
61 self.segmentList = None
62 self.maskCellVariableName = None
63 self.globalCellIdName = None
64 self.CellDataArrayName = "V"
65 self.markedCellSet = None
66 self.maskTestDistanceSquared = None
67 self.projectionAxis = -1
68 self.leafVisitCount = 0
69
70 def Initialize(self, segmentList, cellDataArrayName, maskCellVariableName, globalCellIdName, maskTestDistanceSquared, projectionAxis):
71 self.CellDataArrayName = cellDataArrayName
72 self.maskCellVariableName = maskCellVariableName
73 self.globalCellIdName = globalCellIdName
74 self.segmentList = segmentList
75 self.maskTestDistanceSquared = maskTestDistanceSquared
76 self.markedCellSet = []
77 self.projectionAxis = projectionAxis
78 self.leafVisitCount = 0
79 if PhactoriDbg(100):
80 myDebugPrint3("SetMaskValueForCellsNearSegmentsRecursionParams::Initialize\n" +\
81 "self.segmentList:\n" + str(self.segmentList) + "\n")
82
83 for oneSegment in self.segmentList:
84 cellInfoListForThisSegment = []
85 self.markedCellSet.append(cellInfoListForThisSegment)
86
87 class CellsListClosestToPointsListInBlockStructuredRecursionParams:
88 def __init__(self):
89 self.testPointList = []
90 self.distSqrdList = []
91 self.closestCellList = []
92 self.leafVisitCount = 0
93
94 def InitializeWithPointList(self, inTestPointList):
95 self.testPointList = inTestPointList
96 numTestPoints = len(inTestPointList)
97 self.leafVisitCount = 0
98 for ii in range(0, numTestPoints):
99 self.distSqrdList.append(sys.float_info.max)
100 newCellInfo = PhactoriSampledCellInfo()
101 self.closestCellList.append(newCellInfo)
102
103 class GatherStructuredCellsFromSeedCellsRecursionParams:
104 def __init__(self):
105 self.leafVisitCount = 0
106 self.dataArrayName = "noname"
107 self.SeedCells = None
108 self.CellsPerSeedCell = None
109
110 def SetUpForRecursion(self, creatingSamplerInstance):
111 self.SeedCells = creatingSamplerInstance.StructuredNearbyCellPointList
112 self.dataArrayName = creatingSamplerInstance.CellDataArrayName
113 self.CellsPerSeedCell = []
114 for ii in range(0, len(self.SeedCells)):
115 self.CellsPerSeedCell.append([])
116
117 class PhactoriSegmentCellSampler3(PhactoriOperationSpecifics):
118 """Filter/operation which reads in a .json file which is a list of 3d
119 coordinates pairs (representating segments, and then finds all cells
120 in the input data that cross that segment and sets a masking value
121 into a cell data array for those cells"""
122 def __init__(self):
123 self.myCopyOfInputFilter = None
124
125 #[[[x1,y1],[x2.y2]],[[x3,y3],[x4.y4]],...]
126 self.JsonSegmentListFileName = "PhactoriSegmentCellSampler3.json"
127
128 self.SegmentList = None
129 #self.MaskTestDistanceSquared = 0.00001
130 self.MaskTestDistanceSquared = 1.6e-07
131
132 self.SegmentDefinitionFormat = "two geometric points"
133
134 self.PerSegmentMarkedCellInfoList = []
135
136 #indicates whether we should conduct our segment-picking cell calculations
137 #by projecting onto an ordinal plane. -1 is no projection, 0, 1, or 2 are
138 #the x, y, or z axes. This is of particular use if you are doing a slice
139 #before selecting elements. The slice does not need to be axis aligned,
140 #just choose the axis plane closest to parallel to the slice plane.
141 self.ProjectionAxis = -1
142
143 #as of 2019Aug13, we are having the programmable filter core dump with
144 #structured data: this allows us to turn off the programmable filter to
145 #do the other operations
146 self.DoProgrammableFilterToAddMaskVariable = False
147 self.mProgFilterMaskSetter = None
148 self.mProgFilterString = None
149 self.ProgrammableFilterOutputCellVariableName = "mask1"
150
151 self.StructuredNearbyCellPointList = None
152 self.NearbyGeometryPointList = None
153
154 #"structured" or "unstructured"
155 self.collectionMethod = "segments identify cells"
156
157 self.SampledCellsOutputFilename = "PhactoriSegmentCellSampler3_sampled_cells_"
158 self.SampledCellsOutputDirectory = "PhactoriSegmentCellSampler3_Output"
159 self.NumCounterDigits = 5
160 self.SampledCellsOutputFilenameExtension = ".json"
161
162 self.CellDataArrayName = "V"
163 self.CellDataArrayTupleSize = 8
164
165 #controls whether or not we output a single json file with a list of all
166 #the selected cells (using MPI to combine to one process)
167 self.WriteCellListToJsonFile = True
168
169 def ValidateJsonStructuredSeedCellList(self, inJson):
170 numPoints = len(inJson)
171 if numPoints < 1:
172 myDebugPrint3AndException(
173 "PhactoriSegmentCellSampler3::ValidateJsonStructuredSeedCellList\n"
174 "list must have at least one seed cell\n")
175
176 for ptNdx, onePtJson in enumerate(inJson):
177 key1 = "geometric seed point"
178 if key1 not in onePtJson:
179 myDebugPrint3AndException("point " + str(ptNdx) + " missing '" + key1 + "'\n" + str(inJson) + "\n")
180 geompt = onePtJson[key1]
181 if(len(geompt) != 3):
182 myDebugPrint3AndException("point " + str(ptNdx) + " bad json format for xyz point:\n" + str(inJson) + "\n")
183 key2 = "collection axis"
184 if key2 in onePtJson:
185 axisVal = onePtJson[key2]
186 if (axisVal != "i") and (axisVal != "j") and (axisVal != "k") and \
187 (axisVal != "ij") and (axisVal != "ik") and (axisVal != "jk"):
188 myDebugPrint3AndException(
189 "point " + str(ptNdx) + " bad json format (2):\n"\
190 "needs 'i', 'j', 'k', 'ij', 'ik', or 'jk':\n" + str(inJson) + "\n")
191
192 return True
193
194
195 def ValidateJsonSegmentList2(self, testlist):
196 numSegments = len(testlist)
197 if numSegments < 1:
198 myDebugPrint3AndException(
199 "PhactoriSegmentCellSampler3::ValidateJsonSegmentList2\n"
200 "list must have at least one segment\n")
201
202 for segNdx in range(0,numSegments):
203 oneSegment = testlist[segNdx]
204 if len(oneSegment) != 2:
205 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList2\n" \
206 "segment with index " + str(segNdx) + " does not have a point and a length\n"
207 myDebugPrint3AndException(errStr)
208 pt1 = oneSegment[0]
209 if len(pt1) != 3:
210 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList2\n" \
211 "segment with index " + str(segNdx) + " point does not have 3 components\n"
212 myDebugPrint3AndException(errStr)
213
214 return True
215
216 def ValidateJsonSegmentList3(self, testlist):
217 numSegments = len(testlist)
218 if numSegments < 1:
219 myDebugPrint3AndException(
220 "PhactoriSegmentCellSampler3::ValidateJsonSegmentList3\n"
221 "list must have at least one segment\n")
222
223 for segNdx in range(0,numSegments):
224 oneSegment = testlist[segNdx]
225 if len(oneSegment) != 3:
226 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList3\n" \
227 "segment with index " + str(segNdx) + " does not have a point, direction, and length\n"
228 myDebugPrint3AndException(errStr)
229 pt1 = oneSegment[0]
230 dir1 = oneSegment[1]
231 length1 = oneSegment[2]
232 if len(pt1) != 3:
233 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList3\n" \
234 "segment with index " + str(segNdx) + " point does not have 3 components\n"
235 myDebugPrint3AndException(errStr)
236 if len(dir1) != 3:
237 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList3\n" \
238 "segment with index " + str(segNdx) + " direction does not have 3 components\n"
239 myDebugPrint3AndException(errStr)
240
241 return True
242
243 def ValidateJsonSegmentList(self, testlist):
244 numSegments = len(testlist)
245 if numSegments < 1:
246 myDebugPrint3AndException(
247 "PhactoriSegmentCellSampler3::ValidateJsonSegmentList\n"
248 "list must have at least one segment\n")
249
250 for segNdx in range(0,numSegments):
251 oneSegment = testlist[segNdx]
252 if len(oneSegment) != 2:
253 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList\n" \
254 "segment with index " + str(segNdx) + " does not have two points\n"
255 myDebugPrint3AndException(errStr)
256 pt1 = oneSegment[0]
257 if len(pt1) != 3:
258 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList\n" \
259 "segment with index " + str(segNdx) + " point 1 does not have 3 components\n"
260 myDebugPrint3AndException(errStr)
261 pt2 = oneSegment[1]
262 if len(pt1) != 3:
263 errStr = "PhactoriSegmentCellSampler3:ValidateJsonSegmentList\n" \
264 "segment with index " + str(segNdx) + " point 2 does not have 3 components\n"
265 myDebugPrint3AndException(errStr)
266
267 return True
268
269 def ParseParametersFromJson(self, inJson):
270 keyval6 = "collection method"
271 if keyval6 in inJson:
272 self.collectionMethod = inJson[keyval6]
273 if (self.collectionMethod != "segments identify cells") and \
274 (self.collectionMethod != "seed cell with structured grid"):
275 myDebugPrint3AndException("'collection method' must be one of:\n" \
276 "'segments identify cells' (default)\n" \
277 "'seed cell with structured grid'\n")
278
279 if "filename" in inJson:
280 self.JsonListFileName = inJson['filename']
281 else:
282 myDebugPrint3AndException(
283 "PhactoriSegmentCellSampler3:ParseParametersFromJson\n"
284 "Error: must have 'filename' key\n")
285
286 keyval2 = "cell center to segment test distance"
287 if keyval2 not in inJson:
288 myDebugPrint3AndException(
289 "PhactoriSegmentCellSampler3:ParseParametersFromJson\n"
290 "Error: must have '" + keyval2 + "' key\n")
291 testDist = inJson[keyval2]
292 self.MaskTestDistanceSquared = testDist * testDist
293
294 if self.collectionMethod == "seed cell with structured grid":
295 keyval3 = "segment definition format"
296 if keyval3 in inJson:
297 myDebugPrint3AndException("with collection method '" + str(self.collectionMethod) + "'\n" \
298 "you cannot have 'segment definition format' key\n")
299 else:
300 keyval3 = "segment definition format"
301 if keyval3 in inJson:
302 self.SegmentDefinitionFormat = inJson[keyval3]
303 if (self.SegmentDefinitionFormat != "two geometric points") and \
304 (self.SegmentDefinitionFormat != "geometric point and nearest cell") and \
305 (self.SegmentDefinitionFormat != "structured cell geometric point and length") and \
306 (self.SegmentDefinitionFormat != "geometric point and nearest cell and direction"):
307 myDebugPrint3AndException("PhactoriSegmentCellSampler3::ParseParametersFromJson\n"
308 "bad " + keyval3 + "\n")
309
310 keyval4 = "projection axis"
311 if keyval4 in inJson:
312 val4 = inJson[keyval4]
313 if val4 == "none":
314 self.ProjectionAxis = -1
315 elif val4 == "x":
316 self.ProjectionAxis = 0
317 elif val4 == "y":
318 self.ProjectionAxis = 1
319 elif val4 == "z":
320 self.ProjectionAxis = 2
321 elif int(val4) == 1:
322 self.ProjectionAxis = 0
323 elif int(val4) == 2:
324 self.ProjectionAxis = 1
325 elif int(val4) == 3:
326 self.ProjectionAxis = 2
327
328 keyval5 = "do programmable filter"
329 if keyval5 in inJson:
330 self.DoProgrammableFilterToAddMaskVariable = inJson[keyval5]
331
332 keyval7 = "sampled cells output filename"
333 if keyval7 in inJson:
334 self.SampledCellsOutputFilename = inJson[keyval7]
335
336 keyval7b = "sampled cells output directory"
337 if keyval7b in inJson:
338 self.SampledCellsOutputDirectory = inJson[keyval7b]
339 if self.SampledCellsOutputDirectory != ".":
340 CreateDirectoryFromProcessZero(self.SampledCellsOutputDirectory)
341
342 keyval7c = "sampled cells output filename digit count"
343 if keyval7c in inJson:
344 self.NumCounterDigits = inJson[keyval7c]
345
346 keyval7d = "sampled cells output filename extension"
347 if keyval7d in inJson:
348 self.SampledCellsOutputFilenameExtension = inJson[keyval7d]
349
350 keyval8 = "cell data array name"
351 if keyval8 in inJson:
352 self.CellDataArrayName = inJson[keyval8]
353
354 keyval9 = "cell data array tuple size"
355 if keyval9 in inJson:
356 self.CellDataArrayTupleSize = inJson[keyval9]
357
358 keyval10 = "programmable filter output cell variable name"
359 if keyval10 in inJson:
360 self.ProgrammableFilterOutputCellVariableName = inJson[keyval10]
361
362 keyval11 = "write cell list to json file"
363 if keyval11 in inJson:
364 self.WriteCellListToJsonFile = inJson[keyval11]
365
366 def SetStructuredNearbyCellPointList(self, nearbyGeometryPoints, structuredNearbyCellPointList):
367 self.NearbyGeometryPointList = nearbyGeometryPoints
368 self.StructuredNearbyCellPointList = structuredNearbyCellPointList
369 #self.SegmentList = []
370 if PhactoriDbg(100):
371 myDebugPrint3("(structured) format: " + str(self.SegmentDefinitionFormat) + "\n")
372 for ii in range(0,len(self.StructuredNearbyCellPointList)):
373 seedPoint = nearbyGeometryPoints[ii]
374 segmentPointA = self.StructuredNearbyCellPointList[ii].cellTestPoint
375 SeedToCell = vecFromAToB(seedPoint, segmentPointA)
376 #SeedToCellNorm = vecNormalize(SeedToCell)
377 SeedToCellNorm = vecNormalizeWithSmallCheck(SeedToCell, 0.0, [0.0, 1.0, 0.0])
378 #segmentPointB = vecMultiplyAdd(segmentPointA, SeedToCellNorm, segmentLengths[ii])
379 segmentPointB = vecMultiplyAdd(segmentPointA, SeedToCellNorm, 1.0)
380 if PhactoriDbg(100):
381 myDebugPrint3("segment " + str(ii) + "\n", 100)
382 myDebugPrint3("seedPoint: " + str(seedPoint) + "\n")
383 myDebugPrint3("closest cell point: " + str(segmentPointA) + "\n")
384 myDebugPrint3("SeedToCellNorm: " + str(SeedToCellNorm) + "\n")
385 myDebugPrint3("segmentPointB: " + str(segmentPointB) + "\n")
386 newSegment = PhactoriSegment()
387 newSegment.SetPoints(segmentPointA, segmentPointB)
388 #self.SegmentList.append(newSegment)
389
390 def CreateInternalStructuredSeedCellListFromJson(self, inJson):
391 self.ValidateJsonStructuredSeedCellList(inJson)
392 nearbyGeometryPoints = []
393 collectionAxes = []
394 for oneSeedCellJson in inJson:
395 geometryPoint = oneSeedCellJson["geometric seed point"]
396 nearbyGeometryPoints.append(list(geometryPoint))
397 if "collection axis" in oneSeedCellJson:
398 oneAxis = oneSeedCellJson["collection axis"]
399 if oneAxis == "i":
400 axisInt = 0
401 elif oneAxis == "j":
402 axisInt = 1
403 elif oneAxis == "k":
404 axisInt = 2
405 elif oneAxis == "ij":
406 axisInt = 3
407 elif oneAxis == "ik":
408 axisInt = 4
409 elif oneAxis == "jk":
410 axisInt = 5
411 else:
412 axisInt = 2
413 collectionAxes.append(axisInt)
414 structuredNearbyCellPointList = self.GetListOfCellTestPointsNearestListOfPointsStructured(nearbyGeometryPoints)
415 for ii, oneCell in enumerate(structuredNearbyCellPointList):
416 oneCell.SetCollectionAxis(collectionAxes[ii])
417 self.SetStructuredNearbyCellPointList(nearbyGeometryPoints, structuredNearbyCellPointList)
418
419 def CreateInternalSegmentListFromJson(self, segmentListJson):
420 if PhactoriDbg(100):
421 myDebugPrint3("PhactoriSegmentCellSampler3.ExportOperationData entered\n", 100)
422
423 if self.SegmentDefinitionFormat == "two geometric points":
424 self.ValidateJsonSegmentList(segmentListJson)
425 self.SegmentList = []
426 self.NearbyGeometryPointList = []
427 if PhactoriDbg(100):
428 myDebugPrint3("format: " + str(self.SegmentDefinitionFormat) + "\n")
429 for item in segmentListJson:
430 newSegment = PhactoriSegment()
431 newSegment.SetPoints(item[0], item[1])
432 self.SegmentList.append(newSegment)
433 self.NearbyGeometryPointList.append(item[0])
434 elif self.SegmentDefinitionFormat == "geometric point and nearest cell":
435 self.ValidateJsonSegmentList2(segmentListJson)
436 nearbyGeometryPoints = []
437 segmentLengths = []
438 for item in segmentListJson:
439 nearbyGeometryPoints.append(item[0])
440 segmentLengths.append(item[1])
441 self.NearbyGeometryPointList = nearbyGeometryPoints
442
443 nearbyCellPoints = self.GetListOfCellTestPointsNearestListOfPoints(nearbyGeometryPoints)
444 #use some vector math to construct the segments from the seed points
445 #and the nearest cell to each seed point
446 self.SegmentList = []
447 if PhactoriDbg(100):
448 myDebugPrint3("(unstructured) format: " + str(self.SegmentDefinitionFormat) + "\n")
449 for ii in range(0,len(nearbyGeometryPoints)):
450 seedPoint = nearbyGeometryPoints[ii]
451 segmentPointA = nearbyCellPoints[ii]
452 SeedToCell = vecFromAToB(seedPoint, segmentPointA)
453 SeedToCellNorm = vecNormalize(SeedToCell)
454 segmentPointB = vecMultiplyAdd(segmentPointA, SeedToCellNorm, segmentLengths[ii])
455 if PhactoriDbg(100):
456 myDebugPrint3("segment " + str(ii) + "\n", 100)
457 myDebugPrint3("seedPoint: " + str(seedPoint) + "\n")
458 myDebugPrint3("closest cell point: " + str(segmentPointA) + "\n")
459 myDebugPrint3("SeedToCellNorm: " + str(SeedToCellNorm) + "\n")
460 myDebugPrint3("segmentPointB: " + str(segmentPointB) + "\n")
461 newSegment = PhactoriSegment()
462 newSegment.SetPoints(segmentPointA, segmentPointB)
463 self.SegmentList.append(newSegment)
464 elif self.SegmentDefinitionFormat == "geometric point and nearest cell and direction":
465 self.ValidateJsonSegmentList3(segmentListJson)
466 nearbyGeometryPoints = []
467 segmentLengths = []
468 directionVecs = []
469 for item in segmentListJson:
470 nearbyGeometryPoints.append(item[0])
471 directionVecs.append(item[1])
472 segmentLengths.append(item[2])
473 self.NearbyGeometryPointList = nearbyGeometryPoints
474
475 nearbyCellPoints = self.GetListOfCellTestPointsNearestListOfPoints(nearbyGeometryPoints)
476 self.SegmentList = []
477 if PhactoriDbg(100):
478 myDebugPrint3("format: " + str(self.SegmentDefinitionFormat) + "\n")
479 for ii in range(0,len(nearbyGeometryPoints)):
480 segmentPointA = nearbyCellPoints[ii]
481 directionVecNorm = vecNormalize(directionVecs[ii])
482 segmentPointB = vecMultiplyAdd(segmentPointA, directionVecNorm, segmentLengths[ii])
483 if PhactoriDbg(100):
484 myDebugPrint3("seedPoint: " + str(nearbyGeometryPoints[ii]) + "\n")
485 myDebugPrint3("segmentPointA (nearest cell point): " + str(seedPoint) + "\n")
486 myDebugPrint3("directionVecNorm: " + str(directionVecNorm) + "\n")
487 myDebugPrint3("segmentPointB: " + str(segmentPointB) + "\n")
488 newSegment = PhactoriSegment()
489 newSegment.SetPoints(segmentPointA, segmentPointB)
490 self.SegmentList.append(newSegment)
491 else:
492 myDebugPrint3AndException("bad self.SegmentDefinitionFormat\n")
493
494 if PhactoriDbg(100):
495 myDebugPrint3("PhactoriSegmentCellSampler3.ExportOperationData returning\n", 100)
496
497 def CreateParaViewFilter(self, inInputFilter):
498 if PhactoriDbg(100):
499 myDebugPrint3("PhactoriSegmentCellSampler3.CreateParaViewFilter entered\n", 100)
500
501 savedActiveSource = GetActiveSource()
502
503 segmentListJson = ReadAndMpiBroadcastJsonFile(self.JsonListFileName)
504
505 self.myCopyOfInputFilter = inInputFilter
506
507 UpdatePipelineWithCurrentTimeArgument(self.myCopyOfInputFilter)
508
509 if self.collectionMethod == "seed cell with structured grid":
510 self.CreateInternalStructuredSeedCellListFromJson(segmentListJson)
511 if PhactoriDbg(100):
512 myDebugPrint3("self.StructuredNearbyCellPointList:\n", 100)
513 for ii, oneCell in enumerate(self.StructuredNearbyCellPointList):
514 outStr = oneCell.ToStr()
515 myDebugPrint3("cell " + str(ii) + ":\n" + outStr + \
516 "from geometry point: " + \
517 str(self.NearbyGeometryPointList[ii]) + "\n")
518 else:
519 self.CreateInternalSegmentListFromJson(segmentListJson)
520 if PhactoriDbg(100):
521 myDebugPrint3("self.SegmentList:\n", 100)
522 for oneSeg in self.SegmentList:
523 myDebugPrint3(str(oneSeg.ptA) + ", " + str(oneSeg.ptB) + "\n")
524
525 if self.collectionMethod == "seed cell with structured grid":
526 perSegmentStructuredCellList = self.GatherStructuredCellsFromSeedCells()
527 self.PerSegmentMarkedCellInfoList = perSegmentStructuredCellList
528 else:
529 perSegmentCellList = self.SetMaskValueForCellsNearSegments(
530 self.myCopyOfInputFilter, self.SegmentList, "blargmask1", "Ids",
531 self.MaskTestDistanceSquared, self.ProjectionAxis)
532 self.PerSegmentMarkedCellInfoList = perSegmentCellList
533 #UpdatePipelineWithCurrentTimeArgument(self.myCopyOfInputFilter)
534 #end new work
535
536 if self.DoProgrammableFilterToAddMaskVariable:
537 if PhactoriDbg(100):
538 myDebugPrint3("cell and point data arrays before filter:\n")
539 RecursivelyPrintPointAndCellArrayInformation(inInputFilter)
540 self.mProgFilterMaskSetter = ProgrammableFilter(
541 Input = self.myCopyOfInputFilter)
542 self.mProgFilterMaskSetter.CopyArrays = 1
543 self.CreateProgrammableFilterString()
544 self.mProgFilterMaskSetter.Script = self.mProgFilterString
545 self.mProgFilterMaskSetter.UpdatePipeline()
546 if PhactoriDbg(100):
547 myDebugPrint3("cell and point data arrays after filter:\n")
548 RecursivelyPrintPointAndCellArrayInformation(self.mProgFilterMaskSetter)
549 else:
550 self.mProgFilterMaskSetter = self.myCopyOfInputFilter
551
552 SetActiveSource(savedActiveSource)
553
554 if PhactoriDbg(100):
555 myDebugPrint3("PhactoriSegmentCellSampler3.CreateParaViewFilter returning\n", 100)
556 return self.mProgFilterMaskSetter
557
558 def GetPidWithLeastValueList(self, inLocalDistSqrdList):
559 myPid = int(SmartGetLocalProcessId())
560 globalDistSqrdList = UseReduceOnFloatList(inLocalDistSqrdList, 1)
561 localPidList = []
562 numItems = len(inLocalDistSqrdList)
563 for ndx in range(0,numItems):
564 if globalDistSqrdList[ndx] == inLocalDistSqrdList[ndx]:
565 localPidList.append(myPid)
566 else:
567 localPidList.append(-1)
568
569 pidWithDataList = UseReduceOnIntegerList(localPidList, 0)
570 return pidWithDataList, globalDistSqrdList
571
572 def UseMpiToGetGlobalCellPointsClosestStructured(self, ioCellList, inLocalDistSqrdList):
573 if PhactoriDbg(100):
574 myDebugPrint3("PhactoriSegmentCellSampler3.UseMpiToGetGlobalCellPointsClosestStructured entered\n", 100)
575
576 pidWithDataList, globalDistSqrdList = self.GetPidWithLeastValueList(inLocalDistSqrdList)
577 if PhactoriDbg(100):
578 myDebugPrint3("pidWithDataList:\n" + str(pidWithDataList) + "\nglobalDistSqrdList:\n" + str(globalDistSqrdList) + "\n")
579
580 if PhactoriDbg(100):
581 myDebugPrint3("local nearest cell list (ioCellList) before mpi:\n")
582 for ii, oneCellInfo in enumerate(ioCellList):
583 myDebugPrint3(str(ii) + ": " + \
584 str(inLocalDistSqrdList[ii]) + "\n" + \
585 str(oneCellInfo.cellTestPoint) + "\n" + \
586 str(oneCellInfo.pid) + "\n" + \
587 str(oneCellInfo.leafVisitCount) + "\n" + \
588 str(oneCellInfo.ijk) + "\n")
589
590 #convert cell point list to array of doubles and ints, use mpi reduce to share
591 #the values, then convert back to cell point list
592 serializeFloatArray = []
593 serializeIntArray = []
594 myPid = int(SmartGetLocalProcessId())
595 for ii, oneCell in enumerate(ioCellList):
596 if pidWithDataList[ii] == myPid:
597 oneCell.SerializeAppendToFloatAndIntArray(serializeFloatArray, serializeIntArray)
598 else:
599 oneCell.SerializeAppendToFloatAndIntArrayZeroVersion(serializeFloatArray, serializeIntArray)
600
601 #use mpi reduce to spread array correctly
602 globalSerializeFloatArray = UseReduceOnFloatList(serializeFloatArray, 2)
603 globalSerializeIntArray = UseReduceOnIntegerList(serializeIntArray, 2)
604
605 #now create return global cell point list from arrays
606 for ii, oneCell in enumerate(ioCellList):
607 oneCell.SerializeSetFromFloatAndIntArray(globalSerializeFloatArray, globalSerializeIntArray, ii)
608
609 if PhactoriDbg(100):
610 myDebugPrint3("global nearest cell list (ioCellList) after mpi:\n")
611 for ii, oneCellInfo in enumerate(ioCellList):
612 myDebugPrint3(str(ii) + ": " + \
613 str(globalDistSqrdList[ii]) + "\n" + \
614 str(oneCellInfo.cellTestPoint) + "\n" + \
615 str(oneCellInfo.pid) + "\n" + \
616 str(oneCellInfo.leafVisitCount) + "\n" + \
617 str(oneCellInfo.ijk) + "\n")
618
619 if PhactoriDbg(100):
620 myDebugPrint3("PhactoriSegmentCellSampler3.UseMpiToGetGlobalCellPointsClosestStructured returning\n", 100)
621
622 return ioCellList, globalDistSqrdList
623
624 def UseMpiToGetGlobalCellPointsClosest(self, inLocalCellPointList, inLocalDistSqrdList):
625 if PhactoriDbg(100):
626 myDebugPrint3("PhactoriSegmentCellSampler3.UseMpiToGetGlobalCellPointsClosest entered\n", 100)
627
628 pidWithDataList, globalDistSqrdList = self.GetPidWithLeastValueList(inLocalDistSqrdList)
629 if PhactoriDbg(100):
630 myDebugPrint3("pidWithDataList:\n" + str(pidWithDataList) + "\nglobalDistSqrdList:\n" + str(globalDistSqrdList) + "\n")
631
632 #convert cell point list to array of doubles and ints, use mpi reduce to share
633 #the values, then convert back to cell point list
634 serializeFloatArray = []
635 serializeIntArray = []
636
637 #convert cell point list to array of doubles
638 cellPointFloatArray = []
639 myPid = SmartGetLocalProcessId()
640 for ii, oneCellPoint in enumerate(inLocalCellPointList):
641 if pidWithDataList[ii] == myPid:
642 cellPointFloatArray.append(oneCellPoint[0])
643 cellPointFloatArray.append(oneCellPoint[1])
644 cellPointFloatArray.append(oneCellPoint[2])
645 else:
646 cellPointFloatArray.append(0.0)
647 cellPointFloatArray.append(0.0)
648 cellPointFloatArray.append(0.0)
649
650 #use mpi reduce to spread array correctly
651 globalCellPointFloatArray = UseReduceOnFloatList(cellPointFloatArray, 2)
652
653 #now create return global cell point list from arrays
654 numCells = len(inLocalCellPointList)
655 returnGlobalCellPointList = []
656 for ii in range(0,numCells):
657 myndx = ii*3
658 oneCellPoint = [globalCellPointFloatArray[myndx],
659 globalCellPointFloatArray[myndx+1],
660 globalCellPointFloatArray[myndx+2]]
661 returnGlobalCellPointList.append(oneCellPoint)
662
663 if PhactoriDbg(100):
664 myDebugPrint3("PhactoriSegmentCellSampler3.UseMpiToGetGlobalCellPointsClosest returning\n", 100)
665 return returnGlobalCellPointList, globalDistSqrdList
666
667 @staticmethod
668 def DebugPrintCellPointsFaces(oneCell, cellIndex, inInputCsData):
669 if not PhactoriDbg(100):
670 return
671 myDebugPrint3("cellIndex: " + str(cellIndex) + "\n");
672 cellPointIds = oneCell.GetPointIds()
673 numPoints = cellPointIds.GetNumberOfIds()
674 numFaces = oneCell.GetNumberOfFaces()
675 myDebugPrint3("numFaces: " + str(numFaces) + " numPoints: " + str(numPoints) + " pointIds: " + str(cellPointIds) + "\n")
676 for ii in range(0, numFaces):
677 oneFace = oneCell.GetFace(ii)
678 facePoints = oneFace.GetPointIds()
679 myDebugPrint3("face " + str(ii) + ": " + str(facePoints) + "\n")
680 onePointXyz = [0.0, 0.0, 0.0]
681 for jj in range(0, facePoints.GetNumberOfIds()):
682 onePtId = facePoints.GetId(jj)
683 inInputCsData.GetPoint(onePtId, onePointXyz)
684 myDebugPrint3(" point " + str(jj) + ": id: " + str(onePtId) + " xyz: " + str(onePointXyz) + "\n")
685
686 @staticmethod
687 def SetMaskValueForCellsNearSegmentsInBlock(recursionObject, inInputCsData, inParameters):
688 if PhactoriDbg(100):
689 myDebugPrint3("SetMaskValueForCellsNearSegmentsInBlock entered\n")
690 inParameters.leafVisitCount += 1
691 numCells = inInputCsData.GetNumberOfCells()
692 numPoints = inInputCsData.GetNumberOfPoints()
693 if (numCells != 0) and (numPoints == 0):
694 if PhactoriDbg(100):
695 myDebugPrint3("case where numCells is " + str(numCells) + " and numPoints is 0")
696 myDebugPrint3("number of cell arrays: " + str(inInputCsData.GetCellData().GetNumberOfArrays()) + "\n")
697 myDebugPrint3("number of point arrays: " + str(inInputCsData.GetPointData().GetNumberOfArrays()) + "\n")
698
699 if (numCells == 0) or (numPoints == 0):
700 if PhactoriDbg(100):
701 cellData = inInputCsData.GetCellData()
702 if cellData != None:
703 myDebugPrint3("cellData is None, numCells is 0\n")
704 else:
705 myDebugPrint3("cellData has " + str(cellData.GetNumberOfArrays()) + " arrays, numCells is 0\n")
706 if PhactoriDbg(100):
707 myDebugPrint3("SetMaskValueForCellsNearSegmentsInBlock returning (no cells or no points)\n")
708 return
709
710 cellData = inInputCsData.GetCellData()
711 maskCellData = cellData.GetArray(inParameters.maskCellVariableName)
712 #if maskCellData == None:
713 # maskCellData = cellData.GetArray(inParameters.maskCellVariableName)
714
715 globalCellIdData = cellData.GetArray(inParameters.globalCellIdName)
716 outputCellArray = cellData.GetArray(inParameters.CellDataArrayName)
717
718 if PhactoriDbg(100):
719 myDebugPrint3("numCells \n" + str(numCells) + "\n")
720 myDebugPrint3("inParameters.segmentList:\n" + str(inParameters.segmentList) + "\n")
721 myDebugPrint3("cellData: " + str(cellData) + "\n")
722 myDebugPrint3("maskCellData: " + str(maskCellData) + "\n")
723 myDebugPrint3("globalCellIdData: " + str(globalCellIdData) + "\n")
724 #myDebugPrint3("inInputCsData \n" + str(inInputCsData) + "\n")
725 myDebugPrint3("inInputCsData.GetClassName() \n" + str(inInputCsData.GetClassName()) + "\n")
726 projectionAxis = inParameters.projectionAxis
727 leafVisitCount = inParameters.leafVisitCount
728 myPid = int(SmartGetLocalProcessId())
729 cellBounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
730 for cellIndex in range(0,numCells):
731 oneCell = inInputCsData.GetCell(cellIndex)
732 cellTestPoint = GetCellTestPoint(oneCell)
733 if maskCellData != None:
734 maskCellData.SetValue(cellIndex, 0.0)
735 for segIndex, oneSegment in enumerate(inParameters.segmentList):
736 cellTestPointToSegmentDistanceSquared = oneSegment.FindDistanceSquaredToPointProjected(cellTestPoint, projectionAxis)
737 #if PhactoriDbg(100):
738 # if cellIndex%5000 == 0:
739 # myDebugPrint3(str(cellIndex) + " " + str(cellTestPointToSegmentDistanceSquared) + " " + str(cellTestPoint) + "\n")
740 floatSegIndex = float(segIndex) * 5.0
741 if cellTestPointToSegmentDistanceSquared < inParameters.maskTestDistanceSquared:
742 #nearestPointOnSegment = oneSegment.FindNearestPointOnSegmentToPointProjected(cellTestPoint, projectionAxis)
743 #if IsPointInsideCellBoundingBoxProjected(oneCell, nearestPointOnSegment, projectionAxis):
744 oneCell.GetBounds(cellBounds)
745 if oneSegment.IntersectsBoundingBoxProjected(cellBounds, projectionAxis):
746 PhactoriSegmentCellSampler3.DebugPrintCellPointsFaces(oneCell, cellIndex, inInputCsData)
747 if maskCellData != None:
748 #warning--if element is near two segments, both will be marked,
749 #last will win
750 maskCellData.SetValue(cellIndex, floatSegIndex)
751 if globalCellIdData != None:
752 thisCellId = globalCellIdData.GetValue(cellIndex)
753 else:
754 thisCellId = None
755 if outputCellArray == None:
756 thisCellDataTuple = None
757 else:
758 thisCellDataTuple = outputCellArray.GetTuple(cellIndex)
759 newCellInfo = PhactoriSampledCellInfo()
760 newCellInfo.SetFromList([cellTestPoint, [-1,-1,-1], thisCellDataTuple, myPid, leafVisitCount, cellIndex, segIndex, -1])
761
762 inParameters.markedCellSet[segIndex].append(newCellInfo)
763 if PhactoriDbg(100):
764 myDebugPrint3("new cell close to segment:\n")
765 myDebugPrint3(newCellInfo.ToStr())
766
767
768 if PhactoriDbg(100):
769 for idx, cellsForThisSegment in enumerate(inParameters.markedCellSet):
770 myDebugPrint3("after this block cells for segment (index, count): " + str(idx) + ", " + str(len(cellsForThisSegment)) + "\n")
771 #myDebugPrint3(str(inParameters.markedCellSet[idx]) + "\n")
772 myDebugPrint3("\n")
773
774 if PhactoriDbg(100):
775 myDebugPrint3("SetMaskValueForCellsNearSegmentsInBlock returning\n")
776
777 def SetMaskValueForCellsNearSegments(self,
778 inputFilter, segmentList, maskCellVariableName, globalCellIdName, maskTestDistanceSquared, projectionAxis):
779 if PhactoriDbg(100):
780 myDebugPrint3("SetMaskValueForCellsNearSegments entered\n")
781 numCellArrays = inputFilter.CellData.GetNumberOfArrays()
782 myDebugPrint3("numCellArrays: " + str(numCellArrays) + "\n")
783 for ii in range(0,numCellArrays):
784 myDebugPrint3(str(inputFilter.CellData.GetArray(ii)) + "\n")
785 recursionObj = PhactoriParaviewMultiBlockRecursionControl()
786 recursionObj.mParameters = SetMaskValueForCellsNearSegmentsRecursionParams()
787 recursionObj.mParameters.Initialize(segmentList, self.CellDataArrayName, maskCellVariableName, globalCellIdName, maskTestDistanceSquared, projectionAxis)
788 recursionObj.mOperationToDoPerBlock = self.SetMaskValueForCellsNearSegmentsInBlock
789 PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, inputFilter)
790
791 #if PhactoriDbg(100):
792 # myDebugPrint3("SetMaskValueForCellsNearSegments returning\n")
793 # numCellArrays = inputFilter.CellData.GetNumberOfArrays()
794 # myDebugPrint3("numCellArrays: " + str(numCellArrays) + "\n")
795 # for ii in range(0,numCellArrays):
796 # myDebugPrint3(str(inputFilter.CellData.GetArray(ii)) + "\n")
797 # UpdatePipelineWithCurrentTimeArgument(inputFilter)
798 # numCellArrays = inputFilter.CellData.GetNumberOfArrays()
799 # myDebugPrint3("numCellArrays: " + str(numCellArrays) + "\n")
800 # for ii in range(0,numCellArrays):
801 # myDebugPrint3(str(inputFilter.CellData.GetArray(ii)) + "\n")
802
803 return recursionObj.mParameters.markedCellSet
804
805 @staticmethod
806 def GetCellsClosestToPointsInBlock(recursionObject, inInputCsData, inParameters):
807 if PhactoriDbg(100):
808 myDebugPrint3("GetCellsClosestToPointsInBlock entered\n")
809 numCells = inInputCsData.GetNumberOfCells()
810 numPoints = inInputCsData.GetNumberOfPoints()
811 if (numCells == 0) or (numPoints == 0):
812 #no cells here
813 if PhactoriDbg(100):
814 myDebugPrint3("GetCellsClosestToPointsInBlock returning (no cells or no points)\n")
815 return
816
817 if PhactoriDbg(100):
818 myDebugPrint3(str(inParameters.testPointList) + "\n")
819 myDebugPrint3(str(inParameters.distSqrdList) + "\n")
820
821 for cellIndex in range(0,numCells):
822 oneCell = inInputCsData.GetCell(cellIndex)
823 cellTestPoint = GetCellTestPoint(oneCell)
824 for ptndx, oneTestPt in enumerate(inParameters.testPointList):
825 testDist = vecDistanceSquared(oneTestPt, cellTestPoint)
826 if testDist < inParameters.distSqrdList[ptndx]:
827 inParameters.closestList[ptndx] = cellTestPoint
828 inParameters.distSqrdList[ptndx] = testDist
829
830 if PhactoriDbg(100):
831 myDebugPrint3(str(inParameters.testPointList) + "\n")
832 myDebugPrint3(str(inParameters.distSqrdList) + "\n")
833
834 if PhactoriDbg(100):
835 myDebugPrint3("after this block:\n")
836 for ii, oneCellPoint in enumerate(inParameters.closestList):
837 myDebugPrint3(str(ii) + ": " + \
838 str(inParameters.distSqrdList[ii]) + "\n" + \
839 str(inParameters.testPointList[ii]) + "\n" + str(oneCellPoint))
840 myDebugPrint3("\n")
841
842 if PhactoriDbg(100):
843 myDebugPrint3("GetCellsClosestToPointsInBlock returning\n")
844
845 def GetCellsClosestToPointsOnThisProcessFromParaViewFilter(self, inTestPointList):
846 if PhactoriDbg(100):
847 myDebugPrint3("GetCellsClosestToPointsOnThisProcessFromParaViewFilter entered\n")
848 recursionObj = PhactoriParaviewMultiBlockRecursionControl()
849 recursionObj.mParameters = GetCellsClosestToPointsInBlockRecursionParams()
850 recursionObj.mParameters.InitializeWithPointList(inTestPointList)
851 recursionObj.mOperationToDoPerBlock = self.GetCellsClosestToPointsInBlock
852 PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.myCopyOfInputFilter)
853 if PhactoriDbg(100):
854 myDebugPrint3("GetCellsClosestToPointsOnThisProcessFromParaViewFilter returning\n")
855 return recursionObj.mParameters.closestList, recursionObj.mParameters.distSqrdList
856
857 @staticmethod
858 def GetCellsListClosestToPointsListInBlockStructured(recursionObject, inInputCsData, inParameters):
859 if PhactoriDbg(100):
860 myDebugPrint3("GetCellsListClosestToPointsListInBlockStructured entered\n")
861 numCells = inInputCsData.GetNumberOfCells()
862 numPoints = inInputCsData.GetNumberOfPoints()
863 inParameters.leafVisitCount += 1
864 if (numCells == 0) or (numPoints == 0):
865 #no cells here
866 if PhactoriDbg(100):
867 myDebugPrint3("GetCellsListClosestToPointsListInBlockStructured returning (no cells or no points)\n")
868 return
869
870 mypid = SmartGetLocalProcessId()
871 #myExtent = [0,0,0,0,0,0]
872 myExtent = inInputCsData.GetExtent()
873 if PhactoriDbg(100):
874 myDebugPrint3("myExtent: " + str(myExtent) + "\n")
875 for ii in range(myExtent[0], myExtent[1]):
876 for jj in range(myExtent[2], myExtent[3]):
877 for kk in range(myExtent[4], myExtent[5]):
878 #oneCell = inInputCsData.GetCell(cellIndex)
879 #oneCell = inInputCsData.GetCell(ii, jj, kk)
880 oneCell = localGetCellijk(ii, jj, kk, inInputCsData, myExtent)
881 cellTestPoint = GetCellTestPoint(oneCell)
882 if PhactoriDbg(100):
883 if ((ii==myExtent[0]) and (jj == myExtent[2]) and (kk == myExtent[4])):
884 myDebugPrint3("cellTestPoint " + str([ii,jj,kk]) + " " + str(cellTestPoint) + "\n")
885 oneCellBounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
886 oneCell.GetBounds(oneCellBounds)
887 myDebugPrint3("oneCellBounds " + str([ii,jj,kk]) + " " + str(oneCellBounds) + "\n")
888 for ptndx, oneTestPt in enumerate(inParameters.testPointList):
889 testDist = vecDistanceSquared(oneTestPt, cellTestPoint)
890 if testDist < inParameters.distSqrdList[ptndx]:
891 inParameters.distSqrdList[ptndx] = testDist
892 cellInfo = inParameters.closestCellList[ptndx]
893 cellInfo.pid = mypid
894 cellInfo.SetCellTestPoint(cellTestPoint)
895 cellInfo.leafVisitCount = inParameters.leafVisitCount
896 cellInfo.SetIjk(ii, jj, kk)
897
898 if PhactoriDbg(100):
899 myDebugPrint3(str(inParameters.testPointList) + "\n")
900 myDebugPrint3(str(inParameters.distSqrdList) + "\n")
901
902 if PhactoriDbg(100):
903 myDebugPrint3("after this block:\n")
904 for ii, oneCellInfo in enumerate(inParameters.closestCellList):
905 myDebugPrint3(str(ii) + ": " + \
906 str(inParameters.distSqrdList[ii]) + "\n" + \
907 str(inParameters.testPointList[ii]) + "\n" + \
908 str(oneCellInfo.cellTestPoint) + "\n" + \
909 str(oneCellInfo.pid) + "\n" + \
910 str(oneCellInfo.leafVisitCount) + "\n" + \
911 str(oneCellInfo.ijk) + "\n")
912 myDebugPrint3("\n")
913
914 if PhactoriDbg(100):
915 myDebugPrint3("GetCellsListClosestToPointsListInBlockStructured returning\n")
916
917 def GetListOfCellTestPointsNearestListOfPointsStructured(self, inTestPointList):
918 if PhactoriDbg(100):
919 myDebugPrint3("GetListOfCellTestPointsNearestListOfPointsStructured entered\n")
920 #find the closest structured cells for this process, recursing into
921 #multiblock dataset
922 recursionParams = CellsListClosestToPointsListInBlockStructuredRecursionParams()
923 recursionParams.InitializeWithPointList(inTestPointList)
924 recursionObj = PhactoriParaviewMultiBlockRecursionControl()
925 recursionObj.mParameters = recursionParams
926 recursionObj.mOperationToDoPerBlock = self.GetCellsListClosestToPointsListInBlockStructured
927 PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.myCopyOfInputFilter)
928
929 #use mpi to find closest structured cells across all processes
930 nearestCellList, distanceList = self.UseMpiToGetGlobalCellPointsClosestStructured(
931 recursionObj.mParameters.closestCellList, recursionObj.mParameters.distSqrdList)
932
933 if PhactoriDbg(100):
934 myDebugPrint3("GetListOfCellTestPointsNearestListOfPointsStructured returning\n")
935 return nearestCellList
936
937 @staticmethod
938 def GatherStructuredCellsInBlockFromSeedCells(recursionObject, inInputCsData, inParameters):
939 if PhactoriDbg(100):
940 myDebugPrint3("GatherStructuredCellsInBlockFromSeedCells entered\n")
941
942 numCells = inInputCsData.GetNumberOfCells()
943 numPoints = inInputCsData.GetNumberOfPoints()
944 inParameters.leafVisitCount += 1
945 if (numCells == 0) or (numPoints == 0):
946 #no cells here
947 return
948
949 cellData = inInputCsData.GetCellData()
950 outputCellArray = None
951 if cellData != None:
952 outputCellArray = cellData.GetArray(inParameters.dataArrayName)
953
954 if outputCellArray != None:
955 dataArrayNumCmpnts = outputCellArray.GetNumberOfComponents()
956 defaultTuple = []
957 for ii in range(0, dataArrayNumCmpnts):
958 defaultTuple.append(-1.0)
959 else:
960 dataArrayNumCmpnts = -1
961 defaultTuple = []
962
963 myPid = SmartGetLocalProcessId()
964 myExtent = inInputCsData.GetExtent()
965 myDims = inInputCsData.GetDimensions()
966 if PhactoriDbg(100):
967 myDebugPrint3("myExtent: " + str(myExtent) + \
968 "\nmyDims: " + str(myDims) + \
969 "\nnumCells: " + str(numCells) + "\n")
970 seedCellList = inParameters.SeedCells
971 for seedCellIndex, oneSeedCell in enumerate(seedCellList):
972 if PhactoriDbg(100):
973 myDebugPrint3("oneSeedCell: " + str(seedCellIndex) + " " + str(oneSeedCell.ijk) + " " + str([oneSeedCell.leafVisitCount, inParameters.leafVisitCount]) + " " + str(myExtent) + "\n")
974 #is this seed cell in this block?
975 if oneSeedCell.leafVisitCount != inParameters.leafVisitCount:
976 if PhactoriDbg(100):
977 myDebugPrint3("this block is not for this seed cell: " + \
978 str(oneSeedCell.leafVisitCount) + " " + \
979 str(inParameters.leafVisitCount) + "\n")
980 continue
981 if PhactoriDbg(100):
982 myDebugPrint3("this block is for this seed cell: " + \
983 str(oneSeedCell.leafVisitCount) + " " + \
984 str(inParameters.leafVisitCount) + "\n")
985 #does this process have any data for this seed cell?
986 if oneSeedCell.AxisCrossesExtent(myExtent, oneSeedCell.collectionAxis) == False:
987 if PhactoriDbg(100):
988 myDebugPrint3("this seed cell not in myExtent: " + str(oneSeedCell.ijk) + "\n")
989 continue
990 ijkrange = oneSeedCell.GetIntersectingCollectionExtent(myExtent, oneSeedCell.collectionAxis)
991 if PhactoriDbg(100):
992 myDebugPrint3("this block has cells to collect: " + str(ijkrange) + "\n")
993 #collect all the cells for this seed cell
994 for ii in range(ijkrange[0], ijkrange[1]):
995 for jj in range(ijkrange[2], ijkrange[3]):
996 for kk in range(ijkrange[4], ijkrange[5]):
997 newStructuredCell = PhactoriSampledCellInfo()
998 newStructuredCell.Populate1(ii, jj, kk, myPid,
999 inParameters.leafVisitCount, seedCellIndex, inInputCsData,
1000 myExtent, numCells,
1001 outputCellArray, dataArrayNumCmpnts, defaultTuple)
1002 inParameters.CellsPerSeedCell[seedCellIndex].append(newStructuredCell)
1003
1004 if PhactoriDbg(100):
1005 myDebugPrint3("GatherStructuredCellsInBlockFromSeedCells returning\n")
1006
1007 def GatherStructuredCellsFromSeedCells(self):
1008 """for each structured seed cell in self.StructuredNearbyCellPointList,
1009 gather cells along one u/v/w direction in the structured data (on this
1010 process), including the current data for the cells"""
1011 if PhactoriDbg(100):
1012 myDebugPrint3("GatherStructuredCellsFromSeedCells entered\n")
1013
1014 #find the closest structured cells for this process, recursing into
1015 #multiblock dataset
1016 recursionParams = GatherStructuredCellsFromSeedCellsRecursionParams()
1017 recursionParams.SetUpForRecursion(self)
1018 recursionObj = PhactoriParaviewMultiBlockRecursionControl()
1019 recursionObj.mParameters = recursionParams
1020 recursionObj.mOperationToDoPerBlock = self.GatherStructuredCellsInBlockFromSeedCells
1021 PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.myCopyOfInputFilter)
1022
1023 if PhactoriDbg(100):
1024 myDebugPrint3("GatherStructuredCellsFromSeedCells returning\n")
1025 return recursionParams.CellsPerSeedCell
1026
1027
1028 def GetListOfCellTestPointsNearestListOfPoints(self, pointList):
1029 """for each point in the list, find the cell test point (e.g. center of
1030 cell bounding box) which is nearest the test point. Use MPI to work
1031 in parallel"""
1032
1033 thisProcessNearestCellPointList, thisProcDistSqrdList = \
1034 self.GetCellsClosestToPointsOnThisProcessFromParaViewFilter(pointList)
1035
1036 nearestCellList, distanceList = self.UseMpiToGetGlobalCellPointsClosest(
1037 thisProcessNearestCellPointList, thisProcDistSqrdList)
1038
1039 return nearestCellList
1040
1041 def WritePerSegmentCellListToFile(self, filename, perSegmentCellList):
1042 ff = open(filename, "w")
1043 if self.collectionMethod == "seed cell with structured grid":
1044 ff.write("{\n")
1045 ff.write('"format for cells in lists": ' + PhactoriSampledCellInfo.TerseOneLineJsonFormatComment() + ",\n")
1046 ff.write('"list of collected cells for each seed cell": [\n')
1047 lastIdx = len(perSegmentCellList) - 1
1048 for idx, cellsForThisSegment in enumerate(perSegmentCellList):
1049 ff.write("{\n")
1050 ff.write('"cells for seed cell index": ' + str(idx) + ',\n')
1051 thisStructuredSeedCell = self.StructuredNearbyCellPointList[idx]
1052 nearbyGeometryPoint = self.NearbyGeometryPointList[idx]
1053 ff.write('"structured seed cell xyz": ' + str(thisStructuredSeedCell.cellTestPoint) + ',\n')
1054 ff.write('"structured seed cell ijk": ' + str(thisStructuredSeedCell.ijk) + ',\n')
1055 ff.write('"structured seed cell complete": ' + thisStructuredSeedCell.ToStrTerseOneLineList() + ',\n')
1056 ff.write('"geometry point used to find seed cell": ' + str(nearbyGeometryPoint) + ',\n')
1057 ff.write('"number of cells collected for this seed cell": ' + str(len(cellsForThisSegment)) + ',\n')
1058 ff.write('"cell list for this seed cell": [\n')
1059 for cellCntr, oneCellInfo in enumerate(cellsForThisSegment):
1060 if cellCntr != 0:
1061 ff.write(",\n")
1062 cellStr = oneCellInfo.ToStrTerseOneLineList()
1063 ff.write(cellStr)
1064 ff.write("\n]\n")
1065 if idx != lastIdx:
1066 ff.write("},\n")
1067 else:
1068 ff.write("}\n")
1069 ff.write("]\n")
1070 ff.write("}\n")
1071 else:
1072 ff.write("{\n")
1073 ff.write('"format for cells in lists": ' + PhactoriSampledCellInfo.TerseOneLineJsonFormatComment() + ",\n")
1074 ff.write('"list of collected cells for each segment": [\n')
1075 lastIdx = len(perSegmentCellList) - 1
1076 for idx, cellsForThisSegment in enumerate(perSegmentCellList):
1077 ff.write("{\n")
1078 ff.write('"cells for segment index": ' + str(idx) + ',\n')
1079 thisSegment = self.SegmentList[idx]
1080 nearbyGeometryPoint = self.NearbyGeometryPointList[idx]
1081 ff.write('"segment point A": ' + str(thisSegment.ptA) + ',\n')
1082 ff.write('"segment point B": ' + str(thisSegment.ptB) + ',\n')
1083 ff.write('"geometry point used to find point A": ' + str(nearbyGeometryPoint) + ',\n')
1084 ff.write('"number of cells collected for this segment": ' + str(len(cellsForThisSegment)) + ',\n')
1085 ff.write('"cell list for this segment": [\n')
1086 for cellCntr, oneCellInfo in enumerate(cellsForThisSegment):
1087 if cellCntr != 0:
1088 ff.write(",\n")
1089 cellStr = oneCellInfo.ToStrTerseOneLineList()
1090 ff.write(cellStr)
1091 ff.write("\n]\n")
1092 if idx != lastIdx:
1093 ff.write("},\n")
1094 else:
1095 ff.write("}\n")
1096 ff.write("]\n")
1097 ff.write("}\n")
1098 ff.close()
1099
1100 def WriteAllDataFromOneProcessUsingMPI(self, perSegmentCellList, basename = "PhactoriSegmentCellSampler3_", outputFilenameCounter = -1):
1101 if PhactoriDbg(100):
1102 myDebugPrint3("PhactoriSegmentCellSampler3.WriteAllDataFromOneProcessUsingMPI entered\n", 100)
1103 myPid = SmartGetLocalProcessId()
1104
1105 ##to start with, just have each process output it's data
1106 if PhactoriDbg(100):
1107 thisProcessHasData = False
1108 for idx, cellsForThisSegment in enumerate(perSegmentCellList):
1109 if len(cellsForThisSegment) != 0:
1110 thisProcessHasData = True
1111 break
1112 if self.collectionMethod == "seed cell with structured grid":
1113 structuredFlag = True
1114 else:
1115 structuredFlag = False
1116 if structuredFlag:
1117 basename = "PhactoriSegmentCellSampler3_structured_"
1118 else:
1119 basename = "PhactoriSegmentCellSampler3_"
1120 fname = basename + str(myPid) + self.SampledCellsOutputFilenameExtension
1121 if thisProcessHasData:
1122 self.WritePerSegmentCellListToFile(fname, perSegmentCellList)
1123
1124 if PhactoriDbg(100):
1125 myDebugPrint3("about to serialize cell infos\n", 100)
1126
1127 tupleSize = self.CellDataArrayTupleSize
1128 cellListSerializedFloatArray = []
1129 cellListSerializedIntArray = []
1130 for idx, cellsForThisSegment in enumerate(perSegmentCellList):
1131 for cellCntr, oneCellInfo in enumerate(cellsForThisSegment):
1132 oneCellInfo.SerializeAppendToFloatAndIntArray(cellListSerializedFloatArray, cellListSerializedIntArray, tupleSize)
1133
1134 if PhactoriDbg(100):
1135 myDebugPrint3("done serializing " + str(len(cellListSerializedFloatArray)) + " " + str(len(cellListSerializedIntArray)) + "\n", 100)
1136
1137 pidToDoOutput = SmartGetNumberOfProcesses() - 1
1138 globalSerializedFloatArray = UseMpiToSendAllProcessesFloatArrayToOneProcess(cellListSerializedFloatArray, pidToDoOutput)
1139 globalSerializedIntArray = UseMpiToSendAllProcessesIntArrayToOneProcess(cellListSerializedIntArray, pidToDoOutput)
1140
1141 if PhactoriDbg(100):
1142 myDebugPrint3("returned from getting globalSerializedFloatArray and globalSerializedIntArray\n")
1143
1144 if myPid == pidToDoOutput:
1145 if PhactoriDbg(100):
1146 myDebugPrint3("I'm the output pid, about to serialize in\n")
1147
1148 serialFloatSize, serialIntSize = PhactoriSampledCellInfo.GetSerializeFloatAndIntSize(tupleSize)
1149 globalNumCells = int(len(globalSerializedFloatArray) / serialFloatSize)
1150
1151 globalPerSegmentCellList = []
1152 if self.collectionMethod == "seed cell with structured grid":
1153 structuredFlag = True
1154 else:
1155 structuredFlag = False
1156 for ii in perSegmentCellList:
1157 globalPerSegmentCellList.append([])
1158 for ii in range(0, globalNumCells):
1159 newCell = PhactoriSampledCellInfo()
1160 newCell.SerializeSetFromFloatAndIntArray(globalSerializedFloatArray, globalSerializedIntArray, ii, tupleSize)
1161 whichSegList = globalPerSegmentCellList[newCell.segmentIndex]
1162 whichSegList.append(newCell)
1163
1164 if PhactoriDbg(100):
1165 myDebugPrint3("finished serialize, about to write to file\n")
1166 if PhactoriDbg(100):
1167 for ii, oneSegmentGlobalCellList in enumerate(globalPerSegmentCellList):
1168 myDebugPrint3(str(ii) + " " + str(len(oneSegmentGlobalCellList)) + "\n")
1169
1170 fname = self.CreateCurrentCallbackCellCollectionOutputFilePath(outputFilenameCounter)
1171 if PhactoriDbg(100):
1172 myDebugPrint3("output filename will be: " + str(fname) + "\n")
1173 self.WritePerSegmentCellListToFile(fname, globalPerSegmentCellList)
1174
1175 if PhactoriDbg(100):
1176 myDebugPrint3("PhactoriSegmentCellSampler3.WriteAllDataFromOneProcessUsingMPI returning\n", 100)
1177
1178 def CreateCurrentCallbackCellCollectionOutputFilePath(self, outputFilenameCounter):
1179 if outputFilenameCounter < 0:
1180 ftc = GetFrameTagCounter()
1181 else:
1182 ftc = outputFilenameCounter
1183
1184 outName = self.SampledCellsOutputDirectory + "/" + \
1185 self.SampledCellsOutputFilename + \
1186 str(ftc).zfill(self.NumCounterDigits) + \
1187 self.SampledCellsOutputFilenameExtension
1188
1189 return outName
1190
1191 def WriteAllDataFromOneProcessUsingMPIStructured(self, perSegmentCellList, outputFilenameCounter = -1):
1192 self.WriteAllDataFromOneProcessUsingMPI(perSegmentCellList, "PhactoriSegmentCellSampler3_structured_", outputFilenameCounter)
1193
1194 def ExportOperationData(self, datadescription):
1195 if PhactoriDbg(100):
1196 myDebugPrint3("PhactoriSegmentCellSampler3.ExportOperationData entered\n", 100)
1197
1198 UpdatePipelineWithCurrentTimeArgument(self.myCopyOfInputFilter)
1199
1200 if self.collectionMethod == "seed cell with structured grid":
1201 perSegmentStructuredCellList = self.GatherStructuredCellsFromSeedCells()
1202 if self.WriteCellListToJsonFile:
1203 self.WriteAllDataFromOneProcessUsingMPIStructured(perSegmentStructuredCellList)
1204 else:
1205 perSegmentCellList = self.SetMaskValueForCellsNearSegments(
1206 self.myCopyOfInputFilter, self.SegmentList, "blargmask1", "Ids",
1207 self.MaskTestDistanceSquared, self.ProjectionAxis)
1208
1209 if PhactoriDbg(100):
1210 for idx, cellsForThisSegment in enumerate(perSegmentCellList):
1211 myDebugPrint3("after recursion cells for segment (index, count): " + str(idx) + ", " + str(len(cellsForThisSegment)) + "\n")
1212 #myDebugPrint3(str(cellsForThisSegment) + "\n")
1213 myDebugPrint3("\n")
1214
1215 if self.WriteCellListToJsonFile:
1216 self.WriteAllDataFromOneProcessUsingMPI(perSegmentCellList)
1217
1218 if PhactoriDbg(100):
1219 myDebugPrint3("PhactoriSegmentCellSampler3.ExportOperationData returning\n", 100)
1220
1221 def CreateProgrammableFilterString(self):
1222
1223 scriptLines = []
1224
1225 scriptLines.append("#print('test2')\n")
1226
1227 scriptLines.append("#cellIndexesToSet = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]\n")
1228 scriptLines.append("#maskValuesToSet = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]\n")
1229 scriptLines.append("#leafVisitCount = [0,0,0,0,0,0,0,0,0,0,0]\n")
1230
1231 scriptLines.append("localLeafVisitCount = 0\n")
1232
1233 scriptLines.append("def flatten(input, output):\n")
1234 scriptLines.append(" # Copy the cells etc.\n")
1235 scriptLines.append(" output.ShallowCopy(input)\n")
1236 scriptLines.append(" celldata = output.GetCellData()\n")
1237 scriptLines.append(" #for now if there are no cell arrays here we don't want to add one\n")
1238 scriptLines.append(" #really we don't want to add one of there are actually no cells\n")
1239 scriptLines.append(" numCellArrays = celldata.GetNumberOfArrays()\n")
1240 scriptLines.append(" if numCellArrays == 0:\n")
1241 scriptLines.append(" return\n")
1242 scriptLines.append(" numCells = input.GetNumberOfCells()\n")
1243 scriptLines.append(" numPoints = input.GetNumberOfPoints()\n")
1244 scriptLines.append(" #print(str(numCells))\n")
1245 scriptLines.append(" ncda = vtk.vtkDoubleArray()\n")
1246 scriptLines.append(" ncda.SetNumberOfTuples(numCells)\n")
1247 scriptLines.append(" numTuples = ncda.GetNumberOfTuples()\n")
1248 scriptLines.append(" for ii in range(0, numTuples):\n")
1249 scriptLines.append(" ncda.SetValue(ii, 0.0)\n")
1250 scriptLines.append(" for ii, cellNdx in enumerate(cellIndexesToSet):\n")
1251 scriptLines.append(" if localLeafVisitCount == leafVisitCount[ii]:\n")
1252 scriptLines.append(" if cellNdx < numCells:\n")
1253 scriptLines.append(" ncda.SetValue(cellNdx, float(maskValuesToSet[ii]))\n")
1254 scriptLines.append(" ncda.SetName('" + self.ProgrammableFilterOutputCellVariableName + "')\n")
1255 scriptLines.append(" #print(ncda.GetName())\n")
1256 scriptLines.append(" celldata.AddArray(ncda)\n")
1257
1258 scriptLines.append("input = self.GetInputDataObject(0, 0)\n")
1259 scriptLines.append("output = self.GetOutputDataObject(0)\n")
1260
1261 scriptLines.append("if input.IsA('vtkMultiBlockDataSet'):\n")
1262 scriptLines.append(" output.CopyStructure(input)\n")
1263 scriptLines.append(" iter = input.NewIterator()\n")
1264 scriptLines.append(" iter.UnRegister(None)\n")
1265 scriptLines.append(" iter.InitTraversal()\n")
1266 scriptLines.append(" while not iter.IsDoneWithTraversal():\n")
1267 scriptLines.append(" localLeafVisitCount += 1\n")
1268 scriptLines.append(" curInput = iter.GetCurrentDataObject()\n")
1269 scriptLines.append(" curOutput = curInput.NewInstance()\n")
1270 scriptLines.append(" curOutput.UnRegister(None)\n")
1271 scriptLines.append(" output.SetDataSet(iter, curOutput)\n")
1272 scriptLines.append(" flatten(curInput, curOutput)\n")
1273 scriptLines.append(" iter.GoToNextItem();\n")
1274 scriptLines.append("else:\n")
1275 scriptLines.append(" flatten(input, output)\n")
1276
1277 mainScriptString = "".join(scriptLines)
1278
1279 myCellIndexesToSet = []
1280 myMaskValuesToSet = []
1281 myLeafVisitCount = []
1282 if self.PerSegmentMarkedCellInfoList != None:
1283 for idx, cellsForThisSegment in enumerate(self.PerSegmentMarkedCellInfoList):
1284 if cellsForThisSegment != None:
1285 maskValue = idx+1
1286 for cellIdx, oneCellInfo in enumerate(cellsForThisSegment):
1287 myCellIndexesToSet.append(oneCellInfo.index)
1288 myMaskValuesToSet.append(maskValue)
1289 myLeafVisitCount.append(oneCellInfo.leafVisitCount)
1290
1291 newstr1 = "cellIndexesToSet = " + str(myCellIndexesToSet) + "\n"
1292 newstr2 = "maskValuesToSet = " + str(myMaskValuesToSet) + "\n"
1293 newstr3 = "leafVisitCount = " + str(myLeafVisitCount) + "\n"
1294 self.mProgFilterString = newstr1 + newstr2 + newstr3 + mainScriptString
1295
1296 if PhactoriDbg(100):
1297 myDebugPrint3("PhactoriMarkCellSetsOperation constructed script:\n" + \
1298 self.mProgFilterString)
1299
1300 #phactori_combine_to_single_python_file_subpiece_end_1
1301