Introduction

This page describes a C++ programming project for the physics 373 class. The aim of the project is to teach the students about more advanced C++ ideas, such as: class design, inheritance, and polymorphism (virtual functions).

This project is worth 15% of the total grade for the course. The deadline for submitting the completed project is

June 5 .
We strongly advise that students do not leave the completion of this project to the last moment.

Project description

We will look at the use of C++ to write part of a molecular dynamics program. Molecular dynamics is a technique for studying materials. Newton's laws of motion are solved for a collection of particles. However, we will only look at two simple aspects of a molecular dynamics program: the initialization of the position and velocity, and the calculation of the potential and kinetic energies.

Input files

In the directory
~p373/public_html/assign/project
there are the following files: The file molecular_dynamics.C intializes a typical molecular dynamics run. The program uses three classes: The student'ss task is to create the particle and particle_argon classes so that the programs runs correctly.

When the project is completed, the program can be compiled and run using

g++ molecular_dynamics.C  nr_gasdev.C
a.out < input 
The output from the student's program should agree with the file output .

The program molecular_dynamics.C should not be modified.

Specifics

Constructing particle.h

The solution is now available.

This file contains the particle class. This stores the coordinates and velocities of a given number of particles. This class calculates the kinetic energy of the particles, however it does not calculate the potential energy. We suggest the particle's position and velocities are stored as

    float mass ; 
#define MAX_PARTICLES 5
    float x[MAX_PARTICLES ] ,y[MAX_PARTICLES ] ,z[MAX_PARTICLES ]  ;
    float vx[MAX_PARTICLES ]  , vy[MAX_PARTICLES ]  , vz[MAX_PARTICLES ]  ;
The above data structures are not a very flexible way of storing the particle coordinates. However it makes the class implementation simple.

Here is the list of the required methods (class functions)

Constructing the file particle_argon.h

This class is now provided for the students to look at.

This class inherits the particle class. It provides a specific implementation of the potential energy. These particle interact via a Lenoard-Jones potential.

 V(r_ij) = epsilon * (  (sigma/r_ij) **12  - (sigma/r_ij) **6 )
Where r_ij is the distance between particle i and particle j.

This class only has sigma and epsilon as internal data. The constructor, called particle_argon, takes three arguments: the mass, the value of sigma, and the value of epsilon

The second class method is called calc_potential_energy() This function loops over all the particles and calculates the total potential energy between the particles. The interaction of a particle with itself is ignored. The result is printed to the screen.

Constructing the file particle_hardcore.h

The solution is now available.

This class inherits the particle class. It provides a specific implementation of the potential energy. These particle interact via a box like potential.

V(r_ij) = -V0    if  r_ij < r_0
        =  0         r_ij >= r_0
Where r_ij is the distance between particle i and particle j.

This class only has r0 and v0 as internal data. The constructor, called particle_hardcore, takes three arguments: the mass, the value of r0, and the value of v0.

The second class method is called calc_potential_energy() This function loops over all the particles and calculates the total potential energy between the particles. The interaction of a particle with itself is ignored. The result is printed to the screen.

This class is very similar to the particle_argon class.

Hints and pointers to useful information

The C++ FAQ answers the question: what is a class? There is also a nice discussion about classes in the tutorial by tlardaro.

There is a page that shows how the molecular dynamics program is similar to a vector class program. I strongly recommend that the students work through this example.

I have made some notes on inheritance and virtual functions Students should read this document. Students can also look at the examples I showed the class on using virtual functions.

The example of a vector class may be profitably be consulted.


Craig McNeile <mcneile@mail.physics.utah.edu>
Last modified: Wed Jun 10 11:32:08 1998