commit ffa1b366c8db177043bcc018bd5b39b621afaa31
Author: Carles Martí <carles.marti2@gmail.com>
Date:   Fri Nov 27 20:15:28 2020 +0100

    Check for finished calculations is done without assuming output files are named *.out.

diff --git a/modules/calculation.py b/modules/calculation.py
index 503ee0c..34b451e 100644
--- a/modules/calculation.py
+++ b/modules/calculation.py
@@ -15,53 +15,68 @@ def check_finished_calcs(run_type, code):
     """
     from glob import glob
     import ase.io
+    from pycp2k import CP2K
     from modules.utilities import tail
 
     finished_calcs = []
     unfinished_calcs = []
     for conf in sorted(os.listdir(run_type), key=_human_key):
-        if not os.path.isdir(f'{run_type}/{conf}') or 'conf_' not in conf:
+        conf_path = f'{run_type}/{conf}'
+        if not os.path.isdir(conf_path) or 'conf_' not in conf:
             continue
         if code == 'cp2k':
-            out_file_list = glob(f"{run_type}/{conf}/*.out")
-            restart_file_list = glob(f"{run_type}/{conf}/*-1.restart")
-            if len(out_file_list) == 0 or len(restart_file_list) == 0:
+            cp2k = CP2K()
+            restart_file_list = glob(f"{conf_path}/*-1.restart")
+            if len(restart_file_list) == 0:
+                logger.warning(f"No *-1.restart file found on {conf_path}.")
                 unfinished_calcs.append(conf)
-            elif len(out_file_list) > 1 or len(restart_file_list) > 1:
-                warn_msg = f'There is more than one file matching the {code} ' \
-                           f'pattern for finished calculation (*.out / ' \
-                           f'*-1.restart) in {run_type}/{conf}: ' \
-                           f'{out_file_list, restart_file_list}. ' \
-                           f'Skipping directory.'
+                continue
+            elif len(restart_file_list) > 1:
+                warn_msg = f'There is more than one CP2K restart file ' \
+                           f'(*-1.restart / in {conf_path}: ' \
+                           f'{restart_file_list}. Skipping directory.'
+                unfinished_calcs.append(conf)
+                logger.warning(warn_msg)
+                continue
+            out_files = []
+            for file in os.listdir(conf_path):
+                with open(conf_path+"/"+file, "rb") as out_fh:
+                    tail_out_str = tail(out_fh)
+                if tail_out_str.count("PROGRAM STOPPED IN") == 1:
+                    out_files.append(file)
+            if len(out_files) > 1:
+                warn_msg = f'There is more than one CP2K output file in ' \
+                           f'{conf_path}: {out_files}. Skipping directory.'
+                logger.warning(warn_msg)
+                unfinished_calcs.append(conf)
+            elif len(out_files) == 0:
+                warn_msg = f'There is no CP2K output file in {conf_path}. ' \
+                           'Skipping directory.'
                 logger.warning(warn_msg)
                 unfinished_calcs.append(conf)
             else:
-                with open(out_file_list[0], 'rb') as out_fh:
-                    if "PROGRAM STOPPED IN" not in tail(out_fh):
-                        unfinished_calcs.append(conf)
-                    else:
-                        finished_calcs.append(conf)
+                finished_calcs.append(conf)
         elif code == 'vasp':
-            out_file_list = glob(f"{run_type}/{conf}/OUTCAR")
+            out_file_list = glob(f"{conf_path}/OUTCAR")
             if len(out_file_list) == 0:
                 unfinished_calcs.append(conf)
             elif len(out_file_list) > 1:
                 warn_msg = f'There is more than one file matching the {code} ' \
                            f'pattern for finished calculation (*.out / ' \
-                           f'*-1.restart) in {run_type}/{conf}: ' \
+                           f'*-1.restart) in {conf_path}: ' \
                            f'{out_file_list}. Skipping directory.'
                 logger.warning(warn_msg)
                 unfinished_calcs.append(conf)
             else:
                 try:
-                    ase.io.read(f"{run_type}/{conf}/OUTCAR")
+                    ase.io.read(f"{conf_path}/OUTCAR")
                 except ValueError:
                     unfinished_calcs.append(conf)
                     continue
                 except IndexError:
                     unfinished_calcs.append(conf)
                     continue
-                with open(f"{run_type}/{conf}/OUTCAR", 'rb') as out_fh:
+                with open(f"{conf_path}/OUTCAR", 'rb') as out_fh:
                     if "General timing and accounting" not in tail(out_fh):
                         unfinished_calcs.append(conf)
                     else:
