forked from stanfordhpccenter/HTR-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testAll.py
133 lines (117 loc) · 5.04 KB
/
testAll.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
#!/usr/bin/env python3
import os
import sys
import h5py
import glob
import json
import shutil
import unittest
import itertools
import subprocess
import numpy as np
executable = os.path.join(os.path.expandvars("$HTR_DIR"), "prometeo.sh")
# Leave a tollerance for the noise introduced by atomic
def checkSolutionFile(path, refFile, tol=1e-10):
tiles = glob.glob(os.path.join(path,"*hdf"))
if len(tiles) == 0:
print("ERROR: could not find solution file in {}".format(path))
return False
for tl in tiles:
lb = list(map(int, tl.split("/")[-1].split("-")[0].split(",")))
tile = h5py.File(tl, "r")
for fld in tile:
f = tile[fld][:]
ref = refFile[fld][:]
if (len(f.shape) == 3):
for (k,j,i), value in np.ndenumerate(f):
rval = ref[k+lb[2],j+lb[1],i+lb[0]]
if (abs(value - rval) > max(tol*abs(rval), 1e-12)):
print("Error on field: {}, delta = {}, tol = {}, expected = {}".format(fld, value - rval, max(tol*abs(rval), 1e-12), rval))
return False
elif (len(f.shape) == 4):
for (k,j,i,l), value in np.ndenumerate(f):
rval = ref[k+lb[2],j+lb[1],i+lb[0],l]
if (abs(value - rval) > max(tol*abs(rval), 1e-12)):
print("Error on field: {}, delta = {}, tol = {}, expected = {}".format(fld, value - rval, max(tol*abs(rval), 1e-12), rval))
return False
tile.close()
# if we arrive here, the solution is good
return True
# Load reference solution
def loadRef():
if int(os.getenv("USE_CUDA", "0")):
# ref = h5py.File("gpu_ref.hdf", "r")
ref = h5py.File("cpu_ref.hdf", "r")
else:
ref = h5py.File("cpu_ref.hdf", "r")
return ref
class TestTiledBase(object):
def execute(self, name):
return self.assertTrue(False, msg="{} should overwrite execute method".format(self.testName))
def test(self):
self.cwd = os.getcwd()
os.chdir(os.path.expandvars("$HTR_DIR/solverTests/"+self.testName))
try:
ref = loadRef()
except:
self.fail(self.testName + "failed loading reference solution")
self.runTiledTest(ref)
try:
if os.path.exists("InflowProfile"): shutil.rmtree("InflowProfile")
if os.path.exists("InitialCase"): shutil.rmtree("InitialCase")
for p in range(1, self.npart+1):
fileList = glob.glob(str(p)+"*.json")
for f in fileList: os.remove(f)
dirList = glob.glob(str(p)+"*")
for f in dirList: shutil.rmtree(f)
except:
self.fail(self.testName+" teardown failed")
os.chdir(self.cwd)
class Test2DTiledBase(TestTiledBase):
def runTiledTest(self, ref):
for case in itertools.product(range(1, self.npart+1), repeat=2):
name = "%dx%dx%d"%(case[0], case[1], 1)
with self.subTest(name):
#print(" > case: %s" % name)
# create directory
if os.path.exists(name): shutil.rmtree(name)
os.makedirs(name)
#create input file
with open("base.json", "r") as fin:
config = json.load(fin)
config["Mapping"]["tiles"] = (case[0], case[1], 1)
config["Mapping"]["tilesPerRank"] = (case[0], case[1], 1)
nstep = int(config["Integrator"]["maxIter"])
with open(name+".json", "w") as fout:
json.dump(config, fout, indent=3)
# run the case
MyOut = self.execute(name)
# run the check
self.assertTrue(checkSolutionFile(os.path.join(name, "sample0/fluid_iter"+str(nstep).zfill(10)), ref),
msg="Error on {} of {} test".format(name, self.testName))
class Test3DTiledBase(TestTiledBase):
def runTiledTest(self, ref):
for case in itertools.product(range(1, self.npart+1), repeat=3):
name = "%dx%dx%d"%(case)
with self.subTest(name):
#print(" > case: %s" % name)
# create directory
if os.path.exists(name): shutil.rmtree(name)
os.makedirs(name)
#create input file
with open("base.json", "r") as fin:
config = json.load(fin)
config["Mapping"]["tiles"] = case
config["Mapping"]["tilesPerRank"] = case
nstep = int(config["Integrator"]["maxIter"])
with open(name+".json", "w") as fout:
json.dump(config, fout, indent=3)
# run the case
MyOut = self.execute(name)
# run the check
self.assertTrue(checkSolutionFile(os.path.join(name, "sample0/fluid_iter"+str(nstep).zfill(10)), ref),
msg="Error on {} of {} test".format(name, self.testName))
if __name__ == "__main__":
suite = unittest.TestLoader().discover(os.path.expandvars("$HTR_DIR/solverTests/"), pattern = "test.py")
result = 0 if unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful() else 1
sys.exit(result)