Skip to content

Commit a2a45f8

Browse files
fix: resolve undefined variable and early termination in aveElecStatPot.py (#7640)
Refactored input file handling to check for 'ElecStaticPot.cube' in subdirectories. Added error handling for missing input files.
1 parent 12fd38a commit a2a45f8

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

tools/02_postprocessing/average_pot/aveElecStatPot.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import matplotlib.pyplot as plt
55
from scipy.interpolate import interp1d
66

7+
input_file = None
8+
output_file = None
9+
output_png = None
10+
711
current_dir = os.getcwd()
812

913
if os.path.exists(os.path.join(current_dir, "ElecStaticPot.cube")):
@@ -15,16 +19,20 @@
1519
dir_path = os.path.join(current_dir, dir_name)
1620

1721
if os.path.isdir(dir_path) and dir_name.startswith("OUT."):
18-
input_file = os.path.join(dir_path, "ElecStaticPot.cube")
19-
output_file = os.path.join(dir_path, "ElecStaticPot_AVE")
20-
output_png = os.path.join(dir_path, "ElecStaticPot-vs-Z.png")
21-
if os.path.exists(input_file):
22+
candidate_file = os.path.join(dir_path, "ElecStaticPot.cube")
23+
if os.path.exists(candidate_file):
24+
input_file = candidate_file
25+
output_file = os.path.join(dir_path, "ElecStaticPot_AVE")
26+
output_png = os.path.join(dir_path, "ElecStaticPot-vs-Z.png")
2227
print(f"Processeding: {input_file}")
2328
break
24-
else:
25-
print(f"File does not exist: {input_file}")
26-
sys.exit()
27-
29+
30+
if input_file is None:
31+
print("Error: 'ElecStaticPot.cube' was not found in the current directory or any 'OUT.*' subdirectories.", file=sys.stderr)
32+
print("Please check if ABACUS has completed successfully with 'out_pot' enabled (e.g., set to 2).", file=sys.stderr)
33+
sys.exit(1)
34+
35+
2836
with open(input_file, 'r') as inpt:
2937
temp = inpt.readlines()
3038

0 commit comments

Comments
 (0)