Commit 38b1a1d4 authored by Lauri Himanen's avatar Lauri Himanen

Added unit tests, updated readmes, updated gitignore.

parent 703f63e5
# List of Example Files
This directory contains examples calculations of CP2K.
The content of the folders and files is detailed below.
## Folders
### Si_bulk8
Energy and force calculation of bulk Si with 8 atom unit cell. LDA functional
and Broyden mixing.
This diff is collapsed.
# Unit tests
This directory contains unit tests to evaluate the correctness of the parser in
a systematic way. Ideally each parsed metainfo should have at least one unit
test, and if the resulting values are predetermined, the available values
should all be tested individually. Also certain scenarios that should produce a
parsing error should be tested.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
"""
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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment