Skip to content

Commit 10aaa86

Browse files
committed
basissets: give more details when encountering invalid data
1 parent 33b876c commit 10aaa86

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

cp2k_input_tools/basissets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def from_lines(cls, lines: Sequence[str]) -> "BasisSetData":
5555
blocks = []
5656

5757
# go through all blocks containing different sets of orbitals
58-
for _ in range(n_blocks):
58+
for nblock in range(n_blocks):
5959
# get the quantum numbers for this set, formatted as follows:
6060
# n lmin lmax nexp nshell(lmin) nshell(lmin+1) ... nshell(lmax-1) nshell(lmax)
6161
# ignore everything after nshell(lmax) on the same line (as CP2K does)
@@ -65,11 +65,16 @@ def from_lines(cls, lines: Sequence[str]) -> "BasisSetData":
6565

6666
nline += 1
6767

68+
try:
69+
coefficients = [[Decimal(c) for c in lines[nline + n].split()] for n in range(nexp)]
70+
except IndexError:
71+
raise ValueError(f"Not enough exponents found. Expected {nexp} lines for block {nblock+1}") from None
72+
6873
blocks.append(
6974
BasisSetCoefficients(
7075
n=qn_n,
7176
l=[(lqn, nl) for lqn, nl in zip(range(qn_lmin, qn_lmax + 1), ncoeffs)],
72-
coefficients=[[Decimal(c) for c in lines[nline + n].split()] for n in range(nexp)],
77+
coefficients=coefficients,
7378
)
7479
)
7580

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Rn DZVP-MOLOPT-PBE0-GTH-q8 DZVP-MOLOPT-HYB-GTH-q8
2+
1
3+
2 0 2 5 2 2 1
4+
1.94680473836657 -3.51470729572815E-01 3.41916147676705E-01 -3.07949649145486E-01 2.20431054165274E-01 -1.88427468191243E-01
5+
1.47059662428402 6.95335830053440E-01 -2.14650643145304E-01 7.10220061130612E-02 5.27069605360106E-01 2.62362469873797E-01
6+
0.38413051754849 -5.18308448381857E-01 -3.67640582468798E-01 -6.43697808158820E-01 -6.49462765780797E-02 -1.46737773851035E-01
7+
0.17491279814480 -3.48697531070589E-01 -3.41126711300677E-01 -3.50692597173507E-01 -6.98709515314249E-01 -9.31711221210688E-01

tests/test_basisset.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from cp2k_input_tools.basissets import BasisSetData
24

35
from . import TEST_DIR
@@ -7,12 +9,22 @@
79

810
def test_single_basisset_import():
911
with (TEST_DIR / "inputs" / "BASIS_MOLOPT.H").open() as fhandle:
10-
bset = BasisSetData.from_lines([line for line in fhandle])
12+
lines = [line for line in fhandle]
13+
14+
bset = BasisSetData.from_lines(lines)
1115

1216
assert bset.element == "H"
1317
assert bset.n_el == 1
1418

1519

20+
def test_single_basisset_import_invalid_nexp():
21+
with (TEST_DIR / "inputs" / "BASIS_MOLOPT.invalid_Rn").open() as fhandle:
22+
lines = [line for line in fhandle]
23+
24+
with pytest.raises(ValueError):
25+
BasisSetData.from_lines(lines)
26+
27+
1628
def test_single_basisset_roundtrip():
1729
with (TEST_DIR / "inputs" / "BASIS_MOLOPT.H").open() as fhandle:
1830
lines = [line.rstrip() for line in fhandle]

0 commit comments

Comments
 (0)