Skip to content

Commit

Permalink
Raise an error when faced with ambiguous CASTEP .cell
Browse files Browse the repository at this point in the history
CASTEP and Pymatgen orientation are likely to be different from the
same lattice vectors. To avoid building incorrect structures, refuse
to work with lattice_abc and positions_abs simultaneously.
  • Loading branch information
ajjackson committed Nov 6, 2024
1 parent f5848b5 commit d66c958
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 25 deletions.
8 changes: 5 additions & 3 deletions sumo/io/castep.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ def structure(self):
return Structure(lattice, elements, coords, coords_are_cartesian=False)
elif "positions_abs" in self.blocks:
if "lattice_abc" in self.blocks:
logger.warning(
"Positions are given in absolute coordinates and lattice "
"in angles/lengths format. Structure may not be consistent."
raise ValueError(
"Positions are given in Cartesian coordinates and lattice "
"in angles/lengths format. This structure cannot be "
"interpreted reliably. Consider using c2x to convert to "
"fractional coordinates and/or 3x3 lattice matrix."
)

positions_abs = self.blocks["positions_abs"].values
Expand Down
10 changes: 5 additions & 5 deletions tests/data/NiO/NiO_abc.cell
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
106.80215981 73.19772637 119.99807072
%ENDBLOCK LATTICE_ABC

%BLOCK POSITIONS_ABS
%BLOCK POSITIONS_FRAC
Ni 0.000000 0.000000 0.000000 SPIN=1
Ni 1.491603 0.861143 2.432002 SPIN=-1
O 0.000069 1.722313 1.215967 SPIN=0
O 2.983138 -0.000026 3.648036 SPIN=0
%ENDBLOCK POSITIONS_ABS
Ni 0.500000 0.500000 0.500000 SPIN=-1
O 0.250001 0.749999 0.249993 SPIN=0
O 0.749999 0.250001 0.750007 SPIN=0
%ENDBLOCK POSITIONS_FRAC

%BLOCK SPECIES_POT
Ni C19
Expand Down
20 changes: 20 additions & 0 deletions tests/data/NiO/NiO_abc_cart.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%BLOCK LATTICE_ABC
2.983077 2.98308198 5.15994087
106.80215981 73.19772637 119.99807072
%ENDBLOCK LATTICE_ABC

%BLOCK POSITIONS_ABS
Ni 0.000000 0.000000 0.000000 SPIN=1
Ni 1.491603 0.861143 2.432002 SPIN=-1
O 0.000069 1.722313 1.215967 SPIN=0
O 2.983138 -0.000026 3.648036 SPIN=0
%ENDBLOCK POSITIONS_ABS

%BLOCK SPECIES_POT
Ni C19
O C19
%ENDBLOCK SPECIES_POT

KPOINTS_MP_GRID 7 7 7
SPECTRAL_KPOINTS_MP_GRID 2 2 2

22 changes: 22 additions & 0 deletions tests/data/NiO/NiO_abc_frac.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# written by ASE

%BLOCK LATTICE_CART
2.983077 0.000000 0.000000
-1.491454 2.583475 0.000000
1.491583 -0.861188 4.864003
%ENDBLOCK LATTICE_CART

%BLOCK POSITIONS_FRAC
Ni 0.000000 0.000000 0.000000
Ni 0.500000 0.500000 0.500000
O 0.250001 0.749999 0.249993
O 0.749999 0.250001 0.750007
%ENDBLOCK POSITIONS_FRAC

%BLOCK SPECIES_POT
Ni C19
O C19
%ENDBLOCK SPECIES_POT

KPOINTS_MP_GRID: 7 7 7
SPECTRAL_KPOINTS_MP_GRID: 2 2 2
12 changes: 5 additions & 7 deletions tests/data/NiO/NiO_abc_units.cell
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ Ang
106.80215981 73.19772637 119.99807072
%ENDBLOCK LATTICE_ABC

%BLOCK POSITIONS_ABS
Ang
%BLOCK POSITIONS_FRAC
Ni 0.000000 0.000000 0.000000 SPIN=1
Ni 1.491603 0.861143 2.432002 SPIN=-1
O 0.000069 1.722313 1.215967 SPIN=0
O 2.983138 -0.000026 3.648036 SPIN=0
%ENDBLOCK POSITIONS_ABS
Ni 0.500000 0.500000 0.500000 SPIN=-1
O 0.250001 0.749999 0.249993 SPIN=0
O 0.749999 0.250001 0.750007 SPIN=0
%ENDBLOCK POSITIONS_FRAC

%BLOCK SPECIES_POT
Ni C19
Expand All @@ -19,4 +18,3 @@ O C19

KPOINTS_MP_GRID 7 7 7
SPECTRAL_KPOINTS_MP_GRID 2 2 2

40 changes: 30 additions & 10 deletions tests/tests_io/test_castep.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ def setUp(self):
self.zns_singlepoint_cell = os.path.join(
ilr_files("tests"), "data", "ZnS", "zns-sp.cell"
)
self.nio_cart_cell = os.path.join(
ilr_files("tests"), "data", "NiO", "NiO.cell"
)
self.nio_abc_cell = os.path.join(
ilr_files("tests"), "data", "NiO", "NiO_abc.cell"
)
self.nio_abc_units_cell = os.path.join(
ilr_files("tests"), "data", "NiO", "NiO_abc_units.cell"
)
self.nio_abc_cart_cell = os.path.join(
ilr_files("tests"), "data", "NiO", "NiO_abc_cart.cell"
)


def test_castep_cell_null_init(self):
null_cell = CastepCell()
Expand Down Expand Up @@ -82,20 +89,33 @@ def test_castep_cell_from_singlepoint_file(self):
def test_castep_cell_abc(self):
"""Test .cell file using lattice_abc to define unit cell"""
for filename in self.nio_abc_cell, self.nio_abc_units_cell:
with self.assertLogs(logger) as log:
cc = CastepCell.from_file(filename)
structure = cc.structure
self.assertIn("Structure may not be consistent.", log.output[-1])
cc = CastepCell.from_file(filename)
structure = cc.structure

assert_array_almost_equal(
structure.lattice.matrix,
[
[2.855724, 0.0, 0.862317],
[-1.297582, 2.54391, -0.862313],
[0.0, 0.0, 5.159941],
],
structure.lattice.abc,
[2.983077, 2.98308198, 5.15994087]
)
assert_array_almost_equal(
structure.lattice.angles,
[106.80215981, 73.19772637, 119.99807072]
)

def test_castep_cell_consistent_frac_coords(self):
"""Test fractional positions are consistent between .cell formats"""
cartesian_cell = CastepCell.from_file(self.nio_cart_cell)
frac_abc_cell = CastepCell.from_file(self.nio_abc_cell)

assert_array_almost_equal(
cartesian_cell.structure.frac_coords,
frac_abc_cell.structure.frac_coords
)

def test_castep_abc_cart_raises(self):
"""Test that error is raised for incompatible vectors and positions"""
with self.assertRaisesRegex(ValueError, "Cartesian"):
CastepCell.from_file(self.nio_abc_cart_cell).structure

def test_castep_cell_from_structure(self):
cell = CastepCell.from_structure(self.si_structure)
self.assertEqual(
Expand Down

0 comments on commit d66c958

Please sign in to comment.