run_tests.py 2.44 KB
Newer Older
Georg Martin Reinke's avatar
Georg Martin Reinke committed
1
#!/usr/bin/python3
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# Script for automatically running the tests and comparing the results against
# given CSV files (from some other simulator).
# TODO: supporting different timesteps (interpolating), phasor/EMT conversion,
# more advanced error criterions...

import glob
import numpy as np
import pandas
import subprocess
import sys

EPSILON = 1e-6

def run_test(binary, dpCsv, expectedCsv):
    ret = subprocess.call("./" + binary)
    if ret:
Georg Martin Reinke's avatar
Georg Martin Reinke committed
19
        print(binary + " binary returned code " + str(ret), file=sys.stderr)
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
        return ret
    dpData = pandas.read_csv(dpCsv, header=None)
    expectedData = pandas.read_csv(expectedCsv, header=None)
    if dpData.shape[1] != expectedData.shape[1]:
        print("{}: result vector dimension mismatch (DP: {}, expected: {}".format(binary,
            dpData.shape[1], expectedData.shape[1]), file=sys.stderr)
        return 1

    dpTime = np.array(dpData.ix[:,0])
    expectedTime = np.array(expectedData.ix[:,0])
    diffTime = dpTime - expectedTime
    if np.any(diffTime):
        print(binary + ": time mismatch (wrong timestep?)")
        return 1

    ret = 0
    for i in range(1, int((dpData.shape[1] - 1) / 2)):
        realIdx = i
        imagIdx = i + int((dpData.shape[1] - 1) / 2)
        dpReal = np.array(dpData.ix[:,realIdx])
        dpImag = np.array(dpData.ix[:,imagIdx])
        expectedReal = np.array(expectedData.ix[:,realIdx])
        expectedImag = np.array(expectedData.ix[:,imagIdx])
        diff = np.sqrt((dpReal-expectedReal)**2+(dpImag-expectedImag)**2)
        diffIdx = np.nonzero(diff > EPSILON)
        if len(diffIdx[0]) != 0:
            print("{}: node {} has {} values above diff threshold".format(binary,
                i, len(diffIdx[0])), file=sys.stderr)
            print("(first at {} with diff of {})".format(diffIdx[0][0],
                diff[diffIdx[0][0]]), file=sys.stderr)
            ret = 1
    return ret


if __name__ == "__main__":
    sources = glob.glob("Test*.cpp")
56
    bins = ["../build/" + s.replace(".cpp", "") for s in sources]
Georg Martin Reinke's avatar
Georg Martin Reinke committed
57
    dpCsvs = [s.replace(".cpp", ".csv") for s in sources]
58
59
60
61
62
63
64
65
66
67
68
    expectedCsvs = [s.replace(".cpp", ".expected.csv") for s in sources]
    ret = 0
    for i in range(0, len(sources)):
        if run_test(bins[i], dpCsvs[i], expectedCsvs[i]):
            print(bins[i] + " failed!", file=sys.stderr)
            ret = 1
        else:
            print(bins[i] + " successfull.")
    if not ret:
        print("All tests successfull.")
    sys.exit(ret)