run_tests.py 4.89 KB
Newer Older
1
2
import unittest
import os
3
import logging
4
from cp2kparser.implementation.autoparser import get_parser
5
from cp2kparser.engines.cp2kinputenginedata.xmlpreparser import *
6
7
import cProfile
import pstats
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#===============================================================================
def getparser(folder):
    path = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(path, folder)
    parser = get_parser(path)
    return parser


#===============================================================================
class TestFunctionals(unittest.TestCase):

    def getxc(self, folder, result):
        path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "functionals", folder)
        parser = get_parser(path)
24
25
        xc = parser.get_quantity_unformatted("XC_functional")
        self.assertEqual(xc.value, result)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    def test_pade(self):
        self.getxc("pade", "LDA_XC_TETER93")

    def test_lda(self):
        self.getxc("lda", "LDA_XC_TETER93")

    def test_becke88(self):
        self.getxc("becke88", "GGA_X_B88")

    def test_blyp(self):
        self.getxc("blyp", "GGA_C_LYP_GGA_X_B88")


#===============================================================================
class TestForces(unittest.TestCase):

    def test_forces_in_outputfile_n(self):
        parser = getparser("forces/outputfile/n")
45
        forces = parser.get_quantity_unformatted("particle_forces").value
46
47
48
        n_conf = forces.shape[0]
        n_particles = forces.shape[1]
        n_dim = forces.shape[2]
49
        self.assertEqual(n_conf, 201)
50
        self.assertEqual(n_particles, 2)
51
52
53
54
        self.assertEqual(n_dim, 3)

    def test_forces_in_outputfile_1(self):
        parser = getparser("forces/outputfile/1")
55
        forces = parser.get_quantity_unformatted("particle_forces").value
56
57
58
        n_conf = forces.shape[0]
        n_particles = forces.shape[1]
        n_dim = forces.shape[2]
59
        self.assertEqual(n_conf, 1)
60
        self.assertEqual(n_particles, 2)
61
62
63
64
        self.assertEqual(n_dim, 3)

    def test_forces_in_outputfile_0(self):
        parser = getparser("forces/outputfile/0")
65
        forces = parser.get_quantity_unformatted("particle_forces").value
66
67
        self.assertEqual(forces, None)

68
69
    def test_forces_in_singlexyzfile_n(self):
        parser = getparser("forces/singlexyzfile/n")
70
        forces = parser.get_quantity_unformatted("particle_forces").value
71
72
73
74
75
76
77
78
79
        n_conf = forces.shape[0]
        n_particles = forces.shape[1]
        n_dim = forces.shape[2]
        self.assertEqual(n_conf, 201)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_forces_in_singlexyzfile_1(self):
        parser = getparser("forces/singlexyzfile/1")
80
        forces = parser.get_quantity_unformatted("particle_forces").value
81
82
83
84
85
86
87
88
89
        n_conf = forces.shape[0]
        n_particles = forces.shape[1]
        n_dim = forces.shape[2]
        self.assertEqual(n_conf, 1)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_forces_in_singlexyzfile_0(self):
        parser = getparser("forces/singlexyzfile/0")
90
        forces = parser.get_quantity_unformatted("particle_forces").value
91
92
        self.assertEqual(forces, None)

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

#===============================================================================
class TestParticleNumber(unittest.TestCase):

    def test_input_n(self):
        parser = getparser("particle_number/inputfile/n")
        n = parser.get_quantity_unformatted("particle_number").value
        self.assertEqual(n, 2)

    def test_input_1(self):
        parser = getparser("particle_number/inputfile/1")
        n = parser.get_quantity_unformatted("particle_number").value
        self.assertEqual(n, 1)

    def test_input_extra_lines(self):
        parser = getparser("particle_number/inputfile/extra_lines")
        n = parser.get_quantity_unformatted("particle_number").value
        self.assertEqual(n, 2)

    def test_xyz_n(self):
        parser = getparser("particle_number/xyz/n")
        n = parser.get_quantity_unformatted("particle_number").value
        self.assertEqual(n, 2)

    def test_xyz_1(self):
        parser = getparser("particle_number/xyz/1")
        n = parser.get_quantity_unformatted("particle_number").value
        self.assertEqual(n, 1)

122
if __name__ == '__main__':
123
124
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)
125
126
127
128
129
130
131
132
    # unittest.main()

    suites = []
    # suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForces))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestParticleNumber))
    # suites.append(unittest.TestLoader().loadTestsFromTestCase(TestFunctionals))
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)
133
134
135
136
137
138

    # def runtests():
        # unittest.main()
        # unittest.TextTestRunner().run(suite)
        # unittest.TextTestRunner(verbosity=0).run(suite)
    # s = cProfile.run("runtests()", sort="cumtime", filename="profile_file")