New: cwd approach

Mod: improved cli
Fix: missed boundary condition for main flow
This commit is contained in:
L-Nafaryus 2021-08-30 16:56:57 +05:00
parent 750bf30e11
commit 5119e04a09
3 changed files with 144 additions and 79 deletions

View File

@ -78,26 +78,27 @@ faceCentered = true
fuseEdges = true
checkChartBoundary = false
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
# multiplication velocity value with flow direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flow]
scale = [ 1e-5, 1e-5, 1e-5 ]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "pressureInletVelocity", value = 0.0 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
# multiplication velocity value with flow direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[[structures]]
[structures.structure]
@ -166,26 +167,27 @@ faceCentered = true
fuseEdges = true
checkChartBoundary = false
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
# multiplication velocity value with direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flow]
scale = [ 1e-5, 1e-5, 1e-5 ]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "pressureInletVelocity", value = 0.0 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
# multiplication velocity value with direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[[structures]]
[structures.structure]
@ -254,24 +256,26 @@ faceCentered = true
fuseEdges = true
checkChartBoundary = false
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
# multiplication velocity value with direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flow]
scale = [ 1e-5, 1e-5, 1e-5 ]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "fixedValue", value = 0.0 }
velocity.boundaryField.inlet = { type = "pressureInletVelocity", value = 0.0 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }
[structures.flowapprox]
transportProperties.nu = 1e-6
pressure.boundaryField.inlet = { type = "fixedValue", value = 1e-3 }
pressure.boundaryField.outlet = { type = "fixedValue", value = 0 }
# multiplication velocity value with direction vector
velocity.boundaryField.inlet = { type = "fixedValue", value = 6e-5 }
velocity.boundaryField.outlet = { type = "zeroGradient", value = "None" }

View File

@ -4,7 +4,8 @@
import click
import ast
import os
import os, shutil
import logging
class LiteralOption(click.Option):
def type_cast_value(self, ctx, value):
@ -63,6 +64,29 @@ def version():
def anisotropy():
pass
@anisotropy.command(
help = "Initialize project in cwd"
)
def init():
from anisotropy import env
from anisotropy.core.utils import setupLogger
from anisotropy.core.main import logger, Database
setupLogger(logger, logging.INFO)
cwd = os.getcwd()
wds = [ "build", "logs" ]
for wd in wds:
os.makedirs(os.path.join(cwd, wd), exist_ok = True)
shutil.copy(env["CONFIG"], os.path.join(cwd, "anisotropy.toml"), follow_symlinks = True)
db = Database(env["db_name"], cwd)
db.setup()
logger.info(f"Initialized anisotropy project in { cwd }")
@anisotropy.command(
help = """Computes cases by chain (mesh -> flow)
@ -81,19 +105,15 @@ def anisotropy():
default = 1,
help = "Count of parallel processes"
)
@click.option(
"-D", "--database", "database",
help = "Database path"
)
@click.option(
"-f", "--force", "force",
type = click.BOOL,
is_flag = True,
default = False,
help = "Overwrite existing entries"
)
@click.option(
"-u", "--update", "update",
type = click.BOOL,
is_flag = True,
default = False,
help = "Update db parameters from config"
)
@ -104,10 +124,24 @@ def anisotropy():
cls = KeyValueOption,
help = "Overwrite existing parameter (except control variables)"
)
def compute(stage, nprocs, database, force, update, params):
@click.option(
"-P", "--path", "path",
default = os.getcwd(),
help = "Specify directory to use (instead of cwd)"
)
def compute(stage, nprocs, force, update, params, path):
from anisotropy import env
from anisotropy.core.main import Anisotropy, Database, logger
from anisotropy.core.utils import timer, parallel
from anisotropy.core.utils import setupLogger, timer, parallel
env.update(
LOG = os.path.join(path, "logs"),
BUILD = os.path.join(path, "build"),
CONFIG = os.path.join(path, "anisotropy.toml"),
db_path = path
)
setupLogger(logger, logging.INFO, env["LOG"])
args = dict()
for param in params:
@ -115,30 +149,31 @@ def compute(stage, nprocs, database, force, update, params):
###
model = Anisotropy()
model.db = Database(env["db_name"], env["db_path"])
if database:
if database[-3: ] == ".db":
splitted = database.split("/")
db_path = "/".join(splitted[ :-1])
db_name = splitted[-1: ][0][ :-3]
else:
raise Exception("Invalid database extension")
model.db = Database(db_name, db_path)
logger.info("Constructing database, tables ...")
logger.info("Configuring database ...")
model.db.setup()
def fill_db():
if model.db.isempty() or update:
paramsAll = model.loadFromScratch()
paramsAll = model.loadFromScratch(env["CONFIG"])
if args.get("type"):
paramsAll = [ entry for entry in paramsAll if args["type"] == entry["structure"]["type"] ]
if args.get("direction"):
paramsAll = [ entry for entry in paramsAll if args["direction"] == entry["structure"]["direction"] ]
if args.get("theta"):
paramsAll = [ entry for entry in paramsAll if args["theta"] == entry["structure"]["theta"] ]
for entry in paramsAll:
model.db.update(entry)
_, fill_elapsed = timer(fill_db)()
logger.info(f"Elapsed time = { fill_elapsed }")
logger.info("{} entries was updated.".format(len(paramsAll)))
else:
logger.info("Database was not modified.")
###
def computeCase(stage, type, direction, theta):
@ -152,7 +187,7 @@ def compute(stage, nprocs, database, force, update, params):
if stage == "all" or stage == "mesh":
if not case.params.get("meshresult", {}).get("status") == "Done" or force:
(out, err, returncode), elapsed = timer(case.computeMesh)()
(out, err, returncode), elapsed = timer(case.computeMesh)(path)
if out: logger.info(out)
if err: logger.error(err)
@ -172,7 +207,7 @@ def compute(stage, nprocs, database, force, update, params):
if stage == "all" or stage == "flow":
if not case.params.get("flowresult", {}).get("status") == "Done" or force:
(out, err, returncode), elapsed = timer(case.computeFlow)()
(out, err, returncode), elapsed = timer(case.computeFlow)(path)
if out: logger.info(out)
if err: logger.error(err)
@ -219,7 +254,8 @@ def compute(stage, nprocs, database, force, update, params):
@click.argument("type")
@click.argument("direction")
@click.argument("theta")
def computemesh(root, type, direction, theta):
@click.argument("path")
def computemesh(root, type, direction, theta, path):
# ISSUE: can't hide command from help, 'hidden = True' doesn't work
# [Salome Environment]
@ -239,17 +275,27 @@ def computemesh(root, type, direction, theta):
os.path.join(root, "env/lib/python3.9/site-packages")
])
from anisotropy.core.main import Anisotropy
from anisotropy import env
from anisotropy.core.main import Anisotropy, Database
import salome
###
model = Anisotropy()
model.db = Database(env["db_name"], path)
model.load(type, direction, theta)
salome.salome_init()
model.genmesh()
model.genmesh(path)
salome.salome_close()
@anisotropy.command(
help = "Post processing"
)
def postprocessing():
pass
###
# CLI entry
##

View File

@ -25,7 +25,7 @@ import anisotropy.salomepl.mesh
from anisotropy.samples import Simple, FaceCentered, BodyCentered
logger = logging.getLogger(env["logger_name"])
setupLogger(logger, logging.INFO, env["LOG"])
#setupLogger(logger, logging.INFO, env["LOG"])
#peeweeLogger = logging.getLogger("peewee")
#peeweeLogger.setLevel(logging.INFO)
@ -83,18 +83,22 @@ class Anisotropy(object):
return "\n".join([ f"{ k }: { v }" for k, v in versions.items() ])
def loadFromScratch(self) -> list:
def loadFromScratch(self, configpath: str = None) -> list:
"""Loads parameters from configuration file and expands special values
:return: List of dicts with parameters
:rtype: list
"""
config = configpath or self.env["CONFIG"]
if not os.path.exists(self.env["CONFIG"]):
logger.error("Missed default configuration file")
if not os.path.exists(config):
logger.error("Missed configuration file")
return
buf = toml.load(self.env["CONFIG"]).get("structures")
else:
logger.info(f"Configuration file: { config }")
buf = toml.load(config).get("structures")
paramsAll = []
# TODO: custom config and merge
@ -204,7 +208,7 @@ class Anisotropy(object):
fillets = fillets
)
def getCasePath(self) -> str:
def getCasePath(self, path: str = None) -> str:
"""Constructs case path from control parameters
:return: Absolute path to case
@ -216,15 +220,21 @@ class Anisotropy(object):
logger.error("Trying to use empty parameters")
return
if path:
path = os.path.join(path, "build")
else:
path = self.env["BUILD"]
return os.path.join(
self.env["BUILD"],
path,
structure["type"],
"direction-{}".format(str(structure['direction']).replace(" ", "")),
f"theta-{ structure['theta'] }"
)
def computeMesh(self):
def computeMesh(self, path):
"""Computes a mesh on shape via Salome
:return: Process output, error messages and returncode
@ -237,6 +247,7 @@ class Anisotropy(object):
p["type"],
p["direction"],
p["theta"],
path
]
manager = salomepl.utils.SalomeManager()
@ -249,12 +260,13 @@ class Anisotropy(object):
)
def genmesh(self):
def genmesh(self, path):
"""Computes a mesh on shape
Warning: Working only inside Salome Environment
"""
setupLogger(logger, logging.INFO, self.env["LOG"])
p = self.params
###
@ -327,10 +339,13 @@ class Anisotropy(object):
mesh.removePyramids()
mesh.assignGroups()
casePath = self.getCasePath()
casePath = self.getCasePath(path)
os.makedirs(casePath, exist_ok = True)
logger.info("Exporting mesh ...")
mesh.exportUNV(os.path.join(casePath, "mesh.unv"))
returncode, err = mesh.exportUNV(os.path.join(casePath, "mesh.unv"))
if returncode:
logger.error(err)
meshStats = mesh.stats()
p["meshresult"].update(
@ -352,7 +367,7 @@ class Anisotropy(object):
self.update()
def computeFlow(self):
def computeFlow(self, path):
"""Computes a flow on mesh via OpenFOAM
:return: Process output, error messages and returncode
@ -387,13 +402,13 @@ class Anisotropy(object):
##
if not os.path.exists("mesh.unv"):
logger.error(f"missed 'mesh.unv'")
os.chdir(self.env["ROOT"])
os.chdir(path or self.env["ROOT"])
return "", "", 1
out, err, returncode = openfoam.ideasUnvToFoam("mesh.unv")
if returncode:
os.chdir(self.env["ROOT"])
os.chdir(path or self.env["ROOT"])
return out, err, returncode
openfoam.createPatch(dictfile = "system/createPatchDict")