Destruction [5]: FOAM and Salome

This commit is contained in:
L-Nafaryus 2021-04-14 15:13:41 +05:00
parent 8f941bc451
commit 6d911461c8
4 changed files with 147 additions and 76 deletions

51
run.py
View File

@ -26,9 +26,9 @@ def createTasks():
#"faceCentered"
]
directions = [
[1, 0, 0],
#[1, 0, 0],
[0, 0, 1],
[1, 1, 1]
#[1, 1, 1]
]
fillet = 1
@ -40,7 +40,7 @@ def createTasks():
for structure in structures:
if structure == "simple":
theta = [c * 0.01 for c in range(1, 28 + 1)]
theta = [0.01] #[c * 0.01 for c in range(1, 28 + 1)]
#theta = [ 0.01, 0.28 ]
elif structure == "faceCentered":
@ -114,47 +114,40 @@ def calculate(tasks):
structure type:\t{}
coefficient:\t{}
flow direction:\t{}
path:\t{}\n""".format(tasks.index(task) + 1, len(tasks) , task.structure, task.coeff, task.direction, task.saveto))
path:\t{}""".format(
tasks.index(task) + 1,
len(tasks) ,
task.structure,
task.coeff,
task.direction,
task.saveto
))
foam_utils.ideasUnvToFoam(casepath, "mesh.unv")
foam_utils.ideasUnvToFoam("mesh.unv")
#if not task.direction == [1, 1, 1]:
shutil.copy(os.path.join(task.saveto, "system/createPatchDict.symetry"),
os.path.join(task.saveto, "system/createPatchDict"))
logging.info("""createPatch:
file:\tcreatePatchDict.symetry""")
foam_utils.createPatch(dictfile = "system/createPatchDict.symetry")
#else:
# shutil.copy(os.path.join(task.saveto, "system/createPatchDict.cyclic"),
# os.path.join(task.saveto, "system/createPatchDict"))
# logging.info("""createPatch:
# file:\tcreatePatchDict.cyclic""")
foam_utils.createPatch(casepath)
foam_utils.checkMesh(casepath)
foam_utils.checkMesh()
scale = (1e-5, 1e-5, 1e-5)
foam_utils.transformPoints(casepath, "{}".format(scale).replace(",", ""))
logging.info("""transformPoints:
scale:\t{}""".format(scale))
foam_utils.transformPoints(scale)
foam_utils.decomposePar(casepath)
foam_utils.decomposePar()
foam_utils.potentialFoam(casepath)
foam_utils.renumberMesh()
foam_utils.potentialFoam()
for n in range(4):
foam_utils.foamDictionarySet(casepath, "processor{}/0/U".format(n),
foam_utils.foamDictionary("processor{}/0/U".format(n),
"boundaryField.inlet.type", "pressureInletVelocity")
foam_utils.foamDictionarySet(casepath, "processor{}/0/U".format(n),
foam_utils.foamDictionary("processor{}/0/U".format(n),
"boundaryField.inlet.value", "uniform (0 0 0)")
foam_utils.simpleFoam(casepath)
foam_utils.simpleFoam()
os.chdir(ROOT)
#logging.info(fancyline)
def postprocessing(tasks):

View File

@ -74,9 +74,9 @@ def genMesh(stype, theta, fillet, direction, saveto):
parameters = mesh_utils.Parameters(
minSize = 0.001,
maxSize = 0.1,
growthRate = 0.1,
nbSegPerEdge = 5,
nbSegPerRadius = 10,
growthRate = 0.5,
nbSegPerEdge = 0.5,
nbSegPerRadius = 0.5,
chordalErrorEnabled = False,
chordalError = -1,
secondOrder = False,

View File

@ -2,21 +2,32 @@ import os, sys, shutil
import subprocess
import logging
import time
import re
from datetime import timedelta
def application(name, case, log=False, args=[], parallel=False):
def application(name: str, *args: str, case: str = None, stderr: bool = True, useMPI: bool = False) -> int:
mpirun = []
if parallel:
mpirun = ["mpirun", "-np", "4", "--oversubscribe"]
cmd = []
cmd = mpirun + [name, "-case", case] + args
logging.info("Running '{}'".format(" ".join(cmd)))
if useMPI:
nprocs = os.cpu_count()
cmd.extend(["mpirun", "-np", str(nprocs), "--oversubscribe"])
cmd.append(name)
if case:
cmd.extend(["-case", case])
if args:
cmd.extend([*args])
logging.info("{}: {}".format(name, [*args]))
logpath = os.path.join(case if case else "", "{}.log".format(name))
with subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE) as p, \
open("{}/{}.log".format(case, name), "wb") as logfile:
open(logpath, "wb") as logfile:
for line in p.stdout:
#sys.stdout.buffer.write(line)
@ -26,39 +37,84 @@ def application(name, case, log=False, args=[], parallel=False):
# logfile.write(line)
out, err = p.communicate()
logfile.write(err)
if err:
if err and stderr:
logging.error("""{}:
{}""".format(name, str(err, "utf-8")))
return p.returncode
return out, p.returncode
def ideasUnvToFoam(case, mesh):
application("ideasUnvToFoam", case, True, [mesh])
def foamVersion() -> str:
def createPatch(case):
application("createPatch", case, True, ["-overwrite"])
return "OpenFOAM-{}".format(os.environ["WM_PROJECT_VERSION"])
def transformPoints(case, vector):
application("transformPoints", case, True, ["-scale", vector])
def checkMesh(case):
application("checkMesh", case, True, ["-allGeometry", "-allTopology"])
def foamDictionaryGet(case, foamFile, entry):
application("foamDictionary", case, True, [foamFile, "-entry", entry])
def foamDictionarySet(case, foamFile, entry, value):
application("foamDictionary", case, True, [foamFile, "-entry", entry, "-set", value])
def decomposePar(case):
application("decomposePar", case, True)
def potentialFoam(case):
application("potentialFoam", case, True, ["-parallel"], True)
def simpleFoam(case):
application("simpleFoam", case, True, ["-parallel"], True)
def ideasUnvToFoam(mesh: str, case: str = None):
application("ideasUnvToFoam", mesh, case = case, stderr = True)
def createPatch(dictfile: str = None, case: str = None):
args = ["-overwrite"]
if dictfile:
args.extend(["-dict", dictfile])
application("createPatch", *args, case = case, stderr = True)
def transformPoints(scale: tuple, case: str = None):
scale_ = "{}".format(scale).replace(",", "")
application("transformPoints", "-scale", scale_, case = case, stderr = True)
def checkMesh(case: str = None):
application("checkMesh", "-allGeometry", "-allTopology", case = case, stderr = True)
with open("checkMesh.log", "r") as io:
warnings = []
for line in io:
if re.search("\*\*\*", line):
warnings.append(line.replace("***", "").strip())
if warnings:
logging.warning("checkMesh:\n\t{}".format("\n\t".join(warnings)))
def foamDictionary(filepath: str, entry: str, value: str = None, case: str = None):
args = [filepath, "-entry", entry]
if value:
args.extend(["-set", value])
application("foamDictionary", *args, case = case, stderr = False)
#def foamDictionaryGet(case, foamFile, entry):
# application("foamDictionary", case, True, [foamFile, "-entry", entry])
#def foamDictionarySet(case, foamFile, entry, value):
# application("foamDictionary", case, False, [foamFile, "-entry", entry, "-set", value])
def decomposePar(case: str = None):
application("decomposePar", case = case, stderr = True)
def renumberMesh(case: str = None):
application("renumberMesh", "-parallel", "-overwrite", useMPI = True, case = case, stderr = True)
def potentialFoam(case: str = None):
application("potentialFoam", "-parallel", useMPI = True, case = case, stderr = True)
def simpleFoam(case: str = None):
application("simpleFoam", "-parallel", useMPI = True, case = case, stderr = True)
with open("simpleFoam.log", "r") as io:
for line in io:
if re.search("solution converged", line):
logging.info("simpleFoam:\n\t{}".format(line))

View File

@ -1,6 +1,7 @@
#import salome
import subprocess
import logging
import sys
def hasDesktop() -> bool:
return salome.sg.hasDesktop()
@ -16,20 +17,41 @@ def startServer(port):
return p
def runExecute(port, scriptpath, *args):
def salomeVersion() -> str:
return "Salome 9.6.0"
logging.info("Starting SALOME on port {}".format(port))
def runExecute(port: int, scriptpath: str, *args) -> int:
p = subprocess.Popen(["salome", "start", "--shutdown-servers=1", "--port", str(port), "-t", scriptpath, "args:{}".format(", ".join([str(arg) for arg in args]))],
stderr = subprocess.STDOUT)
_, err = p.communicate()
cmd = ["salome", "start", "--shutdown-servers=1", "--port", str(port), "-t",
scriptpath, "args:{}".format(", ".join([str(arg) for arg in args]))]
if err:
if p.returncode == 1:
logging.error(err)
logging.info("salome: {}".format(cmd[1 : 6]))
#logpath = os.path.join()
else:
logging.warning(err)
#p = subprocess.Popen(["salome", "start", "--shutdown-servers=1", "--port", str(port), "-t", scriptpath, "args:{}".format(", ".join([str(arg) for arg in args]))],
# stderr = subprocess.STDOUT)
#_, err = p.communicate()
with subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE) as p:
#for line in p.stdout:
# sys.stdout.buffer.write(line)
# logfile.write(line)
out, err = p.communicate()
print(str(err, "utf-8"))
#logfile.write(err)
if err and p.returncode == 1:
logging.error("salome:\n\t{}".format(str(err, "utf-8")))
#if err:
# if p.returncode == 1:
# logging.error(err)
# else:
# logging.warning(err)
return p.returncode