From 77d949e68352ce890c104b879db26721f4f7b024 Mon Sep 17 00:00:00 2001 From: Duc Le Date: Tue, 16 Apr 2024 15:14:50 +0100 Subject: [PATCH 1/4] Add error for old castep_bin files #295 --- euphonic/readers/castep.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/euphonic/readers/castep.py b/euphonic/readers/castep.py index 65455a0d9..9661db5a5 100644 --- a/euphonic/readers/castep.py +++ b/euphonic/readers/castep.py @@ -469,8 +469,17 @@ 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)) + try: + cell_origins = np.reshape( + _read_entry(f, int_type), (n_cells_in_sc, 3)) + except ValueError: + 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: " keyword and ' + 'use the new output .castep_bin file in Euphonic.') _ = _read_entry(f, int_type) # FC row not used elif header == b'BORN_CHGS': born = np.reshape( From 2bd1cbfd13791255d657c96e936b0d909752501a Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Wed, 14 Feb 2024 13:39:05 +0000 Subject: [PATCH 2/4] Make error check slightly safer for old Castep format "except ValueError" is a bit broad and could catch unrelated problems. Checking that _read_entry returned an int is still not 100% specific to the problem but seems safer. --- euphonic/readers/castep.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/euphonic/readers/castep.py b/euphonic/readers/castep.py index 9661db5a5..0ab196590 100644 --- a/euphonic/readers/castep.py +++ b/euphonic/readers/castep.py @@ -469,10 +469,9 @@ 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])) - try: - cell_origins = np.reshape( - _read_entry(f, int_type), (n_cells_in_sc, 3)) - except ValueError: + + cell_origins = _read_entry(f, int_type) + if isinstance(cell_origins, int): raise ValueError('Old castep file detected: ' 'Euphonic only supports post-Castep 17.1 files. ' 'Please rerun the calculation with a newer version ' @@ -480,6 +479,9 @@ def read_interpolation_data( '.castep file with a single line with the ' '"continuation: " keyword and ' 'use the new output .castep_bin file in Euphonic.') + + cell_origins = np.reshape(cell_origins, (n_cells_in_sc, 3)) + _ = _read_entry(f, int_type) # FC row not used elif header == b'BORN_CHGS': born = np.reshape( From bc54df3975f21159757713e0e5c2d9c533f54dbd Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Wed, 14 Feb 2024 13:39:05 +0000 Subject: [PATCH 3/4] Explicitly check CASTEP version and error out if too low --- euphonic/readers/castep.py | 25 ++++++++++++++----------- setup.py | 1 + 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/euphonic/readers/castep.py b/euphonic/readers/castep.py index 0ab196590..c7f373dbb 100644 --- a/euphonic/readers/castep.py +++ b/euphonic/readers/castep.py @@ -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, @@ -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: " 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 @@ -470,17 +482,8 @@ def read_interpolation_data( (n_cells_in_sc, 3*n_atoms, 3*n_atoms)), axes=[0, 2, 1])) - cell_origins = _read_entry(f, int_type) - if isinstance(cell_origins, int): - 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: " keyword and ' - 'use the new output .castep_bin file in Euphonic.') - - cell_origins = np.reshape(cell_origins, (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': diff --git a/setup.py b/setup.py index be128a9a1..44e0ebabd 100644 --- a/setup.py +++ b/setup.py @@ -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', From f48f50723ca9b6ef99ced8a27b82a3bec69367ea Mon Sep 17 00:00:00 2001 From: "Adam J. Jackson" Date: Wed, 14 Feb 2024 13:39:05 +0000 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f08a31e5e..ad44dfd77 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,10 +1,21 @@ `Unreleased `_ +- 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