anisotropy/src/simpleCubic.py

510 lines
16 KiB
Python
Raw Normal View History

2021-03-02 16:29:43 +05:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import salome, GEOM, SMESH, SALOMEDS
from salome.geom import geomBuilder
from salome.smesh import smeshBuilder
import math
import os, sys
2021-03-03 16:24:47 +05:00
import logging
import time
from datetime import timedelta
2021-03-02 16:29:43 +05:00
class simpleCubic:
def __init__(self, name = None):
self.name = name if type(name) != None else "simpleCubic"
self.geometry = None
2021-03-05 00:02:47 +05:00
self.geometrybbox = None
2021-03-02 16:29:43 +05:00
self.mesh = None
self.boundary = None
2021-03-05 00:02:47 +05:00
self.rombus = None
self.rombusbbox = None
2021-03-06 19:19:43 +05:00
self.spheres = None
2021-03-02 22:22:54 +05:00
salome.salome_init()
2021-03-02 16:29:43 +05:00
2021-03-09 13:13:31 +05:00
def geometryCreate(self, alpha, fillet):
2021-03-03 13:00:28 +05:00
"""
Create the simple cubic geometry.
Parameters:
alpha (float): Sphere intersection parameter which used for cutting spheres from box.
2021-03-06 19:19:43 +05:00
Radius = R0 / (1 - alpha)
Should be from 0.01 to 0.28
2021-03-03 13:00:28 +05:00
2021-03-09 13:13:31 +05:00
fillet (list): Fillet coefficient.
[fillet1, fillet2]
0 <= fillet <= 1
if fillet = [0, 0] then R_fillet = 0
2021-03-03 13:00:28 +05:00
Returns:
Configured geometry.
"""
2021-03-02 16:29:43 +05:00
geompy = geomBuilder.New()
2021-03-06 19:19:43 +05:00
# Parameters
R0 = 1
R = R0 / (1 - alpha)
2021-03-09 13:13:31 +05:00
C1 = fillet[0]
C2 = fillet[1]
2021-03-06 19:19:43 +05:00
alpha1 = 0.01
alpha2 = 0.28
Cf = C1 + (C2 - C1) / (alpha2 - alpha1) * (alpha - alpha1)
R_fillet = Cf * (R0 * math.sqrt(2) - R)
logging.info("geometryCreate: alpha = {}".format(alpha))
logging.info("geometryCreate: R_fillet = {}".format(R_fillet))
2021-03-02 16:29:43 +05:00
# xyz axes
axes = [
geompy.MakeVectorDXDYDZ(1, 0, 0),
geompy.MakeVectorDXDYDZ(0, 1, 0),
geompy.MakeVectorDXDYDZ(0, 0, 1)
]
# Main box
2021-03-07 16:29:04 +05:00
size = [2 * math.sqrt(2), 2 * math.sqrt(2), 2]
angle = [0, 0, 45]
pos = [2, 0, 0]
2021-03-02 16:29:43 +05:00
2021-03-07 16:29:04 +05:00
box = geompy.MakeBoxDXDYDZ(size[0], size[1], size[2])
for n in range(3):
box = geompy.MakeRotation(box, axes[n], angle[n] * math.pi / 180.0)
box = geompy.MakeTranslation(box, pos[0], pos[1], pos[2])
2021-03-02 16:29:43 +05:00
2021-03-07 19:32:54 +05:00
#[x, y, z, _, _, _, _, _, _] = geompy.GetPosition(box)
#pos = [x, y, z]
2021-03-02 16:29:43 +05:00
# Spheres for cutting
2021-03-07 19:32:54 +05:00
sphere = geompy.MakeSpherePntR(geompy.MakeVertex(pos[0], pos[1], pos[2]), R)
sphere = geompy.MakeMultiTranslation2D(sphere, None, 2 * R0, 3, None, 2 * R0, 3)
sphere = geompy.MakeTranslation(sphere, -2 * R0, 0, 0)
sphere2 = geompy.MakeTranslation(sphere, 0, 0, 2 * R0)
sphere3 = geompy.MakeTranslation(sphere2, 0, 0, 2 * R0)
2021-03-02 16:29:43 +05:00
sphere = geompy.ExtractShapes(sphere, geompy.ShapeType["SOLID"], True)
sphere2 = geompy.ExtractShapes(sphere2, geompy.ShapeType["SOLID"], True)
2021-03-05 00:02:47 +05:00
sphere3 = geompy.ExtractShapes(sphere3, geompy.ShapeType["SOLID"], True)
2021-03-07 19:32:54 +05:00
sphere = geompy.MakeFuseList(sphere + sphere2 + sphere3, False, False)
2021-03-06 19:19:43 +05:00
2021-03-05 23:02:28 +05:00
if not R_fillet == 0:
sphere = geompy.MakeFilletAll(sphere, R_fillet)
2021-03-02 16:29:43 +05:00
2021-03-06 19:19:43 +05:00
self.spheres = sphere
#else:
# sphere = sphere + sphere2 + sphere3 #geompy.MakeCompound(sphere + sphere2 + sphere3)
# geompy.RemoveExtraEdges(obj, True)
2021-03-02 16:29:43 +05:00
self.geometry = geompy.MakeCutList(box, [sphere], True)
2021-03-05 00:02:47 +05:00
self.geometrybbox = box
2021-03-02 16:29:43 +05:00
geompy.addToStudy(self.geometry, self.name)
2021-03-05 00:02:47 +05:00
# Rombus
h = 2
2021-03-06 19:19:43 +05:00
Vertex_2 = geompy.MakeVertex(0, 0, 4)
Vertex_1 = geompy.MakeVertex(2, 0, 2)
Vertex_3 = geompy.MakeVertex(2, 2, 0)
Vertex_4 = geompy.MakeVertex(0, 2, 2)
Edge_1 = geompy.MakeEdge(Vertex_2, Vertex_1)
Edge_2 = geompy.MakeEdge(Vertex_1, Vertex_3)
Edge_3 = geompy.MakeEdge(Vertex_3, Vertex_4)
Edge_4 = geompy.MakeEdge(Vertex_4, Vertex_2)
Face_1 = geompy.MakeFaceWires([Edge_1, Edge_2, Edge_3, Edge_4], 1)
#sk = geompy.Sketcher3D()
#sk.addPointsAbsolute(0, 0, h * 2)
#sk.addPointsAbsolute(h, 0, h)
#sk.addPointsAbsolute(h, h, 0)
#sk.addPointsAbsolute(0, h, h)
#sk.addPointsAbsolute(0, 0, h * 2)
#a3D_Sketcher_1 = sk.wire()
#Face_1 = geompy.MakeFaceWires([a3D_Sketcher_1], 1)
Vector_1 = geompy.MakeVectorDXDYDZ(1, 1, 0)
rombusbbox = geompy.MakePrismVecH(Face_1, Vector_1, round(2 * math.sqrt(2), 14))
geompy.addToStudy(rombusbbox, "rombusbbox")
self.rombus = geompy.MakeCutList(rombusbbox, [sphere], True)
self.rombusbbox = rombusbbox
2021-03-05 15:57:14 +05:00
2021-03-05 00:02:47 +05:00
geompy.addToStudy(self.rombus, "rombus")
2021-03-02 16:29:43 +05:00
2021-03-03 13:00:28 +05:00
return self.geometry
2021-03-02 16:29:43 +05:00
def boundaryCreate(self, direction):
2021-03-03 13:00:28 +05:00
"""
Create the boundary faces from the geometry.
Parameters:
direction (str): Direction of the flow.
2021-03-09 13:13:31 +05:00
'001' for the flow with normal vector (0, 0, 1) to face.
'100' for the flow with normal vector (1, 0, 0) to face.
'111' for (1, 1, 1)
2021-03-03 13:00:28 +05:00
Returns:
boundary (dict):
{
"inlet": <GEOM._objref_GEOM_Object>,
"outlet": <GEOM._objref_GEOM_Object>,
"symetryPlane": <GEOM._objref_GEOM_Object>,
"wall": <GEOM._objref_GEOM_Object>
}
"""
geompy = geomBuilder.New()
2021-03-02 16:29:43 +05:00
rot = [0, 0, 45]
2021-03-05 00:02:47 +05:00
buffergeometry = self.geometry
2021-03-02 16:29:43 +05:00
2021-03-02 22:22:54 +05:00
if direction == "001":
2021-03-05 00:02:47 +05:00
center = geompy.MakeVertex(2, 2, 1)
2021-03-02 16:29:43 +05:00
norm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center, 0, 0, 1))
2021-03-05 00:02:47 +05:00
2021-03-07 16:29:04 +05:00
bnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center,
-math.cos((90 + rot[2]) * math.pi / 180.0),
math.sin((90 + rot[2]) * math.pi / 180.0), 0))
vnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center,
-math.cos((0 + rot[2]) * math.pi / 180.0),
math.sin((0 + rot[2]) * math.pi / 180.0), 0))
2021-03-02 22:22:54 +05:00
vstep = 1
hstep = math.sqrt(2)
2021-03-02 16:29:43 +05:00
2021-03-02 22:22:54 +05:00
elif direction == "100":
2021-03-05 00:02:47 +05:00
center = geompy.MakeVertex(2, 2, 1)
2021-03-02 16:29:43 +05:00
norm = geompy.MakeVector(center,
2021-03-03 13:00:28 +05:00
geompy.MakeVertexWithRef(center,
2021-03-05 00:02:47 +05:00
-math.cos((90 + rot[2]) * math.pi / 180.0),
math.sin((90 + rot[2]) * math.pi / 180.0), 0))
2021-03-07 16:29:04 +05:00
bnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center, 0, 0, 1))
vnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center,
-math.cos((0 + rot[2]) * math.pi / 180.0),
math.sin((0 + rot[2]) * math.pi / 180.0), 0))
2021-03-02 22:22:54 +05:00
vstep = math.sqrt(2)
hstep = 1
2021-03-05 00:02:47 +05:00
elif direction == "111":
center = geompy.MakeVertex(2, 2, 2)
2021-03-06 19:19:43 +05:00
self.geometry = self.rombus
2021-03-05 00:02:47 +05:00
norm = geompy.MakeVector(center,
2021-03-06 19:19:43 +05:00
geompy.MakeVertexWithRef(center, 1, 1, 1))
#-math.cos((90 + rot[2]) * math.pi / 180.0),
#math.sin((90 + rot[2]) * math.pi / 180.0), math.sqrt(2) / 2))
2021-03-05 00:02:47 +05:00
2021-03-07 16:29:04 +05:00
bnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center, 1, -1, 1))
# -math.cos((90 + rot[2]) * math.pi / 180.0),
# math.sin((90 + rot[2]) * math.pi / 180.0), 0))
vnorm = geompy.MakeVector(center,
geompy.MakeVertexWithRef(center, -1, 1, 1))
#-math.cos((0 + rot[2]) * math.pi / 180.0),
#math.sin((0 + rot[2]) * math.pi / 180.0), 0))
2021-03-05 00:02:47 +05:00
vstep = math.sqrt(2)
hstep = 1
2021-03-02 16:29:43 +05:00
2021-03-06 19:19:43 +05:00
logging.info("boundaryCreate: direction = {}".format(direction))
2021-03-05 00:02:47 +05:00
2021-03-06 19:19:43 +05:00
geompy.addToStudy(norm, "normalvector")
2021-03-07 16:29:04 +05:00
geompy.addToStudy(bnorm, "bnorm")
geompy.addToStudy(vnorm, "vnorm")
2021-03-02 16:29:43 +05:00
2021-03-05 00:02:47 +05:00
if direction == "111":
2021-03-06 19:19:43 +05:00
box = self.rombus
2021-03-05 00:02:47 +05:00
else:
box = self.geometrybbox
2021-03-03 13:00:28 +05:00
planes = geompy.ExtractShapes(box, geompy.ShapeType["FACE"], True)
2021-03-06 19:19:43 +05:00
inletplane = []
outletplane = []
2021-03-02 22:22:54 +05:00
hplanes = []
2021-03-06 19:19:43 +05:00
2021-03-07 16:29:04 +05:00
fwplanes = []
bwplanes = []
lplanes = []
rplanes = []
2021-03-02 16:29:43 +05:00
for plane in planes:
2021-03-02 22:22:54 +05:00
planeNorm = geompy.GetNormal(plane)
2021-03-06 19:19:43 +05:00
angle = round(abs(geompy.GetAngle(planeNorm, norm)), 0)
2021-03-05 23:02:28 +05:00
if angle == 0:
2021-03-06 19:19:43 +05:00
outletplane.append(plane)
2021-03-05 23:02:28 +05:00
elif angle == 180:
2021-03-06 19:19:43 +05:00
inletplane.append(plane)
2021-03-02 22:22:54 +05:00
2021-03-06 19:19:43 +05:00
elif direction == "111" and (angle == 109 or angle == 71):
2021-03-07 16:29:04 +05:00
#hplanes.append(plane)
bangle = round(abs(geompy.GetAngle(planeNorm, bnorm)), 0)
#logging.info("bangle = {}".format(bangle))
if bangle == 0:
fwplanes.append(plane)
elif bangle == 180:
bwplanes.append(plane)
vangle = round(abs(geompy.GetAngle(planeNorm, vnorm)), 0)
#logging.info("vangle = {}".format(vangle))
if vangle == 0:
lplanes.append(plane)
elif vangle == 180:
rplanes.append(plane)
2021-03-05 23:02:28 +05:00
2021-03-06 19:19:43 +05:00
elif direction == "100" or direction == "001":
if angle == 90:
2021-03-07 16:29:04 +05:00
#hplanes.append(plane)
bangle = round(abs(geompy.GetAngle(planeNorm, bnorm)), 0)
#logging.info("bangle = {}".format(bangle))
if bangle == 0:
fwplanes.append(plane)
elif bangle == 180:
bwplanes.append(plane)
vangle = round(abs(geompy.GetAngle(planeNorm, vnorm)), 0)
#logging.info("vangle = {}".format(vangle))
if vangle == 0:
lplanes.append(plane)
elif vangle == 180:
rplanes.append(plane)
2021-03-06 19:19:43 +05:00
2021-03-05 00:02:47 +05:00
if salome.sg.hasDesktop():
salome.sg.updateObjBrowser()
2021-03-02 22:22:54 +05:00
2021-03-06 19:19:43 +05:00
logging.info("boundaryCreate: inletplanes = {}, outletplanes = {}, hplanes = {}".format(
len(inletplane), len(outletplane), len(hplanes)))
2021-03-02 22:22:54 +05:00
2021-03-07 16:29:04 +05:00
logging.info("boundaryCreate: fwplanes = {}, bwplanes = {}, lplanes = {}, rplanes = {}".format(
len(fwplanes), len(bwplanes), len(lplanes), len(rplanes)))
2021-03-05 23:02:28 +05:00
2021-03-06 19:19:43 +05:00
def createGroup(planelist, name):
gr = geompy.CreateGroup(self.geometry, geompy.ShapeType["FACE"], name)
grcomp = geompy.MakeCompound(planelist)
grcut = geompy.MakeCutList(grcomp, [self.spheres], True)
2021-03-02 16:29:43 +05:00
2021-03-06 19:19:43 +05:00
gip = geompy.GetInPlace(self.geometry, grcut, True)
faces = geompy.SubShapeAll(gip, geompy.ShapeType["FACE"])
geompy.UnionList(gr, faces)
return gr
2021-03-02 22:22:54 +05:00
2021-03-02 16:29:43 +05:00
2021-03-06 19:19:43 +05:00
# Main groups
inlet = createGroup(inletplane, "inlet")
outlet = createGroup(outletplane, "outlet")
2021-03-07 16:29:04 +05:00
#symetryPlane = createGroup(hplanes, "symetryPlane")
symetryPlaneFW = createGroup(fwplanes, "symetryPlaneFW")
symetryPlaneBW = createGroup(bwplanes, "symetryPlaneBW")
symetryPlaneL = createGroup(lplanes, "symetryPlaneL")
symetryPlaneR = createGroup(rplanes, "symetryPlaneR")
2021-03-02 16:29:43 +05:00
2021-03-02 22:22:54 +05:00
# wall
allgroup = geompy.CreateGroup(self.geometry, geompy.ShapeType["FACE"])
2021-03-02 16:29:43 +05:00
faces = geompy.SubShapeAllIDs(self.geometry, geompy.ShapeType["FACE"])
2021-03-02 22:22:54 +05:00
geompy.UnionIDs(allgroup, faces)
2021-03-07 16:29:04 +05:00
wall = geompy.CutListOfGroups([allgroup],
[inlet, outlet, symetryPlaneFW, symetryPlaneBW, symetryPlaneL, symetryPlaneR], "wall")
2021-03-02 16:29:43 +05:00
self.boundary = {
"inlet": inlet,
"outlet": outlet,
2021-03-07 16:29:04 +05:00
"symetryPlaneFW": symetryPlaneFW,
"symetryPlaneBW": symetryPlaneBW,
"symetryPlaneL": symetryPlaneL,
"symetryPlaneR": symetryPlaneR,
2021-03-02 16:29:43 +05:00
"wall": wall
}
return self.boundary
2021-03-03 13:00:28 +05:00
def meshCreate(self, fineness, viscousLayers=None):
"""
Creates a mesh from a geometry.
Parameters:
fineness (int): Fineness of mesh.
0 - Very coarse,
1 - Coarse,
2 - Moderate,
3 - Fine,
4 - Very fine.
viscousLayers (dict or None): Defines viscous layers for mesh.
By default, inlets and outlets specified without layers.
{
"thickness": float,
"number": int,
"stretch": float
}
Returns:
Configured instance of class <SMESH.SMESH_Mesh>, containig the parameters and boundary groups.
"""
2021-03-02 16:29:43 +05:00
smesh = smeshBuilder.New()
2021-03-06 19:19:43 +05:00
Fineness = {
0: "Very coarse",
1: "Coarse",
2: "Moderate",
3: "Fine",
4: "Very fine"
}[fineness]
logging.info("meshCreate: mesh fineness - {}".format(Fineness))
2021-03-02 16:29:43 +05:00
2021-03-03 13:00:28 +05:00
mesh = smesh.Mesh(self.geometry)
2021-03-02 16:29:43 +05:00
netgen = mesh.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D)
param = netgen.Parameters()
param.SetSecondOrder( 0 )
param.SetOptimize( 1 )
param.SetChordalError( -1 )
param.SetChordalErrorEnabled( 0 )
param.SetUseSurfaceCurvature( 1 )
param.SetFuseEdges( 1 )
param.SetCheckChartBoundary( 0 )
param.SetMinSize( 0.01 )
param.SetMaxSize( 0.1 )
2021-03-03 13:00:28 +05:00
param.SetFineness(fineness)
2021-03-02 16:29:43 +05:00
#param.SetGrowthRate( 0.1 )
#param.SetNbSegPerEdge( 5 )
#param.SetNbSegPerRadius( 10 )
param.SetQuadAllowed( 0 )
2021-03-03 13:00:28 +05:00
if not viscousLayers is None:
2021-03-06 19:19:43 +05:00
logging.info("meshCreate: viscous layers params - thickness = {}, number = {}, stretch factor = {}".format(
viscousLayers["thickness"], viscousLayers["number"], viscousLayers["stretch"]))
2021-03-03 13:00:28 +05:00
vlayer = netgen.ViscousLayers(viscousLayers["thickness"],
viscousLayers["number"],
viscousLayers["stretch"],
[self.boundary["inlet"], self.boundary["outlet"]],
1, smeshBuilder.NODE_OFFSET)
2021-03-06 19:19:43 +05:00
else:
logging.info("meshCreate: viscous layers are disabled")
2021-03-03 13:00:28 +05:00
for name, boundary in self.boundary.items():
2021-03-07 16:29:04 +05:00
mesh.GroupOnGeom(boundary, "{}_".format(name), SMESH.FACE)
2021-03-02 16:29:43 +05:00
self.mesh = mesh
return self.mesh
def meshCompute(self):
2021-03-03 13:00:28 +05:00
"""Compute the mesh."""
2021-03-02 16:29:43 +05:00
status = self.mesh.Compute()
if status:
2021-03-03 22:16:36 +05:00
logging.info("Mesh succesfully computed.")
2021-03-02 16:29:43 +05:00
else:
2021-03-03 22:16:36 +05:00
logging.warning("Mesh is not computed.")
2021-03-02 16:29:43 +05:00
def meshExport(self, path):
2021-03-03 13:00:28 +05:00
"""
Export the mesh in a file in UNV format.
Parameters:
path (string): full path to the expected directory.
"""
2021-03-02 16:29:43 +05:00
exportpath = os.path.join(path, "{}.unv".format(self.name))
try:
self.mesh.ExportUNV(exportpath)
except:
2021-03-03 22:16:36 +05:00
logging.error("Cannot export mesh to '{}'".format(exportpath))
2021-03-02 16:29:43 +05:00
if __name__ == "__main__":
2021-03-03 16:24:47 +05:00
# Arguments
2021-03-02 16:29:43 +05:00
buildpath = str(sys.argv[1])
alpha = float(sys.argv[2])
direction = str(sys.argv[3])
2021-03-03 16:24:47 +05:00
name = "simpleCubic-{}-{}".format(direction, alpha)
# Logger
2021-03-03 22:16:36 +05:00
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers = [
logging.StreamHandler(),
2021-03-05 00:02:47 +05:00
logging.FileHandler(os.path.join(buildpath, "{}.log".format(name)))
2021-03-03 22:16:36 +05:00
])
2021-03-03 16:24:47 +05:00
start_time = time.monotonic()
# Simple cubic
sc = simpleCubic(name)
logging.info("Creating the geometry ...")
2021-03-09 13:13:31 +05:00
sc.geometryCreate(alpha, [0, 0])
2021-03-03 16:24:47 +05:00
logging.info("Extracting boundaries ...")
2021-03-02 16:29:43 +05:00
sc.boundaryCreate(direction)
2021-03-03 16:24:47 +05:00
logging.info("Creating the mesh ...")
2021-03-06 19:19:43 +05:00
sc.meshCreate(2) #, {
# "thickness": 0.001,
# "number": 1,
# "stretch": 1.1
#})
2021-03-03 13:00:28 +05:00
sc.meshCompute()
2021-03-03 16:24:47 +05:00
logging.info("Exporting the mesh ...")
sc.meshExport(buildpath)
end_time = time.monotonic()
logging.info("Elapsed time: {}".format(timedelta(seconds=end_time - start_time)))
logging.info("Done.")
2021-03-02 16:29:43 +05:00
if salome.sg.hasDesktop():
salome.sg.updateObjBrowser()