anisotropy/openfoam/application.py

48 lines
1.1 KiB
Python
Raw Normal View History

2021-05-26 22:02:44 +05:00
import os, sys
2021-03-22 15:44:22 +05:00
import subprocess
2021-05-26 22:02:44 +05:00
import logging
logger = logging.getLogger()
2021-03-22 15:44:22 +05:00
2021-04-14 15:13:41 +05:00
def application(name: str, *args: str, case: str = None, stderr: bool = True, useMPI: bool = False) -> int:
2021-04-14 15:13:41 +05:00
cmd = []
if useMPI:
nprocs = os.cpu_count()
cmd.extend(["mpirun", "-np", str(nprocs), "--oversubscribe"])
2021-04-14 15:13:41 +05:00
cmd.append(name)
if case:
cmd.extend(["-case", case])
if args:
cmd.extend([*args])
logger.info("{}: {}".format(name, [*args]))
2021-04-14 15:13:41 +05:00
logpath = os.path.join(case if case else "", "{}.log".format(name))
with subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE) as p, \
2021-04-14 15:13:41 +05:00
open(logpath, "wb") as logfile:
for line in p.stdout:
#sys.stdout.buffer.write(line)
logfile.write(line)
#for line in p.stderr:
# logfile.write(line)
out, err = p.communicate()
2021-04-14 15:13:41 +05:00
logfile.write(err)
2021-04-14 15:13:41 +05:00
if err and stderr:
logger.error("""{}:
{}""".format(name, str(err, "utf-8")))
2021-03-22 15:44:22 +05:00
2021-04-14 15:13:41 +05:00
return out, p.returncode