Skip to content

Commit 4a0e40f

Browse files
authored
Create simvue_openfoam.py
1 parent dfc0751 commit 4a0e40f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

examples/simvue_openfoam.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import sys
3+
import time
4+
import re
5+
6+
from simvue import Run
7+
8+
if __name__ == "__main__":
9+
# Regular expressions
10+
exp1 = re.compile("^(.+): Solving for (.+), Initial residual = (.+), Final residual = (.+), No Iterations (.+)$")
11+
exp2 = re.compile("^ExecutionTime = ([0-9.]+) s")
12+
13+
# Check the log file for new entries at this interval (in secs)
14+
polling_interval = 5
15+
16+
run = Run()
17+
run.init(tags=['OpenFOAM'])
18+
19+
running = True
20+
file_pos = 0
21+
ttime = None
22+
metrics = {}
23+
24+
while running:
25+
# If log doesn't exist yet, wait for it
26+
if not os.path.isfile(sys.argv[1]):
27+
time.sleep(polling_interval)
28+
continue
29+
30+
# Read log file
31+
with open(sys.argv[1], 'r') as fh:
32+
fh.seek(file_pos)
33+
for line in fh.readlines():
34+
# Get time
35+
match = exp2.match(line)
36+
if match:
37+
ttime = match.group(1)
38+
if metrics:
39+
run.log_metrics(metrics, time=ttime)
40+
metrics = {}
41+
42+
# Get metrics
43+
match = exp1.match(line)
44+
if match:
45+
metrics['residuals.initial.%s' % match.group(2)] = match.group(3)
46+
metrics['residuals.final.%s' % match.group(2)] = match.group(4)
47+
48+
file_pos = fh.tell()
49+
50+
# Check if application is still running
51+
if os.path.isfile('.finished'):
52+
running = False
53+
else:
54+
time.sleep(polling_interval)
55+
56+
run.close()

0 commit comments

Comments
 (0)