test_experiments.py 3.5 KB
Newer Older
1
from unittest import TestCase, main
niklas.baumgarten's avatar
niklas.baumgarten committed
2
import time
niklas.baumgarten's avatar
niklas.baumgarten committed
3
4
import sys
import os
5
6
7
8
9
10
11
12
13

sys.path.append('.')

from mlmc_mppy import mpp


class TestExperiments(TestCase):
    @classmethod
    def setUpClass(cls):
niklas.baumgarten's avatar
niklas.baumgarten committed
14
        mpp.clean_cmake_cache()
15
16
17
18
19
20
21
        mpp.build()

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
niklas.baumgarten's avatar
niklas.baumgarten committed
22
        mpp.reset_sol_data()
23
        mpp.clean_data()
niklas.baumgarten's avatar
niklas.baumgarten committed
24
        self.startTime = time.time()
25
26

    def tearDown(self):
niklas.baumgarten's avatar
niklas.baumgarten committed
27
28
29
30
        t = time.time() - self.startTime
        self.show_timing(t)
        mpp.kill()
        mpp.reset_sol_data()
31
32
        mpp.clean_data()

niklas.baumgarten's avatar
niklas.baumgarten committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    def show_timing(self, t):
        unit = 's'
        if t >= 60:
            t = t / 60.0
            unit = 'min'
        if t >= 60:
            t = t / 60.0
            unit = 'h'

        test_id = self.id().replace('__main__.', '')
        # 64 is length of dashed line minus the print accuracy of 6 digits
        space = (64 - len(test_id) - len(unit)) * ' '
        print('{}{}{:6.3f}{}'.format(test_id, space, t, unit))

47

niklas.baumgarten's avatar
niklas.baumgarten committed
48
class TestEllipticBasics(TestExperiments):
49
50
51
52
53
54
55
    def test_git_repo(self):
        self.assertEqual(mpp.check_git_project(), 0)

    def test_build(self):
        self.assertEqual(mpp.build(), 0)

    def test_run(self):
niklas.baumgarten's avatar
niklas.baumgarten committed
56
57
58
        self.assertEqual(mpp.run(4, config='mlmc_elliptic'), 0)


niklas.baumgarten's avatar
niklas.baumgarten committed
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
# class TestEllipticConvergence(TestExperiments):
#     def test_smoothing_experiment_benchmark(self):
#         self.assertTrue(not os.listdir(mpp.PROJECT_PY_DATA_DIR))
#         mpp.mute = False
#         kwargs = {'Experiment': 'ConvergenceTest', 'initLevels': '3, 4, 5, 6, 7, 8, 9'}
#         smoothings = [1.0, 1.4, 1.8]
#         for smoothing in smoothings:
#             kwargs['smoothing'] = smoothing
#             self.assertEqual(mpp.run(4, config='mlmc_elliptic', kwargs=kwargs), 0)
#             mpp.show_convergence_table(plot_name='Test')
#             mpp.save_convergence_table(table_name='Test')
#         self.assertEqual(len(os.listdir(mpp.PROJECT_PY_DATA_DIR)), 6)
#
#     def test_smoothing_experiment_small(self):
#         mpp.mute = False
#         kwargs = {'Experiment': 'ConvergenceTest', 'initLevels': '3, 4, 5, 6'}
#         smoothings = [1.4, 1.8]
#         for smoothing in smoothings:
#             kwargs['smoothing'] = smoothing
#             self.assertEqual(mpp.run(4, config='mlmc_elliptic', kwargs=kwargs), 0)
#             mpp.show_convergence_table(plot_name='Test')
#             mpp.save_convergence_table(table_name='Test')
#         self.assertEqual(len(os.listdir(mpp.PROJECT_PY_DATA_DIR)), 6)


class TestEllipticExperiment(TestExperiments):
    def test_experiment(self):
niklas.baumgarten's avatar
niklas.baumgarten committed
86
        mpp.mute = False
niklas.baumgarten's avatar
niklas.baumgarten committed
87
88
89
90
        kwargs = {'Experiment': 'MLMCExperiment'}
        epsilons = [0.01, 0.005, 0.03, 0.001]
        for epsilon in epsilons:
            kwargs['epsilon'] = epsilon
niklas.baumgarten's avatar
niklas.baumgarten committed
91
            self.assertEqual(mpp.run(4, config='mlmc_elliptic', kwargs=kwargs), 0)
niklas.baumgarten's avatar
niklas.baumgarten committed
92
93


niklas.baumgarten's avatar
niklas.baumgarten committed
94
95
class TestTransportBasics(TestExperiments):
    def test_problems(self):
niklas.baumgarten's avatar
niklas.baumgarten committed
96
97
98
99
100
101
102
103
        problems = [  # 'StochasticPollution1D',
            # 'DeterministicPollution1D',
            'StochasticPollution2D',
            'DeterministicPollution2D',
            'StochasticPollutionCosHat2D',
            'DeterministicPollutionCosHat2D',
            'StochasticPollutionMollifiedBar2D',
            'DeterministicPollutionMollifiedBar2D']
niklas.baumgarten's avatar
niklas.baumgarten committed
104
105
        for problem in problems:
            kwargs = {'Problem': problem}
niklas.baumgarten's avatar
niklas.baumgarten committed
106
107
108
109
            self.assertEqual(mpp.run(4, config='mlmc_transport', kwargs=kwargs), 0)

    def test_file_creation(self):
        pass
110

niklas.baumgarten's avatar
niklas.baumgarten committed
111

112
113
if __name__ == '__main__':
    main()