run_tests.py 7.95 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
import cStringIO
9
10
11
12
13
14


#===============================================================================
def getparser(folder):
    path = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(path, folder)
15
16
    dump = cStringIO.StringIO()
    parser = get_parser(path, test_mode=True, stream=dump)
17
18
19
20
21
22
23
24
    return parser


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

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

    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")
47
        forces = parser.parse_quantity("particle_forces")
48
49
50
        n_conf = forces.shape[0]
        n_particles = forces.shape[1]
        n_dim = forces.shape[2]
51
        self.assertEqual(n_conf, 201)
52
        self.assertEqual(n_particles, 2)
53
54
55
56
        self.assertEqual(n_dim, 3)

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

    def test_forces_in_outputfile_0(self):
        parser = getparser("forces/outputfile/0")
67
        forces = parser.parse_quantity("particle_forces")
68
69
        self.assertEqual(forces, None)

70
71
    def test_forces_in_singlexyzfile_n(self):
        parser = getparser("forces/singlexyzfile/n")
72
        forces = parser.parse_quantity("particle_forces")
73
74
75
76
77
78
79
80
81
        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")
82
        forces = parser.parse_quantity("particle_forces")
83
84
85
86
87
88
89
90
91
        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")
92
        forces = parser.parse_quantity("particle_forces")
93
94
        self.assertEqual(forces, None)

95
96
97
98
99
100

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

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

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

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

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

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

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

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

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

139
140
141
142
143
144

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

    def test_filenames_bare(self):
        parser = getparser("trajectory/filenames/bare")
145
        pos = parser.parse_quantity("particle_position")
146
147
148
149
150
151
152
153
154
        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")
155
        pos = parser.parse_quantity("particle_position")
156
157
158
159
160
161
162
163
164
        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")
165
        pos = parser.parse_quantity("particle_position")
166
167
168
169
170
171
172
173
174
        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")
175
        pos = parser.parse_quantity("particle_position")
176
177
178
179
180
181
182
183
184
        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")
185
        pos = parser.parse_quantity("particle_position")
186
187
188
189
190
191
192
        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)

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    def test_xmol(self):
        parser = getparser("trajectory/xmol")
        pos = parser.parse_quantity("particle_position")
        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_atomic(self):
        parser = getparser("trajectory/atomic")
        pos = parser.parse_quantity("particle_position")
        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)

213
if __name__ == '__main__':
214
215
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)
216
217
    logger = logging.getLogger("nomadparser")
    logger.setLevel(logging.ERROR)
218
219
220
    # unittest.main()

    suites = []
221
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForces))
222
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestParticleNumber))
223
224
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestFunctionals))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestTrajectory))
225
226
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)
227
228
229
230
231
232

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