-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_deepmd_alchemical.py
203 lines (169 loc) · 9.18 KB
/
test_deepmd_alchemical.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import time
import numpy as np
try:
import openmm as mm
from openmm import unit as u
from openmm.app import PDBFile, StateDataReporter, DCDReporter, Simulation
except:
import simtk.openmm as mm
from simtk import unit as u
from simtk.openmm.app import PDBFile, StateDataReporter, DCDReporter, Simulation
from OpenMMDeepmdPlugin import DeepPotentialModel
def test_deepmd_alchemical_reference(nsteps = 1000, time_step = 0.2, Lambda = 0.5, platform_name = "Reference", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_alchemical_output", temperature_std_tol = 25):
if not os.path.exists(output_temp_dir):
os.mkdir(output_temp_dir)
pdb_file = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "lw_256_test.pdb")
dp_model_file = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "water.pb")
output_dcd = os.path.join(output_temp_dir, f"lw_256_test.alchemical.reference.{Lambda}.dcd")
output_log = os.path.join(output_temp_dir, f"lw_256_test.alchemical.reference.{Lambda}.log")
# Set up the simulation parameters.
nsteps = nsteps
time_step = time_step # unit is femtosecond.
temperature = 300 # Kelvin
report_frequency = 100 # Save trajectory every report_frequency steps.
box = [19.807884, 0, 0, 0, 19.807884, 0, 0, 0, 19.807884]
box = [mm.Vec3(box[0], box[1], box[2]), mm.Vec3(box[3], box[4], box[5]), mm.Vec3(box[6], box[7], box[8])] * u.angstroms
liquid_water = PDBFile(pdb_file)
topology = liquid_water.topology
positions = liquid_water.getPositions()
# Set up the dp_system with the dp_model.
dp_model = DeepPotentialModel(dp_model_file, Lambda = Lambda)
dp_model.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
# By default, createSystem from dp_model will put all atoms in topology into the DP particles for dp_model.
dp_system = dp_model.createSystem(topology)
# Initial the other two dp_models for alchemical simulation.
dp_model_1 = DeepPotentialModel(dp_model_file, Lambda = 1 - Lambda)
dp_model_2 = DeepPotentialModel(dp_model_file, Lambda = 1 - Lambda)
dp_model_1.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
dp_model_2.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
# Split the system particles into two groups for alchemical simulation.
graph1_particles = []
graph2_particles = []
for atom in topology.atoms():
if int(atom.residue.id) == 1:
graph2_particles.append(atom.index)
else:
graph1_particles.append(atom.index)
dp_force_1 = dp_model_1.addParticlesToDPRegion(graph1_particles, topology)
dp_force_2 = dp_model_2.addParticlesToDPRegion(graph2_particles, topology)
# Add the two dp_forces to the dp_system.
dp_system.addForce(dp_force_1)
dp_system.addForce(dp_force_2)
integrator = mm.LangevinIntegrator(
temperature*u.kelvin, # Temperature of heat bath
1.0/u.picoseconds, # Friction coefficient
time_step*u.femtoseconds, # Time step
)
platform = mm.Platform.getPlatformByName(platform_name)
# Build up the simulation object.
sim = Simulation(topology, dp_system, integrator, platform)
sim.context.setPeriodicBoxVectors(box[0], box[1], box[2])
sim.context.setPositions(positions)
sim.context.setVelocitiesToTemperature(temperature * u.kelvin)
# Add state reporters
sim.reporters.append(DCDReporter(output_dcd, report_frequency, enforcePeriodicBox=False))
sim.reporters.append(
StateDataReporter(output_log, report_frequency, step=True, time=True, totalEnergy=True, kineticEnergy=True, potentialEnergy=True, temperature=True, progress=True,
remainingTime=True, speed=True, density=True,totalSteps=nsteps, separator='\t')
)
# Run dynamics
print("Running dynamics")
start_time = time.time()
sim.step(nsteps)
end_time = time.time()
cost_time = end_time - start_time
print("Running on %s platform, time cost: %.4f s"%(platform_name, cost_time))
# Fetch the temperature info from the log file.
temperature_trajectory = []
temperature_index = -4
with open(output_log, "r") as f:
log_content = f.readlines()
for ii , line in enumerate(log_content):
if ii == 0:
continue
tmp = line.split()
temperature_trajectory.append(float(tmp[temperature_index]))
temperature_trajectory = np.array(temperature_trajectory)
# Check the temperature fluctuations is smaller than temperature_std_tol, unit in kelvin.
assert(np.std(temperature_trajectory) < temperature_std_tol)
def test_deepmd_alchemical_cuda(nsteps = 1000, time_step = 0.2, Lambda = 0.5, platform_name = "CUDA", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_alchemical_output", temperature_std_tol = 25):
if not os.path.exists(output_temp_dir):
os.mkdir(output_temp_dir)
pdb_file = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "lw_256_test.pdb")
dp_model_file = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "water.pb")
output_dcd = os.path.join(output_temp_dir, f"lw_256_test.alchemical.cuda.{Lambda}.dcd")
output_log = os.path.join(output_temp_dir, f"lw_256_test.alchemical.cuda.{Lambda}.log")
# Set up the simulation parameters.
nsteps = nsteps
time_step = time_step # unit is femtosecond.
temperature = 300 # Kelvin
report_frequency = 100 # Save trajectory every report_frequency steps.
box = [19.807884, 0, 0, 0, 19.807884, 0, 0, 0, 19.807884]
box = [mm.Vec3(box[0], box[1], box[2]), mm.Vec3(box[3], box[4], box[5]), mm.Vec3(box[6], box[7], box[8])] * u.angstroms
liquid_water = PDBFile(pdb_file)
topology = liquid_water.topology
positions = liquid_water.getPositions()
# Set up the dp_system with the dp_model.
dp_model = DeepPotentialModel(dp_model_file, Lambda = Lambda)
dp_model.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
# By default, createSystem from dp_model will put all atoms in topology into the DP particles for dp_model.
dp_system = dp_model.createSystem(topology)
# Initial the other two dp_models for alchemical simulation.
dp_model_1 = DeepPotentialModel(dp_model_file, Lambda = 1 - Lambda)
dp_model_2 = DeepPotentialModel(dp_model_file, Lambda = 1 - Lambda)
dp_model_1.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
dp_model_2.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
# Split the system particles into two groups for alchemical simulation.
graph1_particles = []
graph2_particles = []
for atom in topology.atoms():
if int(atom.residue.id) == 1:
graph2_particles.append(atom.index)
else:
graph1_particles.append(atom.index)
dp_force_1 = dp_model_1.addParticlesToDPRegion(graph1_particles, topology)
dp_force_2 = dp_model_2.addParticlesToDPRegion(graph2_particles, topology)
# Add the two dp_forces to the dp_system.
dp_system.addForce(dp_force_1)
dp_system.addForce(dp_force_2)
integrator = mm.LangevinIntegrator(
temperature*u.kelvin, # Temperature of heat bath
1.0/u.picoseconds, # Friction coefficient
time_step*u.femtoseconds, # Time step
)
platform = mm.Platform.getPlatformByName(platform_name)
# Build up the simulation object.
sim = Simulation(topology, dp_system, integrator, platform)
sim.context.setPeriodicBoxVectors(box[0], box[1], box[2])
sim.context.setPositions(positions)
sim.context.setVelocitiesToTemperature(temperature * u.kelvin)
# Add state reporters
sim.reporters.append(DCDReporter(output_dcd, report_frequency, enforcePeriodicBox=False))
sim.reporters.append(
StateDataReporter(output_log, report_frequency, step=True, time=True, totalEnergy=True, kineticEnergy=True, potentialEnergy=True, temperature=True, progress=True,
remainingTime=True, speed=True, density=True,totalSteps=nsteps, separator='\t')
)
# Run dynamics
print("Running dynamics")
start_time = time.time()
sim.step(nsteps)
end_time = time.time()
cost_time = end_time - start_time
print("Running on %s platform, time cost: %.4f s"%(platform_name, cost_time))
# Fetch the temperature info from the log file.
temperature_trajectory = []
temperature_index = -4
with open(output_log, "r") as f:
log_content = f.readlines()
for ii , line in enumerate(log_content):
if ii == 0:
continue
tmp = line.split()
temperature_trajectory.append(float(tmp[temperature_index]))
temperature_trajectory = np.array(temperature_trajectory)
# Check the temperature fluctuations is smaller than temperature_std_tol, unit in kelvin.
assert(np.std(temperature_trajectory) < temperature_std_tol)
if __name__ == "__main__":
test_deepmd_alchemical_reference()
test_deepmd_alchemical_cuda()