Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to pass vectors directly as second file #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ A Vesta file that contains vectors connecting the initial and final atomic posit
type: string
default: initial.vesta final.vesta

-cd --calculate_displacements
description: If 1, displacement between positions in Vesta files is calculated. Else, second file is assumed to contain vectors to render.
type: int
default: 1

-r --radius
description: vector radius (in angstrom)
type: float
Expand Down
66 changes: 40 additions & 26 deletions vesta_vectors.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@
import numpy as np
import argparse
import re
from copy import deepcopy
import xyz
import os

def read_in(settings):

combined_positions = []
combined_data = []

for filename in [settings.filenames[0], settings.filenames[1]]:
data = open(filename,'r').read()
combined_data.append(data)

struct_match=re.search(r"STRUC.*THERI",data,flags=re.S)[0]
pos_match=re.findall(r"\d+\D+\s+\D+\d+\s+\d+\.\d+\s+(.*?)\s+\d+\D+\s+\d+",struct_match)

if pos_match:
positions = [[float(y) for y in x.split()] for x in pos_match]
else:
raise ValueError("Invalid file format (I am only able to parse Vesta files)")


for i,filename in enumerate(settings.filenames):
if i==0 or os.path.splitext(filename)[1]=='.vesta':
data = open(filename,'r').read()
combined_data.append(data)

struct_match=re.search(r"STRUC.*THERI",data,flags=re.S).group(0)
pos_match=re.findall(r"\d+\D+\s+\D+\d+\s+\d+\.\d+\s+(.*?)\s+\d+\D+\s+\d+",struct_match)

if pos_match:
positions = [[float(y) for y in x.split()] for x in pos_match]
else:
raise ValueError("Invalid Vesta file!")
elif os.path.splitext(filename)[1]=='.xyz':
positions,ignore=xyz.read(filename)
positions=positions.tolist()
combined_positions.append(positions)

data = {"initial_data": combined_data[0],
"final_data": combined_data[1],
"initial_positions": combined_positions[0], # positions in fractional coordinates
"final_positions": combined_positions[1], # positions in fractional coordinates
}
Expand Down Expand Up @@ -86,32 +90,35 @@ def print_to_file(data,settings):
VECTT_str += "{0} {1} {2} {3} {4} 0\n".format(i,settings.radius[0], settings.colour[0], settings.colour[1], settings.colour[2]) # set vector radius and colour
i+=1

ATOMT_match = re.search(r"ATOMT.*SCENE",data["initial_data"],flags=re.S)[0]
ATOMT_match = re.search(r"ATOMT.*SCENE",data["initial_data"],flags=re.S).group(0)
ATOMT_corrected = re.sub(r'([a-zA-Z]+\s+)\d+\.\d+',r"\1 0.0001",ATOMT_match) # this make all atoms reaaaalllly small

SITET_match = re.search(r"SITET.*VECTR",data["initial_data"],flags=re.S)[0]
SITET_match = re.search(r"SITET.*VECTR",data["initial_data"],flags=re.S).group(0)
SITET_corrected = re.sub(r'([a-zA-Z]+\d+\s+)\d+\.\d+',r"\1 0.0001",SITET_match) # this make all atoms reaaaalllly small

BONDP_match = re.search(r"BONDP.*POLYP",data["initial_data"],flags=re.S)[0]
BONDP_match = re.search(r"BONDP.*POLYP",data["initial_data"],flags=re.S).group(0)
BONDP_corrected = re.sub(r'(\d+\s+\d+\s+)\d+\.\d+',r"\1 0.0001",BONDP_match) # this makes all bonds reaaaalllly small

SBOND_match = re.search(r"SBOND.*SITET",data["initial_data"],flags=re.S)[0]
SBOND_match = re.search(r"SBOND.*SITET",data["initial_data"],flags=re.S).group(0)
SBOND_corrected = re.sub(r'(\s+\d\s+\d\s+\d\s+\d\s+\d\s+)\d+\.\d+',r"\1 0.0001",SBOND_match) # make all bonds reaaaalllly small

# substitute the above strings into the output data string
data["output_data"] = re.sub(r'(VECTR\n)',VECTR_str,data["initial_data"])
data["output_data"] = re.sub(r'(VECTT\n)',VECTT_str,data["output_data"])
data["output_data"] = re.sub(r'(ATOMT.*SCENE)',ATOMT_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(BONDP.*POLYP)',BONDP_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(SBOND.*SITET)',SBOND_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(SITET.*VECTR)',SITET_corrected,data["output_data"],flags=re.S)

if settings.hide_atoms == 1:
data["output_data"] = re.sub(r'(ATOMT.*SCENE)',ATOMT_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(BONDP.*POLYP)',BONDP_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(SBOND.*SITET)',SBOND_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(SITET.*VECTR)',SITET_corrected,data["output_data"],flags=re.S)
data["output_data"] = re.sub(r'(VECTS).*(FORMP)',r"\1 {} \n \2".format(settings.scale_factor[0]),data["output_data"],flags=re.S) # set vector scale factor
if settings.centre_atom: # centre the output structure around a particular atom
data["output_data"] = re.sub(r'(BOUND).*(SBOND)',r"\1 \n {0} {1} {2} {3} {4} {5} \n 0 0 0 0 0 \n \2".format(data["min_bound"][0],data["max_bound"][0],data["min_bound"][1],data["max_bound"][1],data["min_bound"][2],data["max_bound"][2]),data["output_data"],flags=re.S)

file_out = open('vectors.vesta','w+')
file_out = open(settings.outname,'w+')
file_out.write(data["output_data"])
file_out.close()


def parse_args():

Expand All @@ -121,6 +128,8 @@ def parse_args():
parser.add_argument('--filenames','-f',type=str, nargs=2, required=False,
default=["initial.vesta","final.vesta"], help="name of initial and final vesta files"
)
parser.add_argument('--calculate_displacements','-cd',type=int, nargs='?', required=False,default=1, help="If 1, displacement between positions in Vesta files is calculated. Else, second file is assumed to contain vectors to render."
)
parser.add_argument('--colour','-c',type=int, nargs=3,required=False,
default=[255,0,0], help="vector colour (in RGB)")
parser.add_argument('--radius','-r',type=float, nargs=1, required=False,
Expand All @@ -135,7 +144,9 @@ def parse_args():
default=[1.0], help='scale all vector moduli by this fixed amount')
parser.add_argument('--centre_atom', '-ca', type=int, nargs=1, required=False,
default=None, help='the output vesta structure is centred around an atom in the initial structure, specified by this index position (indexing from one)')

parser.add_argument('--hide_atoms', '-ha', type=int, nargs='?', required=False, default=0, help="hide atoms")
parser.add_argument('--outname','-o',type=str,nargs='?',required=False,
default='vectors.vesta',help="output file name")
args = parser.parse_args()

return args
Expand All @@ -151,8 +162,11 @@ def parse_args():
if settings.centre_atom:
data = calc_bounds(data)
data = delete_atoms(data)
data = calc_displacement(data)
if settings.calculate_displacements == 1:
data = calc_displacement(data)
else:
data["vectors"]=deepcopy(data["final_positions"])

print ("Printing to file")
print_to_file(data,settings)
print ("All done.")
print ("Saved output to {0}".format(os.path.abspath(settings.outname))