run_tests.py 7.55 KB
Newer Older
1
2
3
4
"""
This is a module for unit testing the CP2K parser. The unit tests are run with
a custom backend that outputs the results directly into native python object for
easier and faster analysis.
5
6
7
8
9
10
11

Each property that has an enumerable list of different possible options is
assigned a new test class, that should ideally test through all the options.

The properties that can have any value imaginable will be tested only for one
specific case inside a test class that is designed for a certain type of run
(MD, optimization, QM/MM, etc.)
12
13
14
15
16
17
18
19
20
21
"""
import os
import unittest
import logging
import numpy as np
from cp2kparser import CP2KParser
from nomadcore.unit_conversion.unit_conversion import convert_unit


#===============================================================================
22
def get_results(folder, metainfo_to_keep=None):
23
24
25
26
27
28
    """Get the given result from the calculation in the given folder by using
    the Analyzer in the nomadtoolkit package. Tries to optimize the parsing by
    giving the metainfo_to_keep argument.

    Args:
        folder: The folder relative to the directory of this script where the
29
            parsed calculation resides.
30
31
32
        metaname: The quantity to extract.
    """
    dirname = os.path.dirname(__file__)
33
34
    filename = os.path.join(dirname, folder, "unittest.out")
    parser = CP2KParser(filename, metainfo_to_keep)
35
    results = parser.parse()
36
37
38
39
40
41
    return results


#===============================================================================
def get_result(folder, metaname):
    results = get_results(folder, metaname)
42
    result = results[metaname]
Lauri Himanen's avatar
Lauri Himanen committed
43
    return result
44
45
46
47


#===============================================================================
class TestXCFunctional(unittest.TestCase):
48
49
    """Tests that the XC functionals can be properly parsed.
    """
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    def test_pade(self):
        xc = get_result("XC_functional/pade", "XC_functional")
        self.assertEqual(xc, "LDA_XC_TETER93")

    def test_lda(self):
        xc = get_result("XC_functional/lda", "XC_functional")
        self.assertEqual(xc, "LDA_XC_TETER93")

    def test_becke88(self):
        xc = get_result("XC_functional/becke88", "XC_functional")
        self.assertEqual(xc, "GGA_X_B88")

    def test_blyp(self):
        xc = get_result("XC_functional/blyp", "XC_functional")
        self.assertEqual(xc, "GGA_C_LYP_GGA_X_B88")


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#===============================================================================
class TestSelfInteractionCorrectionMethod(unittest.TestCase):
    """Tests that the self-interaction correction can be properly parsed.
    """

    def test_no(self):
        sic = get_result("sic/no", "self_interaction_correction_method")
        self.assertEqual(sic, "")

    def test_ad(self):
        sic = get_result("sic/ad", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_AD")

    def test_explicit_orbitals(self):
        sic = get_result("sic/explicit_orbitals", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_EXPLICIT_ORBITALS")

    def test_mauri_spz(self):
        sic = get_result("sic/mauri_spz", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_MAURI_SPZ")

    def test_mauri_us(self):
        sic = get_result("sic/mauri_us", "self_interaction_correction_method")
        self.assertEqual(sic, "SIC_MAURI_US")


94
#===============================================================================
95
96
97
class TestEnergyForce(unittest.TestCase):
    """Tests for a CP2K calculation with RUN_TYPE ENERGY_FORCE.
    """
98

Lauri Himanen's avatar
Lauri Himanen committed
99
    def setUp(self):
100
        self.results = get_results("energy_force", "section_run")
101

102
    def test_energy_total_scf_iteration(self):
Lauri Himanen's avatar
Lauri Himanen committed
103
        energy_total = self.results["energy_total_scf_iteration"]
104
105
106
        expected_result = convert_unit(np.array(-32.2320848878), "hartree")
        self.assertTrue(np.array_equal(energy_total[0], expected_result))

107
    def test_energy_total(self):
Lauri Himanen's avatar
Lauri Himanen committed
108
        energy_total = self.results["energy_total"]
109
110
111
112
        expected_result = convert_unit(np.array(-31.297885372811063), "hartree")
        self.assertTrue(np.array_equal(energy_total, expected_result))

    def test_atom_forces(self):
Lauri Himanen's avatar
Lauri Himanen committed
113
        atomic_forces = self.results["atom_forces"]
114
115
116
117
118
119
120
121
122
123
124
        expected_result = convert_unit(
            np.array([
                [0.00000000, 0.00000000, 0.00000000],
                [0.00000000, 0.00000001, 0.00000001],
                [0.00000001, 0.00000001, 0.00000000],
                [0.00000001, 0.00000000, 0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
                [-0.00000001, -0.00000001, -0.00000001],
            ]),
125
            "forceAu"
126
127
128
        )
        self.assertTrue(np.array_equal(atomic_forces, expected_result))

129
    def test_atom_label(self):
Lauri Himanen's avatar
Lauri Himanen committed
130
        atom_labels = self.results["atom_label"]
Lauri Himanen's avatar
Lauri Himanen committed
131
132
133
134
        expected_labels = np.array(8*["Si"])
        self.assertTrue(np.array_equal(atom_labels, expected_labels))

    def test_simulation_cell(self):
Lauri Himanen's avatar
Lauri Himanen committed
135
        cell = self.results["simulation_cell"]
Lauri Himanen's avatar
Lauri Himanen committed
136
137
138
139
140
141
142
143
        n_vectors = cell.shape[0]
        n_dim = cell.shape[1]
        self.assertEqual(n_vectors, 3)
        self.assertEqual(n_dim, 3)
        expected_cell = convert_unit(np.array([[5.431, 0, 0], [0, 5.431, 0], [0, 0, 5.431]]), "angstrom")
        self.assertTrue(np.array_equal(cell, expected_cell))

    def test_number_of_atoms(self):
Lauri Himanen's avatar
Lauri Himanen committed
144
        n_atoms = self.results["number_of_atoms"]
Lauri Himanen's avatar
Lauri Himanen committed
145
146
147
        self.assertEqual(n_atoms, 8)

    def test_atom_position(self):
Lauri Himanen's avatar
Lauri Himanen committed
148
        atom_position = self.results["atom_position"]
Lauri Himanen's avatar
Lauri Himanen committed
149
150
151
        expected_position = convert_unit(np.array([4.073023, 4.073023, 1.357674]), "angstrom")
        self.assertTrue(np.array_equal(atom_position[-1, :], expected_position))

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    def test_cp2k_filenames(self):
        input_filename = self.results["cp2k_input_filename"]
        expected_input = "si_bulk8.inp"
        self.assertTrue(input_filename, expected_input)

        bs_filename = self.results["cp2k_basis_set_filename"]
        expected_bs = "../BASIS_SET"
        self.assertEqual(bs_filename, expected_bs)

        geminal_filename = self.results["cp2k_geminal_filename"]
        expected_geminal = "BASIS_GEMINAL"
        self.assertEqual(geminal_filename, expected_geminal)

        potential_filename = self.results["cp2k_potential_filename"]
        expected_potential = "../GTH_POTENTIALS"
        self.assertEqual(potential_filename, expected_potential)

        mm_potential_filename = self.results["cp2k_mm_potential_filename"]
        expected_mm_potential = "MM_POTENTIAL"
        self.assertEqual(mm_potential_filename, expected_mm_potential)

        coordinate_filename = self.results["cp2k_coordinate_filename"]
        expected_coordinate = "__STD_INPUT__"
        self.assertEqual(coordinate_filename, expected_coordinate)
176

177
178
179
180
181
182
183
184
    def test_target_multiplicity(self):
        multiplicity = self.results["target_multiplicity"]
        self.assertEqual(multiplicity, 1)

    def test_total_charge(self):
        charge = self.results["total_charge"]
        self.assertEqual(charge, 0)

185
186
#===============================================================================
if __name__ == '__main__':
Lauri Himanen's avatar
Lauri Himanen committed
187
    pass
188
189
190
191
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)

    suites = []
Lauri Himanen's avatar
Lauri Himanen committed
192
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
193
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestEnergyForce))
194
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSelfInteractionCorrectionMethod))
195
196
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)