Skip to content

Commit cdd3e3d

Browse files
committed
Apply review 2
1 parent 4317238 commit cdd3e3d

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/aiida/orm/nodes/data/array/trajectory.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
import collections.abc
1414
from typing import List
15+
from warnings import warn
1516

1617
from aiida.common.pydantic import MetadataField
18+
from aiida.common.warnings import AiidaDeprecationWarning
1719

1820
from .array import ArrayData
1921

@@ -86,6 +88,8 @@ def _internal_validate(self, stepids, cells, symbols, positions, times, velociti
8688
)
8789
if not (isinstance(pbc, (list, tuple)) and len(pbc) == 3 and all(isinstance(val, bool) for val in pbc)):
8890
raise ValueError('`pbc` must be a list/tuple of length three with boolean values.')
91+
if cells is None and list(pbc) != [False, False, False]:
92+
raise ValueError('Periodic boundary conditions are only possible when a cell is defined.')
8993

9094
def set_trajectory(
9195
self, symbols, positions, stepids=None, cells=None, times=None, velocities=None, pbc: None | list | tuple = None
@@ -145,8 +149,15 @@ def set_trajectory(
145149
"""
146150
import numpy
147151

148-
pbc_default = [True, True, True] if cells is not None else [False, False, False]
149-
pbc = pbc or pbc_default
152+
if cells is None:
153+
pbc = pbc or [False, False, False]
154+
elif pbc is None:
155+
warn(
156+
"When 'cells' is not None, the periodic boundary conditions should be explicitly specified via the "
157+
"'pbc' keyword argument. Defaulting to '[True, True, True]', but this will raise in v3.0.0.",
158+
AiidaDeprecationWarning,
159+
)
160+
pbc = [True, True, True]
150161

151162
self._internal_validate(stepids, cells, symbols, positions, times, velocities, pbc)
152163
# set symbols/pbc as attributes for easier querying

tests/orm/nodes/data/test_trajectory.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from aiida.common.warnings import AiidaDeprecationWarning
67
from aiida.orm import StructureData, TrajectoryData, load_node
78

89

@@ -147,8 +148,8 @@ def test_trajectory_get_step_structure(self, trajectory_data):
147148
with pytest.raises(IndexError):
148149
trajectory.get_step_structure(500)
149150

150-
def test_trajectory_pbc(self, trajectory_data):
151-
"""Test the `pbc` for the `TrajectoryData`."""
151+
def test_trajectory_pbc_structures(self, trajectory_data):
152+
"""Test the `pbc` for the `TrajectoryData` using structure inputs."""
152153
# Test non-pbc structure with no cell
153154
structure = StructureData(cell=None, pbc=[False, False, False])
154155
structure.append_atom(position=[0.0, 0.0, 0.0], symbols='H')
@@ -174,3 +175,62 @@ def test_trajectory_pbc(self, trajectory_data):
174175

175176
with pytest.raises(ValueError, match='All structures should have the same `pbc`'):
176177
TrajectoryData(structurelist=(structure_periodic, structure_non_periodic))
178+
179+
def test_trajectory_pbc_set_trajectory(self):
180+
"""Test the `pbc` for the `TrajectoryData` using `set_trajectory`."""
181+
data = {
182+
'symbols': ['H'],
183+
'positions': np.array(
184+
[
185+
[
186+
[0.0, 0.0, 0.0],
187+
]
188+
]
189+
),
190+
}
191+
trajectory = TrajectoryData()
192+
193+
data.update(
194+
{
195+
'cells': None,
196+
'pbc': None,
197+
}
198+
)
199+
trajectory.set_trajectory(**data)
200+
assert trajectory.get_step_structure(0).pbc == (False, False, False)
201+
202+
data.update(
203+
{
204+
'cells': None,
205+
'pbc': [False, False, False],
206+
}
207+
)
208+
trajectory.set_trajectory(**data)
209+
assert trajectory.get_step_structure(0).pbc == (False, False, False)
210+
211+
data.update(
212+
{
213+
'cells': None,
214+
'pbc': [True, False, False],
215+
}
216+
)
217+
with pytest.raises(ValueError, match='Periodic boundary conditions are only possible when a cell is defined'):
218+
trajectory.set_trajectory(**data)
219+
220+
data.update(
221+
{
222+
'cells': np.array([[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]]),
223+
'pbc': None,
224+
}
225+
)
226+
with pytest.warns(AiidaDeprecationWarning, match="When 'cells' is not None, the periodic"):
227+
trajectory.set_trajectory(**data)
228+
229+
data.update(
230+
{
231+
'cells': np.array([[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]]),
232+
'pbc': (True, False, False),
233+
}
234+
)
235+
trajectory.set_trajectory(**data)
236+
assert trajectory.get_step_structure(0).pbc == (True, False, False)

0 commit comments

Comments
 (0)