From 254a17f25cf8723f19f3dec87b205d6e7a92ee7b Mon Sep 17 00:00:00 2001 From: Danfeng Zhao <154488229+DanielZhao0432@users.noreply.github.com> Date: Wed, 15 Jul 2026 13:28:29 +0800 Subject: [PATCH] fix: resolve undefined variable and early termination in aveElecStatPot.py Refactored input file handling to check for 'ElecStaticPot.cube' in subdirectories. Added error handling for missing input files. --- .../average_pot/aveElecStatPot.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tools/02_postprocessing/average_pot/aveElecStatPot.py b/tools/02_postprocessing/average_pot/aveElecStatPot.py index 60bc1804ac4..e370ba2d93a 100644 --- a/tools/02_postprocessing/average_pot/aveElecStatPot.py +++ b/tools/02_postprocessing/average_pot/aveElecStatPot.py @@ -4,6 +4,10 @@ import matplotlib.pyplot as plt from scipy.interpolate import interp1d +input_file = None +output_file = None +output_png = None + current_dir = os.getcwd() if os.path.exists(os.path.join(current_dir, "ElecStaticPot.cube")): @@ -15,16 +19,20 @@ dir_path = os.path.join(current_dir, dir_name) if os.path.isdir(dir_path) and dir_name.startswith("OUT."): - input_file = os.path.join(dir_path, "ElecStaticPot.cube") - output_file = os.path.join(dir_path, "ElecStaticPot_AVE") - output_png = os.path.join(dir_path, "ElecStaticPot-vs-Z.png") - if os.path.exists(input_file): + candidate_file = os.path.join(dir_path, "ElecStaticPot.cube") + if os.path.exists(candidate_file): + input_file = candidate_file + output_file = os.path.join(dir_path, "ElecStaticPot_AVE") + output_png = os.path.join(dir_path, "ElecStaticPot-vs-Z.png") print(f"Processeding: {input_file}") break - else: - print(f"File does not exist: {input_file}") - sys.exit() - + +if input_file is None: + print("Error: 'ElecStaticPot.cube' was not found in the current directory or any 'OUT.*' subdirectories.", file=sys.stderr) + print("Please check if ABACUS has completed successfully with 'out_pot' enabled (e.g., set to 2).", file=sys.stderr) + sys.exit(1) + + with open(input_file, 'r') as inpt: temp = inpt.readlines()