Skip to content

Commit 95c70d8

Browse files
committed
register to funcs
1 parent 1766182 commit 95c70d8

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,26 @@ Available properties are (nframe: number of frames in the system, natoms: total
131131
## Dump data
132132
The data stored in `System` or `LabeledSystem` can be dumped in 'lammps/lmp' or 'vasp/poscar' format, for example:
133133
```python
134-
d_outcar.to_lammps_lmp('conf.lmp', frame_idx=0)
134+
d_outcar.to('lammps/lmp', 'conf.lmp', frame_idx=0)
135135
```
136136
The first frames of `d_outcar` will be dumped to 'conf.lmp'
137137
```python
138-
d_outcar.to_vasp_poscar('POSCAR', frame_idx=-1)
138+
d_outcar.to('vasp/poscar', 'POSCAR', frame_idx=-1)
139139
```
140140
The last frames of `d_outcar` will be dumped to 'POSCAR'.
141141

142142

143143
The data stored in `LabeledSystem` can be dumped to deepmd-kit raw format, for example
144144
```python
145-
d_outcar.to_deepmd_raw('dpmd_raw')
145+
d_outcar.to('deepmd/raw', 'dpmd_raw')
146146
```
147147
Or a simpler command:
148148
```python
149-
dpdata.LabeledSystem('OUTCAR').to_deepmd_raw('dpmd_raw')
149+
dpdata.LabeledSystem('OUTCAR').to('deepmd/raw', 'dpmd_raw')
150150
```
151151
Frame selection can be implemented by
152152
```python
153-
dpdata.LabeledSystem('OUTCAR').sub_system([0,-1]).to_deepmd_raw('dpmd_raw')
153+
dpdata.LabeledSystem('OUTCAR').sub_system([0,-1]).to('deepmd/raw', 'dpmd_raw')
154154
```
155155
by which only the first and last frames are dumped to `dpmd_raw`.
156156

dpdata/system.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ def __init__ (self,
116116
self.apply_type_map(type_map)
117117

118118
register_from_funcs = Register()
119+
register_to_funcs = Register()
119120

120-
def from_fmt(self, file_name, fmt, **kwargs):
121+
def from_fmt(self, file_name, fmt='auto', **kwargs):
121122
fmt = fmt.lower()
122123
if fmt == 'auto':
123124
fmt = os.path.basename(file_name).split('.')[-1].lower()
@@ -129,6 +130,17 @@ def from_fmt(self, file_name, fmt, **kwargs):
129130
func(self, file_name, **kwargs)
130131
else :
131132
raise RuntimeError('unknow data format ' + fmt)
133+
134+
def to(self, fmt, *args, **kwargs):
135+
fmt = fmt.lower()
136+
to_funcs = self.register_to_funcs.funcs
137+
if fmt in to_funcs:
138+
func = to_funcs[fmt]
139+
func_args = inspect.getfullargspec(func).args
140+
kwargs = {kk: kwargs[kk] for kk in kwargs if kk in func_args}
141+
func(self, *args, **kwargs)
142+
else :
143+
raise RuntimeError('unknow data format %s. Accepted format:' % (fmt, " ".join(to_funcs)))
132144

133145
def __repr__(self):
134146
return self.__str__()
@@ -215,7 +227,7 @@ def map_atom_types(self,type_map=None):
215227

216228
return new_atom_types
217229

218-
230+
@register_to_funcs.register_funcs("list")
219231
def to_list(self):
220232
"""
221233
convert system to list, usefull for data collection
@@ -430,6 +442,7 @@ def from_lammps_lmp (self, file_name, type_map = None) :
430442
self.data = dpdata.lammps.lmp.to_system_data(lines, type_map)
431443
self._shift_orig_zero()
432444

445+
@register_to_funcs.register_funcs("pymatgen/structure")
433446
def to_pymatgen_structure(self):
434447
'''
435448
convert System to Pymatgen Structure obj
@@ -449,6 +462,7 @@ def to_pymatgen_structure(self):
449462
structures.append(structure)
450463
return structures
451464

465+
@register_to_funcs.register_funcs("ase/structure")
452466
def to_ase_structure(self):
453467
'''
454468
convert System to ASE Atom obj
@@ -468,6 +482,7 @@ def to_ase_structure(self):
468482
structures.append(structure)
469483
return structures
470484

485+
@register_to_funcs.register_funcs("lammps/lmp")
471486
def to_lammps_lmp(self, file_name, frame_idx = 0) :
472487
"""
473488
Dump the system in lammps data format
@@ -505,6 +520,7 @@ def from_vasp_poscar(self, file_name) :
505520
self.data = dpdata.vasp.poscar.to_system_data(lines)
506521
self.rot_lower_triangular()
507522

523+
@register_to_funcs.register_funcs("vasp/string")
508524
def to_vasp_string(self, frame_idx=0):
509525
"""
510526
Dump the system in vasp POSCAR format string
@@ -518,6 +534,7 @@ def to_vasp_string(self, frame_idx=0):
518534
w_str = dpdata.vasp.poscar.from_system_data(self.data, frame_idx)
519535
return w_str
520536

537+
@register_to_funcs.register_funcs("vasp/poscar")
521538
def to_vasp_poscar(self, file_name, frame_idx = 0) :
522539
"""
523540
Dump the system in vasp POSCAR format
@@ -556,6 +573,7 @@ def from_deepmd_raw(self, folder, type_map = None) :
556573
if tmp_data is not None :
557574
self.data = tmp_data
558575

576+
@register_to_funcs.register_funcs("deepmd/npy")
559577
def to_deepmd_npy(self, folder, set_size = 5000, prec=np.float32) :
560578
"""
561579
Dump the system in deepmd compressed format (numpy binary) to `folder`.
@@ -578,6 +596,7 @@ def to_deepmd_npy(self, folder, set_size = 5000, prec=np.float32) :
578596
set_size = set_size,
579597
comp_prec = prec)
580598

599+
@register_to_funcs.register_funcs("deepmd/raw")
581600
def to_deepmd_raw(self, folder) :
582601
"""
583602
Dump the system in deepmd raw format to `folder`

tests/test_lammps_lmp_dump.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def setUp(self):
1414
self.system.from_lammps_lmp('tmp.lmp',
1515
type_map = ['O', 'H'])
1616

17+
class TestToFunc(unittest.TestCase, TestPOSCARoh):
18+
19+
def setUp(self):
20+
tmp_system = dpdata.System(os.path.join('poscars', 'conf.lmp'),
21+
type_map = ['O', 'H'])
22+
tmp_system.to('lammps/lmp', 'tmp.lmp')
23+
self.system = dpdata.System()
24+
self.system.from_fmt('tmp.lmp', fmt='lammps/lmp',
25+
type_map = ['O', 'H'])
1726

1827
if __name__ == '__main__':
1928
unittest.main()

0 commit comments

Comments
 (0)