-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.py
121 lines (102 loc) · 4.05 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
__all__ = ['Command']
import functools as f
import pathlib as p
import shlex
import subprocess as s
import typing as t
import warnings as w
from .progress import Default, Apps
if t.TYPE_CHECKING:
from ..core import Foam
class Command:
'''OpenFOAM command wrapper'''
Self = __qualname__
def __init__(self, foam: 'Foam') -> None:
self._foam = foam
@classmethod
def from_foam(cls, foam: 'Foam') -> Self:
return cls(foam)
@property
def times(self) -> t.List[float]:
times = []
for path in self._foam._dest.iterdir():
try:
time = float(path.stem)
except ValueError:
continue
times.append(time)
return sorted(times)
@f.cached_property
def macros(self) -> t.Dict[str, str]:
return {
'__app__': self.application,
'__procs__': str(self.number_of_processors),
'__pwd__': self._foam._dest.absolute().as_posix(),
}
@f.cached_property
def application(self) -> str:
return self._foam['foam']['system', 'controlDict', 'application']
@f.cached_property
def number_of_processors(self) -> int:
try:
return self._foam['foam']['system', 'decomposeParDict', 'numberOfSubdomains']
except:
return 1
@f.cached_property
def pipe(self) -> t.List[str]:
return (self._foam['other'] or {}).get('pipeline', [])
def all_run(
self,
overwrite: bool = False, exception: bool = False,
parallel: bool = True, unsafe: bool = True,
) -> None:
if not self.pipe:
assert (self._foam._dest/'Allrun').exists()
self.raw('./Allrun')
else:
self.run(self.pipe, overwrite=overwrite, exception=exception, parallel=parallel, unsafe=unsafe)
def run(
self,
commands: t.List[str],
suffix: str = '', overwrite: bool = False, exception: bool = True,
parallel: bool = True, unsafe: bool = False,
) -> t.List[p.Path]:
'''https://github.com/OpenFOAM/OpenFOAM-7/blob/master/bin/tools/RunFunctions'''
popen = lambda args: s.Popen(
' '.join(args) if unsafe else args,
cwd=self._foam._dest, shell=unsafe, stdout=s.PIPE,
)
paths = [None] * len(commands)
for ith, command in enumerate(commands):
raws = shlex.split(self._replace(command))
args = self._split(command, parallel and self.number_of_processors>1)
path = self._foam._dest / f'log.{raws[0].replace("./", "")}{suffix}'
if not overwrite and path.exists():
message = f'{raws[0]} already run on {path.parent.absolute()}: remove log file "{path.name}" to re-run'
if exception:
raise Exception(message)
else:
w.warn(message)
continue
print(f'Running {raws[0]} on {path.parent.absolute()} using {self.number_of_processors} processes if in parallel')
# TODO: rewritten as parenthesized context managers when updated to 3.10
App = Apps.get(raws[0], Default)
with popen(args) as proc, open(path, 'wb') as file, App(self._foam) as app:
for line in proc.stdout:
file.write(line)
app.step(line)
paths[ith] = path
# TODO: Use exit status as return
return paths
def raw(self, command: str, output: bool = True) -> s.CompletedProcess:
'''Execute raw command in case directory'''
args = shlex.split(command)
return s.run(args, cwd=self._foam._dest, capture_output=output)
def _replace(self, command: str) -> str:
for old, new in self.macros.items():
command = command.replace(old, new)
return command
def _split(self, command: str, parallel: bool) -> t.List[str]:
if parallel and '__app__' in command:
command = f'mpirun -np __procs__ {command} -parallel'
return shlex.split(self._replace(command))