forked from GeoStat-Examples/ogs5py_benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_active_FEM_scripts.py
161 lines (140 loc) · 4.55 KB
/
gen_active_FEM_scripts.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
Walk all ogs5 benchmarks
"""
from __future__ import print_function
import os
import csv
import zipfile
import urllib.request
from shutil import rmtree
from ogs5py import OGS
def download_benchmarks(zip_dir):
"""Downlaod the ogs5 benchmarks."""
print("Downloading OGS5 Benchmarks")
data_filename = "data.zip"
data_url = "https://github.com/ufz/ogs5-benchmarks/archive/master.zip"
urllib.request.urlretrieve(data_url, data_filename)
# extract the data
print("Extracting OGS5 Benchmarks")
with zipfile.ZipFile(data_filename, "r") as zf:
zf.extractall(zip_dir)
# get all FEM models which have an active benchmark check
with open('active_names.csv') as csvfile:
active_names = csv.reader(csvfile)
use_names = []
for row in active_names:
if row[0] == 'Linux-FEM':
# get rid of the authors initials
use_names.append("_".join(row[1].split("_")[1:]))
# set directories
out_dir = os.path.join(os.getcwd(), "benchmarks_FEM_active")
zip_dir = os.path.join(os.getcwd(), "ogs5_benchmarks_raw")
download_benchmarks(zip_dir)
rootdir = os.path.join(zip_dir, "ogs5-benchmarks-master")
FAIL = []
FAIL_OK = []
log_str = []
test_cnt = 0
start_test = 0
# GENERATION SETTINGS
use_old_dir = True
run = False
write_files = False
gen_scr = True
print_progress = True
# skip some runs
skip_run = [] # takes for ever...
skip_dir_run = []
for name in use_names:
test_cnt += 1
relative_root, task_id = os.path.split(name)
root = os.path.join(rootdir, relative_root)
if use_old_dir:
tmp_root = relative_root
else:
tmp_root = ("test_{:03}".format(test_cnt) + "_model")
tmp_log = (
str(test_cnt)
+ " "
+ tmp_root
+ " - load: "
+ os.path.join(root, task_id)
)
log_str.append(tmp_log)
if print_progress:
print(tmp_log)
model = OGS(task_root=os.path.join(out_dir, tmp_root), output_dir="out")
load_success = False
try:
model.load_model(
verbose=False,
task_root=root,
task_id=task_id,
encoding="ISO-8859-15", # lots of windows files (non utf8)
use_task_id=True, # use the given task_id for some reference
)
except ValueError as valerr:
print("FAIL..." + str(valerr))
log_str.append("FAIL..." + str(valerr))
else:
load_success = True
print("OK...loading successful")
log_str.append("OK...loading successful")
if gen_scr and load_success:
model.gen_script(
script_name=model.task_id + ".py",
script_dir=model.task_root,
task_root=task_id + "_root",
output_dir="out",
separate_files=["ddc", "rfd"]
)
model.task_root = os.path.join(model.task_root, task_id + "_root")
if write_files and load_success:
model.write_input()
skip_this_run = task_id in skip_run
skip_this_run |= any([s_dir in root for s_dir in skip_dir_run])
skip_this_run |= not model.pqc.is_empty # skip phreeqc
skip_this_run |= not model.gem.is_empty # skip gem
skip_this_run |= not model.ddc.is_empty # skip mpi
if run and load_success and not skip_this_run:
success = model.run_model(
ogs_name="ogs", print_log=False
)
if success:
tmp_log = "....OK"
else:
FAIL.append(os.path.join(root, task_id))
tmp_log = "..FAIL"
model.output_dir = os.path.abspath(
os.path.join(model.task_root, "out_ori")
)
model.task_root = root
ori_succ = model.run_model(
ogs_name="ogs", print_log=False # in sys-path
)
if ori_succ:
tmp_log += " ..original OK"
FAIL[-1] = " (original OK)" + FAIL[-1]
FAIL_OK.append(os.path.join(root, task_id))
else:
tmp_log += " ..original FAIL"
log_str.append(tmp_log)
print(tmp_log)
elif run and load_success:
print("run skip " + task_id)
# save some logs
if run:
with open(os.path.join(out_dir, "FAIL.txt"), "w") as outfile:
for line in FAIL:
print(line, file=outfile)
with open(os.path.join(out_dir, "FAIL_OK.txt"), "w") as outfile:
for line in FAIL_OK:
print(line, file=outfile)
with open(os.path.join(out_dir, "LOG.txt"), "w") as outfile:
for line in log_str:
print(line, file=outfile)
# cleanup
print("Removing OGS5 Benchmarks")
os.remove("data.zip")
rmtree(zip_dir)