From bbd77b793aaa64b3f9fe9b5a776ee6e0eee01f28 Mon Sep 17 00:00:00 2001 From: manvirsingh01 Date: Fri, 23 Jan 2026 11:13:53 +0530 Subject: [PATCH 1/2] Fix #57: Add energy validation for ORCA output files - Added validation in get_dict_energy() to check if molecule.energy is None - Raises clear ValueError with guidance for users using ORCA files (.wfn, .mkl) - Added test case for the new validation - ORCA files don't contain total energy, so users are directed to use .fchk files --- chemtools/toolbox/test/test_utils.py | 9 +++++++++ chemtools/toolbox/utils.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/chemtools/toolbox/test/test_utils.py b/chemtools/toolbox/test/test_utils.py index 0440c10a..f45b81be 100644 --- a/chemtools/toolbox/test/test_utils.py +++ b/chemtools/toolbox/test/test_utils.py @@ -110,6 +110,15 @@ def test_get_dict_energy_raises(): assert_raises(ValueError, get_dict_energy, molecule) +def test_get_dict_energy_none_energy_raises(): + """Test that get_dict_energy raises ValueError when energy is None.""" + with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fname: + molecule = Molecule.from_file(fname) + # Simulate a molecule without energy (like ORCA files) + molecule._iodata.energy = None + assert_raises(ValueError, get_dict_energy, molecule) + + def test_get_dict_density_raises(): # check molecule with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as file1: diff --git a/chemtools/toolbox/utils.py b/chemtools/toolbox/utils.py index 9b3564d5..78e880ff 100644 --- a/chemtools/toolbox/utils.py +++ b/chemtools/toolbox/utils.py @@ -185,6 +185,14 @@ def get_dict_energy(molecule): # get homo/lumo energy homo_e, lumo_e, _, _ = get_homo_lumo_data(molecule) nelec = sum(molecule.mo.nelectrons) + # validate that energy is available + if molecule.energy is None: + raise ValueError( + "Molecule does not contain total energy information. " + "This is common for ORCA output files (*.wfn, *.mkl). " + "Please use a file format that includes energy (e.g., *.fchk) " + "or provide multiple molecule files with explicit energy values." + ) # store number of electron and energy in a dictionary energies = {nelec: molecule.energy, nelec + 1: molecule.energy + lumo_e, @@ -197,6 +205,12 @@ def get_dict_energy(molecule): nelec = sum(mol.mo.nelectrons) if nelec in list(energies.keys()): raise ValueError("Two molecules have {0} electrons!".format(nelec)) + # validate that energy is available + if mol.energy is None: + raise ValueError( + "Molecule with {0} electrons does not contain total energy. " + "This is common for ORCA output files (*.wfn, *.mkl).".format(nelec) + ) # store number of electrons and energy in a dictionary energies[nelec] = mol.energy else: From de614c537368e1ab96b358120b6426193bbd07eb Mon Sep 17 00:00:00 2001 From: Manvir singh <157945730+manvirsingh01@users.noreply.github.com> Date: Mon, 16 Feb 2026 16:02:51 +0530 Subject: [PATCH 2/2] Update chemtools/toolbox/utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- chemtools/toolbox/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/chemtools/toolbox/utils.py b/chemtools/toolbox/utils.py index 78e880ff..38a12444 100644 --- a/chemtools/toolbox/utils.py +++ b/chemtools/toolbox/utils.py @@ -208,8 +208,12 @@ def get_dict_energy(molecule): # validate that energy is available if mol.energy is None: raise ValueError( - "Molecule with {0} electrons does not contain total energy. " - "This is common for ORCA output files (*.wfn, *.mkl).".format(nelec) + ( + "Molecule with {0} electrons does not contain total energy. " + "This is common for ORCA output files (*.wfn, *.mkl). " + "Please use a file format that includes energy (e.g., *.fchk) " + "or ensure each Molecule/IOData instance has its energy set." + ).format(nelec) ) # store number of electrons and energy in a dictionary energies[nelec] = mol.energy