From 17c1411551e7dcc083d2b81188bb68596a1ba0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Robidas?= Date: Mon, 26 Aug 2024 12:18:31 -0400 Subject: [PATCH] Fixed bug where extra lines in input files would mistakingly be counted as extra atoms for the purpose of checking the number of atoms in the file --- ccinput/tests/structures/ethanol_extra_line.xyz | 12 ++++++++++++ ccinput/tests/test_cli.py | 14 ++++++++++++++ ccinput/tests/test_structures.py | 15 +++++++++++++++ ccinput/utilities.py | 9 ++++++++- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 ccinput/tests/structures/ethanol_extra_line.xyz diff --git a/ccinput/tests/structures/ethanol_extra_line.xyz b/ccinput/tests/structures/ethanol_extra_line.xyz new file mode 100644 index 0000000..f49db64 --- /dev/null +++ b/ccinput/tests/structures/ethanol_extra_line.xyz @@ -0,0 +1,12 @@ +9 + +C -1.31970 -0.64380 0.00000 +H -0.96310 -1.65260 0.00000 +H -0.96310 -0.13940 -0.87370 +H -2.38970 -0.64380 0.00000 +C -0.80640 0.08220 1.25740 +H -1.16150 1.09160 1.25640 +H -1.16470 -0.42110 2.13110 +O 0.62360 0.07990 1.25870 +H 0.94410 0.53240 2.04240 + diff --git a/ccinput/tests/test_cli.py b/ccinput/tests/test_cli.py index 4a43497..a4c53a6 100644 --- a/ccinput/tests/test_cli.py +++ b/ccinput/tests/test_cli.py @@ -762,6 +762,20 @@ def test_synonym_basis_set(self): objs, outputs = get_input_from_args(args) + def test_file_extra_line(self): + args = { + "software": "gaussian", + "type": "sp", + "method": "HF", + "basis_set": "Def2SVP", + "file": self.struct("ethanol_extra_line"), + "nproc": 1, + "mem": "1G", + "name": "ethanol", + } + line = f"gaussian sp HF -bs Def2SVP -f {self.struct('ethanol')} -n 1 --mem 1G" + self.assertTrue(self.args_cmd_equivalent(args, line)) + class CliPresetTests(InputTests): def test_create_preset(self): diff --git a/ccinput/tests/test_structures.py b/ccinput/tests/test_structures.py index 936b158..a83a421 100644 --- a/ccinput/tests/test_structures.py +++ b/ccinput/tests/test_structures.py @@ -82,6 +82,21 @@ def test_multiple_atoms_invalid_header2(self): with self.assertRaises(InvalidXYZ): standardize_xyz(xyz) + def test_multiple_atoms_extra_lines(self): + xyz = """3 + header + Cl 0.0 0.0 0.0 + Cl 0.0 0.0 0.0 + Cl 0.0 0.0 0.0 + + + + """ + self.assertEqual( + standardize_xyz(xyz), + "Cl 0.00000000 0.00000000 0.00000000\nCl 0.00000000 0.00000000 0.00000000\nCl 0.00000000 0.00000000 0.00000000\n", + ) + def test_invalid_element(self): xyz = "1\n\nBl 0.0 0.0 0.0\n" with self.assertRaises(InvalidXYZ): diff --git a/ccinput/utilities.py b/ccinput/utilities.py index 977c2e1..2965b71 100644 --- a/ccinput/utilities.py +++ b/ccinput/utilities.py @@ -159,7 +159,14 @@ def parse_xyz_from_file(path): raise InvalidParameter(f"Input file not found: {path}") with open(path) as f: - lines = f.readlines() + _lines = f.readlines() + + lines = _lines[2:] + + if len(_lines) < 3: + raise InvalidXYZ("Invalid XYZ: No atoms specified") + + lines = [i.strip() for i in _lines[2:] if i.strip() != ""] return standardize_xyz(lines)