Source code for Muscat.Experimental.Containers.PartitionedMeshTools
# -*- coding: utf-8 -*-## This file is subject to the terms and conditions defined in# file 'LICENSE.txt', which is part of this source code package.#fromtypingimportOptional,List,Callable,Generator,AnyimportnumpyasnpfromMuscat.TypesimportMuscatIndexfromMuscat.Containers.MeshimportMeshfromMuscat.Experimental.Containers.PartitionedMeshimportPartitionedMesh,GlobalIdsFieldNamefromMuscat.Containers.Filters.FilterObjectsimportElementFilterfromMuscat.Containers.Filters.FilterBaseimportElementsSelectionfromMuscat.Containers.MeshModificationToolsimportCleanLonelyNodes
[docs]defElementFilterOnPartitionedMesh(elementFilter:ElementFilter)->Callable[[PartitionedMesh],Generator[ElementsSelection,Any,None]]:"""convert an element filter to work on partitioned meshes for selection in ElementFilterOnPartitionedMesh(ElementFilter(dimensionality=3))(myParMesh): print("--------------------------------------------------") print(selection.part) Parameters ---------- elementFilter : ElementFilter Returns ------- IterOverPartitionedMesh a object to iterate in the same way as a filter """classIterOverPartitionedMesh():def__init__(self,elementFilter):self.elementFilter:ElementFilter=elementFilterdef__call__(self,partitionedMesh:PartitionedMesh):forpart,meshinenumerate(partitionedMesh.storage):forselectioninself.elementFilter(mesh):yieldElementsSelection(mesh=selection.mesh,indices=selection.indices,elements=selection.elements,meshOffset=selection.meshOffset,selectionOffset=selection.selectionOffset,part=part)returnIterOverPartitionedMesh(elementFilter)
[docs]defPartitionMeshFromMask(mesh:Mesh,mask:np.ndarray,nbParts:int=0)->PartitionedMesh:"""Function to construct a PartitionedMesh from a mesh and a element partition (coloring) Parameters ---------- mesh : Mesh the mesh to apply the partition mask : np.ndarray a numpy array of ints of size Number of elements with values starting from zero nbParts : int, optional the number of part in the partition (this must be equal to len(np.unique(mask)) ), by default the value is internally computed by a np.unique Returns ------- PartitionedMesh partitioned mesh (the ) """fromMuscat.Containers.MeshInspectionToolsimportExtractElementsByElementFilterfromMuscat.Containers.MeshFieldOperationsimportCopyFieldsFromOriginalMeshToTargetMeshmyParMesh=PartitionedMesh()ifnbParts==0:nbParts=len(np.unique(mask))forninrange(nbParts):boolMask=mask==nf=ElementFilter(eMask=boolMask)newMesh=ExtractElementsByElementFilter(mesh,f)CopyFieldsFromOriginalMeshToTargetMesh(mesh,newMesh)CleanLonelyNodes(newMesh,inPlace=True)myParMesh.AddMesh(newMesh)returnmyParMesh
[docs]defCheckIntegrity(GUI:bool=False):fromMuscat.Containers.MeshGraphToolsimportPartitionMeshfromMuscat.Containers.Filters.FilterObjectsimportElementFilterfromMuscat.Containers.MeshInspectionToolsimportExtractElementsByElementFilterfromMuscat.Containers.MeshCreationToolsimportCreateCubemyMesh=CreateCube(dimensions=[10,10,10],spacing=[1,1,1],origin=[0,0,0],ofTetras=False)myMesh=ExtractElementsByElementFilter(myMesh,ElementFilter(dimensionality=3))part=np.asarray(PartitionMesh(myMesh,4))myMesh.nodeFields[GlobalIdsFieldName]=np.arange(myMesh.GetNumberOfNodes())myMesh.elemFields[GlobalIdsFieldName]=np.arange(myMesh.GetNumberOfElements())myParMesh=PartitionMeshFromMask(myMesh,part)print(myMesh)ifGUI:# pragma: no coverfromMuscat.Actions.OpenInParaViewimportOpenInParaViewOpenInParaView(myParMesh)forselectioninElementFilterOnPartitionedMesh(ElementFilter(dimensionality=3))(myParMesh):print("--------------------------------------------------")print(selection.Size())return"OK"
if__name__=='__main__':print(CheckIntegrity(True))# pragma: no cover