-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Move PVMergeBlocksEnhanced plugin and create the VTK filter
#129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
06eca95
8651018
fbe6bf2
b9855ce
21d8b7d
54c70eb
2de326a
3d20b99
22828f7
29a038b
3d153cd
af3990e
d9f289d
14bcb85
05a1c17
df24923
f771dd6
4d9c3d0
b3112f9
b1b681b
682f50e
7da34e9
43dd2d1
396ed11
e0ab6fc
ba9e2b1
f8d2b1a
b8b1481
cd48656
b839532
262932a
b98b49e
073f4cd
3d0c57c
3a7b061
907b2d6
24a8d27
f9b5cf0
df766cd
d572700
18b13f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. | ||
| # SPDX-FileContributor: Paloma Martinez | ||
| # ruff: noqa: E402 # disable Module level import not at top of file | ||
| import logging | ||
|
|
||
| from typing import Union | ||
| from typing_extensions import Self | ||
|
|
||
| from geos.utils.Logger import Logger, getLogger | ||
| from geos.mesh.utils.multiblockModifiers import mergeBlocks | ||
|
|
||
| from vtkmodules.vtkCommonDataModel import ( | ||
| vtkCompositeDataSet, | ||
| vtkMultiBlockDataSet, | ||
| vtkUnstructuredGrid, | ||
| ) | ||
|
|
||
| __doc__ = """ | ||
| Merge Blocks Keeping Partial Attributes is a filter that allows to merge blocks from a multiblock dataset while keeping partial attributes. | ||
| Input is a vtkMultiBlockDataSet and output is a vtkUnstructuredGrid. | ||
| .. Note:: | ||
| - You may encounter issues if two datasets of the input multiblock dataset have duplicated cell IDs. | ||
| - Partial attributes are filled with default values depending on their types. | ||
| - 0 for uint data. | ||
| - -1 for int data. | ||
| - nan for float data. | ||
| To use it: | ||
| .. code-block:: python | ||
| from geos.mesh.processing.MergeBlockEnhanced import MergeBlockEnhanced | ||
| # Define filter inputs | ||
| multiblockdataset: vtkMultiblockDataSet | ||
| speHandler: bool # optional | ||
| # Instantiate the filter | ||
| filter: MergeBlockEnhanced = MergeBlockEnhanced( multiblockdataset, speHandler ) | ||
| # Use your own handler (if speHandler is True) | ||
| yourHandler: logging.Handler | ||
| filter.setLoggerHandler( yourHandler ) | ||
| # Do calculations | ||
| filter.applyFilter() | ||
| # Get the merged mesh | ||
| filter.getOutput() | ||
| """ | ||
|
|
||
| loggerTitle: str = "Merge Block Enhanced" | ||
|
|
||
|
|
||
| class MergeBlockEnhanced: | ||
|
|
||
| def __init__( | ||
| self: Self, | ||
| inputMesh: vtkMultiBlockDataSet, | ||
| speHandler: bool = False, | ||
| ) -> None: | ||
| """Merge a multiblock dataset and keep the partial attributes in the output mesh. | ||
| Partial attributes are filled with default values depending on the data type such that: | ||
| - 0 for uint data. | ||
| - -1 for int data. | ||
| - nan for float data. | ||
| Args: | ||
| inputMesh (vtkMultiBlockDataSet): The input multiblock dataset to merge. | ||
| speHandler (bool, optional) : True to use a specific handler, False to use the internal handler. | ||
| Defaults to False. | ||
| """ | ||
| self.inputMesh: vtkMultiBlockDataSet = inputMesh | ||
| self.outputMesh: vtkUnstructuredGrid = vtkUnstructuredGrid() | ||
|
|
||
| # Logger | ||
| self.logger: Logger | ||
| if not speHandler: | ||
| self.logger = getLogger( loggerTitle, True ) | ||
| else: | ||
| self.logger = logging.getLogger( loggerTitle ) | ||
| self.logger.setLevel( logging.INFO ) | ||
|
|
||
| def setLoggerHandler( self: Self, handler: logging.Handler ) -> None: | ||
| """Set a specific handler for the filter logger. | ||
| In this filter 4 log levels are use, .info, .error, .warning and .critical, be sure to have at least the same 4 levels. | ||
| Args: | ||
| handler (logging.Handler): The handler to add. | ||
| """ | ||
| if not self.logger.hasHandlers(): | ||
| self.logger.addHandler( handler ) | ||
| else: | ||
| self.logger.warning( | ||
| "The logger already has an handler, to use yours set the argument 'speHandler' to True during the filter initialization." | ||
| ) | ||
|
|
||
| def applyFilter( self: Self ) -> bool: | ||
| """Merge the blocks of a multiblock dataset mesh. | ||
jafranc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Returns: | ||
| bool: True if the blocks were successfully merged, False otherwise. | ||
| """ | ||
| self.logger.info( f"Applying filter { self.logger.name }." ) | ||
|
|
||
| if not isinstance( self.inputMesh, vtkCompositeDataSet ) or not isinstance( self.inputMesh, | ||
| vtkMultiBlockDataSet ): | ||
| self.logger.error( | ||
| f"Expected a vtkMultiblockdataset or vtkCompositeDataSet, Got a {type(self.inputMesh)} \n The filter { self.logger.name } failed." | ||
| ) | ||
| return False | ||
|
|
||
| success: bool | ||
| outputMesh: vtkUnstructuredGrid | ||
| success, outputMesh = mergeBlocks( self.inputMesh, True, self.logger ) | ||
|
||
|
|
||
| if not success: | ||
| self.logger.error( f"The filter {self.logger.name} failed." ) | ||
| return False | ||
|
|
||
| else: | ||
| self.logger.info( f"The filter { self.logger.name } succeeded." ) | ||
| self.outputMesh = outputMesh | ||
| return True | ||
|
|
||
| def getOutput( self: Self ) -> vtkUnstructuredGrid: | ||
| """Get the merged mesh. | ||
| Returns: | ||
| vtkUnstructuredGrid: The merged mesh. | ||
| """ | ||
| return self.outputMesh | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,45 +1,70 @@ | ||||||
| # SPDX-License-Identifier: Apache-2.0 | ||||||
| # SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. | ||||||
| # SPDX-FileContributor: Martin Lemay | ||||||
| # SPDX-FileContributor: Martin Lemay, Paloma Martinez | ||||||
| from typing import Union | ||||||
| from vtkmodules.vtkCommonDataModel import ( vtkCompositeDataSet, vtkDataObjectTreeIterator, vtkMultiBlockDataSet, | ||||||
| vtkUnstructuredGrid ) | ||||||
| from vtkmodules.vtkFiltersCore import vtkAppendDataSets | ||||||
| from geos.mesh.utils.arrayModifiers import fillAllPartialAttributes | ||||||
| from geos.utils.Logger import getLogger, Logger | ||||||
|
||||||
| from geos.utils.Logger import getLogger, Logger | |
| from geos.utils.Logger import ( getLogger, Logger ) |
jafranc marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe raise a user define error there to avoid the Tuple return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @jafranc here, this function should only return a vtkUnstructuredGrid and raise errors when failure.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0"?> | ||
| <VTKFile type="vtkMultiBlockDataSet" version="1.0"> | ||
| <vtkMultiBlockDataSet> | ||
| <Block name="cartesianMesh"> | ||
| <Block name="Level0"> | ||
| <Block name="CellElementRegion"> | ||
| <Block name="reservoir"> | ||
| <DataSet name="rank_0" file="simpleReservoirViz_small_000478/cartesianMesh/Level0/reservoir/rank_0.vtu" /> | ||
| <DataSet name="rank_1" file="simpleReservoirViz_small_000478/cartesianMesh/Level0/reservoir/rank_1.vtu" /> | ||
| </Block> | ||
| </Block> | ||
| <Block name="WellElementRegion"> | ||
| <Block name="wellRegion"> | ||
| <DataSet name="rank_1" file="simpleReservoirViz_small_000478/cartesianMesh/Level0/wellRegion/rank_1.vtu" /> | ||
| </Block> | ||
| </Block> | ||
| </Block> | ||
| </Block> | ||
| </vtkMultiBlockDataSet> | ||
| </VTKFile> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep parenthesis when doing multiple import other than standard