-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_deepmd_nve.py
143 lines (118 loc) · 6.09 KB
/
test_deepmd_nve.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
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_nve_reference(nsteps = 1000, time_step = 0.2, platform_name = "Reference", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_nve_output", energy_std_tol = 0.0005 ):
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.nve.reference.dcd")
output_log = os.path.join(output_temp_dir, "lw_256_test.nve.reference.log")
# Set up the simulation parameters.
time_step = time_step # unit is femtosecond.
report_frequency = 100
nsteps = nsteps
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()
num_atoms = topology.getNumAtoms()
# 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)
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)
# 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.
total_energy = []
tot_energy_index = -5
with open(output_log, "r") as f:
log_content = f.readlines()
for ii , line in enumerate(log_content):
if ii == 0:
continue
temp = line.split()
total_energy.append(float(temp[tot_energy_index]))
total_energy = np.array(total_energy)
# Check the total energy fluctuations over # of atoms is smaller than energy_std_tol, unit in kJ/mol.
assert(np.std(total_energy) / num_atoms < energy_std_tol)
def test_deepmd_nve_cuda(nsteps = 1000, time_step = 0.2, platform_name = "CUDA", output_temp_dir = "/tmp/openmm_deepmd_plugin_test_nve_output", energy_std_tol = 0.0005 ):
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.nve.cuda.dcd")
output_log = os.path.join(output_temp_dir, "lw_256_test.nve.cuda.log")
# Set up the simulation parameters.
time_step = time_step # unit is femtosecond.
report_frequency = 100
nsteps = nsteps
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()
num_atoms = topology.getNumAtoms()
# 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)
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)
# 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.
total_energy = []
tot_energy_index = -5
with open(output_log, "r") as f:
log_content = f.readlines()
for ii , line in enumerate(log_content):
if ii == 0:
continue
temp = line.split()
total_energy.append(float(temp[tot_energy_index]))
total_energy = np.array(total_energy)
# Check the total energy fluctuations over # of atoms is smaller than energy_std_tol, unit in kJ/mol.
assert(np.std(total_energy) / num_atoms < energy_std_tol)