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" #"faceCentered"
] ]
directions = [ directions = [
[1, 0, 0], #[1, 0, 0],
[0, 0, 1], [0, 0, 1],
[1, 1, 1] #[1, 1, 1]
] ]
fillet = 1 fillet = 1
@ -40,7 +40,7 @@ def createTasks():
for structure in structures: for structure in structures:
if structure == "simple": 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 ] #theta = [ 0.01, 0.28 ]
elif structure == "faceCentered": elif structure == "faceCentered":
@ -114,47 +114,40 @@ def calculate(tasks):
structure type:\t{} structure type:\t{}
coefficient:\t{} coefficient:\t{}
flow direction:\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]: foam_utils.createPatch(dictfile = "system/createPatchDict.symetry")
shutil.copy(os.path.join(task.saveto, "system/createPatchDict.symetry"),
os.path.join(task.saveto, "system/createPatchDict"))
logging.info("""createPatch:
file:\tcreatePatchDict.symetry""")
#else: foam_utils.checkMesh()
# 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)
scale = (1e-5, 1e-5, 1e-5) scale = (1e-5, 1e-5, 1e-5)
foam_utils.transformPoints(casepath, "{}".format(scale).replace(",", "")) foam_utils.transformPoints(scale)
logging.info("""transformPoints:
scale:\t{}""".format(scale))
foam_utils.decomposePar(casepath) foam_utils.decomposePar()
foam_utils.potentialFoam(casepath) foam_utils.renumberMesh()
foam_utils.potentialFoam()
for n in range(4): 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") "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)") "boundaryField.inlet.value", "uniform (0 0 0)")
foam_utils.simpleFoam(casepath) foam_utils.simpleFoam()
os.chdir(ROOT) os.chdir(ROOT)
#logging.info(fancyline)
def postprocessing(tasks): def postprocessing(tasks):

View File

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

View File

@ -2,21 +2,32 @@ import os, sys, shutil
import subprocess import subprocess
import logging import logging
import time import time
import re
from datetime import timedelta 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 = [] cmd = []
if parallel:
mpirun = ["mpirun", "-np", "4", "--oversubscribe"]
cmd = mpirun + [name, "-case", case] + args if useMPI:
logging.info("Running '{}'".format(" ".join(cmd))) 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, with subprocess.Popen(cmd,
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE) as p, \ stderr = subprocess.PIPE) as p, \
open("{}/{}.log".format(case, name), "wb") as logfile: open(logpath, "wb") as logfile:
for line in p.stdout: for line in p.stdout:
#sys.stdout.buffer.write(line) #sys.stdout.buffer.write(line)
@ -26,39 +37,84 @@ def application(name, case, log=False, args=[], parallel=False):
# logfile.write(line) # logfile.write(line)
out, err = p.communicate() out, err = p.communicate()
logfile.write(err)
if err: if err and stderr:
logging.error("""{}: logging.error("""{}:
{}""".format(name, str(err, "utf-8"))) {}""".format(name, str(err, "utf-8")))
return p.returncode return out, p.returncode
def ideasUnvToFoam(case, mesh): def foamVersion() -> str:
application("ideasUnvToFoam", case, True, [mesh])
def createPatch(case): return "OpenFOAM-{}".format(os.environ["WM_PROJECT_VERSION"])
application("createPatch", case, True, ["-overwrite"])
def transformPoints(case, vector): def ideasUnvToFoam(mesh: str, case: str = None):
application("transformPoints", case, True, ["-scale", vector]) application("ideasUnvToFoam", mesh, case = case, stderr = True)
def checkMesh(case):
application("checkMesh", case, True, ["-allGeometry", "-allTopology"]) def createPatch(dictfile: str = None, case: str = None):
args = ["-overwrite"]
def foamDictionaryGet(case, foamFile, entry):
application("foamDictionary", case, True, [foamFile, "-entry", entry]) if dictfile:
args.extend(["-dict", dictfile])
def foamDictionarySet(case, foamFile, entry, value):
application("foamDictionary", case, True, [foamFile, "-entry", entry, "-set", value]) application("createPatch", *args, case = case, stderr = True)
def decomposePar(case):
application("decomposePar", case, True) def transformPoints(scale: tuple, case: str = None):
scale_ = "{}".format(scale).replace(",", "")
def potentialFoam(case):
application("potentialFoam", case, True, ["-parallel"], True) application("transformPoints", "-scale", scale_, case = case, stderr = True)
def simpleFoam(case):
application("simpleFoam", case, True, ["-parallel"], 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 salome
import subprocess import subprocess
import logging import logging
import sys
def hasDesktop() -> bool: def hasDesktop() -> bool:
return salome.sg.hasDesktop() return salome.sg.hasDesktop()
@ -16,20 +17,41 @@ def startServer(port):
return p 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]))], cmd = ["salome", "start", "--shutdown-servers=1", "--port", str(port), "-t",
stderr = subprocess.STDOUT) scriptpath, "args:{}".format(", ".join([str(arg) for arg in args]))]
_, err = p.communicate()
if err: logging.info("salome: {}".format(cmd[1 : 6]))
if p.returncode == 1: #logpath = os.path.join()
logging.error(err)
else: #p = subprocess.Popen(["salome", "start", "--shutdown-servers=1", "--port", str(port), "-t", scriptpath, "args:{}".format(", ".join([str(arg) for arg in args]))],
logging.warning(err) # 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 return p.returncode