Skip to content

Commit

Permalink
HubbardStructureData: add compatibility for <3D structures
Browse files Browse the repository at this point in the history
The `HubbardStructureData.append_hubbard_parameter()` method currently fails for
structures that don't have 3-dimensional periodic boundary conditions. This is caused
by a restriction in the StructureData.get_pymatgen_structure()` method, which raises a
`ValueError` if the periodic boundary conditions are not (True, True, True). This has
been fixed in a recent commit on `aiida-core`:

aiidateam/aiida-core@adcce4b

But this will take some time to be released and would restrict our compatibility to
`aiida-core>=2.6.0`.

Here we directly construct a list of `PeriodicSite` objects from the `sites` list of the
object obtained from `StructureData.get_pymatgen()`, which can be both either a
pymatgen `Structure` or `Molecule`, depending on the dimensionality of the structure.

Co-authored-by: AndresOrtegaGuerrero <34098967+AndresOrtegaGuerrero@users.noreply.github.com>
  • Loading branch information
mbercx and AndresOrtegaGuerrero committed Feb 13, 2024
1 parent 960a371 commit d645069
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/aiida_quantumespresso/data/hubbard_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aiida.orm import StructureData
import numpy as np
from pymatgen.core import Lattice, PeriodicSite

from aiida_quantumespresso.common.hubbard import Hubbard, HubbardParameters

Expand Down Expand Up @@ -110,8 +111,14 @@ def append_hubbard_parameter(
:param hubbard_type: hubbard type (U, V, J, ...), defaults to 'Ueff'
(see :class:`~aiida_quantumespresso.common.hubbard.Hubbard` for full allowed values)
"""
pymat = self.get_pymatgen_structure()
sites = pymat.sites
sites = [
PeriodicSite(
species=site.species,
coords=site.coords,
lattice=Lattice(self.cell, pbc=self.pbc),
coords_are_cartesian=True
) for site in self.get_pymatgen().sites
]

if any((atom_index > len(sites) - 1, neighbour_index > len(sites) - 1)):
raise ValueError(
Expand Down
7 changes: 4 additions & 3 deletions tests/data/test_hubbard_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_from_structure(generate_structure, generate_hubbard):


@pytest.mark.usefixtures('aiida_profile')
@pytest.mark.parametrize('structure_name', ('silicon',))
@pytest.mark.parametrize('structure_name', ('silicon', '2D-xy-arsenic'))
@pytest.mark.parametrize(
'parameters', (
((0, '1s', 0, '1s', 5.0, (0, 0, 0), 'Ueff'),),
Expand All @@ -88,13 +88,14 @@ def test_append_hubbard_parameters(data_regression, generate_structure, structur
assert len(hubbard_structure.hubbard.parameters) == len(set(parameters))


@pytest.mark.parametrize('structure_name', ('cobalt-prim', '1D-x-carbon'))
@pytest.mark.parametrize('parameter', (
(0, '1s', 1, '1s', 5.0, None, 'V'),
(0, '1s', 1, '1s', 5.0, (0, 0, 0), 'V'),
))
def test_append_hubbard_parameters_invalid_index(generate_structure, parameter):
def test_append_hubbard_parameters_invalid_index(generate_structure, structure_name, parameter):
"""Test the `append_hubbard_parameters` method with invalid index."""
hubbard_structure = HubbardStructureData.from_structure(generate_structure('cobalt-prim'))
hubbard_structure = HubbardStructureData.from_structure(generate_structure(structure_name))

with pytest.raises(ValueError, match='atom_index and neighbour_index must be within the range'):
hubbard_structure.append_hubbard_parameter(*parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- - 0
- 1s
- 0
- 1s
- 5.0
- - 0
- 0
- 0
- Ueff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- - 0
- 1s
- 1
- 1s
- 5.0
- - 0
- 0
- 0
- V
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- - 0
- 1s
- 0
- 1s
- 5.0
- - 0
- 0
- 0
- Ueff
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- - 0
- 1s
- 0
- 1s
- 5.0
- - 0
- 0
- 0
- Ueff
- - 0
- 1s
- 1
- 1s
- 5.0
- - 0
- 1
- 0
- V

0 comments on commit d645069

Please sign in to comment.