Skip to content

Commit 8dac1f5

Browse files
authored
Merge pull request #70 from njzjz/nopbc
support nopbc
2 parents 96428c4 + 0ce9879 commit 8dac1f5

22 files changed

+99
-56
lines changed

dpdata/deepmd/comp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def to_system_data(folder,
4848
data['forces'] = np.concatenate(all_forces, axis = 0)
4949
if len(all_virs) > 0:
5050
data['virials'] = np.concatenate(all_virs, axis = 0)
51+
if os.path.isfile(os.path.join(folder, "nopbc")):
52+
data['nopbc'] = True
5153
return data
5254

5355

@@ -101,5 +103,10 @@ def dump(folder,
101103
np.save(os.path.join(set_folder, 'virial'), virials[set_stt:set_end])
102104
if 'atom_pref' in data:
103105
np.save(os.path.join(set_folder, "atom_pref"), atom_pref[set_stt:set_end])
104-
106+
try:
107+
os.remove(os.path.join(folder, "nopbc"))
108+
except OSError:
109+
pass
110+
if data.get("nopbc", False):
111+
os.mknod(os.path.join(folder, "nopbc"))
105112

dpdata/deepmd/raw.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def to_system_data(folder, type_map = None, labels = True) :
4949
if os.path.exists(os.path.join(folder, 'virial.raw')) :
5050
data['virials'] = np.loadtxt(os.path.join(folder, 'virial.raw'))
5151
data['virials'] = np.reshape(data['virials'], [nframes, 3, 3])
52+
if os.path.isfile(os.path.join(folder, "nopbc")):
53+
data['nopbc'] = True
5254
return data
5355
else :
5456
raise RuntimeError('not dir ' + folder)
@@ -67,5 +69,10 @@ def dump (folder, data) :
6769
np.savetxt(os.path.join(folder, 'force.raw'), np.reshape(data['forces'], [nframes, -1]))
6870
if 'virials' in data :
6971
np.savetxt(os.path.join(folder, 'virial.raw'), np.reshape(data['virials'], [nframes, 9]))
70-
72+
try:
73+
os.remove(os.path.join(folder, "nopbc"))
74+
except OSError:
75+
pass
76+
if data.get("nopbc", False):
77+
os.mknod(os.path.join(folder, "nopbc"))
7178

dpdata/gaussian/log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ def to_system_data(file_name, md=False):
6969
data['coords'] = np.array(coords_t)
7070
data['orig'] = np.array([0, 0, 0])
7171
data['cells'] = np.array([[[100., 0., 0.], [0., 100., 0.], [0., 0., 100.]] for _ in energy_t])
72+
data['nopbc'] = True
7273
return data

dpdata/system.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def sub_system(self, f_idx) :
284284
tmp.data[ii] = self.data[ii]
285285
tmp.data['cells'] = self.data['cells'][f_idx].reshape(-1, 3, 3)
286286
tmp.data['coords'] = self.data['coords'][f_idx].reshape(-1, self.data['coords'].shape[1], 3)
287+
tmp.data['nopbc'] = self.nopbc
287288
return tmp
288289

289290

@@ -319,6 +320,9 @@ def append(self, system) :
319320
assert(all(eq))
320321
for ii in ['coords', 'cells'] :
321322
self.data[ii] = np.concatenate((self.data[ii], system[ii]), axis = 0)
323+
if self.nopbc and not system.nopbc:
324+
# appended system uses PBC, cancel nopbc
325+
self.data['nopbc'] = False
322326
return True
323327

324328
def sort_atom_names(self, type_map=None):
@@ -726,6 +730,12 @@ def perturb(self,
726730
perturbed_system.append(tmp_system)
727731
return perturbed_system
728732

733+
@property
734+
def nopbc(self):
735+
if self.data.get("nopbc", False):
736+
return True
737+
return False
738+
729739
def get_cell_perturb_matrix(cell_pert_fraction):
730740
if cell_pert_fraction<0:
731741
raise RuntimeError('cell_pert_fraction can not be negative')
@@ -1018,6 +1028,7 @@ def from_gaussian_log(self, file_name, md=False):
10181028
self.data = dpdata.gaussian.log.to_system_data(file_name, md=md)
10191029
except AssertionError:
10201030
self.data['energies'], self.data['forces']= [], []
1031+
self.data['nopbc'] = True
10211032

10221033

10231034
def from_cp2k_output(self, file_name) :
@@ -1245,7 +1256,7 @@ def to_deepmd_npy(self, folder, set_size = 5000, prec=np.float32) :
12451256
def check_System(data):
12461257
keys={'atom_names','atom_numbs','cells','coords','orig','atom_types'}
12471258
assert( isinstance(data,dict) )
1248-
assert( set(data.keys())==keys )
1259+
assert( keys.issubset(set(data.keys())) )
12491260
if len(data['coords']) > 0 :
12501261
assert( len(data['coords'][0])==len(data['atom_types'])==sum(data['atom_numbs']) )
12511262
else :

tests/comp_sys.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def test_coord(self):
5858
places = self.places,
5959
msg = 'coord[%d][%d][%d] failed' % (ff,ii,jj))
6060

61+
def test_nopbc(self):
62+
self.assertEqual(self.system_1.nopbc, self.system_2.nopbc)
63+
6164

6265
class CompLabeledSys (CompSys) :
6366
def test_energy(self) :
@@ -97,4 +100,12 @@ def test_virial(self) :
97100
places = self.v_places,
98101
msg = 'virials[%d][%d][%d] failed' % (ff,ii,jj))
99102

100-
103+
class IsPBC:
104+
def test_is_pbc(self):
105+
self.assertFalse(self.system_1.nopbc)
106+
self.assertFalse(self.system_2.nopbc)
107+
108+
class IsNoPBC:
109+
def test_is_nopbc(self):
110+
self.assertTrue(self.system_1.nopbc)
111+
self.assertTrue(self.system_2.nopbc)

tests/test_deepmd_comp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import numpy as np
33
import unittest
44
from context import dpdata
5-
from comp_sys import CompLabeledSys, CompSys
5+
from comp_sys import CompLabeledSys, CompSys, IsPBC
66

7-
class TestDeepmdLoadDumpComp(unittest.TestCase, CompLabeledSys):
7+
class TestDeepmdLoadDumpComp(unittest.TestCase, CompLabeledSys, IsPBC):
88
def setUp (self) :
99
self.system_1 = dpdata.LabeledSystem('poscars/OUTCAR.h2o.md',
1010
fmt = 'vasp/outcar')
@@ -25,7 +25,7 @@ def tearDown(self) :
2525
shutil.rmtree('tmp.deepmd.npy')
2626

2727

28-
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys) :
28+
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys, IsPBC) :
2929
def setUp (self) :
3030
self.system_1 = dpdata.System('poscars/POSCAR.h2o.md',
3131
fmt = 'vasp/poscar')
@@ -45,7 +45,7 @@ def tearDown(self) :
4545
shutil.rmtree('tmp.deepmd.npy')
4646

4747

48-
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys) :
48+
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys, IsPBC) :
4949
def setUp(self) :
5050
self.dir_name = 'tmp.deepmd.npy.nol'
5151
natoms = 3

tests/test_deepmd_raw.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import numpy as np
33
import unittest
44
from context import dpdata
5-
from comp_sys import CompLabeledSys, CompSys
5+
from comp_sys import CompLabeledSys, CompSys, IsPBC
66

7-
class TestDeepmdLoadRaw(unittest.TestCase, CompLabeledSys):
7+
class TestDeepmdLoadRaw(unittest.TestCase, CompLabeledSys, IsPBC):
88
def setUp (self) :
99
self.system_1 = dpdata.LabeledSystem('poscars/OUTCAR.h2o.md',
1010
fmt = 'vasp/outcar')
@@ -17,7 +17,7 @@ def setUp (self) :
1717
self.v_places = 6
1818

1919

20-
class TestDeepmdDumpRaw(unittest.TestCase, CompLabeledSys):
20+
class TestDeepmdDumpRaw(unittest.TestCase, CompLabeledSys, IsPBC):
2121
def setUp (self) :
2222
self.system_1 = dpdata.LabeledSystem('poscars/OUTCAR.h2o.md',
2323
fmt = 'vasp/outcar')
@@ -131,7 +131,7 @@ def test_npy_type_map_enforce (self) :
131131

132132

133133

134-
class TestDeepmdRawNoLabels(unittest.TestCase, CompSys) :
134+
class TestDeepmdRawNoLabels(unittest.TestCase, CompSys, IsPBC) :
135135
def setUp (self) :
136136
self.system_1 = dpdata.System('poscars/POSCAR.h2o.md',
137137
fmt = 'vasp/poscar')
@@ -149,7 +149,7 @@ def tearDown(self) :
149149
shutil.rmtree('tmp.deepmd')
150150

151151

152-
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys) :
152+
class TestDeepmdCompNoLabels(unittest.TestCase, CompSys, IsPBC) :
153153
def setUp(self) :
154154
self.dir_name = 'tmp.deepmd.nol'
155155
natoms = 3

tests/test_gaussian_log.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import numpy as np
33
import unittest
44
from context import dpdata
5-
from comp_sys import CompLabeledSys
65

76
class TestGaussianLog :
87
def test_atom_names(self) :
@@ -18,6 +17,9 @@ def test_atom_types(self) :
1817
for ii in range(len(self.atom_types)) :
1918
self.assertEqual(self.system.data['atom_types'][ii], self.atom_types[ii])
2019

20+
def test_nopbc(self):
21+
self.assertEqual(self.system.nopbc, True)
22+
2123
class TestGaussianLoadLog(unittest.TestCase, TestGaussianLog):
2224
def setUp (self) :
2325
self.system = dpdata.LabeledSystem('gaussian/methane.gaussianlog',

tests/test_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import numpy as np
33
import unittest
44
from context import dpdata
5-
from comp_sys import CompLabeledSys
5+
from comp_sys import CompLabeledSys, IsPBC
66

7-
class TestJsonLoad(unittest.TestCase, CompLabeledSys):
7+
class TestJsonLoad(unittest.TestCase, CompLabeledSys, IsPBC):
88
def setUp (self) :
99
self.system_1 = dpdata.LabeledSystem('poscars/OUTCAR.h2o.md',
1010
fmt = 'vasp/outcar')
@@ -14,7 +14,7 @@ def setUp (self) :
1414
self.f_places = 6
1515
self.v_places = 4
1616

17-
class TestAsDict(unittest.TestCase, CompLabeledSys):
17+
class TestAsDict(unittest.TestCase, CompLabeledSys, IsPBC):
1818
def setUp (self) :
1919
self.system_1 = dpdata.LabeledSystem('poscars/OUTCAR.h2o.md',
2020
fmt = 'vasp/outcar')

tests/test_lammps_dump_skipload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import numpy as np
33
import unittest
44
from context import dpdata
5-
from comp_sys import CompSys
5+
from comp_sys import CompSys, IsPBC
66

7-
class TestLmpDumpSkip(unittest.TestCase, CompSys):
7+
class TestLmpDumpSkip(unittest.TestCase, CompSys, IsPBC):
88

99
def setUp(self):
1010
self.system_1 = dpdata.System(os.path.join('poscars', 'conf.5.dump'),

0 commit comments

Comments
 (0)