Adding Analysis Tools
This tutorial builds on Turorial 1. Obviously, you can add custom analysis tools by simply declaring a class that performs your data analysis. However, the Glotzilla philosophy is to create analysis codes that work as independent entities use them via pipes. This 1) creates more re-usable analysis codes, 2) decreases code size, and hence the possibility for errors and 3) often increases efficiency, since the the analysis code runs as a separate process and hence can run on a different cpu. Lets say we want to calculate the mean-squared displacement in our simulation.
#include <glotzilla++.h> class LennardJonesSystem : public MdIntegrator { public: LennardJonesSystem() { mInitialConditions = new MdInitializer; InitializeParticles(); mInitialConditions -> SetInteraction(new LennardJones); mInitialConditions -> SetForceRoutine(new VerletForce); mInitialConditions -> SetBoundaryConditions(new PeriodicBoundary(11)); //begin changes MoveRoutine *move = new VerletMove; move -> SetTimestep(0.005); mInitialConditions -> SetMoveRoutine(move); //end changes InitializeSystem(mInitialConditions); } private: void InitializeParticles() { ipstream particle_pipe("randbox -n=512 -r=-5,5,-5,5,-5,5 -m=1.0 -i=1000000"); vec_t x; while(particle_pipe >> x) { MdPointParticle *p = new MdPointParticle; p -> SetPosition(x); mInitialConditions -> AddParticle(p); } particle_pipe.close(); } MdInitializer *mInitialConditions; }; int main(int argc, char ** argv) { LennardJonesSystem my_simulation; opstream vis3d_pipe("vis3d -n=\"Lennard Jones System\""); opstream msd_pipe("msqdisp -n=512 -p=11 -t=0.5 > msqdispLJ.txt"); for(int timesteps=0; timesteps<100000; timesteps++) { if(timesteps % 100 == 0 && timesteps > 1000) my_simulation.PrintPositions(msd_pipe); if(timesteps % 5 == 0) { //the following line prints the timesteps in the visualizer vis3d_pipe << "T 10 10 \"timesteps : " << timesteps << "\" -\n"; my_simulation.PrintVisual(vis3d_pipe); } my_simulation ++; } msd_pipe.close(); vis3d_pipe.close(); return 0; }
We simply inserted the line
opstream msd_pipe("msqdisp -n=512 -p=11 -t=0.5 > msqdispLJ.txt");
This creates a pipeline to the Glotzilla "msqdisp" module. The > operator redirects the output from the module (i.e., the mean-squared displacement) to the file msqdispLJ.txt. The command line options are -n=[number of particles], -p=[length of period, or box], -t=[time interval]. Note that -t=0.5 since the timestep was set to 0.005 above and we print the coordinates to msqdisp every 100 timesteps using
if(timesteps % 100 == 0 && timesteps > 1000)
my_simulation.PrintPositions(msd_pipe);