Skip to content

Commit 3cfad49

Browse files
committed
add to_pymatgen_structure and to_ase_structure method
1 parent 11e5687 commit 3cfad49

File tree

9 files changed

+108
-2
lines changed

9 files changed

+108
-2
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,43 @@ def from_lammps_lmp (self, file_name, type_map = None) :
346346
self.data = dpdata.lammps.lmp.to_system_data(lines, type_map)
347347
self._shift_orig_zero()
348348

349+
def to_pymatgen_structure(self):
350+
'''
351+
convert System to Pymatgen Structure obj
352+
353+
'''
354+
structures=[]
355+
try:
356+
from pymatgen import Structure
357+
except:
358+
raise ImportError('No module pymatgen.Structure')
359+
360+
for system in self.to_list():
361+
species=[]
362+
for name,numb in zip(system.data['atom_names'],system.data['atom_numbs']):
363+
species.extend([name]*numb)
364+
structure=Structure(system.data['cells'][0],species,system.data['coords'][0],coords_are_cartesian=True)
365+
structures.append(structure)
366+
return structures
367+
368+
def to_ase_structure(self):
369+
'''
370+
convert System to ASE Atom obj
371+
372+
'''
373+
structures=[]
374+
try:
375+
from ase import Atoms
376+
except:
377+
raise ImportError('No module ase.Atoms')
378+
379+
for system in self.to_list():
380+
species=[]
381+
for name,numb in zip(system.data['atom_names'],system.data['atom_numbs']):
382+
species.extend([name]*numb)
383+
structure=Atoms(symbols=species,positions=system.data['coords'][0],pbc=True,cell=system.data['cells'][0])
384+
structures.append(structure)
385+
return structures
349386

350387
def to_lammps_lmp(self, file_name, frame_idx = 0) :
351388
"""
@@ -380,6 +417,19 @@ def from_vasp_poscar(self, file_name) :
380417
self.data = dpdata.vasp.poscar.to_system_data(lines)
381418
self.rot_lower_triangular()
382419

420+
421+
def to_vasp_string(self, frame_idx=0):
422+
"""
423+
Dump the system in vasp POSCAR format string
424+
425+
Parameters
426+
----------
427+
frame_idx : int
428+
The index of the frame to dump
429+
"""
430+
assert(frame_idx < len(self.data['coords']))
431+
w_str = dpdata.vasp.poscar.from_system_data(self.data, frame_idx)
432+
return w_str
383433

384434
def to_vasp_poscar(self, file_name, frame_idx = 0) :
385435
"""
@@ -392,8 +442,7 @@ def to_vasp_poscar(self, file_name, frame_idx = 0) :
392442
frame_idx : int
393443
The index of the frame to dump
394444
"""
395-
assert(frame_idx < len(self.data['coords']))
396-
w_str = dpdata.vasp.poscar.from_system_data(self.data, frame_idx)
445+
w_str=self.to_vasp_string( frame_idx= frame_idx )
397446
with open(file_name, 'w') as fp:
398447
fp.write(w_str)
399448

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)