Skip to content

Commit 84affb1

Browse files
authored
Merge pull request #68 from haidi-ustc/devel
Devel
2 parents 30cbb6a + 84aa9c9 commit 84affb1

File tree

9 files changed

+108
-3
lines changed

9 files changed

+108
-3
lines changed

dpdata/deepmd/__init__.py

Whitespace-only changes.

dpdata/gaussian/__init__.py

Whitespace-only changes.

dpdata/lammps/__init__.py

Whitespace-only changes.

dpdata/md/__init__.py

Whitespace-only changes.

dpdata/pwscf/__init__.py

Whitespace-only changes.

dpdata/system.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,43 @@ def from_lammps_lmp (self, file_name, type_map = None) :
416416
self.data = dpdata.lammps.lmp.to_system_data(lines, type_map)
417417
self._shift_orig_zero()
418418

419+
def to_pymatgen_structure(self):
420+
'''
421+
convert System to Pymatgen Structure obj
422+
423+
'''
424+
structures=[]
425+
try:
426+
from pymatgen import Structure
427+
except:
428+
raise ImportError('No module pymatgen.Structure')
429+
430+
for system in self.to_list():
431+
species=[]
432+
for name,numb in zip(system.data['atom_names'],system.data['atom_numbs']):
433+
species.extend([name]*numb)
434+
structure=Structure(system.data['cells'][0],species,system.data['coords'][0],coords_are_cartesian=True)
435+
structures.append(structure)
436+
return structures
437+
438+
def to_ase_structure(self):
439+
'''
440+
convert System to ASE Atom obj
441+
442+
'''
443+
structures=[]
444+
try:
445+
from ase import Atoms
446+
except:
447+
raise ImportError('No module ase.Atoms')
448+
449+
for system in self.to_list():
450+
species=[]
451+
for name,numb in zip(system.data['atom_names'],system.data['atom_numbs']):
452+
species.extend([name]*numb)
453+
structure=Atoms(symbols=species,positions=system.data['coords'][0],pbc=True,cell=system.data['cells'][0])
454+
structures.append(structure)
455+
return structures
419456

420457
def to_lammps_lmp(self, file_name, frame_idx = 0) :
421458
"""
@@ -450,7 +487,19 @@ def from_vasp_poscar(self, file_name) :
450487
self.data = dpdata.vasp.poscar.to_system_data(lines)
451488
self.rot_lower_triangular()
452489

453-
490+
def to_vasp_string(self, frame_idx=0):
491+
"""
492+
Dump the system in vasp POSCAR format string
493+
494+
Parameters
495+
----------
496+
frame_idx : int
497+
The index of the frame to dump
498+
"""
499+
assert(frame_idx < len(self.data['coords']))
500+
w_str = dpdata.vasp.poscar.from_system_data(self.data, frame_idx)
501+
return w_str
502+
454503
def to_vasp_poscar(self, file_name, frame_idx = 0) :
455504
"""
456505
Dump the system in vasp POSCAR format
@@ -462,8 +511,7 @@ def to_vasp_poscar(self, file_name, frame_idx = 0) :
462511
frame_idx : int
463512
The index of the frame to dump
464513
"""
465-
assert(frame_idx < len(self.data['coords']))
466-
w_str = dpdata.vasp.poscar.from_system_data(self.data, frame_idx)
514+
w_str=self.to_vasp_string( frame_idx= frame_idx )
467515
with open(file_name, 'w') as fp:
468516
fp.write(w_str)
469517

dpdata/vasp/__init__.py

Whitespace-only changes.

tests/test_to_ase.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import numpy as np
3+
import unittest
4+
from context import dpdata
5+
from comp_sys import CompSys
6+
try:
7+
from ase import Atoms
8+
from ase.io import write
9+
exist_module=True
10+
except:
11+
exist_module=False
12+
13+
@unittest.skipIf(not exist_module,"skip test_ase")
14+
class TestASE(unittest.TestCase, CompSys):
15+
16+
def setUp(self):
17+
system_1 = dpdata.System()
18+
system_1.from_lammps_lmp(os.path.join('poscars', 'conf.lmp'), type_map = ['O', 'H'])
19+
write('tmp.POSCAR',system_1.to_ase_structure()[0],vasp5=True)
20+
self.system_1=system_1
21+
self.system_2=dpdata.System('tmp.POSCAR')
22+
self.places = 6
23+
self.e_places = 6
24+
self.f_places = 6
25+
self.v_places = 6
26+
27+
if __name__ == '__main__':
28+
unittest.main()
29+

tests/test_to_pymatgen.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import numpy as np
3+
import unittest
4+
from context import dpdata
5+
from comp_sys import CompSys
6+
try:
7+
from pymatgen import Structure
8+
exist_module=True
9+
except:
10+
exist_module=False
11+
12+
@unittest.skipIf(not exist_module,"skip pymatgen")
13+
class TestPymatgen(unittest.TestCase, CompSys):
14+
15+
def setUp(self):
16+
system_1 = dpdata.System()
17+
system_1.from_lammps_lmp(os.path.join('poscars', 'conf.lmp'), type_map = ['O', 'H'])
18+
system_1.to_pymatgen_structure()[0].to('poscar','tmp.POSCAR')
19+
self.system_1=system_1
20+
self.system_2=dpdata.System('tmp.POSCAR')
21+
self.places = 6
22+
self.e_places = 6
23+
self.f_places = 6
24+
self.v_places = 6
25+
26+
if __name__ == '__main__':
27+
unittest.main()
28+

0 commit comments

Comments
 (0)