-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_deepmd_npt.py
147 lines (122 loc) · 6.34 KB
/
test_deepmd_npt.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
import os
import numpy as np
import time
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_npt_reference(nsteps = 1000, time_step = 0.2, platform_name = "Reference", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_npt_output", density_std_tol = 0.2 ):
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 = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "water.pb")
output_dcd = os.path.join(output_temp_dir, "lw_256_test.npt.reference.dcd")
output_log = os.path.join(output_temp_dir, "lw_256_test.npt.reference.log")
# Set up the simulation parameters.
nsteps = nsteps
time_step = time_step # unit is femtosecond.
temperature = 300 # Kelvin
report_frequency = 100
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)
dp_model.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
dp_system = dp_model.createSystem(topology)
dp_system.addForce(mm.MonteCarloBarostat(1*u.atmospheres, temperature*u.kelvin))
integrator = mm.VerletIntegrator(time_step*u.femtoseconds)
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 total energy from the log file.
density_trajectory = []
density_index = -3
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()
density_trajectory.append(float(tmp[density_index]))
density_trajectory = np.array(density_trajectory)
# Check the density fluctuations is smaller than density_std_tol, unit in g/mL.
assert(np.std(density_trajectory) < density_std_tol)
def test_deepmd_npt_cuda(nsteps = 1000, time_step = 0.2, platform_name = "CUDA", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_npt_output", density_std_tol = 0.2 ):
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 = os.path.join(os.path.dirname(__file__), "../OpenMMDeepmdPlugin/data", "water.pb")
output_dcd = os.path.join(output_temp_dir, "lw_256_test.npt.cuda.dcd")
output_log = os.path.join(output_temp_dir, "lw_256_test.npt.cuda.log")
# Set up the simulation parameters.
nsteps = nsteps
time_step = time_step # unit is femtosecond.
temperature = 300 # Kelvin
report_frequency = 100
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)
dp_model.setUnitTransformCoefficients(10.0, 964.8792534459, 96.48792534459)
dp_system = dp_model.createSystem(topology)
dp_system.addForce(mm.MonteCarloBarostat(1*u.atmospheres, temperature*u.kelvin))
integrator = mm.VerletIntegrator(time_step*u.femtoseconds)
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 total energy from the log file.
density_trajectory = []
density_index = -3
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()
density_trajectory.append(float(tmp[density_index]))
density_trajectory = np.array(density_trajectory)
# Check the density fluctuations is smaller than density_std_tol, unit in g/mL.
assert(np.std(density_trajectory) < density_std_tol)