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

#===============================================================================
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)

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

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

122
123
    def test_xyz_multiplication(self):
        parser = getparser("particle_number/xyz/multiplication")
124
        n = parser.get_quantity_unformatted("particle_number").value
125
126
127
128
129
130
131
132
133
134
135
        self.assertEqual(n, 12)

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

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

137
if __name__ == '__main__':
138
139
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)
140
141
142
    # unittest.main()

    suites = []
143
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForces))
144
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestParticleNumber))
145
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestFunctionals))
146
147
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)
148
149
150
151
152
153

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