Skip to content

Commit 69475d7

Browse files
authored
Merge pull request #122 from amcadmus/devel
Coordination number for RDF and PBC for water molecules.
2 parents 14393ce + 4c6c6fb commit 69475d7

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

dpdata/md/rdf.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ def rdf(sys,
2121
Maximal range of rdf calculation
2222
nbins: int
2323
Number of bins for rdf calculation
24+
25+
Returns
26+
-------
27+
xx: np.array
28+
The lattice of r
29+
rdf: np.array
30+
The value of rdf at r
31+
coord: np.array
32+
The coordination number up to r
2433
"""
2534
return compute_rdf(sys['cells'], sys['coords'], sys['atom_types'],
2635
sel_type = sel_type,
@@ -36,12 +45,16 @@ def compute_rdf(box,
3645
nframes = box.shape[0]
3746
xx = None
3847
all_rdf = []
48+
all_cod = []
3949
for ii in range(nframes):
40-
xx, rdf = _compute_rdf_1frame(box[ii], posis[ii], atype, sel_type, max_r, nbins)
50+
xx, rdf, cod = _compute_rdf_1frame(box[ii], posis[ii], atype, sel_type, max_r, nbins)
4151
all_rdf.append(rdf)
52+
all_cod.append(cod)
4253
all_rdf = np.array(all_rdf).reshape([nframes, -1])
54+
all_cod = np.array(all_cod).reshape([nframes, -1])
4355
all_rdf = np.average(all_rdf, axis = 0)
44-
return xx, all_rdf
56+
all_cod = np.average(all_cod, axis = 0)
57+
return xx, all_rdf, all_cod
4558

4659
def _compute_rdf_1frame(box,
4760
posis,
@@ -65,6 +78,7 @@ def _compute_rdf_1frame(box,
6578
nlist = ase.neighborlist.NeighborList(max_r, self_interaction=False, bothways=True, primitive=ase.neighborlist.NewPrimitiveNeighborList)
6679
nlist.update(atoms)
6780
stat = np.zeros(nbins)
81+
stat_acc = np.zeros(nbins)
6882
hh = max_r / float(nbins)
6983
for ii in range(natoms) :
7084
# atom "0"
@@ -90,13 +104,17 @@ def _compute_rdf_1frame(box,
90104
for ii in sel_type[1]:
91105
c1 += np.sum(atype == ii)
92106
rho1 = c1 / np.linalg.det(box)
107+
# compute coordination number
108+
for ii in range(1, nbins):
109+
stat_acc[ii] = stat_acc[ii-1] + stat[ii-1]
110+
stat_acc = stat_acc / c0
93111
# compute rdf
94112
for ii in range(nbins):
95113
vol = 4./3. * np.pi * ( ((ii+1)*hh) ** 3 - ((ii)*hh) ** 3 )
96114
rho = stat[ii] / vol
97115
stat[ii] = rho / rho1 / c0
98116
xx = np.arange(0, max_r-1e-12, hh)
99-
return xx, stat
117+
return xx, stat, stat_acc
100118

101119
if __name__ == '__main__':
102120
import dpdata

dpdata/md/water.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from .pbc import posi_diff
3+
from .pbc import posi_shift
34

45
def compute_bonds (box,
56
posis,
@@ -179,3 +180,26 @@ def find_ions (atype,
179180
else :
180181
raise RuntimeError("unknow case: numb of O bonded to H > 1")
181182
return no, noh, noh2, noh3, nh
183+
184+
185+
186+
def pbc_coords(cells,
187+
coords,
188+
atom_types,
189+
oh_sel = [0, 1],
190+
max_roh = 1.3):
191+
bonds = compute_bonds(cells, coords, atom_types, oh_sel = oh_sel, max_roh = max_roh, uniq_hbond = True)
192+
193+
new_coords = np.copy(coords)
194+
natoms = len(atom_types)
195+
o_type = oh_sel[0]
196+
h_type = oh_sel[1]
197+
for ii in range(natoms):
198+
if atom_types[ii] == o_type:
199+
assert(len(bonds[ii]) == 2), 'O has more than 2 bonded Hs, stop'
200+
for jj in bonds[ii]:
201+
assert(atom_types[jj] == h_type), 'The atom bonded to O is not H, stop'
202+
shift = posi_shift(cells, coords[jj], coords[ii])
203+
new_coords[jj] = coords[jj] + np.matmul(shift, cells)
204+
return new_coords
205+

0 commit comments

Comments
 (0)