-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxyzimport.py
81 lines (73 loc) · 1.64 KB
/
xyzimport.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
# xyz import.py
import pprint
class Atom:
"""
An atom.
"""
def __init__(self, element, x, y, z):
"""
Initialise the atom.
"""
self.element = element
self.x = x
self.y = y
self.z = z
def importMolecule(path):
"""
Import a molecule from a file.
"""
# Open the file.
with open(path, 'r') as f:
# Read the file.
lines = f.readlines()
# Get the number of atoms.
nAtoms = int(lines[0]) +1
# Create a list to store the atoms.
atoms = []
# Loop over the atoms.
for i in range(1, nAtoms):
# Split the line.
line = lines[i+1].split()
# Get the element and coordinates.
element = line[0]
x = float(line[1])
y = float(line[2])
z = float(line[3])
# Create the atom.
atom = Atom(element, x, y, z)
# Add the atom to the list.
atoms.append(atom)
print(atom.element, atom.x, atom.y, atom.z)
return atoms
def updateXYZ(atoms = [], x = 0, y = 0, z = 0):
"""
Update the coordinates of the atoms.
"""
for atom in atoms:
atom.x += x
atom.y += y
atom.z += z
return atoms
def printXYZ(atoms = [], path='output.xyz'):
"""
Print the molecule to a file.
"""
# Open the file.
with open(path, 'w') as f:
# Print the number of atoms.
f.write(str(len(atoms)) + '\n')
# Loop over the atoms.
for atom in atoms:
# Print the atom.
f.write(atom.element + ' ' + str(atom.x) + ' ' + str(atom.y) + ' ' + str(atom.z) + '\n')
def combineNoCheck(path1, path2, output="output.xyz"):
"""
Combine two xyz files.
"""
atoms1 = importMolecule(path1)
atoms2 = importMolecule(path2)
atoms = atoms1 + atoms2
printXYZ(atoms, output)
# Print the molecule.
if __name__ == "__main__":
importMolecule('test.xyz')