anisotropy/run.py

187 lines
5.3 KiB
Python
Raw Normal View History

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
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
from src import foam_utils
2021-03-22 22:49:56 +05:00
def createTasks():
2021-03-22 22:49:56 +05:00
structures = [
"simpleCubic",
"bodyCenteredCubic",
"faceCenteredCubic"
]
directions = [
[1, 0, 0],
[0, 0, 1],
2021-03-24 21:27:12 +05:00
#[1, 1, 1]
2021-03-22 22:49:56 +05:00
]
Task = namedtuple("Task", ["structure", "coeff", "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:
if structure == "simpleCubic":
2021-03-31 23:11:29 +05:00
theta = [c * 0.01 for c in range(1, 29 + 1)]
2021-03-24 21:27:12 +05:00
elif structure == "faceCenteredCubic":
theta = []
2021-03-22 22:49:56 +05:00
2021-03-24 21:27:12 +05:00
elif structure == "bodyCenteredCubic":
theta = []
2021-03-22 22:49:56 +05:00
2021-03-24 21:27:12 +05:00
for coeff in theta:
for direction in directions:
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
if not os.path.exists(saveto):
os.makedirs(saveto)
t = Task(structure, coeff, direction, saveto)
tasks.append(t)
2021-03-22 22:49:56 +05:00
return tasks
2021-03-22 22:49:56 +05:00
def createMesh(tasks):
scriptpath = os.path.join(ROOT, "samples/__init__.py")
port = 2810
2021-03-22 22:49:56 +05:00
for task in tasks:
returncode = salome_utils.runExecute(port, scriptpath, task.structure, task.coeff, "".join([str(coord) for coord in task.direction]), os.path.join(task.saveto, "mesh.unv"))
logging.info("Return code: {}".format(returncode))
if returncode == 1:
break
2021-03-03 16:24:47 +05:00
def calculate(tasks):
foamCase = [ "0", "constant", "system" ]
rmDirs = ["0", "constant", "system", "postProcessing", "logs"] + [ "processor{}".format(n) for n in range(4)]
fancyline = "--------------------------------------------------------------------------------"
2021-03-24 21:27:12 +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 = "."
logging.info(fancyline)
logging.info("""Args:
structure type:\t{}
coefficient:\t{}
flow direction:\t{}
path:\t{}\n""".format(task.structure, task.coeff, task.direction, task.saveto))
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)
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
os.chdir(ROOT)
2021-03-24 21:27:12 +05:00
logging.info(fancyline)
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
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(),
logging.FileHandler("{}/cubic.log".format(LOG))
2021-03-22 15:44:22 +05:00
])
# TODO: add force arg
Args = namedtuple("Args", ["mesh", "calc"])
2021-03-03 16:24:47 +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
elif action == "calc":
args = Args(False, True)
2021-03-03 22:16:36 +05:00
elif action == "all":
args = Args(True, True)
2021-03-03 22:16:36 +05:00
else:
args = Args(True, True)
2021-03-03 22:16:36 +05:00
tasks = createTasks()
logging.info("Tasks: {}".format(len(tasks)))
2021-03-22 15:44:22 +05:00
if args.mesh:
start_time = time.monotonic()
logging.info("Started at {}".format(timedelta(seconds=start_time)))
2021-03-03 22:16:36 +05:00
createMesh(tasks)
end_time = time.monotonic()
logging.info("Elapsed time: {}".format(timedelta(seconds=end_time - start_time)))
if args.calc:
calculate(tasks)
2021-03-03 22:16:36 +05:00
2021-03-03 16:24:47 +05:00