[docs]defGetPlotlySurfaceMesh(myMesh:Mesh,fieldToPlot:Optional[Union[str,np.ndarray]]=None):"""Function to convert a Mesh and a Nodal fields (element fields to yet implemented) to a Plotly graph_object Mesh3d library. Internally the norm of the field is computed if the fieldToPlot is a vector field (like normals). (This functions need the plotly package installed) Parameters ---------- myMesh : Mesh The mesh to be converted fieldToPlot : Optional[Union[str,np.ndarray]], optional point field or point field name to put the intensity field of the Mesh3d. by default None Returns ------- plotly.graph_objects.Mesh3d the mesh converted to Plotly format Raises ------ RuntimeError if field.shape[0] != number of node of the mesh RuntimeError if the field name is not present in the mesh.nodesFields """importplotly.graph_objectsasgomesh=myMesh.View()#for the moment the compute skin does permit to transport elemField data to the skinmesh.elemFields={}fromMuscat.SimpleimportElementFilter,EDfromMuscat.MeshTools.MeshModificationToolsimportComputeSkinfromMuscat.MeshTools.MeshInspectionToolsimportExtractElementsByElementFilterComputeSkin(mesh=mesh,inPlace=True)mesh2D=ExtractElementsByElementFilter(mesh,ElementFilter(dimensionality=2),copy=False)fromMuscat.MeshTools.MeshTetrahedrizationimportTetrahedrizationmesh_of_tris=Tetrahedrization(mesh2D)ifmesh_of_tris.nodes.shape[1]<3:z=np.zeros_like(mesh_of_tris.nodes[:,0])else:z=mesh_of_tris.nodes[:,2]res=go.Mesh3d(x=mesh_of_tris.nodes[:,0],y=mesh_of_tris.nodes[:,1],z=z,i=mesh_of_tris.GetElementsOfType(ED.Triangle_3).connectivity[:,0],j=mesh_of_tris.GetElementsOfType(ED.Triangle_3).connectivity[:,1],k=mesh_of_tris.GetElementsOfType(ED.Triangle_3).connectivity[:,2],flatshading=True)iffieldToPlotisNone:returnresres.showscale=Trueifisinstance(fieldToPlot,str):iffieldToPlotinmesh.nodeFields:name=fieldToPlotfieldToPlot=mesh.nodeFields[fieldToPlot]intensitymode='vertex'eliffieldToPlotinmesh.elemFields:name=fieldToPlotfieldToPlot=mesh.elemFields[fieldToPlot]intensitymode='cell'else:raiseRuntimeError("Error: fieldToPlot not in nodeFields neither elemFields")else:name="data"iffieldToPlot.shape[0]==myMesh.GetNumberOfNodes():intensitymode='vertex'eliffieldToPlot.shape[0]==myMesh.GetNumberOfElements():intensitymode='cell'else:raiseRuntimeError("Error: fieldToPlot must match the number of nodes or the number of elements")iflen(fieldToPlot.shape)>1:fieldToPlot=np.linalg.norm(fieldToPlot,axis=1)name=f"norm({name})"res.intensity=fieldToPlotres.intensitymode=intensitymoderes.colorbar=dict(title=dict(text=name))res.colorscale='Viridis'returnres
[docs]defPlotMesh(mesh:Union[Mesh,'Mesh3d'],nodeFieldToPlot:Optional[Union[str,np.ndarray]]=None):"""Plot a mesh using Plotly (This functions need the plotly package installed) Parameters ---------- mesh : Union[Mesh,'Mesh3d'] Mesh to plot (Muscat Mesh or plotly Mesh3D) fieldToPlot : Optional[Union[str,np.ndarray]], optional point field or point field name to put the intensity field of the Mesh3d. by default None Returns ------- Plotly Figure the Plotly figure """importplotly.graph_objectsasgoifisinstance(mesh,Mesh):meshtoplot=GetPlotlySurfaceMesh(mesh,nodeFieldToPlot)else:meshtoplot=meshfig=go.Figure(data=[meshtoplot])fig.show()returnfig
[docs]defCheckIntegrity(GUI:bool=False):fromMuscat.Helpers.CheckToolsimportSkipTestifSkipTest("PLOTLY_NO_FAIL"):# pragma: no coverreturn"skip"try:importplotlyexcept:# pragma: no coverreturn"skip : plotly not installed"fromMuscat.MeshTools.MeshToolsimportGetElementsCentersfromMuscat.SimpleimportReadMeshfromMuscat.TestDataimportGetTestDataPathmyMesh:Mesh=ReadMesh(GetTestDataPath()+"dent3D.msh")myMesh.nodeFields["xpos"]=myMesh.nodes[:,0]myMesh.nodeFields["pos"]=myMesh.nodesmyMesh.elemFields["centers"]=GetElementsCenters(myMesh)plotlymesh=GetPlotlySurfaceMesh(myMesh)plotlymesh=GetPlotlySurfaceMesh(myMesh,"xpos")ifGUI:PlotMesh(plotlymesh)PlotMesh(myMesh)return'ok'
if__name__=="__main__":print(CheckIntegrity(True))# pragma: no cover