Minimal Simulation OOP

The sample code in Minimal Simulation provides for an efficient way to create a simple simulation, however the Glotzilla API is much more powerful when used in an object-oriented fashion. This tutorial creates an identical simulation to Minimal Simulation, but it accomplishes this by creating a custom class called LennardJonesSimulation that inherits from the MDSimulation class. This tutorial builds on the topics in Minimal Simulation, so please read this first.

The following code pertains to a single component Lennard-Jones, NVE molecular dynamics simulation.

  • Verified10-4-07; code revision 390
  • Created 5-8-07; code revision 173

Inheriting from MdSimulation

Inheriting from MdSimulation is relatively straight forward. As in the previous Minimal Simulation example, we must first include the header:

#include <glotzilla++.h>

Next we need to create our own custom class that inherits from MdSimulation. Here, our custom class LennardJonesSimulation inherits from class MdSimulation and therefore, contains every public member function and member variable that MdSimulation does:

#include <glotzilla++.h>

class LennardJonesSimulation : public MdSimulation
{
   public:
      LennardJonesSimulation()
      {                                                                 
      }
};

Setting up the simulation

As before, we need to set up a few parameter in the system. In the OOP version we will place these in the constructor of the LennardJonesSimulation class. The constructor will be called everytime we create a new instance of the class LennardJonesSimulation Again, because we have inherited from the MdSimulation class, we can access these methods directly:

#include <glotzilla++.h>

class LennardJonesSimulation : public MdSimulation
{
   public:
      LennardJonesSimulation()
      {                                  
                AddParticle(new MdPointParticle(0,0,0));
                AddParticle(new MdPointParticle(1,1,1));
                SetInteraction(new LennardJones);
                SetForceRoutine(new BruteForce);
                SetBoundaryConditions(new PeriodicBoundary(10));
                SetIntegrationScheme(new VelocityVerletIntegrator);    
      }
};

Instantiating the custom class

In the Minimal Simulation tutorial we created an instance of the MdSimulation class, here we will instead create an instance of the custom LennardJonesSimulation class in our main function:

#include <glotzilla++.h>

class LennardJonesSimulation : public MdSimulation
{
   public:
      LennardJonesSimulation()
      {                                  
                AddParticle(new MdPointParticle(0,0,0));
                AddParticle(new MdPointParticle(1,1,1));
                SetInteraction(new LennardJones);
                SetForceRoutine(new BruteForce);
                SetBoundaryConditions(new PeriodicBoundary(10));
                SetIntegrationScheme(new VelocityVerletIntegrator);                             
      }
};

int main(int argc, char ** argv) 
{
   LennardJonesSimulation my_simulation;
   return 0;
}

Integrating the equations of motion

The equations of motion are integrated in the identical fashion to the Minimal Simulation example:

#include <glotzilla++.h>

class LennardJonesSimulation : public MdSimulation
{
   public:
      LennardJonesSimulation()
      {                                  
                AddParticle(new MdPointParticle(0,0,0));
                AddParticle(new MdPointParticle(1,1,1));
                SetInteraction(new LennardJones);
                SetForceRoutine(new BruteForce);
                SetBoundaryConditions(new PeriodicBoundary(10));
                SetIntegrationScheme(new VelocityVerletIntegrator);                             
      }
};

int main(int argc, char ** argv) 
{
   LennardJonesSimulation my_simulation;

   for(int timesteps=0; timesteps<10000; timesteps++)
      my_simulation ++;

   return 0;
}

Compiling the code

To compile the code you will need to use the "glotzcc" command. The glotzcc command calls the mpic++ compiler and gives it the appropriate library and header search paths. Within your shell, execute the following command:

glotzcc MDMinimalSimulationOOP.cpp

where MDMinimalSimulationOOP.cpp is the name of the file where the code resides.

Problems

Refer to the FAQ? page.

This tutorial builds on the topics in Minimal Simulation.

Attachments