Skip to content

Commit 1d5ac85

Browse files
refactor: Refactor and improve error handling in arrayModifiers (#197)
1 parent 082ebec commit 1d5ac85

11 files changed

Lines changed: 708 additions & 339 deletions

File tree

geos-mesh/src/geos/mesh/utils/arrayModifiers.py

Lines changed: 320 additions & 230 deletions
Large diffs are not rendered by default.

geos-mesh/src/geos/mesh/utils/multiblockModifiers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def mergeBlocks(
6464
vtkErrorLogger.addFilter( RegexExceptionFilter() ) # will raise VTKError if captured VTK Error
6565

6666
# Fill the partial attributes with default values to keep them during the merge.
67-
if keepPartialAttributes and not fillAllPartialAttributes( inputMesh, logger ):
68-
raise ValueError( "Failed to fill partial attributes. Merging without keeping partial attributes." )
67+
if keepPartialAttributes:
68+
fillAllPartialAttributes( inputMesh, logger )
6969

7070
outputMesh: vtkUnstructuredGrid
7171

geos-mesh/tests/test_arrayModifiers.py

Lines changed: 313 additions & 17 deletions
Large diffs are not rendered by default.

geos-processing/src/geos/processing/generic_processing_tools/AttributeMapping.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ def applyFilter( self: Self ) -> None:
181181
raise ValueError( f"The two meshes do not have any shared { self.piece.value }." )
182182

183183
for attributeName in self.attributeNames:
184-
# TODO:: Modify arrayModifiers function to raise error.
185-
if not transferAttributeWithElementMap( self.meshFrom, self.meshTo, self.ElementMap, attributeName,
186-
self.piece, self.logger ):
187-
raise ValueError( f"Fail to transfer the attribute { attributeName }." )
184+
transferAttributeWithElementMap( self.meshFrom, self.meshTo, self.ElementMap, attributeName, self.piece,
185+
self.logger )
188186

189187
# Log the output message.
190188
self._logOutputMessage()

geos-processing/src/geos/processing/generic_processing_tools/CreateConstantAttributePerRegion.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def applyFilter( self: Self ) -> None:
155155
"""Create a constant attribute per region in the mesh.
156156
157157
Raises:
158-
ValueError: Errors with the input value for the region index or errors during the creation of the new attribute.
158+
ValueError: Errors with the input value for the region index.
159159
AttributeError: Errors with the attribute of the mesh.
160160
"""
161161
self.logger.info( f"Apply filter { self.logger.name }." )
@@ -203,14 +203,12 @@ def applyFilter( self: Self ) -> None:
203203
self.logger.warning(
204204
f"The region indexes entered are not in the region attribute { self.regionName }." )
205205

206-
if not createConstantAttributeMultiBlock( self.mesh,
207-
self.defaultValue,
208-
self.newAttributeName,
209-
componentNames=self.componentNames,
210-
piece=self.piece,
211-
logger=self.logger ):
212-
raise ValueError(
213-
f"Something went wrong with the creation of the attribute { self.newAttributeName }." )
206+
createConstantAttributeMultiBlock( self.mesh,
207+
self.defaultValue,
208+
self.newAttributeName,
209+
componentNames=self.componentNames,
210+
piece=self.piece,
211+
logger=self.logger )
214212

215213
else:
216214
if len( invalidIndexes ) > 0:
@@ -224,14 +222,12 @@ def applyFilter( self: Self ) -> None:
224222

225223
regionArray = getArrayInObject( dataSet, self.regionName, self.piece )
226224
newArray = self._createArrayFromRegionArrayWithValueMap( regionArray )
227-
if not createAttribute( dataSet,
228-
newArray,
229-
self.newAttributeName,
230-
componentNames=self.componentNames,
231-
piece=self.piece,
232-
logger=self.logger ):
233-
raise ValueError(
234-
f"Something went wrong with the creation of the attribute { self.newAttributeName }." )
225+
createAttribute( dataSet,
226+
newArray,
227+
self.newAttributeName,
228+
componentNames=self.componentNames,
229+
piece=self.piece,
230+
logger=self.logger )
235231

236232
else:
237233
validIndexes, invalidIndexes = checkValidValuesInDataSet( self.mesh, self.regionName, listIndexes,
@@ -243,14 +239,12 @@ def applyFilter( self: Self ) -> None:
243239
self.logger.warning(
244240
f"The region indexes entered are not in the region attribute { self.regionName }." )
245241

246-
if not createConstantAttributeDataSet( self.mesh,
247-
self.defaultValue,
248-
self.newAttributeName,
249-
componentNames=self.componentNames,
250-
piece=self.piece,
251-
logger=self.logger ):
252-
raise ValueError(
253-
f"Something went wrong with the creation of the attribute { self.newAttributeName }." )
242+
createConstantAttributeDataSet( self.mesh,
243+
self.defaultValue,
244+
self.newAttributeName,
245+
componentNames=self.componentNames,
246+
piece=self.piece,
247+
logger=self.logger )
254248

255249
else:
256250
if len( invalidIndexes ) > 0:
@@ -259,14 +253,12 @@ def applyFilter( self: Self ) -> None:
259253

260254
regionArray = getArrayInObject( self.mesh, self.regionName, self.piece )
261255
newArray = self._createArrayFromRegionArrayWithValueMap( regionArray )
262-
if not createAttribute( self.mesh,
263-
newArray,
264-
self.newAttributeName,
265-
componentNames=self.componentNames,
266-
piece=self.piece,
267-
logger=self.logger ):
268-
raise ValueError(
269-
f"Something went wrong with the creation of the attribute { self.newAttributeName }." )
256+
createAttribute( self.mesh,
257+
newArray,
258+
self.newAttributeName,
259+
componentNames=self.componentNames,
260+
piece=self.piece,
261+
logger=self.logger )
270262

271263
# Log the output message.
272264
self._logOutputMessage( validIndexes )

geos-processing/src/geos/processing/generic_processing_tools/FillPartialArrays.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ def applyFilter( self: Self ) -> None:
131131
f"There is two attribute named { attributeName }, one on points and the other on cells. The attribute name must be unique."
132132
)
133133

134-
if not fillPartialAttributes( self.multiBlockDataSet,
135-
attributeName,
136-
piece=piece,
137-
listValues=self.dictAttributesValues[ attributeName ],
138-
logger=self.logger ):
139-
raise ValueError( "Something went wrong with the filling of partial attributes" )
134+
fillPartialAttributes( self.multiBlockDataSet,
135+
attributeName,
136+
piece=piece,
137+
listValues=self.dictAttributesValues[ attributeName ],
138+
logger=self.logger )
140139

141140
self.logger.info( f"The filter { self.logger.name } succeed." )
142141

geos-processing/src/geos/processing/post_processing/GeosBlockMerge.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,10 @@ def applyFilter( self: Self ) -> None:
164164
logger=self.logger )
165165

166166
# Create index attribute keeping the index in initial mesh
167-
if not createConstantAttribute( volumeMesh, [ blockIndex ],
168-
PostProcessingOutputsEnum.BLOCK_INDEX.attributeName,
169-
piece=Piece.CELLS,
170-
logger=self.logger ):
171-
raise ValueError(
172-
f"Something went wrong during the creation of the attribute { PostProcessingOutputsEnum.BLOCK_INDEX.attributeName }."
173-
)
167+
createConstantAttribute( volumeMesh, [ blockIndex ],
168+
PostProcessingOutputsEnum.BLOCK_INDEX.attributeName,
169+
piece=Piece.CELLS,
170+
logger=self.logger )
174171

175172
# Rename attributes
176173
self.renameAttributes( volumeMesh )
@@ -208,9 +205,9 @@ def renameAttributes(
208205
if suffix == "_density":
209206
for phaseName in self.phaseNameDict[ PhaseTypeEnum.ROCK.type ]:
210207
if phaseName in attributeName:
211-
renameAttribute( mesh, attributeName, newName, piece=Piece.CELLS )
208+
renameAttribute( mesh, attributeName, newName, Piece.CELLS, logger=self.logger )
212209
else:
213-
renameAttribute( mesh, attributeName, newName, piece=Piece.CELLS )
210+
renameAttribute( mesh, attributeName, newName, Piece.CELLS, logger=self.logger )
214211

215212
return
216213

geos-processing/src/geos/processing/post_processing/SurfaceGeomechanics.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,14 @@ def convertAttributesFromLocalToXYZBasis( self: Self ) -> None:
292292
arrayXYZ: npt.NDArray[ np.float64 ] = self.__computeXYZCoordinates( localArray )
293293

294294
# Create converted attribute array in dataset
295-
if createAttribute( self.outputMesh,
296-
arrayXYZ,
297-
attrNameXYZ,
298-
ComponentNameEnum.XYZ.value,
299-
piece=self.attributePiece,
300-
logger=self.logger ):
301-
self.logger.info( f"Attribute {attrNameXYZ} added to the output mesh." )
302-
self.newAttributeNames.add( attrNameXYZ )
303-
else:
304-
raise ValueError( f"Something went wrong during the creation of the attribute { attrNameXYZ }." )
295+
createAttribute( self.outputMesh,
296+
arrayXYZ,
297+
attrNameXYZ,
298+
ComponentNameEnum.XYZ.value,
299+
piece=self.attributePiece,
300+
logger=self.logger )
301+
self.logger.info( f"Attribute {attrNameXYZ} added to the output mesh." )
302+
self.newAttributeNames.add( attrNameXYZ )
305303

306304
return
307305

@@ -387,13 +385,13 @@ def computeShearCapacityUtilization( self: Self ) -> None:
387385
self.frictionAngle )
388386

389387
# Create attribute
390-
if not createAttribute(
391-
self.outputMesh, scuAttribute, SCUAttributeName, (), self.attributePiece, logger=self.logger ):
392-
self.logger.error( f"Failed to create attribute {SCUAttributeName}." )
393-
raise ValueError( f"Failed to create attribute {SCUAttributeName}." )
394-
else:
395-
self.logger.info( "SCU computed and added to the output mesh." )
396-
self.newAttributeNames.add( SCUAttributeName )
388+
createAttribute( self.outputMesh,
389+
scuAttribute,
390+
SCUAttributeName, (),
391+
self.attributePiece,
392+
logger=self.logger )
393+
self.logger.info( "SCU computed and added to the output mesh." )
394+
self.newAttributeNames.add( SCUAttributeName )
397395

398396
return
399397

geos-processing/tests/test_CreateConstantAttributePerRegion.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,55 +103,55 @@ def test_CreateConstantAttributePerRegion(
103103

104104

105105
@pytest.mark.parametrize(
106-
"meshType, regionName",
106+
"meshType, newAttributeName, regionName",
107107
[
108-
( "dataset", "PERM" ), # Region attribute has too many components
109-
( "multiblock", "FAULT" ), # Region attribute is partial.
108+
( "dataset", "newAttribute", "PERM" ), # Region attribute has too many components
109+
( "multiblock", "newAttribute", "FAULT" ), # Region attribute is partial.
110+
( "dataset", "PERM", "FAULT" ), # The attribute name already exist in the mesh.
110111
] )
111112
def test_CreateConstantAttributePerRegionRaisesAttributeError(
112113
dataSetTest: Union[ vtkMultiBlockDataSet, vtkDataSet ],
113114
meshType: str,
115+
newAttributeName: str,
114116
regionName: str,
115117
) -> None:
116-
"""Test tes fails of CreateConstantAttributePerRegion with attributes issues."""
118+
"""Test the fails of CreateConstantAttributePerRegion with attributes issues."""
117119
mesh: Union[ vtkMultiBlockDataSet, vtkDataSet ] = dataSetTest( meshType )
118120

119121
createConstantAttributePerRegionFilter: CreateConstantAttributePerRegion = CreateConstantAttributePerRegion(
120122
mesh,
121123
regionName,
122124
{},
123-
"newAttribute",
125+
newAttributeName,
124126
)
125127

126128
with pytest.raises( AttributeError ):
127129
createConstantAttributePerRegionFilter.applyFilter()
128130

129131

130132
@pytest.mark.parametrize(
131-
"newAttributeName, dictRegionValues, componentNames",
133+
"dictRegionValues, componentNames",
132134
[
133-
( "newAttribute", {
135+
( {
134136
0: [ 0 ],
135137
100: [ 1, 1 ],
136138
}, () ), # Number of value inconsistent.
137-
( "newAttribute", {
139+
( {
138140
0: [ 0, 0 ],
139141
100: [ 1, 1 ],
140142
}, () ), # More values than components.
141-
( "newAttribute", {
143+
( {
142144
0: [ 0 ],
143145
100: [ 1 ],
144146
}, ( "X", "Y" ) ), # More components than value.
145-
( "PERM", {}, () ), # The attribute name already exist on the mesh on the same piece.
146147
] )
147148
def test_CreateConstantAttributePerRegionRaisesValueError(
148149
dataSetTest: vtkDataSet,
149-
newAttributeName: str,
150150
dictRegionValues: dict[ Any, Any ],
151151
componentNames: tuple[ str, ...],
152152
) -> None:
153153
"""Test the fails of CreateConstantAttributePerRegion with inputs value issues."""
154-
mesh: vtkDataSet = dataSetTest( "dataset" )
154+
mesh: vtkDataSet = dataSetTest( 'dataset' )
155155
nbComponents: int = len( componentNames )
156156
if nbComponents == 0: # If the attribute has one component, the component has no name.
157157
nbComponents += 1
@@ -160,7 +160,7 @@ def test_CreateConstantAttributePerRegionRaisesValueError(
160160
mesh,
161161
"FAULT",
162162
dictRegionValues,
163-
newAttributeName,
163+
"newAttribute",
164164
nbComponents=nbComponents,
165165
componentNames=componentNames,
166166
)

geos-pv/src/geos/pv/plugins/post_processing/PVGeosBlockExtractAndMerge.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ def RequestData(
307307

308308
# Create elementCenter attribute in the volume mesh if needed
309309
cellCenterAttributeName: str = GeosMeshOutputsEnum.ELEMENT_CENTER.attributeName
310-
createCellCenterAttribute( outputCells, cellCenterAttributeName )
310+
if cellCenterAttributeName not in meshAttributes:
311+
createCellCenterAttribute( outputCells, cellCenterAttributeName, logger=self.logger )
311312

312313
# Stop the time step iteration
313314
request.Remove( executive.CONTINUE_EXECUTING() )

0 commit comments

Comments
 (0)