forked from SSAGESLabs/PySAGES
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement umbrella integration (SSAGESLabs#68)
Co-authored-by: Pablo Zubieta <[email protected]>
- Loading branch information
1 parent
f8d5794
commit 63f8e88
Showing
8 changed files
with
427 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import numpy as np | ||
import gsd | ||
import gsd.hoomd | ||
|
||
class System: | ||
def __init__(self): | ||
self.L = 5 | ||
self.N = 200 | ||
|
||
|
||
def post_process_pos(snapshot): | ||
snapshot.particles.image = np.rint(snapshot.particles.position/snapshot.configuration.box[:3]) | ||
snapshot.particles.position -= snapshot.particles.image * snapshot.configuration.box[:3] | ||
return snapshot | ||
|
||
def get_snap(system): | ||
snapshot = gsd.hoomd.Snapshot() | ||
snapshot.configuration.box = [system.L, system.L, system.L, 0, 0, 0] | ||
|
||
snapshot.particles.N = system.N | ||
|
||
snapshot.particles.types = ["A", "B"] | ||
snapshot.particles.position = np.zeros((snapshot.particles.N, 3)) | ||
snapshot.particles.velocity = np.random.standard_normal((snapshot.particles.N, 3)) | ||
snapshot.particles.image = np.zeros((snapshot.particles.N, 3), dtype=np.int) | ||
snapshot.particles.typeid = np.zeros(snapshot.particles.N, dtype=np.int) | ||
snapshot.particles.typeid += 1 | ||
snapshot.particles.typeid[0] = 0 | ||
|
||
rng = np.random.default_rng() | ||
for particle in range(system.N): | ||
snapshot.particles.position[particle, 0] = rng.random() * system.L - system.L/2 | ||
snapshot.particles.position[particle, 1] = rng.random() * system.L - system.L/2 | ||
snapshot.particles.position[particle, 2] = rng.random() * system.L - system.L/2 | ||
|
||
snapshot.particles.position[0 , 0] = -np.pi | ||
snapshot.particles.position[0 , 1] = -np.pi | ||
snapshot.particles.position[0 , 1] = -np.pi | ||
|
||
return snapshot | ||
|
||
|
||
def main(argv): | ||
if len(argv) != 1: | ||
print("Usage: ./gen_gsd.py") | ||
|
||
system = System() | ||
snap = get_snap(system) | ||
snap = post_process_pos(snap) | ||
snap.particles.validate() | ||
with gsd.hoomd.open("start.gsd", "wb") as f: | ||
f.append(snap) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import argparse | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
import hoomd | ||
import hoomd.md as md | ||
import hoomd.dlext | ||
|
||
import pysages | ||
from pysages.collective_variables import Component | ||
from pysages.methods import UmbrellaIntegration | ||
|
||
|
||
param1 = {"A": 0.5, "w": 0.2, "p": 2} | ||
|
||
def generate_context(**kwargs): | ||
hoomd.context.initialize("") | ||
context = hoomd.context.SimulationContext() | ||
with context: | ||
print("Operating replica {0}".format(kwargs.get("replica_num"))) | ||
system = hoomd.init.read_gsd("start.gsd") | ||
|
||
hoomd.md.integrate.nve(group=hoomd.group.all()) | ||
hoomd.md.integrate.mode_standard(dt=0.01) | ||
|
||
nl = hoomd.md.nlist.cell() | ||
dpd = hoomd.md.pair.dpd(r_cut=1, nlist=nl, seed=42, kT=1.) | ||
dpd.pair_coeff.set("A", "A", A=5., gamma=1.0) | ||
dpd.pair_coeff.set("A", "B", A=5., gamma=1.0) | ||
dpd.pair_coeff.set("B", "B", A=5., gamma=1.0) | ||
|
||
periodic = hoomd.md.external.periodic() | ||
periodic.force_coeff.set('A', A=param1["A"], i=0, w=param1["w"], p=param1["p"]) | ||
periodic.force_coeff.set('B', A=0.0, i=0, w=0.02, p=1) | ||
return context | ||
|
||
|
||
def plot_hist(result, bins=50): | ||
fig, ax = plt.subplots(2, 2) | ||
|
||
# ax.set_xlabel("CV") | ||
# ax.set_ylabel("p(CV)") | ||
|
||
counter = 0 | ||
hist_per = len(result["center"])//4+1 | ||
for x in range(2): | ||
for y in range(2): | ||
for i in range(hist_per): | ||
if counter+i < len(result["center"]): | ||
center = np.asarray(result["center"][counter+i]) | ||
histo, edges = result["histogram"][counter+i].get_histograms(bins=bins) | ||
edges = np.asarray(edges)[0] | ||
edges = (edges[1:] + edges[:-1]) / 2 | ||
ax[x,y].plot(edges, histo, label="center {0}".format(center)) | ||
ax[x,y].legend(loc="best", fontsize="xx-small") | ||
ax[x,y].set_yscale("log") | ||
counter += hist_per | ||
while counter < len(result["center"]): | ||
center = np.asarray(result["center"][counter]) | ||
histo, edges = result["histogram"][counter].get_histograms(bins=bins) | ||
edges = np.asarray(edges)[0] | ||
edges = (edges[1:] + edges[:-1]) / 2 | ||
ax[1,1].plot(edges, histo, label="center {0}".format(center)) | ||
counter += 1 | ||
|
||
fig.savefig("hist.pdf") | ||
|
||
def external_field(r, A, p, w): | ||
return A*np.tanh(1/(2*np.pi*p*w)*np.cos(p*r)) | ||
|
||
def plot_energy(result): | ||
fig, ax = plt.subplots() | ||
|
||
ax.set_xlabel("CV") | ||
ax.set_ylabel("Free energy $[\epsilon]$") | ||
center = np.asarray(result["center"]) | ||
A = np.asarray(result["A"]) | ||
offset = np.min(A) | ||
ax.plot(center, A-offset, color="teal") | ||
|
||
x = np.linspace(-3, 3, 50) | ||
data = external_field(x, **param1) | ||
offset = np.min(data) | ||
ax.plot(x, data-offset, label="test") | ||
|
||
fig.savefig("energy.pdf") | ||
|
||
|
||
def get_args(argv): | ||
parser = argparse.ArgumentParser(description="Example script to run umbrella integration") | ||
parser.add_argument("--k-spring", "-k", type=float, default=50., help="spring constant for each replica") | ||
parser.add_argument("--N-replica", "-N", type=int, default=25, help="Number of replica along the path") | ||
parser.add_argument("--start-path", "-s", type=float, default=-1.5, help="Start point of the path") | ||
parser.add_argument("--end-path", "-e", type=float, default=1.5, help="Start point of the path") | ||
parser.add_argument("--time-steps", "-t", type=int, default=int(1e5), help="Number of simulation steps for each replica") | ||
parser.add_argument("--log-period", "-l", type=int, default=int(50), help="Frequency of logging the CVS for histogram") | ||
parser.add_argument("--discard-equi", "-d", type=int, default=int(1e4), help="Discard timesteps before logging for equilibration") | ||
args = parser.parse_args(argv) | ||
return args | ||
|
||
def main(argv): | ||
|
||
args = get_args(argv) | ||
|
||
cvs = [Component([0], 0),] | ||
method = UmbrellaIntegration(cvs) | ||
|
||
centers = list(np.linspace(args.start_path, args.end_path, args.N_replica)) | ||
result = method.run(generate_context, args.time_steps, centers, args.k_spring, args.log_period, args.discard_equi) | ||
|
||
plot_energy(result) | ||
plot_hist(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.