-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from robinzyb/devel
Add Pytest for cp2kcell
- Loading branch information
Showing
7 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import regex as re | ||
import numpy as np | ||
|
||
DIPOLE_RE = re.compile( | ||
r""" | ||
\s{2}Dipole\smoment\s\[Debye\]\n | ||
\s{4} | ||
X=\s{3}(?P<x>[\s-]\d+\.\d+)\s | ||
Y=\s{3}(?P<y>[\s-]\d+\.\d+)\s | ||
Z=\s{3}(?P<z>[\s-]\d+\.\d+)\s | ||
\s{4}Total=\s{4}(?P<total>[\s-]\d+\.\d+) | ||
""", | ||
re.VERBOSE | ||
) | ||
|
||
#TODO write a pytest for this | ||
|
||
def parse_dipole_list(output_file): | ||
dipole_list = [] | ||
for match in DIPOLE_RE.finditer(output_file): | ||
for x, y, z, total in zip(*match.captures("x", "y", "z", "total")): | ||
dipole = [x, y, z, total] | ||
dipole_list.append(dipole) | ||
if dipole_list: | ||
return np.array(dipole_list, dtype=float) | ||
else: | ||
return None | ||
|
||
""" | ||
Reference Point [Bohr] 0.00000000 0.00000000 0.00000000 | ||
Charges | ||
Electronic= 864.00000000 Core= -864.00000000 Total= 0.00000000 | ||
Dipole vectors are based on the periodic (Berry phase) operator. | ||
They are defined modulo integer multiples of the cell matrix [Debye]. | ||
[X] [ 46.55265580 0.00000000 0.00000000 ] [i] | ||
[Y]=[ 0.00000000 54.46353324 0.00000000 ]*[j] | ||
[Z] [ 0.00000000 0.00000000 54.47313965 ] [k] | ||
Dipole moment [Debye] | ||
X= -0.07183634 Y= -0.07690441 Z= 1.13302571 Total= 1.13790246 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from email.policy import default | ||
import click | ||
from cp2kdata import Cp2kCube | ||
from .funcs import * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import pytest | ||
import numpy as np | ||
from ase.geometry.cell import cellpar_to_cell | ||
from ase.geometry.cell import cell_to_cellpar | ||
from cp2kdata.cell import Cp2kCell # Replace 'your_module' with the actual module containing the Cp2kCell class. | ||
|
||
class TestCp2kCell: | ||
# Define expected cell matrices for different cases | ||
def _create_expected_cell_matrix(self, cell_param): | ||
if isinstance(cell_param, float): | ||
return np.array([[cell_param, 0, 0], [0, cell_param, 0], [0, 0, cell_param]]) | ||
elif cell_param.shape == (3,): | ||
return np.array([[cell_param[0], 0, 0], [0, cell_param[1], 0], [0, 0, cell_param[2]]]) | ||
elif cell_param.shape == (6,): | ||
return cellpar_to_cell(cell_param) | ||
elif cell_param.shape == (3, 3): | ||
return cell_param | ||
def _create_expected_grid_spacing_matrix(self, cell_matrix, grid_point, grid_spacing_matrix): | ||
if (grid_point is None) and (grid_spacing_matrix is None): | ||
grid_point = None | ||
grid_spacing_matrix = None | ||
print("No grid point information") | ||
elif (grid_point is None) and (grid_spacing_matrix is not None): | ||
grid_spacing_matrix = grid_spacing_matrix | ||
grid_point = np.round(cell_matrix/grid_spacing_matrix) | ||
elif (grid_point is not None) and (grid_spacing_matrix is None): | ||
grid_point = np.array(grid_point) | ||
grid_spacing_matrix = cell_matrix/grid_point[:, np.newaxis] | ||
elif (grid_point is not None) and (grid_spacing_matrix is not None): | ||
grid_point = np.array(grid_point) | ||
grid_spacing_matrix = np.array(grid_spacing_matrix) | ||
return grid_spacing_matrix | ||
|
||
def _create_expected_cell_param(self, cell_param): | ||
if isinstance(cell_param, float): | ||
return np.array([cell_param, cell_param, cell_param, 90.0, 90.0, 90.0]) | ||
elif cell_param.shape == (3,): | ||
return np.array([cell_param[0], cell_param[1], cell_param[2], 90.0, 90.0, 90.0]) | ||
elif cell_param.shape == (6,): | ||
return cell_param | ||
elif cell_param.shape == (3, 3): | ||
return cell_to_cellpar(cell_param) | ||
|
||
# Define sample data using a fixture | ||
@pytest.fixture(params=[ | ||
(np.array([10.0, 12.0, 15.0]), None, None), | ||
#(np.array([10.0, 12.0, 15.0]), None, np.array([[1.0, 0.0, 0.0],[0,1.0,0],[0,0,1.0]])), | ||
(np.array([10.0, 12.0, 15.0, 90.0, 90.0, 90.0]), None, None), | ||
(np.array([[10.0, 0, 0], [0, 12.0, 0], [0, 0, 15.0]]), [2, 2, 2], None) | ||
]) | ||
def sample_data(self, request): | ||
return request.param | ||
|
||
def test_constructor(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
assert np.array_equal(cell.cell_matrix, self._create_expected_cell_matrix(cell_param)) | ||
|
||
def test_copy(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
copied_cell = cell.copy() | ||
assert np.array_equal(cell.cell_matrix, copied_cell.cell_matrix) | ||
assert np.array_equal(cell.grid_point, copied_cell.grid_point) | ||
assert np.array_equal(cell.grid_spacing_matrix, copied_cell.grid_spacing_matrix) | ||
|
||
def test_get_volume(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
expected_volume = np.linalg.det(self._create_expected_cell_matrix(cell_param)) | ||
assert cell.get_volume() == expected_volume | ||
|
||
def test_get_dv(self, sample_data, capsys): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
if (grid_point is None) and (grid_spacing_matrix is None): | ||
cell.get_dv() | ||
captured = capsys.readouterr() | ||
assert captured.out.splitlines()[-1] == "No grid point information is available" | ||
else: | ||
print(cell.cell_matrix, grid_point, grid_spacing_matrix) | ||
expected_grid_spacing_matrix = self._create_expected_grid_spacing_matrix(cell.cell_matrix, grid_point, grid_spacing_matrix) | ||
expected_dv = np.linalg.det(expected_grid_spacing_matrix) | ||
assert cell.get_dv() == expected_dv | ||
|
||
def test_get_cell_param(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
expected_cell_param = self._create_expected_cell_param(cell_param) | ||
assert np.array_equal(cell.get_cell_param(), expected_cell_param) | ||
|
||
def test_get_cell_angles(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
expected_cell_param = self._create_expected_cell_param(cell_param) | ||
expected_cell_angles = expected_cell_param[3:] | ||
assert np.array_equal(cell.get_cell_angles(), expected_cell_angles) | ||
|
||
def test_get_cell_lengths(self, sample_data): | ||
cell_param, grid_point, grid_spacing_matrix = sample_data | ||
cell = Cp2kCell(cell_param, grid_point, grid_spacing_matrix) | ||
expected_cell_param = self._create_expected_cell_param(cell_param) | ||
expected_cell_lengths = expected_cell_param[:3] | ||
assert np.array_equal(cell.get_cell_lengths(), expected_cell_lengths) |