Skip to content

Commit

Permalink
Merge pull request #286 from TheoChem-VU/276-exception-raised-while-t…
Browse files Browse the repository at this point in the history
…rying-to-read-charge-from-orca-calculations

Fixed an issue where reading ORCA calculations would crash the program
  • Loading branch information
SEBeutick authored Jul 18, 2024
2 parents 96d99e4 + 4917494 commit 7b6a65e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
9 changes: 3 additions & 6 deletions src/tcutility/results/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,13 @@ def read(calc_dir: Union[str, pl.Path]) -> Result:
elif ret.engine == "orca":
try:
ret.orca = orca.get_calc_settings(ret)
except:
except: # noqa
ret.orca = None
print('Error reading:', calc_dir)
raise

try:
ret.properties = orca.get_properties(ret)
except:
except: # noqa
ret.properties = None
print('Error reading:', calc_dir)
raise

# unload cached KFReaders associated with this calc_dir
to_delete = [key for key in cache._cache if key.startswith(os.path.abspath(calc_dir))]
Expand Down
53 changes: 32 additions & 21 deletions src/tcutility/results/orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def get_calc_files(calc_dir: str) -> Result:
"""Function that returns files relevant to AMS calculations stored in ``calc_dir``.
"""Function that returns files relevant to ORCA calculations stored in ``calc_dir``.
Args:
calc_dir: path pointing to the desired calculation
Expand All @@ -22,18 +22,22 @@ def get_calc_files(calc_dir: str) -> Result:
for root, _, files_ in os.walk(calc_dir):
files.extend([j(root, file) for file in files_])

# parse the filenames
# we now go through all the files and check their nature
ret = Result()
ret.root = os.path.abspath(calc_dir)
for file in files:
try:
with open(file) as f:
lines = f.readlines()

with open(file, errors='ignore') as f:
lines = f.readlines()
# detect the output file
if any(["* O R C A *" in line for line in lines]):
ret.out = os.path.abspath(file)
except: # noqa
pass
continue

# detect the input file
# there should be lines starting with ! and also the system line, starting with * xyz, * xyzfile, * gzmtfile or * int
if any([line.startswith('!') for line in lines]) and any([(len(line.split()) > 2 and line.split()[0] == '*' and line.split()[1] in ['xyz', 'xyzfile', 'gzmtfile', 'int']) for line in lines]):
ret.inp = os.path.abspath(file)
continue

return ret

Expand Down Expand Up @@ -81,22 +85,29 @@ def get_input(info: Result) -> Result:
- **task (str)** - the task that was performed by the calculation, e.g. "SinglePoint", "TransitionStateSearch".
"""
ret = Result()
with open(info.files.out) as out:
start_reading = False
lines = []
for line in out.readlines():
line = line.strip()
if start_reading:
lines.append(line)

if "INPUT FILE" in line:
start_reading = True
continue
# we read the input file first
if 'inp' in info.files:
with open(info.files.inp) as inp:
lines = inp.readlines()
# if we don't have it we read the output file, which contains the input as a block
else:
with open(info.files.out) as out:
start_reading = False
lines = []
for line in out.readlines():
line = line.strip()
if start_reading:
lines.append(line)

if "****END OF INPUT****" in line:
break
if "INPUT FILE" in line:
start_reading = True
continue

if "****END OF INPUT****" in line:
break

lines = [line.split(">")[1] for line in lines[2:-1] if line.split(">")[1].strip()]
lines = [line.split(">")[1] for line in lines[2:-1] if line.split(">")[1].strip()]

ret.main = []
curr_section = None
Expand Down

0 comments on commit 7b6a65e

Please sign in to comment.