run_tests.py 5.2 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
    result = results[metaname]
Lauri Himanen's avatar
Lauri Himanen committed
38
    return result
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60


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

Lauri Himanen's avatar
Lauri Himanen committed
64
65
    def setUp(self):
        self.results = get_results("section_single_configuration_calculation", "section_single_configuration_calculation")
66

67
    def test_energy_total_scf_iteration(self):
Lauri Himanen's avatar
Lauri Himanen committed
68
        energy_total = self.results["energy_total_scf_iteration"]
69
70
71
        expected_result = convert_unit(np.array(-32.2320848878), "hartree")
        self.assertTrue(np.array_equal(energy_total[0], expected_result))

72
    def test_energy_total(self):
Lauri Himanen's avatar
Lauri Himanen committed
73
        energy_total = self.results["energy_total"]
74
75
76
77
        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
78
        atomic_forces = self.results["atom_forces"]
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        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))

94
95
96
97

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

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

    def test_atom_label(self):
Lauri Himanen's avatar
Lauri Himanen committed
102
        atom_labels = self.results["atom_label"]
Lauri Himanen's avatar
Lauri Himanen committed
103
104
105
106
        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
107
        cell = self.results["simulation_cell"]
Lauri Himanen's avatar
Lauri Himanen committed
108
109
110
111
112
113
114
115
        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
116
        n_atoms = self.results["number_of_atoms"]
Lauri Himanen's avatar
Lauri Himanen committed
117
118
119
        self.assertEqual(n_atoms, 8)

    def test_atom_position(self):
Lauri Himanen's avatar
Lauri Himanen committed
120
        atom_position = self.results["atom_position"]
Lauri Himanen's avatar
Lauri Himanen committed
121
122
123
        expected_position = convert_unit(np.array([4.073023, 4.073023, 1.357674]), "angstrom")
        self.assertTrue(np.array_equal(atom_position[-1, :], expected_position))

124

125
126
#===============================================================================
if __name__ == '__main__':
Lauri Himanen's avatar
Lauri Himanen committed
127
    pass
128
129
130
131
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)

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