run_tests.py 5.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
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.
"""
import os
import unittest
import logging
import numpy as np
from cp2kparser import CP2KParser
from nomadtoolkit import Analyzer
from nomadcore.unit_conversion.unit_conversion import convert_unit


#===============================================================================
16
def get_results(folder, metainfo_to_keep=None):
17
18
19
20
21
22
23
24
25
26
27
    """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
        parsed calculation resides.
        metaname: The quantity to extract.
    """
    dirname = os.path.dirname(__file__)
    folder = os.path.join(dirname, folder)
28
    parser = CP2KParser(folder, metainfo_to_keep)
29
30
    analyzer = Analyzer(parser)
    results = analyzer.parse()
31
32
33
34
35
36
    return results


#===============================================================================
def get_result(folder, metaname):
    results = get_results(folder, metaname)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    result = results[metaname]
    return result.value


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

    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")


61
#===============================================================================
62
class TestSectionSingleConfigurationCalculation(unittest.TestCase):
63

64
    results = get_results("section_single_configuration_calculation", "section_single_configuration_calculation")
65

66
    def test_energy_total_scf_iteration(self):
67
68
69
70
        energy_total = self.results["energy_total_scf_iteration"].value
        expected_result = convert_unit(np.array(-32.2320848878), "hartree")
        self.assertTrue(np.array_equal(energy_total[0], expected_result))

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    def test_energy_total(self):
        energy_total = self.results["energy_total"].value
        expected_result = convert_unit(np.array(-31.297885372811063), "hartree")
        self.assertTrue(np.array_equal(energy_total, expected_result))

    def test_atom_forces(self):
        atomic_forces = self.results["atom_forces"].value
        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],
            ]),
            "force_au"
        )
        self.assertTrue(np.array_equal(atomic_forces, expected_result))

93
94
95
96
97
98
99

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

    results = get_results("section_system_description", "section_system_description")

    def test_atom_label(self):
Lauri Himanen's avatar
Lauri Himanen committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        atom_labels = self.results["atom_label"].value
        expected_labels = np.array(8*["Si"])
        self.assertTrue(np.array_equal(atom_labels, expected_labels))

    def test_simulation_cell(self):
        cell = self.results["simulation_cell"].value
        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):
        n_atoms = self.results["number_of_atoms"].value
        self.assertEqual(n_atoms, 8)

    def test_atom_position(self):
        atom_position = self.results["atom_position"].value
        expected_position = convert_unit(np.array([4.073023, 4.073023, 1.357674]), "angstrom")
        self.assertTrue(np.array_equal(atom_position[-1, :], expected_position))

122

123
124
125
126
127
128
#===============================================================================
if __name__ == '__main__':
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)

    suites = []
Lauri Himanen's avatar
Lauri Himanen committed
129
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
130
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSectionSingleConfigurationCalculation))
131
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSectionSystemDescription))
132
133
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)