anisotropy/run.py

235 lines
6.5 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-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-14 15:13:41 +05:00
#[1, 0, 0],
2021-04-05 22:15:47 +05:00
[0, 0, 1],
2021-04-14 15:13:41 +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":
2021-04-14 15:13:41 +05:00
theta = [0.01] #[c * 0.01 for c in range(1, 28 + 1)]
2021-04-12 16:00:26 +05:00
#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:
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)
2021-04-05 22:15:47 +05:00
t = Task(structure, coeff, fillet, 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-04-05 22:15:47 +05:00
errors = 0
2021-03-22 22:49:56 +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-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
if returncode == 1:
2021-04-05 22:15:47 +05:00
#break
errors += 1
pass
return errors
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)]
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 = "."
2021-04-02 21:41:48 +05:00
logging.info("-" * 80)
logging.info("""calculate:
task:\t{} / {}
structure type:\t{}
coefficient:\t{}
flow direction:\t{}
2021-04-14 15:13:41 +05:00
path:\t{}""".format(
tasks.index(task) + 1,
len(tasks) ,
task.structure,
task.coeff,
task.direction,
task.saveto
))
foam_utils.ideasUnvToFoam("mesh.unv")
2021-04-14 15:13:41 +05:00
foam_utils.createPatch(dictfile = "system/createPatchDict.symetry")
2021-04-14 15:13:41 +05:00
foam_utils.checkMesh()
2021-03-26 17:54:37 +05:00
scale = (1e-5, 1e-5, 1e-5)
2021-04-14 15:13:41 +05:00
foam_utils.transformPoints(scale)
2021-04-14 15:13:41 +05:00
foam_utils.decomposePar()
foam_utils.renumberMesh()
2021-04-14 15:13:41 +05:00
foam_utils.potentialFoam()
for n in range(4):
2021-04-14 15:13:41 +05:00
foam_utils.foamDictionary("processor{}/0/U".format(n),
"boundaryField.inlet.type", "pressureInletVelocity")
2021-04-14 15:13:41 +05:00
foam_utils.foamDictionary("processor{}/0/U".format(n),
"boundaryField.inlet.value", "uniform (0 0 0)")
2021-04-14 15:13:41 +05:00
foam_utils.simpleFoam()
2021-03-24 21:27:12 +05:00
os.chdir(ROOT)
2021-03-24 21:27:12 +05:00
2021-04-13 12:17:10 +05:00
def postprocessing(tasks):
2021-04-13 22:06:25 +05:00
surfaceFieldValue = {}
porosity = {}
for task in tasks:
direction = "direction-{}{}{}".format(task.direction[0], task.direction[1], task.direction[2])
path = os.path.join(BUILD, task.structure, "postProcessing", direction)
surfaceFieldValuePath = os.path.join(task.saveto, "postProcessing", "")
if not os.path.exists(path):
os.makedirs(path)
surfaceFieldValues = [ line.strip().split() for line in open(surfaceFieldValuePath, "r").readlines() ]
with open(os.path.join(path, "porosity.dat")) as io:
io.write("{}\t{}".format(task.coeff, surfaceFieldValues[-1][1]))
2021-04-13 12:17:10 +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
2021-04-13 22:06:25 +05:00
Args = namedtuple("Args", ["mesh", "calc", "pp"])
2021-03-03 16:24:47 +05:00
if len(sys.argv) > 1:
action = sys.argv[1]
if action == "mesh":
2021-04-13 22:06:25 +05:00
args = Args(True, False, False)
2021-03-03 22:16:36 +05:00
elif action == "calc":
2021-04-13 22:06:25 +05:00
args = Args(False, True, False)
elif action == "pp":
args = Args(False, False, True)
2021-03-03 22:16:36 +05:00
elif action == "all":
2021-04-13 22:06:25 +05:00
args = Args(True, True, True)
2021-03-03 22:16:36 +05:00
else:
2021-04-13 22:06:25 +05:00
args = Args(True, 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()
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)
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))
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-04-13 22:06:25 +05:00
if args.pp:
postprocessing(tasks)
2021-03-03 16:24:47 +05:00