-
Notifications
You must be signed in to change notification settings - Fork 0
/
Molecule.py
65 lines (47 loc) · 1.48 KB
/
Molecule.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
#!/usr/bin env python
__author__ = """Kristof Karhan"""
import numpy as np
from read_snapshots import FFormat
class Molecule(list):
__GLOBALID__ = 0
@staticmethod
def __getid__():
Molecule.__GLOBALID__ += 1
return Molecule.__GLOBALID__ - 1
def __init__(self):
super(Molecule, self).__init__()
self.__id = Molecule.__getid__()
def printgeo(self, foutput, printformat=FFormat.xyz):
if printformat != FFormat.xyz:
raise NotImplementedError("lazily left out format")
with open(foutput, 'w') as wfh:
wfh.write(" {0:d}\n".format(len(self)))
for anatom in self:
wfh.write("{0:2s} {1:12.6f} {2:12.6f} {3:12.6f}".format(anatom.element, *anatom.pos))
def __len__(self):
return super(Molecule, self).__len__()
def get_atoms(self):
return super(Molecule, self)
def get_id(self):
return self.__id
def get_com(self):
center = np.zeros(3)
mass = 0
for atom in self:
pos = atom.pos
mass += get_mass(atom.element)
for i in range(3):
center[i] += pos[i]
center /= mass
return center
natoms = property(__len__)
size = property(__len__)
atoms = property(get_atoms)
id = property(get_id)
com = property(get_com)
def get_mass(element):
element = element.strip()
if element == 'H':
return 1
if element == "O":
return 16