| 1 |
# Copyright(C) 1999-2022, 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 |
|
| 36 |
#phactori_combine_to_single_python_file_subpiece_begin_1 |
| 37 |
class PhactoriRepresentationBlock: |
| 38 |
def __init__(self): |
| 39 |
self.mName = "" |
| 40 |
self.mColorVariableInfo = PhactoriVariableInfo() |
| 41 |
self.mColorLegendFlag = True |
| 42 |
#self.mColorLegendPositionAndSize = ['bottom', 1.0] |
| 43 |
#self.mColorLegendPositionAndSize = ['right', 1.0] |
| 44 |
self.mColorLegendPositionAndSize = ['bottom right', 1.0] |
| 45 |
self.mTimeAnnotationSettings = PhactoriAnnotationViewSettings( |
| 46 |
'time', 'time') |
| 47 |
self.mDataCubeAxesFlag = False |
| 48 |
self.mDataCubeAxesInfo = PhactoriDataCubeAxesInfo() |
| 49 |
#self.mColorByBlockFlag = True |
| 50 |
self.mColorByBlockFlag = True |
| 51 |
self.mColorByBlockExplicitlySet = False |
| 52 |
self.mColorBySolidColorFlag = False |
| 53 |
self.mSolidColor = None |
| 54 |
self.mOpacitySetting = 1.0 |
| 55 |
self.mOrientationAxesFlag = True |
| 56 |
self.mFixedColorRange = None |
| 57 |
self.mUseFixedColorRange = False |
| 58 |
self.mColorRangeMinMaxTracker = PlotValMinMaxTrkC() |
| 59 |
self.mUseHighlightSubranges = False |
| 60 |
self.mHighlightSubranges = [] |
| 61 |
self.mUseColorSubrange = False |
| 62 |
self.mColorSubrange = [0.0, 1.0] |
| 63 |
self.mFilenameAddon = "" |
| 64 |
self.mColorSettings = PhactoriColorSettings() |
| 65 |
self.mColorMapSettings = PhactoriColorMapSettings() |
| 66 |
self.mPointSize = 2.0 |
| 67 |
self.InterpretValuesAsCategories = False |
| 68 |
self.CategoryAndColorJsonFile = None |
| 69 |
self.Annotations = None |
| 70 |
self.IndexedColors = None |
| 71 |
self.IndexedOpacities = None |
| 72 |
|
| 73 |
def ParseSettingsFromJson(self, inRepresentationBlockJson): |
| 74 |
"""given a python dict (presumably from json) description of a |
| 75 |
representation block, parse all the representation parameters out of |
| 76 |
the block""" |
| 77 |
|
| 78 |
if PhactoriDbg(100): |
| 79 |
myDebugPrint3('ParseSettingsFromJson entered\n', 100) |
| 80 |
|
| 81 |
inJsn = inRepresentationBlockJson |
| 82 |
|
| 83 |
#hack test for colors start |
| 84 |
#inJsn['surface color'] = [1.0, 0.0, 0.0] |
| 85 |
#inJsn['background color'] = [0.0, 0.0, 0.0] |
| 86 |
#inJsn['edge color'] = [0.0, 1.0, 0.0] |
| 87 |
##inJsn['axes color'] = [0.0, 1.0, 1.0] |
| 88 |
#inJsn['text color'] = [1.0, 1.0, 0.0] |
| 89 |
#hack test for colors end |
| 90 |
|
| 91 |
#myDebugPrint3(' json: ' + str(inJsn) + '\n') |
| 92 |
|
| 93 |
if 'point size' in inJsn: |
| 94 |
self.mPointSize = inJsn['point size'] |
| 95 |
|
| 96 |
#color by variable scalar/vector magnitude/vector component/tensor component |
| 97 |
self.mColorVariableInfo.\ |
| 98 |
ParseVariableNameAndVectorOrTensorComponent(inJsn, 'color by ') |
| 99 |
|
| 100 |
if self.mColorVariableInfo.mVariableName != '': |
| 101 |
self.mColorByBlockFlag = False |
| 102 |
self.mColorBySolidColorFlag = False |
| 103 |
elif 'color by blockid' in inJsn: |
| 104 |
self.mColorByBlockFlag = True |
| 105 |
self.mColorByBlockExplicitlySet = True |
| 106 |
self.mColorBySolidColorFlag = False |
| 107 |
elif 'color by solid color' in inJsn: |
| 108 |
self.mColorBySolidColorFlag = True |
| 109 |
self.mColorByBlockFlag = False |
| 110 |
self.mSolidColor = inJsn['color by solid color'] |
| 111 |
|
| 112 |
#color map range control |
| 113 |
if 'color legend range' in inJsn: |
| 114 |
self.mFixedColorRange = inJsn['color legend range'] |
| 115 |
self.mUseFixedColorRange = True |
| 116 |
else: |
| 117 |
self.mUseFixedColorRange = False |
| 118 |
|
| 119 |
#highlight subranges with solid colors |
| 120 |
if 'highlight subrange 1' in inJsn: |
| 121 |
highlightSubrangeIndex = 1 |
| 122 |
while True: |
| 123 |
oneSubrangeKey = 'highlight subrange ' + str(highlightSubrangeIndex) |
| 124 |
if oneSubrangeKey in inJsn: |
| 125 |
subrangeArgs = inJsn[oneSubrangeKey] |
| 126 |
highlightSubrangeIndex += 1 |
| 127 |
if len(subrangeArgs) != 5: |
| 128 |
if PhactoriDbg(): |
| 129 |
myDebugPrint3("highlight subrange needs 5 values\n"); |
| 130 |
PrintOnProcessZero("highlight subrange needs 5 values, skipping " + \ |
| 131 |
oneSubrangeKey + "\n"); |
| 132 |
continue |
| 133 |
srmin = float(subrangeArgs[0]) |
| 134 |
srmax = float(subrangeArgs[1]) |
| 135 |
if srmin > srmax: |
| 136 |
if PhactoriDbg(): |
| 137 |
myDebugPrint3("subrange highlight min >= max: " + \ |
| 138 |
str(srmin) + ", " + str(srmax) + "\nskipping " + \ |
| 139 |
oneSubrangeKey + "\n", 100) |
| 140 |
PrintOnProcessZero("subrange highlight min >= max: " + \ |
| 141 |
str(srmin) + ", " + str(srmax) + "\nskipping " + \ |
| 142 |
oneSubrangeKey + "\n") |
| 143 |
continue |
| 144 |
srColor = [float(subrangeArgs[2]), float(subrangeArgs[3]), |
| 145 |
float(subrangeArgs[4])] |
| 146 |
if (srColor[0] < 0.0) or (srColor[0] > 1.0) or \ |
| 147 |
(srColor[1] < 0.0) or (srColor[1] > 1.0) or \ |
| 148 |
(srColor[2] < 0.0) or (srColor[2] > 1.0): |
| 149 |
srColor = [1.0, 1.0, 0.0] |
| 150 |
if PhactoriDbg(): |
| 151 |
myDebugPrint3(oneSubrangeKey + ": bad color " |
| 152 |
"(component not 0.0-1.0), using rgb 1.0, 1.0, 0.0\n", 100) |
| 153 |
PrintOnProcessZero(oneSubrangeKey + ": bad color " |
| 154 |
"(component not 0.0-1.0), using rgb 1.0, 1.0, 0.0\n") |
| 155 |
self.mHighlightSubranges.append( |
| 156 |
[srmin, srmax, srColor]) |
| 157 |
self.mUseHighlightSubranges = True |
| 158 |
else: |
| 159 |
break |
| 160 |
if PhactoriDbg(): |
| 161 |
myDebugPrint3("parsed highlight subranges:\n" + \ |
| 162 |
str(self.mHighlightSubranges) + "\n", 100) |
| 163 |
|
| 164 |
#if 'highlight subranges' in inJsn: |
| 165 |
# self.mUseHighlightSubranges = True |
| 166 |
# sbrngsJsn = inJsn['highlight subranges'] |
| 167 |
# for oneSubrange in sbrngsJsn: |
| 168 |
# if 'range' in oneSubrange: |
| 169 |
# srmin = oneSubrange['range'][0] |
| 170 |
# srmax = oneSubrange['range'][1] |
| 171 |
# if srmin < srmax: |
| 172 |
# if 'color' in oneSubrange: |
| 173 |
# srColor = oneSubrange['color'] |
| 174 |
# else: |
| 175 |
# srColor = [1.0, 1.0, 0.0] |
| 176 |
# self.mHighlightSubranges.append( |
| 177 |
# [srmin, srmax, srColor]) |
| 178 |
# else: |
| 179 |
# if PhactoriDbg(): |
| 180 |
# myDebugPrint3("highlight min >= max: " + \ |
| 181 |
# str(srmin) + ", " + str(srmax) + "\nskipping subrange\n", 100) |
| 182 |
# PrintOnProcessZero("highlight min >= max: " + \ |
| 183 |
# str(srmin) + ", " + str(srmax) + "\nskipping subrange\n") |
| 184 |
# else: |
| 185 |
# if PhactoriDbg(): |
| 186 |
# myDebugPrint3("subrange is missing 'range' key; skipping\n") |
| 187 |
# PrintOnProcessZero("subrange is missing 'range' key; skipping\n") |
| 188 |
# if PhactoriDbg(): |
| 189 |
# myDebugPrint3("parsed highlight subranges:\n" + \ |
| 190 |
# str(self.mHighlightSubranges) + "\n", 100) |
| 191 |
|
| 192 |
#additional capability: use ratio-expressed subrange of |
| 193 |
#range that would otherwise be used to increase concentration of |
| 194 |
#dynamic range of color map in range of interest |
| 195 |
if 'color legend subrange' in inJsn: |
| 196 |
self.mUseColorSubrange = True |
| 197 |
self.mColorSubrange = inJsn['color legend subrange'] |
| 198 |
goodSubrange = True |
| 199 |
if self.mColorSubrange[0] < 0.0: |
| 200 |
goodSubrange = False |
| 201 |
if self.mColorSubrange[1] > 1.0: |
| 202 |
goodSubrange = False |
| 203 |
if self.mColorSubrange[0] > \ |
| 204 |
self.mColorSubrange[1]: |
| 205 |
goodSubrange = False |
| 206 |
if goodSubrange == False: |
| 207 |
myDebugPrint3AndException( |
| 208 |
"ParseOneRepresentationBlockC:\n" |
| 209 |
"bad color legend subrange, must be 0.0 <= bottom <= top <= 1.0\n") |
| 210 |
|
| 211 |
#choose color map by name |
| 212 |
self.mColorMapSettings.ParseColorMapSettings(inJsn) |
| 213 |
|
| 214 |
self.InterpretValuesAsCategories = \ |
| 215 |
getParameterFromBlock(inJsn, "interpret values as categories", False) |
| 216 |
if self.InterpretValuesAsCategories: |
| 217 |
key1 = "category and color json file" |
| 218 |
if key1 not in inJsn: |
| 219 |
myDebugPrint3AndException( |
| 220 |
"interpret values as categories is true but '" + key1 + \ |
| 221 |
"' is missing\n") |
| 222 |
self.CategoryAndColorJsonFile = \ |
| 223 |
getParameterFromBlock(inJsn, key1, "dummy1.json") |
| 224 |
elif 'color by scalar' in inJsn: |
| 225 |
if inJsn['color by scalar'] == "ObjectId": |
| 226 |
if PhactoriDbg(): |
| 227 |
myDebugPrint3("variable is ObjectId, using categories\n") |
| 228 |
self.InterpretValuesAsCategories = True |
| 229 |
self.CategoryAndColorJsonFile = "ObjectIdBlockColors.json" |
| 230 |
|
| 231 |
showSurfacesFlag = getParameterFromBlock(inJsn, 'show surfaces', True) |
| 232 |
showEdgesFlag = getParameterFromBlock(inJsn, 'show edges', False) |
| 233 |
showPointsFlag = getParameterFromBlock(inJsn, 'show points', False) |
| 234 |
|
| 235 |
self.mOpacitySetting = getParameterFromBlock(inJsn, 'opacity', |
| 236 |
self.mOpacitySetting) |
| 237 |
|
| 238 |
#doVolumeRenderingFlag = getParameterFromBlock(inJsn, 'volume rendering', True) |
| 239 |
doVolumeRenderingFlag = getParameterFromBlock(inJsn, 'volume rendering', False) |
| 240 |
self.mScalarOpacityUnitDistance = getParameterFromBlock( |
| 241 |
inJsn, 'scalar opacity unit distance', -1.0) |
| 242 |
|
| 243 |
#self.mScalarOpacityUnitDistance = 0.01 |
| 244 |
if PhactoriDbg(): |
| 245 |
myDebugPrint3("doVolumeRenderingFlag: " + \ |
| 246 |
str(doVolumeRenderingFlag) + "\n" + \ |
| 247 |
"self.mScalarOpacityUnitDistance: " + \ |
| 248 |
str(self.mScalarOpacityUnitDistance) + "\n") |
| 249 |
|
| 250 |
self.mPresetsImportFileName = getParameterFromBlock(inJsn, |
| 251 |
'color and opacity presets import file', None) |
| 252 |
if self.mPresetsImportFileName != None: |
| 253 |
retval = ImportPresets(self.mPresetsImportFileName) |
| 254 |
if retval != True: |
| 255 |
myDebugPrint3AndException( |
| 256 |
"paraview.simple.ImportPresets failed with the file:\n" + \ |
| 257 |
str(self.mPresetsImportFileName) + "\n") |
| 258 |
self.mNameOfPresetToUse = getParameterFromBlock(inJsn, |
| 259 |
'color and opacity preset', None) |
| 260 |
|
| 261 |
showBoundingBoxFlag = getParameterFromBlock(inJsn, 'show bounding box', False) |
| 262 |
|
| 263 |
self.mMeshRenderControl = 'Surface' |
| 264 |
if showBoundingBoxFlag: |
| 265 |
self.mMeshRenderControl = 'Outline' |
| 266 |
if showSurfacesFlag | showEdgesFlag | showPointsFlag: |
| 267 |
if PhactoriDbg(): |
| 268 |
myDebugPrint3(" warning: when show bounding box is true, \n" + \ |
| 269 |
" show surfaces, show edges, and show points should be false\n") |
| 270 |
elif showPointsFlag: |
| 271 |
self.mMeshRenderControl = 'Points' |
| 272 |
else: |
| 273 |
if showSurfacesFlag: |
| 274 |
if showEdgesFlag: |
| 275 |
self.mMeshRenderControl = 'Surface With Edges' |
| 276 |
else: |
| 277 |
self.mMeshRenderControl = 'Surface' |
| 278 |
else: |
| 279 |
if showEdgesFlag: |
| 280 |
self.mMeshRenderControl = 'Wireframe' |
| 281 |
#self.mMeshRenderControl = 'Points' |
| 282 |
else: |
| 283 |
self.mMeshRenderControl = 'Outline' |
| 284 |
|
| 285 |
if doVolumeRenderingFlag: |
| 286 |
if PhactoriDbg(100): |
| 287 |
myDebugPrint3('doing volume rendering\n', 100) |
| 288 |
self.mMeshRenderControl = 'Volume' |
| 289 |
self.mDoingVolumeRendering = True |
| 290 |
else: |
| 291 |
self.mDoingVolumeRendering = False |
| 292 |
|
| 293 |
#color legend on/off |
| 294 |
self.mColorLegendFlag = getParameterFromBlock(inJsn, |
| 295 |
'show color legend', self.mColorLegendFlag) |
| 296 |
|
| 297 |
self.mColorLegendPositionAndSize = \ |
| 298 |
getParameterFromBlock(inJsn, 'color legend position', |
| 299 |
self.mColorLegendPositionAndSize) |
| 300 |
|
| 301 |
if self.mColorLegendFlag == True: |
| 302 |
if self.mColorVariableInfo.mVariableName == '': |
| 303 |
self.mColorLegendFlag = False |
| 304 |
|
| 305 |
self.mColorRangeMinMaxTracker.PlotValMinMaxTrkCParseJson( |
| 306 |
inJsn, "color legend ") |
| 307 |
|
| 308 |
self.mTimeAnnotationSettings.ParseAvsFromJson(inJsn) |
| 309 |
|
| 310 |
self.mDataCubeAxesFlag = getParameterFromBlock(inJsn, |
| 311 |
'show axes', self.mDataCubeAxesFlag) |
| 312 |
|
| 313 |
self.mDataCubeAxesInfo.DcaiParseParametersFromJson(inJsn) |
| 314 |
|
| 315 |
self.mOrientationAxesFlag = getParameterFromBlock(inJsn, |
| 316 |
'show orientation axes', self.mOrientationAxesFlag) |
| 317 |
|
| 318 |
if "image name addon" in inJsn: |
| 319 |
self.mFilenameAddon = inJsn["image name addon"] |
| 320 |
|
| 321 |
self.mColorSettings.ParseColorSettingsFromJson(inJsn) |
| 322 |
|
| 323 |
if PhactoriDbg(100): |
| 324 |
myDebugPrint3('ParseOneRepresentationBlockC returning\n', 100) |
| 325 |
|
| 326 |
def SetFromRestartInfo(self, inJson): |
| 327 |
"""given a map (json format), use the info in the map to set the |
| 328 |
representation state--this reads the info created in |
| 329 |
GetRestartInfo""" |
| 330 |
|
| 331 |
if 'mColorRangeMinMaxTracker' not in inJson: |
| 332 |
if PhactoriDbg(): |
| 333 |
myDebugPrint3("PhactoriRepresentationBlock::" + \ |
| 334 |
"SetFromRestartInfo: no mColorRangeMinMaxTracker, return\n") |
| 335 |
return |
| 336 |
|
| 337 |
if PhactoriDbg(): |
| 338 |
myDebugPrint3("Representation::SetFromRestartInfo\n" + |
| 339 |
"currently set to do something\n" + |
| 340 |
"before tracker:\n" + self.mColorRangeMinMaxTracker.SelfToStr()) |
| 341 |
|
| 342 |
jsonItem = inJson['mColorRangeMinMaxTracker'] |
| 343 |
self.mColorRangeMinMaxTracker.SetFromRestartInfo(jsonItem) |
| 344 |
|
| 345 |
if PhactoriDbg(): |
| 346 |
myDebugPrint3("after tracker:\n" + |
| 347 |
self.mColorRangeMinMaxTracker.SelfToStr()) |
| 348 |
|
| 349 |
def GetRestartInfo(self): |
| 350 |
"""construct, in python map/json format, the information from this |
| 351 |
representation instance which contains the information |
| 352 |
which would be needed to restore the representation to the proper |
| 353 |
state after a simulation restart, particularly color range tracking |
| 354 |
information. Return the restart info map/json""" |
| 355 |
newRestartInfoJson = {} |
| 356 |
newRestartInfoJson['mColorRangeMinMaxTracker'] = \ |
| 357 |
self.mColorRangeMinMaxTracker.GetRestartInfo() |
| 358 |
return newRestartInfoJson |
| 359 |
|
| 360 |
def CalculateDefaultScalarOpacityUnitDistance(self, inPhactoriOperation): |
| 361 |
"""given a phactori operation assumed to have an updated pipeline, |
| 362 |
calculate a default scalar opacity unit distance the same as paraview, |
| 363 |
with 0.05 * diagonal length of data bounding box""" |
| 364 |
bnds = GetGlobalDataBoundsParallel(inPhactoriOperation.mParaViewFilter) |
| 365 |
xxlen = bnds[1] - bnds[0] |
| 366 |
yylen = bnds[3] - bnds[2] |
| 367 |
zzlen = bnds[5] - bnds[4] |
| 368 |
self.mScalarOpacityUnitDistance = \ |
| 369 |
0.05 * math.sqrt(xxlen*xxlen + yylen*yylen + zzlen*zzlen) |
| 370 |
|
| 371 |
def SetUpForInterpretValuesAsCateories(self, variableLookUpTable): |
| 372 |
if PhactoriDbg(): |
| 373 |
myDebugPrint3("SetUpForInterpretValuesAsCateories entered\n") |
| 374 |
if(self.Annotations == None): |
| 375 |
jsonCategories = ReadAndMpiBroadcastJsonFile(self.CategoryAndColorJsonFile) |
| 376 |
jsonCategories = convertJsonUnicodeToStrings(jsonCategories) |
| 377 |
self.Annotations = jsonCategories[0]["Annotations"] |
| 378 |
self.IndexedColors = jsonCategories[0]["IndexedColors"] |
| 379 |
self.IndexedOpacities = [] |
| 380 |
numCategories = len(self.Annotations) // 2 |
| 381 |
for ii in range(0, numCategories): |
| 382 |
self.IndexedOpacities.append(1.0) |
| 383 |
if PhactoriDbg(): |
| 384 |
myDebugPrint3("self.Annotations:\n" + str(self.Annotations) + "\n") |
| 385 |
myDebugPrint3("self.IndexedColors:\n" + str(self.IndexedColors) + "\n") |
| 386 |
myDebugPrint3("self.IndexedOpacities:\n" + str(self.IndexedOpacities) + "\n") |
| 387 |
variableLookUpTable.InterpretValuesAsCategories = 1 |
| 388 |
variableLookUpTable.AnnotationsInitialized = 1 |
| 389 |
variableLookUpTable.Annotations = self.Annotations |
| 390 |
variableLookUpTable.IndexedColors = self.IndexedColors |
| 391 |
variableLookUpTable.IndexedOpacities = self.IndexedOpacities |
| 392 |
if PhactoriDbg(): |
| 393 |
myDebugPrint3("SetUpForInterpretValuesAsCateories returning\n") |
| 394 |
#phactori_combine_to_single_python_file_subpiece_end_1 |