-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_geometry.py
42 lines (35 loc) · 1.52 KB
/
optimize_geometry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
import os
def determine_charge_multiplicity(state):
if state == "anion":
charge = -1
multiplicity = 1
elif state == "cation":
charge = 1
multiplicity = 1
else:
charge = 0
multiplicity = 2
return charge, multiplicity
def optimize_geometry(input_xyz_file, output_xyz_file, charge, multiplicity, log_file):
with open(log_file, 'w') as log:
subprocess.run(['xtb', input_xyz_file, '--opt', '--gfn2', '--chrg', str(charge), '--uhf', str(multiplicity-1), '--vtight'], check=True, stdout=log, stderr=log)
subprocess.run(['cp', 'xtbopt.mol', output_xyz_file], check=True)
return output_xyz_file
def optimize_for_states(input_xyz_file):
states = ["radical", "anion", "cation"]
output_files = {}
for state in states:
charge, multiplicity = determine_charge_multiplicity(state)
output_xyz_file = f"opt_{state}_{os.path.splitext(input_xyz_file)[0]}.mol"
log_file = f"{state}_optimization.log"
optimize_geometry(input_xyz_file, output_xyz_file, charge, multiplicity, log_file)
output_files[state] = output_xyz_file
print(f"Optimized geometry for {state} saved in: {output_xyz_file}")
return output_files
if __name__ == "__main__":
input_xyz_file = "stacked_dimer_Blatter1.mol"
optimized_files = optimize_for_states(input_xyz_file)
print("\n--- Optimized Files ---")
for state, file in optimized_files.items():
print(f"{state.capitalize()} optimized file: {file}")