anisotropy/src/geometry_utils.py

263 lines
8.0 KiB
Python
Raw Normal View History

2021-03-18 17:29:30 +05:00
import GEOM
2021-03-15 16:16:48 +05:00
from salome.geom import geomBuilder
2021-03-18 17:29:30 +05:00
geompy = geomBuilder.New()
2021-03-15 16:16:48 +05:00
import math
import logging
2021-03-30 22:14:47 +05:00
from pyquaternion import Quaternion
2021-03-29 19:39:01 +05:00
import numpy as np
2021-03-18 17:29:30 +05:00
def getGeom():
return geompy
2021-03-15 16:16:48 +05:00
2021-03-31 23:11:29 +05:00
#def rotate(gobj, ang):
# x = geompy.MakeVectorDXDYDZ(1, 0, 0)
# y = geompy.MakeVectorDXDYDZ(0, 1, 0)
# z = geompy.MakeVectorDXDYDZ(0, 0, 1)
2021-03-15 16:16:48 +05:00
# yaw
2021-03-31 23:11:29 +05:00
# rotated = geompy.MakeRotation(gobj, z, ang[2])
2021-03-15 16:16:48 +05:00
# pitch
2021-03-31 23:11:29 +05:00
# rotated = geompy.MakeRotation(rotated, y, ang[1])
2021-03-15 16:16:48 +05:00
# roll
2021-03-31 23:11:29 +05:00
# rotated = geompy.MakeRotation(rotated, x, ang[0])
2021-03-15 16:16:48 +05:00
2021-03-31 23:11:29 +05:00
# return rotated
2021-03-15 16:16:48 +05:00
def createGroup(gobj, planelist, grains, name):
gr = geompy.CreateGroup(gobj, geompy.ShapeType["FACE"], name)
grcomp = geompy.MakeCompound(planelist)
2021-03-24 21:27:12 +05:00
#grcut = geompy.MakeCutList(grcomp, [grains], False)
2021-03-15 16:16:48 +05:00
2021-03-24 21:27:12 +05:00
gip = geompy.GetInPlace(gobj, grcomp, True)
2021-03-15 16:16:48 +05:00
faces = geompy.SubShapeAll(gip, geompy.ShapeType["FACE"])
geompy.UnionList(gr, faces)
return gr
2021-03-29 19:39:01 +05:00
2021-03-30 14:49:06 +05:00
def createBoundary(gobj, bcount, dvec, norm, grains):
2021-03-31 23:11:29 +05:00
#normvec = Quaternion(0, norm[0], norm[1], norm[2]).normalised
#ax = lambda alpha: Quaternion(
# np.cos(alpha * 0.5),
# np.sin(alpha * 0.5) * dvec[0],
# 0, 0).normalised
#np.sin(alpha * 0.5) * dvec[1],
#np.sin(alpha * 0.5) * dvec[2]).normalised
2021-03-29 19:39:01 +05:00
ang = lambda n, count: 2 * np.pi * n / count
limit = bcount if np.mod(bcount, 2) else int(bcount / 2)
2021-03-31 23:11:29 +05:00
#vecs = [ ax(ang(n, bcount)) * normvec * ax(ang(n, bcount)).inverse for n in range(limit) ]
vecs = [ Quaternion(axis = dvec, angle = ang(n, bcount)).rotate(norm) for n in range(limit) ]
2021-03-29 19:39:01 +05:00
2021-03-31 23:11:29 +05:00
logging.info("""createBoundary:
flow direction:\t{}
side boundaries:\t{}
normal direction:\t{}
angles:\t{}
2021-04-02 21:41:48 +05:00
side directions:\t{}""".format(dvec, bcount, norm,
[ ang(n, bcount) / (2 * np.pi) * 360 for n in range(limit) ],
len(vecs))) #[ v for v in vecs ]))
2021-03-31 23:11:29 +05:00
2021-03-29 19:39:01 +05:00
#
flowvec = geompy.MakeVector(
geompy.MakeVertex(0, 0, 0),
2021-03-30 14:49:06 +05:00
geompy.MakeVertex(dvec[0], dvec[1], dvec[2]))
2021-03-29 19:39:01 +05:00
symvec = []
2021-03-31 23:11:29 +05:00
for vec in vecs:
#vec = qvec.vector
2021-03-29 19:39:01 +05:00
symvec.append(geompy.MakeVector(
geompy.MakeVertex(0, 0, 0),
geompy.MakeVertex(vec[0], vec[1], vec[2])))
#
planes = geompy.ExtractShapes(gobj, geompy.ShapeType["FACE"], False)
2021-04-02 21:41:48 +05:00
#planes = geompy.MakeCompound(planes)
planes = geompy.MakeShell(planes)
planes = geompy.ProcessShape(planes,
[ "FixShape", "FixFaceSize", "DropSmallEdges", "SameParameter" ],
[ "FixShape.Tolerance3d", "FixShape.MaxTolerance3d", "FixFaceSize.Tolerance", "DropSmallEdges.Tolerance3d", "SameParameter.Tolerance3d" ],
[ "1e-7", "1", "0.05", "0.05", "1e-7" ])
2021-03-29 19:39:01 +05:00
planes = geompy.MakeCutList(planes, [grains], False)
planes = geompy.ExtractShapes(planes, geompy.ShapeType["FACE"], False)
2021-03-31 23:11:29 +05:00
#print("planes: ", len(planes))
2021-03-29 19:39:01 +05:00
inletplanes = []
outletplanes = []
symetryplanes = [[None, None] for n in range(limit)]
for plane in planes:
nvec = geompy.GetNormal(plane)
fwang = round(geompy.GetAngle(nvec, flowvec), 0)
2021-04-02 21:41:48 +05:00
#print("fwang = ", fwang)
2021-03-29 19:39:01 +05:00
if fwang == 0:
inletplanes.append(plane)
elif fwang == 180:
outletplanes.append(plane)
for n in range(len(symvec)):
2021-03-31 23:11:29 +05:00
sang = round(geompy.GetAngle(nvec, symvec[n]), 0)
2021-04-02 21:41:48 +05:00
#print("sang = ", sang)
2021-03-29 19:39:01 +05:00
if sang == 0:
2021-03-31 23:11:29 +05:00
if symetryplanes[n][0] == None:
symetryplanes[n][0] = []
symetryplanes[n][0].append(plane)
2021-03-29 19:39:01 +05:00
elif sang == 180:
2021-03-31 23:11:29 +05:00
if symetryplanes[n][1] == None:
symetryplanes[n][1] = []
symetryplanes[n][1].append(plane)
2021-04-02 21:41:48 +05:00
#print("\n")
symetryplanesinfo = []
for n in range(len(symetryplanes)):
symetryplanesinfo.append([])
for pair in range(len(symetryplanes[n])):
symetryplanesinfo[n].append(len(symetryplanes[n][pair]))
2021-03-31 23:11:29 +05:00
logging.info("""createBoundary:
planes:\t{}
inlet planes:\t{}
outlet planes:\t{}
2021-04-02 21:41:48 +05:00
side planes:\t{}""".format(len(planes), len(inletplanes), len(outletplanes), symetryplanesinfo))
2021-03-29 19:39:01 +05:00
#
boundary = {}
boundary["inlet"] = createGroup(gobj, inletplanes, grains, "inlet")
boundary["outlet"] = createGroup(gobj, outletplanes, grains, "outlet")
for n in range(len(symetryplanes)):
name = "symetryPlane{}".format(n + 1)
boundary[name + "_1"] = createGroup(gobj, symetryplanes[n][0], grains, name + "_1")
if not symetryplanes[n][1] == None:
boundary[name + "_2"] = createGroup(gobj, symetryplanes[n][1], grains, name + "_2")
# wall
allgroup = geompy.CreateGroup(gobj, geompy.ShapeType["FACE"])
faces = geompy.SubShapeAllIDs(gobj, geompy.ShapeType["FACE"])
geompy.UnionIDs(allgroup, faces)
boundary["wall"] = geompy.CutListOfGroups([allgroup], list(boundary.values()), "wall")
return boundary
2021-03-15 16:16:48 +05:00
def boundaryCreate(gobj, dvec, grains):
xvec = geompy.MakeVector(
geompy.MakeVertex(0, 0, 0),
2021-03-24 21:27:12 +05:00
geompy.MakeVertex(dvec.x[0], dvec.x[1], dvec.x[2]))
#xvec = rotate(xvec, [0, 0, 0.25 * math.pi])
2021-03-15 16:16:48 +05:00
2021-03-24 21:27:12 +05:00
#yvec = rotate(xvec, [0.5 * math.pi, 0, 0])
#zvec = rotate(xvec, [0, 0.5 * math.pi, 0])
yvec = geompy.MakeVector(
geompy.MakeVertex(0, 0, 0),
geompy.MakeVertex(dvec.y[0], dvec.y[1], dvec.y[2]))
zvec = geompy.MakeVector(
geompy.MakeVertex(0, 0, 0),
geompy.MakeVertex(dvec.z[0], dvec.z[1], dvec.z[2]))
geompy.addToStudy(xvec, "xvec")
geompy.addToStudy(yvec, "yvec")
geompy.addToStudy(zvec, "zvec")
2021-03-15 16:16:48 +05:00
logging.info("""boundaryCreate:
direction vectors: x = {}
y = {}
z = {}""".format(dvec.x, dvec.y, dvec.z))
2021-03-15 16:16:48 +05:00
2021-03-24 21:27:12 +05:00
planes = geompy.ExtractShapes(gobj, geompy.ShapeType["FACE"], False)
planes = geompy.MakeCompound(planes)
planes = geompy.MakeCutList(planes, [grains], False)
planes = geompy.ExtractShapes(planes, geompy.ShapeType["FACE"], False)
2021-03-15 16:16:48 +05:00
inletplanes = []
outletplanes = []
2021-03-24 21:27:12 +05:00
#uplanes = []
2021-03-15 16:16:48 +05:00
fwplanes = []
bwplanes = []
lplanes = []
rplanes = []
for plane in planes:
nvec = geompy.GetNormal(plane)
2021-03-24 21:27:12 +05:00
xang = round(geompy.GetAngle(nvec, xvec), 0)
yang = round(geompy.GetAngle(nvec, yvec), 0)
zang = round(geompy.GetAngle(nvec, zvec), 0)
#print(xang, yang, zang, sep="\t")
2021-03-15 16:16:48 +05:00
if xang == 0:
inletplanes.append(plane)
elif xang == 180:
outletplanes.append(plane)
elif yang == 0:
fwplanes.append(plane)
elif yang == 180:
bwplanes.append(plane)
elif zang == 0:
lplanes.append(plane)
elif zang == 180:
rplanes.append(plane)
logging.info("""boundaryCreate:
planes count:\t{}
inlet planes:\t{}
outlet planes:\t{}
forward planes:\t{}
backward planes:\t{}
left planes:\t{}
right planes:\t{}""".format(len(planes), len(inletplanes), len(outletplanes),
len(fwplanes), len(bwplanes), len(lplanes), len(rplanes)))
2021-03-15 16:16:48 +05:00
# Main groups
2021-03-24 21:27:12 +05:00
inlet = createGroup(gobj, inletplanes, grains, "inlet")
outlet = createGroup(gobj, outletplanes, grains, "outlet")
2021-03-15 16:16:48 +05:00
symetryPlaneFW = createGroup(gobj, fwplanes, grains, "symetryPlaneFW")
symetryPlaneBW = createGroup(gobj, bwplanes, grains, "symetryPlaneBW")
symetryPlaneL = createGroup(gobj, lplanes, grains, "symetryPlaneL")
symetryPlaneR = createGroup(gobj, rplanes, grains, "symetryPlaneR")
# wall
allgroup = geompy.CreateGroup(gobj, geompy.ShapeType["FACE"])
faces = geompy.SubShapeAllIDs(gobj, geompy.ShapeType["FACE"])
geompy.UnionIDs(allgroup, faces)
wall = geompy.CutListOfGroups([allgroup],
[inlet, outlet, symetryPlaneFW, symetryPlaneBW, symetryPlaneL, symetryPlaneR], "wall")
boundary = {
"inlet": inlet,
"outlet": outlet,
"symetryPlaneFW": symetryPlaneFW,
"symetryPlaneBW": symetryPlaneBW,
"symetryPlaneL": symetryPlaneL,
"symetryPlaneR": symetryPlaneR,
"wall": wall
}
return boundary