forked from Roy-Kid/ADMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
137 lines (101 loc) · 4.14 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from python.neiV2 import construct_nblist as cnbl2
import numpy as np
import jax.numpy as jnp
from python.parser import assemble_covalent, init_residues, read_pdb, read_xml
from python.pme import pme_real, generate_construct_localframes, rot_local2global
import jax
jax.profiler.start_trace("/tmp/tensorboard")
pdb = 'tests/samples/waterdimer_aligned.pdb'
xml = 'tests/samples/mpidwater.xml'
# return a dict with raw data from pdb
pdbinfo = read_pdb(pdb)
serials = pdbinfo['serials']
names = pdbinfo['names']
resNames = pdbinfo['resNames']
resSeqs = pdbinfo['resSeqs']
positions = pdbinfo['positions']
charges = pdbinfo['charges']
box = pdbinfo['box'] # a, b, c, α, β, γ
lx = box[0]
ly = box[1]
lz = box[2]
mScales = np.array([0.0, 0.0, 0.0, 1.0])
pScales = np.array([0.0, 0.0, 0.0, 1.0])
dScales = np.array([0.0, 0.0, 0.0, 1.0])
box = np.eye(3)*np.array([lx, ly, lz])
rc = 8 # in Angstrom
ethresh = 1e-4
natoms = len(serials)
# Here are the templates[dict] from xml.
# atomTemplates are per-atom info,
# residueTemplates contain what atoms in the residue and how they connect.
atomTemplate, residueTemplate = read_xml(xml)
# then we use the template to init atom and residue objects
# TODO: an atomManager and residueManager
atomDicts, residueDicts = init_residues(serials, names, resNames, resSeqs, positions, charges, atomTemplate, residueTemplate)
Q = np.vstack(
[(atom.c0, atom.dX*10, atom.dY*10, atom.dZ*10, atom.qXX*300, atom.qYY*300, atom.qZZ*300, atom.qXY*300, atom.qXZ*300, atom.qYZ*300) for atom in atomDicts.values()]
)
Qlocal = convert_cart2harm(Q, 2)
axis_type = np.array(
[atom.axisType for atom in atomDicts.values()]
)
axis_indices = np.vstack(
[atom.axis_indices for atom in atomDicts.values()]
)
# covalent_map is simply as N*N matrix now
# remove sparse matrix
covalent_map = assemble_covalent(residueDicts, natoms)
## TODO: setup kappa
kappa = 0.328532611
import scipy
from scipy.stats import special_ortho_group
scipy.random.seed(1000)
R1 = special_ortho_group.rvs(3)
R2 = special_ortho_group.rvs(3)
positions[0:3] = positions[0:3].dot(R1)
positions[3:6] = positions[3:6].dot(R2)
positions[3:] += np.array([3.0, 0.0, 0.0])
e = pme_real(positions, box, rc, Qlocal, generate_construct_localframes(axis_type, axis_indices), kappa, covalent_map, mScales, pScales, dScales)
print(e)
print('auto diff: ')
positions = jnp.asarray(positions)
box = jnp.asarray(box)
dreal = jax.grad(pme_real, argnums=0)
f = dreal(positions, box, rc, Qlocal, generate_construct_localframes(axis_type, axis_indices), kappa, covalent_map, mScales, pScales, dScales)
print(f)
# finite differences
# findiff = np.empty((6, 3))
# eps = 1e-4
# delta = np.zeros((6,3))
# for i in range(6):
# for j in range(3):
# delta[i][j] = eps
# findiff[i][j] = (pme_real(positions+delta/2, box, rc, Qlocal, generate_construct_localframes(axis_type, axis_indices), kappa, covalent_map, mScales, pScales, dScales) - pme_real(positions-delta/2, box, rc, Qlocal, generate_construct_localframes(axis_type, axis_indices), kappa, covalent_map, mScales, pScales, dScales))/eps
# delta[i][j] = 0
# print('partial diff')
# print(findiff)
jax.profiler.stop_trace()
neighList = construct_nblist(positions, box, rc)
import time, jax
start = time.time()
nbs, distances2, dr_vecs = jax.jacfwd(cnbl2)(positions, box, rc)
end = time.time()
print(end - start)
local_frames = generate_construct_localframes(axis_type, axis_indices)(positions, box)
Qglobal = rot_local2global(Qlocal, local_frames, 2)
e = pme_real(positions, Qglobal, kappa, covalent_map, mScales, pScales, dScales, neighList)
print(e)
# from openmm.app import *
# from openmm import *
# from openmm.unit import *
# import mpidplugin
# pdb = PDBFile(pdb)
# forcefield = ForceField(xml)
# system = forcefield.createSystem(pdb.topology, nonbondedMethod=LJPME, nonbondedCutoff=8*angstrom, constraints=HBonds, defaultTholeWidth=8)
# integrator = VerletIntegrator(1e-10*femtoseconds)
# simulation = Simulation(pdb.topology, system, integrator)
# context = simulation.context
# context.setPositions(positions*angstrom)
# state = context.getState(getEnergy=True, getPositions=True)
# Etot = state.getPotentialEnergy().value_in_unit(kilojoules_per_mole)