User Tools

Site Tools


simulation:restartable_simulation

Restartable Simulations

Molecular dynamics simulations might run for a very long time, depending on the system size, number of particles, and time steps. You might want to be able to run from a snapshot, either to continue a disrupted or crashed simulation, or to simply split different stages of the simulations into different scripts.

For various reasons, a simulation should save not only a trajectory overall, but also a 'restart' snapshot, from which the simulation can be continued or which can be used as starting point for another simulation, e.g. a equilibration run before production runs. These 'restart' snapshots should contain one configuration (one time step) with all needed information. This also allows to delete equilibration trajectories which are not needed anymore to save disk space and keep the 'restart' files. If a simulation crashes or is disrupted, these 'restart' files can be used to continue without loss of long stretches of the simulation, if the 'restart' snapshots are written in regular time intervals.

Example hoomd-blue restartable script

import numpy as np
import os
import hoomd
from hoomd import data
from hoomd import md
 
gsd_file = './out/trajectory.gsd'
restart_file = './out/restart.gsd'
 
#-----------------------------------------------------------------------
#                             Setup system
#-----------------------------------------------------------------------
 
hoomd.context.initialize()
sim = hoomd.context.SimulationContext()
 
if not os.path.isfile(restart_file):  # restart file doesn't exist - initalize
    snapshot = hoomd.data.make_snapshot( N=2, particle_types=['A'], box=data.boxdim(L=10))
 
    snapshot.particles.position[0]=(0,0,4)
    snapshot.particles.position[1]=(0,0,-4)
 
    snapshot.particles.velocity[:]=np.random.normal(0,np.sqrt(1.0),size=(2,3))
    snapshot.particles.velocity[:] -= np.average(snapshot.particles.velocity,axis=0)
    system = hoomd.init.read_snapshot(snapshot)
else: # restart file exists - read it
    system = hoomd.init.read_gsd(filename=restart_file,frame=-1)
 
#-----------------------------------------------------------------------
#                        Particle interactions
#-----------------------------------------------------------------------
 
nl = hoomd.md.nlist.cell()
lj = hoomd.md.pair.lj(r_cut=3.0, nlist=nl)
lj.set_params(mode='shift')
lj.pair_coeff.set('A', 'A', epsilon=1.0,  sigma=1.0, r_on=2.5,  r_cut=3.0)
 
#-----------------------------------------------------------------------
#                               Output
#-----------------------------------------------------------------------
 
if not os.path.exists("./out/"):
    os.makedirs("./out/")
all = hoomd.group.all()
gsd_dump = hoomd.dump.gsd(filename=gsd_file, period=1e2, group=all)
gsd_restart_dump = hoomd.dump.gsd(filename=restart_file, truncate=True,\
 period=1e5, dynamic=['property','momentum'], group=all)
 
#-----------------------------------------------------------------------
#                                Run
#-----------------------------------------------------------------------
 
hoomd.md.integrate.mode_standard(dt=0.005)
langevin=hoomd.md.integrate.langevin(group=all,kT=1.0,dscale=0.1,seed=567)
 
 
try:
    hoomd.run_upto(1e7)
except WalltimeLimitReached:
    pass
 
gsd_restart_dump.write_restart()

If you want to keep it simpler, the “try-except” at the end is not strictly neccessary, one can simply also do hoomd.run(1e7). One also doesn't need a seperate restart.gsd file, in principle the last frame of the trajectory can be used for restarting, too.

Future reading

simulation/restartable_simulation.txt · Last modified: 2022/11/03 20:51 by statt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki