|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. |
| 3 | +# SPDX-FileContributor: Paloma Martinez |
| 4 | +# ruff: noqa: E402 # disable Module level import not at top of file |
| 5 | +import logging |
| 6 | + |
| 7 | +from typing_extensions import Self |
| 8 | + |
| 9 | +from geos.utils.Logger import Logger, getLogger |
| 10 | +from geos.mesh.utils.multiblockModifiers import mergeBlocks |
| 11 | + |
| 12 | +from vtkmodules.vtkCommonDataModel import ( |
| 13 | + vtkMultiBlockDataSet, |
| 14 | + vtkUnstructuredGrid, |
| 15 | +) |
| 16 | + |
| 17 | +__doc__ = """ |
| 18 | +Merge Blocks Keeping Partial Attributes is a filter that allows to merge blocks from a multiblock dataset while keeping partial attributes. |
| 19 | +
|
| 20 | +Input is a vtkMultiBlockDataSet and output is a vtkUnstructuredGrid. |
| 21 | +
|
| 22 | +.. Note:: |
| 23 | + - You may encounter issues if two datasets of the input multiblock dataset have duplicated cell IDs. |
| 24 | + - Partial attributes are filled with default values depending on their types. |
| 25 | + - 0 for uint data. |
| 26 | + - -1 for int data. |
| 27 | + - nan for float data. |
| 28 | +
|
| 29 | +
|
| 30 | +To use it: |
| 31 | +
|
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + from geos.mesh.processing.MergeBlockEnhanced import MergeBlockEnhanced |
| 35 | + import logging |
| 36 | + from geos.utils.Errors import VTKError |
| 37 | +
|
| 38 | + # Define filter inputs |
| 39 | + multiblockdataset: vtkMultiblockDataSet |
| 40 | + speHandler: bool # optional |
| 41 | +
|
| 42 | + # Instantiate the filter |
| 43 | + filter: MergeBlockEnhanced = MergeBlockEnhanced( multiblockdataset, speHandler ) |
| 44 | +
|
| 45 | + # Use your own handler (if speHandler is True) |
| 46 | + yourHandler: logging.Handler |
| 47 | + filter.setLoggerHandler( yourHandler ) |
| 48 | +
|
| 49 | + # Do calculations |
| 50 | + try: |
| 51 | + filter.applyFilter() |
| 52 | + except VTKError: |
| 53 | + logging.error("Something went wrong in VTK") |
| 54 | +
|
| 55 | + # Get the merged mesh |
| 56 | + filter.getOutput() |
| 57 | +""" |
| 58 | + |
| 59 | +loggerTitle: str = "Merge Block Enhanced" |
| 60 | + |
| 61 | + |
| 62 | +class MergeBlockEnhanced: |
| 63 | + |
| 64 | + def __init__( |
| 65 | + self: Self, |
| 66 | + inputMesh: vtkMultiBlockDataSet, |
| 67 | + speHandler: bool = False, |
| 68 | + ) -> None: |
| 69 | + """Merge a multiblock dataset and keep the partial attributes in the output mesh. |
| 70 | +
|
| 71 | + Partial attributes are filled with default values depending on the data type such that: |
| 72 | + - 0 for uint data. |
| 73 | + - -1 for int data. |
| 74 | + - nan for float data. |
| 75 | +
|
| 76 | + Args: |
| 77 | + inputMesh (vtkMultiBlockDataSet): The input multiblock dataset to merge. |
| 78 | + speHandler (bool, optional) : True to use a specific handler, False to use the internal handler. |
| 79 | + Defaults to False. |
| 80 | + """ |
| 81 | + self.inputMesh: vtkMultiBlockDataSet = inputMesh |
| 82 | + self.outputMesh: vtkUnstructuredGrid = vtkUnstructuredGrid() |
| 83 | + |
| 84 | + # Logger |
| 85 | + self.logger: Logger |
| 86 | + if not speHandler: |
| 87 | + self.logger = getLogger( loggerTitle, True ) |
| 88 | + else: |
| 89 | + self.logger = logging.getLogger( loggerTitle ) |
| 90 | + self.logger.setLevel( logging.INFO ) |
| 91 | + |
| 92 | + def setLoggerHandler( self: Self, handler: logging.Handler ) -> None: |
| 93 | + """Set a specific handler for the filter logger. |
| 94 | +
|
| 95 | + In this filter 4 log levels are use, .info, .error, .warning and .critical, be sure to have at least the same 4 levels. |
| 96 | +
|
| 97 | + Args: |
| 98 | + handler (logging.Handler): The handler to add. |
| 99 | + """ |
| 100 | + if not self.logger.hasHandlers(): |
| 101 | + self.logger.addHandler( handler ) |
| 102 | + else: |
| 103 | + self.logger.warning( |
| 104 | + "The logger already has an handler, to use yours set the argument 'speHandler' to True during the filter initialization." |
| 105 | + ) |
| 106 | + |
| 107 | + def applyFilter( self: Self ) -> None: |
| 108 | + """Merge the blocks of a multiblock dataset mesh. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + bool: True if the blocks were successfully merged, False otherwise. |
| 112 | +
|
| 113 | + Raises: |
| 114 | + VTKError (geos.utils.Errors) : error captured if any from the VTK log |
| 115 | + """ |
| 116 | + self.logger.info( f"Applying filter { self.logger.name }." ) |
| 117 | + |
| 118 | + outputMesh: vtkUnstructuredGrid |
| 119 | + outputMesh = mergeBlocks( self.inputMesh, keepPartialAttributes=True, logger=self.logger ) |
| 120 | + self.outputMesh = outputMesh |
| 121 | + |
| 122 | + def getOutput( self: Self ) -> vtkUnstructuredGrid: |
| 123 | + """Get the merged mesh. |
| 124 | +
|
| 125 | + Returns: |
| 126 | + vtkUnstructuredGrid: The merged mesh. |
| 127 | + """ |
| 128 | + return self.outputMesh |
0 commit comments