run_tests.py 5.97 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
    """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
23
            parsed calculation resides.
24
25
26
        metaname: The quantity to extract.
    """
    dirname = os.path.dirname(__file__)
27
28
    filename = os.path.join(dirname, folder, "unittest.out")
    parser = CP2KParser(filename, 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


#===============================================================================
class TestXCFunctional(unittest.TestCase):
43
44
    """Tests that the XC functionals can be properly parsed.
    """
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

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


63
#===============================================================================
64
65
66
class TestEnergyForce(unittest.TestCase):
    """Tests for a CP2K calculation with RUN_TYPE ENERGY_FORCE.
    """
67

Lauri Himanen's avatar
Lauri Himanen committed
68
    def setUp(self):
69
        self.results = get_results("energy_force", "section_run")
70

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

76
    def test_energy_total(self):
Lauri Himanen's avatar
Lauri Himanen committed
77
        energy_total = self.results["energy_total"]
78
79
80
81
        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
82
        atomic_forces = self.results["atom_forces"]
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],
            ]),
94
            "forceAu"
95
96
97
        )
        self.assertTrue(np.array_equal(atomic_forces, expected_result))

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

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

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    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)
145

146
147
#===============================================================================
if __name__ == '__main__':
Lauri Himanen's avatar
Lauri Himanen committed
148
    pass
149
150
151
152
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)

    suites = []
Lauri Himanen's avatar
Lauri Himanen committed
153
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
154
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestEnergyForce))
155
156
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)