Initialize System Randomly
This tutorial creates a custom class called LennardJonesSimulationRandom that inherits from the MDSimulation class. This code uses a pipe stream to access the "randbox" routine to initialize particles on a simple cubic lattice. Velocities are also randomly assigned using the random number generation class. This tutorial builds on the topics in Minimal Simulation OOP and also those from Initialize System Lattice, so please read these first.
The following code pertains to a single component Lennard-Jones, NVE molecular dynamics simulation.
- Verified10-4-07; code revision 390
- Created 5-12-07; code revision 185
Randbox
We will use an identical technique to those discussed in Initialize System Lattice, however in this case we will create a pipestream that captures the output from the "randbox" application. "randbox" randomly places particles within a set domain, which a specific spacing between points.
"randbox" has the following usage (which can be access via "randbox -h" in your terminal):
usage: -h --help Prints this help -n --numpart Number of particles in system [number], (required) -m --min Minumum separation between points, (default is 0.0) -d --dim Integer number of dimensions (default is 3) -r --region xmin,xmax,ymin,ymax,zmin,zmax (no spaces, required) -s --seed uint,uint,uint (no spaces) -b --boundary p for periodic, h for hard (ex: pph = periodic in xy, hard in z) -i --maxiter maximum number of attempts (default is 1e7)
Only one line of code needs to be modified from the Initialize System Lattice code, namely the pipe_stream call.
ipstream particle_pipe("randbox -n=200 -r=-4,4,-4,4,-4,4 -m=1.1 -i=1000000");Completed Code
#include <glotzilla++.h> class LennardJonesSimulationRandom : public MdSimulation { public: LennardJonesSimulationRandom() { InitializeParticles(); SetInteraction(new LennardJones); SetForceRoutine(new BruteForce); SetBoundaryConditions(new PeriodicBoundary(10)); SetIntegrationScheme(new VelocityVerletIntegrator); } void InitializeParticles() { ipstream particle_pipe("randbox -n=200 -r=-4,4,-4,4,-4,4 -m=1.1 -i=1000000"); StdLibErand48 *mRandomNumberGenerator = new StdLibErand48(); gvector x; while(particle_pipe >> x) { MdPointParticle *p = new MdPointParticle; p -> SetPosition(x); gvector v = mRandomNumberGenerator -> ComputeRandomVecT(-sqrt(3.0), sqrt(3.0)); p -> SetVelocity(v); AddParticle(p); } particle_pipe.close(); } }; int main(int argc, char ** argv) { LennardJonesSimulationRandom my_simulation; opstream vis_pipe("vis3d"); for(int timesteps=0; timesteps<10000; timesteps++) { my_simulation ++; if(timesteps%5 == 0) my_simulation.PrintVisual(vis_pipe); } return 0; }
Attachments
-
MDSimulationRandom.cpp
(1.5 kB) - added by cri
15 months ago.
MD simulation initalized randomly using randbox