Skip to content

Commit 2544d97

Browse files
authored
Merge pull request #5 from pmrv/claude/numpy-24-compat
Support numpy >= 2.4 with backward-compatible fixes
2 parents 46db657 + ace21f4 commit 2544d97

4 files changed

Lines changed: 18 additions & 13 deletions

File tree

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ def build_extension(self, ext: CMakeExtension) -> None:
191191
build_ext=CMakeBuild)),
192192
zip_safe=False,
193193
url='https://github.com/ICAMS/python-ace',
194-
# numpy 2.4 removes np.trapz and makes np.float64(array) return an array
195-
# instead of a scalar, which breaks PyACECalculator energy output
196-
install_requires=['numpy<2.4',
194+
install_requires=['numpy<3',
197195
'ase',
198196
'pandas>=2,<4',
199197
'ruamel.yaml',

src/pyace/asecalc.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def calculate(self, atoms=None, properties=('energy', 'forces', 'stress', 'energ
164164
self.energies = np.array(self.ace.energies)
165165

166166
self.results = {
167-
'energy': np.float64(self.energy.reshape(-1, )),
168-
'free_energy': np.float64(self.energy.reshape(-1, )),
167+
'energy': float(self.energy),
168+
'free_energy': float(self.energy),
169169
'forces': self.forces.astype(np.float64),
170170
'energies': self.energies.astype(np.float64),
171171
'gamma': np.array(self.ace.gamma_grade, dtype=np.float64)
@@ -305,21 +305,21 @@ def calculate(self, atoms=None, properties=('energy', 'forces', 'stress', 'energ
305305

306306
self.results = {
307307
# mean
308-
'energy': np.float64(self.energy.reshape(-1, )),
309-
'free_energy': np.float64(self.energy.reshape(-1, )),
308+
'energy': float(self.energy),
309+
'free_energy': float(self.energy),
310310
'forces': self.forces.astype(np.float64),
311311
'energies': self.energies.astype(np.float64),
312312

313313
# std
314-
'energy_std': np.float64(self.energy_std.reshape(-1, )),
315-
'free_energy_std': np.float64(self.energy_std.reshape(-1, )),
314+
'energy_std': float(self.energy_std),
315+
'free_energy_std': float(self.energy_std),
316316
'forces_std': self.forces_std.astype(np.float64),
317317
'energies_std': self.energies_std.astype(np.float64),
318318

319319
# dev
320-
'energy_dev': np.float64(self.energy_dev),
321-
'energies_dev': np.float64(self.energies_dev),
322-
'forces_dev': np.float64(self.forces_dev)
320+
'energy_dev': float(self.energy_dev),
321+
'energies_dev': self.energies_dev.astype(np.float64),
322+
'forces_dev': self.forces_dev.astype(np.float64)
323323
}
324324

325325
if self.atoms.number_of_lattice_vectors == 3:

src/pyace/radial.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import numpy as np
77
from typing import Union
88

9+
# np.trapz was renamed to np.trapezoid in numpy 2.0 and removed in numpy 2.4
10+
_trapezoid = np.trapezoid if hasattr(np, "trapezoid") else np.trapz
11+
912
from pyace import ACECTildeBasisSet, ACEBBasisSet, BBasisConfiguration
1013

1114

@@ -17,7 +20,7 @@ def integrate(xs, table):
1720
frs = np.abs(table)
1821
sum_frs = np.sum(frs, axis=(1, 2))
1922
integrand = sum_frs * xs ** 2
20-
integral = np.trapz(integrand, x=xs)
23+
integral = _trapezoid(integrand, x=xs)
2124
return integral
2225
else:
2326
return 0

tests/test_PyACECalculator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def test_setup():
4848
f1 = a.get_forces()
4949
print(e1)
5050
print(f1)
51+
# regression check for numpy >= 2.4, where the energy leaked out as a
52+
# 1-element array instead of a scalar
53+
assert isinstance(e1, float)
54+
assert f1.shape == (2, 3)
5155

5256

5357
def test_load_YAML():

0 commit comments

Comments
 (0)