| 1 |
# Copyright(C) 1999-2020, 2024 National Technology & Engineering Solutions |
| 2 |
# of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
# NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions are |
| 7 |
# met: |
| 8 |
# |
| 9 |
# * Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following |
| 14 |
# disclaimer in the documentation and/or other materials provided |
| 15 |
# with the distribution. |
| 16 |
# |
| 17 |
# * Neither the name of NTESS nor the names of its |
| 18 |
# contributors may be used to endorse or promote products derived |
| 19 |
# from this software without specific prior written permission. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
from phactori import * |
| 34 |
from paraview.simple import * |
| 35 |
from .QuadInfoGQC import * |
| 36 |
from .PhactoriMpiUtilities import * |
| 37 |
|
| 38 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 39 |
|
| 40 |
class PhactoriPointSourceGeometrySampler1(PhactoriOperationSpecifics): |
| 41 |
"""Filter/operation which reads in a .json file which is a list of 3d |
| 42 |
coordinates and creates a new point source from that list. Resulting |
| 43 |
point source will have 1 element an N points. This source is intented |
| 44 |
to work correctly in parallel for Catalyst or pvbatch symmetric mode. |
| 45 |
The json file which is read in will only be read on one process and |
| 46 |
mpi broadcast is used to distribute the list, rather than having each |
| 47 |
process read the json file.""" |
| 48 |
def __init__(self): |
| 49 |
self.JsonListFileName = "PhactoriPointSourceGeometrySampler1.json" |
| 50 |
self.JsonList = None |
| 51 |
self.ParaviewPointSource = None |
| 52 |
self.SourceVtkPolyData = None |
| 53 |
self.myVtkPoints = vtk.vtkPoints() |
| 54 |
self.myVtkPolyVertex = vtk.vtkPolyVertex() |
| 55 |
self.myVtkCellArray = vtk.vtkCellArray() |
| 56 |
self.myVtkPolyData = vtk.vtkPolyData() |
| 57 |
self.NearestQuadList = None |
| 58 |
self.DistanceList = None |
| 59 |
|
| 60 |
def ValidateJsonPointList(self): |
| 61 |
numPoints = len(self.JsonList) |
| 62 |
if numPoints < 1: |
| 63 |
myDebugPrint3AndException( |
| 64 |
"PhactoriPointSourceGeometrySampler1::ValidateJsonPointList\n" |
| 65 |
"list must have at least one element\n") |
| 66 |
|
| 67 |
for ptNdx in range(0,numPoints): |
| 68 |
jsonPt = self.JsonList[ptNdx] |
| 69 |
if len(jsonPt) != 3: |
| 70 |
errStr = "PhactoriPointSourceGeometrySampler1::ValidateJsonPointList\n" \ |
| 71 |
"point with index " + str(ptNdx) + "does not have three elements\n" |
| 72 |
myDebugPrint3AndException(errStr) |
| 73 |
|
| 74 |
|
| 75 |
def ParseParametersFromJson(self, inJson): |
| 76 |
if 'filename' in inJson: |
| 77 |
self.JsonListFileName = inJson['filename'] |
| 78 |
else: |
| 79 |
myDebugPrint3AndException( |
| 80 |
"PhactoriPointSourceGeometrySampler1::ParseParametersFromJson\n" |
| 81 |
"Error: must have 'filename' key\n") |
| 82 |
|
| 83 |
def GetPidWithLeastValueList(self, inLocalDistSqrdList): |
| 84 |
myPid = int(SmartGetLocalProcessId()) |
| 85 |
globalDistSqrdList = UseReduceOnFloatList(inLocalDistSqrdList, 1) |
| 86 |
localPidList = [] |
| 87 |
numItems = len(inLocalDistSqrdList) |
| 88 |
for ndx in range(0,numItems): |
| 89 |
if globalDistSqrdList[ndx] == inLocalDistSqrdList[ndx]: |
| 90 |
localPidList.append(myPid) |
| 91 |
else: |
| 92 |
localPidList.append(-1) |
| 93 |
|
| 94 |
pidWithDataList = UseReduceOnIntegerList(localPidList, 0) |
| 95 |
return pidWithDataList, globalDistSqrdList |
| 96 |
|
| 97 |
def UseMpiToGetGlobalQuadsClosest(self, inLocalQuadList, inLocalDistSqrdList): |
| 98 |
if PhactoriDbg(100): |
| 99 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.UseMpiToGetGlobalQuadsClosest entered\n", 100) |
| 100 |
|
| 101 |
pidWithDataList, globalDistSqrdList = self.GetPidWithLeastValueList(inLocalDistSqrdList) |
| 102 |
if PhactoriDbg(100): |
| 103 |
myDebugPrint3("pidWithDataList:\n" + str(pidWithDataList) + "\nglobalDistSqrdList:\n" + str(globalDistSqrdList) + "\n") |
| 104 |
|
| 105 |
#convert quad list to array of doubles and ints, use mpi reduce to share |
| 106 |
#the values, then convert back to quad list |
| 107 |
|
| 108 |
#convert quad list to array of doubles and ints |
| 109 |
quadInfoDoubleArray = [] |
| 110 |
quadInfoIntArray = [] |
| 111 |
myPid = SmartGetLocalProcessId() |
| 112 |
for ii, oneQuad in enumerate(inLocalQuadList): |
| 113 |
if pidWithDataList[ii] == myPid: |
| 114 |
oneQuad.AddToInfoArrays(quadInfoDoubleArray, quadInfoIntArray) |
| 115 |
else: |
| 116 |
oneQuad.AddZeroQuadToInfoArrays(quadInfoDoubleArray, quadInfoIntArray) |
| 117 |
|
| 118 |
#use mpi reduce to spread array correctly |
| 119 |
globalQuadInfoDoubleArray = UseReduceOnFloatList(quadInfoDoubleArray, 2) |
| 120 |
globalQuadInfoIntArray = UseReduceOnIntegerList(quadInfoIntArray, 2) |
| 121 |
|
| 122 |
#now create return global quad list from arrays |
| 123 |
numQuads = len(inLocalQuadList) |
| 124 |
returnGlobalQuadList = [] |
| 125 |
for ii in range(0,numQuads): |
| 126 |
newQuad = QuadInfoGQC() |
| 127 |
newQuad.PopulateFromInfoArrays(ii, globalQuadInfoDoubleArray, globalQuadInfoIntArray) |
| 128 |
returnGlobalQuadList.append(newQuad) |
| 129 |
|
| 130 |
if PhactoriDbg(100): |
| 131 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.UseMpiToGetGlobalQuadsClosest returning\n", 100) |
| 132 |
return returnGlobalQuadList, globalDistSqrdList |
| 133 |
|
| 134 |
def CreateSamplePoints(self, inInputFilter): |
| 135 |
if PhactoriDbg(100): |
| 136 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.CreateSamplePoints entered\n", 100) |
| 137 |
|
| 138 |
thisProcessNearestQuadList, thisProcDistSqrdList = self.mPhactoriOperationBlockOwner.\ |
| 139 |
GetQuadsClosestToPointsOnThisProcessFromParaViewFilter(inInputFilter, self.JsonList) |
| 140 |
|
| 141 |
if PhactoriDbg(100): |
| 142 |
myDebugPrint3("quads before mpi\n", 100) |
| 143 |
for ii, oneQuad in enumerate(thisProcessNearestQuadList): |
| 144 |
myDebugPrint3(str(ii) + ": " + str(thisProcDistSqrdList[ii]) + "\n" + oneQuad.ToString(), 100) |
| 145 |
|
| 146 |
self.NearestQuadList, self.DistanceList = self.UseMpiToGetGlobalQuadsClosest( |
| 147 |
thisProcessNearestQuadList, thisProcDistSqrdList) |
| 148 |
|
| 149 |
if PhactoriDbg(100): |
| 150 |
myDebugPrint3("quads after mpi\n", 100) |
| 151 |
for ii, oneQuad in enumerate(self.NearestQuadList): |
| 152 |
myDebugPrint3(str(ii) + ": " + str(self.NearestQuadList[ii]) + "\n" + oneQuad.ToString(), 100) |
| 153 |
|
| 154 |
if PhactoriDbg(100): |
| 155 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.CreateSamplePoints returning\n", 100) |
| 156 |
|
| 157 |
def CreateParaViewFilter(self, inInputFilter): |
| 158 |
if PhactoriDbg(100): |
| 159 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.CreateParaViewFilter entered\n", 100) |
| 160 |
|
| 161 |
savedActiveSource = GetActiveSource() |
| 162 |
|
| 163 |
self.JsonList = ReadAndMpiBroadcastJsonFile(self.JsonListFileName) |
| 164 |
|
| 165 |
self.ValidateJsonPointList() |
| 166 |
|
| 167 |
UpdatePipelineWithCurrentTimeArgument(inInputFilter) |
| 168 |
self.CreateSamplePoints(inInputFilter) |
| 169 |
|
| 170 |
allSamplePoints = [] |
| 171 |
for ii, oneQuad in enumerate(self.NearestQuadList): |
| 172 |
#oneQuadSamplePoints = oneQuad.MakePerpendicularSamplePoints() |
| 173 |
oneQuadSamplePoints = oneQuad.MakeLogSamplePointsAlongRayFromNearbyPoint(self.JsonList[ii], 0.0000001, 1.414, 0.05) |
| 174 |
allSamplePoints.extend(oneQuadSamplePoints) |
| 175 |
|
| 176 |
numPoints = len(allSamplePoints) |
| 177 |
self.myVtkPoints.SetNumberOfPoints(numPoints) |
| 178 |
self.myVtkPolyVertex.GetPointIds().SetNumberOfIds(numPoints) |
| 179 |
|
| 180 |
vtkPolyVertPtIds = self.myVtkPolyVertex.GetPointIds() |
| 181 |
usePoint = [0.0, 0.0, 0.0] |
| 182 |
for ptNdx in range(0,numPoints): |
| 183 |
#self.myVtkPoints.SetPoint(ptNdx, self.JsonList[ptNdx]) |
| 184 |
quadPtAvg = allSamplePoints[ptNdx] |
| 185 |
usePoint[0] = quadPtAvg[0] |
| 186 |
usePoint[1] = quadPtAvg[1] |
| 187 |
usePoint[2] = quadPtAvg[2] |
| 188 |
self.myVtkPoints.SetPoint(ptNdx, usePoint) |
| 189 |
vtkPolyVertPtIds.SetId(ptNdx, ptNdx) |
| 190 |
|
| 191 |
self.myVtkPolyData.SetPoints(self.myVtkPoints) |
| 192 |
self.myVtkCellArray.InsertNextCell(self.myVtkPolyVertex) |
| 193 |
self.myVtkPolyData.SetVerts(self.myVtkCellArray) |
| 194 |
|
| 195 |
self.ParaviewPointSource = PVTrivialProducer() |
| 196 |
self.ParaviewPointSource.GetClientSideObject().SetOutput(self.myVtkPolyData) |
| 197 |
|
| 198 |
SetActiveSource(self.ParaviewPointSource) |
| 199 |
SetActiveSource(savedActiveSource) |
| 200 |
|
| 201 |
UpdatePipelineWithCurrentTimeArgument(self.ParaviewPointSource) |
| 202 |
|
| 203 |
if PhactoriDbg(100): |
| 204 |
myDebugPrint3("PhactoriPointSourceGeometrySampler1.CreateParaViewFilter returning\n", 100) |
| 205 |
return self.ParaviewPointSource |
| 206 |
|
| 207 |
#phactori_combine_to_single_python_file_subpiece_end_1 |