Mod: meshio object

This commit is contained in:
L-Nafaryus 2022-01-14 14:14:56 +05:00
parent c7111dc9cc
commit d724b45e85
3 changed files with 105 additions and 24 deletions

View File

@ -6,6 +6,8 @@ from netgen.occ import OCCGeometry
from netgen import meshing from netgen import meshing
from numpy import array from numpy import array
import os import os
from .utils import extractPoints, extractCells
import meshio
class NoGeometrySpecified(Exception): class NoGeometrySpecified(Exception):
@ -114,21 +116,55 @@ class Mesh(object):
return out, err, returncode return out, err, returncode
def doubleExport(self): def to_meshio(self):
pass points = extractPoints(self.mesh.Points())
cells = []
if len(self.mesh.Elements1D()) > 0:
cells.extend([ cells_ for cells_ in extractCells(1, self.mesh.Elements1D()).items() ])
if len(self.mesh.Elements2D()) > 0:
cells.extend([ cells_ for cells_ in extractCells(2, self.mesh.Elements2D()).items() ])
if len(self.mesh.Elements3D()) > 0:
cells.extend([ cells_ for cells_ in extractCells(3, self.mesh.Elements3D()).items() ])
return meshio.Mesh(points, cells)
@staticmethod
def volumeTetra(points: array) -> float:
return 1 / 6 * linalg.det(numpy.append(points.transpose(), numpy.array([[1, 1, 1, 1]]), axis = 0))
@property @property
def volumes(self) -> array: def volumes(self) -> array:
return array(self.mesh.Elements3D()) points = []
for cells in self.cells:
if cells.dim == 3:
points.extend([ self.mesh.points[cell] for cell in cells.data ])
return array(points)
@property @property
def faces(self) -> array: def faces(self) -> array:
return array(self.mesh.Elements2D()) points = []
for cells in self.cells:
if cells.dim == 2:
points.extend([ self.mesh.points[cell] for cell in cells.data ])
return array(points)
@property @property
def edges(self) -> array: def edges(self) -> array:
# NOTE: returns zero elements for imported mesh points = []
return array(self.mesh.Elements1D())
for cells in self.cells:
if cells.dim == 1:
points.extend([ self.mesh.points[cell] for cell in cells.data ])
return array(points)
# tetras = numpy.array([ [ [ vertex for vertex in mesh[index] ] for index in element.vertices ] for element in self.mesh.Elements3D() ]) # tetras = numpy.array([ [ [ vertex for vertex in mesh[index] ] for index in element.vertices ] for element in self.mesh.Elements3D() ])

View File

@ -0,0 +1,31 @@
from meshio._mesh import topological_dimension
from meshio._common import num_nodes_per_cell
from numpy import array
def detectTopology(dimension: dict, num_nodes: dict):
for dim in topological_dimension.keys():
for num in num_nodes_per_cell.keys():
if topological_dimension[dim] == dimension and num_nodes_per_cell[num] == num_nodes and dim == num:
return dim
def extractPoints(points):
return array([ point.p for point in points ], dtype = float)
def extractCells(dimension: int, elements):
cellsNew = {}
for cell in elements:
cellTopo = detectTopology(dimension, len(cell.points))
# shift indicies, they should starts from zero
cellNew = array([ pointId.nr for pointId in cell.points ], dtype = int) - 1
if cellsNew.get(cellTopo):
cellsNew[cellTopo].append(cellNew)
else:
cellsNew[cellTopo] = [ cellNew ]
return cellsNew

View File

@ -1,27 +1,27 @@
from netgen.meshing import Mesh as netgenMesh from netgen.meshing import Mesh as netgenMesh
from meshio import Mesh as meshioMesh from meshio import Mesh as meshioMesh
from meshio._mesh import topological_dimension
from meshio._common import num_nodes_per_cell
from numpy import array
meshfile = "mesh.mesh" meshfile = "mesh.mesh"
mesh = netgenMesh() mesh = netgenMesh()
mesh.Load(meshfile) mesh.Load(meshfile)
topology3d = { def detectTopology(dimension: dict, num_nodes: dict):
4: "tetra" for dim in topological_dimension.keys():
} for num in num_nodes_per_cell.keys():
if topological_dimension[dim] == dimension and num_nodes_per_cell[num] == num_nodes and dim == num:
return dim
pointsNew = [] def extractCells(dimension: int, elements):
cellsNew = {} cellsNew = {}
for point in mesh.Points(): for cell in elements:
pointsNew.append(list(point.p)) cellTopo = detectTopology(dimension, len(cell.points))
# shift indicies, they should starts from zero
for cell in mesh.Elements3D(): cellNew = array([ pointId.nr for pointId in cell.points ], dtype = int) - 1
cellTopo = topology3d[len(cell.points)]
cellNew = []
for pointId in cell.points:
cellNew.append(pointId.nr)
if cellsNew.get(cellTopo): if cellsNew.get(cellTopo):
cellsNew[cellTopo].append(cellNew) cellsNew[cellTopo].append(cellNew)
@ -29,5 +29,19 @@ for cell in mesh.Elements3D():
else: else:
cellsNew[cellTopo] = [ cellNew ] cellsNew[cellTopo] = [ cellNew ]
cellsMeshio = [ cell for cell in cellsNew.items() ] return cellsNew
meshNew = meshioMesh(pointsNew, cellsMeshio)
def extractPoints(points):
return array([ point.p for point in mesh.Points() ], dtype = float)
points = extractPoints(mesh.Points())
cells1d = extractCells(1, mesh.Elements1D())
cells2d = extractCells(2, mesh.Elements2D())
cells3d = extractCells(3, mesh.Elements3D())
cells = [
*[ e for e in cells1d.items() ],
*[ e for e in cells2d.items() ],
*[ e for e in cells3d.items() ]
]
meshNew = meshioMesh(points, cells)