2022-01-23 12:27:04 +01:00
|
|
|
import sys
|
2022-01-23 14:13:36 +01:00
|
|
|
import shutil
|
2022-01-23 12:27:04 +01:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import configparser
|
|
|
|
from pprint import pp
|
|
|
|
|
|
|
|
from time import time
|
|
|
|
from datetime import timedelta
|
2022-01-23 16:56:50 +01:00
|
|
|
from datetime import datetime
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
import subprocess as sp
|
2022-01-23 16:56:50 +01:00
|
|
|
import urllib.error
|
2022-01-26 17:54:27 +01:00
|
|
|
|
|
|
|
from .grafana import Silencer
|
2022-01-26 18:06:08 +01:00
|
|
|
from .stl import copy_stl
|
2022-01-26 18:26:29 +01:00
|
|
|
from .run import run
|
2022-01-23 16:56:50 +01:00
|
|
|
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
_t0 = time()
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Run the OpenFoam simulation'
|
|
|
|
)
|
|
|
|
parser.add_argument('-c', '--config', default='config.ini', type=Path,
|
|
|
|
help='Configuration file (default config.ini)')
|
|
|
|
parser.add_argument('-l', '--log-level', default='INFO', type=str,
|
|
|
|
help='Log level')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(args.config)
|
|
|
|
|
|
|
|
logging.basicConfig(
|
2022-01-23 13:16:00 +01:00
|
|
|
filename=config.get('main', 'log_file', fallback=None),
|
2022-01-23 12:27:04 +01:00
|
|
|
level=args.log_level
|
|
|
|
)
|
|
|
|
log = logging.getLogger('openfoam')
|
|
|
|
|
|
|
|
log.info('Starting program')
|
|
|
|
|
2022-01-26 18:26:29 +01:00
|
|
|
|
|
|
|
def step(command, alias=None):
|
|
|
|
if alias == None: alias = command
|
|
|
|
log.info(f'Running {alias}')
|
|
|
|
code = run(command, case_dir, alias)
|
|
|
|
if code != 0:
|
|
|
|
log.error(f'{alias} failed')
|
|
|
|
sys.exit(code)
|
|
|
|
log.info(f'{alias} finished successfully')
|
|
|
|
|
|
|
|
|
2022-01-23 16:56:50 +01:00
|
|
|
if config.getboolean('grafana', 'pause', fallback=False):
|
|
|
|
log.info('Silencing Grafana alert')
|
2022-01-26 17:54:27 +01:00
|
|
|
grafana = Silencer(
|
|
|
|
url=config.get('grafana', 'url'),
|
|
|
|
apikey=config.get('grafana', 'apikey'),
|
|
|
|
)
|
|
|
|
grafana.silence_alert()
|
2022-01-23 16:56:50 +01:00
|
|
|
|
2022-01-23 13:42:53 +01:00
|
|
|
input_dir = Path(config.get('main', 'input_dir'), config.get('main', 'case'))\
|
|
|
|
.expanduser()
|
|
|
|
work_dir = Path(config.get('main', 'work_dir')).expanduser()
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
if not work_dir.exists():
|
|
|
|
log.error(f'Work directory ({work_dir}) not found')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
case_dir = work_dir.joinpath(config.get('main', 'case'))
|
2022-01-23 12:27:04 +01:00
|
|
|
if case_dir.exists():
|
|
|
|
log.info(f'Deleting case ({case_dir})')
|
2022-01-23 14:13:36 +01:00
|
|
|
shutil.rmtree(case_dir)
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
log.info(f'Copying case ({input_dir} -> {case_dir})')
|
2022-01-23 14:13:36 +01:00
|
|
|
shutil.copytree(input_dir, case_dir)
|
2022-01-23 12:27:04 +01:00
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('stl', 'copy', fallback=False):
|
2022-01-23 14:13:36 +01:00
|
|
|
stl_in = Path(config.get('stl', 'from')).expanduser()
|
|
|
|
if not stl_in.exists():
|
|
|
|
log.error(f'STL from directory does not exist ({stl_in})')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-01-26 18:06:08 +01:00
|
|
|
log.info(f'Copying stl directory ({stl_in})')
|
|
|
|
copy_stl(stl_in, case_dir)
|
2022-01-23 13:16:00 +01:00
|
|
|
|
|
|
|
if config.getboolean('blockMesh', 'enable', fallback=False):
|
2022-01-26 18:26:29 +01:00
|
|
|
step(('blockMesh'))
|
2022-01-23 12:27:04 +01:00
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('snappyHexMesh', 'enable', fallback=False):
|
2022-01-26 18:26:29 +01:00
|
|
|
step(('snappyHexMesh', '-overwrite'), 'snappyHexMesh')
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
log.info('Copying 0.org -> 0')
|
2022-01-23 14:13:36 +01:00
|
|
|
shutil.copytree(
|
|
|
|
case_dir.joinpath('0.org'),
|
|
|
|
case_dir.joinpath('0'),
|
2022-01-23 12:27:04 +01:00
|
|
|
)
|
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('setFields', 'enable', fallback=False):
|
2022-01-26 18:26:29 +01:00
|
|
|
step(('setFields'))
|
2022-01-23 12:27:04 +01:00
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('olaFlow', 'enable', fallback=False):
|
2022-01-23 12:27:04 +01:00
|
|
|
cmd = ('olaFlow')
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('parallel', 'enable', fallback=False):
|
2022-01-23 12:27:04 +01:00
|
|
|
cmd = (
|
|
|
|
'mpirun',
|
2022-01-23 13:16:00 +01:00
|
|
|
'-np', config.get('parallel', 'threads'),
|
2022-01-23 12:27:04 +01:00
|
|
|
'olaFlow', '-parallel'
|
|
|
|
)
|
2022-01-26 18:26:29 +01:00
|
|
|
step(('decomposePar'))
|
|
|
|
|
|
|
|
step(cmd, 'olaFlow')
|
2022-01-23 12:27:04 +01:00
|
|
|
|
2022-01-23 13:16:00 +01:00
|
|
|
if config.getboolean('parallel', 'enable', fallback=False) \
|
2022-01-26 18:26:29 +01:00
|
|
|
and config.getboolean('reconstructPar', 'enable', fallback=False):
|
|
|
|
step(('reconstructPar'))
|
2022-01-23 12:27:04 +01:00
|
|
|
|
|
|
|
log.info(f'Deleting processor directories')
|
|
|
|
for proc_dir in case_dir.glob(r'processor*'):
|
|
|
|
log.info(f'Deleting {proc_dir}')
|
2022-01-23 14:13:36 +01:00
|
|
|
shutil.rmtree(proc_dir)
|
2022-01-23 12:27:04 +01:00
|
|
|
|
2022-01-23 16:56:50 +01:00
|
|
|
if config.getboolean('grafana', 'pause', fallback=False):
|
|
|
|
log.info('Unsilencing Grafana alert')
|
2022-01-26 17:54:27 +01:00
|
|
|
grafana.unsilence_alert()
|
2022-01-23 16:56:50 +01:00
|
|
|
|
2022-01-23 12:27:04 +01:00
|
|
|
_t1 = time()
|
|
|
|
log.info(f'Program ended successfully after {timedelta(seconds=_t1-_t0)}')
|