-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.py
403 lines (349 loc) · 12.4 KB
/
project.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""Define the project's workflow logic and operation functions.
Execute this script directly from the command line, to view your project's
status, execute operations and submit them to a cluster. See also:
$ python src/project.py --help
"""
import signac
import pickle
from flow import FlowProject, directives
from flow.environment import DefaultSlurmEnvironment
import os
from unyt import Unit
class PPSCG(FlowProject):
pass
class Borah(DefaultSlurmEnvironment):
hostname_pattern = "borah"
template = "borah.sh"
@classmethod
def add_args(cls, parser):
parser.add_argument(
"--partition",
default="shortgpu-v100",
help="Specify the partition to submit to."
)
class Fry(DefaultSlurmEnvironment):
hostname_pattern = "fry"
template = "fry.sh"
@classmethod
def add_args(cls, parser):
parser.add_argument(
"--partition",
default="v100," "batch",
help="Specify the partition to submit to."
)
@PPSCG.label
def system_built(job):
return job.isfile("init_frame.gsd")
@PPSCG.label
def initial_run_done(job):
return job.doc.runs > 0
@PPSCG.label
def equilibrated(job):
return job.doc.equilibrated
@PPSCG.label
def sampled(job):
return job.doc.sampled
@PPSCG.label
def production_done(job):
return job.isfile("production-restart.gsd")
def get_ref_values(job):
"""These are the reference values for PPS."""
ref_length = 0.3438 * Unit("nm")
ref_mass = 32.06 * Unit("amu")
ref_energy = 1.065 * Unit("kJ/mol")
ref_values_dict = {
"length": ref_length,
"mass": ref_mass,
"energy": ref_energy
}
job.doc.ref_length = ref_length.value
job.doc.ref_length_units = "nm"
job.doc.ref_energy = ref_energy.value
job.doc.ref_energy_units = "kJ/mol"
job.doc.ref_mass = ref_mass.value
job.doc.ref_mass_units = "amu"
return ref_values_dict
def make_cg_system_bulk(job):
from flowermd.base import Pack
from flowermd.library import PPS
job.doc.n_particles = int(job.doc.num_mols * job.doc.lengths)
chains = PPS(num_mols=job.doc.num_mols, lengths=job.doc.lengths)
chains.coarse_grain(beads={"A": "c1cc(S)ccc1"})
ref_values = get_ref_values(job)
system = Pack(
molecules=chains,
density=job.sp.density,
base_units=ref_values
)
return system
def make_cg_system_lattice(job):
"""Make an initial lattice of long polymer chains"""
import math
from flowermd.base import System
from flowermd.library import PPS
from flowermd.utils import get_target_box_mass_density
import numpy as np
import mbuild as mb
class Lattice(System):
def __init__(self, molecules, base_units=dict()):
super(Lattice, self).__init__(
molecules=molecules, base_units=base_units
)
def _build_system(self):
n_per = math.ceil(np.sqrt(self.n_molecules))
system = mb.Compound()
sep = 4
count = 0
layer_num = 0
for i in range(self.n_molecules // n_per):
layer = mb.Compound()
for j in range(n_per):
comp = self.all_molecules[count]
comp.translate(np.array([sep * j, 0, 0]))
layer.add(comp)
count += 1
layer.translate(np.array([0, sep * i, 0]))
system.add(layer)
layer_num += 1
# Make last incomplete layer
if count != self.n_molecules:
last_layer = mb.Compound()
for j, comp in enumerate(self.all_molecules[count:]):
comp.translate(np.array([sep * j, 0, 0]))
last_layer.add(comp)
last_layer.translate(np.array([0, sep * layer_num , 0]))
system.add(last_layer)
box = system.get_boundingbox()
system.box = mb.box.Box(
np.array([box.lengths[0]+sep, box.lengths[1]+sep, box.lengths[2]+sep])
)
system.translate_to(
(system.box.Lx / 2, system.box.Ly / 2, system.box.Lz / 2)
)
return system
job.doc.n_particles = int(job.doc.num_mols * job.doc.lengths)
chains = PPS(num_mols=job.doc.num_mols, lengths=job.doc.lengths)
chains.coarse_grain(beads={"A": "c1cc(S)ccc1"})
ref_values = get_ref_values(job)
system = Lattice(molecules=chains, base_units=ref_values)
job.doc.system_mass_g = system.mass.to("g").value
return system
def get_ff(job):
""""""
msibi_project = signac.get_project(job.sp.msibi_project)
msibi_job = msibi_project.open_job(id=job.sp.msibi_job)
with open(msibi_job.fn("pps-msibi.pickle"), "rb") as f:
hoomd_ff = pickle.load(f)
return hoomd_ff
@PPSCG.post(system_built)
@PPSCG.operation(
directives={"ngpu": 0, "ncpu": 1, "executable": "python -u"}, name="build"
)
def build(job):
"""Run the initial configuration builder on CPU"""
with job:
print("------------------------------------")
print("JOB ID NUMBER:")
print(job.id)
print("------------------------------------")
print("Building initial frame.")
system = make_cg_system_lattice(job)
system.to_gsd(job.fn("init_frame.gsd"))
print("Finished.")
@PPSCG.pre(system_built)
@PPSCG.post(initial_run_done)
@PPSCG.operation(
directives={"ngpu": 1, "ncpu": 1, "executable": "python -u"}, name="run"
)
def run(job):
"""Run initial single-chain simulation."""
import unyt
from unyt import Unit
import flowermd
from flowermd.base import Simulation
from flowermd.utils import get_target_box_mass_density
import hoomd
with job:
print("------------------------------------")
print("JOB ID NUMBER:")
print(job.id)
print("------------------------------------")
hoomd_ff = get_ff(job)
for force in hoomd_ff:
if isinstance(force, hoomd.md.bond.Table):
if job.sp.harmonic_bonds:
print("Replacing bond table potential with harmonic")
hoomd_ff.remove(force)
harmonic_bond = hoomd.md.bond.Harmonic()
harmonic_bond.params["A-A"] = dict(k=1777.6, r0=1.4226)
hoomd_ff.append(harmonic_bond)
else:
pass
# Store reference units and values
ref_values_dict = get_ref_values(job)
# Set up Simulation obj
gsd_path = job.fn(f"trajectory{job.doc.runs}.gsd")
log_path = job.fn(f"log{job.doc.runs}.txt")
sim = Simulation(
initial_state=job.fn("init_frame.gsd"),
forcefield=hoomd_ff,
reference_values=ref_values_dict,
dt=job.sp.dt,
gsd_write_freq=job.sp.gsd_write_freq,
gsd_file_name=gsd_path,
log_write_freq=job.sp.log_write_freq,
log_file_name=log_path,
seed=job.sp.sim_seed,
)
sim.pickle_forcefield(job.fn("forcefield.pickle"))
# Store more unit information in job doc
tau_kT = job.sp.dt * job.sp.tau_kT
job.doc.tau_kT = tau_kT
job.doc.real_time_step = sim.real_timestep.to("fs").value
job.doc.real_time_units = "fs"
target_box = get_target_box_mass_density(
mass=job.doc.system_mass_g * Unit("g"),
density=job.sp.density * Unit("g/cm**3")
)
job.doc.target_box = target_box.value
shrink_kT_ramp = sim.temperature_ramp(
n_steps=job.sp.n_shrink_steps,
kT_start=job.sp.shrink_kT,
kT_final=job.sp.kT
)
sim.run_update_volume(
final_box_lengths=target_box,
n_steps=job.sp.n_shrink_steps,
period=job.sp.shrink_period,
tau_kt=tau_kT,
kT=shrink_kT_ramp
)
sim.save_restart_gsd(job.fn("shrink_restart.gsd"))
print("Shrinking simulation finished...")
sim.run_NVT(n_steps=job.sp.n_equil_steps, kT=job.sp.kT, tau_kt=tau_kT)
sim.save_restart_gsd(job.fn("restart.gsd"))
job.doc.runs = 1
print("Simulation finished.")
@PPSCG.pre(initial_run_done)
@PPSCG.post(equilibrated)
@PPSCG.operation(
directives={"ngpu": 1, "ncpu": 1, "executable": "python -u"},
name="run-longer"
)
def run_longer(job):
import unyt
from unyt import Unit
import flowermd
from flowermd.base import Simulation
import hoomd
with job:
print("------------------------------------")
print("JOB ID NUMBER:")
print(job.id)
print("------------------------------------")
print("Restarting and continuing simulation...")
with open(job.fn("forcefield.pickle"), "rb") as f:
hoomd_ff = pickle.load(f)
gsd_path = job.fn(f"trajectory{job.doc.runs}.gsd")
log_path = job.fn(f"log{job.doc.runs}.txt")
ref_values = get_ref_values(job)
sim = Simulation(
initial_state=job.fn("restart.gsd"),
forcefield=hoomd_ff,
reference_values=ref_values,
dt=job.sp.dt,
gsd_write_freq=job.sp.gsd_write_freq,
gsd_file_name=gsd_path,
log_write_freq=job.sp.log_write_freq,
log_file_name=log_path,
seed=job.sp.sim_seed,
)
print("Running simulation.")
sim.run_NVT(
n_steps=1e7,
kT=job.sp.kT,
tau_kt=job.doc.tau_kT,
)
sim.save_restart_gsd(job.fn("restart.gsd"))
job.doc.runs += 1
print("Simulation finished.")
@PPSCG.pre(equilibrated)
@PPSCG.post(production_done)
@PPSCG.operation(
directives={"ngpu": 1, "ncpu": 1, "executable": "python -u"},
name="production"
)
def production_run(job):
import unyt
from unyt import Unit
import flowermd
from flowermd.base import Simulation
import hoomd
with job:
print("------------------------------------")
print("JOB ID NUMBER:")
print(job.id)
print("------------------------------------")
print("Restarting and continuing simulation...")
print("Running the production run...")
with open(job.fn("forcefield.pickle"), "rb") as f:
hoomd_ff = pickle.load(f)
gsd_path = job.fn(f"production.gsd")
log_path = job.fn(f"production.txt")
ref_values = get_ref_values(job)
sim = Simulation(
initial_state=job.fn("restart.gsd"),
forcefield=hoomd_ff,
reference_values=ref_values,
dt=job.sp.dt,
gsd_write_freq=int(5e5),
gsd_file_name=gsd_path,
log_write_freq=job.sp.log_write_freq,
log_file_name=log_path,
seed=job.sp.sim_seed,
)
print("Running simulation.")
sim.run_NVT(
n_steps=job.sp.n_prod_steps,
kT=job.sp.kT,
tau_kt=job.doc.tau_kT,
)
sim.save_restart_gsd(job.fn("production-restart.gsd"))
print("Simulation finished.")
@PPSCG.pre(production_done)
@PPSCG.post(sampled)
@PPSCG.operation(
directives={"ngpu": 0, "ncpu": 1, "executable": "python -u"},
name="sample"
)
def sample(job):
import numpy as np
from cmeutils.dynamics import msd_from_gsd
with job:
print("------------------------------------")
print("JOB ID NUMBER:")
print(job.id)
print("------------------------------------")
steps_per_frame = int(5e5)
# Update job doc
ts = job.doc.real_time_step * 1e-15
ts_frame = steps_per_frame * ts
msd = msd_from_gsd(
gsdfile=job.fn("production.gsd"),
start=0,
stop=-1,
atom_types="all",
msd_mode="direct"
)
msd_results = np.copy(msd.msd)
conv_factor = job.doc.ref_length**2
job.doc.msd_units = "nm**2"
msd_results *= conv_factor
time_array = np.arange(0, len(msd.msd), 1) * ts_frame
np.save(file=job.fn(f"msd_time.npy"), arr=time_array)
np.save(file=job.fn(f"msd_data_real_nm_squared.npy"), arr=msd_results)
np.save(file=job.fn(f"msd_data_reduced.npy"), arr=msd.msd)
print("Finished.")
job.doc.sampled = True
if __name__ == "__main__":
PPSCG(environment=Fry).main()