A First Example
We will start by running a simple Molecular Dynamics simulation with two particles interacting via the Lennard-Jones potential. The sample code is given below.
#include <glotzilla++.h> int main(int argc, char ** argv) { MdSimulation my_simulation; my_simulation.AddParticle(new MdPointParticle(0,0,0)); my_simulation.AddParticle(new MdPointParticle(1,1,1)); my_simulation.SetInteraction(new LennardJones); my_simulation.SetForceRoutine(new BruteForce); my_simulation.SetBoundaryConditions(new PeriodicBoundary(10)); my_simulation.SetMoveRoutine(new VerletMove); for(int i=0; i<1000; i++) my_simulation ++; return 0; }
Compile the code using
glotzcc md.c
where md.c is the name of the file where the code resides.
Let's take a look at the code step by step. The first thing that we do is include the Glotzilla header files. This is accomplished with the line
#include <glotzilla++.h>
The compiler now knows about all of the pre-defined Glotzilla classes. The next thing that we do is declare a simulation
MdSimulation my_simulation;
Here, MdSimulation is a class from <glotzilla++.h>. An MD simulation needs a few classes in order to work:
- particles - objects that exist in the simulation
- interactions - defines how particles interact
- force routine - calculates force particles exert on one another
- boundary conditions - the space accessable to the particles
- move routine - defines how the particles move
We define these classes using the following lines:
my_simulation.AddParticle(new MdPointParticle(0,0,0)); my_simulation.AddParticle(new MdPointParticle(1,1,1)); my_simulation.SetInteraction(new LennardJones); my_simulation.SetForceRoutine(new BruteForce); my_simulation.SetBoundaryConditions(new PeriodicBoundary(10)); my_simulation.SetMoveRoutine(new VerletMove);
Here, we declare two point particles, one at <0,0,0> and another at <1, 1, 1>. They interact via the 12-6 Lennard-Jones potential. The force routine calculates forces between all particles, interacting or not (brute force). The boundary is periodic with a period of length 10 in x, y, and z. The equations of motion are integrated using the Verlet integration scheme.
To run the simulation for 1000 timesteps, we write:
for(int i=0; i<1000; i++) my_simulation ++;
Here, the ++ operator in my_simulation integrates the equations of motion forward by 1 timestep. We do this 1000 times in all, and then the code exits.