Skip to content

Commit 1e0908b

Browse files
authored
Merge pull request #87 from njzjz/amber
support reading Amber MD trajectory
2 parents 42be217 + d963605 commit 1e0908b

File tree

10 files changed

+4217
-2
lines changed

10 files changed

+4217
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ xyz_multi_systems.to_deepmd_raw('./my_deepmd_data/')
110110
| PWmat | atom.config | False | False | System | 'pwmat/atom.config' |
111111
| PWmat | movement | True | True | LabeledSystem | 'pwmat/movement' |
112112
| PWmat | OUT.MLMD | True | True | LabeledSystem | 'pwmat/out.mlmd' |
113+
| Amber | multi | True | True | LabeledSystem | 'amber/md' |
113114
## Access data
114115
These properties stored in `System` and `LabeledSystem` can be accessed by operator `[]` with the key of the property supplied, for example
115116
```python

dpdata/amber/md.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import re
2+
from scipy.io import netcdf
3+
import numpy as np
4+
5+
kcalmol2eV= 0.04336410390059322
6+
7+
energy_convert = kcalmol2eV
8+
force_convert = energy_convert
9+
10+
11+
def read_amber_traj(parm7_file, nc_file, mdfrc_file, mden_file):
12+
"""The amber trajectory includes:
13+
* nc, NetCDF format, stores coordinates
14+
* mdfrc, NetCDF format, stores forces
15+
* mden, text format, stores energies
16+
* parm7, text format, stores types
17+
"""
18+
19+
flag=False
20+
amber_types = []
21+
with open(parm7_file) as f:
22+
for line in f:
23+
if line.startswith("%FLAG"):
24+
flag = line.startswith("%FLAG AMBER_ATOM_TYPE")
25+
elif flag:
26+
if line.startswith("%FORMAT"):
27+
fmt = re.findall(r'\d+', line)
28+
fmt0 = int(fmt[0])
29+
fmt1 = int(fmt[1])
30+
else:
31+
for ii in range(fmt0):
32+
start_index = ii * fmt1
33+
end_index = (ii + 1) * fmt1
34+
if end_index >= len(line):
35+
continue
36+
amber_types.append(line[start_index:end_index].strip())
37+
38+
with netcdf.netcdf_file(nc_file, 'r') as f:
39+
coords = np.array(f.variables["coordinates"][:])
40+
cell_lengths = np.array(f.variables["cell_lengths"][:])
41+
cell_angles = np.array(f.variables["cell_angles"][:])
42+
if np.all(cell_angles > 89.99 ) and np.all(cell_angles < 90.01):
43+
# only support 90
44+
# TODO: support other angles
45+
shape = cell_lengths.shape
46+
cells = np.zeros((shape[0], 3, 3))
47+
for ii in range(3):
48+
cells[:, ii, ii] = cell_lengths[:, ii]
49+
else:
50+
raise RuntimeError("Unsupported cells")
51+
52+
with netcdf.netcdf_file(mdfrc_file, 'r') as f:
53+
forces = np.array(f.variables["forces"][:])
54+
55+
# energy
56+
energies = []
57+
with open(mden_file) as f:
58+
for line in f:
59+
if line.startswith("L6"):
60+
s = line.split()
61+
if s[2] != "E_pot":
62+
energies.append(float(s[2]))
63+
64+
atom_names, atom_types, atom_numbs = np.unique(amber_types, return_inverse=True, return_counts=True)
65+
66+
data = {}
67+
data['atom_names'] = list(atom_names)
68+
data['atom_numbs'] = list(atom_numbs)
69+
data['atom_types'] = atom_types
70+
data['forces'] = forces * force_convert
71+
data['energies'] = np.array(energies) * energy_convert
72+
data['coords'] = coords
73+
data['cells'] = cells
74+
data['orig'] = np.array([0, 0, 0])
75+
return data
76+

dpdata/system.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import dpdata.siesta.aiMD_output
1717
import dpdata.md.pbc
1818
import dpdata.gaussian.log
19+
import dpdata.amber.md
1920
import dpdata.cp2k.output
2021
from dpdata.cp2k.output import Cp2kSystems
2122
import dpdata.pwmat.movement
@@ -1171,6 +1172,19 @@ def from_gaussian_log(self, file_name, md=False):
11711172
def from_gaussian_md(self, file_name):
11721173
self.from_gaussian_log(file_name, md=True)
11731174

1175+
@register_from_funcs.register_funcs('amber/md')
1176+
def from_amber_md(self, file_name=None, parm7_file=None, nc_file=None, mdfrc_file=None, mden_file=None):
1177+
# assume the prefix is the same if the spefic name is not given
1178+
if parm7_file is None:
1179+
parm7_file = file_name + ".parm7"
1180+
if nc_file is None:
1181+
nc_file = file_name + ".nc"
1182+
if mdfrc_file is None:
1183+
mdfrc_file = file_name + ".mdfrc"
1184+
if mden_file is None:
1185+
mden_file = file_name + ".mden"
1186+
self.data = dpdata.amber.md.read_amber_traj(parm7_file, nc_file, mdfrc_file, mden_file)
1187+
11741188
@register_from_funcs.register_funcs('cp2k/output')
11751189
def from_cp2k_output(self, file_name) :
11761190
self.data['atom_names'], \

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
readme = f.read()
1313

1414
# install_requires = ['xml']
15-
install_requires=['numpy>=1.14.3', 'monty']
15+
install_requires=['numpy>=1.14.3', 'monty', 'scipy']
1616

1717
setuptools.setup(
1818
name="dpdata",
@@ -24,7 +24,7 @@
2424
long_description=readme,
2525
long_description_content_type="text/markdown",
2626
url="https://github.com/deepmodeling/dpdata",
27-
packages=['dpdata', 'dpdata/vasp', 'dpdata/lammps', 'dpdata/md', 'dpdata/deepmd', 'dpdata/qe', 'dpdata/siesta', 'dpdata/gaussian', 'dpdata/cp2k','dpdata/xyz','dpdata/pwmat'],
27+
packages=['dpdata', 'dpdata/vasp', 'dpdata/lammps', 'dpdata/md', 'dpdata/deepmd', 'dpdata/qe', 'dpdata/siesta', 'dpdata/gaussian', 'dpdata/cp2k','dpdata/xyz','dpdata/pwmat', 'dpdata/amber'],
2828
package_data={'dpdata':['*.json']},
2929
classifiers=[
3030
"Programming Language :: Python :: 3.6",

tests/amber/.02_Heat.mden.swp

16 KB
Binary file not shown.

tests/amber/02_Heat.mden

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
L0 Nsteps time(ps) Etot EKinetic
2+
L1 Temp T_solute T_solv Pres_scal_solu
3+
L2 Pres_scal_solv BoxX BoxY BoxZ
4+
L3 volume pres_X pres_Y pres_Z
5+
L4 Pressure EKCoM_x EKCoM_y EKCoM_z
6+
L5 EKComTot VIRIAL_x VIRIAL_y VIRIAL_z
7+
L6 VIRIAL_tot E_pot E_vdw E_el
8+
L7 E_hbon E_bon E_angle E_dih
9+
L8 E_14vdw E_14el E_const E_pol
10+
L9 AV_permMoment AV_indMoment AV_totMoment Density dV/dlambda
11+
L0 100 0.2000000000E+00 -.6889864713E+04 0.5501673987E+02
12+
L1 0.1444218747E+02 0.1444218747E+02 -.6944881453E+10 0.1000000000E+01
13+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
14+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
15+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
16+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
17+
L6 0.0000000000E+00 -.6944881453E+04 0.1326035671E+04 -.8329513895E+04
18+
L7 0.0000000000E+00 0.8178180461E+00 0.1397206322E+01 0.8974699345E+01
19+
L8 0.2549638338E+01 0.4485740872E+02 0.0000000000E+00 0.0000000000E+00
20+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
21+
L0 200 0.4000000000E+00 -.6804856514E+04 0.1185112307E+03
22+
L1 0.3110982976E+02 0.3110982976E+02 -.6923367744E+10 0.1000000000E+01
23+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
24+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
25+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
26+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
27+
L6 0.0000000000E+00 -.6923367744E+04 0.1459095814E+04 -.8441187308E+04
28+
L7 0.0000000000E+00 0.7227087622E+00 0.1557057969E+01 0.8856560757E+01
29+
L8 0.2672013673E+01 0.4491540766E+02 0.0000000000E+00 0.0000000000E+00
30+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
31+
L0 300 0.6000000000E+00 -.6676971553E+04 0.1901470552E+03
32+
L1 0.4991461552E+02 0.4991461552E+02 -.6867118608E+10 0.1000000000E+01
33+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
34+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
35+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
36+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
37+
L6 0.0000000000E+00 -.6867118608E+04 0.1394343109E+04 -.8321376064E+04
38+
L7 0.0000000000E+00 0.9023216702E+00 0.2152790620E+01 0.9338452380E+01
39+
L8 0.2744690750E+01 0.4477609159E+02 0.0000000000E+00 0.0000000000E+00
40+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
41+
L0 400 0.8000000000E+00 -.6501527215E+04 0.2902963534E+03
42+
L1 0.7620434012E+02 0.7620434012E+02 -.6791823568E+10 0.1000000000E+01
43+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
44+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
45+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
46+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
47+
L6 0.0000000000E+00 -.6791823568E+04 0.1394536681E+04 -.8248508025E+04
48+
L7 0.0000000000E+00 0.1423118838E+01 0.2362232658E+01 0.9477806977E+01
49+
L8 0.2646001001E+01 0.4623861664E+02 0.0000000000E+00 0.0000000000E+00
50+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
51+
L0 500 0.1000000000E+01 -.6307854825E+04 0.3996526997E+03
52+
L1 0.1049109639E+03 0.1049109639E+03 -.6707507525E+10 0.1000000000E+01
53+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
54+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
55+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
56+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
57+
L6 0.0000000000E+00 -.6707507525E+04 0.1321289178E+04 -.8093797008E+04
58+
L7 0.0000000000E+00 0.1834943556E+01 0.3775259952E+01 0.1120783525E+02
59+
L8 0.2412889981E+01 0.4576937612E+02 0.0000000000E+00 0.0000000000E+00
60+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
61+
L0 600 0.1200000000E+01 -.6090645607E+04 0.5116326930E+03
62+
L1 0.1343063090E+03 0.1343063090E+03 -.6602278300E+10 0.1000000000E+01
63+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
64+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
65+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
66+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
67+
L6 0.0000000000E+00 -.6602278300E+04 0.1235129417E+04 -.7905740893E+04
68+
L7 0.0000000000E+00 0.2452827391E+01 0.4100464667E+01 0.1030161719E+02
69+
L8 0.2981055535E+01 0.4849721063E+02 0.0000000000E+00 0.0000000000E+00
70+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
71+
L0 700 0.1400000000E+01 -.5890364217E+04 0.5979388269E+03
72+
L1 0.1569621293E+03 0.1569621293E+03 -.6488303044E+10 0.1000000000E+01
73+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
74+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
75+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
76+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
77+
L6 0.0000000000E+00 -.6488303044E+04 0.1214027552E+04 -.7766128315E+04
78+
L7 0.0000000000E+00 0.1875932017E+01 0.3087127144E+01 0.1046702710E+02
79+
L8 0.2962561472E+01 0.4540507117E+02 0.0000000000E+00 0.0000000000E+00
80+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
81+
L0 800 0.1600000000E+01 -.5679921606E+04 0.7224690007E+03
82+
L1 0.1896519636E+03 0.1896519636E+03 -.6402390606E+10 0.1000000000E+01
83+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
84+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
85+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
86+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
87+
L6 0.0000000000E+00 -.6402390606E+04 0.1171410199E+04 -.7642388600E+04
88+
L7 0.0000000000E+00 0.2095090217E+01 0.8362214033E+01 0.1030283081E+02
89+
L8 0.1809646670E+01 0.4601801343E+02 0.0000000000E+00 0.0000000000E+00
90+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
91+
L0 900 0.1800000000E+01 -.5406114850E+04 0.8259401985E+03
92+
L1 0.2168137046E+03 0.2168137046E+03 -.6232055048E+10 0.1000000000E+01
93+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
94+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
95+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
96+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
97+
L6 0.0000000000E+00 -.6232055048E+04 0.1063851607E+04 -.7361918496E+04
98+
L7 0.0000000000E+00 0.5328272517E+01 0.5506635572E+01 0.1117501809E+02
99+
L8 0.2589644661E+01 0.4141227007E+02 0.0000000000E+00 0.0000000000E+00
100+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
101+
L0 1000 0.2000000000E+01 -.5209309012E+04 0.9327702649E+03
102+
L1 0.2448571665E+03 0.2448571665E+03 -.6142079277E+10 0.1000000000E+01
103+
L2 0.1000000000E+01 0.3139785600E+02 0.3410004500E+02 0.2927296600E+02
104+
L3 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
105+
L4 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
106+
L5 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00
107+
L6 0.0000000000E+00 -.6142079277E+04 0.1040039386E+04 -.7246666057E+04
108+
L7 0.0000000000E+00 0.2546276737E+01 0.9090968633E+01 0.1025112603E+02
109+
L8 0.2333931169E+01 0.4032509197E+02 0.0000000000E+00 0.0000000000E+00
110+
L9 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00

tests/amber/02_Heat.mdfrc

225 KB
Binary file not shown.

tests/amber/02_Heat.nc

225 KB
Binary file not shown.

0 commit comments

Comments
 (0)