Skip to content

Commit

Permalink
Add error for old castep_bin files (#296)
Browse files Browse the repository at this point in the history
* Add error for old castep_bin files #295

.castep_bin files before CASTEP 17.1 do not include the required cell offset information. In theory we can calculate this, but not a priority as users can also update their .castep_bin externally with a newer version of CASTEP.

Explicitly check the CASTEP version number provided in .castep_bin file, and give an informative error if it is too low. This uses `packaging` to compare version numbers, so this is added to setup.py dependencies.

* Update CHANGELOG

---------

Co-authored-by: Adam J. Jackson <[email protected]>
  • Loading branch information
mducle and ajjackson authored Apr 19, 2024
1 parent 2ba0302 commit be51d81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
`Unreleased <https://github.com/pace-neutrons/Euphonic/compare/v1.3.1...HEAD>`_

- Requirements

- ``packaging`` library added to dependencies.

- Bug fixes

- Fixed an error loading QpointPhononModes from JSON when there is a
single q-point in the data

- Improvements

- When loading ``.castep_bin`` files, explicitly check the CASTEP
version number and give a useful error message if this is < 17.1.
(These files are missing information about the unit cell origins,
and would previously cause an error with an unhelpful message.)

- Maintenance

- Compatibility fix for spglib 2.4 update: a new sanity-check in
Expand Down
18 changes: 16 additions & 2 deletions euphonic/readers/castep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from packaging.version import Version
import re
import struct
from typing import (Any, BinaryIO, Dict, Iterator, List, NamedTuple,
Expand Down Expand Up @@ -451,6 +452,17 @@ def read_interpolation_data(
float_type = '>f8'
first_cell_read = True
while (header := _read_entry(f).strip()) != b'END':
if header == b'BEGIN_PARAMETERS_DUMP':
castep_version: Version = Version(_read_entry(f).decode().strip())
if castep_version < Version("17.1"):
raise ValueError('Old castep file detected: '
'Euphonic only supports post-Castep 17.1 files. '
'Please rerun the calculation with a newer version '
'of Castep with the original .cell file and a '
'.castep file with a single line with the '
'"continuation: <old.castep_bin>" keyword and '
'use the new output .castep_bin file in Euphonic.')

if header == b'BEGIN_UNIT_CELL':
# CASTEP writes the cell twice: the first is the
# geometry optimised cell, the second is the original
Expand All @@ -469,8 +481,10 @@ def read_interpolation_data(
np.reshape(_read_entry(f, float_type),
(n_cells_in_sc, 3*n_atoms, 3*n_atoms)),
axes=[0, 2, 1]))
cell_origins = np.reshape(
_read_entry(f, int_type), (n_cells_in_sc, 3))

cell_origins = np.reshape(_read_entry(f, int_type),
(n_cells_in_sc, 3))

_ = _read_entry(f, int_type) # FC row not used
elif header == b'BORN_CHGS':
born = np.reshape(
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def run_setup():
packages=packages,
include_package_data=True,
install_requires=[
'packaging',
'scipy>=1.10', # requires numpy >= 1.19.5
'seekpath>=1.1.0',
'spglib>=1.9.4',
Expand Down

0 comments on commit be51d81

Please sign in to comment.