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

Providing support for atomistic StructureData #6632

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
24 changes: 22 additions & 2 deletions src/aiida/cmdline/commands/cmd_data/cmd_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,17 @@
help='Set periodic boundary conditions for each lattice direction, where 0 means periodic and 1 means periodic.',
)
@click.option('--label', type=click.STRING, show_default=False, help='Set the structure node label (empty by default)')
@click.option(
'--to_atomistic',
type=click.BOOL,
default=False,
show_default=True,
help='Set the structure node as atomistic StructureData (default is False)',
)
@options.GROUP()
@options.DRY_RUN()
@decorators.with_dbenv()
def import_aiida_xyz(filename, vacuum_factor, vacuum_addition, pbc, label, group, dry_run):
def import_aiida_xyz(filename, vacuum_factor, vacuum_addition, pbc, label, to_atomistic, group, dry_run):
"""Import structure in XYZ format using AiiDA's internal importer"""
from aiida.orm import StructureData

Expand All @@ -215,6 +222,9 @@
if label:
new_structure.label = label

if to_atomistic:
new_structure = new_structure.to_atomistic()

Check warning on line 226 in src/aiida/cmdline/commands/cmd_data/cmd_structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/cmdline/commands/cmd_data/cmd_structure.py#L226

Added line #L226 was not covered by tests

_store_structure(new_structure, dry_run)

if group:
Expand All @@ -224,10 +234,17 @@
@structure_import.command('ase')
@click.argument('filename', type=click.Path(exists=True, dir_okay=False, resolve_path=True))
@click.option('--label', type=click.STRING, show_default=False, help='Set the structure node label (empty by default)')
@click.option(
'--to_atomistic',
type=click.BOOL,
default=False,
show_default=True,
help='Set the structure node as atomistic StructureData (default is False)',
)
@options.GROUP()
@options.DRY_RUN()
@decorators.with_dbenv()
def import_ase(filename, label, group, dry_run):
def import_ase(filename, label, to_atomistic, group, dry_run):
"""Import structure with the ase library that supports a number of different formats"""
from aiida.orm import StructureData

Expand All @@ -245,6 +262,9 @@
if label:
new_structure.label = label

if to_atomistic:
new_structure = new_structure.to_atomistic()

Check warning on line 266 in src/aiida/cmdline/commands/cmd_data/cmd_structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/cmdline/commands/cmd_data/cmd_structure.py#L266

Added line #L266 was not covered by tests

_store_structure(new_structure, dry_run)

if group:
Expand Down
24 changes: 16 additions & 8 deletions src/aiida/orm/nodes/data/array/kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,24 @@

:param structuredata: an instance of StructureData
"""
from aiida.orm import StructureData
from aiida.orm.nodes.data.structure import StructureData as LegacyStructureData
mikibonacci marked this conversation as resolved.
Show resolved Hide resolved
from aiida.orm.nodes.data.structure import has_atomistic

if not isinstance(structuredata, StructureData):
raise ValueError(
'An instance of StructureData should be passed to ' 'the KpointsData, found instead {}'.format(
structuredata.__class__
)
if not has_atomistic():
structures_classes = (LegacyStructureData,) # type: tuple
else:
from aiida_atomistic import StructureData # type: ignore[import-untyped]

Check warning on line 232 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L232

Added line #L232 was not covered by tests

structures_classes = (LegacyStructureData, StructureData)

Check warning on line 234 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L234

Added line #L234 was not covered by tests

if not isinstance(structuredata, structures_classes):
raise TypeError(

Check warning on line 237 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L237

Added line #L237 was not covered by tests
f'An instance of {structures_classes} should be passed to '
f'the KpointsData, found instead {type(structuredata)}'
)
cell = structuredata.cell
self.set_cell(cell, structuredata.pbc)
else:
cell = structuredata.cell
self.set_cell(cell, structuredata.pbc)

def set_cell(self, cell, pbc=None):
"""Set a cell to be used for symmetry analysis.
Expand Down
35 changes: 35 additions & 0 deletions src/aiida/orm/nodes/data/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@
return True


def has_atomistic() -> bool:
""":return: True if theaiida-atomistic module can be imported, False otherwise."""
try:
import aiida_atomistic # noqa: F401
except ImportError:
return False
return True

Check warning on line 111 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L111

Added line #L111 was not covered by tests
mikibonacci marked this conversation as resolved.
Show resolved Hide resolved


def get_pymatgen_version():
""":return: string with pymatgen version, None if can not import."""
if not has_pymatgen():
Expand Down Expand Up @@ -1876,6 +1885,32 @@
positions = [list(site.position) for site in self.sites]
return Molecule(species, positions)

def to_atomistic(self):
"""
Returns the atomistic StructureData version of the orm.StructureData one.
"""
if not has_atomistic():
raise ImportError(

Check warning on line 1893 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1892-L1893

Added lines #L1892 - L1893 were not covered by tests
mikibonacci marked this conversation as resolved.
Show resolved Hide resolved
'aiida-atomistic plugin is not installed, \
please install it to have full support for atomistic structures'
)
else:
from aiida_atomistic import StructureData, StructureDataMutable

Check warning on line 1898 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1898

Added line #L1898 was not covered by tests

atomistic = StructureDataMutable()
atomistic.set_pbc(self.pbc)
atomistic.set_cell(self.cell)

Check warning on line 1902 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1900-L1902

Added lines #L1900 - L1902 were not covered by tests

for site in self.sites:
atomistic.add_atom(

Check warning on line 1905 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1904-L1905

Added lines #L1904 - L1905 were not covered by tests
symbols=self.get_kind(site.kind_name).symbol,
masses=self.get_kind(site.kind_name).mass,
positions=site.position,
kinds=site.kind_name,
)

return StructureData.from_mutable(atomistic)

Check warning on line 1912 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1912

Added line #L1912 was not covered by tests


class Kind:
"""This class contains the information about the species (kinds) of the system.
Expand Down
19 changes: 16 additions & 3 deletions src/aiida/tools/data/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@

import numpy as np

from aiida.common import exceptions
from aiida.common.constants import elements
from aiida.engine import calcfunction
from aiida.orm.nodes.data.structure import Kind, Site, StructureData
from aiida.orm.nodes.data.structure import Kind, Site
from aiida.orm.nodes.data.structure import StructureData as LegacyStructureData
from aiida.plugins import DataFactory

try:
StructureData = DataFactory('atomistic.structure')
HAS_ATOMISTIC = True

Check warning on line 29 in src/aiida/tools/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/data/structure.py#L29

Added line #L29 was not covered by tests
except exceptions.MissingEntryPointError:
structures_classes = (LegacyStructureData,)
HAS_ATOMISTIC = False
else:
structures_classes = (LegacyStructureData, StructureData) # type: ignore[assignment]

Check warning on line 34 in src/aiida/tools/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/data/structure.py#L34

Added line #L34 was not covered by tests

__all__ = ('spglib_tuple_to_structure', 'structure_to_spglib_tuple')

Expand All @@ -35,7 +47,8 @@
kwargs = {}
if parameters is not None:
kwargs = parameters.get_dict()
cif = CifData(ase=struct.get_ase(**kwargs))
ase_structure = struct.get_ase(**kwargs) if isinstance(struct, LegacyStructureData) else struct.to_ase(**kwargs)
cif = CifData(ase=ase_structure)
formula = struct.get_formula(mode='hill', separator=' ')
for i in cif.values.keys():
cif.values[i]['_symmetry_space_group_name_H-M'] = 'P 1'
Expand Down Expand Up @@ -152,7 +165,7 @@
except KeyError as exc:
raise ValueError(f'Unable to find kind in kind_info for number {exc.args[0]}')

structure = StructureData(cell=cell)
structure = LegacyStructureData(cell=cell)
for k in _kinds:
structure.append_kind(k)
abs_pos = np.dot(rel_pos, cell)
Expand Down
25 changes: 20 additions & 5 deletions tests/orm/nodes/data/test_kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@
import numpy as np
import pytest

from aiida.orm import KpointsData, StructureData, load_node
from aiida.orm import KpointsData, load_node
from aiida.orm import StructureData as LegacyStructureData
from aiida.orm.nodes.data.structure import has_atomistic

skip_atomistic = pytest.mark.skipif(not has_atomistic(), reason='aiida-atomistic not installed')

if not has_atomistic():
structures_classes = [LegacyStructureData, pytest.param('StructureData', marks=skip_atomistic)]
else:
from aiida_atomistic import StructureData # type: ignore[import-untyped]

structures_classes = [LegacyStructureData, StructureData]


@pytest.mark.parametrize('structure_class', structures_classes)
class TestKpoints:
"""Test for the `Kpointsdata` class."""

@pytest.fixture(autouse=True)
def init_profile(self):
"""Initialize the profile."""
def generate_structure(self, structure_class):
"""Generate the StructureData."""
alat = 5.430 # angstrom
cell = [
[
Expand All @@ -35,10 +47,13 @@ def init_profile(self):
[0.5 * alat, 0.0, 0.5 * alat],
]
self.alat = alat
structure = StructureData(cell=cell)
structure = LegacyStructureData(cell=cell)
structure.append_atom(position=(0.000 * alat, 0.000 * alat, 0.000 * alat), symbols=['Si'])
structure.append_atom(position=(0.250 * alat, 0.250 * alat, 0.250 * alat), symbols=['Si'])
self.structure = structure
if structure_class == LegacyStructureData:
self.structure = structure
else:
self.structure = LegacyStructureData.to_atomistic(structure)
# Define the expected reciprocal cell
val = 2.0 * np.pi / alat
self.expected_reciprocal_cell = np.array([[val, val, -val], [-val, val, val], [val, -val, val]])
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_formula,
get_pymatgen_version,
has_ase,
has_atomistic,
has_pymatgen,
has_spglib,
)
Expand Down Expand Up @@ -68,6 +69,7 @@ def simplify(string):
skip_spglib = pytest.mark.skipif(not has_spglib(), reason='Unable to import spglib')
skip_pycifrw = pytest.mark.skipif(not has_pycifrw(), reason='Unable to import PyCifRW')
skip_pymatgen = pytest.mark.skipif(not has_pymatgen(), reason='Unable to import pymatgen')
skip_atomistic = pytest.mark.skipif(not has_atomistic(), reason='Unable to import aiida-atomistic')


@skip_pymatgen
Expand Down Expand Up @@ -1852,6 +1854,26 @@ def test_clone(self):
assert round(abs(c.sites[1].position[i] - 1.0), 7) == 0


@skip_atomistic
def test_to_atomistic(self):
"""Test the conversion from orm.StructureData to the atomistic structure."""

# Create a structure with a single atom
from aiida_atomistic import StructureData as AtomisticStructureData

legacy = StructureData(cell=((1.0, 0.0, 0.0), (0.0, 2.0, 0.0), (0.0, 0.0, 3.0)))
legacy.append_atom(position=(0.0, 0.0, 0.0), symbols=['Ba'], name='Ba1')

# Convert to atomistic structure
structure = legacy.to_atomistic()

# Check that the structure is as expected
assert isinstance(structure, AtomisticStructureData)
assert structure.properties.sites[0].kinds == legacy.sites[0].kind_name
assert structure.properties.sites[0].positions == list(legacy.sites[0].position)
assert structure.properties.cell == legacy.cell


class TestStructureDataFromAse:
"""Tests the creation of Sites from/to a ASE object."""

Expand Down
Loading