run_tests.py 3.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
94
95
96
97
98
99
100
101
102
103
104
105
"""
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


#===============================================================================
def get_result(folder, metaname):
    """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)
    parser = CP2KParser(contents=folder, metainfo_to_keep=[metaname])
    analyzer = Analyzer(parser)
    results = analyzer.parse()
    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")


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

    def test_from_output(self):
        cell = get_result("cell/output_file", "simulation_cell")
        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([[6, 0, 0], [0, 6, 0], [0, 0, 6]]), "angstrom")
        self.assertTrue(np.array_equal(cell, expected_cell))


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

    def test_output_file(self):
        n_atoms = get_result("particle_number/output_file", "number_of_atoms")
        self.assertEqual(n_atoms, 2)


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

    def test_output_file(self):
        atom_position = get_result("atom_position/output_file", "atom_position")
        expected_position = convert_unit(np.array([[2.5, 2.5, 2.5]]), "angstrom")
        self.assertTrue(np.array_equal(atom_position, expected_position))


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

    def test_output_file(self):
        atom_labels = get_result("atom_label/output_file", "atom_label")
        expected_labels = np.array(["Si"])
        self.assertTrue(np.array_equal(atom_labels, expected_labels))

#===============================================================================
if __name__ == '__main__':
    logger = logging.getLogger("cp2kparser")
    logger.setLevel(logging.ERROR)

    suites = []
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSimulationCell))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestNumberOfAtoms))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestAtomPosition))
    suites.append(unittest.TestLoader().loadTestsFromTestCase(TestAtomLabels))
    alltests = unittest.TestSuite(suites)
    unittest.TextTestRunner(verbosity=0).run(alltests)