Visualizing the Simulation

Now let's make things more interesting. One of the most important features of Glotzilla is the ability to link many independent programs together in a trivial but powerful way. The following code runs the same code from step one while visualizing the particles in OpenGL. To do this the program calls vis3d, an independent glotzilla executable, via a pipestream, a custom datatype.

#include <glotzilla++.h>

class LennardJonesSimulation : public MdSimulation
{
   public:
      LennardJonesSimulation()
      {                                  
         AddParticle(new MdPointParticle(0,0,0));
         AddParticle(new MdPointParticle(1,0,0));
         SetInteraction(new LennardJones);
         SetForceRoutine(new BruteForce);
         SetBoundaryConditions(new PeriodicBoundary(11));
         SetMoveRoutine(new VerletMove);                                
      }
};

int main(int argc, char ** argv) 
{
   LennardJonesSimulation my_simulation;
   opstream my_pipe("vis3d");

   for(int timesteps=0; timesteps<10000; timesteps++)
   {
      my_simulation +=5;
      my_simulation.PrintVisual(my_pipe);
   }

   my_pipe.close();
   return 0;
}

Compile the code and run it. You should see the two particles repel one another and shoot off in opposite directions over and over again. If you replace the second particle declaration with, say

intial_conditions -> AddParticle(new MdPointParticle(1.3,0,0));

the particles should attract one another.

Let's consider how the visualization program works. We first declare an output pipe stream called my_pipe.

opstream my_pipe("vis3d");

This command says that anything that we write to my_pipe is sent to vis3d. Note that we can start vis3d with command line options just as if we were using it from the unix terminal.

opstream my_pipe("vis3d -s=400x300 -p=100,100");

This command opens vis3d and sets with window size to 400x300 and the window position to 100,100 on the screen. We write data to vis3d using this function call:

my_simulation.PrintVisual(my_pipe);

This sends data in visual format through the pipe to vis3d. To see what the data looks like, simply replace my_pipe with std::cout to print the data to the screen:

my_simulation.PrintVisual(std::cout);

The result should be lots of text data that describes positions and shapes.

Was this clear?

Attachments