Foam block

This commit is contained in:
L-Nafaryus 2021-03-03 22:16:36 +05:00
parent 2f872a6de2
commit 6e6bbb1266
No known key found for this signature in database
GPG Key ID: C76D8DCD2727DBB7
3 changed files with 153 additions and 24 deletions

View File

@ -1,18 +1,123 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, shutil
import subprocess
import logging
import time
from datetime import timedelta
def application(name, case, log=False, args=[], parallel=False):
logging.info("Running '{}' for {}".format(name, case))
if log:
logfile = open("{}/{}.log".format(case, name), "a")
mpirun = []
if parallel:
mpirun = ["mpirun", "-np", "4", "--oversubscribe"]
subprocess.run(mpirun + [name, "-case", case] + args,
stdout=logfile if log else subprocess.STDOUT,
stderr=logfile if log else subprocess.STDOUT)
if log:
logfile.close()
def ideasUnvToFoam(case, mesh):
application("ideasUnvToFoam", case, True, [mesh])
def transformPoints(case, vector):
application("transformPoints", case, True, ["-scale", vector])
def checkMesh(case):
application("checkMesh", case, True, ["-allGeometry", "-allTopology"])
def foamDictionaryGet(case, foamFile, entry):
application("foamDictionary", case, False, [foamFile, "-entry", entry])
def foamDictionarySet(case, foamFile, entry, value):
application("foamDictionary", case, False, [foamFile, "-entry", entry, "-set", value])
def decomposePar(case):
application("decomposePar", case, True)
def potentialFoam(case):
application("potentialFoam", case, True, ["-parallel"], True)
def simpleFoam(case):
application("simpleFoam", case, True, ["-parallel"], True)
if __name__ == "__main__":
src = os.getcwd()
build = os.path.join(src, "../build")
# Get main paths
project = os.getcwd()
src = os.path.join(project, "src")
build = os.path.join(project, "build")
if not os.path.exists(build):
os.makedirs(build)
os.makedirs(build)
foamCase = [ "0", "constant", "system" ]
# Logger
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers = [
logging.StreamHandler(),
logging.FileHandler("{}/genmesh.log".format(build))
])
start_time = time.monotonic()
# Main entry
structures = ["simpleCubic"] #, "bc-cubic", "fc-cubic"]
directions = ["001", "100"]
coefficients = [ alpha * 0.01 for alpha in range(1, 13 + 1) ]
for structure in structures:
for direction in directions:
for coefficient in coefficients:
foamCase = [ "0", "constant", "system" ]
src_path = os.path.join(src, "baseFOAM")
build_path = os.path.join(build,
structure,
"direction-{}".format(direction),
"alpha-{}".format(coefficient))
logging.info("Entry with parameters: {}, direction = {}, alpha = {}".format(structure, direction, coefficient))
logging.info("Copying baseFOAM case ...")
for d in foamCase:
if not os.path.exists(os.path.join(build_path, d)):
shutil.copytree(os.path.join(src_path, d),
os.path.join(build_path, d))
logging.info("Importing mesh to foam ...")
ideasUnvToFoam(build_path, "{}-{}-{}.unv".format(structure, direction, coefficient))
logging.info("Scaling mesh ...")
transformPoints(build_path, "'(1e-5 1e-5 1e-5)'")
logging.info("Checking mesh ...")
checkMesh(build_path)
# TODO: change type for symetryPlane
logging.info("Changing mesh boundaries types ...")
foamDictionarySet(build_path, "constant/polyMesh/boundary", "entry0.wall.type", "wall")
logging.info("Decomposing case ...")
decomposePar(build_path)
logging.info("Evaluating initial approximation via potentialFoam ...")
potentialFoam(build_path)
logging.info("Preparing boundaryFields for simpleFoam ...")
for n in range(4):
foamDictionarySet(build_path, "processor{}/0/U".format(n),
"boundaryField.inlet.type", "pressureInletVelocity")
foamDictionarySet(build_path, "processor{}/0/U",
"boundaryField.inlet.value", "'uniform (0 0 0)'")
logging.info("Calculating ...")
simpleFoam(build_path)
end_time = time.monotonic()
logging.info("Elapsed time: {}".format(timedelta(seconds=end_time - start_time)))
for d in foamCase:
shutil.copytree("{}/foam/{}".format(src, d),
"{}/simple-cubic/0.1/{}".format(build, d))

View File

@ -14,14 +14,29 @@ def salome(port, src_path, build_path, coefficient, direction):
src_path (str): Path to the execution script.
build_path (str): Output path.
"""
logging.info("Starting SALOME on port {} for {}".format(port, build_path))
subprocess.run(["salome", "start",
"--port", str(port),
"-t", src_path,
"args:{},{},{}".format(build_path, coefficient, direction)])
logging.info("Starting SALOME on port {}.".format(port))
salomelog = open("{}/salome.log".format(build_path), "a")
subprocess.run([
"salome", "start",
"--port", str(port),
"-t", src_path,
"args:{},{},{}".format(build_path, coefficient, direction)
],
stdout=salomelog,
stderr=salomelog)
logging.info("Terminating SALOME on port {}.".format(port))
logging.info("Terminating SALOME on port {} for {}".format(port, build_path))
subprocess.run(["salome", "kill", str(port)])
subprocess.run([
"salome", "kill",
str(port)
],
stdout=salomelog,
stderr=salomelog)
salomelog.close()
if __name__ == "__main__":
# Get main paths
@ -33,8 +48,13 @@ if __name__ == "__main__":
os.makedirs(build)
# Logger
logging.basicConfig(filename="{}/genmesh.log".format(build),
level=logging.INFO, format="%(levelname)s: %(name)s: %(message)s")
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers = [
logging.StreamHandler(),
logging.FileHandler("{}/genmesh.log".format(build))
])
start_time = time.monotonic()
# Start in parallel
@ -55,7 +75,6 @@ if __name__ == "__main__":
if not os.path.exists(build_path):
os.makedirs(build_path)
logging.info("{} created.".format(build_path))
p = multiprocessing.Process(target = salome,
args = (port, src_path, build_path, coefficient, direction))

View File

@ -285,10 +285,10 @@ class simpleCubic:
status = self.mesh.Compute()
if status:
print("Mesh succesfully computed.")
logging.info("Mesh succesfully computed.")
else:
print("Mesh is not computed.")
logging.warning("Mesh is not computed.")
def meshExport(self, path):
"""
@ -303,7 +303,7 @@ class simpleCubic:
self.mesh.ExportUNV(exportpath)
except:
print("Error: Cannot export mesh to '{}'".format(exportpath))
logging.error("Cannot export mesh to '{}'".format(exportpath))
if __name__ == "__main__":
# Arguments
@ -314,8 +314,13 @@ if __name__ == "__main__":
name = "simpleCubic-{}-{}".format(direction, alpha)
# Logger
logging.basicConfig(filename="{}/{}.log".format(buildpath, name),
level=logging.INFO, format="%(levelname)s: %(name)s: %(message)s")
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers = [
logging.StreamHandler(),
logging.FileHandler("{}/{}.log".format(buildpath, name))
])
start_time = time.monotonic()
# Simple cubic