Skip to content

Commit 38fc532

Browse files
authored
Merge pull request #80 from amcadmus/devel
add remove pbc
2 parents b7024c5 + b1589b7 commit 38fc532

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

dpdata/system.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,35 @@ def apply_pbc(self) :
434434
ncoord = ncoord % 1
435435
self.data['coords'] = np.matmul(ncoord, self.data['cells'])
436436

437+
438+
def remove_pbc(self, protect_layer = 9):
439+
"""
440+
This method does NOT delete the definition of the cells, it
441+
(1) revises the cell to a cubic cell and ensures that the cell
442+
boundary to any atom in the system is no less than `protect_layer`
443+
(2) translates the system such that the center-of-geometry of the system
444+
locates at the center of the cell.
445+
446+
Parameters
447+
----------
448+
protect_layer : the protect layer between the atoms and the cell
449+
boundary
450+
"""
451+
nframes = self.get_nframes()
452+
natoms = self.get_natoms()
453+
assert(protect_layer >= 0), "the protect_layer should be no less than 0"
454+
for ff in range(nframes):
455+
tmpcoord = self.data['coords'][ff]
456+
cog = np.average(tmpcoord, axis = 0)
457+
dist = tmpcoord - np.tile(cog, [natoms, 1])
458+
max_dist = np.max(np.linalg.norm(dist, axis = 1))
459+
h_cell_size = max_dist + protect_layer
460+
cell_size = h_cell_size * 2
461+
shift = np.array([1,1,1]) * h_cell_size - cog
462+
self.data['coords'][ff] = self.data['coords'][ff] + np.tile(shift, [natoms, 1])
463+
self.data['cells'][ff] = cell_size * np.eye(3)
464+
465+
437466
@register_from_funcs.register_funcs("lmp")
438467
@register_from_funcs.register_funcs("lammps/lmp")
439468
def from_lammps_lmp (self, file_name, type_map = None) :

tests/test_remove_pbc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import numpy as np
3+
import unittest
4+
from context import dpdata
5+
6+
class TestRemovePBC(unittest.TestCase):
7+
8+
def test_remove(self):
9+
coords = np.array([[[-1, -1, 2], [-1,-1,-3], [-1,-1, 7]],
10+
[[ 3, -1, 3], [-1,-1, 3], [ 7,-1, 3]]], dtype = float)
11+
cogs = np.average(coords, axis = 1)
12+
data = {'atom_names' : ['A', 'B'],
13+
'atom_numbs' : [1, 2],
14+
'atom_types' : np.array([1, 0, 1], dtype = int),
15+
'orig': np.array([0, 0, 0]),
16+
'coords': coords,
17+
'cells': np.random.random([2, 3, 3]),
18+
}
19+
sys = dpdata.System(data = data)
20+
proct = 9.0
21+
22+
mol_size = np.array([5, 4], dtype = float)
23+
cell_size = (mol_size + proct) * 2.0
24+
25+
sys.remove_pbc(proct)
26+
27+
for ff in range(2):
28+
ref = cell_size[ff] * np.eye(3)
29+
for ii in range(3):
30+
for jj in range(3):
31+
self.assertAlmostEqual(sys['cells'][ff][ii][jj], ref[ii][jj], msg = '%d %d %d' %(ff, ii, jj))
32+
dists = []
33+
for ii in range(sys.get_natoms()):
34+
for jj in range(3):
35+
dists.append(np.abs(sys['coords'][ff][ii][jj]))
36+
dists.append(np.abs(sys['cells'][ff][jj][jj] - sys['coords'][ff][ii][jj]))
37+
self.assertAlmostEqual(np.min(dists), proct)

0 commit comments

Comments
 (0)