/
ase.xyz.py
executable file
·149 lines (122 loc) · 5.57 KB
/
ase.xyz.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import sys
import os.path
from ase.io.xyz import read_xyz
from ase.io.xyz import write_xyz
from ase.io.aims import write_aims
from ase.io.vasp import write_vasp
from ase import Atoms
import asekk
from math import pi
from optparse import OptionParser
num = len(sys.argv)
input_file = sys.argv[num-1]
def_cell_file = os.path.splitext(input_file)[0]+".lvs"
parser = OptionParser()
#parser.add_option("-f", "--format", action="store", type="string", default="xyz", help="format of the output file: xyz")
parser.add_option("-a", "--atoms", action="store", type="int", default=[-1,-1], help="specify atoms to whcih changes (translation rotation etc. will be made)", nargs=2)
parser.add_option("-s", "--steps", action="store", type="int", default=None, help="specify steps ", nargs=2)
parser.add_option("-p", "--periods", action="store", type="int", default=[1,1,1], help="repetition of the unit cell", nargs=3)
parser.add_option("-T", "--Translation", action="store", type="float", default=[0.0,0.0,0.0], help="ss", nargs=3)
parser.add_option("-r", "--rotation_angle", action="store", type="float", default=0.0, help="rotation angle", nargs=1)
parser.add_option( "--rotate_around", action="store", type="int", default=0, help="rotate around nth atom", nargs=1)
parser.add_option( "--axis", action="store", type="string", default="z", help="rotation axis", nargs=1)
parser.add_option("-c", "--cell", action="store", type="string", default=def_cell_file, help="format of the output file: xyz")
parser.add_option("-f", "--format", action="store", type="string", default="xyz", help="format of the output file: POSCAR, (xyz in preparation)")
(options, args) = parser.parse_args()
print_debug = False
if(num < 2):
parser.print_help()
else:
file_xyz = read_xyz(input_file, slice(0,None,1))
natoms = file_xyz[0].get_number_of_atoms()
# if possible read unit-cell
if(os.path.isfile(options.cell)):
cell = [[],[],[]]
f = open(options.cell, "r")
ls = f.read().splitlines()
for i in range(3):
l = ls[i].split()
cell[i] = [float(l[0]), float(l[1]), float(l[2])]
for step in file_xyz:
step.set_cell(cell)
step.set_pbc([True,True,True])
# --------------- Atoms -----------------
a = options.atoms
if(a == [-1,-1]): # deafult case: we change all atoms
a = [1,natoms]
a_from = a[0]
a_to = a[1]
atoms_range_negative = (a_to-a_from < 0)
is_range_out_of_natoms = (a_from > natoms or a_to > natoms)
if( a_from > natoms or a_to > natoms ):
print "ERROR: Atoms range is out of [1,number_of_atoms]"
print a
exit()
elif( atoms_range_negative ):
print "ERROR: Atoms range is negative "
print a
exit()
# --------------- Translation -----------------
t = options.Translation
is_translation_nonzero = (t != [0.0,0.0,0.0])
do_we_translate = is_translation_nonzero and (not atoms_range_negative)
# --------------- Rotation -----------------
alpha = options.rotation_angle
# --------------- Periodic repetitions -----------------
p = options.periods
if( p[0] <= 0 and p[1] <= 0 and p[2] <= 0 ):
print "ERROR: At least one of period is not positive"
exit()
do_we_repeat = (p != [1,1,1])
# --------------- Steps -------------------------------
n_steps = len(file_xyz)
is_there_any_step = (n_steps > 0)
s = options.steps
if(s == None): # deafult case: all steps
s = [1, n_steps]
elif(s == (-1,-1)): # last step
s = [n_steps,n_steps]
elif( s[0] > n_steps or s[1] > n_steps ):
print "ERROR: Step range is larger than number of steps in OUTCAR"
exit()
elif( s[0] > s[1] ):
print "ERROR: Max step is smaler that Min step"
exit()
##############################
# loop over steps
##############################
if(is_there_any_step):
# step selection
for i in range(s[0],s[1]+1):
step = file_xyz[i-1]
# print step
# comm = "step no. " + str(i) + " TOTEN =
comm = "I do not know how to obtain comment in ase.io.xyz.read_xyz()"
# --- Translate ----
if(do_we_translate):
for i in range(a_from-1,a_to):
step.arrays['positions'][i] += t
# --- Rotate ----
if(alpha != 0.0):
# set origin of rotation
origin = [0.0,0.0,0.0]
ra = options.rotate_around
if(ra > 0 and ra <= natoms):
positions = step.arrays['positions']
origin = positions[ra-1]
# rotate selected atoms
asekk.rotate_atoms(step, alpha, fromto=[a_from,a_to], axis=options.axis, origin=origin)
# --- Repetytion ----
if(do_we_repeat):
# multiply by periods
step = step*p
# ----- Write -----
if(options.format == 'xyz'):
write_xyz(sys.stdout,step,comment=comm)
elif(options.format == 'aims'):
write_aims("geometry.in",step)
elif(options.format == 'poscar'):
# print step.get_cell()
step.center()
write_vasp(sys.stdout,step,label="POSCAR generated by asm.xyz.py", direct=False,sort=False,vasp5=True)