run_tests.py 7.34 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


#===============================================================================
def getparser(folder):
    path = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(path, folder)
14
    parser = get_parser(path, test=True)
15
16
17
18
19
20
21
22
    return parser


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

    def getxc(self, folder, result):
        path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "functionals", folder)
23
24
        parser = getparser(path)
        xc = parser.parse_quantity("XC_functional")
25
        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.parse_quantity("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.parse_quantity("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.parse_quantity("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.parse_quantity("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.parse_quantity("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.parse_quantity("particle_forces").value
91
92
        self.assertEqual(forces, None)

93
94
95
96
97
98

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

    def test_input_n(self):
        parser = getparser("particle_number/inputfile/n")
99
        n = parser.parse_quantity("particle_number").value
100
101
102
103
        self.assertEqual(n, 2)

    def test_input_1(self):
        parser = getparser("particle_number/inputfile/1")
104
        n = parser.parse_quantity("particle_number").value
105
106
107
108
        self.assertEqual(n, 1)

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

112
113
    def test_input_multiplication(self):
        parser = getparser("particle_number/inputfile/multiplication")
114
        n = parser.parse_quantity("particle_number").value
115
116
        self.assertEqual(n, 12)

117
118
    def test_xyz_n(self):
        parser = getparser("particle_number/xyz/n")
119
        n = parser.parse_quantity("particle_number").value
120
121
        self.assertEqual(n, 2)

122
123
    def test_xyz_multiplication(self):
        parser = getparser("particle_number/xyz/multiplication")
124
        n = parser.parse_quantity("particle_number").value
125
126
127
128
        self.assertEqual(n, 12)

    def test_cif_n(self):
        parser = getparser("particle_number/cif/n")
129
        n = parser.parse_quantity("particle_number").value
130
131
132
133
        self.assertEqual(n, 2)

    def test_pdb_n(self):
        parser = getparser("particle_number/pdb/n")
134
        n = parser.parse_quantity("particle_number").value
135
        self.assertEqual(n, 2)
136

137
138
139
140
141
142

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

    def test_filenames_bare(self):
        parser = getparser("trajectory/filenames/bare")
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        pos = parser.parse_quantity("particle_position").value
        n_conf = pos.shape[0]
        n_particles = pos.shape[1]
        n_dim = pos.shape[2]
        self.assertEqual(n_conf, 11)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_filenames_dotslash(self):
        parser = getparser("trajectory/filenames/dotslash")
        pos = parser.parse_quantity("particle_position").value
        n_conf = pos.shape[0]
        n_particles = pos.shape[1]
        n_dim = pos.shape[2]
        self.assertEqual(n_conf, 11)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_filenames_equals(self):
        parser = getparser("trajectory/filenames/equals")
        pos = parser.parse_quantity("particle_position").value
        n_conf = pos.shape[0]
        n_particles = pos.shape[1]
        n_dim = pos.shape[2]
        self.assertEqual(n_conf, 11)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_pdb(self):
        parser = getparser("trajectory/pdb")
        pos = parser.parse_quantity("particle_position").value
        n_conf = pos.shape[0]
        n_particles = pos.shape[1]
        n_dim = pos.shape[2]
        self.assertEqual(n_conf, 11)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

    def test_xyz(self):
        parser = getparser("trajectory/xyz")
        pos = parser.parse_quantity("particle_position").value
184
185
186
187
188
189
190
        n_conf = pos.shape[0]
        n_particles = pos.shape[1]
        n_dim = pos.shape[2]
        self.assertEqual(n_conf, 11)
        self.assertEqual(n_particles, 2)
        self.assertEqual(n_dim, 3)

191
if __name__ == '__main__':
192
193
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)
194
195
    logger = logging.getLogger("nomadparser")
    logger.setLevel(logging.ERROR)
196
197
198
    # unittest.main()

    suites = []
199
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForces))
200
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestParticleNumber))
201
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestFunctionals))
202
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestTrajectory))
203
204
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)
205
206
207
208
209
210

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