Skip to content

Commit 94022da

Browse files
authored
Merge pull request #81 from deepmodeling/devel
Devel
2 parents ec284a2 + 38fc532 commit 94022da

13 files changed

+320
-112
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/qe/traj.py

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python3
22

33
import numpy as np
4-
import dpdata
4+
import dpdata,warnings
55

66
ry2ev = 13.605693009
77
hartree2ev = 27.211386018
@@ -36,8 +36,10 @@ def convert_celldm(ibrav, celldm) :
3636
return celldm[0] * 0.5 * np.array([[1,1,1], [-1,1,1], [-1,-1,1]])
3737
elif ibrav == -3 :
3838
return celldm[0] * 0.5 * np.array([[-1,1,1], [1,-1,1], [1,1,-1]])
39-
else :
40-
raise RuntimeError('unsupported ibrav ' + str(ibrav))
39+
else :
40+
warnings.warn('unsupported ibrav ' + str(ibrav) + ' if no .cel file, the cell convertion may be wrong. ')
41+
return np.eye(3)
42+
#raise RuntimeError('unsupported ibrav ' + str(ibrav))
4143

4244
def load_cell_parameters(lines) :
4345
blk = load_block(lines, 'CELL_PARAMETERS', 3)
@@ -98,15 +100,16 @@ def _load_pos_block(fp, natoms) :
98100
head = fp.readline()
99101
if not head:
100102
# print('get None')
101-
return None
103+
return None, None
102104
else :
105+
ss = head.split()[0]
103106
blk = []
104107
for ii in range(natoms) :
105108
newline = fp.readline()
106109
if not newline :
107-
return None
110+
return None, None
108111
blk.append([float(jj) for jj in newline.split()])
109-
return blk
112+
return blk, ss
110113

111114

112115
def load_data(fname,
@@ -115,18 +118,20 @@ def load_data(fname,
115118
step = 1,
116119
convert = 1.) :
117120
coords = []
121+
steps = []
118122
cc = 0
119123
with open(fname) as fp:
120124
while True:
121-
blk = _load_pos_block(fp, natoms)
125+
blk, ss = _load_pos_block(fp, natoms)
122126
if blk == None :
123127
break
124128
else :
125129
if cc >= begin and (cc - begin) % step == 0 :
126130
coords.append(blk)
131+
steps.append(ss)
127132
cc += 1
128133
coords= convert * np.array(coords)
129-
return coords
134+
return coords, steps
130135

131136

132137
# def load_pos(fname, natoms) :
@@ -145,14 +150,19 @@ def load_data(fname,
145150

146151
def load_energy(fname, begin = 0, step = 1) :
147152
data = np.loadtxt(fname)
153+
steps = []
154+
for ii in data[begin::step,0]:
155+
steps.append('%d'%ii)
148156
with open(fname) as fp:
149-
line = fp.readline()
150-
if line :
151-
nw = len(line.split())
152-
else :
153-
return None
157+
while True:
158+
line = fp.readline()
159+
if not line :
160+
return None
161+
if line.split()[0][0] != '#':
162+
nw = len(line.split())
163+
break
154164
data = np.reshape(data, [-1, nw])
155-
return energy_convert * data[begin::step,5]
165+
return energy_convert * data[begin::step,5], steps
156166

157167

158168
# def load_force(fname, natoms) :
@@ -176,35 +186,48 @@ def to_system_data(input_name, prefix, begin = 0, step = 1) :
176186
data['atom_types'], \
177187
cell \
178188
= load_param_file(input_name)
179-
data['coords'] \
189+
data['coords'], csteps\
180190
= load_data(prefix + '.pos',
181191
np.sum(data['atom_numbs']),
182192
begin = begin,
183193
step = step,
184194
convert = length_convert)
185195
data['orig'] = np.zeros(3)
186-
data['cells'] = np.tile(cell, (data['coords'].shape[0], 1, 1))
187-
return data
196+
try :
197+
data['cells'], tmp_steps \
198+
= load_data(prefix + '.cel',
199+
3,
200+
begin = begin,
201+
step = step,
202+
convert = length_convert)
203+
assert(csteps == tmp_steps), "the step key between files are not consistent"
204+
except FileNotFoundError :
205+
data['cells'] = np.tile(cell, (data['coords'].shape[0], 1, 1))
206+
return data, csteps
188207

189208

190209
def to_system_label(input_name, prefix, begin = 0, step = 1) :
191210
atom_names, atom_numbs, atom_types, cell = load_param_file(input_name)
192-
energy = load_energy(prefix + '.evp',
193-
begin = begin,
194-
step = step)
195-
force = load_data(prefix + '.for',
196-
np.sum(atom_numbs),
197-
begin = begin,
198-
step = step,
199-
convert = force_convert)
200-
return energy, force
211+
energy, esteps = load_energy(prefix + '.evp',
212+
begin = begin,
213+
step = step)
214+
force, fsteps = load_data(prefix + '.for',
215+
np.sum(atom_numbs),
216+
begin = begin,
217+
step = step,
218+
convert = force_convert)
219+
assert(esteps == fsteps), "the step key between files are not consistent "
220+
return energy, force, esteps
201221

202222

203223
if __name__ == '__main__':
204-
atom_names, atom_numbs, atom_types, cell = load_param_file('oh-md.in')
205-
coords = load_pos('oh-md.pos', np.sum(atom_numbs))
224+
prefix='nacl'
225+
atom_names, atom_numbs, atom_types, cell = load_param_file(prefix+'.in')
226+
coords = load_data(prefix+'.pos', np.sum(atom_numbs))
227+
cells = load_data(prefix+'.cel', 3)
206228
print(atom_names)
207229
print(atom_numbs)
208230
print(atom_types)
209-
print(cell)
231+
print(cells)
210232
print(coords.shape)
233+
print(cells.shape)

0 commit comments

Comments
 (0)