2021-03-22 22:49:56 +05:00
|
|
|
import os, sys
|
|
|
|
from collections import namedtuple
|
2021-03-24 21:27:12 +05:00
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
import multiprocessing
|
2021-03-25 23:22:21 +05:00
|
|
|
import shutil
|
2021-03-22 22:49:56 +05:00
|
|
|
|
|
|
|
ROOT = os.getcwd()
|
|
|
|
sys.path.append(ROOT)
|
|
|
|
|
2021-03-24 21:27:12 +05:00
|
|
|
LOG = os.path.join(ROOT, "logs")
|
|
|
|
BUILD = os.path.join(ROOT, "build")
|
|
|
|
|
|
|
|
from src import utils
|
|
|
|
from src import salome_utils
|
2021-03-25 23:22:21 +05:00
|
|
|
from src import foam_utils
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
def createTasks():
|
2021-04-12 16:00:26 +05:00
|
|
|
###
|
|
|
|
# Control variables
|
|
|
|
##
|
2021-03-22 22:49:56 +05:00
|
|
|
structures = [
|
2021-04-12 16:00:26 +05:00
|
|
|
"simple",
|
|
|
|
#"bodyCentered",
|
|
|
|
#"faceCentered"
|
2021-03-22 22:49:56 +05:00
|
|
|
]
|
|
|
|
directions = [
|
2021-04-05 22:15:47 +05:00
|
|
|
[1, 0, 0],
|
|
|
|
[0, 0, 1],
|
2021-04-01 15:48:20 +05:00
|
|
|
[1, 1, 1]
|
2021-03-22 22:49:56 +05:00
|
|
|
]
|
2021-04-08 00:13:27 +05:00
|
|
|
fillet = 1
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-04-12 16:00:26 +05:00
|
|
|
###
|
|
|
|
# Tasks
|
|
|
|
##
|
2021-04-05 22:15:47 +05:00
|
|
|
Task = namedtuple("Task", ["structure", "coeff", "fillet", "direction", "saveto"])
|
2021-03-24 21:27:12 +05:00
|
|
|
tasks = []
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-24 21:27:12 +05:00
|
|
|
for structure in structures:
|
2021-04-12 16:00:26 +05:00
|
|
|
if structure == "simple":
|
|
|
|
theta = [c * 0.01 for c in range(1, 28 + 1)]
|
|
|
|
#theta = [ 0.01, 0.28 ]
|
2021-03-24 21:27:12 +05:00
|
|
|
|
2021-04-12 16:00:26 +05:00
|
|
|
elif structure == "faceCentered":
|
2021-04-08 00:13:27 +05:00
|
|
|
#theta = [c * 0.01 for c in range(1, 13 + 1)]
|
|
|
|
theta = [ 0.01, 0.13 ]
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-04-12 16:00:26 +05:00
|
|
|
elif structure == "bodyCentered":
|
2021-04-08 00:13:27 +05:00
|
|
|
#theta = [c * 0.01 for c in range(1, 18 + 1)]
|
|
|
|
theta = [ 0.01, 0.13, 0.14, 0.18 ]
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-24 21:27:12 +05:00
|
|
|
for coeff in theta:
|
|
|
|
for direction in directions:
|
2021-03-25 23:22:21 +05:00
|
|
|
saveto = os.path.join(BUILD, structure, "coeff-{}".format(coeff),
|
|
|
|
"direction-{}{}{}".format(direction[0], direction[1], direction[2]))
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
if not os.path.exists(saveto):
|
|
|
|
os.makedirs(saveto)
|
|
|
|
|
2021-04-05 22:15:47 +05:00
|
|
|
t = Task(structure, coeff, fillet, direction, saveto)
|
2021-03-25 23:22:21 +05:00
|
|
|
tasks.append(t)
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
return tasks
|
2021-03-22 22:49:56 +05:00
|
|
|
|
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
def createMesh(tasks):
|
|
|
|
scriptpath = os.path.join(ROOT, "samples/__init__.py")
|
|
|
|
port = 2810
|
2021-04-05 22:15:47 +05:00
|
|
|
errors = 0
|
2021-03-22 22:49:56 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
for task in tasks:
|
2021-04-01 15:48:20 +05:00
|
|
|
logging.info("-" * 80)
|
|
|
|
logging.info("""createMesh:
|
|
|
|
task:\t{} / {}""".format(tasks.index(task) + 1, len(tasks)))
|
2021-04-05 22:15:47 +05:00
|
|
|
start_time = time.monotonic()
|
2021-04-01 15:48:20 +05:00
|
|
|
|
2021-04-05 22:15:47 +05:00
|
|
|
returncode = salome_utils.runExecute(port, scriptpath, task.structure, task.coeff, task.fillet, "".join([str(coord) for coord in task.direction]), os.path.join(task.saveto, "mesh.unv"))
|
2021-03-25 23:22:21 +05:00
|
|
|
|
2021-04-05 22:15:47 +05:00
|
|
|
end_time = time.monotonic()
|
|
|
|
logging.info("createMesh: elapsed time: {}".format(timedelta(seconds=end_time - start_time)))
|
|
|
|
logging.info("salome: return code:\t{}".format(returncode))
|
2021-04-01 15:48:20 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
if returncode == 1:
|
2021-04-05 22:15:47 +05:00
|
|
|
#break
|
|
|
|
errors += 1
|
|
|
|
pass
|
|
|
|
|
|
|
|
return errors
|
2021-03-25 23:22:21 +05:00
|
|
|
|
2021-03-03 16:24:47 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
def calculate(tasks):
|
|
|
|
foamCase = [ "0", "constant", "system" ]
|
|
|
|
rmDirs = ["0", "constant", "system", "postProcessing", "logs"] + [ "processor{}".format(n) for n in range(4)]
|
2021-04-02 21:41:48 +05:00
|
|
|
#fancyline = "--------------------------------------------------------------------------------"
|
2021-03-24 21:27:12 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
for task in tasks:
|
|
|
|
|
|
|
|
for d in rmDirs:
|
|
|
|
if os.path.exists(os.path.join(task.saveto, d)):
|
|
|
|
shutil.rmtree(os.path.join(task.saveto, d))
|
|
|
|
|
|
|
|
for d in foamCase:
|
|
|
|
if not os.path.exists(os.path.join(task.saveto, d)):
|
|
|
|
shutil.copytree(os.path.join(ROOT, "src/cubicFoam", d),
|
|
|
|
os.path.join(task.saveto, d))
|
|
|
|
|
|
|
|
os.chdir(task.saveto)
|
|
|
|
casepath = "."
|
|
|
|
|
2021-04-02 21:41:48 +05:00
|
|
|
logging.info("-" * 80)
|
|
|
|
logging.info("""calculate:
|
|
|
|
task:\t{} / {}
|
2021-03-25 23:22:21 +05:00
|
|
|
structure type:\t{}
|
|
|
|
coefficient:\t{}
|
|
|
|
flow direction:\t{}
|
2021-04-02 21:41:48 +05:00
|
|
|
path:\t{}\n""".format(tasks.index(task) + 1, len(tasks) , task.structure, task.coeff, task.direction, task.saveto))
|
2021-03-25 23:22:21 +05:00
|
|
|
|
|
|
|
foam_utils.ideasUnvToFoam(casepath, "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""")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-03-26 17:54:37 +05:00
|
|
|
foam_utils.checkMesh(casepath)
|
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
scale = (1e-5, 1e-5, 1e-5)
|
|
|
|
foam_utils.transformPoints(casepath, "{}".format(scale).replace(",", ""))
|
|
|
|
logging.info("""transformPoints:
|
|
|
|
scale:\t{}""".format(scale))
|
|
|
|
|
|
|
|
foam_utils.decomposePar(casepath)
|
|
|
|
|
|
|
|
foam_utils.potentialFoam(casepath)
|
|
|
|
|
|
|
|
for n in range(4):
|
|
|
|
foam_utils.foamDictionarySet(casepath, "processor{}/0/U".format(n),
|
|
|
|
"boundaryField.inlet.type", "pressureInletVelocity")
|
|
|
|
foam_utils.foamDictionarySet(casepath, "processor{}/0/U".format(n),
|
|
|
|
"boundaryField.inlet.value", "uniform (0 0 0)")
|
|
|
|
|
|
|
|
foam_utils.simpleFoam(casepath)
|
2021-03-24 21:27:12 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
os.chdir(ROOT)
|
2021-03-24 21:27:12 +05:00
|
|
|
|
2021-04-02 21:41:48 +05:00
|
|
|
#logging.info(fancyline)
|
2021-03-25 23:22:21 +05:00
|
|
|
|
2021-03-24 21:27:12 +05:00
|
|
|
|
2021-03-22 15:44:22 +05:00
|
|
|
if __name__ == "__main__":
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
if not os.path.exists(LOG):
|
|
|
|
os.makedirs(LOG)
|
|
|
|
|
|
|
|
if not os.path.exists(BUILD):
|
|
|
|
os.makedirs(BUILD)
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-22 15:44:22 +05:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
format="%(levelname)s: %(message)s",
|
|
|
|
handlers = [
|
|
|
|
logging.StreamHandler(),
|
2021-03-25 23:22:21 +05:00
|
|
|
logging.FileHandler("{}/cubic.log".format(LOG))
|
2021-03-22 15:44:22 +05:00
|
|
|
])
|
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
# TODO: add force arg
|
|
|
|
Args = namedtuple("Args", ["mesh", "calc"])
|
2021-03-03 16:24:47 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
action = sys.argv[1]
|
|
|
|
|
|
|
|
if action == "mesh":
|
|
|
|
args = Args(True, False)
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
elif action == "calc":
|
|
|
|
args = Args(False, True)
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
elif action == "all":
|
|
|
|
args = Args(True, True)
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
else:
|
|
|
|
args = Args(True, True)
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
tasks = createTasks()
|
|
|
|
logging.info("Tasks: {}".format(len(tasks)))
|
2021-03-22 15:44:22 +05:00
|
|
|
|
2021-03-25 23:22:21 +05:00
|
|
|
if args.mesh:
|
|
|
|
start_time = time.monotonic()
|
2021-04-02 21:41:48 +05:00
|
|
|
#logging.info("Started at {}".format(timedelta(seconds=start_time)))
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-04-05 22:15:47 +05:00
|
|
|
errors = createMesh(tasks)
|
2021-03-25 23:22:21 +05:00
|
|
|
|
|
|
|
end_time = time.monotonic()
|
2021-04-02 21:41:48 +05:00
|
|
|
logging.info("-" * 80)
|
2021-04-05 22:15:47 +05:00
|
|
|
logging.info("Elapsed time:\t{}".format(timedelta(seconds=end_time - start_time)))
|
|
|
|
logging.info("Errors:\t{}".format(errors))
|
2021-03-25 23:22:21 +05:00
|
|
|
|
|
|
|
if args.calc:
|
2021-04-02 21:41:48 +05:00
|
|
|
start_time = time.monotonic()
|
|
|
|
#logging.info("Started at {}".format(timedelta(seconds=start_time)))
|
|
|
|
|
|
|
|
calculate(tasks)
|
|
|
|
|
|
|
|
end_time = time.monotonic()
|
|
|
|
logging.info("-" * 80)
|
|
|
|
logging.info("Elapsed time: {}".format(timedelta(seconds=end_time - start_time)))
|
|
|
|
|
2021-03-03 22:16:36 +05:00
|
|
|
|
2021-03-03 16:24:47 +05:00
|
|
|
|