-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPVMeshQualityEnhanced.py
More file actions
344 lines (289 loc) · 17 KB
/
PVMeshQualityEnhanced.py
File metadata and controls
344 lines (289 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Martin Lemay, Paloma Martinez
# ruff: noqa: E402 # disable Module level import not at top of file
import logging
import sys
from pathlib import Path
from typing_extensions import Self, Optional
from paraview.util.vtkAlgorithm import VTKPythonAlgorithmBase, smdomain, smproperty # type: ignore[import-not-found]
# source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/util/vtkAlgorithm.py
from paraview.detail.loghandler import VTKHandler # type: ignore[import-not-found]
# source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py
from vtkmodules.vtkCommonCore import vtkDataArraySelection
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid
# update sys.path to load all GEOS Python Package dependencies
geos_pv_path: Path = Path( __file__ ).parent.parent.parent.parent.parent.parent
sys.path.insert( 0, str( geos_pv_path / "src" ) )
from geos.pv.utils.config import update_paths
update_paths()
from geos.mesh.model.QualityMetricSummary import QualityMetricSummary
from geos.processing.pre_processing.MeshQualityEnhanced import MeshQualityEnhanced
from geos.mesh.stats.meshQualityMetricHelpers import ( getQualityMetricsOther, getQualityMeasureNameFromIndex,
getQualityMeasureIndexFromName, getQuadQualityMeasure,
getTriangleQualityMeasure, getCommonPolygonQualityMeasure,
getTetQualityMeasure, getPyramidQualityMeasure,
getWedgeQualityMeasure, getHexQualityMeasure,
getCommonPolyhedraQualityMeasure )
from geos.pv.utils.checkboxFunction import createModifiedCallback # type: ignore[attr-defined]
from geos.pv.utils.paraviewTreatments import getArrayChoices
from geos.pv.utils.details import ( SISOFilter, FilterCategory )
__doc__ = f"""
The ``Mesh Quality Enhanced`` filter computes requested mesh quality metrics on meshes. Both surfaces and volumic metrics can be computed with this plugin.
The output stats are available as attributes of the filter output mesh and can be exported from spreadsheet.
A summary figure can be exported by ticking the box and choosing a filename before applying the filter.
.. WARNING::
Due to conflicts with python packages versions in Paraview, the *incident vertex count* metric is not activated as default.
To use it:
* Load the plugin in Paraview: Tools > Manage Plugins ... > Load New ... > .../geosPythonPackages/geos-pv/src/geos/pv/plugins/qc/PVMeshQualityEnhanced
* Select the input mesh to process
* Select the filter: Filters > { FilterCategory.QC.value } > Mesh Quality Enhanced
* Select the metrics to compute
* Choose a filename for export if needed
* Apply
.. IMPORTANT::
Please refer to the `Verdict Manual <https://visit-sphinx-github-user-manual.readthedocs.io/en/v3.4.0/_downloads/9d944264b44b411aeb4a867a1c9b1ed5/VerdictManual-revA.pdf>`_ for metrics and range definitions.
"""
@SISOFilter( category=FilterCategory.QC,
decoratedLabel="Mesh Quality Enhanced",
decoratedType="vtkUnstructuredGrid" )
class PVMeshQualityEnhanced( VTKPythonAlgorithmBase ):
def __init__( self: Self ) -> None:
"""Merge collocated points."""
self._filename: Optional[ str ] = None
self._saveToFile: bool = True
self._blockIndex: int = 0
# Used to concatenate results if vtkMultiBlockDataSet
self._metricsAll: list[ float ] = []
# Surface metrics
self._commonMeshQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._commonCellSurfaceQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._commonCellVolumeQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._triangleQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._quadsQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
# Volumic metrics
self._tetQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._PyrQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._WedgeQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._HexQualityMetric: vtkDataArraySelection = vtkDataArraySelection()
self._initQualityMetricSelection()
def _initQualityMetricSelection( self: Self ) -> None:
"""Initialize all metrics selection."""
self.__initSurfaceQualityMetricSelection()
self.__initVolumeQualityMetricSelection()
@smproperty.dataarrayselection( name="CommonCellSurfaceQualityMetric" )
def a01SetCommonSurfaceMetrics( self: Self ) -> vtkDataArraySelection:
"""Set polygon quality metrics selection."""
return self._commonCellSurfaceQualityMetric
@smproperty.dataarrayselection( name="CommonCellVolumeQualityMetric" )
def a02SetCommonVolumeMetrics( self: Self ) -> vtkDataArraySelection:
"""Set polyhedra quality metrics selection."""
return self._commonCellVolumeQualityMetric
@smproperty.dataarrayselection( name="TriangleSpecificQualityMetric" )
def a03SetTriangleMetrics( self: Self ) -> vtkDataArraySelection:
"""Set triangle quality metrics selection."""
return self._triangleQualityMetric
@smproperty.dataarrayselection( name="QuadSpecificQualityMetric" )
def a04sSetQuadMetrics( self: Self ) -> vtkDataArraySelection:
"""Set quad quality metrics selection."""
return self._quadsQualityMetric
@smproperty.dataarrayselection( name="TetrahedronSpecificQualityMetric" )
def a05SetTetMetrics( self: Self ) -> vtkDataArraySelection:
"""Set tetra quality metrics selection."""
return self._tetQualityMetric
@smproperty.dataarrayselection( name="PyramidSpecificQualityMetric" )
def a06sSetPyrMetrics( self: Self ) -> vtkDataArraySelection:
"""Set Pyramid quality metrics selection."""
return self._PyrQualityMetric
@smproperty.dataarrayselection( name="WedgeSpecificQualityMetric" )
def a07sSetWedgeMetrics( self: Self ) -> vtkDataArraySelection:
"""Set Wedge quality metrics selection."""
return self._WedgeQualityMetric
@smproperty.dataarrayselection( name="HexahedronSpecificQualityMetric" )
def a08sSetHexMetrics( self: Self ) -> vtkDataArraySelection:
"""Set Hexahedron quality metrics selection."""
return self._HexQualityMetric
@smproperty.dataarrayselection( name="OtherMeshQualityMetric" )
def a09SetOtherMeshMetrics( self: Self ) -> vtkDataArraySelection:
"""Set other mesh quality metrics selection."""
return self._commonMeshQualityMetric
@smproperty.intvector(
name="SetSaveToFile",
label="Save to file",
default_values=0,
panel_visibility="default",
)
@smdomain.xml( """
<BooleanDomain name="SetSaveToFile"/>
<Documentation>
Specify if mesh statistics are dumped into a file.
</Documentation>
""" )
def b01SetSaveToFile( self: Self, saveToFile: bool ) -> None:
"""Setter to save the stats into a file.
Args:
saveToFile (bool): if True, a file will be saved.
"""
if self._saveToFile != saveToFile:
self._saveToFile = saveToFile
PVMeshQualityEnhanced.Modified( self )
@smproperty.stringvector( name="FilePath", label="File Path" )
@smdomain.xml( """
<FileListDomain name="files" />
<Documentation>Output file path.</Documentation>
<Hints>
<FileChooser extensions="png" file_description="Output file." />
<AcceptAnyFile/>
</Hints>
""" )
def b02SetFileName( self: Self, fname: str ) -> None:
"""Specify filename for the filter to write.
Args:
fname (str): file path
"""
if self._filename != fname:
self._filename = fname
PVMeshQualityEnhanced.Modified( self )
@smproperty.xml( """
<PropertyGroup
panel_visibility="advanced">
<Property name="FilePath"/>
<Hints>
<PropertyWidgetDecorator type="GenericDecorator"
mode="visibility"
property="SetSaveToFile"
value="1" />
</Hints>
</PropertyGroup>
""" )
def b03GroupAdvancedOutputParameters( self: Self ) -> None:
"""Organize groups."""
PVMeshQualityEnhanced.Modified( self )
def Modified( self: Self ) -> None:
"""Overload Modified method to reset _blockIndex."""
self._blockIndex = 0
VTKPythonAlgorithmBase.Modified( self )
def _getQualityMetricsToUse( self: Self, selection: vtkDataArraySelection ) -> set[ int ]:
"""Get mesh quality metric indexes from user selection.
Returns:
list[int]: List of quality metric indexes
"""
metricsNames: set[ str ] = getArrayChoices( selection )
return { getQualityMeasureIndexFromName( name ) for name in metricsNames }
def ApplyFilter( self, inputMesh: vtkUnstructuredGrid, outputMesh: vtkUnstructuredGrid ) -> None:
"""Is applying MeshQualityEnhanced to the input Mesh.
Args:
inputMesh : A mesh to transform.
outputMesh : A mesh transformed.
"""
triangleMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellSurfaceQualityMetric ).union(
self._getQualityMetricsToUse( self._triangleQualityMetric ) )
quadMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellSurfaceQualityMetric ).union(
self._getQualityMetricsToUse( self._quadsQualityMetric ) )
tetraMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellVolumeQualityMetric ).union(
self._getQualityMetricsToUse( self._tetQualityMetric ) )
pyrMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellVolumeQualityMetric ).union(
self._getQualityMetricsToUse( self._PyrQualityMetric ) )
wedgeMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellVolumeQualityMetric ).union(
self._getQualityMetricsToUse( self._WedgeQualityMetric ) )
hexaMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonCellVolumeQualityMetric ).union(
self._getQualityMetricsToUse( self._HexQualityMetric ) )
otherMetrics: set[ int ] = self._getQualityMetricsToUse( self._commonMeshQualityMetric )
meshQualityEnhancedFilter: MeshQualityEnhanced = MeshQualityEnhanced( inputMesh, True )
if len( meshQualityEnhancedFilter.logger.handlers ) == 0:
meshQualityEnhancedFilter.setLoggerHandler( VTKHandler() )
meshQualityEnhancedFilter.SetCellQualityMetrics( triangleMetrics=triangleMetrics,
quadMetrics=quadMetrics,
tetraMetrics=tetraMetrics,
pyramidMetrics=pyrMetrics,
wedgeMetrics=wedgeMetrics,
hexaMetrics=hexaMetrics )
meshQualityEnhancedFilter.SetOtherMeshQualityMetrics( otherMetrics )
try:
meshQualityEnhancedFilter.applyFilter()
outputMesh.ShallowCopy( meshQualityEnhancedFilter.getOutput() )
# save to file if asked
if self._saveToFile:
stats: QualityMetricSummary = meshQualityEnhancedFilter.GetQualityMetricSummary()
logger: logging.Logger = meshQualityEnhancedFilter.logger
self.saveFile( stats, logger )
self._blockIndex += 1
except ( ValueError, IndexError, TypeError, AttributeError ) as e:
meshQualityEnhancedFilter.logger.error(
f"The filter { meshQualityEnhancedFilter.logger.name } failed due to:\n{ e }" )
except Exception as e:
mess: str = f"The filter { meshQualityEnhancedFilter.logger.name } failed due to:\n{ e }"
meshQualityEnhancedFilter.logger.critical( mess, exc_info=True )
return
def saveFile(
self: Self,
stats: QualityMetricSummary,
logger: logging.Logger,
) -> None:
"""Export mesh quality metric summary file."""
try:
if self._filename is None:
raise AttributeError( "Mesh quality summary report file path is undefined." )
# add index for multiblock meshes
index: int = self._filename.rfind( '.' )
filename: str = self._filename[ :index ] + f"{ self._blockIndex }" + self._filename[ index: ]
fig = stats.plotSummaryFigure()
fig.savefig( filename, dpi=150 )
logger.info( f"File { filename } was successfully written." )
except Exception as e:
logger.error( f"Error while exporting the file due to:\n{ e }" )
def __initVolumeQualityMetricSelection( self: Self ) -> None:
"""Initialize the volumic metrics selection."""
self._commonCellVolumeQualityMetric.RemoveAllArrays()
self._commonCellVolumeQualityMetric.AddObserver(
"ModifiedEvent", # type: ignore[arg-type]
createModifiedCallback( self ) )
commonCellMetrics: set[ int ] = getCommonPolyhedraQualityMeasure()
for measure in commonCellMetrics:
self._commonCellVolumeQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._tetQualityMetric.RemoveAllArrays()
self._tetQualityMetric.AddObserver( "ModifiedEvent", createModifiedCallback( self ) ) # type: ignore[arg-type]
for measure in getTetQualityMeasure().difference( commonCellMetrics ):
self._tetQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._PyrQualityMetric.RemoveAllArrays()
self._PyrQualityMetric.AddObserver( "ModifiedEvent", createModifiedCallback( self ) ) # type: ignore[arg-type]
for measure in getPyramidQualityMeasure().difference( commonCellMetrics ):
self._PyrQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._WedgeQualityMetric.RemoveAllArrays()
self._WedgeQualityMetric.AddObserver(
"ModifiedEvent", # type: ignore[arg-type]
createModifiedCallback( self ) )
for measure in getWedgeQualityMeasure().difference( commonCellMetrics ):
self._WedgeQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._HexQualityMetric.RemoveAllArrays()
self._HexQualityMetric.AddObserver( "ModifiedEvent", createModifiedCallback( self ) ) # type: ignore[arg-type]
for measure in getHexQualityMeasure().difference( commonCellMetrics ):
self._HexQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
otherMetrics: set[ int ] = getQualityMetricsOther()
for measure in otherMetrics:
self._commonMeshQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
def __initSurfaceQualityMetricSelection( self: Self ) -> None:
"""Initialize the surface metrics selection."""
self._commonCellSurfaceQualityMetric.RemoveAllArrays()
self._commonCellSurfaceQualityMetric.AddObserver(
"ModifiedEvent", # type: ignore[arg-type]
createModifiedCallback( self ) )
commonCellMetrics: set[ int ] = getCommonPolygonQualityMeasure()
for measure in commonCellMetrics:
self._commonCellSurfaceQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._triangleQualityMetric.RemoveAllArrays()
self._triangleQualityMetric.AddObserver(
"ModifiedEvent", # type: ignore[arg-type]
createModifiedCallback( self ) )
for measure in getTriangleQualityMeasure().difference( commonCellMetrics ):
self._triangleQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
self._quadsQualityMetric.RemoveAllArrays()
self._quadsQualityMetric.AddObserver(
"ModifiedEvent", # type: ignore[arg-type]
createModifiedCallback( self ) )
for measure in getQuadQualityMeasure().difference( commonCellMetrics ):
self._quadsQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ) )
otherMetrics: set[ int ] = getQualityMetricsOther()
for measure in otherMetrics:
# TODO: fix issue with incident vertex count metrics
self._commonMeshQualityMetric.AddArray( getQualityMeasureNameFromIndex( measure ), False )