Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the properties of KpointsData class in the constructor. #5942

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions aiida/orm/nodes/data/array/kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
lists and meshes of k-points (i.e., points in the reciprocal space of a
periodic crystal structure).
"""
from typing import List, Optional

import numpy

from .array import ArrayData
Expand All @@ -36,6 +38,49 @@ class KpointsData(ArrayData):
set_cell_from_structure methods.
"""

def __init__(
self,
*args,
cell: Optional[List[List[float]]] = None,
pbc: Optional[List[bool]] = None,
mesh: Optional[List[int]] = None,
offset: Optional[List[float]] = None,
kpoints=None,
**kwargs
):
"""Allow setting cell and mesh in the constructor"""
super().__init__(*args, **kwargs)
if cell is not None:
self.set_cell(cell, pbc)

if mesh is not None:
self.set_kpoints_mesh(mesh, offset)

@classmethod
def from_structure(cls, structuredata: object):
kpt = cls()
kpt.set_cell_from_structure(structuredata=structuredata)
return kpt

@classmethod
def from_density(cls, distance: float, offset: Optional[List[float]] = None, force_parity: bool = False):
kpt = cls()
kpt.set_kpoints_mesh_from_density(distance=distance, offset=offset, force_parity=force_parity)
return kpt

@classmethod
def from_list(
cls,
kpoints: List[List[float]],
cartesian: bool = False,
labels: Optional[List[List]] = None,
weights: Optional[List[float]] = None,
fill_values: int = 0
):
kpt = cls()
kpt.set_kpoints(kpoints=kpoints, cartesian=cartesian, labels=labels, weights=weights, fill_values=fill_values)
return kpt

def get_description(self):
"""
Returns a string with infos retrieved from kpoints node's properties.
Expand Down
10 changes: 10 additions & 0 deletions tests/orm/nodes/data/test_kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def init_profile(self): # pylint: disable=unused-argument
# Define the expected reciprocal cell
val = 2. * np.pi / alat
self.expected_reciprocal_cell = np.array([[val, val, -val], [-val, val, val], [val, -val, val]])
self.mesh = [4, 4, 4]
self.offset = [0., 0., 0.]

def test_reciprocal_cell(self):
"""
Expand Down Expand Up @@ -82,3 +84,11 @@ def test_get_kpoints(self):
kpt2 = load_node(kpt.pk)
assert np.abs(kpt2.get_kpoints() - np.array(kpoints)).sum() == 0.
assert np.abs(kpt2.get_kpoints(cartesian=True) - np.array(cartesian_kpoints)).sum() == 0.

def test_init(self):
"""Test that the setters methods of `KpointsData` can be defined via the constructor."""

kpt = KpointsData(mesh=self.mesh, offset=self.offset)
offset_kpoints_size = self.mesh[0] * self.mesh[1] * self.mesh[2]

assert len(kpt.get_kpoints_mesh(print_list=True)) == offset_kpoints_size