anisotropy/salomepl/mesh.py

189 lines
4.8 KiB
Python
Raw Normal View History

2021-03-18 17:29:30 +05:00
import SMESH
2021-03-15 16:16:48 +05:00
from salome.smesh import smeshBuilder
2021-03-18 17:29:30 +05:00
smesh = smeshBuilder.New()
import logging
logger = logging.getLogger("anisotropy")
2021-03-18 17:29:30 +05:00
def getSmesh():
return smesh
2021-03-15 16:16:48 +05:00
2021-03-31 23:11:29 +05:00
2021-06-08 17:15:44 +05:00
def meshCreate(shape, parameters): #fineness, parameters, viscousLayers = None):
2021-03-15 16:16:48 +05:00
"""
Creates a mesh from a geometry.
Parameters:
fineness (int): Fineness of mesh.
0 - Very coarse,
1 - Coarse,
2 - Moderate,
3 - Fine,
4 - Very fine.
Returns:
Configured instance of class <SMESH.SMESH_Mesh>, containig the parameters and boundary groups.
"""
2021-04-12 16:00:26 +05:00
###
# Netgen
##
2021-03-15 16:16:48 +05:00
Fineness = {
0: "Very coarse",
1: "Coarse",
2: "Moderate",
3: "Fine",
2021-04-02 21:41:48 +05:00
4: "Very fine",
5: "Custom"
}[parameters.fineness]
2021-03-15 16:16:48 +05:00
2021-04-12 16:00:26 +05:00
# Mesh
mesh = smesh.Mesh(shape)
2021-03-15 16:16:48 +05:00
netgen = mesh.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D)
2021-04-12 16:00:26 +05:00
# Parameters
2021-03-15 16:16:48 +05:00
param = netgen.Parameters()
2021-04-12 16:00:26 +05:00
param.SetMinSize(parameters.minSize)
param.SetMaxSize(parameters.maxSize)
param.SetFineness(parameters.fineness)
2021-04-02 21:41:48 +05:00
if parameters.fineness == 5:
2021-04-12 16:00:26 +05:00
param.SetGrowthRate(parameters.growthRate)
param.SetNbSegPerEdge(parameters.nbSegPerEdge)
param.SetNbSegPerRadius(parameters.nbSegPerRadius)
2021-04-02 21:41:48 +05:00
2021-04-12 16:00:26 +05:00
param.SetChordalErrorEnabled(parameters.chordalErrorEnabled)
param.SetChordalError(parameters.chordalError)
param.SetSecondOrder(parameters.secondOrder)
param.SetOptimize(parameters.optimize)
param.SetQuadAllowed(parameters.quadAllowed)
param.SetUseSurfaceCurvature(parameters.useSurfaceCurvature)
param.SetFuseEdges(parameters.fuseEdges)
param.SetCheckChartBoundary(parameters.checkChartBoundary)
logger.info("""meshCreate:
2021-04-02 21:41:48 +05:00
fineness:\t{}
min size:\t{}
max size:\t{}
growth rate:\t{}
nb segs per edge:\t{}
nb segs per radius:\t{}
limit size by surface curvature:\t{}
quad-dominated:\t{}
second order:\t{}
optimize:\t{}""".format(
Fineness, param.GetMinSize(), param.GetMaxSize(),
param.GetGrowthRate(), param.GetNbSegPerEdge(), param.GetNbSegPerRadius(),
True if param.GetUseSurfaceCurvature() else False,
True if param.GetQuadAllowed() else False,
True if param.GetSecondOrder() else False,
True if param.GetOptimize() else False))
2021-04-12 16:00:26 +05:00
###
# Viscous layers
##
vlayer = netgen.ViscousLayers(
parameters.thickness,
parameters.numberOfLayers,
parameters.stretchFactor,
parameters.facesToIgnore,
parameters.isFacesToIgnore,
parameters.extrusionMethod
)
logger.info("""meshCreate:
viscous layers:
thickness:\t{}
number:\t{}
stretch factor:\t{}""".format(
parameters.thickness,
parameters.numberOfLayers,
parameters.stretchFactor))
2021-03-15 16:16:48 +05:00
return mesh
2021-03-31 23:11:29 +05:00
2021-06-08 17:15:44 +05:00
def meshCompute(mobj, groups):
2021-03-15 16:16:48 +05:00
"""Compute the mesh."""
status = mobj.Compute()
2021-04-02 21:41:48 +05:00
if status:
logger.info("meshCompute: computed")
###
# Post computing
##
if mobj.NbPyramids() > 0:
logger.info(f"meshCompute: detected {mobj.NbPyramids()} pyramids: splitting volumes into tetrahedrons")
pyramidCriterion = smesh.GetCriterion(
SMESH.VOLUME,
SMESH.FT_ElemGeomType,
SMESH.FT_Undefined,
SMESH.Geom_PYRAMID
)
pyramidGroup = mobj.MakeGroupByCriterion("pyramids", pyramidCriterion)
pyramidVolumes = mobj.GetIDSource(pyramidGroup.GetIDs(), SMESH.VOLUME)
mobj.SplitVolumesIntoTetra(pyramidVolumes, smesh.Hex_5Tet)
mobj.RemoveGroup(pyramidGroup)
mobj.RenumberElements()
2021-06-08 17:15:44 +05:00
###
# Groups
##
for group in groups:
mobj.GroupOnGeom(group, f"{ group.GetName() }_", SMESH.FACE)
2021-04-08 00:13:27 +05:00
else:
logger.warning("meshCompute: not computed")
2021-04-08 00:13:27 +05:00
2021-06-02 16:12:34 +05:00
return not status
2021-03-31 23:11:29 +05:00
def meshStats(mobj):
"""
Print mesh information.
"""
stats = {
"Elements": mobj.NbElements(),
"Edges": mobj.NbEdges(),
"Faces": mobj.NbFaces(),
"Volumes": mobj.NbVolumes(),
"Tetrahedrons": mobj.NbTetras(),
"Prisms": mobj.NbPrisms(),
"Pyramids": mobj.NbPyramids()
}
info = "meshStats:\n"
for key in stats:
info += f"\t{key}:\t{stats[key]}\n"
logger.info(info)
2021-03-15 16:16:48 +05:00
def meshExport(mobj, path):
"""
Export the mesh in a file in UNV format.
Parameters:
path (string): full path to the expected directory.
"""
try:
2021-03-31 23:11:29 +05:00
mobj.ExportUNV(path)
2021-03-15 16:16:48 +05:00
logger.info("""meshExport:
2021-04-12 16:00:26 +05:00
format:\t{}""".format("unv"))
2021-03-15 16:16:48 +05:00
except:
logger.error("""meshExport: Cannot export.""")
2021-03-15 16:16:48 +05:00