-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine NEBs into a single trajectory
- Loading branch information
1 parent
3651ee5
commit 06bac09
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
import re | ||
|
||
def get_xyz_files(): | ||
# List all files in the current working directory | ||
files = os.listdir('.') | ||
# Filter files to get only those with a single number in their name ending with .xyz | ||
xyz_files = [f for f in files if re.match(r'^\d+\.xyz$', f)] | ||
# Sort files based on the numerical value in their name | ||
xyz_files.sort(key=lambda x: int(re.match(r'(\d+)', x).group())) | ||
return xyz_files | ||
|
||
def read_xyz_frames(file): | ||
with open(file, 'r') as f: | ||
lines = f.readlines() | ||
frames = [] | ||
i = 0 | ||
while i < len(lines): | ||
num_atoms = int(lines[i].strip()) | ||
header = lines[i + 1].strip() | ||
frame = lines[i:i + num_atoms + 2] | ||
frames.append(frame) | ||
i += num_atoms + 2 | ||
return frames | ||
|
||
def combine_xyz_files(xyz_files, output_file): | ||
combined_frames = [] | ||
|
||
for i, file in enumerate(xyz_files): | ||
frames = read_xyz_frames(file) | ||
if i < len(xyz_files) - 1: | ||
combined_frames.extend(frames[:-1]) # Exclude the last frame of the file | ||
else: | ||
combined_frames.extend(frames) # Include all frames of the last file | ||
|
||
with open(output_file, 'w') as f: | ||
for frame in combined_frames: | ||
f.writelines(frame) | ||
|
||
def get_combined_trajectory(): | ||
xyz_files = get_xyz_files() | ||
if xyz_files: | ||
combine_xyz_files(xyz_files, 'combined.xyz') | ||
print("Combined file 'combined.xyz' created successfully.") | ||
else: | ||
print("No suitable .xyz files found.") |