Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions phantomwalk/lib/create_system_dpd.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, but two small nitpicks: I don't think we want to calculate average RDFs in this case, just the last frame, and reading from a GSD that has been written out adds some complexity relative to calculating RDFs as the simulation proceeds (which could be used in our stop criteria)

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import hoomd
import time

from dpd_utils import initialize_snapshot_rand_walk,check_bond_length_equilibration,check_inter_particle_distance,add_hoomd_writers,check_pair_energy,simulation_energy_end

from phantomwalk.lib.dpd_utils import (
initialize_snapshot_rand_walk,
check_bond_length_equilibration,
check_inter_particle_distance,
add_hoomd_writers,
simulation_energy_end,
generate_rdf
)

def create_polymer_system_dpd(
num_pol,
Expand All @@ -28,7 +34,15 @@ def create_polymer_system_dpd(
gsd_file_name='trajectory.gsd',
gsd_write_freq=10,
log_file_name='log.txt',
log_write_freq=10
log_write_freq=10,

# rdf parameters
rdf_start_idx=-1,
rdf_end_idx=None,
rdf_file_name='rdf.txt',
rdf_bins=300,
rdf_r_max=3.0,
rdf_r_min=0.0
):

'''
Expand Down Expand Up @@ -79,6 +93,22 @@ def create_polymer_system_dpd(
the file that the .txt log file will be saved to
log_write_freq : int, default 10
Period to write simulation data to the log file.
rdf_start_idx : int, default -1
Which frame to use for the start of the RDF average. Supplying None or 0
will use the first frame. Supports negative indices.
rdf_end_idx : int, default None
Which frame to use for the last frame of the RDF average. Supplying None
will use the last frame. Supports negative indices.
rdf_bins : int, default 300
How many bins to use for the RDF histogram
rdf_r_max : float, default 3.0
Maximum interparticle distance to include in the RDF calculation. If
this value is greater than half the box length, then that will be used
instead.
rdf_r_min : float, default 0.0
Minimum interparticle distance to include in the RDF calculation.
rdf_file_name : str, default 'rdf.txt'
The file to output the RDF data into.

-------
Returns
Expand Down Expand Up @@ -146,6 +176,15 @@ def create_polymer_system_dpd(
check_time = time.perf_counter()
if (check_time-start_time) > loop_timeout:
print("Simulation timed out")
generate_rdf(
start_idx=rdf_start_idx,
end_idx=rdf_end_idx,
bins=rdf_bins,
r_max=rdf_r_max,
r_min=rdf_r_min,
gsd_file_name=gsd_file_name,
output_file_name=rdf_file_name
)
return snap, 0
simulation.run(sim_steps_incr)
for writer in simulation.operations.writers:
Expand All @@ -157,6 +196,15 @@ def create_polymer_system_dpd(
check_time = time.perf_counter()
if (check_time-start_time) > loop_timeout:
print("Simulation timed out")
generate_rdf(
start_idx=rdf_start_idx,
end_idx=rdf_end_idx,
bins=rdf_bins,
r_max=rdf_r_max,
r_min=rdf_r_min,
gsd_file_name=gsd_file_name,
output_file_name=rdf_file_name
)
return snap,0
simulation.run(sim_steps_incr)
for writer in simulation.operations.writers:
Expand All @@ -167,4 +215,13 @@ def create_polymer_system_dpd(
end_time = time.perf_counter()
total_time = end_time - start_time
print("Total build and simulation time:", end_time - start_time)
generate_rdf(
start_idx=rdf_start_idx,
end_idx=rdf_end_idx,
bins=rdf_bins,
r_max=rdf_r_max,
r_min=rdf_r_min,
gsd_file_name=gsd_file_name,
output_file_name=rdf_file_name
)
return snap, total_time
46 changes: 46 additions & 0 deletions phantomwalk/lib/dpd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hoomd
import time
from cmeutils.sampling import is_equilibrated
import csv

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need csv?


def initialize_snapshot_rand_walk(num_pol, num_mon, density, bond_length=1.0, seed=1234):
'''
Expand Down Expand Up @@ -225,3 +226,48 @@ def simulation_energy_end(A,r,r_cut,num_pol,num_mon,density,energy_idx=-1):
else:
return False

def generate_rdf(
start_idx = -1,
end_idx = None,
bins=300,
r_max=3.0,
r_min=0.0,
gsd_file_name="trajectory.gsd",
output_file_name="rdf.txt"
):
'''
Generate a radial distribution function for the provided GSD file and save
the data to the provided output file.

----------
Parameters
----------
start_idx : int, default -1
Which frame to use for the start of the RDF average. Supplying None or 0
will use the first frame. Supports negative indices.
end_idx : int, default None
Which frame to use for the last frame of the RDF average. Supplying None
will use the last frame. Supports negative indices.
bins : int, default 300
How many bins to use for the histogram
r_max : float, default 3.0
Maximum interparticle distance to include in the calculation. If this
value is greater than half the box length, then that will be used
instead.
r_min : float, default 0.0
Minimum interparticle distance to include in the calculation.
gsd_file_name : str, default 'trajectory.gsd'
The file to read the trajectory data used for calculating the RDF.
output_file_name : str, default 'rdf.txt'
The file to output the RDF data into.
'''
rdf = freud.density.RDF(bins=bins, r_max=r_max, r_min=r_min)
traj = gsd.hoomd.open(gsd_file_name, 'r')
for frame in traj[start_idx:end_idx]:
rdf.compute(system=frame, reset=False)

np.savetxt(
fname=output_file_name,
X=np.column_stack((rdf.bin_centers, rdf.rdf)),
header="r rdf"
)