anisotropy/run.py

227 lines
5.8 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
import config
from config import logger
#from src import applogger
2021-03-24 21:27:12 +05:00
from src import utils
from src import salome_utils
from src import foam_utils
2021-03-22 22:49:56 +05:00
def main():
if not os.path.exists(config.LOG):
os.makedirs(config.LOG)
if not os.path.exists(config.BUILD):
os.makedirs(config.BUILD)
#global logger
#logger = applogger.Logger()
2021-04-26 14:32:45 +05:00
check = checkEnv()
if check:
return
tasks = createTasks()
for task in tasks:
2021-04-15 18:57:01 +05:00
logger.fancyline()
logger.info(f"""main:
task:\t{tasks.index(task) + 1} / {len(tasks)}
cpu count:\t{os.cpu_count()}
structure:\t{task.structure}
direction:\t{task.direction}
theta:\t{task.theta}
fillet:\t{task.fillet}
export:\t{task.export}""")
if not os.path.exists(task.export):
os.makedirs(task.export)
createMesh(task)
2021-04-16 15:28:04 +05:00
if os.path.exists(os.path.join(task.export, "mesh.unv")):
task.mesh = True
returncode = calculate(task)
if not returncode:
task.flow = True
with open(os.path.join(config.LOG, "tasks.log"), "a") as io:
idx = tasks.index(task)
io.write(f"""Task {idx}:
structure:\t{task.structure}
direction:\t{task.direction}
theta:\t{task.theta}
mesh:\t{task.mesh}
flow:\t{task.flow}\n""")
logger.info(f"Warnings: {logger.warnings}\tErrors: {logger.errors}")
def checkEnv():
2021-04-26 14:32:45 +05:00
missed = False
try:
pythonVersion = "Python {}".format(sys.version.split(" ")[0])
salomeVersion = salome_utils.salomeVersion()
foamVersion = foam_utils.foamVersion()
except Exception:
logger.critical("Missed environment")
2021-04-26 14:32:45 +05:00
missed = True
else:
2021-04-26 14:32:45 +05:00
logger.info(f"environment:\n\t{pythonVersion}\n\t{salomeVersion}\n\t{foamVersion}")
finally:
return missed
2021-04-16 15:28:04 +05:00
class Task:
def __init__(self, **kwargs):
for (k, v) in kwargs.items():
setattr(self, k, v)
def createTasks():
2021-04-16 15:28:04 +05:00
#Task = namedtuple("Task", ["structure", "theta", "fillet", "direction", "export"])
2021-03-24 21:27:12 +05:00
tasks = []
structures = [ getattr(config, s)() for s in config.structures ]
2021-03-22 22:49:56 +05:00
2021-03-24 21:27:12 +05:00
for structure in structures:
for direction in structure.directions:
for theta in structure.theta:
name = type(structure).__name__
export = os.path.join(
config.BUILD,
name,
"direction-{}{}{}".format(*direction),
"theta-{}".format(theta)
)
2021-04-16 15:28:04 +05:00
task = Task(
structure = name,
theta = theta,
fillet = structure.fillet,
direction = direction,
export = export,
mesh = False,
flow = False
)
2021-03-24 21:27:12 +05:00
2021-04-16 15:28:04 +05:00
tasks.append(task)
return tasks
2021-03-22 22:49:56 +05:00
def createMesh(task):
scriptpath = os.path.join(config.ROOT, "samples/__init__.py")
port = 2810
stime = time.monotonic()
args = (
task.structure,
task.theta,
int(task.fillet),
"".join([str(coord) for coord in task.direction]),
os.path.join(task.export, "mesh.unv"),
config.ROOT
)
returncode = salome_utils.runExecute(port, scriptpath, *args)
etime = time.monotonic()
logger.info("createMesh: elapsed time: {}".format(timedelta(seconds = etime - stime)))
2021-03-22 22:49:56 +05:00
def calculate(task):
foamCase = [ "0", "constant", "system" ]
2021-03-22 22:49:56 +05:00
os.chdir(task.export)
foam_utils.foamClean()
2021-03-22 22:49:56 +05:00
for d in foamCase:
shutil.copytree(
os.path.join(config.ROOT, "src/cubicFoam", d),
os.path.join(task.export, d)
)
stime = time.monotonic()
2021-03-22 22:49:56 +05:00
if not os.path.exists("mesh.unv"):
logger.critical(f"calculate: missed 'mesh.unv'")
return
_, returncode = foam_utils.ideasUnvToFoam("mesh.unv")
if returncode:
os.chdir(config.ROOT)
return returncode
foam_utils.createPatch(dictfile = "system/createPatchDict.symetry")
foam_utils.foamDictionary("constant/polyMesh/boundary", "entry0.defaultFaces.type", "wall")
foam_utils.foamDictionary("constant/polyMesh/boundary", "entry0.defaultFaces.inGroups", "1 (wall)")
foam_utils.checkMesh()
2021-03-03 16:24:47 +05:00
scale = (1e-5, 1e-5, 1e-5)
foam_utils.transformPoints(scale)
foam_utils.decomposePar()
2021-03-24 21:27:12 +05:00
foam_utils.renumberMesh()
foam_utils.potentialFoam()
for n in range(os.cpu_count()):
foam_utils.foamDictionary(f"processor{n}/0/U", "boundaryField.inlet.type", "pressureInletVelocity")
foam_utils.foamDictionary(f"processor{n}/0/U", "boundaryField.inlet.value", "uniform (0 0 0)")
2021-04-16 15:28:04 +05:00
returncode = foam_utils.simpleFoam()
os.chdir(config.ROOT)
etime = time.monotonic()
logger.info("calculate: elapsed time: {}".format(timedelta(seconds = etime - stime)))
2021-03-24 21:27:12 +05:00
2021-04-16 15:28:04 +05:00
return returncode
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.export, "postProcessing", "")
2021-04-13 22:06:25 +05:00
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
###
# Main entry
##
2021-03-22 15:44:22 +05:00
if __name__ == "__main__":
main()
2021-03-03 22:16:36 +05:00
2021-03-03 16:24:47 +05:00